fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / monitor_processor.cc
index ed0664786034059ed8c417590d78dc2645f41cdf..811d5a15b34effa8d61a8fc46d5df4dfc659355d 100644 (file)
@@ -28,7 +28,7 @@
 #include "ardour/monitor_processor.h"
 #include "ardour/session.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace ARDOUR;
 using namespace PBD;
@@ -37,11 +37,11 @@ using namespace std;
 /* specialize for bool because of set_value() semantics */
 
 namespace ARDOUR {
-        template<> void MPControl<bool>::set_value (double v) {
+       template<> void MPControl<bool>::set_value (double v, PBD::Controllable::GroupControlDisposition gcd) {
                 bool newval = fabs (v) >= 0.5;
                 if (newval != _value) {
                         _value = newval;
-                        Changed(); /* EMIT SIGNAL */
+                        Changed (true, gcd); /* EMIT SIGNAL */
                 }
         }
 }
@@ -49,17 +49,18 @@ namespace ARDOUR {
 MonitorProcessor::MonitorProcessor (Session& s)
         : Processor (s, X_("MonitorOut"))
         , solo_cnt (0)
+        , _monitor_active (false)
 
         , _dim_all_ptr (new MPControl<bool> (false, _("monitor dim"), Controllable::Toggle))
         , _cut_all_ptr (new MPControl<bool> (false, _("monitor cut"), Controllable::Toggle))
         , _mono_ptr (new MPControl<bool> (false, _("monitor mono"), Controllable::Toggle))
-        , _dim_level_ptr (new MPControl<volatile gain_t> 
+        , _dim_level_ptr (new MPControl<volatile gain_t>
                          /* default is -12dB, range is -20dB to 0dB */
-                          (dB_to_coefficient(-12.0), _("monitor dim level"), Controllable::Flag (0), 
+                          (dB_to_coefficient(-12.0), _("monitor dim level"), Controllable::Flag (0),
                           dB_to_coefficient(-20.0), dB_to_coefficient (0.0)))
-        , _solo_boost_level_ptr (new MPControl<volatile gain_t> 
+        , _solo_boost_level_ptr (new MPControl<volatile gain_t>
                                 /* default is 0dB, range is 0dB to +20dB */
-                                 (dB_to_coefficient(0.0), _("monitor solo boost level"), Controllable::Flag (0), 
+                                 (dB_to_coefficient(0.0), _("monitor solo boost level"), Controllable::Flag (0),
                                  dB_to_coefficient(0.0), dB_to_coefficient(10.0)))
         , _dim_all_control (_dim_all_ptr)
         , _cut_all_control (_cut_all_ptr)
@@ -111,7 +112,7 @@ MonitorProcessor::set_state (const XMLNode& node, int version)
                 return ret;
         }
 
-        const XMLProperty* prop;
+        XMLProperty const * prop;
 
         if ((prop = node.property (X_("type"))) == 0) {
                 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings have no type information"))
@@ -182,9 +183,9 @@ MonitorProcessor::set_state (const XMLNode& node, int version)
 
                         if ((prop = (*i)->property ("cut")) != 0) {
                                 if (string_is_affirmative (prop->value())){
-                                        cr.cut = 0.0f;
+                                        cr.cut = GAIN_COEFF_ZERO;
                                 } else {
-                                        cr.cut = 1.0f;
+                                        cr.cut = GAIN_COEFF_UNITY;
                                 }
                         }
 
@@ -218,13 +219,14 @@ MonitorProcessor::set_state (const XMLNode& node, int version)
                 }
         }
 
-        return 0;
+       update_monitor_state ();
+       return 0;
 }
 
 XMLNode&
 MonitorProcessor::state (bool full)
 {
-       LocaleGuard lg (X_("POSIX"));
+       LocaleGuard lg;
         XMLNode& node (Processor::state (full));
         char buf[64];
 
@@ -256,8 +258,8 @@ MonitorProcessor::state (bool full)
                 snprintf (buf, sizeof (buf), "%u", chn);
                 chn_node->add_property ("id", buf);
 
-                chn_node->add_property (X_("cut"), (*x)->cut == 1.0f ? "no" : "yes");
-                chn_node->add_property (X_("invert"), (*x)->polarity == 1.0f ? "no" : "yes");
+                chn_node->add_property (X_("cut"), (*x)->cut == GAIN_COEFF_UNITY ? "no" : "yes");
+                chn_node->add_property (X_("invert"), (*x)->polarity == GAIN_COEFF_UNITY ? "no" : "yes");
                 chn_node->add_property (X_("dim"), (*x)->dim ? "yes" : "no");
                 chn_node->add_property (X_("solo"), (*x)->soloed ? "yes" : "no");
 
@@ -268,41 +270,40 @@ MonitorProcessor::state (bool full)
 }
 
 void
-MonitorProcessor::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool /*result_required*/)
+MonitorProcessor::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, double /*speed*/, pframes_t nframes, bool /*result_required*/)
 {
         uint32_t chn = 0;
         gain_t target_gain;
         gain_t dim_level_this_time = _dim_level;
-        gain_t global_cut = (_cut_all ? 0.0f : 1.0f);
-        gain_t global_dim = (_dim_all ? dim_level_this_time : 1.0);
+        gain_t global_cut = (_cut_all ? GAIN_COEFF_ZERO : GAIN_COEFF_UNITY);
+        gain_t global_dim = (_dim_all ? dim_level_this_time : GAIN_COEFF_UNITY);
         gain_t solo_boost;
 
         if (_session.listening() || _session.soloing()) {
                 solo_boost = _solo_boost_level;
         } else {
-                solo_boost = 1.0;
+                solo_boost = GAIN_COEFF_UNITY;
         }
 
         for (BufferSet::audio_iterator b = bufs.audio_begin(); b != bufs.audio_end(); ++b) {
 
                 /* don't double-scale by both track dim and global dim coefficients */
 
-                gain_t dim_level = (global_dim == 1.0 ? (_channels[chn]->dim ? dim_level_this_time : 1.0) : 1.0);
-               
+                gain_t dim_level = (global_dim == GAIN_COEFF_UNITY ? (_channels[chn]->dim ? dim_level_this_time : GAIN_COEFF_UNITY) : GAIN_COEFF_UNITY);
+
                 if (_channels[chn]->soloed) {
                         target_gain = _channels[chn]->polarity * _channels[chn]->cut * dim_level * global_cut * global_dim * solo_boost;
                 } else {
                         if (solo_cnt == 0) {
                                 target_gain = _channels[chn]->polarity * _channels[chn]->cut * dim_level * global_cut * global_dim * solo_boost;
                         } else {
-                                target_gain = 0.0;
+                                target_gain = GAIN_COEFF_ZERO;
                         }
                 }
 
-                if (target_gain != _channels[chn]->current_gain || target_gain != 1.0f) {
+                if (target_gain != _channels[chn]->current_gain || target_gain != GAIN_COEFF_UNITY) {
 
-                        Amp::apply_gain (*b, nframes, _channels[chn]->current_gain, target_gain);
-                        _channels[chn]->current_gain = target_gain;
+                        _channels[chn]->current_gain = Amp::apply_gain (*b, _session.nominal_frame_rate(), nframes, _channels[chn]->current_gain, target_gain);
                 }
 
                 ++chn;
@@ -313,7 +314,7 @@ MonitorProcessor::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /
 
                 /* chn is now the number of channels, use as a scaling factor when mixing
                  */
-                gain_t scale = 1.0/chn;
+                gain_t scale = 1.f / (float)chn;
                 BufferSet::audio_iterator b = bufs.audio_begin();
                 AudioBuffer& ab (*b);
                 Sample* buf = ab.data();
@@ -364,61 +365,68 @@ MonitorProcessor::can_support_io_configuration (const ChanCount& in, ChanCount&
 void
 MonitorProcessor::set_polarity (uint32_t chn, bool invert)
 {
-        if (invert) {
-                _channels[chn]->polarity = -1.0f;
-        } else {
-                _channels[chn]->polarity = 1.0f;
-        }
+       if (invert) {
+               _channels[chn]->polarity = -1.0f;
+       } else {
+               _channels[chn]->polarity = 1.0f;
+       }
+       update_monitor_state ();
 }
 
 void
 MonitorProcessor::set_dim (uint32_t chn, bool yn)
 {
-        _channels[chn]->dim = yn;
+       _channels[chn]->dim = yn;
+       update_monitor_state ();
 }
 
 void
 MonitorProcessor::set_cut (uint32_t chn, bool yn)
 {
-        if (yn) {
-                _channels[chn]->cut = 0.0f;
-        } else {
-                _channels[chn]->cut = 1.0f;
-        }
+       if (yn) {
+               _channels[chn]->cut = GAIN_COEFF_ZERO;
+       } else {
+               _channels[chn]->cut = GAIN_COEFF_UNITY;
+       }
+       update_monitor_state ();
 }
 
 void
 MonitorProcessor::set_solo (uint32_t chn, bool solo)
 {
-        if (solo != _channels[chn]->soloed) {
-                _channels[chn]->soloed = solo;
-
-                if (solo) {
-                        solo_cnt++;
-                } else {
-                        if (solo_cnt > 0) {
-                                solo_cnt--;
-                        }
-                }
-        }
+       if (solo != _channels[chn]->soloed) {
+               _channels[chn]->soloed = solo;
+
+               if (solo) {
+                       solo_cnt++;
+               } else {
+                       if (solo_cnt > 0) {
+                               solo_cnt--;
+                       }
+               }
+       }
+       update_monitor_state ();
 }
 
 void
 MonitorProcessor::set_mono (bool yn)
 {
-        _mono = yn;
+       _mono = yn;
+       update_monitor_state ();
 }
 
 void
 MonitorProcessor::set_cut_all (bool yn)
 {
-        _cut_all = yn;
+       _cut_all = yn;
+       update_monitor_state ();
 }
 
 void
 MonitorProcessor::set_dim_all (bool yn)
 {
-        _dim_all = yn;
+       _dim_all = yn;
+       update_monitor_state ();
 }
 
 bool
@@ -444,7 +452,7 @@ MonitorProcessor::inverted (uint32_t chn) const
 bool
 MonitorProcessor::cut (uint32_t chn) const
 {
-        return _channels[chn]->cut == 0.0f;
+        return _channels[chn]->cut == GAIN_COEFF_ZERO;
 }
 
 bool
@@ -471,6 +479,29 @@ MonitorProcessor::cut_all () const
         return _cut_all;
 }
 
+void
+MonitorProcessor::update_monitor_state ()
+{
+       bool en = false;
+
+       if (_cut_all || _dim_all || _mono) {
+               en = true;
+       }
+
+       const uint32_t nchans = _channels.size();
+       for (uint32_t i = 0; i < nchans && !en; ++i) {
+               if (cut (i) || dimmed (i) || soloed (i) || inverted (i)) {
+                       en = true;
+                       break;
+               }
+       }
+
+       if (_monitor_active != en) {
+               _monitor_active = en;
+               _session.MonitorChanged();
+       }
+}
+
 boost::shared_ptr<Controllable>
 MonitorProcessor::channel_cut_control (uint32_t chn) const
 {
@@ -508,7 +539,7 @@ MonitorProcessor::channel_solo_control (uint32_t chn) const
 }
 
 MonitorProcessor::ChannelRecord::ChannelRecord (uint32_t chn)
-       : current_gain (1.0)
+       : current_gain (GAIN_COEFF_UNITY)
        , cut_ptr (new MPControl<gain_t> (1.0, string_compose (_("cut control %1"), chn), PBD::Controllable::GainLike))
        , dim_ptr (new MPControl<bool> (false, string_compose (_("dim control"), chn), PBD::Controllable::Toggle))
        , polarity_ptr (new MPControl<gain_t> (1.0, string_compose (_("polarity control"), chn), PBD::Controllable::Toggle, -1, 1))