Another not-quite-there-but-better commit.
[ardour.git] / libs / ardour / ladspa_plugin.cc
index 2c72eb6b12479443b91e182c78a16a21bc617a6a..42a593afd96da4389fca335f7af8ac1a71781ca4 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2000-2002 Paul Davis 
+    Copyright (C) 2000-2006 Paul Davis 
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
+#define __STDC_FORMAT_MACROS 1
+#include <inttypes.h>
+
 #include <vector>
 #include <string>
 
@@ -41,6 +43,7 @@
 #include <ardour/session.h>
 #include <ardour/audioengine.h>
 #include <ardour/ladspa_plugin.h>
+#include <ardour/buffer_set.h>
 
 #include <pbd/stl_delete.h>
 
@@ -49,8 +52,9 @@
 
 using namespace std;
 using namespace ARDOUR;
+using namespace PBD;
 
-LadspaPlugin::LadspaPlugin (void *mod, AudioEngine& e, Session& session, uint32_t index, jack_nframes_t rate)
+LadspaPlugin::LadspaPlugin (void *mod, AudioEngine& e, Session& session, uint32_t index, nframes_t rate)
        : Plugin (e, session)
 {
        init (mod, index, rate);
@@ -68,7 +72,7 @@ LadspaPlugin::LadspaPlugin (const LadspaPlugin &other)
 }
 
 void
-LadspaPlugin::init (void *mod, uint32_t index, jack_nframes_t rate)
+LadspaPlugin::init (void *mod, uint32_t index, nframes_t rate)
 {
        LADSPA_Descriptor_Function dfunc;
        uint32_t i, port_cnt;
@@ -132,20 +136,7 @@ LadspaPlugin::init (void *mod, uint32_t index, jack_nframes_t rate)
                }
        }
 
-       Plugin::setup_midi_controls ();
-
        latency_compute_run ();
-
-       MIDI::Controllable *mcontrol;
-
-       for (uint32_t i = 0; i < parameter_count(); ++i) {
-               if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) &&
-                   LADSPA_IS_PORT_CONTROL(port_descriptor (i))) {
-                       if ((mcontrol = get_nth_midi_control (i)) != 0) {
-                               mcontrol->midi_rebind (_session.midi_port(), 0);
-                       }
-               }
-       }
 }
 
 LadspaPlugin::~LadspaPlugin ()
@@ -153,7 +144,7 @@ LadspaPlugin::~LadspaPlugin ()
        deactivate ();
        cleanup ();
 
-       GoingAway (this); /* EMIT SIGNAL */
+       GoingAway (); /* EMIT SIGNAL */
        
        /* XXX who should close a plugin? */
 
@@ -202,6 +193,7 @@ LadspaPlugin::default_value (uint32_t port)
        float ret = 0.0f;
        bool bounds_given = false;
        bool sr_scaling = false;
+       bool earlier_hint = false;
 
        /* defaults - case 1 */
        
@@ -210,6 +202,7 @@ LadspaPlugin::default_value (uint32_t port)
                        ret = prh[port].LowerBound;
                        bounds_given = true;
                        sr_scaling = true;
+                       earlier_hint = true;
                }
                
                /* FIXME: add support for logarithmic defaults */
@@ -218,33 +211,41 @@ LadspaPlugin::default_value (uint32_t port)
                        ret = prh[port].LowerBound * 0.75f + prh[port].UpperBound * 0.25f;
                        bounds_given = true;
                        sr_scaling = true;
