Re-enable creation of stereo bundles for system IO, so that the mixer strip
[ardour.git] / libs / ardour / io.cc
index a708821c20fe27f7c3ee6df01d163e053eb93eb0..00281d510f49c0349ffd09565b2f9fe7ae66a7cb 100644 (file)
 
 #include <sigc++/bind.h>
 
+#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/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 <ardour/user_bundle.h>
 
 #include "i18n.h"
 
@@ -99,10 +102,13 @@ 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)
-       : Automatable (s, name),
-      _output_buffers (new BufferSet()),
+       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),
@@ -132,10 +138,9 @@ IO::IO (Session& s, const string& name,
        deferred_state = 0;
 
        boost::shared_ptr<AutomationList> gl(
-                       new AutomationList(Parameter(GainAutomation), 0.0, 2.0, 1.0));
+                       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);
 
@@ -148,21 +153,20 @@ 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 ();
+       setup_bundles_for_inputs_and_outputs ();
 }
 
 IO::IO (Session& s, const XMLNode& node, DataType dt)
-       : Automatable (s, "unnamed io"),
-      _output_buffers (new BufferSet()),
+       : SessionObject(s, "unnamed io"),
+         AutomatableControls (s),
+         _output_buffers (new BufferSet()),
+         _active(true),
          _default_type (dt)
 {
        _meter = new PeakMeter (_session);
-
+       _public_ports = true; // XXX get this from node
        _panner = 0;
        deferred_state = 0;
        no_panner_reset = false;
@@ -172,10 +176,10 @@ IO::IO (Session& s, const XMLNode& node, DataType dt)
        apply_gain_automation = false;
        
        boost::shared_ptr<AutomationList> gl(
-                       new AutomationList(Parameter(GainAutomation), 0.0, 2.0, 1.0));
+                       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);
 
@@ -188,20 +192,18 @@ 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 ();
+       setup_bundles_for_inputs_and_outputs ();
 }
 
 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);
        }
@@ -214,7 +216,6 @@ IO::~IO ()
 
        delete _meter;
        delete _panner;
-       delete _output_buffers;
 }
 
 void
@@ -223,11 +224,11 @@ 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);
        }
 }
 
-/** Deliver bufs to the IO's Jack outputs.
+/** 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.
@@ -253,51 +254,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;
        }
 }
 
@@ -305,17 +321,19 @@ void
 IO::collect_input (BufferSet& outs, nframes_t nframes, nframes_t offset)
 {
        assert(outs.available() >= n_inputs());
-
-       outs.set_count(n_inputs());
        
-       if (outs.count() == ChanCount::ZERO)
+       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);
+               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);
                }
 
        }
@@ -329,27 +347,64 @@ IO::just_meter_input (nframes_t start_frame, nframes_t end_frame,
 
        collect_input (bufs, nframes, offset);
 
-       _meter->run(bufs, start_frame, end_frame, nframes, offset);
+       _meter->run_in_place(bufs, start_frame, end_frame, nframes, offset);
 }
 
+
 void
-IO::drop_input_bundle ()
+IO::check_bundles_connected_to_inputs ()
 {
-       _input_bundle.reset ();
-       input_bundle_configuration_connection.disconnect();
-       input_bundle_connection_connection.disconnect();
-       _session.set_dirty ();
+       check_bundles (_bundles_connected_to_inputs, inputs());
 }
 
 void
-IO::drop_output_bundle ()
+IO::check_bundles_connected_to_outputs ()
 {
-       _output_bundle.reset ();
-       output_bundle_configuration_connection.disconnect();
-       output_bundle_connection_connection.disconnect();
-       _session.set_dirty ();
+       check_bundles (_bundles_connected_to_outputs, outputs());
+}
+
+void
+IO::check_bundles (std::vector<UserBundleInfo>& list, const PortSet& ports)
+{
+       std::vector<UserBundleInfo> new_list;
+       
+       for (std::vector<UserBundleInfo>::iterator i = list.begin(); i != list.end(); ++i) {
+
+               uint32_t const N = i->bundle->nchannels ();
+
+               if (ports.num_ports (default_type()) < N) {
+                       continue;
+               }
+
+               bool ok = true;
+
+               for (uint32_t j = 0; j < N; ++j) {
+                       /* Every port on bundle channel j must be connected to our input j */
+                       Bundle::PortList const pl = i->bundle->channel_ports (j);
+                       for (uint32_t k = 0; k < pl.size(); ++k) {
+                               if (ports.port(j)->connected_to (pl[k]) == false) {
+                                       ok = false;
+                                       break;
+                               }
+                       }
+
+                       if (ok == false) {
+                               break;
+                       }
+               }
+
+               if (ok) {
+                       new_list.push_back (*i);
+               } else {
+                       i->configuration_changed.disconnect ();
+                       i->ports_changed.disconnect ();
+               }
+       }
+
+       list = new_list;
 }
 
+
 int
 IO::disconnect_input (Port* our_port, string other_port, void* src)
 {
@@ -376,7 +431,7 @@ IO::disconnect_input (Port* our_port, string other_port, void* src)
                                return -1;
                        }
 
-                       drop_input_bundle ();
+                       check_bundles_connected_to_inputs ();
                }
        }
 
@@ -410,8 +465,6 @@ IO::connect_input (Port* our_port, string other_port, void* src)
                        if (_session.engine().connect (other_port, our_port->name())) {
                                return -1;
                        }
-                       
-                       drop_input_bundle ();
                }
        }
 
@@ -446,7 +499,7 @@ IO::disconnect_output (Port* our_port, string other_port, void* src)
                                return -1;
                        }
 
-                       drop_output_bundle ();
+                       check_bundles_connected_to_outputs ();
                }
        }
 
@@ -480,8 +533,6 @@ IO::connect_output (Port* our_port, string other_port, void* src)
                        if (_session.engine().connect (our_port->name(), other_port)) {
                                return -1;
                        }
