*** NEW CODING POLICY ***
[ardour.git] / libs / ardour / io.cc
index 0343545936fcdf07e348c8601cb4427bcc1e5e50..8f7aac90293e64a677a1de649df037c2aa9e2176 100644 (file)
 #include <glibmm.h>
 #include <glibmm/thread.h>
 
-#include <pbd/xml++.h>
-#include <pbd/replace_all.h>
-#include <pbd/unknown_type.h>
-
-#include <ardour/audioengine.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/auto_bundle.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 "pbd/xml++.h"
+#include "pbd/replace_all.h"
+#include "pbd/unknown_type.h"
+
+#include "ardour/audioengine.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"
 
@@ -102,13 +102,12 @@ static double direct_gain_to_control (gain_t gain) {
  */
 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)
+       DataType default_type)
        : 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),
@@ -140,8 +139,7 @@ IO::IO (Session& s, const string& name,
        boost::shared_ptr<AutomationList> gl(
                        new AutomationList(Evoral::Parameter(GainAutomation)));
 
-       _gain_control = boost::shared_ptr<GainControl>(
-                       new GainControl(X_("gaincontrol"), *this, gl));
+       _gain_control = boost::shared_ptr<GainControl>( new GainControl( X_("gaincontrol"), this, Evoral::Parameter(GainAutomation), gl ));
 
        add_control(_gain_control);
 
@@ -154,12 +152,9 @@ IO::IO (Session& s, const string& name,
                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_bundles_for_inputs_and_outputs ();
 }
 
 IO::IO (Session& s, const XMLNode& node, DataType dt)
@@ -170,7 +165,6 @@ IO::IO (Session& s, const XMLNode& node, DataType dt)
          _default_type (dt)
 {
        _meter = new PeakMeter (_session);
-       _public_ports = true; // XXX get this from node
        _panner = 0;
        deferred_state = 0;
        no_panner_reset = false;
@@ -183,7 +177,7 @@ IO::IO (Session& s, const XMLNode& node, DataType dt)
                        new AutomationList(Evoral::Parameter(GainAutomation)));
 
        _gain_control = boost::shared_ptr<GainControl>(
-                       new GainControl(X_("gaincontrol"), *this, gl));
+                       new GainControl( X_("gaincontrol"), this, Evoral::Parameter(GainAutomation), gl));
 
        add_control(_gain_control);
 
@@ -196,12 +190,9 @@ IO::IO (Session& s, const XMLNode& node, DataType dt)
                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_bundles_for_inputs_and_outputs ();
 }
 
 IO::~IO ()
@@ -223,7 +214,6 @@ IO::~IO ()
 
        delete _meter;
        delete _panner;
-       delete _output_buffers;
 }
 
 void
@@ -232,7 +222,7 @@ IO::silence (nframes_t nframes, nframes_t offset)
        /* 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);
+               i->get_buffer(nframes,offset).silence (nframes, offset);
        }
 }
 
@@ -262,51 +252,66 @@ IO::deliver_output (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame,
 
                }
 
-               if (dg != _gain || dg != 1.0)
+               if (dg != _gain || dg != 1.0) {
                        Amp::run_in_place(bufs, nframes, _gain, dg, _phase_invert);
+                       _gain = dg;
+               }
        }
        
+       /* do this so that any processing that comes after deliver_outputs()
+          can use the output buffers.
+       */
+
+       output_buffers().attach_buffers (_outputs, nframes, offset);
+
        // 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
-               
-               BufferSet::iterator o = output_buffers().begin(type);
-               BufferSet::iterator i = bufs.begin(type);
-               BufferSet::iterator prev = i;
+
+       if (0 && _panner && _panner->npanners() && !_panner->bypassed()) {
+
+               /* blech .. we shouldn't be creating and tearing this down every process()
+                  cycle. XXX fix me to not waste cycles and do memory allocation etc.
+               */
                
-               while (i != bufs.end(type) && o != output_buffers().end (type)) {
-                       o->read_from(*i, nframes, offset);
-                       prev = i;
-                       ++i;
-                       ++o;
-               }
+               _panner->run_out_of_place(bufs, output_buffers(), start_frame, end_frame, nframes, offset);
+
+       } else {
 
-               /* extra outputs get a copy of the last buffer */
+               /* do a 1:1 copy of data to output ports */
 
-               while (o != output_buffers().end(type)) {
-                       o->read_from(*prev, nframes, offset);
-                       ++o;
+               if (bufs.count().n_audio() > 0 && _outputs.count().n_audio () > 0) {
+                       copy_to_outputs (bufs, DataType::AUDIO, nframes, offset);
+               }
+               if (bufs.count().n_midi() > 0 && _outputs.count().n_midi () > 0) {
+                       copy_to_outputs (bufs, DataType::MIDI, nframes, offset);
                }
        }
+}
 
