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, string session_uuid)
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, session_uuid)) {
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 #ifdef HAVE_JACK_SESSION
192 if( jack_set_session_callback )
193 jack_set_session_callback (_priv_jack, _session_callback, this);
195 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
196 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
197 jack_set_port_registration_callback (_priv_jack, _registration_callback, this);
198 jack_set_port_connect_callback (_priv_jack, _connect_callback, this);
200 if (_session && _session->config.get_jack_time_master()) {
201 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
204 jack_set_error_function (ardour_jack_error);
206 if (jack_activate (_priv_jack) == 0) {
209 Running(); /* EMIT SIGNAL */
211 // error << _("cannot activate JACK client") << endmsg;
214 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
217 return _running ? 0 : -1;
221 AudioEngine::stop (bool forever)
223 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
227 disconnect_from_jack ();
229 jack_deactivate (_priv_jack);
230 Stopped(); /* EMIT SIGNAL */
231 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
235 return _running ? -1 : 0;
240 AudioEngine::get_sync_offset (nframes_t& offset) const
243 #ifdef HAVE_JACK_VIDEO_SUPPORT
245 GET_PRIVATE_JACK_POINTER_RET (_jack, false);
250 (void) jack_transport_query (_priv_jack, &pos);
252 if (pos.valid & JackVideoFrameOffset) {
253 offset = pos.video_offset;
266 AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
267 jack_position_t* pos, int new_position, void *arg)
269 static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
273 AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
274 jack_position_t* pos, int new_position)
276 if (_jack && _session && _session->synced_to_jack()) {
277 _session->jack_timebase_callback (state, nframes, pos, new_position);
282 AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
284 return static_cast<AudioEngine*> (arg)->jack_sync_callback (state, pos);
288 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
290 if (_jack && _session) {
291 return _session->jack_sync_callback (state, pos);
298 AudioEngine::_xrun_callback (void *arg)
300 AudioEngine* ae = static_cast<AudioEngine*> (arg);
301 if (ae->connected()) {
302 ae->Xrun (); /* EMIT SIGNAL */
307 #ifdef HAVE_JACK_SESSION
309 AudioEngine::_session_callback (jack_session_event_t *event, void *arg)
311 printf( "helo.... " );
312 AudioEngine* ae = static_cast<AudioEngine*> (arg);
313 if (ae->connected()) {
314 ae->JackSessionEvent ( event ); /* EMIT SIGNAL */
319 AudioEngine::_graph_order_callback (void *arg)
321 AudioEngine* ae = static_cast<AudioEngine*> (arg);
322 if (ae->connected()) {
323 ae->GraphReordered (); /* EMIT SIGNAL */
328 /** Wrapped which is called by JACK as its process callback. It is just
329 * here to get us back into C++ land by calling AudioEngine::process_callback()
330 * @param nframes Number of frames passed by JACK.
331 * @param arg User argument passed by JACK, which will be the AudioEngine*.
334 AudioEngine::_process_callback (nframes_t nframes, void *arg)
336 return static_cast<AudioEngine *> (arg)->process_callback (nframes);
340 AudioEngine::_freewheel_callback (int onoff, void *arg)
342 static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
346 AudioEngine::_registration_callback (jack_port_id_t /*id*/, int /*reg*/, void* arg)
348 AudioEngine* ae = static_cast<AudioEngine*> (arg);
349 ae->PortRegisteredOrUnregistered (); /* EMIT SIGNAL */
353 AudioEngine::_connect_callback (jack_port_id_t /*id_a*/, jack_port_id_t /*id_b*/, int /*conn*/, void* arg)
355 AudioEngine* ae = static_cast<AudioEngine*> (arg);
356 ae->PortConnectedOrDisconnected (); /* EMIT SIGNAL */
360 AudioEngine::split_cycle (nframes_t offset)
362 /* caller must hold process lock */
364 Port::increment_port_offset (offset);
366 /* tell all Ports that we're going to start a new (split) cycle */
368 boost::shared_ptr<Ports> p = ports.reader();
370 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
371 (*i)->cycle_split ();
375 /** Method called by JACK (via _process_callback) which says that there
376 * is work to be done.
377 * @param nframes Number of frames to process.
380 AudioEngine::process_callback (nframes_t nframes)
382 GET_PRIVATE_JACK_POINTER_RET(_jack,0)
383 // CycleTimer ct ("AudioEngine::process");
384 Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
386 /// The number of frames that will have been processed when we've finished
387 nframes_t next_processed_frames;
389 /* handle wrap around of total frames counter */
391 if (max_frames - _processed_frames < nframes) {
392 next_processed_frames = nframes - (max_frames - _processed_frames);
394 next_processed_frames = _processed_frames + nframes;
397 if (!tm.locked() || _session == 0) {
398 /* return having done nothing */
399 _processed_frames = next_processed_frames;
403 if (session_remove_pending) {
404 /* perform the actual session removal */
406 session_remove_pending = false;
407 session_removed.signal();
408 _processed_frames = next_processed_frames;
412 /* tell all relevant objects that we're starting a new cycle */
414 Delivery::CycleStart (nframes);
415 Port::set_port_offset (0);
416 InternalReturn::CycleStart (nframes);
418 /* tell all Ports that we're starting a new cycle */
420 boost::shared_ptr<Ports> p = ports.reader();
422 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
423 (*i)->cycle_start (nframes);
427 /* emit the Freewheel signal and stop freewheeling in the event of trouble
428 * the indirection is to pick up the return value of the signal.
430 if (*Freewheel (nframes)) {
431 jack_set_freewheel (_priv_jack, false);
436 _session->process (nframes);
445 _processed_frames = next_processed_frames;
449 if (last_monitor_check + monitor_check_interval < next_processed_frames) {
451 boost::shared_ptr<Ports> p = ports.reader();
453 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
458 if (port->_last_monitor != (x = port->monitoring_input ())) {
459 port->_last_monitor = x;
460 /* XXX I think this is dangerous, due to
461 a likely mutex in the signal handlers ...
463 port->MonitorInputChanged (x); /* EMIT SIGNAL */
466 last_monitor_check = next_processed_frames;
469 if (_session->silent()) {
471 boost::shared_ptr<Ports> p = ports.reader();
473 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
477 if (port->sends_output()) {
478 port->get_buffer(nframes).silence(nframes);
485 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
486 (*i)->cycle_end (nframes);
489 _processed_frames = next_processed_frames;
494 AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
496 return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
500 AudioEngine::jack_sample_rate_callback (nframes_t nframes)
502 _frame_rate = nframes;
503 _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
505 /* check for monitor input change every 1/10th of second */
507 monitor_check_interval = nframes / 10;
508 last_monitor_check = 0;
511 _session->set_frame_rate (nframes);
514 SampleRateChanged (nframes); /* EMIT SIGNAL */
520 AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
522 return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
526 AudioEngine::jack_bufsize_callback (nframes_t nframes)
528 _buffer_size = nframes;
529 _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof(float);
530 cout << "FIXME: Assuming maximum MIDI buffer size " << nframes * 4 << "bytes" << endl;
531 _raw_buffer_sizes[DataType::MIDI] = nframes * 4;
532 _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
533 last_monitor_check = 0;
535 boost::shared_ptr<Ports> p = ports.reader();
537 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
542 _session->set_block_size (_buffer_size);
549 AudioEngine::stop_metering_thread ()
551 if (m_meter_thread) {
552 g_atomic_int_set (&m_meter_exit, 1);
553 m_meter_thread->join ();
559 AudioEngine::start_metering_thread ()
561 if (m_meter_thread == 0) {
562 g_atomic_int_set (&m_meter_exit, 0);
563 m_meter_thread = Glib::Thread::create (boost::bind (&AudioEngine::meter_thread, this),
564 500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
569 AudioEngine::meter_thread ()
571 pthread_set_name (X_("meter"));
574 Glib::usleep (10000); /* 1/100th sec interval */
575 if (g_atomic_int_get(&m_meter_exit)) {
583 AudioEngine::set_session (Session *s)
585 Glib::Mutex::Lock pl (_process_lock);
587 SessionHandlePtr::set_session (s);
591 start_metering_thread ();
593 nframes_t blocksize = jack_get_buffer_size (_jack);
595 /* page in as much of the session process code as we
596 can before we really start running.
599 boost::shared_ptr<Ports> p = ports.reader();
601 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
602 (*i)->cycle_start (blocksize);
605 _session->process (blocksize);
606 _session->process (blocksize);
607 _session->process (blocksize);
608 _session->process (blocksize);
609 _session->process (blocksize);
610 _session->process (blocksize);
611 _session->process (blocksize);
612 _session->process (blocksize);
614 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
615 (*i)->cycle_end (blocksize);
621 AudioEngine::remove_session ()
623 Glib::Mutex::Lock lm (_process_lock);
627 stop_metering_thread ();
630 session_remove_pending = true;
631 session_removed.wait(_process_lock);
635 SessionHandlePtr::set_session (0);
642 AudioEngine::port_registration_failure (const std::string& portname)
644 GET_PRIVATE_JACK_POINTER (_jack);
645 string full_portname = jack_client_name;
646 full_portname += ':';
647 full_portname += portname;
650 jack_port_t* p = jack_port_by_name (_priv_jack, full_portname.c_str());
654 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
656 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);
659 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
663 AudioEngine::register_port (DataType dtype, const string& portname, bool input)
668 if (dtype == DataType::AUDIO) {
669 newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput));
670 } else if (dtype == DataType::MIDI) {
671 newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput));
673 throw PortRegistrationFailure("unable to create port (unknown type)");
676 size_t& old_buffer_size = _raw_buffer_sizes[newport->type()];
677 size_t port_buffer_size = newport->raw_buffer_size(0);
678 if (port_buffer_size > old_buffer_size) {
679 old_buffer_size = port_buffer_size;
682 RCUWriter<Ports> writer (ports);
683 boost::shared_ptr<Ports> ps = writer.get_copy ();
684 ps->insert (ps->begin(), newport);
686 /* writer goes out of scope, forces update */
691 catch (PortRegistrationFailure& err) {
693 } catch (std::exception& e) {
694 throw PortRegistrationFailure(string_compose(
695 _("unable to create port: %1"), e.what()).c_str());
697 throw PortRegistrationFailure("unable to create port (unknown error)");
702 AudioEngine::register_input_port (DataType type, const string& portname)
704 return register_port (type, portname, true);
708 AudioEngine::register_output_port (DataType type, const string& portname)
710 return register_port (type, portname, false);
714 AudioEngine::unregister_port (Port& port)
716 /* caller must hold process lock */
719 /* probably happening when the engine has been halted by JACK,
720 in which case, there is nothing we can do here.
726 RCUWriter<Ports> writer (ports);
727 boost::shared_ptr<Ports> ps = writer.get_copy ();
729 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
737 /* writer goes out of scope, forces update */
744 AudioEngine::connect (const string& source, const string& destination)
746 /* caller must hold process lock */
752 fatal << _("connect called before engine was started") << endmsg;
759 string s = make_port_name_non_relative (source);
760 string d = make_port_name_non_relative (destination);
763 Port* src = get_port_by_name_locked (s);
764 Port* dst = get_port_by_name_locked (d);
767 ret = src->connect (d);
769 ret = dst->connect (s);
771 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
776 /* already exists - no error, no warning */
777 } else if (ret < 0) {
778 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
779 source, s, destination, d)
787 AudioEngine::disconnect (const string& source, const string& destination)
789 /* caller must hold process lock */
795 fatal << _("disconnect called before engine was started") << endmsg;
802 string s = make_port_name_non_relative (source);
803 string d = make_port_name_non_relative (destination);
805 Port* src = get_port_by_name_locked (s);
806 Port* dst = get_port_by_name_locked (d);
809 ret = src->disconnect (d);
811 ret = dst->disconnect (s);
813 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
820 AudioEngine::disconnect (Port& port)
822 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
826 fatal << _("disconnect called before engine was started") << endmsg;
833 return port.disconnect_all ();
837 AudioEngine::frame_rate () const
839 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
840 if (_frame_rate == 0) {
841 return (_frame_rate = jack_get_sample_rate (_priv_jack));
848 AudioEngine::raw_buffer_size (DataType t)
850 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
851 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
855 AudioEngine::frames_per_cycle () const
857 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
858 if (_buffer_size == 0) {
859 return (_buffer_size = jack_get_buffer_size (_jack));
865 /** @param name Full name of port (including prefix:)
866 * @return Corresponding Port*, or 0. This object remains the property of the AudioEngine
867 * so must not be deleted.
870 AudioEngine::get_port_by_name (const string& portname)
873 if (portname.find_first_of (':') == string::npos) {
874 s = make_port_name_non_relative (portname);
879 Glib::Mutex::Lock lm (_process_lock);
880 return get_port_by_name_locked (s);
884 AudioEngine::get_port_by_name_locked (const string& portname)
886 /* caller must hold process lock */
890 fatal << _("get_port_by_name_locked() called before engine was started") << endmsg;
897 if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
898 /* not an ardour: port */
902 std::string const rel = make_port_name_relative (portname);
904 boost::shared_ptr<Ports> pr = ports.reader();
906 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
907 if (rel == (*i)->name()) {
916 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
918 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
921 fatal << _("get_ports called before engine was started") << endmsg;
927 return jack_get_ports (_priv_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
931 AudioEngine::halted (void *arg)
933 /* called from jack shutdown handler */
935 AudioEngine* ae = static_cast<AudioEngine *> (arg);
936 bool was_running = ae->_running;
938 ae->stop_metering_thread ();
940 ae->_running = false;
941 ae->_buffer_size = 0;
945 ae->Halted(); /* EMIT SIGNAL */
946 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
953 /* called from a signal handler for SIGPIPE */
955 stop_metering_thread ();
964 AudioEngine::can_request_hardware_monitoring ()
966 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
969 if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
980 AudioEngine::n_physical_outputs (DataType type) const
982 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
986 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
990 for (i = 0; ports[i]; ++i) {}
997 AudioEngine::n_physical_inputs (DataType type) const
999 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
1000 const char ** ports;
1003 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
1007 for (i = 0; ports[i]; ++i) {}
1014 AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
1016 GET_PRIVATE_JACK_POINTER (_jack);
1017 const char ** ports;
1019 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
1024 for (uint32_t i = 0; ports[i]; ++i) {
1025 ins.push_back (ports[i]);
1032 AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
1034 GET_PRIVATE_JACK_POINTER (_jack);
1035 const char ** ports;
1038 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1042 for (i = 0; ports[i]; ++i) {
1043 outs.push_back (ports[i]);
1049 AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
1051 GET_PRIVATE_JACK_POINTER_RET (_jack,"");
1052 const char ** ports;
1056 assert(type != DataType::NIL);
1058 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|flag)) == 0) {
1062 for (i = 0; i < n && ports[i]; ++i) {}
1068 free ((const char **) ports);
1074 AudioEngine::update_total_latency (const Port& port)
1076 port.recompute_total_latency ();
1080 AudioEngine::transport_stop ()
1082 GET_PRIVATE_JACK_POINTER (_jack);
1083 jack_transport_stop (_priv_jack);
1087 AudioEngine::transport_start ()
1089 GET_PRIVATE_JACK_POINTER (_jack);
1090 jack_transport_start (_priv_jack);
1094 AudioEngine::transport_locate (nframes_t where)
1096 GET_PRIVATE_JACK_POINTER (_jack);
1097 // cerr << "tell JACK to locate to " << where << endl;
1098 jack_transport_locate (_priv_jack, where);
1101 AudioEngine::TransportState
1102 AudioEngine::transport_state ()
1104 GET_PRIVATE_JACK_POINTER_RET (_jack, ((TransportState) JackTransportStopped));
1105 jack_position_t pos;
1106 return (TransportState) jack_transport_query (_priv_jack, &pos);
1110 AudioEngine::reset_timebase ()
1112 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1114 if (_session->config.get_jack_time_master()) {
1115 return jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1117 return jack_release_timebase (_jack);
1124 AudioEngine::freewheel (bool onoff)
1126 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1128 if (onoff != _freewheeling) {
1129 return jack_set_freewheel (_priv_jack, onoff);
1132 /* already doing what has been asked for */
1138 AudioEngine::remove_all_ports ()
1140 /* process lock MUST be held */
1143 RCUWriter<Ports> writer (ports);
1144 boost::shared_ptr<Ports> ps = writer.get_copy ();
1146 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
1153 /* clear dead wood list too */
1159 AudioEngine::connect_to_jack (string client_name, string session_uuid)
1161 jack_options_t options = JackNullOption;
1162 jack_status_t status;
1163 const char *server_name = NULL;
1165 jack_client_name = client_name; /* might be reset below */
1166 #ifdef HAVE_JACK_SESSION
1167 if (! session_uuid.empty())
1168 _jack = jack_client_open (jack_client_name.c_str(), JackSessionID, &status, session_uuid.c_str());
1171 _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
1173 if (_jack == NULL) {
1174 // error message is not useful here
1178 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1180 if (status & JackNameNotUnique) {
1181 jack_client_name = jack_get_client_name (_priv_jack);
1188 AudioEngine::disconnect_from_jack ()
1190 GET_PRIVATE_JACK_POINTER_RET (_jack, 0);
1193 stop_metering_thread ();
1197 Glib::Mutex::Lock lm (_process_lock);
1198 jack_client_close (_priv_jack);
1204 _raw_buffer_sizes.clear();
1208 Stopped(); /* EMIT SIGNAL */
1209 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
1216 AudioEngine::reconnect_to_jack ()
1219 disconnect_from_jack ();
1220 /* XXX give jackd a chance */
1221 Glib::usleep (250000);
1224 if (connect_to_jack (jack_client_name, "")) {
1225 error << _("failed to connect to JACK") << endmsg;
1231 boost::shared_ptr<Ports> p = ports.reader ();
1233 for (i = p->begin(); i != p->end(); ++i) {
1234 if ((*i)->reestablish ()) {
1239 if (i != p->end()) {
1241 remove_all_ports ();
1245 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
1248 _session->reset_jack_connection (_priv_jack);
1249 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
1250 _session->set_block_size (blocksize);
1251 _session->set_frame_rate (jack_get_sample_rate (_priv_jack));
1253 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
1254 cout << "FIXME: Assuming maximum MIDI buffer size " << blocksize * 4 << "bytes" << endl;
1255 _raw_buffer_sizes[DataType::MIDI] = blocksize * 4;
1258 last_monitor_check = 0;
1260 jack_on_shutdown (_priv_jack, halted, this);
1261 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
1262 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
1263 jack_set_process_callback (_priv_jack, _process_callback, this);
1264 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
1265 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
1266 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
1267 #ifdef HAVE_JACK_SESSION
1268 if( jack_set_session_callback )
1269 jack_set_session_callback (_priv_jack, _session_callback, this);
1271 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
1272 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
1274 if (_session && _session->config.get_jack_time_master()) {
1275 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1278 if (jack_activate (_priv_jack) == 0) {
1285 /* re-establish connections */
1287 for (i = p->begin(); i != p->end(); ++i) {
1291 Running (); /* EMIT SIGNAL*/
1293 start_metering_thread ();
1299 AudioEngine::request_buffer_size (nframes_t nframes)
1301 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1303 if (nframes == jack_get_buffer_size (_priv_jack)) {
1307 return jack_set_buffer_size (_priv_jack, nframes);
1311 AudioEngine::update_total_latencies ()
1313 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1314 GET_PRIVATE_JACK_POINTER (_jack);
1315 jack_recompute_total_latencies (_priv_jack);
1320 AudioEngine::make_port_name_relative (string portname)
1322 string::size_type len;
1323 string::size_type n;
1325 len = portname.length();
1327 for (n = 0; n < len; ++n) {
1328 if (portname[n] == ':') {
1333 if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
1334 return portname.substr (n+1);
1341 AudioEngine::make_port_name_non_relative (string portname)
1345 if (portname.find_first_of (':') != string::npos) {
1349 str = jack_client_name;
1357 AudioEngine::is_realtime () const
1359 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
1360 return jack_is_realtime (_priv_jack);