replace fixed-point linear interpolation with double-based version, thereby removing...
[ardour.git] / libs / ardour / lv2_plugin.cc
index 0260e02560df84c6a239b89f182d45334ebed6af..8000bd8e44e3487de6c1524d8fbd1b6ca2dda193 100644 (file)
 #include <cmath>
 #include <cstring>
 
-#include <pbd/compose.h>
-#include <pbd/error.h>
-#include <pbd/pathscanner.h>
-#include <pbd/xml++.h>
+#include "pbd/compose.h"
+#include "pbd/error.h"
+#include "pbd/pathscanner.h"
+#include "pbd/xml++.h"
 
-#include <ardour/ardour.h>
-#include <ardour/session.h>
-#include <ardour/audioengine.h>
-#include <ardour/lv2_plugin.h>
+#include "ardour/ardour.h"
+#include "ardour/session.h"
+#include "ardour/audioengine.h"
+#include "ardour/audio_buffer.h"
+#include "ardour/lv2_event_buffer.h"
+#include "ardour/lv2_plugin.h"
 
-#include <pbd/stl_delete.h>
+#include "pbd/stl_delete.h"
 
 #include "i18n.h"
 #include <locale.h>
 using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
-       
+
+URIMap   LV2Plugin::_uri_map;
+uint32_t LV2Plugin::_midi_event_type = _uri_map.uri_to_id(
+               "http://lv2plug.in/ns/ext/event",
+               "http://lv2plug.in/ns/ext/midi#MidiEvent");
+
 LV2Plugin::LV2Plugin (AudioEngine& e, Session& session, LV2World& world, SLV2Plugin plugin, nframes_t rate)
        : Plugin (e, session)
        , _world(world)
@@ -87,7 +94,8 @@ LV2Plugin::init (LV2World& world, SLV2Plugin plugin, nframes_t rate)
        }
 
        if (slv2_plugin_has_feature(plugin, world.in_place_broken)) {
-               error << string_compose(_("LV2: \"%1\" cannot be used, since it cannot do inplace processing"),
+               error << string_compose(
+                               _("LV2: \"%1\" cannot be used, since it cannot do inplace processing"),
                                slv2_value_as_string(_name));
                slv2_value_free(_name);
                slv2_value_free(_author);
@@ -101,10 +109,11 @@ LV2Plugin::init (LV2World& world, SLV2Plugin plugin, nframes_t rate)
        _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*) * 3);
+       _features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * 4);
        _features[0] = &_instance_access_feature;
        _features[1] = &_data_access_feature;
-       _features[2] = NULL;
+       _features[2] = _uri_map.feature();
+       _features[3] = NULL;
 
        _sample_rate = rate;
 
@@ -118,8 +127,10 @@ LV2Plugin::init (LV2World& world, SLV2Plugin plugin, nframes_t rate)
        uint32_t latency_port = (latent ? slv2_plugin_get_latency_port_index(plugin) : 0);
 
        for (uint32_t i = 0; i < num_ports; ++i) {
+               SLV2Port port = slv2_plugin_get_port_by_index(plugin, i);
+               SLV2Value sym = slv2_port_get_symbol(_plugin, port);
+               _port_indices.insert(std::make_pair(slv2_value_as_string(sym), i));
                if (parameter_is_control(i)) {
-                       SLV2Port port = slv2_plugin_get_port_by_index(plugin, i);
                        SLV2Value def;
                        slv2_port_get_range(plugin, port, &def, NULL, NULL);
                        _defaults[i] = def ? slv2_value_as_float(def) : 0.0f;
@@ -165,13 +176,8 @@ LV2Plugin::~LV2Plugin ()
        slv2_value_free(_name);
        slv2_value_free(_author);
 
-       if (_control_data) {
-               delete [] _control_data;
-       }
-
-       if (_shadow_data) {
-               delete [] _shadow_data;
-       }
+       delete [] _control_data;
+       delete [] _shadow_data;
 }
 
 string
@@ -187,6 +193,19 @@ LV2Plugin::default_value (uint32_t port)
        return _defaults[port];
 }      
 
