#include <sstream>
#include <glibmm/timer.h>
+#include <glibmm/pattern.h>
+#include <glibmm/module.h>
+#include "pbd/epa.h"
+#include "pbd/file_utils.h"
#include "pbd/pthread_utils.h"
#include "pbd/stacktrace.h"
#include "pbd/unknown_type.h"
-#include "pbd/epa.h"
#include <jack/weakjack.h>
#include "midi++/port.h"
-#include "midi++/jack_midi_port.h"
#include "midi++/mmc.h"
-#include "midi++/manager.h"
+#include "ardour/async_midi_port.h"
#include "ardour/audio_port.h"
+#include "ardour/audio_backend.h"
#include "ardour/audioengine.h"
+#include "ardour/backend_search_path.h"
#include "ardour/buffer.h"
#include "ardour/cycle_timer.h"
#include "ardour/internal_send.h"
#include "ardour/meter.h"
#include "ardour/midi_port.h"
+#include "ardour/midiport_manager.h"
+#include "ardour/mtdm.h"
#include "ardour/port.h"
#include "ardour/process_thread.h"
#include "ardour/session.h"
AudioEngine::AudioEngine ()
: session_remove_pending (false)
, session_removal_countdown (-1)
+ , _running (false)
+ , _freewheeling (false)
, monitor_check_interval (INT32_MAX)
, last_monitor_check (0)
, _processed_frames (0)
- , _freewheeling (false)
- , _pre_freewheel_mmc_enabled (false)
- , _usecs_per_cycle (0)
- , port_remove_in_progress (false)
, m_meter_thread (0)
, _main_thread (0)
+ , _mtdm (0)
+ , _measuring_latency (false)
+ , _latency_input_port (0)
+ , _latency_output_port (0)
+ , _latency_flush_frames (0)
+ , _latency_signal_latency (0)
+ , _stopped_for_latency (false)
+ , _in_destructor (false)
{
g_atomic_int_set (&m_meter_exit, 0);
-
- Port::set_engine (this);
+ discover_backends ();
}
AudioEngine::~AudioEngine ()
{
- config_connection.disconnect ();
-
- {
- Glib::Threads::Mutex::Lock tm (_process_lock);
- session_removed.signal ();
- stop_metering_thread ();
- }
+ _in_destructor = true;
+ stop_metering_thread ();
+ drop_backend ();
}
AudioEngine*
if (_instance) {
return _instance;
}
- return new AudioEngine;
-}
-jack_client_t*
-AudioEngine::jack() const
-{
- return _jack;
+ _instance = new AudioEngine ();
+
+ return _instance;
}
void
SessionEvent::create_per_thread_pool (X_("Audioengine"), 512);
- MIDI::JackMIDIPort::set_process_thread (pthread_self());
+ AsyncMIDIPort::set_process_thread (pthread_self());
}
-
-int
-AudioEngine::start ()
+void
+AudioEngine::split_cycle (pframes_t offset)
{
- GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
-
- if (!_running) {
-
- if (!jack_port_type_get_buffer_size) {
- warning << _("This version of JACK is old - you should upgrade to a newer version that supports jack_port_type_get_buffer_size()") << endmsg;
- }
-
- if (_session) {
- BootMessage (_("Connect session to engine"));
- _session->set_frame_rate (jack_get_sample_rate (_priv_jack));
- }
+ /* caller must hold process lock */
- /* a proxy for whether jack_activate() will definitely call the buffer size
- * callback. with older versions of JACK, this function symbol will be null.
- * this is reliable, but not clean.
- */
+ Port::increment_global_port_buffer_offset (offset);
- if (!jack_port_type_get_buffer_size) {
- jack_bufsize_callback (jack_get_buffer_size (_priv_jack));
- }
-
- _processed_frames = 0;
- last_monitor_check = 0;
+ /* tell all Ports that we're going to start a new (split) cycle */
- set_jack_callbacks ();
+ boost::shared_ptr<Ports> p = ports.reader();
- if (jack_activate (_priv_jack) == 0) {
- _running = true;
- _has_run = true;
- Running(); /* EMIT SIGNAL */
- } else {
- // error << _("cannot activate JACK client") << endmsg;
- }
+ for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
+ i->second->cycle_split ();
}
-
- return _running ? 0 : -1;
}
int
-AudioEngine::stop (bool forever)
+AudioEngine::sample_rate_change (pframes_t nframes)
{
- GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
+ /* check for monitor input change every 1/10th of second */
- if (_priv_jack) {
- if (forever) {
- disconnect_from_jack ();
- } else {
- jack_deactivate (_priv_jack);
- MIDI::JackMIDIPort::JackHalted (); /* EMIT SIGNAL */
- Stopped(); /* EMIT SIGNAL */
- }
+ monitor_check_interval = nframes / 10;
+ last_monitor_check = 0;
+
+ if (_session) {
+ _session->set_frame_rate (nframes);
}
- if (forever) {
- stop_metering_thread ();
- }
+ SampleRateChanged (nframes); /* EMIT SIGNAL */
- return _running ? -1 : 0;
+ return 0;
}
-
-void
-AudioEngine::split_cycle (pframes_t offset)
+int
+AudioEngine::buffer_size_change (pframes_t bufsiz)
{
- /* caller must hold process lock */
-
- Port::increment_global_port_buffer_offset (offset);
-
- /* tell all Ports that we're going to start a new (split) cycle */
-
- boost::shared_ptr<Ports> p = ports.reader();
-
- for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
- i->second->cycle_split ();
+ if (_session) {
+ _session->set_block_size (bufsiz);
+ last_monitor_check = 0;
}
-}
+ return 0;
+}
/** Method called by our ::process_thread when there is work to be done.
* @param nframes Number of frames to process.
int
AudioEngine::process_callback (pframes_t nframes)
{
- GET_PRIVATE_JACK_POINTER_RET(_jack,0);
Glib::Threads::Mutex::Lock tm (_process_lock, Glib::Threads::TRY_LOCK);
PT_TIMING_REF;
return 0;
}
+ bool return_after_remove_check = false;
+
+ if (_measuring_latency && _mtdm) {
+ /* run a normal cycle from the perspective of the PortManager
+ so that we get silence on all registered ports.
+
+ we overwrite the silence on the two ports used for latency
+ measurement.
+ */
+
+ PortManager::cycle_start (nframes);
+ PortManager::silence (nframes);
+
+ if (_latency_input_port && _latency_output_port) {
+ PortEngine& pe (port_engine());
+
+ Sample* in = (Sample*) pe.get_buffer (_latency_input_port, nframes);
+ Sample* out = (Sample*) pe.get_buffer (_latency_output_port, nframes);
+
+ _mtdm->process (nframes, in, out);
+ }
+
+ PortManager::cycle_end (nframes);
+ return_after_remove_check = true;
+
+ } else if (_latency_flush_frames) {
+
+ /* wait for the appropriate duration for the MTDM signal to
+ * drain from the ports before we revert to normal behaviour.
+ */
+
+ PortManager::cycle_start (nframes);
+ PortManager::silence (nframes);
+ PortManager::cycle_end (nframes);
+
+ if (_latency_flush_frames > nframes) {
+ _latency_flush_frames -= nframes;
+ } else {
+ _latency_flush_frames = 0;
+ }
+
+ return_after_remove_check = true;
+ }
+
if (session_remove_pending) {
/* perform the actual session removal */
if (session_removal_countdown < 0) {
/* fade out over 1 second */
- session_removal_countdown = _frame_rate/2;
+ session_removal_countdown = sample_rate()/2;
session_removal_gain = 1.0;
session_removal_gain_step = 1.0/session_removal_countdown;
}
}
+ if (return_after_remove_check) {
+ return 0;
+ }
+
if (_session == 0) {
if (!_freewheeling) {
- MIDI::Manager::instance()->cycle_start(nframes);
- MIDI::Manager::instance()->cycle_end();
+ PortManager::cycle_start (nframes);
+ PortManager::cycle_end (nframes);
}
_processed_frames = next_processed_frames;
/* tell all relevant objects that we're starting a new cycle */
InternalSend::CycleStart (nframes);
- Port::set_global_port_buffer_offset (0);
- Port::set_cycle_framecnt (nframes);
/* tell all Ports that we're starting a new cycle */
- boost::shared_ptr<Ports> p = ports.reader();
-
- for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
- i->second->cycle_start (nframes);
- }
+ PortManager::cycle_start (nframes);
/* test if we are freewheeling and there are freewheel signals connected.
ardour should act normally even when freewheeling unless /it/ is
- exporting
+ exporting (which is what Freewheel.empty() tests for).
*/
if (_freewheeling && !Freewheel.empty()) {
-
Freewheel (nframes);
-
} else {
- MIDI::Manager::instance()->cycle_start(nframes);
-
if (_session) {
_session->process (nframes);
}
-
- MIDI::Manager::instance()->cycle_end();
}
if (_freewheeling) {
}
if (last_monitor_check + monitor_check_interval < next_processed_frames) {
-
- boost::shared_ptr<Ports> p = ports.reader();
-
- for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
-
- bool x;
-
- if (i->second->last_monitor() != (x = i->second->monitoring_input ())) {
- i->second->set_last_monitor (x);
- /* XXX I think this is dangerous, due to
- a likely mutex in the signal handlers ...
- */
- i->second->MonitorInputChanged (x); /* EMIT SIGNAL */
- }
- }
+
+ PortManager::check_monitoring ();
last_monitor_check = next_processed_frames;
}
if (_session->silent()) {
-
- for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
-
- if (i->second->sends_output()) {
- i->second->get_buffer(nframes).silence(nframes);
- }
- }
+ PortManager::silence (nframes);
}
if (session_remove_pending && session_removal_countdown) {
- for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
-
- if (i->second->sends_output()) {
-
- boost::shared_ptr<AudioPort> ap = boost::dynamic_pointer_cast<AudioPort> (i->second);
- if (ap) {
- Sample* s = ap->engine_get_whole_audio_buffer ();
- gain_t g = session_removal_gain;
-
- for (pframes_t n = 0; n < nframes; ++n) {
- *s++ *= g;
- g -= session_removal_gain_step;
- }
- }
- }
- }
+ PortManager::fade_out (session_removal_gain, session_removal_gain_step, nframes);
if (session_removal_countdown > nframes) {
session_removal_countdown -= nframes;
session_removal_gain -= (nframes * session_removal_gain_step);
}
- // Finalize ports
-
- for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
- i->second->cycle_end (nframes);
- }
+ PortManager::cycle_end (nframes);
_processed_frames = next_processed_frames;
if (_session) {
- start_metering_thread ();
-
- pframes_t blocksize = jack_get_buffer_size (_jack);
-
- /* page in as much of the session process code as we
- can before we really start running.
- */
-
- boost::shared_ptr<Ports> p = ports.reader();
+ pframes_t blocksize = samples_per_cycle ();
- for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
- i->second->cycle_start (blocksize);
- }
+ PortManager::cycle_start (blocksize);
_session->process (blocksize);
_session->process (blocksize);
_session->process (blocksize);
_session->process (blocksize);
- for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
- i->second->cycle_end (blocksize);
- }
+ PortManager::cycle_end (blocksize);
}
}
if (_running) {
- stop_metering_thread ();
-
if (_session) {
session_remove_pending = true;
+ session_removal_countdown = 0;
session_removed.wait(_process_lock);
}
remove_all_ports ();
}
+
void
-AudioEngine::port_registration_failure (const std::string& portname)
+AudioEngine::died ()
{
- GET_PRIVATE_JACK_POINTER (_jack);
- string full_portname = jack_client_name;
- full_portname += ':';
- full_portname += portname;
+ /* called from a signal handler for SIGPIPE */
+ stop_metering_thread ();
- jack_port_t* p = jack_port_by_name (_priv_jack, full_portname.c_str());
- string reason;
+ _running = false;
+}
- if (p) {
- reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
- } else {
- reason = string_compose (_("No more JACK ports are available. You will need to stop %1 and restart JACK with more ports if you need this many tracks."), PROGRAM_NAME);
+int
+AudioEngine::reset_timebase ()
+{
+ if (_session) {
+ if (_session->config.get_jack_time_master()) {
+ _backend->set_time_master (true);
+ } else {
+ _backend->set_time_master (false);
+ }
}
+ return 0;
+}
- throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
+
+void
+AudioEngine::destroy ()
+{
+ delete _instance;
+ _instance = 0;
}
-boost::shared_ptr<Port>
-AudioEngine::register_port (DataType dtype, const string& portname, bool input)
+int
+AudioEngine::discover_backends ()
{
- boost::shared_ptr<Port> newport;
+ vector<std::string> backend_modules;
- try {
- if (dtype == DataType::AUDIO) {
- newport.reset (new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput)));
- } else if (dtype == DataType::MIDI) {
- newport.reset (new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput)));
- } else {
- throw PortRegistrationFailure("unable to create port (unknown type)");
- }
+ _backends.clear ();
- RCUWriter<Ports> writer (ports);
- boost::shared_ptr<Ports> ps = writer.get_copy ();
- ps->insert (make_pair (make_port_name_relative (portname), newport));
+ Glib::PatternSpec so_extension_pattern("*backend.so");
+ Glib::PatternSpec dylib_extension_pattern("*backend.dylib");
+ Glib::PatternSpec dll_extension_pattern("*backend.dll");
- /* writer goes out of scope, forces update */
+ find_matching_files_in_search_path (backend_search_path (),
+ so_extension_pattern, backend_modules);
- return newport;
+ find_matching_files_in_search_path (backend_search_path (),
+ dylib_extension_pattern, backend_modules);
+
+ find_matching_files_in_search_path (backend_search_path (),
+ dll_extension_pattern, backend_modules);
+
+ DEBUG_TRACE (DEBUG::Panning, string_compose (_("looking for backends in %1\n"), backend_search_path().to_string()));
+
+ for (vector<std::string>::iterator i = backend_modules.begin(); i != backend_modules.end(); ++i) {
+
+ AudioBackendInfo* info;
+
+ if ((info = backend_discover (*i)) != 0) {
+ _backends.insert (make_pair (info->name, info));
+ }
}
- catch (PortRegistrationFailure& err) {
- throw err;
- } catch (std::exception& e) {
- throw PortRegistrationFailure(string_compose(
- _("unable to create port: %1"), e.what()).c_str());
- } catch (...) {
- throw PortRegistrationFailure("unable to create port (unknown error)");
+ return _backends.size();
+}
+
+AudioBackendInfo*
+AudioEngine::backend_discover (const string& path)
+{
+ Glib::Module module (path);
+ AudioBackendInfo* info;
+ AudioBackendInfo* (*dfunc)(void);
+ void* func = 0;
+
+ if (!module) {
+ error << string_compose(_("AudioEngine: cannot load module \"%1\" (%2)"), path,
+ Glib::Module::get_last_error()) << endmsg;
+ return 0;
+ }
+
+ if (!module.get_symbol ("descriptor", func)) {
+ error << string_compose(_("AudioEngine: backend at \"%1\" has no descriptor function."), path) << endmsg;
+ error << Glib::Module::get_last_error() << endmsg;
+ return 0;
}
+
+ module.make_resident ();
+
+ dfunc = (AudioBackendInfo* (*)(void))func;
+ info = dfunc();
+
+ return info;
}
-boost::shared_ptr<Port>
-AudioEngine::register_input_port (DataType type, const string& portname)
+vector<const AudioBackendInfo*>
+AudioEngine::available_backends() const
{
- return register_port (type, portname, true);
+ vector<const AudioBackendInfo*> r;
+
+ for (BackendMap::const_iterator i = _backends.begin(); i != _backends.end(); ++i) {
+ r.push_back (i->second);
+ }
+
+ return r;
}
-boost::shared_ptr<Port>
-AudioEngine::register_output_port (DataType type, const string& portname)
+string
+AudioEngine::current_backend_name() const
{
- return register_port (type, portname, false);
+ if (_backend) {
+ return _backend->name();
+ }
+ return string();
}
-int
-AudioEngine::unregister_port (boost::shared_ptr<Port> port)
+void
+AudioEngine::drop_backend ()
{
- /* caller must hold process lock */
+ if (_backend) {
+ _backend->stop ();
+ _backend.reset ();
+ }
+}
- if (!_running) {
- /* probably happening when the engine has been halted by JACK,
- in which case, there is nothing we can do here.
- */
- return 0;
+boost::shared_ptr<AudioBackend>
+AudioEngine::set_default_backend ()
+{
+ if (_backends.empty()) {
+ return boost::shared_ptr<AudioBackend>();
}
- {
- RCUWriter<Ports> writer (ports);
- boost::shared_ptr<Ports> ps = writer.get_copy ();
- Ports::iterator x = ps->find (make_port_name_relative (port->name()));
+ return set_backend (_backends.begin()->first, "", "");
+}
- if (x != ps->end()) {
- ps->erase (x);
- }
+boost::shared_ptr<AudioBackend>
+AudioEngine::set_backend (const std::string& name, const std::string& arg1, const std::string& arg2)
+{
+ BackendMap::iterator b = _backends.find (name);
- /* writer goes out of scope, forces update */
+ if (b == _backends.end()) {
+ return boost::shared_ptr<AudioBackend>();
}
- ports.flush ();
+ drop_backend ();
+
+ try {
+ if (b->second->instantiate (arg1, arg2)) {
+ throw failed_constructor ();
+ }
+
+ _backend = b->second->factory (*this);
- return 0;
+ } catch (exception& e) {
+ error << string_compose (_("Could not create backend for %1: %2"), name, e.what()) << endmsg;
+ return boost::shared_ptr<AudioBackend>();
+ }
+
+ return _backend;
}
+/* BACKEND PROXY WRAPPERS */
+
int
-AudioEngine::connect (const string& source, const string& destination)
+AudioEngine::start (bool for_latency)
{
- int ret;
-
- if (!_running) {
- if (!_has_run) {
- fatal << _("connect called before engine was started") << endmsg;
- /*NOTREACHED*/
- } else {
- return -1;
- }
+ if (!_backend) {
+ return -1;
}
- string s = make_port_name_non_relative (source);
- string d = make_port_name_non_relative (destination);
+ if (_running) {
+ return 0;
+ }
+ _processed_frames = 0;
+ last_monitor_check = 0;
+
+ if (_backend->start (for_latency)) {
+ return -1;
+ }
- boost::shared_ptr<Port> src = get_port_by_name (s);
- boost::shared_ptr<Port> dst = get_port_by_name (d);
+ _running = true;
+
+ if (_session) {
+ _session->set_frame_rate (_backend->sample_rate());
+
+ if (_session->config.get_jack_time_master()) {
+ _backend->set_time_master (true);
+ }
+ }
+
+ start_metering_thread ();
+
+ if (!for_latency) {
+ Running(); /* EMIT SIGNAL */
+ }
+
+ return 0;
+}
- if (src) {
- ret = src->connect (d);
- } else if (dst) {
- ret = dst->connect (s);
- } else {
- /* neither port is known to us, and this API isn't intended for use as a general patch bay */
- ret = -1;
+int
+AudioEngine::stop (bool for_latency)
+{
+ if (!_backend) {
+ return 0;
}
- if (ret > 0) {
- /* already exists - no error, no warning */
- } else if (ret < 0) {
- error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
- source, s, destination, d)
- << endmsg;
+ Glib::Threads::Mutex::Lock lm (_process_lock);
+
+ if (_backend->stop ()) {
+ return -1;
}
+
+ _running = false;
+ _processed_frames = 0;
+ _measuring_latency = false;
+ _latency_output_port = 0;
+ _latency_input_port = 0;
+ _started_for_latency = false;
+ stop_metering_thread ();
+
+ Port::PortDrop ();
- return ret;
+ if (!for_latency) {
+ Stopped (); /* EMIT SIGNAL */
+ }
+
+ return 0;
}
int
-AudioEngine::disconnect (const string& source, const string& destination)
+AudioEngine::freewheel (bool start_stop)
{
- int ret;
-
- if (!_running) {
- if (!_has_run) {
- fatal << _("disconnect called before engine was started") << endmsg;
- /*NOTREACHED*/
- } else {
- return -1;
- }
+ if (!_backend) {
+ return -1;
}
- string s = make_port_name_non_relative (source);
- string d = make_port_name_non_relative (destination);
+ /* _freewheeling will be set when first Freewheel signal occurs */
- boost::shared_ptr<Port> src = get_port_by_name (s);
- boost::shared_ptr<Port> dst = get_port_by_name (d);
+ return _backend->freewheel (start_stop);
+}
- if (src) {
- ret = src->disconnect (d);
- } else if (dst) {
- ret = dst->disconnect (s);
- } else {
- /* neither port is known to us, and this API isn't intended for use as a general patch bay */
- ret = -1;
+float
+AudioEngine::get_dsp_load() const
+{
+ if (!_backend) {
+ return 0.0;
}
- return ret;
+ return _backend->dsp_load ();
}
-int
-AudioEngine::disconnect (boost::shared_ptr<Port> port)
+bool
+AudioEngine::is_realtime() const
{
- GET_PRIVATE_JACK_POINTER_RET (_jack,-1);
-
- if (!_running) {
- if (!_has_run) {
- fatal << _("disconnect called before engine was started") << endmsg;
- /*NOTREACHED*/
- } else {
- return -1;
- }
+ if (!_backend) {
+ return false;
}
- return port->disconnect_all ();
+ return _backend->is_realtime();
}
-ARDOUR::framecnt_t
-AudioEngine::frame_rate () const
+bool
+AudioEngine::connected() const
{
- GET_PRIVATE_JACK_POINTER_RET (_jack, 0);
- if (_frame_rate == 0) {
- return (_frame_rate = jack_get_sample_rate (_priv_jack));
- } else {
- return _frame_rate;
+ if (!_backend) {
+ return false;
}
+
+ return _backend->available();
}
-size_t
-AudioEngine::raw_buffer_size (DataType t)
+void
+AudioEngine::transport_start ()
{
- std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
- return (s != _raw_buffer_sizes.end()) ? s->second : 0;
+ if (!_backend) {
+ return;
+ }
+ return _backend->transport_start ();
}
-ARDOUR::pframes_t
-AudioEngine::frames_per_cycle () const
+void
+AudioEngine::transport_stop ()
{
- GET_PRIVATE_JACK_POINTER_RET (_jack,0);
- if (_buffer_size == 0) {
- return jack_get_buffer_size (_jack);
- } else {
- return _buffer_size;
+ if (!_backend) {
+ return;
}
+ return _backend->transport_stop ();
}
-/** @param name Full or short name of port
- * @return Corresponding Port or 0.
- */
-
-boost::shared_ptr<Port>
-AudioEngine::get_port_by_name (const string& portname)
+TransportState
+AudioEngine::transport_state ()
{
- if (!_running) {
- if (!_has_run) {
- fatal << _("get_port_by_name() called before engine was started") << endmsg;
- /*NOTREACHED*/
- } else {
- boost::shared_ptr<Port> ();
- }
+ if (!_backend) {
+ return TransportStopped;
}
+ return _backend->transport_state ();
+}
- if (!port_is_mine (portname)) {
- /* not an ardour port */
- return boost::shared_ptr<Port> ();
- }
+void
+AudioEngine::transport_locate (framepos_t pos)
+{
+ if (!_backend) {
+ return;
+ }
+ return _backend->transport_locate (pos);
+}
- boost::shared_ptr<Ports> pr = ports.reader();
- std::string rel = make_port_name_relative (portname);
- Ports::iterator x = pr->find (rel);
+framepos_t
+AudioEngine::transport_frame()
+{
+ if (!_backend) {
+ return 0;
+ }
+ return _backend->transport_frame ();
+}
- if (x != pr->end()) {
- /* its possible that the port was renamed by some 3rd party and
- we don't know about it. check for this (the check is quick
- and cheap), and if so, rename the port (which will alter
- the port map as a side effect).
- */
- const std::string check = make_port_name_relative (jack_port_name (x->second->jack_port()));
- if (check != rel) {
- x->second->set_name (check);
- }
- return x->second;
+framecnt_t
+AudioEngine::sample_rate () const
+{
+ if (!_backend) {
+ return 0;
}
+ return _backend->sample_rate ();
+}
- return boost::shared_ptr<Port> ();
+pframes_t
+AudioEngine::samples_per_cycle () const
+{
+ if (!_backend) {
+ return 0;
+ }
+ return _backend->buffer_size ();
}
-void
-AudioEngine::port_renamed (const std::string& old_relative_name, const std::string& new_relative_name)
+int
+AudioEngine::usecs_per_cycle () const
{
- RCUWriter<Ports> writer (ports);
- boost::shared_ptr<Ports> p = writer.get_copy();
- Ports::iterator x = p->find (old_relative_name);
-
- if (x != p->end()) {
- boost::shared_ptr<Port> port = x->second;
- p->erase (x);
- p->insert (make_pair (new_relative_name, port));
+ if (!_backend) {
+ return -1;
}
+ return _backend->usecs_per_cycle ();
}
-const char **
-AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
+size_t
+AudioEngine::raw_buffer_size (DataType t)
{
- GET_PRIVATE_JACK_POINTER_RET (_jack,0);
- if (!_running) {
- if (!_has_run) {
- fatal << _("get_ports called before engine was started") << endmsg;
- /*NOTREACHED*/
- } else {
- return 0;
- }
+ if (!_backend) {
+ return -1;
}
- return jack_get_ports (_priv_jack, port_name_pattern.c_str(), type_name_pattern.c_str(), flags);
+ return _backend->raw_buffer_size (t);
}
-void
-AudioEngine::died ()
+pframes_t
+AudioEngine::sample_time ()
{
- /* called from a signal handler for SIGPIPE */
+ if (!_backend) {
+ return 0;
+ }
+ return _backend->sample_time ();
+}
- stop_metering_thread ();
+pframes_t
+AudioEngine::sample_time_at_cycle_start ()
+{
+ if (!_backend) {
+ return 0;
+ }
+ return _backend->sample_time_at_cycle_start ();
+}
- _running = false;
- _buffer_size = 0;
- _frame_rate = 0;
- _jack = 0;
+pframes_t
+AudioEngine::samples_since_cycle_start ()
+{
+ if (!_backend) {
+ return 0;
+ }
+ return _backend->samples_since_cycle_start ();
}
bool
-AudioEngine::can_request_hardware_monitoring ()
+AudioEngine::get_sync_offset (pframes_t& offset) const
{
- GET_PRIVATE_JACK_POINTER_RET (_jack,false);
- const char ** ports;
-
- if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
+ if (!_backend) {
return false;
}
-
- free (ports);
-
- return true;
+ return _backend->get_sync_offset (offset);
}
-ChanCount
-AudioEngine::n_physical (unsigned long flags) const
+int
+AudioEngine::create_process_thread (boost::function<void()> func)
{
- ChanCount c;
-
- GET_PRIVATE_JACK_POINTER_RET (_jack, c);
-
- const char ** ports = jack_get_ports (_priv_jack, NULL, NULL, JackPortIsPhysical | flags);
- if (ports == 0) {
- return c;
+ if (!_backend) {
+ return -1;
}
+ return _backend->create_process_thread (func);
+}
- for (uint32_t i = 0; ports[i]; ++i) {
- if (!strstr (ports[i], "Midi-Through")) {
- DataType t (jack_port_type (jack_port_by_name (_jack, ports[i])));
- c.set (t, c.get (t) + 1);
- }
+int
+AudioEngine::join_process_threads ()
+{
+ if (!_backend) {
+ return -1;
}
+ return _backend->join_process_threads ();
+}
- free (ports);
-
- return c;
+bool
+AudioEngine::in_process_thread ()
+{
+ if (!_backend) {
+ return false;
+ }
+ return _backend->in_process_thread ();
}
-ChanCount
-AudioEngine::n_physical_inputs () const
+uint32_t
+AudioEngine::process_thread_count ()
{
- return n_physical (JackPortIsInput);
+ if (!_backend) {
+ return 0;
+ }
+ return _backend->process_thread_count ();
}
-ChanCount
-AudioEngine::n_physical_outputs () const
+int
+AudioEngine::set_device_name (const std::string& name)
{
- return n_physical (JackPortIsOutput);
+ if (!_backend) {
+ return -1;
+ }
+ return _backend->set_device_name (name);
}
-void
-AudioEngine::get_physical (DataType type, unsigned long flags, vector<string>& phy)
+int
+AudioEngine::set_sample_rate (float sr)
{
- GET_PRIVATE_JACK_POINTER (_jack);
- const char ** ports;
+ if (!_backend) {
+ return -1;
+ }
+ return _backend->set_sample_rate (sr);
+}
- if ((ports = jack_get_ports (_priv_jack, NULL, type.to_jack_type(), JackPortIsPhysical | flags)) == 0) {
- return;
+int
+AudioEngine::set_buffer_size (uint32_t bufsiz)
+{
+ if (!_backend) {
+ return -1;
}
+ return _backend->set_buffer_size (bufsiz);
+}
- if (ports) {
- for (uint32_t i = 0; ports[i]; ++i) {
- if (strstr (ports[i], "Midi-Through")) {
- continue;
- }
- phy.push_back (ports[i]);
- }
- free (ports);
+int
+AudioEngine::set_sample_format (SampleFormat sf)
+{
+ if (!_backend) {
+ return -1;
}
+ return _backend->set_sample_format (sf);
}
-/** Get physical ports for which JackPortIsOutput is set; ie those that correspond to
- * a physical input connector.
- */
-void
-AudioEngine::get_physical_inputs (DataType type, vector<string>& ins)
+int
+AudioEngine::set_interleaved (bool yn)
{
- get_physical (type, JackPortIsOutput, ins);
+ if (!_backend) {
+ return -1;
+ }
+ return _backend->set_interleaved (yn);
}
-/** Get physical ports for which JackPortIsInput is set; ie those that correspond to
- * a physical output connector.
- */
-void
-AudioEngine::get_physical_outputs (DataType type, vector<string>& outs)
+int
+AudioEngine::set_input_channels (uint32_t ic)
{
- get_physical (type, JackPortIsInput, outs);
+ if (!_backend) {
+ return -1;
+ }
+ return _backend->set_input_channels (ic);
}
+int
+AudioEngine::set_output_channels (uint32_t oc)
+{
+ if (!_backend) {
+ return -1;
+ }
+ return _backend->set_output_channels (oc);
+}
int
-AudioEngine::reset_timebase ()
+AudioEngine::set_systemic_input_latency (uint32_t il)
{
- GET_PRIVATE_JACK_POINTER_RET (_jack, -1);
- if (_session) {
- if (_session->config.get_jack_time_master()) {
- _backend->set_time_master (true);
- } else {
- _backend->set_time_master (false);
- }
+ if (!_backend) {
+ return -1;
}
- return 0;
+ return _backend->set_systemic_input_latency (il);
}
-void
-AudioEngine::remove_all_ports ()
+int
+AudioEngine::set_systemic_output_latency (uint32_t ol)
{
- /* make sure that JACK callbacks that will be invoked as we cleanup
- * ports know that they have nothing to do.
- */
+ if (!_backend) {
+ return -1;
+ }
+ return _backend->set_systemic_output_latency (ol);
+}
- port_remove_in_progress = true;
+/* END OF BACKEND PROXY API */
- /* process lock MUST be held by caller
+void
+AudioEngine::thread_init_callback (void* arg)
+{
+ /* make sure that anybody who needs to know about this thread
+ knows about it.
*/
- {
- RCUWriter<Ports> writer (ports);
- boost::shared_ptr<Ports> ps = writer.get_copy ();
- ps->clear ();
- }
+ pthread_set_name (X_("audioengine"));
- /* clear dead wood list in RCU */
+ PBD::notify_gui_about_thread_creation ("gui", pthread_self(), X_("AudioEngine"), 4096);
+ PBD::notify_gui_about_thread_creation ("midiui", pthread_self(), X_("AudioEngine"), 128);
- ports.flush ();
+ SessionEvent::create_per_thread_pool (X_("AudioEngine"), 512);
- port_remove_in_progress = false;
-}
+ AsyncMIDIPort::set_process_thread (pthread_self());
+ if (arg) {
+ /* the special thread created/managed by the backend */
+ AudioEngine::instance()->_main_thread = new ProcessThread;
+ }
+}
-string
-AudioEngine::make_port_name_relative (string portname) const
+int
+AudioEngine::sync_callback (TransportState state, framepos_t position)
{
- string::size_type len;
- string::size_type n;
-
- len = portname.length();
-
- for (n = 0; n < len; ++n) {
- if (portname[n] == ':') {
- break;
- }
+ if (_session) {
+ return _session->backend_sync_callback (state, position);
}
+ return 0;
+}
- if ((n != len) && (portname.substr (0, n) == jack_client_name)) {
- return portname.substr (n+1);
- }
+void
+AudioEngine::freewheel_callback (bool onoff)
+{
+ _freewheeling = onoff;
+}
- return portname;
+void
+AudioEngine::latency_callback (bool for_playback)
+{
+ if (_session) {
+ _session->update_latency (for_playback);
+ }
}
-string
-AudioEngine::make_port_name_non_relative (string portname) const
+void
+AudioEngine::update_latencies ()
{
- string str;
+ if (_backend) {
+ _backend->update_latencies ();
+ }
+}
- if (portname.find_first_of (':') != string::npos) {
- return portname;
+void
+AudioEngine::halted_callback (const char* why)
+{
+ if (_in_destructor) {
+ /* everything is under control */
+ return;
}
- str = jack_client_name;
- str += ':';
- str += portname;
+ stop_metering_thread ();
+ _running = false;
- return str;
-}
+ Port::PortDrop (); /* EMIT SIGNAL */
-bool
-AudioEngine::port_is_mine (const string& portname) const
-{
- if (portname.find_first_of (':') != string::npos) {
- if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
- return false;
- }
- }
- return true;
+ if (!_started_for_latency) {
+ Halted (why); /* EMIT SIGNAL */
+ }
}
bool
-AudioEngine::port_is_physical (const std::string& portname) const
+AudioEngine::setup_required () const
{
- GET_PRIVATE_JACK_POINTER_RET(_jack, false);
-
- jack_port_t *port = jack_port_by_name (_priv_jack, portname.c_str());
+ /* If there is only a single backend and it claims to be configured
+ * already there is no setup to be done.
+ *
+ * Primarily for a case where there is only a JACK backend and
+ * JACK is already running.
+ */
+
+ if (_backends.size() == 1 && _backends.begin()->second->already_configured()) {
+ return false;
+ }
- if (!port) {
- return false;
- }
+ return true;
+}
- return jack_port_flags (port) & JackPortIsPhysical;
+MTDM*
+AudioEngine::mtdm()
+{
+ return _mtdm;
}
-void
-AudioEngine::destroy ()
+int
+AudioEngine::prepare_for_latency_measurement ()
{
- delete _instance;
- _instance = 0;
+ if (running()) {
+ _stopped_for_latency = true;
+ stop (true);
+ }
+
+ if (start (true)) {
+ _started_for_latency = true;
+ return -1;
+ }
+
+ return 0;
}
int
-AudioEngine::discover_backends ()
+AudioEngine::start_latency_detection ()
{
- vector<std::string> backend_modules;
- AudioBackend* backend;
+ if (!running()) {
+ if (prepare_for_latency_measurement ()) {
+ return -1;
+ }
+ }
- Glib::PatternSpec so_extension_pattern("*.so");
- Glib::PatternSpec dylib_extension_pattern("*.dylib");
+ PortEngine& pe (port_engine());
- find_matching_files_in_search_path (backend_search_path (),
- so_extension_pattern, backend_modules);
+ delete _mtdm;
+ _mtdm = 0;
- find_matching_files_in_search_path (backend_search_path (),
- dylib_extension_pattern, backend_modules);
+ /* find the ports we will connect to */
- DEBUG_TRACE (DEBUG::Panning, string_compose (_("looking for backends in %1"), backend_search_path().to_string()));
+ PortEngine::PortHandle out = pe.get_port_by_name (_latency_output_name);
+ PortEngine::PortHandle in = pe.get_port_by_name (_latency_input_name);
- for (vector<std::string>::iterator i = backend_modules.begin(); i != backend_modules.end(); ++i) {
- if ((backend = backend_discover (*i)) != 0) {
- _backends.insert (make_pair (backend->name(), backend));
- }
+ if (!out || !in) {
+ stop (true);
+ return -1;
}
+
+ /* create the ports we will use to read/write data */
+
+ if ((_latency_output_port = pe.register_port ("latency_out", DataType::AUDIO, IsOutput)) == 0) {
+ stop (true);
+ return -1;
+ }
+ if (pe.connect (_latency_output_port, _latency_output_name)) {
+ pe.unregister_port (_latency_output_port);
+ stop (true);
+ return -1;
+ }
+
+ const string portname ("latency_in");
+ if ((_latency_input_port = pe.register_port (portname, DataType::AUDIO, IsInput)) == 0) {
+ pe.unregister_port (_latency_output_port);
+ stop (true);
+ return -1;
+ }
+ if (pe.connect (_latency_input_name, make_port_name_non_relative (portname))) {
+ pe.unregister_port (_latency_output_port);
+ stop (true);
+ return -1;
+ }
+
+ LatencyRange lr;
+ _latency_signal_latency = 0;
+ lr = pe.get_latency_range (in, false);
+ _latency_signal_latency = lr.max;
+ lr = pe.get_latency_range (out, true);
+ _latency_signal_latency += lr.max;
+
+ /* all created and connected, lets go */
+
+ _mtdm = new MTDM (sample_rate());
+ _measuring_latency = true;
+ _latency_flush_frames = samples_per_cycle();
+
+ return 0;
}
-AudioBackend*
-AudioEngine::backend_discover (string path)
+void
+AudioEngine::stop_latency_detection ()
{
- Glib::Module* module = new Glib::Module(path);
- AudioBackend* (*dfunc)(void);
- void* func = 0;
+ _measuring_latency = false;
- if (!module) {
- error << string_compose(_("AudioEngine: cannot load module \"%1\" (%2)"), path,
- Glib::Module::get_last_error()) << endmsg;
- delete module;
- return 0;
+ if (_latency_output_port) {
+ port_engine().unregister_port (_latency_output_port);
+ _latency_output_port = 0;
+ }
+ if (_latency_input_port) {
+ port_engine().unregister_port (_latency_input_port);
+ _latency_input_port = 0;
}
- if (!module->get_symbol("backend_factory", func)) {
- error << string_compose(_("AudioEngine: module \"%1\" has no factory function."), path) << endmsg;
- error << Glib::Module::get_last_error() << endmsg;
- delete module;
- return 0;
+ stop (true);
+
+ if (_stopped_for_latency) {
+ start ();
}
- dfunc = (AudioBackend* (*)(void))func;
- AudioBackend* backend = dfunc();
+ _stopped_for_latency = false;
+ _started_for_latency = false;
+}
- return backend;
+void
+AudioEngine::set_latency_output_port (const string& name)
+{
+ _latency_output_name = name;
+}
+
+void
+AudioEngine::set_latency_input_port (const string& name)
+{
+ _latency_input_name = name;
}