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.
25 #include <glibmm/timer.h>
26 #include <pbd/pthread_utils.h>
27 #include <pbd/stacktrace.h>
29 #include <ardour/audioengine.h>
30 #include <ardour/buffer.h>
31 #include <ardour/port.h>
32 #include <ardour/io.h>
33 #include <ardour/session.h>
34 #include <ardour/cycle_timer.h>
35 #include <ardour/utils.h>
37 #include <ardour/timestamps.h>
42 using namespace ARDOUR;
45 gint AudioEngine::m_meter_exit;
46 AudioEngine* AudioEngine::_instance = 0;
48 #define GET_PRIVATE_JACK_POINTER(j) jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return; }
49 #define GET_PRIVATE_JACK_POINTER_RET(j,r) jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return r; }
52 ardour_jack_error (const char* msg)
54 // throw JACK errors away - they cause visual clutter
55 // error << "JACK: " << msg << endmsg;
58 AudioEngine::AudioEngine (string client_name)
61 _instance = this; /* singleton */
64 session_remove_pending = false;
67 last_monitor_check = 0;
68 monitor_check_interval = max_frames;
69 _processed_frames = 0;
74 _freewheel_thread_registered = false;
75 _freewheeling = false;
78 g_atomic_int_set (&m_meter_exit, 0);
80 if (connect_to_jack (client_name)) {
81 throw NoBackendAvailable ();
85 AudioEngine::~AudioEngine ()
88 Glib::Mutex::Lock tm (_process_lock);
89 session_removed.signal ();
92 jack_client_close (_jack);
96 stop_metering_thread ();
101 _thread_init_callback (void *arg)
103 /* make sure that anybody who needs to know about this thread
107 PBD::notify_gui_about_thread_creation (pthread_self(), X_("Audioengine"), 4096);
111 AudioEngine::start ()
113 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
117 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
118 Port::set_buffer_size (blocksize);
122 BootMessage (_("Connect session to engine"));
124 session->set_block_size (blocksize);
125 session->set_frame_rate (jack_get_sample_rate (_priv_jack));
127 /* page in as much of the session process code as we
128 can before we really start running.
131 session->process (blocksize);
132 session->process (blocksize);
133 session->process (blocksize);
134 session->process (blocksize);
135 session->process (blocksize);
136 session->process (blocksize);
137 session->process (blocksize);
138 session->process (blocksize);
141 _processed_frames = 0;
142 last_monitor_check = 0;
144 jack_on_shutdown (_priv_jack, halted, this);
145 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
146 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
147 jack_set_process_callback (_priv_jack, _process_callback, this);
148 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
149 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
150 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
151 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
152 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
154 if (Config->get_jack_time_master()) {
155 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
158 if (jack_activate (_priv_jack) == 0) {
161 Running(); /* EMIT SIGNAL */
163 // error << _("cannot activate JACK client") << endmsg;
166 start_metering_thread();
169 return _running ? 0 : -1;
173 AudioEngine::stop (bool forever)
175 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
179 disconnect_from_jack ();
181 jack_deactivate (_priv_jack);
182 Stopped(); /* EMIT SIGNAL */
186 return _running ? -1 : 0;
191 AudioEngine::get_sync_offset (nframes_t& offset) const
194 #ifdef HAVE_JACK_VIDEO_SUPPORT
196 GET_PRIVATE_JACK_POINTER_RET (_jack, false);
201 (void) jack_transport_query (_priv_jack, &pos);
203 if (pos.valid & JackVideoFrameOffset) {
204 offset = pos.video_offset;
215 AudioEngine::_jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
216 jack_position_t* pos, int new_position, void *arg)
218 static_cast<AudioEngine*> (arg)->jack_timebase_callback (state, nframes, pos, new_position);
222 AudioEngine::jack_timebase_callback (jack_transport_state_t state, nframes_t nframes,
223 jack_position_t* pos, int new_position)
225 if (_jack && session && session->synced_to_jack()) {
226 session->jack_timebase_callback (state, nframes, pos, new_position);
231 AudioEngine::_jack_sync_callback (jack_transport_state_t state, jack_position_t* pos, void* arg)
233 return static_cast<AudioEngine*> (arg)->jack_sync_callback (state, pos);
237 AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t* pos)
239 if (_jack && session) {
240 return session->jack_sync_callback (state, pos);
247 AudioEngine::_xrun_callback (void *arg)
249 AudioEngine* ae = static_cast<AudioEngine*> (arg);
250 if (ae->connected()) {
251 ae->Xrun (); /* EMIT SIGNAL */
257 AudioEngine::_graph_order_callback (void *arg)
259 AudioEngine* ae = static_cast<AudioEngine*> (arg);
260 if (ae->connected()) {
261 ae->GraphReordered (); /* EMIT SIGNAL */
267 AudioEngine::_process_callback (nframes_t nframes, void *arg)
269 return static_cast<AudioEngine *> (arg)->process_callback (nframes);
273 AudioEngine::_freewheel_callback (int onoff, void *arg)
275 static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
279 AudioEngine::process_callback (nframes_t nframes)
281 // CycleTimer ct ("AudioEngine::process");
282 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
283 Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
284 nframes_t next_processed_frames;
286 /* handle wrap around of total frames counter */
288 if (max_frames - _processed_frames < nframes) {
289 next_processed_frames = nframes - (max_frames - _processed_frames);
291 next_processed_frames = _processed_frames + nframes;
294 if (!tm.locked() || session == 0) {
295 _processed_frames = next_processed_frames;
299 if (session_remove_pending) {
301 session_remove_pending = false;
302 session_removed.signal();
303 _processed_frames = next_processed_frames;
307 /* reset port buffer offset for the start of a new cycle */
309 Port::set_port_offset (0);
311 /* tell all IO objects that we're starting a new cycle */
313 IO::CycleStart (nframes);
316 if (Freewheel (nframes)) {
317 jack_set_freewheel (_priv_jack, false);
322 session->process (nframes);
325 /* we were zombified, maybe because a ladspa plugin took
326 too long, or jackd exited, or something like that.
329 _processed_frames = next_processed_frames;
333 if (last_monitor_check + monitor_check_interval < next_processed_frames) {
335 boost::shared_ptr<Ports> p = ports.reader();
337 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
342 if (port->_last_monitor != (x = port->monitoring_input ())) {
343 port->_last_monitor = x;
344 /* XXX I think this is dangerous, due to
345 a likely mutex in the signal handlers ...
347 port->MonitorInputChanged (x); /* EMIT SIGNAL */
350 last_monitor_check = next_processed_frames;
353 if (session->silent()) {
355 boost::shared_ptr<Ports> p = ports.reader();
357 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
361 if (port->sends_output()) {
362 Sample *buf = port->get_buffer (nframes);
363 memset (buf, 0, sizeof(Sample) * nframes);
364 // this should work but doesn't
365 //port->silence(0, nframes); //need to implement declicking fade
370 _processed_frames = next_processed_frames;
375 AudioEngine::_sample_rate_callback (nframes_t nframes, void *arg)
377 return static_cast<AudioEngine *> (arg)->jack_sample_rate_callback (nframes);
381 AudioEngine::jack_sample_rate_callback (nframes_t nframes)
383 _frame_rate = nframes;
384 _usecs_per_cycle = (int) floor ((((double) frames_per_cycle() / nframes)) * 1000000.0);
386 /* check for monitor input change every 1/10th of second */
388 monitor_check_interval = nframes / 10;
389 last_monitor_check = 0;
392 session->set_frame_rate (nframes);
395 SampleRateChanged (nframes); /* EMIT SIGNAL */
401 AudioEngine::_bufsize_callback (nframes_t nframes, void *arg)
403 return static_cast<AudioEngine *> (arg)->jack_bufsize_callback (nframes);
407 AudioEngine::jack_bufsize_callback (nframes_t nframes)
409 _buffer_size = nframes;
410 _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
411 last_monitor_check = 0;
413 cerr << "bufsize: Set Port buffer size to " << nframes << endl;
414 Port::set_buffer_size (nframes);
416 boost::shared_ptr<Ports> p = ports.reader();
418 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
423 session->set_block_size (_buffer_size);
430 AudioEngine::stop_metering_thread ()
432 if (m_meter_thread) {
433 g_atomic_int_set (&m_meter_exit, 1);
434 m_meter_thread->join ();
440 AudioEngine::start_metering_thread ()
442 if (m_meter_thread == 0) {
443 g_atomic_int_set (&m_meter_exit, 0);
444 m_meter_thread = Glib::Thread::create (sigc::mem_fun(this, &AudioEngine::meter_thread),
445 500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
450 AudioEngine::meter_thread ()
453 Glib::usleep (10000); /* 1/100th sec interval */
454 if (g_atomic_int_get(&m_meter_exit)) {
457 IO::update_meters ();
462 AudioEngine::set_session (Session *s)
464 Glib::Mutex::Lock pl (_process_lock);
470 nframes_t blocksize = jack_get_buffer_size (_jack);
472 /* page in as much of the session process code as we
473 can before we really start running.
476 session->process (blocksize);
477 session->process (blocksize);
478 session->process (blocksize);
479 session->process (blocksize);
480 session->process (blocksize);
481 session->process (blocksize);
482 session->process (blocksize);
483 session->process (blocksize);
488 AudioEngine::remove_session ()
490 Glib::Mutex::Lock lm (_process_lock);
495 session_remove_pending = true;
496 session_removed.wait(_process_lock);
507 AudioEngine::port_registration_failure (const std::string& portname)
509 GET_PRIVATE_JACK_POINTER (_jack);
510 string full_portname = jack_client_name;
511 full_portname += ':';
512 full_portname += portname;
515 jack_port_t* p = jack_port_by_name (_priv_jack, full_portname.c_str());
519 reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
521 reason = _("No more JACK ports are available. You will need to stop Ardour and restart JACK with ports if you need this many tracks.");
524 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
528 AudioEngine::register_input_port (DataType type, const string& portname)
530 GET_PRIVATE_JACK_POINTER_RET (_jack, 0);
533 fatal << _("register input port called before engine was started") << endmsg;
540 jack_port_t *p = jack_port_register (_priv_jack, portname.c_str(), type.to_jack_type(), JackPortIsInput, 0);
546 if ((newport = new Port (p)) != 0) {
547 RCUWriter<Ports> writer (ports);
548 boost::shared_ptr<Ports> ps = writer.get_copy ();
549 ps->insert (ps->begin(), newport);
550 /* writer goes out of scope, forces update */
556 port_registration_failure (portname);
563 AudioEngine::register_output_port (DataType type, const string& portname)
565 GET_PRIVATE_JACK_POINTER_RET (_jack, 0);
569 fatal << _("register output port called before engine was started") << endmsg;
578 if ((p = jack_port_register (_priv_jack, portname.c_str(),
579 type.to_jack_type(), JackPortIsOutput, 0)) != 0) {
584 RCUWriter<Ports> writer (ports);
585 boost::shared_ptr<Ports> ps = writer.get_copy ();
587 newport = new Port (p);
588 ps->insert (ps->begin(), newport);
590 /* writer goes out of scope, forces update */
596 port_registration_failure (portname);
604 AudioEngine::unregister_port (Port *port)
606 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
609 /* probably happening when the engine has been halted by JACK,
610 in which case, there is nothing we can do here.
617 int ret = jack_port_unregister (_priv_jack, port->_port);
623 RCUWriter<Ports> writer (ports);
624 boost::shared_ptr<Ports> ps = writer.get_copy ();
626 for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
633 /* writer goes out of scope, forces update */
636 remove_connections_for (port);
647 AudioEngine::connect (const string& source, const string& destination)
649 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
651 string s = make_port_name_non_relative (source);
652 string d = make_port_name_non_relative (destination);
654 int ret = jack_connect (_priv_jack, s.c_str(), d.c_str());
657 pair<string,string> c (s, d);
658 port_connections.push_back (c);
659 } else if (ret == EEXIST) {
660 error << string_compose(_("AudioEngine: connection already exists: %1 (%2) to %3 (%4)"),
661 source, s, destination, d)
664 error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
665 source, s, destination, d)
673 AudioEngine::disconnect (const string& source, const string& destination)
675 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
679 fatal << _("disconnect called before engine was started") << endmsg;
686 string s = make_port_name_non_relative (source);
687 string d = make_port_name_non_relative (destination);
689 int ret = jack_disconnect (_priv_jack, s.c_str(), d.c_str());
692 pair<string,string> c (s, d);
693 PortConnections::iterator i;
695 if ((i = find (port_connections.begin(), port_connections.end(), c)) != port_connections.end()) {
696 port_connections.erase (i);
704 AudioEngine::disconnect (Port *port)
706 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
710 fatal << _("disconnect called before engine was started") << endmsg;
717 int ret = jack_port_disconnect (_priv_jack, port->_port);
720 remove_connections_for (port);
728 AudioEngine::frame_rate ()
730 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
731 if (_frame_rate == 0) {
732 return (_frame_rate = jack_get_sample_rate (_priv_jack));
739 AudioEngine::frames_per_cycle ()
741 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
742 if (_buffer_size == 0) {
743 return (_buffer_size = jack_get_buffer_size (_jack));
750 AudioEngine::get_port_by_name (const string& portname, bool keep)
752 Glib::Mutex::Lock lm (_process_lock);
753 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
757 fatal << _("get_port_by_name() called before engine was started") << endmsg;
764 /* check to see if we have a Port for this name already */
766 boost::shared_ptr<Ports> pr = ports.reader();
768 for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
769 if (portname == (*i)->name()) {
776 if ((p = jack_port_by_name (_priv_jack, portname.c_str())) != 0) {
777 Port *newport = new Port (p);
780 if (keep && newport->is_mine (_jack)) {
781 RCUWriter<Ports> writer (ports);
782 boost::shared_ptr<Ports> ps = writer.get_copy ();
783 ps->insert (newport);
784 /* writer goes out of scope, forces update */
797 AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
799 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
802 fatal << _("get_ports called before engine was started") << endmsg;
808 return jack_get_ports (_priv_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
812 AudioEngine::halted (void *arg)
814 /* called from jack shutdown handler */
816 AudioEngine* ae = static_cast<AudioEngine *> (arg);
817 bool was_running = ae->_running;
819 ae->stop_metering_thread ();
821 ae->_running = false;
822 ae->_buffer_size = 0;
827 ae->Halted(); /* EMIT SIGNAL */
834 /* called from a signal handler for SIGPIPE */
836 stop_metering_thread ();
845 AudioEngine::can_request_hardware_monitoring ()
847 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
850 if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
861 AudioEngine::n_physical_audio_outputs () const
863 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
867 if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical|JackPortIsInput)) == 0) {
871 for (i = 0; ports[i]; ++i);
878 AudioEngine::n_physical_audio_inputs () const
880 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
884 if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical|JackPortIsOutput)) == 0) {
889 for (i = 0; ports[i]; ++i);
896 AudioEngine::get_physical_audio_inputs (vector<string>& ins)
898 GET_PRIVATE_JACK_POINTER (_jack);
902 if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical|JackPortIsOutput)) == 0) {
907 for (i = 0; ports[i]; ++i) {
908 ins.push_back (ports[i]);
915 AudioEngine::get_physical_audio_outputs (vector<string>& outs)
917 GET_PRIVATE_JACK_POINTER (_jack);
921 if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical|JackPortIsInput)) == 0) {
926 for (i = 0; ports[i]; ++i) {
927 outs.push_back (ports[i]);
934 AudioEngine::get_nth_physical_audio (uint32_t n, int flag)
936 GET_PRIVATE_JACK_POINTER_RET (_jack,"");
941 ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical|flag);
943 for (i = 0; i < n && ports[i]; ++i);
949 free ((char *) ports);
955 AudioEngine::get_port_total_latency (const Port& port)
957 GET_PRIVATE_JACK_POINTER_RET (_jack,0);
959 return jack_port_get_total_latency (_priv_jack, port._port);
963 AudioEngine::transport_stop ()
965 GET_PRIVATE_JACK_POINTER (_jack);
966 jack_transport_stop (_priv_jack);
970 AudioEngine::transport_start ()
972 GET_PRIVATE_JACK_POINTER (_jack);
973 jack_transport_start (_priv_jack);
977 AudioEngine::transport_locate (nframes_t where)
979 GET_PRIVATE_JACK_POINTER (_jack);
980 // cerr << "tell JACK to locate to " << where << endl;
981 jack_transport_locate (_priv_jack, where);
984 AudioEngine::TransportState
985 AudioEngine::transport_state ()
987 GET_PRIVATE_JACK_POINTER_RET (_jack, ((TransportState) JackTransportStopped));
989 return (TransportState) jack_transport_query (_priv_jack, &pos);
993 AudioEngine::reset_timebase ()
995 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
996 if (Config->get_jack_time_master()) {
997 return jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
999 return jack_release_timebase (_jack);
1004 AudioEngine::freewheel (bool onoff)
1006 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1008 if (onoff != _freewheeling) {
1011 _freewheel_thread_registered = false;
1014 return jack_set_freewheel (_priv_jack, onoff);
1017 /* already doing what has been asked for */
1023 AudioEngine::remove_all_ports ()
1025 jack_client_t* _priv_jack = _jack;
1027 /* process lock MUST be held */
1031 boost::shared_ptr<Ports> p = ports.reader();
1033 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
1034 jack_port_unregister (_priv_jack, (*i)->_port);
1038 RCUWriter<Ports> writer (ports);
1039 boost::shared_ptr<Ports> ps = writer.get_copy ();
1043 port_connections.clear ();
1047 AudioEngine::remove_connections_for (Port* port)
1049 for (PortConnections::iterator i = port_connections.begin(); i != port_connections.end(); ) {
1050 PortConnections::iterator tmp;
1055 if ((*i).first == port->name()) {
1056 port_connections.erase (i);
1063 #ifdef HAVE_JACK_CLIENT_OPEN
1066 AudioEngine::connect_to_jack (string client_name)
1068 jack_options_t options = JackNullOption;
1069 jack_status_t status;
1070 const char *server_name = NULL;
1072 jack_client_name = client_name; /* might be reset below */
1073 _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
1075 if (_jack == NULL) {
1076 /* just return without an error message. something else will take care of it */
1080 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1082 if (status & JackNameNotUnique) {
1083 jack_client_name = jack_get_client_name (_priv_jack);
1086 jack_set_error_function (ardour_jack_error);
1094 AudioEngine::connect_to_jack (string client_name)
1096 jack_client_name = client_name;
1098 if ((_jack = jack_client_new (client_name.c_str())) == 0) {
1105 #endif /* HAVE_JACK_CLIENT_OPEN */
1108 AudioEngine::disconnect_from_jack ()
1110 GET_PRIVATE_JACK_POINTER_RET (_jack, 0);
1113 stop_metering_thread ();
1117 Glib::Mutex::Lock lm (_process_lock);
1118 jack_client_close (_priv_jack);
1127 Stopped(); /* EMIT SIGNAL */
1134 AudioEngine::reconnect_to_jack ()
1136 GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
1139 disconnect_from_jack ();
1140 /* XXX give jackd a chance */
1141 Glib::usleep (250000);
1144 if (connect_to_jack (jack_client_name)) {
1145 error << _("failed to connect to JACK") << endmsg;
1151 boost::shared_ptr<Ports> p = ports.reader ();
1153 for (i = p->begin(); i != p->end(); ++i) {
1155 /* XXX hack hack hack */
1157 string long_name = (*i)->name();
1160 short_name = long_name.substr (long_name.find_last_of (':') + 1);
1162 if (((*i)->_port = jack_port_register (_priv_jack, short_name.c_str(), (*i)->type(), (*i)->flags(), 0)) == 0) {
1163 error << string_compose (_("could not reregister %1"), (*i)->name()) << endmsg;
1170 if ((*i)->flags() & JackPortIsOutput) {
1171 (*i)->silence (jack_get_buffer_size (_priv_jack));
1175 if (i != p->end()) {
1177 for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
1178 jack_port_unregister (_priv_jack, (*i)->_port);
1185 session->reset_jack_connection (_priv_jack);
1186 nframes_t blocksize = jack_get_buffer_size (_priv_jack);
1187 Port::set_buffer_size (blocksize);
1188 session->set_block_size (blocksize);
1189 session->set_frame_rate (jack_get_sample_rate (_priv_jack));
1192 last_monitor_check = 0;
1194 jack_on_shutdown (_priv_jack, halted, this);
1195 jack_set_graph_order_callback (_priv_jack, _graph_order_callback, this);
1196 jack_set_thread_init_callback (_priv_jack, _thread_init_callback, this);
1197 jack_set_process_callback (_priv_jack, _process_callback, this);
1198 jack_set_sample_rate_callback (_priv_jack, _sample_rate_callback, this);
1199 jack_set_buffer_size_callback (_priv_jack, _bufsize_callback, this);
1200 jack_set_xrun_callback (_priv_jack, _xrun_callback, this);
1201 jack_set_sync_callback (_priv_jack, _jack_sync_callback, this);
1202 jack_set_freewheel_callback (_priv_jack, _freewheel_callback, this);
1204 if (Config->get_jack_time_master()) {
1205 jack_set_timebase_callback (_priv_jack, 0, _jack_timebase_callback, this);
1208 if (jack_activate (_priv_jack) == 0) {
1215 /* re-establish connections */
1217 for (PortConnections::iterator i = port_connections.begin(); i != port_connections.end(); ++i) {
1222 error << _("Disconnected from JACK while reconnecting. You should quit Ardour now.") << endmsg;
1226 if ((err = jack_connect (_priv_jack, (*i).first.c_str(), (*i).second.c_str())) != 0) {
1227 if (err != EEXIST) {
1228 error << string_compose (_("could not reconnect %1 and %2 (err = %3)"),
1229 (*i).first, (*i).second, err)
1235 Running (); /* EMIT SIGNAL*/
1237 start_metering_thread ();
1243 AudioEngine::request_buffer_size (nframes_t nframes)
1245 GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
1247 if (nframes == jack_get_buffer_size (_priv_jack)) {
1251 return jack_set_buffer_size (_priv_jack, nframes);
1255 AudioEngine::update_total_latencies ()
1257 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1258 GET_PRIVATE_JACK_POINTER (_jack);
1259 jack_recompute_total_latencies (_priv_jack);
1264 AudioEngine::make_port_name_relative (string portname)
1266 string::size_type len;
1267 string::size_type n;
1269 len = portname.length();
1271 for (n = 0; n < len; ++n) {
1272 if (portname[n] == ':') {
1277 if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
1278 return portname.substr (n+1);
1285 AudioEngine::make_port_name_non_relative (string portname)
1289 if (portname.find_first_of (':') != string::npos) {
1293 str = jack_client_name;
1301 AudioEngine::is_realtime () const
1303 GET_PRIVATE_JACK_POINTER_RET (_jack,false);
1304 return jack_is_realtime (_priv_jack);