handle sidechain input changes
[ardour.git] / libs / ardour / plugin.cc
index d03024b64b215a4a2c47d70dd867aaf2a68b8d56..4bb5b02c77be15a04d440e136ed2e2d5c834b506 100644 (file)
@@ -46,6 +46,7 @@
 #include "ardour/chan_count.h"
 #include "ardour/chan_mapping.h"
 #include "ardour/data_type.h"
+#include "ardour/luaproc.h"
 #include "ardour/midi_buffer.h"
 #include "ardour/midi_state_tracker.h"
 #include "ardour/plugin.h"
@@ -78,6 +79,8 @@ static bool seen_get_state_message = false;
 static bool seen_set_state_message = false;
 #endif
 
+PBD::Signal2<void, std::string, Plugin*> Plugin::PresetsChanged;
+
 bool
 PluginInfo::is_instrument () const
 {
@@ -93,6 +96,7 @@ Plugin::Plugin (AudioEngine& e, Session& s)
        , _parameter_changed_since_last_preset (false)
 {
        _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
+       PresetsChanged.connect_same_thread (_preset_connection, boost::bind (&Plugin::update_presets, this, _1 ,_2));
 }
 
 Plugin::Plugin (const Plugin& other)
@@ -107,6 +111,7 @@ Plugin::Plugin (const Plugin& other)
        , _parameter_changed_since_last_preset (false)
 {
        _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
+       PresetsChanged.connect_same_thread (_preset_connection, boost::bind (&Plugin::update_presets, this, _1 ,_2));
 }
 
 Plugin::~Plugin ()
@@ -116,23 +121,36 @@ Plugin::~Plugin ()
 void
 Plugin::remove_preset (string name)
 {
+       Plugin::PresetRecord const * p = preset_by_label (name);
+       if (!p->user) {
+               PBD::error << _("Cannot remove plugin factory preset.") << endmsg;
+               return;
+       }
+
        do_remove_preset (name);
        _presets.erase (preset_by_label (name)->uri);
 
        _last_preset.uri = "";
        _parameter_changed_since_last_preset = false;
        PresetRemoved (); /* EMIT SIGNAL */
+       PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
 }
 
 /** @return PresetRecord with empty URI on failure */
 Plugin::PresetRecord
 Plugin::save_preset (string name)
 {
+       if (preset_by_label (name)) {
+               PBD::error << _("Preset with given name already exists.") << endmsg;
+               return Plugin::PresetRecord ();
+       }
+
        string const uri = do_save_preset (name);
 
        if (!uri.empty()) {
                _presets.insert (make_pair (uri, PresetRecord (uri, name)));
                PresetAdded (); /* EMIT SIGNAL */
+               PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
        }
 
        return PresetRecord (uri, name);
@@ -145,6 +163,13 @@ ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
        PluginInfoList plugs;
 
        switch (type) {
+       case ARDOUR::Lua:
+               {
+                       PluginPtr plugin (new LuaProc (session.engine(), session, ""));
+                       return plugin;
+               }
+               break;
+
        case ARDOUR::LADSPA:
                plugs = mgr.ladspa_plugin_info();
                break;
@@ -235,6 +260,12 @@ Plugin::input_streams () const
 const Plugin::PresetRecord *
 Plugin::preset_by_label (const string& label)
 {
+#ifndef NO_PLUGIN_STATE
+       if (!_have_presets) {
+               find_presets ();
+               _have_presets = true;
+       }
+#endif
        // FIXME: O(n)
        for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
                if (i->second.label == label) {
@@ -248,6 +279,12 @@ Plugin::preset_by_label (const string& label)
 const Plugin::PresetRecord *
 Plugin::preset_by_uri (const string& uri)
 {
+#ifndef NO_PLUGIN_STATE
+       if (!_have_presets) {
+               find_presets ();
+               _have_presets = true;
+       }
+#endif
        map<string, PresetRecord>::const_iterator pr = _presets.find (uri);
        if (pr != _presets.end()) {
                return &pr->second;
@@ -309,6 +346,17 @@ Plugin::resolve_midi ()
        _have_pending_stop_events = true;
 }
 
+void
+Plugin::update_presets (std::string src_unique_id, Plugin* src )
+{
+       if (src == this || unique_id() != src_unique_id) {
+               return;
+       }
+       _have_presets = false;
+       // TODO check if a preset was added/removed and emit the proper signal
+       // so far no subscriber distinguishes between PresetAdded and PresetRemoved
+       PresetAdded();
+}
 
 vector<Plugin::PresetRecord>
 Plugin::get_presets ()