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/port.h"
47 #include "ardour/port_set.h"
48 #include "ardour/session.h"
49 #include "ardour/timestamps.h"
50 #include "ardour/utils.h"
55 using namespace ARDOUR;
58 gint AudioEngine::m_meter_exit;
60 AudioEngine::AudioEngine (string client_name)
64 session_remove_pending = false;
67 last_monitor_check = 0;
68 monitor_check_interval = max_frames;
69 _processed_frames = 0;
74 _freewheel_thread_registered = false;
75 _freewheeling = false;
78 g_atomic_int_set (&m_meter_exit, 0);
80 if (connect_to_jack (client_name)) {
81 throw NoBackendAvailable ();
84 Port::set_engine (this);
86 // Initialize parameter metadata (e.g. ranges)
87 Evoral::Parameter p(NullAutomation);
88 p = EventTypeMap::instance().new_parameter(NullAutomation);
89 p = EventTypeMap::instance().new_parameter(GainAutomation);
90 p = EventTypeMap::instance().new_parameter(PanAutomation);
91 p = EventTypeMap::instance().new_parameter(PluginAutomation);
92 p = EventTypeMap::instance().new_parameter(SoloAutomation);
93 p = EventTypeMap::instance().new_parameter(MuteAutomation);
94 p = EventTypeMap::instance().new_parameter(MidiCCAutomation);
95 p = EventTypeMap::instance().new_parameter(MidiPgmChangeAutomation);
96 p = EventTypeMap::instance().new_parameter(MidiPitchBenderAutomation);
97 p = EventTypeMap::instance().new_parameter(MidiChannelPressureAutomation);
98 p = EventTypeMap::instance().new_parameter(FadeInAutomation);
99 p = EventTypeMap::instance().new_parameter(FadeOutAutomation);
100 p = EventTypeMap::instance().new_parameter(EnvelopeAutomation);
101 p = EventTypeMap::instance().new_parameter(MidiCCAutomation);
104 AudioEngine::~AudioEngine ()
107 Glib::Mutex::Lock tm (_process_lock);
108 session_removed.signal ();
111 jack_client_close (_jack);
115 stop_metering_thread ();
120 AudioEngine::jack() const
126 _thread_init_callback (void * /*arg*/)
128 /* make sure that anybody who needs to know about this thread
132 PBD::notify_gui_about_thread_creation (pthread_self(), X_("Audioengine"), 4096);
133 #ifdef WITH_JACK_MIDI
134 MIDI::JACK_MidiPort::set_process_thread (pthread_self());
135 #endif // WITH_JACK_MIDI
139 AudioEngine::start ()
142 error << _("AudioEngine::start() called while disconnected from JACK") << endmsg;
148 nframes_t blocksize = jack_get_buffer_size (_jack);
151 BootMessage (_("Connect session to engine"));
153 session->set_block_size (blocksize);
154 session->set_frame_rate (jack_get_sample_rate (_jack));
156 /* page in as much of the session process code as we
157 can before we really start running.
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);
167 session->process (blocksize);
170 _processed_frames = 0;
171 last_monitor_check = 0;
173 jack_on_shutdown (_jack, halted, this);
174 jack_set_graph_order_callback (_jack, _graph_order_callback, this);
175 jack_set_thread_init_callback (_jack, _thread_init_callback, this);
176 jack_set_process_callback (_jack, _process_callback, this);
177 jack_set_sample_rate_callback (_jack, _sample_rate_callback, this);
178 jack_set_buffer_size_callback (_jack, _bufsize_callback, this);
179 jack_set_xrun_callback (_jack, _xrun_callback, this);
180 jack_set_sync_callback (_jack, _jack_sync_callback, this);
181 jack_set_freewheel_callback (_jack, _freewheel_callback, this);
183 if (session && session->config.get_jack_time_master()) {
184 jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
187 if (jack_activate (_jack) == 0) {
190 Running(); /* EMIT SIGNAL */
192 // error << _("cannot activate JACK client") << endmsg;
195 start_metering_thread();
197 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
200 return _running ? 0 : -1;
204 AudioEngine::stop (bool forever)
208 disconnect_from_jack ();
210 jack_deactivate (_jack);
211 Stopped(); /* EMIT SIGNAL */
215 return _running ? -1 : 0;
220 AudioEngine::get_sync_offset (nframes_t& offset) const
223 #ifdef HAVE_JACK_VIDEO_SUPPORT
228 (void) jack_transport_query (_jack, &pos);
230 if (pos.valid & JackVideoFrameOffset) {
231 offset = pos.video_offset;
244 AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
245 jack_position_t* pos, int new_position, void *arg)
247 static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
251 AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
252 jack_position_t* pos, int new_position)
254 if (_jack && session && session->synced_to_jack()) {
255 session->jack_timebase_callback (state, nframes, pos, new_position);
260 AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
262 return static_cast<AudioEngine*> (arg)->jack_sync_callback (state, pos);
266 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
268 if (_jack && session) {
269 return session->jack_sync_callback (state, pos);
276 AudioEngine::_xrun_callback (void *arg)
278 AudioEngine* ae = static_cast<AudioEngine*> (arg);
279 if (ae->connected()) {
280 ae->Xrun (); /* EMIT SIGNAL */
286 AudioEngine::_graph_order_callback (void *arg)
288 AudioEngine* ae = static_cast<AudioEngine*> (arg);
289 if (ae->connected()) {
290 ae->GraphReordered (); /* EMIT SIGNAL */
295 /** Wrapped which is called by JACK as its process callback. It is just
296 * here to get us back into C++ land by calling AudioEngine::process_callback()
297 * @param nframes Number of frames passed by JACK.
298 * @param arg User argument passed by JACK, which will be the AudioEngine*.
301 AudioEngine::_process_callback (nframes_t nframes, void *arg)
303 return static_cast<AudioEngine *> (arg)->process_callback (nframes);
307 AudioEngine::_freewheel_callback (int onoff, void *arg)
309 static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
313 AudioEngine::split_cycle (nframes_t offset)
315 /* caller must hold process lock */
317 Port::increment_port_offset (offset);
319 /* tell all Ports that we're going to start a new (split) cycle */
321 boost::shared_ptr<Ports> p = ports.reader();
323 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
324 (*i)->cycle_split ();
328 /** Method called by JACK (via _process_callback) which says that there
329 * is work to be done.
330 * @param nframes Number of frames to process.
333 AudioEngine::process_callback (nframes_t nframes)
335 // CycleTimer ct ("AudioEngine::process");
337 Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
339 /// The number of frames that will have been processed when we've finished
340 nframes_t next_processed_frames;
342 /* handle wrap around of total frames counter */
344 if (max_frames - _processed_frames < nframes) {
345 next_processed_frames = nframes - (max_frames - _processed_frames);
347 next_processed_frames = _processed_frames + nframes;
350 if (!tm.locked() || session == 0) {
351 /* return having done nothing */
352 _processed_frames = next_processed_frames;
356 if (session_remove_pending) {
357 /* perform the actual session removal */
359 session_remove_pending = false;
360 session_removed.signal();
361 _processed_frames = next_processed_frames;
365 /* tell all relevant objects that we're starting a new cycle */
367 Delivery::CycleStart (nframes);
368 Port::set_port_offset (0);
369 InternalReturn::CycleStart (nframes);
371 /* tell all Ports that we're starting a new cycle */
373 boost::shared_ptr<Ports> p = ports.reader();
375 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
376 (*i)->cycle_start (nframes);
380 /* emit the Freewheel signal and stop freewheeling in the event of trouble */
381 if (Freewheel (nframes)) {
382 jack_set_freewheel (_jack, false);
387 session->process (nframes);
396 _processed_frames = next_processed_frames;
400 if (last_monitor_check + monitor_check_interval < next_processed_frames) {
402 boost::shared_ptr<Ports> p = ports.reader();
404 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
409 if (port->_last_monitor != (x = port->monitoring_input ())) {
410 port->_last_monitor = x;
411 /* XXX I think this is dangerous, due to
412 a likely mutex in the signal handlers ...
414 port->MonitorInputChanged (x); /* EMIT SIGNAL */
417 last_monitor_check = next_processed_frames;
420 if (session->silent()) {
422 boost::shared_ptr<Ports> p = ports.reader();
424 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
428 if (port->sends_output()) {
429 port->get_buffer(nframes).silence(nframes);
436 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
437 (*i)->cycle_end (nframes);
440 _processed_frames = next_processed_frames;
445 AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
447 return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
451 AudioEngine::jack_sample_rate_callback (nframes_t nframes)
453 _frame_rate = nframes;
454 _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
456 /* check for monitor input change every 1/10th of second */
458 monitor_check_interval = nframes / 10;
459 last_monitor_check = 0;
462 session->set_frame_rate (nframes);
465 SampleRateChanged (nframes); /* EMIT SIGNAL */
471 AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
473 return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
477 AudioEngine::jack_bufsize_callback (nframes_t nframes)
479 _buffer_size = nframes;
480 _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof(float);
481 cout << "FIXME: Assuming maximum MIDI buffer size " << nframes * 4 << "bytes" << endl;
482 _raw_buffer_sizes[DataType::MIDI] = nframes * 4;
483 _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
484 last_monitor_check = 0;
486 boost::shared_ptr<Ports> p = ports.reader();
488 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
493 session->set_block_size (_buffer_size);
500 AudioEngine::stop_metering_thread ()
502 if (m_meter_thread) {
503 g_atomic_int_set (&m_meter_exit, 1);
504 m_meter_thread->join ();
510 AudioEngine::start_metering_thread ()
512 if (m_meter_thread == 0) {
513 g_atomic_int_set (&m_meter_exit, 0);
514 m_meter_thread = Glib::Thread::create (sigc::mem_fun(this, &AudioEngine::meter_thread),
515 500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
520 AudioEngine::meter_thread ()
523 Glib::usleep (10000); /* 1/100th sec interval */
524 if (g_atomic_int_get(&m_meter_exit)) {
527 Metering::update_meters ();
532 AudioEngine::set_session (Session *s)
534 Glib::Mutex::Lock pl (_process_lock);
540 nframes_t blocksize = jack_get_buffer_size (_jack);
542 /* page in as much of the session process code as we
543 can before we really start running.
546 boost::shared_ptr<Ports> p = ports.reader();
548 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
549 (*i)->cycle_start (blocksize);
552 s->process (blocksize);
553 s->process (blocksize);
554 s->process (blocksize);
555 s->process (blocksize);
556 s->process (blocksize);
557 s->process (blocksize);
558 s->process (blocksize);
559 s->process (blocksize);
561 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
562 (*i)->cycle_end (blocksize);
568 AudioEngine::remove_session ()
570 Glib::Mutex::Lock lm (_process_lock);
575 session_remove_pending = true;
576 session_removed.wait(_process_lock);
583 //FIXME: Preliminary bugfix for http://tracker.ardour.org/view.php?id=1985
584 //remove_all_ports ();
588 AudioEngine::port_registration_failure (const std::string& portname)
590 string full_portname = jack_client_name;
591 full_portname += ':';
592 full_portname += portname;
595 jack_port_t* p = jack_port_by_name (_jack, full_portname.c_str());
599 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
601 reason = _("No more JACK ports are available. You will need to stop Ardour and restart JACK with ports if you need this many tracks.");
604 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
608 AudioEngine::register_port (DataType dtype, const string& portname, bool input)
613 if (dtype == DataType::AUDIO) {
614 newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput));
615 } else if (dtype == DataType::MIDI) {
616 newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput));
618 throw PortRegistrationFailure("unable to create port (unknown type)");
621 size_t& old_buffer_size = _raw_buffer_sizes[newport->type()];
622 size_t port_buffer_size = newport->raw_buffer_size(0);
623 if (port_buffer_size > old_buffer_size) {
624 old_buffer_size = port_buffer_size;
627 RCUWriter<Ports> writer (ports);
628 boost::shared_ptr<Ports> ps = writer.get_copy ();
629 ps->insert (ps->begin(), newport);
631 /* writer goes out of scope, forces update */
636 catch (PortRegistrationFailure& err) {
638 } catch (std::exception& e) {
639 throw PortRegistrationFailure(string_compose(
640 _("unable to create port: %1"), e.what()).c_str());
642 throw PortRegistrationFailure("unable to create port (unknown error)");
647 AudioEngine::register_input_port (DataType type, const string& portname)
649 return register_port (type, portname, true);
653 AudioEngine::register_output_port (DataType type, const string& portname)
655 return register_port (type, portname, false);
659 AudioEngine::unregister_port (Port& port)
661 /* caller must hold process lock */
664 /* probably happening when the engine has been halted by JACK,
665 in which case, there is nothing we can do here.
671 RCUWriter<Ports> writer (ports);
672 boost::shared_ptr<Ports> ps = writer.get_copy ();
674 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
682 /* writer goes out of scope, forces update */
685 remove_connections_for (port);
691 AudioEngine::connect (const string& source, const string& destination)
693 /* caller must hold process lock */
699 fatal << _("connect called before engine was started") << endmsg;
706 string s = make_port_name_non_relative (source);
707 string d = make_port_name_non_relative (destination);
709 Port* src = get_port_by_name_locked (s);
710 Port* dst = get_port_by_name_locked (d);
713 ret = src->connect (d);
715 ret = dst->connect (s);
717 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
722 /* already exists - no error, no warning */
723 } else if (ret < 0) {
724 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
725 source, s, destination, d)
733 AudioEngine::disconnect (const string& source, const string& destination)
735 /* caller must hold process lock */
741 fatal << _("disconnect called before engine was started") << endmsg;
748 string s = make_port_name_non_relative (source);
749 string d = make_port_name_non_relative (destination);
751 Port* src = get_port_by_name_locked (s);
752 Port* dst = get_port_by_name_locked (d);
755 ret = src->disconnect (d);
757 ret = dst->disconnect (s);
759 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
766 AudioEngine::disconnect (Port& port)
770 fatal << _("disconnect called before engine was started") << endmsg;
777 return port.disconnect_all ();
781 AudioEngine::frame_rate ()
784 if (_frame_rate == 0) {
785 return (_frame_rate = jack_get_sample_rate (_jack));
790 fatal << X_("programming error: AudioEngine::frame_rate() called while disconnected from JACK")
798 AudioEngine::raw_buffer_size (DataType t)
800 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
801 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
805 AudioEngine::frames_per_cycle ()
808 if (_buffer_size == 0) {
809 return (_buffer_size = jack_get_buffer_size (_jack));
814 fatal << X_("programming error: AudioEngine::frame_rate() called while disconnected from JACK")
821 /** @param name Full name of port (including prefix:)
822 * @return Corresponding Port*, or 0. This object remains the property of the AudioEngine
823 * so must not be deleted.
826 AudioEngine::get_port_by_name (const string& portname)
829 if (portname.find_first_of (':') == string::npos) {
830 s = make_port_name_non_relative (portname);
835 Glib::Mutex::Lock lm (_process_lock);
836 return get_port_by_name_locked (s);
840 AudioEngine::get_port_by_name_locked (const string& portname)
842 /* caller must hold process lock */
846 fatal << _("get_port_by_name_locked() called before engine was started") << endmsg;
853 if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
854 /* not an ardour: port */
858 std::string const rel = make_port_name_relative (portname);
860 boost::shared_ptr<Ports> pr = ports.reader();
862 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
863 if (rel == (*i)->name()) {
876 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
880 fatal << _("get_ports called before engine was started") << endmsg;
886 return jack_get_ports (_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
890 AudioEngine::halted (void *arg)
892 AudioEngine* ae = static_cast<AudioEngine *> (arg);
893 bool was_running = ae->_running;
895 ae->stop_metering_thread ();
897 ae->_running = false;
898 ae->_buffer_size = 0;
902 ae->Halted(); /* EMIT SIGNAL */
907 AudioEngine::can_request_hardware_monitoring ()
915 if ((ports = jack_get_ports (_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
926 AudioEngine::n_physical_outputs (DataType type) const
935 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
939 for (i = 0; ports[i]; ++i) {}
946 AudioEngine::n_physical_inputs (DataType type) const
955 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
960 for (i = 0; ports[i]; ++i) {}
967 AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
976 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
981 for (i = 0; ports[i]; ++i) {
982 ins.push_back (ports[i]);
989 AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
998 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1003 for (i = 0; ports[i]; ++i) {
1004 outs.push_back (ports[i]);
1011 AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
1013 const char ** ports;
1017 assert(type != DataType::NIL);
1019 if (!_running || !_jack) {
1021 fatal << _("get_nth_physical called before engine was started") << endmsg;
1028 ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|flag);
1034 for (i = 0; i < n && ports[i]; ++i) {}
1040 free ((char *) ports);
1046 AudioEngine::update_total_latency (const Port& port)
1049 fatal << _("update_total_latency() called with no JACK client connection") << endmsg;
1055 fatal << _("update_total_latency() called before engine was started") << endmsg;
1060 port.recompute_total_latency ();
1064 AudioEngine::transport_stop ()
1067 jack_transport_stop (_jack);
1072 AudioEngine::transport_start ()
1075 jack_transport_start (_jack);
1080 AudioEngine::transport_locate (nframes_t where)
1082 // cerr << "tell JACK to locate to " << where << endl;
1084 jack_transport_locate (_jack, where);
1088 AudioEngine::TransportState
1089 AudioEngine::transport_state ()
1092 jack_position_t pos;
1093 return (TransportState) jack_transport_query (_jack, &pos);
1095 return (TransportState) JackTransportStopped;
1100 AudioEngine::reset_timebase ()
1102 if (_jack && session) {
1103 if (session->config.get_jack_time_master()) {
1104 return jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
1106 return jack_release_timebase (_jack);
1114 AudioEngine::freewheel (bool onoff)
1118 if (onoff != _freewheeling) {
1121 _freewheel_thread_registered = false;
1124 return jack_set_freewheel (_jack, onoff);
1127 /* already doing what has been asked for */
1137 AudioEngine::remove_all_ports ()
1139 /* process lock MUST be held */
1142 RCUWriter<Ports> writer (ports);
1143 boost::shared_ptr<Ports> ps = writer.get_copy ();
1149 AudioEngine::remove_connections_for (Port& port)
1151 for (PortConnections::iterator i = port_connections.begin(); i != port_connections.end(); ) {
1152 PortConnections::iterator tmp;
1157 if ((*i).first == port.name()) {
1158 port_connections.erase (i);
1167 ardour_jack_error (const char* msg)
1169 error << "JACK: " << msg << endmsg;
1173 AudioEngine::connect_to_jack (string client_name)
1175 jack_options_t options = JackNullOption;
1176 jack_status_t status;
1177 const char *server_name = NULL;
1179 jack_client_name = client_name; /* might be reset below */
1180 _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
1182 if (_jack == NULL) {
1183 // error message is not useful here
1187 if (status & JackNameNotUnique) {
1188 jack_client_name = jack_get_client_name (_jack);
1191 jack_set_error_function (ardour_jack_error);
1197 AudioEngine::disconnect_from_jack ()
1204 stop_metering_thread ();
1208 Glib::Mutex::Lock lm (_process_lock);
1209 jack_client_close (_jack);
1215 _raw_buffer_sizes.clear();
1219 Stopped(); /* EMIT SIGNAL */
1226 AudioEngine::reconnect_to_jack ()
1229 disconnect_from_jack ();
1230 /* XXX give jackd a chance */
1231 Glib::usleep (250000);
1234 if (connect_to_jack (jack_client_name)) {
1235 error << _("failed to connect to JACK") << endmsg;
1241 boost::shared_ptr<Ports> p = ports.reader ();
1243 for (i = p->begin(); i != p->end(); ++i) {
1244 if ((*i)->reestablish ()) {
1249 if (i != p->end()) {
1251 remove_all_ports ();
1257 session->reset_jack_connection (_jack);
1258 nframes_t blocksize = jack_get_buffer_size (_jack);
1259 session->set_block_size (blocksize);
1260 session->set_frame_rate (jack_get_sample_rate (_jack));
1262 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
1263 cout << "FIXME: Assuming maximum MIDI buffer size " << blocksize * 4 << "bytes" << endl;
1264 _raw_buffer_sizes[DataType::MIDI] = blocksize * 4;
1267 last_monitor_check = 0;
1269 jack_on_shutdown (_jack, halted, this);
1270 jack_set_graph_order_callback (_jack, _graph_order_callback, this);
1271 jack_set_thread_init_callback (_jack, _thread_init_callback, this);
1272 jack_set_process_callback (_jack, _process_callback, this);
1273 jack_set_sample_rate_callback (_jack, _sample_rate_callback, this);
1274 jack_set_buffer_size_callback (_jack, _bufsize_callback, this);
1275 jack_set_xrun_callback (_jack, _xrun_callback, this);
1276 jack_set_sync_callback (_jack, _jack_sync_callback, this);
1277 jack_set_freewheel_callback (_jack, _freewheel_callback, this);
1279 if (session && session->config.get_jack_time_master()) {
1280 jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
1283 if (jack_activate (_jack) == 0) {
1290 /* re-establish connections */
1292 for (i = p->begin(); i != p->end(); ++i) {
1296 Running (); /* EMIT SIGNAL*/
1298 start_metering_thread ();
1304 AudioEngine::request_buffer_size (nframes_t nframes)
1308 if (nframes == jack_get_buffer_size (_jack)) {
1312 return jack_set_buffer_size (_jack, nframes);
1320 AudioEngine::update_total_latencies ()
1322 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1324 jack_recompute_total_latencies (_jack);
1330 AudioEngine::make_port_name_relative (string portname)
1332 string::size_type len;
1333 string::size_type n;
1335 len = portname.length();
1337 for (n = 0; n < len; ++n) {
1338 if (portname[n] == ':') {
1343 if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
1344 return portname.substr (n+1);
1351 AudioEngine::make_port_name_non_relative (string portname)
1355 if (portname.find_first_of (':') != string::npos) {
1359 str = jack_client_name;
1367 AudioEngine::is_realtime () const
1370 return jack_is_realtime (_jack);