rename ParameterChanged signal in Plugin to ParameterChangedExternally to reflect...
authorPaul Davis <paul@linuxaudiosystems.com>
Tue, 20 Oct 2015 13:07:51 +0000 (09:07 -0400)
committerPaul Davis <paul@linuxaudiosystems.com>
Tue, 20 Oct 2015 13:07:58 +0000 (09:07 -0400)
The signal exists to notify listeners that something outside of the host's control (e.g. a plugin's own GUI for AU or VST)
has modified a plugin parameter. Previous code had strange feedback loops and ambiguous semantics.

Significant modification of LV2 GUI updating was required.

Still to be tested for feedback loop issues: AudioUnits

gtk2_ardour/lv2_plugin_ui.cc
gtk2_ardour/lv2_plugin_ui.h
gtk2_ardour/plugin_ui.cc
gtk2_ardour/plugin_ui.h
libs/ardour/ardour/plugin.h
libs/ardour/ardour/plugin_insert.h
libs/ardour/plugin.cc
libs/ardour/plugin_insert.cc
libs/ardour/session_vst.cc
libs/ardour/source.cc
libs/ardour/vst_plugin.cc

index 141f437f4cdadbc21c7d8fcc4fc2ab179e9ed454..e2c446ef20555de5b9ee6d1a46e40e67a55f85ad 100644 (file)
@@ -20,7 +20,9 @@
 #include "ardour/lv2_plugin.h"
 #include "ardour/session.h"
 #include "pbd/error.h"
+#include "pbd/stacktrace.h"
 
+#include "gui_thread.h"
 #include "lv2_plugin_ui.h"
 #include "timers.h"
 
@@ -53,6 +55,9 @@ LV2PluginUI::write_from_ui(void*       controller,
                }
 
                boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
+               /* Cache our local copy of the last value received from the GUI */
+               me->_values[port_index] = *(const float*) buffer;
+               /* Now update the control itself */
                if (ac) {
                        ac->set_value(*(const float*)buffer);
                }
@@ -120,26 +125,18 @@ LV2PluginUI::on_external_ui_closed(void* controller)
 }
 
 void
-LV2PluginUI::parameter_changed(uint32_t port_index, float val)
+LV2PluginUI::control_changed (uint32_t port_index)
 {
-       PlugUIBase::parameter_changed(port_index, val);
-
-       if (val != _values[port_index]) {
-               parameter_update(port_index, val);
+       /* Must run in GUI thread because we modify _updates with no lock */
+       if (_lv2->get_parameter (port_index) != _values[port_index]) {
+               /* current plugin parameter does not match last value received
+                  from GUI, so queue an update to push it to the GUI during
+                  our regular timeout.
+               */
+               _updates.insert (port_index);
        }
 }
 
-void
-LV2PluginUI::parameter_update(uint32_t port_index, float val)
-{
-       if (!_inst) {
-               return;
-       }
-
-       suil_instance_port_event((SuilInstance*)_inst, port_index, 4, 0, &val);
-       _values[port_index] = val;
-}
-
 bool
 LV2PluginUI::start_updating(GdkEventAny*)
 {
@@ -183,13 +180,14 @@ LV2PluginUI::output_update()
                }
        }
 
-       /* FIXME only works with control output ports (which is all we support now anyway) */
-       uint32_t nports = _output_ports.size();
-       for (uint32_t i = 0; i < nports; ++i) {
-               uint32_t index = _output_ports[i];
-               parameter_changed(index, _lv2->get_parameter(index));
+       if (_inst) {
+               for (Updates::iterator i = _updates.begin(); i != _updates.end(); ++i) {
+                       float val = _lv2->get_parameter (*i);
+                       /* push current value to the GUI */
+                       suil_instance_port_event ((SuilInstance*)_inst, (*i), 4, 0, &val);
+               }
+               _updates.clear ();
        }
-
 }
 
 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
@@ -358,12 +356,14 @@ LV2PluginUI::lv2ui_instantiate(const std::string& title)
                bool     ok;
                uint32_t port = _lv2->nth_parameter(i, ok);
                if (ok) {
-                       _values[port]        = _lv2->get_parameter(port);
                        _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
                                insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
 
-                       if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
-                               parameter_update(port, _values[port]);
+                       /* FIXME only works with control output ports (which is all we support now anyway) */
+                       if (_controllables[port] && _lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
+                               _controllables[port]->Changed.connect (control_connections, invalidator (*this), boost::bind (&LV2PluginUI::control_changed, this, port), gui_context());
+                               /* queue for first update ("push") to GUI */
+                               _updates.insert (port);
                        }
                }
        }
