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/delivery.h"
37 #include "ardour/port.h"
38 #include "ardour/audio_port.h"
39 #include "ardour/midi_port.h"
40 #include "ardour/meter.h"
41 #include "ardour/session.h"
42 #include "ardour/cycle_timer.h"
43 #include "ardour/utils.h"
44 #include "ardour/event_type_map.h"
45 #include "ardour/io.h"
46 #include "ardour/amp.h"
47 #include "ardour/port_set.h"
48 #include "ardour/buffer_set.h"
50 #include "ardour/timestamps.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;
242 AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
243 jack_position_t* pos, int new_position, void *arg)
245 static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
249 AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
250 jack_position_t* pos, int new_position)
252 if (_jack && session && session->synced_to_jack()) {
253 session->jack_timebase_callback (state, nframes, pos, new_position);
258 AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
260 return static_cast<AudioEngine*> (arg)->jack_sync_callback (state, pos);
264 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
266 if (_jack && session) {
267 return session->jack_sync_callback (state, pos);
274 AudioEngine::_xrun_callback (void *arg)
276 AudioEngine* ae = static_cast<AudioEngine*> (arg);
277 if (ae->connected()) {
278 ae->Xrun (); /* EMIT SIGNAL */
284 AudioEngine::_graph_order_callback (void *arg)
286 AudioEngine* ae = static_cast<AudioEngine*> (arg);
287 if (ae->connected()) {
288 ae->GraphReordered (); /* EMIT SIGNAL */
293 /** Wrapped which is called by JACK as its process callback. It is just
294 * here to get us back into C++ land by calling AudioEngine::process_callback()
295 * @param nframes Number of frames passed by JACK.
296 * @param arg User argument passed by JACK, which will be the AudioEngine*.
299 AudioEngine::_process_callback (nframes_t nframes, void *arg)
301 return static_cast<AudioEngine *> (arg)->process_callback (nframes);
305 AudioEngine::_freewheel_callback (int onoff, void *arg)
307 static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
311 AudioEngine::split_cycle (nframes_t offset)
313 /* caller must hold process lock */
315 Port::increment_port_offset (offset);
317 /* tell all Ports that we're going to start a new (split) cycle */
319 boost::shared_ptr<Ports> p = ports.reader();
321 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
322 (*i)->cycle_split ();
326 /** Method called by JACK (via _process_callback) which says that there
327 * is work to be done.
328 * @param nframes Number of frames to process.
331 AudioEngine::process_callback (nframes_t nframes)
333 // CycleTimer ct ("AudioEngine::process");
335 Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
337 /// The number of frames that will have been processed when we've finished
338 nframes_t next_processed_frames;
340 /* handle wrap around of total frames counter */
342 if (max_frames - _processed_frames < nframes) {
343 next_processed_frames = nframes - (max_frames - _processed_frames);
345 next_processed_frames = _processed_frames + nframes;
348 if (!tm.locked() || session == 0) {
349 /* return having done nothing */
350 _processed_frames = next_processed_frames;
354 if (session_remove_pending) {
355 /* perform the actual session removal */
357 session_remove_pending = false;
358 session_removed.signal();
359 _processed_frames = next_processed_frames;
363 /* tell all relevant objects that we're starting a new cycle */
365 Delivery::CycleStart (nframes);
366 Port::set_port_offset (0);
368 /* tell all Ports that we're starting a new cycle */
370 boost::shared_ptr<Ports> p = ports.reader();
372 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
373 (*i)->cycle_start (nframes);
377 /* emit the Freewheel signal and stop freewheeling in the event of trouble */
378 if (Freewheel (nframes)) {
379 jack_set_freewheel (_jack, false);
384 session->process (nframes);
393 _processed_frames = next_processed_frames;
397 if (last_monitor_check + monitor_check_interval < next_processed_frames) {
399 boost::shared_ptr<Ports> p = ports.reader();
401 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
406 if (port->_last_monitor != (x = port->monitoring_input ())) {
407 port->_last_monitor = x;
408 /* XXX I think this is dangerous, due to
409 a likely mutex in the signal handlers ...
411 port->MonitorInputChanged (x); /* EMIT SIGNAL */
414 last_monitor_check = next_processed_frames;
417 if (session->silent()) {
419 boost::shared_ptr<Ports> p = ports.reader();
421 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
425 if (port->sends_output()) {
426 port->get_buffer(nframes).silence(nframes);
433 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
434 (*i)->cycle_end (nframes);
437 _processed_frames = next_processed_frames;
442 AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
444 return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
448 AudioEngine::jack_sample_rate_callback (nframes_t nframes)
450 _frame_rate = nframes;
451 _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
453 /* check for monitor input change every 1/10th of second */
455 monitor_check_interval = nframes / 10;
456 last_monitor_check = 0;
459 session->set_frame_rate (nframes);
462 SampleRateChanged (nframes); /* EMIT SIGNAL */
468 AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
470 return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
474 AudioEngine::jack_bufsize_callback (nframes_t nframes)
476 _buffer_size = nframes;
477 _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof(float);
478 cout << "FIXME: Assuming maximum MIDI buffer size " << nframes * 4 << "bytes" << endl;
479 _raw_buffer_sizes[DataType::MIDI] = nframes * 4;
480 _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
481 last_monitor_check = 0;
483 boost::shared_ptr<Ports> p = ports.reader();
485 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
490 session->set_block_size (_buffer_size);
497 AudioEngine::stop_metering_thread ()
499 if (m_meter_thread) {
500 g_atomic_int_set (&m_meter_exit, 1);
501 m_meter_thread->join ();
507 AudioEngine::start_metering_thread ()
509 if (m_meter_thread == 0) {
510 g_atomic_int_set (&m_meter_exit, 0);
511 m_meter_thread = Glib::Thread::create (sigc::mem_fun(this, &AudioEngine::meter_thread),
512 500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
517 AudioEngine::meter_thread ()
520 Glib::usleep (10000); /* 1/100th sec interval */
521 if (g_atomic_int_get(&m_meter_exit)) {
524 Metering::update_meters ();
529 AudioEngine::set_session (Session *s)
531 Glib::Mutex::Lock pl (_process_lock);
537 nframes_t blocksize = jack_get_buffer_size (_jack);
539 /* page in as much of the session process code as we
540 can before we really start running.
543 boost::shared_ptr<Ports> p = ports.reader();
545 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
546 (*i)->cycle_start (blocksize);
549 s->process (blocksize);
550 s->process (blocksize);
551 s->process (blocksize);
552 s->process (blocksize);
553 s->process (blocksize);
554 s->process (blocksize);
555 s->process (blocksize);
556 s->process (blocksize);
558 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
559 (*i)->cycle_end (blocksize);
565 AudioEngine::remove_session ()
567 Glib::Mutex::Lock lm (_process_lock);
572 session_remove_pending = true;
573 session_removed.wait(_process_lock);
580 //FIXME: Preliminary bugfix for http://tracker.ardour.org/view.php?id=1985
581 //remove_all_ports ();
585 AudioEngine::port_registration_failure (const std::string& portname)
587 string full_portname = jack_client_name;
588 full_portname += ':';
589 full_portname += portname;
592 jack_port_t* p = jack_port_by_name (_jack, full_portname.c_str());
596 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
598 reason = _("No more JACK ports are available. You will need to stop Ardour and restart JACK with ports if you need this many tracks.");
601 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
605 AudioEngine::register_port (DataType dtype, const string& portname, bool input)
610 if (dtype == DataType::AUDIO) {
611 newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput));
612 } else if (dtype == DataType::MIDI) {
613 newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput));
615 throw PortRegistrationFailure("unable to create port (unknown type)");
618 size_t& old_buffer_size = _raw_buffer_sizes[newport->type()];
619 size_t port_buffer_size = newport->raw_buffer_size(0);
620 if (port_buffer_size > old_buffer_size) {
621 old_buffer_size = port_buffer_size;
624 RCUWriter<Ports> writer (ports);
625 boost::shared_ptr<Ports> ps = writer.get_copy ();
626 ps->insert (ps->begin(), newport);
628 /* writer goes out of scope, forces update */
633 catch (PortRegistrationFailure& err) {
635 } catch (std::exception& e) {
636 throw PortRegistrationFailure(string_compose(
637 _("unable to create port: %1"), e.what()).c_str());
639 throw PortRegistrationFailure("unable to create port (unknown error)");
644 AudioEngine::register_input_port (DataType type, const string& portname)
646 return register_port (type, portname, true);
650 AudioEngine::register_output_port (DataType type, const string& portname)
652 return register_port (type, portname, false);
656 AudioEngine::unregister_port (Port& port)
658 /* caller must hold process lock */
661 /* probably happening when the engine has been halted by JACK,
662 in which case, there is nothing we can do here.
668 RCUWriter<Ports> writer (ports);
669 boost::shared_ptr<Ports> ps = writer.get_copy ();
671 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
679 /* writer goes out of scope, forces update */
682 remove_connections_for (port);
688 AudioEngine::connect (const string& source, const string& destination)
690 /* caller must hold process lock */
696 fatal << _("connect called before engine was started") << endmsg;
703 string s = make_port_name_non_relative (source);
704 string d = make_port_name_non_relative (destination);
706 Port* src = get_port_by_name_locked (s);
707 Port* dst = get_port_by_name_locked (d);
710 ret = src->connect (d);
712 ret = dst->connect (s);
714 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
719 /* already exists - no error, no warning */
720 } else if (ret < 0) {
721 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
722 source, s, destination, d)
730 AudioEngine::disconnect (const string& source, const string& destination)
732 /* caller must hold process lock */
738 fatal << _("disconnect called before engine was started") << endmsg;
745 string s = make_port_name_non_relative (source);
746 string d = make_port_name_non_relative (destination);
748 Port* src = get_port_by_name_locked (s);
749 Port* dst = get_port_by_name_locked (d);
752 ret = src->disconnect (d);
754 ret = dst->disconnect (s);
756 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
763 AudioEngine::disconnect (Port& port)
767 fatal << _("disconnect called before engine was started") << endmsg;
774 return port.disconnect_all ();
778 AudioEngine::frame_rate ()
781 if (_frame_rate == 0) {
782 return (_frame_rate = jack_get_sample_rate (_jack));
787 fatal << X_("programming error: AudioEngine::frame_rate() called while disconnected from JACK")
795 AudioEngine::raw_buffer_size (DataType t)
797 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
798 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
802 AudioEngine::frames_per_cycle ()
805 if (_buffer_size == 0) {
806 return (_buffer_size = jack_get_buffer_size (_jack));
811 fatal << X_("programming error: AudioEngine::frame_rate() called while disconnected from JACK")
818 /** @param name Full name of port (including prefix:)
819 * @return Corresponding Port*, or 0. This object remains the property of the AudioEngine
820 * so must not be deleted.
823 AudioEngine::get_port_by_name (const string& portname)
825 assert (portname.find_first_of (':') != string::npos);
827 Glib::Mutex::Lock lm (_process_lock);
828 return get_port_by_name_locked (portname);
832 AudioEngine::get_port_by_name_locked (const string& portname)
834 /* caller must hold process lock */
838 fatal << _("get_port_by_name_locked() called before engine was started") << endmsg;
845 if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
846 /* not an ardour: port */
850 std::string const rel = make_port_name_relative (portname);
852 boost::shared_ptr<Ports> pr = ports.reader();
854 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
855 if (rel == (*i)->name()) {
868 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
872 fatal << _("get_ports called before engine was started") << endmsg;
878 return jack_get_ports (_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
882 AudioEngine::halted (void *arg)
884 AudioEngine* ae = static_cast<AudioEngine *> (arg);
885 bool was_running = ae->_running;
887 ae->stop_metering_thread ();
889 ae->_running = false;
890 ae->_buffer_size = 0;
894 ae->Halted(); /* EMIT SIGNAL */
899 AudioEngine::can_request_hardware_monitoring ()
907 if ((ports = jack_get_ports (_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
918 AudioEngine::n_physical_outputs (DataType type) const
927 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
931 for (i = 0; ports[i]; ++i) {}
938 AudioEngine::n_physical_inputs (DataType type) const
947 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
952 for (i = 0; ports[i]; ++i) {}
959 AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
968 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
973 for (i = 0; ports[i]; ++i) {
974 ins.push_back (ports[i]);
981 AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
990 if ((ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
995 for (i = 0; ports[i]; ++i) {
996 outs.push_back (ports[i]);
1003 AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
1005 const char ** ports;
1009 assert(type != DataType::NIL);
1011 if (!_running || !_jack) {
1013 fatal << _("get_nth_physical called before engine was started") << endmsg;
1020 ports = jack_get_ports (_jack, NULL, type.to_jack_type(), JackPortIsPhysical|flag);
1026 for (i = 0; i < n && ports[i]; ++i) {}
1032 free ((char *) ports);
1038 AudioEngine::update_total_latency (const Port& port)
1041 fatal << _("update_total_latency() called with no JACK client connection") << endmsg;
1047 fatal << _("update_total_latency() called before engine was started") << endmsg;
1052 port.recompute_total_latency ();
1056 AudioEngine::transport_stop ()
1059 jack_transport_stop (_jack);
1064 AudioEngine::transport_start ()
1067 jack_transport_start (_jack);
1072 AudioEngine::transport_locate (nframes_t where)
1074 // cerr << "tell JACK to locate to " << where << endl;
1076 jack_transport_locate (_jack, where);
1080 AudioEngine::TransportState
1081 AudioEngine::transport_state ()
1084 jack_position_t pos;
1085 return (TransportState) jack_transport_query (_jack, &pos);
1087 return (TransportState) JackTransportStopped;
1092 AudioEngine::reset_timebase ()
1094 if (_jack && session) {
1095 if (session->config.get_jack_time_master()) {
1096 return jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
1098 return jack_release_timebase (_jack);
1106 AudioEngine::freewheel (bool onoff)
1110 if (onoff != _freewheeling) {
1113 _freewheel_thread_registered = false;
1116 return jack_set_freewheel (_jack, onoff);
1119 /* already doing what has been asked for */
1129 AudioEngine::remove_all_ports ()
1131 /* process lock MUST be held */
1134 RCUWriter<Ports> writer (ports);
1135 boost::shared_ptr<Ports> ps = writer.get_copy ();
1141 AudioEngine::remove_connections_for (Port& port)
1143 for (PortConnections::iterator i = port_connections.begin(); i != port_connections.end(); ) {
1144 PortConnections::iterator tmp;
1149 if ((*i).first == port.name()) {
1150 port_connections.erase (i);
1158 #ifdef HAVE_JACK_CLIENT_OPEN
1161 ardour_jack_error (const char* msg)
1163 error << "JACK: " << msg << endmsg;
1167 AudioEngine::connect_to_jack (string client_name)
1169 jack_options_t options = JackNullOption;
1170 jack_status_t status;
1171 const char *server_name = NULL;
1173 jack_client_name = client_name; /* might be reset below */
1174 _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
1176 if (_jack == NULL) {
1177 // error message is not useful here
1181 if (status & JackNameNotUnique) {
1182 jack_client_name = jack_get_client_name (_jack);
1185 jack_set_error_function (ardour_jack_error);
1193 AudioEngine::connect_to_jack (string client_name)
1195 jack_client_name = client_name;
1197 if ((_jack = jack_client_new (client_name.c_str())) == 0) {
1204 #endif /* HAVE_JACK_CLIENT_OPEN */
1207 AudioEngine::disconnect_from_jack ()
1214 stop_metering_thread ();
1218 Glib::Mutex::Lock lm (_process_lock);
1219 jack_client_close (_jack);
1225 _raw_buffer_sizes.clear();
1229 Stopped(); /* EMIT SIGNAL */
1236 AudioEngine::reconnect_to_jack ()
1239 disconnect_from_jack ();
1240 /* XXX give jackd a chance */
1241 Glib::usleep (250000);
1244 if (connect_to_jack (jack_client_name)) {
1245 error << _("failed to connect to JACK") << endmsg;
1251 boost::shared_ptr<Ports> p = ports.reader ();
1253 for (i = p->begin(); i != p->end(); ++i) {
1254 if ((*i)->reestablish ()) {
1259 if (i != p->end()) {
1261 remove_all_ports ();
1267 session->reset_jack_connection (_jack);
1268 nframes_t blocksize = jack_get_buffer_size (_jack);
1269 session->set_block_size (blocksize);
1270 session->set_frame_rate (jack_get_sample_rate (_jack));
1272 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
1273 cout << "FIXME: Assuming maximum MIDI buffer size " << blocksize * 4 << "bytes" << endl;
1274 _raw_buffer_sizes[DataType::MIDI] = blocksize * 4;
1277 last_monitor_check = 0;
1279 jack_on_shutdown (_jack, halted, this);
1280 jack_set_graph_order_callback (_jack, _graph_order_callback, this);
1281 jack_set_thread_init_callback (_jack, _thread_init_callback, this);
1282 jack_set_process_callback (_jack, _process_callback, this);
1283 jack_set_sample_rate_callback (_jack, _sample_rate_callback, this);
1284 jack_set_buffer_size_callback (_jack, _bufsize_callback, this);
1285 jack_set_xrun_callback (_jack, _xrun_callback, this);
1286 jack_set_sync_callback (_jack, _jack_sync_callback, this);
1287 jack_set_freewheel_callback (_jack, _freewheel_callback, this);
1289 if (session && session->config.get_jack_time_master()) {
1290 jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
1293 if (jack_activate (_jack) == 0) {
1300 /* re-establish connections */
1302 for (i = p->begin(); i != p->end(); ++i) {
1306 Running (); /* EMIT SIGNAL*/
1308 start_metering_thread ();
1314 AudioEngine::request_buffer_size (nframes_t nframes)
1318 if (nframes == jack_get_buffer_size (_jack)) {
1322 return jack_set_buffer_size (_jack, nframes);
1330 AudioEngine::update_total_latencies ()
1332 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1334 jack_recompute_total_latencies (_jack);
1340 AudioEngine::make_port_name_relative (string portname)
1342 string::size_type len;
1343 string::size_type n;
1345 len = portname.length();
1347 for (n = 0; n < len; ++n) {
1348 if (portname[n] == ':') {
1353 if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
1354 return portname.substr (n+1);
1361 AudioEngine::make_port_name_non_relative (string portname)
1365 if (portname.find_first_of (':') != string::npos) {
1369 str = jack_client_name;
1377 AudioEngine::is_realtime () const
1380 return jack_is_realtime (_jack);