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);
182 jack_set_port_registration_callback (_jack, _registration_callback, this);
184 if (session && session->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();
198 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
201 return _running ? 0 : -1;
205 AudioEngine::stop (bool forever)
209 disconnect_from_jack ();
211 jack_deactivate (_jack);
212 Stopped(); /* EMIT SIGNAL */
216 return _running ? -1 : 0;
221 AudioEngine::get_sync_offset (nframes_t& offset) const
224 #ifdef HAVE_JACK_VIDEO_SUPPORT
229 (void) jack_transport_query (_jack, &pos);
231 if (pos.valid & JackVideoFrameOffset) {
232 offset = pos.video_offset;
245 AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
246 jack_position_t* pos, int new_position, void *arg)
248 static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
252 AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
253 jack_position_t* pos, int new_position)
255 if (_jack && session && session->synced_to_jack()) {
256 session->jack_timebase_callback (state, nframes, pos, new_position);
261 AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
263 return static_cast<AudioEngine*> (arg)->jack_sync_callback (state, pos);
267 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
269 if (_jack && session) {
270 return session->jack_sync_callback (state, pos);
277 AudioEngine::_xrun_callback (void *arg)
279 AudioEngine* ae = static_cast<AudioEngine*> (arg);
280 if (ae->connected()) {
281 ae->Xrun (); /* EMIT SIGNAL */
287 AudioEngine::_graph_order_callback (void *arg)
289 AudioEngine* ae = static_cast<AudioEngine*> (arg);
290 if (ae->connected()) {
291 ae->GraphReordered (); /* EMIT SIGNAL */
296 /** Wrapped which is called by JACK as its process callback. It is just
297 * here to get us back into C++ land by calling AudioEngine::process_callback()
298 * @param nframes Number of frames passed by JACK.
299 * @param arg User argument passed by JACK, which will be the AudioEngine*.
302 AudioEngine::_process_callback (nframes_t nframes, void *arg)
304 return static_cast<AudioEngine *> (arg)->process_callback (nframes);
308 AudioEngine::_freewheel_callback (int onoff, void *arg)
310 static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
314 AudioEngine::_registration_callback (jack_port_id_t /*id*/, int /*reg*/, void* arg)
316 AudioEngine* ae = static_cast<AudioEngine*> (arg);
317 ae->PortRegisteredOrUnregistered (); /* EMIT SIGNAL */
321 AudioEngine::split_cycle (nframes_t offset)
323 /* caller must hold process lock */
325 Port::increment_port_offset (offset);
327 /* tell all Ports that we're going to start a new (split) cycle */
329 boost::shared_ptr<Ports> p = ports.reader();
331 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
332 (*i)->cycle_split ();
336 /** Method called by JACK (via _process_callback) which says that there
337 * is work to be done.
338 * @param nframes Number of frames to process.
341 AudioEngine::process_callback (nframes_t nframes)
343 // CycleTimer ct ("AudioEngine::process");
345 Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
347 /// The number of frames that will have been processed when we've finished
348 nframes_t next_processed_frames;
350 /* handle wrap around of total frames counter */
352 if (max_frames - _processed_frames < nframes) {
353 next_processed_frames = nframes - (max_frames - _processed_frames);
355 next_processed_frames = _processed_frames + nframes;
358 if (!tm.locked() || session == 0) {
359 /* return having done nothing */
360 _processed_frames = next_processed_frames;
364 if (session_remove_pending) {
365 /* perform the actual session removal */
367 session_remove_pending = false;
368 session_removed.signal();
369 _processed_frames = next_processed_frames;
373 /* tell all relevant objects that we're starting a new cycle */
375 Delivery::CycleStart (nframes);
376 Port::set_port_offset (0);
377 InternalReturn::CycleStart (nframes);
379 /* tell all Ports that we're starting a new cycle */
381 boost::shared_ptr<Ports> p = ports.reader();
383 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
384 (*i)->cycle_start (nframes);
388 /* emit the Freewheel signal and stop freewheeling in the event of trouble */
389 if (Freewheel (nframes)) {
390 jack_set_freewheel (_jack, false);
395 session->process (nframes);
404 _processed_frames = next_processed_frames;
408 if (last_monitor_check + monitor_check_interval < next_processed_frames) {
410 boost::shared_ptr<Ports> p = ports.reader();
412 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
417 if (port->_last_monitor != (x = port->monitoring_input ())) {
418 port->_last_monitor = x;
419 /* XXX I think this is dangerous, due to
420 a likely mutex in the signal handlers ...
422 port->MonitorInputChanged (x); /* EMIT SIGNAL */
425 last_monitor_check = next_processed_frames;
428 if (session->silent()) {
430 boost::shared_ptr<Ports> p = ports.reader();
432 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
436 if (port->sends_output()) {
437 port->get_buffer(nframes).silence(nframes);
444 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
445 (*i)->cycle_end (nframes);
448 _processed_frames = next_processed_frames;
453 AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
455 return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
459 AudioEngine::jack_sample_rate_callback (nframes_t nframes)
461 _frame_rate = nframes;
462 _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
464 /* check for monitor input change every 1/10th of second */
466 monitor_check_interval = nframes / 10;
467 last_monitor_check = 0;
470 session->set_frame_rate (nframes);
473 SampleRateChanged (nframes); /* EMIT SIGNAL */
479 AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
481 return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
485 AudioEngine::jack_bufsize_callback (nframes_t nframes)
487 _buffer_size = nframes;
488 _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof(float);
489 cout << "FIXME: Assuming maximum MIDI buffer size " << nframes * 4 << "bytes" << endl;
490 _raw_buffer_sizes[DataType::MIDI] = nframes * 4;
491 _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
492 last_monitor_check = 0;
494 boost::shared_ptr<Ports> p = ports.reader();
496 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
501 session->set_block_size (_buffer_size);
508 AudioEngine::stop_metering_thread ()
510 if (m_meter_thread) {
511 g_atomic_int_set (&m_meter_exit, 1);
512 m_meter_thread->join ();
518 AudioEngine::start_metering_thread ()
520 if (m_meter_thread == 0) {
521 g_atomic_int_set (&m_meter_exit, 0);
522 m_meter_thread = Glib::Thread::create (sigc::mem_fun(this, &AudioEngine::meter_thread),
523 500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
528 AudioEngine::meter_thread ()
531 Glib::usleep (10000); /* 1/100th sec interval */
532 if (g_atomic_int_get(&m_meter_exit)) {
535 Metering::update_meters ();
540 AudioEngine::set_session (Session *s)
542 Glib::Mutex::Lock pl (_process_lock);
548 nframes_t blocksize = jack_get_buffer_size (_jack);
550 /* page in as much of the session process code as we
551 can before we really start running.
554 boost::shared_ptr<Ports> p = ports.reader();
556 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
557 (*i)->cycle_start (blocksize);
560 s->process (blocksize);
561 s->process (blocksize);
562 s->process (blocksize);
563 s->process (blocksize);
564 s->process (blocksize);
565 s->process (blocksize);
566 s->process (blocksize);
567 s->process (blocksize);
569 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
570 (*i)->cycle_end (blocksize);
576 AudioEngine::remove_session ()
578 Glib::Mutex::Lock lm (_process_lock);
583 session_remove_pending = true;
584 session_removed.wait(_process_lock);
595 AudioEngine::port_registration_failure (const std::string& portname)
597 string full_portname = jack_client_name;
598 full_portname += ':';
599 full_portname += portname;
602 jack_port_t* p = jack_port_by_name (_jack, full_portname.c_str());
606 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
608 reason = _("No more JACK ports are available. You will need to stop Ardour and restart JACK with ports if you need this many tracks.");
611 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
615 AudioEngine::register_port (DataType dtype, const string& portname, bool input)
620 if (dtype == DataType::AUDIO) {
621 newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput));
622 } else if (dtype == DataType::MIDI) {
623 newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput));
625 throw PortRegistrationFailure("unable to create port (unknown type)");
628 size_t& old_buffer_size = _raw_buffer_sizes[newport->type()];
629 size_t port_buffer_size = newport->raw_buffer_size(0);
630 if (port_buffer_size > old_buffer_size) {
631 old_buffer_size = port_buffer_size;
634 RCUWriter<Ports> writer (ports);
635 boost::shared_ptr<Ports> ps = writer.get_copy ();
636 ps->insert (ps->begin(), newport);
638 /* writer goes out of scope, forces update */
643 catch (PortRegistrationFailure& err) {
645 } catch (std::exception& e) {
646 throw PortRegistrationFailure(string_compose(
647 _("unable to create port: %1"), e.what()).c_str());
649 throw PortRegistrationFailure("unable to create port (unknown error)");
654 AudioEngine::register_input_port (DataType type, const string& portname)
656 return register_port (type, portname, true);
660 AudioEngine::register_output_port (DataType type, const string& portname)
662 return register_port (type, portname, false);
666 AudioEngine::unregister_port (Port& port)
668 /* caller must hold process lock */
671 /* probably happening when the engine has been halted by JACK,
672 in which case, there is nothing we can do here.
678 RCUWriter<Ports> writer (ports);
679 boost::shared_ptr<Ports> ps = writer.get_copy ();
681 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
689 /* writer goes out of scope, forces update */
696 AudioEngine::connect (const string& source, const string& destination)
698 /* caller must hold process lock */
704 fatal << _("connect called before engine was started") << endmsg;
711 string s = make_port_name_non_relative (source);
712 string d = make_port_name_non_relative (destination);
714 Port* src = get_port_by_name_locked (s);
715 Port* dst = get_port_by_name_locked (d);
718 ret = src->connect (d);
720 ret = dst->connect (s);
722 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
727 /* already exists - no error, no warning */
728 } else if (ret < 0) {
729 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
730 source, s, destination, d)
738 AudioEngine::disconnect (const string& source, const string& destination)
740 /* caller must hold process lock */
746 fatal << _("disconnect called before engine was started") << endmsg;
753 string s = make_port_name_non_relative (source);
754 string d = make_port_name_non_relative (destination);
756 Port* src = get_port_by_name_locked (s);
757 Port* dst = get_port_by_name_locked (d);
760 ret = src->disconnect (d);
762 ret = dst->disconnect (s);
764 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
771 AudioEngine::disconnect (Port& port)
775 fatal << _("disconnect called before engine was started") << endmsg;
782 return port.disconnect_all ();
786 AudioEngine::frame_rate ()
789 if (_frame_rate == 0) {
790 return (_frame_rate = jack_get_sample_rate (_jack));
795 fatal << X_("programming error: AudioEngine::frame_rate() called while disconnected from JACK")
803 AudioEngine::raw_buffer_size (DataType t)
805 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
806 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
810 AudioEngine::frames_per_cycle ()
813 if (_buffer_size == 0) {
814 return (_buffer_size = jack_get_buffer_size (_jack));
819 fatal << X_("programming error: AudioEngine::frame_rate() called while disconnected from JACK")
826 /** @param name Full name of port (including prefix:)
827 * @return Corresponding Port*, or 0. This object remains the property of the AudioEngine
828 * so must not be deleted.
831 AudioEngine::get_port_by_name (const string& portname)
834 if (portname.find_first_of (':') == string::npos) {
835 s = make_port_name_non_relative (portname);
840 Glib::Mutex::Lock lm (_process_lock);
841 return get_port_by_name_locked (s);
845 AudioEngine::get_port_by_name_locked (const string& portname)
847 /* caller must hold process lock */
851 fatal << _("get_port_by_name_locked() called before engine was started") << endmsg;
858 if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
859 /* not an ardour: port */
863 std::string const rel = make_port_name_relative (portname);
865 boost::shared_ptr<Ports> pr = ports.reader();
867 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
868 if (rel == (*i)->name()) {
881 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
885 fatal << _("get_ports called before engine was started") << endmsg;
891 return jack_get_ports (_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
895 AudioEngine::halted (void *arg)
897 AudioEngine* ae = static_cast<AudioEngine *> (arg);
898 bool was_running = ae->_running;
900 ae->stop_metering_thread ();
902 ae->_running = false;
903 ae->_buffer_size = 0;
907 ae->Halted(); /* EMIT SIGNAL */
912 AudioEngine::can_request_hardware_monitoring ()
920 if ((ports = jack_get_ports (_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
931 AudioEngine::n_physical_outputs (DataType type) const
940 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
944 for (i = 0; ports[i]; ++i) {}
951 AudioEngine::n_physical_inputs (DataType type) const
960 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
965 for (i = 0; ports[i]; ++i) {}
972 AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
981 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
986 for (i = 0; ports[i]; ++i) {
987 ins.push_back (ports[i]);
994 AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
1003 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1008 for (i = 0; ports[i]; ++i) {
1009 outs.push_back (ports[i]);
1016 AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
1018 const char ** ports;
1022 assert(type != DataType::NIL);
1024 if (!_running || !_jack) {
1026 fatal << _("get_nth_physical called before engine was started") << endmsg;
1033 ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|flag);
1039 for (i = 0; i < n && ports[i]; ++i) {}
1045 free ((char *) ports);
1051 AudioEngine::update_total_latency (const Port& port)
1054 fatal << _("update_total_latency() called with no JACK client connection") << endmsg;
1060 fatal << _("update_total_latency() called before engine was started") << endmsg;
1065 port.recompute_total_latency ();
1069 AudioEngine::transport_stop ()
1072 jack_transport_stop (_jack);
1077 AudioEngine::transport_start ()
1080 jack_transport_start (_jack);
1085 AudioEngine::transport_locate (nframes_t where)
1087 // cerr << "tell JACK to locate to " << where << endl;
1089 jack_transport_locate (_jack, where);
1093 AudioEngine::TransportState
1094 AudioEngine::transport_state ()
1097 jack_position_t pos;
1098 return (TransportState) jack_transport_query (_jack, &pos);
1100 return (TransportState) JackTransportStopped;
1105 AudioEngine::reset_timebase ()
1107 if (_jack && session) {
1108 if (session->config.get_jack_time_master()) {
1109 return jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
1111 return jack_release_timebase (_jack);
1119 AudioEngine::freewheel (bool onoff)
1123 if (onoff != _freewheeling) {
1126 _freewheel_thread_registered = false;
1129 return jack_set_freewheel (_jack, onoff);
1132 /* already doing what has been asked for */
1142 AudioEngine::remove_all_ports ()
1144 /* process lock MUST be held */
1147 RCUWriter<Ports> writer (ports);
1148 boost::shared_ptr<Ports> ps = writer.get_copy ();
1150 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
1159 ardour_jack_error (const char* msg)
1161 error << "JACK: " << msg << endmsg;
1165 AudioEngine::connect_to_jack (string client_name)
1167 jack_options_t options = JackNullOption;
1168 jack_status_t status;
1169 const char *server_name = NULL;
1171 jack_client_name = client_name; /* might be reset below */
1172 _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
1174 if (_jack == NULL) {
1175 // error message is not useful here
1179 if (status & JackNameNotUnique) {
1180 jack_client_name = jack_get_client_name (_jack);
1183 jack_set_error_function (ardour_jack_error);
1189 AudioEngine::disconnect_from_jack ()
1196 stop_metering_thread ();
1200 Glib::Mutex::Lock lm (_process_lock);
1201 jack_client_close (_jack);
1207 _raw_buffer_sizes.clear();
1211 Stopped(); /* EMIT SIGNAL */
1218 AudioEngine::reconnect_to_jack ()
1221 disconnect_from_jack ();
1222 /* XXX give jackd a chance */
1223 Glib::usleep (250000);
1226 if (connect_to_jack (jack_client_name)) {
1227 error << _("failed to connect to JACK") << endmsg;
1233 boost::shared_ptr<Ports> p = ports.reader ();
1235 for (i = p->begin(); i != p->end(); ++i) {
1236 if ((*i)->reestablish ()) {
1241 if (i != p->end()) {
1243 remove_all_ports ();
1249 session->reset_jack_connection (_jack);
1250 nframes_t blocksize = jack_get_buffer_size (_jack);
1251 session->set_block_size (blocksize);
1252 session->set_frame_rate (jack_get_sample_rate (_jack));
1254 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
1255 cout << "FIXME: Assuming maximum MIDI buffer size " << blocksize * 4 << "bytes" << endl;
1256 _raw_buffer_sizes[DataType::MIDI] = blocksize * 4;
1259 last_monitor_check = 0;
1261 jack_on_shutdown (_jack, halted, this);
1262 jack_set_graph_order_callback (_jack, _graph_order_callback, this);
1263 jack_set_thread_init_callback (_jack, _thread_init_callback, this);
1264 jack_set_process_callback (_jack, _process_callback, this);
1265 jack_set_sample_rate_callback (_jack, _sample_rate_callback, this);
1266 jack_set_buffer_size_callback (_jack, _bufsize_callback, this);
1267 jack_set_xrun_callback (_jack, _xrun_callback, this);
1268 jack_set_sync_callback (_jack, _jack_sync_callback, this);
1269 jack_set_freewheel_callback (_jack, _freewheel_callback, this);
1271 if (session && session->config.get_jack_time_master()) {
1272 jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
1275 if (jack_activate (_jack) == 0) {
1282 /* re-establish connections */
1284 for (i = p->begin(); i != p->end(); ++i) {
1288 Running (); /* EMIT SIGNAL*/
1290 start_metering_thread ();
1296 AudioEngine::request_buffer_size (nframes_t nframes)
1300 if (nframes == jack_get_buffer_size (_jack)) {
1304 return jack_set_buffer_size (_jack, nframes);
1312 AudioEngine::update_total_latencies ()
1314 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1316 jack_recompute_total_latencies (_jack);
1322 AudioEngine::make_port_name_relative (string portname)
1324 string::size_type len;
1325 string::size_type n;
1327 len = portname.length();
1329 for (n = 0; n < len; ++n) {
1330 if (portname[n] == ':') {
1335 if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
1336 return portname.substr (n+1);
1343 AudioEngine::make_port_name_non_relative (string portname)
1347 if (portname.find_first_of (':') != string::npos) {
1351 str = jack_client_name;
1359 AudioEngine::is_realtime () const
1362 return jack_is_realtime (_jack);