@@ -401,9 +401,7 @@ LV2PluginUI::lv2ui_free()
 
 LV2PluginUI::~LV2PluginUI ()
 {
-       if (_values) {
-               delete[] _values;
-       }
+       delete [] _values;
 
        _message_update_connection.disconnect();
        _screen_update_connection.disconnect();
index 6a8acf9cf53e0454e38a3c08d1127ac201da43db..25ab4b9e30f3771f9c8f49b047bad686435586bb 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <list>
 #include <map>
+#include <set>
 #include <vector>
 
 #include <gtkmm/widget.h>
@@ -64,7 +65,7 @@ class LV2PluginUI : public PlugUIBase, public Gtk::VBox
 
   private:
 
-       void parameter_changed (uint32_t, float);
+       void control_changed (uint32_t);
 
        typedef boost::shared_ptr<ARDOUR::AutomationControl> ControllableRef;
 
@@ -85,6 +86,8 @@ class LV2PluginUI : public PlugUIBase, public Gtk::VBox
        LV2_Feature                          _parent_feature;
        Gtk::Window*                         _win_ptr;
        void*                                _inst;
+       typedef std::set<uint32_t> Updates;
+       Updates                              _updates;
 
        static void on_external_ui_closed(void* controller);
 
index 37c01462b28348e631edeca709bb8394cbe82744..19dbee422fb8a08e074f0aa08d5a669ff2ac7954 100644 (file)
@@ -484,7 +484,7 @@ PlugUIBase::PlugUIBase (boost::shared_ptr<PluginInsert> pi)
        plugin->PresetAdded.connect (*this, invalidator (*this), boost::bind (&PlugUIBase::preset_added_or_removed, this), gui_context ());
        plugin->PresetRemoved.connect (*this, invalidator (*this), boost::bind (&PlugUIBase::preset_added_or_removed, this), gui_context ());
        plugin->PresetLoaded.connect (*this, invalidator (*this), boost::bind (&PlugUIBase::update_preset, this), gui_context ());
-       plugin->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&PlugUIBase::parameter_changed, this, _1, _2), gui_context ());
+       plugin->PresetDirty.connect (*this, invalidator (*this), boost::bind (&PlugUIBase::update_preset_modified, this), gui_context ());
 
        insert->AutomationStateChanged.connect (*this, invalidator (*this), boost::bind (&PlugUIBase::automation_state_changed, this), gui_context());
 
@@ -813,12 +813,6 @@ PlugUIBase::update_preset_modified ()
        }
 }
 
-void
-PlugUIBase::parameter_changed (uint32_t, float)
-{
-       update_preset_modified ();
-}
-
 void
 PlugUIBase::preset_added_or_removed ()
 {
index 41f4ef39f1d01bbf1721e8b7a5423e4ca3f8f8dd..d2e5e21cca15f918d86fd0337ffbc7b9fc292f7d 100644 (file)
@@ -170,7 +170,6 @@ class PlugUIBase : public virtual sigc::trackable, public PBD::ScopedConnectionL
        void processor_active_changed (boost::weak_ptr<ARDOUR::Processor> p);
        void plugin_going_away ();
        void automation_state_changed ();
-       virtual void parameter_changed (uint32_t, float);
        void preset_added_or_removed ();
        void update_preset_modified ();
 
index 2554a6816c01a5e6fa94f6623d6152c29a73ff3c..7bef40ab88814dc28601a4e4b931bd8d45578e3b 100644 (file)
@@ -208,10 +208,17 @@ class LIBARDOUR_API Plugin : public PBD::StatefulDestructible, public Latent
        /** Emitted when a preset has been loaded */
        PBD::Signal0<void> PresetLoaded;
 
+       /** Emitted when a parameter is altered in a way that may have
+        *  changed the settings with respect to any loaded preset.
+        */
+       PBD::Signal0<void> PresetDirty;
+
        virtual bool has_editor () const = 0;
 
-       /** Emitted when any parameter changes */
-       PBD::Signal2<void, uint32_t, float> ParameterChanged;
+       /** Emitted when a parameter is altered by something outside of our
+        * control, most typically a Plugin GUI/editor
+        */
+       PBD::Signal2<void, uint32_t, float> ParameterChangedExternally;
 
        virtual bool configure_io (ChanCount /*in*/, ChanCount /*out*/) { return true; }
 
@@ -272,9 +279,18 @@ class LIBARDOUR_API Plugin : public PBD::StatefulDestructible, public Latent
 protected:
 
        friend class PluginInsert;
+       friend class Session;
 
+       /* Called when a parameter of the plugin is changed outside of this
+        * host's control (typical via a plugin's own GUI/editor)
+        */
+       void parameter_changed_externally (uint32_t which, float val);
+
+       /* should be overridden by plugin API specific derived types to
+        * actually implement changing the parameter. The derived type should
+        * call this after the change is made.
+        */
        virtual void set_parameter (uint32_t which, float val);
-       virtual void set_parameter_automated (uint32_t which, float val);
 
        /** Do the actual saving of the current plugin settings to a preset of the provided name.
         *  Should return a URI on success, or an empty string on failure.
index 8788778a3a5a8f0177b618e034b22483d72835fa..d4d9adb54dc6a8fc2b521b5b82d2de6268400888 100644 (file)
@@ -96,6 +96,7 @@ class LIBARDOUR_API PluginInsert : public Processor
 
                void set_value (double val);
                double get_value (void) const;
+               void catch_up_with_external_value (double val);
                XMLNode& get_state();
 
        private:
@@ -164,10 +165,9 @@ class LIBARDOUR_API PluginInsert : public Processor
        /* disallow copy construction */
        PluginInsert (const PluginInsert&);
 
-       void parameter_changed (uint32_t, float);
+       void parameter_changed_externally (uint32_t, float);
 
        void  set_parameter (Evoral::Parameter param, float val);
-       float get_parameter (Evoral::Parameter param);
 
        float default_parameter_value (const Evoral::Parameter& param);
 
index f14c56798b65e4cb404c8cf6b8162037baa90fdf..d68502713c840caa2c63df731c5ed2302d53d91d 100644 (file)
@@ -356,19 +356,21 @@ Plugin::clear_preset ()
        PresetLoaded (); /* EMIT SIGNAL */
 }
 
