Optimize plugin-processing for non-automated params
authorRobin Gareus <robin@gareus.org>
Sun, 16 Dec 2018 03:01:42 +0000 (04:01 +0100)
committerRobin Gareus <robin@gareus.org>
Sun, 16 Dec 2018 03:25:20 +0000 (04:25 +0100)
Keep a dedicated list of automated parameters to evaluate in realtime.
This fixes a performance issue with plugins that have many controls
with only few of them being automated.

libs/ardour/ardour/automatable.h
libs/ardour/ardour/plugin_insert.h
libs/ardour/automatable.cc
libs/ardour/plugin_insert.cc

index c7db1c7a218021f94c92b66dd85f470fecbec614..fe14cacf7723e90121e8d886a50acef4fd0ffd77 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <boost/shared_ptr.hpp>
 
+#include "pbd/rcu.h"
 #include "pbd/signals.h"
 
 #include "evoral/ControlSet.hpp"
@@ -88,7 +89,7 @@ public:
        virtual void non_realtime_locate (samplepos_t now);
        virtual void non_realtime_transport_stop (samplepos_t now, bool flush);
 
-       virtual void automation_run (samplepos_t, pframes_t);
+       virtual void automation_run (samplepos_t, pframes_t, bool only_active = false);
 
        virtual std::string describe_parameter(Evoral::Parameter param);
 
@@ -112,7 +113,8 @@ protected:
 
        void can_automate(Evoral::Parameter);
 
-       virtual void automation_list_automation_state_changed (Evoral::Parameter, AutoState) {}
+       virtual void automation_list_automation_state_changed (Evoral::Parameter, AutoState);
+       SerializedRCUManager<ControlList> _automated_controls;
 
        int load_automation (const std::string& path);
        int old_set_automation_state(const XMLNode&);
index 30dd04057a2402f616bdecde7159661e0f09a6bf..64b2bd875d21353b069259071f4e3edc1575a13d 100644 (file)
@@ -79,7 +79,7 @@ public:
 
        bool write_immediate_event (size_t size, const uint8_t* buf);
 
-       void automation_run (samplepos_t, pframes_t);
+       void automation_run (samplepos_t, pframes_t, bool only_active = false);
        bool find_next_event (double, double, Evoral::ControlEvent&, bool only_active = true) const;
 
        int set_block_size (pframes_t nframes);
index fd7c0a9b01bc7d8b7c3c097b954e04cfba53a40f..51a58ce94293f10b4e00e60827c8556d35f7c061 100644 (file)
@@ -55,13 +55,15 @@ const string Automatable::xml_node_name = X_("Automation");
 
 Automatable::Automatable(Session& session)
        : _a_session(session)
+       , _automated_controls (new ControlList)
 {
 }
 
 Automatable::Automatable (const Automatable& other)
