Heavy-duty abstraction work to split type-specific classes into
[ardour.git] / libs / ardour / io.cc
index 6b6773c49d85139c49761e06486eba12c9624dab..a70bf8abd32f28c7f33bd5568aa831c0999d9307 100644 (file)
@@ -25,7 +25,8 @@
 
 #include <sigc++/bind.h>
 
-#include <pbd/lockmonitor.h>
+#include <glibmm/thread.h>
+
 #include <pbd/xml++.h>
 
 #include <ardour/audioengine.h>
@@ -54,7 +55,8 @@ 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;
 
@@ -63,24 +65,26 @@ const string IO::state_node_name = "IO";
 bool         IO::connecting_legal = false;
 bool         IO::ports_legal = false;
 bool         IO::panners_legal = false;
-sigc::signal<void>                IO::GrabPeakPower;
+sigc::signal<void>                IO::Meter;
 sigc::signal<int>                 IO::ConnectingLegal;
 sigc::signal<int>                 IO::PortsLegal;
 sigc::signal<int>                 IO::PannersLegal;
 sigc::signal<void,uint32_t>  IO::MoreOutputs;
 sigc::signal<int>                 IO::PortsCreated;
 
-/* this is a default mapper of MIDI control values to a gain coefficient.
-   others can be imagined. see IO::set_midi_to_gain_function().
+Glib::StaticMutex       IO::m_meter_signal_lock = GLIBMM_STATIC_MUTEX_INIT;
+
+/* this is a default mapper of [0 .. 1.0] control values to a gain coefficient.
+   others can be imagined. 
 */
 
-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;
        
