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"
43 using namespace ARDOUR;
49 #define GET_PRIVATE_JACK_POINTER(localvar) jack_client_t* localvar = _jack_connection->jack(); if (!(localvar)) { return; }
50 #define GET_PRIVATE_JACK_POINTER_RET(localvar,r) jack_client_t* localvar = _jack_connection->jack(); if (!(localvar)) { return r; }
52 JACKAudioBackend::JACKAudioBackend (AudioEngine& e, boost::shared_ptr<JackConnection> jc)
54 , _jack_connection (jc)
56 , _freewheeling (false)
57 , _target_sample_rate (48000)
58 , _target_buffer_size (1024)
59 , _target_sample_format (FormatFloat)
60 , _target_interleaved (false)
61 , _target_input_channels (0)
62 , _target_output_channels (0)
63 , _target_systemic_input_latency (0)
64 , _target_systemic_output_latency (0)
65 , _current_sample_rate (0)
66 , _current_buffer_size (0)
68 _jack_connection->Connected.connect_same_thread (jack_connection_connection, boost::bind (&JACKAudioBackend::when_connected_to_jack, this));
69 _jack_connection->Disconnected.connect_same_thread (disconnect_connection, boost::bind (&JACKAudioBackend::disconnected, this, _1));
72 JACKAudioBackend::~JACKAudioBackend()
77 JACKAudioBackend::name() const
83 JACKAudioBackend::private_handle() const
85 return _jack_connection->jack();
89 JACKAudioBackend::available() const
91 return (private_handle() != 0);
95 JACKAudioBackend::is_realtime () const
97 GET_PRIVATE_JACK_POINTER_RET (_priv_jack,false);
98 return jack_is_realtime (_priv_jack);
102 JACKAudioBackend::requires_driver_selection() const
108 JACKAudioBackend::enumerate_drivers () const
110 vector<string> currently_available;
111 get_jack_audio_driver_names (currently_available);
112 return currently_available;
116 JACKAudioBackend::set_driver (const std::string& name)
118 _target_driver = name;
122 vector<AudioBackend::DeviceStatus>
123 JACKAudioBackend::enumerate_devices () const
125 vector<string> currently_available = get_jack_device_names_for_audio_driver (_target_driver);
126 vector<DeviceStatus> statuses;
128 if (all_devices.find (_target_driver) == all_devices.end()) {
129 all_devices.insert (make_pair (_target_driver, std::set<string>()));
132 /* store every device we've found, by driver name.
134 * This is so we do not confuse ALSA, FFADO, netjack etc. devices
138 DeviceList& all (all_devices[_target_driver]);
140 for (vector<string>::const_iterator d = currently_available.begin(); d != currently_available.end(); ++d) {
144 for (DeviceList::const_iterator d = all.begin(); d != all.end(); ++d) {
145 if (find (currently_available.begin(), currently_available.end(), *d) == currently_available.end()) {
146 statuses.push_back (DeviceStatus (*d, false));
148 statuses.push_back (DeviceStatus (*d, false));
156 JACKAudioBackend::available_sample_rates (const string& /*device*/) const
161 f.push_back (sample_rate());
165 /* if JACK is not already running, just list a bunch of reasonable
166 values and let the future sort it all out.
169 f.push_back (8000.0);
170 f.push_back (16000.0);
171 f.push_back (24000.0);
172 f.push_back (32000.0);
173 f.push_back (44100.0);
174 f.push_back (48000.0);
175 f.push_back (88200.0);
176 f.push_back (96000.0);
177 f.push_back (192000.0);
178 f.push_back (384000.0);
184 JACKAudioBackend::available_buffer_sizes (const string& /*device*/) const
189 s.push_back (buffer_size());
209 JACKAudioBackend::available_input_channel_count (const string& /*device*/) const
215 JACKAudioBackend::available_output_channel_count (const string& /*device*/) const
220 /* -- parameter setting -- */
223 JACKAudioBackend::set_device_name (const string& dev)
226 /* need to stop and restart JACK for this to work, at present */
230 _target_device = dev;
235 JACKAudioBackend::set_sample_rate (float sr)
238 _target_sample_rate = sr;
242 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
244 if (sr == jack_get_sample_rate (_priv_jack)) {
252 JACKAudioBackend::set_buffer_size (uint32_t nframes)
255 _target_buffer_size = nframes;
259 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
261 if (nframes == jack_get_buffer_size (_priv_jack)) {
265 return jack_set_buffer_size (_priv_jack, nframes);
269 JACKAudioBackend::set_sample_format (SampleFormat sf)
271 /* as far as JACK clients are concerned, the hardware is always
272 * floating point format.
274 if (sf == FormatFloat) {
281 JACKAudioBackend::set_interleaved (bool yn)
283 /* as far as JACK clients are concerned, the hardware is always
293 JACKAudioBackend::set_input_channels (uint32_t cnt)
297 /* can't set a real value for this while JACK runs */
302 _target_input_channels = cnt;
308 JACKAudioBackend::set_output_channels (uint32_t cnt)
312 /* can't set a real value for this while JACK runs */
317 _target_output_channels = cnt;
323 JACKAudioBackend::set_systemic_input_latency (uint32_t l)
326 /* can't do this while JACK runs */
330 _target_systemic_input_latency = l;
336 JACKAudioBackend::set_systemic_output_latency (uint32_t l)
339 /* can't do this while JACK runs */
343 _target_systemic_output_latency = l;
348 /* --- Parameter retrieval --- */
351 JACKAudioBackend::device_name () const
353 if (!_jack_connection->in_control()) {
354 return "???"; // JACK has no way (as of fall 2013) to return
358 return _target_device;
362 JACKAudioBackend::driver_name() const
364 if (!_jack_connection->in_control()) {
365 return "???"; // JACK has no way (as of fall 2013) to return
369 return _target_driver;
373 JACKAudioBackend::sample_rate () const
375 if (!_jack_connection->in_control()) {
377 return _current_sample_rate;
382 return _target_sample_rate;
386 JACKAudioBackend::buffer_size () const
388 if (!_jack_connection->in_control()) {
390 return _current_buffer_size;
395 return _target_buffer_size;
399 JACKAudioBackend::sample_format () const
405 JACKAudioBackend::interleaved () const
411 JACKAudioBackend::input_channels () const
413 if (!_jack_connection->in_control()) {
415 return n_physical (JackPortIsInput).n_audio();
421 return n_physical (JackPortIsInput).n_audio();
423 return _target_input_channels;
429 JACKAudioBackend::output_channels () const
431 if (!_jack_connection->in_control()) {
433 return n_physical (JackPortIsOutput).n_audio();
439 return n_physical (JackPortIsOutput).n_audio();
441 return _target_output_channels;
447 JACKAudioBackend::systemic_input_latency () const
449 return _target_systemic_output_latency;
453 JACKAudioBackend::systemic_output_latency () const
455 return _target_systemic_output_latency;
459 JACKAudioBackend::raw_buffer_size(DataType t)
461 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
462 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
466 JACKAudioBackend::setup_jack_startup_command ()
468 /* first we map the parameters that have been set onto a
469 * JackCommandLineOptions object.
472 JackCommandLineOptions options;
474 get_jack_default_server_path (options.server_path);
475 options.driver = _target_driver;
476 options.samplerate = _target_sample_rate;
477 options.period_size = _target_buffer_size;
478 options.num_periods = 2;
479 options.input_device = _target_device;
480 options.output_device = _target_device;
481 options.input_latency = _target_systemic_input_latency;
482 options.output_latency = _target_systemic_output_latency;
483 options.input_channels = _target_input_channels;
484 options.output_channels = _target_output_channels;
485 if (_target_sample_format == FormatInt16) {
486 options.force16_bit = _target_sample_format;
488 options.realtime = true;
489 options.ports_max = 2048;
491 /* this must always be true for any server instance we start ourselves
494 options.temporary = true;
498 if (!get_jack_command_line_string (options, cmdline)) {
499 /* error, somehow - we will still try to start JACK
500 * automatically but it will be without our preferred options
505 std::cerr << "JACK command line will be: " << cmdline << std::endl;
507 write_jack_config_file (get_jack_server_user_config_file_path(), cmdline);
510 /* ---- BASIC STATE CONTROL API: start/stop/pause/freewheel --- */
513 JACKAudioBackend::start ()
517 if (_jack_connection->in_control()) {
518 /* we will be starting JACK, so set up the
519 command that JACK will use when it (auto-)starts
521 setup_jack_startup_command ();
524 if (_jack_connection->open ()) {
529 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
531 /* get the buffer size and sample rates established */
533 jack_sample_rate_callback (jack_get_sample_rate (_priv_jack));
534 jack_bufsize_callback (jack_get_buffer_size (_priv_jack));
536 /* Now that we have buffer size and sample rate established, the engine
537 can go ahead and do its stuff
540 engine.reestablish_ports ();
542 if (!jack_port_type_get_buffer_size) {
543 warning << _("This version of JACK is old - you should upgrade to a newer version that supports jack_port_type_get_buffer_size()") << endmsg;
546 set_jack_callbacks ();
548 if (jack_activate (_priv_jack) == 0) {
551 // error << _("cannot activate JACK client") << endmsg;
554 engine.reconnect_ports ();
560 JACKAudioBackend::stop ()
562 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
564 _jack_connection->close ();
566 _current_buffer_size = 0;
567 _current_sample_rate = 0;
569 _raw_buffer_sizes.clear();
575 JACKAudioBackend::pause ()
577 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
580 jack_deactivate (_priv_jack);
587 JACKAudioBackend::freewheel (bool onoff)
589 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
591 if (onoff == _freewheeling) {
592 /* already doing what has been asked for */
597 if (jack_set_freewheel (_priv_jack, onoff) == 0) {
598 _freewheeling = true;
605 /* --- TRANSPORT STATE MANAGEMENT --- */
608 JACKAudioBackend::transport_stop ()
610 GET_PRIVATE_JACK_POINTER (_priv_jack);
611 jack_transport_stop (_priv_jack);
615 JACKAudioBackend::transport_start ()
617 GET_PRIVATE_JACK_POINTER (_priv_jack);
618 jack_transport_start (_priv_jack);
622 JACKAudioBackend::transport_locate (framepos_t where)
624 GET_PRIVATE_JACK_POINTER (_priv_jack);
625 jack_transport_locate (_priv_jack, where);
629 JACKAudioBackend::transport_frame () const
631 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
632 return jack_get_current_transport_frame (_priv_jack);
636 JACKAudioBackend::transport_state () const
638 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, ((TransportState) JackTransportStopped));
640 return (TransportState) jack_transport_query (_priv_jack, &pos);
644 JACKAudioBackend::set_time_master (bool yn)
646 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
648 return jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
650 return jack_release_timebase (_priv_jack);
657 JACKAudioBackend::get_sync_offset (pframes_t& offset) const
660 #ifdef HAVE_JACK_VIDEO_SUPPORT
662 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
667 (void) jack_transport_query (_priv_jack, &pos);
669 if (pos.valid & JackVideoFrameOffset) {
670 offset = pos.video_offset;
683 JACKAudioBackend::sample_time ()
685 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
686 return jack_frame_time (_priv_jack);
690 JACKAudioBackend::sample_time_at_cycle_start ()
692 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
693 return jack_last_frame_time (_priv_jack);
697 JACKAudioBackend::samples_since_cycle_start ()
699 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
700 return jack_frames_since_cycle_start (_priv_jack);
706 ardour_jack_error (const char* msg)
708 error << "JACK: " << msg << endmsg;
712 JACKAudioBackend::set_jack_callbacks ()
714 GET_PRIVATE_JACK_POINTER (_priv_jack);
716 jack_set_thread_init_callback (_priv_jack, AudioEngine::thread_init_callback, 0);
718 jack_set_process_thread (_priv_jack, _process_thread, this);
719 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
720 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
721 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
722 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
723 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
725 #ifdef HAVE_JACK_SESSION
726 if( jack_set_session_callback)
727 jack_set_session_callback (_priv_jack, _session_callback, this);
730 if (jack_set_latency_callback) {
731 jack_set_latency_callback (_priv_jack, _latency_callback, this);
734 jack_set_error_function (ardour_jack_error);
738 JACKAudioBackend::_jack_timebase_callback (jack_transport_state_t state, pframes_t nframes,
739 jack_position_t* pos, int new_position, void *arg)
741 static_cast<JACKAudioBackend*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
745 JACKAudioBackend::jack_timebase_callback (jack_transport_state_t state, pframes_t nframes,
746 jack_position_t* pos, int new_position)
748 ARDOUR::Session* session = engine.session();
751 session->jack_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 */
796 #ifdef HAVE_JACK_SESSION
798 JACKAudioBackend::_session_callback (jack_session_event_t *event, void *arg)
800 JACKAudioBackend* jab = static_cast<JACKAudioBackend*> (arg);
801 ARDOUR::Session* session = jab->engine.session();
804 session->jack_session_event (event);
810 JACKAudioBackend::_freewheel_callback (int onoff, void *arg)
812 static_cast<JACKAudioBackend*>(arg)->freewheel_callback (onoff);
816 JACKAudioBackend::freewheel_callback (int onoff)
818 _freewheeling = onoff;
819 engine.freewheel_callback (onoff);
823 JACKAudioBackend::_latency_callback (jack_latency_callback_mode_t mode, void* arg)
825 return static_cast<JACKAudioBackend*> (arg)->jack_latency_callback (mode);
829 JACKAudioBackend::create_process_thread (boost::function<void()> f)
831 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
833 jack_native_thread_t thread_id;
834 ThreadData* td = new ThreadData (this, f, thread_stack_size());
836 if (jack_client_create_thread (_priv_jack, &thread_id, jack_client_real_time_priority (_priv_jack),
837 jack_is_realtime (_priv_jack), _start_process_thread, td)) {
841 _jack_threads.push_back(thread_id);
846 JACKAudioBackend::join_process_threads ()
848 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
852 for (std::vector<jack_native_thread_t>::const_iterator i = _jack_threads.begin ();
853 i != _jack_threads.end(); i++) {
855 #if defined(USING_JACK2_EXPANSION_OF_JACK_API) || defined(PLATFORM_WINDOWS)
856 if (jack_client_stop_thread (_priv_jack, *i) != 0) {
859 if (pthread_join (*i, &status) != 0) {
861 error << "AudioEngine: cannot stop process thread" << endmsg;
866 _jack_threads.clear();
872 JACKAudioBackend::in_process_thread ()
874 for (std::vector<jack_native_thread_t>::const_iterator i = _jack_threads.begin ();
875 i != _jack_threads.end(); i++) {
877 #ifdef COMPILER_MINGW
878 if (*i == GetCurrentThread()) {
882 if (pthread_equal (*i, pthread_self()) != 0) {
892 JACKAudioBackend::process_thread_count ()
894 return _jack_threads.size();
898 JACKAudioBackend::_start_process_thread (void* arg)
900 ThreadData* td = reinterpret_cast<ThreadData*> (arg);
901 boost::function<void()> f = td->f;
910 JACKAudioBackend::_process_thread (void *arg)
912 return static_cast<JACKAudioBackend*> (arg)->process_thread ();
916 JACKAudioBackend::process_thread ()
918 /* JACK doesn't do this for us when we use the wait API
921 AudioEngine::thread_init_callback (this);
924 GET_PRIVATE_JACK_POINTER_RET(_priv_jack,0);
926 pframes_t nframes = jack_cycle_wait (_priv_jack);
928 if (engine.process_callback (nframes)) {
932 jack_cycle_signal (_priv_jack, 0);
939 JACKAudioBackend::_sample_rate_callback (pframes_t nframes, void *arg)
941 return static_cast<JACKAudioBackend*> (arg)->jack_sample_rate_callback (nframes);
945 JACKAudioBackend::jack_sample_rate_callback (pframes_t nframes)
947 _current_sample_rate = nframes;
948 return engine.sample_rate_change (nframes);
952 JACKAudioBackend::jack_latency_callback (jack_latency_callback_mode_t mode)
954 engine.latency_callback (mode == JackPlaybackLatency);
958 JACKAudioBackend::_bufsize_callback (pframes_t nframes, void *arg)
960 return static_cast<JACKAudioBackend*> (arg)->jack_bufsize_callback (nframes);
964 JACKAudioBackend::jack_bufsize_callback (pframes_t nframes)
966 /* if the size has not changed, this should be a no-op */
968 if (nframes == _current_buffer_size) {
972 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 1);
974 _current_buffer_size = nframes;
976 if (jack_port_type_get_buffer_size) {
977 _raw_buffer_sizes[DataType::AUDIO] = jack_port_type_get_buffer_size (_priv_jack, JACK_DEFAULT_AUDIO_TYPE);
978 _raw_buffer_sizes[DataType::MIDI] = jack_port_type_get_buffer_size (_priv_jack, JACK_DEFAULT_MIDI_TYPE);
981 /* Old version of JACK.
983 These crude guesses, see below where we try to get the right answers.
985 Note that our guess for MIDI deliberatey tries to overestimate
986 by a little. It would be nicer if we could get the actual
987 size from a port, but we have to use this estimate in the
988 event that there are no MIDI ports currently. If there are
989 the value will be adjusted below.
992 _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof (Sample);
993 _raw_buffer_sizes[DataType::MIDI] = nframes * 4 - (nframes/2);
996 engine.buffer_size_change (nframes);
1002 JACKAudioBackend::disconnected (const char* why)
1004 bool was_running = _running;
1007 _current_buffer_size = 0;
1008 _current_sample_rate = 0;
1011 engine.halted_callback (why); /* EMIT SIGNAL */
1016 JACKAudioBackend::cpu_load() const
1018 GET_PRIVATE_JACK_POINTER_RET(_priv_jack,0);
1019 return jack_cpu_load (_priv_jack);
1023 JACKAudioBackend::update_latencies ()
1025 GET_PRIVATE_JACK_POINTER (_priv_jack);
1026 jack_recompute_total_latencies (_priv_jack);
1030 JACKAudioBackend::n_physical (unsigned long flags) const
1034 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, c);
1036 const char ** ports = jack_get_ports (_priv_jack, NULL, NULL, JackPortIsPhysical | flags);
1039 for (uint32_t i = 0; ports[i]; ++i) {
1040 if (!strstr (ports[i], "Midi-Through")) {
1041 DataType t = port_data_type (jack_port_by_name (_priv_jack, ports[i]));
1042 if (t != DataType::NIL) {
1043 c.set (t, c.get (t) + 1);
1055 JACKAudioBackend::can_change_sample_rate_when_running () const
1061 JACKAudioBackend::can_change_buffer_size_when_running () const
1067 JACKAudioBackend::control_app_name () const
1069 /* Since JACK/ALSA really don't provide particularly integrated support
1070 for the idea of a control app to be used to control a device,
1071 allow the user to take some control themselves if necessary.
1074 const char* env_value = g_getenv ("ARDOUR_DEVICE_CONTROL_APP");
1078 if (_target_driver.empty() || _target_device.empty()) {
1082 if (_target_driver == "ALSA") {
1084 if (_target_device == "Hammerfall DSP") {
1085 appname = "hdspconf";
1086 } else if (_target_device == "M Audio Delta 1010") {
1087 appname = "mudita24";
1091 appname = env_value;
1098 JACKAudioBackend::launch_control_app ()
1100 string appname = control_app_name();
1102 if (appname.empty()) {
1103 error << string_compose (_("There is no control application for the device \"%1\""), _target_device) << endmsg;
1107 std::list<string> args;
1108 args.push_back (appname);
1109 Glib::spawn_async ("", args, Glib::SPAWN_SEARCH_PATH);