Can't call the wrong function when there's only one of them: remove ARDOUR::Parameter...
[ardour.git] / libs / ardour / io.cc
index 323c58b65e1e83dbf6d5fcdae6ba87c6e73b3f38..0343545936fcdf07e348c8601cb4427bcc1e5e50 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2000 Paul Davis 
+    Copyright (C) 2000-2006 Paul Davis 
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -14,8 +14,6 @@
     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
-    $Id$
 */
 
 #include <fstream>
 
 #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/connection.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/dB.h>
+#include <ardour/buffer_set.h>
+#include <ardour/meter.h>
+#include <ardour/amp.h>
 
 #include "i18n.h"
 
@@ -53,31 +59,30 @@ extern "C" int isnan (double);
 extern "C" int isinf (double);
 #endif
 
+#define BLOCK_PROCESS_CALLBACK() Glib::Mutex::Lock em (_session.engine().process_lock())
 
 using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
+const string                 IO::state_node_name = "IO";
+bool                         IO::connecting_legal = false;
+bool                         IO::ports_legal = false;
+bool                         IO::panners_legal = false;
+sigc::signal<void>           IO::Meter;
+sigc::signal<int>            IO::ConnectingLegal;
+sigc::signal<int>            IO::PortsLegal;
+sigc::signal<int>            IO::PannersLegal;
+sigc::signal<void,ChanCount> IO::PortCountChanged;
+sigc::signal<int>            IO::PortsCreated;
 
-static float current_automation_version_number = 1.0;
-
-const string IO::state_node_name = "IO";
-bool         IO::connecting_legal = false;
-bool         IO::ports_legal = false;
-bool         IO::panners_legal = false;
-sigc::signal<void>                IO::Meter;
-sigc::signal<int>                 IO::ConnectingLegal;
-sigc::signal<int>                 IO::PortsLegal;
-sigc::signal<int>                 IO::PannersLegal;
-sigc::signal<void,uint32_t>  IO::MoreOutputs;
-sigc::signal<int>                 IO::PortsCreated;
-
-Glib::StaticMutex       IO::m_meter_signal_lock = GLIBMM_STATIC_MUTEX_INIT;
+Glib::StaticMutex IO::m_meter_signal_lock = GLIBMM_STATIC_MUTEX_INIT;
 
 /* this is a default mapper of [0 .. 1.0] control values to a gain coefficient.
    others can be imagined. 
 */
 
+#if 0
 static gain_t direct_control_to_gain (double fract) { 
        /* XXX Marcus writes: this doesn't seem right to me. but i don't have a better answer ... */
        /* this maxes at +6dB */
@@ -90,491 +95,310 @@ static double direct_gain_to_control (gain_t gain) {
        
        return pow((6.0*log(gain)/log(2.0)+192.0)/198.0, 8.0);
 }
-
-static bool sort_ports_by_name (Port* a, Port* b)
-{
-       return a->name() < b->name();
-}
-
+#endif
 
 /** @param default_type The type of port that will be created by ensure_io
  * and friends if no type is explicitly requested (to avoid breakage).
  */
-IO::IO (Session& s, string name,
+IO::IO (Session& s, const string& name,
        int input_min, int input_max, int output_min, int output_max,
-       DataType default_type)
-       : _session (s),
-         _name (name),
-         _default_type(default_type),
-         _gain_control (X_("gaincontrol"), *this),
-         _gain_automation_curve (0.0, 2.0, 1.0),
-         _input_minimum (input_min),
-         _input_maximum (input_max),
-         _output_minimum (output_min),
-         _output_maximum (output_max)
+       DataType default_type, bool public_ports)
+       : SessionObject(s, name),
+         AutomatableControls (s),
+         _output_buffers (new BufferSet()),
+         _active(true),
+         _default_type (default_type),
+         _public_ports (public_ports),
+         _input_minimum (ChanCount::ZERO),
+         _input_maximum (ChanCount::INFINITE),
+         _output_minimum (ChanCount::ZERO),
+         _output_maximum (ChanCount::INFINITE)
 {
        _panner = new Panner (name, _session);
+       _meter = new PeakMeter (_session);
+
+       if (input_min > 0) {
+               _input_minimum = ChanCount(_default_type, input_min);
+       }
+       if (input_max >= 0) {
+               _input_maximum = ChanCount(_default_type, input_max);
+       }
+       if (output_min > 0) {
+               _output_minimum = ChanCount(_default_type, output_min);
+       }
+       if (output_max >= 0) {
+               _output_maximum = ChanCount(_default_type, output_max);
+       }
+
        _gain = 1.0;
        _desired_gain = 1.0;
-       _input_connection = 0;
-       _output_connection = 0;
        pending_state_node = 0;
-       _ninputs = 0;
-       _noutputs = 0;
        no_panner_reset = false;
+       _phase_invert = false;
        deferred_state = 0;
 
+       boost::shared_ptr<AutomationList> gl(
+                       new AutomationList(Evoral::Parameter(GainAutomation)));
+
+       _gain_control = boost::shared_ptr<GainControl>(
+                       new GainControl(X_("gaincontrol"), *this, gl));
+
+       add_control(_gain_control);
+
        apply_gain_automation = false;
-       _ignore_gain_on_deliver = false;
        
-       _gain_automation_state = Off;
-       _gain_automation_style = Absolute;
-
        {
                // IO::Meter is emitted from another thread so the
                // Meter signal must be protected.
                Glib::Mutex::Lock guard (m_meter_signal_lock);
                m_meter_connection = Meter.connect (mem_fun (*this, &IO::meter));
        }
-}
-
-IO::~IO ()
-{
-
-       Glib::Mutex::Lock guard (m_meter_signal_lock);
        
-       Glib::Mutex::Lock lm (io_lock);
-       vector<Port *>::iterator i;
-
-       for (i = _inputs.begin(); i != _inputs.end(); ++i) {
-               _session.engine().unregister_port (*i);
-       }
-
-       for (i = _outputs.begin(); i != _outputs.end(); ++i) {
-               _session.engine().unregister_port (*i);
-       }
-
-       m_meter_connection.disconnect();
-}
+       // Connect to our own PortCountChanged signal to connect output buffers
+       IO::PortCountChanged.connect (mem_fun (*this, &IO::attach_buffers));
 
-void
-IO::silence (nframes_t nframes, nframes_t offset)
-{
-       /* io_lock, not taken: function must be called from Session::process() calltree */
+       _session.add_controllable (_gain_control);
 
-       for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
-               (*i)->silence (nframes, offset);
-       }
+       create_bundles_for_inputs_and_outputs ();
 }
 
-void
-IO::apply_declick (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, gain_t initial, gain_t target, bool invert_polarity)
+IO::IO (Session& s, const XMLNode& node, DataType dt)
+       : SessionObject(s, "unnamed io"),
+         AutomatableControls (s),
+         _output_buffers (new BufferSet()),
+         _active(true),
+         _default_type (dt)
 {
-       nframes_t declick = min ((nframes_t)128, nframes);
-       gain_t delta;
-       Sample *buffer;
-       double fractional_shift;
-       double fractional_pos;
-       gain_t polscale = invert_polarity ? -1.0f : 1.0f;
+       _meter = new PeakMeter (_session);
+       _public_ports = true; // XXX get this from node
+       _panner = 0;
+       deferred_state = 0;
+       no_panner_reset = false;
+       _desired_gain = 1.0;
+       _gain = 1.0;
 
-       if (nframes == 0) return;
+       apply_gain_automation = false;
        
-       fractional_shift = -1.0/declick;
-
-       if (target < initial) {
-               /* fade out: remove more and more of delta from initial */
-               delta = -(initial - target);
-       } else {
-               /* fade in: add more and more of delta from initial */
-               delta = target - initial;
-       }
+       boost::shared_ptr<AutomationList> gl(
+                       new AutomationList(Evoral::Parameter(GainAutomation)));
 
-       for (uint32_t n = 0; n < nbufs; ++n) {
+       _gain_control = boost::shared_ptr<GainControl>(
+                       new GainControl(X_("gaincontrol"), *this, gl));
 
-               buffer = bufs[n];
-               fractional_pos = 1.0;
+       add_control(_gain_control);
 
-               for (nframes_t nx = 0; nx < declick; ++nx) {
-                       buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
-                       fractional_pos += fractional_shift;
-               }
-               
-               /* now ensure the rest of the buffer has the target value
-                  applied, if necessary.
-               */
+       set_state (node);
 
-               if (declick != nframes) {
-                       float this_target;
+       {
+               // IO::Meter is emitted from another thread so the
+               // Meter signal must be protected.
+               Glib::Mutex::Lock guard (m_meter_signal_lock);
+               m_meter_connection = Meter.connect (mem_fun (*this, &IO::meter));
+       }
+       
+       // Connect to our own PortCountChanged signal to connect output buffers
+       IO::PortCountChanged.connect (mem_fun (*this, &IO::attach_buffers));
 
-                       if (invert_polarity) {
-                               this_target = -target;
-                       } else { 
-                               this_target = target;
-                       }
+       _session.add_controllable (_gain_control);
 
-                       if (this_target == 0.0) {
-                               memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
-                       } else if (this_target != 1.0) {
-                               for (nframes_t nx = declick; nx < nframes; ++nx) {
-                                       buffer[nx] *= this_target;
-                               }
-                       }
-               }
-       }
+       create_bundles_for_inputs_and_outputs ();
 }
 
-void
-IO::pan_automated (vector<Sample*>& bufs, uint32_t nbufs, nframes_t start, nframes_t end, nframes_t nframes, nframes_t offset)
+IO::~IO ()
 {
-       Sample* dst;
-
-       /* io_lock, not taken: function must be called from Session::process() calltree */
-
-       if (_noutputs == 0) {
-               return;
-       }
-
-       if (_noutputs == 1) {
-
-               dst = output(0)->get_buffer (nframes) + offset;
-
-               for (uint32_t n = 0; n < nbufs; ++n) {
-                       if (bufs[n] != dst) {
-                               memcpy (dst, bufs[n], sizeof (Sample) * nframes);
-                       } 
-               }
+       Glib::Mutex::Lock guard (m_meter_signal_lock);
+       Glib::Mutex::Lock lm (io_lock);
 
-               output(0)->mark_silence (false);
+       BLOCK_PROCESS_CALLBACK ();
 
-               return;
+       for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
+               _session.engine().unregister_port (*i);
        }
 
-       uint32_t o;
-       vector<Port *>::iterator out;
-       vector<Sample *>::iterator in;
-       Panner::iterator pan;
-       Sample* obufs[_noutputs];
-
-       /* the terrible silence ... */
-
-       for (out = _outputs.begin(), o = 0; out != _outputs.end(); ++out, ++o) {
-               obufs[o] = (*out)->get_buffer (nframes) + offset;
-               memset (obufs[o], 0, sizeof (Sample) * nframes);
-               (*out)->mark_silence (false);
+       for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
+               _session.engine().unregister_port (*i);
        }
 
-       uint32_t n;
+       m_meter_connection.disconnect();
 
-       for (pan = _panner->begin(), n = 0; n < nbufs; ++n, ++pan) {
-               (*pan)->distribute_automated (bufs[n], obufs, start, end, nframes, _session.pan_automation_buffer());
-       }
+       delete _meter;
+       delete _panner;
+       delete _output_buffers;
 }
 
 void
