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 SessionEvent::create_per_thread_pool (X_("Audioengine"), 512);
141 MIDI::JACK_MidiPort::set_process_thread (pthread_self());
145 ardour_jack_error (const char* msg)
147 error << "JACK: " << msg << endmsg;
151 AudioEngine::start ()
153 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
157 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
160 BootMessage (_("Connect session to engine"));
162 session->set_block_size (blocksize);
163 session->set_frame_rate (jack_get_sample_rate (_priv_jack));
165 /* page in as much of the session process code as we
166 can before we really start running.
169 session->process (blocksize);
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);
179 _processed_frames = 0;
180 last_monitor_check = 0;
182 jack_on_shutdown (_priv_jack, halted, this);
183 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
184 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
185 jack_set_process_callback (_priv_jack, _process_callback, this);
186 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
187 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
188 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
189 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
190 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
191 jack_set_port_registration_callback (_priv_jack, _registration_callback, this);
193 if (session && session->config.get_jack_time_master()) {
194 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
197 jack_set_error_function (ardour_jack_error);
199 if (jack_activate (_priv_jack) == 0) {
202 Running(); /* EMIT SIGNAL */
204 // error << _("cannot activate JACK client") << endmsg;
207 start_metering_thread();
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 */
229 return _running ? -1 : 0;
234 AudioEngine::get_sync_offset (nframes_t& offset) const
237 #ifdef HAVE_JACK_VIDEO_SUPPORT
239 GET_PRIVATE_JACK_POINTER_RET (_jack, false);
244 (void) jack_transport_query (_priv_jack, &pos);
246 if (pos.valid & JackVideoFrameOffset) {
247 offset = pos.video_offset;
260 AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
261 jack_position_t* pos, int new_position, void *arg)
263 static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
267 AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
268 jack_position_t* pos, int new_position)
270 if (_jack && session && session->synced_to_jack()) {
271 session->jack_timebase_callback (state, nframes, pos, new_position);
276 AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
278 return static_cast<AudioEngine*> (arg)->jack_sync_callback (state, pos);
282 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
284 if (_jack && session) {
285 return session->jack_sync_callback (state, pos);
292 AudioEngine::_xrun_callback (void *arg)
294 AudioEngine* ae = static_cast<AudioEngine*> (arg);
295 if (ae->connected()) {
296 ae->Xrun (); /* EMIT SIGNAL */
302 AudioEngine::_graph_order_callback (void *arg)
304 AudioEngine* ae = static_cast<AudioEngine*> (arg);
305 if (ae->connected()) {
306 ae->GraphReordered (); /* EMIT SIGNAL */
311 /** Wrapped which is called by JACK as its process callback. It is just
312 * here to get us back into C++ land by calling AudioEngine::process_callback()
313 * @param nframes Number of frames passed by JACK.
314 * @param arg User argument passed by JACK, which will be the AudioEngine*.
317 AudioEngine::_process_callback (nframes_t nframes, void *arg)
319 return static_cast<AudioEngine *> (arg)->process_callback (nframes);
323 AudioEngine::_freewheel_callback (int onoff, void *arg)
325 static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
329 AudioEngine::_registration_callback (jack_port_id_t /*id*/, int /*reg*/, void* arg)
331 AudioEngine* ae = static_cast<AudioEngine*> (arg);
332 ae->PortRegisteredOrUnregistered (); /* EMIT SIGNAL */
336 AudioEngine::split_cycle (nframes_t offset)
338 /* caller must hold process lock */
340 Port::increment_port_offset (offset);
342 /* tell all Ports that we're going to start a new (split) cycle */
344 boost::shared_ptr<Ports> p = ports.reader();
346 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
347 (*i)->cycle_split ();
351 /** Method called by JACK (via _process_callback) which says that there
352 * is work to be done.
353 * @param nframes Number of frames to process.
356 AudioEngine::process_callback (nframes_t nframes)
358 GET_PRIVATE_JACK_POINTER_RET(_jack,0)
359 // CycleTimer ct ("AudioEngine::process");
360 Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
362 /// The number of frames that will have been processed when we've finished
363 nframes_t next_processed_frames;
365 /* handle wrap around of total frames counter */
367 if (max_frames - _processed_frames < nframes) {
368 next_processed_frames = nframes - (max_frames - _processed_frames);
370 next_processed_frames = _processed_frames + nframes;
373 if (!tm.locked() || session == 0) {
374 /* return having done nothing */
375 _processed_frames = next_processed_frames;
379 if (session_remove_pending) {
380 /* perform the actual session removal */
382 session_remove_pending = false;
383 session_removed.signal();
384 _processed_frames = next_processed_frames;
388 /* tell all relevant objects that we're starting a new cycle */
390 Delivery::CycleStart (nframes);
391 Port::set_port_offset (0);
392 InternalReturn::CycleStart (nframes);
394 /* tell all Ports that we're starting a new cycle */
396 boost::shared_ptr<Ports> p = ports.reader();
398 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
399 (*i)->cycle_start (nframes);
403 /* emit the Freewheel signal and stop freewheeling in the event of trouble */
404 if (Freewheel (nframes)) {
405 jack_set_freewheel (_priv_jack, false);
410 session->process (nframes);
419 _processed_frames = next_processed_frames;
423 if (last_monitor_check + monitor_check_interval < next_processed_frames) {
425 boost::shared_ptr<Ports> p = ports.reader();
427 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
432 if (port->_last_monitor != (x = port->monitoring_input ())) {
433 port->_last_monitor = x;
434 /* XXX I think this is dangerous, due to
435 a likely mutex in the signal handlers ...
437 port->MonitorInputChanged (x); /* EMIT SIGNAL */
440 last_monitor_check = next_processed_frames;
443 if (session->silent()) {
445 boost::shared_ptr<Ports> p = ports.reader();
447 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
451 if (port->sends_output()) {
452 port->get_buffer(nframes).silence(nframes);
459 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
460 (*i)->cycle_end (nframes);
463 _processed_frames = next_processed_frames;
468 AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
470 return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
474 AudioEngine::jack_sample_rate_callback (nframes_t nframes)
476 _frame_rate = nframes;
477 _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
479 /* check for monitor input change every 1/10th of second */
481 monitor_check_interval = nframes / 10;
482 last_monitor_check = 0;
485 session->set_frame_rate (nframes);
488 SampleRateChanged (nframes); /* EMIT SIGNAL */
494 AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
496 return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
500 AudioEngine::jack_bufsize_callback (nframes_t nframes)
502 _buffer_size = nframes;
503 _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof(float);
504 cout << "FIXME: Assuming maximum MIDI buffer size " << nframes * 4 << "bytes" << endl;
505 _raw_buffer_sizes[DataType::MIDI] = nframes * 4;
506 _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
507 last_monitor_check = 0;
509 boost::shared_ptr<Ports> p = ports.reader();
511 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
516 session->set_block_size (_buffer_size);
523 AudioEngine::stop_metering_thread ()
525 if (m_meter_thread) {
526 g_atomic_int_set (&m_meter_exit, 1);
527 m_meter_thread->join ();
533 AudioEngine::start_metering_thread ()
535 if (m_meter_thread == 0) {
536 g_atomic_int_set (&m_meter_exit, 0);
537 m_meter_thread = Glib::Thread::create (sigc::mem_fun(this, &AudioEngine::meter_thread),
538 500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
543 AudioEngine::meter_thread ()
546 Glib::usleep (10000); /* 1/100th sec interval */
547 if (g_atomic_int_get(&m_meter_exit)) {
550 Metering::update_meters ();
555 AudioEngine::set_session (Session *s)
557 Glib::Mutex::Lock pl (_process_lock);
563 nframes_t blocksize = jack_get_buffer_size (_jack);
565 /* page in as much of the session process code as we
566 can before we really start running.
569 boost::shared_ptr<Ports> p = ports.reader();
571 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
572 (*i)->cycle_start (blocksize);
575 s->process (blocksize);
576 s->process (blocksize);
577 s->process (blocksize);
578 s->process (blocksize);
579 s->process (blocksize);
580 s->process (blocksize);
581 s->process (blocksize);
582 s->process (blocksize);
584 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
585 (*i)->cycle_end (blocksize);
591 AudioEngine::remove_session ()
593 Glib::Mutex::Lock lm (_process_lock);
598 session_remove_pending = true;
599 session_removed.wait(_process_lock);
610 AudioEngine::port_registration_failure (const std::string& portname)
612 GET_PRIVATE_JACK_POINTER (_jack);
613 string full_portname = jack_client_name;
614 full_portname += ':';
615 full_portname += portname;
618 jack_port_t* p = jack_port_by_name (_priv_jack, full_portname.c_str());
622 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
624 reason = _("No more JACK ports are available. You will need to stop Ardour and restart JACK with ports if you need this many tracks.");
627 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
631 AudioEngine::register_port (DataType dtype, const string& portname, bool input)
636 if (dtype == DataType::AUDIO) {
637 newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput));
638 } else if (dtype == DataType::MIDI) {
639 newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput));
641 throw PortRegistrationFailure("unable to create port (unknown type)");
644 size_t& old_buffer_size = _raw_buffer_sizes[newport->type()];
645 size_t port_buffer_size = newport->raw_buffer_size(0);
646 if (port_buffer_size > old_buffer_size) {
647 old_buffer_size = port_buffer_size;
650 RCUWriter<Ports> writer (ports);
651 boost::shared_ptr<Ports> ps = writer.get_copy ();
652 ps->insert (ps->begin(), newport);
654 /* writer goes out of scope, forces update */
659 catch (PortRegistrationFailure& err) {
661 } catch (std::exception& e) {
662 throw PortRegistrationFailure(string_compose(
663 _("unable to create port: %1"), e.what()).c_str());
665 throw PortRegistrationFailure("unable to create port (unknown error)");
670 AudioEngine::register_input_port (DataType type, const string& portname)
672 return register_port (type, portname, true);
676 AudioEngine::register_output_port (DataType type, const string& portname)
678 return register_port (type, portname, false);
682 AudioEngine::unregister_port (Port& port)
684 /* caller must hold process lock */
687 /* probably happening when the engine has been halted by JACK,
688 in which case, there is nothing we can do here.
694 RCUWriter<Ports> writer (ports);
695 boost::shared_ptr<Ports> ps = writer.get_copy ();
697 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
705 /* writer goes out of scope, forces update */
712 AudioEngine::connect (const string& source, const string& destination)
714 /* caller must hold process lock */
720 fatal << _("connect called before engine was started") << endmsg;
727 string s = make_port_name_non_relative (source);
728 string d = make_port_name_non_relative (destination);
731 Port* src = get_port_by_name_locked (s);
732 Port* dst = get_port_by_name_locked (d);
735 ret = src->connect (d);
737 ret = dst->connect (s);
739 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
744 /* already exists - no error, no warning */
745 } else if (ret < 0) {
746 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
747 source, s, destination, d)
755 AudioEngine::disconnect (const string& source, const string& destination)
757 /* caller must hold process lock */
763 fatal << _("disconnect called before engine was started") << endmsg;
770 string s = make_port_name_non_relative (source);
771 string d = make_port_name_non_relative (destination);
773 Port* src = get_port_by_name_locked (s);
774 Port* dst = get_port_by_name_locked (d);
777 ret = src->disconnect (d);
779 ret = dst->disconnect (s);
781 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
788 AudioEngine::disconnect (Port& port)
790 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
794 fatal << _("disconnect called before engine was started") << endmsg;
801 return port.disconnect_all ();
805 AudioEngine::frame_rate ()
807 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
808 if (_frame_rate == 0) {
809 return (_frame_rate = jack_get_sample_rate (_priv_jack));
816 AudioEngine::raw_buffer_size (DataType t)
818 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
819 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
823 AudioEngine::frames_per_cycle ()
825 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
826 if (_buffer_size == 0) {
827 return (_buffer_size = jack_get_buffer_size (_jack));
833 /** @param name Full name of port (including prefix:)
834 * @return Corresponding Port*, or 0. This object remains the property of the AudioEngine
835 * so must not be deleted.
838 AudioEngine::get_port_by_name (const string& portname)
841 if (portname.find_first_of (':') == string::npos) {
842 s = make_port_name_non_relative (portname);
847 Glib::Mutex::Lock lm (_process_lock);
848 return get_port_by_name_locked (s);
852 AudioEngine::get_port_by_name_locked (const string& portname)
854 /* caller must hold process lock */
858 fatal << _("get_port_by_name_locked() called before engine was started") << endmsg;
865 if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
866 /* not an ardour: port */
870 std::string const rel = make_port_name_relative (portname);
872 boost::shared_ptr<Ports> pr = ports.reader();
874 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
875 if (rel == (*i)->name()) {
884 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
886 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
889 fatal << _("get_ports called before engine was started") << endmsg;
895 return jack_get_ports (_priv_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
899 AudioEngine::halted (void *arg)
901 /* called from jack shutdown handler */
903 AudioEngine* ae = static_cast<AudioEngine *> (arg);
904 bool was_running = ae->_running;
906 ae->stop_metering_thread ();
908 ae->_running = false;
909 ae->_buffer_size = 0;
913 ae->Halted(); /* EMIT SIGNAL */
920 /* called from a signal handler for SIGPIPE */
922 stop_metering_thread ();
931 AudioEngine::can_request_hardware_monitoring ()
933 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
936 if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
947 AudioEngine::n_physical_outputs (DataType type) const
949 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
953 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
957 for (i = 0; ports[i]; ++i) {}
964 AudioEngine::n_physical_inputs (DataType type) const
966 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
970 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
974 for (i = 0; ports[i]; ++i) {}
981 AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
983 GET_PRIVATE_JACK_POINTER (_jack);
988 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
993 for (i = 0; ports[i]; ++i) {
994 ins.push_back (ports[i]);
1001 AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
1003 GET_PRIVATE_JACK_POINTER (_jack);
1004 const char ** ports;
1007 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1011 for (i = 0; ports[i]; ++i) {
1012 outs.push_back (ports[i]);
1018 AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
1020 GET_PRIVATE_JACK_POINTER_RET (_jack,"");
1021 const char ** ports;
1025 assert(type != DataType::NIL);
1027 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|flag)) == 0) {
1031 for (i = 0; i < n && ports[i]; ++i) {}
1037 free ((char *) ports);
1043 AudioEngine::update_total_latency (const Port& port)
1045 port.recompute_total_latency ();
1049 AudioEngine::transport_stop ()
1051 GET_PRIVATE_JACK_POINTER (_jack);
1052 jack_transport_stop (_priv_jack);
1056 AudioEngine::transport_start ()
1058 GET_PRIVATE_JACK_POINTER (_jack);
1059 jack_transport_start (_priv_jack);
1063 AudioEngine::transport_locate (nframes_t where)
1065 GET_PRIVATE_JACK_POINTER (_jack);
1066 // cerr << "tell JACK to locate to " << where << endl;
1067 jack_transport_locate (_priv_jack, where);
1070 AudioEngine::TransportState
1071 AudioEngine::transport_state ()
1073 GET_PRIVATE_JACK_POINTER_RET (_jack, ((TransportState) JackTransportStopped));
1074 jack_position_t pos;
1075 return (TransportState) jack_transport_query (_priv_jack, &pos);
1079 AudioEngine::reset_timebase ()
1081 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1083 if (session->config.get_jack_time_master()) {
1084 return jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1086 return jack_release_timebase (_jack);
1093 AudioEngine::freewheel (bool onoff)
1095 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1097 if (onoff != _freewheeling) {
1100 _freewheel_thread_registered = false;
1103 return jack_set_freewheel (_priv_jack, onoff);
1106 /* already doing what has been asked for */
1112 AudioEngine::remove_all_ports ()
1114 /* process lock MUST be held */
1117 RCUWriter<Ports> writer (ports);
1118 boost::shared_ptr<Ports> ps = writer.get_copy ();
1120 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
1127 /* clear dead wood list too */
1133 AudioEngine::connect_to_jack (string client_name)
1135 jack_options_t options = JackNullOption;
1136 jack_status_t status;
1137 const char *server_name = NULL;
1139 jack_client_name = client_name; /* might be reset below */
1140 _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
1142 if (_jack == NULL) {
1143 // error message is not useful here
1147 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1149 if (status & JackNameNotUnique) {
1150 jack_client_name = jack_get_client_name (_priv_jack);
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);