Various work on Bundles, especially dynamic ones so that you can, for example, pass...
[ardour.git] / libs / ardour / io.cc
index 7cc507f2371163d9a50da9c22740dde860271f53..0c1ba32c74ab39a057fe0a97ac66d16e111ad7d5 100644 (file)
 #include <algorithm>
 #include <unistd.h>
 #include <locale.h>
+#include <errno.h>
 
 #include <sigc++/bind.h>
 
 #include <glibmm/thread.h>
 
 #include <pbd/xml++.h>
+#include <pbd/replace_all.h>
 
 #include <ardour/audioengine.h>
 #include <ardour/io.h>
 #include <ardour/port.h>
 #include <ardour/audio_port.h>
 #include <ardour/midi_port.h>
-#include <ardour/connection.h>
+#include <ardour/bundle.h>
 #include <ardour/session.h>
 #include <ardour/cycle_timer.h>
 #include <ardour/panner.h>
 #include <ardour/buffer_set.h>
 #include <ardour/meter.h>
+#include <ardour/amp.h>
 
 #include "i18n.h"
 
@@ -53,15 +56,12 @@ 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;
 
-
-static float current_automation_version_number = 1.0;
-
-jack_nframes_t               IO::_automation_interval = 0;
 const string                 IO::state_node_name = "IO";
 bool                         IO::connecting_legal = false;
 bool                         IO::ports_legal = false;
@@ -79,6 +79,7 @@ Glib::StaticMutex IO::m_meter_signal_lock = GLIBMM_STATIC_MUTEX_INIT;
    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 */
@@ -91,57 +92,114 @@ static double direct_gain_to_control (gain_t gain) {
        
        return pow((6.0*log(gain)/log(2.0)+192.0)/198.0, 8.0);
 }
-
+#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),
-      _output_buffers(new BufferSet()),
-         _name (name),
-         _default_type(default_type),
-         _gain_control (*this),
-         _gain_automation_curve (0.0, 2.0, 1.0),
-         _input_minimum (_default_type, input_min),
-         _input_maximum (_default_type, input_max),
-         _output_minimum (_default_type, output_min),
-         _output_maximum (_default_type, output_max)
+       : Automatable (s, name),
+      _output_buffers (new BufferSet()),
+         _default_type (default_type),
+         _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;
        no_panner_reset = false;
+       _phase_invert = false;
        deferred_state = 0;
 
+       boost::shared_ptr<AutomationList> gl(
+                       new AutomationList(Parameter(GainAutomation), 0.0, 2.0, 1.0));
+
+       _gain_control = boost::shared_ptr<GainControl>(
+                       new GainControl(X_("gaincontrol"), *this, gl));
+
+       add_control(_gain_control);
+
        apply_gain_automation = false;
+       
+       {
+               // IO::Meter is emitted from another thread so the
+               // Meter signal must be protected.
+               Glib::Mutex::Lock guard (m_meter_signal_lock);
+               m_meter_connection = Meter.connect (mem_fun (*this, &IO::meter));
+       }
+       
+       // Connect to our own MoreChannels signal to connect output buffers
+       IO::MoreChannels.connect (mem_fun (*this, &IO::attach_buffers));
 
-       last_automation_snapshot = 0;
+       _session.add_controllable (_gain_control);
 
-       _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));
-    }
+       create_bundles ();
+}
+
+IO::IO (Session& s, const XMLNode& node, DataType dt)
+       : Automatable (s, "unnamed io"),
+      _output_buffers (new BufferSet()),
+         _default_type (dt)
+{
+       _meter = new PeakMeter (_session);
+
+       _panner = 0;
+       deferred_state = 0;
+       no_panner_reset = false;
+       _desired_gain = 1.0;
+       _gain = 1.0;
+
+       apply_gain_automation = false;
+       
+       boost::shared_ptr<AutomationList> gl(
+                       new AutomationList(Parameter(GainAutomation), 0.0, 2.0, 1.0));
+
+       _gain_control = boost::shared_ptr<GainControl>(
+                       new GainControl(X_("gaincontrol"), *this, gl));
+
+       add_control(_gain_control);
+
+       set_state (node);
+
+       {
+               // IO::Meter is emitted from another thread so the
+               // Meter signal must be protected.
+               Glib::Mutex::Lock guard (m_meter_signal_lock);
+               m_meter_connection = Meter.connect (mem_fun (*this, &IO::meter));
+       }
        
        // Connect to our own MoreChannels signal to connect output buffers
        IO::MoreChannels.connect (mem_fun (*this, &IO::attach_buffers));
+
+       _session.add_controllable (_gain_control);
+
+       create_bundles ();
 }
 
 IO::~IO ()
 {
-    Glib::Mutex::Lock guard (m_meter_signal_lock);
+       Glib::Mutex::Lock guard (m_meter_signal_lock);
+       
        Glib::Mutex::Lock lm (io_lock);
 
        for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
@@ -152,7 +210,7 @@ IO::~IO ()
                _session.engine().unregister_port (*i);
        }
 
-    m_meter_connection.disconnect();
+       m_meter_connection.disconnect();
 
        delete _meter;
        delete _panner;
@@ -160,171 +218,94 @@ IO::~IO ()
 }
 
 void
-IO::silence (jack_nframes_t nframes, jack_nframes_t offset)
+IO::silence (nframes_t nframes, nframes_t offset)
 {
        /* io_lock, not taken: function must be called from Session::process() calltree */
 
        for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
-               i->silence (nframes, offset);
+               i->get_buffer().silence (nframes, offset);
        }
 }
 
+/** Deliver bufs to the IO's Jack outputs.
+ *
+ * This function should automatically do whatever it necessary to correctly deliver bufs
+ * to the outputs, eg applying gain or pan or whatever else needs to be done.
+ */
 void
