2 Copyright (C) 2002 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.
27 #include <glibmm/timer.h>
28 #include "pbd/pthread_utils.h"
29 #include "pbd/stacktrace.h"
30 #include "pbd/unknown_type.h"
32 #include "midi++/jack.h"
34 #include "ardour/amp.h"
35 #include "ardour/audio_port.h"
36 #include "ardour/audioengine.h"
37 #include "ardour/buffer.h"
38 #include "ardour/buffer_set.h"
39 #include "ardour/cycle_timer.h"
40 #include "ardour/delivery.h"
41 #include "ardour/event_type_map.h"
42 #include "ardour/internal_return.h"
43 #include "ardour/io.h"
44 #include "ardour/meter.h"
45 #include "ardour/midi_port.h"
46 #include "ardour/process_thread.h"
47 #include "ardour/port.h"
48 #include "ardour/port_set.h"
49 #include "ardour/session.h"
50 #include "ardour/timestamps.h"
51 #include "ardour/utils.h"
56 using namespace ARDOUR;
59 gint AudioEngine::m_meter_exit;
60 AudioEngine* AudioEngine::_instance = 0;
62 #define GET_PRIVATE_JACK_POINTER(j) jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return; }
63 #define GET_PRIVATE_JACK_POINTER_RET(j,r) jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return r; }
65 AudioEngine::AudioEngine (string client_name, string session_uuid)
68 _instance = this; /* singleton */
70 session_remove_pending = false;
73 last_monitor_check = 0;
74 monitor_check_interval = max_frames;
75 _processed_frames = 0;
80 _freewheeling = false;
84 g_atomic_int_set (&m_meter_exit, 0);
86 if (connect_to_jack (client_name, session_uuid)) {
87 throw NoBackendAvailable ();
90 Port::set_engine (this);
92 // Initialize parameter metadata (e.g. ranges)
93 Evoral::Parameter p(NullAutomation);
94 p = EventTypeMap::instance().new_parameter(NullAutomation);
95 p = EventTypeMap::instance().new_parameter(GainAutomation);
96 p = EventTypeMap::instance().new_parameter(PanAutomation);
97 p = EventTypeMap::instance().new_parameter(PluginAutomation);
98 p = EventTypeMap::instance().new_parameter(SoloAutomation);
99 p = EventTypeMap::instance().new_parameter(MuteAutomation);
100 p = EventTypeMap::instance().new_parameter(MidiCCAutomation);
101 p = EventTypeMap::instance().new_parameter(MidiPgmChangeAutomation);
102 p = EventTypeMap::instance().new_parameter(MidiPitchBenderAutomation);
103 p = EventTypeMap::instance().new_parameter(MidiChannelPressureAutomation);
104 p = EventTypeMap::instance().new_parameter(FadeInAutomation);
105 p = EventTypeMap::instance().new_parameter(FadeOutAutomation);
106 p = EventTypeMap::instance().new_parameter(EnvelopeAutomation);
107 p = EventTypeMap::instance().new_parameter(MidiCCAutomation);
110 AudioEngine::~AudioEngine ()
113 Glib::Mutex::Lock tm (_process_lock);
114 session_removed.signal ();
117 jack_client_close (_jack);
121 stop_metering_thread ();
126 AudioEngine::jack() const
132 _thread_init_callback (void * /*arg*/)
134 /* make sure that anybody who needs to know about this thread
138 pthread_set_name (X_("audioengine"));
140 PBD::notify_gui_about_thread_creation ("gui", pthread_self(), X_("Audioengine"), 4096);
141 PBD::notify_gui_about_thread_creation ("midiui", pthread_self(), X_("Audioengine"), 128);
143 SessionEvent::create_per_thread_pool (X_("Audioengine"), 512);
145 MIDI::JACK_MidiPort::set_process_thread (pthread_self());
148 typedef void (*_JackInfoShutdownCallback)(jack_status_t code, const char* reason, void *arg);
150 static void (*on_info_shutdown)(jack_client_t*, _JackInfoShutdownCallback, void *);
151 extern void jack_on_info_shutdown (jack_client_t*, _JackInfoShutdownCallback, void *) __attribute__((weak));
153 static void check_jack_symbols () __attribute__((constructor));
155 void check_jack_symbols ()
157 /* use weak linking to see if we really have various late-model JACK function */
158 on_info_shutdown = jack_on_info_shutdown;
162 ardour_jack_error (const char* msg)
164 error << "JACK: " << msg << endmsg;
168 AudioEngine::start ()
170 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
174 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
177 BootMessage (_("Connect session to engine"));
179 _session->set_block_size (blocksize);
180 _session->set_frame_rate (jack_get_sample_rate (_priv_jack));
182 /* page in as much of the session process code as we
183 can before we really start running.
186 _session->process (blocksize);
187 _session->process (blocksize);
188 _session->process (blocksize);
189 _session->process (blocksize);
190 _session->process (blocksize);
191 _session->process (blocksize);
192 _session->process (blocksize);
193 _session->process (blocksize);
196 _processed_frames = 0;
197 last_monitor_check = 0;
199 if (on_info_shutdown) {
200 jack_on_info_shutdown (_priv_jack, halted_info, this);
202 jack_on_shutdown (_priv_jack, halted, this);
204 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
205 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
206 // jack_set_process_callback (_priv_jack, _process_callback, this);
207 jack_set_process_thread (_priv_jack, _process_thread, this);
208 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
209 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
210 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
211 #ifdef HAVE_JACK_SESSION
212 if( jack_set_session_callback )
213 jack_set_session_callback (_priv_jack, _session_callback, this);
215 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
216 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
217 jack_set_port_registration_callback (_priv_jack, _registration_callback, this);
218 jack_set_port_connect_callback (_priv_jack, _connect_callback, this);
220 if (_session && _session->config.get_jack_time_master()) {
221 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
224 jack_set_error_function (ardour_jack_error);
226 if (jack_activate (_priv_jack) == 0) {
229 Running(); /* EMIT SIGNAL */
231 // error << _("cannot activate JACK client") << endmsg;
234 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
237 return _running ? 0 : -1;
241 AudioEngine::stop (bool forever)
243 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
247 disconnect_from_jack ();
249 jack_deactivate (_priv_jack);
250 Stopped(); /* EMIT SIGNAL */
251 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
255 return _running ? -1 : 0;
260 AudioEngine::get_sync_offset (nframes_t& offset) const
263 #ifdef HAVE_JACK_VIDEO_SUPPORT
265 GET_PRIVATE_JACK_POINTER_RET (_jack, false);
270 (void) jack_transport_query (_priv_jack, &pos);
272 if (pos.valid & JackVideoFrameOffset) {
273 offset = pos.video_offset;
286 AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
287 jack_position_t* pos, int new_position, void *arg)
289 static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
293 AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
294 jack_position_t* pos, int new_position)
296 if (_jack && _session && _session->synced_to_jack()) {
297 _session->jack_timebase_callback (state, nframes, pos, new_position);
302 AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
304 return static_cast<AudioEngine*> (arg)->jack_sync_callback (state, pos);
308 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
310 if (_jack && _session) {
311 return _session->jack_sync_callback (state, pos);
318 AudioEngine::_xrun_callback (void *arg)
320 AudioEngine* ae = static_cast<AudioEngine*> (arg);
321 if (ae->connected()) {
322 ae->Xrun (); /* EMIT SIGNAL */
327 #ifdef HAVE_JACK_SESSION
329 AudioEngine::_session_callback (jack_session_event_t *event, void *arg)
331 printf( "helo.... " );
332 AudioEngine* ae = static_cast<AudioEngine*> (arg);
333 if (ae->connected()) {
334 ae->JackSessionEvent ( event ); /* EMIT SIGNAL */
339 AudioEngine::_graph_order_callback (void *arg)
341 AudioEngine* ae = static_cast<AudioEngine*> (arg);
342 if (ae->connected()) {
343 ae->GraphReordered (); /* EMIT SIGNAL */
348 /** Wrapped which is called by JACK as its process callback. It is just
349 * here to get us back into C++ land by calling AudioEngine::process_callback()
350 * @param nframes Number of frames passed by JACK.
351 * @param arg User argument passed by JACK, which will be the AudioEngine*.
354 AudioEngine::_process_callback (nframes_t nframes, void *arg)
356 return static_cast<AudioEngine *> (arg)->process_callback (nframes);
360 AudioEngine::_process_thread (void *arg)
362 return static_cast<AudioEngine *> (arg)->process_thread ();
366 AudioEngine::_freewheel_callback (int onoff, void *arg)
368 static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
372 AudioEngine::_registration_callback (jack_port_id_t /*id*/, int /*reg*/, void* arg)
374 AudioEngine* ae = static_cast<AudioEngine*> (arg);
375 ae->PortRegisteredOrUnregistered (); /* EMIT SIGNAL */
379 AudioEngine::_connect_callback (jack_port_id_t /*id_a*/, jack_port_id_t /*id_b*/, int /*conn*/, void* arg)
381 AudioEngine* ae = static_cast<AudioEngine*> (arg);
382 ae->PortConnectedOrDisconnected (); /* EMIT SIGNAL */
386 AudioEngine::split_cycle (nframes_t offset)
388 /* caller must hold process lock */
390 Port::increment_port_offset (offset);
392 /* tell all Ports that we're going to start a new (split) cycle */
394 boost::shared_ptr<Ports> p = ports.reader();
396 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
397 (*i)->cycle_split ();
402 AudioEngine::finish_process_cycle (int status)
404 GET_PRIVATE_JACK_POINTER(_jack);
405 jack_cycle_signal (_jack, 0);
409 AudioEngine::process_thread ()
411 /* JACK doesn't do this for us when we use the wait API
414 _thread_init_callback (0);
416 _main_thread = new ProcessThread;
419 GET_PRIVATE_JACK_POINTER_RET(_jack,0);
420 jack_nframes_t nframes = jack_cycle_wait (_jack);
422 if (process_callback (nframes)) {
426 finish_process_cycle (0);
432 /** Method called by JACK (via _process_callback) which says that there
433 * is work to be done.
434 * @param nframes Number of frames to process.
437 AudioEngine::process_callback (nframes_t nframes)
439 GET_PRIVATE_JACK_POINTER_RET(_jack,0);
440 // CycleTimer ct ("AudioEngine::process");
441 Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
443 /// The number of frames that will have been processed when we've finished
444 nframes_t next_processed_frames;
446 /* handle wrap around of total frames counter */
448 if (max_frames - _processed_frames < nframes) {
449 next_processed_frames = nframes - (max_frames - _processed_frames);
451 next_processed_frames = _processed_frames + nframes;
454 if (!tm.locked() || _session == 0) {
455 /* return having done nothing */
456 _processed_frames = next_processed_frames;
460 if (session_remove_pending) {
461 /* perform the actual session removal */
463 session_remove_pending = false;
464 session_removed.signal();
465 _processed_frames = next_processed_frames;
469 /* tell all relevant objects that we're starting a new cycle */
471 Delivery::CycleStart (nframes);
472 Port::set_port_offset (0);
473 InternalReturn::CycleStart (nframes);
475 /* tell all Ports that we're starting a new cycle */
477 boost::shared_ptr<Ports> p = ports.reader();
479 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
480 (*i)->cycle_start (nframes);
484 /* emit the Freewheel signal and stop freewheeling in the event of trouble
485 * the indirection is to pick up the return value of the signal.
487 if (*Freewheel (nframes)) {
488 jack_set_freewheel (_priv_jack, false);
493 _session->process (nframes);
503 _processed_frames = next_processed_frames;
507 if (last_monitor_check + monitor_check_interval < next_processed_frames) {
509 boost::shared_ptr<Ports> p = ports.reader();
511 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
516 if (port->_last_monitor != (x = port->monitoring_input ())) {
517 port->_last_monitor = x;
518 /* XXX I think this is dangerous, due to
519 a likely mutex in the signal handlers ...
521 port->MonitorInputChanged (x); /* EMIT SIGNAL */
524 last_monitor_check = next_processed_frames;
527 if (_session->silent()) {
529 boost::shared_ptr<Ports> p = ports.reader();
531 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
535 if (port->sends_output()) {
536 port->get_buffer(nframes).silence(nframes);
543 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
544 (*i)->cycle_end (nframes);
547 _processed_frames = next_processed_frames;
552 AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
554 return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
558 AudioEngine::jack_sample_rate_callback (nframes_t nframes)
560 _frame_rate = nframes;
561 _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
563 /* check for monitor input change every 1/10th of second */
565 monitor_check_interval = nframes / 10;
566 last_monitor_check = 0;
569 _session->set_frame_rate (nframes);
572 SampleRateChanged (nframes); /* EMIT SIGNAL */
578 AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
580 return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
584 AudioEngine::jack_bufsize_callback (nframes_t nframes)
586 _buffer_size = nframes;
587 _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof(float);
588 cout << "FIXME: Assuming maximum MIDI buffer size " << nframes * 4 << "bytes" << endl;
589 _raw_buffer_sizes[DataType::MIDI] = nframes * 4;
590 _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
591 last_monitor_check = 0;
593 boost::shared_ptr<Ports> p = ports.reader();
595 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
600 _session->set_block_size (_buffer_size);
607 AudioEngine::stop_metering_thread ()
609 if (m_meter_thread) {
610 g_atomic_int_set (&m_meter_exit, 1);
611 m_meter_thread->join ();
617 AudioEngine::start_metering_thread ()
619 if (m_meter_thread == 0) {
620 g_atomic_int_set (&m_meter_exit, 0);
621 m_meter_thread = Glib::Thread::create (boost::bind (&AudioEngine::meter_thread, this),
622 500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
627 AudioEngine::meter_thread ()
629 pthread_set_name (X_("meter"));
632 Glib::usleep (10000); /* 1/100th sec interval */
633 if (g_atomic_int_get(&m_meter_exit)) {
641 AudioEngine::set_session (Session *s)
643 Glib::Mutex::Lock pl (_process_lock);
645 SessionHandlePtr::set_session (s);
649 start_metering_thread ();
651 nframes_t blocksize = jack_get_buffer_size (_jack);
653 /* page in as much of the session process code as we
654 can before we really start running.
657 boost::shared_ptr<Ports> p = ports.reader();
659 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
660 (*i)->cycle_start (blocksize);
663 _session->process (blocksize);
664 _session->process (blocksize);
665 _session->process (blocksize);
666 _session->process (blocksize);
667 _session->process (blocksize);
668 _session->process (blocksize);
669 _session->process (blocksize);
670 _session->process (blocksize);
672 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
673 (*i)->cycle_end (blocksize);
679 AudioEngine::remove_session ()
681 Glib::Mutex::Lock lm (_process_lock);
685 stop_metering_thread ();
688 session_remove_pending = true;
689 session_removed.wait(_process_lock);
693 SessionHandlePtr::set_session (0);
700 AudioEngine::port_registration_failure (const std::string& portname)
702 GET_PRIVATE_JACK_POINTER (_jack);
703 string full_portname = jack_client_name;
704 full_portname += ':';
705 full_portname += portname;
708 jack_port_t* p = jack_port_by_name (_priv_jack, full_portname.c_str());
712 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
714 reason = string_compose (_("No more JACK ports are available. You will need to stop %1 and restart JACK with ports if you need this many tracks."), PROGRAM_NAME);
717 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
721 AudioEngine::register_port (DataType dtype, const string& portname, bool input)
726 if (dtype == DataType::AUDIO) {
727 newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput));
728 } else if (dtype == DataType::MIDI) {
729 newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput));
731 throw PortRegistrationFailure("unable to create port (unknown type)");
734 size_t& old_buffer_size = _raw_buffer_sizes[newport->type()];
735 size_t port_buffer_size = newport->raw_buffer_size(0);
736 if (port_buffer_size > old_buffer_size) {
737 old_buffer_size = port_buffer_size;
740 RCUWriter<Ports> writer (ports);
741 boost::shared_ptr<Ports> ps = writer.get_copy ();
742 ps->insert (ps->begin(), newport);
744 /* writer goes out of scope, forces update */
749 catch (PortRegistrationFailure& err) {
751 } catch (std::exception& e) {
752 throw PortRegistrationFailure(string_compose(
753 _("unable to create port: %1"), e.what()).c_str());
755 throw PortRegistrationFailure("unable to create port (unknown error)");
760 AudioEngine::register_input_port (DataType type, const string& portname)
762 return register_port (type, portname, true);
766 AudioEngine::register_output_port (DataType type, const string& portname)
768 return register_port (type, portname, false);
772 AudioEngine::unregister_port (Port& port)
774 /* caller must hold process lock */
777 /* probably happening when the engine has been halted by JACK,
778 in which case, there is nothing we can do here.
784 RCUWriter<Ports> writer (ports);
785 boost::shared_ptr<Ports> ps = writer.get_copy ();
787 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
795 /* writer goes out of scope, forces update */
802 AudioEngine::connect (const string& source, const string& destination)
804 /* caller must hold process lock */
810 fatal << _("connect called before engine was started") << endmsg;
817 string s = make_port_name_non_relative (source);
818 string d = make_port_name_non_relative (destination);
821 Port* src = get_port_by_name_locked (s);
822 Port* dst = get_port_by_name_locked (d);
825 ret = src->connect (d);
827 ret = dst->connect (s);
829 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
834 /* already exists - no error, no warning */
835 } else if (ret < 0) {
836 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
837 source, s, destination, d)
845 AudioEngine::disconnect (const string& source, const string& destination)
847 /* caller must hold process lock */
853 fatal << _("disconnect called before engine was started") << endmsg;
860 string s = make_port_name_non_relative (source);
861 string d = make_port_name_non_relative (destination);
863 Port* src = get_port_by_name_locked (s);
864 Port* dst = get_port_by_name_locked (d);
867 ret = src->disconnect (d);
869 ret = dst->disconnect (s);
871 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
878 AudioEngine::disconnect (Port& port)
880 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
884 fatal << _("disconnect called before engine was started") << endmsg;
891 return port.disconnect_all ();
895 AudioEngine::frame_rate () const
897 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
898 if (_frame_rate == 0) {
899 return (_frame_rate = jack_get_sample_rate (_priv_jack));
906 AudioEngine::raw_buffer_size (DataType t)
908 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
909 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
913 AudioEngine::frames_per_cycle () const
915 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
916 if (_buffer_size == 0) {
917 return (_buffer_size = jack_get_buffer_size (_jack));
923 /** @param name Full name of port (including prefix:)
924 * @return Corresponding Port*, or 0. This object remains the property of the AudioEngine
925 * so must not be deleted.
928 AudioEngine::get_port_by_name (const string& portname)
931 if (portname.find_first_of (':') == string::npos) {
932 s = make_port_name_non_relative (portname);
937 Glib::Mutex::Lock lm (_process_lock);
938 return get_port_by_name_locked (s);
942 AudioEngine::get_port_by_name_locked (const string& portname)
944 /* caller must hold process lock */
948 fatal << _("get_port_by_name_locked() called before engine was started") << endmsg;
955 if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
956 /* not an ardour: port */
960 std::string const rel = make_port_name_relative (portname);
962 boost::shared_ptr<Ports> pr = ports.reader();
964 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
965 if (rel == (*i)->name()) {
974 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
976 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
979 fatal << _("get_ports called before engine was started") << endmsg;
985 return jack_get_ports (_priv_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
989 AudioEngine::halted_info (jack_status_t code, const char* reason, void *arg)
991 /* called from jack shutdown handler */
993 AudioEngine* ae = static_cast<AudioEngine *> (arg);
994 bool was_running = ae->_running;
996 ae->stop_metering_thread ();
998 ae->_running = false;
999 ae->_buffer_size = 0;
1000 ae->_frame_rate = 0;
1004 #ifdef HAVE_JACK_ON_INFO_SHUTDOWN
1006 case JackBackendError:
1007 ae->Halted(reason); /* EMIT SIGNAL */
1010 ae->Halted(""); /* EMIT SIGNAL */
1013 ae->Halted(""); /* EMIT SIGNAL */
1019 AudioEngine::halted (void *arg)
1021 cerr << "HALTED by JACK\n";
1023 /* called from jack shutdown handler */
1025 AudioEngine* ae = static_cast<AudioEngine *> (arg);
1026 bool was_running = ae->_running;
1028 ae->stop_metering_thread ();
1030 ae->_running = false;
1031 ae->_buffer_size = 0;
1032 ae->_frame_rate = 0;
1036 ae->Halted(""); /* EMIT SIGNAL */
1037 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
1042 AudioEngine::died ()
1044 /* called from a signal handler for SIGPIPE */
1046 stop_metering_thread ();
1055 AudioEngine::can_request_hardware_monitoring ()
1057 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
1058 const char ** ports;
1060 if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
1071 AudioEngine::n_physical_outputs (DataType type) const
1073 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
1074 const char ** ports;
1077 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1081 for (i = 0; ports[i]; ++i) {}
1088 AudioEngine::n_physical_inputs (DataType type) const
1090 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
1091 const char ** ports;
1094 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
1098 for (i = 0; ports[i]; ++i) {}
1105 AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
1107 GET_PRIVATE_JACK_POINTER (_jack);
1108 const char ** ports;
1110 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
1115 for (uint32_t i = 0; ports[i]; ++i) {
1116 ins.push_back (ports[i]);
1123 AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
1125 GET_PRIVATE_JACK_POINTER (_jack);
1126 const char ** ports;
1129 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1133 for (i = 0; ports[i]; ++i) {
1134 outs.push_back (ports[i]);
1140 AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
1142 GET_PRIVATE_JACK_POINTER_RET (_jack,"");
1143 const char ** ports;
1147 assert(type != DataType::NIL);
1149 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|flag)) == 0) {
1153 for (i = 0; i < n && ports[i]; ++i) {}
1159 free ((const char **) ports);
1165 AudioEngine::update_total_latency (const Port& port)
1167 port.recompute_total_latency ();
1171 AudioEngine::transport_stop ()
1173 GET_PRIVATE_JACK_POINTER (_jack);
1174 jack_transport_stop (_priv_jack);
1178 AudioEngine::transport_start ()
1180 GET_PRIVATE_JACK_POINTER (_jack);
1181 jack_transport_start (_priv_jack);
1185 AudioEngine::transport_locate (nframes_t where)
1187 GET_PRIVATE_JACK_POINTER (_jack);
1188 // cerr << "tell JACK to locate to " << where << endl;
1189 jack_transport_locate (_priv_jack, where);
1192 AudioEngine::TransportState
1193 AudioEngine::transport_state ()
1195 GET_PRIVATE_JACK_POINTER_RET (_jack, ((TransportState) JackTransportStopped));
1196 jack_position_t pos;
1197 return (TransportState) jack_transport_query (_priv_jack, &pos);
1201 AudioEngine::reset_timebase ()
1203 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1205 if (_session->config.get_jack_time_master()) {
1206 return jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1208 return jack_release_timebase (_jack);
1215 AudioEngine::freewheel (bool onoff)
1217 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1219 if (onoff != _freewheeling) {
1220 return jack_set_freewheel (_priv_jack, onoff);
1223 /* already doing what has been asked for */
1229 AudioEngine::remove_all_ports ()
1231 /* process lock MUST be held */
1234 RCUWriter<Ports> writer (ports);
1235 boost::shared_ptr<Ports> ps = writer.get_copy ();
1237 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
1244 /* clear dead wood list too */
1250 AudioEngine::connect_to_jack (string client_name, string session_uuid)
1252 jack_options_t options = JackNullOption;
1253 jack_status_t status;
1254 const char *server_name = NULL;
1256 jack_client_name = client_name; /* might be reset below */
1257 #ifdef HAVE_JACK_SESSION
1258 if (! session_uuid.empty())
1259 _jack = jack_client_open (jack_client_name.c_str(), JackSessionID, &status, session_uuid.c_str());
1262 _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
1264 if (_jack == NULL) {
1265 // error message is not useful here
1269 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1271 if (status & JackNameNotUnique) {
1272 jack_client_name = jack_get_client_name (_priv_jack);
1279 AudioEngine::disconnect_from_jack ()
1281 GET_PRIVATE_JACK_POINTER_RET (_jack, 0);
1284 stop_metering_thread ();
1288 Glib::Mutex::Lock lm (_process_lock);
1289 jack_client_close (_priv_jack);
1295 _raw_buffer_sizes.clear();
1299 Stopped(); /* EMIT SIGNAL */
1300 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
1307 AudioEngine::reconnect_to_jack ()
1310 disconnect_from_jack ();
1311 /* XXX give jackd a chance */
1312 Glib::usleep (250000);
1315 if (connect_to_jack (jack_client_name, "")) {
1316 error << _("failed to connect to JACK") << endmsg;
1322 boost::shared_ptr<Ports> p = ports.reader ();
1324 for (i = p->begin(); i != p->end(); ++i) {
1325 if ((*i)->reestablish ()) {
1330 if (i != p->end()) {
1332 remove_all_ports ();
1336 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
1339 _session->reset_jack_connection (_priv_jack);
1340 jack_bufsize_callback (jack_get_buffer_size (_priv_jack));
1341 _session->set_frame_rate (jack_get_sample_rate (_priv_jack));
1344 last_monitor_check = 0;
1346 jack_on_shutdown (_priv_jack, halted, this);
1347 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
1348 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
1349 // jack_set_process_callback (_priv_jack, _process_callback, this);
1350 jack_set_process_thread (_priv_jack, _process_thread, this);
1351 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
1352 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
1353 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
1354 #ifdef HAVE_JACK_SESSION
1355 if( jack_set_session_callback )
1356 jack_set_session_callback (_priv_jack, _session_callback, this);
1358 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
1359 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
1361 if (_session && _session->config.get_jack_time_master()) {
1362 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1365 if (jack_activate (_priv_jack) == 0) {
1372 /* re-establish connections */
1374 for (i = p->begin(); i != p->end(); ++i) {
1378 Running (); /* EMIT SIGNAL*/
1380 start_metering_thread ();
1386 AudioEngine::request_buffer_size (nframes_t nframes)
1388 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1390 if (nframes == jack_get_buffer_size (_priv_jack)) {
1394 return jack_set_buffer_size (_priv_jack, nframes);
1398 AudioEngine::update_total_latencies ()
1400 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1401 GET_PRIVATE_JACK_POINTER (_jack);
1402 jack_recompute_total_latencies (_priv_jack);
1407 AudioEngine::make_port_name_relative (string portname)
1409 string::size_type len;
1410 string::size_type n;
1412 len = portname.length();
1414 for (n = 0; n < len; ++n) {
1415 if (portname[n] == ':') {
1420 if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
1421 return portname.substr (n+1);
1428 AudioEngine::make_port_name_non_relative (string portname)
1432 if (portname.find_first_of (':') != string::npos) {
1436 str = jack_client_name;
1444 AudioEngine::is_realtime () const
1446 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
1447 return jack_is_realtime (_priv_jack);