2 * Copyright (C) 2000-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2005-2006 Sampo Savolainen <v2@iki.fi>
4 * Copyright (C) 2005-2006 Taybin Rutkin <taybin@taybin.com>
5 * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
6 * Copyright (C) 2007-2017 Tim Mayberry <mojofunk@gmail.com>
7 * Copyright (C) 2008-2012 Carl Hetherington <carl@carlh.net>
8 * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
9 * Copyright (C) 2013 John Emmas <john@creativepost.co.uk>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "libardour-config.h"
36 #include <cstdio> // so libraptor doesn't complain
48 #include "pbd/compose.h"
49 #include "pbd/error.h"
50 #include "pbd/locale_guard.h"
51 #include "pbd/xml++.h"
52 #include "pbd/stacktrace.h"
54 #include "ardour/session.h"
55 #include "ardour/ladspa_plugin.h"
56 #include "ardour/buffer_set.h"
57 #include "ardour/audio_buffer.h"
59 #include "pbd/stl_delete.h"
65 using namespace ARDOUR;
68 LadspaPlugin::LadspaPlugin (string module_path, AudioEngine& e, Session& session, uint32_t index, samplecnt_t rate)
71 init (module_path, index, rate);
74 LadspaPlugin::LadspaPlugin (const LadspaPlugin &other)
77 init (other._module_path, other._index, other._sample_rate);
79 for (uint32_t i = 0; i < parameter_count(); ++i) {
80 _control_data[i] = other._shadow_data[i];
81 _shadow_data[i] = other._shadow_data[i];
86 LadspaPlugin::init (string module_path, uint32_t index, samplecnt_t rate)
89 LADSPA_Descriptor_Function dfunc;
92 _module_path = module_path;
93 _module = new Glib::Module(_module_path);
96 _latency_control_port = 0;
97 _was_activated = false;
100 warning << _("LADSPA: Unable to open module: ") << Glib::Module::get_last_error() << endmsg;
102 throw failed_constructor();
105 if (!_module->get_symbol("ladspa_descriptor", func)) {
106 warning << _("LADSPA: module has no descriptor function.") << endmsg;
107 throw failed_constructor();
110 dfunc = (LADSPA_Descriptor_Function)func;
112 if ((_descriptor = dfunc (index)) == 0) {
113 warning << _("LADSPA: plugin has gone away since discovery!") << endmsg;
114 throw failed_constructor();
119 if (LADSPA_IS_INPLACE_BROKEN(_descriptor->Properties)) {
120 info << string_compose(_("LADSPA: \"%1\" cannot be used, since it cannot do inplace processing"), _descriptor->Name) << endmsg;
121 throw failed_constructor();
126 if (_descriptor->instantiate == 0) {
127 throw failed_constructor();
130 if ((_handle = _descriptor->instantiate (_descriptor, rate)) == 0) {
131 throw failed_constructor();
134 port_cnt = parameter_count();
136 _control_data = new LADSPA_Data[port_cnt];
137 memset (_control_data, 0, sizeof (LADSPA_Data) * port_cnt);
138 _shadow_data = new LADSPA_Data[port_cnt];
139 memset (_shadow_data, 0, sizeof (LADSPA_Data) * port_cnt);
141 for (i = 0; i < port_cnt; ++i) {
142 if (LADSPA_IS_PORT_CONTROL(port_descriptor (i))) {
143 connect_port (i, &_control_data[i]);
145 if (LADSPA_IS_PORT_OUTPUT(port_descriptor (i)) &&
146 strcmp (port_names()[i], X_("latency")) == 0) {
147 _latency_control_port = &_control_data[i];
148 *_latency_control_port = 0;
151 _shadow_data[i] = _default_value (i);
152 _control_data[i] = _shadow_data[i];
156 latency_compute_run ();
159 LadspaPlugin::~LadspaPlugin ()
164 // glib has internal reference counting on modules so this is ok
167 delete [] _control_data;
168 delete [] _shadow_data;
172 LadspaPlugin::unique_id() const
175 snprintf (buf, sizeof (buf), "%lu", _descriptor->UniqueID);
180 LadspaPlugin::_default_value (uint32_t port) const
182 const LADSPA_PortRangeHint *prh = port_range_hints();
184 bool bounds_given = false;
185 bool sr_scaling = false;
186 bool earlier_hint = false;
188 /* defaults - case 1 */
190 if (LADSPA_IS_HINT_HAS_DEFAULT(prh[port].HintDescriptor)) {
191 if (LADSPA_IS_HINT_DEFAULT_MINIMUM(prh[port].HintDescriptor)) {
192 ret = prh[port].LowerBound;
197 else if (LADSPA_IS_HINT_DEFAULT_LOW(prh[port].HintDescriptor)) {
198 ret = prh[port].LowerBound * 0.75f + prh[port].UpperBound * 0.25f;
202 else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(prh[port].HintDescriptor)) {
203 ret = prh[port].LowerBound * 0.5f + prh[port].UpperBound * 0.5f;
207 else if (LADSPA_IS_HINT_DEFAULT_HIGH(prh[port].HintDescriptor)) {
208 ret = prh[port].LowerBound * 0.25f + prh[port].UpperBound * 0.75f;
212 else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(prh[port].HintDescriptor)) {
213 ret = prh[port].UpperBound;
217 else if (LADSPA_IS_HINT_DEFAULT_0(prh[port].HintDescriptor)) {
221 else if (LADSPA_IS_HINT_DEFAULT_1(prh[port].HintDescriptor)) {
225 else if (LADSPA_IS_HINT_DEFAULT_100(prh[port].HintDescriptor)) {
229 else if (LADSPA_IS_HINT_DEFAULT_440(prh[port].HintDescriptor)) {
239 /* defaults - case 2 */
240 else if (LADSPA_IS_HINT_BOUNDED_BELOW(prh[port].HintDescriptor) &&
241 !LADSPA_IS_HINT_BOUNDED_ABOVE(prh[port].HintDescriptor)) {
243 if (prh[port].LowerBound < 0) {
246 ret = prh[port].LowerBound;
253 /* defaults - case 3 */
254 else if (!LADSPA_IS_HINT_BOUNDED_BELOW(prh[port].HintDescriptor) &&
255 LADSPA_IS_HINT_BOUNDED_ABOVE(prh[port].HintDescriptor)) {
257 if (prh[port].UpperBound > 0) {
260 ret = prh[port].UpperBound;
267 /* defaults - case 4 */
268 else if (LADSPA_IS_HINT_BOUNDED_BELOW(prh[port].HintDescriptor) &&
269 LADSPA_IS_HINT_BOUNDED_ABOVE(prh[port].HintDescriptor)) {
271 if (prh[port].LowerBound < 0 && prh[port].UpperBound > 0) {
273 } else if (prh[port].LowerBound < 0 && prh[port].UpperBound < 0) {
274 ret = prh[port].UpperBound;
276 ret = prh[port].LowerBound;
282 /* defaults - case 5 */
284 if (LADSPA_IS_HINT_SAMPLE_RATE(prh[port].HintDescriptor) && !earlier_hint) {
298 LadspaPlugin::set_parameter (uint32_t which, float val)
300 if (which < _descriptor->PortCount) {
302 if (get_parameter (which) == val) {
306 _shadow_data[which] = (LADSPA_Data) val;
309 if (which < parameter_count() && controls[which]) {
310 controls[which]->Changed ();
315 warning << string_compose (_("illegal parameter number used with plugin \"%1\". This may "
316 "indicate a change in the plugin design, and presets may be "
321 Plugin::set_parameter (which, val);
324 /** @return `plugin' value */
326 LadspaPlugin::get_parameter (uint32_t which) const
328 if (LADSPA_IS_PORT_INPUT(port_descriptor (which))) {
329 return (float) _shadow_data[which];
331 return (float) _control_data[which];
336 LadspaPlugin::nth_parameter (uint32_t n, bool& ok) const
342 for (c = 0, x = 0; x < _descriptor->PortCount; ++x) {
343 if (LADSPA_IS_PORT_CONTROL (port_descriptor (x))) {
354 LadspaPlugin::add_state (XMLNode* root) const
358 for (uint32_t i = 0; i < parameter_count(); ++i){
360 if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) &&
361 LADSPA_IS_PORT_CONTROL(port_descriptor (i))){
363 child = new XMLNode("Port");
364 child->set_property("number", i);
365 child->set_property("value", _shadow_data[i]);
366 root->add_child_nocopy (*child);
372 LadspaPlugin::set_state (const XMLNode& node, int version)
374 if (version < 3000) {
375 return set_state_2X (node, version);
379 XMLNodeConstIterator iter;
382 if (node.name() != state_node_name()) {
383 error << _("Bad node sent to LadspaPlugin::set_state") << endmsg;
387 nodes = node.children ("Port");
389 for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
396 if (!child->get_property ("number", port_id)) {
397 warning << _("LADSPA: no ladspa port number") << endmsg;
401 if (!child->get_property ("value", value)) {
402 warning << _("LADSPA: no ladspa port data") << endmsg;
406 set_parameter (port_id, value);
409 latency_compute_run ();
411 return Plugin::set_state (node, version);
415 LadspaPlugin::set_state_2X (const XMLNode& node, int /* version */)
418 XMLProperty const * prop;
419 XMLNodeConstIterator iter;
426 if (node.name() != state_node_name()) {
427 error << _("Bad node sent to LadspaPlugin::set_state") << endmsg;
431 nodes = node.children ("port");
433 for(iter = nodes.begin(); iter != nodes.end(); ++iter){
437 if ((prop = child->property("number")) != 0) {
438 port = prop->value().c_str();
440 warning << _("LADSPA: no ladspa port number") << endmsg;
443 if ((prop = child->property("value")) != 0) {
444 data = prop->value().c_str();
446 warning << _("LADSPA: no ladspa port data") << endmsg;
450 sscanf (port, "%" PRIu32, &port_id);
451 set_parameter (port_id, atof(data));
454 latency_compute_run ();
460 LadspaPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
462 LADSPA_PortRangeHint prh;
464 prh = port_range_hints()[which];
467 if (LADSPA_IS_HINT_BOUNDED_BELOW(prh.HintDescriptor)) {
468 if (LADSPA_IS_HINT_SAMPLE_RATE(prh.HintDescriptor)) {
469 desc.lower = prh.LowerBound * _session.sample_rate();
471 desc.lower = prh.LowerBound;
478 if (LADSPA_IS_HINT_BOUNDED_ABOVE(prh.HintDescriptor)) {
479 if (LADSPA_IS_HINT_SAMPLE_RATE(prh.HintDescriptor)) {
480 desc.upper = prh.UpperBound * _session.sample_rate();
482 desc.upper = prh.UpperBound;
485 if (LADSPA_IS_HINT_TOGGLED (prh.HintDescriptor)) {
488 desc.upper = 4; /* completely arbitrary */
492 if (LADSPA_IS_HINT_HAS_DEFAULT (prh.HintDescriptor)) {
493 desc.normal = _default_value(which);
495 /* if there is no explicit hint for the default
496 * value, use lower bound. This is not great but
497 * better than just assuming '0' which may be out-of range.
499 desc.normal = desc.lower;
502 desc.toggled = LADSPA_IS_HINT_TOGGLED (prh.HintDescriptor);
503 desc.logarithmic = LADSPA_IS_HINT_LOGARITHMIC (prh.HintDescriptor);
504 desc.sr_dependent = LADSPA_IS_HINT_SAMPLE_RATE (prh.HintDescriptor);
505 desc.integer_step = LADSPA_IS_HINT_INTEGER (prh.HintDescriptor);
507 desc.label = port_names()[which];
509 desc.scale_points = get_scale_points(which);
516 LadspaPlugin::describe_parameter (Evoral::Parameter which)
518 if (which.type() == PluginAutomation && which.id() < parameter_count()) {
519 return port_names()[which.id()];
526 LadspaPlugin::plugin_latency () const
528 if (_latency_control_port) {
529 return (samplecnt_t) floor (*_latency_control_port);
535 set<Evoral::Parameter>
536 LadspaPlugin::automatable () const
538 set<Evoral::Parameter> ret;
540 for (uint32_t i = 0; i < parameter_count(); ++i){
541 if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) &&
542 LADSPA_IS_PORT_CONTROL(port_descriptor (i))){
544 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
552 LadspaPlugin::connect_and_run (BufferSet& bufs,
553 samplepos_t start, samplepos_t end, double speed,
554 ChanMapping const& in_map, ChanMapping const& out_map,
555 pframes_t nframes, samplecnt_t offset)
557 Plugin::connect_and_run (bufs, start, end, speed, in_map, out_map, nframes, offset);
560 cycles_t then = get_cycles ();
562 BufferSet& silent_bufs = _session.get_silent_buffers(ChanCount(DataType::AUDIO, 1));
563 BufferSet& scratch_bufs = _session.get_scratch_buffers(ChanCount(DataType::AUDIO, 1));
565 uint32_t audio_in_index = 0;
566 uint32_t audio_out_index = 0;
568 for (uint32_t port_index = 0; port_index < parameter_count(); ++port_index) {
569 if (LADSPA_IS_PORT_AUDIO(port_descriptor(port_index))) {
570 if (LADSPA_IS_PORT_INPUT(port_descriptor(port_index))) {
571 const uint32_t buf_index = in_map.get(DataType::AUDIO, audio_in_index++, &valid);
572 connect_port(port_index,
573 valid ? bufs.get_audio(buf_index).data(offset)
574 : silent_bufs.get_audio(0).data(offset));
575 } else if (LADSPA_IS_PORT_OUTPUT(port_descriptor(port_index))) {
576 const uint32_t buf_index = out_map.get(DataType::AUDIO, audio_out_index++, &valid);
577 connect_port(port_index,
578 valid ? bufs.get_audio(buf_index).data(offset)
579 : scratch_bufs.get_audio(0).data(offset));
584 run_in_place (nframes);
586 set_cycles ((uint32_t) (now - then));
592 LadspaPlugin::parameter_is_control (uint32_t param) const
594 return LADSPA_IS_PORT_CONTROL(port_descriptor (param));
598 LadspaPlugin::parameter_is_audio (uint32_t param) const
600 return LADSPA_IS_PORT_AUDIO(port_descriptor (param));
604 LadspaPlugin::parameter_is_output (uint32_t param) const
606 return LADSPA_IS_PORT_OUTPUT(port_descriptor (param));
610 LadspaPlugin::parameter_is_input (uint32_t param) const
612 return LADSPA_IS_PORT_INPUT(port_descriptor (param));
615 boost::shared_ptr<ScalePoints>
616 LadspaPlugin::get_scale_points(uint32_t port_index) const
618 boost::shared_ptr<ScalePoints> ret;
620 const uint32_t id = atol(unique_id().c_str());
621 lrdf_defaults* points = lrdf_get_scale_values(id, port_index);
627 ret = boost::shared_ptr<ScalePoints>(new ScalePoints());
629 for (uint32_t i = 0; i < points->count; ++i) {
630 ret->insert(make_pair(points->items[i].label,
631 points->items[i].value));
634 lrdf_free_setting_values(points);
640 LadspaPlugin::run_in_place (pframes_t nframes)
642 for (uint32_t i = 0; i < parameter_count(); ++i) {
643 if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) && LADSPA_IS_PORT_CONTROL(port_descriptor (i))) {
644 _control_data[i] = _shadow_data[i];
648 assert (_was_activated);
650 _descriptor->run (_handle, nframes);
654 LadspaPlugin::latency_compute_run ()
656 if (!_latency_control_port) {
660 /* we need to run the plugin so that it can set its latency
666 uint32_t port_index = 0;
667 uint32_t in_index = 0;
668 uint32_t out_index = 0;
669 const samplecnt_t bufsize = 1024;
670 LADSPA_Data buffer[bufsize];
672 memset(buffer,0,sizeof(LADSPA_Data)*bufsize);
674 /* Note that we've already required that plugins
675 be able to handle in-place processing.
680 while (port_index < parameter_count()) {
681 if (LADSPA_IS_PORT_AUDIO (port_descriptor (port_index))) {
682 if (LADSPA_IS_PORT_INPUT (port_descriptor (port_index))) {
683 connect_port (port_index, buffer);
685 } else if (LADSPA_IS_PORT_OUTPUT (port_descriptor (port_index))) {
686 connect_port (port_index, buffer);
693 run_in_place (bufsize);
698 LadspaPluginInfo::load (Session& session)
701 PluginPtr plugin (new LadspaPlugin (path, session.engine(), session, index, session.sample_rate()));
702 plugin->set_info(PluginInfoPtr(new LadspaPluginInfo(*this)));
706 catch (failed_constructor &err) {
707 return PluginPtr ((Plugin*) 0);
711 std::vector<Plugin::PresetRecord>
712 LadspaPluginInfo::get_presets (bool /*user_only*/) const
714 std::vector<Plugin::PresetRecord> p;
716 if (!isdigit (unique_id[0])) {
719 uint32_t id = atol (unique_id);
720 lrdf_uris* set_uris = lrdf_get_setting_uris(id);
723 for (uint32_t i = 0; i < (uint32_t) set_uris->count; ++i) {
724 if (char* label = lrdf_get_label (set_uris->items[i])) {
725 p.push_back (Plugin::PresetRecord (set_uris->items[i], label));
728 lrdf_free_uris(set_uris);
734 LadspaPluginInfo::LadspaPluginInfo()
736 type = ARDOUR::LADSPA;
741 LadspaPlugin::find_presets ()
745 std::string unique (unique_id());
747 if (!isdigit (unique[0])) {
751 id = atol (unique.c_str());
753 lrdf_uris* set_uris = lrdf_get_setting_uris(id);
756 for (uint32_t i = 0; i < (uint32_t) set_uris->count; ++i) {
757 if (char* label = lrdf_get_label(set_uris->items[i])) {
758 PresetRecord rec (set_uris->items[i], label);
759 _presets.insert (make_pair (set_uris->items[i], rec));
762 lrdf_free_uris(set_uris);
769 LadspaPlugin::load_preset (PresetRecord r)
772 lrdf_defaults* defs = lrdf_get_setting_values (r.uri.c_str());
775 for (uint32_t i = 0; i < (uint32_t) defs->count; ++i) {
776 if (parameter_is_input (defs->items[i].pid)) {
777 set_parameter(defs->items[i].pid, defs->items[i].value);
778 PresetPortSetValue (defs->items[i].pid, defs->items[i].value); /* EMIT SIGNAL */
781 lrdf_free_setting_values(defs);
784 Plugin::load_preset (r);
790 /* XXX: should be in liblrdf */
792 lrdf_remove_preset (const char* /*source*/, const char *setting_uri)
797 char setting_uri_copy[64];
800 strncpy(setting_uri_copy, setting_uri, sizeof(setting_uri_copy));
802 p.subject = setting_uri_copy;
803 strncpy(buf, LADSPA_BASE "hasPortValue", sizeof(buf));
806 q = lrdf_matches(&p);
810 for (i = q; i; i = i->next) {
811 p.subject = i->object;
812 lrdf_remove_matches(&p);
815 lrdf_free_statements(q);
818 strncpy(buf, LADSPA_BASE "hasSetting", sizeof(buf));
820 p.object = setting_uri_copy;
821 lrdf_remove_matches(&p);
823 p.subject = setting_uri_copy;
826 lrdf_remove_matches (&p);
831 LadspaPlugin::do_remove_preset (string name)
834 string const envvar = preset_envvar ();
835 if (envvar.empty()) {
836 warning << _("Could not locate HOME. Preset not removed.") << endmsg;
840 Plugin::PresetRecord const * p = preset_by_label (name);
845 string const source = preset_source (envvar);
846 lrdf_remove_preset (source.c_str(), p->uri.c_str ());
848 write_preset_file (envvar);
853 LadspaPlugin::preset_envvar () const
856 if ((envvar = getenv ("HOME")) == 0) {
864 LadspaPlugin::preset_source (string envvar) const
866 return string_compose ("file:%1/.ladspa/rdf/ardour-presets.n3", envvar);
870 LadspaPlugin::write_preset_file (string envvar)
873 string path = string_compose("%1/.ladspa", envvar);
874 if (g_mkdir_with_parents (path.c_str(), 0775)) {
875 warning << string_compose(_("Could not create %1. Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
880 if (g_mkdir_with_parents (path.c_str(), 0775)) {
881 warning << string_compose(_("Could not create %1. Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
885 string const source = preset_source (envvar);
887 if (lrdf_export_by_source (source.c_str(), source.substr(5).c_str())) {
888 warning << string_compose(_("Error saving presets file %1."), source) << endmsg;
899 LadspaPlugin::do_save_preset (string name)
902 /* make a vector of pids that are input parameters */
903 vector<int> input_parameter_pids;
904 for (uint32_t i = 0; i < parameter_count(); ++i) {
905 if (parameter_is_input (i)) {
906 input_parameter_pids.push_back (i);
910 std::string unique (unique_id());
912 if (!isdigit (unique[0])) {
916 uint32_t const id = atol (unique.c_str());
918 lrdf_defaults defaults;
919 defaults.count = input_parameter_pids.size ();
920 std::vector<lrdf_portvalue> portvalues(input_parameter_pids.size());
921 defaults.items = &portvalues[0];
923 for (vector<int>::size_type i = 0; i < input_parameter_pids.size(); ++i) {
924 portvalues[i].pid = input_parameter_pids[i];
925 portvalues[i].value = get_parameter (input_parameter_pids[i]);
928 string const envvar = preset_envvar ();
929 if (envvar.empty()) {
930 warning << _("Could not locate HOME. Preset not saved.") << endmsg;
934 string const source = preset_source (envvar);
936 char* uri_char = lrdf_add_preset (source.c_str(), name.c_str(), id, &defaults);
937 string uri (uri_char);
940 if (!write_preset_file (envvar)) {
950 LADSPA_PortDescriptor
951 LadspaPlugin::port_descriptor (uint32_t i) const
953 if (i < _descriptor->PortCount) {
954 return _descriptor->PortDescriptors[i];
957 warning << "LADSPA plugin port index " << i << " out of range." << endmsg;