-/** @param val `plugin' value */
 void
-Plugin::set_parameter (uint32_t which, float)
+Plugin::set_parameter (uint32_t /* which */, float /* value */)
 {
        _parameter_changed_since_last_preset = true;
        _session.set_dirty ();
-       ParameterChanged (which, get_parameter (which)); /* EMIT SIGNAL */
+       PresetDirty (); /* EMIT SIGNAL */
 }
 
 void
-Plugin::set_parameter_automated (uint32_t which, float val)
+Plugin::parameter_changed_externally (uint32_t which, float /* value */)
 {
-       Plugin::set_parameter (which, val);
+       _parameter_changed_since_last_preset = true;
+       _session.set_dirty ();
+       ParameterChangedExternally (which, get_parameter (which)); /* EMIT SIGNAL */
+       PresetDirty (); /* EMIT SIGNAL */
 }
 
 int
index 98ff9ab4f0983040e1fc1eb0d49a42788dfab612..ec6d86d71a76c66094fdba48db8e72065af82033 100644 (file)
@@ -264,26 +264,53 @@ PluginInsert::create_automatable_parameters ()
                }
        }
 }
-
+/** Called when something outside of this host has modified a plugin
+ * parameter. Responsible for propagating the change to two places:
+ *
+ *   1) anything listening to the Control itself
+ *   2) any replicated plugins that make up this PluginInsert.
+ *
+ * The PluginInsert is connected to the ParameterChangedExternally signal for
+ * the first (primary) plugin, and here broadcasts that change to any others.
+ *
+ * XXX We should probably drop this whole replication idea (Paul, October 2015)
+ * since it isn't used by sensible plugin APIs (AU, LV2).
+ */
 void
-PluginInsert::parameter_changed (uint32_t which, float val)
+PluginInsert::parameter_changed_externally (uint32_t which, float val)
 {
        boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, which));
 
-       if (ac) {
-               ac->set_value (val);
+       /* First propagation: alter the underlying value of the control,
+        * without telling the plugin(s) that own/use it to set it.
+        */
+
+       if (!ac) {
+               return;
+       }
 
-                Plugins::iterator i = _plugins.begin();
+       boost::shared_ptr<PluginControl> pc = boost::dynamic_pointer_cast<PluginControl> (ac);
 
-                /* don't set the first plugin, just all the slaves */
+       if (pc) {
+               pc->catch_up_with_external_value (val);
+       }
 
-                if (i != _plugins.end()) {
-                        ++i;
-                        for (; i != _plugins.end(); ++i) {
-                                (*i)->set_parameter (which, val);
-                        }
-                }
-        }
+       /* Second propagation: tell all plugins except the first to
+          update the value of this parameter. For sane plugin APIs,
+          there are no other plugins, so this is a no-op in those
+          cases.
+       */
+
+       Plugins::iterator i = _plugins.begin();
+
+       /* don't set the first plugin, just all the slaves */
+
+       if (i != _plugins.end()) {
+               ++i;
+               for (; i != _plugins.end(); ++i) {
+                       (*i)->set_parameter (which, val);
+               }
+       }
 }
 
 int
