Move panner bypass state up to the PannerShell so that it is preserved even when...
[ardour2.git] / libs / ardour / lv2_plugin_lilv.cc
bloba1f42e640769a892171bd4672a02655ff530bcb5
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 <boost/utility.hpp>
32 #include "pbd/compose.h"
33 #include "pbd/error.h"
34 #include "pbd/pathscanner.h"
35 #include "pbd/stl_delete.h"
36 #include "pbd/xml++.h"
38 #include "libardour-config.h"
40 #include "ardour/ardour.h"
41 #include "ardour/audio_buffer.h"
42 #include "ardour/audioengine.h"
43 #include "ardour/debug.h"
44 #include "ardour/lv2_event_buffer.h"
45 #include "ardour/lv2_plugin.h"
46 #include "ardour/lv2_state.h"
47 #include "ardour/session.h"
49 #include "i18n.h"
50 #include <locale.h>
52 #include <lilv/lilv.h>
54 #include "lv2ext/lv2_files.h"
55 #include "lv2ext/lv2_persist.h"
56 #include "rdff.h"
57 #ifdef HAVE_SUIL
58 #include <suil/suil.h>
59 #endif
61 #define NS_DC "http://dublincore.org/documents/dcmi-namespace/"
62 #define NS_LV2 "http://lv2plug.in/ns/lv2core#"
63 #define NS_PSET "http://lv2plug.in/ns/dev/presets#"
64 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
66 using namespace std;
67 using namespace ARDOUR;
68 using namespace PBD;
70 URIMap LV2Plugin::_uri_map;
71 uint32_t LV2Plugin::_midi_event_type = _uri_map.uri_to_id(
72 "http://lv2plug.in/ns/ext/event",
73 "http://lv2plug.in/ns/ext/midi#MidiEvent");
75 class LV2World : boost::noncopyable {
76 public:
77 LV2World ();
78 ~LV2World ();
80 LilvWorld* world;
81 LilvNode* input_class; ///< Input port
82 LilvNode* output_class; ///< Output port
83 LilvNode* audio_class; ///< Audio port
84 LilvNode* control_class; ///< Control port
85 LilvNode* event_class; ///< Event port
86 LilvNode* midi_class; ///< MIDI event
87 LilvNode* in_place_broken;
88 LilvNode* integer;
89 LilvNode* toggled;
90 LilvNode* srate;
91 LilvNode* gtk_gui;
92 LilvNode* external_gui;
93 LilvNode* logarithmic;
96 static LV2World _world;
98 struct LV2Plugin::Impl {
99 Impl() : plugin(0), ui(0), ui_type(0), name(0), author(0), instance(0) {}
100 LilvPlugin* plugin;
101 const LilvUI* ui;
102 const LilvNode* ui_type;
103 LilvNode* name;
104 LilvNode* author;
105 LilvInstance* instance;
108 LV2Plugin::LV2Plugin (AudioEngine& engine,
109 Session& session,
110 void* c_plugin,
111 framecnt_t rate)
112 : Plugin(engine, session)
113 , _impl(new Impl())
114 , _features(NULL)
115 , _insert_id("0")
117 init(c_plugin, rate);
120 LV2Plugin::LV2Plugin (const LV2Plugin& other)
121 : Plugin(other)
122 , _impl(new Impl())
123 , _features(NULL)
124 , _insert_id(other._insert_id)
126 init(other._impl->plugin, other._sample_rate);
128 for (uint32_t i = 0; i < parameter_count(); ++i) {
129 _control_data[i] = other._shadow_data[i];
130 _shadow_data[i] = other._shadow_data[i];
134 void
135 LV2Plugin::init(void* c_plugin, framecnt_t rate)
137 DEBUG_TRACE(DEBUG::LV2, "init\n");
139 _impl->plugin = (LilvPlugin*)c_plugin;
140 _impl->ui = NULL;
141 _impl->ui_type = NULL;
142 _control_data = 0;
143 _shadow_data = 0;
144 _latency_control_port = 0;
145 _was_activated = false;
147 _instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
148 _data_access_feature.URI = "http://lv2plug.in/ns/ext/data-access";
149 _path_support_feature.URI = LV2_FILES_PATH_SUPPORT_URI;
150 _new_file_support_feature.URI = LV2_FILES_NEW_FILE_SUPPORT_URI;
151 _persist_feature.URI = "http://lv2plug.in/ns/ext/persist";
152 _persist_feature.data = NULL;
154 LilvPlugin* plugin = _impl->plugin;
156 LilvNode* persist_uri = lilv_new_uri(_world.world, _persist_feature.URI);
157 _supports_persist = lilv_plugin_has_feature(plugin, persist_uri);
158 lilv_node_free(persist_uri);
160 _features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * 7);
161 _features[0] = &_instance_access_feature;
162 _features[1] = &_data_access_feature;
163 _features[2] = &_path_support_feature;
164 _features[3] = &_new_file_support_feature;
165 _features[4] = &_persist_feature;
166 _features[5] = _uri_map.feature();
167 _features[6] = NULL;
169 LV2_Files_Path_Support* path_support = (LV2_Files_Path_Support*)malloc(
170 sizeof(LV2_Files_Path_Support));
171 path_support->host_data = this;
172 path_support->abstract_path = &lv2_files_abstract_path;
173 path_support->absolute_path = &lv2_files_absolute_path;
174 _path_support_feature.data = path_support;
176 LV2_Files_New_File_Support* new_file_support = (LV2_Files_New_File_Support*)malloc(
177 sizeof(LV2_Files_New_File_Support));
178 new_file_support->host_data = this;
179 new_file_support->new_file_path = &lv2_files_new_file_path;
180 _new_file_support_feature.data = new_file_support;
182 _impl->instance = lilv_plugin_instantiate(plugin, rate, _features);
183 _impl->name = lilv_plugin_get_name(plugin);
184 _impl->author = lilv_plugin_get_author_name(plugin);
186 if (_impl->instance == 0) {
187 error << _("LV2: Failed to instantiate plugin ") << uri() << endmsg;
188 throw failed_constructor();
191 _instance_access_feature.data = (void*)_impl->instance->lv2_handle;
192 _data_access_extension_data.extension_data = _impl->instance->lv2_descriptor->extension_data;
193 _data_access_feature.data = &_data_access_extension_data;
195 if (lilv_plugin_has_feature(plugin, _world.in_place_broken)) {
196 error << string_compose(
197 _("LV2: \"%1\" cannot be used, since it cannot do inplace processing"),
198 lilv_node_as_string(_impl->name)) << endmsg;
199 lilv_node_free(_impl->name);
200 lilv_node_free(_impl->author);
201 throw failed_constructor();
204 _sample_rate = rate;
206 const uint32_t num_ports = this->num_ports();
207 const bool latent = lilv_plugin_has_latency(plugin);
208 const uint32_t latency_port = (latent)
209 ? lilv_plugin_get_latency_port_index(plugin)
210 : 0;
212 _control_data = new float[num_ports];
213 _shadow_data = new float[num_ports];
214 _defaults = new float[num_ports];
216 for (uint32_t i = 0; i < num_ports; ++i) {
217 const LilvPort* port = lilv_plugin_get_port_by_index(plugin, i);
218 const LilvNode* sym = lilv_port_get_symbol(plugin, port);
220 // Store index in map so we can look up index by symbol
221 _port_indices.insert(std::make_pair(lilv_node_as_string(sym), i));
223 // Get range and default value if applicable
224 if (parameter_is_control(i)) {
225 LilvNode* def;
226 lilv_port_get_range(plugin, port, &def, NULL, NULL);
227 _defaults[i] = def ? lilv_node_as_float(def) : 0.0f;
228 if (lilv_port_has_property (plugin, port, _world.srate)) {
229 _defaults[i] *= _session.frame_rate ();
231 lilv_node_free(def);
233 lilv_instance_connect_port(_impl->instance, i, &_control_data[i]);
235 if (latent && ( i == latency_port) ) {
236 _latency_control_port = &_control_data[i];
237 *_latency_control_port = 0;
240 if (parameter_is_input(i)) {
241 _shadow_data[i] = default_value(i);
243 } else {
244 _defaults[i] = 0.0f;
248 LilvUIs* uis = lilv_plugin_get_uis(plugin);
249 if (lilv_uis_size(uis) > 0) {
250 #ifdef HAVE_SUIL
251 // Look for embeddable UI
252 LILV_FOREACH(uis, u, uis) {
253 const LilvUI* this_ui = lilv_uis_get(uis, u);
254 const LilvNode* this_ui_type = NULL;
255 if (lilv_ui_is_supported(this_ui,
256 suil_ui_supported,
257 _world.gtk_gui,
258 &this_ui_type)) {
259 // TODO: Multiple UI support
260 _impl->ui = this_ui;
261 _impl->ui_type = this_ui_type;
262 break;
265 #else
266 // Look for Gtk native UI
267 LILV_FOREACH(uis, i, uis) {
268 const LilvUI* ui = lilv_uis_get(uis, i);
269 if (lilv_ui_is_a(ui, _world.gtk_gui)) {
270 _impl->ui = ui;
271 _impl->ui_type = _world.gtk_gui;
272 break;
275 #endif
277 // If Gtk UI is not available, try to find external UI
278 if (!_impl->ui) {
279 LILV_FOREACH(uis, i, uis) {
280 const LilvUI* ui = lilv_uis_get(uis, i);
281 if (lilv_ui_is_a(ui, _world.external_gui)) {
282 _impl->ui = ui;
283 _impl->ui_type = _world.external_gui;
284 break;
290 latency_compute_run();
293 LV2Plugin::~LV2Plugin ()
295 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 destroy\n", name()));
297 deactivate();
298 cleanup();
300 lilv_instance_free(_impl->instance);
301 lilv_node_free(_impl->name);
302 lilv_node_free(_impl->author);
304 delete [] _control_data;
305 delete [] _shadow_data;
308 bool
309 LV2Plugin::is_external_ui() const
311 if (!_impl->ui) {
312 return false;
314 return lilv_ui_is_a(_impl->ui, _world.external_gui);
317 string
318 LV2Plugin::unique_id() const
320 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
323 const char*
324 LV2Plugin::uri() const
326 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
329 const char*
330 LV2Plugin::label() const
332 return lilv_node_as_string(_impl->name);
335 const char*
336 LV2Plugin::name() const
338 return lilv_node_as_string(_impl->name);
341 const char*
342 LV2Plugin::maker() const
344 return _impl->author ? lilv_node_as_string (_impl->author) : "Unknown";
347 uint32_t
348 LV2Plugin::num_ports() const
350 return lilv_plugin_get_num_ports(_impl->plugin);
353 uint32_t
354 LV2Plugin::parameter_count() const
356 return lilv_plugin_get_num_ports(_impl->plugin);
359 float
360 LV2Plugin::default_value(uint32_t port)
362 return _defaults[port];
365 const char*
366 LV2Plugin::port_symbol(uint32_t index) const
368 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, index);
369 if (!port) {
370 error << name() << ": Invalid port index " << index << endmsg;
373 const LilvNode* sym = lilv_port_get_symbol(_impl->plugin, port);
374 return lilv_node_as_string(sym);
377 void
378 LV2Plugin::set_parameter(uint32_t which, float val)
380 DEBUG_TRACE(DEBUG::LV2, string_compose(
381 "%1 set parameter %2 to %3\n", name(), which, val));
383 if (which < lilv_plugin_get_num_ports(_impl->plugin)) {
384 _shadow_data[which] = val;
385 } else {
386 warning << string_compose(
387 _("Illegal parameter number used with plugin \"%1\". "
388 "This is a bug in either %2 or the LV2 plugin <%3>"),
389 name(), PROGRAM_NAME, unique_id()) << endmsg;
392 Plugin::set_parameter(which, val);
395 float
396 LV2Plugin::get_parameter(uint32_t which) const
398 if (parameter_is_input(which)) {
399 return (float)_shadow_data[which];
400 } else {
401 return (float)_control_data[which];
403 return 0.0f;
406 uint32_t
407 LV2Plugin::nth_parameter(uint32_t n, bool& ok) const
409 ok = false;
410 for (uint32_t c = 0, x = 0; x < lilv_plugin_get_num_ports(_impl->plugin); ++x) {
411 if (parameter_is_control(x)) {
412 if (c++ == n) {
413 ok = true;
414 return x;
419 return 0;
422 const void*
423 LV2Plugin::extension_data (const char* uri) const
425 return lilv_instance_get_extension_data(_impl->instance, uri);
428 void*
429 LV2Plugin::c_plugin ()
431 return _impl->plugin;
434 void*
435 LV2Plugin::c_ui ()
437 return (void*)_impl->ui;
440 void*
441 LV2Plugin::c_ui_type ()
443 return (void*)_impl->ui_type;
447 LV2Plugin::lv2_persist_store_callback(void* host_data,
448 uint32_t key,
449 const void* value,
450 size_t size,
451 uint32_t type,
452 uint32_t flags)
454 DEBUG_TRACE(DEBUG::LV2, string_compose(
455 "persist store %1 (size: %2, type: %3)\n",
456 _uri_map.id_to_uri(NULL, key),
457 size,
458 _uri_map.id_to_uri(NULL, type)));
460 LV2PersistState* state = (LV2PersistState*)host_data;
461 state->add_uri(key, _uri_map.id_to_uri(NULL, key));
462 state->add_uri(type, _uri_map.id_to_uri(NULL, type));
463 return state->add_value(key, value, size, type, flags);
466 const void*
467 LV2Plugin::lv2_persist_retrieve_callback(void* host_data,
468 uint32_t key,
469 size_t* size,
470 uint32_t* type,
471 uint32_t* flags)
473 LV2PersistState* state = (LV2PersistState*)host_data;
474 LV2PersistState::Values::const_iterator i = state->values.find(key);
475 if (i == state->values.end()) {
476 warning << "LV2 plugin attempted to retrieve nonexistent key: "
477 << _uri_map.id_to_uri(NULL, key) << endmsg;
478 return NULL;
480 *size = i->second.size;
481 *type = i->second.type;
482 *flags = LV2_PERSIST_IS_POD | LV2_PERSIST_IS_PORTABLE; // FIXME
483 DEBUG_TRACE(DEBUG::LV2, string_compose(
484 "persist retrieve %1 = %2 (size: %3, type: %4)\n",
485 _uri_map.id_to_uri(NULL, key),
486 i->second.value, *size, *type));
487 return i->second.value;
490 char*
491 LV2Plugin::lv2_files_abstract_path(LV2_Files_Host_Data host_data,
492 const char* absolute_path)
494 LV2Plugin* me = (LV2Plugin*)host_data;
495 if (me->_insert_id == PBD::ID("0")) {
496 return g_strdup(absolute_path);
499 const std::string state_dir = Glib::build_filename(me->_session.plugins_dir(),
500 me->_insert_id.to_s());
502 char* ret = NULL;
503 if (strncmp(absolute_path, state_dir.c_str(), state_dir.length())) {
504 ret = g_strdup(absolute_path);
505 } else {
506 const std::string path(absolute_path + state_dir.length() + 1);
507 ret = g_strndup(path.c_str(), path.length());
510 DEBUG_TRACE(DEBUG::LV2, string_compose("abstract path %1 => %2\n",
511 absolute_path, ret));
513 return ret;
516 char*
517 LV2Plugin::lv2_files_absolute_path(LV2_Files_Host_Data host_data,
518 const char* abstract_path)
520 LV2Plugin* me = (LV2Plugin*)host_data;
521 if (me->_insert_id == PBD::ID("0")) {
522 return g_strdup(abstract_path);
525 char* ret = NULL;
526 if (g_path_is_absolute(abstract_path)) {
527 ret = g_strdup(abstract_path);
528 } else {
529 const std::string apath(abstract_path);
530 const std::string state_dir = Glib::build_filename(me->_session.plugins_dir(),
531 me->_insert_id.to_s());
532 const std::string path = Glib::build_filename(state_dir,
533 apath);
534 ret = g_strndup(path.c_str(), path.length());
537 DEBUG_TRACE(DEBUG::LV2, string_compose("absolute path %1 => %2\n",
538 abstract_path, ret));
540 return ret;
543 char*
544 LV2Plugin::lv2_files_new_file_path(LV2_Files_Host_Data host_data,
545 const char* relative_path)
547 LV2Plugin* me = (LV2Plugin*)host_data;
548 if (me->_insert_id == PBD::ID("0")) {
549 return g_strdup(relative_path);
552 const std::string state_dir = Glib::build_filename(me->_session.plugins_dir(),
553 me->_insert_id.to_s());
554 const std::string path = Glib::build_filename(state_dir,
555 relative_path);
557 char* dirname = g_path_get_dirname(path.c_str());
558 g_mkdir_with_parents(dirname, 0744);
559 free(dirname);
561 DEBUG_TRACE(DEBUG::LV2, string_compose("new file path %1 => %2\n",
562 relative_path, path));
564 return g_strndup(path.c_str(), path.length());
567 void
568 LV2Plugin::add_state(XMLNode* root) const
570 assert(_insert_id != PBD::ID("0"));
572 XMLNode* child;
573 char buf[16];
574 LocaleGuard lg(X_("POSIX"));
576 for (uint32_t i = 0; i < parameter_count(); ++i) {
577 if (parameter_is_input(i) && parameter_is_control(i)) {
578 child = new XMLNode("Port");
579 child->add_property("symbol", port_symbol(i));
580 snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
581 child->add_property("value", string(buf));
582 root->add_child_nocopy(*child);
586 if (_supports_persist) {
587 // Create state directory for this plugin instance
588 cerr << "Create statefile name from ID " << _insert_id << endl;
589 const std::string state_filename = _insert_id.to_s() + ".rdff";
590 const std::string state_path = Glib::build_filename(
591 _session.plugins_dir(), state_filename);
593 cout << "Saving LV2 plugin state to " << state_path << endl;
595 // Get LV2 Persist extension data from plugin instance
596 LV2_Persist* persist = (LV2_Persist*)extension_data(
597 "http://lv2plug.in/ns/ext/persist");
598 if (!persist) {
599 warning << string_compose(
600 _("Plugin \"%1\% failed to return LV2 persist data"),
601 unique_id());
602 return;
605 // Save plugin state to state object
606 LV2PersistState state(_uri_map);
607 persist->save(_impl->instance->lv2_handle,
608 &LV2Plugin::lv2_persist_store_callback,
609 &state);
611 // Write state object to RDFF file
612 RDFF file = rdff_open(state_path.c_str(), true);
613 state.write(file);
614 rdff_close(file);
616 root->add_property("state-file", state_filename);
620 static inline const LilvNode*
621 get_value(LilvWorld* world, const LilvNode* subject, const LilvNode* predicate)
623 LilvNodes* vs = lilv_world_find_nodes(world, subject, predicate, NULL);
624 return vs ? lilv_nodes_get_first(vs) : NULL;
627 void
628 LV2Plugin::find_presets()
630 LilvNode* dc_title = lilv_new_uri(_world.world, NS_DC "title");
631 LilvNode* pset_hasPreset = lilv_new_uri(_world.world, NS_PSET "hasPreset");
633 LilvNodes* presets = lilv_plugin_get_value(_impl->plugin, pset_hasPreset);
634 LILV_FOREACH(nodes, i, presets) {
635 const LilvNode* preset = lilv_nodes_get(presets, i);
636 const LilvNode* name = get_value(_world.world, preset, dc_title);
637 if (name) {
638 _presets.insert(std::make_pair(lilv_node_as_string(preset),
639 PresetRecord(
640 lilv_node_as_string(preset),
641 lilv_node_as_string(name))));
642 } else {
643 warning << string_compose(
644 _("Plugin \"%1\% preset \"%2%\" is missing a dc:title\n"),
645 unique_id(), lilv_node_as_string(preset));
648 lilv_nodes_free(presets);
650 lilv_node_free(pset_hasPreset);
651 lilv_node_free(dc_title);
654 bool
655 LV2Plugin::load_preset(PresetRecord r)
657 Plugin::load_preset(r);
659 LilvNode* lv2_port = lilv_new_uri(_world.world, NS_LV2 "port");
660 LilvNode* lv2_symbol = lilv_new_uri(_world.world, NS_LV2 "symbol");
661 LilvNode* pset_value = lilv_new_uri(_world.world, NS_PSET "value");
662 LilvNode* preset = lilv_new_uri(_world.world, r.uri.c_str());
664 LilvNodes* ports = lilv_world_find_nodes(_world.world, preset, lv2_port, NULL);
665 LILV_FOREACH(nodes, i, ports) {
666 const LilvNode* port = lilv_nodes_get(ports, i);
667 const LilvNode* symbol = get_value(_world.world, port, lv2_symbol);
668 const LilvNode* value = get_value(_world.world, port, pset_value);
669 if (value && lilv_node_is_float(value)) {
670 set_parameter(_port_indices[lilv_node_as_string(symbol)],
671 lilv_node_as_float(value));
674 lilv_nodes_free(ports);
676 lilv_node_free(preset);
677 lilv_node_free(pset_value);
678 lilv_node_free(lv2_symbol);
679 lilv_node_free(lv2_port);
681 return true;
684 std::string
685 LV2Plugin::do_save_preset(string /*name*/)
687 return "";
690 void
691 LV2Plugin::do_remove_preset(string /*name*/)
694 bool
695 LV2Plugin::has_editor() const
697 return _impl->ui != NULL;
700 void
701 LV2Plugin::set_insert_info(const PluginInsert* insert)
703 _insert_id = insert->id();
707 LV2Plugin::set_state(const XMLNode& node, int version)
709 XMLNodeList nodes;
710 const XMLProperty* prop;
711 XMLNodeConstIterator iter;
712 XMLNode* child;
713 const char* sym;
714 const char* value;
715 uint32_t port_id;
716 LocaleGuard lg(X_("POSIX"));
718 if (node.name() != state_node_name()) {
719 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
720 return -1;
723 if (version < 3000) {
724 nodes = node.children("port");
725 } else {
726 nodes = node.children("Port");
729 for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
731 child = *iter;
733 if ((prop = child->property("symbol")) != 0) {
734 sym = prop->value().c_str();
735 } else {
736 warning << _("LV2: port has no symbol, ignored") << endmsg;
737 continue;
740 map<string, uint32_t>::iterator i = _port_indices.find(sym);
742 if (i != _port_indices.end()) {
743 port_id = i->second;
744 } else {
745 warning << _("LV2: port has unknown index, ignored") << endmsg;
746 continue;
749 if ((prop = child->property("value")) != 0) {
750 value = prop->value().c_str();
751 } else {
752 warning << _("LV2: port has no value, ignored") << endmsg;
753 continue;
756 set_parameter(port_id, atof(value));
759 if ((prop = node.property("state-file")) != 0) {
760 std::string state_path = Glib::build_filename(_session.plugins_dir(),
761 prop->value());
763 cout << "LV2 state path " << state_path << endl;
765 // Get LV2 Persist extension data from plugin instance
766 LV2_Persist* persist = (LV2_Persist*)extension_data(
767 "http://lv2plug.in/ns/ext/persist");
768 if (persist) {
769 cout << "Loading LV2 state from " << state_path << endl;
770 RDFF file = rdff_open(state_path.c_str(), false);
771 LV2PersistState state(_uri_map);
772 state.read(file);
773 persist->restore(_impl->instance->lv2_handle,
774 &LV2Plugin::lv2_persist_retrieve_callback,
775 &state);
776 rdff_close(file);
777 } else {
778 warning << string_compose(
779 _("Plugin \"%1\% failed to return LV2 persist data"),
780 unique_id());
784 latency_compute_run();
786 return Plugin::set_state(node, version);
790 LV2Plugin::get_parameter_descriptor(uint32_t which, ParameterDescriptor& desc) const
792 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, which);
794 LilvNode *def, *min, *max;
795 lilv_port_get_range(_impl->plugin, port, &def, &min, &max);
797 desc.integer_step = lilv_port_has_property(_impl->plugin, port, _world.integer);
798 desc.toggled = lilv_port_has_property(_impl->plugin, port, _world.toggled);
799 desc.logarithmic = lilv_port_has_property(_impl->plugin, port, _world.logarithmic);
800 desc.sr_dependent = lilv_port_has_property(_impl->plugin, port, _world.srate);
801 desc.label = lilv_node_as_string(lilv_port_get_name(_impl->plugin, port));
802 desc.lower = min ? lilv_node_as_float(min) : 0.0f;
803 desc.upper = max ? lilv_node_as_float(max) : 1.0f;
804 if (desc.sr_dependent) {
805 desc.lower *= _session.frame_rate ();
806 desc.upper *= _session.frame_rate ();
809 desc.min_unbound = false; // TODO: LV2 extension required
810 desc.max_unbound = false; // TODO: LV2 extension required
812 if (desc.integer_step) {
813 desc.step = 1.0;
814 desc.smallstep = 0.1;
815 desc.largestep = 10.0;
816 } else {
817 const float delta = desc.upper - desc.lower;
818 desc.step = delta / 1000.0f;
819 desc.smallstep = delta / 10000.0f;
820 desc.largestep = delta / 10.0f;
823 lilv_node_free(def);
824 lilv_node_free(min);
825 lilv_node_free(max);
827 return 0;
830 string
831 LV2Plugin::describe_parameter(Evoral::Parameter which)
833 if (( which.type() == PluginAutomation) && ( which.id() < parameter_count()) ) {
834 LilvNode* name = lilv_port_get_name(_impl->plugin,
835 lilv_plugin_get_port_by_index(_impl->plugin, which.id()));
836 string ret(lilv_node_as_string(name));
837 lilv_node_free(name);
838 return ret;
839 } else {
840 return "??";
844 framecnt_t
845 LV2Plugin::signal_latency() const
847 if (_latency_control_port) {
848 return (framecnt_t)floor(*_latency_control_port);
849 } else {
850 return 0;
854 set<Evoral::Parameter>
855 LV2Plugin::automatable() const
857 set<Evoral::Parameter> ret;
859 for (uint32_t i = 0; i < parameter_count(); ++i) {
860 if (parameter_is_input(i) && parameter_is_control(i)) {
861 ret.insert(ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
865 return ret;
868 void
869 LV2Plugin::activate()
871 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 activate\n", name()));
873 if (!_was_activated) {
874 lilv_instance_activate(_impl->instance);
875 _was_activated = true;
879 void
880 LV2Plugin::deactivate()
882 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 deactivate\n", name()));
884 if (_was_activated) {
885 lilv_instance_deactivate(_impl->instance);
886 _was_activated = false;
890 void
891 LV2Plugin::cleanup()
893 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 cleanup\n", name()));
895 activate();
896 deactivate();
897 lilv_instance_free(_impl->instance);
898 _impl->instance = NULL;
902 LV2Plugin::connect_and_run(BufferSet& bufs,
903 ChanMapping in_map, ChanMapping out_map,
904 pframes_t nframes, framecnt_t offset)
906 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 run %2 offset %3\n", name(), nframes, offset));
907 Plugin::connect_and_run(bufs, in_map, out_map, nframes, offset);
909 cycles_t then = get_cycles();
911 uint32_t audio_in_index = 0;
912 uint32_t audio_out_index = 0;
913 uint32_t midi_in_index = 0;
914 uint32_t midi_out_index = 0;
915 for (uint32_t port_index = 0; port_index < parameter_count(); ++port_index) {
916 if (parameter_is_audio(port_index)) {
917 if (parameter_is_input(port_index)) {
918 const uint32_t buf_index = in_map.get(DataType::AUDIO, audio_in_index++);
919 lilv_instance_connect_port(_impl->instance, port_index,
920 bufs.get_audio(buf_index).data(offset));
921 } else if (parameter_is_output(port_index)) {
922 const uint32_t buf_index = out_map.get(DataType::AUDIO, audio_out_index++);
923 //cerr << port_index << " : " << " AUDIO OUT " << buf_index << endl;
924 lilv_instance_connect_port(_impl->instance, port_index,
925 bufs.get_audio(buf_index).data(offset));
927 } else if (parameter_is_midi(port_index)) {
928 if (parameter_is_input(port_index)) {
929 const uint32_t buf_index = in_map.get(DataType::MIDI, midi_in_index++);
930 lilv_instance_connect_port(_impl->instance, port_index,
931 bufs.get_lv2_midi(true, buf_index).data());
932 } else if (parameter_is_output(port_index)) {
933 const uint32_t buf_index = out_map.get(DataType::MIDI, midi_out_index++);
934 lilv_instance_connect_port(_impl->instance, port_index,
935 bufs.get_lv2_midi(false, buf_index).data());
937 } else if (!parameter_is_control(port_index)) {
938 // Optional port (it'd better be if we've made it this far...)
939 lilv_instance_connect_port(_impl->instance, port_index, NULL);
943 run(nframes);
945 midi_out_index = 0;
946 for (uint32_t port_index = 0; port_index < parameter_count(); ++port_index) {
947 if (parameter_is_midi(port_index) && parameter_is_output(port_index)) {
948 const uint32_t buf_index = out_map.get(DataType::MIDI, midi_out_index++);
949 bufs.flush_lv2_midi(true, buf_index);
953 cycles_t now = get_cycles();
954 set_cycles((uint32_t)(now - then));
956 return 0;
959 bool
960 LV2Plugin::parameter_is_control(uint32_t param) const
962 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
963 return lilv_port_is_a(_impl->plugin, port, _world.control_class);
966 bool
967 LV2Plugin::parameter_is_audio(uint32_t param) const
969 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
970 return lilv_port_is_a(_impl->plugin, port, _world.audio_class);
973 bool
974 LV2Plugin::parameter_is_midi(uint32_t param) const
976 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
977 return lilv_port_is_a(_impl->plugin, port, _world.event_class);
978 // && lilv_port_supports_event(_impl->plugin, port, _world.midi_class);
981 bool
982 LV2Plugin::parameter_is_output(uint32_t param) const
984 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
985 return lilv_port_is_a(_impl->plugin, port, _world.output_class);
988 bool
989 LV2Plugin::parameter_is_input(uint32_t param) const
991 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
992 return lilv_port_is_a(_impl->plugin, port, _world.input_class);
995 void
996 LV2Plugin::print_parameter(uint32_t param, char* buf, uint32_t len) const
998 if (buf && len) {
999 if (param < parameter_count()) {
1000 snprintf(buf, len, "%.3f", get_parameter(param));
1001 } else {
1002 strcat(buf, "0");
1007 boost::shared_ptr<Plugin::ScalePoints>
1008 LV2Plugin::get_scale_points(uint32_t port_index) const
1010 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, port_index);
1011 LilvScalePoints* points = lilv_port_get_scale_points(_impl->plugin, port);
1013 boost::shared_ptr<Plugin::ScalePoints> ret;
1014 if (!points) {
1015 return ret;
1018 ret = boost::shared_ptr<Plugin::ScalePoints>(new ScalePoints());
1020 LILV_FOREACH(scale_points, i, points) {
1021 const LilvScalePoint* p = lilv_scale_points_get(points, i);
1022 const LilvNode* label = lilv_scale_point_get_label(p);
1023 const LilvNode* value = lilv_scale_point_get_value(p);
1024 if (label && (lilv_node_is_float(value) || lilv_node_is_int(value))) {
1025 ret->insert(make_pair(lilv_node_as_string(label),
1026 lilv_node_as_float(value)));
1030 lilv_scale_points_free(points);
1031 return ret;
1034 void
1035 LV2Plugin::run(pframes_t nframes)
1037 for (uint32_t i = 0; i < parameter_count(); ++i) {
1038 if (parameter_is_control(i) && parameter_is_input(i)) {
1039 _control_data[i] = _shadow_data[i];
1043 lilv_instance_run(_impl->instance, nframes);
1046 void
1047 LV2Plugin::latency_compute_run()
1049 if (!_latency_control_port) {
1050 return;
1053 // Run the plugin so that it can set its latency parameter
1055 activate();
1057 uint32_t port_index = 0;
1058 uint32_t in_index = 0;
1059 uint32_t out_index = 0;
1061 const framecnt_t bufsize = 1024;
1062 float buffer[bufsize];
1064 memset(buffer, 0, sizeof(float) * bufsize);
1066 // FIXME: Ensure plugins can handle in-place processing
1068 port_index = 0;
1070 while (port_index < parameter_count()) {
1071 if (parameter_is_audio(port_index)) {
1072 if (parameter_is_input(port_index)) {
1073 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1074 in_index++;
1075 } else if (parameter_is_output(port_index)) {
1076 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1077 out_index++;
1080 port_index++;
1083 run(bufsize);
1084 deactivate();
1087 LV2World::LV2World()
1088 : world(lilv_world_new())
1090 lilv_world_load_all(world);
1091 input_class = lilv_new_uri(world, LILV_URI_INPUT_PORT);
1092 output_class = lilv_new_uri(world, LILV_URI_OUTPUT_PORT);
1093 control_class = lilv_new_uri(world, LILV_URI_CONTROL_PORT);
1094 audio_class = lilv_new_uri(world, LILV_URI_AUDIO_PORT);
1095 event_class = lilv_new_uri(world, LILV_URI_EVENT_PORT);
1096 midi_class = lilv_new_uri(world, LILV_URI_MIDI_EVENT);
1097 in_place_broken = lilv_new_uri(world, LILV_NS_LV2 "inPlaceBroken");
1098 integer = lilv_new_uri(world, LILV_NS_LV2 "integer");
1099 toggled = lilv_new_uri(world, LILV_NS_LV2 "toggled");
1100 srate = lilv_new_uri(world, LILV_NS_LV2 "sampleRate");
1101 gtk_gui = lilv_new_uri(world, NS_UI "GtkUI");
1102 external_gui = lilv_new_uri(world, NS_UI "external");
1103 logarithmic = lilv_new_uri(world, "http://lv2plug.in/ns/dev/extportinfo#logarithmic");
1106 LV2World::~LV2World()
1108 lilv_node_free(input_class);
1109 lilv_node_free(output_class);
1110 lilv_node_free(control_class);
1111 lilv_node_free(audio_class);
1112 lilv_node_free(event_class);
1113 lilv_node_free(midi_class);
1114 lilv_node_free(in_place_broken);
1117 LV2PluginInfo::LV2PluginInfo (void* c_plugin)
1118 : _c_plugin(c_plugin)
1120 type = ARDOUR::LV2;
1123 LV2PluginInfo::~LV2PluginInfo()
1126 PluginPtr
1127 LV2PluginInfo::load(Session& session)
1129 try {
1130 PluginPtr plugin;
1132 plugin.reset(new LV2Plugin(session.engine(), session,
1133 (LilvPlugin*)_c_plugin,
1134 session.frame_rate()));
1136 plugin->set_info(PluginInfoPtr(new LV2PluginInfo(*this)));
1137 return plugin;
1138 } catch (failed_constructor& err) {
1139 return PluginPtr((Plugin*)0);
1142 return PluginPtr();
1145 PluginInfoList*
1146 LV2PluginInfo::discover()
1148 PluginInfoList* plugs = new PluginInfoList;
1149 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
1151 cerr << "LV2: Discovering " << lilv_plugins_size(plugins) << " plugins" << endl;
1153 LILV_FOREACH(plugins, i, plugins) {
1154 const LilvPlugin* p = lilv_plugins_get(plugins, i);
1155 LV2PluginInfoPtr info(new LV2PluginInfo((void*)p));
1157 LilvNode* name = lilv_plugin_get_name(p);
1158 if (!name) {
1159 cerr << "LV2: invalid plugin\n";
1160 continue;
1163 info->type = LV2;
1165 info->name = string(lilv_node_as_string(name));
1166 lilv_node_free(name);
1168 const LilvPluginClass* pclass = lilv_plugin_get_class(p);
1169 const LilvNode* label = lilv_plugin_class_get_label(pclass);
1170 info->category = lilv_node_as_string(label);
1172 LilvNode* author_name = lilv_plugin_get_author_name(p);
1173 info->creator = author_name ? string(lilv_node_as_string(author_name)) : "Unknown";
1174 lilv_node_free(author_name);
1176 info->path = "/NOPATH"; // Meaningless for LV2
1178 info->n_inputs.set_audio(
1179 lilv_plugin_get_num_ports_of_class(
1180 p, _world.input_class, _world.audio_class, NULL));
1181 info->n_inputs.set_midi(
1182 lilv_plugin_get_num_ports_of_class(
1183 p, _world.input_class, _world.event_class, NULL));
1185 info->n_outputs.set_audio(
1186 lilv_plugin_get_num_ports_of_class(
1187 p, _world.output_class, _world.audio_class, NULL));
1188 info->n_outputs.set_midi(
1189 lilv_plugin_get_num_ports_of_class(
1190 p, _world.output_class, _world.event_class, NULL));
1192 info->unique_id = lilv_node_as_uri(lilv_plugin_get_uri(p));
1193 info->index = 0; // Meaningless for LV2
1195 plugs->push_back(info);
1198 cerr << "Done LV2 discovery" << endl;
1200 return plugs;