Optimize automation-event process splitting
[ardour.git] / libs / ardour / monitor_processor.cc
index 70e4ed52c4584b3d623b3fd47c957c760e087686..56d8f879d62e4e9c9d0135e579c6b3b252921489 100644 (file)
@@ -17,9 +17,7 @@
 
 */
 
-#include "pbd/convert.h"
 #include "pbd/error.h"
-#include "pbd/locale_guard.h"
 #include "pbd/xml++.h"
 
 #include "ardour/amp.h"
@@ -28,7 +26,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 +35,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 +47,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,64 +110,58 @@ MonitorProcessor::set_state (const XMLNode& node, int version)
                 return ret;
         }
 
-        const XMLProperty* prop;
-
-        if ((prop = node.property (X_("type"))) == 0) {
+        std::string type_name;
+        if (!node.get_property (X_("type"), type_name)) {
                 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings have no type information"))
                       << endmsg;
                 return -1;
         }
 
-        if (prop->value() != X_("monitor")) {
+        if (type_name != X_("monitor")) {
                 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor given unknown XML settings"))
                       << endmsg;
                 return -1;
         }
 
-        if ((prop = node.property (X_("channels"))) == 0) {
+        uint32_t channels = 0;
+        if (!node.get_property (X_("channels"), channels)) {
                 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing a channel cnt"))
                       << endmsg;
                 return -1;
         }
 
-        allocate_channels (atoi (prop->value()));
+        allocate_channels (channels);
 
-        if ((prop = node.property (X_("dim-level"))) != 0) {
-                gain_t val = atof (prop->value());
+        // need to check that these conversions are working as expected
+        gain_t val;
+        if (node.get_property (X_("dim-level"), val)) {
                 _dim_level = val;
         }
 
-        if ((prop = node.property (X_("solo-boost-level"))) != 0) {
-                gain_t val = atof (prop->value());
+        if (node.get_property (X_("solo-boost-level"), val)) {
                 _solo_boost_level = val;
         }
 
-        if ((prop = node.property (X_("cut-all"))) != 0) {
-                bool val = string_is_affirmative (prop->value());
-                _cut_all = val;
+        bool bool_val;
+        if (node.get_property (X_("cut-all"), bool_val)) {
+                _cut_all = bool_val;
         }
-        if ((prop = node.property (X_("dim-all"))) != 0) {
-                bool val = string_is_affirmative (prop->value());
-                _dim_all = val;
+
+        if (node.get_property (X_("dim-all"), bool_val)) {
+                _dim_all = bool_val;
         }
-        if ((prop = node.property (X_("mono"))) != 0) {
-                bool val = string_is_affirmative (prop->value());
-                _mono = val;
+
+        if (node.get_property (X_("mono"), bool_val)) {
+                _mono = bool_val;
         }
 
         for (XMLNodeList::const_iterator i = node.children().begin(); i != node.children().end(); ++i) {
 
                 if ((*i)->name() == X_("Channel")) {
-                        if ((prop = (*i)->property (X_("id"))) == 0) {
-                                error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing an ID"))
-                                      << endmsg;
-                                return -1;
-                        }
 
                         uint32_t chn;
-
-                        if (sscanf (prop->value().c_str(), "%u", &chn) != 1) {
-                                error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an unreadable channel ID"))
+                        if (!(*i)->get_property (X_("id"), chn)) {
+                                error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing an ID"))
                                       << endmsg;
                                 return -1;
                         }
@@ -180,30 +173,32 @@ MonitorProcessor::set_state (const XMLNode& node, int version)
                         }
                         ChannelRecord& cr (*_channels[chn]);
 
-                        if ((prop = (*i)->property ("cut")) != 0) {
-                                if (string_is_affirmative (prop->value())){
+                        bool gain_coeff_zero;
+                        if ((*i)->get_property ("cut", gain_coeff_zero)) {
+                                if (gain_coeff_zero) {
                                         cr.cut = GAIN_COEFF_ZERO;
                                 } else {
                                         cr.cut = GAIN_COEFF_UNITY;
                                 }
                         }
 
-                        if ((prop = (*i)->property ("dim")) != 0) {
-                                bool val = string_is_affirmative (prop->value());
-                                cr.dim = val;
+                        bool dim;
+                        if ((*i)->get_property ("dim", dim)) {
+                                cr.dim = dim;
                         }
 
-                        if ((prop = (*i)->property ("invert")) != 0) {
-                                if (string_is_affirmative (prop->value())) {
+                        bool invert_polarity;
+                        if ((*i)->get_property ("invert", invert_polarity)) {
+                                if (invert_polarity) {
                                         cr.polarity = -1.0f;
                                 } else {
                                         cr.polarity = 1.0f;
                                 }
                         }
 
-                        if ((prop = (*i)->property ("solo")) != 0) {
-                                bool val = string_is_affirmative (prop->value());
-                                cr.soloed = val;
+                        bool soloed;
+                        if ((*i)->get_property ("solo", soloed)) {
+                                cr.soloed = soloed;
                         }
                 }
         }
@@ -218,57 +213,51 @@ MonitorProcessor::set_state (const XMLNode& node, int version)
                 }
         }
 
-        return 0;
+       update_monitor_state ();
+       return 0;
 }
 
 XMLNode&
-MonitorProcessor::state (bool full)
+MonitorProcessor::state ()
 {
-       LocaleGuard lg (X_("C"));
-        XMLNode& node (Processor::state (full));
-        char buf[64];
+       XMLNode& node(Processor::state ());
 
        /* this replaces any existing "type" property */
 
-       node.add_property (X_("type"), X_("monitor"));
-
-        snprintf (buf, sizeof(buf), "%.12g", _dim_level.val());
-        node.add_property (X_("dim-level"), buf);
+       node.set_property (X_("type"), X_("monitor"));
 
-        snprintf (buf, sizeof(buf), "%.12g", _solo_boost_level.val());
-        node.add_property (X_("solo-boost-level"), buf);
+       node.set_property (X_ ("dim-level"), (float)_dim_level.val ());
+       node.set_property (X_ ("solo-boost-level"), (float)_solo_boost_level.val ());
 
-        node.add_property (X_("cut-all"), (_cut_all ? "yes" : "no"));
-        node.add_property (X_("dim-all"), (_dim_all ? "yes" : "no"));
-        node.add_property (X_("mono"), (_mono ? "yes" : "no"));
+       node.set_property (X_("cut-all"), _cut_all.val());
+       node.set_property (X_("dim-all"), _dim_all.val());
+       node.set_property (X_("mono"), _mono.val());
 
-        uint32_t limit = _channels.size();
+       node.set_property (X_("channels"), (uint32_t)_channels.size ());
 
-        snprintf (buf, sizeof (buf), "%u", limit);
-        node.add_property (X_("channels"), buf);
+       XMLNode* chn_node;
+       uint32_t chn = 0;
 
-        XMLNode* chn_node;
-        uint32_t chn = 0;
+       for (vector<ChannelRecord*>::const_iterator x = _channels.begin (); x != _channels.end ();
+            ++x, ++chn) {
+               chn_node = new XMLNode (X_("Channel"));
 
-        for (vector<ChannelRecord*>::const_iterator x = _channels.begin(); x != _channels.end(); ++x, ++chn) {
-                chn_node = new XMLNode (X_("Channel"));
+               chn_node->set_property ("id", chn);
 
-                snprintf (buf, sizeof (buf), "%u", chn);
-                chn_node->add_property ("id", buf);
+               // implicitly cast these to bool
+               chn_node->set_property (X_("cut"), (*x)->cut != GAIN_COEFF_UNITY);
+               chn_node->set_property (X_("invert"), (*x)->polarity != GAIN_COEFF_UNITY);
+               chn_node->set_property (X_("dim"), (*x)->dim == true);
+               chn_node->set_property (X_("solo"), (*x)->soloed == true);
 
-                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");
+               node.add_child_nocopy (*chn_node);
+       }
 
-                node.add_child_nocopy (*chn_node);
-        }
-
-        return node;
+       return node;
 }
 
 void
-MonitorProcessor::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool /*result_required*/)
+MonitorProcessor::run (BufferSet& bufs, samplepos_t /*start_sample*/, samplepos_t /*end_sample*/, double /*speed*/, pframes_t nframes, bool /*result_required*/)
 {
         uint32_t chn = 0;
         gain_t target_gain;
@@ -288,7 +277,7 @@ MonitorProcessor::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /
                 /* don't double-scale by both track dim and global dim coefficients */
 
                 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 {
@@ -301,7 +290,7 @@ MonitorProcessor::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /
 
                 if (target_gain != _channels[chn]->current_gain || target_gain != GAIN_COEFF_UNITY) {
 
-                        _channels[chn]->current_gain = Amp::apply_gain (*b, _session.nominal_frame_rate(), nframes, _channels[chn]->current_gain, target_gain);
+                        _channels[chn]->current_gain = Amp::apply_gain (*b, _session.nominal_sample_rate(), nframes, _channels[chn]->current_gain, target_gain);
                 }
 
                 ++chn;
@@ -312,7 +301,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();
@@ -363,61 +352,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 = GAIN_COEFF_ZERO;
-        } else {
-                _channels[chn]->cut = GAIN_COEFF_UNITY;
-        }
+       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
@@ -470,6 +466,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
 {