-       /* ********** MIDI ********** */
+void
+IO::copy_to_outputs (BufferSet& bufs, DataType type, nframes_t nframes, nframes_t offset)
+{
+       // Copy any buffers 1:1 to outputs
+       
+       PortSet::iterator o = _outputs.begin(type);
+       BufferSet::iterator i = bufs.begin(type);
+       BufferSet::iterator prev = i;
+       
+       while (i != bufs.end(type) && o != _outputs.end (type)) {
+               
+               Buffer& port_buffer (o->get_buffer (nframes, offset));
+               port_buffer.read_from (*i, nframes, offset);
 
-       // No MIDI, we're done here
-       if (bufs.count().n_midi() == 0) {
-               return;
+               prev = i;
+               ++i;
+               ++o;
        }
-
-       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);
+       
+       /* extra outputs get a copy of the last buffer */
+       
+       while (o != _outputs.end(type)) {
+               Buffer& port_buffer (o->get_buffer (nframes, offset));
+               port_buffer.read_from(*prev, nframes, offset);
+               ++o;
        }
 }
 
@@ -323,9 +328,10 @@ IO::collect_input (BufferSet& outs, nframes_t nframes, nframes_t offset)
        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) {
-                       (*i).cycle_start (nframes, offset);
-                       o->read_from(i->get_buffer(), nframes, offset);
+               PortSet::iterator e = _inputs.end (*t);
+               for (PortSet::iterator i = _inputs.begin(*t); i != e; ++i, ++o) {
+                       Buffer& b (i->get_buffer (nframes,offset));
+                       o->read_from (b, nframes, offset);
                }
 
        }