-IO::pan_automated (BufferSet& bufs, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t nframes, jack_nframes_t offset)
-{
-       _panner->distribute_automated(bufs, output_buffers(), start_frame, end_frame, nframes, offset);
-}
-
-void
-IO::pan (BufferSet& bufs, jack_nframes_t nframes, jack_nframes_t offset, gain_t gain_coeff)
-{
-       /* the panner can be empty if there are no inputs to the route, but still outputs */
-       if (_panner->bypassed() || _panner->empty()) {
-               // FIXME: gain
-               deliver_output_no_pan (bufs, nframes, offset);
-               return;
-       } else {
-               _panner->distribute(bufs, output_buffers(), nframes, offset, gain_coeff);
-       }
-}
-
-void
-IO::deliver_output (BufferSet& bufs, jack_nframes_t nframes, jack_nframes_t offset)
+IO::deliver_output (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
 {
-       throw;
-#if 0
-       /* io_lock, not taken: function must be called from Session::process() calltree */
-
-       if (n_outputs().get(DataType::AUDIO) == 0) {
-               return;
-       }
-       
-       if (_panner->bypassed() || _panner->empty()) {
-               deliver_output_no_pan (bufs, nbufs, nframes, offset);
-               return;
-       }
+       // FIXME: type specific code doesn't actually need to be here, it will go away in time
 
+       /* ********** AUDIO ********** */
 
-       gain_t dg;
-       gain_t pangain = _gain;
-       
-       {
-               Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
+       // Apply gain if gain automation isn't playing
+       if ( ! apply_gain_automation) {
                
-               if (dm.locked()) {
-                       dg = _desired_gain;
-               } else {
-                       dg = _gain;
-               }
-       }
-
-       if (dg != _gain) {
-               Declicker::run (bufs, nbufs, nframes, _gain, dg, false);
-               _gain = dg;
-               pangain = 1.0f;
-       } 
+               gain_t dg = _gain; // desired gain
 
-       /* simple, non-automation panning to outputs */
+               {
+                       Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
 
-       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);
-       }
-#endif
-}
+                       if (dm.locked()) {
+                               dg = _desired_gain;
+                       }
 
-void
-IO::deliver_output_no_pan (BufferSet& bufs, jack_nframes_t nframes, jack_nframes_t offset)
-{
-       throw;
-#if 0
-       /* io_lock, not taken: function must be called from Session::process() calltree */
+               }
 
-       if (n_outputs().get(DataType::AUDIO) == 0) {
-               return;
+               if (dg != _gain || dg != 1.0)
+                       Amp::run_in_place(bufs, nframes, _gain, dg, _phase_invert);
        }
-
-       gain_t dg;
-       gain_t old_gain = _gain;
-
-       if (apply_gain_automation) {
-
-               /* gain has already been applied by automation code. do nothing here except
-                  speed quietning.
-               */
-
-               _gain = 1.0f;
-               dg = _gain;
-               
+       
+       // 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 {
-
-               Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
+               const DataType type = DataType::AUDIO;
                
-               if (dm.locked()) {
-                       dg = _desired_gain;
-               } else {
-                       dg = _gain;
+               // 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;
                }
-       }
 
-       Sample* src;
-       Sample* dst;
-       uint32_t i;
-       vector<Sample*> outs;
-       gain_t actual_gain;
+               /* extra outputs get a copy of the last buffer */
 
-       if (dg != _gain) {
-               /* unlikely condition */
-               i = 0;
-               for (PortSet::audio_iterator o = _outputs.audio_begin(); o != _outputs.audio_end(); ++o, ++i) {
-                       outs.push_back ((*o)->get_audio_buffer().data (nframes, offset));
+               while (o != output_buffers().end(type)) {
+                       o->read_from(*prev, nframes, offset);
+                       ++o;
                }
        }
 
-       /* reduce nbufs to the index of the last input buffer */
-
-       nbufs--;
+       /* ********** MIDI ********** */
 
-       if (_session.transport_speed() > 1.5f || _session.transport_speed() < -1.5f) {
-               actual_gain = _gain * speed_quietning;
-       } else {
-               actual_gain = _gain;
-       }
-       
-       i = 0;
-       for (PortSet::audio_iterator o = _outputs.audio_begin(); o != _outputs.audio_end(); ++o, ++i) {
-
-               dst = (*o)->get_audio_buffer().data(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 (jack_nframes_t x = 0; x < nframes; ++x) {
-                               dst[x] = src[x] * actual_gain;
-                       }
-               }
-               
-               (*o)->mark_silence (false);
+       // No MIDI, we're done here
+       if (bufs.count().n_midi() == 0) {
+               return;
        }
 
-       if (dg != _gain) {
-               Declicker::run (outs, outs.size(), nframes, _gain, dg, false);
-               _gain = dg;
-       }
+       const DataType type = DataType::MIDI;
 
-       if (apply_gain_automation) {
-               _gain = old_gain;
+       // 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);
        }
-#endif
 }
 
 void
-IO::collect_input (BufferSet& outs, jack_nframes_t nframes, jack_nframes_t offset)
+IO::collect_input (BufferSet& outs, nframes_t nframes, nframes_t offset)
 {
+       assert(outs.available() >= n_inputs());
+
        outs.set_count(n_inputs());
        
        if (outs.count() == ChanCount::ZERO)
@@ -341,32 +322,31 @@ IO::collect_input (BufferSet& outs, jack_nframes_t nframes, jack_nframes_t offse
 }
 
 void
-IO::just_meter_input (jack_nframes_t start_frame, jack_nframes_t end_frame, 
-                     jack_nframes_t nframes, jack_nframes_t offset)
+IO::just_meter_input (nframes_t start_frame, nframes_t end_frame, 
+                     nframes_t nframes, nframes_t offset)
 {
-       BufferSet& bufs = _session.get_scratch_buffers ();
-       ChanCount nbufs = n_process_buffers ();
+       BufferSet& bufs = _session.get_scratch_buffers (n_inputs());
 
        collect_input (bufs, nframes, offset);
 
-       _meter->run(bufs, nframes);
+       _meter->run(bufs, start_frame, end_frame, nframes, offset);
 }
 
 void
-IO::drop_input_connection ()
+IO::drop_input_bundle ()
 {
-       _input_connection = 0;
-       input_connection_configuration_connection.disconnect();
-       input_connection_connection_connection.disconnect();
+       _input_bundle.reset ();
+       input_bundle_configuration_connection.disconnect();
+       input_bundle_connection_connection.disconnect();
        _session.set_dirty ();
 }
 
 void
-IO::drop_output_connection ()
+IO::drop_output_bundle ()
 {
-       _output_connection = 0;
-       output_connection_configuration_connection.disconnect();
-       output_connection_connection_connection.disconnect();
+       _output_bundle.reset ();
+       output_bundle_configuration_connection.disconnect();
+       output_bundle_connection_connection.disconnect();
        _session.set_dirty ();
 }
 
@@ -378,7 +358,7 @@ 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);
@@ -396,7 +376,7 @@ IO::disconnect_input (Port* our_port, string other_port, void* src)
                                return -1;
                        }
 
-                       drop_input_connection();
+                       drop_input_bundle ();
                }
        }
 
@@ -414,7 +394,7 @@ 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);
@@ -431,7 +411,7 @@ IO::connect_input (Port* our_port, string other_port, void* src)
                                return -1;
                        }
                        
-                       drop_input_connection ();
+                       drop_input_bundle ();
                }
        }
 
@@ -448,7 +428,7 @@ 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);
@@ -466,7 +446,7 @@ IO::disconnect_output (Port* our_port, string other_port, void* src)
                                return -1;
                        }
 
-                       drop_output_connection ();
+                       drop_output_bundle ();
                }
        }
 
@@ -483,7 +463,8 @@ 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);
@@ -500,7 +481,7 @@ IO::connect_output (Port* our_port, string other_port, void* src)
                                return -1;
                        }
 
-                       drop_output_connection ();
+                       drop_output_bundle ();
                }
        }
 
@@ -516,20 +497,20 @@ IO::set_input (Port* other_port, void* src)
           to the specified source.
        */
 
