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 <jack/jack.h>
29 #include <jack/thread.h>
31 #include "pbd/pthread_utils.h"
32 #include "pbd/stacktrace.h"
33 #include "pbd/unknown_type.h"
35 #include "midi++/jack.h"
37 #include "ardour/amp.h"
38 #include "ardour/audio_port.h"
39 #include "ardour/audioengine.h"
40 #include "ardour/buffer.h"
41 #include "ardour/buffer_set.h"
42 #include "ardour/cycle_timer.h"
43 #include "ardour/delivery.h"
44 #include "ardour/event_type_map.h"
45 #include "ardour/internal_return.h"
46 #include "ardour/io.h"
47 #include "ardour/meter.h"
48 #include "ardour/midi_port.h"
49 #include "ardour/process_thread.h"
50 #include "ardour/port.h"
51 #include "ardour/port_set.h"
52 #include "ardour/session.h"
53 #include "ardour/timestamps.h"
54 #include "ardour/utils.h"
59 using namespace ARDOUR
;
62 gint
AudioEngine::m_meter_exit
;
63 AudioEngine
* AudioEngine::_instance
= 0;
65 #define GET_PRIVATE_JACK_POINTER(j) jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return; }
66 #define GET_PRIVATE_JACK_POINTER_RET(j,r) jack_client_t* _priv_jack = (jack_client_t*) (j); if (!_priv_jack) { return r; }
68 AudioEngine::AudioEngine (string client_name
, string session_uuid
)
71 _instance
= this; /* singleton */
73 session_remove_pending
= false;
76 last_monitor_check
= 0;
77 monitor_check_interval
= max_frames
;
78 _processed_frames
= 0;
83 _freewheeling
= false;
87 g_atomic_int_set (&m_meter_exit
, 0);
89 if (connect_to_jack (client_name
, session_uuid
)) {
90 throw NoBackendAvailable ();
93 Port::set_engine (this);
95 // Initialize parameter metadata (e.g. ranges)
96 Evoral::Parameter
p(NullAutomation
);
97 p
= EventTypeMap::instance().new_parameter(NullAutomation
);
98 p
= EventTypeMap::instance().new_parameter(GainAutomation
);
99 p
= EventTypeMap::instance().new_parameter(PanAutomation
);
100 p
= EventTypeMap::instance().new_parameter(PluginAutomation
);
101 p
= EventTypeMap::instance().new_parameter(SoloAutomation
);
102 p
= EventTypeMap::instance().new_parameter(MuteAutomation
);
103 p
= EventTypeMap::instance().new_parameter(MidiCCAutomation
);
104 p
= EventTypeMap::instance().new_parameter(MidiPgmChangeAutomation
);
105 p
= EventTypeMap::instance().new_parameter(MidiPitchBenderAutomation
);
106 p
= EventTypeMap::instance().new_parameter(MidiChannelPressureAutomation
);
107 p
= EventTypeMap::instance().new_parameter(FadeInAutomation
);
108 p
= EventTypeMap::instance().new_parameter(FadeOutAutomation
);
109 p
= EventTypeMap::instance().new_parameter(EnvelopeAutomation
);
110 p
= EventTypeMap::instance().new_parameter(MidiCCAutomation
);
113 AudioEngine::~AudioEngine ()
116 Glib::Mutex::Lock
tm (_process_lock
);
117 session_removed
.signal ();
120 jack_client_close (_jack
);
124 stop_metering_thread ();
129 AudioEngine::jack() const
135 _thread_init_callback (void * /*arg*/)
137 /* make sure that anybody who needs to know about this thread
141 pthread_set_name (X_("audioengine"));
143 PBD::notify_gui_about_thread_creation ("gui", pthread_self(), X_("Audioengine"), 4096);
144 PBD::notify_gui_about_thread_creation ("midiui", pthread_self(), X_("Audioengine"), 128);
146 SessionEvent::create_per_thread_pool (X_("Audioengine"), 512);
148 MIDI::JACK_MidiPort::set_process_thread (pthread_self());
151 typedef void (*_JackInfoShutdownCallback
)(jack_status_t code
, const char* reason
, void *arg
);
153 static void (*on_info_shutdown
)(jack_client_t
*, _JackInfoShutdownCallback
, void *);
154 extern void jack_on_info_shutdown (jack_client_t
*, _JackInfoShutdownCallback
, void *) __attribute__((weak
));
156 static void check_jack_symbols () __attribute__((constructor
));
158 void check_jack_symbols ()
160 /* use weak linking to see if we really have various late-model JACK function */
161 on_info_shutdown
= jack_on_info_shutdown
;
165 ardour_jack_error (const char* msg
)
167 error
<< "JACK: " << msg
<< endmsg
;
171 AudioEngine::start ()
173 GET_PRIVATE_JACK_POINTER_RET (_jack
, -1);
177 nframes_t blocksize
= jack_get_buffer_size (_priv_jack
);
180 BootMessage (_("Connect session to engine"));
182 _session
->set_block_size (blocksize
);
183 _session
->set_frame_rate (jack_get_sample_rate (_priv_jack
));
185 /* page in as much of the session process code as we
186 can before we really start running.
189 _session
->process (blocksize
);
190 _session
->process (blocksize
);
191 _session
->process (blocksize
);
192 _session
->process (blocksize
);
193 _session
->process (blocksize
);
194 _session
->process (blocksize
);
195 _session
->process (blocksize
);
196 _session
->process (blocksize
);
199 _processed_frames
= 0;
200 last_monitor_check
= 0;
202 if (on_info_shutdown
) {
203 jack_on_info_shutdown (_priv_jack
, halted_info
, this);
205 jack_on_shutdown (_priv_jack
, halted
, this);
207 jack_set_graph_order_callback (_priv_jack
, _graph_order_callback
, this);
208 jack_set_thread_init_callback (_priv_jack
, _thread_init_callback
, this);
209 // jack_set_process_callback (_priv_jack, _process_callback, this);
210 jack_set_process_thread (_priv_jack
, _process_thread
, this);
211 jack_set_sample_rate_callback (_priv_jack
, _sample_rate_callback
, this);
212 jack_set_buffer_size_callback (_priv_jack
, _bufsize_callback
, this);
213 jack_set_xrun_callback (_priv_jack
, _xrun_callback
, this);
214 #ifdef HAVE_JACK_SESSION
215 if( jack_set_session_callback
)
216 jack_set_session_callback (_priv_jack
, _session_callback
, this);
218 jack_set_sync_callback (_priv_jack
, _jack_sync_callback
, this);
219 jack_set_freewheel_callback (_priv_jack
, _freewheel_callback
, this);
220 jack_set_port_registration_callback (_priv_jack
, _registration_callback
, this);
221 jack_set_port_connect_callback (_priv_jack
, _connect_callback
, this);
223 if (_session
&& _session
->config
.get_jack_time_master()) {
224 jack_set_timebase_callback (_priv_jack
, 0, _jack_timebase_callback
, this);
227 jack_set_error_function (ardour_jack_error
);
229 if (jack_activate (_priv_jack
) == 0) {
232 Running(); /* EMIT SIGNAL */
234 // error << _("cannot activate JACK client") << endmsg;
237 _raw_buffer_sizes
[DataType::AUDIO
] = blocksize
* sizeof(float);
239 jack_port_t
* midi_port
= jack_port_register (_priv_jack
, "m", JACK_DEFAULT_MIDI_TYPE
, JackPortIsOutput
, 0);
241 error
<< _("Cannot create temporary MIDI port to determine MIDI buffer size") << endmsg
;
243 _raw_buffer_sizes
[DataType::MIDI
] = jack_midi_max_event_size (jack_port_get_buffer(midi_port
, blocksize
));
244 cerr
<< "MIDI port buffers = " << _raw_buffer_sizes
[DataType::MIDI
] << endl
;
245 jack_port_unregister (_priv_jack
, midi_port
);
249 return _running
? 0 : -1;
253 AudioEngine::stop (bool forever
)
255 GET_PRIVATE_JACK_POINTER_RET (_jack
, -1);
259 disconnect_from_jack ();
261 jack_deactivate (_priv_jack
);
262 Stopped(); /* EMIT SIGNAL */
263 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
267 return _running
? -1 : 0;
272 AudioEngine::get_sync_offset (nframes_t
& offset
) const
275 #ifdef HAVE_JACK_VIDEO_SUPPORT
277 GET_PRIVATE_JACK_POINTER_RET (_jack
, false);
282 (void) jack_transport_query (_priv_jack
, &pos
);
284 if (pos
.valid
& JackVideoFrameOffset
) {
285 offset
= pos
.video_offset
;
298 AudioEngine::_jack_timebase_callback (jack_transport_state_t state
, nframes_t nframes
,
299 jack_position_t
* pos
, int new_position
, void *arg
)
301 static_cast<AudioEngine
*> (arg
)->jack_timebase_callback (state
, nframes
, pos
, new_position
);
305 AudioEngine::jack_timebase_callback (jack_transport_state_t state
, nframes_t nframes
,
306 jack_position_t
* pos
, int new_position
)
308 if (_jack
&& _session
&& _session
->synced_to_jack()) {
309 _session
->jack_timebase_callback (state
, nframes
, pos
, new_position
);
314 AudioEngine::_jack_sync_callback (jack_transport_state_t state
, jack_position_t
* pos
, void* arg
)
316 return static_cast<AudioEngine
*> (arg
)->jack_sync_callback (state
, pos
);
320 AudioEngine::jack_sync_callback (jack_transport_state_t state
, jack_position_t
* pos
)
322 if (_jack
&& _session
) {
323 return _session
->jack_sync_callback (state
, pos
);
330 AudioEngine::_xrun_callback (void *arg
)
332 AudioEngine
* ae
= static_cast<AudioEngine
*> (arg
);
333 if (ae
->connected()) {
334 ae
->Xrun (); /* EMIT SIGNAL */
339 #ifdef HAVE_JACK_SESSION
341 AudioEngine::_session_callback (jack_session_event_t
*event
, void *arg
)
343 printf( "helo.... " );
344 AudioEngine
* ae
= static_cast<AudioEngine
*> (arg
);
345 if (ae
->connected()) {
346 ae
->JackSessionEvent ( event
); /* EMIT SIGNAL */
351 AudioEngine::_graph_order_callback (void *arg
)
353 AudioEngine
* ae
= static_cast<AudioEngine
*> (arg
);
354 if (ae
->connected()) {
355 ae
->GraphReordered (); /* EMIT SIGNAL */
360 /** Wrapped which is called by JACK as its process callback. It is just
361 * here to get us back into C++ land by calling AudioEngine::process_callback()
362 * @param nframes Number of frames passed by JACK.
363 * @param arg User argument passed by JACK, which will be the AudioEngine*.
366 AudioEngine::_process_callback (nframes_t nframes
, void *arg
)
368 return static_cast<AudioEngine
*> (arg
)->process_callback (nframes
);
372 AudioEngine::_process_thread (void *arg
)
374 return static_cast<AudioEngine
*> (arg
)->process_thread ();
378 AudioEngine::_freewheel_callback (int onoff
, void *arg
)
380 static_cast<AudioEngine
*>(arg
)->_freewheeling
= onoff
;
384 AudioEngine::_registration_callback (jack_port_id_t
/*id*/, int /*reg*/, void* arg
)
386 AudioEngine
* ae
= static_cast<AudioEngine
*> (arg
);
387 ae
->PortRegisteredOrUnregistered (); /* EMIT SIGNAL */
391 AudioEngine::_connect_callback (jack_port_id_t
/*id_a*/, jack_port_id_t
/*id_b*/, int /*conn*/, void* arg
)
393 AudioEngine
* ae
= static_cast<AudioEngine
*> (arg
);
394 ae
->PortConnectedOrDisconnected (); /* EMIT SIGNAL */
398 AudioEngine::split_cycle (nframes_t offset
)
400 /* caller must hold process lock */
402 Port::increment_port_offset (offset
);
404 /* tell all Ports that we're going to start a new (split) cycle */
406 boost::shared_ptr
<Ports
> p
= ports
.reader();
408 for (Ports::iterator i
= p
->begin(); i
!= p
->end(); ++i
) {
409 (*i
)->cycle_split ();
414 AudioEngine::finish_process_cycle (int status
)
416 GET_PRIVATE_JACK_POINTER(_jack
);
417 jack_cycle_signal (_jack
, 0);
421 AudioEngine::process_thread ()
423 /* JACK doesn't do this for us when we use the wait API
426 _thread_init_callback (0);
428 _main_thread
= new ProcessThread
;
431 GET_PRIVATE_JACK_POINTER_RET(_jack
,0);
432 jack_nframes_t nframes
= jack_cycle_wait (_jack
);
434 if (process_callback (nframes
)) {
435 cerr
<< "--- process\n";
439 finish_process_cycle (0);
445 /** Method called by JACK (via _process_callback) which says that there
446 * is work to be done.
447 * @param nframes Number of frames to process.
450 AudioEngine::process_callback (nframes_t nframes
)
452 GET_PRIVATE_JACK_POINTER_RET(_jack
,0);
453 // CycleTimer ct ("AudioEngine::process");
454 Glib::Mutex::Lock
tm (_process_lock
, Glib::TRY_LOCK
);
456 /// The number of frames that will have been processed when we've finished
457 nframes_t next_processed_frames
;
459 /* handle wrap around of total frames counter */
461 if (max_frames
- _processed_frames
< nframes
) {
462 next_processed_frames
= nframes
- (max_frames
- _processed_frames
);
464 next_processed_frames
= _processed_frames
+ nframes
;
467 if (!tm
.locked() || _session
== 0) {
468 /* return having done nothing */
469 _processed_frames
= next_processed_frames
;
473 if (session_remove_pending
) {
474 /* perform the actual session removal */
476 session_remove_pending
= false;
477 session_removed
.signal();
478 _processed_frames
= next_processed_frames
;
482 /* tell all relevant objects that we're starting a new cycle */
484 Delivery::CycleStart (nframes
);
485 Port::set_port_offset (0);
486 InternalReturn::CycleStart (nframes
);
488 /* tell all Ports that we're starting a new cycle */
490 boost::shared_ptr
<Ports
> p
= ports
.reader();
492 for (Ports::iterator i
= p
->begin(); i
!= p
->end(); ++i
) {
493 (*i
)->cycle_start (nframes
);
497 /* emit the Freewheel signal and stop freewheeling in the event of trouble
499 boost::optional
<int> r
= Freewheel (nframes
);
500 if (r
.get_value_or (0)) {
501 jack_set_freewheel (_priv_jack
, false);
506 _session
->process (nframes
);
516 _processed_frames
= next_processed_frames
;
520 if (last_monitor_check
+ monitor_check_interval
< next_processed_frames
) {
522 boost::shared_ptr
<Ports
> p
= ports
.reader();
524 for (Ports::iterator i
= p
->begin(); i
!= p
->end(); ++i
) {
529 if (port
->_last_monitor
!= (x
= port
->monitoring_input ())) {
530 port
->_last_monitor
= x
;
531 /* XXX I think this is dangerous, due to
532 a likely mutex in the signal handlers ...
534 port
->MonitorInputChanged (x
); /* EMIT SIGNAL */
537 last_monitor_check
= next_processed_frames
;
540 if (_session
->silent()) {
542 boost::shared_ptr
<Ports
> p
= ports
.reader();
544 for (Ports::iterator i
= p
->begin(); i
!= p
->end(); ++i
) {
548 if (port
->sends_output()) {
549 port
->get_buffer(nframes
).silence(nframes
);
556 for (Ports::iterator i
= p
->begin(); i
!= p
->end(); ++i
) {
557 (*i
)->cycle_end (nframes
);
560 _processed_frames
= next_processed_frames
;
565 AudioEngine::_sample_rate_callback (nframes_t nframes
, void *arg
)
567 return static_cast<AudioEngine
*> (arg
)->jack_sample_rate_callback (nframes
);
571 AudioEngine::jack_sample_rate_callback (nframes_t nframes
)
573 _frame_rate
= nframes
;
574 _usecs_per_cycle
= (int) floor ((((double) frames_per_cycle() / nframes
)) * 1000000.0);
576 /* check for monitor input change every 1/10th of second */
578 monitor_check_interval
= nframes
/ 10;
579 last_monitor_check
= 0;
582 _session
->set_frame_rate (nframes
);
585 SampleRateChanged (nframes
); /* EMIT SIGNAL */
591 AudioEngine::_bufsize_callback (nframes_t nframes
, void *arg
)
593 return static_cast<AudioEngine
*> (arg
)->jack_bufsize_callback (nframes
);
597 AudioEngine::jack_bufsize_callback (nframes_t nframes
)
599 bool need_midi_size
= true;
600 bool need_audio_size
= true;
602 _buffer_size
= nframes
;
603 _usecs_per_cycle
= (int) floor ((((double) nframes
/ frame_rate())) * 1000000.0);
604 last_monitor_check
= 0;
606 boost::shared_ptr
<Ports
> p
= ports
.reader();
608 /* crude guesses, see below where we try to get the right answers.
610 Note that our guess for MIDI deliberatey tries to overestimate
611 by a little. It would be nicer if we could get the actual
612 size from a port, but we have to use this estimate in the
613 event that there are no MIDI ports currently. If there are
614 the value will be adjusted below.
617 _raw_buffer_sizes
[DataType::AUDIO
] = nframes
* sizeof (Sample
);
618 _raw_buffer_sizes
[DataType::MIDI
] = nframes
* 4 - (nframes
/2);
620 for (Ports::iterator i
= p
->begin(); i
!= p
->end(); ++i
) {
622 if (need_audio_size
&& (*i
)->type() == DataType::AUDIO
) {
623 _raw_buffer_sizes
[DataType::AUDIO
] = (*i
)->raw_buffer_size (nframes
);
624 need_audio_size
= false;
628 if (need_midi_size
&& (*i
)->type() == DataType::MIDI
) {
629 _raw_buffer_sizes
[DataType::MIDI
] = (*i
)->raw_buffer_size (nframes
);
630 need_midi_size
= false;
637 _session
->set_block_size (_buffer_size
);
644 AudioEngine::stop_metering_thread ()
646 if (m_meter_thread
) {
647 g_atomic_int_set (&m_meter_exit
, 1);
648 m_meter_thread
->join ();
654 AudioEngine::start_metering_thread ()
656 if (m_meter_thread
== 0) {
657 g_atomic_int_set (&m_meter_exit
, 0);
658 m_meter_thread
= Glib::Thread::create (boost::bind (&AudioEngine::meter_thread
, this),
659 500000, true, true, Glib::THREAD_PRIORITY_NORMAL
);
664 AudioEngine::meter_thread ()
666 pthread_set_name (X_("meter"));
669 Glib::usleep (10000); /* 1/100th sec interval */
670 if (g_atomic_int_get(&m_meter_exit
)) {
678 AudioEngine::set_session (Session
*s
)
680 Glib::Mutex::Lock
pl (_process_lock
);
682 SessionHandlePtr::set_session (s
);
686 start_metering_thread ();
688 nframes_t blocksize
= jack_get_buffer_size (_jack
);
690 /* page in as much of the session process code as we
691 can before we really start running.
694 boost::shared_ptr
<Ports
> p
= ports
.reader();
696 for (Ports::iterator i
= p
->begin(); i
!= p
->end(); ++i
) {
697 (*i
)->cycle_start (blocksize
);
700 _session
->process (blocksize
);
701 _session
->process (blocksize
);
702 _session
->process (blocksize
);
703 _session
->process (blocksize
);
704 _session
->process (blocksize
);
705 _session
->process (blocksize
);
706 _session
->process (blocksize
);
707 _session
->process (blocksize
);
709 for (Ports::iterator i
= p
->begin(); i
!= p
->end(); ++i
) {
710 (*i
)->cycle_end (blocksize
);
716 AudioEngine::remove_session ()
718 Glib::Mutex::Lock
lm (_process_lock
);
722 stop_metering_thread ();
725 session_remove_pending
= true;
726 session_removed
.wait(_process_lock
);
730 SessionHandlePtr::set_session (0);
737 AudioEngine::port_registration_failure (const std::string
& portname
)
739 GET_PRIVATE_JACK_POINTER (_jack
);
740 string full_portname
= jack_client_name
;
741 full_portname
+= ':';
742 full_portname
+= portname
;
745 jack_port_t
* p
= jack_port_by_name (_priv_jack
, full_portname
.c_str());
749 reason
= string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname
);
751 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
);
754 throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname
, reason
).c_str());
758 AudioEngine::register_port (DataType dtype
, const string
& portname
, bool input
)
763 if (dtype
== DataType::AUDIO
) {
764 newport
= new AudioPort (portname
, (input
? Port::IsInput
: Port::IsOutput
));
765 } else if (dtype
== DataType::MIDI
) {
766 newport
= new MidiPort (portname
, (input
? Port::IsInput
: Port::IsOutput
));
768 throw PortRegistrationFailure("unable to create port (unknown type)");
771 size_t& old_buffer_size
= _raw_buffer_sizes
[newport
->type()];
772 size_t port_buffer_size
= newport
->raw_buffer_size(0);
773 if (port_buffer_size
> old_buffer_size
) {
774 old_buffer_size
= port_buffer_size
;
777 RCUWriter
<Ports
> writer (ports
);
778 boost::shared_ptr
<Ports
> ps
= writer
.get_copy ();
779 ps
->insert (ps
->begin(), newport
);
781 /* writer goes out of scope, forces update */
786 catch (PortRegistrationFailure
& err
) {
788 } catch (std::exception
& e
) {
789 throw PortRegistrationFailure(string_compose(
790 _("unable to create port: %1"), e
.what()).c_str());
792 throw PortRegistrationFailure("unable to create port (unknown error)");
797 AudioEngine::register_input_port (DataType type
, const string
& portname
)
799 return register_port (type
, portname
, true);
803 AudioEngine::register_output_port (DataType type
, const string
& portname
)
805 return register_port (type
, portname
, false);
809 AudioEngine::unregister_port (Port
& port
)
811 /* caller must hold process lock */
814 /* probably happening when the engine has been halted by JACK,
815 in which case, there is nothing we can do here.
821 RCUWriter
<Ports
> writer (ports
);
822 boost::shared_ptr
<Ports
> ps
= writer
.get_copy ();
824 for (Ports::iterator i
= ps
->begin(); i
!= ps
->end(); ++i
) {
832 /* writer goes out of scope, forces update */
839 AudioEngine::connect (const string
& source
, const string
& destination
)
841 /* caller must hold process lock */
847 fatal
<< _("connect called before engine was started") << endmsg
;
854 string s
= make_port_name_non_relative (source
);
855 string d
= make_port_name_non_relative (destination
);
858 Port
* src
= get_port_by_name_locked (s
);
859 Port
* dst
= get_port_by_name_locked (d
);
862 ret
= src
->connect (d
);
864 ret
= dst
->connect (s
);
866 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
871 /* already exists - no error, no warning */
872 } else if (ret
< 0) {
873 error
<< string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
874 source
, s
, destination
, d
)
882 AudioEngine::disconnect (const string
& source
, const string
& destination
)
884 /* caller must hold process lock */
890 fatal
<< _("disconnect called before engine was started") << endmsg
;
897 string s
= make_port_name_non_relative (source
);
898 string d
= make_port_name_non_relative (destination
);
900 Port
* src
= get_port_by_name_locked (s
);
901 Port
* dst
= get_port_by_name_locked (d
);
904 ret
= src
->disconnect (d
);
906 ret
= dst
->disconnect (s
);
908 /* neither port is known to us, and this API isn't intended for use as a general patch bay */
915 AudioEngine::disconnect (Port
& port
)
917 GET_PRIVATE_JACK_POINTER_RET (_jack
,-1);
921 fatal
<< _("disconnect called before engine was started") << endmsg
;
928 return port
.disconnect_all ();
932 AudioEngine::frame_rate () const
934 GET_PRIVATE_JACK_POINTER_RET (_jack
,0);
935 if (_frame_rate
== 0) {
936 return (_frame_rate
= jack_get_sample_rate (_priv_jack
));
943 AudioEngine::raw_buffer_size (DataType t
)
945 std::map
<DataType
,size_t>::const_iterator s
= _raw_buffer_sizes
.find(t
);
946 return (s
!= _raw_buffer_sizes
.end()) ? s
->second
: 0;
950 AudioEngine::frames_per_cycle () const
952 GET_PRIVATE_JACK_POINTER_RET (_jack
,0);
953 if (_buffer_size
== 0) {
954 return (_buffer_size
= jack_get_buffer_size (_jack
));
960 /** @param name Full name of port (including prefix:)
961 * @return Corresponding Port*, or 0. This object remains the property of the AudioEngine
962 * so must not be deleted.
965 AudioEngine::get_port_by_name (const string
& portname
)
968 if (portname
.find_first_of (':') == string::npos
) {
969 s
= make_port_name_non_relative (portname
);
974 Glib::Mutex::Lock
lm (_process_lock
);
975 return get_port_by_name_locked (s
);
979 AudioEngine::get_port_by_name_locked (const string
& portname
)
981 /* caller must hold process lock */
985 fatal
<< _("get_port_by_name_locked() called before engine was started") << endmsg
;
992 if (portname
.substr (0, jack_client_name
.length ()) != jack_client_name
) {
993 /* not an ardour: port */
997 std::string
const rel
= make_port_name_relative (portname
);
999 boost::shared_ptr
<Ports
> pr
= ports
.reader();
1001 for (Ports::iterator i
= pr
->begin(); i
!= pr
->end(); ++i
) {
1002 if (rel
== (*i
)->name()) {
1011 AudioEngine::get_ports (const string
& port_name_pattern
, const string
& type_name_pattern
, uint32_t flags
)
1013 GET_PRIVATE_JACK_POINTER_RET (_jack
,0);
1016 fatal
<< _("get_ports called before engine was started") << endmsg
;
1022 return jack_get_ports (_priv_jack
, port_name_pattern
.c_str(), type_name_pattern
.c_str(), flags
);
1026 AudioEngine::halted_info (jack_status_t code
, const char* reason
, void *arg
)
1028 /* called from jack shutdown handler */
1030 AudioEngine
* ae
= static_cast<AudioEngine
*> (arg
);
1031 bool was_running
= ae
->_running
;
1033 ae
->stop_metering_thread ();
1035 ae
->_running
= false;
1036 ae
->_buffer_size
= 0;
1037 ae
->_frame_rate
= 0;
1041 #ifdef HAVE_JACK_ON_INFO_SHUTDOWN
1043 case JackBackendError
:
1044 ae
->Halted(reason
); /* EMIT SIGNAL */
1047 ae
->Halted(""); /* EMIT SIGNAL */
1050 ae
->Halted(""); /* EMIT SIGNAL */
1056 AudioEngine::halted (void *arg
)
1058 cerr
<< "HALTED by JACK\n";
1060 /* called from jack shutdown handler */
1062 AudioEngine
* ae
= static_cast<AudioEngine
*> (arg
);
1063 bool was_running
= ae
->_running
;
1065 ae
->stop_metering_thread ();
1067 ae
->_running
= false;
1068 ae
->_buffer_size
= 0;
1069 ae
->_frame_rate
= 0;
1073 ae
->Halted(""); /* EMIT SIGNAL */
1074 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
1079 AudioEngine::died ()
1081 /* called from a signal handler for SIGPIPE */
1083 stop_metering_thread ();
1092 AudioEngine::can_request_hardware_monitoring ()
1094 GET_PRIVATE_JACK_POINTER_RET (_jack
,false);
1095 const char ** ports
;
1097 if ((ports
= jack_get_ports (_priv_jack
, NULL
, JACK_DEFAULT_AUDIO_TYPE
, JackPortCanMonitor
)) == 0) {
1108 AudioEngine::n_physical_outputs (DataType type
) const
1110 GET_PRIVATE_JACK_POINTER_RET (_jack
,0);
1111 const char ** ports
;
1114 if ((ports
= jack_get_ports (_priv_jack
, NULL
, type
.to_jack_type(), JackPortIsPhysical
|JackPortIsInput
)) == 0) {
1118 for (uint32_t i
= 0; ports
[i
]; ++i
) {
1119 if (!strstr (ports
[i
], "Midi-Through")) {
1130 AudioEngine::n_physical_inputs (DataType type
) const
1132 GET_PRIVATE_JACK_POINTER_RET (_jack
,0);
1133 const char ** ports
;
1136 if ((ports
= jack_get_ports (_priv_jack
, NULL
, type
.to_jack_type(), JackPortIsPhysical
|JackPortIsOutput
)) == 0) {
1140 for (uint32_t i
= 0; ports
[i
]; ++i
) {
1141 if (!strstr (ports
[i
], "Midi-Through")) {
1152 AudioEngine::get_physical_inputs (DataType type
, vector
<string
>& ins
)
1154 GET_PRIVATE_JACK_POINTER (_jack
);
1155 const char ** ports
;
1157 if ((ports
= jack_get_ports (_priv_jack
, NULL
, type
.to_jack_type(), JackPortIsPhysical
|JackPortIsOutput
)) == 0) {
1162 for (uint32_t i
= 0; ports
[i
]; ++i
) {
1163 if (strstr (ports
[i
], "Midi-Through")) {
1166 ins
.push_back (ports
[i
]);
1173 AudioEngine::get_physical_outputs (DataType type
, vector
<string
>& outs
)
1175 GET_PRIVATE_JACK_POINTER (_jack
);
1176 const char ** ports
;
1179 if ((ports
= jack_get_ports (_priv_jack
, NULL
, type
.to_jack_type(), JackPortIsPhysical
|JackPortIsInput
)) == 0) {
1183 for (i
= 0; ports
[i
]; ++i
) {
1184 if (strstr (ports
[i
], "Midi-Through")) {
1187 outs
.push_back (ports
[i
]);
1193 AudioEngine::get_nth_physical (DataType type
, uint32_t n
, int flag
)
1195 GET_PRIVATE_JACK_POINTER_RET (_jack
,"");
1196 const char ** ports
;
1201 assert(type
!= DataType::NIL
);
1203 if ((ports
= jack_get_ports (_priv_jack
, NULL
, type
.to_jack_type(), JackPortIsPhysical
|flag
)) == 0) {
1207 for (i
= 0, idx
= 0; idx
< n
&& ports
[i
]; ++i
) {
1208 if (!strstr (ports
[i
], "Midi-Through")) {
1217 free ((const char **) ports
);
1223 AudioEngine::update_total_latency (const Port
& port
)
1225 port
.recompute_total_latency ();
1229 AudioEngine::transport_stop ()
1231 GET_PRIVATE_JACK_POINTER (_jack
);
1232 jack_transport_stop (_priv_jack
);
1236 AudioEngine::transport_start ()
1238 GET_PRIVATE_JACK_POINTER (_jack
);
1239 jack_transport_start (_priv_jack
);
1243 AudioEngine::transport_locate (nframes_t where
)
1245 GET_PRIVATE_JACK_POINTER (_jack
);
1246 // cerr << "tell JACK to locate to " << where << endl;
1247 jack_transport_locate (_priv_jack
, where
);
1250 AudioEngine::TransportState
1251 AudioEngine::transport_state ()
1253 GET_PRIVATE_JACK_POINTER_RET (_jack
, ((TransportState
) JackTransportStopped
));
1254 jack_position_t pos
;
1255 return (TransportState
) jack_transport_query (_priv_jack
, &pos
);
1259 AudioEngine::reset_timebase ()
1261 GET_PRIVATE_JACK_POINTER_RET (_jack
, -1);
1263 if (_session
->config
.get_jack_time_master()) {
1264 return jack_set_timebase_callback (_priv_jack
, 0, _jack_timebase_callback
, this);
1266 return jack_release_timebase (_jack
);
1273 AudioEngine::freewheel (bool onoff
)
1275 GET_PRIVATE_JACK_POINTER_RET (_jack
, -1);
1277 if (onoff
!= _freewheeling
) {
1278 return jack_set_freewheel (_priv_jack
, onoff
);
1281 /* already doing what has been asked for */
1287 AudioEngine::remove_all_ports ()
1289 /* process lock MUST be held */
1292 RCUWriter
<Ports
> writer (ports
);
1293 boost::shared_ptr
<Ports
> ps
= writer
.get_copy ();
1295 for (Ports::iterator i
= ps
->begin(); i
!= ps
->end(); ++i
) {
1302 /* clear dead wood list too */
1308 AudioEngine::connect_to_jack (string client_name
, string session_uuid
)
1310 jack_options_t options
= JackNullOption
;
1311 jack_status_t status
;
1312 const char *server_name
= NULL
;
1314 jack_client_name
= client_name
; /* might be reset below */
1315 #ifdef HAVE_JACK_SESSION
1316 if (! session_uuid
.empty())
1317 _jack
= jack_client_open (jack_client_name
.c_str(), JackSessionID
, &status
, session_uuid
.c_str());
1320 _jack
= jack_client_open (jack_client_name
.c_str(), options
, &status
, server_name
);
1322 if (_jack
== NULL
) {
1323 // error message is not useful here
1327 GET_PRIVATE_JACK_POINTER_RET (_jack
, -1);
1329 if (status
& JackNameNotUnique
) {
1330 jack_client_name
= jack_get_client_name (_priv_jack
);
1337 AudioEngine::disconnect_from_jack ()
1339 GET_PRIVATE_JACK_POINTER_RET (_jack
, 0);
1342 stop_metering_thread ();
1346 Glib::Mutex::Lock
lm (_process_lock
);
1347 jack_client_close (_priv_jack
);
1353 _raw_buffer_sizes
.clear();
1357 Stopped(); /* EMIT SIGNAL */
1358 MIDI::JACK_MidiPort::JackHalted (); /* EMIT SIGNAL */
1365 AudioEngine::reconnect_to_jack ()
1368 disconnect_from_jack ();
1369 /* XXX give jackd a chance */
1370 Glib::usleep (250000);
1373 if (connect_to_jack (jack_client_name
, "")) {
1374 error
<< _("failed to connect to JACK") << endmsg
;
1380 boost::shared_ptr
<Ports
> p
= ports
.reader ();
1382 for (i
= p
->begin(); i
!= p
->end(); ++i
) {
1383 if ((*i
)->reestablish ()) {
1388 if (i
!= p
->end()) {
1390 remove_all_ports ();
1394 GET_PRIVATE_JACK_POINTER_RET (_jack
,-1);
1397 _session
->reset_jack_connection (_priv_jack
);
1398 jack_bufsize_callback (jack_get_buffer_size (_priv_jack
));
1399 _session
->set_frame_rate (jack_get_sample_rate (_priv_jack
));
1402 last_monitor_check
= 0;
1404 jack_on_shutdown (_priv_jack
, halted
, this);
1405 jack_set_graph_order_callback (_priv_jack
, _graph_order_callback
, this);
1406 jack_set_thread_init_callback (_priv_jack
, _thread_init_callback
, this);
1407 // jack_set_process_callback (_priv_jack, _process_callback, this);
1408 jack_set_process_thread (_priv_jack
, _process_thread
, this);
1409 jack_set_sample_rate_callback (_priv_jack
, _sample_rate_callback
, this);
1410 jack_set_buffer_size_callback (_priv_jack
, _bufsize_callback
, this);
1411 jack_set_xrun_callback (_priv_jack
, _xrun_callback
, this);
1412 #ifdef HAVE_JACK_SESSION
1413 if( jack_set_session_callback
)
1414 jack_set_session_callback (_priv_jack
, _session_callback
, this);
1416 jack_set_sync_callback (_priv_jack
, _jack_sync_callback
, this);
1417 jack_set_freewheel_callback (_priv_jack
, _freewheel_callback
, this);
1419 if (_session
&& _session
->config
.get_jack_time_master()) {
1420 jack_set_timebase_callback (_priv_jack
, 0, _jack_timebase_callback
, this);
1423 if (jack_activate (_priv_jack
) == 0) {
1430 /* re-establish connections */
1432 for (i
= p
->begin(); i
!= p
->end(); ++i
) {
1436 Running (); /* EMIT SIGNAL*/
1438 start_metering_thread ();
1444 AudioEngine::request_buffer_size (nframes_t nframes
)
1446 GET_PRIVATE_JACK_POINTER_RET (_jack
, -1);
1448 if (nframes
== jack_get_buffer_size (_priv_jack
)) {
1452 return jack_set_buffer_size (_priv_jack
, nframes
);
1456 AudioEngine::update_total_latencies ()
1458 #ifdef HAVE_JACK_RECOMPUTE_LATENCIES
1459 GET_PRIVATE_JACK_POINTER (_jack
);
1460 jack_recompute_total_latencies (_priv_jack
);
1465 AudioEngine::make_port_name_relative (string portname
)
1467 string::size_type len
;
1468 string::size_type n
;
1470 len
= portname
.length();
1472 for (n
= 0; n
< len
; ++n
) {
1473 if (portname
[n
] == ':') {
1478 if ((n
!= len
) && (portname
.substr (0, n
) == jack_client_name
)) {
1479 return portname
.substr (n
+1);
1486 AudioEngine::make_port_name_non_relative (string portname
)
1490 if (portname
.find_first_of (':') != string::npos
) {
1494 str
= jack_client_name
;
1502 AudioEngine::is_realtime () const
1504 GET_PRIVATE_JACK_POINTER_RET (_jack
,false);
1505 return jack_is_realtime (_priv_jack
);
1509 AudioEngine::create_process_thread (boost::function
<void()> f
, size_t stacksize
)
1511 GET_PRIVATE_JACK_POINTER_RET (_jack
, 0);
1513 ThreadData
* td
= new ThreadData (this, f
, stacksize
);
1515 if (jack_client_create_thread (_priv_jack
, &thread
, jack_client_real_time_priority (_priv_jack
),
1516 jack_is_realtime (_priv_jack
), _start_process_thread
, td
)) {
1524 AudioEngine::_start_process_thread (void* arg
)
1526 ThreadData
* td
= reinterpret_cast<ThreadData
*> (arg
);
1527 boost::function
<void()> f
= td
->f
;