commits 6001-6525 inclusive from 2.X applied to 3.0
[ardour.git] / libs / ardour / lv2_plugin.cc
index c6f18da80e8641171943a94b82cd0c72fbd687cb..9ba453345616b62275222654f2b93ed03efad9bf 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2008 Paul Davis 
+    Copyright (C) 2008 Paul Davis
     Author: Dave Robillard
 
     This program is free software; you can redistribute it and/or modify
@@ -82,7 +82,7 @@ LV2Plugin::init (LV2World& world, SLV2Plugin plugin, nframes_t rate)
        _shadow_data = 0;
        _latency_control_port = 0;
        _was_activated = false;
-       
+
        _instance = slv2_plugin_instantiate(plugin, rate, _features);
        _name = slv2_plugin_get_name(plugin);
        assert(_name);
@@ -101,14 +101,14 @@ LV2Plugin::init (LV2World& world, SLV2Plugin plugin, nframes_t rate)
                slv2_value_free(_author);
                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;
@@ -150,7 +150,7 @@ LV2Plugin::init (LV2World& world, SLV2Plugin plugin, nframes_t rate)
                        _defaults[i] = 0.0f;
                }
        }
-       
+
        SLV2UIs uis = slv2_plugin_get_uis(_plugin);
        if (slv2_uis_size(uis) > 0) {
                for (unsigned i=0; i < slv2_uis_size(uis); ++i) {
@@ -181,8 +181,6 @@ LV2Plugin::~LV2Plugin ()
        deactivate ();
        cleanup ();
 
-       GoingAway (); /* EMIT SIGNAL */
-       
        slv2_instance_free(_instance);
        slv2_value_free(_name);
        slv2_value_free(_author);
@@ -208,7 +206,7 @@ float
 LV2Plugin::default_value (uint32_t port)
 {
        return _defaults[port];
-}      
+}
 
 const char*
 LV2Plugin::port_symbol (uint32_t index)
@@ -235,11 +233,11 @@ LV2Plugin::set_parameter (uint32_t which, float val)
                        controls[which]->Changed ();
                }
 #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;
        }
 }
 
@@ -269,7 +267,7 @@ LV2Plugin::nth_parameter (uint32_t n, bool& ok) const
                        }
                }
        }
-       
+
        return 0;
 }
 
@@ -321,7 +319,7 @@ LV2Plugin::get_presets()
 }
 
 bool
-LV2Plugin::load_preset(const string uri)
+LV2Plugin::load_preset(const string& uri)
 {
        const string query = string(
                        "PREFIX lv2p: <http://lv2plug.in/ns/dev/presets#>\n"
@@ -346,7 +344,7 @@ LV2Plugin::save_preset (string /*name*/)
 {
        return false;
 }
-       
+
 bool
 LV2Plugin::has_editor() const
 {
@@ -354,7 +352,7 @@ LV2Plugin::has_editor() const
 }
 
 int
-LV2Plugin::set_state(const XMLNode& node)
+LV2Plugin::set_state(const XMLNode& node, int version)
 {
        XMLNodeList nodes;
        XMLProperty *prop;
@@ -370,8 +368,12 @@ LV2Plugin::set_state(const XMLNode& node)
                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;
@@ -384,6 +386,7 @@ LV2Plugin::set_state(const XMLNode& node)
                }
 
                map<string,uint32_t>::iterator i = _port_indices.find(sym);
+               
                if (i != _port_indices.end()) {
                        port_id = i->second;
                } else {
@@ -400,7 +403,7 @@ LV2Plugin::set_state(const XMLNode& node)
 
                set_parameter (port_id, atof(value));
        }
-       
+
        latency_compute_run ();
 
        return 0;
@@ -413,7 +416,7 @@ LV2Plugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc)
 
        SLV2Value def, min, max;
        slv2_port_get_range(_plugin, port, &def, &min, &max);
-       
+
     desc.integer_step = slv2_port_has_property(_plugin, port, _world.integer);
     desc.toggled = slv2_port_has_property(_plugin, port, _world.toggled);
     desc.logarithmic = slv2_port_has_property(_plugin, port, _world.logarithmic);
@@ -423,7 +426,7 @@ LV2Plugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc)
     desc.upper = max ? slv2_value_as_float(max) : 1.0f;
     desc.min_unbound = false; // TODO (LV2 extension)
     desc.max_unbound = false; // TODO (LV2 extension)
-       
+
        if (desc.integer_step) {
                desc.step = 1.0;
                desc.smallstep = 0.1;
@@ -522,9 +525,9 @@ LV2Plugin::connect_and_run (BufferSet& bufs,
                        slv2_instance_connect_port(_instance, port_index, NULL);
                }
        }
-       
+
        run (nframes);