+                       earlier_hint = true;
                }
                else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(prh[port].HintDescriptor)) {
                        ret = prh[port].LowerBound * 0.50f + prh[port].UpperBound * 0.50f;
                        bounds_given = true;
                        sr_scaling = true;
+                       earlier_hint = true;
                }
                else if (LADSPA_IS_HINT_DEFAULT_HIGH(prh[port].HintDescriptor)) {
                        ret = prh[port].LowerBound * 0.25f + prh[port].UpperBound * 0.75f;
                        bounds_given = true;
                        sr_scaling = true;
+                       earlier_hint = true;
                }
                else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(prh[port].HintDescriptor)) {
                        ret = prh[port].UpperBound;
                        bounds_given = true;
                        sr_scaling = true;
+                       earlier_hint = true;
                }
                else if (LADSPA_IS_HINT_DEFAULT_0(prh[port].HintDescriptor)) {
                        ret = 0.0f;
+                       earlier_hint = true;
                }
                else if (LADSPA_IS_HINT_DEFAULT_1(prh[port].HintDescriptor)) {
                        ret = 1.0f;
+                       earlier_hint = true;
                }
                else if (LADSPA_IS_HINT_DEFAULT_100(prh[port].HintDescriptor)) {
                        ret = 100.0f;
+                       earlier_hint = true;
                }
                else if (LADSPA_IS_HINT_DEFAULT_440(prh[port].HintDescriptor)) {
                        ret = 440.0f;
+                       earlier_hint = true;
                }
                else {
                        /* no hint found */
@@ -297,7 +298,7 @@ LadspaPlugin::default_value (uint32_t port)
        
        /* defaults - case 5 */
                
-       if (LADSPA_IS_HINT_SAMPLE_RATE(prh[port].HintDescriptor)) {
+       if (LADSPA_IS_HINT_SAMPLE_RATE(prh[port].HintDescriptor) && !earlier_hint) {
                if (bounds_given) {
                        if (sr_scaling) {
                                ret *= sample_rate;
@@ -315,19 +316,18 @@ LadspaPlugin::set_parameter (uint32_t which, float val)
 {
        if (which < descriptor->PortCount) {
                shadow_data[which] = (LADSPA_Data) val;
-               ParameterChanged (which, val); /* EMIT SIGNAL */
-
-               if (session().get_midi_feedback()) {
+#if 0
+               ParameterChanged (ParamID(PluginAutomation, which), val); /* EMIT SIGNAL */
 
-                       if (which < parameter_count() && midi_controls[which]) {
-                               midi_controls[which]->send_feedback (val);
-                       }
+               if (which < parameter_count() && controls[which]) {
+                       controls[which]->Changed ();
                }
-
+#endif
+               
        } else {
                warning << string_compose (_("illegal parameter number used with plugin \"%1\". This may"
-                                     "indicate a change in the plugin design, and presets may be"
-                                     "invalid"), name())
+                                            "indicate a change in the plugin design, and presets may be"
+                                            "invalid"), name())
                        << endmsg;
        }
 }
@@ -379,28 +379,6 @@ LadspaPlugin::get_state()
                        snprintf(buf, sizeof(buf), "%+f", shadow_data[i]);
                        child->add_property("value", string(buf));
                        root->add_child_nocopy (*child);
-
-                       MIDI::Controllable *pcontrol = get_nth_midi_control (i);
-                       
-                       if (pcontrol) {
-
-                               MIDI::eventType ev;
-                               MIDI::byte      additional;
-                               MIDI::channel_t chn;
-                               XMLNode*        midi_node;
-
-                               if (pcontrol->get_control_info (chn, ev, additional)) {
-
-                                       midi_node = child->add_child ("midi-control");
-               
-                                       snprintf (buf, sizeof(buf), "0x%x", ev);
-                                       midi_node->add_property ("event", buf);
-                                       snprintf (buf, sizeof(buf), "%d", chn);
-                                       midi_node->add_property ("channel", buf);
-                                       snprintf (buf, sizeof(buf), "0x%x", additional);
-                                       midi_node->add_property ("additional", buf);
-                               }
-                       }
                }
        }
 
@@ -451,52 +429,6 @@ LadspaPlugin::set_state(const XMLNode& node)
 
                sscanf (port, "%" PRIu32, &port_id);
                set_parameter (port_id, atof(data));
-
-               XMLNodeList midi_kids;
-               XMLNodeConstIterator iter;
-               
-               midi_kids = child->children ("midi-control");
-               
-               for (iter = midi_kids.begin(); iter != midi_kids.end(); ++iter) {
-                       
-                       child = *iter;
-
-                       MIDI::eventType ev = MIDI::on; /* initialize to keep gcc happy */
-                       MIDI::byte additional = 0; /* initialize to keep gcc happy */
-                       MIDI::channel_t chn = 0; /* initialize to keep gcc happy */
-                       bool ok = true;
-                       int xx;
-                       
-                       if ((prop = child->property ("event")) != 0) {
-                               sscanf (prop->value().c_str(), "0x%x", &xx);
-                               ev = (MIDI::eventType) xx;
-                       } else {
-                               ok = false;
-                       }
-                       
-                       if (ok && ((prop = child->property ("channel")) != 0)) {
-                               sscanf (prop->value().c_str(), "%d", &xx);
-                               chn = (MIDI::channel_t) xx;
-                       } else {
-                               ok = false;
-                       }
-                       
-                       if (ok && ((prop = child->property ("additional")) != 0)) {
-                               sscanf (prop->value().c_str(), "0x%x", &xx);
-                               additional = (MIDI::byte) xx;
-                       }
-                       
-                       if (ok) {
-                               MIDI::Controllable* pcontrol = get_nth_midi_control (port_id);
-
-                               if (pcontrol) {
-                                       pcontrol->set_control_type (chn, ev, additional);
-                               }
-
-                       } else {
-                               error << string_compose(_("LADSPA LadspaPlugin MIDI control specification for port %1 is incomplete, so it has been ignored"), port) << endl;
-                       }
-               }
        }
 
        latency_compute_run ();
@@ -559,37 +491,40 @@ LadspaPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& des
        return 0;
 }
 
-
 string