-
-                       drop_output_bundle ();
                }
        }
 
@@ -542,7 +593,7 @@ IO::remove_output_port (Port* port, void* src)
                                } 
 
                                _session.engine().unregister_port (*port);
-                               drop_output_bundle ();
+                               check_bundles_connected_to_outputs ();
                                
                                setup_peak_meters ();
                                reset_panner ();
@@ -552,8 +603,8 @@ IO::remove_output_port (Port* port, void* src)
                PortCountChanged (n_outputs()); /* EMIT SIGNAL */
        }
 
-       if (change == ConnectionsChanged) {
-               setup_bundles ();
+       if (change == ConfigurationChanged) {
+               setup_bundles_for_inputs_and_outputs ();
        }
 
        if (change != NoChange) {
@@ -575,7 +626,6 @@ int
 IO::add_output_port (string destination, void* src, DataType type)
 {
        Port* our_port;
-       char name[64];
 
        if (type == DataType::NIL)
                type = _default_type;
@@ -593,20 +643,14 @@ IO::add_output_port (string destination, void* src, DataType type)
                
                        /* Create a new output port */
                        
-                       // FIXME: naming scheme for differently typed ports?
-                       if (_output_maximum.get(type) == 1) {
-                               snprintf (name, sizeof (name), _("%s/out"), _name.c_str());
-                       } else {
-                               snprintf (name, sizeof (name), _("%s/out %u"), _name.c_str(), find_output_port_hole());
-                       }
+                       string portname = build_legal_port_name (type, false);
                        
-                       if ((our_port = _session.engine().register_output_port (type, name)) == 0) {
-                               error << string_compose(_("IO: cannot register output port %1"), name) << endmsg;
+                       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;
                        }
                        
                        _outputs.add (our_port);
-                       drop_output_bundle ();
                        setup_peak_meters ();
                        reset_panner ();
                }
@@ -622,7 +666,7 @@ IO::add_output_port (string destination, void* src, DataType type)
        
        // pan_changed (src); /* EMIT SIGNAL */
        output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
-       setup_bundles ();
+       setup_bundles_for_inputs_and_outputs ();
        _session.set_dirty ();
 
        return 0;
@@ -653,7 +697,7 @@ IO::remove_input_port (Port* port, void* src)
                                } 
 
                                _session.engine().unregister_port (*port);
-                               drop_input_bundle ();
+                               check_bundles_connected_to_inputs ();
                                
                                setup_peak_meters ();
                                reset_panner ();
@@ -664,7 +708,7 @@ IO::remove_input_port (Port* port, void* src)
        }
 
        if (change == ConfigurationChanged) {
-               setup_bundles ();
+               setup_bundles_for_inputs_and_outputs ();
        }
 
        if (change != NoChange) {
@@ -679,7 +723,7 @@ IO::remove_input_port (Port* port, void* src)
 
 /** Add an input port.
  *
- * @param type Data type of port.  The appropriate Jack port type, and @ref Port will be created.
+ * @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.
  */
@@ -687,7 +731,6 @@ int
 IO::add_input_port (string source, void* src, DataType type)
 {
        Port* our_port;
-       char name[64];
        
        if (type == DataType::NIL)
                type = _default_type;
@@ -697,27 +740,21 @@ IO::add_input_port (string source, void* src, DataType type)
                
                { 
                        Glib::Mutex::Lock lm (io_lock);
-                       
-                       if (n_inputs() >= _input_maximum) {
+
+                       if (_input_maximum.get(type) >= 0 && n_inputs().get (type) >= _input_maximum.get (type)) {
                                return -1;
                        }
 
                        /* Create a new input port */
                        
-                       // FIXME: naming scheme for differently typed ports?
-                       if (_input_maximum.get(type) == 1) {
-                               snprintf (name, sizeof (name), _("%s/in"), _name.c_str());
-                       } else {
-                               snprintf (name, sizeof (name), _("%s/in %u"), _name.c_str(), find_input_port_hole());
-                       }
+                       string portname = build_legal_port_name (type, true);
 
-                       if ((our_port = _session.engine().register_input_port (type, name)) == 0) {
-                               error << string_compose(_("IO: cannot register input port %1"), name) << endmsg;
+                       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;
                        }
 
                        _inputs.add (our_port);
-                       drop_input_bundle ();
                        setup_peak_meters ();
                        reset_panner ();
                }
@@ -734,7 +771,7 @@ IO::add_input_port (string source, void* src, DataType type)
 
        // pan_changed (src); /* EMIT SIGNAL */
        input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
-       setup_bundles ();
+       setup_bundles_for_inputs_and_outputs ();
        _session.set_dirty ();
        
        return 0;
@@ -753,7 +790,7 @@ IO::disconnect_inputs (void* src)
                                _session.engine().disconnect (*i);
                        }
 
-                       drop_input_bundle ();
+                       check_bundles_connected_to_inputs ();
                }
        }
        
@@ -775,7 +812,7 @@ IO::disconnect_outputs (void* src)
                                _session.engine().disconnect (*i);
                        }
 
-                       drop_output_bundle ();
+                       check_bundles_connected_to_outputs ();
                }
        }
 