-       
+
        midi_out_index = 0;
        for (uint32_t port_index = 0; port_index < parameter_count(); ++port_index) {
                if (parameter_is_midi(port_index) && parameter_is_output(port_index)) {
@@ -532,7 +535,7 @@ LV2Plugin::connect_and_run (BufferSet& bufs,
                        bufs.flush_lv2_midi(true, buf_index);
                }
        }
-       
+
        cycles_t now = get_cycles ();
        set_cycles ((uint32_t) (now - then));
 
@@ -609,9 +612,9 @@ LV2Plugin::latency_compute_run ()
        /* we need to run the plugin so that it can set its latency
           parameter.
        */
-       
+
        activate ();
-       
+
        uint32_t port_index = 0;
        uint32_t in_index = 0;
        uint32_t out_index = 0;
@@ -619,13 +622,13 @@ LV2Plugin::latency_compute_run ()
        float buffer[bufsize];
 
        memset(buffer,0,sizeof(float)*bufsize);
-               
+
        /* Note that we've already required that plugins
           be able to handle in-place processing.
        */
-       
+
        port_index = 0;
-       
+
        while (port_index < parameter_count()) {
                if (parameter_is_audio (port_index)) {
                        if (parameter_is_input (port_index)) {
@@ -638,7 +641,7 @@ LV2Plugin::latency_compute_run ()
                }
                port_index++;
        }
-       
+
        run (bufsize);
        deactivate ();
 }
@@ -659,7 +662,7 @@ LV2World::LV2World()
        srate = slv2_value_new_uri(world, SLV2_NAMESPACE_LV2 "sampleRate");
        gtk_gui = slv2_value_new_uri(world, "http://lv2plug.in/ns/extensions/ui#GtkUI");
        external_gui = slv2_value_new_uri(world, "http://lv2plug.in/ns/extensions/ui#external");
-       logarithmic = slv2_value_new_uri(world, "http://lv2plug.in/ns/dev/extportinfo#logarithmic");
+       logarithmic = slv2_value_new_uri(world, "http://lv2plug.in/ns/dev/extportinfo#logarithmic");
 }
 
 LV2World::~LV2World()
@@ -677,6 +680,7 @@ LV2PluginInfo::LV2PluginInfo (void* lv2_world, void* slv2_plugin)
        : _lv2_world(lv2_world)
        , _slv2_plugin(slv2_plugin)
 {
+        type = ARDOUR::LV2;
 }
 
 LV2PluginInfo::~LV2PluginInfo()
@@ -698,20 +702,20 @@ LV2PluginInfo::load (Session& session)
 
        catch (failed_constructor &err) {
                return PluginPtr ((Plugin*) 0);
-       }       
-       
+       }
+
        return PluginPtr();
 }
 
-PluginInfoList
+PluginInfoList*
 LV2PluginInfo::discover (void* lv2_world)
 {
-       PluginInfoList plugs;
-       
+       PluginInfoList* plugs = new PluginInfoList;
+
        LV2World* world = (LV2World*)lv2_world;
        SLV2Plugins plugins = slv2_world_get_all_plugins(world->world);
 
-       cerr << "LV2: Discovered " << slv2_plugins_size (plugins) << " plugins\n";
+       cerr << "LV2: Discovering " << slv2_plugins_size (plugins) << " plugins" << endl;
 
        for (unsigned i=0; i < slv2_plugins_size(plugins); ++i) {
                SLV2Plugin p = slv2_plugins_get_at(plugins, i);
@@ -723,7 +727,9 @@ LV2PluginInfo::discover (void* lv2_world)
                        cerr << "LV2: invalid plugin\n";
                        continue;
                }
-               
+
+               info->type = LV2;
+
                info->name = string(slv2_value_as_string(name));
                slv2_value_free(name);
 
@@ -741,7 +747,7 @@ LV2PluginInfo::discover (void* lv2_world)
                                world->input_class, world->audio_class, NULL));
                info->n_inputs.set_midi(slv2_plugin_get_num_ports_of_class(p,
                                world->input_class, world->event_class, NULL));
-               
+
                info->n_outputs.set_audio(slv2_plugin_get_num_ports_of_class(p,
                                world->output_class, world->audio_class, NULL));
                info->n_outputs.set_midi(slv2_plugin_get_num_ports_of_class(p,
@@ -749,10 +755,12 @@ LV2PluginInfo::discover (void* lv2_world)
 
                info->unique_id = slv2_value_as_uri(slv2_plugin_get_uri(p));
                info->index = 0; // Meaningless for LV2
-               
-               plugs.push_back (info);
+
+               plugs->push_back (info);
        }
 
+       cerr << "Done LV2 discovery" << endl;
+
        return plugs;
 }