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_utils.h"
40 #include "jack_session.h"
44 using namespace ARDOUR;
50 #define GET_PRIVATE_JACK_POINTER(localvar) jack_client_t* localvar = _jack_connection->jack(); if (!(localvar)) { return; }
51 #define GET_PRIVATE_JACK_POINTER_RET(localvar,r) jack_client_t* localvar = _jack_connection->jack(); if (!(localvar)) { return r; }
53 JACKAudioBackend::JACKAudioBackend (AudioEngine& e, boost::shared_ptr<JackConnection> jc)
55 , _jack_connection (jc)
57 , _freewheeling (false)
58 , _target_sample_rate (48000)
59 , _target_buffer_size (1024)
60 , _target_sample_format (FormatFloat)
61 , _target_interleaved (false)
62 , _target_input_channels (0)
63 , _target_output_channels (0)
64 , _target_systemic_input_latency (0)
65 , _target_systemic_output_latency (0)
66 , _current_sample_rate (0)
67 , _current_buffer_size (0)
70 _jack_connection->Connected.connect_same_thread (jack_connection_connection, boost::bind (&JACKAudioBackend::when_connected_to_jack, this));
71 _jack_connection->Disconnected.connect_same_thread (disconnect_connection, boost::bind (&JACKAudioBackend::disconnected, this, _1));
74 JACKAudioBackend::~JACKAudioBackend()
79 JACKAudioBackend::name() const
85 JACKAudioBackend::private_handle() const
87 return _jack_connection->jack();
91 JACKAudioBackend::available() const
93 return (private_handle() != 0);
97 JACKAudioBackend::is_realtime () const
99 GET_PRIVATE_JACK_POINTER_RET (_priv_jack,false);
100 return jack_is_realtime (_priv_jack);
104 JACKAudioBackend::requires_driver_selection() const
110 JACKAudioBackend::enumerate_drivers () const
112 vector<string> currently_available;
113 get_jack_audio_driver_names (currently_available);
114 return currently_available;
118 JACKAudioBackend::set_driver (const std::string& name)
120 _target_driver = name;
124 vector<AudioBackend::DeviceStatus>
125 JACKAudioBackend::enumerate_devices () const
127 vector<string> currently_available = get_jack_device_names_for_audio_driver (_target_driver);
128 vector<DeviceStatus> statuses;
130 if (all_devices.find (_target_driver) == all_devices.end()) {
131 all_devices.insert (make_pair (_target_driver, std::set<string>()));
134 /* store every device we've found, by driver name.
136 * This is so we do not confuse ALSA, FFADO, netjack etc. devices
140 DeviceList& all (all_devices[_target_driver]);
142 for (vector<string>::const_iterator d = currently_available.begin(); d != currently_available.end(); ++d) {
146 for (DeviceList::const_iterator d = all.begin(); d != all.end(); ++d) {
147 if (find (currently_available.begin(), currently_available.end(), *d) == currently_available.end()) {
148 statuses.push_back (DeviceStatus (*d, false));
150 statuses.push_back (DeviceStatus (*d, false));
158 JACKAudioBackend::available_sample_rates (const string& /*device*/) const
163 f.push_back (sample_rate());
167 /* if JACK is not already running, just list a bunch of reasonable
168 values and let the future sort it all out.
171 f.push_back (8000.0);
172 f.push_back (16000.0);
173 f.push_back (24000.0);
174 f.push_back (32000.0);
175 f.push_back (44100.0);
176 f.push_back (48000.0);
177 f.push_back (88200.0);
178 f.push_back (96000.0);
179 f.push_back (192000.0);
180 f.push_back (384000.0);
186 JACKAudioBackend::available_buffer_sizes (const string& /*device*/) const
191 s.push_back (buffer_size());
211 JACKAudioBackend::available_input_channel_count (const string& /*device*/) const
217 JACKAudioBackend::available_output_channel_count (const string& /*device*/) const
222 /* -- parameter setting -- */
225 JACKAudioBackend::set_device_name (const string& dev)
228 /* need to stop and restart JACK for this to work, at present */
232 _target_device = dev;
237 JACKAudioBackend::set_sample_rate (float sr)
240 _target_sample_rate = sr;
244 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
246 if (sr == jack_get_sample_rate (_priv_jack)) {
254 JACKAudioBackend::set_buffer_size (uint32_t nframes)
257 _target_buffer_size = nframes;
261 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
263 if (nframes == jack_get_buffer_size (_priv_jack)) {
267 return jack_set_buffer_size (_priv_jack, nframes);
271 JACKAudioBackend::set_sample_format (SampleFormat sf)
273 /* as far as JACK clients are concerned, the hardware is always
274 * floating point format.
276 if (sf == FormatFloat) {
283 JACKAudioBackend::set_interleaved (bool yn)
285 /* as far as JACK clients are concerned, the hardware is always
295 JACKAudioBackend::set_input_channels (uint32_t cnt)
299 /* can't set a real value for this while JACK runs */
304 _target_input_channels = cnt;
310 JACKAudioBackend::set_output_channels (uint32_t cnt)
314 /* can't set a real value for this while JACK runs */
319 _target_output_channels = cnt;
325 JACKAudioBackend::set_systemic_input_latency (uint32_t l)
328 /* can't do this while JACK runs */
332 _target_systemic_input_latency = l;
338 JACKAudioBackend::set_systemic_output_latency (uint32_t l)
341 /* can't do this while JACK runs */
345 _target_systemic_output_latency = l;
350 /* --- Parameter retrieval --- */
353 JACKAudioBackend::device_name () const
355 if (!_jack_connection->in_control()) {
356 return "???"; // JACK has no way (as of fall 2013) to return
360 return _target_device;
364 JACKAudioBackend::driver_name() const
366 if (!_jack_connection->in_control()) {
367 return "???"; // JACK has no way (as of fall 2013) to return
371 return _target_driver;
375 JACKAudioBackend::sample_rate () const
377 if (!_jack_connection->in_control()) {
379 return _current_sample_rate;
384 return _target_sample_rate;
388 JACKAudioBackend::buffer_size () const
390 if (!_jack_connection->in_control()) {
392 return _current_buffer_size;
397 return _target_buffer_size;
401 JACKAudioBackend::sample_format () const
407 JACKAudioBackend::interleaved () const
413 JACKAudioBackend::midi_option () const
415 return _target_midi_option;
419 JACKAudioBackend::input_channels () const
421 if (!_jack_connection->in_control()) {
423 return n_physical (JackPortIsInput).n_audio();
429 return n_physical (JackPortIsInput).n_audio();
431 return _target_input_channels;
437 JACKAudioBackend::output_channels () const
439 if (!_jack_connection->in_control()) {
441 return n_physical (JackPortIsOutput).n_audio();
447 return n_physical (JackPortIsOutput).n_audio();
449 return _target_output_channels;
455 JACKAudioBackend::systemic_input_latency () const
457 return _target_systemic_output_latency;
461 JACKAudioBackend::systemic_output_latency () const
463 return _target_systemic_output_latency;
467 JACKAudioBackend::raw_buffer_size(DataType t)
469 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
470 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
474 JACKAudioBackend::setup_jack_startup_command (bool for_latency_measurement)
476 /* first we map the parameters that have been set onto a
477 * JackCommandLineOptions object.
480 JackCommandLineOptions options;
482 get_jack_default_server_path (options.server_path);
483 options.driver = _target_driver;
484 options.samplerate = _target_sample_rate;
485 options.period_size = _target_buffer_size;
486 options.num_periods = 2;
487 options.input_device = _target_device;
488 options.output_device = _target_device;
489 options.input_latency = _target_systemic_input_latency;
490 options.output_latency = _target_systemic_output_latency;
491 options.input_channels = _target_input_channels;
492 options.output_channels = _target_output_channels;
493 if (_target_sample_format == FormatInt16) {
494 options.force16_bit = _target_sample_format;
496 options.realtime = true;
497 options.ports_max = 2048;
499 ARDOUR::set_midi_option (options, _target_midi_option);
501 /* this must always be true for any server instance we start ourselves
504 options.temporary = true;
508 if (!get_jack_command_line_string (options, cmdline, for_latency_measurement)) {
509 /* error, somehow - we will still try to start JACK
510 * automatically but it will be without our preferred options
512 std::cerr << "get_jack_command_line_string () failed: using default settings." << std::endl;
516 std::cerr << "JACK command line will be: " << cmdline << std::endl;
518 write_jack_config_file (get_jack_server_user_config_file_path(), cmdline);
521 /* ---- BASIC STATE CONTROL API: start/stop/pause/freewheel --- */
524 JACKAudioBackend::_start (bool for_latency_measurement)
528 if (_jack_connection->in_control()) {
529 /* we will be starting JACK, so set up the
530 command that JACK will use when it (auto-)starts
532 setup_jack_startup_command (for_latency_measurement);
535 if (_jack_connection->open ()) {
540 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
542 /* get the buffer size and sample rates established */
544 jack_sample_rate_callback (jack_get_sample_rate (_priv_jack));
545 jack_bufsize_callback (jack_get_buffer_size (_priv_jack));
547 /* Now that we have buffer size and sample rate established, the engine
548 can go ahead and do its stuff
551 engine.reestablish_ports ();
553 if (!jack_port_type_get_buffer_size) {
554 warning << _("This version of JACK is old - you should upgrade to a newer version that supports jack_port_type_get_buffer_size()") << endmsg;
557 set_jack_callbacks ();
559 if (jack_activate (_priv_jack) == 0) {
562 // error << _("cannot activate JACK client") << endmsg;
565 engine.reconnect_ports ();
571 JACKAudioBackend::stop ()
573 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
575 _jack_connection->close ();
577 _current_buffer_size = 0;
578 _current_sample_rate = 0;
580 _raw_buffer_sizes.clear();
586 JACKAudioBackend::freewheel (bool onoff)
588 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
590 if (onoff == _freewheeling) {
591 /* already doing what has been asked for */
596 if (jack_set_freewheel (_priv_jack, onoff) == 0) {
597 _freewheeling = onoff;
604 /* --- TRANSPORT STATE MANAGEMENT --- */
607 JACKAudioBackend::transport_stop ()
609 GET_PRIVATE_JACK_POINTER (_priv_jack);
610 jack_transport_stop (_priv_jack);
614 JACKAudioBackend::transport_start ()
616 GET_PRIVATE_JACK_POINTER (_priv_jack);
617 jack_transport_start (_priv_jack);
621 JACKAudioBackend::transport_locate (framepos_t where)
623 GET_PRIVATE_JACK_POINTER (_priv_jack);
624 jack_transport_locate (_priv_jack, where);
628 JACKAudioBackend::transport_frame () const
630 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
631 return jack_get_current_transport_frame (_priv_jack);
635 JACKAudioBackend::transport_state () const
637 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, ((TransportState) JackTransportStopped));
639 return (TransportState) jack_transport_query (_priv_jack, &pos);
643 JACKAudioBackend::set_time_master (bool yn)
645 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
647 return jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
649 return jack_release_timebase (_priv_jack);
656 JACKAudioBackend::get_sync_offset (pframes_t& offset) const
659 #ifdef HAVE_JACK_VIDEO_SUPPORT
661 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
666 (void) jack_transport_query (_priv_jack, &pos);
668 if (pos.valid & JackVideoFrameOffset) {
669 offset = pos.video_offset;
682 JACKAudioBackend::sample_time ()
684 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
685 return jack_frame_time (_priv_jack);
689 JACKAudioBackend::sample_time_at_cycle_start ()
691 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
692 return jack_last_frame_time (_priv_jack);
696 JACKAudioBackend::samples_since_cycle_start ()
698 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
699 return jack_frames_since_cycle_start (_priv_jack);
705 ardour_jack_error (const char* msg)
707 error << "JACK: " << msg << endmsg;
711 JACKAudioBackend::set_jack_callbacks ()
713 GET_PRIVATE_JACK_POINTER (_priv_jack);
715 jack_set_thread_init_callback (_priv_jack, AudioEngine::thread_init_callback, 0);
717 jack_set_process_thread (_priv_jack, _process_thread, this);
718 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
719 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
720 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
721 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
722 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
724 #ifdef HAVE_JACK_SESSION
725 if( jack_set_session_callback)
726 jack_set_session_callback (_priv_jack, _session_callback, this);
729 if (jack_set_latency_callback) {
730 jack_set_latency_callback (_priv_jack, _latency_callback, this);
733 jack_set_error_function (ardour_jack_error);
737 JACKAudioBackend::_jack_timebase_callback (jack_transport_state_t state, pframes_t nframes,
738 jack_position_t* pos, int new_position, void *arg)
740 static_cast<JACKAudioBackend*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
744 JACKAudioBackend::jack_timebase_callback (jack_transport_state_t state, pframes_t nframes,
745 jack_position_t* pos, int new_position)
747 ARDOUR::Session* session = engine.session();
750 JACKSession jsession (session);
751 jsession.timebase_callback (state, nframes, pos, new_position);
756 JACKAudioBackend::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
758 return static_cast<JACKAudioBackend*> (arg)->jack_sync_callback (state, pos);
762 JACKAudioBackend::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
764 TransportState tstate;
767 case JackTransportStopped:
768 tstate = TransportStopped;
770 case JackTransportRolling:
771 tstate = TransportRolling;
773 case JackTransportLooping:
774 tstate = TransportLooping;
776 case JackTransportStarting:
777 tstate = TransportStarting;
781 return engine.sync_callback (tstate, pos->frame);
787 JACKAudioBackend::_xrun_callback (void *arg)
789 JACKAudioBackend* jab = static_cast<JACKAudioBackend*> (arg);
790 if (jab->available()) {
791 jab->engine.Xrun (); /* EMIT SIGNAL */
797 JACKAudioBackend::_session_callback (jack_session_event_t *event, void *arg)
799 JACKAudioBackend* jab = static_cast<JACKAudioBackend*> (arg);
800 ARDOUR::Session* session = jab->engine.session();
803 JACKSession jsession (session);
804 jsession.session_event (event);
809 JACKAudioBackend::_freewheel_callback (int onoff, void *arg)
811 static_cast<JACKAudioBackend*>(arg)->freewheel_callback (onoff);
815 JACKAudioBackend::freewheel_callback (int onoff)
817 _freewheeling = onoff;
818 engine.freewheel_callback (onoff);
822 JACKAudioBackend::_latency_callback (jack_latency_callback_mode_t mode, void* arg)
824 return static_cast<JACKAudioBackend*> (arg)->jack_latency_callback (mode);
828 JACKAudioBackend::create_process_thread (boost::function<void()> f)
830 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
832 jack_native_thread_t thread_id;
833 ThreadData* td = new ThreadData (this, f, thread_stack_size());
835 if (jack_client_create_thread (_priv_jack, &thread_id, jack_client_real_time_priority (_priv_jack),
836 jack_is_realtime (_priv_jack), _start_process_thread, td)) {
840 _jack_threads.push_back(thread_id);
845 JACKAudioBackend::join_process_threads ()
847 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
851 for (std::vector<jack_native_thread_t>::const_iterator i = _jack_threads.begin ();
852 i != _jack_threads.end(); i++) {
854 #if defined(USING_JACK2_EXPANSION_OF_JACK_API) || defined(PLATFORM_WINDOWS)
855 if (jack_client_stop_thread (_priv_jack, *i) != 0) {
858 if (pthread_join (*i, &status) != 0) {
860 error << "AudioEngine: cannot stop process thread" << endmsg;
865 _jack_threads.clear();
871 JACKAudioBackend::in_process_thread ()
873 for (std::vector<jack_native_thread_t>::const_iterator i = _jack_threads.begin ();
874 i != _jack_threads.end(); i++) {
876 #ifdef COMPILER_MINGW
877 if (*i == GetCurrentThread()) {
881 if (pthread_equal (*i, pthread_self()) != 0) {
891 JACKAudioBackend::process_thread_count ()
893 return _jack_threads.size();
897 JACKAudioBackend::_start_process_thread (void* arg)
899 ThreadData* td = reinterpret_cast<ThreadData*> (arg);
900 boost::function<void()> f = td->f;
909 JACKAudioBackend::_process_thread (void *arg)
911 return static_cast<JACKAudioBackend*> (arg)->process_thread ();
915 JACKAudioBackend::process_thread ()
917 /* JACK doesn't do this for us when we use the wait API
920 AudioEngine::thread_init_callback (this);
923 GET_PRIVATE_JACK_POINTER_RET(_priv_jack,0);
925 pframes_t nframes = jack_cycle_wait (_priv_jack);
927 if (engine.process_callback (nframes)) {
931 jack_cycle_signal (_priv_jack, 0);
938 JACKAudioBackend::_sample_rate_callback (pframes_t nframes, void *arg)
940 return static_cast<JACKAudioBackend*> (arg)->jack_sample_rate_callback (nframes);
944 JACKAudioBackend::jack_sample_rate_callback (pframes_t nframes)
946 _current_sample_rate = nframes;
947 return engine.sample_rate_change (nframes);
951 JACKAudioBackend::jack_latency_callback (jack_latency_callback_mode_t mode)
953 engine.latency_callback (mode == JackPlaybackLatency);
957 JACKAudioBackend::_bufsize_callback (pframes_t nframes, void *arg)
959 return static_cast<JACKAudioBackend*> (arg)->jack_bufsize_callback (nframes);
963 JACKAudioBackend::jack_bufsize_callback (pframes_t nframes)
965 /* if the size has not changed, this should be a no-op */
967 if (nframes == _current_buffer_size) {
971 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 1);
973 _current_buffer_size = nframes;
975 if (jack_port_type_get_buffer_size) {
976 _raw_buffer_sizes[DataType::AUDIO] = jack_port_type_get_buffer_size (_priv_jack, JACK_DEFAULT_AUDIO_TYPE);
977 _raw_buffer_sizes[DataType::MIDI] = jack_port_type_get_buffer_size (_priv_jack, JACK_DEFAULT_MIDI_TYPE);
980 /* Old version of JACK.
982 These crude guesses, see below where we try to get the right answers.
984 Note that our guess for MIDI deliberatey tries to overestimate
985 by a little. It would be nicer if we could get the actual
986 size from a port, but we have to use this estimate in the
987 event that there are no MIDI ports currently. If there are
988 the value will be adjusted below.
991 _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof (Sample);
992 _raw_buffer_sizes[DataType::MIDI] = nframes * 4 - (nframes/2);
995 engine.buffer_size_change (nframes);
1001 JACKAudioBackend::disconnected (const char* why)
1003 bool was_running = _running;
1006 _current_buffer_size = 0;
1007 _current_sample_rate = 0;
1010 engine.halted_callback (why); /* EMIT SIGNAL */
1015 JACKAudioBackend::cpu_load() const
1017 GET_PRIVATE_JACK_POINTER_RET(_priv_jack,0);
1018 return jack_cpu_load (_priv_jack);
1022 JACKAudioBackend::update_latencies ()
1024 GET_PRIVATE_JACK_POINTER (_priv_jack);
1025 jack_recompute_total_latencies (_priv_jack);
1029 JACKAudioBackend::n_physical (unsigned long flags) const
1033 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, c);
1035 const char ** ports = jack_get_ports (_priv_jack, NULL, NULL, JackPortIsPhysical | flags);
1038 for (uint32_t i = 0; ports[i]; ++i) {
1039 if (!strstr (ports[i], "Midi-Through")) {
1040 DataType t = port_data_type (jack_port_by_name (_priv_jack, ports[i]));
1041 if (t != DataType::NIL) {
1042 c.set (t, c.get (t) + 1);
1054 JACKAudioBackend::can_change_sample_rate_when_running () const
1060 JACKAudioBackend::can_change_buffer_size_when_running () const
1066 JACKAudioBackend::control_app_name () const
1068 /* Since JACK/ALSA really don't provide particularly integrated support
1069 for the idea of a control app to be used to control a device,
1070 allow the user to take some control themselves if necessary.
1073 const char* env_value = g_getenv ("ARDOUR_DEVICE_CONTROL_APP");
1077 if (_target_driver.empty() || _target_device.empty()) {
1081 if (_target_driver == "ALSA") {
1083 if (_target_device == "Hammerfall DSP") {
1084 appname = "hdspconf";
1085 } else if (_target_device == "M Audio Delta 1010") {
1086 appname = "mudita24";
1090 appname = env_value;
1097 JACKAudioBackend::launch_control_app ()
1099 string appname = control_app_name();
1101 if (appname.empty()) {
1102 error << string_compose (_("There is no control application for the device \"%1\""), _target_device) << endmsg;
1106 std::list<string> args;
1107 args.push_back (appname);
1108 Glib::spawn_async ("", args, Glib::SPAWN_SEARCH_PATH);
1112 JACKAudioBackend::enumerate_midi_options () const
1114 return ARDOUR::enumerate_midi_options ();
1118 JACKAudioBackend::set_midi_option (const string& opt)
1120 _target_midi_option = opt;
1125 JACKAudioBackend::speed_and_position (double& speed, framepos_t& position)
1127 jack_position_t pos;
1128 jack_transport_state_t state;
1131 /* this won't be called if the port engine in use is not JACK, so we do
1132 not have to worry about the type of PortEngine::private_handle()
1138 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, true);
1140 state = jack_transport_query (_priv_jack, &pos);
1143 case JackTransportStopped:
1147 case JackTransportRolling:
1151 case JackTransportLooping:
1155 case JackTransportStarting:
1157 // don't adjust speed here, just leave it as it was
1160 std::cerr << "WARNING: Unknown JACK transport state: " << state << std::endl;
1163 position = pos.frame;