-       if (_input_minimum.get_total() > 1) {
+       if (_input_minimum.n_total() > 1) {
                /* sorry, you can't do this */
                return -1;
        }
 
        if (other_port == 0) {
                if (_input_minimum == ChanCount::ZERO) {
-                       return ensure_inputs (0, false, true, src);
+                       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;
        }
 
@@ -539,50 +520,46 @@ IO::set_input (Port* other_port, void* src)
 int
 IO::remove_output_port (Port* port, void* src)
 {
-       throw; // FIXME
-#if 0
        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 (PortSet::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 (change != NoChange) {
+                       if (_outputs.remove(port)) {
+                               change = IOChange (change|ConfigurationChanged);
+
+                               if (port->connected()) {
+                                       change = IOChange (change|ConnectionsChanged);
+                               } 
+
+                               _session.engine().unregister_port (*port);
+                               drop_output_bundle ();
+                               
                                setup_peak_meters ();
                                reset_panner ();
                        }
                }
        }
-       
+
+       if (change == ConnectionsChanged) {
+               setup_bundles ();
+       }
+
        if (change != NoChange) {
-               output_changed (change, src); /* EMIT SIGNAL */
+               output_changed (change, src);
                _session.set_dirty ();
                return 0;
-       }
-#endif
+       } 
+       
        return -1;
 }
 
@@ -602,7 +579,8 @@ IO::add_output_port (string destination, void* src, DataType type)
                type = _default_type;
 
        {
-               Glib::Mutex::Lock em(_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
+
                
                { 
                        Glib::Mutex::Lock lm (io_lock);
@@ -614,7 +592,7 @@ IO::add_output_port (string destination, void* src, DataType type)
                        /* Create a new output port */
                        
                        // FIXME: naming scheme for differently typed ports?
-                       if (_output_maximum.get_total() == 1) {
+                       if (_output_maximum.get(type) == 1) {
                                snprintf (name, sizeof (name), _("%s/out"), _name.c_str());
                        } else {
                                snprintf (name, sizeof (name), _("%s/out %u"), _name.c_str(), find_output_port_hole());
@@ -625,8 +603,8 @@ IO::add_output_port (string destination, void* src, DataType type)
                                return -1;
                        }
                        
-                       _outputs.add_port (our_port);
-                       drop_output_connection ();
+                       _outputs.add (our_port);
+                       drop_output_bundle ();
                        setup_peak_meters ();
                        reset_panner ();
                }
@@ -642,6 +620,7 @@ IO::add_output_port (string destination, void* src, DataType type)
        
        // pan_changed (src); /* EMIT SIGNAL */
        output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
+       setup_bundles ();
        _session.set_dirty ();
        
        return 0;
@@ -650,51 +629,46 @@ IO::add_output_port (string destination, void* src, DataType type)
 int
 IO::remove_input_port (Port* port, void* src)
 {
-       throw; // FIXME
-#if 0
        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 (PortSet::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);
+                               drop_input_bundle ();
+                               
                                setup_peak_meters ();
                                reset_panner ();
                        }
                }
        }
 
+       if (change == ConfigurationChanged) {
+               setup_bundles ();
+       }
+
        if (change != NoChange) {
                input_changed (change, src);
                _session.set_dirty ();
                return 0;
        } 
-#endif
+       
        return -1;
 }
 
@@ -715,7 +689,7 @@ IO::add_input_port (string source, void* src, DataType type)
                type = _default_type;
 
        {
-               Glib::Mutex::Lock em (_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
                
                { 
                        Glib::Mutex::Lock lm (io_lock);
@@ -727,19 +701,19 @@ IO::add_input_port (string source, void* src, DataType type)
                        /* Create a new input port */
                        
                        // FIXME: naming scheme for differently typed ports?
-                       if (_input_maximum.get_total() == 1) {
+                       if (_input_maximum.get(type) == 1) {
                                snprintf (name, sizeof (name), _("%s/in"), _name.c_str());
                        } else {
                                snprintf (name, sizeof (name), _("%s/in %u"), _name.c_str(), find_input_port_hole());
                        }
-                       
+
                        if ((our_port = _session.engine().register_input_port (type, name)) == 0) {
                                error << string_compose(_("IO: cannot register input port %1"), name) << endmsg;
                                return -1;
                        }
-                       
-                       _inputs.add_port(our_port);
-                       drop_input_connection ();
+
+                       _inputs.add (our_port);
+                       drop_input_bundle ();
                        setup_peak_meters ();
                        reset_panner ();
                }
@@ -756,6 +730,7 @@ IO::add_input_port (string source, void* src, DataType type)
 
        // pan_changed (src); /* EMIT SIGNAL */
        input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
+       setup_bundles ();
        _session.set_dirty ();
        
        return 0;
@@ -765,7 +740,7 @@ int
 IO::disconnect_inputs (void* src)
 {
        { 
-               Glib::Mutex::Lock em (_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
                
                {
                        Glib::Mutex::Lock lm (io_lock);
@@ -774,7 +749,7 @@ IO::disconnect_inputs (void* src)
                                _session.engine().disconnect (*i);
                        }
 
-                       drop_input_connection ();
+                       drop_input_bundle ();
                }
        }
        
@@ -787,7 +762,7 @@ int
 IO::disconnect_outputs (void* src)
 {
        {
-               Glib::Mutex::Lock em (_session.engine().process_lock());
+               BLOCK_PROCESS_CALLBACK ();
                
                {
                        Glib::Mutex::Lock lm (io_lock);
@@ -796,7 +771,7 @@ IO::disconnect_outputs (void* src)
                                _session.engine().disconnect (*i);
                        }
 
-                       drop_output_connection ();
+                       drop_output_bundle ();
                }
        }
 
@@ -807,59 +782,60 @@ IO::disconnect_outputs (void* src)
 }
 
 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;
-       
-       /* remove unused ports */
-
-       while (n_inputs().get(_default_type) > n) {
-               throw; // FIXME
-               /*
-               _session.engine().unregister_port (_inputs.back());
-               _inputs.pop_back();
-               _ninputs--;
-               changed = true;
-               */
-       }
-               
-       /* create any necessary new ports */
-               
-       while (n_inputs().get(_default_type) < n) {
-               
-               char buf[64];
-               
-               /* Create a new input port (of the default type) */
+       Port* input_port = 0;
+       bool  changed    = false;
+
+
+       for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
                
-               if (_input_maximum.get_total() == 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) {
+
+                       char buf[64];
+
+                       if (_input_maximum.get(*t) == 1) {
+                               snprintf (buf, sizeof (buf), _("%s/in"), _name.c_str());
+                       } else {
+                               snprintf (buf, sizeof (buf), _("%s/in %u"), _name.c_str(), find_input_port_hole());
                        }
-               }
 
-               catch (AudioEngine::PortRegistrationFailure& err) {
-                       setup_peak_meters ();
-                       reset_panner ();
-                       /* pass it on */
-                       throw err;
+                       try {
+
+                               if ((input_port = _session.engine().register_input_port (*t, buf)) == 0) {
+                                       error << string_compose(_("IO: cannot register input port %1"), buf) << endmsg;
+                                       return -1;
+                               }
+                       }
+
+                       catch (AudioEngine::PortRegistrationFailure& err) {
+                               setup_peak_meters ();
+                               reset_panner ();
+                               /* pass it on */
+                               throw AudioEngine::PortRegistrationFailure();
+                       }
+
+                       _inputs.add (input_port);
+                       changed = true;
                }
-               
-               _inputs.add_port (input_port);
-               changed = true;
        }
        
        if (changed) {
-               drop_input_connection ();
+               drop_input_bundle ();
                setup_peak_meters ();
                reset_panner ();
                MoreChannels (n_inputs()); /* EMIT SIGNAL */
@@ -868,7 +844,6 @@ IO::ensure_inputs_locked (uint32_t n, bool clear, void* src)
        
        if (clear) {
                /* disconnect all existing ports so that we get a fresh start */
-                       
                for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
                        _session.engine().disconnect (*i);
                }
@@ -879,7 +854,7 @@ IO::ensure_inputs_locked (uint32_t n, bool clear, void* src)
 
 /** Attach output_buffers to port buffers.
  * 
- * Connected to IOs own MoreChannels signal.
+ * Connected to IO's own MoreChannels signal.
  */
 void
 IO::attach_buffers(ChanCount ignored)
@@ -888,129 +863,123 @@ IO::attach_buffers(ChanCount ignored)
 }
 
 int
-IO::ensure_io (const ChanCount& in, const ChanCount& out, bool clear, void* src)
-{
-       // FIXME: TYPE
-       uint32_t nin = in.get(_default_type);
-       uint32_t nout = out.get(_default_type);
-
-       // We only deal with one type still.  Sorry about your luck.
-       assert(nin == in.get_total());
-       assert(nout == out.get_total());
-
-       return ensure_io(nin, nout, clear, src);
-}
-
-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 need_pan_reset;
+       bool in_changed     = false;
+       bool out_changed    = false;
+       bool need_pan_reset = false;
 
-       nin = min (_input_maximum.get(_default_type), static_cast<size_t>(nin));
+       in = min (_input_maximum, in);
 
-       nout = min (_output_maximum.get(_default_type), static_cast<size_t>(nout));
+       out = min (_output_maximum, out);
 
-       if (nin == n_inputs().get(_default_type) && nout == n_outputs().get(_default_type) && !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 (n_outputs().get(_default_type) == nout) {
-                       need_pan_reset = false;
-               } else {
+               if (n_outputs() != out) {
                        need_pan_reset = true;
                }
                
-               /* remove unused ports */
-               
-               while (n_inputs().get(_default_type) > nin) {
-                       throw; // FIXME
-                       /*
-                       _session.engine().unregister_port (_inputs.back());
-                       _inputs.pop_back();
-                       _ninputs--;
-                       in_changed = true;*/
-               }
-               
-               while (n_outputs().get(_default_type) > nout) {
-                       throw; // FIXME
-                       /*
-                       _session.engine().unregister_port (_outputs.back());
-                       _outputs.pop_back();
-                       _noutputs--;
-                       out_changed = true;*/
-               }
-               
-               /* create any necessary new ports (of the default type) */
-               
-               while (n_inputs().get(_default_type) < nin) {
-                       
-                       char buf[64];
+               for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
 
-                       /* Create a new input port */
-                       
-                       if (_input_maximum.get_total() == 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) {
+
+                               char buf[64];
+
+                               /* Create a new input port */
+
+                               if (_input_maximum.get(*t) == 1) {
+                                       snprintf (buf, sizeof (buf), _("%s/in"), _name.c_str());
+                               } else {
+                                       snprintf (buf, sizeof (buf), _("%s/in %u"), _name.c_str(), find_input_port_hole());
                                }
-                       }
 
-                       catch (AudioEngine::PortRegistrationFailure& err) {
-                               setup_peak_meters ();
-                               reset_panner ();
-                               /* pass it on */
-                               throw err;
-                       }
-               
-                       _inputs.add_port (port);
-                       in_changed = true;
-               }
+                               try {
+                                       if ((port = _session.engine().register_input_port (*t, buf)) == 0) {
+                                               error << string_compose(_("IO: cannot register input port %1"), buf) << endmsg;
+                                               return -1;
+                                       }
+                               }
+                               
+                               catch (AudioEngine::PortRegistrationFailure& err) {
+                                       setup_peak_meters ();
+                                       reset_panner ();
+                                       /* pass it on */
+                                       throw AudioEngine::PortRegistrationFailure();
+                               }
 
-               /* create any necessary new ports */
-               
-               while (n_outputs().get(_default_type) < nout) {
-                       
-                       char buf[64];
-                       
-                       /* Create a new output port */
-                       
-                       if (_output_maximum.get_total() == 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());
+                               _inputs.add (port);
+                               in_changed = true;
                        }
-                       
-                       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) {
+
+                               char buf[64];
+
+                               /* Create a new output port */
+
+                               if (_output_maximum.get(*t) == 1) {
+                                       snprintf (buf, sizeof (buf), _("%s/out"), _name.c_str());
+                               } else {
+                                       snprintf (buf, sizeof (buf), _("%s/out %u"), _name.c_str(), find_output_port_hole());
                                }
+
+                               try { 
+                                       if ((port = _session.engine().register_output_port (*t, buf)) == 0) {
+                                               error << string_compose(_("IO: cannot register output port %1"), buf) << endmsg;
+                                               return -1;
+                                       }
+                               }
+
+                               catch (AudioEngine::PortRegistrationFailure& err) {
+                                       setup_peak_meters ();
+                                       reset_panner ();
+                                       /* pass it on */
+                                       throw AudioEngine::PortRegistrationFailure ();
+                               }
+
+                               _outputs.add (port);
+                               out_changed = true;
                        }
-                       
-                       catch (AudioEngine::PortRegistrationFailure& err) {
-                               setup_peak_meters ();
-                               reset_panner ();
-                               /* pass it on */
-                               throw err;
-                       }
-               
-                       _outputs.add_port (port);
-                       out_changed = true;
                }
                
                if (clear) {
@@ -1033,17 +1002,18 @@ IO::ensure_io (uint32_t nin, uint32_t nout, bool clear, void* src)
        }
 
        if (out_changed) {
-               drop_output_connection ();
+               drop_output_bundle ();
                output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
        }
        
        if (in_changed) {
-               drop_input_connection ();
+               drop_input_bundle ();
                input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
        }
 
        if (in_changed || out_changed) {
                MoreChannels (max (n_outputs(), n_inputs())); /* EMIT SIGNAL */
+               setup_bundles ();
                _session.set_dirty ();
        }
 
@@ -1051,93 +1021,92 @@ 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;
 
-       n = min (_input_maximum.get(_default_type), static_cast<size_t>(n));
+       count = min (_input_maximum, count);
 
-       if (n == n_inputs().get(_default_type) && !clear) {
+       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 ();
                _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 need_pan_reset;
+       Port* output_port    = 0;
+       bool  changed        = false;
+       bool  need_pan_reset = false;
 
-       if (n_outputs().get(_default_type) == n) {
-               need_pan_reset = false;
-       } else {
+       if (n_outputs() != count) {
                need_pan_reset = true;
        }
        
-       /* remove unused ports */
-       
-       while (n_outputs().get(_default_type) > n) {
-               throw; // FIXME
-               /*
-               _session.engine().unregister_port (_outputs.back());
-               _outputs.pop_back();
-               _noutputs--;
-               changed = true;
-               */
-       }
-       
-       /* create any necessary new ports */
-       
-       while (n_outputs().get(_default_type) < n) {
-               
-               char buf[64];
-               
-               /* Create a new output port */
-               
-               if (_output_maximum.get(_default_type) == 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.add_port (output_port);
-               changed = true;
-               setup_peak_meters ();
 
-               if (need_pan_reset) {
-                       reset_panner ();
+               /* create any necessary new ports */
+               while (n_outputs().get(*t) < n) {
+
+                       char buf[64];
+
+                       if (_output_maximum.get(*t) == 1) {
+                               snprintf (buf, sizeof (buf), _("%s/out"), _name.c_str());
+                       } else {
+                               snprintf (buf, sizeof (buf), _("%s/out %u"), _name.c_str(), find_output_port_hole());
+                       }
+
+                       if ((output_port = _session.engine().register_output_port (*t, buf)) == 0) {
+                               error << string_compose(_("IO: cannot register output port %1"), buf) << endmsg;
+                               return -1;
+                       }
+
+                       _outputs.add (output_port);
+                       changed = true;
+                       setup_peak_meters ();
+
+                       if (need_pan_reset) {
+                               reset_panner ();
+                       }
                }
        }
        
        if (changed) {
-               drop_output_connection ();
+               drop_output_bundle ();
                MoreChannels (n_outputs()); /* EMIT SIGNAL */
                _session.set_dirty ();
        }
        
        if (clear) {
                /* disconnect all existing ports so that we get a fresh start */
-               
                for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
                        _session.engine().disconnect (*i);
                }
@@ -1147,13 +1116,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 < ChanCount::INFINITE) {
-               n = min (_output_maximum.get(_default_type), static_cast<size_t>(n));
-               if (n == n_outputs().get(_default_type) && !clear) {
+               count = min (_output_maximum, count);
+               if (count == n_outputs() && !clear) {
                        return 0;
                }
        }
@@ -1161,24 +1130,26 @@ 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 ();
        }
+
        return 0;
 }
 
 gain_t
 IO::effective_gain () const
 {
-       if (gain_automation_playback()) {
-               return _effective_gain;
+       if (_gain_control->list()->automation_playback()) {
+               return _gain_control->get_value();
        } else {
                return _desired_gain;
        }
@@ -1189,7 +1160,7 @@ IO::reset_panner ()
 {
        if (panners_legal) {
                if (!no_panner_reset) {
-                       _panner->reset (n_outputs().get(_default_type), pans_required());
+                       _panner->reset (n_outputs().n_audio(), pans_required());
                }
        } else {
                panner_legal_c.disconnect ();
@@ -1200,7 +1171,7 @@ IO::reset_panner ()
 int
 IO::panners_became_legal ()
 {
-       _panner->reset (n_outputs().get(_default_type), pans_required());
+       _panner->reset (n_outputs().n_audio(), pans_required());
        _panner->load (); // automation
        panner_legal_c.disconnect ();
        return 0;
@@ -1238,18 +1209,18 @@ IO::state (bool full_state)
        Glib::Mutex::Lock lm (io_lock);
 
        node->add_property("name", _name);
-       id().print (buf);
+       id().print (buf, sizeof (buf));
        node->add_property("id", buf);
 
        str = "";
 
-       if (_input_connection) {
-               node->add_property ("input-connection", _input_connection->name());
+       if (_input_bundle && !_input_bundle->dynamic()) {
+               node->add_property ("input-connection", _input_bundle->name());
                need_ins = false;
        }
 
-       if (_output_connection) {
-               node->add_property ("output-connection", _output_connection->name());
+       if (_output_bundle && !_output_bundle->dynamic()) {
+               node->add_property ("output-connection", _output_bundle->name());
                need_outs = false;
        }
 
@@ -1320,10 +1291,12 @@ IO::state (bool full_state)
        }
 
        node->add_child_nocopy (_panner->state (full_state));
+       node->add_child_nocopy (_gain_control->get_state ());
 
        snprintf (buf, sizeof(buf), "%2.12f", gain());
        node->add_property ("gain", buf);
 
+       // FIXME: this is NOT sufficient!
        const int in_min  = (_input_minimum == ChanCount::ZERO) ? -1 : _input_minimum.get(_default_type);
        const int in_max  = (_input_maximum == ChanCount::INFINITE) ? -1 : _input_maximum.get(_default_type);
        const int out_min = (_output_minimum == ChanCount::ZERO) ? -1 : _output_minimum.get(_default_type);
@@ -1334,68 +1307,13 @@ IO::state (bool full_state)
        node->add_property ("iolimits", buf);
 
        /* automation */
-
-       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);
-
-       /* XXX same for pan etc. */
+       
+       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)
 {
@@ -1414,7 +1332,7 @@ 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) {
@@ -1440,26 +1358,31 @@ IO::set_state (const XMLNode& node)
                _gain = _desired_gain;
        }
 
+       if ((prop = node.property ("automation-state")) != 0 || (prop = node.property ("automation-style")) != 0) {
+               /* old school automation handling */
+       }
+
        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 ((prop = node.property ("automation-state")) != 0) {
+               if ((*iter)->name() == X_("Automation")) {
 
-               long int x;
-               x = strtol (prop->value().c_str(), 0, 16);
-               set_gain_automation_state (AutoState (x));
-       }
-
-       if ((prop = node.property ("automation-style")) != 0) {
+                       set_automation_state (*(*iter), Parameter(GainAutomation));
+               }
 
-              long int x;
-               x = strtol (prop->value().c_str(), 0, 16);
-               set_gain_automation_style (AutoStyle (x));
+               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)) {
@@ -1477,22 +1400,145 @@ IO::set_state (const XMLNode& node)
                panner_legal_c = PannersLegal.connect (mem_fun (*this, &IO::panners_became_legal));
        }
 
-       if (connecting_legal) {
-
-               if (make_connections (node)) {
-                       return -1;
-               }
+       if (connecting_legal) {
+
+               if (make_connections (node)) {
+                       return -1;
+               }
+
+       } else {
+               
+               connection_legal_c = ConnectingLegal.connect (mem_fun (*this, &IO::connecting_became_legal));
+       }
+
+       if (!ports_legal || !connecting_legal) {
+               pending_state_node = new XMLNode (node);
+       }
+
+       return 0;
+}
+
+int
+IO::load_automation (string path)
+{
+       string fullpath;
+       ifstream in;
+       char line[128];
+       uint32_t linecnt = 0;
+       float version;
+       LocaleGuard lg (X_("POSIX"));
+
+       fullpath = _session.automation_dir();
+       fullpath += path;
+
+       in.open (fullpath.c_str());
+
+       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\""), _name, fullpath) << endmsg;
+                       return -1;
+               }
+       }
+
+       clear_automation ();
+
+       while (in.getline (line, sizeof(line), '\n')) {
+               char type;
+               nframes_t when;
+               double value;
+
+               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;
+                       }
+
+                       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 ();
 
-       } else {
-               
-               connection_legal_c = ConnectingLegal.connect (mem_fun (*this, &IO::connecting_became_legal));
-       }
+       ret = create_ports (*pending_state_node);
 
-       if (!ports_legal || !connecting_legal) {
-               pending_state_node = new XMLNode (node);
+       if (connecting_legal) {
+               delete pending_state_node;
+               pending_state_node = 0;
        }
 
-       return 0;
+       return ret;
 }
 
 int
@@ -1502,24 +1548,27 @@ IO::create_ports (const XMLNode& node)
        int num_inputs = 0;
        int num_outputs = 0;
 
+       /* XXX: we could change *-connection to *-bundle, but it seems a bit silly to
+        * break the session file format.
+        */
        if ((prop = node.property ("input-connection")) != 0) {
 
-               Connection* c = _session.connection_by_name (prop->value());
+               boost::shared_ptr<Bundle> c = _session.bundle_by_name (prop->value());
                
                if (c == 0) {
-                       error << string_compose(_("Unknown connection \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
+                       error << string_compose(_("Unknown bundle \"%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")
+                       if ((c = _session.bundle_by_name (_("in 1"))) == 0) {
+                               error << _("No input bundles available as a replacement")
                                      << endmsg;
                                return -1;
                        }  else {
-                               info << string_compose (_("Connection %1 was not available - \"in 1\" used instead"), prop->value())
+                               info << string_compose (_("Bundle %1 was not available - \"in 1\" used instead"), prop->value())
                                     << endmsg;
                        }
                } 
 
-               num_inputs = c->nports();
+               num_inputs = c->nchannels();
 
        } else if ((prop = node.property ("inputs")) != 0) {
 
@@ -1527,22 +1576,22 @@ IO::create_ports (const XMLNode& node)
        }
        
        if ((prop = node.property ("output-connection")) != 0) {
-               Connection* c = _session.connection_by_name (prop->value());
+               boost::shared_ptr<Bundle> c = _session.bundle_by_name (prop->value());
 
                if (c == 0) {
-                       error << string_compose(_("Unknown connection \"%1\" listed for output of %2"), prop->value(), _name) << endmsg;
+                       error << string_compose(_("Unknown bundle \"%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")
+                       if ((c = _session.bundle_by_name (_("out 1"))) == 0) {
+                               error << _("No output bundles available as a replacement")
                                      << endmsg;
                                return -1;
                        }  else {
-                               info << string_compose (_("Connection %1 was not available - \"out 1\" used instead"), prop->value())
+                               info << string_compose (_("Bundle %1 was not available - \"out 1\" used instead"), prop->value())
                                     << endmsg;
                        }
                } 
 
-               num_outputs = c->nports ();
+               num_outputs = c->nchannels ();
                
        } else if ((prop = node.property ("outputs")) != 0) {
                num_outputs = count (prop->value().begin(), prop->value().end(), '{');
@@ -1550,7 +1599,8 @@ IO::create_ports (const XMLNode& node)
 
        no_panner_reset = true;
 
-       if (ensure_io (num_inputs, num_outputs, true, this)) {
+       // FIXME: audio-only
+       if (ensure_io (ChanCount(DataType::AUDIO, num_inputs), ChanCount(DataType::AUDIO, num_outputs), true, this)) {
                error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg;
                return -1;
        }
@@ -1570,22 +1620,22 @@ 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 = _session.bundle_by_name (prop->value());
                
                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) {
+                       if ((c = _session.bundle_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())
+                               info << string_compose (_("Bundle %1 was not available - \"in 1\" used instead"), prop->value())
                                     << endmsg;
                        }
                } 
 
-               use_input_connection (*c, this);
+               connect_input_ports_to_bundle (c, this);
 
        } else if ((prop = node.property ("inputs")) != 0) {
                if (set_inputs (prop->value())) {
@@ -1594,23 +1644,23 @@ IO::make_connections (const XMLNode& node)
                }
        }
        
-       if ((prop = node.property ("output-connection")) != 0) {
-               Connection* c = _session.connection_by_name (prop->value());
+       if ((prop = node.property ("output-bundle")) != 0) {
+               boost::shared_ptr<Bundle> c = _session.bundle_by_name (prop->value());
                
                if (c == 0) {
-                       error << string_compose(_("Unknown connection \"%1\" listed for output of %2"), prop->value(), _name) << endmsg;
+                       error << string_compose(_("Unknown bundle \"%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")
+                       if ((c = _session.bundle_by_name (_("out 1"))) == 0) {
+                               error << _("No output bundles available as a replacement")
                                      << endmsg;
                                return -1;
                        }  else {
-                               info << string_compose (_("Connection %1 was not available - \"out 1\" used instead"), prop->value())
+                               info << string_compose (_("Bundle %1 was not available - \"out 1\" used instead"), prop->value())
                                     << endmsg;
                        }
                } 
 
-               use_output_connection (*c, this);
+               connect_output_ports_to_bundle (c, this);
                
        } else if ((prop = node.property ("outputs")) != 0) {
                if (set_outputs (prop->value())) {
@@ -1634,7 +1684,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;
        }
 
@@ -1684,7 +1735,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;
        }
 
@@ -1769,11 +1821,18 @@ 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& str)
 {
-       if (name == _name) {
-               return 0;
+       if (str == _name) {
+               return true;
+       }
+       
+       /* replace all colons in the name. i wish we didn't have to do this */
+       string name = str;
+
+       if (replace_all (name, ":", "-")) {
+               warning << _("you cannot use colons to name objects with I/O connections") << endmsg;
        }
 
        for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
@@ -1788,46 +1847,11 @@ IO::set_name (string name, void* src)
                i->set_name (current_name);
        }
 
-       _name = name;
-        name_changed (src); /* EMIT SIGNAL */
-
-        return 0;
-}
-
-void
-IO::set_input_minimum (int n)
-{
-       if (n < 0)
-               _input_minimum = ChanCount::ZERO;
-       else
-               _input_minimum = ChanCount(_default_type, n);
-}
-
-void
-IO::set_input_maximum (int n)
-{
-       if (n < 0)
-               _input_maximum = ChanCount::INFINITE;
-       else
-               _input_maximum = ChanCount(_default_type, n);
-}
+       bool const r = SessionObject::set_name(name);
 
-void
-IO::set_output_minimum (int n)
-{
-       if (n < 0)
-               _output_minimum = ChanCount::ZERO;
-       else
-               _output_minimum = ChanCount(_default_type, n);
-}
+       setup_bundles ();
 
-void
-IO::set_output_maximum (int n)
-{
-       if (n < 0)
-               _output_maximum = ChanCount::INFINITE;
-       else
-               _output_maximum = ChanCount(_default_type, n);
+       return r;
 }
 
 void
@@ -1855,7 +1879,7 @@ IO::set_output_maximum (ChanCount n)
 }
 
 void
-IO::set_port_latency (jack_nframes_t nframes)
+IO::set_port_latency (nframes_t nframes)
 {
        Glib::Mutex::Lock lm (io_lock);
 
@@ -1864,11 +1888,11 @@ IO::set_port_latency (jack_nframes_t nframes)
        }
 }
 
-jack_nframes_t
+nframes_t
 IO::output_latency () const
 {
-       jack_nframes_t max_latency;
-       jack_nframes_t latency;
+       nframes_t max_latency;
+       nframes_t latency;
 
        max_latency = 0;
 
@@ -1883,11 +1907,11 @@ IO::output_latency () const
        return max_latency;
 }
 
-jack_nframes_t
+nframes_t
 IO::input_latency () const
 {
-       jack_nframes_t max_latency;
-       jack_nframes_t latency;
+       nframes_t max_latency;
+       nframes_t latency;
 
        max_latency = 0;
 
@@ -1896,26 +1920,27 @@ IO::input_latency () const
        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();
+               limit = c->nchannels();
                
-               drop_input_connection ();
+               drop_input_bundle ();
                
-               if (ensure_inputs (limit, false, false, src)) {
+               // FIXME bundles only work for audio-only
+               if (ensure_inputs (ChanCount(DataType::AUDIO, limit), false, false, src)) {
                        return -1;
                }
 
@@ -1924,9 +1949,9 @@ IO::use_input_connection (Connection& c, void* src)
                */
                
                for (uint32_t n = 0; n < limit; ++n) {
-                       const Connection::PortList& pl = c.port_connections (n);
+                       const Bundle::PortList& pl = c->channel_ports (n);
                        
-                       for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
+                       for (Bundle::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
                                
                                if (!_inputs.port(n)->connected_to ((*i))) {
                                        
@@ -1954,9 +1979,9 @@ IO::use_input_connection (Connection& c, void* src)
                /* second pass: connect all requested ports where necessary */
                
                for (uint32_t n = 0; n < limit; ++n) {
-                       const Connection::PortList& pl = c.port_connections (n);
+                       const Bundle::PortList& pl = c->channel_ports (n);
                        
-                       for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
+                       for (Bundle::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
                                
                                if (!_inputs.port(n)->connected_to ((*i))) {
                                        
@@ -1968,12 +1993,12 @@ IO::use_input_connection (Connection& c, void* src)
                        }
                }
                
-               _input_connection = &c;
+               _input_bundle = c;
                
-               input_connection_configuration_connection = c.ConfigurationChanged.connect
-                       (mem_fun (*this, &IO::input_connection_configuration_changed));
-               input_connection_connection_connection = c.ConnectionsChanged.connect
-                       (mem_fun (*this, &IO::input_connection_connection_changed));
+               input_bundle_configuration_connection = c->ConfigurationChanged.connect
+                       (mem_fun (*this, &IO::input_bundle_configuration_changed));
+               input_bundle_connection_connection = c->PortsChanged.connect
+                       (mem_fun (*this, &IO::input_bundle_connection_changed));
        }
 
        input_changed (IOChange (ConfigurationChanged|ConnectionsChanged), src); /* EMIT SIGNAL */
@@ -1981,19 +2006,20 @@ 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();
+               limit = c->nchannels();
                        
-               drop_output_connection ();
+               drop_output_bundle ();
 
-               if (ensure_outputs (limit, false, false, src)) {
+               // FIXME: audio-only
+               if (ensure_outputs (ChanCount(DataType::AUDIO, limit), false, false, src)) {
                        return -1;
                }
 
@@ -2003,9 +2029,9 @@ IO::use_output_connection (Connection& c, void* src)
                        
                for (uint32_t n = 0; n < limit; ++n) {
 
-                       const Connection::PortList& pl = c.port_connections (n);
+                       const Bundle::PortList& pl = c->channel_ports (n);
                                
-                       for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
+                       for (Bundle::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
                                        
                                if (!_outputs.port(n)->connected_to ((*i))) {
 
@@ -2033,9 +2059,9 @@ IO::use_output_connection (Connection& c, void* src)
 
                for (uint32_t n = 0; n < limit; ++n) {
 
-                       const Connection::PortList& pl = c.port_connections (n);
+                       const Bundle::PortList& pl = c->channel_ports (n);
                                
-                       for (Connection::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
+                       for (Bundle::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
                                        
                                if (!_outputs.port(n)->connected_to ((*i))) {
                                                
@@ -2046,12 +2072,12 @@ IO::use_output_connection (Connection& c, void* src)
                        }
                }
 
-               _output_connection = &c;
+               _output_bundle = c;
 
-               output_connection_configuration_connection = c.ConfigurationChanged.connect
-                       (mem_fun (*this, &IO::output_connection_configuration_changed));
-               output_connection_connection_connection = c.ConnectionsChanged.connect
-                       (mem_fun (*this, &IO::output_connection_connection_changed));
+               output_bundle_configuration_connection = c->ConfigurationChanged.connect
+                       (mem_fun (*this, &IO::output_bundle_configuration_changed));
+               output_bundle_connection_connection = c->PortsChanged.connect
+                       (mem_fun (*this, &IO::output_bundle_connection_changed));
        }
 
        output_changed (IOChange (ConnectionsChanged|ConfigurationChanged), src); /* EMIT SIGNAL */
@@ -2102,64 +2128,53 @@ IO::reset_panners ()
 }
 
 void
-IO::input_connection_connection_changed (int ignored)
+IO::input_bundle_connection_changed (int ignored)
 {
-       use_input_connection (*_input_connection, this);
+       connect_input_ports_to_bundle (_input_bundle, this);
 }
 
 void
-IO::input_connection_configuration_changed ()
+IO::input_bundle_configuration_changed ()
 {
-       use_input_connection (*_input_connection, this);
+       connect_input_ports_to_bundle (_input_bundle, this);
 }
 
 void
-IO::output_connection_connection_changed (int ignored)
+IO::output_bundle_connection_changed (int ignored)
 {
-       use_output_connection (*_output_connection, this);
+       connect_output_ports_to_bundle (_output_bundle, this);
 }
 
 void
-IO::output_connection_configuration_changed ()
+IO::output_bundle_configuration_changed ()
 {
-       use_output_connection (*_output_connection, this);
+       connect_output_ports_to_bundle (_output_bundle, this);
 }
 
 void
-IO::GainControllable::set_value (float val)
-{
-       io.set_gain (direct_control_to_gain (val), this);
-}
-
-float
-IO::GainControllable::get_value (void) const
-{
-       return direct_gain_to_control (io.effective_gain());
-}
-
-UndoAction
-IO::get_memento() const
+IO::GainControl::set_value (float val)
 {
-  return sigc::bind (mem_fun (*(const_cast<IO *>(this)), &StateManager::use_state), _current_state_id);
-}
+       // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
+       if (val > 1.99526231f)
+               val = 1.99526231f;
 
-Change
-IO::restore_state (StateManager::State& state)
-{
-       return Change (0);
+       _user_value = val;
+       _io.set_gain (val, this);
+       
+       Changed(); /* EMIT SIGNAL */
 }
 
-StateManager::State*
-IO::state_factory (std::string why) const
+float
+IO::GainControl::get_value (void) const
 {
-       StateManager::State* state = new StateManager::State (why);
-       return state;
+       return AutomationControl::get_value();
 }
 
 void
 IO::setup_peak_meters()
 {
-       _meter->setup(std::max(_inputs.count(), _outputs.count()));
+       ChanCount max_streams = std::max(_inputs.count(), _outputs.count());
+       _meter->configure_io(max_streams, max_streams);
 }
 
 /**
@@ -2181,175 +2196,54 @@ IO::update_meters()
 void
 IO::meter ()
 {
-       // FIXME: Remove this function and just connect signal directly to PeakMeter::meter
+       // FIXME: Ugly.  Meter should manage the lock, if it's necessary
        
        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 " << (jack_nframes_t) floor ((*i)->when) << ' ' << (*i)->value << endl;
-       }
-
-       _panner->save ();
-
-       return 0;
+       Automatable::clear_automation (); // clears gain automation
+       _panner->clear_automation ();
 }
 
-int
-IO::load_automation (const string& path)
+void
+IO::set_parameter_automation_state (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\""), _name, fullpath) << endmsg;
-                               return -1;
-               }
-       }
+               { 
+                       Glib::Mutex::Lock lm (_automation_lock);
 
-       clear_automation ();
+                       boost::shared_ptr<AutomationList> gain_auto = _gain_control->list();
 
-       while (in.getline (line, sizeof(line), '\n')) {
-               char type;
-               jack_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;
                }
-       }
-
-       _gain_automation_curve.save_state (_("loaded from disk"));
-
-       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;
-                       last_automation_snapshot = 0;
-                       _gain_automation_curve.set_automation_state (state);
-                       
-                       if (state != Off) {
-                               set_gain (_gain_automation_curve.eval (_session.transport_frame()), this);
-                       }
+               if (changed) {
+                       _session.set_dirty ();
                }
-       }
 
-       if (changed) {
-               _session.set_dirty ();
-               gain_automation_state_changed (); /* EMIT SIGNAL */
+       } else {
+               Automatable::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)
 {
@@ -2363,7 +2257,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);
@@ -2371,38 +2273,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->list()->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()->list()->start_touch();
        }
 }
 
@@ -2410,42 +2296,34 @@ void
 IO::end_pan_touch (uint32_t which)
 {
        if (which < _panner->size()) {
-               (*_panner)[which]->automation().stop_touch();
+               (*_panner)[which]->pan_control()->list()->stop_touch();
        }
 
 }
 
 void
-IO::automation_snapshot (jack_nframes_t now)
+IO::automation_snapshot (nframes_t now)
 {
-       if (last_automation_snapshot > now || (now - last_automation_snapshot) > _automation_interval) {
+       Automatable::automation_snapshot (now);
 
-               if (gain_automation_recording()) {
-                       _gain_automation_curve.rt_add (now, gain());
-               }
-               
+       if (_last_automation_snapshot > now || (now - _last_automation_snapshot) > _session.automation_interval()) {
                _panner->snapshot (now);
-
-               last_automation_snapshot = now;
        }
 }
 
 void
-IO::transport_stopped (jack_nframes_t frame)
+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->list()->automation_state() != Off) {
                
-               if (gain_automation_recording()) {
-                       _gain_automation_curve.save_state (_("automation write/touch"));
-               }
-
                /* 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);
@@ -2536,3 +2414,131 @@ 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 ()
+{
+        char buf[32];
+
+        snprintf(buf, sizeof (buf), _("%s in"), _name.c_str());
+        _bundle_for_inputs->set_name (buf, 0);
+        int const ins = n_inputs().n_total();
+        _bundle_for_inputs->set_nchannels (ins);
+        
+        for (int i = 0; i < ins; ++i) {
+                _bundle_for_inputs->add_port_to_channel (i, _inputs.port(i)->name ());
+        }
+
+        snprintf(buf, sizeof (buf), _("%s out"), _name.c_str());
+        _bundle_for_outputs->set_name (buf, 0);
+        int const outs = n_outputs().n_total();
+        _bundle_for_outputs->set_nchannels (outs);
+        
+        for (int i = 0; i < outs; ++i) {
+                _bundle_for_outputs->add_port_to_channel (i, _outputs.port(i)->name ());
+        }
+}
+
+
+/**
+ *  Create and setup bundles that describe our inputs and outputs.
+ */
+
+void
+IO::create_bundles ()
+{
+        _bundle_for_inputs = boost::shared_ptr<Bundle> (
+                new InputBundle ("", true)
+                );
+        
+        _bundle_for_outputs = boost::shared_ptr<Bundle> (
+                new OutputBundle ("", true)
+                );
+
+        setup_bundles ();
+}
+
+boost::shared_ptr<Bundle>
+IO::input_bundle()
+{
+       if (_input_bundle) {
+               return _input_bundle;
+       }
+
+       /* XXX: will only report the first bundle found; should really return a list, I think */
+          
+       /* check that _input_bundle is right wrt the connections that are currently made */
+
+       /* make a vector of the first output connected to each of our inputs */
+       std::vector<std::string> connected;
+        for (uint32_t i = 0; i < _inputs.num_ports(); ++i) {
+               const char** c = _inputs.port(i)->get_connections ();
+               if (c) {
+                       connected.push_back (c[0]);
+               }
+       }
+
+       _input_bundle = _session.bundle_by_ports (connected);
+       return _input_bundle;
+}
+
+
+boost::shared_ptr<Bundle>
+IO::output_bundle()
+{
+       if (_output_bundle) {
+               return _output_bundle;
+       }
+       
+       /* XXX: will only report the first bundle found; should really return a list, I think */
+          
+       /* check that _output_bundle is right wrt the connections that are currently made */
+
+       /* make a vector of the first input connected to each of our outputs */
+       std::vector<std::string> connected;
+        for (uint32_t i = 0; i < _outputs.num_ports(); ++i) {
+               const char** c = _outputs.port(i)->get_connections ();
+               if (c) {
+                       connected.push_back (c[0]);
+               }
+       }
+
+       _output_bundle = _session.bundle_by_ports (connected);
+       return _output_bundle;
+}