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;
59 AudioEngine* AudioEngine::_instance = 0;
61 #define GET_PRIVATE_JACK_POINTER(j) jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return; }
62 #define GET_PRIVATE_JACK_POINTER_RET(j,r) jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return r; }
64 AudioEngine::AudioEngine (string client_name)
67 _instance = this; /* singleton */
70 session_remove_pending = false;
73 last_monitor_check = 0;
74 monitor_check_interval = max_frames;
75 _processed_frames = 0;
80 _freewheel_thread_registered = false;
81 _freewheeling = false;
84 g_atomic_int_set (&m_meter_exit, 0);
86 if (connect_to_jack (client_name)) {
87 throw NoBackendAvailable ();
90 Port::set_engine (this);
92 // Initialize parameter metadata (e.g. ranges)
93 Evoral::Parameter p(NullAutomation);
94 p = EventTypeMap::instance().new_parameter(NullAutomation);
95 p = EventTypeMap::instance().new_parameter(GainAutomation);
96 p = EventTypeMap::instance().new_parameter(PanAutomation);
97 p = EventTypeMap::instance().new_parameter(PluginAutomation);
98 p = EventTypeMap::instance().new_parameter(SoloAutomation);
99 p = EventTypeMap::instance().new_parameter(MuteAutomation);
100 p = EventTypeMap::instance().new_parameter(MidiCCAutomation);
101 p = EventTypeMap::instance().new_parameter(MidiPgmChangeAutomation);
102 p = EventTypeMap::instance().new_parameter(MidiPitchBenderAutomation);
103 p = EventTypeMap::instance().new_parameter(MidiChannelPressureAutomation);
104 p = EventTypeMap::instance().new_parameter(FadeInAutomation);
105 p = EventTypeMap::instance().new_parameter(FadeOutAutomation);
106 p = EventTypeMap::instance().new_parameter(EnvelopeAutomation);
107 p = EventTypeMap::instance().new_parameter(MidiCCAutomation);
110 AudioEngine::~AudioEngine ()
113 Glib::Mutex::Lock tm (_process_lock);
114 session_removed.signal ();
117 jack_client_close (_jack);
121 stop_metering_thread ();
126 AudioEngine::jack() const
132 _thread_init_callback (void * /*arg*/)
134 /* make sure that anybody who needs to know about this thread
138 PBD::notify_gui_about_thread_creation (pthread_self(), X_("Audioengine"), 4096);
139 #ifdef WITH_JACK_MIDI
140 MIDI::JACK_MidiPort::set_process_thread (pthread_self());
141 #endif // WITH_JACK_MIDI
145 AudioEngine::start ()
147 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
151 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
154 BootMessage (_("Connect session to engine"));
156 session->set_block_size (blocksize);
157 session->set_frame_rate (jack_get_sample_rate (_priv_jack));
159 /* page in as much of the session process code as we
160 can before we really start running.
163 session->process (blocksize);
164 session->process (blocksize);
165 session->process (blocksize);
166 session->process (blocksize);
167 session->process (blocksize);
168 session->process (blocksize);
169 session->process (blocksize);
170 session->process (blocksize);
173 _processed_frames = 0;
174 last_monitor_check = 0;
176 jack_on_shutdown (_priv_jack, halted, this);
177 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
178 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
179 jack_set_process_callback (_priv_jack, _process_callback, this);
180 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
181 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
182 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
183 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
184 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
185 jack_set_port_registration_callback (_priv_jack, _registration_callback, this);
187 if (session && session->config.get_jack_time_master()) {
188 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
191 if (jack_activate (_priv_jack) == 0) {
194 Running(); /* EMIT SIGNAL */
196 // error << _("cannot activate JACK client") << endmsg;
199 start_metering_thread();
201 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
204 return _running ? 0 : -1;
208 AudioEngine::stop (bool forever)
210 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
214 disconnect_from_jack ();
216 jack_deactivate (_priv_jack);
217 Stopped(); /* EMIT SIGNAL */
221 return _running ? -1 : 0;
226 AudioEngine::get_sync_offset (nframes_t& offset) const
229 #ifdef HAVE_JACK_VIDEO_SUPPORT
231 GET_PRIVATE_JACK_POINTER_RET (_jack, false);
236 (void) jack_transport_query (_priv_jack, &pos);
238 if (pos.valid & JackVideoFrameOffset) {
239 offset = pos.video_offset;
252 AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
253 jack_position_t* pos, int new_position, void *arg)
255 static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
259 AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
260 jack_position_t* pos, int new_position)
262 if (_jack && session && session->synced_to_jack()) {
263 session->jack_timebase_callback (state, nframes, pos, new_position);
268 AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
270 return static_cast<AudioEngine*> (arg)->jack_sync_callback (state, pos);
274 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
276 if (_jack && session) {
277 return session->jack_sync_callback (state, pos);
284 AudioEngine::_xrun_callback (void *arg)
286 AudioEngine* ae = static_cast<AudioEngine*> (arg);
287 if (ae->connected()) {
288 ae->Xrun (); /* EMIT SIGNAL */
294 AudioEngine::_graph_order_callback (void *arg)
296 AudioEngine* ae = static_cast<AudioEngine*> (arg);
297 if (ae->connected()) {
298 ae->GraphReordered (); /* EMIT SIGNAL */
303 /** Wrapped which is called by JACK as its process callback. It is just
304 * here to get us back into C++ land by calling AudioEngine::process_callback()
305 * @param nframes Number of frames passed by JACK.
306 * @param arg User argument passed by JACK, which will be the AudioEngine*.
309 AudioEngine::_process_callback (nframes_t nframes, void *arg)
311 return static_cast<AudioEngine *> (arg)->process_callback (nframes);
315 AudioEngine::_freewheel_callback (int onoff, void *arg)
317 static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
321 AudioEngine::_registration_callback (jack_port_id_t /*id*/, int /*reg*/, void* arg)
323 AudioEngine* ae = static_cast<AudioEngine*> (arg);
324 ae->PortRegisteredOrUnregistered (); /* EMIT SIGNAL */
328 AudioEngine::split_cycle (nframes_t offset)
330 /* caller must hold process lock */
332 Port::increment_port_offset (offset);
334 /* tell all Ports that we're going to start a new (split) cycle */
336 boost::shared_ptr<Ports> p = ports.reader();
338 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
339 (*i)->cycle_split ();
343 /** Method called by JACK (via _process_callback) which says that there
344 * is work to be done.
345 * @param nframes Number of frames to process.
348 AudioEngine::process_callback (nframes_t nframes)
350 GET_PRIVATE_JACK_POINTER_RET(_jack,0)
351 // CycleTimer ct ("AudioEngine::process");
352 Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
354 /// The number of frames that will have been processed when we've finished
355 nframes_t next_processed_frames;
357 /* handle wrap around of total frames counter */
359 if (max_frames - _processed_frames < nframes) {
360 next_processed_frames = nframes - (max_frames - _processed_frames);
362 next_processed_frames = _processed_frames + nframes;
365 if (!tm.locked() || session == 0) {
366 /* return having done nothing */
367 _processed_frames = next_processed_frames;
371 if (session_remove_pending) {
372 /* perform the actual session removal */
374 session_remove_pending = false;
375 session_removed.signal();
376 _processed_frames = next_processed_frames;
380 /* tell all relevant objects that we're starting a new cycle */
382 Delivery::CycleStart (nframes);
383 Port::set_port_offset (0);
384 InternalReturn::CycleStart (nframes);
386 /* tell all Ports that we're starting a new cycle */
388 boost::shared_ptr<Ports> p = ports.reader();
390 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
391 (*i)->cycle_start (nframes);
395 /* emit the Freewheel signal and stop freewheeling in the event of trouble */
396 if (Freewheel (nframes)) {
397 jack_set_freewheel (_priv_jack, false);
402 session->process (nframes);
411 _processed_frames = next_processed_frames;
415 if (last_monitor_check + monitor_check_interval < next_processed_frames) {
417 boost::shared_ptr<Ports> p = ports.reader();
419 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
424 if (port->_last_monitor != (x = port->monitoring_input ())) {
425 port->_last_monitor = x;
426 /* XXX I think this is dangerous, due to
427 a likely mutex in the signal handlers ...
429 port->MonitorInputChanged (x); /* EMIT SIGNAL */
432 last_monitor_check = next_processed_frames;
435 if (session->silent()) {
437 boost::shared_ptr<Ports> p = ports.reader();
439 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
443 if (port->sends_output()) {
444 port->get_buffer(nframes).silence(nframes);
451 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
452 (*i)->cycle_end (nframes);
455 _processed_frames = next_processed_frames;
460 AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
462 return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
466 AudioEngine::jack_sample_rate_callback (nframes_t nframes)
468 _frame_rate = nframes;
469 _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
471 /* check for monitor input change every 1/10th of second */
473 monitor_check_interval = nframes / 10;
474 last_monitor_check = 0;
477 session->set_frame_rate (nframes);
480 SampleRateChanged (nframes); /* EMIT SIGNAL */
486 AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
488 return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
492 AudioEngine::jack_bufsize_callback (nframes_t nframes)
494 _buffer_size = nframes;
495 _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof(float);
496 cout << "FIXME: Assuming maximum MIDI buffer size " << nframes * 4 << "bytes" << endl;
497 _raw_buffer_sizes[DataType::MIDI] = nframes * 4;
498 _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
499 last_monitor_check = 0;
501 boost::shared_ptr<Ports> p = ports.reader();
503 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
508 session->set_block_size (_buffer_size);
515 AudioEngine::stop_metering_thread ()
517 if (m_meter_thread) {
518 g_atomic_int_set (&m_meter_exit, 1);
519 m_meter_thread->join ();
525 AudioEngine::start_metering_thread ()
527 if (m_meter_thread == 0) {
528 g_atomic_int_set (&m_meter_exit, 0);
529 m_meter_thread = Glib::Thread::create (sigc::mem_fun(this, &AudioEngine::meter_thread),
530 500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
535 AudioEngine::meter_thread ()
538 Glib::usleep (10000); /* 1/100th sec interval */
539 if (g_atomic_int_get(&m_meter_exit)) {
542 Metering::update_meters ();
547 AudioEngine::set_session (Session *s)
549 Glib::Mutex::Lock pl (_process_lock);
555 nframes_t blocksize = jack_get_buffer_size (_jack);
557 /* page in as much of the session process code as we
558 can before we really start running.
561 boost::shared_ptr<Ports> p = ports.reader();
563 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
564 (*i)->cycle_start (blocksize);
567 s->process (blocksize);
568 s->process (blocksize);
569 s->process (blocksize);
570 s->process (blocksize);
571 s->process (blocksize);
572 s->process (blocksize);
573 s->process (blocksize);
574 s->process (blocksize);
576 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
577 (*i)->cycle_end (blocksize);
583 AudioEngine::remove_session ()
585 Glib::Mutex::Lock lm (_process_lock);
590 session_remove_pending = true;
591 session_removed.wait(_process_lock);
602 AudioEngine::port_registration_failure (const std::string& portname)
604 GET_PRIVATE_JACK_POINTER (_jack);
605 string full_portname = jack_client_name;
606 full_portname += ':';
607 full_portname += portname;
610 jack_port_t* p = jack_port_by_name (_priv_jack, full_portname.c_str());
614 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
616 reason = _("No more JACK ports are available. You will need to stop Ardour and restart JACK with ports if you need this many tracks.");
619 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
623 AudioEngine::register_port (DataType dtype, const string& portname, bool input)
628 if (dtype == DataType::AUDIO) {
629 newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput));
630 } else if (dtype == DataType::MIDI) {
631 newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput));
633 throw PortRegistrationFailure("unable to create port (unknown type)");
636 size_t& old_buffer_size = _raw_buffer_sizes[newport->type()];
637 size_t port_buffer_size = newport->raw_buffer_size(0);
638 if (port_buffer_size > old_buffer_size) {
639 old_buffer_size = port_buffer_size;
642 RCUWriter<Ports> writer (ports);
643 boost::shared_ptr<Ports> ps = writer.get_copy ();
644 ps->insert (ps->begin(), newport);
646 /* writer goes out of scope, forces update */
651 catch (PortRegistrationFailure& err) {
653 } catch (std::exception& e) {
654 throw PortRegistrationFailure(string_compose(
655 _("unable to create port: %1"), e.what()).c_str());
657 throw PortRegistrationFailure("unable to create port (unknown error)");
662 AudioEngine::register_input_port (DataType type, const string& portname)
664 return register_port (type, portname, true);
668 AudioEngine::register_output_port (DataType type, const string& portname)
670 return register_port (type, portname, false);
674 AudioEngine::unregister_port (Port& port)
676 /* caller must hold process lock */
679 /* probably happening when the engine has been halted by JACK,
680 in which case, there is nothing we can do here.
686 RCUWriter<Ports> writer (ports);
687 boost::shared_ptr<Ports> ps = writer.get_copy ();
689 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
697 /* writer goes out of scope, forces update */
704 AudioEngine::connect (const string& source, const string& destination)
706 /* caller must hold process lock */
712 fatal << _("connect called before engine was started") << endmsg;
719 string s = make_port_name_non_relative (source);
720 string d = make_port_name_non_relative (destination);
723 Port* src = get_port_by_name_locked (s);
724 Port* dst = get_port_by_name_locked (d);
727 ret = src->connect (d);
729 ret = dst->connect (s);
731 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
736 /* already exists - no error, no warning */
737 } else if (ret < 0) {
738 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
739 source, s, destination, d)
747 AudioEngine::disconnect (const string& source, const string& destination)
749 /* caller must hold process lock */
755 fatal << _("disconnect called before engine was started") << endmsg;
762 string s = make_port_name_non_relative (source);
763 string d = make_port_name_non_relative (destination);
765 Port* src = get_port_by_name_locked (s);
766 Port* dst = get_port_by_name_locked (d);
769 ret = src->disconnect (d);
771 ret = dst->disconnect (s);
773 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
780 AudioEngine::disconnect (Port& port)
782 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
786 fatal << _("disconnect called before engine was started") << endmsg;
793 return port.disconnect_all ();
797 AudioEngine::frame_rate ()
799 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
800 if (_frame_rate == 0) {
801 return (_frame_rate = jack_get_sample_rate (_priv_jack));
808 AudioEngine::raw_buffer_size (DataType t)
810 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
811 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
815 AudioEngine::frames_per_cycle ()
817 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
818 if (_buffer_size == 0) {
819 return (_buffer_size = jack_get_buffer_size (_jack));
825 /** @param name Full name of port (including prefix:)
826 * @return Corresponding Port*, or 0. This object remains the property of the AudioEngine
827 * so must not be deleted.
830 AudioEngine::get_port_by_name (const string& portname)
833 if (portname.find_first_of (':') == string::npos) {
834 s = make_port_name_non_relative (portname);
839 Glib::Mutex::Lock lm (_process_lock);
840 return get_port_by_name_locked (s);
844 AudioEngine::get_port_by_name_locked (const string& portname)
846 /* caller must hold process lock */
850 fatal << _("get_port_by_name_locked() called before engine was started") << endmsg;
857 if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
858 /* not an ardour: port */
862 std::string const rel = make_port_name_relative (portname);
864 boost::shared_ptr<Ports> pr = ports.reader();
866 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
867 if (rel == (*i)->name()) {
876 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
878 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
881 fatal << _("get_ports called before engine was started") << endmsg;
887 return jack_get_ports (_priv_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
891 AudioEngine::halted (void *arg)
893 /* called from jack shutdown handler */
895 AudioEngine* ae = static_cast<AudioEngine *> (arg);
896 bool was_running = ae->_running;
898 ae->stop_metering_thread ();
900 ae->_running = false;
901 ae->_buffer_size = 0;
905 ae->Halted(); /* EMIT SIGNAL */
912 /* called from a signal handler for SIGPIPE */
914 stop_metering_thread ();
923 AudioEngine::can_request_hardware_monitoring ()
925 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
928 if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
939 AudioEngine::n_physical_outputs (DataType type) const
941 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
945 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
949 for (i = 0; ports[i]; ++i) {}
956 AudioEngine::n_physical_inputs (DataType type) const
958 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
962 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
966 for (i = 0; ports[i]; ++i) {}
973 AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
975 GET_PRIVATE_JACK_POINTER (_jack);
980 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
985 for (i = 0; ports[i]; ++i) {
986 ins.push_back (ports[i]);
993 AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
995 GET_PRIVATE_JACK_POINTER (_jack);
999 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1003 for (i = 0; ports[i]; ++i) {
1004 outs.push_back (ports[i]);
1010 AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
1012 GET_PRIVATE_JACK_POINTER_RET (_jack,"");
1013 const char ** ports;
1017 assert(type != DataType::NIL);
1019 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|flag)) == 0) {
1023 for (i = 0; i < n && ports[i]; ++i) {}
1029 free ((char *) ports);
1035 AudioEngine::update_total_latency (const Port& port)
1037 port.recompute_total_latency ();
1041 AudioEngine::transport_stop ()
1043 GET_PRIVATE_JACK_POINTER (_jack);
1044 jack_transport_stop (_priv_jack);
1048 AudioEngine::transport_start ()
1050 GET_PRIVATE_JACK_POINTER (_jack);
1051 jack_transport_start (_priv_jack);
1055 AudioEngine::transport_locate (nframes_t where)
1057 GET_PRIVATE_JACK_POINTER (_jack);
1058 // cerr << "tell JACK to locate to " << where << endl;
1059 jack_transport_locate (_priv_jack, where);
1062 AudioEngine::TransportState
1063 AudioEngine::transport_state ()
1065 GET_PRIVATE_JACK_POINTER_RET (_jack, ((TransportState) JackTransportStopped));
1066 jack_position_t pos;
1067 return (TransportState) jack_transport_query (_priv_jack, &pos);
1071 AudioEngine::reset_timebase ()
1073 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1075 if (session->config.get_jack_time_master()) {
1076 return jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1078 return jack_release_timebase (_jack);
1085 AudioEngine::freewheel (bool onoff)
1087 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1089 if (onoff != _freewheeling) {
1092 _freewheel_thread_registered = false;
1095 return jack_set_freewheel (_priv_jack, onoff);
1098 /* already doing what has been asked for */
1104 AudioEngine::remove_all_ports ()
1106 /* process lock MUST be held */
1109 RCUWriter<Ports> writer (ports);
1110 boost::shared_ptr<Ports> ps = writer.get_copy ();
1112 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
1119 /* clear dead wood list too */
1125 ardour_jack_error (const char* msg)
1127 error << "JACK: " << msg << endmsg;
1131 AudioEngine::connect_to_jack (string client_name)
1133 jack_options_t options = JackNullOption;
1134 jack_status_t status;
1135 const char *server_name = NULL;
1137 jack_client_name = client_name; /* might be reset below */
1138 _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
1140 if (_jack == NULL) {
1141 // error message is not useful here
1145 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1147 if (status & JackNameNotUnique) {
1148 jack_client_name = jack_get_client_name (_priv_jack);
1151 jack_set_error_function (ardour_jack_error);
1157 AudioEngine::disconnect_from_jack ()
1159 GET_PRIVATE_JACK_POINTER_RET (_jack, 0);
1162 stop_metering_thread ();
1166 Glib::Mutex::Lock lm (_process_lock);
1167 jack_client_close (_priv_jack);
1173 _raw_buffer_sizes.clear();
1177 Stopped(); /* EMIT SIGNAL */
1184 AudioEngine::reconnect_to_jack ()
1187 disconnect_from_jack ();
1188 /* XXX give jackd a chance */
1189 Glib::usleep (250000);
1192 if (connect_to_jack (jack_client_name)) {
1193 error << _("failed to connect to JACK") << endmsg;
1199 boost::shared_ptr<Ports> p = ports.reader ();
1201 for (i = p->begin(); i != p->end(); ++i) {
1202 if ((*i)->reestablish ()) {
1207 if (i != p->end()) {
1209 remove_all_ports ();
1213 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
1216 session->reset_jack_connection (_priv_jack);
1217 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
1218 session->set_block_size (blocksize);
1219 session->set_frame_rate (jack_get_sample_rate (_priv_jack));
1221 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
1222 cout << "FIXME: Assuming maximum MIDI buffer size " << blocksize * 4 << "bytes" << endl;
1223 _raw_buffer_sizes[DataType::MIDI] = blocksize * 4;
1226 last_monitor_check = 0;
1228 jack_on_shutdown (_priv_jack, halted, this);
1229 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
1230 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
1231 jack_set_process_callback (_priv_jack, _process_callback, this);
1232 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
1233 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
1234 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
1235 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
1236 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
1238 if (session && session->config.get_jack_time_master()) {
1239 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1242 if (jack_activate (_priv_jack) == 0) {
1249 /* re-establish connections */
1251 for (i = p->begin(); i != p->end(); ++i) {
1255 Running (); /* EMIT SIGNAL*/
1257 start_metering_thread ();
1263 AudioEngine::request_buffer_size (nframes_t nframes)
1265 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1267 if (nframes == jack_get_buffer_size (_priv_jack)) {
1271 return jack_set_buffer_size (_priv_jack, nframes);
1275 AudioEngine::update_total_latencies ()
1277 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1278 GET_PRIVATE_JACK_POINTER (_jack);
1279 jack_recompute_total_latencies (_priv_jack);
1284 AudioEngine::make_port_name_relative (string portname)
1286 string::size_type len;
1287 string::size_type n;
1289 len = portname.length();
1291 for (n = 0; n < len; ++n) {
1292 if (portname[n] == ':') {
1297 if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
1298 return portname.substr (n+1);
1305 AudioEngine::make_port_name_non_relative (string portname)
1309 if (portname.find_first_of (':') != string::npos) {
1313 str = jack_client_name;
1321 AudioEngine::is_realtime () const
1323 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
1324 return jack_is_realtime (_priv_jack);