remove StateManager code entirely and more debugging output cruft
[ardour.git] / libs / ardour / io.cc
index 4d2d26f8014e6a3590311c9a6a5a740e5331602b..323c58b65e1e83dbf6d5fcdae6ba87c6e73b3f38 100644 (file)
@@ -22,6 +22,7 @@
 #include <algorithm>
 #include <unistd.h>
 #include <locale.h>
+#include <errno.h>
 
 #include <sigc++/bind.h>
 
@@ -60,7 +61,6 @@ 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;
@@ -106,7 +106,7 @@ IO::IO (Session& s, string name,
        : _session (s),
          _name (name),
          _default_type(default_type),
-         _gain_control (*this),
+         _gain_control (X_("gaincontrol"), *this),
          _gain_automation_curve (0.0, 2.0, 1.0),
          _input_minimum (input_min),
          _input_maximum (input_max),
@@ -125,24 +125,24 @@ IO::IO (Session& s, string name,
        deferred_state = 0;
 
        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;
 
@@ -154,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 */
 
@@ -168,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)128, nframes);
+       nframes_t declick = min ((nframes_t)128, nframes);
        gain_t delta;
        Sample *buffer;
        double fractional_shift;
@@ -194,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;
                }
@@ -202,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;
                                }
                        }
                }
@@ -221,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;
 
@@ -268,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;
@@ -309,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];
                                }
                        }
@@ -324,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;
                                }       
                        }
@@ -375,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 */
 
@@ -418,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 */
 
@@ -429,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.
@@ -483,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;
                        }
                }
@@ -496,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 */
 
@@ -542,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 ();
@@ -977,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;
 }
 
@@ -1426,7 +1429,7 @@ 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 = "";
@@ -1508,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);
@@ -1622,9 +1626,15 @@ IO::set_state (const XMLNode& node)
        }
 
        for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
+
                if ((*iter)->name() == "Panner") {
                        _panner->set_state (**iter);
                }
+
+               if ((*iter)->name() == X_("gaincontrol")) {
+                       _gain_control.set_state (**iter);
+                       _session.add_controllable (&_gain_control);
+               }
        }
 
        if ((prop = node.property ("automation-state")) != 0) {
@@ -2000,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);
 
@@ -2009,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;
 
@@ -2028,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;
 
@@ -2303,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.
 
@@ -2361,11 +2352,11 @@ 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);
                }
        }
@@ -2392,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 ();
@@ -2422,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;
                }
        }
 
@@ -2431,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) {
@@ -2478,8 +2469,6 @@ IO::load_automation (const string& path)
                }
        }
 
-       _gain_automation_curve.save_state (_("loaded from disk"));
-
        return 0;
 }
        
@@ -2501,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) {
@@ -2600,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.
                */