fix lookup of LV2 plugin parameter name
[ardour.git] / libs / ardour / lv2_plugin.cc
index b48395b40f5c5b95c0dfe9da4696ae88128ba402..3f333e3c920b8aac4974a1dafd28e0014371b2cd 100644 (file)
@@ -25,6 +25,8 @@
 #include <cmath>
 #include <cstring>
 
+#include <glibmm.h>
+
 #include "pbd/compose.h"
 #include "pbd/error.h"
 #include "pbd/pathscanner.h"
@@ -42,6 +44,8 @@
 #include "i18n.h"
 #include <locale.h>
 
+#include "lv2ext/lv2_persist.h"
+
 using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
@@ -83,6 +87,22 @@ LV2Plugin::init (LV2World& world, SLV2Plugin plugin, nframes_t rate)
        _latency_control_port = 0;
        _was_activated = false;
 
+       SLV2Value persist_uri = slv2_value_new_uri(_world.world, "http://lv2plug.in/ns/ext/persist");
+       _supports_persist = slv2_plugin_has_feature(plugin, persist_uri);
+       slv2_value_free(persist_uri);
+
+       _instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
+       _data_access_feature.URI = "http://lv2plug.in/ns/ext/data-access";
+       _persist_feature.URI = "http://lv2plug.in/ns/ext/persist";
+       _persist_feature.data = NULL;
+
+       _features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * 5);
+       _features[0] = &_instance_access_feature;
+       _features[1] = &_data_access_feature;
+       _features[2] = &_persist_feature;
+       _features[3] = _uri_map.feature();
+       _features[4] = NULL;
+
        _instance = slv2_plugin_instantiate(plugin, rate, _features);
        _name = slv2_plugin_get_name(plugin);
        assert(_name);
@@ -93,6 +113,10 @@ LV2Plugin::init (LV2World& world, SLV2Plugin plugin, nframes_t rate)
                throw failed_constructor();
        }
 
+       _instance_access_feature.data = (void*)_instance->lv2_handle;
+       _data_access_extension_data.extension_data = _instance->lv2_descriptor->extension_data;
+       _data_access_feature.data = &_data_access_extension_data;
+
        if (slv2_plugin_has_feature(plugin, world.in_place_broken)) {
                error << string_compose(
                                _("LV2: \"%1\" cannot be used, since it cannot do inplace processing"),
@@ -102,19 +126,6 @@ LV2Plugin::init (LV2World& world, SLV2Plugin plugin, nframes_t rate)
                throw failed_constructor();
        }
 
-       _instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
-       _instance_access_feature.data = (void*)_instance->lv2_handle;
-
-       _data_access_extension_data.extension_data = _instance->lv2_descriptor->extension_data;
-       _data_access_feature.URI = "http://lv2plug.in/ns/ext/data-access";
-       _data_access_feature.data = &_data_access_extension_data;
-
-       _features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * 4);
-       _features[0] = &_instance_access_feature;
-       _features[1] = &_data_access_feature;
-       _features[2] = _uri_map.feature();
-       _features[3] = NULL;
-
        _sample_rate = rate;
 
        const uint32_t num_ports = slv2_plugin_get_num_ports(plugin);
@@ -226,18 +237,18 @@ LV2Plugin::set_parameter (uint32_t which, float val)
 {
        if (which < slv2_plugin_get_num_ports(_plugin)) {
                _shadow_data[which] = val;
-#if 0
                ParameterChanged (which, val); /* EMIT SIGNAL */
 
+#if 0          
                if (which < parameter_count() && controls[which]) {
                        controls[which]->Changed ();
                }
-#endif
+#endif         
 
        } else {
                warning << string_compose (_("Illegal parameter number used with plugin \"%1\"."
-                               "This is a bug in either Ardour or the LV2 plugin (%2)"),
-                               name(), unique_id()) << endmsg;
+                                             "This is a bug in either %2 or the LV2 plugin (%3)"),
+                                           name(), PROGRAM_NAME, unique_id()) << endmsg;
        }
 }
 
@@ -271,6 +282,19 @@ LV2Plugin::nth_parameter (uint32_t n, bool& ok) const
        return 0;
 }
 
+struct LV2Value { void* value; uint32_t type; };
+typedef std::map< std::string, LV2Value > LV2State;
+
+static void
+lv2_persist_store_callback(void*       callback_data,
+                           const char* key,
+                           const void* value,
+                           size_t      size,
+                           uint32_t    type)
+{
+       cout << "LV2 PERSIST STORE " << key << " = " << value << " :: " << type << endl;
+}
+
 XMLNode&
 LV2Plugin::get_state()
 {
@@ -296,6 +320,25 @@ LV2Plugin::get_state()
                }
        }
 
+       if (_supports_persist) {
+               // Create state directory for this plugin instance
+               const std::string state_path = Glib::build_filename(_session.plugins_dir(), _id.to_s());
+               cout << "LV2 plugin state path " << state_path << endl;
+
+               // Get LV2 Persist extension data from plugin instance
+               LV2_Persist* persist = (LV2_Persist*)slv2_instance_get_extension_data(
+                       _instance, "http://lv2plug.in/ns/ext/persist");
+               if (!persist) {
+                       warning << string_compose(
+                               _("Plugin \"%1\% failed to return LV2 persist data"),
+                               unique_id());
+                       return *root; // FIXME: Possibly inconsistent state
+               }
+
+               LV2State state;
+               persist->save(_instance, lv2_persist_store_callback, &state);
+       }
+       
        return *root;
 }
 
@@ -352,7 +395,7 @@ LV2Plugin::has_editor() const
 }
 
 int
-LV2Plugin::set_state(const XMLNode& node, int /*version*/)
+LV2Plugin::set_state(const XMLNode& node, int version)
 {
        XMLNodeList nodes;
        XMLProperty *prop;
@@ -368,8 +411,12 @@ LV2Plugin::set_state(const XMLNode& node, int /*version*/)
                return -1;
        }
 
-       nodes = node.children ("Port");
-
+       if (version < 3000){
+               nodes = node.children ("port");
+       } else {
+               nodes = node.children ("Port");
+       }
+       
        for (iter = nodes.begin(); iter != nodes.end(); ++iter){
 
                child = *iter;
@@ -382,6 +429,7 @@ LV2Plugin::set_state(const XMLNode& node, int /*version*/)
                }
 
                map<string,uint32_t>::iterator i = _port_indices.find(sym);
+               
                if (i != _port_indices.end()) {
                        port_id = i->second;
                } else {
@@ -446,7 +494,7 @@ LV2Plugin::describe_parameter (Evoral::Parameter which)
 {
        if (which.type() == PluginAutomation && which.id() < parameter_count()) {
                SLV2Value name = slv2_port_get_name(_plugin,
-                       slv2_plugin_get_port_by_index(_plugin, which));
+                                                    slv2_plugin_get_port_by_index(_plugin, which.id()));
                string ret(slv2_value_as_string(name));
                slv2_value_free(name);
                return ret;
@@ -675,6 +723,7 @@ LV2PluginInfo::LV2PluginInfo (void* lv2_world, void* slv2_plugin)
        : _lv2_world(lv2_world)
        , _slv2_plugin(slv2_plugin)
 {
+        type = ARDOUR::LV2;
 }
 
 LV2PluginInfo::~LV2PluginInfo()