@@ -810,18 +847,12 @@ IO::ensure_inputs_locked (ChanCount count, bool clear, void* src)
                /* create any necessary new ports */
                while (n_inputs().get(*t) < n) {
 
-                       char buf[64];
-
-                       if (_input_maximum.get(*t) == 1) {
-                               snprintf (buf, sizeof (buf), _("%s/in"), _name.c_str());
-                       } else {
-                               snprintf (buf, sizeof (buf), _("%s/in %u"), _name.c_str(), find_input_port_hole());
-                       }
+                       string portname = build_legal_port_name (*t, true);
 
                        try {
 
-                               if ((input_port = _session.engine().register_input_port (*t, buf)) == 0) {
-                                       error << string_compose(_("IO: cannot register input port %1"), buf) << endmsg;
+                               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;
                                }
                        }
@@ -839,7 +870,7 @@ IO::ensure_inputs_locked (ChanCount count, bool clear, void* src)
        }
        
        if (changed) {
-               drop_input_bundle ();
+               check_bundles_connected_to_inputs ();
                setup_peak_meters ();
                reset_panner ();
                PortCountChanged (n_inputs()); /* EMIT SIGNAL */
@@ -856,16 +887,6 @@ IO::ensure_inputs_locked (ChanCount count, bool clear, void* src)
        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)
 {
@@ -925,19 +946,11 @@ IO::ensure_io (ChanCount in, ChanCount out, bool clear, void* src)
 
                        while (n_inputs().get(*t) < nin) {
 
-                               char buf[64];
-
-                               /* Create a new input port */
-
-                               if (_input_maximum.get(*t) == 1) {
-                                       snprintf (buf, sizeof (buf), _("%s/in"), _name.c_str());
-                               } else {
-                                       snprintf (buf, sizeof (buf), _("%s/in %u"), _name.c_str(), find_input_port_hole());
-                               }
+                               string portname = build_legal_port_name (*t, true);
 
                                try {
-                                       if ((port = _session.engine().register_input_port (*t, buf)) == 0) {
-                                               error << string_compose(_("IO: cannot register input port %1"), buf) << endmsg;
+                                       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;
                                        }
                                }
@@ -957,19 +970,11 @@ IO::ensure_io (ChanCount in, ChanCount out, bool clear, void* src)
 
                        while (n_outputs().get(*t) < nout) {
 
-                               char buf[64];
-
-                               /* Create a new output port */
-
-                               if (_output_maximum.get(*t) == 1) {
-                                       snprintf (buf, sizeof (buf), _("%s/out"), _name.c_str());
-                               } else {
-                                       snprintf (buf, sizeof (buf), _("%s/out %u"), _name.c_str(), find_output_port_hole());
-                               }
-
+                               string portname = build_legal_port_name (*t, false);
+                               
                                try { 
-                                       if ((port = _session.engine().register_output_port (*t, buf)) == 0) {
-                                               error << string_compose(_("IO: cannot register output port %1"), buf) << endmsg;
+                                       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;
                                        }
                                }
@@ -1006,18 +1011,18 @@ IO::ensure_io (ChanCount in, ChanCount out, bool clear, void* src)
        }
 
        if (out_changed) {
-               drop_output_bundle ();
+               check_bundles_connected_to_outputs ();
                output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
        }
        
        if (in_changed) {
-               drop_input_bundle ();
+               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 ();
+               setup_bundles_for_inputs_and_outputs ();
                _session.set_dirty ();
        }
 
@@ -1045,7 +1050,7 @@ IO::ensure_inputs (ChanCount count, bool clear, bool lockit, void* src)
 
        if (changed) {
                input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
-               setup_bundles ();
+               setup_bundles_for_inputs_and_outputs ();
                _session.set_dirty ();
        }
        return 0;
@@ -1080,16 +1085,10 @@ IO::ensure_outputs_locked (ChanCount count, bool clear, void* src)
                /* create any necessary new ports */
                while (n_outputs().get(*t) < n) {
 
-                       char buf[64];
-
-                       if (_output_maximum.get(*t) == 1) {
-                               snprintf (buf, sizeof (buf), _("%s/out"), _name.c_str());
-                       } else {
-                               snprintf (buf, sizeof (buf), _("%s/out %u"), _name.c_str(), find_output_port_hole());
-                       }
+                       string portname = build_legal_port_name (*t, false);
 
-                       if ((output_port = _session.engine().register_output_port (*t, buf)) == 0) {
-                               error << string_compose(_("IO: cannot register output port %1"), buf) << endmsg;
+                       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;
                        }
 
@@ -1104,7 +1103,7 @@ IO::ensure_outputs_locked (ChanCount count, bool clear, void* src)
        }
        
        if (changed) {
-               drop_output_bundle ();
+               check_bundles_connected_to_outputs ();
                PortCountChanged (n_outputs()); /* EMIT SIGNAL */
                _session.set_dirty ();
        }
@@ -1143,7 +1142,7 @@ IO::ensure_outputs (ChanCount count, bool clear, bool lockit, void* src)
 
        if (changed) {
                 output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
-                setup_bundles ();
+                setup_bundles_for_inputs_and_outputs ();
        }
 
        return 0;
@@ -1152,7 +1151,7 @@ IO::ensure_outputs (ChanCount count, bool clear, bool lockit, void* src)
 gain_t
 IO::effective_gain () const
 {
-       if (_gain_control->list()->automation_playback()) {
+       if (_gain_control->automation_playback()) {
                return _gain_control->get_value();
        } else {
                return _desired_gain;
@@ -1207,8 +1206,8 @@ IO::state (bool full_state)
        XMLNode* node = new XMLNode (state_node_name);
        char buf[64];
        string str;
-       bool need_ins = true;
-       bool need_outs = true;
+       vector<string>::iterator ci;
+       int n;
        LocaleGuard lg (X_("POSIX"));
        Glib::Mutex::Lock lm (io_lock);
 
@@ -1216,83 +1215,88 @@ IO::state (bool full_state)
        id().print (buf, sizeof (buf));
        node->add_property("id", buf);
 
-       str = "";
-
-       if (_input_bundle && !_input_bundle->dynamic()) {
-               node->add_property ("input-connection", _input_bundle->name());
-               need_ins = false;
+       for (
+         std::vector<UserBundleInfo>::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 ());
+               node->add_child_nocopy (*n);
        }
 
-       if (_output_bundle && !_output_bundle->dynamic()) {
-               node->add_property ("output-connection", _output_bundle->name());
-               need_outs = false;
+       for (
+         std::vector<UserBundleInfo>::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 = "";
 
-       if (need_ins) {
-               for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
+       for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
                        
-                       const char **connections = i->get_connections();
+               vector<string> connections;
+
+               if (i->get_connections (connections)) {
+
+                       str += '{';
                        
-                       if (connections && connections[0]) {
-                               str += '{';
+                       for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) {
+                               if (n) {
+                                       str += ',';
+                               }
                                
-                               for (int n = 0; connections && connections[n]; ++n) {
-                                       if (n) {
-                                               str += ',';
-                                       }
-                                       
-                                       /* if its a connection to our own port,
-                                          return only the port name, not the
-                                          whole thing. this allows connections
-                                          to be re-established even when our
-                                          client name is different.
-                                       */
-                                       
-                                       str += _session.engine().make_port_name_relative (connections[n]);
-                               }       
-
-                               str += '}';
+                               /* 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.
+                               */
                                
-                               free (connections);
-                       }
-                       else {
-                               str += "{}";
-                       }
+                               str += _session.engine().make_port_name_relative (*ci);
+                       }       
+                       
+                       str += '}';
+
+               } else {
+                       str += "{}";
                }
-               
-               node->add_property ("inputs", str);
        }
+       
+       node->add_property ("inputs", str);
 
-       if (need_outs) {
-               str = "";
+       str = "";
+       
+       for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
                
-               for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
+               vector<string> connections;
+
+               if (i->get_connections (connections)) {
                        
-                       const char **connections = i->get_connections();
+                       str += '{';
                        
-                       if (connections && connections[0]) {
-                               
-                               str += '{';
-                               
-                               for (int n = 0; connections[n]; ++n) {
-                                       if (n) {
-                                               str += ',';
-                                       }
-
-                                       str += _session.engine().make_port_name_relative (connections[n]);
+                       for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) {
+                               if (n) {
+                                       str += ',';
                                }
-
-                               str += '}';
                                
-                               free (connections);
-                       }
-                       else {
-                               str += "{}";
+                               str += _session.engine().make_port_name_relative (*ci);
                        }
+                       
+                       str += '}';
+
+               } else {
+                       str += "{}";
                }
-               
-               node->add_property ("outputs", str);
        }
+       
+       node->add_property ("outputs", str);
 
        node->add_child_nocopy (_panner->state (full_state));
        node->add_child_nocopy (_gain_control->get_state ());
@@ -1307,7 +1311,7 @@ IO::state (bool full_state)
        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, "%zd,%d,%zd,%d", _input_minimum.get(_default_type), in_max, _output_minimum.get(_default_type), out_max);
+       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);
 
@@ -1393,6 +1397,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);
@@ -1400,9 +1405,18 @@ 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), Parameter(GainAutomation));
+                       set_automation_state (*(*iter), Evoral::Parameter(GainAutomation));
                }
 
                if ((*iter)->name() == X_("controllable")) {
@@ -1423,6 +1437,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 {
@@ -1457,16 +1473,12 @@ IO::load_automation (string path)
        float version;
        LocaleGuard lg (X_("POSIX"));
 
-       fullpath = _session.automation_dir();
-       fullpath += path;
+       fullpath = Glib::build_filename(_session.automation_dir(), path);
 
        in.open (fullpath.c_str());
 
        if (!in) {
-               fullpath = _session.automation_dir();
-               fullpath += _session.snap_name();
-               fullpath += '-';
-               fullpath += path;
+               fullpath = Glib::build_filename(_session.automation_dir(), _session.snap_name() + '-' + path);
 
                in.open (fullpath.c_str());
 
@@ -1570,31 +1582,111 @@ IO::ports_became_legal ()
        return ret;
 }
 
+boost::shared_ptr<Bundle>
+IO::find_possible_bundle (const string &desired_name, const string &default_name, const string &bundle_type_name) 
+{
+       static const string digits = "0123456789";
+
+       boost::shared_ptr<Bundle> c = _session.bundle_by_name (desired_name);
+
+       if (!c) {
+               int bundle_number, mask;
+               string possible_name;
+               bool stereo = false;
+               string::size_type last_non_digit_pos;
+
+               error << string_compose(_("Unknown bundle \"%1\" listed for %2 of %3"), desired_name, bundle_type_name, _name)
+                     << endmsg;
+
+               // 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) {
+                       stringstream s;
+                       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) {
+                               stringstream s;
+                               s << desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1);
+                               s >> left_bundle_number;
+
+                               if (left_bundle_number > 0 && left_bundle_number + 1 == bundle_number) {
+                                       bundle_number--;
+                                       stereo = true;
+                               }
+                       }
+               }
+
+               // make 0-based
+               if (bundle_number)
+                       bundle_number--;
+
+               // find highest set bit
+               mask = 1;
+               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) {
+                                       break;
+                               }
+                       }
+                       mask >>= 1;
+               }
+               if (c) {
+                       info << string_compose (_("Bundle %1 was not available - \"%2\" used instead"), desired_name, possible_name)
+                            << endmsg;
+               } else {
+                       error << string_compose(_("No %1 bundles available as a replacement"), bundle_type_name)
+                             << endmsg;
+               }
+
+       }
+
+       return c;
+
+}
+
 int
 IO::create_ports (const XMLNode& node)
 {
-       const XMLProperty* prop;
-       int num_inputs = 0;
-       int num_outputs = 0;
+       XMLProperty const * prop;
+       uint32_t num_inputs = 0;
+       uint32_t num_outputs = 0;
 
-       /* XXX: we could change *-connection to *-bundle, but it seems a bit silly to
-        * break the session file format.
-        */
        if ((prop = node.property ("input-connection")) != 0) {
 
-               boost::shared_ptr<Bundle> c = _session.bundle_by_name (prop->value());
+               boost::shared_ptr<Bundle> c = find_possible_bundle (prop->value(), _("in"), _("input"));
                
-               if (c == 0) {
-                       error << string_compose(_("Unknown bundle \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
-
-                       if ((c = _session.bundle_by_name (_("in 1"))) == 0) {
-                               error << _("No input bundles available as a replacement")
-                                     << endmsg;
-                               return -1;
-                       }  else {
-                               info << string_compose (_("Bundle %1 was not available - \"in 1\" used instead"), prop->value())
-                                    << endmsg;
-                       }
+               if (!c) {
+                       return -1;
                } 
 
                num_inputs = c->nchannels();
@@ -1605,31 +1697,26 @@ IO::create_ports (const XMLNode& node)
        }
        
        if ((prop = node.property ("output-connection")) != 0) {
-               boost::shared_ptr<Bundle> c = _session.bundle_by_name (prop->value());
 
-               if (c == 0) {
-                       error << string_compose(_("Unknown bundle \"%1\" listed for output of %2"), prop->value(), _name) << endmsg;
+               boost::shared_ptr<Bundle> c = find_possible_bundle(prop->value(), _("out"), _("output"));
 
-                       if ((c = _session.bundle_by_name (_("out 1"))) == 0) {
-                               error << _("No output bundles available as a replacement")
-                                     << endmsg;
-                               return -1;
-                       }  else {
-                               info << string_compose (_("Bundle %1 was not available - \"out 1\" used instead"), prop->value())
-                                    << endmsg;
-                       }
+               if (!c) {
+                       return -1;
                } 
 
                num_outputs = c->nchannels ();
                
        } else if ((prop = node.property ("outputs")) != 0) {
+
                num_outputs = count (prop->value().begin(), prop->value().end(), '{');
        }
 
        no_panner_reset = true;
 
-       // FIXME: audio-only
-       if (ensure_io (ChanCount(DataType::AUDIO, num_inputs), ChanCount(DataType::AUDIO, 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;
        }
@@ -1646,22 +1733,14 @@ IO::create_ports (const XMLNode& node)
 int
 IO::make_connections (const XMLNode& node)
 {
+
        const XMLProperty* prop;
 
        if ((prop = node.property ("input-connection")) != 0) {
-               boost::shared_ptr<Bundle> c = _session.bundle_by_name (prop->value());
+               boost::shared_ptr<Bundle> c = find_possible_bundle (prop->value(), _("in"), _("input"));
                
-               if (c == 0) {
-                       error << string_compose(_("Unknown connection \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
-
-                       if ((c = _session.bundle_by_name (_("in 1"))) == 0) {
-                               error << _("No input connections available as a replacement")
-                                     << endmsg;
-                               return -1;
-                       } else {
-                               info << string_compose (_("Bundle %1 was not available - \"in 1\" used instead"), prop->value())
-                                    << endmsg;
-                       }
+               if (!c) {
+                       return -1;
                } 
 
                connect_input_ports_to_bundle (c, this);
@@ -1672,23 +1751,14 @@ IO::make_connections (const XMLNode& node)
                        return -1;
                }
        }
-       
-       if ((prop = node.property ("output-bundle")) != 0) {
-               boost::shared_ptr<Bundle> c = _session.bundle_by_name (prop->value());
-               
-               if (c == 0) {
-                       error << string_compose(_("Unknown bundle \"%1\" listed for output of %2"), prop->value(), _name) << endmsg;
 
-                       if ((c = _session.bundle_by_name (_("out 1"))) == 0) {
-                               error << _("No output bundles available as a replacement")
-                                     << endmsg;
-                               return -1;
-                       }  else {
-                               info << string_compose (_("Bundle %1 was not available - \"out 1\" used instead"), prop->value())
-                                    << endmsg;
-                       }
+       if ((prop = node.property ("output-connection")) != 0) {
+               boost::shared_ptr<Bundle> 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) {
@@ -1697,6 +1767,28 @@ IO::make_connections (const XMLNode& node)
                        return -1;
                }
        }
+
+       for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) {
+
+               if ((*i)->name() == "InputBundle") {
+                       XMLProperty const * prop = (*i)->property ("name");
+                       if (prop) {
+                               boost::shared_ptr<Bundle> b = find_possible_bundle (prop->value(), _("in"), _("input"));
+                               if (b) {
+                                       connect_input_ports_to_bundle (b, this);
+                               }
+                       }
+                       
+               } else if ((*i)->name() == "OutputBundle") {
+                       XMLProperty const * prop = (*i)->property ("name");
+                       if (prop) {
+                               boost::shared_ptr<Bundle> b = find_possible_bundle (prop->value(), _("out"), _("output"));
+                               if (b) {
+                                       connect_output_ports_to_bundle (b, this);
+                               } 
+                       }
+               }
+       }
        
        return 0;
 }
@@ -1851,34 +1943,42 @@ IO::parse_gain_string (const string& str, vector<string>& ports)
 }
 
 bool
-IO::set_name (const string& str)
+IO::set_name (const string& requested_name)
 {
-       if (str == _name) {
+       if (requested_name == _name) {
                return true;
        }
        
+       string name;
+       Route *rt;
+       if ( (rt = dynamic_cast<Route *>(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 */
-       string name = str;
 
        if (replace_all (name, ":", "-")) {
                warning << _("you cannot use colons to name objects with I/O connections") << endmsg;
        }
 
        for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
-               string current_name = i->short_name();
+               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);
        }
 
        bool const r = SessionObject::set_name(name);
 
-       setup_bundles ();
+       setup_bundles_for_inputs_and_outputs ();
 
        return r;
 }
@@ -1928,7 +2028,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;
                }
        }
@@ -1947,7 +2047,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;
                } 
        }
@@ -1958,76 +2058,28 @@ IO::input_latency () const
 int
 IO::connect_input_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
 {
-       uint32_t limit;
-
        {
                BLOCK_PROCESS_CALLBACK ();
                Glib::Mutex::Lock lm2 (io_lock);
-               
-               limit = c->nchannels();
-               
-               drop_input_bundle ();
-               
-               // FIXME bundles only work for audio-only
-               if (ensure_inputs (ChanCount(DataType::AUDIO, limit), false, false, src)) {
-                       return -1;
-               }
 
-               /* first pass: check the current state to see what's correctly
-                  connected, and drop anything that we don't want.
-               */
-               
-               for (uint32_t n = 0; n < limit; ++n) {
-                       const 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))) {
-                                       
-                                       /* clear any existing connections */
-                                       
-                                       _session.engine().disconnect (*_inputs.port(n));
-                                       
-                               } else if (_inputs.port(n)->connected() > 1) {
-                                       
-                                       /* OK, it is connected to the port we want,
-                                          but its also connected to other ports.
-                                          Change that situation.
-                                       */
-                                       
-                                       /* XXX could be optimized to not drop
-                                          the one we want.
-                                       */
-                                       
-                                       _session.engine().disconnect (*_inputs.port(n));
-                                       
-                               }
+               c->connect (_bundle_for_inputs, _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) {
+
+                       /* See if we already know about this one */
+                       std::vector<UserBundleInfo>::iterator i = _bundles_connected_to_inputs.begin();
+                       while (i != _bundles_connected_to_inputs.end() && i->bundle != ub) {
+                               ++i;
                        }
-               }
-               
-               /* second pass: connect all requested ports where necessary */
-               
-               for (uint32_t n = 0; n < limit; ++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 (i == _bundles_connected_to_inputs.end()) {
+                               /* We don't, so make a note */
+                               _bundles_connected_to_inputs.push_back (UserBundleInfo (this, ub));
                        }
                }
-               
-               _input_bundle = c;
-               
-               input_bundle_configuration_connection = c->ConfigurationChanged.connect
-                       (mem_fun (*this, &IO::input_bundle_configuration_changed));
-               input_bundle_connection_connection = c->PortsChanged.connect
-                       (mem_fun (*this, &IO::input_bundle_connection_changed));
        }
 
        input_changed (IOChange (ConfigurationChanged|ConnectionsChanged), src); /* EMIT SIGNAL */
@@ -2035,85 +2087,96 @@ 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)
 {
-       uint32_t limit; 
-
        {
                BLOCK_PROCESS_CALLBACK ();
                Glib::Mutex::Lock lm2 (io_lock);
 
-               limit = c->nchannels();
+               c->disconnect (_bundle_for_inputs, _session.engine());
                        
-               drop_output_bundle ();
+               /* If this is a UserBundle, make a note of what we've done */
 
-               // FIXME: audio-only
-               if (ensure_outputs (ChanCount(DataType::AUDIO, limit), false, false, src)) {
-                       return -1;
+               boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
+               if (ub) {
+
+                       std::vector<UserBundleInfo>::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()) {
+                               _bundles_connected_to_inputs.erase (i);
+                       }
                }
+       }
 
-               /* first pass: check the current state to see what's correctly
-                  connected, and drop anything that we don't want.
-               */
-                       
-               for (uint32_t n = 0; n < limit; ++n) {
+       input_changed (IOChange (ConfigurationChanged|ConnectionsChanged), src); /* EMIT SIGNAL */
+       return 0;
+}
 
-                       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))) {
+int
+IO::connect_output_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
+{
+       {
+               BLOCK_PROCESS_CALLBACK ();
+               Glib::Mutex::Lock lm2 (io_lock);
 
-                                       /* clear any existing connections */
+               c->connect (_bundle_for_outputs, _session.engine());
 
-                                       _session.engine().disconnect (*_outputs.port(n));
+               /* If this is a UserBundle, make a note of what we've done */
 
-                               } else if (_outputs.port(n)->connected() > 1) {
+               boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
+               if (ub) {
 
-                                       /* OK, it is connected to the port we want,
-                                          but its also connected to other ports.
-                                          Change that situation.
-                                       */
+                       /* See if we already know about this one */
+                       std::vector<UserBundleInfo>::iterator i = _bundles_connected_to_outputs.begin();
+                       while (i != _bundles_connected_to_outputs.end() && i->bundle != ub) {
+                               ++i;
+                       }
 
-                                       /* XXX could be optimized to not drop
-                                          the one we want.
-                                       */
-                                               
-                                       _session.engine().disconnect (*_outputs.port(n));
-                               }
+                       if (i == _bundles_connected_to_outputs.end()) {
+                               /* We don't, so make a note */
+                               _bundles_connected_to_outputs.push_back (UserBundleInfo (this, ub));
                        }
                }
+       }
 
-               /* second pass: connect all requested ports where necessary */
+       output_changed (IOChange (ConnectionsChanged|ConfigurationChanged), src); /* EMIT SIGNAL */
 
-               for (uint32_t n = 0; n < limit; ++n) {
+       return 0;
+}
 
-                       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;
-                                       }
-                               }
-                       }
-               }
+int
+IO::disconnect_output_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
+{
+       {
+               BLOCK_PROCESS_CALLBACK ();
+               Glib::Mutex::Lock lm2 (io_lock);
 
-               _output_bundle = c;
+               c->disconnect (_bundle_for_outputs, _session.engine());
+                       
+               /* If this is a UserBundle, make a note of what we've done */
 
-               output_bundle_configuration_connection = c->ConfigurationChanged.connect
-                       (mem_fun (*this, &IO::output_bundle_configuration_changed));
-               output_bundle_connection_connection = c->PortsChanged.connect
-                       (mem_fun (*this, &IO::output_bundle_connection_changed));
-       }
+               boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
+               if (ub) {
 
-       output_changed (IOChange (ConnectionsChanged|ConfigurationChanged), src); /* EMIT SIGNAL */
+                       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 ()
 {
@@ -2157,27 +2220,17 @@ IO::reset_panners ()
 }
 
 void
-IO::input_bundle_connection_changed (int ignored)
+IO::bundle_configuration_changed ()
 {
-       connect_input_ports_to_bundle (_input_bundle, this);
+       //XXX
+//     connect_input_ports_to_bundle (_input_bundle, this);
 }
 
 void
-IO::input_bundle_configuration_changed ()
+IO::bundle_ports_changed (int ignored)
 {
-       connect_input_ports_to_bundle (_input_bundle, this);
-}
-
-void
-IO::output_bundle_connection_changed (int ignored)
-{
-       connect_output_ports_to_bundle (_output_bundle, this);
-}
-
-void
-IO::output_bundle_configuration_changed ()
-{
-       connect_output_ports_to_bundle (_output_bundle, this);
+       //XXX
+//     connect_output_ports_to_bundle (_output_bundle, this);
 }
 
 void
@@ -2187,10 +2240,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
@@ -2202,8 +2254,8 @@ IO::GainControl::get_value (void) const
 void
 IO::setup_peak_meters()
 {
-       ChanCount max_streams = std::max(_inputs.count(), _outputs.count());
-       _meter->configure_io(max_streams, max_streams);
+       ChanCount max_streams = std::max (_inputs.count(), _outputs.count());
+       _meter->configure_io (max_streams, max_streams);
 }
 
 /**
@@ -2217,9 +2269,8 @@ IO::setup_peak_meters()
 void
 IO::update_meters()
 {
-    Glib::Mutex::Lock guard (m_meter_signal_lock);
-    
-    Meter(); /* EMIT SIGNAL */
+       Glib::Mutex::Lock guard (m_meter_signal_lock);
+       Meter(); /* EMIT SIGNAL */
 }
 
 void
@@ -2234,12 +2285,12 @@ IO::meter ()
 void
 IO::clear_automation ()
 {
-       Automatable::clear_automation (); // clears gain automation
-       _panner->clear_automation ();
+       data().clear (); // clears gain automation
+       _panner->data().clear();
 }
 
 void
-IO::set_parameter_automation_state (Parameter param, AutoState state)
+IO::set_parameter_automation_state (Evoral::Parameter param, AutoState state)
 {
        // XXX: would be nice to get rid of this special hack
 
@@ -2248,9 +2299,10 @@ IO::set_parameter_automation_state (Parameter param, AutoState state)
                bool changed = false;
 
                { 
-                       Glib::Mutex::Lock lm (_automation_lock);
+                       Glib::Mutex::Lock lm (control_lock());
 
-                       boost::shared_ptr<AutomationList> gain_auto = _gain_control->list();
+                       boost::shared_ptr<AutomationList> gain_auto
+                               = boost::dynamic_pointer_cast<AutomationList>(_gain_control->list());
 
                        if (state != gain_auto->automation_state()) {
                                changed = true;
@@ -2269,7 +2321,7 @@ IO::set_parameter_automation_state (Parameter param, AutoState state)
                }
 
        } else {
-               Automatable::set_parameter_automation_state(param, state);
+               AutomatableControls::set_parameter_automation_state(param, state);
        }
 }
 
@@ -2286,8 +2338,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);
@@ -2302,13 +2357,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->list()->automation_write()) {
+       /*
+       if (_session.transport_stopped() && src != 0 && src != this && _gain_control->automation_write()) {
                _gain_control->list()->add (_session.transport_frame(), val);
                
        }
+       */
 
        _session.set_dirty();
 }
