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());
149 ardour_jack_error (const char* msg)
151 error << "JACK: " << msg << endmsg;
155 AudioEngine::start ()
157 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
161 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
164 BootMessage (_("Connect session to engine"));
166 _session->set_block_size (blocksize);
167 _session->set_frame_rate (jack_get_sample_rate (_priv_jack));
169 /* page in as much of the session process code as we
170 can before we really start running.
173 _session->process (blocksize);
174 _session->process (blocksize);
175 _session->process (blocksize);
176 _session->process (blocksize);
177 _session->process (blocksize);
178 _session->process (blocksize);
179 _session->process (blocksize);
180 _session->process (blocksize);
183 _processed_frames = 0;
184 last_monitor_check = 0;
186 jack_on_shutdown (_priv_jack, halted, this);
187 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
188 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
189 // jack_set_process_callback (_priv_jack, _process_callback, this);
190 jack_set_process_thread (_priv_jack, _process_thread, this);
191 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
192 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
193 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
194 #ifdef HAVE_JACK_SESSION
195 if( jack_set_session_callback )
196 jack_set_session_callback (_priv_jack, _session_callback, this);
198 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
199 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
200 jack_set_port_registration_callback (_priv_jack, _registration_callback, this);
201 jack_set_port_connect_callback (_priv_jack, _connect_callback, this);
203 if (_session && _session->config.get_jack_time_master()) {
204 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
207 jack_set_error_function (ardour_jack_error);
209 if (jack_activate (_priv_jack) == 0) {
212 Running(); /* EMIT SIGNAL */
214 // error << _("cannot activate JACK client") << endmsg;
217 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
220 return _running ? 0 : -1;
224 AudioEngine::stop (bool forever)
226 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
230 disconnect_from_jack ();
232 jack_deactivate (_priv_jack);
233 Stopped(); /* EMIT SIGNAL */
234 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
238 return _running ? -1 : 0;
243 AudioEngine::get_sync_offset (nframes_t& offset) const
246 #ifdef HAVE_JACK_VIDEO_SUPPORT
248 GET_PRIVATE_JACK_POINTER_RET (_jack, false);
253 (void) jack_transport_query (_priv_jack, &pos);
255 if (pos.valid & JackVideoFrameOffset) {
256 offset = pos.video_offset;
269 AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
270 jack_position_t* pos, int new_position, void *arg)
272 static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
276 AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
277 jack_position_t* pos, int new_position)
279 if (_jack && _session && _session->synced_to_jack()) {
280 _session->jack_timebase_callback (state, nframes, pos, new_position);
285 AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
287 return static_cast<AudioEngine*> (arg)->jack_sync_callback (state, pos);
291 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
293 if (_jack && _session) {
294 return _session->jack_sync_callback (state, pos);
301 AudioEngine::_xrun_callback (void *arg)
303 AudioEngine* ae = static_cast<AudioEngine*> (arg);
304 if (ae->connected()) {
305 ae->Xrun (); /* EMIT SIGNAL */
310 #ifdef HAVE_JACK_SESSION
312 AudioEngine::_session_callback (jack_session_event_t *event, void *arg)
314 printf( "helo.... " );
315 AudioEngine* ae = static_cast<AudioEngine*> (arg);
316 if (ae->connected()) {
317 ae->JackSessionEvent ( event ); /* EMIT SIGNAL */
322 AudioEngine::_graph_order_callback (void *arg)
324 AudioEngine* ae = static_cast<AudioEngine*> (arg);
325 if (ae->connected()) {
326 ae->GraphReordered (); /* EMIT SIGNAL */
331 /** Wrapped which is called by JACK as its process callback. It is just
332 * here to get us back into C++ land by calling AudioEngine::process_callback()
333 * @param nframes Number of frames passed by JACK.
334 * @param arg User argument passed by JACK, which will be the AudioEngine*.
337 AudioEngine::_process_callback (nframes_t nframes, void *arg)
339 return static_cast<AudioEngine *> (arg)->process_callback (nframes);
343 AudioEngine::_process_thread (void *arg)
345 return static_cast<AudioEngine *> (arg)->process_thread ();
349 AudioEngine::_freewheel_callback (int onoff, void *arg)
351 static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
355 AudioEngine::_registration_callback (jack_port_id_t /*id*/, int /*reg*/, void* arg)
357 AudioEngine* ae = static_cast<AudioEngine*> (arg);
358 ae->PortRegisteredOrUnregistered (); /* EMIT SIGNAL */
362 AudioEngine::_connect_callback (jack_port_id_t /*id_a*/, jack_port_id_t /*id_b*/, int /*conn*/, void* arg)
364 AudioEngine* ae = static_cast<AudioEngine*> (arg);
365 ae->PortConnectedOrDisconnected (); /* EMIT SIGNAL */
369 AudioEngine::split_cycle (nframes_t offset)
371 /* caller must hold process lock */
373 Port::increment_port_offset (offset);
375 /* tell all Ports that we're going to start a new (split) cycle */
377 boost::shared_ptr<Ports> p = ports.reader();
379 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
380 (*i)->cycle_split ();
385 AudioEngine::finish_process_cycle (int status)
387 GET_PRIVATE_JACK_POINTER(_jack);
388 jack_cycle_signal (_jack, 0);
392 AudioEngine::process_thread ()
394 /* JACK doesn't do this for us when we use the wait API
397 _thread_init_callback (0);
399 _main_thread = new ProcessThread;
402 GET_PRIVATE_JACK_POINTER_RET(_jack,0);
403 jack_nframes_t nframes = jack_cycle_wait (_jack);
405 if (process_callback (nframes)) {
409 finish_process_cycle (0);
415 /** Method called by JACK (via _process_callback) which says that there
416 * is work to be done.
417 * @param nframes Number of frames to process.
420 AudioEngine::process_callback (nframes_t nframes)
422 GET_PRIVATE_JACK_POINTER_RET(_jack,0);
423 // CycleTimer ct ("AudioEngine::process");
424 Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
426 /// The number of frames that will have been processed when we've finished
427 nframes_t next_processed_frames;
429 /* handle wrap around of total frames counter */
431 if (max_frames - _processed_frames < nframes) {
432 next_processed_frames = nframes - (max_frames - _processed_frames);
434 next_processed_frames = _processed_frames + nframes;
437 if (!tm.locked() || _session == 0) {
438 /* return having done nothing */
439 _processed_frames = next_processed_frames;
443 if (session_remove_pending) {
444 /* perform the actual session removal */
446 session_remove_pending = false;
447 session_removed.signal();
448 _processed_frames = next_processed_frames;
452 /* tell all relevant objects that we're starting a new cycle */
454 Delivery::CycleStart (nframes);
455 Port::set_port_offset (0);
456 InternalReturn::CycleStart (nframes);
458 /* tell all Ports that we're starting a new cycle */
460 boost::shared_ptr<Ports> p = ports.reader();
462 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
463 (*i)->cycle_start (nframes);
467 /* emit the Freewheel signal and stop freewheeling in the event of trouble
468 * the indirection is to pick up the return value of the signal.
470 if (*Freewheel (nframes)) {
471 jack_set_freewheel (_priv_jack, false);
476 _session->process (nframes);
486 _processed_frames = next_processed_frames;
490 if (last_monitor_check + monitor_check_interval < next_processed_frames) {
492 boost::shared_ptr<Ports> p = ports.reader();
494 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
499 if (port->_last_monitor != (x = port->monitoring_input ())) {
500 port->_last_monitor = x;
501 /* XXX I think this is dangerous, due to
502 a likely mutex in the signal handlers ...
504 port->MonitorInputChanged (x); /* EMIT SIGNAL */
507 last_monitor_check = next_processed_frames;
510 if (_session->silent()) {
512 boost::shared_ptr<Ports> p = ports.reader();
514 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
518 if (port->sends_output()) {
519 port->get_buffer(nframes).silence(nframes);
526 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
527 (*i)->cycle_end (nframes);
530 _processed_frames = next_processed_frames;
535 AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
537 return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
541 AudioEngine::jack_sample_rate_callback (nframes_t nframes)
543 _frame_rate = nframes;
544 _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
546 /* check for monitor input change every 1/10th of second */
548 monitor_check_interval = nframes / 10;
549 last_monitor_check = 0;
552 _session->set_frame_rate (nframes);
555 SampleRateChanged (nframes); /* EMIT SIGNAL */
561 AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
563 return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
567 AudioEngine::jack_bufsize_callback (nframes_t nframes)
569 _buffer_size = nframes;
570 _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof(float);
571 cout << "FIXME: Assuming maximum MIDI buffer size " << nframes * 4 << "bytes" << endl;
572 _raw_buffer_sizes[DataType::MIDI] = nframes * 4;
573 _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
574 last_monitor_check = 0;
576 boost::shared_ptr<Ports> p = ports.reader();
578 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
583 _session->set_block_size (_buffer_size);
590 AudioEngine::stop_metering_thread ()
592 if (m_meter_thread) {
593 g_atomic_int_set (&m_meter_exit, 1);
594 m_meter_thread->join ();
600 AudioEngine::start_metering_thread ()
602 if (m_meter_thread == 0) {
603 g_atomic_int_set (&m_meter_exit, 0);
604 m_meter_thread = Glib::Thread::create (boost::bind (&AudioEngine::meter_thread, this),
605 500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
610 AudioEngine::meter_thread ()
612 pthread_set_name (X_("meter"));
615 Glib::usleep (10000); /* 1/100th sec interval */
616 if (g_atomic_int_get(&m_meter_exit)) {
624 AudioEngine::set_session (Session *s)
626 Glib::Mutex::Lock pl (_process_lock);
628 SessionHandlePtr::set_session (s);
632 start_metering_thread ();
634 nframes_t blocksize = jack_get_buffer_size (_jack);
636 /* page in as much of the session process code as we
637 can before we really start running.
640 boost::shared_ptr<Ports> p = ports.reader();
642 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
643 (*i)->cycle_start (blocksize);
646 _session->process (blocksize);
647 _session->process (blocksize);
648 _session->process (blocksize);
649 _session->process (blocksize);
650 _session->process (blocksize);
651 _session->process (blocksize);
652 _session->process (blocksize);
653 _session->process (blocksize);
655 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
656 (*i)->cycle_end (blocksize);
662 AudioEngine::remove_session ()
664 Glib::Mutex::Lock lm (_process_lock);
668 stop_metering_thread ();
671 session_remove_pending = true;
672 session_removed.wait(_process_lock);
676 SessionHandlePtr::set_session (0);
683 AudioEngine::port_registration_failure (const std::string& portname)
685 GET_PRIVATE_JACK_POINTER (_jack);
686 string full_portname = jack_client_name;
687 full_portname += ':';
688 full_portname += portname;
691 jack_port_t* p = jack_port_by_name (_priv_jack, full_portname.c_str());
695 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
697 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);
700 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
704 AudioEngine::register_port (DataType dtype, const string& portname, bool input)
709 if (dtype == DataType::AUDIO) {
710 newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput));
711 } else if (dtype == DataType::MIDI) {
712 newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput));
714 throw PortRegistrationFailure("unable to create port (unknown type)");
717 size_t& old_buffer_size = _raw_buffer_sizes[newport->type()];
718 size_t port_buffer_size = newport->raw_buffer_size(0);
719 if (port_buffer_size > old_buffer_size) {
720 old_buffer_size = port_buffer_size;
723 RCUWriter<Ports> writer (ports);
724 boost::shared_ptr<Ports> ps = writer.get_copy ();
725 ps->insert (ps->begin(), newport);
727 /* writer goes out of scope, forces update */
732 catch (PortRegistrationFailure& err) {
734 } catch (std::exception& e) {
735 throw PortRegistrationFailure(string_compose(
736 _("unable to create port: %1"), e.what()).c_str());
738 throw PortRegistrationFailure("unable to create port (unknown error)");
743 AudioEngine::register_input_port (DataType type, const string& portname)
745 return register_port (type, portname, true);
749 AudioEngine::register_output_port (DataType type, const string& portname)
751 return register_port (type, portname, false);
755 AudioEngine::unregister_port (Port& port)
757 /* caller must hold process lock */
760 /* probably happening when the engine has been halted by JACK,
761 in which case, there is nothing we can do here.
767 RCUWriter<Ports> writer (ports);
768 boost::shared_ptr<Ports> ps = writer.get_copy ();
770 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
778 /* writer goes out of scope, forces update */
785 AudioEngine::connect (const string& source, const string& destination)
787 /* caller must hold process lock */
793 fatal << _("connect called before engine was started") << endmsg;
800 string s = make_port_name_non_relative (source);
801 string d = make_port_name_non_relative (destination);
804 Port* src = get_port_by_name_locked (s);
805 Port* dst = get_port_by_name_locked (d);
808 ret = src->connect (d);
810 ret = dst->connect (s);
812 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
817 /* already exists - no error, no warning */
818 } else if (ret < 0) {
819 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
820 source, s, destination, d)
828 AudioEngine::disconnect (const string& source, const string& destination)
830 /* caller must hold process lock */
836 fatal << _("disconnect called before engine was started") << endmsg;
843 string s = make_port_name_non_relative (source);
844 string d = make_port_name_non_relative (destination);
846 Port* src = get_port_by_name_locked (s);
847 Port* dst = get_port_by_name_locked (d);
850 ret = src->disconnect (d);
852 ret = dst->disconnect (s);
854 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
861 AudioEngine::disconnect (Port& port)
863 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
867 fatal << _("disconnect called before engine was started") << endmsg;
874 return port.disconnect_all ();
878 AudioEngine::frame_rate () const
880 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
881 if (_frame_rate == 0) {
882 return (_frame_rate = jack_get_sample_rate (_priv_jack));
889 AudioEngine::raw_buffer_size (DataType t)
891 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
892 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
896 AudioEngine::frames_per_cycle () const
898 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
899 if (_buffer_size == 0) {
900 return (_buffer_size = jack_get_buffer_size (_jack));
906 /** @param name Full name of port (including prefix:)
907 * @return Corresponding Port*, or 0. This object remains the property of the AudioEngine
908 * so must not be deleted.
911 AudioEngine::get_port_by_name (const string& portname)
914 if (portname.find_first_of (':') == string::npos) {
915 s = make_port_name_non_relative (portname);
920 Glib::Mutex::Lock lm (_process_lock);
921 return get_port_by_name_locked (s);
925 AudioEngine::get_port_by_name_locked (const string& portname)
927 /* caller must hold process lock */
931 fatal << _("get_port_by_name_locked() called before engine was started") << endmsg;
938 if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
939 /* not an ardour: port */
943 std::string const rel = make_port_name_relative (portname);
945 boost::shared_ptr<Ports> pr = ports.reader();
947 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
948 if (rel == (*i)->name()) {
957 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
959 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
962 fatal << _("get_ports called before engine was started") << endmsg;
968 return jack_get_ports (_priv_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
972 AudioEngine::halted (void *arg)
974 cerr << "HALTED by JACK\n";
976 /* called from jack shutdown handler */
978 AudioEngine* ae = static_cast<AudioEngine *> (arg);
979 bool was_running = ae->_running;
981 ae->stop_metering_thread ();
983 ae->_running = false;
984 ae->_buffer_size = 0;
988 ae->Halted(); /* EMIT SIGNAL */
989 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
996 /* called from a signal handler for SIGPIPE */
998 stop_metering_thread ();
1007 AudioEngine::can_request_hardware_monitoring ()
1009 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
1010 const char ** ports;
1012 if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
1023 AudioEngine::n_physical_outputs (DataType type) const
1025 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
1026 const char ** ports;
1029 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1033 for (i = 0; ports[i]; ++i) {}
1040 AudioEngine::n_physical_inputs (DataType type) const
1042 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
1043 const char ** ports;
1046 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
1050 for (i = 0; ports[i]; ++i) {}
1057 AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
1059 GET_PRIVATE_JACK_POINTER (_jack);
1060 const char ** ports;
1062 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
1067 for (uint32_t i = 0; ports[i]; ++i) {
1068 ins.push_back (ports[i]);
1075 AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
1077 GET_PRIVATE_JACK_POINTER (_jack);
1078 const char ** ports;
1081 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1085 for (i = 0; ports[i]; ++i) {
1086 outs.push_back (ports[i]);
1092 AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
1094 GET_PRIVATE_JACK_POINTER_RET (_jack,"");
1095 const char ** ports;
1099 assert(type != DataType::NIL);
1101 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|flag)) == 0) {
1105 for (i = 0; i < n && ports[i]; ++i) {}
1111 free ((const char **) ports);
1117 AudioEngine::update_total_latency (const Port& port)
1119 port.recompute_total_latency ();
1123 AudioEngine::transport_stop ()
1125 GET_PRIVATE_JACK_POINTER (_jack);
1126 jack_transport_stop (_priv_jack);
1130 AudioEngine::transport_start ()
1132 GET_PRIVATE_JACK_POINTER (_jack);
1133 jack_transport_start (_priv_jack);
1137 AudioEngine::transport_locate (nframes_t where)
1139 GET_PRIVATE_JACK_POINTER (_jack);
1140 // cerr << "tell JACK to locate to " << where << endl;
1141 jack_transport_locate (_priv_jack, where);
1144 AudioEngine::TransportState
1145 AudioEngine::transport_state ()
1147 GET_PRIVATE_JACK_POINTER_RET (_jack, ((TransportState) JackTransportStopped));
1148 jack_position_t pos;
1149 return (TransportState) jack_transport_query (_priv_jack, &pos);
1153 AudioEngine::reset_timebase ()
1155 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1157 if (_session->config.get_jack_time_master()) {
1158 return jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1160 return jack_release_timebase (_jack);
1167 AudioEngine::freewheel (bool onoff)
1169 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1171 if (onoff != _freewheeling) {
1172 return jack_set_freewheel (_priv_jack, onoff);
1175 /* already doing what has been asked for */
1181 AudioEngine::remove_all_ports ()
1183 /* process lock MUST be held */
1186 RCUWriter<Ports> writer (ports);
1187 boost::shared_ptr<Ports> ps = writer.get_copy ();
1189 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
1196 /* clear dead wood list too */
1202 AudioEngine::connect_to_jack (string client_name, string session_uuid)
1204 jack_options_t options = JackNullOption;
1205 jack_status_t status;
1206 const char *server_name = NULL;
1208 jack_client_name = client_name; /* might be reset below */
1209 #ifdef HAVE_JACK_SESSION
1210 if (! session_uuid.empty())
1211 _jack = jack_client_open (jack_client_name.c_str(), JackSessionID, &status, session_uuid.c_str());
1214 _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
1216 if (_jack == NULL) {
1217 // error message is not useful here
1221 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1223 if (status & JackNameNotUnique) {
1224 jack_client_name = jack_get_client_name (_priv_jack);
1231 AudioEngine::disconnect_from_jack ()
1233 GET_PRIVATE_JACK_POINTER_RET (_jack, 0);
1236 stop_metering_thread ();
1240 Glib::Mutex::Lock lm (_process_lock);
1241 jack_client_close (_priv_jack);
1247 _raw_buffer_sizes.clear();
1251 Stopped(); /* EMIT SIGNAL */
1252 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
1259 AudioEngine::reconnect_to_jack ()
1262 disconnect_from_jack ();
1263 /* XXX give jackd a chance */
1264 Glib::usleep (250000);
1267 if (connect_to_jack (jack_client_name, "")) {
1268 error << _("failed to connect to JACK") << endmsg;
1274 boost::shared_ptr<Ports> p = ports.reader ();
1276 for (i = p->begin(); i != p->end(); ++i) {
1277 if ((*i)->reestablish ()) {
1282 if (i != p->end()) {
1284 remove_all_ports ();
1288 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
1291 _session->reset_jack_connection (_priv_jack);
1292 jack_bufsize_callback (jack_get_buffer_size (_priv_jack));
1293 _session->set_frame_rate (jack_get_sample_rate (_priv_jack));
1296 last_monitor_check = 0;
1298 jack_on_shutdown (_priv_jack, halted, this);
1299 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
1300 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
1301 // jack_set_process_callback (_priv_jack, _process_callback, this);
1302 jack_set_process_thread (_priv_jack, _process_thread, this);
1303 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
1304 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
1305 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
1306 #ifdef HAVE_JACK_SESSION
1307 if( jack_set_session_callback )
1308 jack_set_session_callback (_priv_jack, _session_callback, this);
1310 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
1311 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
1313 if (_session && _session->config.get_jack_time_master()) {
1314 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1317 if (jack_activate (_priv_jack) == 0) {
1324 /* re-establish connections */
1326 for (i = p->begin(); i != p->end(); ++i) {
1330 Running (); /* EMIT SIGNAL*/
1332 start_metering_thread ();
1338 AudioEngine::request_buffer_size (nframes_t nframes)
1340 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1342 if (nframes == jack_get_buffer_size (_priv_jack)) {
1346 return jack_set_buffer_size (_priv_jack, nframes);
1350 AudioEngine::update_total_latencies ()
1352 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1353 GET_PRIVATE_JACK_POINTER (_jack);
1354 jack_recompute_total_latencies (_priv_jack);
1359 AudioEngine::make_port_name_relative (string portname)
1361 string::size_type len;
1362 string::size_type n;
1364 len = portname.length();
1366 for (n = 0; n < len; ++n) {
1367 if (portname[n] == ':') {
1372 if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
1373 return portname.substr (n+1);
1380 AudioEngine::make_port_name_non_relative (string portname)
1384 if (portname.find_first_of (':') != string::npos) {
1388 str = jack_client_name;
1396 AudioEngine::is_realtime () const
1398 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
1399 return jack_is_realtime (_priv_jack);