@@ -506,41 +533,6 @@ PluginInsert::run (BufferSet& bufs, framepos_t start_frame, framepos_t /*end_fra
 
 }
 
-void
-PluginInsert::set_parameter (Evoral::Parameter param, float val)
-{
-       if (param.type() != PluginAutomation) {
-               return;
-       }
-
-       /* the others will be set from the event triggered by this */
-
-       _plugins[0]->set_parameter (param.id(), val);
-
-       boost::shared_ptr<AutomationControl> ac
-                       = boost::dynamic_pointer_cast<AutomationControl>(control(param));
-
-       if (ac) {
-               ac->set_value(val);
-       } else {
-               warning << "set_parameter called for nonexistent parameter "
-                       << EventTypeMap::instance().to_symbol(param) << endmsg;
-       }
-
-       _session.set_dirty();
-}
-
-float
-PluginInsert::get_parameter (Evoral::Parameter param)
-{
-       if (param.type() != PluginAutomation) {
-               return 0.0;
-       } else {
-               assert (!_plugins.empty ());
-               return _plugins[0]->get_parameter (param.id());
-       }
-}
-
 void
 PluginInsert::automation_run (BufferSet& bufs, framepos_t start, pframes_t nframes)
 {
@@ -1317,6 +1309,12 @@ PluginInsert::PluginControl::set_value (double user_val)
        AutomationControl::set_value (user_val);
 }
 
+void
+PluginInsert::PluginControl::catch_up_with_external_value (double user_val)
+{
+       AutomationControl::set_value (user_val);
+}
+
 XMLNode&
 PluginInsert::PluginControl::get_state ()
 {
@@ -1333,8 +1331,13 @@ PluginInsert::PluginControl::get_state ()
 double
 PluginInsert::PluginControl::get_value () const
 {
-       /* FIXME: probably should be taking out some lock here.. */
-       return _plugin->get_parameter (_list->parameter());
+       boost::shared_ptr<Plugin> plugin = _plugin->plugin (0);
+
+       if (!plugin) {
+               return 0.0;
+       }
+
+       return plugin->get_parameter (_list->parameter().id());
 }
 
 PluginInsert::PluginPropertyControl::PluginPropertyControl (PluginInsert*                     p,
@@ -1430,7 +1433,7 @@ PluginInsert::add_plugin (boost::shared_ptr<Plugin> plugin)
                 /* first (and probably only) plugin instance - connect to relevant signals
                  */
 
-               plugin->ParameterChanged.connect_same_thread (*this, boost::bind (&PluginInsert::parameter_changed, this, _1, _2));
+               plugin->ParameterChangedExternally.connect_same_thread (*this, boost::bind (&PluginInsert::parameter_changed_externally, this, _1, _2));
                 plugin->StartTouch.connect_same_thread (*this, boost::bind (&PluginInsert::start_touch, this, _1));
                 plugin->EndTouch.connect_same_thread (*this, boost::bind (&PluginInsert::end_touch, this, _1));
        }
index 9bf28473317e556166a68888988586d418144e29..009a4acc40a6e5a2c21fd8eddf3b8f745d589f70 100644 (file)
@@ -86,7 +86,7 @@ intptr_t Session::vst_callback (
                SHOW_CALLBACK ("audioMasterAutomate");
                // index, value, returns 0
                if (plug) {
-                       plug->set_parameter_automated (index, opt);
+                       plug->parameter_changed_externally (index, opt);
                }
                return 0;
 
index 29093035c2c62d4c81607cd8dab94e493dbed1b3..aaa50ff2973aaaa79039011f7c3e043c14dc247a 100644 (file)
@@ -178,7 +178,7 @@ Source::set_been_analysed (bool yn)
                        yn = false;
                }
        }
-       if (yn != _analysed); {
+       if (yn != _analysed) {
                Glib::Threads::Mutex::Lock lm (_analysis_lock);
                _analysed = yn;
        }
index 08db7dec5e56fa18d533a366a13dd6f9377e51bf..1614b1d8fe394cc6a6ee62a6c9e9737a91347aec 100644 (file)
@@ -117,12 +117,6 @@ VSTPlugin::set_parameter (uint32_t which, float newval)
        }
 }
 
-void
-VSTPlugin::set_parameter_automated (uint32_t which, float newval)
-{
-       Plugin::set_parameter_automated (which, newval);
-}
-
 uint32_t
 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
 {