2 Copyright (C) 2000-2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define __STDC_FORMAT_MACROS 1
28 #include <cstdio> // so libraptor doesn't complain
36 #include <pbd/compose.h>
37 #include <pbd/error.h>
38 #include <pbd/pathscanner.h>
39 #include <pbd/xml++.h>
41 #include <midi++/manager.h>
43 #include <ardour/ardour.h>
44 #include <ardour/session.h>
45 #include <ardour/audioengine.h>
46 #include <ardour/ladspa_plugin.h>
47 #include <ardour/buffer_set.h>
49 #include <pbd/stl_delete.h>
55 using namespace ARDOUR;
58 LadspaPlugin::LadspaPlugin (void *mod, AudioEngine& e, Session& session, uint32_t index, nframes_t rate)
61 init (mod, index, rate);
64 LadspaPlugin::LadspaPlugin (const LadspaPlugin &other)
67 init (other.module, other._index, other.sample_rate);
69 for (uint32_t i = 0; i < parameter_count(); ++i) {
70 control_data[i] = other.shadow_data[i];
71 shadow_data[i] = other.shadow_data[i];
76 LadspaPlugin::init (void *mod, uint32_t index, nframes_t rate)
78 LADSPA_Descriptor_Function dfunc;
85 latency_control_port = 0;
86 was_activated = false;
88 dfunc = (LADSPA_Descriptor_Function) dlsym (module, "ladspa_descriptor");
90 if ((errstr = dlerror()) != NULL) {
91 error << _("LADSPA: module has no descriptor function.") << endmsg;
92 throw failed_constructor();
95 if ((descriptor = dfunc (index)) == 0) {
96 error << _("LADSPA: plugin has gone away since discovery!") << endmsg;
97 throw failed_constructor();
102 if (LADSPA_IS_INPLACE_BROKEN(descriptor->Properties)) {
103 error << string_compose(_("LADSPA: \"%1\" cannot be used, since it cannot do inplace processing"), descriptor->Name) << endmsg;
104 throw failed_constructor();
109 if (descriptor->instantiate == 0) {
110 throw failed_constructor();
113 if ((handle = descriptor->instantiate (descriptor, rate)) == 0) {
114 throw failed_constructor();
117 port_cnt = parameter_count();
119 control_data = new LADSPA_Data[port_cnt];
120 shadow_data = new LADSPA_Data[port_cnt];
122 for (i = 0; i < port_cnt; ++i) {
123 if (LADSPA_IS_PORT_CONTROL(port_descriptor (i))) {
124 connect_port (i, &control_data[i]);
126 if (LADSPA_IS_PORT_OUTPUT(port_descriptor (i)) &&
127 strcmp (port_names()[i], X_("latency")) == 0) {
128 latency_control_port = &control_data[i];
129 *latency_control_port = 0;
132 if (!LADSPA_IS_PORT_INPUT(port_descriptor (i))) {
136 shadow_data[i] = default_value (i);
140 Plugin::setup_controls ();
142 latency_compute_run ();
145 LadspaPlugin::~LadspaPlugin ()
150 GoingAway (); /* EMIT SIGNAL */
152 /* XXX who should close a plugin? */
157 delete [] control_data;
161 delete [] shadow_data;
166 LadspaPlugin::store_state (PluginState& state)
168 state.parameters.clear ();
170 for (uint32_t i = 0; i < parameter_count(); ++i){
172 if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) &&
173 LADSPA_IS_PORT_CONTROL(port_descriptor (i))){
174 pair<uint32_t,float> datum;
177 datum.second = shadow_data[i];
179 state.parameters.insert (datum);
185 LadspaPlugin::restore_state (PluginState& state)
187 for (map<uint32_t,float>::iterator i = state.parameters.begin(); i != state.parameters.end(); ++i) {
188 set_parameter (i->first, i->second);
193 LadspaPlugin::default_value (uint32_t port)
195 const LADSPA_PortRangeHint *prh = port_range_hints();
197 bool bounds_given = false;
198 bool sr_scaling = false;
200 /* defaults - case 1 */
202 if (LADSPA_IS_HINT_HAS_DEFAULT(prh[port].HintDescriptor)) {
203 if (LADSPA_IS_HINT_DEFAULT_MINIMUM(prh[port].HintDescriptor)) {
204 ret = prh[port].LowerBound;
209 /* FIXME: add support for logarithmic defaults */
211 else if (LADSPA_IS_HINT_DEFAULT_LOW(prh[port].HintDescriptor)) {
212 ret = prh[port].LowerBound * 0.75f + prh[port].UpperBound * 0.25f;
216 else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(prh[port].HintDescriptor)) {
217 ret = prh[port].LowerBound * 0.50f + prh[port].UpperBound * 0.50f;
221 else if (LADSPA_IS_HINT_DEFAULT_HIGH(prh[port].HintDescriptor)) {
222 ret = prh[port].LowerBound * 0.25f + prh[port].UpperBound * 0.75f;
226 else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(prh[port].HintDescriptor)) {
227 ret = prh[port].UpperBound;
231 else if (LADSPA_IS_HINT_DEFAULT_0(prh[port].HintDescriptor)) {
234 else if (LADSPA_IS_HINT_DEFAULT_1(prh[port].HintDescriptor)) {
237 else if (LADSPA_IS_HINT_DEFAULT_100(prh[port].HintDescriptor)) {
240 else if (LADSPA_IS_HINT_DEFAULT_440(prh[port].HintDescriptor)) {
249 /* defaults - case 2 */
250 else if (LADSPA_IS_HINT_BOUNDED_BELOW(prh[port].HintDescriptor) &&
251 !LADSPA_IS_HINT_BOUNDED_ABOVE(prh[port].HintDescriptor)) {
253 if (prh[port].LowerBound < 0) {
256 ret = prh[port].LowerBound;
263 /* defaults - case 3 */
264 else if (!LADSPA_IS_HINT_BOUNDED_BELOW(prh[port].HintDescriptor) &&
265 LADSPA_IS_HINT_BOUNDED_ABOVE(prh[port].HintDescriptor)) {
267 if (prh[port].UpperBound > 0) {
270 ret = prh[port].UpperBound;
277 /* defaults - case 4 */
278 else if (LADSPA_IS_HINT_BOUNDED_BELOW(prh[port].HintDescriptor) &&
279 LADSPA_IS_HINT_BOUNDED_ABOVE(prh[port].HintDescriptor)) {
281 if (prh[port].LowerBound < 0 && prh[port].UpperBound > 0) {
283 } else if (prh[port].LowerBound < 0 && prh[port].UpperBound < 0) {
284 ret = prh[port].UpperBound;
286 ret = prh[port].LowerBound;
292 /* defaults - case 5 */
294 if (LADSPA_IS_HINT_SAMPLE_RATE(prh[port].HintDescriptor)) {
308 LadspaPlugin::set_parameter (uint32_t which, float val)
310 if (which < descriptor->PortCount) {
311 shadow_data[which] = (LADSPA_Data) val;
312 ParameterChanged (which, val); /* EMIT SIGNAL */
314 if (which < parameter_count() && controls[which]) {
315 controls[which]->Changed ();
319 warning << string_compose (_("illegal parameter number used with plugin \"%1\". This may"
320 "indicate a change in the plugin design, and presets may be"
327 LadspaPlugin::get_parameter (uint32_t which) const
329 if (LADSPA_IS_PORT_INPUT(port_descriptor (which))) {
330 return (float) shadow_data[which];
332 return (float) control_data[which];
337 LadspaPlugin::nth_parameter (uint32_t n, bool& ok) const
343 for (c = 0, x = 0; x < descriptor->PortCount; ++x) {
344 if (LADSPA_IS_PORT_CONTROL (port_descriptor (x))) {
355 LadspaPlugin::get_state()
357 XMLNode *root = new XMLNode(state_node_name());
360 LocaleGuard lg (X_("POSIX"));
362 for (uint32_t i = 0; i < parameter_count(); ++i){
364 if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) &&
365 LADSPA_IS_PORT_CONTROL(port_descriptor (i))){
367 child = new XMLNode("port");
368 snprintf(buf, sizeof(buf), "%u", i);
369 child->add_property("number", string(buf));
370 snprintf(buf, sizeof(buf), "%+f", shadow_data[i]);
371 child->add_property("value", string(buf));
372 root->add_child_nocopy (*child);
380 LadspaPlugin::save_preset (string name)
382 return Plugin::save_preset (name, "ladspa");
386 LadspaPlugin::set_state(const XMLNode& node)
390 XMLNodeConstIterator iter;
395 LocaleGuard lg (X_("POSIX"));
397 if (node.name() != state_node_name()) {
398 error << _("Bad node sent to LadspaPlugin::set_state") << endmsg;
402 nodes = node.children ("port");
404 for(iter = nodes.begin(); iter != nodes.end(); ++iter){
408 if ((prop = child->property("number")) != 0) {
409 port = prop->value().c_str();
411 warning << _("LADSPA: no ladspa port number") << endmsg;
414 if ((prop = child->property("value")) != 0) {
415 data = prop->value().c_str();
417 warning << _("LADSPA: no ladspa port data") << endmsg;
421 sscanf (port, "%" PRIu32, &port_id);
422 set_parameter (port_id, atof(data));
425 latency_compute_run ();
431 LadspaPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
433 LADSPA_PortRangeHint prh;
435 prh = port_range_hints()[which];
438 if (LADSPA_IS_HINT_BOUNDED_BELOW(prh.HintDescriptor)) {
439 desc.min_unbound = false;
440 if (LADSPA_IS_HINT_SAMPLE_RATE(prh.HintDescriptor)) {
441 desc.lower = prh.LowerBound * _session.frame_rate();
443 desc.lower = prh.LowerBound;
446 desc.min_unbound = true;
451 if (LADSPA_IS_HINT_BOUNDED_ABOVE(prh.HintDescriptor)) {
452 desc.max_unbound = false;
453 if (LADSPA_IS_HINT_SAMPLE_RATE(prh.HintDescriptor)) {
454 desc.upper = prh.UpperBound * _session.frame_rate();
456 desc.upper = prh.UpperBound;
459 desc.max_unbound = true;
460 desc.upper = 4; /* completely arbitrary */
463 if (LADSPA_IS_HINT_INTEGER (prh.HintDescriptor)) {
465 desc.smallstep = 0.1;
466 desc.largestep = 10.0;
468 float delta = desc.upper - desc.lower;
469 desc.step = delta / 1000.0f;
470 desc.smallstep = delta / 10000.0f;
471 desc.largestep = delta/10.0f;
474 desc.toggled = LADSPA_IS_HINT_TOGGLED (prh.HintDescriptor);
475 desc.logarithmic = LADSPA_IS_HINT_LOGARITHMIC (prh.HintDescriptor);
476 desc.sr_dependent = LADSPA_IS_HINT_SAMPLE_RATE (prh.HintDescriptor);
477 desc.integer_step = LADSPA_IS_HINT_INTEGER (prh.HintDescriptor);
479 desc.label = port_names()[which];
487 LadspaPlugin::describe_parameter (uint32_t which)
489 if (which < parameter_count()) {
490 return port_names()[which];
497 LadspaPlugin::latency () const
499 if (latency_control_port) {
500 return (nframes_t) floor (*latency_control_port);
507 LadspaPlugin::automatable () const
511 for (uint32_t i = 0; i < parameter_count(); ++i){
512 if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) &&
513 LADSPA_IS_PORT_CONTROL(port_descriptor (i))){
515 ret.insert (ret.end(), i);
523 LadspaPlugin::connect_and_run (BufferSet& bufs, uint32_t& in_index, uint32_t& out_index, nframes_t nframes, nframes_t offset)
525 uint32_t port_index = 0;
528 then = get_cycles ();
530 const uint32_t nbufs = bufs.count().get(DataType::AUDIO);
532 while (port_index < parameter_count()) {
533 if (LADSPA_IS_PORT_AUDIO (port_descriptor(port_index))) {
534 if (LADSPA_IS_PORT_INPUT (port_descriptor(port_index))) {
535 const size_t index = min(in_index, nbufs - 1);
536 connect_port (port_index, bufs.get_audio(index).data(nframes, offset));
537 //cerr << this << ' ' << name() << " @ " << offset << " inport " << in_index << " = buf "
538 // << min((uint32_t)in_index,nbufs) << " = " << &bufs[min((uint32_t)in_index,nbufs)][offset] << endl;
542 } else if (LADSPA_IS_PORT_OUTPUT (port_descriptor (port_index))) {
543 const size_t index = min(out_index,nbufs - 1);
544 connect_port (port_index, bufs.get_audio(index).data(nframes, offset));
545 // cerr << this << ' ' << name() << " @ " << offset << " outport " << out_index << " = buf "
546 // << min((uint32_t)out_index,nbufs) << " = " << &bufs[min((uint32_t)out_index,nbufs)][offset] << endl;
555 set_cycles ((uint32_t) (now - then));
561 LadspaPlugin::parameter_is_control (uint32_t param) const
563 return LADSPA_IS_PORT_CONTROL(port_descriptor (param));
567 LadspaPlugin::parameter_is_audio (uint32_t param) const
569 return LADSPA_IS_PORT_AUDIO(port_descriptor (param));
573 LadspaPlugin::parameter_is_output (uint32_t param) const
575 return LADSPA_IS_PORT_OUTPUT(port_descriptor (param));
579 LadspaPlugin::parameter_is_input (uint32_t param) const
581 return LADSPA_IS_PORT_INPUT(port_descriptor (param));
585 LadspaPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
588 if (param < parameter_count()) {
589 snprintf (buf, len, "%.3f", get_parameter (param));
597 LadspaPlugin::run (nframes_t nframes)
599 for (uint32_t i = 0; i < parameter_count(); ++i) {
600 if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) && LADSPA_IS_PORT_CONTROL(port_descriptor (i))) {
601 control_data[i] = shadow_data[i];
604 descriptor->run (handle, nframes);
608 LadspaPlugin::latency_compute_run ()
610 if (!latency_control_port) {
614 /* we need to run the plugin so that it can set its latency
620 uint32_t port_index = 0;
621 uint32_t in_index = 0;
622 uint32_t out_index = 0;
623 const nframes_t bufsize = 1024;
624 LADSPA_Data buffer[bufsize];
626 memset(buffer,0,sizeof(LADSPA_Data)*bufsize);
628 /* Note that we've already required that plugins
629 be able to handle in-place processing.
634 while (port_index < parameter_count()) {
635 if (LADSPA_IS_PORT_AUDIO (port_descriptor (port_index))) {
636 if (LADSPA_IS_PORT_INPUT (port_descriptor (port_index))) {
637 connect_port (port_index, buffer);
639 } else if (LADSPA_IS_PORT_OUTPUT (port_descriptor (port_index))) {
640 connect_port (port_index, buffer);
652 LadspaPluginInfo::load (Session& session)
658 if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
659 error << string_compose(_("LADSPA: cannot load module from \"%1\""), path) << endmsg;
660 error << dlerror() << endmsg;
662 plugin.reset (new LadspaPlugin (module, session.engine(), session, index, session.frame_rate()));
665 plugin->set_info(PluginInfoPtr(new LadspaPluginInfo(*this)));
669 catch (failed_constructor &err) {
670 return PluginPtr ((Plugin*) 0);