Fix well-known control Lua bindings
[ardour.git] / libs / ardour / ladspa_plugin.cc
index 38d0d8b9447860d33c8de6abe2b9e64e67583156..570c5109ffe44dea075e29cacbf7d7dd43f879bf 100644 (file)
@@ -29,7 +29,9 @@
 #include <cstdlib>
 #include <cstdio> // so libraptor doesn't complain
 #include <cmath>
+#ifndef COMPILER_MSVC
 #include <dirent.h>
+#endif
 #include <sys/stat.h>
 #include <cerrno>
 
 
 #include "pbd/compose.h"
 #include "pbd/error.h"
+#include "pbd/locale_guard.h"
 #include "pbd/xml++.h"
 #include "pbd/stacktrace.h"
 
-#include "midi++/manager.h"
-
 #include "ardour/session.h"
 #include "ardour/ladspa_plugin.h"
 #include "ardour/buffer_set.h"
 
 #include "pbd/stl_delete.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 #include <locale.h>
 
 using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
-LadspaPlugin::LadspaPlugin (string module_path, AudioEngine& e, Session& session, uint32_t index, framecnt_t rate)
+LadspaPlugin::LadspaPlugin (string module_path, AudioEngine& e, Session& session, uint32_t index, samplecnt_t rate)
        : Plugin (e, session)
 {
        init (module_path, index, rate);
@@ -76,7 +77,7 @@ LadspaPlugin::LadspaPlugin (const LadspaPlugin &other)
 }
 
 void
-LadspaPlugin::init (string module_path, uint32_t index, framecnt_t rate)
+LadspaPlugin::init (string module_path, uint32_t index, samplecnt_t rate)
 {
        void* func;
        LADSPA_Descriptor_Function dfunc;
@@ -127,7 +128,9 @@ LadspaPlugin::init (string module_path, uint32_t index, framecnt_t rate)
        port_cnt = parameter_count();
 
        _control_data = new LADSPA_Data[port_cnt];
+       memset (_control_data, 0, sizeof (LADSPA_Data) * port_cnt);
        _shadow_data = new LADSPA_Data[port_cnt];
+       memset (_shadow_data, 0, sizeof (LADSPA_Data) * port_cnt);
 
        for (i = 0; i < port_cnt; ++i) {
                if (LADSPA_IS_PORT_CONTROL(port_descriptor (i))) {
@@ -139,11 +142,8 @@ LadspaPlugin::init (string module_path, uint32_t index, framecnt_t rate)
                                *_latency_control_port = 0;
                        }
 
-                       if (!LADSPA_IS_PORT_INPUT(port_descriptor (i))) {
-                               continue;
-                       }
-
-                       _shadow_data[i] = default_value (i);
+                       _shadow_data[i] = _default_value (i);
+                       _control_data[i] = _shadow_data[i];
                }
        }
 
@@ -171,7 +171,7 @@ LadspaPlugin::unique_id() const
 }
 
 float