@@ -2316,28 +2373,36 @@ IO::set_gain (gain_t val, void *src)
 void
 IO::start_pan_touch (uint32_t which)
 {
-       if (which < _panner->size()) {
-               (*_panner)[which]->pan_control()->list()->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()->list()->stop_touch();
+       if (which < _panner->npanners()) {
+               (*_panner).pan_control(which)->stop_touch();
        }
 
 }
 
 void
-IO::automation_snapshot (nframes_t now)
+IO::automation_snapshot (nframes_t now, bool force)
 {
-       Automatable::automation_snapshot (now);
+       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) > _session.automation_interval()) {
-               _panner->snapshot (now);
+       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;
 }
 
 void
@@ -2345,7 +2410,7 @@ IO::transport_stopped (nframes_t frame)
 {
        _gain_control->list()->reposition_for_rt_add (frame);
 
-       if (_gain_control->list()->automation_state() != Off) {
+       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.
@@ -2358,8 +2423,62 @@ IO::transport_stopped (nframes_t frame)
        _panner->transport_stopped (frame);
 }
 
+string
+IO::build_legal_port_name (DataType type, bool in)
+{
+       const int name_size = jack_port_name_size();
+       int limit;
+       string suffix;
+       int maxports;
+
+       if (type == DataType::AUDIO) {
+               suffix = _("audio");
+       } else if (type == DataType::MIDI) {
+               suffix = _("midi");
+       } else {
+               throw unknown_type();
+       }
+       
+       if (in) {
+               suffix += _("_in");
+               maxports = _input_maximum.get(type);
+       } else {
+               suffix += _("_out");
+               maxports = _output_maximum.get(type);
+       }
+       
+       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 (buf2, name_size+1, "%s %d", buf1, port_number);
+       
+       return string (buf2);
+}
+
 int32_t
-IO::find_input_port_hole ()
+IO::find_input_port_hole (const char* base)
 {
        /* CALLER MUST HOLD IO LOCK */
 
@@ -2369,14 +2488,17 @@ IO::find_input_port_hole ()
                return 1;
        }
 
-       for (n = 1; n < UINT_MAX; ++n) {
+       /* we only allow up to 4 characters for the port number
+        */
+
+       for (n = 1; n < 9999; ++n) {
                char buf[jack_port_name_size()];
                PortSet::iterator i = _inputs.begin();
 
-               snprintf (buf, jack_port_name_size(), _("%s/in %u"), _name.c_str(), n);
+               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;
                        }
                }
@@ -2389,7 +2511,7 @@ IO::find_input_port_hole ()
 }
 
 int32_t