@@ -362,18 +368,17 @@ IO::check_bundles (std::vector<UserBundleInfo>& list, const PortSet& ports)
        
        for (std::vector<UserBundleInfo>::iterator i = list.begin(); i != list.end(); ++i) {
 
-               ChanCount const N = i->bundle->nchannels ();
+               uint32_t const N = i->bundle->nchannels ();
 
-               if (ports.num_ports (default_type()) < N.get (default_type())) {
+               if (ports.num_ports (default_type()) < N) {
                        continue;
                }
 
                bool ok = true;
-               uint32_t n = N.get (default_type());
 
-               for (uint32_t j = 0; j < n; ++j) {
+               for (uint32_t j = 0; j < N; ++j) {
                        /* Every port on bundle channel j must be connected to our input j */
-                       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;
@@ -389,10 +394,7 @@ IO::check_bundles (std::vector<UserBundleInfo>& list, const PortSet& ports)
                if (ok) {
                        new_list.push_back (*i);
                } else {
-                       i->configuration_will_change.disconnect ();
-                       i->configuration_has_changed.disconnect ();
-                       i->ports_will_change.disconnect ();
-                       i->ports_have_changed.disconnect ();
+                       i->changed.disconnect ();
                }
        }
 
@@ -421,7 +423,7 @@ IO::disconnect_input (Port* our_port, string other_port, void* src)
                        
                        /* disconnect it from the source */
                        
-                       if (_session.engine().disconnect (other_port, our_port->name())) {
+                       if (our_port->disconnect (other_port)) {
                                error << string_compose(_("IO: cannot disconnect input port %1 from %2"), our_port->name(), other_port) << endmsg;
                                return -1;
                        }
@@ -457,7 +459,7 @@ IO::connect_input (Port* our_port, string other_port, void* src)
                        
                        /* connect it to the source */
 
-                       if (_session.engine().connect (other_port, our_port->name())) {
+                       if (our_port->connect (other_port)) {
                                return -1;
                        }
                }
@@ -489,7 +491,7 @@ IO::disconnect_output (Port* our_port, string other_port, void* src)
                        
                        /* disconnect it from the destination */
                        
-                       if (_session.engine().disconnect (our_port->name(), other_port)) {
+                       if (our_port->disconnect (other_port)) {
                                error << string_compose(_("IO: cannot disconnect output port %1 from %2"), our_port->name(), other_port) << endmsg;
                                return -1;
                        }
@@ -525,7 +527,7 @@ IO::connect_output (Port* our_port, string other_port, void* src)
                        
                        /* connect it to the destination */
                        
-                       if (_session.engine().connect (our_port->name(), other_port)) {
+                       if (our_port->connect (other_port)) {
                                return -1;
                        }
                }
@@ -598,8 +600,8 @@ IO::remove_output_port (Port* port, void* src)
                PortCountChanged (n_outputs()); /* EMIT SIGNAL */
        }
 
-       if (change == ConnectionsChanged) {
-               setup_bundles_for_inputs_and_outputs ();
+       if (change == ConfigurationChanged) {
+               setup_bundle_for_outputs ();
        }
 
        if (change != NoChange) {
@@ -640,7 +642,7 @@ IO::add_output_port (string destination, void* src, DataType type)
                        
                        string portname = build_legal_port_name (type, false);
                        
-                       if ((our_port = _session.engine().register_output_port (type, portname, _public_ports)) == 0) {
+                       if ((our_port = _session.engine().register_output_port (type, portname)) == 0) {
                                error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
                                return -1;
                        }
@@ -654,14 +656,14 @@ IO::add_output_port (string destination, void* src, DataType type)
        }
 
        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 ();
+       setup_bundle_for_outputs ();
        _session.set_dirty ();
 
        return 0;
@@ -703,7 +705,7 @@ IO::remove_input_port (Port* port, void* src)
        }
 
        if (change == ConfigurationChanged) {
-               setup_bundles_for_inputs_and_outputs ();
+               setup_bundle_for_inputs ();
        }
 
        if (change != NoChange) {
@@ -744,7 +746,7 @@ IO::add_input_port (string source, void* src, DataType type)
                        
                        string portname = build_legal_port_name (type, true);
 
-                       if ((our_port = _session.engine().register_input_port (type, portname, _public_ports)) == 0) {
+                       if ((our_port = _session.engine().register_input_port (type, portname)) == 0) {
                                error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
                                return -1;
                        }
@@ -759,14 +761,14 @@ IO::add_input_port (string source, void* src, DataType type)
 
        if (source.length()) {
 
-               if (_session.engine().connect (source, our_port->name())) {
+               if (our_port->connect (source)) {
                        return -1;
                }
        } 
 
        // pan_changed (src); /* EMIT SIGNAL */
        input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
-       setup_bundles_for_inputs_and_outputs ();
+       setup_bundle_for_inputs ();
        _session.set_dirty ();
        
        return 0;
@@ -782,7 +784,7 @@ IO::disconnect_inputs (void* src)
                        Glib::Mutex::Lock lm (io_lock);
                        
                        for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
-                               _session.engine().disconnect (*i);
+                               i->disconnect_all ();
                        }
 
                        check_bundles_connected_to_inputs ();
@@ -804,7 +806,7 @@ IO::disconnect_outputs (void* src)
                        Glib::Mutex::Lock lm (io_lock);
                        
                        for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
-                               _session.engine().disconnect (*i);
+                               i->disconnect_all ();
                        }
 
                        check_bundles_connected_to_outputs ();
@@ -846,7 +848,7 @@ IO::ensure_inputs_locked (ChanCount count, bool clear, void* src)
 
                        try {
 
-                               if ((input_port = _session.engine().register_input_port (*t, portname, _public_ports)) == 0) {
+                               if ((input_port = _session.engine().register_input_port (*t, portname)) == 0) {
                                        error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
                                        return -1;
                                }
@@ -875,23 +877,13 @@ IO::ensure_inputs_locked (ChanCount count, bool clear, void* src)
        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);
+                       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)
 {
@@ -954,7 +946,7 @@ IO::ensure_io (ChanCount in, ChanCount out, bool clear, void* src)
                                string portname = build_legal_port_name (*t, true);
 
                                try {
-                                       if ((port = _session.engine().register_input_port (*t, portname, _public_ports)) == 0) {
+                                       if ((port = _session.engine().register_input_port (*t, portname)) == 0) {
                                                error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
                                                return -1;
                                        }
@@ -978,7 +970,7 @@ IO::ensure_io (ChanCount in, ChanCount out, bool clear, void* src)
                                string portname = build_legal_port_name (*t, false);
                                
                                try { 
-                                       if ((port = _session.engine().register_output_port (*t, portname, _public_ports)) == 0) {
+                                       if ((port = _session.engine().register_output_port (*t, portname)) == 0) {
                                                error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
                                                return -1;
                                        }
@@ -1001,11 +993,11 @@ IO::ensure_io (ChanCount in, ChanCount out, bool clear, void* src)
                        /* 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);
+                               i->disconnect_all ();
                        }
                        
                        for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
-                               _session.engine().disconnect (*i);
+                               i->disconnect_all ();
                        }
                }
                
@@ -1018,16 +1010,17 @@ IO::ensure_io (ChanCount in, ChanCount out, bool clear, void* src)
        if (out_changed) {
                check_bundles_connected_to_outputs ();
                output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
+               setup_bundle_for_outputs ();
        }
        
        if (in_changed) {
                check_bundles_connected_to_inputs ();
                input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
+               setup_bundle_for_inputs ();
        }
 
        if (in_changed || out_changed) {
                PortCountChanged (max (n_outputs(), n_inputs())); /* EMIT SIGNAL */
-               setup_bundles_for_inputs_and_outputs ();
                _session.set_dirty ();
        }
 
@@ -1055,7 +1048,7 @@ IO::ensure_inputs (ChanCount count, bool clear, bool lockit, void* src)
 
        if (changed) {
                input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
-               setup_bundles_for_inputs_and_outputs ();
+               setup_bundle_for_inputs ();
                _session.set_dirty ();
        }
        return 0;
@@ -1092,7 +1085,7 @@ IO::ensure_outputs_locked (ChanCount count, bool clear, void* src)
 
                        string portname = build_legal_port_name (*t, false);
 
-                       if ((output_port = _session.engine().register_output_port (*t, portname, _public_ports)) == 0) {
+                       if ((output_port = _session.engine().register_output_port (*t, portname)) == 0) {
                                error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
                                return -1;
                        }
@@ -1116,7 +1109,7 @@ IO::ensure_outputs_locked (ChanCount count, bool clear, void* src)
        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);
+                       i->disconnect_all ();
                }
        }
 
@@ -1147,7 +1140,7 @@ IO::ensure_outputs (ChanCount count, bool clear, bool lockit, void* src)
 
        if (changed) {
                 output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
-                setup_bundles_for_inputs_and_outputs ();
+                setup_bundle_for_outputs ();
        }
 
        return 0;
@@ -1347,7 +1340,7 @@ 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 ();
@@ -1402,6 +1395,7 @@ IO::set_state (const XMLNode& node)
 
        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);
@@ -1409,12 +1403,21 @@ IO::set_state (const XMLNode& node)
                        _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 ((*iter)->name() == X_("Controllable")) {
                        if ((prop = (*iter)->property("name")) != 0 && prop->value() == "gaincontrol") {
                                _gain_control->set_state (**iter);
                        }
@@ -1432,6 +1435,8 @@ IO::set_state (const XMLNode& node)
                port_legal_c = PortsLegal.connect (mem_fun (*this, &IO::ports_became_legal));
        }
 
+       if( !_panner )
+           _panner = new Panner( _name, _session );
        if (panners_legal) {
                reset_panner ();
        } else {
@@ -1671,8 +1676,8 @@ int
 IO::create_ports (const XMLNode& node)
 {
        XMLProperty const * prop;
-       ChanCount num_inputs;
-       ChanCount num_outputs;
+       uint32_t num_inputs = 0;
+       uint32_t num_outputs = 0;
 
        if ((prop = node.property ("input-connection")) != 0) {
 
@@ -1686,7 +1691,7 @@ IO::create_ports (const XMLNode& node)
 
        } else if ((prop = node.property ("inputs")) != 0) {
 
-               num_inputs.set (default_type(), count (prop->value().begin(), prop->value().end(), '{'));
+               num_inputs = count (prop->value().begin(), prop->value().end(), '{');
        }
        
        if ((prop = node.property ("output-connection")) != 0) {
@@ -1701,12 +1706,15 @@ IO::create_ports (const XMLNode& node)
                
        } else if ((prop = node.property ("outputs")) != 0) {
 
-               num_outputs.set (default_type(), count (prop->value().begin(), prop->value().end(), '{'));
+               num_outputs = count (prop->value().begin(), prop->value().end(), '{');
        }
 
        no_panner_reset = true;
 
-       if (ensure_io (num_inputs, num_outputs, true, this)) {
+       if (ensure_io (ChanCount (_default_type, num_inputs),
+                      ChanCount (_default_type, num_outputs),
+                      true, this)) {
+               
                error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg;
                return -1;
        }
@@ -1955,13 +1963,13 @@ IO::set_name (const string& requested_name)
        }
 
        for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
-               string current_name = i->short_name();
+               string current_name = i->name();
                current_name.replace (current_name.find (_name), _name.length(), name);
                i->set_name (current_name);
        }
 
        for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
-               string current_name = i->short_name();
+               string current_name = i->name();
                current_name.replace (current_name.find (_name), _name.length(), name);
                i->set_name (current_name);
        }
@@ -2018,7 +2026,7 @@ 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) {
+               if ((latency = i->total_latency ()) > max_latency) {
                        max_latency = latency;
                }
        }
@@ -2037,7 +2045,7 @@ IO::input_latency () const
        /* io lock not taken - must be protected by other means */
 
        for (PortSet::const_iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
-               if ((latency = _session.engine().get_port_total_latency (*i)) > max_latency) {
+               if ((latency = i->total_latency ()) > max_latency) {
                        max_latency = latency;
                } 
        }
@@ -2052,26 +2060,7 @@ IO::connect_input_ports_to_bundle (boost::shared_ptr<Bundle> 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. */
-
-               ChanCount const channels = c->nchannels ();
-               uint32_t cnt = channels.get (default_type());
-
-               for (uint32_t n = 0; n < cnt; ++n) {
-                       const PortList& pl = c->channel_ports (n);
-
-                       for (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;
-                                       }
-                               }
-                               
-                       }
-               }
+               c->connect (_bundle_for_inputs, _session.engine());
 
                /* If this is a UserBundle, make a note of what we've done */
 
@@ -2096,32 +2085,42 @@ IO::connect_input_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
 }
 
 int
-IO::connect_output_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
+IO::disconnect_input_ports_from_bundle (boost::shared_ptr<Bundle> 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. */
-
-               ChanCount const channels = c->nchannels ();
-               uint32_t cnt = channels.get (default_type());
-
-               for (uint32_t n = 0; n < cnt; ++n) {
+               c->disconnect (_bundle_for_inputs, _session.engine());
+                       
+               /* If this is a UserBundle, make a note of what we've done */
 
-                       const PortList& pl = c->channel_ports (n);
+               boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
+               if (ub) {
 
-                       for (PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
+                       std::vector<UserBundleInfo>::iterator i = _bundles_connected_to_inputs.begin();
+                       while (i != _bundles_connected_to_inputs.end() && i->bundle != ub) {
+                               ++i;
+                       }
 
-                               if (!_outputs.port(n)->connected_to (*i)) {
-                                               
-                                       if (_session.engine().connect (_outputs.port(n)->name(), *i)) {
-                                               return -1;
-                                       }
-                               }
+                       if (i != _bundles_connected_to_inputs.end()) {
+                               _bundles_connected_to_inputs.erase (i);
                        }
                }
+       }
+
+       input_changed (IOChange (ConfigurationChanged|ConnectionsChanged), src); /* EMIT SIGNAL */
+       return 0;
+}
+
+int
+IO::connect_output_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
+{
+       {
+               BLOCK_PROCESS_CALLBACK ();
+               Glib::Mutex::Lock lm2 (io_lock);
+
+               c->connect (_bundle_for_outputs, _session.engine());
 
                /* If this is a UserBundle, make a note of what we've done */
 
@@ -2146,6 +2145,36 @@ IO::connect_output_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
        return 0;
 }
 
+int
+IO::disconnect_output_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
+{
+       {
+               BLOCK_PROCESS_CALLBACK ();
+               Glib::Mutex::Lock lm2 (io_lock);
+
+               c->disconnect (_bundle_for_outputs, _session.engine());
+                       
+               /* If this is a UserBundle, make a note of what we've done */
+
+               boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
+               if (ub) {
+
+                       std::vector<UserBundleInfo>::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()) {
+                               _bundles_connected_to_outputs.erase (i);
+                       }
+               }
+       }
+
+       output_changed (IOChange (ConfigurationChanged|ConnectionsChanged), src); /* EMIT SIGNAL */
+       return 0;
+}
+
+
 int
 IO::disable_connecting ()
 {
@@ -2189,33 +2218,12 @@ IO::reset_panners ()
 }
 
 void
-IO::bundle_configuration_will_change ()
+IO::bundle_changed (Bundle::Change c)
 {
        //XXX
 //     connect_input_ports_to_bundle (_input_bundle, this);
 }
 
-void
-IO::bundle_configuration_has_changed ()
-{
-       //XXX
-//     connect_input_ports_to_bundle (_input_bundle, this);
-}
-
-void
-IO::bundle_ports_will_change (int ignored)
-{
-//XXX
-//     connect_output_ports_to_bundle (_output_bundle, this);
-}
-
-void
-IO::bundle_ports_have_changed (int ignored)
-{
-       //XXX
-//     connect_output_ports_to_bundle (_output_bundle, this);
-}
-
 void
 IO::GainControl::set_value (float val)
 {
@@ -2223,10 +2231,9 @@ IO::GainControl::set_value (float val)
        if (val > 1.99526231f)
                val = 1.99526231f;
 
-       _user_value = val;
-       _io.set_gain (val, this);
+       _io->set_gain (val, this);
        
-       Changed(); /* EMIT SIGNAL */
+       AutomationControl::set_value(val);
 }
 
 float
@@ -2270,7 +2277,7 @@ void
 IO::clear_automation ()
 {
        data().clear (); // clears gain automation
-       _panner->clear_automation ();
+       _panner->data().clear();
 }
 
 void
@@ -2322,8 +2329,11 @@ void
 IO::set_gain (gain_t val, void *src)
 {
        // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
-       if (val > 1.99526231f)
+       if (val > 1.99526231f) {
                val = 1.99526231f;
+       }
+
+       //cerr << "set desired gain to " << val << " when curgain = " << _gain_control->get_value () << endl;
 
        if (src != _gain_control.get()) {
                _gain_control->set_value(val);
@@ -2338,13 +2348,15 @@ IO::set_gain (gain_t val, void *src)
        }
 
        if (_session.transport_stopped()) {
-               _gain = val;
+               // _gain = val;
        }
        
+       /*
        if (_session.transport_stopped() && src != 0 && src != this && _gain_control->automation_write()) {
                _gain_control->list()->add (_session.transport_frame(), val);
                
        }
+       */
 
        _session.set_dirty();
 }
@@ -2352,16 +2364,16 @@ IO::set_gain (gain_t val, void *src)
 void
 IO::start_pan_touch (uint32_t which)
 {
-       if (which < _panner->size()) {
-               (*_panner)[which]->pan_control()->start_touch();
+       if (which < _panner->npanners()) {
+               (*_panner).pan_control(which)->start_touch();
        }
 }
 
 void
 IO::end_pan_touch (uint32_t which)
 {
-       if (which < _panner->size()) {
-               (*_panner)[which]->pan_control()->stop_touch();
+       if (which < _panner->npanners()) {
+               (*_panner).pan_control(which)->stop_touch();
        }
 
 }
@@ -2370,12 +2382,17 @@ void
 IO::automation_snapshot (nframes_t now, bool force)
 {
        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->snapshot (now);
+               _panner->automation_snapshot (now, force);
        }
        
-       _panner->snapshot (now);
+       _panner->automation_snapshot (now, force);
        _last_automation_snapshot = now;
 }
 
@@ -2472,7 +2489,7 @@ IO::find_input_port_hole (const char* base)
                snprintf (buf, jack_port_name_size(), _("%s %u"), base, n);
 
                for ( ; i != _inputs.end(); ++i) {
-                       if (i->short_name() == buf) {
+                       if (i->name() == buf) {
                                break;
                        }
                }
@@ -2505,7 +2522,7 @@ IO::find_output_port_hole (const char* base)
                snprintf (buf, jack_port_name_size(), _("%s %u"), base, n);
 
                for ( ; i != _outputs.end(); ++i) {
-                       if (i->short_name() == buf) {
+                       if (i->name() == buf) {
                                break;
                        }
                }
@@ -2525,7 +2542,6 @@ IO::set_active (bool yn)
         active_changed(); /* EMIT SIGNAL */
 }
 
-
 AudioPort*
 IO::audio_input(uint32_t n) const
 {
@@ -2584,145 +2600,128 @@ IO::update_port_total_latencies ()
 
 
 /**
- *  Setup bundles that describe our inputs and outputs.
+ *  Setup bundles that describe our inputs and outputs. Also creates bundles if necessary.
  */
 
 void
 IO::setup_bundles_for_inputs_and_outputs ()
+{
+       setup_bundle_for_inputs ();
+       setup_bundle_for_outputs ();
+}
+
+
+void
+IO::setup_bundle_for_inputs ()
 {
         char buf[32];
 
+       if (!_bundle_for_inputs) {
+               _bundle_for_inputs.reset (new Bundle (true));
+       }
+
+       _bundle_for_inputs->suspend_signals ();
+
+       _bundle_for_inputs->set_type (default_type ());
+
+       _bundle_for_inputs->remove_channels ();
+
         snprintf(buf, sizeof (buf), _("%s in"), _name.c_str());
         _bundle_for_inputs->set_name (buf);
        uint32_t const ni = inputs().num_ports();
-       _bundle_for_inputs->set_channels (ni);
        for (uint32_t i = 0; i < ni; ++i) {
-               _bundle_for_inputs->set_port (i, inputs().port(i)->name());
+               _bundle_for_inputs->add_channel (bundle_channel_name (i, ni));
+               _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();
-       _bundle_for_outputs->set_channels (no);
-       for (uint32_t i = 0; i < no; ++i) {
-               _bundle_for_outputs->set_port (i, outputs().port(i)->name());
-       }
+       _bundle_for_inputs->resume_signals ();
 }
 
 
-/**
- *  Create and setup bundles that describe our inputs and outputs.
- */
-
-void
-IO::create_bundles_for_inputs_and_outputs ()
-{
-       _bundle_for_inputs = boost::shared_ptr<AutoBundle> (new AutoBundle (true));
-        _bundle_for_outputs = boost::shared_ptr<AutoBundle> (new AutoBundle (false));
-        setup_bundles_for_inputs_and_outputs ();
-}
-
-/** 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<Bundle> b, std::vector<boost::shared_ptr<Bundle> >* bundles)
+IO::setup_bundle_for_outputs ()
 {
-       boost::shared_ptr<AutoBundle> ab = boost::dynamic_pointer_cast<AutoBundle, Bundle> (b);
+        char buf[32];
 
-       if (ab == 0 || ab->ports_are_outputs() == false) {
-               return;
-       }
-       
-       if (ab->nchannels().get (default_type()) != n_inputs().n_total ()) {
-               return;
+       if (!_bundle_for_outputs) {
+               _bundle_for_outputs.reset (new Bundle (false));
        }
 
-       for (uint32_t i = 0; i < n_inputs().n_total (); ++i) {
+       _bundle_for_outputs->suspend_signals ();
 
-               PortList const & pl = b->channel_ports (i);
+       _bundle_for_outputs->set_type (default_type ());
 
-               if (pl.empty()) {
-                       return;
-               }
+       _bundle_for_outputs->remove_channels ();
 
-               if (!input(i)->connected_to (pl[0])) {
-                       return;
-               }
+        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) {
+               _bundle_for_outputs->add_channel (bundle_channel_name (i, no));
+               _bundle_for_outputs->set_port (i, _session.engine().make_port_name_non_relative (outputs().port(i)->name()));
        }
 
-       bundles->push_back (b);
+       _bundle_for_outputs->resume_signals ();
 }
 
+
 /** @return Bundles connected to our inputs */
-std::vector<boost::shared_ptr<Bundle> >
+BundleList
 IO::bundles_connected_to_inputs ()
 {
-       std::vector<boost::shared_ptr<Bundle> > bundles;
+       BundleList bundles;
        
        /* User bundles */
        for (std::vector<UserBundleInfo>::iterator i = _bundles_connected_to_inputs.begin(); i != _bundles_connected_to_inputs.end(); ++i) {
                bundles.push_back (i->bundle);
        }
 
-       /* Auto bundles */
-       _session.foreach_bundle (
-               sigc::bind (sigc::mem_fun (*this, &IO::maybe_add_input_bundle_to_list), &bundles)
-               );
-
-       return bundles;
-}
-
-
-/** 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<Bundle> b, std::vector<boost::shared_ptr<Bundle> >* bundles)
-{
-       boost::shared_ptr<AutoBundle> ab = boost::dynamic_pointer_cast<AutoBundle, Bundle> (b);
-       if (ab == 0 || ab->ports_are_inputs() == false) {
-               return;
-       }
-
-       if (ab->nchannels ().get (default_type()) != n_outputs().n_total ()) {
-               return;
-       }
-
-       for (uint32_t i = 0; i < n_outputs().n_total (); ++i) {
-
-               PortList const & pl = b->channel_ports (i);
-
-               if (pl.empty()) {
-                       return;
+       /* Session bundles */
+       boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
+       for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
+               if ((*i)->connected_to (_bundle_for_inputs, _session.engine())) {
+                       bundles.push_back (*i);
                }
+       }
 
-               if (!output(i)->connected_to (pl[0])) {
-                       return;
+       /* Route bundles */
+       boost::shared_ptr<ARDOUR::RouteList> r = _session.get_routes ();
+       for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+               if ((*i)->bundle_for_outputs()->connected_to (_bundle_for_inputs, _session.engine())) {
+                       bundles.push_back ((*i)->bundle_for_outputs());
                }
        }
-
-       bundles->push_back (b);
+         
+       return bundles;
 }
 
 
 /* @return Bundles connected to our outputs */
-std::vector<boost::shared_ptr<Bundle> >
+BundleList
 IO::bundles_connected_to_outputs ()
 {
-       std::vector<boost::shared_ptr<Bundle> > bundles;
+       BundleList bundles;
 
        /* User bundles */
        for (std::vector<UserBundleInfo>::iterator i = _bundles_connected_to_outputs.begin(); i != _bundles_connected_to_outputs.end(); ++i) {
                bundles.push_back (i->bundle);
        }
 
-       /* Auto bundles */
-       _session.foreach_bundle (
-               sigc::bind (sigc::mem_fun (*this, &IO::maybe_add_output_bundle_to_list), &bundles)
-               );
+       /* Session bundles */
+       boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
+       for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
+               if ((*i)->connected_to (_bundle_for_outputs, _session.engine())) {
+                       bundles.push_back (*i);
+               }
+       }
+
+       /* Route bundles */
+       boost::shared_ptr<ARDOUR::RouteList> r = _session.get_routes ();
+       for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+               if ((*i)->bundle_for_inputs()->connected_to (_bundle_for_outputs, _session.engine())) {
+                       bundles.push_back ((*i)->bundle_for_inputs());
+               }
+       }
 
        return bundles; 
 }
@@ -2731,17 +2730,8 @@ IO::bundles_connected_to_outputs ()
 IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
 {
        bundle = b;
-       configuration_will_change = b->ConfigurationWillChange.connect (
-               sigc::mem_fun (*io, &IO::bundle_configuration_will_change)
-               );
-       configuration_has_changed = b->ConfigurationHasChanged.connect (
-               sigc::mem_fun (*io, &IO::bundle_configuration_has_changed)
-               );
-       ports_will_change = b->PortsWillChange.connect (
-               sigc::mem_fun (*io, &IO::bundle_ports_will_change)
-               );
-       ports_have_changed = b->PortsHaveChanged.connect (
-               sigc::mem_fun (*io, &IO::bundle_ports_have_changed)
+       changed = b->Changed.connect (
+               sigc::mem_fun (*io, &IO::bundle_changed)
                );
 }
 
@@ -2749,8 +2739,40 @@ void
 IO::prepare_inputs (nframes_t nframes, nframes_t offset)
 {
        /* io_lock, not taken: function must be called from Session::process() calltree */
+}
 
-       for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
-               (*i).cycle_start (nframes, offset);
+void
+IO::flush_outputs (nframes_t nframes, nframes_t offset)
+{
+       /* io_lock, not taken: function must be called from Session::process() calltree */
+       for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
+
+               /* 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);
        }
+               
 }
+
+std::string
+IO::bundle_channel_name (uint32_t c, uint32_t n) const
+{
+       char buf[32];
+       
+       switch (n) {
+       case 1:
+               return _("mono");
+       case 2:
+               return c == 0 ? _("L") : _("R");
+       default:
+               snprintf (buf, sizeof(buf), _("%d"), (c + 1));
+               return buf;
+       }
+
+       return "";
+}
+
+