-        : ControlSet (other)
-        , Slavable ()
-        , _a_session (other._a_session)
+       : ControlSet (other)
+       , Slavable ()
+       , _a_session (other._a_session)
+       , _automated_controls (new ControlList)
 {
         Glib::Threads::Mutex::Lock lm (other._control_lock);
 
@@ -73,6 +75,13 @@ Automatable::Automatable (const Automatable& other)
 
 Automatable::~Automatable ()
 {
+       {
+               RCUWriter<ControlList> writer (_automated_controls);
+               boost::shared_ptr<ControlList> cl = writer.get_copy ();
+               cl->clear ();
+       }
+       _automated_controls.flush ();
+
        Glib::Threads::Mutex::Lock lm (_control_lock);
        for (Controls::const_iterator li = _controls.begin(); li != _controls.end(); ++li) {
                boost::dynamic_pointer_cast<AutomationControl>(li->second)->drop_references ();
@@ -439,8 +448,16 @@ Automatable::non_realtime_transport_stop (samplepos_t now, bool /*flush_processo
 }
 
 void
-Automatable::automation_run (samplepos_t start, pframes_t nframes)
+Automatable::automation_run (samplepos_t start, pframes_t nframes, bool only_active)
 {
+       if (only_active) {
+               boost::shared_ptr<ControlList> cl = _automated_controls.reader ();
+               for (ControlList::const_iterator ci = cl->begin(); ci != cl->end(); ++ci) {
+                       (*ci)->automation_run (start, nframes);
+               }
+               return;
+       }
+
        for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
                boost::shared_ptr<AutomationControl> c =
                        boost::dynamic_pointer_cast<AutomationControl>(li->second);
@@ -451,6 +468,35 @@ Automatable::automation_run (samplepos_t start, pframes_t nframes)
        }
 }
 
+void
+Automatable::automation_list_automation_state_changed (Evoral::Parameter param, AutoState as)
+{
+       {
+               boost::shared_ptr<AutomationControl> c (automation_control(param));
+               assert (c && c->list());
+
+               RCUWriter<ControlList> writer (_automated_controls);
+               boost::shared_ptr<ControlList> cl = writer.get_copy ();
+
+               ControlList::const_iterator fi = std::find (cl->begin(), cl->end(), c);
+               if (fi != cl->end()) {
+                       cl->erase (fi);
+               }
+               switch (as) {
+                       /* all potential  automation_playback() states */
+                       case Play:
+                       case Touch:
+                       case Latch:
+                               cl->push_back (c);
+                               break;
+                       case Off:
+                       case Write:
+                               break;
+               }
+       }
+       _automated_controls.flush();
+}
+
 boost::shared_ptr<Evoral::Control>
 Automatable::control_factory(const Evoral::Parameter& param)
 {
index 351d6dae6a873c81629ef0fe905d669733f2f1aa..d7ae4da711fd1ce9a71f026b6b686d699617fc11 100644 (file)
@@ -589,7 +589,7 @@ PluginInsert::set_block_size (pframes_t nframes)
 }
 
 void
-PluginInsert::automation_run (samplepos_t start, pframes_t nframes)
+PluginInsert::automation_run (samplepos_t start, pframes_t nframes, bool only_active)
 {
        // XXX does not work when rolling backwards
        if (_loop_location && nframes > 0) {
@@ -607,13 +607,13 @@ PluginInsert::automation_run (samplepos_t start, pframes_t nframes)
                        }
                        samplecnt_t move = std::min ((samplecnt_t)nframes, loop_end - start_pos);
 
-                       Automatable::automation_run (start_pos, move);
+                       Automatable::automation_run (start_pos, move, only_active);
                        remain -= move;
                        start_pos += move;
                }
                return;
        }
-       Automatable::automation_run (start, nframes);
+       Automatable::automation_run (start, nframes, only_active);
 }
 
 bool
@@ -880,6 +880,7 @@ PluginInsert::connect_and_run (BufferSet& bufs, samplepos_t start, samplepos_t e
 
        if (with_auto) {
 
+#if 0
                uint32_t n = 0;
 
                for (Controls::const_iterator li = controls().begin(); li != controls().end(); ++li, ++n) {
@@ -898,6 +899,21 @@ PluginInsert::connect_and_run (BufferSet& bufs, samplepos_t start, samplepos_t e
                                }
                        }
                }
+#else
+               boost::shared_ptr<ControlList> cl = _automated_controls.reader ();
+               for (ControlList::const_iterator ci = cl->begin(); ci != cl->end(); ++ci) {
+                       AutomationControl& c = *(ci->get());
+                       boost::shared_ptr<const Evoral::ControlList> clist (c.list());
+                       /* we still need to check for Touch and Latch */
+                       if (clist && (static_cast<AutomationList const&> (*clist)).automation_playback ()) {
+                               bool valid;
+                               const float val = c.list()->rt_safe_eval (start, valid);
+                               if (valid) {
+                                       c.set_value_unchecked(val);
+                               }
+                       }
+               }
+#endif
        }
 
        /* Calculate if, and how many samples we need to collect for analysis */
@@ -1188,7 +1204,7 @@ PluginInsert::bypass (BufferSet& bufs, pframes_t nframes)
 void
 PluginInsert::silence (samplecnt_t nframes, samplepos_t start_sample)
 {
-       automation_run (start_sample, nframes); // evaluate automation only
+       automation_run (start_sample, nframes, true); // evaluate automation only
 
        if (!active ()) {
                // XXX delaybuffers need to be offset by nframes
@@ -1254,7 +1270,7 @@ PluginInsert::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sa
                // XXX should call ::silence() to run plugin(s) for consistent load.
                // We'll need to change this anyway when bypass can be automated
                bypass (bufs, nframes);
-               automation_run (start_sample, nframes); // evaluate automation only
+               automation_run (start_sample, nframes, true); // evaluate automation only
                _delaybuffers.flush ();
        }