Fix crash on relayering.
[ardour.git] / libs / ardour / meter.cc
index d8fee1ac475e2e663aa792be17b6acdfe9c3d59e..76d2be262c72c1696b645f3a579f0ff2a90d67bc 100644 (file)
 
 using namespace std;
 
-namespace ARDOUR {
+using namespace ARDOUR;
 
+sigc::signal<void> Metering::Meter;
+Glib::StaticMutex  Metering::m_meter_signal_lock;
+
+sigc::connection
+Metering::connect (sigc::slot<void> the_slot)
+{
+       // SignalProcessor::Meter is emitted from another thread so the
+       // Meter signal must be protected.
+       Glib::Mutex::Lock guard (m_meter_signal_lock);
+       return Meter.connect (the_slot);
+}
+
+void
+Metering::disconnect (sigc::connection& c)
+{
+       Glib::Mutex::Lock guard (m_meter_signal_lock);
+       c.disconnect ();
+}
+
+/**
+    Update the 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
+Metering::update_meters()
+{
+       Glib::Mutex::Lock guard (m_meter_signal_lock);
+       Meter(); /* EMIT SIGNAL */
+}
 
 /** Get peaks from @a bufs
  * Input acceptance is lenient - the first n buffers from @a bufs will
@@ -38,8 +71,12 @@ namespace ARDOUR {
  * be set to 0.
  */
 void
-PeakMeter::run_in_place (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes)
+PeakMeter::run (BufferSet& bufs, sframes_t /*start_frame*/, sframes_t /*end_frame*/, nframes_t nframes)
 {
+       if (!_active && !_pending_active) {
+               return;
+       }
+
        const uint32_t n_audio = min(_configured_input.n_audio(), bufs.count().n_audio());
        const uint32_t n_midi  = min(_configured_input.n_midi(), bufs.count().n_midi());
        
@@ -74,6 +111,13 @@ PeakMeter::run_in_place (BufferSet& bufs, nframes_t start_frame, nframes_t end_f
        for (uint32_t i = n; i < _peak_power.size(); ++i) {
                _peak_power[i] = 0.0f;
        }
+
+       _active = _pending_active;
+}
+
+PeakMeter::PeakMeter (Session& s, const XMLNode& node)
+       : Processor (s, node)
+{
 }
 
 void
@@ -128,28 +172,32 @@ PeakMeter::configure_io (ChanCount in, ChanCount out)
 }
 
 /** To be driven by the Meter signal from IO.
- * Caller MUST hold io_lock!
+ * Caller MUST hold its own processor_lock to prevent reconfiguration
+ * of meter size during this call.
  */
+
 void
 PeakMeter::meter ()
 {
+       if (!_active) {
+               return;
+       }
+
        assert(_visible_peak_power.size() == _peak_power.size());
 
        const size_t limit = _peak_power.size();
 
        for (size_t n = 0; n < limit; ++n) {
 
-               /* XXX we should use atomic exchange here */
-
                /* grab peak since last read */
 
-               float new_peak = _peak_power[n];
-               _peak_power[n] = 0;
-               
+               float new_peak = _peak_power[n]; /* XXX we should use atomic exchange from here ... */
+               _peak_power[n] = 0;              /* ... to here */
+
                /* compute new visible value using falloff */
 
                if (new_peak > 0.0) {
-                       new_peak = coefficient_to_dB (new_peak);
+                       new_peak = fast_coefficient_to_dB (new_peak);
                } else {
                        new_peak = minus_infinity();
                }
@@ -171,15 +219,14 @@ PeakMeter::meter ()
 XMLNode&
 PeakMeter::state (bool full_state)
 {
-       return get_state();
+       XMLNode& node (Processor::state (full_state));
+       node.add_property("type", "meter");
+       return node;
 }
 
-XMLNode&
-PeakMeter::get_state()
+
+bool
+PeakMeter::visible() const
 {
-       XMLNode* node = new XMLNode(state_node_name);
-       node->add_property("type", "meter");
-       return *node;
+       return true;
 }
-
-} // namespace ARDOUR