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 PBD::notify_gui_about_thread_creation ("gui", pthread_self(), X_("Audioengine"), 4096);
138 PBD::notify_gui_about_thread_creation ("midiui", pthread_self(), X_("Audioengine"), 128);
140 SessionEvent::create_per_thread_pool (X_("Audioengine"), 512);
142 MIDI::JACK_MidiPort::set_process_thread (pthread_self());
146 ardour_jack_error (const char* msg)
148 error << "JACK: " << msg << endmsg;
152 AudioEngine::start ()
154 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
158 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
161 BootMessage (_("Connect session to engine"));
163 _session->set_block_size (blocksize);
164 _session->set_frame_rate (jack_get_sample_rate (_priv_jack));
166 /* page in as much of the session process code as we
167 can before we really start running.
170 _session->process (blocksize);
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);
180 _processed_frames = 0;
181 last_monitor_check = 0;
183 jack_on_shutdown (_priv_jack, halted, this);
184 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
185 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
186 jack_set_process_callback (_priv_jack, _process_callback, this);
187 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
188 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
189 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
190 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
191 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
192 jack_set_port_registration_callback (_priv_jack, _registration_callback, this);
194 if (_session && _session->config.get_jack_time_master()) {
195 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
198 jack_set_error_function (ardour_jack_error);
200 if (jack_activate (_priv_jack) == 0) {
203 Running(); /* EMIT SIGNAL */
205 // error << _("cannot activate JACK client") << endmsg;
208 start_metering_thread();
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 */
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 ()
549 Glib::usleep (10000); /* 1/100th sec interval */
550 if (g_atomic_int_get(&m_meter_exit)) {
553 Metering::update_meters ();
558 AudioEngine::set_session (Session *s)
560 Glib::Mutex::Lock pl (_process_lock);
562 SessionHandlePtr::set_session (s);
566 nframes_t blocksize = jack_get_buffer_size (_jack);
568 /* page in as much of the session process code as we
569 can before we really start running.
572 boost::shared_ptr<Ports> p = ports.reader();
574 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
575 (*i)->cycle_start (blocksize);
578 _session->process (blocksize);
579 _session->process (blocksize);
580 _session->process (blocksize);
581 _session->process (blocksize);
582 _session->process (blocksize);
583 _session->process (blocksize);
584 _session->process (blocksize);
585 _session->process (blocksize);
587 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
588 (*i)->cycle_end (blocksize);
594 AudioEngine::remove_session ()
596 Glib::Mutex::Lock lm (_process_lock);
601 session_remove_pending = true;
602 session_removed.wait(_process_lock);
606 SessionHandlePtr::set_session (0);
613 AudioEngine::port_registration_failure (const std::string& portname)
615 GET_PRIVATE_JACK_POINTER (_jack);
616 string full_portname = jack_client_name;
617 full_portname += ':';
618 full_portname += portname;
621 jack_port_t* p = jack_port_by_name (_priv_jack, full_portname.c_str());
625 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
627 reason = _("No more JACK ports are available. You will need to stop Ardour and restart JACK with ports if you need this many tracks.");
630 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
634 AudioEngine::register_port (DataType dtype, const string& portname, bool input)
639 if (dtype == DataType::AUDIO) {
640 newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput));
641 } else if (dtype == DataType::MIDI) {
642 newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput));
644 throw PortRegistrationFailure("unable to create port (unknown type)");
647 size_t& old_buffer_size = _raw_buffer_sizes[newport->type()];
648 size_t port_buffer_size = newport->raw_buffer_size(0);
649 if (port_buffer_size > old_buffer_size) {
650 old_buffer_size = port_buffer_size;
653 RCUWriter<Ports> writer (ports);
654 boost::shared_ptr<Ports> ps = writer.get_copy ();
655 ps->insert (ps->begin(), newport);
657 /* writer goes out of scope, forces update */
662 catch (PortRegistrationFailure& err) {
664 } catch (std::exception& e) {
665 throw PortRegistrationFailure(string_compose(
666 _("unable to create port: %1"), e.what()).c_str());
668 throw PortRegistrationFailure("unable to create port (unknown error)");
673 AudioEngine::register_input_port (DataType type, const string& portname)
675 return register_port (type, portname, true);
679 AudioEngine::register_output_port (DataType type, const string& portname)
681 return register_port (type, portname, false);
685 AudioEngine::unregister_port (Port& port)
687 /* caller must hold process lock */
690 /* probably happening when the engine has been halted by JACK,
691 in which case, there is nothing we can do here.
697 RCUWriter<Ports> writer (ports);
698 boost::shared_ptr<Ports> ps = writer.get_copy ();
700 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
708 /* writer goes out of scope, forces update */
715 AudioEngine::connect (const string& source, const string& destination)
717 /* caller must hold process lock */
723 fatal << _("connect called before engine was started") << endmsg;
730 string s = make_port_name_non_relative (source);
731 string d = make_port_name_non_relative (destination);
734 Port* src = get_port_by_name_locked (s);
735 Port* dst = get_port_by_name_locked (d);
738 ret = src->connect (d);
740 ret = dst->connect (s);
742 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
747 /* already exists - no error, no warning */
748 } else if (ret < 0) {
749 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
750 source, s, destination, d)
758 AudioEngine::disconnect (const string& source, const string& destination)
760 /* caller must hold process lock */
766 fatal << _("disconnect called before engine was started") << endmsg;
773 string s = make_port_name_non_relative (source);
774 string d = make_port_name_non_relative (destination);
776 Port* src = get_port_by_name_locked (s);
777 Port* dst = get_port_by_name_locked (d);
780 ret = src->disconnect (d);
782 ret = dst->disconnect (s);
784 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
791 AudioEngine::disconnect (Port& port)
793 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
797 fatal << _("disconnect called before engine was started") << endmsg;
804 return port.disconnect_all ();
808 AudioEngine::frame_rate ()
810 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
811 if (_frame_rate == 0) {
812 return (_frame_rate = jack_get_sample_rate (_priv_jack));
819 AudioEngine::raw_buffer_size (DataType t)
821 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
822 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
826 AudioEngine::frames_per_cycle ()
828 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
829 if (_buffer_size == 0) {
830 return (_buffer_size = jack_get_buffer_size (_jack));
836 /** @param name Full name of port (including prefix:)
837 * @return Corresponding Port*, or 0. This object remains the property of the AudioEngine
838 * so must not be deleted.
841 AudioEngine::get_port_by_name (const string& portname)
844 if (portname.find_first_of (':') == string::npos) {
845 s = make_port_name_non_relative (portname);
850 Glib::Mutex::Lock lm (_process_lock);
851 return get_port_by_name_locked (s);
855 AudioEngine::get_port_by_name_locked (const string& portname)
857 /* caller must hold process lock */
861 fatal << _("get_port_by_name_locked() called before engine was started") << endmsg;
868 if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
869 /* not an ardour: port */
873 std::string const rel = make_port_name_relative (portname);
875 boost::shared_ptr<Ports> pr = ports.reader();
877 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
878 if (rel == (*i)->name()) {
887 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
889 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
892 fatal << _("get_ports called before engine was started") << endmsg;
898 return jack_get_ports (_priv_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
902 AudioEngine::halted (void *arg)
904 /* called from jack shutdown handler */
906 AudioEngine* ae = static_cast<AudioEngine *> (arg);
907 bool was_running = ae->_running;
909 ae->stop_metering_thread ();
911 ae->_running = false;
912 ae->_buffer_size = 0;
916 ae->Halted(); /* EMIT SIGNAL */
923 /* called from a signal handler for SIGPIPE */
925 stop_metering_thread ();
934 AudioEngine::can_request_hardware_monitoring ()
936 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
939 if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
950 AudioEngine::n_physical_outputs (DataType type) const
952 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
956 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
960 for (i = 0; ports[i]; ++i) {}
967 AudioEngine::n_physical_inputs (DataType type) const
969 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
973 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
977 for (i = 0; ports[i]; ++i) {}
984 AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
986 GET_PRIVATE_JACK_POINTER (_jack);
991 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
996 for (i = 0; ports[i]; ++i) {
997 ins.push_back (ports[i]);
1004 AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
1006 GET_PRIVATE_JACK_POINTER (_jack);
1007 const char ** ports;
1010 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1014 for (i = 0; ports[i]; ++i) {
1015 outs.push_back (ports[i]);
1021 AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
1023 GET_PRIVATE_JACK_POINTER_RET (_jack,"");
1024 const char ** ports;
1028 assert(type != DataType::NIL);
1030 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|flag)) == 0) {
1034 for (i = 0; i < n && ports[i]; ++i) {}
1040 free ((char *) ports);
1046 AudioEngine::update_total_latency (const Port& port)
1048 port.recompute_total_latency ();
1052 AudioEngine::transport_stop ()
1054 GET_PRIVATE_JACK_POINTER (_jack);
1055 jack_transport_stop (_priv_jack);
1059 AudioEngine::transport_start ()
1061 GET_PRIVATE_JACK_POINTER (_jack);
1062 jack_transport_start (_priv_jack);
1066 AudioEngine::transport_locate (nframes_t where)
1068 GET_PRIVATE_JACK_POINTER (_jack);
1069 // cerr << "tell JACK to locate to " << where << endl;
1070 jack_transport_locate (_priv_jack, where);
1073 AudioEngine::TransportState
1074 AudioEngine::transport_state ()
1076 GET_PRIVATE_JACK_POINTER_RET (_jack, ((TransportState) JackTransportStopped));
1077 jack_position_t pos;
1078 return (TransportState) jack_transport_query (_priv_jack, &pos);
1082 AudioEngine::reset_timebase ()
1084 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1086 if (_session->config.get_jack_time_master()) {
1087 return jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1089 return jack_release_timebase (_jack);
1096 AudioEngine::freewheel (bool onoff)
1098 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1100 if (onoff != _freewheeling) {
1103 _freewheel_thread_registered = false;
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 */
1187 AudioEngine::reconnect_to_jack ()
1190 disconnect_from_jack ();
1191 /* XXX give jackd a chance */
1192 Glib::usleep (250000);
1195 if (connect_to_jack (jack_client_name)) {
1196 error << _("failed to connect to JACK") << endmsg;
1202 boost::shared_ptr<Ports> p = ports.reader ();
1204 for (i = p->begin(); i != p->end(); ++i) {
1205 if ((*i)->reestablish ()) {
1210 if (i != p->end()) {
1212 remove_all_ports ();
1216 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
1219 _session->reset_jack_connection (_priv_jack);
1220 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
1221 _session->set_block_size (blocksize);
1222 _session->set_frame_rate (jack_get_sample_rate (_priv_jack));
1224 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
1225 cout << "FIXME: Assuming maximum MIDI buffer size " << blocksize * 4 << "bytes" << endl;
1226 _raw_buffer_sizes[DataType::MIDI] = blocksize * 4;
1229 last_monitor_check = 0;
1231 jack_on_shutdown (_priv_jack, halted, this);
1232 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
1233 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
1234 jack_set_process_callback (_priv_jack, _process_callback, this);
1235 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
1236 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
1237 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
1238 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
1239 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
1241 if (_session && _session->config.get_jack_time_master()) {
1242 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1245 if (jack_activate (_priv_jack) == 0) {
1252 /* re-establish connections */
1254 for (i = p->begin(); i != p->end(); ++i) {
1258 Running (); /* EMIT SIGNAL*/
1260 start_metering_thread ();
1266 AudioEngine::request_buffer_size (nframes_t nframes)
1268 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1270 if (nframes == jack_get_buffer_size (_priv_jack)) {
1274 return jack_set_buffer_size (_priv_jack, nframes);
1278 AudioEngine::update_total_latencies ()
1280 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1281 GET_PRIVATE_JACK_POINTER (_jack);
1282 jack_recompute_total_latencies (_priv_jack);
1287 AudioEngine::make_port_name_relative (string portname)
1289 string::size_type len;
1290 string::size_type n;
1292 len = portname.length();
1294 for (n = 0; n < len; ++n) {
1295 if (portname[n] == ':') {
1300 if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
1301 return portname.substr (n+1);
1308 AudioEngine::make_port_name_non_relative (string portname)
1312 if (portname.find_first_of (':') != string::npos) {
1316 str = jack_client_name;
1324 AudioEngine::is_realtime () const
1326 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
1327 return jack_is_realtime (_priv_jack);