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 char* c = new char[12];
138 strcpy (c, X_("audioengine"));
139 pthread_set_name (c);
141 PBD::notify_gui_about_thread_creation ("gui", pthread_self(), X_("Audioengine"), 4096);
142 PBD::notify_gui_about_thread_creation ("midiui", pthread_self(), X_("Audioengine"), 128);
144 SessionEvent::create_per_thread_pool (X_("Audioengine"), 512);
146 MIDI::JACK_MidiPort::set_process_thread (pthread_self());
150 ardour_jack_error (const char* msg)
152 error << "JACK: " << msg << endmsg;
156 AudioEngine::start ()
158 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
162 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
165 BootMessage (_("Connect session to engine"));
167 _session->set_block_size (blocksize);
168 _session->set_frame_rate (jack_get_sample_rate (_priv_jack));
170 /* page in as much of the session process code as we
171 can before we really start running.
174 _session->process (blocksize);
175 _session->process (blocksize);
176 _session->process (blocksize);
177 _session->process (blocksize);
178 _session->process (blocksize);
179 _session->process (blocksize);
180 _session->process (blocksize);
181 _session->process (blocksize);
184 _processed_frames = 0;
185 last_monitor_check = 0;
187 jack_on_shutdown (_priv_jack, halted, this);
188 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
189 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
190 jack_set_process_callback (_priv_jack, _process_callback, this);
191 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
192 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
193 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
194 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
195 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
196 jack_set_port_registration_callback (_priv_jack, _registration_callback, this);
198 if (_session && _session->config.get_jack_time_master()) {
199 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
202 jack_set_error_function (ardour_jack_error);
204 if (jack_activate (_priv_jack) == 0) {
207 Running(); /* EMIT SIGNAL */
209 // error << _("cannot activate JACK client") << endmsg;
212 start_metering_thread();
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 */
234 return _running ? -1 : 0;
239 AudioEngine::get_sync_offset (nframes_t& offset) const
242 #ifdef HAVE_JACK_VIDEO_SUPPORT
244 GET_PRIVATE_JACK_POINTER_RET (_jack, false);
249 (void) jack_transport_query (_priv_jack, &pos);
251 if (pos.valid & JackVideoFrameOffset) {
252 offset = pos.video_offset;
265 AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
266 jack_position_t* pos, int new_position, void *arg)
268 static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
272 AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
273 jack_position_t* pos, int new_position)
275 if (_jack && _session && _session->synced_to_jack()) {
276 _session->jack_timebase_callback (state, nframes, pos, new_position);
281 AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
283 return static_cast<AudioEngine*> (arg)->jack_sync_callback (state, pos);
287 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
289 if (_jack && _session) {
290 return _session->jack_sync_callback (state, pos);
297 AudioEngine::_xrun_callback (void *arg)
299 AudioEngine* ae = static_cast<AudioEngine*> (arg);
300 if (ae->connected()) {
301 ae->Xrun (); /* EMIT SIGNAL */
307 AudioEngine::_graph_order_callback (void *arg)
309 AudioEngine* ae = static_cast<AudioEngine*> (arg);
310 if (ae->connected()) {
311 ae->GraphReordered (); /* EMIT SIGNAL */
316 /** Wrapped which is called by JACK as its process callback. It is just
317 * here to get us back into C++ land by calling AudioEngine::process_callback()
318 * @param nframes Number of frames passed by JACK.
319 * @param arg User argument passed by JACK, which will be the AudioEngine*.
322 AudioEngine::_process_callback (nframes_t nframes, void *arg)
324 return static_cast<AudioEngine *> (arg)->process_callback (nframes);
328 AudioEngine::_freewheel_callback (int onoff, void *arg)
330 static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
334 AudioEngine::_registration_callback (jack_port_id_t /*id*/, int /*reg*/, void* arg)
336 AudioEngine* ae = static_cast<AudioEngine*> (arg);
337 ae->PortRegisteredOrUnregistered (); /* EMIT SIGNAL */
341 AudioEngine::split_cycle (nframes_t offset)
343 /* caller must hold process lock */
345 Port::increment_port_offset (offset);
347 /* tell all Ports that we're going to start a new (split) cycle */
349 boost::shared_ptr<Ports> p = ports.reader();
351 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
352 (*i)->cycle_split ();
356 /** Method called by JACK (via _process_callback) which says that there
357 * is work to be done.
358 * @param nframes Number of frames to process.
361 AudioEngine::process_callback (nframes_t nframes)
363 GET_PRIVATE_JACK_POINTER_RET(_jack,0)
364 // CycleTimer ct ("AudioEngine::process");
365 Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
367 /// The number of frames that will have been processed when we've finished
368 nframes_t next_processed_frames;
370 /* handle wrap around of total frames counter */
372 if (max_frames - _processed_frames < nframes) {
373 next_processed_frames = nframes - (max_frames - _processed_frames);
375 next_processed_frames = _processed_frames + nframes;
378 if (!tm.locked() || _session == 0) {
379 /* return having done nothing */
380 _processed_frames = next_processed_frames;
384 if (session_remove_pending) {
385 /* perform the actual session removal */
387 session_remove_pending = false;
388 session_removed.signal();
389 _processed_frames = next_processed_frames;
393 /* tell all relevant objects that we're starting a new cycle */
395 Delivery::CycleStart (nframes);
396 Port::set_port_offset (0);
397 InternalReturn::CycleStart (nframes);
399 /* tell all Ports that we're starting a new cycle */
401 boost::shared_ptr<Ports> p = ports.reader();
403 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
404 (*i)->cycle_start (nframes);
408 /* emit the Freewheel signal and stop freewheeling in the event of trouble
409 * the indirection is to pick up the return value of the signal.
411 if (*Freewheel (nframes)) {
412 jack_set_freewheel (_priv_jack, false);
417 _session->process (nframes);
426 _processed_frames = next_processed_frames;
430 if (last_monitor_check + monitor_check_interval < next_processed_frames) {
432 boost::shared_ptr<Ports> p = ports.reader();
434 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
439 if (port->_last_monitor != (x = port->monitoring_input ())) {
440 port->_last_monitor = x;
441 /* XXX I think this is dangerous, due to
442 a likely mutex in the signal handlers ...
444 port->MonitorInputChanged (x); /* EMIT SIGNAL */
447 last_monitor_check = next_processed_frames;
450 if (_session->silent()) {
452 boost::shared_ptr<Ports> p = ports.reader();
454 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
458 if (port->sends_output()) {
459 port->get_buffer(nframes).silence(nframes);
466 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
467 (*i)->cycle_end (nframes);
470 _processed_frames = next_processed_frames;
475 AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
477 return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
481 AudioEngine::jack_sample_rate_callback (nframes_t nframes)
483 _frame_rate = nframes;
484 _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
486 /* check for monitor input change every 1/10th of second */
488 monitor_check_interval = nframes / 10;
489 last_monitor_check = 0;
492 _session->set_frame_rate (nframes);
495 SampleRateChanged (nframes); /* EMIT SIGNAL */
501 AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
503 return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
507 AudioEngine::jack_bufsize_callback (nframes_t nframes)
509 _buffer_size = nframes;
510 _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof(float);
511 cout << "FIXME: Assuming maximum MIDI buffer size " << nframes * 4 << "bytes" << endl;
512 _raw_buffer_sizes[DataType::MIDI] = nframes * 4;
513 _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
514 last_monitor_check = 0;
516 boost::shared_ptr<Ports> p = ports.reader();
518 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
523 _session->set_block_size (_buffer_size);
530 AudioEngine::stop_metering_thread ()
532 if (m_meter_thread) {
533 g_atomic_int_set (&m_meter_exit, 1);
534 m_meter_thread->join ();
540 AudioEngine::start_metering_thread ()
542 if (m_meter_thread == 0) {
543 g_atomic_int_set (&m_meter_exit, 0);
544 m_meter_thread = Glib::Thread::create (boost::bind (&AudioEngine::meter_thread, this),
545 500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
550 AudioEngine::meter_thread ()
553 Glib::usleep (10000); /* 1/100th sec interval */
554 if (g_atomic_int_get(&m_meter_exit)) {
562 AudioEngine::set_session (Session *s)
564 Glib::Mutex::Lock pl (_process_lock);
566 SessionHandlePtr::set_session (s);
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);
605 session_remove_pending = true;
606 session_removed.wait(_process_lock);
610 SessionHandlePtr::set_session (0);
617 AudioEngine::port_registration_failure (const std::string& portname)
619 GET_PRIVATE_JACK_POINTER (_jack);
620 string full_portname = jack_client_name;
621 full_portname += ':';
622 full_portname += portname;
625 jack_port_t* p = jack_port_by_name (_priv_jack, full_portname.c_str());
629 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
631 reason = _("No more JACK ports are available. You will need to stop Ardour and restart JACK with ports if you need this many tracks.");
634 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
638 AudioEngine::register_port (DataType dtype, const string& portname, bool input)
643 if (dtype == DataType::AUDIO) {
644 newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput));
645 } else if (dtype == DataType::MIDI) {
646 newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput));
648 throw PortRegistrationFailure("unable to create port (unknown type)");
651 size_t& old_buffer_size = _raw_buffer_sizes[newport->type()];
652 size_t port_buffer_size = newport->raw_buffer_size(0);
653 if (port_buffer_size > old_buffer_size) {
654 old_buffer_size = port_buffer_size;
657 RCUWriter<Ports> writer (ports);
658 boost::shared_ptr<Ports> ps = writer.get_copy ();
659 ps->insert (ps->begin(), newport);
661 /* writer goes out of scope, forces update */
666 catch (PortRegistrationFailure& err) {
668 } catch (std::exception& e) {
669 throw PortRegistrationFailure(string_compose(
670 _("unable to create port: %1"), e.what()).c_str());
672 throw PortRegistrationFailure("unable to create port (unknown error)");
677 AudioEngine::register_input_port (DataType type, const string& portname)
679 return register_port (type, portname, true);
683 AudioEngine::register_output_port (DataType type, const string& portname)
685 return register_port (type, portname, false);
689 AudioEngine::unregister_port (Port& port)
691 /* caller must hold process lock */
694 /* probably happening when the engine has been halted by JACK,
695 in which case, there is nothing we can do here.
701 RCUWriter<Ports> writer (ports);
702 boost::shared_ptr<Ports> ps = writer.get_copy ();
704 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
712 /* writer goes out of scope, forces update */
719 AudioEngine::connect (const string& source, const string& destination)
721 /* caller must hold process lock */
727 fatal << _("connect called before engine was started") << endmsg;
734 string s = make_port_name_non_relative (source);
735 string d = make_port_name_non_relative (destination);
738 Port* src = get_port_by_name_locked (s);
739 Port* dst = get_port_by_name_locked (d);
742 ret = src->connect (d);
744 ret = dst->connect (s);
746 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
751 /* already exists - no error, no warning */
752 } else if (ret < 0) {
753 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
754 source, s, destination, d)
762 AudioEngine::disconnect (const string& source, const string& destination)
764 /* caller must hold process lock */
770 fatal << _("disconnect called before engine was started") << endmsg;
777 string s = make_port_name_non_relative (source);
778 string d = make_port_name_non_relative (destination);
780 Port* src = get_port_by_name_locked (s);
781 Port* dst = get_port_by_name_locked (d);
784 ret = src->disconnect (d);
786 ret = dst->disconnect (s);
788 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
795 AudioEngine::disconnect (Port& port)
797 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
801 fatal << _("disconnect called before engine was started") << endmsg;
808 return port.disconnect_all ();
812 AudioEngine::frame_rate ()
814 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
815 if (_frame_rate == 0) {
816 return (_frame_rate = jack_get_sample_rate (_priv_jack));
823 AudioEngine::raw_buffer_size (DataType t)
825 std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
826 return (s != _raw_buffer_sizes.end()) ? s->second : 0;
830 AudioEngine::frames_per_cycle ()
832 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
833 if (_buffer_size == 0) {
834 return (_buffer_size = jack_get_buffer_size (_jack));
840 /** @param name Full name of port (including prefix:)
841 * @return Corresponding Port*, or 0. This object remains the property of the AudioEngine
842 * so must not be deleted.
845 AudioEngine::get_port_by_name (const string& portname)
848 if (portname.find_first_of (':') == string::npos) {
849 s = make_port_name_non_relative (portname);
854 Glib::Mutex::Lock lm (_process_lock);
855 return get_port_by_name_locked (s);
859 AudioEngine::get_port_by_name_locked (const string& portname)
861 /* caller must hold process lock */
865 fatal << _("get_port_by_name_locked() called before engine was started") << endmsg;
872 if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
873 /* not an ardour: port */
877 std::string const rel = make_port_name_relative (portname);
879 boost::shared_ptr<Ports> pr = ports.reader();
881 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
882 if (rel == (*i)->name()) {
891 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
893 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
896 fatal << _("get_ports called before engine was started") << endmsg;
902 return jack_get_ports (_priv_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
906 AudioEngine::halted (void *arg)
908 /* called from jack shutdown handler */
910 AudioEngine* ae = static_cast<AudioEngine *> (arg);
911 bool was_running = ae->_running;
913 ae->stop_metering_thread ();
915 ae->_running = false;
916 ae->_buffer_size = 0;
920 ae->Halted(); /* EMIT SIGNAL */
927 /* called from a signal handler for SIGPIPE */
929 stop_metering_thread ();
938 AudioEngine::can_request_hardware_monitoring ()
940 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
943 if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
954 AudioEngine::n_physical_outputs (DataType type) const
956 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
960 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
964 for (i = 0; ports[i]; ++i) {}
971 AudioEngine::n_physical_inputs (DataType type) const
973 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
977 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
981 for (i = 0; ports[i]; ++i) {}
988 AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
990 GET_PRIVATE_JACK_POINTER (_jack);
995 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsOutput)) == 0) {
1000 for (i = 0; ports[i]; ++i) {
1001 ins.push_back (ports[i]);
1008 AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
1010 GET_PRIVATE_JACK_POINTER (_jack);
1011 const char ** ports;
1014 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|JackPortIsInput)) == 0) {
1018 for (i = 0; ports[i]; ++i) {
1019 outs.push_back (ports[i]);
1025 AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
1027 GET_PRIVATE_JACK_POINTER_RET (_jack,"");
1028 const char ** ports;
1032 assert(type != DataType::NIL);
1034 if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical|flag)) == 0) {
1038 for (i = 0; i < n && ports[i]; ++i) {}
1044 free ((char *) ports);
1050 AudioEngine::update_total_latency (const Port& port)
1052 port.recompute_total_latency ();
1056 AudioEngine::transport_stop ()
1058 GET_PRIVATE_JACK_POINTER (_jack);
1059 jack_transport_stop (_priv_jack);
1063 AudioEngine::transport_start ()
1065 GET_PRIVATE_JACK_POINTER (_jack);
1066 jack_transport_start (_priv_jack);
1070 AudioEngine::transport_locate (nframes_t where)
1072 GET_PRIVATE_JACK_POINTER (_jack);
1073 // cerr << "tell JACK to locate to " << where << endl;
1074 jack_transport_locate (_priv_jack, where);
1077 AudioEngine::TransportState
1078 AudioEngine::transport_state ()
1080 GET_PRIVATE_JACK_POINTER_RET (_jack, ((TransportState) JackTransportStopped));
1081 jack_position_t pos;
1082 return (TransportState) jack_transport_query (_priv_jack, &pos);
1086 AudioEngine::reset_timebase ()
1088 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1090 if (_session->config.get_jack_time_master()) {
1091 return jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1093 return jack_release_timebase (_jack);
1100 AudioEngine::freewheel (bool onoff)
1102 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1104 if (onoff != _freewheeling) {
1107 _freewheel_thread_registered = false;
1110 return jack_set_freewheel (_priv_jack, onoff);
1113 /* already doing what has been asked for */
1119 AudioEngine::remove_all_ports ()
1121 /* process lock MUST be held */
1124 RCUWriter<Ports> writer (ports);
1125 boost::shared_ptr<Ports> ps = writer.get_copy ();
1127 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
1134 /* clear dead wood list too */
1140 AudioEngine::connect_to_jack (string client_name)
1142 jack_options_t options = JackNullOption;
1143 jack_status_t status;
1144 const char *server_name = NULL;
1146 jack_client_name = client_name; /* might be reset below */
1147 _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
1149 if (_jack == NULL) {
1150 // error message is not useful here
1154 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1156 if (status & JackNameNotUnique) {
1157 jack_client_name = jack_get_client_name (_priv_jack);
1164 AudioEngine::disconnect_from_jack ()
1166 GET_PRIVATE_JACK_POINTER_RET (_jack, 0);
1169 stop_metering_thread ();
1173 Glib::Mutex::Lock lm (_process_lock);
1174 jack_client_close (_priv_jack);
1180 _raw_buffer_sizes.clear();
1184 Stopped(); /* EMIT SIGNAL */
1191 AudioEngine::reconnect_to_jack ()
1194 disconnect_from_jack ();
1195 /* XXX give jackd a chance */
1196 Glib::usleep (250000);
1199 if (connect_to_jack (jack_client_name)) {
1200 error << _("failed to connect to JACK") << endmsg;
1206 boost::shared_ptr<Ports> p = ports.reader ();
1208 for (i = p->begin(); i != p->end(); ++i) {
1209 if ((*i)->reestablish ()) {
1214 if (i != p->end()) {
1216 remove_all_ports ();
1220 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
1223 _session->reset_jack_connection (_priv_jack);
1224 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
1225 _session->set_block_size (blocksize);
1226 _session->set_frame_rate (jack_get_sample_rate (_priv_jack));
1228 _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
1229 cout << "FIXME: Assuming maximum MIDI buffer size " << blocksize * 4 << "bytes" << endl;
1230 _raw_buffer_sizes[DataType::MIDI] = blocksize * 4;
1233 last_monitor_check = 0;
1235 jack_on_shutdown (_priv_jack, halted, this);
1236 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
1237 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
1238 jack_set_process_callback (_priv_jack, _process_callback, this);
1239 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
1240 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
1241 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
1242 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
1243 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
1245 if (_session && _session->config.get_jack_time_master()) {
1246 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1249 if (jack_activate (_priv_jack) == 0) {
1256 /* re-establish connections */
1258 for (i = p->begin(); i != p->end(); ++i) {
1262 Running (); /* EMIT SIGNAL*/
1264 start_metering_thread ();
1270 AudioEngine::request_buffer_size (nframes_t nframes)
1272 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1274 if (nframes == jack_get_buffer_size (_priv_jack)) {
1278 return jack_set_buffer_size (_priv_jack, nframes);
1282 AudioEngine::update_total_latencies ()
1284 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1285 GET_PRIVATE_JACK_POINTER (_jack);
1286 jack_recompute_total_latencies (_priv_jack);
1291 AudioEngine::make_port_name_relative (string portname)
1293 string::size_type len;
1294 string::size_type n;
1296 len = portname.length();
1298 for (n = 0; n < len; ++n) {
1299 if (portname[n] == ':') {
1304 if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
1305 return portname.substr (n+1);
1312 AudioEngine::make_port_name_non_relative (string portname)
1316 if (portname.find_first_of (':') != string::npos) {
1320 str = jack_client_name;
1328 AudioEngine::is_realtime () const
1330 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
1331 return jack_is_realtime (_priv_jack);