-LadspaPlugin::describe_parameter (uint32_t which)
+LadspaPlugin::describe_parameter (ParamID which)
 {
-       if (which < parameter_count()) {
-               return port_names()[which];
+       if (which.type() == PluginAutomation && which.id() < parameter_count()) {
+               return port_names()[which.id()];
        } else {
                return "??";
        }
 }
 
-jack_nframes_t
-LadspaPlugin::latency () const
+ARDOUR::nframes_t
+LadspaPlugin::signal_latency () const
 {
+       if (_user_latency) {
+               return _user_latency;
+       }
+
        if (latency_control_port) {
-               return (jack_nframes_t) floor (*latency_control_port);
+               return (nframes_t) floor (*latency_control_port);
        } else {
                return 0;
        }
 }
 
-set<uint32_t>
+set<ParamID>
 LadspaPlugin::automatable () const
 {
-       set<uint32_t> ret;
+       set<ParamID> ret;
 
        for (uint32_t i = 0; i < parameter_count(); ++i){
                if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) && 
                    LADSPA_IS_PORT_CONTROL(port_descriptor (i))){
                        
-                       ret.insert (ret.end(), i);
+                       ret.insert (ret.end(), ParamID(PluginAutomation, i));
                }
        }
 
@@ -597,28 +532,30 @@ LadspaPlugin::automatable () const
 }
 
 int
-LadspaPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t nbufs, int32_t& in_index, int32_t& out_index, jack_nframes_t nframes, jack_nframes_t offset)
+LadspaPlugin::connect_and_run (BufferSet& bufs, uint32_t& in_index, uint32_t& out_index, nframes_t nframes, nframes_t offset)
 {
-       uint32_t port_index;
+       uint32_t port_index = 0;
        cycles_t then, now;
 
-       port_index = 0;
-
        then = get_cycles ();
 
+       const uint32_t nbufs = bufs.count().n_audio();
+
        while (port_index < parameter_count()) {
                if (LADSPA_IS_PORT_AUDIO (port_descriptor(port_index))) {
                        if (LADSPA_IS_PORT_INPUT (port_descriptor(port_index))) {
-                               connect_port (port_index, bufs[min((uint32_t) in_index,nbufs)] + offset);
+                               const size_t index = min(in_index, nbufs - 1);
+                               connect_port (port_index, bufs.get_audio(index).data(nframes, offset));
                                //cerr << this << ' ' << name() << " @ " << offset << " inport " << in_index << " = buf " 
-                               //<< min((uint32_t)in_index,nbufs) << " = " << &bufs[min((uint32_t)in_index,nbufs)][offset] << endl;
+                               //     << min((uint32_t)in_index,nbufs) << " = " << &bufs[min((uint32_t)in_index,nbufs)][offset] << endl;
                                in_index++;
 
 
                        } else if (LADSPA_IS_PORT_OUTPUT (port_descriptor (port_index))) {
-                               connect_port (port_index, bufs[min((uint32_t) out_index,nbufs)] + offset);
-                               //cerr << this << ' ' << name() << " @ " << offset << " outport " << out_index << " = buf " 
-                               //<< min((uint32_t)out_index,nbufs) << " = " << &bufs[min((uint32_t)out_index,nbufs)][offset] << endl;
+                               const size_t index = min(out_index,nbufs - 1);
+                               connect_port (port_index, bufs.get_audio(index).data(nframes, offset));
+                               // cerr << this << ' ' << name() << " @ " << offset << " outport " << out_index << " = buf " 
+                               //     << min((uint32_t)out_index,nbufs) << " = " << &bufs[min((uint32_t)out_index,nbufs)][offset] << endl;
                                out_index++;
                        }
                }
@@ -669,7 +606,7 @@ LadspaPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
 }
 
 void
-LadspaPlugin::run (jack_nframes_t nframes)
+LadspaPlugin::run (nframes_t nframes)
 {
        for (uint32_t i = 0; i < parameter_count(); ++i) {
                if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) && LADSPA_IS_PORT_CONTROL(port_descriptor (i))) {
@@ -695,7 +632,7 @@ LadspaPlugin::latency_compute_run ()
        uint32_t port_index = 0;
        uint32_t in_index = 0;
        uint32_t out_index = 0;
-       const jack_nframes_t bufsize = 1024;
+       const nframes_t bufsize = 1024;
        LADSPA_Data buffer[bufsize];
 
        memset(buffer,0,sizeof(LADSPA_Data)*bufsize);
@@ -722,3 +659,26 @@ LadspaPlugin::latency_compute_run ()
        run (bufsize);
        deactivate ();
 }
+
+PluginPtr
+LadspaPluginInfo::load (Session& session)
+{
+       try {
+               PluginPtr plugin;
+               void *module;
+
+               if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
+                       error << string_compose(_("LADSPA: cannot load module from \"%1\""), path) << endmsg;
+                       error << dlerror() << endmsg;
+               } else {
+                       plugin.reset (new LadspaPlugin (module, session.engine(), session, index, session.frame_rate()));
+               }
+
+               plugin->set_info(PluginInfoPtr(new LadspaPluginInfo(*this)));
+               return plugin;
+       }
+
+       catch (failed_constructor &err) {
+               return PluginPtr ((Plugin*) 0);
+       }       
+}