remove StateManager code entirely and more debugging output cruft
[ardour.git] / libs / ardour / io.cc
index 2f04b7eaca380e595a9528ff20eaaf119f2aa6b2..323c58b65e1e83dbf6d5fcdae6ba87c6e73b3f38 100644 (file)
@@ -22,6 +22,7 @@
 #include <algorithm>
 #include <unistd.h>
 #include <locale.h>
+#include <errno.h>
 
 #include <sigc++/bind.h>
 
@@ -55,11 +56,11 @@ extern "C" int isinf (double);
 
 using namespace std;
 using namespace ARDOUR;
-//using namespace sigc;
+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;
@@ -73,17 +74,17 @@ sigc::signal<int>                 IO::PortsCreated;
 
 Glib::StaticMutex       IO::m_meter_signal_lock = GLIBMM_STATIC_MUTEX_INIT;
 
-/* this is a default mapper of MIDI control values to a gain coefficient.
-   others can be imagined. see IO::set_midi_to_gain_function().
+/* this is a default mapper of [0 .. 1.0] control values to a gain coefficient.
+   others can be imagined. 
 */
 
-static gain_t direct_midi_to_gain (double fract) { 
+static gain_t direct_control_to_gain (double fract) { 
        /* XXX Marcus writes: this doesn't seem right to me. but i don't have a better answer ... */
        /* this maxes at +6dB */
        return pow (2.0,(sqrt(sqrt(sqrt(fract)))*198.0-192.0)/6.0);
 }
 
-static double direct_gain_to_midi (gain_t gain) { 
+static double direct_gain_to_control (gain_t gain) { 
        /* XXX Marcus writes: this doesn't seem right to me. but i don't have a better answer ... */
        if (gain == 0) return 0.0;
        
@@ -96,19 +97,22 @@ static bool sort_ports_by_name (Port* a, Port* b)
 }
 
 
+/** @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,
-
-       int input_min, int input_max, int output_min, int output_max)
+       int input_min, int input_max, int output_min, int output_max,
+       DataType default_type)
        : _session (s),
          _name (name),
-         _midi_gain_control (*this, _session.midi_port()),
+         _default_type(default_type),
+         _gain_control (X_("gaincontrol"), *this),
          _gain_automation_curve (0.0, 2.0, 1.0),
          _input_minimum (input_min),
          _input_maximum (input_max),
          _output_minimum (output_min),
          _output_maximum (output_max)
 {
-       _id = new_id();
        _panner = new Panner (name, _session);
        _gain = 1.0;
        _desired_gain = 1.0;
@@ -120,28 +124,25 @@ IO::IO (Session& s, string name,
        no_panner_reset = false;
        deferred_state = 0;
 
-       _midi_gain_control.midi_to_gain = direct_midi_to_gain;
-       _midi_gain_control.gain_to_midi = direct_gain_to_midi;
-
        apply_gain_automation = false;
-
-       last_automation_snapshot = 0;
-
+       _ignore_gain_on_deliver = false;
+       
        _gain_automation_state = Off;
        _gain_automation_style = Absolute;
-    
-    {
-        // IO::Meter is emitted from another thread so the
-        // Meter signal must be protected.
-        Glib::Mutex::Lock guard (m_meter_signal_lock);
-        m_meter_connection = Meter.connect (mem_fun (*this, &IO::meter));
-    }
+
+       {
+               // IO::Meter is emitted from another thread so the
+               // Meter signal must be protected.
+               Glib::Mutex::Lock guard (m_meter_signal_lock);
+               m_meter_connection = Meter.connect (mem_fun (*this, &IO::meter));
+       }
 }
 
 IO::~IO ()
 {
 
-    Glib::Mutex::Lock guard (m_meter_signal_lock);
+       Glib::Mutex::Lock guard (m_meter_signal_lock);
+       
        Glib::Mutex::Lock lm (io_lock);
        vector<Port *>::iterator i;
 
@@ -153,11 +154,11 @@ IO::~IO ()
                _session.engine().unregister_port (*i);
        }
 
-    m_meter_connection.disconnect();
+       m_meter_connection.disconnect();
 }
 
 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 */
 
@@ -167,9 +168,9 @@ IO::silence (jack_nframes_t nframes, jack_nframes_t offset)
 }
 
 void
-IO::apply_declick (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframes, gain_t initial, gain_t target, bool invert_polarity)
+IO::apply_declick (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, gain_t initial, gain_t target, bool invert_polarity)
 {
-       jack_nframes_t declick = min ((jack_nframes_t)4096, nframes);
+       nframes_t declick = min ((nframes_t)128, nframes);
        gain_t delta;
        Sample *buffer;
        double fractional_shift;
@@ -193,7 +194,7 @@ IO::apply_declick (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframe
                buffer = bufs[n];
                fractional_pos = 1.0;
 
-               for (jack_nframes_t nx = 0; nx < declick; ++nx) {
+               for (nframes_t nx = 0; nx < declick; ++nx) {
                        buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
                        fractional_pos += fractional_shift;
                }
@@ -201,18 +202,21 @@ IO::apply_declick (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframe
                /* now ensure the rest of the buffer has the target value
                   applied, if necessary.
                */
-               
+
                if (declick != nframes) {
+                       float this_target;
 
                        if (invert_polarity) {
-                               target = -target;
+                               this_target = -target;
+                       } else { 
+                               this_target = target;
                        }
 
-                       if (target == 0.0) {
+                       if (this_target == 0.0) {
                                memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
-                       } else if (target != 1.0) {
-                               for (jack_nframes_t nx = declick; nx < nframes; ++nx) {
-                                       buffer[nx] *= target;
+                       } else if (this_target != 1.0) {
+                               for (nframes_t nx = declick; nx < nframes; ++nx) {
+                                       buffer[nx] *= this_target;
                                }
                        }
                }
@@ -220,7 +224,7 @@ IO::apply_declick (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframe
 }
 
 void
-IO::pan_automated (vector<Sample*>& bufs, uint32_t nbufs, jack_nframes_t start, jack_nframes_t end, jack_nframes_t nframes, jack_nframes_t offset)
+IO::pan_automated (vector<Sample*>& bufs, uint32_t nbufs, nframes_t start, nframes_t end, nframes_t nframes, nframes_t offset)
 {
        Sample* dst;
 
@@ -267,7 +271,7 @@ IO::pan_automated (vector<Sample*>& bufs, uint32_t nbufs, jack_nframes_t start,
 }
 
 void
-IO::pan (vector<Sample*>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset, gain_t gain_coeff)
+IO::pan (vector<Sample*>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset, gain_t gain_coeff)
 {
        Sample* dst;
        Sample* src;
@@ -308,7 +312,7 @@ IO::pan (vector<Sample*>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nfr
                        for (n = 1; n < nbufs; ++n) {
                                src = bufs[n];
                                
-                               for (jack_nframes_t n = 0; n < nframes; ++n) {
+                               for (nframes_t n = 0; n < nframes; ++n) {
                                        dst[n] += src[n];
                                }
                        }
@@ -323,14 +327,14 @@ IO::pan (vector<Sample*>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nfr
 
                        src = bufs[0];
                        
-                       for (jack_nframes_t n = 0; n < nframes; ++n) {
+                       for (nframes_t n = 0; n < nframes; ++n) {
                                dst[n] = src[n] * gain_coeff;
                        }       
 
                        for (n = 1; n < nbufs; ++n) {
                                src = bufs[n];
                                
-                               for (jack_nframes_t n = 0; n < nframes; ++n) {
+                               for (nframes_t n = 0; n < nframes; ++n) {
                                        dst[n] += src[n] * gain_coeff;
                                }       
                        }
@@ -374,7 +378,7 @@ IO::pan (vector<Sample*>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nfr
 }
 
 void
-IO::deliver_output (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset)
+IO::deliver_output (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset)
 {
        /* io_lock, not taken: function must be called from Session::process() calltree */
 
@@ -417,7 +421,7 @@ IO::deliver_output (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nfram
 }
 
 void
-IO::deliver_output_no_pan (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset)
+IO::deliver_output_no_pan (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset)
 {
        /* io_lock, not taken: function must be called from Session::process() calltree */
 
@@ -428,7 +432,7 @@ IO::deliver_output_no_pan (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_
        gain_t dg;
        gain_t old_gain = _gain;
 
-       if (apply_gain_automation) {
+       if (apply_gain_automation || _ignore_gain_on_deliver) {
 
                /* gain has already been applied by automation code. do nothing here except
                   speed quietning.
@@ -482,7 +486,7 @@ IO::deliver_output_no_pan (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_
                } else if (actual_gain == 0.0f) {
                        memset (dst, 0, sizeof (Sample) * nframes);
                } else {
-                       for (jack_nframes_t x = 0; x < nframes; ++x) {
+                       for (nframes_t x = 0; x < nframes; ++x) {
                                dst[x] = src[x] * actual_gain;
                        }
                }
@@ -495,13 +499,13 @@ IO::deliver_output_no_pan (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_
                _gain = dg;
        }
 
-       if (apply_gain_automation) {
+       if (apply_gain_automation || _ignore_gain_on_deliver) {
                _gain = old_gain;
        }
 }
 
 void
-IO::collect_input (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset)
+IO::collect_input (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset)
 {
        /* io_lock, not taken: function must be called from Session::process() calltree */
 
@@ -541,8 +545,8 @@ IO::collect_input (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframe
 }
 
 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)
 {
        vector<Sample*>& bufs = _session.get_passthru_buffers ();
        uint32_t nbufs = n_process_buffers ();
@@ -784,11 +788,20 @@ IO::remove_output_port (Port* port, void* src)
        return -1;
 }
 
+/** Add an output port.
+ *
+ * @param destination Name of input port to connect new port to.
+ * @param src Source for emitted ConfigurationChanged signal.
+ * @param type Data type of port.  Default value (NIL) will use this IO's default type.
+ */
 int
-IO::add_output_port (string destination, void* src)
+IO::add_output_port (string destination, void* src, DataType type)
 {
        Port* our_port;
-       char buf[64];
+       char name[64];
+
+       if (type == DataType::NIL)
+               type = _default_type;
 
        {
                Glib::Mutex::Lock em(_session.engine().process_lock());
@@ -802,14 +815,15 @@ IO::add_output_port (string destination, void* src)
                
                        /* Create a new output port */
                        
+                       // FIXME: naming scheme for differently typed ports?
                        if (_output_maximum == 1) {
-                               snprintf (buf, sizeof (buf), _("%s/out"), _name.c_str());
+                               snprintf (name, sizeof (name), _("%s/out"), _name.c_str());
                        } else {
-                               snprintf (buf, sizeof (buf), _("%s/out %u"), _name.c_str(), find_output_port_hole());
+                               snprintf (name, sizeof (name), _("%s/out %u"), _name.c_str(), find_output_port_hole());
                        }
                        
-                       if ((our_port = _session.engine().register_audio_output_port (buf)) == 0) {
-                               error << string_compose(_("IO: cannot register output port %1"), buf) << endmsg;
+                       if ((our_port = _session.engine().register_output_port (type, name)) == 0) {
+                               error << string_compose(_("IO: cannot register output port %1"), name) << endmsg;
                                return -1;
                        }
                        
@@ -885,11 +899,21 @@ IO::remove_input_port (Port* port, void* src)
        return -1;
 }
 
+
+/** Add an input port.
+ *
+ * @param type Data type of port.  The appropriate Jack port type, and @ref Port will be created.
+ * @param destination Name of input port to connect new port to.
+ * @param src Source for emitted ConfigurationChanged signal.
+ */
 int
-IO::add_input_port (string source, void* src)
+IO::add_input_port (string source, void* src, DataType type)
 {
        Port* our_port;
-       char buf[64];
+       char name[64];
+       
+       if (type == DataType::NIL)
+               type = _default_type;
 
        {
                Glib::Mutex::Lock em (_session.engine().process_lock());
@@ -903,14 +927,15 @@ IO::add_input_port (string source, void* src)
 
                        /* Create a new input port */
                        
+                       // FIXME: naming scheme for differently typed ports?
                        if (_input_maximum == 1) {
-                               snprintf (buf, sizeof (buf), _("%s/in"), _name.c_str());
+                               snprintf (name, sizeof (name), _("%s/in"), _name.c_str());
                        } else {
-                               snprintf (buf, sizeof (buf), _("%s/in %u"), _name.c_str(), find_input_port_hole());
+                               snprintf (name, sizeof (name), _("%s/in %u"), _name.c_str(), find_input_port_hole());
                        }
                        
-                       if ((our_port = _session.engine().register_audio_input_port (buf)) == 0) {
-                               error << string_compose(_("IO: cannot register input port %1"), buf) << endmsg;
+                       if ((our_port = _session.engine().register_input_port (type, name)) == 0) {
+                               error << string_compose(_("IO: cannot register input port %1"), name) << endmsg;
                                return -1;
                        }
                        
@@ -955,7 +980,7 @@ IO::disconnect_inputs (void* src)
                        drop_input_connection ();
                }
        }
-        input_changed (ConnectionsChanged, src); /* EMIT SIGNAL */
+       input_changed (ConnectionsChanged, src); /* EMIT SIGNAL */
        return 0;
 }
 
@@ -1004,7 +1029,7 @@ IO::ensure_inputs_locked (uint32_t n, bool clear, void* src)
                
                char buf[64];
                
-               /* Create a new input port */
+               /* Create a new input port (of the default type) */
                
                if (_input_maximum == 1) {
                        snprintf (buf, sizeof (buf), _("%s/in"), _name.c_str());
@@ -1015,7 +1040,7 @@ IO::ensure_inputs_locked (uint32_t n, bool clear, void* src)
                
                try {
                        
-                       if ((input_port = _session.engine().register_audio_input_port (buf)) == 0) {
+                       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;
                        }
@@ -1104,7 +1129,7 @@ IO::ensure_io (uint32_t nin, uint32_t nout, bool clear, void* src)
                        out_changed = true;
                }
                
-               /* create any necessary new ports */
+               /* create any necessary new ports (of the default type) */
                
                while (_ninputs < nin) {
                        
@@ -1120,7 +1145,7 @@ IO::ensure_io (uint32_t nin, uint32_t nout, bool clear, void* src)
                        }
                        
                        try {
-                               if ((port = _session.engine().register_audio_input_port (buf)) == 0) {
+                               if ((port = _session.engine().register_input_port (_default_type, buf)) == 0) {
                                        error << string_compose(_("IO: cannot register input port %1"), buf) << endmsg;
                                        return -1;
                                }
@@ -1153,7 +1178,7 @@ IO::ensure_io (uint32_t nin, uint32_t nout, bool clear, void* src)
                        }
                        
                        try { 
-                               if ((port = _session.engine().register_audio_output_port (buf)) == 0) {
+                               if ((port = _session.engine().register_output_port (_default_type, buf)) == 0) {
                                        error << string_compose(_("IO: cannot register output port %1"), buf) << endmsg;
                                        return -1;
                                }
@@ -1278,7 +1303,7 @@ IO::ensure_outputs_locked (uint32_t n, bool clear, void* src)
                        snprintf (buf, sizeof (buf), _("%s/out %u"), _name.c_str(), find_output_port_hole());
                }
                
-               if ((output_port = _session.engine().register_audio_output_port (buf)) == 0) {
+               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;
                }
@@ -1396,7 +1421,7 @@ XMLNode&
 IO::state (bool full_state)
 {
        XMLNode* node = new XMLNode (state_node_name);
-       char buf[32];
+       char buf[64];
        string str;
        bool need_ins = true;
        bool need_outs = true;
@@ -1404,7 +1429,7 @@ IO::state (bool full_state)
        Glib::Mutex::Lock lm (io_lock);
 
        node->add_property("name", _name);
-       snprintf (buf, sizeof(buf), "%" PRIu64, id());
+       id().print (buf, sizeof (buf));
        node->add_property("id", buf);
 
        str = "";
@@ -1486,6 +1511,7 @@ 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);
@@ -1498,22 +1524,6 @@ IO::state (bool full_state)
 
        node->add_property ("iolimits", buf);
 
-       /* MIDI control */
-
-       MIDI::channel_t chn;
-       MIDI::eventType ev;
-       MIDI::byte      additional;
-       XMLNode*        midi_node = 0;
-       XMLNode*        child;
-
-       if (_midi_gain_control.get_control_info (chn, ev, additional)) {
-
-               midi_node = node->add_child ("MIDI");
-
-               child = midi_node->add_child ("gain");
-               set_midi_node_info (child, ev, chn, additional);
-       }
-
        /* automation */
 
        if (full_state) {
@@ -1582,7 +1592,6 @@ IO::set_state (const XMLNode& node)
 {
        const XMLProperty* prop;
        XMLNodeConstIterator iter;
-       XMLNodeList midi_kids;
        LocaleGuard lg (X_("POSIX"));
 
        /* force use of non-localized representation of decimal point,
@@ -1600,7 +1609,7 @@ IO::set_state (const XMLNode& node)
        } 
 
        if ((prop = node.property ("id")) != 0) {
-               sscanf (prop->value().c_str(), "%" PRIu64, &_id);
+               _id = prop->value ();
        }
 
        if ((prop = node.property ("iolimits")) != 0) {
@@ -1617,40 +1626,17 @@ IO::set_state (const XMLNode& node)
        }
 
        for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
+
                if ((*iter)->name() == "Panner") {
                        _panner->set_state (**iter);
                }
-       }
-
-       midi_kids = node.children ("MIDI");
-       
-       for (iter = midi_kids.begin(); iter != midi_kids.end(); ++iter) {
-       
-               XMLNodeList kids;
-               XMLNodeConstIterator miter;
-               XMLNode*    child;
-
-               kids = (*iter)->children ();
-
-               for (miter = kids.begin(); miter != kids.end(); ++miter) {
 
-                       child =* miter;
-
-                       if (child->name() == "gain") {
-                       
-                               MIDI::eventType ev = MIDI::on; /* initialize to keep gcc happy */
-                               MIDI::byte additional = 0;  /* ditto */
-                               MIDI::channel_t chn = 0;    /* ditto */
-
-                               if (get_midi_node_info (child, ev, chn, additional)) {
-                                       _midi_gain_control.set_control_type (chn, ev, additional);
-                               } else {
-                                       error << string_compose(_("MIDI gain control specification for %1 is incomplete, so it has been ignored"), _name) << endmsg;
-                               }
-                       }
+               if ((*iter)->name() == X_("gaincontrol")) {
+                       _gain_control.set_state (**iter);
+                       _session.add_controllable (&_gain_control);
                }
        }
-                       
+
        if ((prop = node.property ("automation-state")) != 0) {
 
                long int x;
@@ -1768,50 +1754,6 @@ IO::create_ports (const XMLNode& node)
        return 0;
 }
 
-bool
-IO::get_midi_node_info (XMLNode * node, MIDI::eventType & ev, MIDI::channel_t & chan, MIDI::byte & additional)
-{
-       bool ok = true;
-       const XMLProperty* prop;
-       int xx;
-
-       if ((prop = node->property ("event")) != 0) {
-               sscanf (prop->value().c_str(), "0x%x", &xx);
-               ev = (MIDI::eventType) xx;
-       } else {
-               ok = false;
-       }
-
-       if (ok && ((prop = node->property ("channel")) != 0)) {
-               sscanf (prop->value().c_str(), "%d", &xx);
-               chan = (MIDI::channel_t) xx;
-       } else {
-               ok = false;
-       }
-
-       if (ok && ((prop = node->property ("additional")) != 0)) {
-               sscanf (prop->value().c_str(), "0x%x", &xx);
-               additional = (MIDI::byte) xx;
-       }
-
-       return ok;
-}
-
-bool
-IO::set_midi_node_info (XMLNode * node, MIDI::eventType ev, MIDI::channel_t chan, MIDI::byte additional)
-{
-       char buf[32];
-
-       snprintf (buf, sizeof(buf), "0x%x", ev);
-       node->add_property ("event", buf);
-       snprintf (buf, sizeof(buf), "%d", chan);
-       node->add_property ("channel", buf);
-       snprintf (buf, sizeof(buf), "0x%x", additional);
-       node->add_property ("additional", buf);
-
-       return true;
-}
-
 
 int
 IO::make_connections (const XMLNode& node)
@@ -2068,7 +2010,7 @@ IO::set_output_maximum (int n)
 }
 
 void
-IO::set_port_latency (jack_nframes_t nframes)
+IO::set_port_latency (nframes_t nframes)
 {
        Glib::Mutex::Lock lm (io_lock);
 
@@ -2077,11 +2019,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;
 
@@ -2096,11 +2038,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;
 
@@ -2338,69 +2280,16 @@ IO::output_connection_configuration_changed ()
        use_output_connection (*_output_connection, this);
 }
 
-IO::MIDIGainControl::MIDIGainControl (IO& i, MIDI::Port* port)
-       : MIDI::Controllable (port, 0), io (i), setting(false)
-{
-       midi_to_gain = 0;
-       gain_to_midi = 0;
-       setting = false;
-       last_written = 0; /* XXX need a good out-of-bound-value */
-}
-
 void
-IO::MIDIGainControl::set_value (float val)
+IO::GainControllable::set_value (float val)
 {
-       if (midi_to_gain == 0) return;
-       
-       setting = true;
-       io.set_gain (midi_to_gain (val), this);
-       setting = false;
-}
-
-void
-IO::MIDIGainControl::send_feedback (gain_t gain)
-{
-       if (!setting && get_midi_feedback() && gain_to_midi) {
-               MIDI::byte val = (MIDI::byte) (gain_to_midi (gain) * 127.0);
-               MIDI::channel_t ch = 0;
-               MIDI::eventType ev = MIDI::none;
-               MIDI::byte additional = 0;
-               MIDI::EventTwoBytes data;
-           
-               if (get_control_info (ch, ev, additional)) {
-                       data.controller_number = additional;
-                       data.value = val;
-                       last_written = val;
-                       
-                       io._session.send_midi_message (get_port(), ev, ch, data);
-               }
-               //send_midi_feedback (gain_to_midi (gain));
-       }
+       io.set_gain (direct_control_to_gain (val), this);
 }
 
-MIDI::byte*
-IO::MIDIGainControl::write_feedback (MIDI::byte* buf, int32_t& bufsize, gain_t val, bool force)
+float
+IO::GainControllable::get_value (void) const
 {
-       if (get_midi_feedback() && gain_to_midi && bufsize > 2) {
-               MIDI::channel_t ch = 0;
-               MIDI::eventType ev = MIDI::none;
-               MIDI::byte additional = 0;
-               MIDI::byte gm;
-
-               if (get_control_info (ch, ev, additional)) {
-                       gm = (MIDI::byte) (gain_to_midi (val) * 127.0);
-                       
-                       if (gm != last_written) {
-                               *buf++ = (0xF0 & ev) | (0xF & ch);
-                               *buf++ = additional; /* controller number */
-                               *buf++ = gm;
-                               last_written = gm;
-                               bufsize -= 3;
-                       }
-               }
-       }
-       
-       return buf;
+       return direct_gain_to_control (io.effective_gain());
 }
 
 void
@@ -2424,25 +2313,6 @@ IO::setup_peak_meters ()
        }
 }
 
-UndoAction
-IO::get_memento() const
-{
-  return sigc::bind (mem_fun (*(const_cast<IO *>(this)), &StateManager::use_state), _current_state_id);
-}
-
-Change
-IO::restore_state (StateManager::State& state)
-{
-       return Change (0);
-}
-
-StateManager::State*
-IO::state_factory (std::string why) const
-{
-       StateManager::State* state = new StateManager::State (why);
-       return state;
-}
-
 /**
     Update the peak meters.
 
@@ -2482,33 +2352,16 @@ IO::meter ()
                        new_peak = minus_infinity();
                }
                
-               if (_session.meter_falloff() == 0.0f || new_peak > _visible_peak_power[n]) {
+               if (Config->get_meter_falloff() == 0.0f || new_peak > _visible_peak_power[n]) {
                        _visible_peak_power[n] = new_peak;
                } else {
                        // do falloff
-                       new_peak = _visible_peak_power[n] - _session.meter_falloff();
+                       new_peak = _visible_peak_power[n] - Config->get_meter_falloff();
                        _visible_peak_power[n] = max (new_peak, -INFINITY);
                }
        }
 }
 
-void
-IO::reset_midi_control (MIDI::Port* port, bool on)
-{
-       MIDI::channel_t chn;
-       MIDI::eventType ev;
-       MIDI::byte extra;
-
-       _midi_gain_control.get_control_info (chn, ev, extra);
-       if (!on) {
-               chn = -1;
-       }
-       _midi_gain_control.midi_rebind (port, chn);
-       
-       _panner->reset_midi_control (port, on);
-}
-
-
 int
 IO::save_automation (const string& path)
 {
@@ -2530,7 +2383,7 @@ IO::save_automation (const string& path)
        /* 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;
+               out << "g " << (nframes_t) floor ((*i)->when) << ' ' << (*i)->value << endl;
        }
 
        _panner->save ();
@@ -2560,8 +2413,8 @@ IO::load_automation (const string& path)
                fullpath += path;
                in.open (fullpath.c_str());
                if (!in) {
-                               error << string_compose(_("%1: cannot open automation event file \"%2\""), _name, fullpath) << endmsg;
-                               return -1;
+                       error << string_compose(_("%1: cannot open automation event file \"%2\" (%2)"), _name, fullpath, strerror (errno)) << endmsg;
+                       return -1;
                }
        }
 
@@ -2569,7 +2422,7 @@ IO::load_automation (const string& path)
 
        while (in.getline (line, sizeof(line), '\n')) {
                char type;
-               jack_nframes_t when;
+               nframes_t when;
                double value;
 
                if (++linecnt == 1) {
@@ -2616,8 +2469,6 @@ IO::load_automation (const string& path)
                }
        }
 
-       _gain_automation_curve.save_state (_("loaded from disk"));
-
        return 0;
 }
        
@@ -2639,7 +2490,6 @@ IO::set_gain_automation_state (AutoState state)
 
                if (state != _gain_automation_curve.automation_state()) {
                        changed = true;
-                       last_automation_snapshot = 0;
                        _gain_automation_curve.set_automation_state (state);
                        
                        if (state != Off) {
@@ -2698,10 +2548,7 @@ IO::set_gain (gain_t val, void *src)
        }
 
        gain_changed (src);
-
-       if (_session.get_midi_feedback()) {
-               _midi_gain_control.send_feedback (_desired_gain);
-       }
+       _gain_control.Changed (); /* EMIT SIGNAL */
        
        if (_session.transport_stopped() && src != 0 && src != this && gain_automation_recording()) {
                _gain_automation_curve.add (_session.transport_frame(), val);
@@ -2711,30 +2558,6 @@ IO::set_gain (gain_t val, void *src)
        _session.set_dirty();
 }
 
-void
-IO::send_all_midi_feedback ()
-{
-       if (_session.get_midi_feedback()) {
-               _midi_gain_control.send_feedback (_effective_gain);
-
-               // panners
-               _panner->send_all_midi_feedback();
-       }
-}
-
-MIDI::byte*
-IO::write_midi_feedback (MIDI::byte* buf, int32_t& bufsize)
-{
-       if (_session.get_midi_feedback()) {
-               if (gain_automation_playback ()) {
-                       buf = _midi_gain_control.write_feedback (buf, bufsize, _effective_gain);
-               }
-               buf = _panner->write_midi_feedback (buf, bufsize);
-       }
-
-       return buf;
-}
-
 void
 IO::start_gain_touch ()
 {
@@ -2765,31 +2588,12 @@ IO::end_pan_touch (uint32_t which)
 }
 
 void
-IO::automation_snapshot (jack_nframes_t now)
-{
-       if (last_automation_snapshot > now || (now - last_automation_snapshot) > _automation_interval) {
-
-               if (gain_automation_recording()) {
-                       _gain_automation_curve.rt_add (now, gain());
-               }
-               
-               _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);
 
        if (_gain_automation_curve.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.
                */