-IO::find_output_port_hole ()
+IO::find_output_port_hole (const char* base)
 {
        /* CALLER MUST HOLD IO LOCK */
 
@@ -2399,14 +2521,17 @@ IO::find_output_port_hole ()
                return 1;
        }
 
-       for (n = 1; n < UINT_MAX; ++n) {
+       /* we only allow up to 4 characters for the port number
+        */
+
+       for (n = 1; n < 9999; ++n) {
                char buf[jack_port_name_size()];
                PortSet::iterator i = _outputs.begin();
 
-               snprintf (buf, jack_port_name_size(), _("%s/out %u"), _name.c_str(), n);
+               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;
                        }
                }
@@ -2419,6 +2544,13 @@ IO::find_output_port_hole ()
        return n;
 }
 
+void
+IO::set_active (bool yn)
+{
+       _active = yn; 
+        active_changed(); /* EMIT SIGNAL */
+}
+
 AudioPort*
 IO::audio_input(uint32_t n) const
 {
@@ -2477,97 +2609,154 @@ 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 ()
+IO::setup_bundles_for_inputs_and_outputs ()
 {
         char buf[32];
 
+       if (!_bundle_for_inputs) {
+               _bundle_for_inputs.reset (new Bundle (true));
+       }
+       if (!_bundle_for_outputs) {
+               _bundle_for_outputs.reset (new Bundle (false));
+       }
+
+       _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, 0);
-        int const ins = n_inputs().n_total();
-        _bundle_for_inputs->set_nchannels (ins);
-        
-        for (int i = 0; i < ins; ++i) {
-                _bundle_for_inputs->add_port_to_channel (i, _inputs.port(i)->name ());
-        }
+        _bundle_for_inputs->set_name (buf);
+       uint32_t const ni = inputs().num_ports();
+       for (uint32_t i = 0; i < ni; ++i) {
+               _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, 0);
-        int const outs = n_outputs().n_total();
-        _bundle_for_outputs->set_nchannels (outs);
-        
-        for (int i = 0; i < outs; ++i) {
-                _bundle_for_outputs->add_port_to_channel (i, _outputs.port(i)->name ());
-        }
+        _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()));
+       }
 }
 
