X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fio.cc;h=9da93848f0c4c45fe3a821bed10df1d6ae3122b4;hb=7b94110c545415af8437fc6631e7663ebb5c28f3;hp=b3dc2a89c9ddcbd203e87408ecf433f3d6218932;hpb=1e7bcd8b0fd451117e241bf49660684314b5757f;p=ardour.git diff --git a/libs/ardour/io.cc b/libs/ardour/io.cc index b3dc2a89c9..9da93848f0 100644 --- a/libs/ardour/io.cc +++ b/libs/ardour/io.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2000-2006 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 @@ -24,30 +24,35 @@ #include +#include #include -#include -#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 "ardour/audioengine.h" +#include "ardour/buffer.h" +#include "ardour/io.h" +#include "ardour/route.h" +#include "ardour/port.h" +#include "ardour/audio_port.h" +#include "ardour/midi_port.h" +#include "ardour/session.h" +#include "ardour/cycle_timer.h" +#include "ardour/panner.h" +#include "ardour/buffer_set.h" +#include "ardour/meter.h" +#include "ardour/amp.h" +#include "ardour/user_bundle.h" #include "i18n.h" #include /* - A bug in OS X's cmath that causes isnan() and isinf() to be + A bug in OS X's cmath that causes isnan() and isinf() to be "undeclared". the following works around that */ @@ -62,316 +67,106 @@ 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::MoreChannels; -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); -} - +sigc::signal 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), - _output_buffers(new BufferSet()), - _name (name), - _default_type(default_type), - _gain_control (X_("gaincontrol"), *this), - _gain_automation_curve (0.0, 2.0, 1.0), - _input_minimum (ChanCount::ZERO), - _input_maximum (ChanCount::INFINITE), - _output_minimum (ChanCount::ZERO), - _output_maximum (ChanCount::INFINITE) +IO::IO (Session& s, const string& name, Direction dir, DataType default_type) + : SessionObject (s, name) + , _direction (dir) + , _default_type (default_type) { - _panner = new Panner (name, _session); - _meter = new PeakMeter (_session); - - if (input_min > 0) { - _input_minimum = ChanCount(_default_type, input_min); - } - if (input_max >= 0) { - _input_maximum = ChanCount(_default_type, input_max); - } - if (output_min > 0) { - _output_minimum = ChanCount(_default_type, output_min); - } - if (output_max >= 0) { - _output_maximum = ChanCount(_default_type, output_max); - } - - _gain = 1.0; - _desired_gain = 1.0; - _input_connection = 0; - _output_connection = 0; + _active = true; pending_state_node = 0; - no_panner_reset = false; - _phase_invert = false; - deferred_state = 0; - - apply_gain_automation = false; - - last_automation_snapshot = 0; - - _gain_automation_state = Off; - _gain_automation_style = Absolute; - - { - // 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)); - } - - // Connect to our own MoreChannels signal to connect output buffers - IO::MoreChannels.connect (mem_fun (*this, &IO::attach_buffers)); - - _session.add_controllable (&_gain_control); + setup_bundles (); } IO::IO (Session& s, const XMLNode& node, DataType dt) - : _session (s), - _output_buffers(new BufferSet()), - _default_type (dt), - _gain_control (X_("gaincontrol"), *this), - _gain_automation_curve (0, 0, 0) // all reset in set_state() + : SessionObject(s, "unnamed io") + , _direction (Input) + , _default_type (dt) { - // FIXME: hack - _meter = new PeakMeter (_session); - - _panner = 0; - deferred_state = 0; - no_panner_reset = false; - _desired_gain = 1.0; - _gain = 1.0; - _input_connection = 0; - _output_connection = 0; - - apply_gain_automation = false; - - set_state (node); - - { - // 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)); - } - - // Connect to our own MoreChannels signal to connect output buffers - IO::MoreChannels.connect (mem_fun (*this, &IO::attach_buffers)); + _active = true; + pending_state_node = 0; - _session.add_controllable (&_gain_control); + set_state (node, Stateful::loading_state_version); + setup_bundles (); } IO::~IO () { - Glib::Mutex::Lock guard (m_meter_signal_lock); - Glib::Mutex::Lock lm (io_lock); - for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) { - _session.engine().unregister_port (*i); - } + BLOCK_PROCESS_CALLBACK (); - for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { _session.engine().unregister_port (*i); } - - m_meter_connection.disconnect(); - - delete _meter; - delete _panner; - delete _output_buffers; } void -IO::silence (nframes_t nframes, nframes_t offset) +IO::silence (nframes_t nframes) { /* io_lock, not taken: function must be called from Session::process() calltree */ - for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - i->get_buffer().silence (nframes, offset); - } -} - -/** Deliver bufs to the IO's Jack outputs. - * - * This function should automatically do whatever it necessary to correctly deliver bufs - * to the outputs, eg applying gain or pan or whatever else needs to be done. - */ -void -IO::deliver_output (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset) -{ - // FIXME: type specific code doesn't actually need to be here, it will go away in time - - /* ********** AUDIO ********** */ - - // Apply gain if gain automation isn't playing - if ( ! apply_gain_automation) { - - gain_t dg = _gain; // desired gain - - { - Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK); - - if (dm.locked()) { - dg = _desired_gain; - } - - } - - if (dg != _gain || dg != 1.0) - Amp::run(bufs, nframes, _gain, dg, _phase_invert); - } - - // Use the panner to distribute audio to output port buffers - if (_panner && !_panner->empty() && !_panner->bypassed()) { - _panner->distribute(bufs, output_buffers(), start_frame, end_frame, nframes, offset); - } else { - const DataType type = DataType::AUDIO; - - // Copy any audio 1:1 to outputs - assert(bufs.count().get(DataType::AUDIO) == output_buffers().count().get(DataType::AUDIO)); - BufferSet::iterator o = output_buffers().begin(type); - for (BufferSet::iterator i = bufs.begin(type); i != bufs.end(type); ++i, ++o) { - o->read_from(*i, nframes, offset); - } - } - - - /* ********** MIDI ********** */ - - // No MIDI, we're done here - if (bufs.count().get(DataType::MIDI) == 0) { - return; - } - - const DataType type = DataType::MIDI; - - // Copy any MIDI 1:1 to outputs - assert(bufs.count().get(DataType::MIDI) == output_buffers().count().get(DataType::MIDI)); - BufferSet::iterator o = output_buffers().begin(type); - for (BufferSet::iterator i = bufs.begin(type); i != bufs.end(type); ++i, ++o) { - o->read_from(*i, nframes, offset); + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { + i->get_buffer(nframes).silence (nframes); } } void -IO::collect_input (BufferSet& outs, nframes_t nframes, nframes_t offset) +IO::check_bundles_connected () { - assert(outs.available() >= n_inputs()); - - outs.set_count(n_inputs()); - - if (outs.count() == ChanCount::ZERO) - return; - - for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { - - BufferSet::iterator o = outs.begin(*t); - for (PortSet::iterator i = _inputs.begin(*t); i != _inputs.end(*t); ++i, ++o) { - o->read_from(i->get_buffer(), nframes, offset); - } - - } + check_bundles (_bundles_connected, ports()); } void -IO::just_meter_input (nframes_t start_frame, nframes_t end_frame, - nframes_t nframes, nframes_t offset) +IO::check_bundles (std::vector& list, const PortSet& ports) { - BufferSet& bufs = _session.get_scratch_buffers (n_inputs()); + std::vector new_list; - collect_input (bufs, nframes, offset); + for (std::vector::iterator i = list.begin(); i != list.end(); ++i) { - _meter->run(bufs, nframes); -} - -void -IO::drop_input_connection () -{ - _input_connection = 0; - input_connection_configuration_connection.disconnect(); - input_connection_connection_connection.disconnect(); - _session.set_dirty (); -} + uint32_t const N = i->bundle->nchannels (); -void -IO::drop_output_connection () -{ - _output_connection = 0; - output_connection_configuration_connection.disconnect(); - output_connection_connection_connection.disconnect(); - _session.set_dirty (); -} + if (_ports.num_ports (default_type()) < N) { + continue; + } -int -IO::disconnect_input (Port* our_port, string other_port, void* src) -{ - if (other_port.length() == 0 || our_port == 0) { - return 0; - } + bool ok = true; - { - BLOCK_PROCESS_CALLBACK (); - - { - Glib::Mutex::Lock lm (io_lock); - - /* check that our_port is really one of ours */ - - if ( ! _inputs.contains(our_port)) { - return -1; + 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; + } } - - /* 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; + + if (ok == false) { + break; } + } - drop_input_connection(); + if (ok) { + new_list.push_back (*i); + } else { + i->changed.disconnect (); } } - input_changed (ConnectionsChanged, src); /* EMIT SIGNAL */ - _session.set_dirty (); - - return 0; + list = new_list; } + int -IO::connect_input (Port* our_port, string other_port, void* src) +IO::disconnect (Port* our_port, string other_port, void* src) { if (other_port.length() == 0 || our_port == 0) { return 0; @@ -379,68 +174,35 @@ IO::connect_input (Port* our_port, string other_port, void* src) { BLOCK_PROCESS_CALLBACK (); - + { Glib::Mutex::Lock lm (io_lock); - + /* check that our_port is really one of ours */ - - if ( ! _inputs.contains(our_port) ) { - return -1; - } - - /* connect it to the source */ - if (_session.engine().connect (other_port, our_port->name())) { + if ( ! _ports.contains(our_port)) { return -1; } - - drop_input_connection (); - } - } - - input_changed (ConnectionsChanged, src); /* EMIT SIGNAL */ - _session.set_dirty (); - return 0; -} -int -IO::disconnect_output (Port* our_port, string other_port, void* src) -{ - if (other_port.length() == 0 || our_port == 0) { - return 0; - } + /* disconnect it from the source */ - { - BLOCK_PROCESS_CALLBACK (); - - { - Glib::Mutex::Lock lm (io_lock); - - /* check that our_port is really one of ours */ - - if ( ! _outputs.contains(our_port) ) { - 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; + if (our_port->disconnect (other_port)) { + error << string_compose(_("IO: cannot disconnect port %1 from %2"), our_port->name(), other_port) << endmsg; return -1; } - drop_output_connection (); + check_bundles_connected (); } } - output_changed (ConnectionsChanged, src); /* EMIT SIGNAL */ + changed (ConnectionsChanged, src); /* EMIT SIGNAL */ _session.set_dirty (); + return 0; } int -IO::connect_output (Port* our_port, string other_port, void* src) +IO::connect (Port* our_port, string other_port, void* src) { if (other_port.length() == 0 || our_port == 0) { return 0; @@ -449,97 +211,65 @@ IO::connect_output (Port* our_port, string other_port, void* src) { BLOCK_PROCESS_CALLBACK (); - { Glib::Mutex::Lock lm (io_lock); - + /* check that our_port is really one of ours */ - - if ( ! _outputs.contains(our_port) ) { + + if ( ! _ports.contains(our_port) ) { return -1; } - - /* connect it to the destination */ - - if (_session.engine().connect (our_port->name(), other_port)) { + + /* connect it to the source */ + + if (our_port->connect (other_port)) { return -1; } - - drop_output_connection (); } } - output_changed (ConnectionsChanged, src); /* EMIT SIGNAL */ + changed (ConnectionsChanged, src); /* EMIT SIGNAL */ _session.set_dirty (); return 0; } int -IO::set_input (Port* other_port, void* src) -{ - /* this removes all but one ports, and connects that one port - to the specified source. - */ - - if (_input_minimum.get_total() > 1) { - /* sorry, you can't do this */ - return -1; - } - - if (other_port == 0) { - if (_input_minimum == ChanCount::ZERO) { - return ensure_inputs (ChanCount::ZERO, false, true, src); - } else { - return -1; - } - } - - if (ensure_inputs (ChanCount(other_port->type(), 1), true, true, src)) { - return -1; - } - - return connect_input (_inputs.port(0), other_port->name(), src); -} - -int -IO::remove_output_port (Port* port, void* src) +IO::remove_port (Port* port, void* src) { IOChange change (NoChange); { BLOCK_PROCESS_CALLBACK (); - + { Glib::Mutex::Lock lm (io_lock); - if (n_outputs() <= _output_minimum) { - /* sorry, you can't do this */ - return -1; - } - - if (_outputs.remove(port)) { + if (_ports.remove(port)) { change = IOChange (change|ConfigurationChanged); if (port->connected()) { change = IOChange (change|ConnectionsChanged); - } + } _session.engine().unregister_port (*port); - drop_output_connection (); - - setup_peak_meters (); - reset_panner (); + check_bundles_connected (); } } + + PortCountChanged (n_ports()); /* EMIT SIGNAL */ + } + + if (change & ConfigurationChanged) { + setup_bundles (); } if (change != NoChange) { - output_changed (change, src); + changed (change, src); _session.set_dirty (); return 0; - } - + } + return -1; } @@ -550,618 +280,179 @@ IO::remove_output_port (Port* port, void* src) * @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) +IO::add_port (string destination, void* src, DataType type) { Port* our_port; - char name[64]; - if (type == DataType::NIL) + if (type == DataType::NIL) { type = _default_type; + } { BLOCK_PROCESS_CALLBACK (); - - { + + { Glib::Mutex::Lock lm (io_lock); - - if (n_outputs() >= _output_maximum) { - return -1; - } - + /* Create a new output port */ - - // FIXME: naming scheme for differently typed ports? - if (_output_maximum.get(type) == 1) { - snprintf (name, sizeof (name), _("%s/out"), _name.c_str()); + + 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 { - 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; + if ((our_port = _session.engine().register_output_port (type, portname)) == 0) { + error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg; + return -1; + } } - - _outputs.add (our_port); - drop_output_connection (); - setup_peak_meters (); - reset_panner (); + + _ports.add (our_port); } - MoreChannels (n_outputs()); /* EMIT SIGNAL */ + PortCountChanged (n_ports()); /* EMIT SIGNAL */ } if (destination.length()) { - if (_session.engine().connect (our_port->name(), destination)) { + if (our_port->connect (destination)) { return -1; } } - + // pan_changed (src); /* EMIT SIGNAL */ - output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ + changed (ConfigurationChanged, src); /* EMIT SIGNAL */ + setup_bundles (); _session.set_dirty (); - + return 0; } int -IO::remove_input_port (Port* port, void* src) +IO::disconnect (void* src) { - IOChange change (NoChange); - { BLOCK_PROCESS_CALLBACK (); - { Glib::Mutex::Lock lm (io_lock); - if (n_inputs() <= _input_minimum) { - /* sorry, you can't do this */ - return -1; + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { + i->disconnect_all (); } - if (_inputs.remove(port)) { - change = IOChange (change|ConfigurationChanged); - - if (port->connected()) { - change = IOChange (change|ConnectionsChanged); - } - - _session.engine().unregister_port (*port); - drop_input_connection (); - - setup_peak_meters (); - reset_panner (); - } + check_bundles_connected (); } } - if (change != NoChange) { - input_changed (change, src); - _session.set_dirty (); - return 0; - } - - return -1; -} + changed (ConnectionsChanged, src); /* EMIT SIGNAL */ + return 0; +} -/** 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) +bool +IO::ensure_ports_locked (ChanCount count, bool clear, void* /*src*/) { - Port* our_port; - char name[64]; - - if (type == DataType::NIL) - type = _default_type; + Port* port = 0; + bool changed = false; - { - BLOCK_PROCESS_CALLBACK (); - - { - Glib::Mutex::Lock lm (io_lock); - - if (n_inputs() >= _input_maximum) { - return -1; - } + for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { - /* Create a new input port */ - - // FIXME: naming scheme for differently typed ports? - if (_input_maximum.get(type) == 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.add (our_port); - drop_input_connection (); - setup_peak_meters (); - reset_panner (); - } - - MoreChannels (n_inputs()); /* EMIT SIGNAL */ - } - - if (source.length()) { - - if (_session.engine().connect (source, our_port->name())) { - 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 (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) { - _session.engine().disconnect (*i); - } - - drop_input_connection (); - } - } - - input_changed (ConnectionsChanged, src); /* EMIT SIGNAL */ - - return 0; -} - -int -IO::disconnect_outputs (void* src) -{ - { - BLOCK_PROCESS_CALLBACK (); - - { - Glib::Mutex::Lock lm (io_lock); - - for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - _session.engine().disconnect (*i); - } - - drop_output_connection (); - } - } - - output_changed (ConnectionsChanged, src); /* EMIT SIGNAL */ - _session.set_dirty (); - - return 0; -} - -bool -IO::ensure_inputs_locked (ChanCount count, bool clear, void* src) -{ - Port* input_port = 0; - bool changed = false; - - - for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { - const size_t n = count.get(*t); - + /* remove unused ports */ - for (size_t i = n_inputs().get(*t); i > n; --i) { - input_port = _inputs.port(*t, i-1); + for (size_t i = n_ports().get(*t); i > n; --i) { + port = _ports.port(*t, i-1); - assert(input_port); - _inputs.remove(input_port); - _session.engine().unregister_port (*input_port); + assert(port); + _ports.remove(port); + _session.engine().unregister_port (*port); changed = true; } /* create any necessary new ports */ - while (n_inputs().get(*t) < n) { - - char buf[64]; + while (n_ports().get(*t) < n) { - if (_input_maximum.get(*t) == 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()); - } + string portname = build_legal_port_name (*t); try { - if ((input_port = _session.engine().register_input_port (*t, buf)) == 0) { - error << string_compose(_("IO: cannot register input port %1"), buf) << endmsg; - return -1; + 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; + } } } catch (AudioEngine::PortRegistrationFailure& err) { - setup_peak_meters (); - reset_panner (); /* pass it on */ throw AudioEngine::PortRegistrationFailure(); } - _inputs.add (input_port); + _ports.add (port); changed = true; } } - + if (changed) { - drop_input_connection (); - setup_peak_meters (); - reset_panner (); - MoreChannels (n_inputs()); /* EMIT SIGNAL */ + check_bundles_connected (); + PortCountChanged (n_ports()); /* EMIT SIGNAL */ _session.set_dirty (); } - + if (clear) { /* disconnect all existing ports so that we get a fresh start */ - for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) { - _session.engine().disconnect (*i); + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { + i->disconnect_all (); } } return changed; } -/** Attach output_buffers to port buffers. - * - * Connected to IO's own MoreChannels signal. - */ -void -IO::attach_buffers(ChanCount ignored) -{ - _output_buffers->attach_buffers(_outputs); -} - -int -IO::ensure_io (ChanCount in, ChanCount out, bool clear, void* src) -{ - bool in_changed = false; - bool out_changed = false; - bool need_pan_reset = false; - - in = min (_input_maximum, in); - - out = min (_output_maximum, out); - - if (in == n_inputs() && out == n_outputs() && !clear) { - return 0; - } - - { - BLOCK_PROCESS_CALLBACK (); - Glib::Mutex::Lock lm (io_lock); - - Port* port; - - if (n_outputs() != out) { - need_pan_reset = true; - } - - for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { - - const size_t nin = in.get(*t); - const size_t nout = out.get(*t); - - Port* output_port = 0; - Port* input_port = 0; - - /* remove unused output ports */ - for (size_t i = n_outputs().get(*t); i > nout; --i) { - output_port = _outputs.port(*t, i-1); - - assert(output_port); - _outputs.remove(output_port); - _session.engine().unregister_port (*output_port); - - out_changed = true; - } - - /* remove unused input ports */ - for (size_t i = n_inputs().get(*t); i > nin; --i) { - input_port = _inputs.port(*t, i-1); - - assert(input_port); - _inputs.remove(input_port); - _session.engine().unregister_port (*input_port); - - in_changed = true; - } - - /* create any necessary new input ports */ - - while (n_inputs().get(*t) < nin) { - - char buf[64]; - - /* Create a new input port */ - - if (_input_maximum.get(*t) == 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 (*t, 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.add (port); - in_changed = true; - } - - /* create any necessary new output ports */ - - while (n_outputs().get(*t) < nout) { - - char buf[64]; - - /* Create a new output port */ - - if (_output_maximum.get(*t) == 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 (*t, 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.add (port); - out_changed = true; - } - } - - if (clear) { - - /* disconnect all existing ports so that we get a fresh start */ - - for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) { - _session.engine().disconnect (*i); - } - - for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - _session.engine().disconnect (*i); - } - } - - if (in_changed || out_changed) { - setup_peak_meters (); - reset_panner (); - } - } - - if (out_changed) { - drop_output_connection (); - output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ - } - - if (in_changed) { - drop_input_connection (); - input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ - } - - if (in_changed || out_changed) { - MoreChannels (max (n_outputs(), n_inputs())); /* EMIT SIGNAL */ - _session.set_dirty (); - } - - return 0; -} int -IO::ensure_inputs (ChanCount count, bool clear, bool lockit, void* src) +IO::ensure_ports (ChanCount count, bool clear, bool lockit, void* src) { bool changed = false; - count = min (_input_maximum, count); - - if (count == n_inputs() && !clear) { + if (count == n_ports() && !clear) { return 0; } if (lockit) { BLOCK_PROCESS_CALLBACK (); Glib::Mutex::Lock im (io_lock); - changed = ensure_inputs_locked (count, clear, src); + changed = ensure_ports_locked (count, clear, src); } else { - changed = ensure_inputs_locked (count, clear, src); + changed = ensure_ports_locked (count, clear, src); } if (changed) { - input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ - _session.set_dirty (); - } - return 0; -} - -bool -IO::ensure_outputs_locked (ChanCount count, bool clear, void* src) -{ - Port* output_port = 0; - bool changed = false; - bool need_pan_reset = false; - - if (n_outputs() != count) { - need_pan_reset = true; - } - - for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { - - const size_t n = count.get(*t); - - /* remove unused ports */ - for (size_t i = n_outputs().get(*t); i > n; --i) { - output_port = _outputs.port(*t, i-1); - - assert(output_port); - _outputs.remove(output_port); - _session.engine().unregister_port (*output_port); - - changed = true; - } - - /* create any necessary new ports */ - while (n_outputs().get(*t) < n) { - - char buf[64]; - - if (_output_maximum.get(*t) == 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 (*t, buf)) == 0) { - error << string_compose(_("IO: cannot register output port %1"), buf) << endmsg; - return -1; - } - - _outputs.add (output_port); - changed = true; - setup_peak_meters (); - - if (need_pan_reset) { - reset_panner (); - } - } - } - - if (changed) { - drop_output_connection (); - MoreChannels (n_outputs()); /* EMIT SIGNAL */ + this->changed (ConfigurationChanged, src); /* EMIT SIGNAL */ + setup_bundles (); _session.set_dirty (); } - - if (clear) { - /* disconnect all existing ports so that we get a fresh start */ - for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - _session.engine().disconnect (*i); - } - } - - return changed; -} - -int -IO::ensure_outputs (ChanCount count, bool clear, bool lockit, void* src) -{ - bool changed = false; - - if (_output_maximum < ChanCount::INFINITE) { - count = min (_output_maximum, count); - if (count == n_outputs() && !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 (count, clear, src); - } else { - changed = ensure_outputs_locked (count, clear, src); - } - - if (changed) { - output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ - } 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 (n_outputs().get(DataType::AUDIO), pans_required()); - } - } else { - panner_legal_c.disconnect (); - panner_legal_c = PannersLegal.connect (mem_fun (*this, &IO::panners_became_legal)); - } -} - int -IO::panners_became_legal () +IO::ensure_io (ChanCount count, bool clear, void* src) { - _panner->reset (n_outputs().get(DataType::AUDIO), pans_required()); - _panner->load (); // automation - panner_legal_c.disconnect (); - return 0; -} - -void -IO::defer_pan_reset () -{ - no_panner_reset = true; + return ensure_ports (count, clear, true, src); } -void -IO::allow_pan_reset () -{ - no_panner_reset = false; - reset_panner (); -} - - XMLNode& IO::get_state (void) { @@ -1169,134 +460,69 @@ IO::get_state (void) } XMLNode& -IO::state (bool full_state) +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; + vector::iterator ci; + int n; 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); + node->add_property ("direction", enum_2_string (_direction)); + node->add_property ("default-type", _default_type.to_string()); - 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 (PortSet::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 (PortSet::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); + 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); } - node->add_child_nocopy (_panner->state (full_state)); - node->add_child_nocopy (_gain_control.get_state ()); + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { - snprintf (buf, sizeof(buf), "%2.12f", gain()); - node->add_property ("gain", buf); + vector connections; - // FIXME: this is NOT sufficient! - const int in_min = (_input_minimum == ChanCount::ZERO) ? -1 : _input_minimum.get(_default_type); - const int in_max = (_input_maximum == ChanCount::INFINITE) ? -1 : _input_maximum.get(_default_type); - const int out_min = (_output_minimum == ChanCount::ZERO) ? -1 : _output_minimum.get(_default_type); - const int out_max = (_output_maximum == ChanCount::INFINITE) ? -1 : _output_maximum.get(_default_type); + XMLNode* pnode = new XMLNode (X_("Port")); + pnode->add_property (X_("type"), i->type().to_string()); + pnode->add_property (X_("name"), i->name()); - snprintf (buf, sizeof(buf)-1, "%d,%d,%d,%d", in_min, in_max, out_min, out_max); + if (i->get_connections (connections)) { - node->add_property ("iolimits", buf); + for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) { - /* automation */ + /* 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. + */ - if (full_state) { + XMLNode* cnode = new XMLNode (X_("Connection")); - XMLNode* autonode = new XMLNode (X_("Automation")); - autonode->add_child_nocopy (get_automation_state()); - node->add_child_nocopy (*autonode); + cnode->add_property (X_("other"), _session.engine().make_port_name_relative (*ci)); + pnode->add_child_nocopy (*cnode); + } + } - 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); + node->add_child_nocopy (*pnode); } return *node; } int -IO::set_state (const XMLNode& node) +IO::set_state (const XMLNode& node, int version) { + /* 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); + const XMLProperty* prop; XMLNodeConstIterator iter; LocaleGuard lg (X_("POSIX")); @@ -1311,423 +537,468 @@ IO::set_state (const XMLNode& node) } if ((prop = node.property ("name")) != 0) { - _name = prop->value(); - /* used to set panner name with this, but no more */ - } + set_name (prop->value()); + } + + if ((prop = node.property (X_("default-type"))) != 0) { + _default_type = DataType(prop->value()); + assert(_default_type != DataType::NIL); + } if ((prop = node.property ("id")) != 0) { _id = prop->value (); } - size_t in_min = -1; - size_t in_max = -1; - size_t out_min = -1; - size_t out_max = -1; - - if ((prop = node.property ("iolimits")) != 0) { - sscanf (prop->value().c_str(), "%zd,%zd,%zd,%zd", - &in_min, &in_max, &out_min, &out_max); - _input_minimum = ChanCount(_default_type, in_min); - _input_maximum = ChanCount(_default_type, in_max); - _output_minimum = ChanCount(_default_type, out_min); - _output_maximum = ChanCount(_default_type, out_max); - } - - if ((prop = node.property ("gain")) != 0) { - set_gain (atof (prop->value().c_str()), this); - _gain = _desired_gain; + if ((prop = node.property ("direction")) != 0) { + _direction = (Direction) string_2_enum (prop->value(), _direction); } - if ((prop = node.property ("automation-state")) != 0 || (prop = node.property ("automation-style")) != 0) { - /* old school automation handling */ + if (create_ports (node, version)) { + return -1; } - for (iter = node.children().begin(); iter != node.children().end(); ++iter) { + if (connecting_legal) { - if ((*iter)->name() == "Panner") { - if (_panner == 0) { - _panner = new Panner (_name, _session); - } - _panner->set_state (**iter); + if (make_connections (node, version, false)) { + return -1; } - if ((*iter)->name() == X_("Automation")) { - - set_automation_state (*(*iter)->children().front()); - } + } else { - if ((*iter)->name() == X_("controllable")) { - if ((prop = (*iter)->property("name")) != 0 && prop->value() == "gaincontrol") { - _gain_control.set_state (**iter); - } - } + pending_state_node = new XMLNode (node); + pending_state_node_version = version; + pending_state_node_in = false; + connection_legal_c = ConnectingLegal.connect (mem_fun (*this, &IO::connecting_became_legal)); } - if (ports_legal) { - if (create_ports (node)) { - return -1; - } + return 0; +} - } else { +int +IO::set_state_2X (const XMLNode& node, int version, bool in) +{ + const XMLProperty* prop; + XMLNodeConstIterator iter; + LocaleGuard lg (X_("POSIX")); - port_legal_c = PortsLegal.connect (mem_fun (*this, &IO::ports_became_legal)); + /* force use of non-localized representation of decimal point, + since we use it a lot in XML files and so forth. + */ + + if (node.name() != state_node_name) { + error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg; + return -1; + } + + if ((prop = node.property ("name")) != 0) { + set_name (prop->value()); } - if (panners_legal) { - reset_panner (); - } else { - panner_legal_c = PannersLegal.connect (mem_fun (*this, &IO::panners_became_legal)); + if ((prop = node.property (X_("default-type"))) != 0) { + _default_type = DataType(prop->value()); + assert(_default_type != DataType::NIL); } - if (connecting_legal) { + if ((prop = node.property ("id")) != 0) { + _id = prop->value (); + } + + _direction = in ? Input : Output; - if (make_connections (node)) { + if (create_ports (node, version)) { + return -1; + } + + if (connecting_legal) { + + if (make_connections_2X (node, version, in)) { return -1; } } else { - - connection_legal_c = ConnectingLegal.connect (mem_fun (*this, &IO::connecting_became_legal)); - } - if (!ports_legal || !connecting_legal) { pending_state_node = new XMLNode (node); + pending_state_node_version = version; + pending_state_node_in = in; + connection_legal_c = ConnectingLegal.connect (mem_fun (*this, &IO::connecting_became_legal)); } - last_automation_snapshot = 0; - return 0; } int -IO::set_automation_state (const XMLNode& node) +IO::connecting_became_legal () { - return _gain_automation_curve.set_state (node); -} + int ret; -XMLNode& -IO::get_automation_state () -{ - return (_gain_automation_curve.get_state ()); + assert (pending_state_node); + + connection_legal_c.disconnect (); + + ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in); + + delete pending_state_node; + pending_state_node = 0; + + return ret; } -int -IO::load_automation (string path) +boost::shared_ptr +IO::find_possible_bundle (const string &desired_name) { - string fullpath; - ifstream in; - char line[128]; - uint32_t linecnt = 0; - float version; - 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")); - fullpath = _session.automation_dir(); - fullpath += path; + boost::shared_ptr c = _session.bundle_by_name (desired_name); - in.open (fullpath.c_str()); + if (!c) { + int bundle_number, mask; + string possible_name; + bool stereo = false; + string::size_type last_non_digit_pos; - if (!in) { - fullpath = _session.automation_dir(); - fullpath += _session.snap_name(); - fullpath += '-'; - fullpath += path; + error << string_compose(_("Unknown bundle \"%1\" listed for %2 of %3"), desired_name, bundle_type_name, _name) + << endmsg; - in.open (fullpath.c_str()); + // find numeric suffix of desired name + bundle_number = 0; - if (!in) { - error << string_compose(_("%1: cannot open automation event file \"%2\""), _name, fullpath) << endmsg; - return -1; + last_non_digit_pos = desired_name.find_last_not_of(digits); + + if (last_non_digit_pos != string::npos) { + stringstream s; + s << desired_name.substr(last_non_digit_pos); + s >> bundle_number; } - } - clear_automation (); + // see if it's a stereo connection e.g. "in 3+4" - while (in.getline (line, sizeof(line), '\n')) { - char type; - nframes_t when; - double value; + if (last_non_digit_pos > 1 && desired_name[last_non_digit_pos] == '+') { + int left_bundle_number = 0; + string::size_type left_last_non_digit_pos; - 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; + left_last_non_digit_pos = desired_name.find_last_not_of(digits, last_non_digit_pos-1); + + if (left_last_non_digit_pos != string::npos) { + stringstream s; + s << desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1); + s >> left_bundle_number; + + if (left_bundle_number > 0 && left_bundle_number + 1 == bundle_number) { + bundle_number--; + stereo = true; } - } 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; - } + // make 0-based + if (bundle_number) + bundle_number--; - switch (type) { - case 'g': - _gain_automation_curve.fast_simple_add (when, value); - break; + // find highest set bit + mask = 1; + while ((mask <= bundle_number) && (mask <<= 1)) {} - case 's': - break; + // "wrap" bundle number into largest possible power of 2 + // that works... - case 'm': - break; + while (mask) { - case 'p': - /* older (pre-1.0) versions of ardour used this */ - break; + if (bundle_number & mask) { + bundle_number &= ~mask; - default: - warning << _("dubious automation event found (and ignored)") << endmsg; + stringstream s; + s << default_name << " " << bundle_number + 1; + + if (stereo) { + s << "+" << bundle_number + 2; + } + + 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; } + } - return 0; + return c; + } int -IO::connecting_became_legal () +IO::get_port_counts_2X (XMLNode const & node, int version, ChanCount& n, boost::shared_ptr& c) { - int ret; - - if (pending_state_node == 0) { - fatal << _("IO::connecting_became_legal() called without a pending state node") << endmsg; - /*NOTREACHED*/ - return -1; - } + XMLProperty const * prop; + XMLNodeList children = node.children (); - connection_legal_c.disconnect (); + uint32_t n_audio = 0; - ret = make_connections (*pending_state_node); + for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) { - if (ports_legal) { - delete pending_state_node; - pending_state_node = 0; + if ((prop = node.property ("inputs")) != 0 && _direction == Input) { + n_audio = count (prop->value().begin(), prop->value().end(), '{'); + } else if ((prop = node.property ("outputs")) != 0 && _direction == Output) { + n_audio = count (prop->value().begin(), prop->value().end(), '{'); + } } - return ret; + ChanCount cnt; + cnt.set_audio (n_audio); + n = ChanCount::max (n, cnt); + + return 0; } + int -IO::ports_became_legal () +IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr& c) { - 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 get_port_counts_2X (node, version, n, c); } + + XMLProperty const * prop; + XMLNodeConstIterator iter; + uint32_t n_audio = 0; + uint32_t n_midi = 0; + ChanCount cnt; - port_legal_c.disconnect (); + n = n_ports(); - ret = create_ports (*pending_state_node); + if ((prop = node.property ("connection")) != 0) { - if (connecting_legal) { - delete pending_state_node; - pending_state_node = 0; + if ((c = find_possible_bundle (prop->value())) != 0) { + n = ChanCount::max (n, ChanCount(c->type(), c->nchannels())); + } + return 0; } - return ret; -} - -int -IO::create_ports (const XMLNode& node) -{ - const XMLProperty* prop; - int num_inputs = 0; - int num_outputs = 0; - - 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; + for (iter = node.children().begin(); iter != node.children().end(); ++iter) { - if ((c = _session.connection_by_name (_("in 1"))) == 0) { - error << _("No input connections available as a replacement") - << endmsg; + if ((*iter)->name() == X_("Bundle")) { + if ((c = find_possible_bundle (prop->value())) != 0) { + n = ChanCount::max (n, ChanCount(c->type(), c->nchannels())); + return 0; + } else { return -1; - } else { - info << string_compose (_("Connection %1 was not available - \"in 1\" used instead"), prop->value()) - << endmsg; } - } - - num_inputs = c->nports(); - - } else if ((prop = node.property ("inputs")) != 0) { - - num_inputs = count (prop->value().begin(), prop->value().end(), '{'); - } - - 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 ((*iter)->name() == X_("Port")) { + prop = (*iter)->property (X_("type")); - 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) { + continue; } - } - num_outputs = c->nports (); - - } else if ((prop = node.property ("outputs")) != 0) { - num_outputs = count (prop->value().begin(), prop->value().end(), '{'); + if (prop->value() == X_("audio")) { + cnt.set_audio (++n_audio); + } else if (prop->value() == X_("midi")) { + cnt.set_midi (++n_midi); + } + } } - no_panner_reset = true; + n = ChanCount::max (n, cnt); + return 0; +} - // FIXME: audio-only - if (ensure_io (ChanCount(DataType::AUDIO, num_inputs), ChanCount(DataType::AUDIO, num_outputs), true, this)) { +int +IO::create_ports (const XMLNode& node, int version) +{ + ChanCount n; + boost::shared_ptr c; + + get_port_counts (node, version, n, c); + + if (ensure_ports (n, true, true, this)) { error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg; return -1; } - no_panner_reset = false; + /* XXX use c */ - set_deferred_state (); - - PortsCreated(); return 0; } - int -IO::make_connections (const XMLNode& node) +IO::make_connections (const XMLNode& node, int version, bool in) { + if (version < 3000) { + return make_connections_2X (node, version, 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; + for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) { - 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 ((*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, this); + } } - } - 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; + return 0; } - } - - 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 ((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 ((*i)->name() == "Port") { + + prop = (*i)->property (X_("name")); + + if (!prop) { + continue; } - } - 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; + Port* p = port_by_name (prop->value()); + + if (p) { + for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) { + + XMLNode* cnode = (*c); + + if (cnode->name() != X_("Connection")) { + continue; + } + + if ((prop = cnode->property (X_("other"))) == 0) { + continue; + } + + if (prop) { + p->connect (prop->value()); + } + } + } } } - + return 0; } + int -IO::set_inputs (const string& str) +IO::make_connections_2X (const XMLNode& node, int version, bool in) { - vector ports; - int i; - int n; - uint32_t nports; - - if ((nports = count (str.begin(), str.end(), '{')) == 0) { - return 0; - } + const XMLProperty* prop; - // FIXME: audio-only - if (ensure_inputs (ChanCount(DataType::AUDIO, nports), true, true, this)) { - return -1; - } + /* XXX: bundles ("connections" as was) */ + + if ((prop = node.property ("inputs")) != 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; + + 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 ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) { + error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg; + + return -1; + + } else if (n > 0) { - 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; + 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]); + } + } + + ostart = end+1; + i++; } - if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) { - error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg; + } - return -1; - - } else if (n > 0) { + if ((prop = node.property ("outputs")) != 0 && !in) { - for (int x = 0; x < n; ++x) { - connect_input (input (i), ports[x], this); + string::size_type ostart = 0; + string::size_type start = 0; + string::size_type end = 0; + int i = 0; + int n; + vector ports; + + 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 outputs \"%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; + + return -1; + + } 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; } // FIXME: audio-only - if (ensure_outputs (ChanCount(DataType::AUDIO, nports), true, true, this)) { + if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, true, this)) { return -1; } @@ -1742,19 +1013,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); } } @@ -1783,7 +1054,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)); } @@ -1804,7 +1075,7 @@ 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)); } @@ -1812,59 +1083,32 @@ IO::parse_gain_string (const string& str, vector& ports) return ports.size(); } -int -IO::set_name (string name, void* src) +bool +IO::set_name (const string& requested_name) { + string name = requested_name; + if (name == _name) { - return 0; + return true; } /* 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 (PortSet::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); + if (replace_all (name, ":", "-")) { + warning << _("you cannot use colons to name objects with I/O connections") << endmsg; } - for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - string current_name = i->short_name(); + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { + string current_name = i->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 (ChanCount n) -{ - _input_minimum = n; -} - -void -IO::set_input_maximum (ChanCount n) -{ - _input_maximum = n; -} + bool const r = SessionObject::set_name (name); -void -IO::set_output_minimum (ChanCount n) -{ - _output_minimum = n; -} + setup_bundles (); -void -IO::set_output_maximum (ChanCount n) -{ - _output_maximum = n; + return r; } void @@ -1872,13 +1116,13 @@ IO::set_port_latency (nframes_t nframes) { Glib::Mutex::Lock lm (io_lock); - for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { i->set_latency (nframes); } } nframes_t -IO::output_latency () const +IO::latency () const { nframes_t max_latency; nframes_t latency; @@ -1887,8 +1131,8 @@ IO::output_latency () const /* io lock not taken - must be protected by other means */ - for (PortSet::const_iterator i = _outputs.begin(); i != _outputs.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->total_latency ()) > max_latency) { max_latency = latency; } } @@ -1896,184 +1140,77 @@ IO::output_latency () const return max_latency; } -nframes_t -IO::input_latency () const +void +IO::update_port_total_latencies () { - nframes_t max_latency; - nframes_t latency; - - max_latency = 0; - - /* io lock not taken - must be protected by other means */ + /* io_lock, not taken: function must be called from Session::process() calltree */ - for (PortSet::const_iterator i = _inputs.begin(); i != _inputs.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) { + _session.engine().update_total_latency (*i); } - - return max_latency; } int -IO::use_input_connection (Connection& c, void* src) +IO::connect_ports_to_bundle (boost::shared_ptr c, void* src) { - uint32_t limit; - { BLOCK_PROCESS_CALLBACK (); Glib::Mutex::Lock lm2 (io_lock); - - limit = c.nports(); - - drop_input_connection (); - - // FIXME connections only work for audio-only - if (ensure_inputs (ChanCount(DataType::AUDIO, 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 (!_inputs.port(n)->connected_to ((*i))) { - - /* clear any existing connections */ - - _session.engine().disconnect (*_inputs.port(n)); - - } else if (_inputs.port(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.port(n)); - - } + 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; } - } - - /* 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.port(n)->connected_to ((*i))) { - - if (_session.engine().connect (*i, _inputs.port(n)->name())) { - return -1; - } - } - + + if (i == _bundles_connected.end()) { + /* We don't, so make a note */ + _bundles_connected.push_back (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 (ConfigurationChanged|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 (); Glib::Mutex::Lock lm2 (io_lock); - limit = c.nports(); - - drop_output_connection (); - - // FIXME: audio-only - if (ensure_outputs (ChanCount(DataType::AUDIO, 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.port(n)->connected_to ((*i))) { - - /* clear any existing connections */ - - _session.engine().disconnect (*_outputs.port(n)); + c->disconnect (_bundle, _session.engine()); - } else if (_outputs.port(n)->connected() > 1) { + /* If this is a UserBundle, make a note of what we've done */ - /* OK, it is connected to the port we want, - but its also connected to other ports. - Change that situation. - */ + boost::shared_ptr ub = boost::dynamic_pointer_cast (c); + if (ub) { - /* XXX could be optimized to not drop - the one we want. - */ - - _session.engine().disconnect (*_outputs.port(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.port(n)->connected_to ((*i))) { - - if (_session.engine().connect (_outputs.port(n)->name(), *i)) { - return -1; - } - } + if (i != _bundles_connected.end()) { + _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 (ConfigurationChanged|ConnectionsChanged), src); /* EMIT SIGNAL */ return 0; } + int IO::disable_connecting () { @@ -2088,340 +1225,336 @@ IO::enable_connecting () return ConnectingLegal (); } -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 = jack_port_name_size(); + int limit; + string suffix; -int -IO::reset_panners () -{ - panners_legal = true; - return PannersLegal (); -} + if (type == DataType::AUDIO) { + suffix = _("audio"); + } else if (type == DataType::MIDI) { + suffix = _("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 (_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 - _session.engine().client_name().length() - (suffix.length() + 5); -void -IO::GainControllable::set_value (float val) -{ - io.set_gain (direct_control_to_gain (val), this); -} + char buf1[name_size+1]; + char buf2[name_size+1]; -float -IO::GainControllable::get_value (void) const -{ - return direct_gain_to_control (io.effective_gain()); + snprintf (buf1, name_size+1, ("%.*s/%s"), limit, _name.c_str(), suffix.c_str()); + + int port_number = find_port_hole (buf1); + snprintf (buf2, name_size+1, "%s %d", buf1, port_number); + + return string (buf2); } -void -IO::setup_peak_meters() +int32_t +IO::find_port_hole (const char* base) { - _meter->setup(std::max(_inputs.count(), _outputs.count())); -} + /* CALLER MUST HOLD IO LOCK */ -/** - Update the peak meters. + uint32_t n; - 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() -{ - Glib::Mutex::Lock guard (m_meter_signal_lock); - - Meter(); /* EMIT SIGNAL */ + if (_ports.empty()) { + return 1; + } + + /* we only allow up to 4 characters for the port number + */ + + for (n = 1; n < 9999; ++n) { + char buf[jack_port_name_size()]; + PortSet::iterator i = _ports.begin(); + + snprintf (buf, jack_port_name_size(), _("%s %u"), base, n); + + for ( ; i != _ports.end(); ++i) { + if (i->name() == buf) { + break; + } + } + + if (i == _ports.end()) { + break; + } + } + return n; } -void -IO::meter () + +AudioPort* +IO::audio(uint32_t n) const { - // FIXME: Ugly. Meter should manage the lock, if it's necessary - - Glib::Mutex::Lock lm (io_lock); // READER: meter thread. - _meter->meter(); + return _ports.nth_audio_port (n); + } -void -IO::clear_automation () +MidiPort* +IO::midi(uint32_t n) const { - Glib::Mutex::Lock lm (automation_lock); - _gain_automation_curve.clear (); - _panner->clear_automation (); + return _ports.nth_midi_port (n); } +/** + * Setup bundles that describe our inputs and outputs. Also creates bundles if necessary. + */ + void -IO::set_gain_automation_state (AutoState state) +IO::setup_bundles () { - bool changed = false; - - { - Glib::Mutex::Lock lm (automation_lock); + char buf[32]; - 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); - } - } + if (!_bundle) { + _bundle.reset (new Bundle (_direction == Input)); } - if (changed) { - _session.set_dirty (); - gain_automation_state_changed (); /* EMIT SIGNAL */ - } -} + _bundle->suspend_signals (); -void -IO::set_gain_automation_style (AutoStyle style) -{ - bool changed = false; + _bundle->set_type (default_type ()); - { - Glib::Mutex::Lock lm (automation_lock); + _bundle->remove_channels (); - if (style != _gain_automation_curve.automation_style()) { - changed = true; - _gain_automation_curve.set_automation_style (style); - } + if (_direction == Input) { + snprintf(buf, sizeof (buf), _("%s in"), _name.c_str()); + } else { + snprintf(buf, sizeof (buf), _("%s out"), _name.c_str()); } - - if (changed) { - gain_automation_style_changed (); /* EMIT SIGNAL */ + _bundle->set_name (buf); + uint32_t const ni = _ports.num_ports(); + for (uint32_t i = 0; i < ni; ++i) { + _bundle->add_channel (bundle_channel_name (i, ni)); + _bundle->set_port (i, _session.engine().make_port_name_non_relative (_ports.port(i)->name())); } -} -void -IO::inc_gain (gain_t factor, void *src) -{ - if (_desired_gain == 0.0f) - set_gain (0.000001f + (0.000001f * factor), src); - else - set_gain (_desired_gain + (_desired_gain * factor), src); + + _bundle->resume_signals (); } -void -IO::set_gain (gain_t val, void *src) +/** @return Bundles connected to our ports */ +BundleList +IO::bundles_connected () { - // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05)) - if (val>1.99526231f) val=1.99526231f; + BundleList bundles; - { - Glib::Mutex::Lock dm (declick_lock); - _desired_gain = val; + /* User bundles */ + for (std::vector::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) { + bundles.push_back (i->bundle); } - if (_session.transport_stopped()) { - _effective_gain = val; - _gain = val; + /* 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); + } } - 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); - + /* Route bundles */ + + boost::shared_ptr r = _session.get_routes (); + + 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()); + } + } } - _session.set_dirty(); + return bundles; } -void -IO::start_gain_touch () -{ - _gain_automation_curve.start_touch (); -} -void -IO::end_gain_touch () +IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr b) { - _gain_automation_curve.stop_touch (); + bundle = b; + changed = b->Changed.connect ( + sigc::mem_fun (*io, &IO::bundle_changed) + ); } -void -IO::start_pan_touch (uint32_t which) +std::string +IO::bundle_channel_name (uint32_t c, uint32_t n) const { - if (which < _panner->size()) { - (*_panner)[which]->automation().start_touch(); - } -} + char buf[32]; -void -IO::end_pan_touch (uint32_t which) -{ - if (which < _panner->size()) { - (*_panner)[which]->automation().stop_touch(); + switch (n) { + case 1: + return _("mono"); + case 2: + return c == 0 ? _("L") : _("R"); + default: + snprintf (buf, sizeof(buf), _("%d"), (c + 1)); + return buf; } + return ""; } -void -IO::automation_snapshot (nframes_t now) +string +IO::name_from_state (const XMLNode& node) { - if (last_automation_snapshot > now || (now - last_automation_snapshot) > _automation_interval) { - - if (gain_automation_recording()) { - _gain_automation_curve.rt_add (now, gain()); - } - - _panner->snapshot (now); + const XMLProperty* prop; - last_automation_snapshot = now; + if ((prop = node.property ("name")) != 0) { + return prop->value(); } + + return string(); } void -IO::transport_stopped (nframes_t frame) +IO::set_name_in_state (XMLNode& node, const string& new_name) { - _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. - */ + const XMLProperty* prop; - set_gain (_gain_automation_curve.eval (frame), 0); + if ((prop = node.property ("name")) != 0) { + node.add_property ("name", new_name); } - - _panner->transport_stopped (frame); } -int32_t -IO::find_input_port_hole () +bool +IO::connected_to (boost::shared_ptr other) const { - /* CALLER MUST HOLD IO LOCK */ + if (!other) { + /* do we have any connections at all? */ - uint32_t n; + for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) { + if (p->connected()) { + return true; + } + } - if (_inputs.empty()) { - return 1; + return false; } - for (n = 1; n < UINT_MAX; ++n) { - char buf[jack_port_name_size()]; - PortSet::iterator i = _inputs.begin(); + assert (_direction != other->direction()); - snprintf (buf, jack_port_name_size(), _("%s/in %u"), _name.c_str(), n); + uint32_t i, j; + uint32_t no = n_ports().n_total(); + uint32_t ni = other->n_ports ().n_total(); - for ( ; i != _inputs.end(); ++i) { - if (i->short_name() == buf) { - break; + for (i = 0; i < no; ++i) { + for (j = 0; j < ni; ++j) { + if (nth(i)->connected_to (other->nth(j)->name())) { + return true; } } - - if (i == _inputs.end()) { - break; - } } - return n; + + return false; } -int32_t -IO::find_output_port_hole () +void +IO::process_input (boost::shared_ptr proc, sframes_t start_frame, sframes_t end_frame, nframes_t nframes) { - /* CALLER MUST HOLD IO LOCK */ + BufferSet bufs; - uint32_t n; + /* don't read the data into new buffers - just use the port buffers directly */ - if (_outputs.empty()) { - return 1; + bufs.attach_buffers (_ports, nframes, 0); + proc->run (bufs, start_frame, end_frame, nframes); +} + +void +IO::collect_input (BufferSet& bufs, nframes_t nframes, ChanCount offset) +{ + assert(bufs.available() >= _ports.count()); + + if (_ports.count() == ChanCount::ZERO) { + return; } - for (n = 1; n < UINT_MAX; ++n) { - char buf[jack_port_name_size()]; - PortSet::iterator i = _outputs.begin(); + bufs.set_count (_ports.count()); - snprintf (buf, jack_port_name_size(), _("%s/out %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 != _outputs.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 == _outputs.end()) { - break; + for ( ; i != _ports.end(*t); ++i, ++b) { + Buffer& bb (i->get_buffer (nframes)); + b->read_from (bb, nframes); } } - - return n; } -AudioPort* -IO::audio_input(uint32_t n) const +void +IO::copy_to_outputs (BufferSet& bufs, DataType type, nframes_t nframes, nframes_t offset) { - return dynamic_cast(input(n)); -} + // Copy any buffers 1:1 to outputs -AudioPort* -IO::audio_output(uint32_t n) const -{ - return dynamic_cast(output(n)); -} + PortSet::iterator o = _ports.begin(type); + BufferSet::iterator i = bufs.begin(type); + BufferSet::iterator prev = i; -MidiPort* -IO::midi_input(uint32_t n) const -{ - return dynamic_cast(input(n)); -} + 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; + } -MidiPort* -IO::midi_output(uint32_t n) const -{ - return dynamic_cast(output(n)); + // Copy last buffer to any extra outputs + + while (o != _ports.end(type)) { + Buffer& port_buffer (o->get_buffer (nframes)); + port_buffer.read_from (*prev, nframes, offset); + ++o; + } } -void -IO::set_phase_invert (bool yn, void *src) +Port* +IO::port_by_name (const std::string& str) const { - if (_phase_invert != yn) { - _phase_invert = yn; + /* to be called only from ::set_state() - no locking */ + + for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) { + + const Port& p(*i); + + if (p.name() == str) { + return const_cast(&p); + } } - // phase_invert_changed (src); /* EMIT SIGNAL */ -} + return 0; +}