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);
236 jack_port_t* midi_port = jack_port_register (_priv_jack, "m", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
238 error << _("Cannot create temporary MIDI port to determine MIDI buffer size") << endmsg;
240 _raw_buffer_sizes[DataType::MIDI] = jack_midi_max_event_size (jack_port_get_buffer(midi_port, blocksize));
241 cerr << "MIDI port buffers = " << _raw_buffer_sizes[DataType::MIDI] << endl;
242 jack_port_unregister (_priv_jack, midi_port);
246 return _running ? 0 : -1;
250 AudioEngine::stop (bool forever)
252 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
256 disconnect_from_jack ();
258 jack_deactivate (_priv_jack);
259 Stopped(); /* EMIT SIGNAL */
260 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
264 return _running ? -1 : 0;
269 AudioEngine::get_sync_offset (nframes_t& offset) const
272 #ifdef HAVE_JACK_VIDEO_SUPPORT
274 GET_PRIVATE_JACK_POINTER_RET (_jack, false);
279 (void) jack_transport_query (_priv_jack, &pos);
281 if (pos.valid & JackVideoFrameOffset) {
282 offset = pos.video_offset;
295 AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
296 jack_position_t* pos, int new_position, void *arg)
298 static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
302 AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
303 jack_position_t* pos, int new_position)
305 if (_jack && _session && _session->synced_to_jack()) {
306 _session->jack_timebase_callback (state, nframes, pos, new_position);
311 AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
313 return static_cast<AudioEngine*> (arg)->jack_sync_callback (state, pos);
317 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
319 if (_jack && _session) {
320 return _session->jack_sync_callback (state, pos);
327 AudioEngine::_xrun_callback (void *arg)
329 AudioEngine* ae = static_cast<AudioEngine*> (arg);
330 if (ae->connected()) {
331 ae->Xrun (); /* EMIT SIGNAL */
336 #ifdef HAVE_JACK_SESSION
338 AudioEngine::_session_callback (jack_session_event_t *event, void *arg)
340 printf( "helo.... " );
341 AudioEngine* ae = static_cast<AudioEngine*> (arg);
342 if (ae->connected()) {
343 ae->JackSessionEvent ( event ); /* EMIT SIGNAL */
348 AudioEngine::_graph_order_callback (void *arg)
350 AudioEngine* ae = static_cast<AudioEngine*> (arg);
351 if (ae->connected()) {
352 ae->GraphReordered (); /* EMIT SIGNAL */
357 /** Wrapped which is called by JACK as its process callback. It is just
358 * here to get us back into C++ land by calling AudioEngine::process_callback()
359 * @param nframes Number of frames passed by JACK.
360 * @param arg User argument passed by JACK, which will be the AudioEngine*.
363 AudioEngine::_process_callback (nframes_t nframes, void *arg)
365 return static_cast<AudioEngine *> (arg)->process_callback (nframes);
369 AudioEngine::_process_thread (void *arg)
371 return static_cast<AudioEngine *> (arg)->process_thread ();
375 AudioEngine::_freewheel_callback (int onoff, void *arg)
377 static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
381 AudioEngine::_registration_callback (jack_port_id_t /*id*/, int /*reg*/, void* arg)
383 AudioEngine* ae = static_cast<AudioEngine*> (arg);
384 ae->PortRegisteredOrUnregistered (); /* EMIT SIGNAL */
388 AudioEngine::_connect_callback (jack_port_id_t /*id_a*/, jack_port_id_t /*id_b*/, int /*conn*/, void* arg)
390 AudioEngine* ae = static_cast<AudioEngine*> (arg);
391 ae->PortConnectedOrDisconnected (); /* EMIT SIGNAL */
395 AudioEngine::split_cycle (nframes_t offset)
397 /* caller must hold process lock */
399 Port::increment_port_offset (offset);
401 /* tell all Ports that we're going to start a new (split) cycle */
403 boost::shared_ptr<Ports> p = ports.reader();
405 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
406 (*i)->cycle_split ();
411 AudioEngine::finish_process_cycle (int status)
413 GET_PRIVATE_JACK_POINTER(_jack);
414 jack_cycle_signal (_jack, 0);
418 AudioEngine::process_thread ()
420 /* JACK doesn't do this for us when we use the wait API
423 _thread_init_callback (0);
425 _main_thread = new ProcessThread;
428 GET_PRIVATE_JACK_POINTER_RET(_jack,0);
429 jack_nframes_t nframes = jack_cycle_wait (_jack);
431 if (process_callback (nframes)) {
432 cerr << "--- process\n";
436 finish_process_cycle (0);
442 /** Method called by JACK (via _process_callback) which says that there
443 * is work to be done.
444 * @param nframes Number of frames to process.
447 AudioEngine::process_callback (nframes_t nframes)
449 GET_PRIVATE_JACK_POINTER_RET(_jack,0);
450 // CycleTimer ct ("AudioEngine::process");
451 Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
453 /// The number of frames that will have been processed when we've finished
454 nframes_t next_processed_frames;
456 /* handle wrap around of total frames counter */
458 if (max_frames - _processed_frames < nframes) {
459 next_processed_frames = nframes - (max_frames - _processed_frames);
461 next_processed_frames = _processed_frames + nframes;
464 if (!tm.locked() || _session == 0) {
465 /* return having done nothing */
466 _processed_frames = next_processed_frames;
470 if (session_remove_pending) {
471 /* perform the actual session removal */
473 session_remove_pending = false;
474 session_removed.signal();
475 _processed_frames = next_processed_frames;
479 /* tell all relevant objects that we're starting a new cycle */
481 Delivery::CycleStart (nframes);
482 Port::set_port_offset (0);
483 InternalReturn::CycleStart (nframes);
485 /* tell all Ports that we're starting a new cycle */
487 boost::shared_ptr<Ports> p = ports.reader();
489 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
490 (*i)->cycle_start (nframes);
494 /* emit the Freewheel signal and stop freewheeling in the event of trouble
496 boost::optional<int> r = Freewheel (nframes);
497 if (r.get_value_or (0)) {
498 jack_set_freewheel (_priv_jack, false);
503 _session->process (nframes);
513 _processed_frames = next_processed_frames;
517 if (last_monitor_check + monitor_check_interval < next_processed_frames) {
519 boost::shared_ptr<Ports> p = ports.reader();
521 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
526 if (port->_last_monitor != (x = port->monitoring_input ())) {
527 port->_last_monitor = x;
528 /* XXX I think this is dangerous, due to
529 a likely mutex in the signal handlers ...
531 port->MonitorInputChanged (x); /* EMIT SIGNAL */
534 last_monitor_check = next_processed_frames;
537 if (_session->silent()) {
539 boost::shared_ptr<Ports> p = ports.reader();
541 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
545 if (port->sends_output()) {
546 port->get_buffer(nframes).silence(nframes);
553 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
554 (*i)->cycle_end (nframes);
557 _processed_frames = next_processed_frames;
562 AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
564 return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
568 AudioEngine::jack_sample_rate_callback (nframes_t nframes)
570 _frame_rate = nframes;
571 _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
573 /* check for monitor input change every 1/10th of second */
575 monitor_check_interval = nframes / 10;
576 last_monitor_check = 0;
579 _session->set_frame_rate (nframes);
582 SampleRateChanged (nframes); /* EMIT SIGNAL */
588 AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
590 return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
594 AudioEngine::jack_bufsize_callback (nframes_t nframes)
596 bool need_midi_size = true;
597 bool need_audio_size = true;
599 _buffer_size = nframes;
600 _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
601 last_monitor_check = 0;
603 boost::shared_ptr<Ports> p = ports.reader();
605 /* crude guesses, see below where we try to get the right answers.
607 Note that our guess for MIDI deliberatey tries to overestimate
608 by a little. It would be nicer if we could get the actual
609 size from a port, but we have to use this estimate in the
610 event that there are no MIDI ports currently. If there are
611 the value will be adjusted below.
614 _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof (Sample);
615 _raw_buffer_sizes[DataType::MIDI] = nframes * 4 - (nframes/2);
617 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
619 if (need_audio_size && (*i)->type() == DataType::AUDIO) {
620 _raw_buffer_sizes[DataType::AUDIO] = (*i)->raw_buffer_size (nframes);
621 need_audio_size = false;
625 if (need_midi_size && (*i)->type() == DataType::MIDI) {
626 _raw_buffer_sizes[DataType::MIDI] = (*i)->raw_buffer_size (nframes);
627 need_midi_size = false;
634 _session->set_block_size (_buffer_size);
641 AudioEngine::stop_metering_thread ()
643 if (m_meter_thread) {
644 g_atomic_int_set (&m_meter_exit, 1);
645 m_meter_thread->join ();
651 AudioEngine::start_metering_thread ()
653 if (m_meter_thread == 0) {
654 g_atomic_int_set (&m_meter_exit, 0);
655 m_meter_thread = Glib::Thread::create (boost::bind (&AudioEngine::meter_thread, this),
656 500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
661 AudioEngine::meter_thread ()
663 pthread_set_name (X_("meter"));
666 Glib::usleep (10000); /* 1/100th sec interval */
667 if (g_atomic_int_get(&m_meter_exit)) {
675 AudioEngine::set_session (Session *s)
677 Glib::Mutex::Lock pl (_process_lock);
679 SessionHandlePtr::set_session (s);
683 start_metering_thread ();
685 nframes_t blocksize = jack_get_buffer_size (_jack);
687 /* page in as much of the session process code as we
688 can before we really start running.
691 boost::shared_ptr<Ports> p = ports.reader();
693 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
694 (*i)->cycle_start (blocksize);
697 _session->process (blocksize);
698 _session->process (blocksize);
699 _session->process (blocksize);
700 _session->process (blocksize);
701 _session->process (blocksize);
702 _session->process (blocksize);
703 _session->process (blocksize);
704 _session->process (blocksize);
706 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
707 (*i)->cycle_end (blocksize);
713 AudioEngine::remove_session ()
715 Glib::Mutex::Lock lm (_process_lock);
719 stop_metering_thread ();
722 session_remove_pending = true;
723 session_removed.wait(_process_lock);
727 SessionHandlePtr::set_session (0);
734 AudioEngine::port_registration_failure (const std::string& portname)
736 GET_PRIVATE_JACK_POINTER (_jack);
737 string full_portname = jack_client_name;
738 full_portname += ':';
739 full_portname += portname;
742 jack_port_t* p = jack_port_by_name (_priv_jack, full_portname.c_str());
746 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
748 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);
751 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
755 AudioEngine::register_port (DataType dtype, const string& portname, bool input)
760 if (dtype == DataType::AUDIO) {
761 newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput));
762 } else if (dtype == DataType::MIDI) {
763 newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput));
765 throw PortRegistrationFailure("unable to create port (unknown type)");
768 size_t& old_buffer_size = _raw_buffer_sizes[newport->type()];
769 size_t port_buffer_size = newport->raw_buffer_size(0);
770 if (port_buffer_size > old_buffer_size) {
771 old_buffer_size = port_buffer_size;
774 RCUWriter<Ports> writer (ports);
775 boost::shared_ptr<Ports> ps = writer.get_copy ();
776 ps->insert (ps->begin(), newport);
778 /* writer goes out of scope, forces update */
783 catch (PortRegistrationFailure& err) {
785 } catch (std::exception& e) {
786 throw PortRegistrationFailure(string_compose(
787 _("unable to create port: %1"), e.what()).c_str());
789 throw PortRegistrationFailure("unable to create port (unknown error)");
794 AudioEngine::register_input_port (DataType type, const string& portname)
796 return register_port (type, portname, true);
800 AudioEngine::register_output_port (DataType type, const string& portname)
802 return register_port (type, portname, false);
806 AudioEngine::unregister_port (Port& port)
808 /* caller must hold process lock */
811 /* probably happening when the engine has been halted by JACK,
812 in which case, there is nothing we can do here.
818 RCUWriter<Ports> writer (ports);
819 boost::shared_ptr<Ports> ps = writer.get_copy ();
821 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
829 /* writer goes out of scope, forces update */
836 AudioEngine::connect (const string& source, const string& destination)
838 /* caller must hold process lock */
844 fatal << _("connect called before engine was started") << endmsg;
851 string s = make_port_name_non_relative (source);
852 string d = make_port_name_non_relative (destination);
855 Port* src = get_port_by_name_locked (s);
856 Port* dst = get_port_by_name_locked (d);
859 ret = src->connect (d);
861 ret = dst->connect (s);
863 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
868 /* already exists - no error, no warning */
869 } else if (ret < 0) {
870 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
871 source, s, destination, d)
879 AudioEngine::disconnect (const string& source, const string& destination)
881 /* caller must hold process lock */
887 fatal << _("disconnect called before engine was started") << endmsg;
894 string s = make_port_name_non_relative (source);
895 string d = make_port_name_non_relative (destination);
897 Port* src = get_port_by_name_locked (s);
898 Port* dst = get_port_by_name_locked (d);
901 ret = src->disconnect (d);
903 ret = dst->disconnect (s);
905 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
912 AudioEngine::disconnect (Port& port)
914 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
918 fatal << _("disconnect called before engine was started") << endmsg;
925 return port.disconnect_all ();
929 AudioEngine::frame_rate () const
931 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
932 if (_frame_rate == 0) {
933 return (_frame_rate = jack_get_sample_rate (_priv_jack));
940 AudioEngine::raw_buffer_size (DataType t)
942 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
943 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
947 AudioEngine::frames_per_cycle () const
949 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
950 if (_buffer_size == 0) {
951 return (_buffer_size = jack_get_buffer_size (_jack));
957 /** @param name Full name of port (including prefix:)
958 * @return Corresponding Port*, or 0. This object remains the property of the AudioEngine
959 * so must not be deleted.
962 AudioEngine::get_port_by_name (const string& portname)
965 if (portname.find_first_of (':') == string::npos) {
966 s = make_port_name_non_relative (portname);
971 Glib::Mutex::Lock lm (_process_lock);
972 return get_port_by_name_locked (s);
976 AudioEngine::get_port_by_name_locked (const string& portname)
978 /* caller must hold process lock */
982 fatal << _("get_port_by_name_locked() called before engine was started") << endmsg;
989 if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
990 /* not an ardour: port */
994 std::string const rel = make_port_name_relative (portname);
996 boost::shared_ptr<Ports> pr = ports.reader();
998 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
999 if (rel == (*i)->name()) {
1008 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
1010 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
1013 fatal << _("get_ports called before engine was started") << endmsg;
1019 return jack_get_ports (_priv_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
1023 AudioEngine::halted_info (jack_status_t code, const char* reason, void *arg)
1025 /* called from jack shutdown handler */
1027 AudioEngine* ae = static_cast<AudioEngine *> (arg);
1028 bool was_running = ae->_running;
1030 ae->stop_metering_thread ();
1032 ae->_running = false;
1033 ae->_buffer_size = 0;
1034 ae->_frame_rate = 0;
1038 #ifdef HAVE_JACK_ON_INFO_SHUTDOWN
1040 case JackBackendError:
1041 ae->Halted(reason); /* EMIT SIGNAL */
1044 ae->Halted(""); /* EMIT SIGNAL */
1047 ae->Halted(""); /* EMIT SIGNAL */
1053 AudioEngine::halted (void *arg)
1055 cerr << "HALTED by JACK\n";
1057 /* called from jack shutdown handler */
1059 AudioEngine* ae = static_cast<AudioEngine *> (arg);
1060 bool was_running = ae->_running;
1062 ae->stop_metering_thread ();
1064 ae->_running = false;
1065 ae->_buffer_size = 0;
1066 ae->_frame_rate = 0;
1070 ae->Halted(""); /* EMIT SIGNAL */
1071 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
1076 AudioEngine::died ()
1078 /* called from a signal handler for SIGPIPE */
1080 stop_metering_thread ();
1089 AudioEngine::can_request_hardware_monitoring ()
1091 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
1092 const char ** ports;
1094 if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
1105 AudioEngine::n_physical_outputs (DataType type) const
1107 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
1108 const char ** ports;
1111 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1115 for (i = 0; ports[i]; ++i) {}
1122 AudioEngine::n_physical_inputs (DataType type) const
1124 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
1125 const char ** ports;
1128 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
1132 for (i = 0; ports[i]; ++i) {}
1139 AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
1141 GET_PRIVATE_JACK_POINTER (_jack);
1142 const char ** ports;
1144 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
1149 for (uint32_t i = 0; ports[i]; ++i) {
1150 ins.push_back (ports[i]);
1157 AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
1159 GET_PRIVATE_JACK_POINTER (_jack);
1160 const char ** ports;
1163 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1167 for (i = 0; ports[i]; ++i) {
1168 outs.push_back (ports[i]);
1174 AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
1176 GET_PRIVATE_JACK_POINTER_RET (_jack,"");
1177 const char ** ports;
1181 assert(type != DataType::NIL);
1183 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|flag)) == 0) {
1187 for (i = 0; i < n && ports[i]; ++i) {}
1193 free ((const char **) ports);
1199 AudioEngine::update_total_latency (const Port& port)
1201 port.recompute_total_latency ();
1205 AudioEngine::transport_stop ()
1207 GET_PRIVATE_JACK_POINTER (_jack);
1208 jack_transport_stop (_priv_jack);
1212 AudioEngine::transport_start ()
1214 GET_PRIVATE_JACK_POINTER (_jack);
1215 jack_transport_start (_priv_jack);
1219 AudioEngine::transport_locate (nframes_t where)
1221 GET_PRIVATE_JACK_POINTER (_jack);
1222 // cerr << "tell JACK to locate to " << where << endl;
1223 jack_transport_locate (_priv_jack, where);
1226 AudioEngine::TransportState
1227 AudioEngine::transport_state ()
1229 GET_PRIVATE_JACK_POINTER_RET (_jack, ((TransportState) JackTransportStopped));
1230 jack_position_t pos;
1231 return (TransportState) jack_transport_query (_priv_jack, &pos);
1235 AudioEngine::reset_timebase ()
1237 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1239 if (_session->config.get_jack_time_master()) {
1240 return jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1242 return jack_release_timebase (_jack);
1249 AudioEngine::freewheel (bool onoff)
1251 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1253 if (onoff != _freewheeling) {
1254 return jack_set_freewheel (_priv_jack, onoff);
1257 /* already doing what has been asked for */
1263 AudioEngine::remove_all_ports ()
1265 /* process lock MUST be held */
1268 RCUWriter<Ports> writer (ports);
1269 boost::shared_ptr<Ports> ps = writer.get_copy ();
1271 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
1278 /* clear dead wood list too */
1284 AudioEngine::connect_to_jack (string client_name, string session_uuid)
1286 jack_options_t options = JackNullOption;
1287 jack_status_t status;
1288 const char *server_name = NULL;
1290 jack_client_name = client_name; /* might be reset below */
1291 #ifdef HAVE_JACK_SESSION
1292 if (! session_uuid.empty())
1293 _jack = jack_client_open (jack_client_name.c_str(), JackSessionID, &status, session_uuid.c_str());
1296 _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
1298 if (_jack == NULL) {
1299 // error message is not useful here
1303 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1305 if (status & JackNameNotUnique) {
1306 jack_client_name = jack_get_client_name (_priv_jack);
1313 AudioEngine::disconnect_from_jack ()
1315 GET_PRIVATE_JACK_POINTER_RET (_jack, 0);
1318 stop_metering_thread ();
1322 Glib::Mutex::Lock lm (_process_lock);
1323 jack_client_close (_priv_jack);
1329 _raw_buffer_sizes.clear();
1333 Stopped(); /* EMIT SIGNAL */
1334 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
1341 AudioEngine::reconnect_to_jack ()
1344 disconnect_from_jack ();
1345 /* XXX give jackd a chance */
1346 Glib::usleep (250000);
1349 if (connect_to_jack (jack_client_name, "")) {
1350 error << _("failed to connect to JACK") << endmsg;
1356 boost::shared_ptr<Ports> p = ports.reader ();
1358 for (i = p->begin(); i != p->end(); ++i) {
1359 if ((*i)->reestablish ()) {
1364 if (i != p->end()) {
1366 remove_all_ports ();
1370 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
1373 _session->reset_jack_connection (_priv_jack);
1374 jack_bufsize_callback (jack_get_buffer_size (_priv_jack));
1375 _session->set_frame_rate (jack_get_sample_rate (_priv_jack));
1378 last_monitor_check = 0;
1380 jack_on_shutdown (_priv_jack, halted, this);
1381 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
1382 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
1383 // jack_set_process_callback (_priv_jack, _process_callback, this);
1384 jack_set_process_thread (_priv_jack, _process_thread, this);
1385 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
1386 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
1387 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
1388 #ifdef HAVE_JACK_SESSION
1389 if( jack_set_session_callback )
1390 jack_set_session_callback (_priv_jack, _session_callback, this);
1392 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
1393 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
1395 if (_session && _session->config.get_jack_time_master()) {
1396 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1399 if (jack_activate (_priv_jack) == 0) {
1406 /* re-establish connections */
1408 for (i = p->begin(); i != p->end(); ++i) {
1412 Running (); /* EMIT SIGNAL*/
1414 start_metering_thread ();
1420 AudioEngine::request_buffer_size (nframes_t nframes)
1422 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1424 if (nframes == jack_get_buffer_size (_priv_jack)) {
1428 return jack_set_buffer_size (_priv_jack, nframes);
1432 AudioEngine::update_total_latencies ()
1434 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1435 GET_PRIVATE_JACK_POINTER (_jack);
1436 jack_recompute_total_latencies (_priv_jack);
1441 AudioEngine::make_port_name_relative (string portname)
1443 string::size_type len;
1444 string::size_type n;
1446 len = portname.length();
1448 for (n = 0; n < len; ++n) {
1449 if (portname[n] == ':') {
1454 if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
1455 return portname.substr (n+1);
1462 AudioEngine::make_port_name_non_relative (string portname)
1466 if (portname.find_first_of (':') != string::npos) {
1470 str = jack_client_name;
1478 AudioEngine::is_realtime () const
1480 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
1481 return jack_is_realtime (_priv_jack);