+/** @return Bundles connected to our inputs */
+BundleList
+IO::bundles_connected_to_inputs ()
+{
+       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);
+       }
 
-/**
- *  Create and setup bundles that describe our inputs and outputs.
- */
+       /* Normal bundles */
+       boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
+       for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
+               if ((*i)->ports_are_outputs() == false || (*i)->nchannels() != n_inputs().n_total()) {
+                       continue;
+               }
 
-void
-IO::create_bundles ()
-{
-        _bundle_for_inputs = boost::shared_ptr<Bundle> (
-                new InputBundle ("", true)
-                );
-        
-        _bundle_for_outputs = boost::shared_ptr<Bundle> (
-                new OutputBundle ("", true)
-                );
+               for (uint32_t j = 0; j < n_inputs().n_total(); ++j) {
 
-        setup_bundles ();
+                       Bundle::PortList const& pl = (*i)->channel_ports (j);
+                       if (!pl.empty() && input(j)->connected_to (pl[0])) {
+                               bundles.push_back (*i);
+                       }
+
+               }
+       }
+
+       return bundles;
 }
 
-boost::shared_ptr<Bundle>
-IO::input_bundle()
+
+/* @return Bundles connected to our outputs */
+BundleList
+IO::bundles_connected_to_outputs ()
 {
-       if (_input_bundle) {
-               return _input_bundle;
+       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);
        }
 
