X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fio.cc;h=ebd295411e9cdfaf3d5198e0c36f0a737cd86d92;hb=4861eca97483128e5febb575b94688581abb0154;hp=f899b71d1ea2c15e01d1eed4ebc34933d0c2ab5a;hpb=45d3ec1437cf661533bc7750c623865def4424df;p=ardour.git diff --git a/libs/ardour/io.cc b/libs/ardour/io.cc index f899b71d1e..ebd295411e 100644 --- a/libs/ardour/io.cc +++ b/libs/ardour/io.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2000 Paul Davis + Copyright (C) 2000-2006 Paul Davis This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -14,2003 +14,1104 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ #include #include +#include +#include + #include #include #include -#include - -#include +#include +#include -#include -#include +#include "pbd/xml++.h" +#include "pbd/replace_all.h" +#include "pbd/unknown_type.h" +#include "pbd/enumwriter.h" -#include -#include -#include -#include -#include -#include -#include -#include +#include "ardour/audioengine.h" +#include "ardour/buffer.h" +#include "ardour/buffer_set.h" +#include "ardour/debug.h" +#include "ardour/io.h" +#include "ardour/port.h" +#include "ardour/route.h" +#include "ardour/session.h" +#include "ardour/user_bundle.h" #include "i18n.h" -#include - -/* - A bug in OS X's cmath that causes isnan() and isinf() to be - "undeclared". the following works around that -*/ - -#if defined(__APPLE__) && defined(__MACH__) -extern "C" int isnan (double); -extern "C" int isinf (double); -#endif - -#define BLOCK_PROCESS_CALLBACK() Glib::Mutex::Lock em (_session.engine().process_lock()) +#define BLOCK_PROCESS_CALLBACK() Glib::Threads::Mutex::Lock em (AudioEngine::instance()->process_lock()) using namespace std; using namespace ARDOUR; using namespace PBD; -nframes_t IO::_automation_interval = 0; -const string IO::state_node_name = "IO"; -bool IO::connecting_legal = false; -bool IO::ports_legal = false; -bool IO::panners_legal = false; -sigc::signal IO::Meter; -sigc::signal IO::ConnectingLegal; -sigc::signal IO::PortsLegal; -sigc::signal IO::PannersLegal; -sigc::signal IO::MoreOutputs; -sigc::signal IO::PortsCreated; - -Glib::StaticMutex IO::m_meter_signal_lock = GLIBMM_STATIC_MUTEX_INIT; - -/* this is a default mapper of [0 .. 1.0] control values to a gain coefficient. - others can be imagined. -*/ - -static gain_t direct_control_to_gain (double fract) { - /* XXX Marcus writes: this doesn't seem right to me. but i don't have a better answer ... */ - /* this maxes at +6dB */ - return pow (2.0,(sqrt(sqrt(sqrt(fract)))*198.0-192.0)/6.0); -} - -static double direct_gain_to_control (gain_t gain) { - /* XXX Marcus writes: this doesn't seem right to me. but i don't have a better answer ... */ - if (gain == 0) return 0.0; - - return pow((6.0*log(gain)/log(2.0)+192.0)/198.0, 8.0); -} - -static bool sort_ports_by_name (Port* a, Port* b) -{ - return a->name() < b->name(); -} - +const string IO::state_node_name = "IO"; +bool IO::connecting_legal = false; +PBD::Signal0 IO::ConnectingLegal; +PBD::Signal1 IO::PortCountChanged; /** @param default_type The type of port that will be created by ensure_io * and friends if no type is explicitly requested (to avoid breakage). */ -IO::IO (Session& s, string name, - int input_min, int input_max, int output_min, int output_max, - DataType default_type) - : _session (s), - _name (name), - _default_type(default_type), - _gain_control (X_("gaincontrol"), *this), - _gain_automation_curve (0.0, 2.0, 1.0), - _input_minimum (input_min), - _input_maximum (input_max), - _output_minimum (output_min), - _output_maximum (output_max) -{ - _panner = new Panner (name, _session); - _gain = 1.0; - _desired_gain = 1.0; - _input_connection = 0; - _output_connection = 0; +IO::IO (Session& s, const string& name, Direction dir, DataType default_type, bool sendish) + : SessionObject (s, name) + , _direction (dir) + , _default_type (default_type) + , _sendish (sendish) +{ + _active = true; + Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2)); pending_state_node = 0; - _ninputs = 0; - _noutputs = 0; - no_panner_reset = false; - deferred_state = 0; - - apply_gain_automation = false; - _ignore_gain_on_deliver = false; - - last_automation_snapshot = 0; - - _gain_automation_state = Off; - _gain_automation_style = Absolute; + setup_bundle (); +} - { - // IO::Meter is emitted from another thread so the - // Meter signal must be protected. - Glib::Mutex::Lock guard (m_meter_signal_lock); - m_meter_connection = Meter.connect (mem_fun (*this, &IO::meter)); - } +IO::IO (Session& s, const XMLNode& node, DataType dt, bool sendish) + : SessionObject(s, "unnamed io") + , _direction (Input) + , _default_type (dt) + , _sendish (sendish) +{ + _active = true; + pending_state_node = 0; + Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2)); - _session.add_controllable (&_gain_control); + set_state (node, Stateful::loading_state_version); + setup_bundle (); } -IO::IO (Session& s, const XMLNode& node, DataType dt) - : _session (s), - _default_type (dt), - _gain_control (X_("gaincontrol"), *this), - _gain_automation_curve (0, 0, 0) // all reset in set_state() +IO::~IO () { - _panner = 0; - deferred_state = 0; - no_panner_reset = false; - _desired_gain = 1.0; - _gain = 1.0; - _input_connection = 0; - _output_connection = 0; - _ninputs = 0; - _noutputs = 0; - - apply_gain_automation = false; - _ignore_gain_on_deliver = false; + Glib::Threads::Mutex::Lock lm (io_lock); - set_state (node); + BLOCK_PROCESS_CALLBACK (); - { - // IO::Meter is emitted from another thread so the - // Meter signal must be protected. - Glib::Mutex::Lock guard (m_meter_signal_lock); - m_meter_connection = Meter.connect (mem_fun (*this, &IO::meter)); + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { + _session.engine().unregister_port (*i); } - - _session.add_controllable (&_gain_control); } -IO::~IO () +void +IO::disconnect_check (boost::shared_ptr a, boost::shared_ptr b) { - Glib::Mutex::Lock guard (m_meter_signal_lock); - - Glib::Mutex::Lock lm (io_lock); - vector::iterator i; + /* this could be called from within our own ::disconnect() method(s) + or from somewhere that operates directly on a port. so, we don't + know for sure if we can take this lock or not. if we fail, + we assume that its safely locked by our own ::disconnect(). + */ - for (i = _inputs.begin(); i != _inputs.end(); ++i) { - _session.engine().unregister_port (*i); - } + Glib::Threads::Mutex::Lock tm (io_lock, Glib::Threads::TRY_LOCK); - for (i = _outputs.begin(); i != _outputs.end(); ++i) { - _session.engine().unregister_port (*i); + if (tm.locked()) { + /* we took the lock, so we cannot be here from inside + * ::disconnect() + */ + if (_ports.contains (a) || _ports.contains (b)) { + changed (IOChange (IOChange::ConnectionsChanged), this); /* EMIT SIGNAL */ + } + } else { + /* we didn't get the lock, so assume that we're inside + * ::disconnect(), and it will call changed() appropriately. + */ } - - m_meter_connection.disconnect(); } void -IO::silence (nframes_t nframes, nframes_t offset) +IO::increment_port_buffer_offset (pframes_t offset) { /* io_lock, not taken: function must be called from Session::process() calltree */ - for (vector::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - (*i)->silence (nframes, offset); - } + if (_direction == Output) { + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { + i->increment_port_buffer_offset (offset); + } + } } void -IO::apply_declick (vector& bufs, uint32_t nbufs, nframes_t nframes, gain_t initial, gain_t target, bool invert_polarity) +IO::silence (framecnt_t nframes) { - nframes_t declick = min ((nframes_t)128, nframes); - gain_t delta; - Sample *buffer; - double fractional_shift; - double fractional_pos; - gain_t polscale = invert_polarity ? -1.0f : 1.0f; - - if (nframes == 0) return; - - fractional_shift = -1.0/declick; + /* io_lock, not taken: function must be called from Session::process() calltree */ - if (target < initial) { - /* fade out: remove more and more of delta from initial */ - delta = -(initial - target); - } else { - /* fade in: add more and more of delta from initial */ - delta = target - initial; + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { + i->get_buffer(nframes).silence (nframes); } +} + +/** Set _bundles_connected to those bundles that are connected such that every + * port on every bundle channel x is connected to port x in _ports. + */ +void +IO::check_bundles_connected () +{ + std::vector new_list; - for (uint32_t n = 0; n < nbufs; ++n) { + for (std::vector::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) { - buffer = bufs[n]; - fractional_pos = 1.0; + uint32_t const N = (*i)->bundle->nchannels().n_total(); - for (nframes_t nx = 0; nx < declick; ++nx) { - buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos)))); - fractional_pos += fractional_shift; + if (_ports.num_ports() < N) { + continue; } - - /* now ensure the rest of the buffer has the target value - applied, if necessary. - */ - if (declick != nframes) { - float this_target; + bool ok = true; - if (invert_polarity) { - this_target = -target; - } else { - this_target = target; + for (uint32_t j = 0; j < N; ++j) { + /* Every port on bundle channel j must be connected to our input j */ + Bundle::PortList const pl = (*i)->bundle->channel_ports (j); + for (uint32_t k = 0; k < pl.size(); ++k) { + if (_ports.port(j)->connected_to (pl[k]) == false) { + ok = false; + break; + } } - if (this_target == 0.0) { - memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick)); - } else if (this_target != 1.0) { - for (nframes_t nx = declick; nx < nframes; ++nx) { - buffer[nx] *= this_target; - } + if (ok == false) { + break; } } + + if (ok) { + new_list.push_back (*i); + } else { + delete *i; + } } -} -void -IO::pan_automated (vector& bufs, uint32_t nbufs, nframes_t start, nframes_t end, nframes_t nframes, nframes_t offset) -{ - Sample* dst; + _bundles_connected = new_list; +} - /* io_lock, not taken: function must be called from Session::process() calltree */ - if (_noutputs == 0) { - return; +int +IO::disconnect (boost::shared_ptr our_port, string other_port, void* src) +{ + if (other_port.length() == 0 || our_port == 0) { + return 0; } - if (_noutputs == 1) { - - dst = output(0)->get_buffer (nframes) + offset; + { + Glib::Threads::Mutex::Lock lm (io_lock); - for (uint32_t n = 0; n < nbufs; ++n) { - if (bufs[n] != dst) { - memcpy (dst, bufs[n], sizeof (Sample) * nframes); - } - } + /* check that our_port is really one of ours */ - output(0)->mark_silence (false); + if ( ! _ports.contains(our_port)) { + return -1; + } - return; - } + /* disconnect it from the source */ - uint32_t o; - vector::iterator out; - vector::iterator in; - Panner::iterator pan; - Sample* obufs[_noutputs]; + if (our_port->disconnect (other_port)) { + error << string_compose(_("IO: cannot disconnect port %1 from %2"), our_port->name(), other_port) << endmsg; + return -1; + } - /* the terrible silence ... */ + check_bundles_connected (); + } - for (out = _outputs.begin(), o = 0; out != _outputs.end(); ++out, ++o) { - obufs[o] = (*out)->get_buffer (nframes) + offset; - memset (obufs[o], 0, sizeof (Sample) * nframes); - (*out)->mark_silence (false); - } + changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */ - uint32_t n; + _session.set_dirty (); - for (pan = _panner->begin(), n = 0; n < nbufs; ++n, ++pan) { - (*pan)->distribute_automated (bufs[n], obufs, start, end, nframes, _session.pan_automation_buffer()); - } + return 0; } -void -IO::pan (vector& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset, gain_t gain_coeff) +int +IO::connect (boost::shared_ptr our_port, string other_port, void* src) { - Sample* dst; - Sample* src; + if (other_port.length() == 0 || our_port == 0) { + return 0; + } - /* io_lock, not taken: function must be called from Session::process() calltree */ + { + Glib::Threads::Mutex::Lock lm (io_lock); - if (_noutputs == 0) { - return; - } + /* check that our_port is really one of ours */ - /* the panner can be empty if there are no inputs to the - route, but still outputs - */ + if ( ! _ports.contains(our_port) ) { + return -1; + } - if (_panner->bypassed() || _panner->empty()) { - deliver_output_no_pan (bufs, nbufs, nframes, offset); - return; + /* connect it to the source */ + + if (our_port->connect (other_port)) { + return -1; + } } + changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */ + _session.set_dirty (); + return 0; +} - if (_noutputs == 1) { +int +IO::remove_port (boost::shared_ptr port, void* src) +{ + ChanCount before = _ports.count (); + ChanCount after = before; + after.set (port->type(), after.get (port->type()) - 1); - dst = output(0)->get_buffer (nframes) + offset; + boost::optional const r = PortCountChanging (after); /* EMIT SIGNAL */ + if (r.get_value_or (false)) { + return -1; + } - if (gain_coeff == 0.0f) { + IOChange change; - /* only one output, and gain was zero, so make it silent */ + { + BLOCK_PROCESS_CALLBACK (); - memset (dst, 0, sizeof (Sample) * nframes); - - } else if (gain_coeff == 1.0f){ + { + Glib::Threads::Mutex::Lock lm (io_lock); - /* mix all buffers into the output */ + if (_ports.remove(port)) { + change.type = IOChange::Type (change.type | IOChange::ConfigurationChanged); + change.before = before; + change.after = _ports.count (); - uint32_t n; - - memcpy (dst, bufs[0], sizeof (Sample) * nframes); - - for (n = 1; n < nbufs; ++n) { - src = bufs[n]; - - for (nframes_t n = 0; n < nframes; ++n) { - dst[n] += src[n]; + if (port->connected()) { + change.type = IOChange::Type (change.type | IOChange::ConnectionsChanged); } - } - output(0)->mark_silence (false); - - } else { - - /* mix all buffers into the output, scaling them all by the gain */ + _session.engine().unregister_port (port); + check_bundles_connected (); + } + } - uint32_t n; + PortCountChanged (n_ports()); /* EMIT SIGNAL */ - src = bufs[0]; - - for (nframes_t n = 0; n < nframes; ++n) { - dst[n] = src[n] * gain_coeff; - } - - for (n = 1; n < nbufs; ++n) { - src = bufs[n]; - - for (nframes_t n = 0; n < nframes; ++n) { - dst[n] += src[n] * gain_coeff; - } - } - - output(0)->mark_silence (false); + if (change.type != IOChange::NoChange) { + changed (change, src); + _buffers.attach_buffers (_ports); } + } - return; + if (change.type & IOChange::ConfigurationChanged) { + setup_bundle (); + } + + if (change.type == IOChange::NoChange) { + return -1; } - uint32_t o; - vector::iterator out; - vector::iterator in; - Panner::iterator pan; - Sample* obufs[_noutputs]; + _session.set_dirty (); + + return 0; +} + +/** Add a port. + * + * @param destination Name of port to connect new port to. + * @param src Source for emitted ConfigurationChanged signal. + * @param type Data type of port. Default value (NIL) will use this IO's default type. + */ +int +IO::add_port (string destination, void* src, DataType type) +{ + boost::shared_ptr our_port; - /* the terrible silence ... */ + if (type == DataType::NIL) { + type = _default_type; + } - /* XXX this is wasteful but i see no way to avoid it */ + ChanCount before = _ports.count (); + ChanCount after = before; + after.set (type, after.get (type) + 1); - for (out = _outputs.begin(), o = 0; out != _outputs.end(); ++out, ++o) { - obufs[o] = (*out)->get_buffer (nframes) + offset; - memset (obufs[o], 0, sizeof (Sample) * nframes); - (*out)->mark_silence (false); + bool const r = PortCountChanging (after); /* EMIT SIGNAL */ + if (r) { + return -1; } + + IOChange change; + + { + BLOCK_PROCESS_CALLBACK (); - uint32_t n; - for (pan = _panner->begin(), n = 0; n < nbufs; ++n) { - Panner::iterator tmp; + { + Glib::Threads::Mutex::Lock lm (io_lock); - tmp = pan; - ++tmp; + /* Create a new port */ - (*pan)->distribute (bufs[n], obufs, gain_coeff, nframes); + string portname = build_legal_port_name (type); + + if (_direction == Input) { + if ((our_port = _session.engine().register_input_port (type, portname)) == 0) { + error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg; + return -1; + } + } else { + if ((our_port = _session.engine().register_output_port (type, portname)) == 0) { + error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg; + return -1; + } + } - if (tmp != _panner->end()) { - pan = tmp; + change.before = _ports.count (); + _ports.add (our_port); } + + PortCountChanged (n_ports()); /* EMIT SIGNAL */ + change.type = IOChange::ConfigurationChanged; + change.after = _ports.count (); + changed (change, src); /* EMIT SIGNAL */ + _buffers.attach_buffers (_ports); } -} - -void -IO::deliver_output (vector& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset) -{ - /* io_lock, not taken: function must be called from Session::process() calltree */ - if (_noutputs == 0) { - return; - } - - if (_panner->bypassed() || _panner->empty()) { - deliver_output_no_pan (bufs, nbufs, nframes, offset); - return; + if (!destination.empty()) { + if (our_port->connect (destination)) { + return -1; + } } + setup_bundle (); + _session.set_dirty (); - gain_t dg; - gain_t pangain = _gain; - + return 0; +} + +int +IO::disconnect (void* src) +{ { - Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK); - - if (dm.locked()) { - dg = _desired_gain; - } else { - dg = _gain; + Glib::Threads::Mutex::Lock lm (io_lock); + + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { + i->disconnect_all (); } - } - if (dg != _gain) { - apply_declick (bufs, nbufs, nframes, _gain, dg, false); - _gain = dg; - pangain = 1.0f; - } + check_bundles_connected (); + } - /* simple, non-automation panning to outputs */ + changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */ - if (_session.transport_speed() > 1.5f || _session.transport_speed() < -1.5f) { - pan (bufs, nbufs, nframes, offset, pangain * speed_quietning); - } else { - pan (bufs, nbufs, nframes, offset, pangain); - } + return 0; } -void -IO::deliver_output_no_pan (vector& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset) +/** Caller must hold process lock */ +int +IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed) { - /* io_lock, not taken: function must be called from Session::process() calltree */ - - if (_noutputs == 0) { - return; - } +#ifndef PLATFORM_WINDOWS + assert (!AudioEngine::instance()->process_lock().trylock()); +#endif - gain_t dg; - gain_t old_gain = _gain; + boost::shared_ptr port; - if (apply_gain_automation || _ignore_gain_on_deliver) { + changed = false; - /* gain has already been applied by automation code. do nothing here except - speed quietning. - */ + for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { - _gain = 1.0f; - dg = _gain; - - } else { + const size_t n = count.get(*t); - Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK); - - if (dm.locked()) { - dg = _desired_gain; - } else { - dg = _gain; - } - } + /* remove unused ports */ + for (size_t i = n_ports().get(*t); i > n; --i) { + port = _ports.port(*t, i-1); - Sample* src; - Sample* dst; - uint32_t i; - vector::iterator o; - vector outs; - gain_t actual_gain; + assert(port); + _ports.remove(port); + _session.engine().unregister_port (port); - if (dg != _gain) { - /* unlikely condition */ - for (o = _outputs.begin(), i = 0; o != _outputs.end(); ++o, ++i) { - outs.push_back ((*o)->get_buffer (nframes) + offset); + changed = true; } - } - /* reduce nbufs to the index of the last input buffer */ + /* create any necessary new ports */ + while (n_ports().get(*t) < n) { - nbufs--; + string portname = build_legal_port_name (*t); - if (_session.transport_speed() > 1.5f || _session.transport_speed() < -1.5f) { - actual_gain = _gain * speed_quietning; - } else { - actual_gain = _gain; - } - - for (o = _outputs.begin(), i = 0; o != _outputs.end(); ++o, ++i) { + try { - dst = (*o)->get_buffer (nframes) + offset; - src = bufs[min(nbufs,i)]; + if (_direction == Input) { + if ((port = _session.engine().register_input_port (*t, portname)) == 0) { + error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg; + return -1; + } + } else { + if ((port = _session.engine().register_output_port (*t, portname)) == 0) { + error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg; + return -1; + } + } + } - if (dg != _gain || actual_gain == 1.0f) { - memcpy (dst, src, sizeof (Sample) * nframes); - } else if (actual_gain == 0.0f) { - memset (dst, 0, sizeof (Sample) * nframes); - } else { - for (nframes_t x = 0; x < nframes; ++x) { - dst[x] = src[x] * actual_gain; + catch (AudioEngine::PortRegistrationFailure& err) { + /* pass it on */ + throw; } + + _ports.add (port); + changed = true; } - - (*o)->mark_silence (false); } - if (dg != _gain) { - apply_declick (outs, outs.size(), nframes, _gain, dg, false); - _gain = dg; + if (changed) { + check_bundles_connected (); + PortCountChanged (n_ports()); /* EMIT SIGNAL */ + _session.set_dirty (); } - if (apply_gain_automation || _ignore_gain_on_deliver) { - _gain = old_gain; + if (clear) { + /* disconnect all existing ports so that we get a fresh start */ + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { + i->disconnect_all (); + } } + + return 0; } -void -IO::collect_input (vector& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset) +/** Caller must hold process lock */ +int +IO::ensure_ports (ChanCount count, bool clear, void* src) { - /* io_lock, not taken: function must be called from Session::process() calltree */ +#ifndef PLATFORM_WINDOWS + assert (!AudioEngine::instance()->process_lock().trylock()); +#endif - vector::iterator i; - uint32_t n; - Sample *last = 0; - - /* we require that bufs.size() >= 1 */ + bool changed = false; - for (n = 0, i = _inputs.begin(); n < nbufs; ++i, ++n) { - if (i == _inputs.end()) { - break; - } - - /* XXX always read the full extent of the port buffer that - we need. One day, we may use jack_port_get_buffer_at_offset() - or something similar. For now, this simple hack will - have to do. + if (count == n_ports() && !clear) { + return 0; + } - Hack? Why yes .. we only need to read nframes-worth of - data, but the data we want is at `offset' within the - buffer. - */ + IOChange change; - last = (*i)->get_buffer (nframes+offset) + offset; - // the dest buffer's offset has already been applied - memcpy (bufs[n], last, sizeof (Sample) * nframes); - } + change.before = _ports.count (); - /* fill any excess outputs with the last input */ - - if (last) { - while (n < nbufs) { - // the dest buffer's offset has already been applied - memcpy (bufs[n], last, sizeof (Sample) * nframes); - ++n; - } - } else { - while (n < nbufs) { - memset (bufs[n], 0, sizeof (Sample) * nframes); - ++n; + { + Glib::Threads::Mutex::Lock im (io_lock); + if (ensure_ports_locked (count, clear, changed)) { + return -1; } } -} - -void -IO::just_meter_input (nframes_t start_frame, nframes_t end_frame, - nframes_t nframes, nframes_t offset) -{ - vector& bufs = _session.get_passthru_buffers (); - uint32_t nbufs = n_process_buffers (); - - collect_input (bufs, nbufs, nframes, offset); - for (uint32_t n = 0; n < nbufs; ++n) { - _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]); + if (changed) { + change.after = _ports.count (); + change.type = IOChange::ConfigurationChanged; + this->changed (change, src); /* EMIT SIGNAL */ + _buffers.attach_buffers (_ports); + setup_bundle (); + _session.set_dirty (); } + + return 0; } -void -IO::drop_input_connection () +/** Caller must hold process lock */ +int +IO::ensure_io (ChanCount count, bool clear, void* src) { - _input_connection = 0; - input_connection_configuration_connection.disconnect(); - input_connection_connection_connection.disconnect(); - _session.set_dirty (); +#ifndef PLATFORM_WINDOWS + assert (!AudioEngine::instance()->process_lock().trylock()); +#endif + + return ensure_ports (count, clear, src); } -void -IO::drop_output_connection () +XMLNode& +IO::get_state () { - _output_connection = 0; - output_connection_configuration_connection.disconnect(); - output_connection_connection_connection.disconnect(); - _session.set_dirty (); + return state (true); } -int -IO::disconnect_input (Port* our_port, string other_port, void* src) +XMLNode& +IO::state (bool /*full_state*/) { - if (other_port.length() == 0 || our_port == 0) { - return 0; - } + XMLNode* node = new XMLNode (state_node_name); + char buf[64]; + string str; + vector::iterator ci; + int n; + LocaleGuard lg (X_("POSIX")); + Glib::Threads::Mutex::Lock lm (io_lock); - { - BLOCK_PROCESS_CALLBACK (); - - { - Glib::Mutex::Lock lm (io_lock); - - /* check that our_port is really one of ours */ - - if (find (_inputs.begin(), _inputs.end(), our_port) == _inputs.end()) { - return -1; - } - - /* disconnect it from the source */ - - if (_session.engine().disconnect (other_port, our_port->name())) { - error << string_compose(_("IO: cannot disconnect input port %1 from %2"), our_port->name(), other_port) << endmsg; - return -1; - } + node->add_property("name", _name); + id().print (buf, sizeof (buf)); + node->add_property("id", buf); + node->add_property ("direction", enum_2_string (_direction)); + node->add_property ("default-type", _default_type.to_string()); - drop_input_connection(); - } + for (std::vector::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) { + XMLNode* n = new XMLNode ("Bundle"); + n->add_property ("name", (*i)->bundle->name ()); + node->add_child_nocopy (*n); } - input_changed (ConnectionsChanged, src); /* EMIT SIGNAL */ - _session.set_dirty (); + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { - return 0; -} + vector connections; -int -IO::connect_input (Port* our_port, string other_port, void* src) -{ - if (other_port.length() == 0 || our_port == 0) { - return 0; - } + XMLNode* pnode = new XMLNode (X_("Port")); + pnode->add_property (X_("type"), i->type().to_string()); + pnode->add_property (X_("name"), i->name()); - { - BLOCK_PROCESS_CALLBACK (); - - { - Glib::Mutex::Lock lm (io_lock); - - /* check that our_port is really one of ours */ - - if (find (_inputs.begin(), _inputs.end(), our_port) == _inputs.end()) { - return -1; - } - - /* connect it to the source */ + if (i->get_connections (connections)) { - if (_session.engine().connect (other_port, our_port->name())) { - return -1; - } - - drop_input_connection (); - } - } + for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) { - input_changed (ConnectionsChanged, src); /* EMIT SIGNAL */ - _session.set_dirty (); - return 0; -} + /* if its a connection to our own port, + return only the port name, not the + whole thing. this allows connections + to be re-established even when our + client name is different. + */ -int -IO::disconnect_output (Port* our_port, string other_port, void* src) -{ - if (other_port.length() == 0 || our_port == 0) { - return 0; - } + XMLNode* cnode = new XMLNode (X_("Connection")); - { - BLOCK_PROCESS_CALLBACK (); - - { - Glib::Mutex::Lock lm (io_lock); - - if (find (_outputs.begin(), _outputs.end(), our_port) == _outputs.end()) { - return -1; - } - - /* disconnect it from the destination */ - - if (_session.engine().disconnect (our_port->name(), other_port)) { - error << string_compose(_("IO: cannot disconnect output port %1 from %2"), our_port->name(), other_port) << endmsg; - return -1; + cnode->add_property (X_("other"), _session.engine().make_port_name_relative (*ci)); + pnode->add_child_nocopy (*cnode); } - - drop_output_connection (); } - } - - output_changed (ConnectionsChanged, src); /* EMIT SIGNAL */ - _session.set_dirty (); - return 0; -} - -int -IO::connect_output (Port* our_port, string other_port, void* src) -{ - if (other_port.length() == 0 || our_port == 0) { - return 0; - } - - { - BLOCK_PROCESS_CALLBACK (); - - { - Glib::Mutex::Lock lm (io_lock); - - /* check that our_port is really one of ours */ - - if (find (_outputs.begin(), _outputs.end(), our_port) == _outputs.end()) { - return -1; - } - - /* connect it to the destination */ - - if (_session.engine().connect (our_port->name(), other_port)) { - return -1; - } - - drop_output_connection (); - } + node->add_child_nocopy (*pnode); } - output_changed (ConnectionsChanged, src); /* EMIT SIGNAL */ - _session.set_dirty (); - return 0; + snprintf (buf, sizeof (buf), "%" PRId64, _user_latency); + node->add_property (X_("user-latency"), buf); + + return *node; } int -IO::set_input (Port* other_port, void* src) +IO::set_state (const XMLNode& node, int version) { - /* this removes all but one ports, and connects that one port - to the specified source. - */ + /* callers for version < 3000 need to call set_state_2X directly, as A3 IOs + * are input OR output, not both, so the direction needs to be specified + * by the caller. + */ + assert (version >= 3000); - if (_input_minimum > 1 || _input_minimum == 0) { - /* sorry, you can't do this */ - return -1; - } + const XMLProperty* prop; + XMLNodeConstIterator iter; + LocaleGuard lg (X_("POSIX")); - if (other_port == 0) { - if (_input_minimum < 0) { - return ensure_inputs (0, false, true, src); - } else { - return -1; - } - } + /* force use of non-localized representation of decimal point, + since we use it a lot in XML files and so forth. + */ - if (ensure_inputs (1, true, true, src)) { + if (node.name() != state_node_name) { + error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg; return -1; } - return connect_input (_inputs.front(), other_port->name(), src); -} - -int -IO::remove_output_port (Port* port, void* src) -{ - IOChange change (NoChange); - - { - BLOCK_PROCESS_CALLBACK (); - - - { - Glib::Mutex::Lock lm (io_lock); - - if (_noutputs - 1 == (uint32_t) _output_minimum) { - /* sorry, you can't do this */ - return -1; - } - - for (vector::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - if (*i == port) { - change = IOChange (change|ConfigurationChanged); - if (port->connected()) { - change = IOChange (change|ConnectionsChanged); - } - - _session.engine().unregister_port (*i); - _outputs.erase (i); - _noutputs--; - drop_output_connection (); - - break; - } - } - - if (change != NoChange) { - setup_peak_meters (); - reset_panner (); - } - } - } - - if (change != NoChange) { - output_changed (change, src); /* EMIT SIGNAL */ - _session.set_dirty (); - return 0; - } - - return -1; -} - -/** Add an output port. - * - * @param destination Name of input port to connect new port to. - * @param src Source for emitted ConfigurationChanged signal. - * @param type Data type of port. Default value (NIL) will use this IO's default type. - */ -int -IO::add_output_port (string destination, void* src, DataType type) -{ - Port* our_port; - char name[64]; - - if (type == DataType::NIL) - type = _default_type; - - { - BLOCK_PROCESS_CALLBACK (); - - - { - Glib::Mutex::Lock lm (io_lock); - - if (_output_maximum >= 0 && (int) _noutputs == _output_maximum) { - return -1; - } - - /* Create a new output port */ - - // FIXME: naming scheme for differently typed ports? - if (_output_maximum == 1) { - snprintf (name, sizeof (name), _("%s/out"), _name.c_str()); - } else { - snprintf (name, sizeof (name), _("%s/out %u"), _name.c_str(), find_output_port_hole()); - } - - if ((our_port = _session.engine().register_output_port (type, name)) == 0) { - error << string_compose(_("IO: cannot register output port %1"), name) << endmsg; - return -1; - } - - _outputs.push_back (our_port); - sort (_outputs.begin(), _outputs.end(), sort_ports_by_name); - ++_noutputs; - drop_output_connection (); - setup_peak_meters (); - reset_panner (); - } - - MoreOutputs (_noutputs); /* EMIT SIGNAL */ + if ((prop = node.property ("name")) != 0) { + set_name (prop->value()); } - if (destination.length()) { - if (_session.engine().connect (our_port->name(), destination)) { - return -1; - } + if ((prop = node.property (X_("default-type"))) != 0) { + _default_type = DataType(prop->value()); + assert(_default_type != DataType::NIL); } - - // pan_changed (src); /* EMIT SIGNAL */ - output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ - _session.set_dirty (); - return 0; -} -int -IO::remove_input_port (Port* port, void* src) -{ - IOChange change (NoChange); + set_id (node); - { - BLOCK_PROCESS_CALLBACK (); - - - { - Glib::Mutex::Lock lm (io_lock); - - if (((int)_ninputs - 1) < _input_minimum) { - /* sorry, you can't do this */ - return -1; - } - for (vector::iterator i = _inputs.begin(); i != _inputs.end(); ++i) { - - if (*i == port) { - change = IOChange (change|ConfigurationChanged); - - if (port->connected()) { - change = IOChange (change|ConnectionsChanged); - } - - _session.engine().unregister_port (*i); - _inputs.erase (i); - _ninputs--; - drop_input_connection (); - - break; - } - } - - if (change != NoChange) { - setup_peak_meters (); - reset_panner (); - } - } + if ((prop = node.property ("direction")) != 0) { + _direction = (Direction) string_2_enum (prop->value(), _direction); } - if (change != NoChange) { - input_changed (change, src); - _session.set_dirty (); - return 0; - } - - return -1; -} - - -/** Add an input port. - * - * @param type Data type of port. The appropriate Jack port type, and @ref Port will be created. - * @param destination Name of input port to connect new port to. - * @param src Source for emitted ConfigurationChanged signal. - */ -int -IO::add_input_port (string source, void* src, DataType type) -{ - Port* our_port; - char name[64]; - - if (type == DataType::NIL) - type = _default_type; - - { - BLOCK_PROCESS_CALLBACK (); - - { - Glib::Mutex::Lock lm (io_lock); - - if (_input_maximum >= 0 && (int) _ninputs == _input_maximum) { - return -1; - } - - /* Create a new input port */ - - // FIXME: naming scheme for differently typed ports? - if (_input_maximum == 1) { - snprintf (name, sizeof (name), _("%s/in"), _name.c_str()); - } else { - snprintf (name, sizeof (name), _("%s/in %u"), _name.c_str(), find_input_port_hole()); - } - - if ((our_port = _session.engine().register_input_port (type, name)) == 0) { - error << string_compose(_("IO: cannot register input port %1"), name) << endmsg; - return -1; - } - - _inputs.push_back (our_port); - sort (_inputs.begin(), _inputs.end(), sort_ports_by_name); - ++_ninputs; - drop_input_connection (); - setup_peak_meters (); - reset_panner (); - } - - MoreOutputs (_ninputs); /* EMIT SIGNAL */ + if (create_ports (node, version)) { + return -1; } - if (source.length()) { + if (connecting_legal) { - if (_session.engine().connect (source, our_port->name())) { + if (make_connections (node, version, false)) { return -1; } - } - - // pan_changed (src); /* EMIT SIGNAL */ - input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ - _session.set_dirty (); - - return 0; -} -int -IO::disconnect_inputs (void* src) -{ - { - BLOCK_PROCESS_CALLBACK (); - - { - Glib::Mutex::Lock lm (io_lock); - - for (vector::iterator i = _inputs.begin(); i != _inputs.end(); ++i) { - _session.engine().disconnect (*i); - } + } else { - drop_input_connection (); - } + pending_state_node = new XMLNode (node); + pending_state_node_version = version; + pending_state_node_in = false; + ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this)); } - input_changed (ConnectionsChanged, src); /* EMIT SIGNAL */ - return 0; -} -int -IO::disconnect_outputs (void* src) -{ - { - BLOCK_PROCESS_CALLBACK (); - - { - Glib::Mutex::Lock lm (io_lock); - - for (vector::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - _session.engine().disconnect (*i); - } - - drop_output_connection (); - } + if ((prop = node.property ("user-latency")) != 0) { + _user_latency = atoi (prop->value ()); } - output_changed (ConnectionsChanged, src); /* EMIT SIGNAL */ - _session.set_dirty (); return 0; } -bool -IO::ensure_inputs_locked (uint32_t n, bool clear, void* src) -{ - Port* input_port; - bool changed = false; - bool reduced = false; - - /* remove unused ports */ - - while (_ninputs > n) { - _session.engine().unregister_port (_inputs.back()); - _inputs.pop_back(); - _ninputs--; - reduced = true; - changed = true; - } - - /* create any necessary new ports */ - - while (_ninputs < n) { - - char buf[64]; - - /* Create a new input port (of the default type) */ - - if (_input_maximum == 1) { - snprintf (buf, sizeof (buf), _("%s/in"), _name.c_str()); - } - else { - snprintf (buf, sizeof (buf), _("%s/in %u"), _name.c_str(), find_input_port_hole()); - } - - try { - - if ((input_port = _session.engine().register_input_port (_default_type, buf)) == 0) { - error << string_compose(_("IO: cannot register input port %1"), buf) << endmsg; - return -1; - } - } - - catch (AudioEngine::PortRegistrationFailure& err) { - setup_peak_meters (); - reset_panner (); - /* pass it on */ - throw AudioEngine::PortRegistrationFailure(); - } - - _inputs.push_back (input_port); - sort (_inputs.begin(), _inputs.end(), sort_ports_by_name); - ++_ninputs; - changed = true; - } - - if (changed) { - drop_input_connection (); - setup_peak_meters (); - reset_panner (); - MoreOutputs (_ninputs); /* EMIT SIGNAL */ - _session.set_dirty (); - } - - if (clear) { - /* disconnect all existing ports so that we get a fresh start */ - - for (vector::iterator i = _inputs.begin(); i != _inputs.end(); ++i) { - _session.engine().disconnect (*i); - } - } - - return changed; -} - int -IO::ensure_io (uint32_t nin, uint32_t nout, bool clear, void* src) +IO::set_state_2X (const XMLNode& node, int version, bool in) { - bool in_changed = false; - bool out_changed = false; - bool in_reduced = false; - bool out_reduced = false; - bool need_pan_reset; - - if (_input_maximum >= 0) { - nin = min (_input_maximum, (int) nin); - } - - if (_output_maximum >= 0) { - nout = min (_output_maximum, (int) nout); - } - - if (nin == _ninputs && nout == _noutputs && !clear) { - return 0; - } - - { - BLOCK_PROCESS_CALLBACK (); - Glib::Mutex::Lock lm (io_lock); - - Port* port; - - if (_noutputs == nout) { - need_pan_reset = false; - } else { - need_pan_reset = true; - } - - /* remove unused ports */ - - while (_ninputs > nin) { - _session.engine().unregister_port (_inputs.back()); - _inputs.pop_back(); - _ninputs--; - in_reduced = true; - in_changed = true; - } - - while (_noutputs > nout) { - _session.engine().unregister_port (_outputs.back()); - _outputs.pop_back(); - _noutputs--; - out_reduced = true; - out_changed = true; - } - - /* create any necessary new ports (of the default type) */ - - while (_ninputs < nin) { - - char buf[64]; - - /* Create a new input port */ - - if (_input_maximum == 1) { - snprintf (buf, sizeof (buf), _("%s/in"), _name.c_str()); - } - else { - snprintf (buf, sizeof (buf), _("%s/in %u"), _name.c_str(), find_input_port_hole()); - } - - try { - if ((port = _session.engine().register_input_port (_default_type, buf)) == 0) { - error << string_compose(_("IO: cannot register input port %1"), buf) << endmsg; - return -1; - } - } + const XMLProperty* prop; + XMLNodeConstIterator iter; + LocaleGuard lg (X_("POSIX")); - catch (AudioEngine::PortRegistrationFailure& err) { - setup_peak_meters (); - reset_panner (); - /* pass it on */ - throw AudioEngine::PortRegistrationFailure(); - } - - _inputs.push_back (port); - ++_ninputs; - in_changed = true; - } + /* force use of non-localized representation of decimal point, + since we use it a lot in XML files and so forth. + */ - /* create any necessary new ports */ - - while (_noutputs < nout) { - - char buf[64]; - - /* Create a new output port */ - - if (_output_maximum == 1) { - snprintf (buf, sizeof (buf), _("%s/out"), _name.c_str()); - } else { - snprintf (buf, sizeof (buf), _("%s/out %u"), _name.c_str(), find_output_port_hole()); - } - - try { - if ((port = _session.engine().register_output_port (_default_type, buf)) == 0) { - error << string_compose(_("IO: cannot register output port %1"), buf) << endmsg; - return -1; - } - } - - catch (AudioEngine::PortRegistrationFailure& err) { - setup_peak_meters (); - reset_panner (); - /* pass it on */ - throw AudioEngine::PortRegistrationFailure (); - } - - _outputs.push_back (port); - ++_noutputs; - out_changed = true; - } - - if (clear) { - - /* disconnect all existing ports so that we get a fresh start */ - - for (vector::iterator i = _inputs.begin(); i != _inputs.end(); ++i) { - _session.engine().disconnect (*i); - } - - for (vector::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - _session.engine().disconnect (*i); - } - } - - if (in_changed || out_changed) { - setup_peak_meters (); - reset_panner (); - } + if (node.name() != state_node_name) { + error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg; + return -1; } - if (out_changed) { - sort (_outputs.begin(), _outputs.end(), sort_ports_by_name); - drop_output_connection (); - output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ - } - - if (in_changed) { - sort (_inputs.begin(), _inputs.end(), sort_ports_by_name); - drop_input_connection (); - input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ + if ((prop = node.property ("name")) != 0) { + set_name (prop->value()); } - if (in_changed || out_changed) { - MoreOutputs (max (_noutputs, _ninputs)); /* EMIT SIGNAL */ - _session.set_dirty (); + if ((prop = node.property (X_("default-type"))) != 0) { + _default_type = DataType(prop->value()); + assert(_default_type != DataType::NIL); } - return 0; -} + set_id (node); -int -IO::ensure_inputs (uint32_t n, bool clear, bool lockit, void* src) -{ - bool changed = false; + _direction = in ? Input : Output; - if (_input_maximum >= 0) { - n = min (_input_maximum, (int) n); - - if (n == _ninputs && !clear) { - return 0; - } - } - - if (lockit) { - BLOCK_PROCESS_CALLBACK (); - Glib::Mutex::Lock im (io_lock); - changed = ensure_inputs_locked (n, clear, src); - } else { - changed = ensure_inputs_locked (n, clear, src); - } - - if (changed) { - input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ - _session.set_dirty (); + if (create_ports (node, version)) { + return -1; } - return 0; -} - -bool -IO::ensure_outputs_locked (uint32_t n, bool clear, void* src) -{ - Port* output_port; - bool changed = false; - bool reduced = false; - bool need_pan_reset; + if (connecting_legal) { - if (_noutputs == n) { - need_pan_reset = false; - } else { - need_pan_reset = true; - } - - /* remove unused ports */ - - while (_noutputs > n) { - - _session.engine().unregister_port (_outputs.back()); - _outputs.pop_back(); - _noutputs--; - reduced = true; - changed = true; - } - - /* create any necessary new ports */ - - while (_noutputs < n) { - - char buf[64]; - - /* Create a new output port */ - - if (_output_maximum == 1) { - snprintf (buf, sizeof (buf), _("%s/out"), _name.c_str()); - } else { - snprintf (buf, sizeof (buf), _("%s/out %u"), _name.c_str(), find_output_port_hole()); - } - - if ((output_port = _session.engine().register_output_port (_default_type, buf)) == 0) { - error << string_compose(_("IO: cannot register output port %1"), buf) << endmsg; + if (make_connections_2X (node, version, in)) { return -1; } - - _outputs.push_back (output_port); - sort (_outputs.begin(), _outputs.end(), sort_ports_by_name); - ++_noutputs; - changed = true; - setup_peak_meters (); - - if (need_pan_reset) { - reset_panner (); - } - } - - if (changed) { - drop_output_connection (); - MoreOutputs (_noutputs); /* EMIT SIGNAL */ - _session.set_dirty (); - } - - if (clear) { - /* disconnect all existing ports so that we get a fresh start */ - - for (vector::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - _session.engine().disconnect (*i); - } - } - - return changed; -} - -int -IO::ensure_outputs (uint32_t n, bool clear, bool lockit, void* src) -{ - bool changed = false; - - if (_output_maximum >= 0) { - n = min (_output_maximum, (int) n); - if (n == _noutputs && !clear) { - return 0; - } - } - /* XXX caller should hold io_lock, but generally doesn't */ - - if (lockit) { - BLOCK_PROCESS_CALLBACK (); - Glib::Mutex::Lock im (io_lock); - changed = ensure_outputs_locked (n, clear, src); } else { - changed = ensure_outputs_locked (n, clear, src); - } - if (changed) { - output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ + pending_state_node = new XMLNode (node); + pending_state_node_version = version; + pending_state_node_in = in; + ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this)); } return 0; } -gain_t -IO::effective_gain () const -{ - if (gain_automation_playback()) { - return _effective_gain; - } else { - return _desired_gain; - } -} - -void -IO::reset_panner () -{ - if (panners_legal) { - if (!no_panner_reset) { - _panner->reset (_noutputs, pans_required()); - } - } else { - panner_legal_c.disconnect (); - panner_legal_c = PannersLegal.connect (mem_fun (*this, &IO::panners_became_legal)); - } -} - int -IO::panners_became_legal () -{ - _panner->reset (_noutputs, pans_required()); - _panner->load (); // automation - panner_legal_c.disconnect (); - return 0; -} - -void -IO::defer_pan_reset () -{ - no_panner_reset = true; -} - -void -IO::allow_pan_reset () -{ - no_panner_reset = false; - reset_panner (); -} - - -XMLNode& -IO::get_state (void) -{ - return state (true); -} - -XMLNode& -IO::state (bool full_state) -{ - XMLNode* node = new XMLNode (state_node_name); - char buf[64]; - string str; - bool need_ins = true; - bool need_outs = true; - LocaleGuard lg (X_("POSIX")); - Glib::Mutex::Lock lm (io_lock); - - node->add_property("name", _name); - id().print (buf, sizeof (buf)); - node->add_property("id", buf); - - str = ""; - - if (_input_connection) { - node->add_property ("input-connection", _input_connection->name()); - need_ins = false; - } - - if (_output_connection) { - node->add_property ("output-connection", _output_connection->name()); - need_outs = false; - } - - if (need_ins) { - for (vector::iterator i = _inputs.begin(); i != _inputs.end(); ++i) { - - const char **connections = (*i)->get_connections(); - - if (connections && connections[0]) { - str += '{'; - - for (int n = 0; connections && connections[n]; ++n) { - if (n) { - str += ','; - } - - /* if its a connection to our own port, - return only the port name, not the - whole thing. this allows connections - to be re-established even when our - client name is different. - */ - - str += _session.engine().make_port_name_relative (connections[n]); - } - - str += '}'; - - free (connections); - } - else { - str += "{}"; - } - } - - node->add_property ("inputs", str); - } - - if (need_outs) { - str = ""; - - for (vector::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - - const char **connections = (*i)->get_connections(); - - if (connections && connections[0]) { - - str += '{'; - - for (int n = 0; connections[n]; ++n) { - if (n) { - str += ','; - } - - str += _session.engine().make_port_name_relative (connections[n]); - } - - str += '}'; - - free (connections); - } - else { - str += "{}"; - } - } - - node->add_property ("outputs", str); - } - - node->add_child_nocopy (_panner->state (full_state)); - node->add_child_nocopy (_gain_control.get_state ()); - - snprintf (buf, sizeof(buf), "%2.12f", gain()); - node->add_property ("gain", buf); - - snprintf (buf, sizeof(buf)-1, "%d,%d,%d,%d", - _input_minimum, - _input_maximum, - _output_minimum, - _output_maximum); - - node->add_property ("iolimits", buf); +IO::connecting_became_legal () +{ + int ret; - /* automation */ + assert (pending_state_node); - if (full_state) { + connection_legal_c.disconnect (); - XMLNode* autonode = new XMLNode (X_("Automation")); - autonode->add_child_nocopy (get_automation_state()); - node->add_child_nocopy (*autonode); + ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in); - snprintf (buf, sizeof (buf), "0x%x", (int) _gain_automation_curve.automation_state()); - } else { - /* never store anything except Off for automation state in a template */ - snprintf (buf, sizeof (buf), "0x%x", ARDOUR::Off); - } + delete pending_state_node; + pending_state_node = 0; - return *node; + return ret; } -int -IO::set_state (const XMLNode& node) +boost::shared_ptr +IO::find_possible_bundle (const string &desired_name) { - const XMLProperty* prop; - XMLNodeConstIterator iter; - LocaleGuard lg (X_("POSIX")); + static const string digits = "0123456789"; + const string &default_name = (_direction == Input ? _("in") : _("out")); + const string &bundle_type_name = (_direction == Input ? _("input") : _("output")); - /* force use of non-localized representation of decimal point, - since we use it a lot in XML files and so forth. - */ + boost::shared_ptr c = _session.bundle_by_name (desired_name); - if (node.name() != state_node_name) { - error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg; - return -1; - } + if (!c) { + int bundle_number, mask; + string possible_name; + bool stereo = false; + string::size_type last_non_digit_pos; - if ((prop = node.property ("name")) != 0) { - _name = prop->value(); - /* used to set panner name with this, but no more */ - } + error << string_compose(_("Unknown bundle \"%1\" listed for %2 of %3"), desired_name, bundle_type_name, _name) + << endmsg; - if ((prop = node.property ("id")) != 0) { - _id = prop->value (); - } + // find numeric suffix of desired name + bundle_number = 0; - if ((prop = node.property ("iolimits")) != 0) { - sscanf (prop->value().c_str(), "%d,%d,%d,%d", - &_input_minimum, - &_input_maximum, - &_output_minimum, - &_output_maximum); - } - - if ((prop = node.property ("gain")) != 0) { - set_gain (atof (prop->value().c_str()), this); - _gain = _desired_gain; - } + last_non_digit_pos = desired_name.find_last_not_of(digits); - if ((prop = node.property ("automation-state")) != 0 || (prop = node.property ("automation-style")) != 0) { - /* old school automation handling */ - } + if (last_non_digit_pos != string::npos) { + stringstream s; + s << desired_name.substr(last_non_digit_pos); + s >> bundle_number; + } - for (iter = node.children().begin(); iter != node.children().end(); ++iter) { + // see if it's a stereo connection e.g. "in 3+4" - if ((*iter)->name() == "Panner") { - if (_panner == 0) { - _panner = new Panner (_name, _session); - } - _panner->set_state (**iter); - } + if (last_non_digit_pos > 1 && desired_name[last_non_digit_pos] == '+') { + string::size_type left_last_non_digit_pos; - if ((*iter)->name() == X_("Automation")) { + left_last_non_digit_pos = desired_name.find_last_not_of(digits, last_non_digit_pos-1); - set_automation_state (*(*iter)->children().front()); - } + if (left_last_non_digit_pos != string::npos) { + int left_bundle_number = 0; + stringstream s; + s << desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1); + s >> left_bundle_number; - if ((*iter)->name() == X_("controllable")) { - if ((prop = (*iter)->property("name")) != 0 && prop->value() == "gaincontrol") { - _gain_control.set_state (**iter); + if (left_bundle_number > 0 && left_bundle_number + 1 == bundle_number) { + bundle_number--; + stereo = true; + } } } - } - if (ports_legal) { + // make 0-based + if (bundle_number) + bundle_number--; - if (create_ports (node)) { - return -1; - } + // find highest set bit + mask = 1; + while ((mask <= bundle_number) && (mask <<= 1)) {} - } else { + // "wrap" bundle number into largest possible power of 2 + // that works... - port_legal_c = PortsLegal.connect (mem_fun (*this, &IO::ports_became_legal)); - } + while (mask) { - if (panners_legal) { - reset_panner (); - } else { - panner_legal_c = PannersLegal.connect (mem_fun (*this, &IO::panners_became_legal)); - } + if (bundle_number & mask) { + bundle_number &= ~mask; - if (connecting_legal) { + stringstream s; + s << default_name << " " << bundle_number + 1; - if (make_connections (node)) { - return -1; - } + if (stereo) { + s << "+" << bundle_number + 2; + } - } else { - - connection_legal_c = ConnectingLegal.connect (mem_fun (*this, &IO::connecting_became_legal)); - } + possible_name = s.str(); + + if ((c = _session.bundle_by_name (possible_name)) != 0) { + break; + } + } + mask >>= 1; + } + if (c) { + info << string_compose (_("Bundle %1 was not available - \"%2\" used instead"), desired_name, possible_name) + << endmsg; + } else { + error << string_compose(_("No %1 bundles available as a replacement"), bundle_type_name) + << endmsg; + } - if (!ports_legal || !connecting_legal) { - pending_state_node = new XMLNode (node); } - last_automation_snapshot = 0; + return c; - return 0; } int -IO::set_automation_state (const XMLNode& node) +IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boost::shared_ptr& /*c*/) { - return _gain_automation_curve.set_state (node); -} + XMLProperty const * prop; + XMLNodeList children = node.children (); -XMLNode& -IO::get_automation_state () -{ - return (_gain_automation_curve.get_state ()); + uint32_t n_audio = 0; + + for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) { + + if ((prop = node.property ("inputs")) != 0 && _direction == Input) { + n_audio = count (prop->value().begin(), prop->value().end(), '{'); + } else if ((prop = node.property ("input-connection")) != 0 && _direction == Input) { + n_audio = 1; + } else if ((prop = node.property ("outputs")) != 0 && _direction == Output) { + n_audio = count (prop->value().begin(), prop->value().end(), '{'); + } else if ((prop = node.property ("output-connection")) != 0 && _direction == Output) { + n_audio = 2; + } + } + + ChanCount cnt; + cnt.set_audio (n_audio); + n = ChanCount::max (n, cnt); + + return 0; } int -IO::load_automation (string path) +IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr& c) { - string fullpath; - ifstream in; - char line[128]; - uint32_t linecnt = 0; - float version; - LocaleGuard lg (X_("POSIX")); - - fullpath = _session.automation_dir(); - fullpath += path; + if (version < 3000) { + return get_port_counts_2X (node, version, n, c); + } - in.open (fullpath.c_str()); + XMLProperty const * prop; + XMLNodeConstIterator iter; + uint32_t n_audio = 0; + uint32_t n_midi = 0; + ChanCount cnt; - if (!in) { - fullpath = _session.automation_dir(); - fullpath += _session.snap_name(); - fullpath += '-'; - fullpath += path; + n = n_ports(); - in.open (fullpath.c_str()); + if ((prop = node.property ("connection")) != 0) { - if (!in) { - error << string_compose(_("%1: cannot open automation event file \"%2\""), _name, fullpath) << endmsg; - return -1; + if ((c = find_possible_bundle (prop->value())) != 0) { + n = ChanCount::max (n, c->nchannels()); } + return 0; } - clear_automation (); - - while (in.getline (line, sizeof(line), '\n')) { - char type; - jack_nframes_t when; - double value; + for (iter = node.children().begin(); iter != node.children().end(); ++iter) { - if (++linecnt == 1) { - if (memcmp (line, "version", 7) == 0) { - if (sscanf (line, "version %f", &version) != 1) { - error << string_compose(_("badly formed version number in automation event file \"%1\""), path) << endmsg; - return -1; - } + if ((*iter)->name() == X_("Bundle")) { + if ((c = find_possible_bundle (prop->value())) != 0) { + n = ChanCount::max (n, c->nchannels()); + return 0; } else { - error << string_compose(_("no version information in automation event file \"%1\""), path) << endmsg; return -1; } - - continue; - } - - if (sscanf (line, "%c %" PRIu32 " %lf", &type, &when, &value) != 3) { - warning << string_compose(_("badly formatted automation event record at line %1 of %2 (ignored)"), linecnt, path) << endmsg; - continue; } - switch (type) { - case 'g': - _gain_automation_curve.fast_simple_add (when, value); - break; - - case 's': - break; - - case 'm': - break; + if ((*iter)->name() == X_("Port")) { + prop = (*iter)->property (X_("type")); - case 'p': - /* older (pre-1.0) versions of ardour used this */ - break; + if (!prop) { + continue; + } - default: - warning << _("dubious automation event found (and ignored)") << endmsg; + if (prop->value() == X_("audio")) { + cnt.set_audio (++n_audio); + } else if (prop->value() == X_("midi")) { + cnt.set_midi (++n_midi); + } } } + n = ChanCount::max (n, cnt); return 0; } int -IO::connecting_became_legal () +IO::create_ports (const XMLNode& node, int version) { - int ret; - - if (pending_state_node == 0) { - fatal << _("IO::connecting_became_legal() called without a pending state node") << endmsg; - /*NOTREACHED*/ - return -1; - } + ChanCount n; + boost::shared_ptr c; - connection_legal_c.disconnect (); + get_port_counts (node, version, n, c); - ret = make_connections (*pending_state_node); + { + Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ()); - if (ports_legal) { - delete pending_state_node; - pending_state_node = 0; + if (ensure_ports (n, true, this)) { + error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg; + return -1; + } } - return ret; + /* XXX use c */ + + return 0; } + int -IO::ports_became_legal () +IO::make_connections (const XMLNode& node, int version, bool in) { - int ret; - - if (pending_state_node == 0) { - fatal << _("IO::ports_became_legal() called without a pending state node") << endmsg; - /*NOTREACHED*/ - return -1; + if (version < 3000) { + return make_connections_2X (node, version, in); } - port_legal_c.disconnect (); - - ret = create_ports (*pending_state_node); + const XMLProperty* prop; - if (connecting_legal) { - delete pending_state_node; - pending_state_node = 0; - } + for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) { - return ret; -} + if ((*i)->name() == "Bundle") { + XMLProperty const * prop = (*i)->property ("name"); + if (prop) { + boost::shared_ptr b = find_possible_bundle (prop->value()); + if (b) { + connect_ports_to_bundle (b, true, this); + } + } -int -IO::create_ports (const XMLNode& node) -{ - const XMLProperty* prop; - int num_inputs = 0; - int num_outputs = 0; + return 0; + } - if ((prop = node.property ("input-connection")) != 0) { + if ((*i)->name() == "Port") { - Connection* c = _session.connection_by_name (prop->value()); - - if (c == 0) { - error << string_compose(_("Unknown connection \"%1\" listed for input of %2"), prop->value(), _name) << endmsg; + prop = (*i)->property (X_("name")); - if ((c = _session.connection_by_name (_("in 1"))) == 0) { - error << _("No input connections available as a replacement") - << endmsg; - return -1; - } else { - info << string_compose (_("Connection %1 was not available - \"in 1\" used instead"), prop->value()) - << endmsg; + if (!prop) { + continue; } - } - num_inputs = c->nports(); + boost::shared_ptr p = port_by_name (prop->value()); - } else if ((prop = node.property ("inputs")) != 0) { + if (p) { + for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) { - num_inputs = count (prop->value().begin(), prop->value().end(), '{'); - } - - if ((prop = node.property ("output-connection")) != 0) { - Connection* c = _session.connection_by_name (prop->value()); + XMLNode* cnode = (*c); - if (c == 0) { - error << string_compose(_("Unknown connection \"%1\" listed for output of %2"), prop->value(), _name) << endmsg; + if (cnode->name() != X_("Connection")) { + continue; + } - if ((c = _session.connection_by_name (_("out 1"))) == 0) { - error << _("No output connections available as a replacement") - << endmsg; - return -1; - } else { - info << string_compose (_("Connection %1 was not available - \"out 1\" used instead"), prop->value()) - << endmsg; - } - } + if ((prop = cnode->property (X_("other"))) == 0) { + continue; + } - num_outputs = c->nports (); - - } else if ((prop = node.property ("outputs")) != 0) { - num_outputs = count (prop->value().begin(), prop->value().end(), '{'); + if (prop) { + connect (p, prop->value(), this); + } + } + } + } } - no_panner_reset = true; + return 0; +} - if (ensure_io (num_inputs, num_outputs, true, this)) { - error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg; - return -1; - } +void +IO::prepare_for_reset (XMLNode& node, const std::string& name) +{ + /* reset name */ + node.add_property ("name", name); + + /* now find connections and reset the name of the port + in one so that when we re-use it it will match + the name of the thing we're applying it to. + */ - no_panner_reset = false; + XMLProperty* prop; + XMLNodeList children = node.children(); - set_deferred_state (); + for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) { - PortsCreated(); - return 0; + if ((*i)->name() == "Port") { + + prop = (*i)->property (X_("name")); + + if (prop) { + string new_name; + string old = prop->value(); + string::size_type slash = old.find ('/'); + + if (slash != string::npos) { + /* port name is of form: / */ + + new_name = name; + new_name += old.substr (old.find ('/')); + + prop->set_value (new_name); + } + } + } + } } int -IO::make_connections (const XMLNode& node) +IO::make_connections_2X (const XMLNode& node, int /*version*/, bool in) { const XMLProperty* prop; - if ((prop = node.property ("input-connection")) != 0) { - Connection* c = _session.connection_by_name (prop->value()); - - if (c == 0) { - error << string_compose(_("Unknown connection \"%1\" listed for input of %2"), prop->value(), _name) << endmsg; + /* XXX: bundles ("connections" as was) */ + + if ((prop = node.property ("inputs")) != 0 && in) { + + string::size_type ostart = 0; + string::size_type start = 0; + string::size_type end = 0; + int i = 0; + int n; + vector ports; - if ((c = _session.connection_by_name (_("in 1"))) == 0) { - error << _("No input connections available as a replacement") - << endmsg; + string const str = prop->value (); + + while ((start = str.find_first_of ('{', ostart)) != string::npos) { + start += 1; + + if ((end = str.find_first_of ('}', start)) == string::npos) { + error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg; return -1; - } else { - info << string_compose (_("Connection %1 was not available - \"in 1\" used instead"), prop->value()) - << endmsg; } - } - - use_input_connection (*c, this); - } else if ((prop = node.property ("inputs")) != 0) { - if (set_inputs (prop->value())) { - error << string_compose(_("improper input channel list in XML node (%1)"), prop->value()) << endmsg; - return -1; - } - } - - if ((prop = node.property ("output-connection")) != 0) { - Connection* c = _session.connection_by_name (prop->value()); - - if (c == 0) { - error << string_compose(_("Unknown connection \"%1\" listed for output of %2"), prop->value(), _name) << endmsg; + if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) { + error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg; - if ((c = _session.connection_by_name (_("out 1"))) == 0) { - error << _("No output connections available as a replacement") - << endmsg; return -1; - } else { - info << string_compose (_("Connection %1 was not available - \"out 1\" used instead"), prop->value()) - << endmsg; + + } else if (n > 0) { + + + for (int x = 0; x < n; ++x) { + /* XXX: this is a bit of a hack; need to check if it's always valid */ + string::size_type const p = ports[x].find ("/out"); + if (p != string::npos) { + ports[x].replace (p, 4, "/audio_out"); + } + nth(i)->connect (ports[x]); + } } - } - use_output_connection (*c, this); - - } else if ((prop = node.property ("outputs")) != 0) { - if (set_outputs (prop->value())) { - error << string_compose(_("improper output channel list in XML node (%1)"), prop->value()) << endmsg; - return -1; + ostart = end+1; + i++; } - } - - return 0; -} -int -IO::set_inputs (const string& str) -{ - vector ports; - int i; - int n; - uint32_t nports; - - if ((nports = count (str.begin(), str.end(), '{')) == 0) { - return 0; } - if (ensure_inputs (nports, true, true, this)) { - return -1; - } + if ((prop = node.property ("outputs")) != 0 && !in) { - string::size_type start, end, ostart; + string::size_type ostart = 0; + string::size_type start = 0; + string::size_type end = 0; + int i = 0; + int n; + vector ports; - ostart = 0; - start = 0; - end = 0; - i = 0; + string const str = prop->value (); - while ((start = str.find_first_of ('{', ostart)) != string::npos) { - start += 1; + while ((start = str.find_first_of ('{', ostart)) != string::npos) { + start += 1; - if ((end = str.find_first_of ('}', start)) == string::npos) { - error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg; - return -1; - } + if ((end = str.find_first_of ('}', start)) == string::npos) { + error << string_compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg; + return -1; + } - if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) { - error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg; + if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) { + error << string_compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg; - return -1; - - } else if (n > 0) { + return -1; - for (int x = 0; x < n; ++x) { - connect_input (input (i), ports[x], this); + } else if (n > 0) { + + for (int x = 0; x < n; ++x) { + /* XXX: this is a bit of a hack; need to check if it's always valid */ + string::size_type const p = ports[x].find ("/in"); + if (p != string::npos) { + ports[x].replace (p, 3, "/audio_in"); + } + nth(i)->connect (ports[x]); + } } - } - ostart = end+1; - i++; + ostart = end+1; + i++; + } } return 0; } int -IO::set_outputs (const string& str) +IO::set_ports (const string& str) { vector ports; int i; int n; uint32_t nports; - + if ((nports = count (str.begin(), str.end(), '{')) == 0) { return 0; } - if (ensure_outputs (nports, true, true, this)) { - return -1; + { + Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ()); + + // FIXME: audio-only + if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, this)) { + return -1; + } } string::size_type start, end, ostart; @@ -2024,19 +1125,19 @@ IO::set_outputs (const string& str) start += 1; if ((end = str.find_first_of ('}', start)) == string::npos) { - error << string_compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg; + error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg; return -1; } if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) { - error << string_compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg; + error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg; return -1; - + } else if (n > 0) { for (int x = 0; x < n; ++x) { - connect_output (output (i), ports[x], this); + connect (nth (i), ports[x], this); } } @@ -2065,7 +1166,7 @@ IO::parse_io_string (const string& str, vector& ports) ports.push_back (str.substr (opos, pos - opos)); opos = pos + 1; } - + if (opos < str.length()) { ports.push_back (str.substr(opos)); } @@ -2086,274 +1187,136 @@ IO::parse_gain_string (const string& str, vector& ports) ports.push_back (str.substr (opos, pos - opos)); opos = pos + 1; } - + if (opos < str.length()) { ports.push_back (str.substr(opos)); } - return ports.size(); -} - -int -IO::set_name (string name, void* src) -{ - if (name == _name) { - return 0; - } - - /* replace all colons in the name. i wish we didn't have to do this */ - - if (replace_all (name, ":", "-")) { - warning << _("you cannot use colons to name objects with I/O connections") << endmsg; - } - - for (vector::iterator i = _inputs.begin(); i != _inputs.end(); ++i) { - string current_name = (*i)->short_name(); - current_name.replace (current_name.find (_name), _name.length(), name); - (*i)->set_name (current_name); - } - - for (vector::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - string current_name = (*i)->short_name(); - current_name.replace (current_name.find (_name), _name.length(), name); - (*i)->set_name (current_name); - } - - _name = name; - name_changed (src); /* EMIT SIGNAL */ - - return 0; -} - -void -IO::set_input_minimum (int n) -{ - _input_minimum = n; -} - -void -IO::set_input_maximum (int n) -{ - _input_maximum = n; -} - -void -IO::set_output_minimum (int n) -{ - _output_minimum = n; -} - -void -IO::set_output_maximum (int n) -{ - _output_maximum = n; + return ports.size(); } -void -IO::set_port_latency (nframes_t nframes) +bool +IO::set_name (const string& requested_name) { - Glib::Mutex::Lock lm (io_lock); + string name = requested_name; - for (vector::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - (*i)->set_latency (nframes); + if (_name == name) { + return true; } -} - -nframes_t -IO::output_latency () const -{ - nframes_t max_latency; - nframes_t latency; - max_latency = 0; + /* replace all colons in the name. i wish we didn't have to do this */ - /* io lock not taken - must be protected by other means */ + replace_all (name, ":", "-"); - for (vector::const_iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - if ((latency = _session.engine().get_port_total_latency (*(*i))) > max_latency) { - max_latency = latency; - } + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { + string current_name = i->name(); + current_name.replace (current_name.find (_name), _name.val().length(), name); + i->set_name (current_name); } - return max_latency; + bool const r = SessionObject::set_name (name); + + setup_bundle (); + + return r; } -nframes_t -IO::input_latency () const +framecnt_t +IO::latency () const { - nframes_t max_latency; - nframes_t latency; + framecnt_t max_latency; + framecnt_t latency; max_latency = 0; /* io lock not taken - must be protected by other means */ - for (vector::const_iterator i = _inputs.begin(); i != _inputs.end(); ++i) { - if ((latency = _session.engine().get_port_total_latency (*(*i))) > max_latency) { + for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) { + if ((latency = i->private_latency_range (_direction == Output).max) > max_latency) { + DEBUG_TRACE (DEBUG::Latency, string_compose ("port %1 has %2 latency of %3 - use\n", + name(), + ((_direction == Output) ? "PLAYBACK" : "CAPTURE"), + latency)); max_latency = latency; } } + DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: max %4 latency from %2 ports = %3\n", + name(), _ports.num_ports(), max_latency, + ((_direction == Output) ? "PLAYBACK" : "CAPTURE"))); return max_latency; } int -IO::use_input_connection (Connection& c, void* src) +IO::connect_ports_to_bundle (boost::shared_ptr c, bool exclusive, void* src) { - uint32_t limit; + BLOCK_PROCESS_CALLBACK (); { - BLOCK_PROCESS_CALLBACK (); - Glib::Mutex::Lock lm2 (io_lock); - - limit = c.nports(); - - drop_input_connection (); - - if (ensure_inputs (limit, false, false, src)) { - return -1; - } + Glib::Threads::Mutex::Lock lm2 (io_lock); - /* first pass: check the current state to see what's correctly - connected, and drop anything that we don't want. - */ - - for (uint32_t n = 0; n < limit; ++n) { - const Connection::PortList& pl = c.port_connections (n); - - for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) { - - if (!_inputs[n]->connected_to ((*i))) { - - /* clear any existing connections */ - - _session.engine().disconnect (_inputs[n]); - - } else if (_inputs[n]->connected() > 1) { - - /* OK, it is connected to the port we want, - but its also connected to other ports. - Change that situation. - */ - - /* XXX could be optimized to not drop - the one we want. - */ - - _session.engine().disconnect (_inputs[n]); - - } + if (exclusive) { + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { + i->disconnect_all (); } } - - /* second pass: connect all requested ports where necessary */ - - for (uint32_t n = 0; n < limit; ++n) { - const Connection::PortList& pl = c.port_connections (n); - - for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) { - - if (!_inputs[n]->connected_to ((*i))) { - - if (_session.engine().connect (*i, _inputs[n]->name())) { - return -1; - } - } - + + c->connect (_bundle, _session.engine()); + + /* If this is a UserBundle, make a note of what we've done */ + + boost::shared_ptr ub = boost::dynamic_pointer_cast (c); + if (ub) { + + /* See if we already know about this one */ + std::vector::iterator i = _bundles_connected.begin(); + while (i != _bundles_connected.end() && (*i)->bundle != ub) { + ++i; + } + + if (i == _bundles_connected.end()) { + /* We don't, so make a note */ + _bundles_connected.push_back (new UserBundleInfo (this, ub)); } } - - _input_connection = &c; - - input_connection_configuration_connection = c.ConfigurationChanged.connect - (mem_fun (*this, &IO::input_connection_configuration_changed)); - input_connection_connection_connection = c.ConnectionsChanged.connect - (mem_fun (*this, &IO::input_connection_connection_changed)); } - input_changed (IOChange (ConfigurationChanged|ConnectionsChanged), src); /* EMIT SIGNAL */ + changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */ return 0; } int -IO::use_output_connection (Connection& c, void* src) +IO::disconnect_ports_from_bundle (boost::shared_ptr c, void* src) { - uint32_t limit; + BLOCK_PROCESS_CALLBACK (); { - BLOCK_PROCESS_CALLBACK (); - Glib::Mutex::Lock lm2 (io_lock); + Glib::Threads::Mutex::Lock lm2 (io_lock); - limit = c.nports(); - - drop_output_connection (); - - if (ensure_outputs (limit, false, false, src)) { - return -1; - } - - /* first pass: check the current state to see what's correctly - connected, and drop anything that we don't want. - */ - - for (uint32_t n = 0; n < limit; ++n) { - - const Connection::PortList& pl = c.port_connections (n); - - for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) { - - if (!_outputs[n]->connected_to ((*i))) { + c->disconnect (_bundle, _session.engine()); - /* clear any existing connections */ + /* If this is a UserBundle, make a note of what we've done */ - _session.engine().disconnect (_outputs[n]); + boost::shared_ptr ub = boost::dynamic_pointer_cast (c); + if (ub) { - } else if (_outputs[n]->connected() > 1) { - - /* OK, it is connected to the port we want, - but its also connected to other ports. - Change that situation. - */ - - /* XXX could be optimized to not drop - the one we want. - */ - - _session.engine().disconnect (_outputs[n]); - } + std::vector::iterator i = _bundles_connected.begin(); + while (i != _bundles_connected.end() && (*i)->bundle != ub) { + ++i; } - } - /* second pass: connect all requested ports where necessary */ - - for (uint32_t n = 0; n < limit; ++n) { - - const Connection::PortList& pl = c.port_connections (n); - - for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) { - - if (!_outputs[n]->connected_to ((*i))) { - - if (_session.engine().connect (_outputs[n]->name(), *i)) { - return -1; - } - } + if (i != _bundles_connected.end()) { + delete *i; + _bundles_connected.erase (i); } } - - _output_connection = &c; - - output_connection_configuration_connection = c.ConfigurationChanged.connect - (mem_fun (*this, &IO::output_connection_configuration_changed)); - output_connection_connection_connection = c.ConnectionsChanged.connect - (mem_fun (*this, &IO::output_connection_connection_changed)); } - output_changed (IOChange (ConnectionsChanged|ConfigurationChanged), src); /* EMIT SIGNAL */ - + changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */ return 0; } + int IO::disable_connecting () { @@ -2364,365 +1327,414 @@ IO::disable_connecting () int IO::enable_connecting () { + Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock()); connecting_legal = true; - return ConnectingLegal (); + boost::optional r = ConnectingLegal (); + return r.get_value_or (0); } -int -IO::disable_ports () +void +IO::bundle_changed (Bundle::Change /*c*/) { - ports_legal = false; - return 0; + /* XXX */ +// connect_input_ports_to_bundle (_input_bundle, this); } -int -IO::enable_ports () -{ - ports_legal = true; - return PortsLegal (); -} -int -IO::disable_panners (void) +string +IO::build_legal_port_name (DataType type) { - panners_legal = false; - return 0; -} + const int name_size = AudioEngine::instance()->port_name_size(); + int limit; + string suffix; -int -IO::reset_panners () -{ - panners_legal = true; - return PannersLegal (); -} + if (type == DataType::AUDIO) { + suffix = X_("audio"); + } else if (type == DataType::MIDI) { + suffix = X_("midi"); + } else { + throw unknown_type(); + } -void -IO::input_connection_connection_changed (int ignored) -{ - use_input_connection (*_input_connection, this); -} + /* note that if "in" or "out" are translated it will break a session + across locale switches because a port's connection list will + show (old) translated names, but the current port name will + use the (new) translated name. + */ -void -IO::input_connection_configuration_changed () -{ - use_input_connection (*_input_connection, this); -} + if (_sendish) { + if (_direction == Input) { + suffix += X_("_return"); + } else { + suffix += X_("_send"); + } + } else { + if (_direction == Input) { + suffix += X_("_in"); + } else { + suffix += X_("_out"); + } + } -void -IO::output_connection_connection_changed (int ignored) -{ - use_output_connection (*_output_connection, this); -} + // allow up to 4 digits for the output port number, plus the slash, suffix and extra space -void -IO::output_connection_configuration_changed () -{ - use_output_connection (*_output_connection, this); -} + limit = name_size - AudioEngine::instance()->my_name().length() - (suffix.length() + 5); -void -IO::GainControllable::set_value (float val) -{ - io.set_gain (direct_control_to_gain (val), this); -} + std::vector buf1(name_size+1); + std::vector buf2(name_size+1); -float -IO::GainControllable::get_value (void) const -{ - return direct_gain_to_control (io.effective_gain()); + /* colons are illegal in port names, so fix that */ + + string nom = _name.val(); + replace_all (nom, ":", ";"); + + snprintf (&buf1[0], name_size+1, ("%.*s/%s"), limit, nom.c_str(), suffix.c_str()); + + int port_number = find_port_hole (&buf1[0]); + snprintf (&buf2[0], name_size+1, "%s %d", &buf1[0], port_number); + + return string (&buf2[0]); } -void -IO::reset_peak_meters () +int32_t +IO::find_port_hole (const char* base) { - uint32_t limit = max (_ninputs, _noutputs); + /* CALLER MUST HOLD IO LOCK */ + + uint32_t n; - for (uint32_t i = 0; i < limit; ++i) { - _peak_power[i] = 0; + if (_ports.empty()) { + return 1; } -} -void -IO::reset_max_peak_meters () -{ - uint32_t limit = max (_ninputs, _noutputs); + /* we only allow up to 4 characters for the port number + */ - for (uint32_t i = 0; i < limit; ++i) { - _max_peak_power[i] = -INFINITY; + for (n = 1; n < 9999; ++n) { + std::vector buf (AudioEngine::instance()->port_name_size()); + PortSet::iterator i = _ports.begin(); + + snprintf (&buf[0], jack_port_name_size(), _("%s %u"), base, n); + + for ( ; i != _ports.end(); ++i) { + if (string(i->name()) == string(&buf[0])) { + break; + } + } + + if (i == _ports.end()) { + break; + } } + return n; } -void -IO::setup_peak_meters () + +boost::shared_ptr +IO::audio(uint32_t n) const { - uint32_t limit = max (_ninputs, _noutputs); + return _ports.nth_audio_port (n); - while (_peak_power.size() < limit) { - _peak_power.push_back (0); - _visible_peak_power.push_back (-INFINITY); - _max_peak_power.push_back (-INFINITY); - } } -/** - Update the peak meters. - - The meter signal lock is taken to prevent modification of the - Meter signal while updating the meters, taking the meter signal - lock prior to taking the io_lock ensures that all IO will remain - valid while metering. -*/ -void -IO::update_meters() +boost::shared_ptr +IO::midi(uint32_t n) const { - Glib::Mutex::Lock guard (m_meter_signal_lock); - - Meter(); + return _ports.nth_midi_port (n); } +/** + * Setup a bundle that describe our inputs or outputs. Also creates the bundle if necessary. + */ + void -IO::meter () +IO::setup_bundle () { - Glib::Mutex::Lock lm (io_lock); // READER: meter thread. - uint32_t limit = max (_ninputs, _noutputs); - - for (uint32_t n = 0; n < limit; ++n) { + char buf[32]; - /* XXX we should use atomic exchange here */ + if (!_bundle) { + _bundle.reset (new Bundle (_direction == Input)); + } - /* grab peak since last read */ + _bundle->suspend_signals (); - float new_peak = _peak_power[n]; - _peak_power[n] = 0; + _bundle->remove_channels (); - /* compute new visible value using falloff */ + if (_direction == Input) { + snprintf(buf, sizeof (buf), _("%s in"), _name.val().c_str()); + } else { + snprintf(buf, sizeof (buf), _("%s out"), _name.val().c_str()); + } + _bundle->set_name (buf); - if (new_peak > 0.0f) { - new_peak = coefficient_to_dB (new_peak); - } else { - new_peak = -INFINITY; - } + int c = 0; + for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) { - /* update max peak */ - - _max_peak_power[n] = max (new_peak, _max_peak_power[n]); - - - if (Config->get_meter_falloff() == 0.0f || new_peak > _visible_peak_power[n]) { - _visible_peak_power[n] = new_peak; - } else { - // do falloff, the config value is in dB/sec, we get updated at 100/sec currently (should be a var somewhere) - new_peak = _visible_peak_power[n] - (Config->get_meter_falloff() * 0.01f); - _visible_peak_power[n] = max (new_peak, -INFINITY); + uint32_t const N = _ports.count().get (*i); + for (uint32_t j = 0; j < N; ++j) { + _bundle->add_channel (bundle_channel_name (j, N, *i), *i); + _bundle->set_port (c, _session.engine().make_port_name_non_relative (_ports.port(*i, j)->name())); + ++c; } + } -} -void -IO::clear_automation () -{ - Glib::Mutex::Lock lm (automation_lock); - _gain_automation_curve.clear (); - _panner->clear_automation (); + _bundle->resume_signals (); } -void -IO::set_gain_automation_state (AutoState state) +/** @return Bundles connected to our ports */ +BundleList +IO::bundles_connected () { - bool changed = false; - - { - Glib::Mutex::Lock lm (automation_lock); + BundleList bundles; - if (state != _gain_automation_curve.automation_state()) { - changed = true; - last_automation_snapshot = 0; - _gain_automation_curve.set_automation_state (state); - - if (state != Off) { - set_gain (_gain_automation_curve.eval (_session.transport_frame()), this); - } - } + /* User bundles */ + for (std::vector::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) { + bundles.push_back ((*i)->bundle); } - if (changed) { - _session.set_dirty (); - gain_automation_state_changed (); /* EMIT SIGNAL */ + /* Session bundles */ + boost::shared_ptr b = _session.bundles (); + for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) { + if ((*i)->connected_to (_bundle, _session.engine())) { + bundles.push_back (*i); + } } -} -void -IO::set_gain_automation_style (AutoStyle style) -{ - bool changed = false; + /* Route bundles */ - { - Glib::Mutex::Lock lm (automation_lock); + boost::shared_ptr r = _session.get_routes (); - if (style != _gain_automation_curve.automation_style()) { - changed = true; - _gain_automation_curve.set_automation_style (style); + if (_direction == Input) { + for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) { + if ((*i)->output()->bundle()->connected_to (_bundle, _session.engine())) { + bundles.push_back ((*i)->output()->bundle()); + } + } + } else { + for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) { + if ((*i)->input()->bundle()->connected_to (_bundle, _session.engine())) { + bundles.push_back ((*i)->input()->bundle()); + } } } - if (changed) { - gain_automation_style_changed (); /* EMIT SIGNAL */ - } + return bundles; } -void -IO::inc_gain (gain_t factor, void *src) + + +IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr b) { - if (_desired_gain == 0.0f) - set_gain (0.000001f + (0.000001f * factor), src); - else - set_gain (_desired_gain + (_desired_gain * factor), src); + bundle = b; + b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1)); } -void -IO::set_gain (gain_t val, void *src) +std::string +IO::bundle_channel_name (uint32_t c, uint32_t n, DataType t) const { - // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05)) - if (val>1.99526231f) val=1.99526231f; + char buf[32]; - { - Glib::Mutex::Lock dm (declick_lock); - _desired_gain = val; - } + if (t == DataType::AUDIO) { - if (_session.transport_stopped()) { - _effective_gain = val; - _gain = val; - } + switch (n) { + case 1: + return _("mono"); + case 2: + return c == 0 ? _("L") : _("R"); + default: + snprintf (buf, sizeof(buf), _("%d"), (c + 1)); + return buf; + } + + } else { + + snprintf (buf, sizeof(buf), _("%d"), (c + 1)); + return buf; - gain_changed (src); - _gain_control.Changed (); /* EMIT SIGNAL */ - - if (_session.transport_stopped() && src != 0 && src != this && gain_automation_recording()) { - _gain_automation_curve.add (_session.transport_frame(), val); - } - _session.set_dirty(); + return ""; } -void -IO::start_gain_touch () +string +IO::name_from_state (const XMLNode& node) { - _gain_automation_curve.start_touch (); -} + const XMLProperty* prop; -void -IO::end_gain_touch () -{ - _gain_automation_curve.stop_touch (); + if ((prop = node.property ("name")) != 0) { + return prop->value(); + } + + return string(); } void -IO::start_pan_touch (uint32_t which) +IO::set_name_in_state (XMLNode& node, const string& new_name) { - if (which < _panner->size()) { - (*_panner)[which]->automation().start_touch(); + node.add_property (X_("name"), new_name); + XMLNodeList children = node.children (); + for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) { + if ((*i)->name() == X_("Port")) { + string const old_name = (*i)->property(X_("name"))->value(); + string const old_name_second_part = old_name.substr (old_name.find_first_of ("/") + 1); + (*i)->add_property (X_("name"), string_compose ("%1/%2", new_name, old_name_second_part)); + } } } -void -IO::end_pan_touch (uint32_t which) +bool +IO::connected () const { - if (which < _panner->size()) { - (*_panner)[which]->automation().stop_touch(); - } + /* do we have any connections at all? */ + + for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) { + if (p->connected()) { + return true; + } + } + return false; } -void -IO::automation_snapshot (nframes_t now) +bool +IO::connected_to (boost::shared_ptr other) const { - if (last_automation_snapshot > now || (now - last_automation_snapshot) > _automation_interval) { + if (!other) { + return connected (); + } + + assert (_direction != other->direction()); - if (gain_automation_recording()) { - _gain_automation_curve.rt_add (now, gain()); + uint32_t i, j; + uint32_t no = n_ports().n_total(); + uint32_t ni = other->n_ports ().n_total(); + + for (i = 0; i < no; ++i) { + for (j = 0; j < ni; ++j) { + if (nth(i)->connected_to (other->nth(j)->name())) { + return true; + } } - - _panner->snapshot (now); + } + + return false; +} - last_automation_snapshot = now; +bool +IO::connected_to (const string& str) const +{ + for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) { + if (i->connected_to (str)) { + return true; + } } + + return false; } +/** Call a processor's ::run() method, giving it our buffers + * Caller must hold process lock. + */ void -IO::transport_stopped (nframes_t frame) +IO::process_input (boost::shared_ptr proc, framepos_t start_frame, framepos_t end_frame, pframes_t nframes) { - _gain_automation_curve.reposition_for_rt_add (frame); - - if (_gain_automation_curve.automation_state() != Off) { - - /* the src=0 condition is a special signal to not propagate - automation gain changes into the mix group when locating. - */ + /* don't read the data into new buffers - just use the port buffers directly */ - set_gain (_gain_automation_curve.eval (frame), 0); + if (n_ports().n_total() == 0) { + /* We have no ports, so nothing to process */ + return; } - _panner->transport_stopped (frame); + _buffers.get_backend_port_addresses (_ports, nframes); + if (proc) { + proc->run (_buffers, start_frame, end_frame, nframes, true); + } } -int32_t -IO::find_input_port_hole () +void +IO::collect_input (BufferSet& bufs, pframes_t nframes, ChanCount offset) { - /* CALLER MUST HOLD IO LOCK */ + assert(bufs.available() >= _ports.count()); - uint32_t n; - - if (_inputs.empty()) { - return 1; + if (_ports.count() == ChanCount::ZERO) { + return; } - for (n = 1; n < UINT_MAX; ++n) { - char buf[jack_port_name_size()]; - vector::iterator i; + bufs.set_count (_ports.count()); - snprintf (buf, jack_port_name_size(), _("%s/in %u"), _name.c_str(), n); + for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { + PortSet::iterator i = _ports.begin(*t); + BufferSet::iterator b = bufs.begin(*t); - for (i = _inputs.begin(); i != _inputs.end(); ++i) { - if ((*i)->short_name() == buf) { - break; + for (uint32_t off = 0; off < offset.get(*t); ++off, ++b) { + if (b == bufs.end(*t)) { + continue; } } - if (i == _inputs.end()) { - break; + for ( ; i != _ports.end(*t); ++i, ++b) { + Buffer& bb (i->get_buffer (nframes)); + b->read_from (bb, nframes); } } - return n; } -int32_t -IO::find_output_port_hole () +void +IO::copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt_t offset) { - /* CALLER MUST HOLD IO LOCK */ + // Copy any buffers 1:1 to outputs - uint32_t n; + PortSet::iterator o = _ports.begin(type); + BufferSet::iterator i = bufs.begin(type); + BufferSet::iterator prev = i; - if (_outputs.empty()) { - return 1; + while (i != bufs.end(type) && o != _ports.end (type)) { + Buffer& port_buffer (o->get_buffer (nframes)); + port_buffer.read_from (*i, nframes, offset); + prev = i; + ++i; + ++o; } - for (n = 1; n < UINT_MAX; ++n) { - char buf[jack_port_name_size()]; - vector::iterator i; + // Copy last buffer to any extra outputs - snprintf (buf, jack_port_name_size(), _("%s/out %u"), _name.c_str(), n); + while (o != _ports.end(type)) { + Buffer& port_buffer (o->get_buffer (nframes)); + port_buffer.read_from (*prev, nframes, offset); + ++o; + } +} - for (i = _outputs.begin(); i != _outputs.end(); ++i) { - if ((*i)->short_name() == buf) { - break; - } - } +boost::shared_ptr +IO::port_by_name (const std::string& str) const +{ + /* to be called only from ::set_state() - no locking */ - if (i == _outputs.end()) { - break; + for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) { + + if (i->name() == str) { + return boost::const_pointer_cast (*i); } } - - return n; + + return boost::shared_ptr (); +} + +bool +IO::physically_connected () const +{ + for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) { + if (i->physically_connected()) { + return true; + } + } + + return false; +} + +bool +IO::has_port (boost::shared_ptr p) const +{ + Glib::Threads::Mutex::Lock lm (io_lock); + return _ports.contains (p); }