2 Copyright (C) 2008 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <pbd/compose.h>
28 #include <pbd/error.h>
29 #include <pbd/pathscanner.h>
30 #include <pbd/xml++.h>
32 #include <ardour/ardour.h>
33 #include <ardour/session.h>
34 #include <ardour/audioengine.h>
35 #include <ardour/lv2_plugin.h>
37 #include <pbd/stl_delete.h>
43 using namespace ARDOUR;
46 LV2Plugin::LV2Plugin (AudioEngine& e, Session& session, SLV2Plugin plugin, nframes_t rate)
52 LV2Plugin::LV2Plugin (const LV2Plugin &other)
55 init (other._plugin, other._sample_rate);
57 for (uint32_t i = 0; i < parameter_count(); ++i) {
58 _control_data[i] = other._shadow_data[i];
59 _shadow_data[i] = other._shadow_data[i];
64 LV2Plugin::init (SLV2Plugin plugin, nframes_t rate)
67 _template = slv2_plugin_get_template(plugin);
70 _latency_control_port = 0;
71 _was_activated = false;
73 _instance = slv2_plugin_instantiate(plugin, rate, NULL);
76 error << _("LV2: Failed to instantiate plugin ") << slv2_plugin_get_uri(plugin) << endl;
77 throw failed_constructor();
80 if (slv2_plugin_has_feature(plugin, "http://lv2plug.in/ns/lv2core#inPlaceBroken")) {
81 error << string_compose(_("LV2: \"%1\" cannot be used, since it cannot do inplace processing"), slv2_plugin_get_name(plugin)) << endmsg;
82 throw failed_constructor();
87 const uint32_t num_ports = slv2_plugin_get_num_ports(plugin);
89 _control_data = new float[num_ports];
90 _shadow_data = new float[num_ports];
92 const bool latent = slv2_plugin_has_latency(plugin);
93 uint32_t latency_port = (latent ? slv2_plugin_get_latency_port(plugin) : 0);
95 for (uint32_t i = 0; i < num_ports; ++i) {
96 if (parameter_is_control(i)) {
97 slv2_instance_connect_port (_instance, i, &_control_data[i]);
99 if (latent && i == latency_port) {
100 _latency_control_port = &_control_data[i];
101 *_latency_control_port = 0;
104 if (parameter_is_input(i)) {
105 _shadow_data[i] = default_value (i);
110 Plugin::setup_controls ();
112 latency_compute_run ();
115 LV2Plugin::~LV2Plugin ()
120 GoingAway (); /* EMIT SIGNAL */
122 slv2_instance_free(_instance);
125 delete [] _control_data;
129 delete [] _shadow_data;
134 LV2Plugin::unique_id() const
136 return slv2_plugin_get_uri(_plugin);
141 LV2Plugin::default_value (uint32_t port)
143 return slv2_port_get_default_value(_plugin,
144 slv2_plugin_get_port_by_index(_plugin, port));
148 LV2Plugin::set_parameter (uint32_t which, float val)
150 if (which < slv2_plugin_get_num_ports(_plugin)) {
151 _shadow_data[which] = val;
152 ParameterChanged (which, val); /* EMIT SIGNAL */
154 if (which < parameter_count() && controls[which]) {
155 controls[which]->Changed ();
159 warning << string_compose (_("Illegal parameter number used with plugin \"%1\"."
160 "This is a bug in either Ardour or the LV2 plugin (%2)"),
161 name(), unique_id()) << endmsg;
166 LV2Plugin::get_parameter (uint32_t which) const
168 if (parameter_is_input(which)) {
169 return (float) _shadow_data[which];
171 return (float) _control_data[which];
177 LV2Plugin::nth_parameter (uint32_t n, bool& ok) const
183 for (c = 0, x = 0; x < slv2_plugin_get_num_ports(_plugin); ++x) {
184 if (parameter_is_control (x)) {
196 LV2Plugin::get_state()
198 XMLNode *root = new XMLNode(state_node_name());
201 LocaleGuard lg (X_("POSIX"));
203 for (uint32_t i = 0; i < parameter_count(); ++i){
205 if (parameter_is_input(i) && parameter_is_control(i)) {
206 child = new XMLNode("port");
207 snprintf(buf, sizeof(buf), "%u", i);
208 child->add_property("number", string(buf));
209 snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
210 child->add_property("value", string(buf));
211 root->add_child_nocopy (*child);
213 if (i < controls.size() && controls[i]) {
214 root->add_child_nocopy (controls[i]->get_state());
223 LV2Plugin::save_preset (string name)
225 return Plugin::save_preset (name, "lv2");
229 LV2Plugin::set_state(const XMLNode& node)
233 XMLNodeConstIterator iter;
238 LocaleGuard lg (X_("POSIX"));
240 if (node.name() != state_node_name()) {
241 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
245 nodes = node.children ("port");
247 for(iter = nodes.begin(); iter != nodes.end(); ++iter){
251 if ((prop = child->property("number")) != 0) {
252 port = prop->value().c_str();
254 warning << _("LV2: no lv2 port number") << endmsg;
258 if ((prop = child->property("value")) != 0) {
259 data = prop->value().c_str();
261 warning << _("LV2: no lv2 port data") << endmsg;
265 sscanf (port, "%" PRIu32, &port_id);
266 set_parameter (port_id, atof(data));
269 latency_compute_run ();
275 LV2Plugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
277 SLV2Port port = slv2_plugin_get_port_by_index(_plugin, which);
279 #define LV2_URI "http://lv2plug.in/ns/lv2core#"
281 desc.integer_step = slv2_port_has_property(_plugin, port, LV2_URI "integer");
282 desc.toggled = slv2_port_has_property(_plugin, port, LV2_URI "toggled");
283 desc.logarithmic = false; // TODO (LV2 extension)
284 desc.sr_dependent = slv2_port_has_property(_plugin, port, LV2_URI "sampleRate");
285 desc.label = slv2_port_get_name(_plugin, port);
286 desc.lower = slv2_port_get_minimum_value(_plugin, port);
287 desc.upper = slv2_port_get_maximum_value(_plugin, port);
288 desc.min_unbound = false; // TODO (LV2 extension)
289 desc.max_unbound = false; // TODO (LV2 extension)
291 if (desc.integer_step) {
293 desc.smallstep = 0.1;
294 desc.largestep = 10.0;
296 const float delta = desc.upper - desc.lower;
297 desc.step = delta / 1000.0f;
298 desc.smallstep = delta / 10000.0f;
299 desc.largestep = delta/10.0f;
307 LV2Plugin::describe_parameter (uint32_t which)
309 if (which < parameter_count()) {
310 return slv2_port_get_name(_plugin,
311 slv2_plugin_get_port_by_index(_plugin, which));
318 LV2Plugin::latency () const
320 if (_latency_control_port) {
321 return (nframes_t) floor (*_latency_control_port);
328 LV2Plugin::automatable () const
332 for (uint32_t i = 0; i < parameter_count(); ++i){
333 if (parameter_is_input(i) && parameter_is_control(i)) {
334 ret.insert (ret.end(), i);
342 LV2Plugin::connect_and_run (vector<Sample*>& bufs, uint32_t nbufs, int32_t& in_index, int32_t& out_index, nframes_t nframes, nframes_t offset)
349 then = get_cycles ();
351 while (port_index < parameter_count()) {
352 if (parameter_is_audio(port_index)) {
353 if (parameter_is_input(port_index)) {
354 slv2_instance_connect_port(_instance, port_index,
355 bufs[min((uint32_t)in_index, nbufs - 1)] + offset);
357 } else if (parameter_is_output(port_index)) {
358 slv2_instance_connect_port(_instance, port_index,
359 bufs[min((uint32_t)out_index, nbufs - 1)] + offset);
368 set_cycles ((uint32_t) (now - then));
374 LV2Plugin::parameter_is_control (uint32_t param) const
376 SLV2PortSignature sig = slv2_template_get_port(_template, param);
377 return (slv2_port_signature_get_type(sig) == SLV2_PORT_DATA_TYPE_CONTROL);
381 LV2Plugin::parameter_is_audio (uint32_t param) const
383 SLV2PortSignature sig = slv2_template_get_port(_template, param);
384 return (slv2_port_signature_get_type(sig) == SLV2_PORT_DATA_TYPE_AUDIO);
388 LV2Plugin::parameter_is_output (uint32_t param) const
390 SLV2PortSignature sig = slv2_template_get_port(_template, param);
391 return (slv2_port_signature_get_direction(sig) == SLV2_PORT_DIRECTION_OUTPUT);
395 LV2Plugin::parameter_is_input (uint32_t param) const
397 SLV2PortSignature sig = slv2_template_get_port(_template, param);
398 return (slv2_port_signature_get_direction(sig) == SLV2_PORT_DIRECTION_INPUT);
402 LV2Plugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
405 if (param < parameter_count()) {
406 snprintf (buf, len, "%.3f", get_parameter (param));
414 LV2Plugin::run (nframes_t nframes)
416 for (uint32_t i = 0; i < parameter_count(); ++i) {
417 SLV2PortSignature sig = slv2_template_get_port(_template, i);
418 if (slv2_port_signature_get_type(sig) == SLV2_PORT_DATA_TYPE_CONTROL
419 && slv2_port_signature_get_direction(sig) == SLV2_PORT_DIRECTION_INPUT) {
420 _control_data[i] = _shadow_data[i];
424 slv2_instance_run(_instance, nframes);
428 LV2Plugin::latency_compute_run ()
430 if (!_latency_control_port) {
434 /* we need to run the plugin so that it can set its latency
440 uint32_t port_index = 0;
441 uint32_t in_index = 0;
442 uint32_t out_index = 0;
443 const nframes_t bufsize = 1024;
444 float buffer[bufsize];
446 memset(buffer,0,sizeof(float)*bufsize);
448 /* Note that we've already required that plugins
449 be able to handle in-place processing.
454 while (port_index < parameter_count()) {
455 if (parameter_is_audio (port_index)) {
456 if (parameter_is_input (port_index)) {
457 slv2_instance_connect_port (_instance, port_index, buffer);
459 } else if (parameter_is_output (port_index)) {
460 slv2_instance_connect_port (_instance, port_index, buffer);
471 LV2PluginInfo::LV2PluginInfo (void* slv2_plugin)
472 : _slv2_plugin(slv2_plugin)
476 LV2PluginInfo::~LV2PluginInfo()
481 LV2PluginInfo::load (Session& session)
483 SLV2Plugin p = (SLV2Plugin)_slv2_plugin;
488 plugin.reset (new LV2Plugin (session.engine(), session, p, session.frame_rate()));
490 plugin->set_info(PluginInfoPtr(new LV2PluginInfo(*this)));
494 catch (failed_constructor &err) {
495 return PluginPtr ((Plugin*) 0);
502 LV2PluginInfo::discover (void* slv2_world)
504 PluginInfoList plugs;
506 SLV2Plugins plugins = slv2_world_get_all_plugins((SLV2World)slv2_world);
508 for (unsigned i=0; i < slv2_plugins_size(plugins); ++i) {
509 SLV2Plugin p = slv2_plugins_get_at(plugins, i);
510 LV2PluginInfoPtr info (new LV2PluginInfo(p));
512 info->name = slv2_plugin_get_name(p);
514 SLV2PluginClass pclass = slv2_plugin_get_class(p);
515 info->category = slv2_plugin_class_get_label(pclass);
517 char* author_name = slv2_plugin_get_author_name(p);
518 info->creator = author_name ? string(author_name) : "Unknown";
521 info->path = "/NOPATH"; // Meaningless for LV2
523 SLV2Template io = slv2_plugin_get_template(p);
525 info->n_inputs = slv2_template_get_num_ports_of_type(io,
526 SLV2_PORT_DIRECTION_INPUT, SLV2_PORT_DATA_TYPE_AUDIO);
528 info->n_outputs = slv2_template_get_num_ports_of_type(io,
529 SLV2_PORT_DIRECTION_OUTPUT, SLV2_PORT_DATA_TYPE_AUDIO);
531 info->unique_id = slv2_plugin_get_uri(p);
532 info->index = 0; // Meaningless for LV2
534 plugs.push_back (info);