-IO::pan (vector<Sample*>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset, gain_t gain_coeff)
+IO::silence (nframes_t nframes, nframes_t offset)
 {
-       Sample* dst;
-       Sample* src;
-
        /* io_lock, not taken: function must be called from Session::process() calltree */
 
-       if (_noutputs == 0) {
-               return;
-       }
-
-       /* the panner can be empty if there are no inputs to the 
-          route, but still outputs
-       */
-
-       if (_panner->bypassed() || _panner->empty()) {
-               deliver_output_no_pan (bufs, nbufs, nframes, offset);
-               return;
+       for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
+               i->get_buffer().silence (nframes, offset);
        }
+}
 
-       if (_noutputs == 1) {
-
-               dst = output(0)->get_buffer (nframes) + offset;
-
-               if (gain_coeff == 0.0f) {
+/** Deliver bufs to the IO's output ports
+ *
+ * This function should automatically do whatever it necessary to correctly deliver bufs
+ * to the outputs, eg applying gain or pan or whatever else needs to be done.
+ */
+void
+IO::deliver_output (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
+{
+       // FIXME: type specific code doesn't actually need to be here, it will go away in time
 
-                       /* only one output, and gain was zero, so make it silent */
+       /* ********** AUDIO ********** */
 
-                       memset (dst, 0, sizeof (Sample) * nframes); 
-                       
-               } else if (gain_coeff == 1.0f){
+       // Apply gain if gain automation isn't playing
+       if ( ! apply_gain_automation) {
+               
+               gain_t dg = _gain; // desired gain
 
-                       /* mix all buffers into the output */
+               {
+                       Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
 
-                       uint32_t n;
-                       
-                       memcpy (dst, bufs[0], sizeof (Sample) * nframes);
-                       
-                       for (n = 1; n < nbufs; ++n) {
-                               src = bufs[n];
-                               
-                               for (nframes_t n = 0; n < nframes; ++n) {
-                                       dst[n] += src[n];
-                               }
+                       if (dm.locked()) {
+                               dg = _desired_gain;
                        }
 
-                       output(0)->mark_silence (false);
-
-               } else {
-
-                       /* mix all buffers into the output, scaling them all by the gain */
-
-                       uint32_t n;
-
-                       src = bufs[0];
-                       
-                       for (nframes_t n = 0; n < nframes; ++n) {
-                               dst[n] = src[n] * gain_coeff;
-                       }       
-
-                       for (n = 1; n < nbufs; ++n) {
-                               src = bufs[n];
-                               
-                               for (nframes_t n = 0; n < nframes; ++n) {
-                                       dst[n] += src[n] * gain_coeff;
-                               }       
-                       }
-                       
-                       output(0)->mark_silence (false);
                }
 
-               return;
+               if (dg != _gain || dg != 1.0)
+                       Amp::run_in_place(bufs, nframes, _gain, dg, _phase_invert);
        }
+       
+       // Use the panner to distribute audio to output port buffers
+       if (_panner && !_panner->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;
+               
+               while (i != bufs.end(type) && o != output_buffers().end (type)) {
+                       o->read_from(*i, nframes, offset);
+                       prev = i;
+                       ++i;
+                       ++o;
+               }
 
-       uint32_t o;
-       vector<Port *>::iterator out;
-       vector<Sample *>::iterator in;
-       Panner::iterator pan;
-       Sample* obufs[_noutputs];
-
-       /* the terrible silence ... */
+               /* extra outputs get a copy of the last buffer */
 
-       /* XXX this is wasteful but i see no way to avoid it */
-       
-       for (out = _outputs.begin(), o = 0; out != _outputs.end(); ++out, ++o) {
-               obufs[o] = (*out)->get_buffer (nframes) + offset;
-               memset (obufs[o], 0, sizeof (Sample) * nframes);
-               (*out)->mark_silence (false);
+               while (o != output_buffers().end(type)) {
+                       o->read_from(*prev, nframes, offset);
+                       ++o;
+               }
        }
 
-       uint32_t n;
-
-       for (pan = _panner->begin(), n = 0; n < nbufs; ++n) {
-               Panner::iterator tmp;
+       /* ********** MIDI ********** */
 
-               tmp = pan;
-               ++tmp;
+       // No MIDI, we're done here
+       if (bufs.count().n_midi() == 0) {
+               return;
+       }
 
-               (*pan)->distribute (bufs[n], obufs, gain_coeff, nframes);
+       const DataType type = DataType::MIDI;
 
-               if (tmp != _panner->end()) {
-                       pan = tmp;
-               }
+       // Copy any MIDI 1:1 to outputs
+       assert(bufs.count().n_midi() == output_buffers().count().n_midi());
+       BufferSet::iterator o = output_buffers().begin(type);
+       for (BufferSet::iterator i = bufs.begin(type); i != bufs.end(type); ++i, ++o) {
+               o->read_from(*i, nframes, offset);
        }
 }
 
 void
-IO::deliver_output (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset)
+IO::collect_input (BufferSet& outs, nframes_t nframes, nframes_t offset)
 {
-       /* io_lock, not taken: function must be called from Session::process() calltree */
-
-       if (_noutputs == 0) {
-               return;
-       }
+       assert(outs.available() >= n_inputs());
        
-       if (_panner->bypassed() || _panner->empty()) {
-               deliver_output_no_pan (bufs, nbufs, nframes, offset);
+       if (n_inputs() == ChanCount::ZERO)
                return;
-       }
-
 
-       gain_t dg;
-       gain_t pangain = _gain;
+       outs.set_count(n_inputs());
        
-       {
-               Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
+       for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
                
-               if (dm.locked()) {
-                       dg = _desired_gain;
-               } else {
-                       dg = _gain;
+               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);
                }
-       }
-
-       if (dg != _gain) {
-               apply_declick (bufs, nbufs, nframes, _gain, dg, false);
-               _gain = dg;
-               pangain = 1.0f;
-       } 
-
-       /* simple, non-automation panning to outputs */
 
-       if (_session.transport_speed() > 1.5f || _session.transport_speed() < -1.5f) {
-               pan (bufs, nbufs, nframes, offset, pangain * speed_quietning);
-       } else {
-               pan (bufs, nbufs, nframes, offset, pangain);
        }
 }
 
 void
-IO::deliver_output_no_pan (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset)
+IO::just_meter_input (nframes_t start_frame, nframes_t end_frame, 
+                     nframes_t nframes, nframes_t offset)
 {
-       /* io_lock, not taken: function must be called from Session::process() calltree */
-
-       if (_noutputs == 0) {
-               return;
-       }
-
-       gain_t dg;
-       gain_t old_gain = _gain;
-
-       if (apply_gain_automation || _ignore_gain_on_deliver) {
-
-               /* gain has already been applied by automation code. do nothing here except
-                  speed quietning.
-               */
-
-               _gain = 1.0f;
-               dg = _gain;
-               
-       } else {
-
-               Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
-               
-               if (dm.locked()) {
-                       dg = _desired_gain;
-               } else {
-                       dg = _gain;
-               }
-       }
-
-       Sample* src;
-       Sample* dst;
-       uint32_t i;
-       vector<Port*>::iterator o;
-       vector<Sample*> outs;
-       gain_t actual_gain;
-
-       if (dg != _gain) {
-               /* unlikely condition */
-               for (o = _outputs.begin(), i = 0; o != _outputs.end(); ++o, ++i) {
-                       outs.push_back ((*o)->get_buffer (nframes) + offset);
-               }
-       }
-
-       /* reduce nbufs to the index of the last input buffer */
+       BufferSet& bufs = _session.get_scratch_buffers (n_inputs());
 
-       nbufs--;
+       collect_input (bufs, nframes, offset);
 
-       if (_session.transport_speed() > 1.5f || _session.transport_speed() < -1.5f) {
-               actual_gain = _gain * speed_quietning;
-       } else {
-               actual_gain = _gain;
-       }
-       
-       for (o = _outputs.begin(), i = 0; o != _outputs.end(); ++o, ++i) {
-
-               dst = (*o)->get_buffer (nframes) + offset;
-               src = bufs[min(nbufs,i)];
-
-               if (dg != _gain || actual_gain == 1.0f) {
-                       memcpy (dst, src, sizeof (Sample) * nframes);
-               } else if (actual_gain == 0.0f) {
-                       memset (dst, 0, sizeof (Sample) * nframes);
-               } else {
-                       for (nframes_t x = 0; x < nframes; ++x) {
-                               dst[x] = src[x] * actual_gain;
-                       }
-               }
-               
-               (*o)->mark_silence (false);
-       }
+       _meter->run_in_place(bufs, start_frame, end_frame, nframes, offset);
+}
 
-       if (dg != _gain) {
-               apply_declick (outs, outs.size(), nframes, _gain, dg, false);
-               _gain = dg;
-       }
 
-       if (apply_gain_automation || _ignore_gain_on_deliver) {
-               _gain = old_gain;
-       }
+void
+IO::check_bundles_connected_to_inputs ()
+{
+       check_bundles (_bundles_connected_to_inputs, inputs());
 }
 
 void
-IO::collect_input (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset)
+IO::check_bundles_connected_to_outputs ()
 {
-       /* io_lock, not taken: function must be called from Session::process() calltree */
+       check_bundles (_bundles_connected_to_outputs, outputs());
+}
 
-       vector<Port *>::iterator i;
-       uint32_t n;
-       Sample *last = 0;
+void
+IO::check_bundles (std::vector<UserBundleInfo>& list, const PortSet& ports)
+{
+       std::vector<UserBundleInfo> new_list;
        
-       /* we require that bufs.size() >= 1 */
+       for (std::vector<UserBundleInfo>::iterator i = list.begin(); i != list.end(); ++i) {
 
-       for (n = 0, i = _inputs.begin(); n < nbufs; ++i, ++n) {
-               if (i == _inputs.end()) {
-                       break;
-               }
-               
-               /* XXX always read the full extent of the port buffer that
-                  we need. One day, we may use jack_port_get_buffer_at_offset()
-                  or something similar. For now, this simple hack will
-                  have to do.
-
-                  Hack? Why yes .. we only need to read nframes-worth of
-                  data, but the data we want is at `offset' within the
-                  buffer.
-               */
+               ChanCount const N = i->bundle->nchannels ();
 
-               last = (*i)->get_buffer (nframes+offset) + offset;
-               // the dest buffer's offset has already been applied
-               memcpy (bufs[n], last, sizeof (Sample) * nframes);
-       }
+               if (ports.num_ports (default_type()) < N.get (default_type())) {
+                       continue;
+               }
 
-       /* fill any excess outputs with the last input */
-       
-       while (n < nbufs && last) {
-               // the dest buffer's offset has already been applied
-               memcpy (bufs[n], last, sizeof (Sample) * nframes);
-               ++n;
-       }
-}
+               bool ok = true;
+               uint32_t n = N.get (default_type());
 
-void
-IO::just_meter_input (nframes_t start_frame, nframes_t end_frame, 
-                     nframes_t nframes, nframes_t offset)
-{
-       vector<Sample*>& bufs = _session.get_passthru_buffers ();
-       uint32_t nbufs = n_process_buffers ();
+               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);
+                       for (uint32_t k = 0; k < pl.size(); ++k) {
+                               if (ports.port(j)->connected_to (pl[k]) == false) {
+                                       ok = false;
+                                       break;
+                               }
+                       }
 
-       collect_input (bufs, nbufs, nframes, offset);
+                       if (ok == false) {
+                               break;
+                       }
+               }
 
-       for (uint32_t n = 0; n < nbufs; ++n) {
-               _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
+               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 ();
+               }
        }
-}
 
-void
-IO::drop_input_connection ()
-{
-       _input_connection = 0;
-       input_connection_configuration_connection.disconnect();
-       input_connection_connection_connection.disconnect();
-       _session.set_dirty ();
+       list = new_list;
 }
 
-void
-IO::drop_output_connection ()
-{
-       _output_connection = 0;
-       output_connection_configuration_connection.disconnect();
-       output_connection_connection_connection.disconnect();
-       _session.set_dirty ();
-}
 
 int
 IO::disconnect_input (Port* our_port, string other_port, void* src)
@@ -584,14 +408,14 @@ IO::disconnect_input (Port* our_port, string other_port, void* src)
        }
 
        { 
-               Glib::Mutex::Lock em (_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
                
                {
                        Glib::Mutex::Lock lm (io_lock);
                        
                        /* check that our_port is really one of ours */
                        
-                       if (find (_inputs.begin(), _inputs.end(), our_port) == _inputs.end()) {
+                       if ( ! _inputs.contains(our_port)) {
                                return -1;
                        }
                        
@@ -602,7 +426,7 @@ IO::disconnect_input (Port* our_port, string other_port, void* src)
                                return -1;
                        }
 
-                       drop_input_connection();
+                       check_bundles_connected_to_inputs ();
                }
        }
 
@@ -620,14 +444,14 @@ IO::connect_input (Port* our_port, string other_port, void* src)
        }
 
        {
-               Glib::Mutex::Lock em(_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
                
                {
                        Glib::Mutex::Lock lm (io_lock);
                        
                        /* check that our_port is really one of ours */
                        
-                       if (find (_inputs.begin(), _inputs.end(), our_port) == _inputs.end()) {
+                       if ( ! _inputs.contains(our_port) ) {
                                return -1;
                        }
                        
@@ -636,8 +460,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_connection ();
                }
        }
 
@@ -654,12 +476,14 @@ IO::disconnect_output (Port* our_port, string other_port, void* src)
        }
 
        {
-               Glib::Mutex::Lock em(_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
                
                {
                        Glib::Mutex::Lock lm (io_lock);
                        
-                       if (find (_outputs.begin(), _outputs.end(), our_port) == _outputs.end()) {
+                       /* check that our_port is really one of ours */
+                       
+                       if ( ! _outputs.contains(our_port) ) {
                                return -1;
                        }
                        
@@ -670,7 +494,7 @@ IO::disconnect_output (Port* our_port, string other_port, void* src)
                                return -1;
                        }
 
-                       drop_output_connection ();
+                       check_bundles_connected_to_outputs ();
                }
        }
 
@@ -687,14 +511,15 @@ IO::connect_output (Port* our_port, string other_port, void* src)
        }
 
        {
-               Glib::Mutex::Lock em(_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
+
                
                {
                        Glib::Mutex::Lock lm (io_lock);
                        
                        /* check that our_port is really one of ours */
                        
-                       if (find (_outputs.begin(), _outputs.end(), our_port) == _outputs.end()) {
+                       if ( ! _outputs.contains(our_port) ) {
                                return -1;
                        }
                        
@@ -703,8 +528,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_connection ();
                }
        }
 
@@ -720,24 +543,24 @@ IO::set_input (Port* other_port, void* src)
           to the specified source.
        */
 
-       if (_input_minimum > 1 || _input_minimum == 0) {
+       if (_input_minimum.n_total() > 1) {
                /* sorry, you can't do this */
                return -1;
        }
 
        if (other_port == 0) {
-               if (_input_minimum < 0) {
-                       return ensure_inputs (0, false, true, src);
+               if (_input_minimum == ChanCount::ZERO) {
+                       return ensure_inputs (ChanCount::ZERO, false, true, src);
                } else {
                        return -1;
                }
        }
 
-       if (ensure_inputs (1, true, true, src)) {
+       if (ensure_inputs (ChanCount(other_port->type(), 1), true, true, src)) {
                return -1;
        }
 
-       return connect_input (_inputs.front(), other_port->name(), src);
+       return connect_input (_inputs.port(0), other_port->name(), src);
 }
 
 int
@@ -746,41 +569,41 @@ IO::remove_output_port (Port* port, void* src)
        IOChange change (NoChange);
 
        {
-               Glib::Mutex::Lock em(_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
+
                
                {
                        Glib::Mutex::Lock lm (io_lock);
-                       
-                       if (_noutputs - 1 == (uint32_t) _output_minimum) {
+
+                       if (n_outputs() <= _output_minimum) {
                                /* sorry, you can't do this */
                                return -1;
                        }
-                       
-                       for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
-                               if (*i == port) {
-                                       change = IOChange (change|ConfigurationChanged);
-                                       if (port->connected()) {
-                                               change = IOChange (change|ConnectionsChanged);
-                                       } 
-
-                                       _session.engine().unregister_port (*i);
-                                       _outputs.erase (i);
-                                       _noutputs--;
-                                       drop_output_connection ();
 
-                                       break;
-                               }
-                       }
+                       if (_outputs.remove(port)) {
+                               change = IOChange (change|ConfigurationChanged);
+
+                               if (port->connected()) {
+                                       change = IOChange (change|ConnectionsChanged);
+                               } 
 
-                       if (change != NoChange) {
+                               _session.engine().unregister_port (*port);
+                               check_bundles_connected_to_outputs ();
+                               
                                setup_peak_meters ();
                                reset_panner ();
                        }
                }
+
+               PortCountChanged (n_outputs()); /* EMIT SIGNAL */
        }
-       
+
+       if (change == ConnectionsChanged) {
+               setup_bundles_for_inputs_and_outputs ();
+       }
+
        if (change != NoChange) {
-               output_changed (change, src); /* EMIT SIGNAL */
+               output_changed (change, src);
                _session.set_dirty ();
                return 0;
        }
@@ -798,44 +621,36 @@ int
 IO::add_output_port (string destination, void* src, DataType type)
 {
        Port* our_port;
-       char name[64];
 
        if (type == DataType::NIL)
                type = _default_type;
 
        {
-               Glib::Mutex::Lock em(_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
+
                
                { 
                        Glib::Mutex::Lock lm (io_lock);
                        
-                       if (_output_maximum >= 0 && (int) _noutputs == _output_maximum) {
+                       if (n_outputs() >= _output_maximum) {
                                return -1;
                        }
                
                        /* Create a new output port */
                        
-                       // FIXME: naming scheme for differently typed ports?
-                       if (_output_maximum == 1) {
-                               snprintf (name, sizeof (name), _("%s/out"), _name.c_str());
-                       } else {
-                               snprintf (name, sizeof (name), _("%s/out %u"), _name.c_str(), find_output_port_hole());
-                       }
+                       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.push_back (our_port);
-                       sort (_outputs.begin(), _outputs.end(), sort_ports_by_name);
-                       ++_noutputs;
-                       drop_output_connection ();
+                       _outputs.add (our_port);
                        setup_peak_meters ();
                        reset_panner ();
                }
 
-               MoreOutputs (_noutputs); /* EMIT SIGNAL */
+               PortCountChanged (n_outputs()); /* EMIT SIGNAL */
        }
 
        if (destination.length()) {
@@ -846,7 +661,9 @@ IO::add_output_port (string destination, void* src, DataType type)
        
        // pan_changed (src); /* EMIT SIGNAL */
        output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
+       setup_bundles_for_inputs_and_outputs ();
        _session.set_dirty ();
+
        return 0;
 }
 
@@ -856,38 +673,37 @@ IO::remove_input_port (Port* port, void* src)
        IOChange change (NoChange);
 
        {
-               Glib::Mutex::Lock em(_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
+
                
                {
                        Glib::Mutex::Lock lm (io_lock);
 
-                       if (((int)_ninputs - 1) < _input_minimum) {
+                       if (n_inputs() <= _input_minimum) {
                                /* sorry, you can't do this */
                                return -1;
                        }
-                       for (vector<Port *>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
-
-                               if (*i == port) {
-                                       change = IOChange (change|ConfigurationChanged);
 
-                                       if (port->connected()) {
-                                               change = IOChange (change|ConnectionsChanged);
-                                       } 
+                       if (_inputs.remove(port)) {
+                               change = IOChange (change|ConfigurationChanged);
 
-                                       _session.engine().unregister_port (*i);
-                                       _inputs.erase (i);
-                                       _ninputs--;
-                                       drop_input_connection ();
+                               if (port->connected()) {
+                                       change = IOChange (change|ConnectionsChanged);
+                               } 
 
-                                       break;
-                               }
-                       }
-                       
-                       if (change != NoChange) {
+                               _session.engine().unregister_port (*port);
+                               check_bundles_connected_to_inputs ();
+                               
                                setup_peak_meters ();
                                reset_panner ();
                        }
                }
+               
+               PortCountChanged (n_inputs ()); /* EMIT SIGNAL */
+       }
+
+       if (change == ConfigurationChanged) {
+               setup_bundles_for_inputs_and_outputs ();
        }
 
        if (change != NoChange) {
@@ -895,14 +711,14 @@ IO::remove_input_port (Port* port, void* src)
                _session.set_dirty ();
                return 0;
        } 
-
+       
        return -1;
 }
 
 
 /** Add an input port.
  *
- * @param type Data type of port.  The appropriate Jack port type, and @ref Port will be created.
+ * @param 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.
  */
@@ -910,44 +726,35 @@ int
 IO::add_input_port (string source, void* src, DataType type)
 {
        Port* our_port;
-       char name[64];
        
        if (type == DataType::NIL)
                type = _default_type;
 
        {
-               Glib::Mutex::Lock em (_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
                
                { 
                        Glib::Mutex::Lock lm (io_lock);
-                       
-                       if (_input_maximum >= 0 && (int) _ninputs == _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 == 1) {
-                               snprintf (name, sizeof (name), _("%s/in"), _name.c_str());
-                       } else {
-                               snprintf (name, sizeof (name), _("%s/in %u"), _name.c_str(), find_input_port_hole());
-                       }
-                       
-                       if ((our_port = _session.engine().register_input_port (type, name)) == 0) {
-                               error << string_compose(_("IO: cannot register input port %1"), name) << endmsg;
+                       string portname = build_legal_port_name (type, true);
+
+                       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.push_back (our_port);
-                       sort (_inputs.begin(), _inputs.end(), sort_ports_by_name);
-                       ++_ninputs;
-                       drop_input_connection ();
+
+                       _inputs.add (our_port);
                        setup_peak_meters ();
                        reset_panner ();
                }
 
-               MoreOutputs (_ninputs); /* EMIT SIGNAL */
+               PortCountChanged (n_inputs()); /* EMIT SIGNAL */
        }
 
        if (source.length()) {
@@ -959,8 +766,9 @@ IO::add_input_port (string source, void* src, DataType type)
 
        // pan_changed (src); /* EMIT SIGNAL */
        input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
+       setup_bundles_for_inputs_and_outputs ();
        _session.set_dirty ();
-
+       
        return 0;
 }
 
@@ -968,19 +776,21 @@ int
 IO::disconnect_inputs (void* src)
 {
        { 
-               Glib::Mutex::Lock em (_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
                
                {
                        Glib::Mutex::Lock lm (io_lock);
                        
-                       for (vector<Port *>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
+                       for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
                                _session.engine().disconnect (*i);
                        }
 
-                       drop_input_connection ();
+                       check_bundles_connected_to_inputs ();
                }
        }
+       
        input_changed (ConnectionsChanged, src); /* EMIT SIGNAL */
+       
        return 0;
 }
 
@@ -988,89 +798,83 @@ int
 IO::disconnect_outputs (void* src)
 {
        {
-               Glib::Mutex::Lock em (_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
                
                {
                        Glib::Mutex::Lock lm (io_lock);
                        
-                       for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
+                       for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
                                _session.engine().disconnect (*i);
                        }
 
-                       drop_output_connection ();
+                       check_bundles_connected_to_outputs ();
                }
        }
 
        output_changed (ConnectionsChanged, src); /* EMIT SIGNAL */
        _session.set_dirty ();
+       
        return 0;
 }
 
 bool
-IO::ensure_inputs_locked (uint32_t n, bool clear, void* src)
+IO::ensure_inputs_locked (ChanCount count, bool clear, void* src)
 {
-       Port* input_port;
-       bool changed = false;
-       bool reduced = false;
-       
-       /* remove unused ports */
+       Port* input_port = 0;
+       bool  changed    = false;
 
-       while (_ninputs > n) {
-               _session.engine().unregister_port (_inputs.back());
-               _inputs.pop_back();
-               _ninputs--;
-               reduced = true;
-               changed = true;
-       }
-               
-       /* create any necessary new ports */
-               
-       while (_ninputs < n) {
-               
-               char buf[64];
-               
-               /* Create a new input port (of the default type) */
+
+       for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
                
-               if (_input_maximum == 1) {
-                       snprintf (buf, sizeof (buf), _("%s/in"), _name.c_str());
-               }
-               else {
-                       snprintf (buf, sizeof (buf), _("%s/in %u"), _name.c_str(), find_input_port_hole());
+               const size_t n = count.get(*t);
+       
+               /* remove unused ports */
+               for (size_t i = n_inputs().get(*t); i > n; --i) {
+                       input_port = _inputs.port(*t, i-1);
+
+                       assert(input_port);
+                       _inputs.remove(input_port);
+                       _session.engine().unregister_port (*input_port);
+
+                       changed = true;
                }
-               
-               try {
-                       
-                       if ((input_port = _session.engine().register_input_port (_default_type, buf)) == 0) {
-                               error << string_compose(_("IO: cannot register input port %1"), buf) << endmsg;
-                               return -1;
+
+               /* create any necessary new ports */
+               while (n_inputs().get(*t) < n) {
+
+                       string portname = build_legal_port_name (*t, true);
+
+                       try {
+
+                               if ((input_port = _session.engine().register_input_port (*t, portname, _public_ports)) == 0) {
+                                       error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
+                                       return -1;
+                               }
                        }
-               }
 
-               catch (AudioEngine::PortRegistrationFailure& err) {
-                       setup_peak_meters ();
-                       reset_panner ();
-                       /* pass it on */
-                       throw err;
+                       catch (AudioEngine::PortRegistrationFailure& err) {
+                               setup_peak_meters ();
+                               reset_panner ();
+                               /* pass it on */
+                               throw AudioEngine::PortRegistrationFailure();
+                       }
+
+                       _inputs.add (input_port);
+                       changed = true;
                }
-               
-               _inputs.push_back (input_port);
-               sort (_inputs.begin(), _inputs.end(), sort_ports_by_name);
-               ++_ninputs;
-               changed = true;
        }
        
        if (changed) {
-               drop_input_connection ();
+               check_bundles_connected_to_inputs ();
                setup_peak_meters ();
                reset_panner ();
-               MoreOutputs (_ninputs); /* EMIT SIGNAL */
+               PortCountChanged (n_inputs()); /* EMIT SIGNAL */
                _session.set_dirty ();
        }
        
        if (clear) {
                /* disconnect all existing ports so that we get a fresh start */
-                       
-               for (vector<Port *>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
+               for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
                        _session.engine().disconnect (*i);
                }
        }
@@ -1078,133 +882,129 @@ IO::ensure_inputs_locked (uint32_t n, 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 (uint32_t nin, uint32_t nout, bool clear, void* src)
+IO::ensure_io (ChanCount in, ChanCount out, bool clear, void* src)
 {
-       bool in_changed = false;
-       bool out_changed = false;
-       bool in_reduced = false;
-       bool out_reduced = false;
-       bool need_pan_reset;
+       bool in_changed     = false;
+       bool out_changed    = false;
+       bool need_pan_reset = false;
 
-       if (_input_maximum >= 0) {
-               nin = min (_input_maximum, (int) nin);
-       }
+       in = min (_input_maximum, in);
 
-       if (_output_maximum >= 0) {
-               nout = min (_output_maximum, (int) nout);
-       }
+       out = min (_output_maximum, out);
 
-       if (nin == _ninputs && nout == _noutputs && !clear) {
+       if (in == n_inputs() && out == n_outputs() && !clear) {
                return 0;
        }
 
        {
-               Glib::Mutex::Lock em (_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
                Glib::Mutex::Lock lm (io_lock);
 
                Port* port;
                
-               if (_noutputs == nout) {
-                       need_pan_reset = false;
-               } else {
+               if (n_outputs() != out) {
                        need_pan_reset = true;
                }
                
-               /* remove unused ports */
-               
-               while (_ninputs > nin) {
-                       _session.engine().unregister_port (_inputs.back());
-                       _inputs.pop_back();
-                       _ninputs--;
-                       in_reduced = true;
-                       in_changed = true;
-               }
-               
-               while (_noutputs > nout) {
-                       _session.engine().unregister_port (_outputs.back());
-                       _outputs.pop_back();
-                       _noutputs--;
-                       out_reduced = true;
-                       out_changed = true;
-               }
-               
-               /* create any necessary new ports (of the default type) */
-               
-               while (_ninputs < nin) {
-                       
-                       char buf[64];
+               for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
 
-                       /* Create a new input port */
-                       
-                       if (_input_maximum == 1) {
-                               snprintf (buf, sizeof (buf), _("%s/in"), _name.c_str());
+                       const size_t nin = in.get(*t);
+                       const size_t nout = out.get(*t);
+
+                       Port* output_port = 0;
+                       Port* input_port = 0;
+
+                       /* remove unused output ports */
+                       for (size_t i = n_outputs().get(*t); i > nout; --i) {
+                               output_port = _outputs.port(*t, i-1);
+
+                               assert(output_port);
+                               _outputs.remove(output_port);
+                               _session.engine().unregister_port (*output_port);
+
+                               out_changed = true;
                        }
-                       else {
-                               snprintf (buf, sizeof (buf), _("%s/in %u"), _name.c_str(), find_input_port_hole());
+
+                       /* remove unused input ports */
+                       for (size_t i = n_inputs().get(*t); i > nin; --i) {
+                               input_port = _inputs.port(*t, i-1);
+
+                               assert(input_port);
+                               _inputs.remove(input_port);
+                               _session.engine().unregister_port (*input_port);
+
+                               in_changed = true;
                        }
-                       
-                       try {
-                               if ((port = _session.engine().register_input_port (_default_type, buf)) == 0) {
-                                       error << string_compose(_("IO: cannot register input port %1"), buf) << endmsg;
-                                       return -1;
+
+                       /* create any necessary new input ports */
+
+                       while (n_inputs().get(*t) < nin) {
+
+                               string portname = build_legal_port_name (*t, true);
+
+                               try {
+                                       if ((port = _session.engine().register_input_port (*t, portname, _public_ports)) == 0) {
+                                               error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
+                                               return -1;
+                                       }
+                               }
+                               
+                               catch (AudioEngine::PortRegistrationFailure& err) {
+                                       setup_peak_meters ();
+                                       reset_panner ();
+                                       /* pass it on */
+                                       throw AudioEngine::PortRegistrationFailure();
                                }
-                       }
 
-                       catch (AudioEngine::PortRegistrationFailure& err) {
-                               setup_peak_meters ();
-                               reset_panner ();
-                               /* pass it on */
-                               throw err;
+                               _inputs.add (port);
+                               in_changed = true;
                        }
-               
-                       _inputs.push_back (port);
-                       ++_ninputs;
-                       in_changed = true;
-               }
 
-               /* create any necessary new ports */
-               
-               while (_noutputs < nout) {
-                       
-                       char buf[64];
-                       
-                       /* Create a new output port */
-                       
-                       if (_output_maximum == 1) {
-                               snprintf (buf, sizeof (buf), _("%s/out"), _name.c_str());
-                       } else {
-                               snprintf (buf, sizeof (buf), _("%s/out %u"), _name.c_str(), find_output_port_hole());
-                       }
-                       
-                       try { 
-                               if ((port = _session.engine().register_output_port (_default_type, buf)) == 0) {
-                                       error << string_compose(_("IO: cannot register output port %1"), buf) << endmsg;
-                                       return -1;
+                       /* create any necessary new output ports */
+
+                       while (n_outputs().get(*t) < nout) {
+
+                               string portname = build_legal_port_name (*t, false);
+                               
+                               try { 
+                                       if ((port = _session.engine().register_output_port (*t, portname, _public_ports)) == 0) {
+                                               error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
+                                               return -1;
+                                       }
                                }
+
+                               catch (AudioEngine::PortRegistrationFailure& err) {
+                                       setup_peak_meters ();
+                                       reset_panner ();
+                                       /* pass it on */
+                                       throw AudioEngine::PortRegistrationFailure ();
+                               }
+
+                               _outputs.add (port);
+                               out_changed = true;
                        }
-                       
-                       catch (AudioEngine::PortRegistrationFailure& err) {
-                               setup_peak_meters ();
-                               reset_panner ();
-                               /* pass it on */
-                               throw err;
-                       }
-               
-                       _outputs.push_back (port);
-                       ++_noutputs;
-                       out_changed = true;
                }
                
                if (clear) {
                        
                        /* disconnect all existing ports so that we get a fresh start */
                        
-                       for (vector<Port *>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
+                       for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
                                _session.engine().disconnect (*i);
                        }
                        
-                       for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
+                       for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
                                _session.engine().disconnect (*i);
                        }
                }
@@ -1216,19 +1016,18 @@ IO::ensure_io (uint32_t nin, uint32_t nout, bool clear, void* src)
        }
 
        if (out_changed) {
-               sort (_outputs.begin(), _outputs.end(), sort_ports_by_name);
-               drop_output_connection ();
+               check_bundles_connected_to_outputs ();
                output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
        }
        
        if (in_changed) {
-               sort (_inputs.begin(), _inputs.end(), sort_ports_by_name);
-               drop_input_connection ();
+               check_bundles_connected_to_inputs ();
                input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
        }
 
        if (in_changed || out_changed) {
-               MoreOutputs (max (_noutputs, _ninputs)); /* EMIT SIGNAL */
+               PortCountChanged (max (n_outputs(), n_inputs())); /* EMIT SIGNAL */
+               setup_bundles_for_inputs_and_outputs ();
                _session.set_dirty ();
        }
 
@@ -1236,99 +1035,87 @@ IO::ensure_io (uint32_t nin, uint32_t nout, bool clear, void* src)
 }
 
 int
-IO::ensure_inputs (uint32_t n, bool clear, bool lockit, void* src)
+IO::ensure_inputs (ChanCount count, bool clear, bool lockit, void* src)
 {
        bool changed = false;
 
-       if (_input_maximum >= 0) {
-               n = min (_input_maximum, (int) n);
-               
-               if (n == _ninputs && !clear) {
-                       return 0;
-               }
+       count = min (_input_maximum, count);
+
+       if (count == n_inputs() && !clear) {
+               return 0;
        }
-       
+
        if (lockit) {
-               Glib::Mutex::Lock em (_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
                Glib::Mutex::Lock im (io_lock);
-               changed = ensure_inputs_locked (n, clear, src);
+               changed = ensure_inputs_locked (count, clear, src);
        } else {
-               changed = ensure_inputs_locked (n, clear, src);
+               changed = ensure_inputs_locked (count, clear, src);
        }
 
        if (changed) {
                input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
+               setup_bundles_for_inputs_and_outputs ();
                _session.set_dirty ();
        }
-
        return 0;
 }
 
 bool
-IO::ensure_outputs_locked (uint32_t n, bool clear, void* src)
+IO::ensure_outputs_locked (ChanCount count, bool clear, void* src)
 {
-       Port* output_port;
-       bool changed = false;
-       bool reduced = false;
-       bool need_pan_reset;
+       Port* output_port    = 0;
+       bool  changed        = false;
+       bool  need_pan_reset = false;
 
-       if (_noutputs == n) {
-               need_pan_reset = false;
-       } else {
+       if (n_outputs() != count) {
                need_pan_reset = true;
        }
        
-       /* remove unused ports */
-       
-       while (_noutputs > n) {
-               
-               _session.engine().unregister_port (_outputs.back());
-               _outputs.pop_back();
-               _noutputs--;
-               reduced = true;
-               changed = true;
-       }
-       
-       /* create any necessary new ports */
-       
-       while (_noutputs < n) {
-               
-               char buf[64];
-               
-               /* Create a new output port */
-               
-               if (_output_maximum == 1) {
-                       snprintf (buf, sizeof (buf), _("%s/out"), _name.c_str());
-               } else {
-                       snprintf (buf, sizeof (buf), _("%s/out %u"), _name.c_str(), find_output_port_hole());
-               }
-               
-               if ((output_port = _session.engine().register_output_port (_default_type, buf)) == 0) {
-                       error << string_compose(_("IO: cannot register output port %1"), buf) << endmsg;
-                       return -1;
+       for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
+
+               const size_t n = count.get(*t);
+
+               /* remove unused ports */
+               for (size_t i = n_outputs().get(*t); i > n; --i) {
+                       output_port = _outputs.port(*t, i-1);
+
+                       assert(output_port);
+                       _outputs.remove(output_port);
+                       _session.engine().unregister_port (*output_port);
+
+                       changed = true;
                }
-               
-               _outputs.push_back (output_port);
-               sort (_outputs.begin(), _outputs.end(), sort_ports_by_name);
-               ++_noutputs;
-               changed = true;
-               setup_peak_meters ();
 
-               if (need_pan_reset) {
-                       reset_panner ();
+               /* create any necessary new ports */
+               while (n_outputs().get(*t) < n) {
+
+                       string portname = build_legal_port_name (*t, false);
+
+                       if ((output_port = _session.engine().register_output_port (*t, portname, _public_ports)) == 0) {
+                               error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
+                               return -1;
+                       }
+
+                       _outputs.add (output_port);
+                       changed = true;
+                       setup_peak_meters ();
+
+                       if (need_pan_reset) {
+                               reset_panner ();
+                       }
                }
        }
        
        if (changed) {
-               drop_output_connection ();
-               MoreOutputs (_noutputs); /* EMIT SIGNAL */
+               check_bundles_connected_to_outputs ();
+               PortCountChanged (n_outputs()); /* EMIT SIGNAL */
                _session.set_dirty ();
        }
        
        if (clear) {
                /* disconnect all existing ports so that we get a fresh start */
-               
-               for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
+               for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
                        _session.engine().disconnect (*i);
                }
        }
@@ -1337,13 +1124,13 @@ IO::ensure_outputs_locked (uint32_t n, bool clear, void* src)
 }
 
 int
-IO::ensure_outputs (uint32_t n, bool clear, bool lockit, void* src)
+IO::ensure_outputs (ChanCount count, bool clear, bool lockit, void* src)
 {
        bool changed = false;
 
-       if (_output_maximum >= 0) {
-               n = min (_output_maximum, (int) n);
-               if (n == _noutputs && !clear) {
+       if (_output_maximum < ChanCount::INFINITE) {
+               count = min (_output_maximum, count);
+               if (count == n_outputs() && !clear) {
                        return 0;
                }
        }
@@ -1351,15 +1138,16 @@ IO::ensure_outputs (uint32_t n, bool clear, bool lockit, void* src)
        /* XXX caller should hold io_lock, but generally doesn't */
 
        if (lockit) {
-               Glib::Mutex::Lock em (_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
                Glib::Mutex::Lock im (io_lock);
-               changed = ensure_outputs_locked (n, clear, src);
+               changed = ensure_outputs_locked (count, clear, src);
        } else {
-               changed = ensure_outputs_locked (n, clear, src);
+               changed = ensure_outputs_locked (count, clear, src);
        }
 
        if (changed) {
                 output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
+                setup_bundles_for_inputs_and_outputs ();
        }
 
        return 0;
@@ -1368,8 +1156,8 @@ IO::ensure_outputs (uint32_t n, bool clear, bool lockit, void* src)
 gain_t
 IO::effective_gain () const
 {
-       if (gain_automation_playback()) {
-               return _effective_gain;
+       if (_gain_control->automation_playback()) {
+               return _gain_control->get_value();
        } else {
                return _desired_gain;
        }
@@ -1380,7 +1168,7 @@ IO::reset_panner ()
 {
        if (panners_legal) {
                if (!no_panner_reset) {
-                       _panner->reset (_noutputs, pans_required());
+                       _panner->reset (n_outputs().n_audio(), pans_required());
                }
        } else {
                panner_legal_c.disconnect ();
@@ -1391,7 +1179,7 @@ IO::reset_panner ()
 int
 IO::panners_became_legal ()
 {
-       _panner->reset (_noutputs, pans_required());
+       _panner->reset (n_outputs().n_audio(), pans_required());
        _panner->load (); // automation
        panner_legal_c.disconnect ();
        return 0;
@@ -1423,8 +1211,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);
 
@@ -1432,161 +1220,114 @@ IO::state (bool full_state)
        id().print (buf, sizeof (buf));
        node->add_property("id", buf);
 
-       str = "";
-
-       if (_input_connection) {
-               node->add_property ("input-connection", _input_connection->name());
-               need_ins = false;
+       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_connection) {
-               node->add_property ("output-connection", _output_connection->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 (vector<Port *>::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 (vector<Port *>::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 ());
+       node->add_child_nocopy (_gain_control->get_state ());
 
        snprintf (buf, sizeof(buf), "%2.12f", gain());
        node->add_property ("gain", buf);
 
-       snprintf (buf, sizeof(buf)-1, "%d,%d,%d,%d",
-                 _input_minimum,
-                 _input_maximum,
-                 _output_minimum,
-                 _output_maximum);
+       /* To make backwards compatibility a bit easier, write ChanCount::INFINITE to the session file
+          as -1.
+       */
 
-       node->add_property ("iolimits", buf);
+       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);
 
-       /* automation */
+       snprintf (buf, sizeof(buf)-1, "%d,%d,%d,%d", _input_minimum.get(_default_type), in_max, _output_minimum.get(_default_type), out_max);
 
-       if (full_state) {
-               snprintf (buf, sizeof (buf), "0x%x", (int) _gain_automation_curve.automation_state());
-       } else {
-               /* never store anything except Off for automation state in a template */
-               snprintf (buf, sizeof (buf), "0x%x", ARDOUR::Off); 
-       }
-       node->add_property ("automation-state", buf);
-       snprintf (buf, sizeof (buf), "0x%x", (int) _gain_automation_curve.automation_style());
-       node->add_property ("automation-style", buf);
+       node->add_property ("iolimits", buf);
 
-       /* XXX same for pan etc. */
+       /* automation */
+       
+       if (full_state)
+               node->add_child_nocopy (get_automation_state());
 
        return *node;
 }
 
-int
-IO::connecting_became_legal ()
-{
-       int ret;
-
-       if (pending_state_node == 0) {
-               fatal << _("IO::connecting_became_legal() called without a pending state node") << endmsg;
-               /*NOTREACHED*/
-               return -1;
-       }
-
-       connection_legal_c.disconnect ();
-
-       ret = make_connections (*pending_state_node);
-
-       if (ports_legal) {
-               delete pending_state_node;
-               pending_state_node = 0;
-       }
-
-       return ret;
-}
-
-int
-IO::ports_became_legal ()
-{
-       int ret;
-
-       if (pending_state_node == 0) {
-               fatal << _("IO::ports_became_legal() called without a pending state node") << endmsg;
-               /*NOTREACHED*/
-               return -1;
-       }
-
-       port_legal_c.disconnect ();
-
-       ret = create_ports (*pending_state_node);
-
-       if (connecting_legal) {
-               delete pending_state_node;
-               pending_state_node = 0;
-       }
-
-       return ret;
-}
-
 int
 IO::set_state (const XMLNode& node)
 {
@@ -1605,52 +1346,81 @@ IO::set_state (const XMLNode& node)
 
        if ((prop = node.property ("name")) != 0) {
                _name = prop->value();
-               _panner->set_name (_name);
+               /* used to set panner name with this, but no more */
        } 
 
        if ((prop = node.property ("id")) != 0) {
                _id = prop->value ();
        }
 
+       int in_min = -1;
+       int in_max = -1;
+       int out_min = -1;
+       int out_max = -1;
+
        if ((prop = node.property ("iolimits")) != 0) {
-               sscanf (prop->value().c_str(), "%d,%d,%d,%d", 
-                       &_input_minimum,
-                       &_input_maximum,
-                       &_output_minimum,
-                       &_output_maximum);
-       }
-       
-       if ((prop = node.property ("gain")) != 0) {
-               set_gain (atof (prop->value().c_str()), this);
-               _gain = _desired_gain;
-       }
+               sscanf (prop->value().c_str(), "%d,%d,%d,%d",
+                       &in_min, &in_max, &out_min, &out_max);
 
-       for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
+               /* Correct for the difference between the way we write things to session files and the
+                  way things are described by ChanCount; see comments in io.h about what the different
+                  ChanCount values mean. */
 
-               if ((*iter)->name() == "Panner") {
-                       _panner->set_state (**iter);
+               if (in_min < 0) {
+                       _input_minimum = ChanCount::ZERO;
+               } else {
+                       _input_minimum = ChanCount (_default_type, in_min);
                }
 
-               if ((*iter)->name() == X_("gaincontrol")) {
-                       _gain_control.set_state (**iter);
-                       _session.add_controllable (&_gain_control);
+               if (in_max < 0) {
+                       _input_maximum = ChanCount::INFINITE;
+               } else {
+                       _input_maximum = ChanCount (_default_type, in_max);
                }
-       }
 
-       if ((prop = node.property ("automation-state")) != 0) {
+               if (out_min < 0) {
+                       _output_minimum = ChanCount::ZERO;
+               } else {
+                       _output_minimum = ChanCount (_default_type, out_min);
+               }
+               
+               if (out_max < 0) {
+                       _output_maximum = ChanCount::INFINITE;
+               } else {
+                       _output_maximum = ChanCount (_default_type, out_max);
+               }
+       }
+       
+       if ((prop = node.property ("gain")) != 0) {
+               set_gain (atof (prop->value().c_str()), this);
+               _gain = _desired_gain;
+       }
 
-               long int x;
-               x = strtol (prop->value().c_str(), 0, 16);
-               set_gain_automation_state (AutoState (x));
+       if ((prop = node.property ("automation-state")) != 0 || (prop = node.property ("automation-style")) != 0) {
+               /* old school automation handling */
        }
 
-       if ((prop = node.property ("automation-style")) != 0) {
+       for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
+
+               if ((*iter)->name() == "Panner") {
+                       if (_panner == 0) {
+                               _panner = new Panner (_name, _session);
+                       }
+                       _panner->set_state (**iter);
+               }
+
+               if ((*iter)->name() == X_("Automation")) {
 
-              long int x;
-               x = strtol (prop->value().c_str(), 0, 16);
-               set_gain_automation_style (AutoStyle (x));
+                       set_automation_state (*(*iter), Evoral::Parameter(GainAutomation));
+               }
+
+               if ((*iter)->name() == X_("controllable")) {
+                       if ((prop = (*iter)->property("name")) != 0 && prop->value() == "gaincontrol") {
+                               _gain_control->set_state (**iter);
+                       }
+               }
        }
-       
+
        if (ports_legal) {
 
                if (create_ports (node)) {
@@ -1687,56 +1457,251 @@ IO::set_state (const XMLNode& node)
 }
 
 int
-IO::create_ports (const XMLNode& node)
+IO::load_automation (string path)
 {
-       const XMLProperty* prop;
-       int num_inputs = 0;
-       int num_outputs = 0;
+       string fullpath;
+       ifstream in;
+       char line[128];
+       uint32_t linecnt = 0;
+       float version;
+       LocaleGuard lg (X_("POSIX"));
 
-       if ((prop = node.property ("input-connection")) != 0) {
+       fullpath = Glib::build_filename(_session.automation_dir(), path);
 
-               Connection* c = _session.connection_by_name (prop->value());
-               
-               if (c == 0) {
-                       error << string_compose(_("Unknown connection \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
+       in.open (fullpath.c_str());
+
+       if (!in) {
+               fullpath = Glib::build_filename(_session.automation_dir(), _session.snap_name() + '-' + path);
+
+               in.open (fullpath.c_str());
+
+               if (!in) {
+                       error << string_compose(_("%1: cannot open automation event file \"%2\""), _name, fullpath) << endmsg;
+                       return -1;
+               }
+       }
+
+       clear_automation ();
+
+       while (in.getline (line, sizeof(line), '\n')) {
+               char type;
+               nframes_t when;
+               double value;
 
-                       if ((c = _session.connection_by_name (_("in 1"))) == 0) {
-                               error << _("No input connections available as a replacement")
-                                     << endmsg;
+               if (++linecnt == 1) {
+                       if (memcmp (line, "version", 7) == 0) {
+                               if (sscanf (line, "version %f", &version) != 1) {
+                                       error << string_compose(_("badly formed version number in automation event file \"%1\""), path) << endmsg;
+                                       return -1;
+                               }
+                       } else {
+                               error << string_compose(_("no version information in automation event file \"%1\""), path) << endmsg;
                                return -1;
-                       }  else {
-                               info << string_compose (_("Connection %1 was not available - \"in 1\" used instead"), prop->value())
-                                    << endmsg;
                        }
+
+                       continue;
+               }
+
+               if (sscanf (line, "%c %" PRIu32 " %lf", &type, &when, &value) != 3) {
+                       warning << string_compose(_("badly formatted automation event record at line %1 of %2 (ignored)"), linecnt, path) << endmsg;
+                       continue;
+               }
+
+               switch (type) {
+               case 'g':
+                       _gain_control->list()->fast_simple_add (when, value);
+                       break;
+
+               case 's':
+                       break;
+
+               case 'm':
+                       break;
+
+               case 'p':
+                       /* older (pre-1.0) versions of ardour used this */
+                       break;
+
+               default:
+                       warning << _("dubious automation event found (and ignored)") << endmsg;
+               }
+       }
+
+       return 0;
+}
+
+int
+IO::connecting_became_legal ()
+{
+       int ret;
+
+       if (pending_state_node == 0) {
+               fatal << _("IO::connecting_became_legal() called without a pending state node") << endmsg;
+               /*NOTREACHED*/
+               return -1;
+       }
+
+       connection_legal_c.disconnect ();
+
+       ret = make_connections (*pending_state_node);
+
+       if (ports_legal) {
+               delete pending_state_node;
+               pending_state_node = 0;
+       }
+
+       return ret;
+}
+int
+IO::ports_became_legal ()
+{
+       int ret;
+
+       if (pending_state_node == 0) {
+               fatal << _("IO::ports_became_legal() called without a pending state node") << endmsg;
+               /*NOTREACHED*/
+               return -1;
+       }
+
+       port_legal_c.disconnect ();
+
+       ret = create_ports (*pending_state_node);
+
+       if (connecting_legal) {
+               delete pending_state_node;
+               pending_state_node = 0;
+       }
+
+       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)
+{
+       XMLProperty const * prop;
+       ChanCount num_inputs;
+       ChanCount num_outputs;
+
+       if ((prop = node.property ("input-connection")) != 0) {
+
+               boost::shared_ptr<Bundle> c = find_possible_bundle (prop->value(), _("in"), _("input"));
+               
+               if (!c) {
+                       return -1;
                } 
 
-               num_inputs = c->nports();
+               num_inputs = c->nchannels();
 
        } else if ((prop = node.property ("inputs")) != 0) {
 
-               num_inputs = count (prop->value().begin(), prop->value().end(), '{');
+               num_inputs.set (default_type(), count (prop->value().begin(), prop->value().end(), '{'));
        }
        
        if ((prop = node.property ("output-connection")) != 0) {
-               Connection* c = _session.connection_by_name (prop->value());
 
-               if (c == 0) {
-                       error << string_compose(_("Unknown connection \"%1\" listed for output of %2"), prop->value(), _name) << endmsg;
+               boost::shared_ptr<Bundle> c = find_possible_bundle(prop->value(), _("out"), _("output"));
 
-                       if ((c = _session.connection_by_name (_("out 1"))) == 0) {
-                               error << _("No output connections available as a replacement")
-                                     << endmsg;
-                               return -1;
-                       }  else {
-                               info << string_compose (_("Connection %1 was not available - \"out 1\" used instead"), prop->value())
-                                    << endmsg;
-                       }
+               if (!c) {
+                       return -1;
                } 
 
-               num_outputs = c->nports ();
+               num_outputs = c->nchannels ();
                
        } else if ((prop = node.property ("outputs")) != 0) {
-               num_outputs = count (prop->value().begin(), prop->value().end(), '{');
+
+               num_outputs.set (default_type(), count (prop->value().begin(), prop->value().end(), '{'));
        }
 
        no_panner_reset = true;
@@ -1758,25 +1723,17 @@ IO::create_ports (const XMLNode& node)
 int
 IO::make_connections (const XMLNode& node)
 {
+
        const XMLProperty* prop;
 
        if ((prop = node.property ("input-connection")) != 0) {
-               Connection* c = _session.connection_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.connection_by_name (_("in 1"))) == 0) {
-                               error << _("No input connections available as a replacement")
-                                     << endmsg;
-                               return -1;
-                       } else {
-                               info << string_compose (_("Connection %1 was not available - \"in 1\" used instead"), prop->value())
-                                    << endmsg;
-                       }
+               if (!c) {
+                       return -1;
                } 
 
-               use_input_connection (*c, this);
+               connect_input_ports_to_bundle (c, this);
 
        } else if ((prop = node.property ("inputs")) != 0) {
                if (set_inputs (prop->value())) {
@@ -1784,24 +1741,15 @@ IO::make_connections (const XMLNode& node)
                        return -1;
                }
        }
-       
+
        if ((prop = node.property ("output-connection")) != 0) {
-               Connection* c = _session.connection_by_name (prop->value());
+               boost::shared_ptr<Bundle> c = find_possible_bundle (prop->value(), _("out"), _("output"));
                
-               if (c == 0) {
-                       error << string_compose(_("Unknown connection \"%1\" listed for output of %2"), prop->value(), _name) << endmsg;
-
-                       if ((c = _session.connection_by_name (_("out 1"))) == 0) {
-                               error << _("No output connections available as a replacement")
-                                     << endmsg;
-                               return -1;
-                       }  else {
-                               info << string_compose (_("Connection %1 was not available - \"out 1\" used instead"), prop->value())
-                                    << endmsg;
-                       }
+               if (!c) {
+                       return -1;
                } 
-
-               use_output_connection (*c, this);
+               
+               connect_output_ports_to_bundle (c, this);
                
        } else if ((prop = node.property ("outputs")) != 0) {
                if (set_outputs (prop->value())) {
@@ -1809,6 +1757,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;
 }
@@ -1825,7 +1795,8 @@ IO::set_inputs (const string& str)
                return 0;
        }
 
-       if (ensure_inputs (nports, true, true, this)) {
+       // FIXME: audio-only
+       if (ensure_inputs (ChanCount(DataType::AUDIO, nports), true, true, this)) {
                return -1;
        }
 
@@ -1875,7 +1846,8 @@ IO::set_outputs (const string& str)
                return 0;
        }
 
-       if (ensure_outputs (nports, true, true, this)) {
+       // FIXME: audio-only
+       if (ensure_outputs (ChanCount(DataType::AUDIO, nports), true, true, this)) {
                return -1;
        }
 
@@ -1960,51 +1932,67 @@ IO::parse_gain_string (const string& str, vector<string>& ports)
        return ports.size();
 }
 
-int
-IO::set_name (string name, void* src)
+bool
+IO::set_name (const string& requested_name)
 {
-       if (name == _name) {
-               return 0;
+       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;
+       }
+
 
-       for (vector<Port *>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
-               string current_name = (*i)->short_name();
+       /* replace all colons in the name. i wish we didn't have to do this */
+
+       if (replace_all (name, ":", "-")) {
+               warning << _("you cannot use colons to name objects with I/O connections") << endmsg;
+       }
+
+       for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
+               string current_name = i->short_name();
                current_name.replace (current_name.find (_name), _name.length(), name);
-               (*i)->set_name (current_name);
+               i->set_name (current_name);
        }
 
-       for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
-               string current_name = (*i)->short_name();
+       for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
+               string current_name = i->short_name();
                current_name.replace (current_name.find (_name), _name.length(), name);
-               (*i)->set_name (current_name);
+               i->set_name (current_name);
        }
 
-       _name = name;
-        name_changed (src); /* EMIT SIGNAL */
+       bool const r = SessionObject::set_name(name);
+
+       setup_bundles_for_inputs_and_outputs ();
 
-        return 0;
+       return r;
 }
 
 void
-IO::set_input_minimum (int n)
+IO::set_input_minimum (ChanCount n)
 {
        _input_minimum = n;
 }
 
 void
-IO::set_input_maximum (int n)
+IO::set_input_maximum (ChanCount n)
 {
        _input_maximum = n;
 }
 
 void
-IO::set_output_minimum (int n)
+IO::set_output_minimum (ChanCount n)
 {
        _output_minimum = n;
 }
 
 void
-IO::set_output_maximum (int n)
+IO::set_output_maximum (ChanCount n)
 {
        _output_maximum = n;
 }
@@ -2014,8 +2002,8 @@ IO::set_port_latency (nframes_t nframes)
 {
        Glib::Mutex::Lock lm (io_lock);
 
-       for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
-               (*i)->set_latency (nframes);
+       for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
+               i->set_latency (nframes);
        }
 }
 
@@ -2029,8 +2017,8 @@ IO::output_latency () const
 
        /* io lock not taken - must be protected by other means */
 
-       for (vector<Port *>::const_iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
-               if ((latency = _session.engine().get_port_total_latency (*(*i))) > max_latency) {
+       for (PortSet::const_iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
+               if ((latency = _session.engine().get_port_total_latency (*i)) > max_latency) {
                        max_latency = latency;
                }
        }
@@ -2048,87 +2036,59 @@ IO::input_latency () const
 
        /* io lock not taken - must be protected by other means */
 
-       for (vector<Port *>::const_iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
-               if ((latency = _session.engine().get_port_total_latency (*(*i))) > max_latency) {
+       for (PortSet::const_iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
+               if ((latency = _session.engine().get_port_total_latency (*i)) > max_latency) {
                        max_latency = latency;
-               }
+               } 
        }
 
        return max_latency;
 }
 
 int
-IO::use_input_connection (Connection& c, void* src)
+IO::connect_input_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
 {
-       uint32_t limit;
-
        {
-               Glib::Mutex::Lock lm (_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
                Glib::Mutex::Lock lm2 (io_lock);
-               
-               limit = c.nports();
-               
-               drop_input_connection ();
-               
-               if (ensure_inputs (limit, false, false, src)) {
-                       return -1;
-               }
 
-               /* first pass: check the current state to see what's correctly
-                  connected, and drop anything that we don't want.
-               */
-               
-               for (uint32_t n = 0; n < limit; ++n) {
-                       const Connection::PortList& pl = c.port_connections (n);
-                       
-                       for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
-                               
-                               if (!_inputs[n]->connected_to ((*i))) {
-                                       
-                                       /* clear any existing connections */
-                                       
-                                       _session.engine().disconnect (_inputs[n]);
-                                       
-                               } else if (_inputs[n]->connected() > 1) {
-                                       
-                                       /* OK, it is connected to the port we want,
-                                          but its also connected to other ports.
-                                          Change that situation.
-                                       */
-                                       
-                                       /* XXX could be optimized to not drop
-                                          the one we want.
-                                       */
-                                       
-                                       _session.engine().disconnect (_inputs[n]);
-                                       
-                               }
-                       }
-               }
-               
-               /* second pass: connect all requested ports where necessary */
-               
-               for (uint32_t n = 0; n < limit; ++n) {
-                       const Connection::PortList& pl = c.port_connections (n);
-                       
-                       for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
-                               
-                               if (!_inputs[n]->connected_to ((*i))) {
+               /* 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[n]->name())) {
+                                       if (_session.engine().connect (*i, _inputs.port(n)->name())) {
                                                return -1;
                                        }
                                }
                                
                        }
                }
-               
-               _input_connection = &c;
-               
-               input_connection_configuration_connection = c.ConfigurationChanged.connect
-                       (mem_fun (*this, &IO::input_connection_configuration_changed));
-               input_connection_connection_connection = c.ConnectionsChanged.connect
-                       (mem_fun (*this, &IO::input_connection_connection_changed));
+
+               /* 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;
+                       }
+
+                       if (i == _bundles_connected_to_inputs.end()) {
+                               /* We don't, so make a note */
+                               _bundles_connected_to_inputs.push_back (UserBundleInfo (this, ub));
+                       }
+               }
        }
 
        input_changed (IOChange (ConfigurationChanged|ConnectionsChanged), src); /* EMIT SIGNAL */
@@ -2136,77 +2096,49 @@ IO::use_input_connection (Connection& c, void* src)
 }
 
 int
-IO::use_output_connection (Connection& c, void* src)
+IO::connect_output_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
 {
-       uint32_t limit; 
-
        {
-               Glib::Mutex::Lock lm (_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
                Glib::Mutex::Lock lm2 (io_lock);
 
-               limit = c.nports();
-                       
-               drop_output_connection ();
+               /* Connect to the bundle, not worrying about any connections
+                  that are already made. */
 
-               if (ensure_outputs (limit, false, false, src)) {
-                       return -1;
-               }
+               ChanCount const channels = c->nchannels ();
+               uint32_t cnt = channels.get (default_type());
 
-               /* first pass: check the current state to see what's correctly
-                  connected, and drop anything that we don't want.
-               */
-                       
-               for (uint32_t n = 0; n < limit; ++n) {
-
-                       const Connection::PortList& pl = c.port_connections (n);
-                               
-                       for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
-                                       
-                               if (!_outputs[n]->connected_to ((*i))) {
+               for (uint32_t n = 0; n < cnt; ++n) {
 
-                                       /* clear any existing connections */
+                       const PortList& pl = c->channel_ports (n);
 
-                                       _session.engine().disconnect (_outputs[n]);
+                       for (PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
 
-                               } else if (_outputs[n]->connected() > 1) {
-
-                                       /* OK, it is connected to the port we want,
-                                          but its also connected to other ports.
-                                          Change that situation.
-                                       */
-
-                                       /* XXX could be optimized to not drop
-                                          the one we want.
-                                       */
+                               if (!_outputs.port(n)->connected_to (*i)) {
                                                
-                                       _session.engine().disconnect (_outputs[n]);
+                                       if (_session.engine().connect (_outputs.port(n)->name(), *i)) {
+                                               return -1;
+                                       }
                                }
                        }
                }
 
-               /* second pass: connect all requested ports where necessary */
+               /* If this is a UserBundle, make a note of what we've done */
 
-               for (uint32_t n = 0; n < limit; ++n) {
+               boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
+               if (ub) {
 
-                       const Connection::PortList& pl = c.port_connections (n);
-                               
-                       for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
-                                       
-                               if (!_outputs[n]->connected_to ((*i))) {
-                                               
-                                       if (_session.engine().connect (_outputs[n]->name(), *i)) {
-                                               return -1;
-                                       }
-                               }
+                       /* 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;
                        }
-               }
-
-               _output_connection = &c;
 
-               output_connection_configuration_connection = c.ConfigurationChanged.connect
-                       (mem_fun (*this, &IO::output_connection_configuration_changed));
-               output_connection_connection_connection = c.ConnectionsChanged.connect
-                       (mem_fun (*this, &IO::output_connection_connection_changed));
+                       if (i == _bundles_connected_to_outputs.end()) {
+                               /* We don't, so make a note */
+                               _bundles_connected_to_outputs.push_back (UserBundleInfo (this, ub));
+                       }
+               }
        }
 
        output_changed (IOChange (ConnectionsChanged|ConfigurationChanged), src); /* EMIT SIGNAL */
@@ -2257,60 +2189,57 @@ IO::reset_panners ()
 }
 
 void
-IO::input_connection_connection_changed (int ignored)
+IO::bundle_configuration_will_change ()
 {
-       use_input_connection (*_input_connection, this);
+       //XXX
+//     connect_input_ports_to_bundle (_input_bundle, this);
 }
 
 void
-IO::input_connection_configuration_changed ()
+IO::bundle_configuration_has_changed ()
 {
-       use_input_connection (*_input_connection, this);
+       //XXX
+//     connect_input_ports_to_bundle (_input_bundle, this);
 }
 
 void
-IO::output_connection_connection_changed (int ignored)
+IO::bundle_ports_will_change (int ignored)
 {
-       use_output_connection (*_output_connection, this);
+//XXX
+//     connect_output_ports_to_bundle (_output_bundle, this);
 }
 
 void
-IO::output_connection_configuration_changed ()
+IO::bundle_ports_have_changed (int ignored)
 {
-       use_output_connection (*_output_connection, this);
+       //XXX
+//     connect_output_ports_to_bundle (_output_bundle, this);
 }
 
 void
-IO::GainControllable::set_value (float val)
+IO::GainControl::set_value (float val)
 {
-       io.set_gain (direct_control_to_gain (val), this);
-}
+       // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
+       if (val > 1.99526231f)
+               val = 1.99526231f;
 
-float
-IO::GainControllable::get_value (void) const
-{
-       return direct_gain_to_control (io.effective_gain());
+       _user_value = val;
+       _io.set_gain (val, this);
+       
+       Changed(); /* EMIT SIGNAL */
 }
 
-void
-IO::reset_peak_meters ()
+float
+IO::GainControl::get_value (void) const
 {
-       uint32_t limit = max (_ninputs, _noutputs);
-
-       for (uint32_t i = 0; i < limit; ++i) {
-               _peak_power[i] = 0;
-       }
+       return AutomationControl::get_value();
 }
 
 void
-IO::setup_peak_meters ()
+IO::setup_peak_meters()
 {
-       uint32_t limit = max (_ninputs, _noutputs);
-
-       while (_peak_power.size() < limit) {
-               _peak_power.push_back (0);
-               _visible_peak_power.push_back (0);
-       }
+       ChanCount max_streams = std::max (_inputs.count(), _outputs.count());
+       _meter->configure_io (max_streams, max_streams);
 }
 
 /**
@@ -2324,204 +2253,62 @@ IO::setup_peak_meters ()
 void
 IO::update_meters()
 {
-    Glib::Mutex::Lock guard (m_meter_signal_lock);
-    
-    Meter();
+       Glib::Mutex::Lock guard (m_meter_signal_lock);
+       Meter(); /* EMIT SIGNAL */
 }
 
 void
 IO::meter ()
 {
-       Glib::Mutex::Lock lm (io_lock); // READER: meter thread.
-       uint32_t limit = max (_ninputs, _noutputs);
+       // FIXME: Ugly.  Meter should manage the lock, if it's necessary
        
-       for (uint32_t n = 0; n < limit; ++n) {
-
-               /* XXX we should use atomic exchange here */
-
-               /* grab peak since last read */
-
-               float new_peak = _peak_power[n];
-               _peak_power[n] = 0;
-               
-               /* compute new visible value using falloff */
-
-               if (new_peak > 0.0) {
-                       new_peak = coefficient_to_dB (new_peak);
-               } else {
-                       new_peak = minus_infinity();
-               }
-               
-               if (Config->get_meter_falloff() == 0.0f || new_peak > _visible_peak_power[n]) {
-                       _visible_peak_power[n] = new_peak;
-               } else {
-                       // do falloff
-                       new_peak = _visible_peak_power[n] - Config->get_meter_falloff();
-                       _visible_peak_power[n] = max (new_peak, -INFINITY);
-               }
-       }
+       Glib::Mutex::Lock lm (io_lock); // READER: meter thread.
+       _meter->meter();
 }
 
-int
-IO::save_automation (const string& path)
+void
+IO::clear_automation ()
 {
-       string fullpath;
-       ofstream out;
-
-       fullpath = _session.automation_dir();
-       fullpath += path;
-
-       out.open (fullpath.c_str());
-
-       if (!out) {
-               error << string_compose(_("%1: could not open automation event file \"%2\""), _name, fullpath) << endmsg;
-               return -1;
-       }
-
-       out << X_("version ") << current_automation_version_number << endl;
-
-       /* XXX use apply_to_points to get thread safety */
-       
-       for (AutomationList::iterator i = _gain_automation_curve.begin(); i != _gain_automation_curve.end(); ++i) {
-               out << "g " << (nframes_t) floor ((*i)->when) << ' ' << (*i)->value << endl;
-       }
-
-       _panner->save ();
-
-       return 0;
+       data().clear (); // clears gain automation
+       _panner->clear_automation ();
 }
 
-int
-IO::load_automation (const string& path)
+void
+IO::set_parameter_automation_state (Evoral::Parameter param, AutoState state)
 {
-       string fullpath;
-       ifstream in;
-       char line[128];
-       uint32_t linecnt = 0;
-       float version;
-       LocaleGuard lg (X_("POSIX"));
+       // XXX: would be nice to get rid of this special hack
 
-       fullpath = _session.automation_dir();
-       fullpath += path;
+       if (param.type() == GainAutomation) {
 
-       in.open (fullpath.c_str());
+               bool changed = false;
 
-       if (!in) {
-               fullpath = _session.automation_dir();
-               fullpath += _session.snap_name();
-               fullpath += '-';
-               fullpath += path;
-               in.open (fullpath.c_str());
-               if (!in) {
-                       error << string_compose(_("%1: cannot open automation event file \"%2\" (%2)"), _name, fullpath, strerror (errno)) << endmsg;
-                       return -1;
-               }
-       }
+               { 
+                       Glib::Mutex::Lock lm (control_lock());
 
-       clear_automation ();
+                       boost::shared_ptr<AutomationList> gain_auto
+                               = boost::dynamic_pointer_cast<AutomationList>(_gain_control->list());
 
-       while (in.getline (line, sizeof(line), '\n')) {
-               char type;
-               nframes_t when;
-               double value;
+                       if (state != gain_auto->automation_state()) {
+                               changed = true;
+                               _last_automation_snapshot = 0;
+                               gain_auto->set_automation_state (state);
 
-               if (++linecnt == 1) {
-                       if (memcmp (line, "version", 7) == 0) {
-                               if (sscanf (line, "version %f", &version) != 1) {
-                                       error << string_compose(_("badly formed version number in automation event file \"%1\""), path) << endmsg;
-                                       return -1;
+                               if (state != Off) {
+                                       // FIXME: shouldn't this use Curve?
+                                       set_gain (gain_auto->eval (_session.transport_frame()), this);
                                }
-                       } else {
-                               error << string_compose(_("no version information in automation event file \"%1\""), path) << endmsg;
-                               return -1;
                        }
-
-                       if (version != current_automation_version_number) {
-                               error << string_compose(_("mismatched automation event file version (%1)"), version) << endmsg;
-                               return -1;
-                       }
-
-                       continue;
-               }
-
-               if (sscanf (line, "%c %" PRIu32 " %lf", &type, &when, &value) != 3) {
-                       warning << string_compose(_("badly formatted automation event record at line %1 of %2 (ignored)"), linecnt, path) << endmsg;
-                       continue;
-               }
-
-               switch (type) {
-               case 'g':
-                       _gain_automation_curve.add (when, value, true);
-                       break;
-
-               case 's':
-                       break;
-
-               case 'm':
-                       break;
-
-               case 'p':
-                       /* older (pre-1.0) versions of ardour used this */
-                       break;
-
-               default:
-                       warning << _("dubious automation event found (and ignored)") << endmsg;
                }
-       }
 
-       return 0;
-}
-       
-void
-IO::clear_automation ()
-{
-       Glib::Mutex::Lock lm (automation_lock);
-       _gain_automation_curve.clear ();
-       _panner->clear_automation ();
-}
-
-void
-IO::set_gain_automation_state (AutoState state)
-{
-       bool changed = false;
-
-       {
-               Glib::Mutex::Lock lm (automation_lock);
-
-               if (state != _gain_automation_curve.automation_state()) {
-                       changed = true;
-                       _gain_automation_curve.set_automation_state (state);
-                       
-                       if (state != Off) {
-                               set_gain (_gain_automation_curve.eval (_session.transport_frame()), this);
-                       }
+               if (changed) {
+                       _session.set_dirty ();
                }
-       }
 
-       if (changed) {
-               _session.set_dirty ();
-               gain_automation_state_changed (); /* EMIT SIGNAL */
+       } else {
+               AutomatableControls::set_parameter_automation_state(param, state);
        }
 }
 
-void
-IO::set_gain_automation_style (AutoStyle style)
-{
-       bool changed = false;
-
-       {
-               Glib::Mutex::Lock lm (automation_lock);
-
-               if (style != _gain_automation_curve.automation_style()) {
-                       changed = true;
-                       _gain_automation_curve.set_automation_style (style);
-               }
-       }
-
-       if (changed) {
-               gain_automation_style_changed (); /* EMIT SIGNAL */
-       }
-}
 void
 IO::inc_gain (gain_t factor, void *src)
 {
@@ -2535,7 +2322,15 @@ void
 IO::set_gain (gain_t val, void *src)
 {
        // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
-       if (val>1.99526231f) val=1.99526231f;
+       if (val > 1.99526231f)
+               val = 1.99526231f;
+
+       if (src != _gain_control.get()) {
+               _gain_control->set_value(val);
+               // bit twisty, this will come back and call us again
+               // (this keeps control in sync with reality)
+               return;
+       }
 
        {
                Glib::Mutex::Lock dm (declick_lock);
@@ -2543,38 +2338,22 @@ IO::set_gain (gain_t val, void *src)
        }
 
        if (_session.transport_stopped()) {
-               _effective_gain = val;
                _gain = val;
        }
-
-       gain_changed (src);
-       _gain_control.Changed (); /* EMIT SIGNAL */
        
-       if (_session.transport_stopped() && src != 0 && src != this && gain_automation_recording()) {
-               _gain_automation_curve.add (_session.transport_frame(), val);
+       if (_session.transport_stopped() && src != 0 && src != this && _gain_control->automation_write()) {
+               _gain_control->list()->add (_session.transport_frame(), val);
                
        }
 
        _session.set_dirty();
 }
 
-void
-IO::start_gain_touch ()
-{
-       _gain_automation_curve.start_touch ();
-}
-
-void
-IO::end_gain_touch ()
-{
-       _gain_automation_curve.stop_touch ();
-}
-
 void
 IO::start_pan_touch (uint32_t which)
 {
        if (which < _panner->size()) {
-               (*_panner)[which]->automation().start_touch();
+               (*_panner)[which]->pan_control()->start_touch();
        }
 }
 
@@ -2582,30 +2361,98 @@ void
 IO::end_pan_touch (uint32_t which)
 {
        if (which < _panner->size()) {
-               (*_panner)[which]->automation().stop_touch();
+               (*_panner)[which]->pan_control()->stop_touch();
        }
 
 }
 
+void
+IO::automation_snapshot (nframes_t now, bool force)
+{
+       AutomatableControls::automation_snapshot (now, force);
+
+       if (_last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) {
+               _panner->snapshot (now);
+       }
+       
+       _panner->snapshot (now);
+       _last_automation_snapshot = now;
+}
+
 void
 IO::transport_stopped (nframes_t frame)
 {
-       _gain_automation_curve.reposition_for_rt_add (frame);
+       _gain_control->list()->reposition_for_rt_add (frame);
 
-       if (_gain_automation_curve.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.
                */
 
-               set_gain (_gain_automation_curve.eval (frame), 0);
+               // FIXME: shouldn't this use Curve?
+               set_gain (_gain_control->list()->eval (frame), 0);
        }
 
        _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 */
 
@@ -2615,14 +2462,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()];
-               vector<Port*>::iterator i;
+               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.begin(); i != _inputs.end(); ++i) {
-                       if ((*i)->short_name() == buf) {
+               for ( ; i != _inputs.end(); ++i) {
+                       if (i->short_name() == buf) {
                                break;
                        }
                }
@@ -2635,7 +2485,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 */
 
@@ -2645,14 +2495,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()];
-               vector<Port*>::iterator i;
+               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.begin(); i != _outputs.end(); ++i) {
-                       if ((*i)->short_name() == buf) {
+               for ( ; i != _outputs.end(); ++i) {
+                       if (i->short_name() == buf) {
                                break;
                        }
                }
@@ -2664,3 +2517,240 @@ 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
+{
+       return dynamic_cast<AudioPort*>(input(n));
+}
+
+AudioPort*
+IO::audio_output(uint32_t n) const
+{
+       return dynamic_cast<AudioPort*>(output(n));
+}
+
+MidiPort*
+IO::midi_input(uint32_t n) const
+{
+       return dynamic_cast<MidiPort*>(input(n));
+}
+
+MidiPort*
+IO::midi_output(uint32_t n) const
+{
+       return dynamic_cast<MidiPort*>(output(n));
+}
+
+void
+IO::set_phase_invert (bool yn, void *src)
+{
+       if (_phase_invert != yn) {
+               _phase_invert = yn;
+               //  phase_invert_changed (src); /* EMIT SIGNAL */
+       }
+}
+
+void
+IO::set_denormal_protection (bool yn, void *src)
+{
+       if (_denormal_protection != yn) {
+               _denormal_protection = yn;
+               //  denormal_protection_changed (src); /* EMIT SIGNAL */
+       }
+}
+
+void
+IO::update_port_total_latencies ()
+{
+       /* io_lock, not taken: function must be called from Session::process() calltree */
+
+       for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
+               _session.engine().update_total_latency (*i);
+       }
+
+       for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
+               _session.engine().update_total_latency (*i);
+       }
+}
+
+
+/**
+ *  Setup bundles that describe our inputs and outputs.
+ */
+
+void
+IO::setup_bundles_for_inputs_and_outputs ()
+{
+        char buf[32];
+
+        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());
+       }
+
+        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());
+       }
+}
+
+
+/**
+ *  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)
+{
+       boost::shared_ptr<AutoBundle> ab = boost::dynamic_pointer_cast<AutoBundle, Bundle> (b);
+
+       if (ab == 0 || ab->ports_are_outputs() == false) {
+               return;
+       }
+       
+       if (ab->nchannels().get (default_type()) != n_inputs().n_total ()) {
+               return;
+       }
+
+       for (uint32_t i = 0; i < n_inputs().n_total (); ++i) {
+
+               PortList const & pl = b->channel_ports (i);
+
+               if (pl.empty()) {
+                       return;
+               }
+
+               if (!input(i)->connected_to (pl[0])) {
+                       return;
+               }
+       }
+
+       bundles->push_back (b);
+}
+
+/** @return Bundles connected to our inputs */
+std::vector<boost::shared_ptr<Bundle> >
+IO::bundles_connected_to_inputs ()
+{
+       std::vector<boost::shared_ptr<Bundle> > 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;
+               }
+
+               if (!output(i)->connected_to (pl[0])) {
+                       return;
+               }
+       }
+
+       bundles->push_back (b);
+}
+
+
+/* @return Bundles connected to our outputs */
+std::vector<boost::shared_ptr<Bundle> >
+IO::bundles_connected_to_outputs ()
+{
+       std::vector<boost::shared_ptr<Bundle> > 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)
+               );
+
+       return bundles; 
+}
+
+
+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)
+               );
+}
+
+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);
+       }
+}