2 Copyright (C) 2013 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <boost/scoped_ptr.hpp>
25 #include <glibmm/timer.h>
26 #include <glibmm/spawn.h>
28 #include "pbd/error.h"
30 #include "jack/jack.h"
31 #include "jack/thread.h"
33 #include "ardour/audioengine.h"
34 #include "ardour/session.h"
35 #include "ardour/types.h"
37 #include "jack_audiobackend.h"
38 #include "jack_connection.h"
39 #include "jack_portengine.h"
40 #include "jack_utils.h"
44 using namespace ARDOUR;
51 #define GET_PRIVATE_JACK_POINTER(localvar) jack_client_t* localvar = _jack_connection->jack(); if (!(localvar)) { return; }
52 #define GET_PRIVATE_JACK_POINTER_RET(localvar,r) jack_client_t* localvar = _jack_connection->jack(); if (!(localvar)) { return r; }
54 JACKAudioBackend::JACKAudioBackend (AudioEngine& e, boost::shared_ptr<JackConnection> jc)
56 , _jack_connection (jc)
58 , _freewheeling (false)
59 , _target_sample_rate (48000)
60 , _target_buffer_size (1024)
61 , _target_sample_format (FormatFloat)
62 , _target_interleaved (false)
63 , _target_input_channels (-1)
64 , _target_output_channels (-1)
65 , _target_systemic_input_latency (0)
66 , _target_systemic_output_latency (0)
67 , _current_sample_rate (0)
68 , _current_buffer_size (0)
70 _jack_connection->Disconnected.connect_same_thread (disconnect_connection, boost::bind (&JACKAudioBackend::disconnected, this, _1));
73 JACKAudioBackend::~JACKAudioBackend()
78 JACKAudioBackend::name() const
84 JACKAudioBackend::private_handle() const
86 return _jack_connection->jack();
90 JACKAudioBackend::connected() const
92 return (private_handle() != 0);
96 JACKAudioBackend::is_realtime () const
98 GET_PRIVATE_JACK_POINTER_RET (_priv_jack,false);
99 return jack_is_realtime (_priv_jack);
103 JACKAudioBackend::requires_driver_selection() const
109 JACKAudioBackend::enumerate_drivers () const
111 vector<string> currently_available;
112 get_jack_audio_driver_names (currently_available);
113 return currently_available;
117 JACKAudioBackend::set_driver (const std::string& name)
119 _target_driver = name;
123 vector<AudioBackend::DeviceStatus>
124 JACKAudioBackend::enumerate_devices () const
126 vector<string> currently_available = get_jack_device_names_for_audio_driver (_target_driver);
127 vector<DeviceStatus> statuses;
129 if (all_devices.find (_target_driver) == all_devices.end()) {
130 all_devices.insert (make_pair (_target_driver, std::set<string>()));
133 /* store every device we've found, by driver name.
135 * This is so we do not confuse ALSA, FFADO, netjack etc. devices
139 DeviceList& all (all_devices[_target_driver]);
141 for (vector<string>::const_iterator d = currently_available.begin(); d != currently_available.end(); ++d) {
145 for (DeviceList::const_iterator d = all.begin(); d != all.end(); ++d) {
146 if (find (currently_available.begin(), currently_available.end(), *d) == currently_available.end()) {
147 statuses.push_back (DeviceStatus (*d, false));
149 statuses.push_back (DeviceStatus (*d, false));
157 JACKAudioBackend::available_sample_rates (const string& /*device*/) const
162 f.push_back (sample_rate());
166 /* if JACK is not already running, just list a bunch of reasonable
167 values and let the future sort it all out.
170 f.push_back (8000.0);
171 f.push_back (16000.0);
172 f.push_back (24000.0);
173 f.push_back (32000.0);
174 f.push_back (44100.0);
175 f.push_back (48000.0);
176 f.push_back (88200.0);
177 f.push_back (96000.0);
178 f.push_back (192000.0);
179 f.push_back (384000.0);
185 JACKAudioBackend::available_buffer_sizes (const string& /*device*/) const
190 s.push_back (buffer_size());
210 JACKAudioBackend::available_input_channel_count (const string& /*device*/) const
216 JACKAudioBackend::available_output_channel_count (const string& /*device*/) const
221 /* -- parameter setting -- */
224 JACKAudioBackend::set_device_name (const string& dev)
227 /* need to stop and restart JACK for this to work, at present */
231 _target_device = dev;
236 JACKAudioBackend::set_sample_rate (float sr)
239 _target_sample_rate = sr;
243 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
245 if (sr == jack_get_sample_rate (_priv_jack)) {
253 JACKAudioBackend::set_buffer_size (uint32_t nframes)
256 _target_buffer_size = nframes;
260 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
262 if (nframes == jack_get_buffer_size (_priv_jack)) {
266 return jack_set_buffer_size (_priv_jack, nframes);
270 JACKAudioBackend::set_sample_format (SampleFormat sf)
272 /* as far as JACK clients are concerned, the hardware is always
273 * floating point format.
275 if (sf == FormatFloat) {
282 JACKAudioBackend::set_interleaved (bool yn)
284 /* as far as JACK clients are concerned, the hardware is always
294 JACKAudioBackend::set_input_channels (uint32_t cnt)
300 _target_input_channels = cnt;
306 JACKAudioBackend::set_output_channels (uint32_t cnt)
312 _target_output_channels = cnt;
318 JACKAudioBackend::set_systemic_input_latency (uint32_t l)
324 _target_systemic_input_latency = l;
330 JACKAudioBackend::set_systemic_output_latency (uint32_t l)
336 _target_systemic_output_latency = l;
341 /* --- Parameter retrieval --- */
344 JACKAudioBackend::device_name () const
350 return _target_device;
354 JACKAudioBackend::sample_rate () const
357 return _current_sample_rate;
359 return _target_sample_rate;
363 JACKAudioBackend::buffer_size () const
366 return _current_buffer_size;
368 return _target_buffer_size;
372 JACKAudioBackend::sample_format () const
378 JACKAudioBackend::interleaved () const
384 JACKAudioBackend::input_channels () const
387 return n_physical (JackPortIsInput).n_audio();
389 return _target_input_channels;
393 JACKAudioBackend::output_channels () const
396 return n_physical (JackPortIsOutput).n_audio();
398 return _target_output_channels;
402 JACKAudioBackend::systemic_input_latency () const
404 return _target_systemic_output_latency;
408 JACKAudioBackend::systemic_output_latency () const
410 return _target_systemic_output_latency;
414 JACKAudioBackend::raw_buffer_size(DataType t)
416 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
417 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
421 JACKAudioBackend::setup_jack_startup_command ()
423 /* first we map the parameters that have been set onto a
424 * JackCommandLineOptions object.
427 JackCommandLineOptions options;
429 get_jack_default_server_path (options.server_path);
430 options.driver = _target_driver;
431 options.samplerate = _target_sample_rate;
432 options.period_size = _target_buffer_size;
433 options.num_periods = 2;
434 options.input_device = _target_device;
435 options.output_device = _target_device;
436 options.input_latency = _target_systemic_input_latency;
437 options.output_latency = _target_systemic_output_latency;
438 options.input_channels = _target_input_channels;
439 options.output_channels = _target_output_channels;
440 if (_target_sample_format == FormatInt16) {
441 options.force16_bit = _target_sample_format;
443 options.realtime = true;
444 options.ports_max = 2048;
446 /* this must always be true for any server instance we start ourselves
449 options.temporary = true;
453 if (!get_jack_command_line_string (options, cmdline)) {
458 std::cerr << "JACK command line will be: " << cmdline << std::endl;
460 write_jack_config_file (get_jack_server_user_config_file_path(), cmdline);
463 /* ---- BASIC STATE CONTROL API: start/stop/pause/freewheel --- */
466 JACKAudioBackend::start ()
470 if (!_jack_connection->server_running()) {
471 setup_jack_startup_command ();
474 if (_jack_connection->open ()) {
479 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
481 /* get the buffer size and sample rates established */
483 jack_sample_rate_callback (jack_get_sample_rate (_priv_jack));
484 jack_bufsize_callback (jack_get_buffer_size (_priv_jack));
486 /* Now that we have buffer size and sample rate established, the engine
487 can go ahead and do its stuff
490 engine.reestablish_ports ();
492 if (!jack_port_type_get_buffer_size) {
493 warning << _("This version of JACK is old - you should upgrade to a newer version that supports jack_port_type_get_buffer_size()") << endmsg;
496 set_jack_callbacks ();
498 if (jack_activate (_priv_jack) == 0) {
501 // error << _("cannot activate JACK client") << endmsg;
504 engine.reconnect_ports ();
510 JACKAudioBackend::stop ()
512 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
514 _jack_connection->close ();
516 _current_buffer_size = 0;
517 _current_sample_rate = 0;
519 _raw_buffer_sizes.clear();
525 JACKAudioBackend::pause ()
527 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
530 jack_deactivate (_priv_jack);
537 JACKAudioBackend::freewheel (bool onoff)
539 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
541 if (onoff == _freewheeling) {
542 /* already doing what has been asked for */
547 if (jack_set_freewheel (_priv_jack, onoff) == 0) {
548 _freewheeling = true;
555 /* --- TRANSPORT STATE MANAGEMENT --- */
558 JACKAudioBackend::transport_stop ()
560 GET_PRIVATE_JACK_POINTER (_priv_jack);
561 jack_transport_stop (_priv_jack);
565 JACKAudioBackend::transport_start ()
567 GET_PRIVATE_JACK_POINTER (_priv_jack);
568 jack_transport_start (_priv_jack);
572 JACKAudioBackend::transport_locate (framepos_t where)
574 GET_PRIVATE_JACK_POINTER (_priv_jack);
575 jack_transport_locate (_priv_jack, where);
579 JACKAudioBackend::transport_frame () const
581 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
582 return jack_get_current_transport_frame (_priv_jack);
586 JACKAudioBackend::transport_state () const
588 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, ((TransportState) JackTransportStopped));
590 return (TransportState) jack_transport_query (_priv_jack, &pos);
594 JACKAudioBackend::set_time_master (bool yn)
596 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
598 return jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
600 return jack_release_timebase (_priv_jack);
607 JACKAudioBackend::get_sync_offset (pframes_t& offset) const
610 #ifdef HAVE_JACK_VIDEO_SUPPORT
612 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
617 (void) jack_transport_query (_priv_jack, &pos);
619 if (pos.valid & JackVideoFrameOffset) {
620 offset = pos.video_offset;
633 JACKAudioBackend::sample_time ()
635 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
636 return jack_frame_time (_priv_jack);
640 JACKAudioBackend::sample_time_at_cycle_start ()
642 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
643 return jack_last_frame_time (_priv_jack);
647 JACKAudioBackend::samples_since_cycle_start ()
649 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
650 return jack_frames_since_cycle_start (_priv_jack);
656 ardour_jack_error (const char* msg)
658 error << "JACK: " << msg << endmsg;
662 JACKAudioBackend::set_jack_callbacks ()
664 GET_PRIVATE_JACK_POINTER (_priv_jack);
666 jack_set_thread_init_callback (_priv_jack, AudioEngine::thread_init_callback, 0);
668 jack_set_process_thread (_priv_jack, _process_thread, this);
669 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
670 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
671 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
672 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
673 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
675 #ifdef HAVE_JACK_SESSION
676 if( jack_set_session_callback)
677 jack_set_session_callback (_priv_jack, _session_callback, this);
680 if (jack_set_latency_callback) {
681 jack_set_latency_callback (_priv_jack, _latency_callback, this);
684 jack_set_error_function (ardour_jack_error);
688 JACKAudioBackend::_jack_timebase_callback (jack_transport_state_t state, pframes_t nframes,
689 jack_position_t* pos, int new_position, void *arg)
691 static_cast<JACKAudioBackend*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
695 JACKAudioBackend::jack_timebase_callback (jack_transport_state_t state, pframes_t nframes,
696 jack_position_t* pos, int new_position)
698 ARDOUR::Session* session = engine.session();
701 session->jack_timebase_callback (state, nframes, pos, new_position);
706 JACKAudioBackend::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
708 return static_cast<JACKAudioBackend*> (arg)->jack_sync_callback (state, pos);
712 JACKAudioBackend::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
714 TransportState tstate;
717 case JackTransportStopped:
718 tstate = TransportStopped;
720 case JackTransportRolling:
721 tstate = TransportRolling;
723 case JackTransportLooping:
724 tstate = TransportLooping;
726 case JackTransportStarting:
727 tstate = TransportStarting;
731 return engine.sync_callback (tstate, pos->frame);
737 JACKAudioBackend::_xrun_callback (void *arg)
739 JACKAudioBackend* ae = static_cast<JACKAudioBackend*> (arg);
740 if (ae->connected()) {
741 ae->engine.Xrun (); /* EMIT SIGNAL */
746 #ifdef HAVE_JACK_SESSION
748 JACKAudioBackend::_session_callback (jack_session_event_t *event, void *arg)
750 JACKAudioBackend* ae = static_cast<JACKAudioBackend*> (arg);
751 ARDOUR::Session* session = ae->engine.session();
754 session->jack_session_event (event);
760 JACKAudioBackend::_freewheel_callback (int onoff, void *arg)
762 static_cast<JACKAudioBackend*>(arg)->freewheel_callback (onoff);
766 JACKAudioBackend::freewheel_callback (int onoff)
768 _freewheeling = onoff;
769 engine.freewheel_callback (onoff);
773 JACKAudioBackend::_latency_callback (jack_latency_callback_mode_t mode, void* arg)
775 return static_cast<JACKAudioBackend*> (arg)->jack_latency_callback (mode);
779 JACKAudioBackend::create_process_thread (boost::function<void()> f, pthread_t* thread, size_t stacksize)
781 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
782 ThreadData* td = new ThreadData (this, f, stacksize);
784 if (jack_client_create_thread (_priv_jack, thread, jack_client_real_time_priority (_priv_jack),
785 jack_is_realtime (_priv_jack), _start_process_thread, td)) {
793 JACKAudioBackend::wait_for_process_thread_exit (AudioBackendNativeThread thr)
796 /* this doesn't actively try to stop the thread, it just waits till it exits */
797 return pthread_join (thr, &status);
801 JACKAudioBackend::_start_process_thread (void* arg)
803 ThreadData* td = reinterpret_cast<ThreadData*> (arg);
804 boost::function<void()> f = td->f;
813 JACKAudioBackend::_process_thread (void *arg)
815 return static_cast<JACKAudioBackend*> (arg)->process_thread ();
819 JACKAudioBackend::process_thread ()
821 /* JACK doesn't do this for us when we use the wait API
824 AudioEngine::thread_init_callback (this);
827 GET_PRIVATE_JACK_POINTER_RET(_priv_jack,0);
829 pframes_t nframes = jack_cycle_wait (_priv_jack);
831 if (engine.process_callback (nframes)) {
835 jack_cycle_signal (_priv_jack, 0);
842 JACKAudioBackend::_sample_rate_callback (pframes_t nframes, void *arg)
844 return static_cast<JACKAudioBackend*> (arg)->jack_sample_rate_callback (nframes);
848 JACKAudioBackend::jack_sample_rate_callback (pframes_t nframes)
850 _current_sample_rate = nframes;
851 return engine.sample_rate_change (nframes);
855 JACKAudioBackend::jack_latency_callback (jack_latency_callback_mode_t mode)
857 engine.latency_callback (mode == JackPlaybackLatency);
861 JACKAudioBackend::_bufsize_callback (pframes_t nframes, void *arg)
863 return static_cast<JACKAudioBackend*> (arg)->jack_bufsize_callback (nframes);
867 JACKAudioBackend::jack_bufsize_callback (pframes_t nframes)
869 /* if the size has not changed, this should be a no-op */
871 if (nframes == _current_buffer_size) {
875 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 1);
877 _current_buffer_size = nframes;
879 if (jack_port_type_get_buffer_size) {
880 _raw_buffer_sizes[DataType::AUDIO] = jack_port_type_get_buffer_size (_priv_jack, JACK_DEFAULT_AUDIO_TYPE);
881 _raw_buffer_sizes[DataType::MIDI] = jack_port_type_get_buffer_size (_priv_jack, JACK_DEFAULT_MIDI_TYPE);
884 /* Old version of JACK.
886 These crude guesses, see below where we try to get the right answers.
888 Note that our guess for MIDI deliberatey tries to overestimate
889 by a little. It would be nicer if we could get the actual
890 size from a port, but we have to use this estimate in the
891 event that there are no MIDI ports currently. If there are
892 the value will be adjusted below.
895 _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof (Sample);
896 _raw_buffer_sizes[DataType::MIDI] = nframes * 4 - (nframes/2);
899 engine.buffer_size_change (nframes);
905 JACKAudioBackend::disconnected (const char* why)
907 bool was_running = _running;
910 _current_buffer_size = 0;
911 _current_sample_rate = 0;
914 engine.halted_callback (why); /* EMIT SIGNAL */
918 JACKAudioBackend::cpu_load() const
920 GET_PRIVATE_JACK_POINTER_RET(_priv_jack,0);
921 return jack_cpu_load (_priv_jack);
925 JACKAudioBackend::update_latencies ()
927 GET_PRIVATE_JACK_POINTER (_priv_jack);
928 jack_recompute_total_latencies (_priv_jack);
932 JACKAudioBackend::n_physical (unsigned long flags) const
936 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, c);
938 const char ** ports = jack_get_ports (_priv_jack, NULL, NULL, JackPortIsPhysical | flags);
941 for (uint32_t i = 0; ports[i]; ++i) {
942 if (!strstr (ports[i], "Midi-Through")) {
943 DataType t (jack_port_type (jack_port_by_name (_priv_jack, ports[i])));
944 c.set (t, c.get (t) + 1);
955 JACKAudioBackend::can_change_sample_rate_when_running () const
961 JACKAudioBackend::can_change_buffer_size_when_running () const
967 JACKAudioBackend::control_app_name () const
969 /* Since JACK/ALSA really don't provide particularly integrated support
970 for the idea of a control app to be used to control a device,
971 allow the user to take some control themselves if necessary.
974 const char* env_value = g_getenv ("ARDOUR_DEVICE_CONTROL_APP");
978 if (_target_driver.empty() || _target_device.empty()) {
982 if (_target_driver == "ALSA") {
984 if (_target_device == "Hammerfall DSP") {
985 appname = "hdspconf";
986 } else if (_target_device == "M Audio Delta 1010") {
987 appname = "mudita24";
998 JACKAudioBackend::launch_control_app ()
1000 string appname = control_app_name();
1002 if (appname.empty()) {
1003 error << string_compose (_("There is no control application for the device \"%1\""), _target_device) << endmsg;
1007 std::list<string> args;
1008 args.push_back (appname);
1009 Glib::spawn_async ("", args, Glib::SPAWN_SEARCH_PATH);