X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fio.cc;h=ed2b23f7ee918348a97a11924466ee445c9a0543;hb=4d112a8e6b90fa64a5cd333042044768111ba994;hp=0857fad94d3082cc5a54ec65f0c90065ad445bb9;hpb=76b735680226d9691d8c1bc38c3191faa633ee7a;p=ardour.git diff --git a/libs/ardour/io.cc b/libs/ardour/io.cc index 0857fad94d..ed2b23f7ee 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 @@ -18,48 +18,37 @@ #include #include +#include + #include #include #include -#include - #include #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/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 - "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::Mutex::Lock em (AudioEngine::instance()->process_lock()) using namespace std; using namespace ARDOUR; @@ -67,302 +56,75 @@ using namespace PBD; 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::PortCountChanged; -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. -*/ - -#if 0 -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); -} -#endif +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, const string& name, - int input_min, int input_max, int output_min, int output_max, - DataType default_type, bool public_ports) - : SessionObject(s, name), - AutomatableControls (s), - _output_buffers (new BufferSet()), - _active(true), - _default_type (default_type), - _public_ports (public_ports), - _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; + _active = true; pending_state_node = 0; - no_panner_reset = false; - _phase_invert = false; - deferred_state = 0; - - boost::shared_ptr gl( - new AutomationList(Evoral::Parameter(GainAutomation))); - - _gain_control = boost::shared_ptr( new GainControl( X_("gaincontrol"), this, Evoral::Parameter(GainAutomation), gl )); - - add_control(_gain_control); - - apply_gain_automation = false; - - { - // 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 PortCountChanged signal to connect output buffers - IO::PortCountChanged.connect (mem_fun (*this, &IO::attach_buffers)); - - _session.add_controllable (_gain_control); - - create_bundles_for_inputs_and_outputs (); + setup_bundle (); } IO::IO (Session& s, const XMLNode& node, DataType dt) - : SessionObject(s, "unnamed io"), - AutomatableControls (s), - _output_buffers (new BufferSet()), - _active(true), - _default_type (dt) + : SessionObject(s, "unnamed io") + , _direction (Input) + , _default_type (dt) { - _meter = new PeakMeter (_session); - _public_ports = true; // XXX get this from node - _panner = 0; - deferred_state = 0; - no_panner_reset = false; - _desired_gain = 1.0; - _gain = 1.0; - - apply_gain_automation = false; - - boost::shared_ptr gl( - new AutomationList(Evoral::Parameter(GainAutomation))); - - _gain_control = boost::shared_ptr( - new GainControl( X_("gaincontrol"), this, Evoral::Parameter(GainAutomation), gl)); - - add_control(_gain_control); - - 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 PortCountChanged signal to connect output buffers - IO::PortCountChanged.connect (mem_fun (*this, &IO::attach_buffers)); - - _session.add_controllable (_gain_control); + _active = true; + pending_state_node = 0; - create_bundles_for_inputs_and_outputs (); + set_state (node, Stateful::loading_state_version); + setup_bundle (); } IO::~IO () { - Glib::Mutex::Lock guard (m_meter_signal_lock); Glib::Mutex::Lock lm (io_lock); BLOCK_PROCESS_CALLBACK (); - for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) { - _session.engine().unregister_port (*i); - } - - 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(nframes,offset).silence (nframes, offset); - } -} - -/** Deliver bufs to the IO's output ports - * - * 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_in_place(bufs, nframes, _gain, dg, _phase_invert); - } - - // Use the panner to distribute audio to output port buffers - if( _panner && _panner->npanners() && !_panner->bypassed()) { - _panner->run_out_of_place(bufs, output_buffers(), start_frame, end_frame, nframes, offset); - } else { - const DataType type = DataType::AUDIO; - - // Copy any audio 1:1 to outputs - - BufferSet::iterator o = output_buffers().begin(type); - BufferSet::iterator i = bufs.begin(type); - BufferSet::iterator prev = i; - - while (i != bufs.end(type) && o != output_buffers().end (type)) { - o->read_from(*i, nframes, offset); - prev = i; - ++i; - ++o; - } - - /* extra outputs get a copy of the last buffer */ - - while (o != output_buffers().end(type)) { - o->read_from(*prev, nframes, offset); - ++o; - } - } - - /* ********** MIDI ********** */ - - // No MIDI, we're done here - if (bufs.count().n_midi() == 0 || output_buffers().count().n_midi () == 0) { - return; - } - - const DataType type = DataType::MIDI; - - // Copy any MIDI 1:1 to outputs - assert(bufs.count().n_midi() == output_buffers().count().n_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); - } -} - -void -IO::collect_input (BufferSet& outs, nframes_t nframes, nframes_t offset) -{ - assert(outs.available() >= n_inputs()); - - if (n_inputs() == ChanCount::ZERO) - return; - - outs.set_count(n_inputs()); - - 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), nframes, offset); - } - + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { + i->get_buffer(nframes).silence (nframes); } } void -IO::just_meter_input (nframes_t start_frame, nframes_t end_frame, - nframes_t nframes, nframes_t offset) +IO::check_bundles_connected () { - BufferSet& bufs = _session.get_scratch_buffers (n_inputs()); - - collect_input (bufs, nframes, offset); - - _meter->run_in_place(bufs, start_frame, end_frame, nframes, offset); -} - - -void -IO::check_bundles_connected_to_inputs () -{ - check_bundles (_bundles_connected_to_inputs, inputs()); + check_bundles (_bundles_connected, ports()); } +/** Check the bundles in list to see which are connected to a given PortSet, + * and update list with those that are connected such that every port on every + * bundle channel x is connected to port x in ports. + */ void -IO::check_bundles_connected_to_outputs () +IO::check_bundles (std::vector& list, const PortSet& ports) { - check_bundles (_bundles_connected_to_outputs, outputs()); -} + std::vector new_list; -void -IO::check_bundles (std::vector& list, const PortSet& ports) -{ - std::vector new_list; - - for (std::vector::iterator i = list.begin(); i != list.end(); ++i) { + for (std::vector::iterator i = list.begin(); i != list.end(); ++i) { - uint32_t const N = i->bundle->nchannels (); + uint32_t const N = (*i)->bundle->nchannels().n_total(); - if (ports.num_ports (default_type()) < N) { + if (_ports.num_ports() < N) { continue; } @@ -370,7 +132,7 @@ IO::check_bundles (std::vector& list, const PortSet& ports) 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); + 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; @@ -386,8 +148,7 @@ IO::check_bundles (std::vector& list, const PortSet& ports) if (ok) { new_list.push_back (*i); } else { - i->configuration_changed.disconnect (); - i->ports_changed.disconnect (); + delete *i; } } @@ -396,43 +157,7 @@ IO::check_bundles (std::vector& list, const PortSet& ports) int -IO::disconnect_input (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 ( ! _inputs.contains(our_port)) { - 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; - } - - check_bundles_connected_to_inputs (); - } - } - - input_changed (ConnectionsChanged, src); /* EMIT SIGNAL */ - _session.set_dirty (); - - return 0; -} - -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; @@ -440,66 +165,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; } - } - } - - 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; } - check_bundles_connected_to_outputs (); + check_bundles_connected (); } } - output_changed (ConnectionsChanged, src); /* EMIT SIGNAL */ + changed (IOChange (IOChange::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; @@ -508,97 +202,64 @@ 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; } } } - output_changed (ConnectionsChanged, src); /* EMIT SIGNAL */ + changed (IOChange (IOChange::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.n_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); + IOChange change; { BLOCK_PROCESS_CALLBACK (); - { Glib::Mutex::Lock lm (io_lock); - if (n_outputs() <= _output_minimum) { - /* sorry, you can't do this */ - return -1; - } + ChanCount before = _ports.count (); - if (_outputs.remove(port)) { - change = IOChange (change|ConfigurationChanged); + if (_ports.remove(port)) { + change.type = IOChange::Type (change.type | IOChange::ConfigurationChanged); + change.before = before; + change.after = _ports.count (); if (port->connected()) { - change = IOChange (change|ConnectionsChanged); - } + change.type = IOChange::Type (change.type | IOChange::ConnectionsChanged); + } _session.engine().unregister_port (*port); - check_bundles_connected_to_outputs (); - - setup_peak_meters (); - reset_panner (); + check_bundles_connected (); } } - PortCountChanged (n_outputs()); /* EMIT SIGNAL */ + PortCountChanged (n_ports()); /* EMIT SIGNAL */ } - if (change == ConfigurationChanged) { - setup_bundles_for_inputs_and_outputs (); + if (change.type & IOChange::ConfigurationChanged) { + setup_bundle (); } - if (change != NoChange) { - output_changed (change, src); + if (change.type != IOChange::NoChange) { + changed (change, src); _session.set_dirty (); return 0; } @@ -606,594 +267,197 @@ IO::remove_output_port (Port* port, void* src) return -1; } -/** Add an output port. +/** Add a port. * - * @param destination Name of input port to connect new port to. + * @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_output_port (string destination, void* src, DataType type) +IO::add_port (string destination, void* src, DataType type) { Port* our_port; - if (type == DataType::NIL) + if (type == DataType::NIL) { type = _default_type; + } + + IOChange change; { BLOCK_PROCESS_CALLBACK (); - - { + + { Glib::Mutex::Lock lm (io_lock); - - if (n_outputs() >= _output_maximum) { - return -1; - } - - /* Create a new output port */ - - string portname = build_legal_port_name (type, false); - - if ((our_port = _session.engine().register_output_port (type, portname, _public_ports)) == 0) { - error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg; - return -1; + + /* Create a new port */ + + 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; + } } - - _outputs.add (our_port); - setup_peak_meters (); - reset_panner (); + + change.before = _ports.count (); + _ports.add (our_port); } - PortCountChanged (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 */ - setup_bundles_for_inputs_and_outputs (); + change.type = IOChange::ConfigurationChanged; + change.after = _ports.count (); + changed (change, src); /* EMIT SIGNAL */ + setup_bundle (); _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); - check_bundles_connected_to_inputs (); - - setup_peak_meters (); - reset_panner (); - } + check_bundles_connected (); } - - PortCountChanged (n_inputs ()); /* EMIT SIGNAL */ } - if (change == ConfigurationChanged) { - setup_bundles_for_inputs_and_outputs (); - } + changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */ - if (change != NoChange) { - input_changed (change, src); - _session.set_dirty (); - return 0; - } - - return -1; + return 0; } - -/** Add an input port. - * - * @param type Data type of port. The appropriate 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; - - if (type == DataType::NIL) - type = _default_type; + Port* port = 0; + bool changed = false; - { - BLOCK_PROCESS_CALLBACK (); - - { - Glib::Mutex::Lock lm (io_lock); + for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { - if (_input_maximum.get(type) >= 0 && n_inputs().get (type) >= _input_maximum.get (type)) { - return -1; - } + const size_t n = count.get(*t); - /* Create a new input port */ - - string portname = build_legal_port_name (type, true); + /* remove unused ports */ + for (size_t i = n_ports().get(*t); i > n; --i) { + port = _ports.port(*t, i-1); - if ((our_port = _session.engine().register_input_port (type, portname, _public_ports)) == 0) { - error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg; - return -1; - } + assert(port); + _ports.remove(port); + _session.engine().unregister_port (*port); - _inputs.add (our_port); - setup_peak_meters (); - reset_panner (); + changed = true; } - PortCountChanged (n_inputs()); /* EMIT SIGNAL */ - } + /* create any necessary new ports */ + while (n_ports().get(*t) < n) { - if (source.length()) { + string portname = build_legal_port_name (*t); - if (_session.engine().connect (source, our_port->name())) { - return -1; - } - } + try { - // pan_changed (src); /* EMIT SIGNAL */ - input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ - setup_bundles_for_inputs_and_outputs (); - _session.set_dirty (); - - return 0; -} + 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; + } + } + } -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); + catch (AudioEngine::PortRegistrationFailure& err) { + /* pass it on */ + throw; } - check_bundles_connected_to_inputs (); + _ports.add (port); + changed = true; } } - - 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); - } - - check_bundles_connected_to_outputs (); - } - } - - 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); - - assert(input_port); - _inputs.remove(input_port); - _session.engine().unregister_port (*input_port); - - changed = true; - } - - /* create any necessary new ports */ - while (n_inputs().get(*t) < n) { - - string portname = build_legal_port_name (*t, true); - - try { - - if ((input_port = _session.engine().register_input_port (*t, portname, _public_ports)) == 0) { - error << string_compose(_("IO: cannot register input 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); - changed = true; - } - } - if (changed) { - check_bundles_connected_to_inputs (); - setup_peak_meters (); - reset_panner (); - PortCountChanged (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 PortCountChanged 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) { - - string portname = build_legal_port_name (*t, true); - - try { - if ((port = _session.engine().register_input_port (*t, portname, _public_ports)) == 0) { - error << string_compose(_("IO: cannot register input port %1"), portname) << 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) { - - string portname = build_legal_port_name (*t, false); - - try { - if ((port = _session.engine().register_output_port (*t, portname, _public_ports)) == 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 (); - } - - _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) { - check_bundles_connected_to_outputs (); - output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ - } - - if (in_changed) { - check_bundles_connected_to_inputs (); - input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ - } - - if (in_changed || out_changed) { - PortCountChanged (max (n_outputs(), n_inputs())); /* EMIT SIGNAL */ - setup_bundles_for_inputs_and_outputs (); - _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); - } else { - changed = ensure_inputs_locked (count, clear, src); - } + IOChange change; - if (changed) { - input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ - setup_bundles_for_inputs_and_outputs (); - _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) { - - string portname = build_legal_port_name (*t, false); - - if ((output_port = _session.engine().register_output_port (*t, portname, _public_ports)) == 0) { - error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg; - return -1; - } - - _outputs.add (output_port); - changed = true; - setup_peak_meters (); - - if (need_pan_reset) { - reset_panner (); - } - } - } + change.before = _ports.count (); - if (changed) { - check_bundles_connected_to_outputs (); - PortCountChanged (n_outputs()); /* EMIT SIGNAL */ - _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); + changed = ensure_ports_locked (count, clear, src); } else { - changed = ensure_outputs_locked (count, clear, src); + changed = ensure_ports_locked (count, clear, src); } if (changed) { - output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */ - setup_bundles_for_inputs_and_outputs (); + change.after = _ports.count (); + change.type = IOChange::ConfigurationChanged; + this->changed (change, src); /* EMIT SIGNAL */ + setup_bundle (); + _session.set_dirty (); } return 0; } -gain_t -IO::effective_gain () const -{ - if (_gain_control->automation_playback()) { - return _gain_control->get_value(); - } else { - return _desired_gain; - } -} - -void -IO::reset_panner () -{ - if (panners_legal) { - if (!no_panner_reset) { - _panner->reset (n_outputs().n_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 () -{ - _panner->reset (n_outputs().n_audio(), pans_required()); - _panner->load (); // automation - panner_legal_c.disconnect (); - return 0; -} - -void -IO::defer_pan_reset () +IO::ensure_io (ChanCount count, bool clear, void* src) { - 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) { @@ -1201,7 +465,7 @@ 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]; @@ -1214,118 +478,56 @@ IO::state (bool full_state) 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()); - for ( - std::vector::iterator i = _bundles_connected_to_inputs.begin(); - i != _bundles_connected_to_inputs.end(); - ++i - ) - { - XMLNode* n = new XMLNode ("InputBundle"); - n->add_property ("name", i->bundle->name ()); + 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); } - for ( - std::vector::iterator i = _bundles_connected_to_outputs.begin(); - i != _bundles_connected_to_outputs.end(); - ++i - ) - { - XMLNode* n = new XMLNode ("OutputBundle"); - n->add_property ("name", i->bundle->name ()); - node->add_child_nocopy (*n); - } - - str = ""; + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { - for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) { - vector connections; + XMLNode* pnode = new XMLNode (X_("Port")); + pnode->add_property (X_("type"), i->type().to_string()); + pnode->add_property (X_("name"), i->name()); + if (i->get_connections (connections)) { - str += '{'; - for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++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 (*ci); - } - - str += '}'; - } else { - str += "{}"; - } - } - - node->add_property ("inputs", str); + XMLNode* cnode = new XMLNode (X_("Connection")); - str = ""; - - for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - - vector connections; - - if (i->get_connections (connections)) { - - str += '{'; - - for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) { - if (n) { - str += ','; - } - - str += _session.engine().make_port_name_relative (*ci); + cnode->add_property (X_("other"), _session.engine().make_port_name_relative (*ci)); + pnode->add_child_nocopy (*cnode); } - - str += '}'; - - } 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); - - /* To make backwards compatibility a bit easier, write ChanCount::INFINITE to the session file - as -1. - */ - - int const in_max = _input_maximum == ChanCount::INFINITE ? -1 : _input_maximum.get(_default_type); - int const out_max = _output_maximum == ChanCount::INFINITE ? -1 : _output_maximum.get(_default_type); - snprintf (buf, sizeof(buf)-1, "%d,%d,%d,%d", _input_minimum.get(_default_type), in_max, _output_minimum.get(_default_type), out_max); - - node->add_property ("iolimits", buf); - - /* automation */ - - if (full_state) - node->add_child_nocopy (get_automation_state()); + 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")); @@ -1340,198 +542,91 @@ 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 */ - } - - if ((prop = node.property ("id")) != 0) { - _id = prop->value (); - } - - int in_min = -1; - int in_max = -1; - int out_min = -1; - int out_max = -1; - - if ((prop = node.property ("iolimits")) != 0) { - sscanf (prop->value().c_str(), "%d,%d,%d,%d", - &in_min, &in_max, &out_min, &out_max); - - /* Correct for the difference between the way we write things to session files and the - way things are described by ChanCount; see comments in io.h about what the different - ChanCount values mean. */ - - if (in_min < 0) { - _input_minimum = ChanCount::ZERO; - } else { - _input_minimum = ChanCount (_default_type, in_min); - } - - if (in_max < 0) { - _input_maximum = ChanCount::INFINITE; - } else { - _input_maximum = ChanCount (_default_type, in_max); - } - - if (out_min < 0) { - _output_minimum = ChanCount::ZERO; - } else { - _output_minimum = ChanCount (_default_type, out_min); - } - - if (out_max < 0) { - _output_maximum = ChanCount::INFINITE; - } else { - _output_maximum = ChanCount (_default_type, out_max); - } - } - - if ((prop = node.property ("gain")) != 0) { - set_gain (atof (prop->value().c_str()), this); - _gain = _desired_gain; + set_name (prop->value()); } - if ((prop = node.property ("automation-state")) != 0 || (prop = node.property ("automation-style")) != 0) { - /* old school automation handling */ + if ((prop = node.property (X_("default-type"))) != 0) { + _default_type = DataType(prop->value()); + assert(_default_type != DataType::NIL); } - for (iter = node.children().begin(); iter != node.children().end(); ++iter) { - - // Old school Panner. - if ((*iter)->name() == "Panner") { - if (_panner == 0) { - _panner = new Panner (_name, _session); - } - _panner->set_state (**iter); - } - - if ((*iter)->name() == "Processor") { - if ((*iter)->property ("type") && ((*iter)->property ("type")->value() == "panner" ) ) { - if (_panner == 0) { - _panner = new Panner (_name, _session); - } - _panner->set_state (**iter); - } - } - - if ((*iter)->name() == X_("Automation")) { - - set_automation_state (*(*iter), Evoral::Parameter(GainAutomation)); - } - - if ((*iter)->name() == X_("controllable")) { - if ((prop = (*iter)->property("name")) != 0 && prop->value() == "gaincontrol") { - _gain_control->set_state (**iter); - } - } + if ((prop = node.property ("id")) != 0) { + _id = prop->value (); } - if (ports_legal) { - - if (create_ports (node)) { - return -1; - } - - } else { - - port_legal_c = PortsLegal.connect (mem_fun (*this, &IO::ports_became_legal)); + if ((prop = node.property ("direction")) != 0) { + _direction = (Direction) string_2_enum (prop->value(), _direction); } - if( !_panner ) - _panner = new Panner( _name, _session ); - if (panners_legal) { - reset_panner (); - } else { - panner_legal_c = PannersLegal.connect (mem_fun (*this, &IO::panners_became_legal)); + if (create_ports (node, version)) { + return -1; } if (connecting_legal) { - if (make_connections (node)) { + if (make_connections (node, version, false)) { 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 = false; + ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this)); } + return 0; } int -IO::load_automation (string path) +IO::set_state_2X (const XMLNode& node, int version, bool in) { - string fullpath; - ifstream in; - char line[128]; - uint32_t linecnt = 0; - float version; + const XMLProperty* prop; + XMLNodeConstIterator iter; LocaleGuard lg (X_("POSIX")); - fullpath = Glib::build_filename(_session.automation_dir(), path); - - in.open (fullpath.c_str()); + /* force use of non-localized representation of decimal point, + since we use it a lot in XML files and so forth. + */ - if (!in) { - fullpath = Glib::build_filename(_session.automation_dir(), _session.snap_name() + '-' + path); + if (node.name() != state_node_name) { + error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg; + return -1; + } - in.open (fullpath.c_str()); + if ((prop = node.property ("name")) != 0) { + set_name (prop->value()); + } - if (!in) { - error << string_compose(_("%1: cannot open automation event file \"%2\""), _name, fullpath) << endmsg; - return -1; - } + if ((prop = node.property (X_("default-type"))) != 0) { + _default_type = DataType(prop->value()); + assert(_default_type != DataType::NIL); } - clear_automation (); + if ((prop = node.property ("id")) != 0) { + _id = prop->value (); + } - while (in.getline (line, sizeof(line), '\n')) { - char type; - nframes_t when; - double value; + _direction = in ? Input : Output; - 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; - } - } else { - error << string_compose(_("no version information in automation event file \"%1\""), path) << endmsg; - return -1; - } + if (create_ports (node, version)) { + return -1; + } - continue; - } + if (connecting_legal) { - 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; + if (make_connections_2X (node, version, in)) { + return -1; } - switch (type) { - case 'g': - _gain_control->list()->fast_simple_add (when, value); - break; - - case 's': - break; - - case 'm': - break; - - case 'p': - /* older (pre-1.0) versions of ardour used this */ - break; + } else { - default: - warning << _("dubious automation event found (and ignored)") << endmsg; - } + 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; @@ -1542,50 +637,24 @@ IO::connecting_became_legal () { int ret; - if (pending_state_node == 0) { - fatal << _("IO::connecting_became_legal() called without a pending state node") << endmsg; - /*NOTREACHED*/ - return -1; - } + assert (pending_state_node); connection_legal_c.disconnect (); - ret = make_connections (*pending_state_node); - - if (ports_legal) { - delete pending_state_node; - pending_state_node = 0; - } - - return ret; -} -int -IO::ports_became_legal () -{ - int ret; + ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in); - if (pending_state_node == 0) { - fatal << _("IO::ports_became_legal() called without a pending state node") << endmsg; - /*NOTREACHED*/ - return -1; - } - - port_legal_c.disconnect (); - - ret = create_ports (*pending_state_node); - - if (connecting_legal) { - delete pending_state_node; - pending_state_node = 0; - } + delete pending_state_node; + pending_state_node = 0; return ret; } boost::shared_ptr -IO::find_possible_bundle (const string &desired_name, const string &default_name, const string &bundle_type_name) +IO::find_possible_bundle (const string &desired_name) { static const string digits = "0123456789"; + const string &default_name = (_direction == Input ? _("in") : _("out")); + const string &bundle_type_name = (_direction == Input ? _("input") : _("output")); boost::shared_ptr c = _session.bundle_by_name (desired_name); @@ -1600,7 +669,7 @@ IO::find_possible_bundle (const string &desired_name, const string &default_name // find numeric suffix of desired name bundle_number = 0; - + last_non_digit_pos = desired_name.find_last_not_of(digits); if (last_non_digit_pos != string::npos) { @@ -1608,16 +677,16 @@ IO::find_possible_bundle (const string &desired_name, const string &default_name s << desired_name.substr(last_non_digit_pos); s >> bundle_number; } - + // see if it's a stereo connection e.g. "in 3+4" 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; 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) { + 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; @@ -1635,23 +704,23 @@ IO::find_possible_bundle (const string &desired_name, const string &default_name // find highest set bit mask = 1; - while ((mask <= bundle_number) && (mask <<= 1)); - - // "wrap" bundle number into largest possible power of 2 + while ((mask <= bundle_number) && (mask <<= 1)) {} + + // "wrap" bundle number into largest possible power of 2 // that works... while (mask) { if (bundle_number & mask) { bundle_number &= ~mask; - + 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) { @@ -1675,189 +744,270 @@ IO::find_possible_bundle (const string &desired_name, const string &default_name } int -IO::create_ports (const XMLNode& node) +IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boost::shared_ptr& /*c*/) { XMLProperty const * prop; - uint32_t num_inputs = 0; - uint32_t num_outputs = 0; + XMLNodeList children = node.children (); - if ((prop = node.property ("input-connection")) != 0) { + uint32_t n_audio = 0; - boost::shared_ptr c = find_possible_bundle (prop->value(), _("in"), _("input")); - - if (!c) { - return -1; - } + 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; + } + } - num_inputs = c->nchannels(); + ChanCount cnt; + cnt.set_audio (n_audio); + n = ChanCount::max (n, cnt); - } else if ((prop = node.property ("inputs")) != 0) { + return 0; +} - num_inputs = count (prop->value().begin(), prop->value().end(), '{'); +int +IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr& c) +{ + if (version < 3000) { + return get_port_counts_2X (node, version, n, c); } - - if ((prop = node.property ("output-connection")) != 0) { - boost::shared_ptr c = find_possible_bundle(prop->value(), _("out"), _("output")); + XMLProperty const * prop; + XMLNodeConstIterator iter; + uint32_t n_audio = 0; + uint32_t n_midi = 0; + ChanCount cnt; - if (!c) { - return -1; - } + n = n_ports(); + + if ((prop = node.property ("connection")) != 0) { + + if ((c = find_possible_bundle (prop->value())) != 0) { + n = ChanCount::max (n, c->nchannels()); + } + return 0; + } + + for (iter = node.children().begin(); iter != node.children().end(); ++iter) { - num_outputs = c->nchannels (); - - } else if ((prop = node.property ("outputs")) != 0) { + if ((*iter)->name() == X_("Bundle")) { + if ((c = find_possible_bundle (prop->value())) != 0) { + n = ChanCount::max (n, c->nchannels()); + return 0; + } else { + return -1; + } + } + + if ((*iter)->name() == X_("Port")) { + prop = (*iter)->property (X_("type")); - num_outputs = count (prop->value().begin(), prop->value().end(), '{'); + if (!prop) { + continue; + } + + 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; +} + +int +IO::create_ports (const XMLNode& node, int version) +{ + ChanCount n; + boost::shared_ptr c; - if (ensure_io (ChanCount (_default_type, num_inputs), - ChanCount (_default_type, num_outputs), - true, this)) { - + 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; - - set_deferred_state (); - - PortsCreated(); + /* XXX use c */ + + return 0; +} + +int +IO::make_connections (const XMLNode& node, int version, bool in) +{ + if (version < 3000) { + return make_connections_2X (node, version, in); + } + + const XMLProperty* prop; + + for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) { + + 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); + } + } + + return 0; + } + + if ((*i)->name() == "Port") { + + prop = (*i)->property (X_("name")); + + if (!prop) { + continue; + } + + 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::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) { - boost::shared_ptr c = find_possible_bundle (prop->value(), _("in"), _("input")); - - if (!c) { - return -1; - } + /* XXX: bundles ("connections" as was) */ - connect_input_ports_to_bundle (c, this); + if ((prop = node.property ("inputs")) != 0 && in) { - } 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; - } - } + string::size_type ostart = 0; + string::size_type start = 0; + string::size_type end = 0; + int i = 0; + int n; + vector ports; - if ((prop = node.property ("output-connection")) != 0) { - boost::shared_ptr c = find_possible_bundle (prop->value(), _("out"), _("output")); - - if (!c) { - return -1; - } - - connect_output_ports_to_bundle (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; - } - } + string const str = prop->value (); - for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) { + while ((start = str.find_first_of ('{', ostart)) != string::npos) { + start += 1; - if ((*i)->name() == "InputBundle") { - XMLProperty const * prop = (*i)->property ("name"); - if (prop) { - boost::shared_ptr b = find_possible_bundle (prop->value(), _("in"), _("input")); - if (b) { - connect_input_ports_to_bundle (b, this); - } + 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 if ((*i)->name() == "OutputBundle") { - XMLProperty const * prop = (*i)->property ("name"); - if (prop) { - boost::shared_ptr b = find_possible_bundle (prop->value(), _("out"), _("output")); - if (b) { - connect_output_ports_to_bundle (b, this); - } + + 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) { + + + 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++; } - } - - 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; } - // FIXME: audio-only - if (ensure_inputs (ChanCount(DataType::AUDIO, 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; } // 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; } @@ -1872,19 +1022,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); } } @@ -1913,7 +1063,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)); } @@ -1934,7 +1084,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)); } @@ -1945,466 +1095,159 @@ IO::parse_gain_string (const string& str, vector& ports) bool IO::set_name (const string& requested_name) { - if (requested_name == _name) { + string name = requested_name; + + if (_name == name) { return true; } - - string name; - Route *rt; - if ( (rt = dynamic_cast(this))) { - name = Route::ensure_track_or_route_name(requested_name, _session); - } else { - name = requested_name; - } - /* 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->name(); - current_name.replace (current_name.find (_name), _name.length(), name); - i->set_name (current_name); - } + replace_all (name, ":", "-"); - for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { + 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); + current_name.replace (current_name.find (_name), _name.val().length(), name); i->set_name (current_name); } - bool const r = SessionObject::set_name(name); + bool const r = SessionObject::set_name (name); - setup_bundles_for_inputs_and_outputs (); + setup_bundle (); return r; } -void -IO::set_input_minimum (ChanCount n) -{ - _input_minimum = n; -} - -void -IO::set_input_maximum (ChanCount n) -{ - _input_maximum = n; -} - -void -IO::set_output_minimum (ChanCount n) -{ - _output_minimum = n; -} - -void -IO::set_output_maximum (ChanCount n) -{ - _output_maximum = n; -} - void 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 -{ - nframes_t max_latency; - nframes_t latency; - - max_latency = 0; - - /* io lock not taken - must be protected by other means */ - - for (PortSet::const_iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - if ((latency = i->total_latency ()) > max_latency) { - max_latency = latency; - } - } - - return max_latency; -} - -nframes_t -IO::input_latency () const -{ - nframes_t max_latency; - nframes_t latency; - - max_latency = 0; - - /* io lock not taken - must be protected by other means */ - - for (PortSet::const_iterator i = _inputs.begin(); i != _inputs.end(); ++i) { - if ((latency = i->total_latency ()) > max_latency) { - max_latency = latency; - } - } - - return max_latency; -} - -int -IO::connect_input_ports_to_bundle (boost::shared_ptr c, void* src) -{ - { - BLOCK_PROCESS_CALLBACK (); - Glib::Mutex::Lock lm2 (io_lock); - - /* Connect to the bundle, not worrying about any connections - that are already made. */ - - uint32_t cnt = c->nchannels (); - - for (uint32_t n = 0; n < cnt; ++n) { - const Bundle::PortList& pl = c->channel_ports (n); - - for (Bundle::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 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_to_inputs.begin(); - while (i != _bundles_connected_to_inputs.end() && i->bundle != ub) { - ++i; - } - - if (i == _bundles_connected_to_inputs.end()) { - /* We don't, so make a note */ - _bundles_connected_to_inputs.push_back (UserBundleInfo (this, ub)); - } - } - } - - input_changed (IOChange (ConfigurationChanged|ConnectionsChanged), src); /* EMIT SIGNAL */ - return 0; -} - -int -IO::connect_output_ports_to_bundle (boost::shared_ptr c, void* src) -{ - { - BLOCK_PROCESS_CALLBACK (); - Glib::Mutex::Lock lm2 (io_lock); - - /* Connect to the bundle, not worrying about any connections - that are already made. */ - - uint32_t cnt = c->nchannels (); - - for (uint32_t n = 0; n < cnt; ++n) { - - const Bundle::PortList& pl = c->channel_ports (n); - - for (Bundle::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 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_to_outputs.begin(); - while (i != _bundles_connected_to_outputs.end() && i->bundle != ub) { - ++i; - } - - if (i == _bundles_connected_to_outputs.end()) { - /* We don't, so make a note */ - _bundles_connected_to_outputs.push_back (UserBundleInfo (this, ub)); - } - } - } - - output_changed (IOChange (ConnectionsChanged|ConfigurationChanged), src); /* EMIT SIGNAL */ - - return 0; -} - -int -IO::disable_connecting () -{ - connecting_legal = false; - return 0; -} - -int -IO::enable_connecting () -{ - connecting_legal = true; - return ConnectingLegal (); -} - -int -IO::disable_ports () -{ - ports_legal = false; - return 0; -} - -int -IO::enable_ports () -{ - ports_legal = true; - return PortsLegal (); -} - -int -IO::disable_panners (void) -{ - panners_legal = false; - return 0; -} - -int -IO::reset_panners () -{ - panners_legal = true; - return PannersLegal (); -} - -void -IO::bundle_configuration_changed () -{ - //XXX -// connect_input_ports_to_bundle (_input_bundle, this); -} - -void -IO::bundle_ports_changed (int ignored) -{ - //XXX -// connect_output_ports_to_bundle (_output_bundle, this); -} - -void -IO::GainControl::set_value (float val) -{ - // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05)) - if (val > 1.99526231f) - val = 1.99526231f; - - _io->set_gain (val, this); - - AutomationControl::set_value(val); -} - -float -IO::GainControl::get_value (void) const +nframes_t +IO::latency () const { - return AutomationControl::get_value(); -} + nframes_t max_latency; + nframes_t latency; -void -IO::setup_peak_meters() -{ - ChanCount max_streams = std::max (_inputs.count(), _outputs.count()); - _meter->configure_io (max_streams, max_streams); -} + max_latency = 0; -/** - Update the peak meters. + /* io lock not taken - must be protected by other means */ - 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 */ -} + for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) { + if ((latency = i->total_latency ()) > max_latency) { + max_latency = latency; + } + } -void -IO::meter () -{ - // FIXME: Ugly. Meter should manage the lock, if it's necessary - - Glib::Mutex::Lock lm (io_lock); // READER: meter thread. - _meter->meter(); + return max_latency; } void -IO::clear_automation () +IO::update_port_total_latencies () { - data().clear (); // clears gain automation - _panner->data().clear(); + /* io_lock, not taken: function must be called from Session::process() calltree */ + + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { + _session.engine().update_total_latency (*i); + } } -void -IO::set_parameter_automation_state (Evoral::Parameter param, AutoState state) +int +IO::connect_ports_to_bundle (boost::shared_ptr c, void* src) { - // XXX: would be nice to get rid of this special hack - - if (param.type() == GainAutomation) { - - bool changed = false; + { + BLOCK_PROCESS_CALLBACK (); + Glib::Mutex::Lock lm2 (io_lock); - { - Glib::Mutex::Lock lm (control_lock()); + c->connect (_bundle, _session.engine()); - boost::shared_ptr gain_auto - = boost::dynamic_pointer_cast(_gain_control->list()); + /* If this is a UserBundle, make a note of what we've done */ - if (state != gain_auto->automation_state()) { - changed = true; - _last_automation_snapshot = 0; - gain_auto->set_automation_state (state); + boost::shared_ptr ub = boost::dynamic_pointer_cast (c); + if (ub) { - if (state != Off) { - // FIXME: shouldn't this use Curve? - set_gain (gain_auto->eval (_session.transport_frame()), this); - } + /* 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 (changed) { - _session.set_dirty (); + if (i == _bundles_connected.end()) { + /* We don't, so make a note */ + _bundles_connected.push_back (new UserBundleInfo (this, ub)); + } } - - } else { - AutomatableControls::set_parameter_automation_state(param, state); } -} -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); + changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */ + return 0; } -void -IO::set_gain (gain_t val, void *src) +int +IO::disconnect_ports_from_bundle (boost::shared_ptr c, void* src) { - // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05)) - if (val > 1.99526231f) { - val = 1.99526231f; - } + { + BLOCK_PROCESS_CALLBACK (); + Glib::Mutex::Lock lm2 (io_lock); - if (src != _gain_control.get()) { - _gain_control->set_value(val); - // bit twisty, this will come back and call us again - // (this keeps control in sync with reality) - return; - } + c->disconnect (_bundle, _session.engine()); - { - Glib::Mutex::Lock dm (declick_lock); - _desired_gain = val; - } + /* If this is a UserBundle, make a note of what we've done */ - if (_session.transport_stopped()) { - // _gain = val; - } - - /* - if (_session.transport_stopped() && src != 0 && src != this && _gain_control->automation_write()) { - _gain_control->list()->add (_session.transport_frame(), val); - - } - */ + boost::shared_ptr ub = boost::dynamic_pointer_cast (c); + if (ub) { - _session.set_dirty(); -} + std::vector::iterator i = _bundles_connected.begin(); + while (i != _bundles_connected.end() && (*i)->bundle != ub) { + ++i; + } -void -IO::start_pan_touch (uint32_t which) -{ - if (which < _panner->npanners()) { - (*_panner).pan_control(which)->start_touch(); + if (i != _bundles_connected.end()) { + delete *i; + _bundles_connected.erase (i); + } + } } + + changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */ + return 0; } -void -IO::end_pan_touch (uint32_t which) -{ - if (which < _panner->npanners()) { - (*_panner).pan_control(which)->stop_touch(); - } +int +IO::disable_connecting () +{ + connecting_legal = false; + return 0; } -void -IO::automation_snapshot (nframes_t now, bool force) +int +IO::enable_connecting () { - AutomatableControls::automation_snapshot (now, force); - // XXX: This seems to be wrong. - // drobilla: shouldnt automation_snapshot for panner be called - // "automagically" because its an Automatable now ? - // - // we could dump this whole method then. <3 - - if (_last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) { - _panner->automation_snapshot (now, force); - } - - _panner->automation_snapshot (now, force); - _last_automation_snapshot = now; + connecting_legal = true; + boost::optional r = ConnectingLegal (); + return r.get_value_or (0); } void -IO::transport_stopped (nframes_t frame) +IO::bundle_changed (Bundle::Change /*c*/) { - _gain_control->list()->reposition_for_rt_add (frame); - - if (_gain_control->automation_state() != Off) { - - /* the src=0 condition is a special signal to not propagate - automation gain changes into the mix group when locating. - */ - - // FIXME: shouldn't this use Curve? - set_gain (_gain_control->list()->eval (frame), 0); - } - - _panner->transport_stopped (frame); + //XXX +// connect_input_ports_to_bundle (_input_bundle, this); } + string -IO::build_legal_port_name (DataType type, bool in) +IO::build_legal_port_name (DataType type) { const int name_size = jack_port_name_size(); int limit; string suffix; - int maxports; if (type == DataType::AUDIO) { suffix = _("audio"); @@ -2413,53 +1256,42 @@ IO::build_legal_port_name (DataType type, bool in) } else { throw unknown_type(); } - - if (in) { - suffix += _("_in"); - maxports = _input_maximum.get(type); + + /* 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. + */ + + if (_direction == Input) { + suffix += X_("_in"); } else { - suffix += _("_out"); - maxports = _output_maximum.get(type); + suffix += X_("_out"); } - - if (maxports == 1) { - // allow space for the slash + the suffix - limit = name_size - _session.engine().client_name().length() - (suffix.length() + 1); - char buf[name_size+1]; - snprintf (buf, name_size+1, ("%.*s/%s"), limit, _name.c_str(), suffix.c_str()); - return string (buf); - } - + // allow up to 4 digits for the output port number, plus the slash, suffix and extra space limit = name_size - _session.engine().client_name().length() - (suffix.length() + 5); char buf1[name_size+1]; char buf2[name_size+1]; - - snprintf (buf1, name_size+1, ("%.*s/%s"), limit, _name.c_str(), suffix.c_str()); - - int port_number; - - if (in) { - port_number = find_input_port_hole (buf1); - } else { - port_number = find_output_port_hole (buf1); - } - + + snprintf (buf1, name_size+1, ("%.*s/%s"), limit, _name.val().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); } int32_t -IO::find_input_port_hole (const char* base) +IO::find_port_hole (const char* base) { /* CALLER MUST HOLD IO LOCK */ uint32_t n; - if (_inputs.empty()) { + if (_ports.empty()) { return 1; } @@ -2468,297 +1300,300 @@ IO::find_input_port_hole (const char* base) for (n = 1; n < 9999; ++n) { char buf[jack_port_name_size()]; - PortSet::iterator i = _inputs.begin(); + PortSet::iterator i = _ports.begin(); snprintf (buf, jack_port_name_size(), _("%s %u"), base, n); - for ( ; i != _inputs.end(); ++i) { + for ( ; i != _ports.end(); ++i) { if (i->name() == buf) { break; } } - if (i == _inputs.end()) { + if (i == _ports.end()) { break; } } return n; } -int32_t -IO::find_output_port_hole (const char* base) + +AudioPort* +IO::audio(uint32_t n) const { - /* CALLER MUST HOLD IO LOCK */ + return _ports.nth_audio_port (n); - uint32_t n; +} - if (_outputs.empty()) { - return 1; +MidiPort* +IO::midi(uint32_t n) const +{ + return _ports.nth_midi_port (n); +} + +/** + * Setup a bundle that describe our inputs or outputs. Also creates the bundle if necessary. + */ + +void +IO::setup_bundle () +{ + char buf[32]; + + if (!_bundle) { + _bundle.reset (new Bundle (_direction == Input)); } - /* we only allow up to 4 characters for the port number - */ + _bundle->suspend_signals (); - for (n = 1; n < 9999; ++n) { - char buf[jack_port_name_size()]; - PortSet::iterator i = _outputs.begin(); + _bundle->remove_channels (); - snprintf (buf, jack_port_name_size(), _("%s %u"), base, n); + 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); - for ( ; i != _outputs.end(); ++i) { - if (i->name() == buf) { - break; - } - } + int c = 0; + for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) { - if (i == _outputs.end()) { - break; + 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; } + } - - return n; -} -void -IO::set_active (bool yn) -{ - _active = yn; - active_changed(); /* EMIT SIGNAL */ + _bundle->resume_signals (); } -AudioPort* -IO::audio_input(uint32_t n) const +/** @return Bundles connected to our ports */ +BundleList +IO::bundles_connected () { - return dynamic_cast(input(n)); -} + BundleList bundles; -AudioPort* -IO::audio_output(uint32_t n) const -{ - return dynamic_cast(output(n)); -} + /* User bundles */ + for (std::vector::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) { + bundles.push_back ((*i)->bundle); + } -MidiPort* -IO::midi_input(uint32_t n) const -{ - return dynamic_cast(input(n)); -} + /* 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); + } + } -MidiPort* -IO::midi_output(uint32_t n) const -{ - return dynamic_cast(output(n)); -} + /* Route bundles */ -void -IO::set_phase_invert (bool yn, void *src) -{ - if (_phase_invert != yn) { - _phase_invert = yn; - // phase_invert_changed (src); /* EMIT SIGNAL */ + 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()); + } + } } + + return bundles; } -void -IO::set_denormal_protection (bool yn, void *src) + +IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr b) { - if (_denormal_protection != yn) { - _denormal_protection = yn; - // denormal_protection_changed (src); /* EMIT SIGNAL */ - } + bundle = b; + b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1)); } -void -IO::update_port_total_latencies () +std::string +IO::bundle_channel_name (uint32_t c, uint32_t n, DataType t) const { - /* io_lock, not taken: function must be called from Session::process() calltree */ + char buf[32]; - for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) { - _session.engine().update_total_latency (*i); - } + if (t == DataType::AUDIO) { + + 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; - for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { - _session.engine().update_total_latency (*i); } + + return ""; } +string +IO::name_from_state (const XMLNode& node) +{ + const XMLProperty* prop; -/** - * Setup bundles that describe our inputs and outputs. - */ + if ((prop = node.property ("name")) != 0) { + return prop->value(); + } + + return string(); +} void -IO::setup_bundles_for_inputs_and_outputs () +IO::set_name_in_state (XMLNode& node, const string& new_name) { - char buf[32]; + const XMLProperty* prop; - _bundle_for_inputs->remove_channels (); - _bundle_for_outputs->remove_channels (); - - snprintf(buf, sizeof (buf), _("%s in"), _name.c_str()); - _bundle_for_inputs->set_name (buf); - uint32_t const ni = inputs().num_ports(); - for (uint32_t i = 0; i < ni; ++i) { - snprintf (buf, sizeof(buf), _("in %d"), (i + 1)); - _bundle_for_inputs->add_channel (buf); - _bundle_for_inputs->set_port (i, _session.engine().make_port_name_non_relative (inputs().port(i)->name())); - } - - snprintf(buf, sizeof (buf), _("%s out"), _name.c_str()); - _bundle_for_outputs->set_name (buf); - uint32_t const no = outputs().num_ports(); - for (uint32_t i = 0; i < no; ++i) { - snprintf (buf, sizeof(buf), _("out %d"), (i + 1)); - _bundle_for_outputs->add_channel (buf); - _bundle_for_outputs->set_port (i, _session.engine().make_port_name_non_relative (outputs().port(i)->name())); + if ((prop = node.property ("name")) != 0) { + node.add_property ("name", new_name); } } - -/** - * Create and setup bundles that describe our inputs and outputs. - */ - -void -IO::create_bundles_for_inputs_and_outputs () +bool +IO::connected () const { - _bundle_for_inputs = boost::shared_ptr (new Bundle (true)); - _bundle_for_outputs = boost::shared_ptr (new Bundle (false)); - setup_bundles_for_inputs_and_outputs (); + /* 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; } -/** Add a bundle to a list if is connected to our inputs. - * @param b Bundle to check. - * @param bundles List to add to. - */ -void -IO::maybe_add_input_bundle_to_list (boost::shared_ptr b, std::vector >* bundles) +bool +IO::connected_to (boost::shared_ptr other) const { - if (b->ports_are_outputs() == false) { - return; - } - - if (b->nchannels() != n_inputs().n_total ()) { - return; + if (!other) { + return connected (); } - for (uint32_t i = 0; i < n_inputs().n_total (); ++i) { + assert (_direction != other->direction()); - Bundle::PortList const & pl = b->channel_ports (i); - - if (pl.empty()) { - return; - } + uint32_t i, j; + uint32_t no = n_ports().n_total(); + uint32_t ni = other->n_ports ().n_total(); - if (!input(i)->connected_to (pl[0])) { - return; + for (i = 0; i < no; ++i) { + for (j = 0; j < ni; ++j) { + if (nth(i)->connected_to (other->nth(j)->name())) { + return true; + } } } - bundles->push_back (b); + return false; } -/** @return Bundles connected to our inputs */ -std::vector > -IO::bundles_connected_to_inputs () +void +IO::process_input (boost::shared_ptr proc, sframes_t start_frame, sframes_t end_frame, nframes_t nframes) { - std::vector > bundles; - - /* User bundles */ - for (std::vector::iterator i = _bundles_connected_to_inputs.begin(); i != _bundles_connected_to_inputs.end(); ++i) { - bundles.push_back (i->bundle); - } + BufferSet bufs; - /* Normal bundles */ - _session.foreach_bundle ( - sigc::bind (sigc::mem_fun (*this, &IO::maybe_add_input_bundle_to_list), &bundles) - ); + /* don't read the data into new buffers - just use the port buffers directly */ - return bundles; + bufs.attach_buffers (_ports, nframes, 0); + proc->run (bufs, start_frame, end_frame, nframes, true); } - -/** Add a bundle to a list if is connected to our outputs. - * @param b Bundle to check. - * @param bundles List to add to. - */ void -IO::maybe_add_output_bundle_to_list (boost::shared_ptr b, std::vector >* bundles) +IO::collect_input (BufferSet& bufs, nframes_t nframes, ChanCount offset) { - if (b->ports_are_inputs() == false) { - return; - } + assert(bufs.available() >= _ports.count()); - if (b->nchannels () != n_outputs().n_total ()) { + if (_ports.count() == ChanCount::ZERO) { return; } - for (uint32_t i = 0; i < n_outputs().n_total (); ++i) { + bufs.set_count (_ports.count()); - Bundle::PortList const & pl = b->channel_ports (i); + for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { + PortSet::iterator i = _ports.begin(*t); + BufferSet::iterator b = bufs.begin(*t); - if (pl.empty()) { - return; + for (uint32_t off = 0; off < offset.get(*t); ++off, ++b) { + if (b == bufs.end(*t)) { + continue; + } } - if (!output(i)->connected_to (pl[0])) { - return; + for ( ; i != _ports.end(*t); ++i, ++b) { + Buffer& bb (i->get_buffer (nframes)); + b->read_from (bb, nframes); } } - - bundles->push_back (b); } - -/* @return Bundles connected to our outputs */ -std::vector > -IO::bundles_connected_to_outputs () +void +IO::copy_to_outputs (BufferSet& bufs, DataType type, nframes_t nframes, nframes_t offset) { - std::vector > bundles; + // Copy any buffers 1:1 to outputs - /* User bundles */ - for (std::vector::iterator i = _bundles_connected_to_outputs.begin(); i != _bundles_connected_to_outputs.end(); ++i) { - bundles.push_back (i->bundle); + PortSet::iterator o = _ports.begin(type); + BufferSet::iterator i = bufs.begin(type); + BufferSet::iterator prev = i; + + 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; } - /* Auto bundles */ - _session.foreach_bundle ( - sigc::bind (sigc::mem_fun (*this, &IO::maybe_add_output_bundle_to_list), &bundles) - ); + // Copy last buffer to any extra outputs - return bundles; + while (o != _ports.end(type)) { + Buffer& port_buffer (o->get_buffer (nframes)); + port_buffer.read_from (*prev, nframes, offset); + ++o; + } } - -IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr b) +Port* +IO::port_by_name (const std::string& str) const { - bundle = b; - configuration_changed = b->ConfigurationChanged.connect ( - sigc::mem_fun (*io, &IO::bundle_configuration_changed) - ); - ports_changed = b->PortsChanged.connect ( - sigc::mem_fun (*io, &IO::bundle_ports_changed) - ); -} + /* to be called only from ::set_state() - no locking */ -void -IO::prepare_inputs (nframes_t nframes, nframes_t offset) -{ - /* io_lock, not taken: function must be called from Session::process() calltree */ + for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) { + + const Port& p(*i); + + if (p.name() == str) { + return const_cast(&p); + } + } + + return 0; } -void -IO::flush_outputs (nframes_t nframes, nframes_t offset) +bool +IO::physically_connected () const { - /* io_lock, not taken: function must be called from Session::process() calltree */ - for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) { + for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) { + if (i->physically_connected()) { + return true; + } + } - /* Only run cycle_start() on output ports, because - inputs must be done in the correct processing order, - which requires interleaving with route processing. - */ - - (*i).flush_buffers (nframes, offset); - } - + return false; }