some LV2 debug tracing
[ardour2.git] / libs / ardour / lv2_plugin.cc
blobb02cc401c4f2c2dc7d983301a74eda4ce62541fb
1 /*
2 Copyright (C) 2008-2011 Paul Davis
3 Author: David Robillard
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.
21 #include <string>
22 #include <vector>
24 #include <cmath>
25 #include <cstdlib>
26 #include <cstring>
28 #include <glibmm.h>
30 #include "pbd/compose.h"
31 #include "pbd/error.h"
32 #include "pbd/pathscanner.h"
33 #include "pbd/xml++.h"
35 #include "ardour/ardour.h"
36 #include "ardour/audio_buffer.h"
37 #include "ardour/audioengine.h"
38 #include "ardour/debug.h"
39 #include "ardour/lv2_event_buffer.h"
40 #include "ardour/lv2_plugin.h"
41 #include "ardour/session.h"
43 #include "pbd/stl_delete.h"
45 #include "i18n.h"
46 #include <locale.h>
48 #include "lv2ext/lv2_persist.h"
49 #include "lv2_pfile.h"
51 #define NS_DC "http://dublincore.org/documents/dcmi-namespace/"
52 #define NS_LV2 "http://lv2plug.in/ns/lv2core#"
53 #define NS_PSET "http://lv2plug.in/ns/dev/presets#"
54 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
56 using namespace std;
57 using namespace ARDOUR;
58 using namespace PBD;
60 URIMap LV2Plugin::_uri_map;
61 uint32_t LV2Plugin::_midi_event_type = _uri_map.uri_to_id (
62 "http://lv2plug.in/ns/ext/event",
63 "http://lv2plug.in/ns/ext/midi#MidiEvent");
65 LV2Plugin::LV2Plugin (AudioEngine& engine,
66 Session& session,
67 LV2World& world,
68 SLV2Plugin plugin,
69 framecnt_t rate)
70 : Plugin (engine, session)
71 , _world (world)
72 , _features (NULL)
74 init (world, plugin, rate);
77 LV2Plugin::LV2Plugin (const LV2Plugin &other)
78 : Plugin (other)
79 , _world(other._world)
80 , _features(NULL)
82 init (other._world, other._plugin, other._sample_rate);
84 for (uint32_t i = 0; i < parameter_count(); ++i) {
85 _control_data[i] = other._shadow_data[i];
86 _shadow_data[i] = other._shadow_data[i];
90 void
91 LV2Plugin::init (LV2World& world, SLV2Plugin plugin, framecnt_t rate)
93 DEBUG_TRACE (DEBUG::LV2, "LV2 plugin init\n");
95 _world = world;
96 _plugin = plugin;
97 _ui = NULL;
98 _control_data = 0;
99 _shadow_data = 0;
100 _latency_control_port = 0;
101 _was_activated = false;
103 _instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
104 _data_access_feature.URI = "http://lv2plug.in/ns/ext/data-access";
105 _persist_feature.URI = "http://lv2plug.in/ns/ext/persist";
106 _persist_feature.data = NULL;
108 SLV2Value persist_uri = slv2_value_new_uri(_world.world, _persist_feature.URI);
109 _supports_persist = slv2_plugin_has_feature(plugin, persist_uri);
110 slv2_value_free(persist_uri);
112 _features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * 5);
113 _features[0] = &_instance_access_feature;
114 _features[1] = &_data_access_feature;
115 _features[2] = &_persist_feature;
116 _features[3] = _uri_map.feature();
117 _features[4] = NULL;
119 _instance = slv2_plugin_instantiate(plugin, rate, _features);
120 _name = slv2_plugin_get_name(plugin);
121 _author = slv2_plugin_get_author_name(plugin);
123 if (_instance == 0) {
124 error << _("LV2: Failed to instantiate plugin ")
125 << slv2_value_as_string (slv2_plugin_get_uri(plugin)) << endmsg;
126 throw failed_constructor();
129 _instance_access_feature.data = (void*)_instance->lv2_handle;
130 _data_access_extension_data.extension_data = _instance->lv2_descriptor->extension_data;
131 _data_access_feature.data = &_data_access_extension_data;
133 if (slv2_plugin_has_feature(plugin, world.in_place_broken)) {
134 error << string_compose(
135 _("LV2: \"%1\" cannot be used, since it cannot do inplace processing"),
136 slv2_value_as_string(_name)) << endmsg;
137 slv2_value_free(_name);
138 slv2_value_free(_author);
139 throw failed_constructor();
142 _sample_rate = rate;
144 const uint32_t num_ports = slv2_plugin_get_num_ports(plugin);
145 const bool latent = slv2_plugin_has_latency(plugin);
146 const uint32_t latency_port = (latent)
147 ? slv2_plugin_get_latency_port_index(plugin)
148 : 0;
150 _control_data = new float[num_ports];
151 _shadow_data = new float[num_ports];
152 _defaults = new float[num_ports];
154 for (uint32_t i = 0; i < num_ports; ++i) {
155 SLV2Port port = slv2_plugin_get_port_by_index(plugin, i);
156 SLV2Value sym = slv2_port_get_symbol(_plugin, port);
157 _port_indices.insert(std::make_pair(slv2_value_as_string(sym), i));
158 if (parameter_is_control(i)) {
159 SLV2Value def;
160 slv2_port_get_range(plugin, port, &def, NULL, NULL);
161 _defaults[i] = def ? slv2_value_as_float(def) : 0.0f;
162 slv2_value_free(def);
164 slv2_instance_connect_port (_instance, i, &_control_data[i]);
166 if (latent && i == latency_port) {
167 _latency_control_port = &_control_data[i];
168 *_latency_control_port = 0;
171 if (parameter_is_input(i)) {
172 _shadow_data[i] = default_value (i);
174 } else {
175 _defaults[i] = 0.0f;
179 SLV2UIs uis = slv2_plugin_get_uis(_plugin);
180 if (slv2_uis_size(uis) > 0) {
181 for (unsigned i=0; i < slv2_uis_size(uis); ++i) {
182 SLV2UI ui = slv2_uis_get_at(uis, i);
183 if (slv2_ui_is_a(ui, _world.gtk_gui)) {
184 _ui = ui;
185 break;
189 // if gtk gui is not available, try to find external gui
190 if (!_ui) {
191 for (unsigned i=0; i < slv2_uis_size(uis); ++i) {
192 SLV2UI ui = slv2_uis_get_at(uis, i);
193 if (slv2_ui_is_a(ui, _world.external_gui)) {
194 _ui = ui;
195 break;
201 latency_compute_run ();
204 LV2Plugin::~LV2Plugin ()
206 DEBUG_TRACE (DEBUG::LV2, string_compose ("%1 destroy\n", name()));
208 deactivate ();
209 cleanup ();
211 slv2_instance_free(_instance);
212 slv2_value_free(_name);
213 slv2_value_free(_author);
215 delete [] _control_data;
216 delete [] _shadow_data;
219 bool
220 LV2Plugin::is_external_ui() const
222 return slv2_ui_is_a(_ui, _world.external_gui);
225 string
226 LV2Plugin::unique_id() const
228 return slv2_value_as_uri(slv2_plugin_get_uri(_plugin));
232 float
233 LV2Plugin::default_value (uint32_t port)
235 return _defaults[port];
238 const char*
239 LV2Plugin::port_symbol (uint32_t index) const
241 SLV2Port port = slv2_plugin_get_port_by_index(_plugin, index);
242 if (!port) {
243 error << name() << ": Invalid port index " << index << endmsg;
246 SLV2Value sym = slv2_port_get_symbol(_plugin, port);
247 return slv2_value_as_string(sym);
251 void
252 LV2Plugin::set_parameter (uint32_t which, float val)
254 DEBUG_TRACE (DEBUG::LV2, string_compose ("%1 set parameter %2 to %3\n", name(), which, val));
256 if (which < slv2_plugin_get_num_ports(_plugin)) {
257 _shadow_data[which] = val;
258 } else {
259 warning << string_compose (
260 _("Illegal parameter number used with plugin \"%1\". "
261 "This is a bug in either %2 or the LV2 plugin (%3)"),
262 name(), PROGRAM_NAME, unique_id()) << endmsg;
265 Plugin::set_parameter (which, val);
268 float
269 LV2Plugin::get_parameter (uint32_t which) const
271 if (parameter_is_input(which)) {
272 return (float) _shadow_data[which];
273 } else {
274 return (float) _control_data[which];
276 return 0.0f;
279 uint32_t
280 LV2Plugin::nth_parameter (uint32_t n, bool& ok) const
282 ok = false;
283 for (uint32_t c = 0, x = 0; x < slv2_plugin_get_num_ports (_plugin); ++x) {
284 if (parameter_is_control (x)) {
285 if (c++ == n) {
286 ok = true;
287 return x;
292 return 0;
295 void
296 LV2Plugin::lv2_persist_store_callback(void* callback_data,
297 const char* key,
298 const void* value,
299 size_t size,
300 uint32_t type)
302 LV2PFile file = (LV2PFile)callback_data;
304 // FIXME: assumes URIs are mapped in the default context (or not event, at least)
305 const char* type_uri = LV2Plugin::_uri_map.id_to_uri(NULL, type);
306 cout << "LV2 PERSIST STORE " << key << " = " << value << " :: " << type_uri << endl;
307 lv2_pfile_write(file, key, value, size, type_uri);
310 const void*
311 LV2Plugin::lv2_persist_retrieve_callback(void* callback_data,
312 const char* key,
313 size_t* size,
314 uint32_t* type)
316 //LV2PFile file = (LV2PFile)callback_data;
317 cout << "LV2 PERSIST RETRIEVE " << key << endl;
318 return NULL;
321 void
322 LV2Plugin::add_state (XMLNode* root) const
324 XMLNode *child;
325 char buf[16];
326 LocaleGuard lg (X_("POSIX"));
328 for (uint32_t i = 0; i < parameter_count(); ++i){
329 if (parameter_is_input(i) && parameter_is_control(i)) {
330 child = new XMLNode("Port");
331 child->add_property("symbol", port_symbol(i));
332 snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
333 child->add_property("value", string(buf));
334 root->add_child_nocopy (*child);
338 if (_supports_persist) {
339 // Create state directory for this plugin instance
340 const std::string state_filename = _id.to_s() + ".lv2pfile";
341 const std::string state_path = Glib::build_filename(
342 _session.plugins_dir(), state_filename);
344 cout << "Saving LV2 plugin state to " << state_path << endl;
346 // Get LV2 Persist extension data from plugin instance
347 LV2_Persist* persist = (LV2_Persist*)slv2_instance_get_extension_data(
348 _instance, "http://lv2plug.in/ns/ext/persist");
349 if (!persist) {
350 warning << string_compose(
351 _("Plugin \"%1\% failed to return LV2 persist data"),
352 unique_id());
353 return;
356 LV2PFile file = lv2_pfile_open(state_path.c_str(), true);
357 persist->save(_instance->lv2_handle, &LV2Plugin::lv2_persist_store_callback, file);
358 lv2_pfile_close(file);
360 root->add_property("state-file", state_filename);
364 static inline SLV2Value
365 get_value(SLV2Plugin p, SLV2Value subject, SLV2Value predicate)
367 SLV2Values vs = slv2_plugin_get_value_for_subject(p, subject, predicate);
368 return vs ? slv2_values_get_at(vs, 0) : NULL;
371 void
372 LV2Plugin::find_presets ()
374 SLV2Value dc_title = slv2_value_new_uri(_world.world, NS_DC "title");
375 SLV2Value pset_hasPreset = slv2_value_new_uri(_world.world, NS_PSET "hasPreset");
377 SLV2Values presets = slv2_plugin_get_value(_plugin, pset_hasPreset);
378 for (unsigned i = 0; i < slv2_values_size(presets); ++i) {
379 SLV2Value preset = slv2_values_get_at(presets, i);
380 SLV2Value name = get_value(_plugin, preset, dc_title);
381 if (name) {
382 _presets.insert(std::make_pair(slv2_value_as_string(preset),
383 PresetRecord(
384 slv2_value_as_string(preset),
385 slv2_value_as_string(name))));
386 } else {
387 warning << string_compose(
388 _("Plugin \"%1\% preset \"%2%\" is missing a dc:title\n"),
389 unique_id(), slv2_value_as_string(preset));
392 slv2_values_free(presets);
394 slv2_value_free(pset_hasPreset);
395 slv2_value_free(dc_title);
398 bool
399 LV2Plugin::load_preset (PresetRecord r)
401 Plugin::load_preset (r);
403 #ifdef HAVE_NEW_SLV2
404 // New (>= 0.7.0) slv2 no longer supports SPARQL, but exposes blank nodes
405 // so querying ports is possible with the simple API
406 SLV2Value lv2_port = slv2_value_new_uri(_world.world, NS_LV2 "port");
407 SLV2Value lv2_symbol = slv2_value_new_uri(_world.world, NS_LV2 "symbol");
408 SLV2Value pset_value = slv2_value_new_uri(_world.world, NS_PSET "value");
409 SLV2Value preset = slv2_value_new_uri(_world.world, r.uri.c_str());
411 SLV2Values ports = slv2_plugin_get_value_for_subject(_plugin, preset, lv2_port);
412 for (unsigned i = 0; i < slv2_values_size(ports); ++i) {
413 SLV2Value port = slv2_values_get_at(ports, i);
414 SLV2Value symbol = get_value(_plugin, port, lv2_symbol);
415 SLV2Value value = get_value(_plugin, port, pset_value);
416 if (value && slv2_value_is_float(value)) {
417 set_parameter(_port_indices[slv2_value_as_string(symbol)],
418 slv2_value_as_float(value));
421 slv2_values_free(ports);
423 slv2_value_free(preset);
424 slv2_value_free(pset_value);
425 slv2_value_free(lv2_symbol);
426 slv2_value_free(lv2_port);
427 #else
428 const string query = string(
429 "PREFIX lv2p: <http://lv2plug.in/ns/dev/presets#>\n"
430 "PREFIX dc: <http://dublincore.org/documents/dcmi-namespace/>\n"
431 "SELECT ?sym ?val WHERE { <") + r.uri + "> lv2:port ?port . "
432 " ?port lv2:symbol ?sym ; lv2p:value ?val . }";
433 SLV2Results values = slv2_plugin_query_sparql(_plugin, query.c_str());
434 for (; !slv2_results_finished(values); slv2_results_next(values)) {
435 SLV2Value sym = slv2_results_get_binding_value(values, 0);
436 SLV2Value val = slv2_results_get_binding_value(values, 1);
437 if (slv2_value_is_float(val)) {
438 uint32_t index = _port_indices[slv2_value_as_string(sym)];
439 set_parameter(index, slv2_value_as_float(val));
442 slv2_results_free(values);
443 #endif
444 return true;
447 std::string
448 LV2Plugin::do_save_preset (string /*name*/)
450 return "";
453 void
454 LV2Plugin::do_remove_preset (string /*name*/)
456 return;
459 bool
460 LV2Plugin::has_editor() const
462 return (_ui != NULL);
466 LV2Plugin::set_state(const XMLNode& node, int version)
468 XMLNodeList nodes;
469 const XMLProperty* prop;
470 XMLNodeConstIterator iter;
471 XMLNode* child;
472 const char* sym;
473 const char* value;
474 uint32_t port_id;
475 LocaleGuard lg(X_("POSIX"));
477 if (node.name() != state_node_name()) {
478 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
479 return -1;
482 if (version < 3000){
483 nodes = node.children ("port");
484 } else {
485 nodes = node.children ("Port");
488 for (iter = nodes.begin(); iter != nodes.end(); ++iter){
490 child = *iter;
492 if ((prop = child->property("symbol")) != 0) {
493 sym = prop->value().c_str();
494 } else {
495 warning << _("LV2: port has no symbol, ignored") << endmsg;
496 continue;
499 map<string,uint32_t>::iterator i = _port_indices.find(sym);
501 if (i != _port_indices.end()) {
502 port_id = i->second;
503 } else {
504 warning << _("LV2: port has unknown index, ignored") << endmsg;
505 continue;
508 if ((prop = child->property("value")) != 0) {
509 value = prop->value().c_str();
510 } else {
511 warning << _("LV2: port has no value, ignored") << endmsg;
512 continue;
515 set_parameter (port_id, atof(value));
518 if ((prop = node.property("state-file")) != 0) {
519 std::string state_path = Glib::build_filename(_session.plugins_dir(),
520 prop->value());
522 // Get LV2 Persist extension data from plugin instance
523 LV2_Persist* persist = (LV2_Persist*)slv2_instance_get_extension_data(
524 _instance, "http://lv2plug.in/ns/ext/persist");
525 if (persist) {
526 cout << "Loading LV2 state from " << state_path << endl;
527 LV2PFile file = lv2_pfile_open(state_path.c_str(), false);
528 persist->restore(_instance->lv2_handle,
529 &LV2Plugin::lv2_persist_retrieve_callback,
530 file);
531 lv2_pfile_close(file);
532 } else {
533 warning << string_compose(
534 _("Plugin \"%1\% failed to return LV2 persist data"),
535 unique_id());
539 latency_compute_run ();
541 return Plugin::set_state (node, version);
545 LV2Plugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
547 SLV2Port port = slv2_plugin_get_port_by_index(_plugin, which);
549 SLV2Value def, min, max;
550 slv2_port_get_range(_plugin, port, &def, &min, &max);
552 desc.integer_step = slv2_port_has_property(_plugin, port, _world.integer);
553 desc.toggled = slv2_port_has_property(_plugin, port, _world.toggled);
554 desc.logarithmic = slv2_port_has_property(_plugin, port, _world.logarithmic);
555 desc.sr_dependent = slv2_port_has_property(_plugin, port, _world.srate);
556 desc.label = slv2_value_as_string(slv2_port_get_name(_plugin, port));
557 desc.lower = min ? slv2_value_as_float(min) : 0.0f;
558 desc.upper = max ? slv2_value_as_float(max) : 1.0f;
559 desc.min_unbound = false; // TODO: LV2 extension required
560 desc.max_unbound = false; // TODO: LV2 extension required
562 if (desc.integer_step) {
563 desc.step = 1.0;
564 desc.smallstep = 0.1;
565 desc.largestep = 10.0;
566 } else {
567 const float delta = desc.upper - desc.lower;
568 desc.step = delta / 1000.0f;
569 desc.smallstep = delta / 10000.0f;
570 desc.largestep = delta/10.0f;
573 slv2_value_free(def);
574 slv2_value_free(min);
575 slv2_value_free(max);
577 return 0;
581 string
582 LV2Plugin::describe_parameter (Evoral::Parameter which)
584 if (which.type() == PluginAutomation && which.id() < parameter_count()) {
585 SLV2Value name = slv2_port_get_name(_plugin,
586 slv2_plugin_get_port_by_index(_plugin, which.id()));
587 string ret(slv2_value_as_string(name));
588 slv2_value_free(name);
589 return ret;
590 } else {
591 return "??";
595 framecnt_t
596 LV2Plugin::signal_latency () const
598 if (_latency_control_port) {
599 return (framecnt_t) floor (*_latency_control_port);
600 } else {
601 return 0;
605 set<Evoral::Parameter>
606 LV2Plugin::automatable () const
608 set<Evoral::Parameter> ret;
610 for (uint32_t i = 0; i < parameter_count(); ++i){
611 if (parameter_is_input(i) && parameter_is_control(i)) {
612 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
616 return ret;
619 void
620 LV2Plugin::activate ()
622 DEBUG_TRACE (DEBUG::LV2, string_compose ("%1 activate\n", name()));
624 if (!_was_activated) {
625 slv2_instance_activate (_instance);
626 _was_activated = true;
630 void
631 LV2Plugin::deactivate ()
633 DEBUG_TRACE (DEBUG::LV2, string_compose ("%1 deactivate\n", name()));
635 if (_was_activated) {
636 slv2_instance_deactivate (_instance);
637 _was_activated = false;
641 void
642 LV2Plugin::cleanup ()
644 DEBUG_TRACE (DEBUG::LV2, string_compose ("%1 cleanup\n", name()));
646 activate ();
647 deactivate ();
648 slv2_instance_free (_instance);
649 _instance = NULL;
653 LV2Plugin::connect_and_run (BufferSet& bufs,
654 ChanMapping in_map, ChanMapping out_map,
655 pframes_t nframes, framecnt_t offset)
657 DEBUG_TRACE (DEBUG::LV2, string_compose ("%1 run %2 offset %3\n", name(), nframes, offset));
659 Plugin::connect_and_run (bufs, in_map, out_map, nframes, offset);
661 cycles_t then = get_cycles ();
663 uint32_t audio_in_index = 0;
664 uint32_t audio_out_index = 0;
665 uint32_t midi_in_index = 0;
666 uint32_t midi_out_index = 0;
667 for (uint32_t port_index = 0; port_index < parameter_count(); ++port_index) {
668 if (parameter_is_audio(port_index)) {
669 if (parameter_is_input(port_index)) {
670 const uint32_t buf_index = in_map.get(DataType::AUDIO, audio_in_index++);
671 //cerr << port_index << " : " << " AUDIO IN " << buf_index << endl;
672 slv2_instance_connect_port(_instance, port_index,
673 bufs.get_audio(buf_index).data(offset));
674 } else if (parameter_is_output(port_index)) {
675 const uint32_t buf_index = out_map.get(DataType::AUDIO, audio_out_index++);
676 //cerr << port_index << " : " << " AUDIO OUT " << buf_index << endl;
677 slv2_instance_connect_port(_instance, port_index,
678 bufs.get_audio(buf_index).data(offset));
680 } else if (parameter_is_midi(port_index)) {
681 if (parameter_is_input(port_index)) {
682 const uint32_t buf_index = in_map.get(DataType::MIDI, midi_in_index++);
683 //cerr << port_index << " : " << " MIDI IN " << buf_index << endl;
684 slv2_instance_connect_port(_instance, port_index,
685 bufs.get_lv2_midi(true, buf_index).data());
686 } else if (parameter_is_output(port_index)) {
687 const uint32_t buf_index = out_map.get(DataType::MIDI, midi_out_index++);
688 //cerr << port_index << " : " << " MIDI OUT " << buf_index << endl;
689 slv2_instance_connect_port(_instance, port_index,
690 bufs.get_lv2_midi(false, buf_index).data());
692 } else if (!parameter_is_control(port_index)) {
693 // Optional port (it'd better be if we've made it this far...)
694 slv2_instance_connect_port(_instance, port_index, NULL);
698 run (nframes);
700 midi_out_index = 0;
701 for (uint32_t port_index = 0; port_index < parameter_count(); ++port_index) {
702 if (parameter_is_midi(port_index) && parameter_is_output(port_index)) {
703 const uint32_t buf_index = out_map.get(DataType::MIDI, midi_out_index++);
704 bufs.flush_lv2_midi(true, buf_index);
708 cycles_t now = get_cycles ();
709 set_cycles ((uint32_t) (now - then));
711 return 0;
714 bool
715 LV2Plugin::parameter_is_control (uint32_t param) const
717 SLV2Port port = slv2_plugin_get_port_by_index(_plugin, param);
718 return slv2_port_is_a(_plugin, port, _world.control_class);
721 bool
722 LV2Plugin::parameter_is_audio (uint32_t param) const
724 SLV2Port port = slv2_plugin_get_port_by_index(_plugin, param);
725 return slv2_port_is_a(_plugin, port, _world.audio_class);
728 bool
729 LV2Plugin::parameter_is_midi (uint32_t param) const
731 SLV2Port port = slv2_plugin_get_port_by_index(_plugin, param);
732 return slv2_port_is_a(_plugin, port, _world.event_class);
733 // && slv2_port_supports_event(_plugin, port, _world.midi_class);
736 bool
737 LV2Plugin::parameter_is_output (uint32_t param) const
739 SLV2Port port = slv2_plugin_get_port_by_index(_plugin, param);
740 return slv2_port_is_a(_plugin, port, _world.output_class);
743 bool
744 LV2Plugin::parameter_is_input (uint32_t param) const
746 SLV2Port port = slv2_plugin_get_port_by_index(_plugin, param);
747 return slv2_port_is_a(_plugin, port, _world.input_class);
750 void
751 LV2Plugin::print_parameter (uint32_t param, char* buf, uint32_t len) const
753 if (buf && len) {
754 if (param < parameter_count()) {
755 snprintf (buf, len, "%.3f", get_parameter (param));
756 } else {
757 strcat (buf, "0");
762 void
763 LV2Plugin::run (pframes_t nframes)
765 for (uint32_t i = 0; i < parameter_count(); ++i) {
766 if (parameter_is_control(i) && parameter_is_input(i)) {
767 _control_data[i] = _shadow_data[i];
771 slv2_instance_run(_instance, nframes);
774 void
775 LV2Plugin::latency_compute_run ()
777 if (!_latency_control_port) {
778 return;
781 /* we need to run the plugin so that it can set its latency
782 parameter.
785 activate ();
787 uint32_t port_index = 0;
788 uint32_t in_index = 0;
789 uint32_t out_index = 0;
791 const framecnt_t bufsize = 1024;
792 float buffer[bufsize];
794 memset(buffer, 0, sizeof(float) * bufsize);
796 // FIXME: Ensure plugins can handle in-place processing
798 port_index = 0;
800 while (port_index < parameter_count()) {
801 if (parameter_is_audio (port_index)) {
802 if (parameter_is_input (port_index)) {
803 slv2_instance_connect_port (_instance, port_index, buffer);
804 in_index++;
805 } else if (parameter_is_output (port_index)) {
806 slv2_instance_connect_port (_instance, port_index, buffer);
807 out_index++;
810 port_index++;
813 run (bufsize);
814 deactivate ();
817 LV2World::LV2World()
818 : world(slv2_world_new())
820 slv2_world_load_all(world);
821 input_class = slv2_value_new_uri(world, SLV2_PORT_CLASS_INPUT);
822 output_class = slv2_value_new_uri(world, SLV2_PORT_CLASS_OUTPUT);
823 control_class = slv2_value_new_uri(world, SLV2_PORT_CLASS_CONTROL);
824 audio_class = slv2_value_new_uri(world, SLV2_PORT_CLASS_AUDIO);
825 event_class = slv2_value_new_uri(world, SLV2_PORT_CLASS_EVENT);
826 midi_class = slv2_value_new_uri(world, SLV2_EVENT_CLASS_MIDI);
827 in_place_broken = slv2_value_new_uri(world, SLV2_NAMESPACE_LV2 "inPlaceBroken");
828 integer = slv2_value_new_uri(world, SLV2_NAMESPACE_LV2 "integer");
829 toggled = slv2_value_new_uri(world, SLV2_NAMESPACE_LV2 "toggled");
830 srate = slv2_value_new_uri(world, SLV2_NAMESPACE_LV2 "sampleRate");
831 gtk_gui = slv2_value_new_uri(world, NS_UI "GtkUI");
832 external_gui = slv2_value_new_uri(world, NS_UI "external");
833 logarithmic = slv2_value_new_uri(world, "http://lv2plug.in/ns/dev/extportinfo#logarithmic");
836 LV2World::~LV2World()
838 slv2_value_free(input_class);
839 slv2_value_free(output_class);
840 slv2_value_free(control_class);
841 slv2_value_free(audio_class);
842 slv2_value_free(event_class);
843 slv2_value_free(midi_class);
844 slv2_value_free(in_place_broken);
847 LV2PluginInfo::LV2PluginInfo (void* lv2_world, void* slv2_plugin)
848 : _lv2_world(lv2_world)
849 , _slv2_plugin(slv2_plugin)
851 type = ARDOUR::LV2;
854 LV2PluginInfo::~LV2PluginInfo()
858 PluginPtr
859 LV2PluginInfo::load (Session& session)
861 try {
862 PluginPtr plugin;
864 plugin.reset (new LV2Plugin (session.engine(), session,
865 *(LV2World*)_lv2_world, (SLV2Plugin)_slv2_plugin, session.frame_rate()));
867 plugin->set_info(PluginInfoPtr(new LV2PluginInfo(*this)));
868 return plugin;
871 catch (failed_constructor &err) {
872 return PluginPtr ((Plugin*) 0);
875 return PluginPtr();
878 PluginInfoList*
879 LV2PluginInfo::discover (void* lv2_world)
881 PluginInfoList* plugs = new PluginInfoList;
882 LV2World* world = (LV2World*)lv2_world;
883 SLV2Plugins plugins = slv2_world_get_all_plugins(world->world);
885 cerr << "LV2: Discovering " << slv2_plugins_size (plugins) << " plugins" << endl;
887 for (unsigned i=0; i < slv2_plugins_size(plugins); ++i) {
888 SLV2Plugin p = slv2_plugins_get_at(plugins, i);
889 LV2PluginInfoPtr info (new LV2PluginInfo(lv2_world, p));
891 SLV2Value name = slv2_plugin_get_name(p);
893 if (!name) {
894 cerr << "LV2: invalid plugin\n";
895 continue;
898 info->type = LV2;
900 info->name = string(slv2_value_as_string(name));
901 slv2_value_free(name);
903 SLV2PluginClass pclass = slv2_plugin_get_class(p);
904 SLV2Value label = slv2_plugin_class_get_label(pclass);
905 info->category = slv2_value_as_string(label);
907 SLV2Value author_name = slv2_plugin_get_author_name(p);
908 info->creator = author_name ? string(slv2_value_as_string(author_name)) : "Unknown";
909 slv2_value_free(author_name);
911 info->path = "/NOPATH"; // Meaningless for LV2
913 info->n_inputs.set_audio(slv2_plugin_get_num_ports_of_class(p,
914 world->input_class, world->audio_class, NULL));
915 info->n_inputs.set_midi(slv2_plugin_get_num_ports_of_class(p,
916 world->input_class, world->event_class, NULL));
918 info->n_outputs.set_audio(slv2_plugin_get_num_ports_of_class(p,
919 world->output_class, world->audio_class, NULL));
920 info->n_outputs.set_midi(slv2_plugin_get_num_ports_of_class(p,
921 world->output_class, world->event_class, NULL));
923 info->unique_id = slv2_value_as_uri(slv2_plugin_get_uri(p));
924 info->index = 0; // Meaningless for LV2
926 plugs->push_back (info);
929 cerr << "Done LV2 discovery" << endl;
931 return plugs;