-       /* XXX: will only report the first bundle found; should really return a list, I think */
-          
-       /* check that _input_bundle is right wrt the connections that are currently made */
+       /* Auto bundles */
+       boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
+       for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
+               if ((*i)->ports_are_inputs() == false || (*i)->nchannels() != n_outputs().n_total()) {
+                       continue;
+               }
 
-       /* make a vector of the first output connected to each of our inputs */
-       std::vector<std::string> connected;
-        for (uint32_t i = 0; i < _inputs.num_ports(); ++i) {
-               const char** c = _inputs.port(i)->get_connections ();
-               if (c) {
-                       connected.push_back (c[0]);
+               for (uint32_t j = 0; j < n_outputs().n_total(); ++j) {
+
+                       Bundle::PortList const& pl = (*i)->channel_ports (j);
+
+                       if (!pl.empty() && output(j)->connected_to (pl[0])) {
+                               bundles.push_back (*i);
+                       }
                }
        }
 
-       _input_bundle = _session.bundle_by_ports (connected);
-       return _input_bundle;
+       return bundles; 
 }
 
 
-boost::shared_ptr<Bundle>
-IO::output_bundle()
+IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
+{
+       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)
+               );
+}
+
+void
+IO::prepare_inputs (nframes_t nframes, nframes_t offset)
+{
+       /* io_lock, not taken: function must be called from Session::process() calltree */
+}
+
+void
+IO::flush_outputs (nframes_t nframes, nframes_t offset)
 {
-       if (_output_bundle) {
-               return _output_bundle;
+       /* 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];
        
-       /* XXX: will only report the first bundle found; should really return a list, I think */
-          
-       /* check that _output_bundle is right wrt the connections that are currently made */
-
-       /* make a vector of the first input connected to each of our outputs */
-       std::vector<std::string> connected;
-        for (uint32_t i = 0; i < _outputs.num_ports(); ++i) {
-               const char** c = _outputs.port(i)->get_connections ();
-               if (c) {
-                       connected.push_back (c[0]);
-               }
+       switch (n) {
+       case 1:
+               return _("mono");
+       case 2:
+               return c == 0 ? _("L") : _("R");
+       default:
+               snprintf (buf, sizeof(buf), _("%d"), (c + 1));
+               return buf;
        }
 
-       _output_bundle = _session.bundle_by_ports (connected);
-       return _output_bundle;
+       return "";
 }
+
+