@@ -93,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 (*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;
@@ -117,22 +124,26 @@ 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;
 
        _gain_automation_state = Off;
        _gain_automation_style = Absolute;
-
-       GrabPeakPower.connect (mem_fun (*this, &IO::grab_peak_power));
+    
+    {
+        // 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 ()
 {
-       LockMonitor lm (io_lock, __LINE__, __FILE__);
+
+    Glib::Mutex::Lock guard (m_meter_signal_lock);
+       Glib::Mutex::Lock lm (io_lock);
        vector<Port *>::iterator i;
 
        for (i = _inputs.begin(); i != _inputs.end(); ++i) {
@@ -142,6 +153,8 @@ IO::~IO ()
        for (i = _outputs.begin(); i != _outputs.end(); ++i) {
                _session.engine().unregister_port (*i);
        }
+
+    m_meter_connection.disconnect();
 }
 
 void
@@ -162,7 +175,10 @@ IO::apply_declick (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframe
        Sample *buffer;
        double fractional_shift;
        double fractional_pos;
+       gain_t polscale = invert_polarity ? -1.0f : 1.0f;
 
+       if (nframes == 0) return;
+       
        fractional_shift = -1.0/declick;
 
        if (target < initial) {
@@ -178,16 +194,9 @@ IO::apply_declick (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframe
                buffer = bufs[n];
                fractional_pos = 1.0;
 
-               if (invert_polarity) {
-                       for (jack_nframes_t nx = 0; nx < declick; ++nx) {
-                               buffer[nx] *= -(initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
-                               fractional_pos += fractional_shift;
-                       }
-               } else {
-                       for (jack_nframes_t nx = 0; nx < declick; ++nx) {
-                               buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
-                               fractional_pos += fractional_shift;
-                       }
+               for (jack_nframes_t nx = 0; nx < declick; ++nx) {
+                       buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
+                       fractional_pos += fractional_shift;
                }
                
                /* now ensure the rest of the buffer has the target value
@@ -374,16 +383,17 @@ IO::deliver_output (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nfram
                return;
        }
        
-       if (_panner->bypassed()) {
+       if (_panner->bypassed() || _panner->empty()) {
                deliver_output_no_pan (bufs, nbufs, nframes, offset);
                return;
        }
 
 
        gain_t dg;
-
+       gain_t pangain = _gain;
+       
        {
-               TentativeLockMonitor dm (declick_lock, __LINE__, __FILE__);
+               Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
                
                if (dm.locked()) {
                        dg = _desired_gain;
@@ -395,14 +405,15 @@ IO::deliver_output (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nfram
        if (dg != _gain) {
                apply_declick (bufs, nbufs, nframes, _gain, dg, false);
                _gain = dg;
+               pangain = 1.0f;
        } 
 
        /* simple, non-automation panning to outputs */
 
        if (_session.transport_speed() > 1.5f || _session.transport_speed() < -1.5f) {
-               pan (bufs, nbufs, nframes, offset, _gain * speed_quietning);
+               pan (bufs, nbufs, nframes, offset, pangain * speed_quietning);
        } else {
-               pan (bufs, nbufs, nframes, offset, _gain);
+               pan (bufs, nbufs, nframes, offset, pangain);
        }
 }
 
@@ -429,7 +440,7 @@ IO::deliver_output_no_pan (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_
                
        } else {
 
-               TentativeLockMonitor dm (declick_lock, __LINE__, __FILE__);
+               Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
                
                if (dm.locked()) {
                        dg = _desired_gain;
@@ -570,10 +581,10 @@ IO::disconnect_input (Port* our_port, string other_port, void* src)
        }
 
        { 
-               LockMonitor em (_session.engine().process_lock(), __LINE__, __FILE__);
+               Glib::Mutex::Lock em (_session.engine().process_lock());
                
                {
-                       LockMonitor lm (io_lock, __LINE__, __FILE__);
+                       Glib::Mutex::Lock lm (io_lock);
                        
                        /* check that our_port is really one of ours */
                        
@@ -606,10 +617,10 @@ IO::connect_input (Port* our_port, string other_port, void* src)
        }
 
        {
-               LockMonitor em(_session.engine().process_lock(), __LINE__, __FILE__);
+               Glib::Mutex::Lock em(_session.engine().process_lock());
                
                {
-                       LockMonitor lm (io_lock, __LINE__, __FILE__);
+                       Glib::Mutex::Lock lm (io_lock);
                        
                        /* check that our_port is really one of ours */
                        
@@ -640,10 +651,10 @@ IO::disconnect_output (Port* our_port, string other_port, void* src)
        }
 
        {
-               LockMonitor em(_session.engine().process_lock(), __LINE__, __FILE__);
+               Glib::Mutex::Lock em(_session.engine().process_lock());
                
                {
-                       LockMonitor lm (io_lock, __LINE__, __FILE__);
+                       Glib::Mutex::Lock lm (io_lock);
                        
                        if (find (_outputs.begin(), _outputs.end(), our_port) == _outputs.end()) {
                                return -1;
@@ -673,10 +684,10 @@ IO::connect_output (Port* our_port, string other_port, void* src)
        }
 
        {
-               LockMonitor em(_session.engine().process_lock(), __LINE__, __FILE__);
+               Glib::Mutex::Lock em(_session.engine().process_lock());
                
                {
-                       LockMonitor lm (io_lock, __LINE__, __FILE__);
+                       Glib::Mutex::Lock lm (io_lock);
                        
                        /* check that our_port is really one of ours */
                        
@@ -732,10 +743,10 @@ IO::remove_output_port (Port* port, void* src)
        IOChange change (NoChange);
 
        {
-               LockMonitor em(_session.engine().process_lock(), __LINE__, __FILE__);
+               Glib::Mutex::Lock em(_session.engine().process_lock());
                
                {
-                       LockMonitor lm (io_lock, __LINE__, __FILE__);
+                       Glib::Mutex::Lock lm (io_lock);
                        
                        if (_noutputs - 1 == (uint32_t) _output_minimum) {
                                /* sorry, you can't do this */
@@ -774,17 +785,26 @@ 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 == NIL)
+               type = _default_type;
 
        {
-               LockMonitor em(_session.engine().process_lock(), __LINE__, __FILE__);
+               Glib::Mutex::Lock em(_session.engine().process_lock());
                
                { 
-                       LockMonitor lm (io_lock, __LINE__, __FILE__);
+                       Glib::Mutex::Lock lm (io_lock);
                        
                        if (_output_maximum >= 0 && (int) _noutputs == _output_maximum) {
                                return -1;
@@ -792,14 +812,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;
                        }
                        
@@ -832,10 +853,10 @@ IO::remove_input_port (Port* port, void* src)
        IOChange change (NoChange);
 
        {
-               LockMonitor em(_session.engine().process_lock(), __LINE__, __FILE__);
+               Glib::Mutex::Lock em(_session.engine().process_lock());
                
                {
-                       LockMonitor lm (io_lock, __LINE__, __FILE__);
+                       Glib::Mutex::Lock lm (io_lock);
 
                        if (((int)_ninputs - 1) < _input_minimum) {
                                /* sorry, you can't do this */
@@ -875,17 +896,27 @@ 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 == NIL)
+               type = _default_type;
 
        {
-               LockMonitor em (_session.engine().process_lock(), __LINE__, __FILE__);
+               Glib::Mutex::Lock em (_session.engine().process_lock());
                
                { 
-                       LockMonitor lm (io_lock, __LINE__, __FILE__);
+                       Glib::Mutex::Lock lm (io_lock);
                        
                        if (_input_maximum >= 0 && (int) _ninputs == _input_maximum) {
                                return -1;
@@ -893,14 +924,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;
                        }
                        
@@ -933,10 +965,10 @@ int
 IO::disconnect_inputs (void* src)
 {
        { 
-               LockMonitor em (_session.engine().process_lock(), __LINE__, __FILE__);
+               Glib::Mutex::Lock em (_session.engine().process_lock());
                
                {
-                       LockMonitor lm (io_lock, __LINE__, __FILE__);
+                       Glib::Mutex::Lock lm (io_lock);
                        
                        for (vector<Port *>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
                                _session.engine().disconnect (*i);
@@ -953,10 +985,10 @@ int
 IO::disconnect_outputs (void* src)
 {
        {
-               LockMonitor em (_session.engine().process_lock(), __LINE__, __FILE__);
+               Glib::Mutex::Lock em (_session.engine().process_lock());
                
                {
-                       LockMonitor lm (io_lock, __LINE__, __FILE__);
+                       Glib::Mutex::Lock lm (io_lock);
                        
                        for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
                                _session.engine().disconnect (*i);
@@ -994,7 +1026,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());
@@ -1005,7 +1037,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;
                        }
@@ -1065,8 +1097,8 @@ IO::ensure_io (uint32_t nin, uint32_t nout, bool clear, void* src)
        }
 
        {
-               LockMonitor em (_session.engine().process_lock(), __LINE__, __FILE__);
-               LockMonitor lm (io_lock, __LINE__, __FILE__);
+               Glib::Mutex::Lock em (_session.engine().process_lock());
+               Glib::Mutex::Lock lm (io_lock);
 
                Port* port;
                
@@ -1094,7 +1126,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) {
                        
@@ -1110,7 +1142,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;
                                }
@@ -1143,7 +1175,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;
                                }
@@ -1173,11 +1205,11 @@ IO::ensure_io (uint32_t nin, uint32_t nout, bool clear, void* src)
                                _session.engine().disconnect (*i);
                        }
                }
-       }
-
-       if (in_changed || out_changed) {
-               setup_peak_meters ();
-               reset_panner ();
+               
+               if (in_changed || out_changed) {
+                       setup_peak_meters ();
+                       reset_panner ();
+               }
        }
 
        if (out_changed) {
@@ -1214,7 +1246,8 @@ IO::ensure_inputs (uint32_t n, bool clear, bool lockit, void* src)
        }
        
        if (lockit) {
-               LockMonitor em (_session.engine().process_lock(), __LINE__, __FILE__);
+               Glib::Mutex::Lock em (_session.engine().process_lock());
+               Glib::Mutex::Lock im (io_lock);
                changed = ensure_inputs_locked (n, clear, src);
        } else {
                changed = ensure_inputs_locked (n, clear, src);
@@ -1267,7 +1300,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;
                }
@@ -1315,7 +1348,8 @@ IO::ensure_outputs (uint32_t n, bool clear, bool lockit, void* src)
        /* XXX caller should hold io_lock, but generally doesn't */
 
        if (lockit) {
-               LockMonitor em (_session.engine().process_lock(), __LINE__, __FILE__);
+               Glib::Mutex::Lock em (_session.engine().process_lock());
+               Glib::Mutex::Lock im (io_lock);
                changed = ensure_outputs_locked (n, clear, src);
        } else {
                changed = ensure_outputs_locked (n, clear, src);
@@ -1384,15 +1418,15 @@ 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;
        LocaleGuard lg (X_("POSIX"));
-       LockMonitor lm (io_lock, __LINE__, __FILE__);
+       Glib::Mutex::Lock lm (io_lock);
 
        node->add_property("name", _name);
-       snprintf (buf, sizeof(buf), "%" PRIu64, id());
+       id().print (buf);
        node->add_property("id", buf);
 
        str = "";
@@ -1486,22 +1520,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) {
@@ -1570,7 +1588,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,
@@ -1588,7 +1605,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) {
@@ -1610,35 +1627,6 @@ IO::set_state (const XMLNode& node)
                }
        }
 
-       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 ((prop = node.property ("automation-state")) != 0) {
 
                long int x;
@@ -1756,50 +1744,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)
@@ -2058,7 +2002,7 @@ IO::set_output_maximum (int n)
 void
 IO::set_port_latency (jack_nframes_t nframes)
 {
-       LockMonitor lm (io_lock, __LINE__, __FILE__);
+       Glib::Mutex::Lock lm (io_lock);
 
        for (vector<Port *>::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
                (*i)->set_latency (nframes);
@@ -2109,8 +2053,8 @@ IO::use_input_connection (Connection& c, void* src)
        uint32_t limit;
 
        {
-               LockMonitor lm (_session.engine().process_lock(), __LINE__, __FILE__);
-               LockMonitor lm2 (io_lock, __LINE__, __FILE__);
+               Glib::Mutex::Lock lm (_session.engine().process_lock());
+               Glib::Mutex::Lock lm2 (io_lock);
                
                limit = c.nports();
                
@@ -2187,8 +2131,8 @@ IO::use_output_connection (Connection& c, void* src)
        uint32_t limit; 
 
        {
-               LockMonitor lm (_session.engine().process_lock(), __LINE__, __FILE__);
-               LockMonitor lm2 (io_lock, __LINE__, __FILE__);
+               Glib::Mutex::Lock lm (_session.engine().process_lock());
+               Glib::Mutex::Lock lm2 (io_lock);
 
                limit = c.nports();
                        
@@ -2326,69 +2270,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;
+       io.set_gain (direct_control_to_gain (val), this);
 }
 
-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));
-       }
-}
-
-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
@@ -2408,7 +2299,7 @@ IO::setup_peak_meters ()
 
        while (_peak_power.size() < limit) {
                _peak_power.push_back (0);
-               _stored_peak_power.push_back (0);
+               _visible_peak_power.push_back (0);
        }
 }
 
@@ -2431,43 +2322,55 @@ IO::state_factory (std::string why) const
        return state;
 }
 
+/**
+    Update the peak meters.
+
+    The meter signal lock is taken to prevent modification of the 
+    Meter signal while updating the meters, taking the meter signal
+    lock prior to taking the io_lock ensures that all IO will remain 
+    valid while metering.
+*/   
 void
-IO::send_state_changed ()
+IO::update_meters()
 {
-       return;
+    Glib::Mutex::Lock guard (m_meter_signal_lock);
+    
+    Meter();
 }
 
 void
-IO::grab_peak_power ()
+IO::meter ()
 {
-       LockMonitor lm (io_lock, __LINE__, __FILE__);
-
+       Glib::Mutex::Lock lm (io_lock); // READER: meter thread.
        uint32_t limit = max (_ninputs, _noutputs);
-
+       
        for (uint32_t n = 0; n < limit; ++n) {
-               /* XXX should we use atomic exchange here ? */
-               _stored_peak_power[n] = _peak_power[n];
-               _peak_power[n] = 0;
-       }
-}
 
-void
-IO::reset_midi_control (MIDI::Port* port, bool on)
-{
-       MIDI::channel_t chn;
-       MIDI::eventType ev;
-       MIDI::byte extra;
+               /* XXX we should use atomic exchange here */
+
+               /* grab peak since last read */
+
+               float new_peak = _peak_power[n];
+               _peak_power[n] = 0;
+               
+               /* compute new visible value using falloff */
 
-       _midi_gain_control.get_control_info (chn, ev, extra);
-       if (!on) {
-               chn = -1;
+               if (new_peak > 0.0) {
+                       new_peak = coefficient_to_dB (new_peak);
+               } else {
+                       new_peak = minus_infinity();
+               }
+               
+               if (_session.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();
+                       _visible_peak_power[n] = max (new_peak, -INFINITY);
+               }
        }
-       _midi_gain_control.midi_rebind (port, chn);
-       
-       _panner->reset_midi_control (port, on);
 }
 
-
 int
 IO::save_automation (const string& path)
 {
@@ -2583,7 +2486,7 @@ IO::load_automation (const string& path)
 void
 IO::clear_automation ()
 {
-       LockMonitor lm (automation_lock, __LINE__, __FILE__);
+       Glib::Mutex::Lock lm (automation_lock);
        _gain_automation_curve.clear ();
        _panner->clear_automation ();
 }
@@ -2594,7 +2497,7 @@ IO::set_gain_automation_state (AutoState state)
        bool changed = false;
 
        {
-               LockMonitor lm (automation_lock, __LINE__, __FILE__);
+               Glib::Mutex::Lock lm (automation_lock);
 
                if (state != _gain_automation_curve.automation_state()) {
                        changed = true;
@@ -2619,7 +2522,7 @@ IO::set_gain_automation_style (AutoStyle style)
        bool changed = false;
 
        {
-               LockMonitor lm (automation_lock, __LINE__, __FILE__);
+               Glib::Mutex::Lock lm (automation_lock);
 
                if (style != _gain_automation_curve.automation_style()) {
                        changed = true;
@@ -2647,7 +2550,7 @@ IO::set_gain (gain_t val, void *src)
        if (val>1.99526231f) val=1.99526231f;
 
        {
-               LockMonitor dm (declick_lock, __LINE__, __FILE__);
+               Glib::Mutex::Lock dm (declick_lock);
                _desired_gain = val;
        }
 
@@ -2657,10 +2560,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);
@@ -2670,30 +2570,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 ()
 {