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 // Initialize parameter metadata (e.g. ranges)
90 Evoral::Parameter p(NullAutomation);
91 p = EventTypeMap::instance().new_parameter(NullAutomation);
92 p = EventTypeMap::instance().new_parameter(GainAutomation);
93 p = EventTypeMap::instance().new_parameter(PanAutomation);
94 p = EventTypeMap::instance().new_parameter(PluginAutomation);
95 p = EventTypeMap::instance().new_parameter(SoloAutomation);
96 p = EventTypeMap::instance().new_parameter(MuteAutomation);
97 p = EventTypeMap::instance().new_parameter(MidiCCAutomation);
98 p = EventTypeMap::instance().new_parameter(MidiPgmChangeAutomation);
99 p = EventTypeMap::instance().new_parameter(MidiPitchBenderAutomation);
100 p = EventTypeMap::instance().new_parameter(MidiChannelPressureAutomation);
101 p = EventTypeMap::instance().new_parameter(FadeInAutomation);
102 p = EventTypeMap::instance().new_parameter(FadeOutAutomation);
103 p = EventTypeMap::instance().new_parameter(EnvelopeAutomation);
104 p = EventTypeMap::instance().new_parameter(MidiCCAutomation);
107 AudioEngine::~AudioEngine ()
110 Glib::Mutex::Lock tm (_process_lock);
111 session_removed.signal ();
114 jack_client_close (_jack);
118 stop_metering_thread ();
123 AudioEngine::jack() const
129 _thread_init_callback (void *arg)
131 /* make sure that anybody who needs to know about this thread
135 PBD::ThreadCreatedWithRequestSize (pthread_self(), X_("Audioengine"), 4096);
136 MIDI::JACK_MidiPort::set_process_thread (pthread_self());
140 AudioEngine::start ()
143 error << _("AudioEngine::start() called while disconnected from JACK") << endmsg;
150 nframes_t blocksize = jack_get_buffer_size (_jack);
152 BootMessage (_("Connect session to engine"));
154 session->set_block_size (blocksize);
155 session->set_frame_rate (jack_get_sample_rate (_jack));
157 /* page in as much of the session process code as we
158 can before we really start running.
161 session->process (blocksize);
162 session->process (blocksize);
163 session->process (blocksize);
164 session->process (blocksize);
165 session->process (blocksize);
166 session->process (blocksize);
167 session->process (blocksize);
168 session->process (blocksize);
171 _processed_frames = 0;
172 last_monitor_check = 0;
174 jack_on_shutdown (_jack, halted, this);
175 jack_set_graph_order_callback (_jack, _graph_order_callback, this);
176 jack_set_thread_init_callback (_jack, _thread_init_callback, this);
177 jack_set_process_callback (_jack, _process_callback, this);
178 jack_set_sample_rate_callback (_jack, _sample_rate_callback, this);
179 jack_set_buffer_size_callback (_jack, _bufsize_callback, this);
180 jack_set_xrun_callback (_jack, _xrun_callback, this);
181 jack_set_sync_callback (_jack, _jack_sync_callback, this);
182 jack_set_freewheel_callback (_jack, _freewheel_callback, this);
184 if (Config->get_jack_time_master()) {
185 jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
188 if (jack_activate (_jack) == 0) {
191 Running(); /* EMIT SIGNAL */
193 // error << _("cannot activate JACK client") << endmsg;
196 start_metering_thread();
199 return _running ? 0 : -1;
203 AudioEngine::stop (bool forever)
207 disconnect_from_jack ();
209 jack_deactivate (_jack);
210 Stopped(); /* EMIT SIGNAL */
214 return _running ? -1 : 0;
219 AudioEngine::get_sync_offset (nframes_t& offset) const
222 #ifdef HAVE_JACK_VIDEO_SUPPORT
227 (void) jack_transport_query (_jack, &pos);
229 if (pos.valid & JackVideoFrameOffset) {
230 offset = pos.video_offset;
241 AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
242 jack_position_t* pos, int new_position, void *arg)
244 static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
248 AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
249 jack_position_t* pos, int new_position)
251 if (_jack && session && session->synced_to_jack()) {
252 session->jack_timebase_callback (state, nframes, pos, new_position);
257 AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
259 return static_cast<AudioEngine*> (arg)->jack_sync_callback (state, pos);
263 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
265 if (_jack && session) {
266 return session->jack_sync_callback (state, pos);
273 AudioEngine::_xrun_callback (void *arg)
275 AudioEngine* ae = static_cast<AudioEngine*> (arg);
276 if (ae->connected()) {
277 ae->Xrun (); /* EMIT SIGNAL */
283 AudioEngine::_graph_order_callback (void *arg)
285 AudioEngine* ae = static_cast<AudioEngine*> (arg);
286 if (ae->connected()) {
287 ae->GraphReordered (); /* EMIT SIGNAL */
292 /** Wrapped which is called by JACK as its process callback. It is just
293 * here to get us back into C++ land by calling AudioEngine::process_callback()
294 * @param nframes Number of frames passed by JACK.
295 * @param arg User argument passed by JACK, which will be the AudioEngine*.
298 AudioEngine::_process_callback (nframes_t nframes, void *arg)
300 return static_cast<AudioEngine *> (arg)->process_callback (nframes);
304 AudioEngine::_freewheel_callback (int onoff, void *arg)
306 static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
309 /** Method called by JACK (via _process_callback) which says that there
310 * is work to be done.
311 * @param nframes Number of frames to process.
314 AudioEngine::process_callback (nframes_t nframes)
316 // CycleTimer ct ("AudioEngine::process");
317 Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
319 /// The number of frames that will have been processed when we've finished
320 nframes_t next_processed_frames;
322 /* handle wrap around of total frames counter */
324 if (max_frames - _processed_frames < nframes) {
325 next_processed_frames = nframes - (max_frames - _processed_frames);
327 next_processed_frames = _processed_frames + nframes;
330 if (!tm.locked() || session == 0) {
331 /* return having done nothing */
332 _processed_frames = next_processed_frames;
336 if (session_remove_pending) {
337 /* perform the actual session removal */
339 session_remove_pending = false;
340 session_removed.signal();
341 _processed_frames = next_processed_frames;
345 boost::shared_ptr<Ports> p = ports.reader();
347 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
349 /* Only run cycle_start() on output ports, because
350 inputs must be done in the correct processing order,
351 which requires interleaving with route processing.
354 if ((*i)->sends_output()) {
355 (*i)->cycle_start (nframes, 0);
360 /* emit the Freewheel signal and stop freewheeling in the event of trouble */
361 if (Freewheel (nframes)) {
362 jack_set_freewheel (_jack, false);
367 session->process (nframes);
371 // Finalize ports (ie write data if necessary)
373 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
374 (*i)->cycle_end (nframes, 0);
382 _processed_frames = next_processed_frames;
386 if (last_monitor_check + monitor_check_interval < next_processed_frames) {
388 boost::shared_ptr<Ports> p = ports.reader();
390 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
395 if (port->_last_monitor != (x = port->monitoring_input ())) {
396 port->_last_monitor = x;
397 /* XXX I think this is dangerous, due to
398 a likely mutex in the signal handlers ...
400 port->MonitorInputChanged (x); /* EMIT SIGNAL */
403 last_monitor_check = next_processed_frames;
406 if (session->silent()) {
408 boost::shared_ptr<Ports> p = ports.reader();
410 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
414 if (port->sends_output()) {
415 port->get_buffer().silence(nframes);
420 _processed_frames = next_processed_frames;
425 AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
427 return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
431 AudioEngine::jack_sample_rate_callback (nframes_t nframes)
433 _frame_rate = nframes;
434 _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
436 /* check for monitor input change every 1/10th of second */
438 monitor_check_interval = nframes / 10;
439 last_monitor_check = 0;
442 session->set_frame_rate (nframes);
445 SampleRateChanged (nframes); /* EMIT SIGNAL */
451 AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
453 return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
457 AudioEngine::jack_bufsize_callback (nframes_t nframes)
459 _buffer_size = nframes;
460 _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
461 last_monitor_check = 0;
463 boost::shared_ptr<Ports> p = ports.reader();
465 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
470 session->set_block_size (_buffer_size);
477 AudioEngine::stop_metering_thread ()
479 if (m_meter_thread) {
480 g_atomic_int_set (&m_meter_exit, 1);
481 m_meter_thread->join ();
487 AudioEngine::start_metering_thread ()
489 if (m_meter_thread == 0) {
490 g_atomic_int_set (&m_meter_exit, 0);
491 m_meter_thread = Glib::Thread::create (sigc::mem_fun(this, &AudioEngine::meter_thread),
492 500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
497 AudioEngine::meter_thread ()
500 Glib::usleep (10000); /* 1/100th sec interval */
501 if (g_atomic_int_get(&m_meter_exit)) {
504 IO::update_meters ();
509 AudioEngine::set_session (Session *s)
511 Glib::Mutex::Lock pl (_process_lock);
517 nframes_t blocksize = jack_get_buffer_size (_jack);
519 /* page in as much of the session process code as we
520 can before we really start running.
523 boost::shared_ptr<Ports> p = ports.reader();
525 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
526 if ((*i)->sends_output()) {
527 (*i)->cycle_start (blocksize, 0);
531 s->process (blocksize);
532 s->process (blocksize);
533 s->process (blocksize);
534 s->process (blocksize);
535 s->process (blocksize);
536 s->process (blocksize);
537 s->process (blocksize);
538 s->process (blocksize);
540 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
541 (*i)->cycle_end (blocksize, 0);
547 AudioEngine::remove_session ()
549 Glib::Mutex::Lock lm (_process_lock);
554 session_remove_pending = true;
555 session_removed.wait(_process_lock);
562 //FIXME: Preliminary bugfix for http://tracker.ardour.org/view.php?id=1985
563 //remove_all_ports ();
567 AudioEngine::port_registration_failure (const std::string& portname)
569 string full_portname = jack_client_name;
570 full_portname += ':';
571 full_portname += portname;
574 jack_port_t* p = jack_port_by_name (_jack, full_portname.c_str());
578 reason = _("a port with this name already exists: check for duplicated track/bus names");
580 reason = _("unknown error");
583 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
587 AudioEngine::register_port (DataType dtype, const string& portname, bool input, bool publish)
591 /*cerr << "trying to register port with name " << portname << endl;*/
593 if (dtype == DataType::AUDIO) {
594 newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput), publish, frames_per_cycle());
595 } else if (dtype == DataType::MIDI) {
596 newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput), publish, frames_per_cycle());
598 throw unknown_type();
601 /*cerr << "successfully got port " << portname << " with address " << newport << endl;*/
603 RCUWriter<Ports> writer (ports);
604 boost::shared_ptr<Ports> ps = writer.get_copy ();
605 /*cerr << "Address of ports list: " << ps << endl
606 << "Ports set size before insert: " << ps->size() << endl;*/
607 ps->insert (ps->begin(), newport);
608 /*cerr << "Ports set size after insert: " << ps->size() << endl;*/
610 /* writer goes out of scope, forces update */
616 throw PortRegistrationFailure("unable to create port (unknown type?)");
621 AudioEngine::get_port (const std::string& full_name)
623 boost::shared_ptr<Ports> p = ports.reader();
625 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
626 //cerr << "comparing port name '" << (*i)->name() << "' with '" << full_name << "'" << endl;
627 if ((*i)->name() == full_name) {
636 AudioEngine::register_input_port (DataType type, const string& portname, bool publish)
638 return register_port (type, portname, true, publish);
642 AudioEngine::register_output_port (DataType type, const string& portname, bool publish)
644 return register_port (type, portname, false, publish);
648 AudioEngine::unregister_port (Port& port)
650 /* caller must hold process lock */
652 cerr << "about to unregister Port xx x" << &port << "\n";
655 /* probably happening when the engine has been halted by JACK,
656 in which case, there is nothing we can do here.
658 cerr << "not running\n";
663 cerr << "before getcopy\n";
665 RCUWriter<Ports> writer (ports);
666 boost::shared_ptr<Ports> ps = writer.get_copy ();
668 cerr << "Ports set size: " << ps.get()->size() << endl;
670 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
671 cerr << "before delete" << endl;
673 cerr << "About to delete " << &port << endl;
676 cerr << "After erasing ports size: " << ps->size();
681 /* writer goes out of scope, forces update */
684 cerr << "before remove_connections\n";
685 remove_connections_for (port);
691 AudioEngine::connect (const string& source, const string& destination)
697 fatal << _("connect called before engine was started") << endmsg;
704 string s = make_port_name_non_relative (source);
705 string d = make_port_name_non_relative (destination);
707 //cerr << "Trying to connect source: " << s << " with destination " << d << endl;
709 Port* src = get_port (s);
710 Port* dst = get_port (d);
714 /* both ports are known to us, so do the internal connect stuff */
716 if ((ret = src->connect (*dst)) == 0) {
717 ret = dst->connect (*src);
720 } else if (src || dst) {
722 /* one port is known to us, try to connect it to something external */
724 PortConnectableByName* pcn;
728 pcn = dynamic_cast<PortConnectableByName*>(src);
731 pcn = dynamic_cast<PortConnectableByName*>(dst);
736 ret = pcn->connect (other);
743 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
750 error << string_compose(_("AudioEngine: connection already exists: %1 (%2) to %3 (%4)"),
751 source, s, destination, d)
753 } else if (ret < 0) {
754 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
755 source, s, destination, d)
763 AudioEngine::disconnect (const string& source, const string& destination)
769 fatal << _("disconnect called before engine was started") << endmsg;
776 string s = make_port_name_non_relative (source);
777 string d = make_port_name_non_relative (destination);
779 //cerr << "trying to disconnect port '" << s << "' from port '" << d << endl;
781 Port* src = get_port (s);
782 Port* dst = get_port (d);
786 /* both ports are known to us, so do the internal connect stuff */
788 if ((ret = src->disconnect (*dst)) == 0) {
789 ret = dst->disconnect (*src);
792 } else if (src || dst) {
794 /* one port is known to us, try to connect it to something external */
797 PortConnectableByName* pcn;
801 pcn = dynamic_cast<PortConnectableByName*>(src);
804 pcn = dynamic_cast<PortConnectableByName*>(dst);
809 ret = pcn->disconnect (other);
816 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
826 AudioEngine::disconnect (Port& port)
830 fatal << _("disconnect called before engine was started") << endmsg;
837 return port.disconnect_all ();
841 AudioEngine::frame_rate ()
844 if (_frame_rate == 0) {
845 return (_frame_rate = jack_get_sample_rate (_jack));
850 fatal << X_("programming error: AudioEngine::frame_rate() called while disconnected from JACK")
858 AudioEngine::frames_per_cycle ()
861 if (_buffer_size == 0) {
862 return (_buffer_size = jack_get_buffer_size (_jack));
867 fatal << X_("programming error: AudioEngine::frame_rate() called while disconnected from JACK")
874 /** Get a port by name.
875 * Note this can return NULL, it will NOT create a port if it is not found (any more).
878 AudioEngine::get_port_by_name (const string& portname, bool keep)
880 Glib::Mutex::Lock lm (_process_lock);
884 fatal << _("get_port_by_name() called before engine was started") << endmsg;
891 boost::shared_ptr<Ports> pr = ports.reader();
893 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
894 if (portname == (*i)->name()) {
903 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
907 fatal << _("get_ports called before engine was started") << endmsg;
913 return jack_get_ports (_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
917 AudioEngine::halted (void *arg)
919 AudioEngine* ae = static_cast<AudioEngine *> (arg);
920 bool was_running = ae->_running;
922 ae->stop_metering_thread ();
924 ae->_running = false;
925 ae->_buffer_size = 0;
928 cerr << "!!! HALTED !!!\n";
931 ae->Halted(); /* EMIT SIGNAL */
936 AudioEngine::can_request_hardware_monitoring ()
944 if ((ports = jack_get_ports (_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
955 AudioEngine::n_physical_outputs (DataType type) const
964 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
968 for (i = 0; ports[i]; ++i);
975 AudioEngine::n_physical_inputs (DataType type) const
984 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
989 for (i = 0; ports[i]; ++i);
996 AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
1005 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
1010 for (i = 0; ports[i]; ++i) {
1011 ins.push_back (ports[i]);
1018 AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
1020 const char ** ports;
1027 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1032 for (i = 0; ports[i]; ++i) {
1033 outs.push_back (ports[i]);
1040 AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
1042 const char ** ports;
1046 assert(type != DataType::NIL);
1048 if (!_running || !_jack) {
1050 fatal << _("get_nth_physical called before engine was started") << endmsg;
1057 ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|flag);
1063 for (i = 0; i < n && ports[i]; ++i);
1069 free ((char *) ports);
1075 AudioEngine::get_port_total_latency (const Port& port)
1077 return port.total_latency ();
1081 AudioEngine::update_total_latency (const Port& port)
1084 fatal << _("update_total_latency() called with no JACK client connection") << endmsg;
1090 fatal << _("update_total_latency() called before engine was started") << endmsg;
1095 port.recompute_total_latency ();
1099 AudioEngine::transport_stop ()
1102 jack_transport_stop (_jack);
1107 AudioEngine::transport_start ()
1109 // cerr << "tell JACK to start\n";
1111 jack_transport_start (_jack);
1116 AudioEngine::transport_locate (nframes_t where)
1118 // cerr << "tell JACK to locate to " << where << endl;
1120 jack_transport_locate (_jack, where);
1124 AudioEngine::TransportState
1125 AudioEngine::transport_state ()
1128 jack_position_t pos;
1129 return (TransportState) jack_transport_query (_jack, &pos);
1131 return (TransportState) JackTransportStopped;
1136 AudioEngine::reset_timebase ()
1139 if (Config->get_jack_time_master()) {
1140 return jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
1142 return jack_release_timebase (_jack);
1150 AudioEngine::freewheel (bool onoff)
1154 if (onoff != _freewheeling) {
1157 _freewheel_thread_registered = false;
1160 return jack_set_freewheel (_jack, onoff);
1163 /* already doing what has been asked for */
1173 AudioEngine::remove_all_ports ()
1175 /* process lock MUST be held */
1178 RCUWriter<Ports> writer (ports);
1179 boost::shared_ptr<Ports> ps = writer.get_copy ();
1185 AudioEngine::remove_connections_for (Port& port)
1187 for (PortConnections::iterator i = port_connections.begin(); i != port_connections.end(); ) {
1188 PortConnections::iterator tmp;
1193 if ((*i).first == port.name()) {
1194 port_connections.erase (i);
1202 #ifdef HAVE_JACK_CLIENT_OPEN
1205 AudioEngine::connect_to_jack (string client_name)
1207 jack_options_t options = JackNullOption;
1208 jack_status_t status;
1209 const char *server_name = NULL;
1211 jack_client_name = client_name; /* might be reset below */
1212 _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
1214 if (_jack == NULL) {
1215 // error message is not useful here
1219 if (status & JackNameNotUnique) {
1220 jack_client_name = jack_get_client_name (_jack);
1223 jack_set_error_function (ardour_jack_error);
1231 AudioEngine::connect_to_jack (string client_name)
1233 jack_client_name = client_name;
1235 if ((_jack = jack_client_new (client_name.c_str())) == 0) {
1242 #endif /* HAVE_JACK_CLIENT_OPEN */
1245 AudioEngine::disconnect_from_jack ()
1253 stop_metering_thread ();
1257 Glib::Mutex::Lock lm (_process_lock);
1258 jack_client_close (_jack);
1267 Stopped(); /* EMIT SIGNAL */
1274 AudioEngine::reconnect_to_jack ()
1277 disconnect_from_jack ();
1278 /* XXX give jackd a chance */
1279 Glib::usleep (250000);
1282 if (connect_to_jack (jack_client_name)) {
1283 error << _("failed to connect to JACK") << endmsg;
1289 boost::shared_ptr<Ports> p = ports.reader ();
1291 for (i = p->begin(); i != p->end(); ++i) {
1292 if ((*i)->reestablish ()) {
1297 if (i != p->end()) {
1299 remove_all_ports ();
1305 session->reset_jack_connection (_jack);
1306 nframes_t blocksize = jack_get_buffer_size (_jack);
1307 session->set_block_size (blocksize);
1308 session->set_frame_rate (jack_get_sample_rate (_jack));
1311 last_monitor_check = 0;
1313 jack_on_shutdown (_jack, halted, this);
1314 jack_set_graph_order_callback (_jack, _graph_order_callback, this);
1315 jack_set_thread_init_callback (_jack, _thread_init_callback, this);
1316 jack_set_process_callback (_jack, _process_callback, this);
1317 jack_set_sample_rate_callback (_jack, _sample_rate_callback, this);
1318 jack_set_buffer_size_callback (_jack, _bufsize_callback, this);
1319 jack_set_xrun_callback (_jack, _xrun_callback, this);
1320 jack_set_sync_callback (_jack, _jack_sync_callback, this);
1321 jack_set_freewheel_callback (_jack, _freewheel_callback, this);
1323 if (Config->get_jack_time_master()) {
1324 jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
1327 if (jack_activate (_jack) == 0) {
1334 /* re-establish connections */
1336 for (i = p->begin(); i != p->end(); ++i) {
1340 Running (); /* EMIT SIGNAL*/
1342 start_metering_thread ();
1348 AudioEngine::request_buffer_size (nframes_t nframes)
1352 if (nframes == jack_get_buffer_size (_jack)) {
1356 return jack_set_buffer_size (_jack, nframes);
1364 AudioEngine::update_total_latencies ()
1366 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1368 jack_recompute_total_latencies (_jack);
1374 AudioEngine::make_port_name_relative (string portname)
1376 string::size_type len;
1377 string::size_type n;
1379 len = portname.length();
1381 for (n = 0; n < len; ++n) {
1382 if (portname[n] == ':') {
1387 if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
1388 return portname.substr (n+1);
1395 AudioEngine::make_port_name_non_relative (string portname)
1399 if (portname.find_first_of (':') != string::npos) {
1403 str = jack_client_name;
1411 AudioEngine::is_realtime () const
1414 return jack_is_realtime (_jack);