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 _freewheeling = false;
82 g_atomic_int_set (&m_meter_exit, 0);
84 if (connect_to_jack (client_name)) {
85 throw NoBackendAvailable ();
88 Port::set_engine (this);
90 // Initialize parameter metadata (e.g. ranges)
91 Evoral::Parameter p(NullAutomation);
92 p = EventTypeMap::instance().new_parameter(NullAutomation);
93 p = EventTypeMap::instance().new_parameter(GainAutomation);
94 p = EventTypeMap::instance().new_parameter(PanAutomation);
95 p = EventTypeMap::instance().new_parameter(PluginAutomation);
96 p = EventTypeMap::instance().new_parameter(SoloAutomation);
97 p = EventTypeMap::instance().new_parameter(MuteAutomation);
98 p = EventTypeMap::instance().new_parameter(MidiCCAutomation);
99 p = EventTypeMap::instance().new_parameter(MidiPgmChangeAutomation);
100 p = EventTypeMap::instance().new_parameter(MidiPitchBenderAutomation);
101 p = EventTypeMap::instance().new_parameter(MidiChannelPressureAutomation);
102 p = EventTypeMap::instance().new_parameter(FadeInAutomation);
103 p = EventTypeMap::instance().new_parameter(FadeOutAutomation);
104 p = EventTypeMap::instance().new_parameter(EnvelopeAutomation);
105 p = EventTypeMap::instance().new_parameter(MidiCCAutomation);
108 AudioEngine::~AudioEngine ()
111 Glib::Mutex::Lock tm (_process_lock);
112 session_removed.signal ();
115 jack_client_close (_jack);
119 stop_metering_thread ();
124 AudioEngine::jack() const
130 _thread_init_callback (void * /*arg*/)
132 /* make sure that anybody who needs to know about this thread
136 pthread_set_name (X_("audioengine"));
138 PBD::notify_gui_about_thread_creation ("gui", pthread_self(), X_("Audioengine"), 4096);
139 PBD::notify_gui_about_thread_creation ("midiui", pthread_self(), X_("Audioengine"), 128);
141 SessionEvent::create_per_thread_pool (X_("Audioengine"), 512);
143 MIDI::JACK_MidiPort::set_process_thread (pthread_self());
147 ardour_jack_error (const char* msg)
149 error << "JACK: " << msg << endmsg;
153 AudioEngine::start ()
155 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
159 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
162 BootMessage (_("Connect session to engine"));
164 _session->set_block_size (blocksize);
165 _session->set_frame_rate (jack_get_sample_rate (_priv_jack));
167 /* page in as much of the session process code as we
168 can before we really start running.
171 _session->process (blocksize);
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);
181 _processed_frames = 0;
182 last_monitor_check = 0;
184 jack_on_shutdown (_priv_jack, halted, this);
185 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
186 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
187 jack_set_process_callback (_priv_jack, _process_callback, this);
188 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
189 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
190 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
191 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
192 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
193 jack_set_port_registration_callback (_priv_jack, _registration_callback, this);
195 if (_session && _session->config.get_jack_time_master()) {
196 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
199 jack_set_error_function (ardour_jack_error);
201 if (jack_activate (_priv_jack) == 0) {
204 Running(); /* EMIT SIGNAL */
206 // error << _("cannot activate JACK client") << endmsg;
209 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
212 return _running ? 0 : -1;
216 AudioEngine::stop (bool forever)
218 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
222 disconnect_from_jack ();
224 jack_deactivate (_priv_jack);
225 Stopped(); /* EMIT SIGNAL */
226 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
230 return _running ? -1 : 0;
235 AudioEngine::get_sync_offset (nframes_t& offset) const
238 #ifdef HAVE_JACK_VIDEO_SUPPORT
240 GET_PRIVATE_JACK_POINTER_RET (_jack, false);
245 (void) jack_transport_query (_priv_jack, &pos);
247 if (pos.valid & JackVideoFrameOffset) {
248 offset = pos.video_offset;
261 AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
262 jack_position_t* pos, int new_position, void *arg)
264 static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
268 AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
269 jack_position_t* pos, int new_position)
271 if (_jack && _session && _session->synced_to_jack()) {
272 _session->jack_timebase_callback (state, nframes, pos, new_position);
277 AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
279 return static_cast<AudioEngine*> (arg)->jack_sync_callback (state, pos);
283 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
285 if (_jack && _session) {
286 return _session->jack_sync_callback (state, pos);
293 AudioEngine::_xrun_callback (void *arg)
295 AudioEngine* ae = static_cast<AudioEngine*> (arg);
296 if (ae->connected()) {
297 ae->Xrun (); /* EMIT SIGNAL */
303 AudioEngine::_graph_order_callback (void *arg)
305 AudioEngine* ae = static_cast<AudioEngine*> (arg);
306 if (ae->connected()) {
307 ae->GraphReordered (); /* EMIT SIGNAL */
312 /** Wrapped which is called by JACK as its process callback. It is just
313 * here to get us back into C++ land by calling AudioEngine::process_callback()
314 * @param nframes Number of frames passed by JACK.
315 * @param arg User argument passed by JACK, which will be the AudioEngine*.
318 AudioEngine::_process_callback (nframes_t nframes, void *arg)
320 return static_cast<AudioEngine *> (arg)->process_callback (nframes);
324 AudioEngine::_freewheel_callback (int onoff, void *arg)
326 static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
330 AudioEngine::_registration_callback (jack_port_id_t /*id*/, int /*reg*/, void* arg)
332 AudioEngine* ae = static_cast<AudioEngine*> (arg);
333 ae->PortRegisteredOrUnregistered (); /* EMIT SIGNAL */
337 AudioEngine::split_cycle (nframes_t offset)
339 /* caller must hold process lock */
341 Port::increment_port_offset (offset);
343 /* tell all Ports that we're going to start a new (split) cycle */
345 boost::shared_ptr<Ports> p = ports.reader();
347 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
348 (*i)->cycle_split ();
352 /** Method called by JACK (via _process_callback) which says that there
353 * is work to be done.
354 * @param nframes Number of frames to process.
357 AudioEngine::process_callback (nframes_t nframes)
359 GET_PRIVATE_JACK_POINTER_RET(_jack,0)
360 // CycleTimer ct ("AudioEngine::process");
361 Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
363 /// The number of frames that will have been processed when we've finished
364 nframes_t next_processed_frames;
366 /* handle wrap around of total frames counter */
368 if (max_frames - _processed_frames < nframes) {
369 next_processed_frames = nframes - (max_frames - _processed_frames);
371 next_processed_frames = _processed_frames + nframes;
374 if (!tm.locked() || _session == 0) {
375 /* return having done nothing */
376 _processed_frames = next_processed_frames;
380 if (session_remove_pending) {
381 /* perform the actual session removal */
383 session_remove_pending = false;
384 session_removed.signal();
385 _processed_frames = next_processed_frames;
389 /* tell all relevant objects that we're starting a new cycle */
391 Delivery::CycleStart (nframes);
392 Port::set_port_offset (0);
393 InternalReturn::CycleStart (nframes);
395 /* tell all Ports that we're starting a new cycle */
397 boost::shared_ptr<Ports> p = ports.reader();
399 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
400 (*i)->cycle_start (nframes);
404 /* emit the Freewheel signal and stop freewheeling in the event of trouble
405 * the indirection is to pick up the return value of the signal.
407 if (*Freewheel (nframes)) {
408 jack_set_freewheel (_priv_jack, false);
413 _session->process (nframes);
422 _processed_frames = next_processed_frames;
426 if (last_monitor_check + monitor_check_interval < next_processed_frames) {
428 boost::shared_ptr<Ports> p = ports.reader();
430 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
435 if (port->_last_monitor != (x = port->monitoring_input ())) {
436 port->_last_monitor = x;
437 /* XXX I think this is dangerous, due to
438 a likely mutex in the signal handlers ...
440 port->MonitorInputChanged (x); /* EMIT SIGNAL */
443 last_monitor_check = next_processed_frames;
446 if (_session->silent()) {
448 boost::shared_ptr<Ports> p = ports.reader();
450 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
454 if (port->sends_output()) {
455 port->get_buffer(nframes).silence(nframes);
462 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
463 (*i)->cycle_end (nframes);
466 _processed_frames = next_processed_frames;
471 AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
473 return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
477 AudioEngine::jack_sample_rate_callback (nframes_t nframes)
479 _frame_rate = nframes;
480 _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
482 /* check for monitor input change every 1/10th of second */
484 monitor_check_interval = nframes / 10;
485 last_monitor_check = 0;
488 _session->set_frame_rate (nframes);
491 SampleRateChanged (nframes); /* EMIT SIGNAL */
497 AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
499 return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
503 AudioEngine::jack_bufsize_callback (nframes_t nframes)
505 _buffer_size = nframes;
506 _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof(float);
507 cout << "FIXME: Assuming maximum MIDI buffer size " << nframes * 4 << "bytes" << endl;
508 _raw_buffer_sizes[DataType::MIDI] = nframes * 4;
509 _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
510 last_monitor_check = 0;
512 boost::shared_ptr<Ports> p = ports.reader();
514 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
519 _session->set_block_size (_buffer_size);
526 AudioEngine::stop_metering_thread ()
528 if (m_meter_thread) {
529 g_atomic_int_set (&m_meter_exit, 1);
530 m_meter_thread->join ();
536 AudioEngine::start_metering_thread ()
538 if (m_meter_thread == 0) {
539 g_atomic_int_set (&m_meter_exit, 0);
540 m_meter_thread = Glib::Thread::create (boost::bind (&AudioEngine::meter_thread, this),
541 500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
546 AudioEngine::meter_thread ()
548 pthread_set_name (X_("meter"));
551 Glib::usleep (10000); /* 1/100th sec interval */
552 if (g_atomic_int_get(&m_meter_exit)) {
560 AudioEngine::set_session (Session *s)
562 Glib::Mutex::Lock pl (_process_lock);
564 SessionHandlePtr::set_session (s);
568 start_metering_thread ();
570 nframes_t blocksize = jack_get_buffer_size (_jack);
572 /* page in as much of the session process code as we
573 can before we really start running.
576 boost::shared_ptr<Ports> p = ports.reader();
578 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
579 (*i)->cycle_start (blocksize);
582 _session->process (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);
591 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
592 (*i)->cycle_end (blocksize);
598 AudioEngine::remove_session ()
600 Glib::Mutex::Lock lm (_process_lock);
604 stop_metering_thread ();
607 session_remove_pending = true;
608 session_removed.wait(_process_lock);
612 SessionHandlePtr::set_session (0);
619 AudioEngine::port_registration_failure (const std::string& portname)
621 GET_PRIVATE_JACK_POINTER (_jack);
622 string full_portname = jack_client_name;
623 full_portname += ':';
624 full_portname += portname;
627 jack_port_t* p = jack_port_by_name (_priv_jack, full_portname.c_str());
631 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
633 reason = string_compose (_("No more JACK ports are available. You will need to stop %1 and restart JACK with ports if you need this many tracks."), PROGRAM_NAME);
636 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
640 AudioEngine::register_port (DataType dtype, const string& portname, bool input)
645 if (dtype == DataType::AUDIO) {
646 newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput));
647 } else if (dtype == DataType::MIDI) {
648 newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput));
650 throw PortRegistrationFailure("unable to create port (unknown type)");
653 size_t& old_buffer_size = _raw_buffer_sizes[newport->type()];
654 size_t port_buffer_size = newport->raw_buffer_size(0);
655 if (port_buffer_size > old_buffer_size) {
656 old_buffer_size = port_buffer_size;
659 RCUWriter<Ports> writer (ports);
660 boost::shared_ptr<Ports> ps = writer.get_copy ();
661 ps->insert (ps->begin(), newport);
663 /* writer goes out of scope, forces update */
668 catch (PortRegistrationFailure& err) {
670 } catch (std::exception& e) {
671 throw PortRegistrationFailure(string_compose(
672 _("unable to create port: %1"), e.what()).c_str());
674 throw PortRegistrationFailure("unable to create port (unknown error)");
679 AudioEngine::register_input_port (DataType type, const string& portname)
681 return register_port (type, portname, true);
685 AudioEngine::register_output_port (DataType type, const string& portname)
687 return register_port (type, portname, false);
691 AudioEngine::unregister_port (Port& port)
693 /* caller must hold process lock */
696 /* probably happening when the engine has been halted by JACK,
697 in which case, there is nothing we can do here.
703 RCUWriter<Ports> writer (ports);
704 boost::shared_ptr<Ports> ps = writer.get_copy ();
706 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
714 /* writer goes out of scope, forces update */
721 AudioEngine::connect (const string& source, const string& destination)
723 /* caller must hold process lock */
729 fatal << _("connect called before engine was started") << endmsg;
736 string s = make_port_name_non_relative (source);
737 string d = make_port_name_non_relative (destination);
740 Port* src = get_port_by_name_locked (s);
741 Port* dst = get_port_by_name_locked (d);
744 ret = src->connect (d);
746 ret = dst->connect (s);
748 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
753 /* already exists - no error, no warning */
754 } else if (ret < 0) {
755 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
756 source, s, destination, d)
764 AudioEngine::disconnect (const string& source, const string& destination)
766 /* caller must hold process lock */
772 fatal << _("disconnect called before engine was started") << endmsg;
779 string s = make_port_name_non_relative (source);
780 string d = make_port_name_non_relative (destination);
782 Port* src = get_port_by_name_locked (s);
783 Port* dst = get_port_by_name_locked (d);
786 ret = src->disconnect (d);
788 ret = dst->disconnect (s);
790 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
797 AudioEngine::disconnect (Port& port)
799 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
803 fatal << _("disconnect called before engine was started") << endmsg;
810 return port.disconnect_all ();
814 AudioEngine::frame_rate () const
816 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
817 if (_frame_rate == 0) {
818 return (_frame_rate = jack_get_sample_rate (_priv_jack));
825 AudioEngine::raw_buffer_size (DataType t)
827 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
828 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
832 AudioEngine::frames_per_cycle () const
834 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
835 if (_buffer_size == 0) {
836 return (_buffer_size = jack_get_buffer_size (_jack));
842 /** @param name Full name of port (including prefix:)
843 * @return Corresponding Port*, or 0. This object remains the property of the AudioEngine
844 * so must not be deleted.
847 AudioEngine::get_port_by_name (const string& portname)
850 if (portname.find_first_of (':') == string::npos) {
851 s = make_port_name_non_relative (portname);
856 Glib::Mutex::Lock lm (_process_lock);
857 return get_port_by_name_locked (s);
861 AudioEngine::get_port_by_name_locked (const string& portname)
863 /* caller must hold process lock */
867 fatal << _("get_port_by_name_locked() called before engine was started") << endmsg;
874 if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
875 /* not an ardour: port */
879 std::string const rel = make_port_name_relative (portname);
881 boost::shared_ptr<Ports> pr = ports.reader();
883 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
884 if (rel == (*i)->name()) {
893 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
895 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
898 fatal << _("get_ports called before engine was started") << endmsg;
904 return jack_get_ports (_priv_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
908 AudioEngine::halted (void *arg)
910 /* called from jack shutdown handler */
912 AudioEngine* ae = static_cast<AudioEngine *> (arg);
913 bool was_running = ae->_running;
915 ae->stop_metering_thread ();
917 ae->_running = false;
918 ae->_buffer_size = 0;
922 ae->Halted(); /* EMIT SIGNAL */
923 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
930 /* called from a signal handler for SIGPIPE */
932 stop_metering_thread ();
941 AudioEngine::can_request_hardware_monitoring ()
943 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
946 if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
957 AudioEngine::n_physical_outputs (DataType type) const
959 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
963 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
967 for (i = 0; ports[i]; ++i) {}
974 AudioEngine::n_physical_inputs (DataType type) const
976 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
980 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
984 for (i = 0; ports[i]; ++i) {}
991 AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
993 GET_PRIVATE_JACK_POINTER (_jack);
996 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
1001 for (uint32_t i = 0; ports[i]; ++i) {
1002 ins.push_back (ports[i]);
1009 AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
1011 GET_PRIVATE_JACK_POINTER (_jack);
1012 const char ** ports;
1015 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1019 for (i = 0; ports[i]; ++i) {
1020 outs.push_back (ports[i]);
1026 AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
1028 GET_PRIVATE_JACK_POINTER_RET (_jack,"");
1029 const char ** ports;
1033 assert(type != DataType::NIL);
1035 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|flag)) == 0) {
1039 for (i = 0; i < n && ports[i]; ++i) {}
1045 free ((const char **) ports);
1051 AudioEngine::update_total_latency (const Port& port)
1053 port.recompute_total_latency ();
1057 AudioEngine::transport_stop ()
1059 GET_PRIVATE_JACK_POINTER (_jack);
1060 jack_transport_stop (_priv_jack);
1064 AudioEngine::transport_start ()
1066 GET_PRIVATE_JACK_POINTER (_jack);
1067 jack_transport_start (_priv_jack);
1071 AudioEngine::transport_locate (nframes_t where)
1073 GET_PRIVATE_JACK_POINTER (_jack);
1074 // cerr << "tell JACK to locate to " << where << endl;
1075 jack_transport_locate (_priv_jack, where);
1078 AudioEngine::TransportState
1079 AudioEngine::transport_state ()
1081 GET_PRIVATE_JACK_POINTER_RET (_jack, ((TransportState) JackTransportStopped));
1082 jack_position_t pos;
1083 return (TransportState) jack_transport_query (_priv_jack, &pos);
1087 AudioEngine::reset_timebase ()
1089 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1091 if (_session->config.get_jack_time_master()) {
1092 return jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1094 return jack_release_timebase (_jack);
1101 AudioEngine::freewheel (bool onoff)
1103 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1105 if (onoff != _freewheeling) {
1106 return jack_set_freewheel (_priv_jack, onoff);
1109 /* already doing what has been asked for */
1115 AudioEngine::remove_all_ports ()
1117 /* process lock MUST be held */
1120 RCUWriter<Ports> writer (ports);
1121 boost::shared_ptr<Ports> ps = writer.get_copy ();
1123 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
1130 /* clear dead wood list too */
1136 AudioEngine::connect_to_jack (string client_name)
1138 jack_options_t options = JackNullOption;
1139 jack_status_t status;
1140 const char *server_name = NULL;
1142 jack_client_name = client_name; /* might be reset below */
1143 _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
1145 if (_jack == NULL) {
1146 // error message is not useful here
1150 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1152 if (status & JackNameNotUnique) {
1153 jack_client_name = jack_get_client_name (_priv_jack);
1160 AudioEngine::disconnect_from_jack ()
1162 GET_PRIVATE_JACK_POINTER_RET (_jack, 0);
1165 stop_metering_thread ();
1169 Glib::Mutex::Lock lm (_process_lock);
1170 jack_client_close (_priv_jack);
1176 _raw_buffer_sizes.clear();
1180 Stopped(); /* EMIT SIGNAL */
1181 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
1188 AudioEngine::reconnect_to_jack ()
1191 disconnect_from_jack ();
1192 /* XXX give jackd a chance */
1193 Glib::usleep (250000);
1196 if (connect_to_jack (jack_client_name)) {
1197 error << _("failed to connect to JACK") << endmsg;
1203 boost::shared_ptr<Ports> p = ports.reader ();
1205 for (i = p->begin(); i != p->end(); ++i) {
1206 if ((*i)->reestablish ()) {
1211 if (i != p->end()) {
1213 remove_all_ports ();
1217 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
1220 _session->reset_jack_connection (_priv_jack);
1221 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
1222 _session->set_block_size (blocksize);
1223 _session->set_frame_rate (jack_get_sample_rate (_priv_jack));
1225 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
1226 cout << "FIXME: Assuming maximum MIDI buffer size " << blocksize * 4 << "bytes" << endl;
1227 _raw_buffer_sizes[DataType::MIDI] = blocksize * 4;
1230 last_monitor_check = 0;
1232 jack_on_shutdown (_priv_jack, halted, this);
1233 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
1234 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
1235 jack_set_process_callback (_priv_jack, _process_callback, this);
1236 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
1237 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
1238 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
1239 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
1240 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
1242 if (_session && _session->config.get_jack_time_master()) {
1243 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1246 if (jack_activate (_priv_jack) == 0) {
1253 /* re-establish connections */
1255 for (i = p->begin(); i != p->end(); ++i) {
1259 Running (); /* EMIT SIGNAL*/
1261 start_metering_thread ();
1267 AudioEngine::request_buffer_size (nframes_t nframes)
1269 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1271 if (nframes == jack_get_buffer_size (_priv_jack)) {
1275 return jack_set_buffer_size (_priv_jack, nframes);
1279 AudioEngine::update_total_latencies ()
1281 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1282 GET_PRIVATE_JACK_POINTER (_jack);
1283 jack_recompute_total_latencies (_priv_jack);
1288 AudioEngine::make_port_name_relative (string portname)
1290 string::size_type len;
1291 string::size_type n;
1293 len = portname.length();
1295 for (n = 0; n < len; ++n) {
1296 if (portname[n] == ':') {
1301 if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
1302 return portname.substr (n+1);
1309 AudioEngine::make_port_name_non_relative (string portname)
1313 if (portname.find_first_of (':') != string::npos) {
1317 str = jack_client_name;
1325 AudioEngine::is_realtime () const
1327 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
1328 return jack_is_realtime (_priv_jack);