+const char*
+LV2Plugin::port_symbol (uint32_t index)
+{
+       SLV2Port port = slv2_plugin_get_port_by_index(_plugin, index);
+       if (!port) {
+               error << name() << ": Invalid port index " << index << endmsg;
+       }
+
+       SLV2Value sym = slv2_port_get_symbol(_plugin, port);
+       return slv2_value_as_string(sym);
+}
+
+
 void
 LV2Plugin::set_parameter (uint32_t which, float val)
 {
@@ -248,9 +267,10 @@ LV2Plugin::get_state()
        for (uint32_t i = 0; i < parameter_count(); ++i){
 
                if (parameter_is_input(i) && parameter_is_control(i)) {
-                       child = new XMLNode("port");
-                       snprintf(buf, sizeof(buf), "%u", i);
-                       child->add_property("number", string(buf));
+                       child = new XMLNode("Port");
+                       /*snprintf(buf, sizeof(buf), "%u", i);
+                       child->add_property("number", string(buf));*/
+                       child->add_property("symbol", port_symbol(i));
                        snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
                        child->add_property("value", string(buf));
                        root->add_child_nocopy (*child);
@@ -264,10 +284,50 @@ LV2Plugin::get_state()
        return *root;
 }
 
+vector<Plugin::PresetRecord>
+LV2Plugin::get_presets()
+{
+       vector<PresetRecord> result;
+       SLV2Results presets = slv2_plugin_query_sparql(_plugin,
+                       "PREFIX lv2p: <http://lv2plug.in/ns/dev/presets#>\n"
+                       "PREFIX dc:  <http://dublincore.org/documents/dcmi-namespace/>\n"
+                       "SELECT ?p ?name WHERE { <> lv2p:hasPreset ?p . ?p dc:title ?name }\n");
+       for (; !slv2_results_finished(presets); slv2_results_next(presets)) {
+               SLV2Value uri  = slv2_results_get_binding_value(presets, 0);
+               SLV2Value name = slv2_results_get_binding_value(presets, 1);
+               PresetRecord rec(slv2_value_as_string(uri), slv2_value_as_string(name));
+               result.push_back(rec);
+               this->presets.insert(std::make_pair(slv2_value_as_string(uri), rec));
+       }
+       slv2_results_free(presets);
+       return result;
+}
+
+bool
+LV2Plugin::load_preset(const string uri)
+{
+       const string query = string(
+                       "PREFIX lv2p: <http://lv2plug.in/ns/dev/presets#>\n"
+                       "PREFIX dc:  <http://dublincore.org/documents/dcmi-namespace/>\n"
+                       "SELECT ?sym ?val WHERE { <") + uri + "> lv2:port ?port . "
+                               " ?port lv2:symbol ?sym ; lv2p:value ?val . }";
+       SLV2Results values = slv2_plugin_query_sparql(_plugin, query.c_str());
+       for (; !slv2_results_finished(values); slv2_results_next(values)) {
+               SLV2Value sym  = slv2_results_get_binding_value(values, 0);
+               SLV2Value val = slv2_results_get_binding_value(values, 1);
+               if (slv2_value_is_float(val)) {
+                       uint32_t index = _port_indices[slv2_value_as_string(sym)];
+                       set_parameter(index, slv2_value_as_float(val));
+               }
+       }
+       slv2_results_free(values);
+       return true;
+}
+
 bool
 LV2Plugin::save_preset (string name)
 {
-       return Plugin::save_preset (name, "lv2");
+       return false;
 }
        
 bool
@@ -283,8 +343,8 @@ LV2Plugin::set_state(const XMLNode& node)
        XMLProperty *prop;
        XMLNodeConstIterator iter;
        XMLNode *child;
-       const char *port;
-       const char *data;
+       const char *sym;
+       const char *value;
        uint32_t port_id;
        LocaleGuard lg (X_("POSIX"));
 
@@ -293,28 +353,35 @@ LV2Plugin::set_state(const XMLNode& node)
                return -1;
        }
 
-       nodes = node.children ("port");
+       nodes = node.children ("Port");
 
-       for(iter = nodes.begin(); iter != nodes.end(); ++iter){
+       for (iter = nodes.begin(); iter != nodes.end(); ++iter){
 
                child = *iter;
 
-               if ((prop = child->property("number")) != 0) {
-                       port = prop->value().c_str();
+               if ((prop = child->property("symbol")) != 0) {
+                       sym = prop->value().c_str();
                } else {
-                       warning << _("LV2: no lv2 port number") << endmsg;
+                       warning << _("LV2: port has no symbol, ignored") << endmsg;
+                       continue;
+               }
+
+               map<string,uint32_t>::iterator i = _port_indices.find(sym);
+               if (i != _port_indices.end()) {
+                       port_id = i->second;
+               } else {
+                       warning << _("LV2: port has unknown index, ignored") << endmsg;
                        continue;
                }
 
                if ((prop = child->property("value")) != 0) {
-                       data = prop->value().c_str();
+                       value = prop->value().c_str();
                } else {
-                       warning << _("LV2: no lv2 port data") << endmsg;
+                       warning << _("LV2: port has no value, ignored") << endmsg;
                        continue;
                }
 
-               sscanf (port, "%" PRIu32, &port_id);
-               set_parameter (port_id, atof(data));
+               set_parameter (port_id, atof(value));
        }
        
        latency_compute_run ();
@@ -398,53 +465,58 @@ LV2Plugin::automatable () const
 }
 
 int
-LV2Plugin::connect_and_run (BufferSet& bufs, uint32_t& in_index, uint32_t& out_index, nframes_t nframes, nframes_t offset)
+LV2Plugin::connect_and_run (BufferSet& bufs,
+               ChanMapping in_map, ChanMapping out_map,
+               nframes_t nframes, nframes_t offset)
 {
-       uint32_t port_index;
-       cycles_t then, now;
-
-       port_index = 0;
+       cycles_t then = get_cycles ();
 
-       then = get_cycles ();
-       
-       const uint32_t nbufs = bufs.count().n_audio();
-
-       while (port_index < parameter_count()) {
+       uint32_t audio_in_index  = 0;
+       uint32_t audio_out_index = 0;
+       uint32_t midi_in_index   = 0;
+       uint32_t midi_out_index  = 0;
+       for (uint32_t port_index = 0; port_index < parameter_count(); ++port_index) {
                if (parameter_is_audio(port_index)) {
                        if (parameter_is_input(port_index)) {
-                               const size_t index = min(in_index, nbufs - 1);
+                               const uint32_t buf_index = in_map.get(DataType::AUDIO, audio_in_index++);
+                               //cerr << port_index << " : " << " AUDIO IN " << buf_index << endl;
                                slv2_instance_connect_port(_instance, port_index,
-                                               bufs.get_audio(index).data(nframes, offset));
-                               in_index++;
+                                               bufs.get_audio(buf_index).data(offset));
                        } else if (parameter_is_output(port_index)) {
-                               const size_t index = min(out_index,nbufs - 1);
+                               const uint32_t buf_index = out_map.get(DataType::AUDIO, audio_out_index++);
+                               //cerr << port_index << " : " << " AUDIO OUT " << buf_index << endl;
                                slv2_instance_connect_port(_instance, port_index,
-                                               bufs.get_audio(index).data(nframes, offset));
-                               out_index++;
+                                               bufs.get_audio(buf_index).data(offset));
                        }
                } else if (parameter_is_midi(port_index)) {
-                       // FIXME: Switch MIDI buffer format to LV2 event buffer
                        if (parameter_is_input(port_index)) {
-                               //const size_t index = min(in_index, nbufs - 1);
-                               //slv2_instance_connect_port(_instance, port_index,
-                               //              bufs.get_midi(index).data(nframes, offset));
-                               // FIXME: hope it's connection optional...
-                               slv2_instance_connect_port(_instance, port_index, NULL);
-                               in_index++;
+                               const uint32_t buf_index = in_map.get(DataType::MIDI, midi_in_index++);
+                               //cerr << port_index << " : " << " MIDI IN " << buf_index << endl;
+                               slv2_instance_connect_port(_instance, port_index,
+                                               bufs.get_lv2_midi(true, buf_index).data());
                        } else if (parameter_is_output(port_index)) {
-                               //const size_t index = min(out_index,nbufs - 1);
-                               //slv2_instance_connect_port(_instance, port_index,
-                               //              bufs.get_midi(index).data(nframes, offset));
-                               // FIXME: hope it's connection optional...
-                               slv2_instance_connect_port(_instance, port_index, NULL);
-                               out_index++;
+                               const uint32_t buf_index = out_map.get(DataType::MIDI, midi_out_index++);
+                               //cerr << port_index << " : " << " MIDI OUT " << buf_index << endl;
+                               slv2_instance_connect_port(_instance, port_index,
+                                               bufs.get_lv2_midi(false, buf_index).data());
                        }
+               } else if (!parameter_is_control(port_index)) {
+                       // Optional port (it'd better be if we've made it this far...)
+                       slv2_instance_connect_port(_instance, port_index, NULL);
                }
-               port_index++;
        }
        
        run (nframes);
-       now = get_cycles ();
+       
+       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)) {
+                       const uint32_t buf_index = out_map.get(DataType::MIDI, midi_out_index++);
+                       bufs.flush_lv2_midi(true, buf_index);
+               }
+       }
+       
+       cycles_t now = get_cycles ();
        set_cycles ((uint32_t) (now - then));
 
        return 0;
@@ -468,8 +540,8 @@ bool
 LV2Plugin::parameter_is_midi (uint32_t param) const
 {
        SLV2Port port = slv2_plugin_get_port_by_index(_plugin, param);
-       return slv2_port_is_a(_plugin, port, _world.event_class)
-               && slv2_port_supports_event(_plugin, port, _world.midi_class);
+       return slv2_port_is_a(_plugin, port, _world.event_class);
+       //      && slv2_port_supports_event(_plugin, port, _world.midi_class);
 }
 
 bool
@@ -620,6 +692,8 @@ LV2PluginInfo::discover (void* lv2_world)
        LV2World* world = (LV2World*)lv2_world;
        SLV2Plugins plugins = slv2_world_get_all_plugins(world->world);
 
+       cerr << "LV2: Discovered " << slv2_plugins_size (plugins) << " plugins\n";
+
        for (unsigned i=0; i < slv2_plugins_size(plugins); ++i) {
                SLV2Plugin p = slv2_plugins_get_at(plugins, i);
                LV2PluginInfoPtr info (new LV2PluginInfo(lv2_world, p));