-LadspaPlugin::default_value (uint32_t port)
+LadspaPlugin::_default_value (uint32_t port) const
 {
        const LADSPA_PortRangeHint *prh = port_range_hints();
        float ret = 0.0f;
@@ -348,8 +348,6 @@ void
 LadspaPlugin::add_state (XMLNode* root) const
 {
        XMLNode *child;
-       char buf[16];
-       LocaleGuard lg (X_("POSIX"));
 
        for (uint32_t i = 0; i < parameter_count(); ++i){
 
@@ -357,10 +355,8 @@ LadspaPlugin::add_state (XMLNode* root) const
                    LADSPA_IS_PORT_CONTROL(port_descriptor (i))){
 
                        child = new XMLNode("Port");
-                       snprintf(buf, sizeof(buf), "%u", i);
-                       child->add_property("number", string(buf));
-                       snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
-                       child->add_property("value", string(buf));
+                       child->set_property("number", i);
+                       child->set_property("value", _shadow_data[i]);
                        root->add_child_nocopy (*child);
                }
        }
@@ -375,14 +371,9 @@ LadspaPlugin::set_state (const XMLNode& node, int version)
 
 #ifndef NO_PLUGIN_STATE
        XMLNodeList nodes;
-       XMLProperty *prop;
        XMLNodeConstIterator iter;
        XMLNode *child;
-       const char *port;
-       const char *data;
-       uint32_t port_id;
 #endif
-       LocaleGuard lg (X_("POSIX"));
 
        if (node.name() != state_node_name()) {
                error << _("Bad node sent to LadspaPlugin::set_state") << endmsg;
@@ -397,21 +388,20 @@ LadspaPlugin::set_state (const XMLNode& node, int version)
 
                child = *iter;
 
-               if ((prop = child->property("number")) != 0) {
-                       port = prop->value().c_str();
-               } else {
+               uint32_t port_id;
+               float value;
+
+               if (!child->get_property ("number", port_id)) {
                        warning << _("LADSPA: no ladspa port number") << endmsg;
                        continue;
                }
-               if ((prop = child->property("value")) != 0) {
-                       data = prop->value().c_str();
-               } else {
+
+               if (!child->get_property ("value", value)) {
                        warning << _("LADSPA: no ladspa port data") << endmsg;
                        continue;
                }
 
-               sscanf (port, "%" PRIu32, &port_id);
-               set_parameter (port_id, atof(data));
+               set_parameter (port_id, value);
        }
 #endif
 
@@ -425,14 +415,14 @@ LadspaPlugin::set_state_2X (const XMLNode& node, int /* version */)
 {
 #ifndef NO_PLUGIN_STATE
        XMLNodeList nodes;
-       XMLProperty *prop;
+       XMLProperty const * prop;
        XMLNodeConstIterator iter;
        XMLNode *child;
        const char *port;
        const char *data;
        uint32_t port_id;
 #endif
-       LocaleGuard lg (X_("POSIX"));
+       LocaleGuard lg;
 
        if (node.name() != state_node_name()) {
                error << _("Bad node sent to LadspaPlugin::set_state") << endmsg;
@@ -478,39 +468,38 @@ LadspaPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& des
 
 
        if (LADSPA_IS_HINT_BOUNDED_BELOW(prh.HintDescriptor)) {
-               desc.min_unbound = false;
                if (LADSPA_IS_HINT_SAMPLE_RATE(prh.HintDescriptor)) {
-                       desc.lower = prh.LowerBound * _session.frame_rate();
+                       desc.lower = prh.LowerBound * _session.sample_rate();
                } else {
                        desc.lower = prh.LowerBound;
                }
        } else {
-               desc.min_unbound = true;
                desc.lower = 0;
        }
 
 
        if (LADSPA_IS_HINT_BOUNDED_ABOVE(prh.HintDescriptor)) {
-               desc.max_unbound = false;
                if (LADSPA_IS_HINT_SAMPLE_RATE(prh.HintDescriptor)) {
-                       desc.upper = prh.UpperBound * _session.frame_rate();
+                       desc.upper = prh.UpperBound * _session.sample_rate();
                } else {
                        desc.upper = prh.UpperBound;
                }
        } else {
-               desc.max_unbound = true;
-               desc.upper = 4; /* completely arbitrary */
+               if (LADSPA_IS_HINT_TOGGLED (prh.HintDescriptor)) {
+                       desc.upper = 1;
+               } else {
+                       desc.upper = 4; /* completely arbitrary */
+               }
        }
 
-       if (LADSPA_IS_HINT_INTEGER (prh.HintDescriptor)) {
-               desc.step = 1.0;
-               desc.smallstep = 0.1;
-               desc.largestep = 10.0;
+       if (LADSPA_IS_HINT_HAS_DEFAULT (prh.HintDescriptor)) {
+               desc.normal = _default_value(which);
        } else {
-               float delta = desc.upper - desc.lower;
-               desc.step = delta / 1000.0f;
-               desc.smallstep = delta / 10000.0f;
-               desc.largestep = delta/10.0f;
+               /* if there is no explicit hint for the default
+                * value, use lower bound. This is not great but
+                * better than just assuming '0' which may be out-of range.
+                */
+               desc.normal = desc.lower;
        }
 
        desc.toggled = LADSPA_IS_HINT_TOGGLED (prh.HintDescriptor);
@@ -520,6 +509,9 @@ LadspaPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& des
 
        desc.label = port_names()[which];
 
+       desc.scale_points = get_scale_points(which);
+       desc.update_steps();
+
        return 0;
 }
 
@@ -533,7 +525,7 @@ LadspaPlugin::describe_parameter (Evoral::Parameter which)
        }
 }
 
-ARDOUR::framecnt_t
+ARDOUR::samplecnt_t
 LadspaPlugin::signal_latency () const
 {
        if (_user_latency) {
@@ -541,7 +533,7 @@ LadspaPlugin::signal_latency () const
        }
 
        if (_latency_control_port) {
-               return (framecnt_t) floor (*_latency_control_port);
+               return (samplecnt_t) floor (*_latency_control_port);
        } else {
                return 0;
        }
@@ -565,10 +557,11 @@ LadspaPlugin::automatable () const
 
 int
 LadspaPlugin::connect_and_run (BufferSet& bufs,
+               samplepos_t start, samplepos_t end, double speed,
                ChanMapping in_map, ChanMapping out_map,
-               pframes_t nframes, framecnt_t offset)
+               pframes_t nframes, samplecnt_t offset)
 {
-       Plugin::connect_and_run (bufs, in_map, out_map, nframes, offset);
+       Plugin::connect_and_run (bufs, start, end, speed, in_map, out_map, nframes, offset);
 
        cycles_t now;
        cycles_t then = get_cycles ();
@@ -638,10 +631,10 @@ LadspaPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
        }
 }
 
-boost::shared_ptr<Plugin::ScalePoints>
+boost::shared_ptr<ScalePoints>
 LadspaPlugin::get_scale_points(uint32_t port_index) const
 {
-       boost::shared_ptr<Plugin::ScalePoints> ret;
+       boost::shared_ptr<ScalePoints> ret;
 #ifdef HAVE_LRDF
        const uint32_t id     = atol(unique_id().c_str());
        lrdf_defaults* points = lrdf_get_scale_values(id, port_index);
@@ -650,7 +643,7 @@ LadspaPlugin::get_scale_points(uint32_t port_index) const
                return ret;
        }
 
-       ret = boost::shared_ptr<Plugin::ScalePoints>(new ScalePoints());
+       ret = boost::shared_ptr<ScalePoints>(new ScalePoints());
 
        for (uint32_t i = 0; i < points->count; ++i) {
                ret->insert(make_pair(points->items[i].label,
@@ -692,7 +685,7 @@ LadspaPlugin::latency_compute_run ()
        uint32_t port_index = 0;
        uint32_t in_index = 0;
        uint32_t out_index = 0;
-       const framecnt_t bufsize = 1024;
+       const samplecnt_t bufsize = 1024;
        LADSPA_Data buffer[bufsize];
 
        memset(buffer,0,sizeof(LADSPA_Data)*bufsize);
@@ -724,7 +717,7 @@ PluginPtr
 LadspaPluginInfo::load (Session& session)
 {
        try {
-               PluginPtr plugin (new LadspaPlugin (path, session.engine(), session, index, session.frame_rate()));
+               PluginPtr plugin (new LadspaPlugin (path, session.engine(), session, index, session.sample_rate()));
                plugin->set_info(PluginInfoPtr(new LadspaPluginInfo(*this)));
                return plugin;
        }
@@ -734,6 +727,29 @@ LadspaPluginInfo::load (Session& session)
        }
 }
 
+std::vector<Plugin::PresetRecord>
+LadspaPluginInfo::get_presets (bool /*user_only*/) const
+{
+       std::vector<Plugin::PresetRecord> p;
+#if (defined HAVE_LRDF && !defined NO_PLUGIN_STATE)
+       if (!isdigit (unique_id[0])) {
+               return p;
+       }
+       uint32_t id = atol (unique_id);
+       lrdf_uris* set_uris = lrdf_get_setting_uris(id);
+
+       if (set_uris) {
+               for (uint32_t i = 0; i < (uint32_t) set_uris->count; ++i) {
+                       if (char* label = lrdf_get_label (set_uris->items[i])) {
+                               p.push_back (Plugin::PresetRecord (set_uris->items[i], label));
+                       }
+               }
+               lrdf_free_uris(set_uris);
+       }
+#endif
+       return p;
+}
+
 LadspaPluginInfo::LadspaPluginInfo()
 {
        type = ARDOUR::LADSPA;
@@ -778,6 +794,7 @@ LadspaPlugin::load_preset (PresetRecord r)
                for (uint32_t i = 0; i < (uint32_t) defs->count; ++i) {
                        if (parameter_is_input (defs->items[i].pid)) {
                                set_parameter(defs->items[i].pid, defs->items[i].value);
+                               PresetPortSetValue (defs->items[i].pid, defs->items[i].value); /* EMIT SIGNAL */
                        }
                }
                lrdf_free_setting_values(defs);
@@ -788,11 +805,11 @@ LadspaPlugin::load_preset (PresetRecord r)
        return true;
 }
 
+#ifdef HAVE_LRDF
 /* XXX: should be in liblrdf */
 static void
 lrdf_remove_preset (const char* /*source*/, const char *setting_uri)
 {
-#ifdef HAVE_LRDF
        lrdf_statement p;
        lrdf_statement *q;
        lrdf_statement *i;
@@ -826,8 +843,8 @@ lrdf_remove_preset (const char* /*source*/, const char *setting_uri)
        p.predicate = NULL;
        p.object = NULL;
        lrdf_remove_matches (&p);
-#endif
 }
+#endif
 
 void
 LadspaPlugin::do_remove_preset (string name)
@@ -919,8 +936,8 @@ LadspaPlugin::do_save_preset (string name)
 
        lrdf_defaults defaults;
        defaults.count = input_parameter_pids.size ();
-       lrdf_portvalue portvalues[input_parameter_pids.size()];
-       defaults.items = portvalues;
+       std::vector<lrdf_portvalue> portvalues(input_parameter_pids.size());
+       defaults.items = &portvalues[0];
 
        for (vector<int>::size_type i = 0; i < input_parameter_pids.size(); ++i) {
                portvalues[i].pid = input_parameter_pids[i];
@@ -952,13 +969,13 @@ LadspaPlugin::do_save_preset (string name)
 LADSPA_PortDescriptor
 LadspaPlugin::port_descriptor (uint32_t i) const
 {
-       if (i < _descriptor->PortCount) {               
+       if (i < _descriptor->PortCount) {
                return _descriptor->PortDescriptors[i];
        }
-       
+
        warning << "LADSPA plugin port index " << i << " out of range." << endmsg;
        return 0;
 }
 
-               
-       
+
+