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/audioengine.h>
35 #include <ardour/buffer.h>
36 #include <ardour/port.h>
37 #include <ardour/jack_audio_port.h>
38 #include <ardour/jack_midi_port.h>
39 #include <ardour/audio_port.h>
40 #include <ardour/session.h>
41 #include <ardour/cycle_timer.h>
42 #include <ardour/utils.h>
47 #include <ardour/timestamps.h>
52 using namespace ARDOUR;
55 gint AudioEngine::m_meter_exit;
58 ardour_jack_error (const char* msg)
60 error << "JACK: " << msg << endmsg;
63 AudioEngine::AudioEngine (string client_name)
67 session_remove_pending = false;
70 last_monitor_check = 0;
71 monitor_check_interval = max_frames;
72 _processed_frames = 0;
77 _freewheel_thread_registered = false;
78 _freewheeling = false;
81 g_atomic_int_set (&m_meter_exit, 0);
83 if (connect_to_jack (client_name)) {
84 throw NoBackendAvailable ();
87 Port::set_engine (this);
89 Parameter::init_metadata(NullAutomation);
90 Parameter::init_metadata(GainAutomation);
91 Parameter::init_metadata(PanAutomation);
92 Parameter::init_metadata(PluginAutomation);
93 Parameter::init_metadata(SoloAutomation);
94 Parameter::init_metadata(MuteAutomation);
95 Parameter::init_metadata(MidiCCAutomation);
96 Parameter::init_metadata(MidiPgmChangeAutomation);
97 Parameter::init_metadata(MidiPitchBenderAutomation);
98 Parameter::init_metadata(MidiChannelPressureAutomation);
99 Parameter::init_metadata(FadeInAutomation);
100 Parameter::init_metadata(FadeOutAutomation);
101 Parameter::init_metadata(EnvelopeAutomation);
102 Parameter::init_metadata(MidiCCAutomation);
105 AudioEngine::~AudioEngine ()
108 Glib::Mutex::Lock tm (_process_lock);
109 session_removed.signal ();
112 jack_client_close (_jack);
116 stop_metering_thread ();
121 AudioEngine::jack() const
127 _thread_init_callback (void *arg)
129 /* make sure that anybody who needs to know about this thread
133 PBD::ThreadCreatedWithRequestSize (pthread_self(), X_("Audioengine"), 4096);
134 MIDI::JACK_MidiPort::set_process_thread (pthread_self());
138 AudioEngine::start ()
141 error << _("AudioEngine::start() called while disconnected from JACK") << endmsg;
148 nframes_t blocksize = jack_get_buffer_size (_jack);
150 BootMessage (_("Connect session to engine"));
152 session->set_block_size (blocksize);
153 session->set_frame_rate (jack_get_sample_rate (_jack));
155 /* page in as much of the session process code as we
156 can before we really start running.
159 session->process (blocksize);
160 session->process (blocksize);
161 session->process (blocksize);
162 session->process (blocksize);
163 session->process (blocksize);
164 session->process (blocksize);
165 session->process (blocksize);
166 session->process (blocksize);
169 _processed_frames = 0;
170 last_monitor_check = 0;
172 jack_on_shutdown (_jack, halted, this);
173 jack_set_graph_order_callback (_jack, _graph_order_callback, this);
174 jack_set_thread_init_callback (_jack, _thread_init_callback, this);
175 jack_set_process_callback (_jack, _process_callback, this);
176 jack_set_sample_rate_callback (_jack, _sample_rate_callback, this);
177 jack_set_buffer_size_callback (_jack, _bufsize_callback, this);
178 jack_set_xrun_callback (_jack, _xrun_callback, this);
179 jack_set_sync_callback (_jack, _jack_sync_callback, this);
180 jack_set_freewheel_callback (_jack, _freewheel_callback, this);
182 if (Config->get_jack_time_master()) {
183 jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
186 if (jack_activate (_jack) == 0) {
189 Running(); /* EMIT SIGNAL */
191 // error << _("cannot activate JACK client") << endmsg;
194 start_metering_thread();
197 return _running ? 0 : -1;
201 AudioEngine::stop (bool forever)
205 disconnect_from_jack ();
207 jack_deactivate (_jack);
208 Stopped(); /* EMIT SIGNAL */
212 return _running ? -1 : 0;
217 AudioEngine::get_sync_offset (nframes_t& offset) const
220 #ifdef HAVE_JACK_VIDEO_SUPPORT
225 (void) jack_transport_query (_jack, &pos);
227 if (pos.valid & JackVideoFrameOffset) {
228 offset = pos.video_offset;
239 AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
240 jack_position_t* pos, int new_position, void *arg)
242 static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
246 AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
247 jack_position_t* pos, int new_position)
249 if (_jack && session && session->synced_to_jack()) {
250 session->jack_timebase_callback (state, nframes, pos, new_position);
255 AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
257 return static_cast<AudioEngine*> (arg)->jack_sync_callback (state, pos);
261 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
263 if (_jack && session) {
264 return session->jack_sync_callback (state, pos);
271 AudioEngine::_xrun_callback (void *arg)
273 AudioEngine* ae = static_cast<AudioEngine*> (arg);
274 if (ae->connected()) {
275 ae->Xrun (); /* EMIT SIGNAL */
281 AudioEngine::_graph_order_callback (void *arg)
283 AudioEngine* ae = static_cast<AudioEngine*> (arg);
284 if (ae->connected()) {
285 ae->GraphReordered (); /* EMIT SIGNAL */
290 /** Wrapped which is called by JACK as its process callback. It is just
291 * here to get us back into C++ land by calling AudioEngine::process_callback()
292 * @param nframes Number of frames passed by JACK.
293 * @param arg User argument passed by JACK, which will be the AudioEngine*.
296 AudioEngine::_process_callback (nframes_t nframes, void *arg)
298 return static_cast<AudioEngine *> (arg)->process_callback (nframes);
302 AudioEngine::_freewheel_callback (int onoff, void *arg)
304 static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
307 /** Method called by JACK (via _process_callback) which says that there
308 * is work to be done.
309 * @param nframes Number of frames to process.
312 AudioEngine::process_callback (nframes_t nframes)
314 // CycleTimer ct ("AudioEngine::process");
315 Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
317 /// The number of frames that will have been processed when we've finished
318 nframes_t next_processed_frames;
320 /* handle wrap around of total frames counter */
322 if (max_frames - _processed_frames < nframes) {
323 next_processed_frames = nframes - (max_frames - _processed_frames);
325 next_processed_frames = _processed_frames + nframes;
328 if (!tm.locked() || session == 0) {
329 /* return having done nothing */
330 _processed_frames = next_processed_frames;
334 if (session_remove_pending) {
335 /* perform the actual session removal */
337 session_remove_pending = false;
338 session_removed.signal();
339 _processed_frames = next_processed_frames;
343 boost::shared_ptr<Ports> p = ports.reader();
345 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
347 /* Only run cycle_start() on output ports, because
348 inputs must be done in the correct processing order,
349 which requires interleaving with route processing.
352 if ((*i)->sends_output()) {
353 (*i)->cycle_start (nframes, 0);
358 /* emit the Freewheel signal and stop freewheeling in the event of trouble */
359 if (Freewheel (nframes)) {
360 jack_set_freewheel (_jack, false);
365 session->process (nframes);
369 // Finalize ports (ie write data if necessary)
371 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
372 (*i)->cycle_end (nframes, 0);
380 _processed_frames = next_processed_frames;
384 if (last_monitor_check + monitor_check_interval < next_processed_frames) {
386 boost::shared_ptr<Ports> p = ports.reader();
388 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
393 if (port->_last_monitor != (x = port->monitoring_input ())) {
394 port->_last_monitor = x;
395 /* XXX I think this is dangerous, due to
396 a likely mutex in the signal handlers ...
398 port->MonitorInputChanged (x); /* EMIT SIGNAL */
401 last_monitor_check = next_processed_frames;
404 if (session->silent()) {
406 boost::shared_ptr<Ports> p = ports.reader();
408 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
412 if (port->sends_output()) {
413 port->get_buffer().silence(nframes);
418 _processed_frames = next_processed_frames;
423 AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
425 return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
429 AudioEngine::jack_sample_rate_callback (nframes_t nframes)
431 _frame_rate = nframes;
432 _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
434 /* check for monitor input change every 1/10th of second */
436 monitor_check_interval = nframes / 10;
437 last_monitor_check = 0;
440 session->set_frame_rate (nframes);
443 SampleRateChanged (nframes); /* EMIT SIGNAL */
449 AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
451 return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
455 AudioEngine::jack_bufsize_callback (nframes_t nframes)
457 _buffer_size = nframes;
458 _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
459 last_monitor_check = 0;
461 boost::shared_ptr<Ports> p = ports.reader();
463 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
468 session->set_block_size (_buffer_size);
475 AudioEngine::stop_metering_thread ()
477 if (m_meter_thread) {
478 g_atomic_int_set (&m_meter_exit, 1);
479 m_meter_thread->join ();
485 AudioEngine::start_metering_thread ()
487 if (m_meter_thread == 0) {
488 g_atomic_int_set (&m_meter_exit, 0);
489 m_meter_thread = Glib::Thread::create (sigc::mem_fun(this, &AudioEngine::meter_thread),
490 500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
495 AudioEngine::meter_thread ()
498 Glib::usleep (10000); /* 1/100th sec interval */
499 if (g_atomic_int_get(&m_meter_exit)) {
502 IO::update_meters ();
507 AudioEngine::set_session (Session *s)
509 Glib::Mutex::Lock pl (_process_lock);
515 nframes_t blocksize = jack_get_buffer_size (_jack);
517 /* page in as much of the session process code as we
518 can before we really start running.
521 boost::shared_ptr<Ports> p = ports.reader();
523 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
524 if ((*i)->sends_output()) {
525 (*i)->cycle_start (blocksize, 0);
529 s->process (blocksize);
530 s->process (blocksize);
531 s->process (blocksize);
532 s->process (blocksize);
533 s->process (blocksize);
534 s->process (blocksize);
535 s->process (blocksize);
536 s->process (blocksize);
538 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
539 (*i)->cycle_end (blocksize, 0);
545 AudioEngine::remove_session ()
547 Glib::Mutex::Lock lm (_process_lock);
552 session_remove_pending = true;
553 session_removed.wait(_process_lock);
560 //FIXME: Preliminary bugfix for http://tracker.ardour.org/view.php?id=1985
561 //remove_all_ports ();
565 AudioEngine::port_registration_failure (const std::string& portname)
567 string full_portname = jack_client_name;
568 full_portname += ':';
569 full_portname += portname;
572 jack_port_t* p = jack_port_by_name (_jack, full_portname.c_str());
576 reason = _("a port with this name already exists: check for duplicated track/bus names");
578 reason = _("unknown error");
581 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
585 AudioEngine::register_port (DataType dtype, const string& portname, bool input, bool publish)
589 /*cerr << "trying to register port with name " << portname << endl;*/
591 if (dtype == DataType::AUDIO) {
592 newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput), publish, frames_per_cycle());
593 } else if (dtype == DataType::MIDI) {
594 newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput), publish, frames_per_cycle());
596 throw unknown_type();
599 /*cerr << "successfully got port " << portname << " with address " << newport << endl;*/
601 RCUWriter<Ports> writer (ports);
602 boost::shared_ptr<Ports> ps = writer.get_copy ();
603 /*cerr << "Address of ports list: " << ps << endl
604 << "Ports set size before insert: " << ps->size() << endl;*/
605 ps->insert (ps->begin(), newport);
606 /*cerr << "Ports set size after insert: " << ps->size() << endl;*/
608 /* writer goes out of scope, forces update */
614 throw PortRegistrationFailure("unable to create port (unknown type?)");
619 AudioEngine::get_port (const std::string& full_name)
621 boost::shared_ptr<Ports> p = ports.reader();
623 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
624 //cerr << "comparing port name '" << (*i)->name() << "' with '" << full_name << "'" << endl;
625 if ((*i)->name() == full_name) {
634 AudioEngine::register_input_port (DataType type, const string& portname, bool publish)
636 return register_port (type, portname, true, publish);
640 AudioEngine::register_output_port (DataType type, const string& portname, bool publish)
642 return register_port (type, portname, false, publish);
646 AudioEngine::unregister_port (Port& port)
648 /* caller must hold process lock */
650 cerr << "about to unregister Port xx x" << &port << "\n";
653 /* probably happening when the engine has been halted by JACK,
654 in which case, there is nothing we can do here.
656 cerr << "not running\n";
661 cerr << "before getcopy\n";
663 RCUWriter<Ports> writer (ports);
664 boost::shared_ptr<Ports> ps = writer.get_copy ();
666 cerr << "Ports set size: " << ps.get()->size() << endl;
668 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
669 cerr << "before delete" << endl;
671 cerr << "About to delete " << &port << endl;
674 cerr << "After erasing ports size: " << ps->size();
679 /* writer goes out of scope, forces update */
682 cerr << "before remove_connections\n";
683 remove_connections_for (port);
689 AudioEngine::connect (const string& source, const string& destination)
695 fatal << _("connect called before engine was started") << endmsg;
702 string s = make_port_name_non_relative (source);
703 string d = make_port_name_non_relative (destination);
705 //cerr << "Trying to connect source: " << s << " with destination " << d << endl;
707 Port* src = get_port (s);
708 Port* dst = get_port (d);
712 /* both ports are known to us, so do the internal connect stuff */
714 if ((ret = src->connect (*dst)) == 0) {
715 ret = dst->connect (*src);
718 } else if (src || dst) {
720 /* one port is known to us, try to connect it to something external */
722 PortConnectableByName* pcn;
726 pcn = dynamic_cast<PortConnectableByName*>(src);
729 pcn = dynamic_cast<PortConnectableByName*>(dst);
734 ret = pcn->connect (other);
741 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
748 error << string_compose(_("AudioEngine: connection already exists: %1 (%2) to %3 (%4)"),
749 source, s, destination, d)
751 } else if (ret < 0) {
752 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
753 source, s, destination, d)
761 AudioEngine::disconnect (const string& source, const string& destination)
767 fatal << _("disconnect called before engine was started") << endmsg;
774 string s = make_port_name_non_relative (source);
775 string d = make_port_name_non_relative (destination);
777 //cerr << "trying to disconnect port '" << s << "' from port '" << d << endl;
779 Port* src = get_port (s);
780 Port* dst = get_port (d);
784 /* both ports are known to us, so do the internal connect stuff */
786 if ((ret = src->disconnect (*dst)) == 0) {
787 ret = dst->disconnect (*src);
790 } else if (src || dst) {
792 /* one port is known to us, try to connect it to something external */
795 PortConnectableByName* pcn;
799 pcn = dynamic_cast<PortConnectableByName*>(src);
802 pcn = dynamic_cast<PortConnectableByName*>(dst);
807 ret = pcn->disconnect (other);
814 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
824 AudioEngine::disconnect (Port& port)
828 fatal << _("disconnect called before engine was started") << endmsg;
835 return port.disconnect_all ();
839 AudioEngine::frame_rate ()
842 if (_frame_rate == 0) {
843 return (_frame_rate = jack_get_sample_rate (_jack));
848 fatal << X_("programming error: AudioEngine::frame_rate() called while disconnected from JACK")
856 AudioEngine::frames_per_cycle ()
859 if (_buffer_size == 0) {
860 return (_buffer_size = jack_get_buffer_size (_jack));
865 fatal << X_("programming error: AudioEngine::frame_rate() called while disconnected from JACK")
872 /** Get a port by name.
873 * Note this can return NULL, it will NOT create a port if it is not found (any more).
876 AudioEngine::get_port_by_name (const string& portname, bool keep)
878 Glib::Mutex::Lock lm (_process_lock);
882 fatal << _("get_port_by_name() called before engine was started") << endmsg;
889 boost::shared_ptr<Ports> pr = ports.reader();
891 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
892 if (portname == (*i)->name()) {
901 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
905 fatal << _("get_ports called before engine was started") << endmsg;
911 return jack_get_ports (_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
915 AudioEngine::halted (void *arg)
917 AudioEngine* ae = static_cast<AudioEngine *> (arg);
918 bool was_running = ae->_running;
920 ae->stop_metering_thread ();
922 ae->_running = false;
923 ae->_buffer_size = 0;
926 cerr << "!!! HALTED !!!\n";
929 ae->Halted(); /* EMIT SIGNAL */
934 AudioEngine::can_request_hardware_monitoring ()
942 if ((ports = jack_get_ports (_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
953 AudioEngine::n_physical_outputs (DataType type) const
962 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
966 for (i = 0; ports[i]; ++i);
973 AudioEngine::n_physical_inputs (DataType type) const
982 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
987 for (i = 0; ports[i]; ++i);
994 AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
1003 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
1008 for (i = 0; ports[i]; ++i) {
1009 ins.push_back (ports[i]);
1016 AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
1018 const char ** ports;
1025 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1030 for (i = 0; ports[i]; ++i) {
1031 outs.push_back (ports[i]);
1038 AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
1040 const char ** ports;
1044 assert(type != DataType::NIL);
1046 if (!_running || !_jack) {
1048 fatal << _("get_nth_physical called before engine was started") << endmsg;
1055 ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|flag);
1061 for (i = 0; i < n && ports[i]; ++i);
1067 free ((char *) ports);
1073 AudioEngine::get_port_total_latency (const Port& port)
1075 return port.total_latency ();
1079 AudioEngine::update_total_latency (const Port& port)
1082 fatal << _("update_total_latency() called with no JACK client connection") << endmsg;
1088 fatal << _("update_total_latency() called before engine was started") << endmsg;
1093 port.recompute_total_latency ();
1097 AudioEngine::transport_stop ()
1100 jack_transport_stop (_jack);
1105 AudioEngine::transport_start ()
1107 // cerr << "tell JACK to start\n";
1109 jack_transport_start (_jack);
1114 AudioEngine::transport_locate (nframes_t where)
1116 // cerr << "tell JACK to locate to " << where << endl;
1118 jack_transport_locate (_jack, where);
1122 AudioEngine::TransportState
1123 AudioEngine::transport_state ()
1126 jack_position_t pos;
1127 return (TransportState) jack_transport_query (_jack, &pos);
1129 return (TransportState) JackTransportStopped;
1134 AudioEngine::reset_timebase ()
1137 if (Config->get_jack_time_master()) {
1138 return jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
1140 return jack_release_timebase (_jack);
1148 AudioEngine::freewheel (bool onoff)
1152 if (onoff != _freewheeling) {
1155 _freewheel_thread_registered = false;
1158 return jack_set_freewheel (_jack, onoff);
1161 /* already doing what has been asked for */
1171 AudioEngine::remove_all_ports ()
1173 /* process lock MUST be held */
1176 RCUWriter<Ports> writer (ports);
1177 boost::shared_ptr<Ports> ps = writer.get_copy ();
1183 AudioEngine::remove_connections_for (Port& port)
1185 for (PortConnections::iterator i = port_connections.begin(); i != port_connections.end(); ) {
1186 PortConnections::iterator tmp;
1191 if ((*i).first == port.name()) {
1192 port_connections.erase (i);
1200 #ifdef HAVE_JACK_CLIENT_OPEN
1203 AudioEngine::connect_to_jack (string client_name)
1205 jack_options_t options = JackNullOption;
1206 jack_status_t status;
1207 const char *server_name = NULL;
1209 jack_client_name = client_name; /* might be reset below */
1210 _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
1212 if (_jack == NULL) {
1214 if (status & JackServerFailed) {
1215 error << _("Unable to connect to JACK server") << endmsg;
1218 // error message is not useful here
1222 if (status & JackServerStarted) {
1223 info << _("JACK server started") << endmsg;
1226 if (status & JackNameNotUnique) {
1227 jack_client_name = jack_get_client_name (_jack);
1230 jack_set_error_function (ardour_jack_error);
1238 AudioEngine::connect_to_jack (string client_name)
1240 jack_client_name = client_name;
1242 if ((_jack = jack_client_new (client_name.c_str())) == 0) {
1249 #endif /* HAVE_JACK_CLIENT_OPEN */
1252 AudioEngine::disconnect_from_jack ()
1260 stop_metering_thread ();
1264 Glib::Mutex::Lock lm (_process_lock);
1265 jack_client_close (_jack);
1274 Stopped(); /* EMIT SIGNAL */
1281 AudioEngine::reconnect_to_jack ()
1284 disconnect_from_jack ();
1285 /* XXX give jackd a chance */
1286 Glib::usleep (250000);
1289 if (connect_to_jack (jack_client_name)) {
1290 error << _("failed to connect to JACK") << endmsg;
1296 boost::shared_ptr<Ports> p = ports.reader ();
1298 for (i = p->begin(); i != p->end(); ++i) {
1299 if ((*i)->reestablish ()) {
1304 if (i != p->end()) {
1306 remove_all_ports ();
1312 session->reset_jack_connection (_jack);
1313 nframes_t blocksize = jack_get_buffer_size (_jack);
1314 session->set_block_size (blocksize);
1315 session->set_frame_rate (jack_get_sample_rate (_jack));
1318 last_monitor_check = 0;
1320 jack_on_shutdown (_jack, halted, this);
1321 jack_set_graph_order_callback (_jack, _graph_order_callback, this);
1322 jack_set_thread_init_callback (_jack, _thread_init_callback, this);
1323 jack_set_process_callback (_jack, _process_callback, this);
1324 jack_set_sample_rate_callback (_jack, _sample_rate_callback, this);
1325 jack_set_buffer_size_callback (_jack, _bufsize_callback, this);
1326 jack_set_xrun_callback (_jack, _xrun_callback, this);
1327 jack_set_sync_callback (_jack, _jack_sync_callback, this);
1328 jack_set_freewheel_callback (_jack, _freewheel_callback, this);
1330 if (Config->get_jack_time_master()) {
1331 jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
1334 if (jack_activate (_jack) == 0) {
1341 /* re-establish connections */
1343 for (i = p->begin(); i != p->end(); ++i) {
1347 Running (); /* EMIT SIGNAL*/
1349 start_metering_thread ();
1355 AudioEngine::request_buffer_size (nframes_t nframes)
1359 if (nframes == jack_get_buffer_size (_jack)) {
1363 return jack_set_buffer_size (_jack, nframes);
1371 AudioEngine::update_total_latencies ()
1373 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1375 jack_recompute_total_latencies (_jack);
1381 AudioEngine::make_port_name_relative (string portname)
1383 string::size_type len;
1384 string::size_type n;
1386 len = portname.length();
1388 for (n = 0; n < len; ++n) {
1389 if (portname[n] == ':') {
1394 if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
1395 return portname.substr (n+1);
1402 AudioEngine::make_port_name_non_relative (string portname)
1406 if (portname.find_first_of (':') != string::npos) {
1410 str = jack_client_name;
1418 AudioEngine::is_realtime () const
1421 return jack_is_realtime (_jack);