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 */
69 session_remove_pending = false;
72 last_monitor_check = 0;
73 monitor_check_interval = max_frames;
74 _processed_frames = 0;
79 _freewheel_thread_registered = false;
80 _freewheeling = false;
83 g_atomic_int_set (&m_meter_exit, 0);
85 if (connect_to_jack (client_name)) {
86 throw NoBackendAvailable ();
89 Port::set_engine (this);
91 // Initialize parameter metadata (e.g. ranges)
92 Evoral::Parameter p(NullAutomation);
93 p = EventTypeMap::instance().new_parameter(NullAutomation);
94 p = EventTypeMap::instance().new_parameter(GainAutomation);
95 p = EventTypeMap::instance().new_parameter(PanAutomation);
96 p = EventTypeMap::instance().new_parameter(PluginAutomation);
97 p = EventTypeMap::instance().new_parameter(SoloAutomation);
98 p = EventTypeMap::instance().new_parameter(MuteAutomation);
99 p = EventTypeMap::instance().new_parameter(MidiCCAutomation);
100 p = EventTypeMap::instance().new_parameter(MidiPgmChangeAutomation);
101 p = EventTypeMap::instance().new_parameter(MidiPitchBenderAutomation);
102 p = EventTypeMap::instance().new_parameter(MidiChannelPressureAutomation);
103 p = EventTypeMap::instance().new_parameter(FadeInAutomation);
104 p = EventTypeMap::instance().new_parameter(FadeOutAutomation);
105 p = EventTypeMap::instance().new_parameter(EnvelopeAutomation);
106 p = EventTypeMap::instance().new_parameter(MidiCCAutomation);
109 AudioEngine::~AudioEngine ()
112 Glib::Mutex::Lock tm (_process_lock);
113 session_removed.signal ();
116 jack_client_close (_jack);
120 stop_metering_thread ();
125 AudioEngine::jack() const
131 _thread_init_callback (void * /*arg*/)
133 /* make sure that anybody who needs to know about this thread
137 pthread_set_name (X_("audioengine"));
139 PBD::notify_gui_about_thread_creation ("gui", pthread_self(), X_("Audioengine"), 4096);
140 PBD::notify_gui_about_thread_creation ("midiui", pthread_self(), X_("Audioengine"), 128);
142 SessionEvent::create_per_thread_pool (X_("Audioengine"), 512);
144 MIDI::JACK_MidiPort::set_process_thread (pthread_self());
148 ardour_jack_error (const char* msg)
150 error << "JACK: " << msg << endmsg;
154 AudioEngine::start ()
156 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
160 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
163 BootMessage (_("Connect session to engine"));
165 _session->set_block_size (blocksize);
166 _session->set_frame_rate (jack_get_sample_rate (_priv_jack));
168 /* page in as much of the session process code as we
169 can before we really start running.
172 _session->process (blocksize);
173 _session->process (blocksize);
174 _session->process (blocksize);
175 _session->process (blocksize);
176 _session->process (blocksize);
177 _session->process (blocksize);
178 _session->process (blocksize);
179 _session->process (blocksize);
182 _processed_frames = 0;
183 last_monitor_check = 0;
185 jack_on_shutdown (_priv_jack, halted, this);
186 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
187 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
188 jack_set_process_callback (_priv_jack, _process_callback, this);
189 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
190 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
191 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
192 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
193 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
194 jack_set_port_registration_callback (_priv_jack, _registration_callback, this);
196 if (_session && _session->config.get_jack_time_master()) {
197 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
200 jack_set_error_function (ardour_jack_error);
202 if (jack_activate (_priv_jack) == 0) {
205 Running(); /* EMIT SIGNAL */
207 // error << _("cannot activate JACK client") << endmsg;
210 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
213 return _running ? 0 : -1;
217 AudioEngine::stop (bool forever)
219 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
223 disconnect_from_jack ();
225 jack_deactivate (_priv_jack);
226 Stopped(); /* EMIT SIGNAL */
227 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
231 return _running ? -1 : 0;
236 AudioEngine::get_sync_offset (nframes_t& offset) const
239 #ifdef HAVE_JACK_VIDEO_SUPPORT
241 GET_PRIVATE_JACK_POINTER_RET (_jack, false);
246 (void) jack_transport_query (_priv_jack, &pos);
248 if (pos.valid & JackVideoFrameOffset) {
249 offset = pos.video_offset;
262 AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
263 jack_position_t* pos, int new_position, void *arg)
265 static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
269 AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
270 jack_position_t* pos, int new_position)
272 if (_jack && _session && _session->synced_to_jack()) {
273 _session->jack_timebase_callback (state, nframes, pos, new_position);
278 AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
280 return static_cast<AudioEngine*> (arg)->jack_sync_callback (state, pos);
284 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
286 if (_jack && _session) {
287 return _session->jack_sync_callback (state, pos);
294 AudioEngine::_xrun_callback (void *arg)
296 AudioEngine* ae = static_cast<AudioEngine*> (arg);
297 if (ae->connected()) {
298 ae->Xrun (); /* EMIT SIGNAL */
304 AudioEngine::_graph_order_callback (void *arg)
306 AudioEngine* ae = static_cast<AudioEngine*> (arg);
307 if (ae->connected()) {
308 ae->GraphReordered (); /* EMIT SIGNAL */
313 /** Wrapped which is called by JACK as its process callback. It is just
314 * here to get us back into C++ land by calling AudioEngine::process_callback()
315 * @param nframes Number of frames passed by JACK.
316 * @param arg User argument passed by JACK, which will be the AudioEngine*.
319 AudioEngine::_process_callback (nframes_t nframes, void *arg)
321 return static_cast<AudioEngine *> (arg)->process_callback (nframes);
325 AudioEngine::_freewheel_callback (int onoff, void *arg)
327 static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
331 AudioEngine::_registration_callback (jack_port_id_t /*id*/, int /*reg*/, void* arg)
333 AudioEngine* ae = static_cast<AudioEngine*> (arg);
334 ae->PortRegisteredOrUnregistered (); /* EMIT SIGNAL */
338 AudioEngine::split_cycle (nframes_t offset)
340 /* caller must hold process lock */
342 Port::increment_port_offset (offset);
344 /* tell all Ports that we're going to start a new (split) cycle */
346 boost::shared_ptr<Ports> p = ports.reader();
348 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
349 (*i)->cycle_split ();
353 /** Method called by JACK (via _process_callback) which says that there
354 * is work to be done.
355 * @param nframes Number of frames to process.
358 AudioEngine::process_callback (nframes_t nframes)
360 GET_PRIVATE_JACK_POINTER_RET(_jack,0)
361 // CycleTimer ct ("AudioEngine::process");
362 Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
364 /// The number of frames that will have been processed when we've finished
365 nframes_t next_processed_frames;
367 /* handle wrap around of total frames counter */
369 if (max_frames - _processed_frames < nframes) {
370 next_processed_frames = nframes - (max_frames - _processed_frames);
372 next_processed_frames = _processed_frames + nframes;
375 if (!tm.locked() || _session == 0) {
376 /* return having done nothing */
377 _processed_frames = next_processed_frames;
381 if (session_remove_pending) {
382 /* perform the actual session removal */
384 session_remove_pending = false;
385 session_removed.signal();
386 _processed_frames = next_processed_frames;
390 /* tell all relevant objects that we're starting a new cycle */
392 Delivery::CycleStart (nframes);
393 Port::set_port_offset (0);
394 InternalReturn::CycleStart (nframes);
396 /* tell all Ports that we're starting a new cycle */
398 boost::shared_ptr<Ports> p = ports.reader();
400 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
401 (*i)->cycle_start (nframes);
405 /* emit the Freewheel signal and stop freewheeling in the event of trouble
406 * the indirection is to pick up the return value of the signal.
408 if (*Freewheel (nframes)) {
409 jack_set_freewheel (_priv_jack, false);
414 _session->process (nframes);
423 _processed_frames = next_processed_frames;
427 if (last_monitor_check + monitor_check_interval < next_processed_frames) {
429 boost::shared_ptr<Ports> p = ports.reader();
431 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
436 if (port->_last_monitor != (x = port->monitoring_input ())) {
437 port->_last_monitor = x;
438 /* XXX I think this is dangerous, due to
439 a likely mutex in the signal handlers ...
441 port->MonitorInputChanged (x); /* EMIT SIGNAL */
444 last_monitor_check = next_processed_frames;
447 if (_session->silent()) {
449 boost::shared_ptr<Ports> p = ports.reader();
451 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
455 if (port->sends_output()) {
456 port->get_buffer(nframes).silence(nframes);
463 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
464 (*i)->cycle_end (nframes);
467 _processed_frames = next_processed_frames;
472 AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
474 return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
478 AudioEngine::jack_sample_rate_callback (nframes_t nframes)
480 _frame_rate = nframes;
481 _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
483 /* check for monitor input change every 1/10th of second */
485 monitor_check_interval = nframes / 10;
486 last_monitor_check = 0;
489 _session->set_frame_rate (nframes);
492 SampleRateChanged (nframes); /* EMIT SIGNAL */
498 AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
500 return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
504 AudioEngine::jack_bufsize_callback (nframes_t nframes)
506 _buffer_size = nframes;
507 _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof(float);
508 cout << "FIXME: Assuming maximum MIDI buffer size " << nframes * 4 << "bytes" << endl;
509 _raw_buffer_sizes[DataType::MIDI] = nframes * 4;
510 _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
511 last_monitor_check = 0;
513 boost::shared_ptr<Ports> p = ports.reader();
515 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
520 _session->set_block_size (_buffer_size);
527 AudioEngine::stop_metering_thread ()
529 if (m_meter_thread) {
530 g_atomic_int_set (&m_meter_exit, 1);
531 m_meter_thread->join ();
537 AudioEngine::start_metering_thread ()
539 if (m_meter_thread == 0) {
540 g_atomic_int_set (&m_meter_exit, 0);
541 m_meter_thread = Glib::Thread::create (boost::bind (&AudioEngine::meter_thread, this),
542 500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
547 AudioEngine::meter_thread ()
549 pthread_set_name (X_("meter"));
552 Glib::usleep (10000); /* 1/100th sec interval */
553 if (g_atomic_int_get(&m_meter_exit)) {
561 AudioEngine::set_session (Session *s)
563 Glib::Mutex::Lock pl (_process_lock);
565 SessionHandlePtr::set_session (s);
569 start_metering_thread ();
571 nframes_t blocksize = jack_get_buffer_size (_jack);
573 /* page in as much of the session process code as we
574 can before we really start running.
577 boost::shared_ptr<Ports> p = ports.reader();
579 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
580 (*i)->cycle_start (blocksize);
583 _session->process (blocksize);
584 _session->process (blocksize);
585 _session->process (blocksize);
586 _session->process (blocksize);
587 _session->process (blocksize);
588 _session->process (blocksize);
589 _session->process (blocksize);
590 _session->process (blocksize);
592 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
593 (*i)->cycle_end (blocksize);
599 AudioEngine::remove_session ()
601 Glib::Mutex::Lock lm (_process_lock);
605 stop_metering_thread ();
608 session_remove_pending = true;
609 session_removed.wait(_process_lock);
613 SessionHandlePtr::set_session (0);
620 AudioEngine::port_registration_failure (const std::string& portname)
622 GET_PRIVATE_JACK_POINTER (_jack);
623 string full_portname = jack_client_name;
624 full_portname += ':';
625 full_portname += portname;
628 jack_port_t* p = jack_port_by_name (_priv_jack, full_portname.c_str());
632 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
634 reason = _("No more JACK ports are available. You will need to stop Ardour and restart JACK with ports if you need this many tracks.");
637 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
641 AudioEngine::register_port (DataType dtype, const string& portname, bool input)
646 if (dtype == DataType::AUDIO) {
647 newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput));
648 } else if (dtype == DataType::MIDI) {
649 newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput));
651 throw PortRegistrationFailure("unable to create port (unknown type)");
654 size_t& old_buffer_size = _raw_buffer_sizes[newport->type()];
655 size_t port_buffer_size = newport->raw_buffer_size(0);
656 if (port_buffer_size > old_buffer_size) {
657 old_buffer_size = port_buffer_size;
660 RCUWriter<Ports> writer (ports);
661 boost::shared_ptr<Ports> ps = writer.get_copy ();
662 ps->insert (ps->begin(), newport);
664 /* writer goes out of scope, forces update */
669 catch (PortRegistrationFailure& err) {
671 } catch (std::exception& e) {
672 throw PortRegistrationFailure(string_compose(
673 _("unable to create port: %1"), e.what()).c_str());
675 throw PortRegistrationFailure("unable to create port (unknown error)");
680 AudioEngine::register_input_port (DataType type, const string& portname)
682 return register_port (type, portname, true);
686 AudioEngine::register_output_port (DataType type, const string& portname)
688 return register_port (type, portname, false);
692 AudioEngine::unregister_port (Port& port)
694 /* caller must hold process lock */
697 /* probably happening when the engine has been halted by JACK,
698 in which case, there is nothing we can do here.
704 RCUWriter<Ports> writer (ports);
705 boost::shared_ptr<Ports> ps = writer.get_copy ();
707 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
715 /* writer goes out of scope, forces update */
722 AudioEngine::connect (const string& source, const string& destination)
724 /* caller must hold process lock */
730 fatal << _("connect called before engine was started") << endmsg;
737 string s = make_port_name_non_relative (source);
738 string d = make_port_name_non_relative (destination);
741 Port* src = get_port_by_name_locked (s);
742 Port* dst = get_port_by_name_locked (d);
745 ret = src->connect (d);
747 ret = dst->connect (s);
749 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
754 /* already exists - no error, no warning */
755 } else if (ret < 0) {
756 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
757 source, s, destination, d)
765 AudioEngine::disconnect (const string& source, const string& destination)
767 /* caller must hold process lock */
773 fatal << _("disconnect called before engine was started") << endmsg;
780 string s = make_port_name_non_relative (source);
781 string d = make_port_name_non_relative (destination);
783 Port* src = get_port_by_name_locked (s);
784 Port* dst = get_port_by_name_locked (d);
787 ret = src->disconnect (d);
789 ret = dst->disconnect (s);
791 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
798 AudioEngine::disconnect (Port& port)
800 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
804 fatal << _("disconnect called before engine was started") << endmsg;
811 return port.disconnect_all ();
815 AudioEngine::frame_rate () const
817 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
818 if (_frame_rate == 0) {
819 return (_frame_rate = jack_get_sample_rate (_priv_jack));
826 AudioEngine::raw_buffer_size (DataType t)
828 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
829 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
833 AudioEngine::frames_per_cycle () const
835 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
836 if (_buffer_size == 0) {
837 return (_buffer_size = jack_get_buffer_size (_jack));
843 /** @param name Full name of port (including prefix:)
844 * @return Corresponding Port*, or 0. This object remains the property of the AudioEngine
845 * so must not be deleted.
848 AudioEngine::get_port_by_name (const string& portname)
851 if (portname.find_first_of (':') == string::npos) {
852 s = make_port_name_non_relative (portname);
857 Glib::Mutex::Lock lm (_process_lock);
858 return get_port_by_name_locked (s);
862 AudioEngine::get_port_by_name_locked (const string& portname)
864 /* caller must hold process lock */
868 fatal << _("get_port_by_name_locked() called before engine was started") << endmsg;
875 if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
876 /* not an ardour: port */
880 std::string const rel = make_port_name_relative (portname);
882 boost::shared_ptr<Ports> pr = ports.reader();
884 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
885 if (rel == (*i)->name()) {
894 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
896 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
899 fatal << _("get_ports called before engine was started") << endmsg;
905 return jack_get_ports (_priv_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
909 AudioEngine::halted (void *arg)
911 /* called from jack shutdown handler */
913 AudioEngine* ae = static_cast<AudioEngine *> (arg);
914 bool was_running = ae->_running;
916 ae->stop_metering_thread ();
918 ae->_running = false;
919 ae->_buffer_size = 0;
923 ae->Halted(); /* EMIT SIGNAL */
924 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
931 /* called from a signal handler for SIGPIPE */
933 stop_metering_thread ();
942 AudioEngine::can_request_hardware_monitoring ()
944 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
947 if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
958 AudioEngine::n_physical_outputs (DataType type) const
960 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
964 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
968 for (i = 0; ports[i]; ++i) {}
975 AudioEngine::n_physical_inputs (DataType type) const
977 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
981 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
985 for (i = 0; ports[i]; ++i) {}
992 AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
994 GET_PRIVATE_JACK_POINTER (_jack);
999 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
1004 for (i = 0; ports[i]; ++i) {
1005 ins.push_back (ports[i]);
1012 AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
1014 GET_PRIVATE_JACK_POINTER (_jack);
1015 const char ** ports;
1018 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1022 for (i = 0; ports[i]; ++i) {
1023 outs.push_back (ports[i]);
1029 AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
1031 GET_PRIVATE_JACK_POINTER_RET (_jack,"");
1032 const char ** ports;
1036 assert(type != DataType::NIL);
1038 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|flag)) == 0) {
1042 for (i = 0; i < n && ports[i]; ++i) {}
1048 free ((char *) ports);
1054 AudioEngine::update_total_latency (const Port& port)
1056 port.recompute_total_latency ();
1060 AudioEngine::transport_stop ()
1062 GET_PRIVATE_JACK_POINTER (_jack);
1063 jack_transport_stop (_priv_jack);
1067 AudioEngine::transport_start ()
1069 GET_PRIVATE_JACK_POINTER (_jack);
1070 jack_transport_start (_priv_jack);
1074 AudioEngine::transport_locate (nframes_t where)
1076 GET_PRIVATE_JACK_POINTER (_jack);
1077 // cerr << "tell JACK to locate to " << where << endl;
1078 jack_transport_locate (_priv_jack, where);
1081 AudioEngine::TransportState
1082 AudioEngine::transport_state ()
1084 GET_PRIVATE_JACK_POINTER_RET (_jack, ((TransportState) JackTransportStopped));
1085 jack_position_t pos;
1086 return (TransportState) jack_transport_query (_priv_jack, &pos);
1090 AudioEngine::reset_timebase ()
1092 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1094 if (_session->config.get_jack_time_master()) {
1095 return jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1097 return jack_release_timebase (_jack);
1104 AudioEngine::freewheel (bool onoff)
1106 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1108 if (onoff != _freewheeling) {
1111 _freewheel_thread_registered = false;
1114 return jack_set_freewheel (_priv_jack, onoff);
1117 /* already doing what has been asked for */
1123 AudioEngine::remove_all_ports ()
1125 /* process lock MUST be held */
1128 RCUWriter<Ports> writer (ports);
1129 boost::shared_ptr<Ports> ps = writer.get_copy ();
1131 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
1138 /* clear dead wood list too */
1144 AudioEngine::connect_to_jack (string client_name)
1146 jack_options_t options = JackNullOption;
1147 jack_status_t status;
1148 const char *server_name = NULL;
1150 jack_client_name = client_name; /* might be reset below */
1151 _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
1153 if (_jack == NULL) {
1154 // error message is not useful here
1158 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1160 if (status & JackNameNotUnique) {
1161 jack_client_name = jack_get_client_name (_priv_jack);
1168 AudioEngine::disconnect_from_jack ()
1170 GET_PRIVATE_JACK_POINTER_RET (_jack, 0);
1173 stop_metering_thread ();
1177 Glib::Mutex::Lock lm (_process_lock);
1178 jack_client_close (_priv_jack);
1184 _raw_buffer_sizes.clear();
1188 Stopped(); /* EMIT SIGNAL */
1189 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
1196 AudioEngine::reconnect_to_jack ()
1199 disconnect_from_jack ();
1200 /* XXX give jackd a chance */
1201 Glib::usleep (250000);
1204 if (connect_to_jack (jack_client_name)) {
1205 error << _("failed to connect to JACK") << endmsg;
1211 boost::shared_ptr<Ports> p = ports.reader ();
1213 for (i = p->begin(); i != p->end(); ++i) {
1214 if ((*i)->reestablish ()) {
1219 if (i != p->end()) {
1221 remove_all_ports ();
1225 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
1228 _session->reset_jack_connection (_priv_jack);
1229 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
1230 _session->set_block_size (blocksize);
1231 _session->set_frame_rate (jack_get_sample_rate (_priv_jack));
1233 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
1234 cout << "FIXME: Assuming maximum MIDI buffer size " << blocksize * 4 << "bytes" << endl;
1235 _raw_buffer_sizes[DataType::MIDI] = blocksize * 4;
1238 last_monitor_check = 0;
1240 jack_on_shutdown (_priv_jack, halted, this);
1241 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
1242 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
1243 jack_set_process_callback (_priv_jack, _process_callback, this);
1244 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
1245 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
1246 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
1247 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
1248 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
1250 if (_session && _session->config.get_jack_time_master()) {
1251 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1254 if (jack_activate (_priv_jack) == 0) {
1261 /* re-establish connections */
1263 for (i = p->begin(); i != p->end(); ++i) {
1267 Running (); /* EMIT SIGNAL*/
1269 start_metering_thread ();
1275 AudioEngine::request_buffer_size (nframes_t nframes)
1277 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1279 if (nframes == jack_get_buffer_size (_priv_jack)) {
1283 return jack_set_buffer_size (_priv_jack, nframes);
1287 AudioEngine::update_total_latencies ()
1289 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1290 GET_PRIVATE_JACK_POINTER (_jack);
1291 jack_recompute_total_latencies (_priv_jack);
1296 AudioEngine::make_port_name_relative (string portname)
1298 string::size_type len;
1299 string::size_type n;
1301 len = portname.length();
1303 for (n = 0; n < len; ++n) {
1304 if (portname[n] == ':') {
1309 if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
1310 return portname.substr (n+1);
1317 AudioEngine::make_port_name_non_relative (string portname)
1321 if (portname.find_first_of (':') != string::npos) {
1325 str = jack_client_name;
1333 AudioEngine::is_realtime () const
1335 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
1336 return jack_is_realtime (_priv_jack);