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.
29 #include <giomm/file.h>
31 #include <boost/utility.hpp>
33 #include "pbd/compose.h"
34 #include "pbd/error.h"
35 #include "pbd/pathscanner.h"
36 #include "pbd/stl_delete.h"
37 #include "pbd/xml++.h"
39 #include "libardour-config.h"
41 #include "ardour/ardour.h"
42 #include "ardour/audio_buffer.h"
43 #include "ardour/audioengine.h"
44 #include "ardour/debug.h"
45 #include "ardour/lv2_event_buffer.h"
46 #include "ardour/lv2_plugin.h"
47 #include "ardour/lv2_state.h"
48 #include "ardour/session.h"
53 #include <lilv/lilv.h>
55 #include "lv2/lv2plug.in/ns/ext/state/state.h"
58 #include <suil/suil.h>
61 #define NS_DC "http://dublincore.org/documents/dcmi-namespace/"
62 #define NS_LV2 "http://lv2plug.in/ns/lv2core#"
63 #define NS_OLDPSET "http://lv2plug.in/ns/dev/presets#"
64 #define NS_PSET "http://lv2plug.in/ns/ext/presets#"
65 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
66 #define NS_RDFS "http://www.w3.org/2000/01/rdf-schema#"
69 using namespace ARDOUR;
72 URIMap LV2Plugin::_uri_map;
73 uint32_t LV2Plugin::_midi_event_type = _uri_map.uri_to_id(
74 "http://lv2plug.in/ns/ext/event",
75 "http://lv2plug.in/ns/ext/midi#MidiEvent");
76 uint32_t LV2Plugin::_state_path_type = _uri_map.uri_to_id(
77 NULL, LV2_STATE_PATH_URI);
79 class LV2World : boost::noncopyable {
85 LilvNode* input_class; ///< Input port
86 LilvNode* output_class; ///< Output port
87 LilvNode* audio_class; ///< Audio port
88 LilvNode* control_class; ///< Control port
89 LilvNode* event_class; ///< Event port
90 LilvNode* midi_class; ///< MIDI event
91 LilvNode* in_place_broken;
96 LilvNode* external_gui;
97 LilvNode* logarithmic;
100 static LV2World _world;
102 struct LV2Plugin::Impl {
103 Impl() : plugin(0), ui(0), ui_type(0), name(0), author(0), instance(0) {}
106 const LilvNode* ui_type;
109 LilvInstance* instance;
112 LV2Plugin::LV2Plugin (AudioEngine& engine,
116 : Plugin(engine, session)
121 init(c_plugin, rate);
124 LV2Plugin::LV2Plugin (const LV2Plugin& other)
128 , _insert_id(other._insert_id)
130 init(other._impl->plugin, other._sample_rate);
132 for (uint32_t i = 0; i < parameter_count(); ++i) {
133 _control_data[i] = other._shadow_data[i];
134 _shadow_data[i] = other._shadow_data[i];
139 LV2Plugin::init(void* c_plugin, framecnt_t rate)
141 DEBUG_TRACE(DEBUG::LV2, "init\n");
143 _impl->plugin = (LilvPlugin*)c_plugin;
145 _impl->ui_type = NULL;
148 _latency_control_port = 0;
149 _was_activated = false;
151 _instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
152 _data_access_feature.URI = "http://lv2plug.in/ns/ext/data-access";
153 _map_path_feature.URI = LV2_STATE_MAP_PATH_URI;
154 _make_path_feature.URI = LV2_STATE_MAKE_PATH_URI;
156 LilvPlugin* plugin = _impl->plugin;
158 LilvNode* state_iface_uri = lilv_new_uri(_world.world, LV2_STATE_INTERFACE_URI);
159 #ifdef lilv_plugin_has_extension_data
160 _has_state_interface = lilv_plugin_has_extension_data(plugin, state_iface_uri);
161 lilv_node_free(state_iface_uri);
163 LilvNode* lv2_extensionData = lilv_new_uri(_world.world, NS_LV2 "extensionData");
164 LilvNodes* extdatas = lilv_plugin_get_value(plugin, lv2_extensionData);
165 LILV_FOREACH(nodes, i, extdatas) {
166 if (lilv_node_equals(lilv_nodes_get(extdatas, i), state_iface_uri)) {
167 _has_state_interface = true;
173 _features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * 8);
174 _features[0] = &_instance_access_feature;
175 _features[1] = &_data_access_feature;
176 _features[2] = &_map_path_feature;
177 _features[3] = &_make_path_feature;
178 _features[4] = _uri_map.uri_map_feature();
179 _features[5] = _uri_map.urid_map_feature();
180 _features[6] = _uri_map.urid_unmap_feature();
183 LV2_State_Map_Path* map_path = (LV2_State_Map_Path*)malloc(
184 sizeof(LV2_State_Map_Path));
185 map_path->handle = this;
186 map_path->abstract_path = &lv2_state_abstract_path;
187 map_path->absolute_path = &lv2_state_absolute_path;
188 _map_path_feature.data = map_path;
190 LV2_State_Make_Path* make_path = (LV2_State_Make_Path*)malloc(
191 sizeof(LV2_State_Make_Path));
192 make_path->handle = this;
193 make_path->path = &lv2_state_make_path;
194 _make_path_feature.data = make_path;
196 _impl->instance = lilv_plugin_instantiate(plugin, rate, _features);
197 _impl->name = lilv_plugin_get_name(plugin);
198 _impl->author = lilv_plugin_get_author_name(plugin);
200 if (_impl->instance == 0) {
201 error << _("LV2: Failed to instantiate plugin ") << uri() << endmsg;
202 throw failed_constructor();
205 _instance_access_feature.data = (void*)_impl->instance->lv2_handle;
206 _data_access_extension_data.extension_data = _impl->instance->lv2_descriptor->extension_data;
207 _data_access_feature.data = &_data_access_extension_data;
209 if (lilv_plugin_has_feature(plugin, _world.in_place_broken)) {
210 error << string_compose(
211 _("LV2: \"%1\" cannot be used, since it cannot do inplace processing"),
212 lilv_node_as_string(_impl->name)) << endmsg;
213 lilv_node_free(_impl->name);
214 lilv_node_free(_impl->author);
215 throw failed_constructor();
220 const uint32_t num_ports = this->num_ports();
221 const bool latent = lilv_plugin_has_latency(plugin);
222 const uint32_t latency_port = (latent)
223 ? lilv_plugin_get_latency_port_index(plugin)
226 _control_data = new float[num_ports];
227 _shadow_data = new float[num_ports];
228 _defaults = new float[num_ports];
230 for (uint32_t i = 0; i < num_ports; ++i) {
231 const LilvPort* port = lilv_plugin_get_port_by_index(plugin, i);
232 const LilvNode* sym = lilv_port_get_symbol(plugin, port);
234 // Store index in map so we can look up index by symbol
235 _port_indices.insert(std::make_pair(lilv_node_as_string(sym), i));
237 // Get range and default value if applicable
238 if (parameter_is_control(i)) {
240 lilv_port_get_range(plugin, port, &def, NULL, NULL);
241 _defaults[i] = def ? lilv_node_as_float(def) : 0.0f;
242 if (lilv_port_has_property (plugin, port, _world.srate)) {
243 _defaults[i] *= _session.frame_rate ();
247 lilv_instance_connect_port(_impl->instance, i, &_control_data[i]);
249 if (latent && ( i == latency_port) ) {
250 _latency_control_port = &_control_data[i];
251 *_latency_control_port = 0;
254 if (parameter_is_input(i)) {
255 _shadow_data[i] = default_value(i);
262 LilvUIs* uis = lilv_plugin_get_uis(plugin);
263 if (lilv_uis_size(uis) > 0) {
265 // Look for embeddable UI
266 LILV_FOREACH(uis, u, uis) {
267 const LilvUI* this_ui = lilv_uis_get(uis, u);
268 const LilvNode* this_ui_type = NULL;
269 if (lilv_ui_is_supported(this_ui,
273 // TODO: Multiple UI support
275 _impl->ui_type = this_ui_type;
280 // Look for Gtk native UI
281 LILV_FOREACH(uis, i, uis) {
282 const LilvUI* ui = lilv_uis_get(uis, i);
283 if (lilv_ui_is_a(ui, _world.gtk_gui)) {
285 _impl->ui_type = _world.gtk_gui;
291 // If Gtk UI is not available, try to find external UI
293 LILV_FOREACH(uis, i, uis) {
294 const LilvUI* ui = lilv_uis_get(uis, i);
295 if (lilv_ui_is_a(ui, _world.external_gui)) {
297 _impl->ui_type = _world.external_gui;
304 latency_compute_run();
307 LV2Plugin::~LV2Plugin ()
309 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 destroy\n", name()));
314 lilv_instance_free(_impl->instance);
315 lilv_node_free(_impl->name);
316 lilv_node_free(_impl->author);
318 delete [] _control_data;
319 delete [] _shadow_data;
323 LV2Plugin::is_external_ui() const
328 return lilv_ui_is_a(_impl->ui, _world.external_gui);
332 LV2Plugin::unique_id() const
334 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
338 LV2Plugin::uri() const
340 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
344 LV2Plugin::label() const
346 return lilv_node_as_string(_impl->name);
350 LV2Plugin::name() const
352 return lilv_node_as_string(_impl->name);
356 LV2Plugin::maker() const
358 return _impl->author ? lilv_node_as_string (_impl->author) : "Unknown";
362 LV2Plugin::num_ports() const
364 return lilv_plugin_get_num_ports(_impl->plugin);
368 LV2Plugin::parameter_count() const
370 return lilv_plugin_get_num_ports(_impl->plugin);
374 LV2Plugin::default_value(uint32_t port)
376 return _defaults[port];
380 LV2Plugin::port_symbol(uint32_t index) const
382 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, index);
384 error << name() << ": Invalid port index " << index << endmsg;
387 const LilvNode* sym = lilv_port_get_symbol(_impl->plugin, port);
388 return lilv_node_as_string(sym);
392 LV2Plugin::set_parameter(uint32_t which, float val)
394 DEBUG_TRACE(DEBUG::LV2, string_compose(
395 "%1 set parameter %2 to %3\n", name(), which, val));
397 if (which < lilv_plugin_get_num_ports(_impl->plugin)) {
398 _shadow_data[which] = val;
400 warning << string_compose(
401 _("Illegal parameter number used with plugin \"%1\". "
402 "This is a bug in either %2 or the LV2 plugin <%3>"),
403 name(), PROGRAM_NAME, unique_id()) << endmsg;
406 Plugin::set_parameter(which, val);
410 LV2Plugin::get_parameter(uint32_t which) const
412 if (parameter_is_input(which)) {
413 return (float)_shadow_data[which];
415 return (float)_control_data[which];
421 LV2Plugin::nth_parameter(uint32_t n, bool& ok) const
424 for (uint32_t c = 0, x = 0; x < lilv_plugin_get_num_ports(_impl->plugin); ++x) {
425 if (parameter_is_control(x)) {
437 LV2Plugin::extension_data (const char* uri) const
439 return lilv_instance_get_extension_data(_impl->instance, uri);
443 LV2Plugin::c_plugin ()
445 return _impl->plugin;
451 return (void*)_impl->ui;
455 LV2Plugin::c_ui_type ()
457 return (void*)_impl->ui_type;
461 LV2Plugin::state_dir() const
463 return Glib::build_filename(_session.plugins_dir(),
468 LV2Plugin::lv2_state_store_callback(LV2_State_Handle handle,
475 DEBUG_TRACE(DEBUG::LV2, string_compose(
476 "state store %1 (size: %2, type: %3, flags: %4)\n",
477 _uri_map.id_to_uri(NULL, key),
479 _uri_map.id_to_uri(NULL, type),
482 LV2State* state = (LV2State*)handle;
483 state->add_uri(key, _uri_map.id_to_uri(NULL, key));
484 state->add_uri(type, _uri_map.id_to_uri(NULL, type));
486 if (type == _state_path_type) {
487 const LV2Plugin& me = state->plugin;
488 LV2_State_Map_Path* mp = (LV2_State_Map_Path*)me._map_path_feature.data;
489 return state->add_value(
491 lv2_state_abstract_path(mp->handle, (const char*)value),
493 } else if ((flags & LV2_STATE_IS_POD) && (flags & LV2_STATE_IS_PORTABLE)) {
494 return state->add_value(key, value, size, type, flags);
496 PBD::warning << "LV2 plugin attempted to store non-portable property." << endl;
502 LV2Plugin::lv2_state_retrieve_callback(LV2_State_Handle host_data,
508 LV2State* state = (LV2State*)host_data;
509 LV2State::Values::const_iterator i = state->values.find(key);
510 if (i == state->values.end()) {
511 warning << "LV2 plugin attempted to retrieve nonexistent key: "
512 << _uri_map.id_to_uri(NULL, key) << endmsg;
515 *size = i->second.size;
516 *type = i->second.type;
517 *flags = LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE;
518 DEBUG_TRACE(DEBUG::LV2, string_compose(
519 "state retrieve %1 = %2 (size: %3, type: %4)\n",
520 _uri_map.id_to_uri(NULL, key),
521 i->second.value, *size, *type));
522 if (*type == _state_path_type) {
523 const LV2Plugin& me = state->plugin;
524 LV2_State_Map_Path* mp = (LV2_State_Map_Path*)me._map_path_feature.data;
525 return lv2_state_absolute_path(mp->handle, (const char*)i->second.value);
527 return i->second.value;
532 LV2Plugin::lv2_state_abstract_path(LV2_State_Map_Path_Handle handle,
533 const char* absolute_path)
535 LV2Plugin* me = (LV2Plugin*)handle;
536 if (me->_insert_id == PBD::ID("0")) {
537 return g_strdup(absolute_path);
540 const std::string state_dir = me->state_dir();
543 if (strncmp(absolute_path, state_dir.c_str(), state_dir.length())) {
544 // Path outside state directory, make symbolic link
545 const std::string name = Glib::path_get_basename(absolute_path);
546 const std::string path = Glib::build_filename(state_dir, name);
547 Gio::File::create_for_path(path)->make_symbolic_link(absolute_path);
548 ret = g_strndup(path.c_str(), path.length());
550 // Path inside the state directory, return relative path
551 const std::string path(absolute_path + state_dir.length() + 1);
552 ret = g_strndup(path.c_str(), path.length());
555 DEBUG_TRACE(DEBUG::LV2, string_compose("abstract path %1 => %2\n",
556 absolute_path, ret));
562 LV2Plugin::lv2_state_absolute_path(LV2_State_Map_Path_Handle handle,
563 const char* abstract_path)
565 LV2Plugin* me = (LV2Plugin*)handle;
566 if (me->_insert_id == PBD::ID("0")) {
567 return g_strdup(abstract_path);
571 if (g_path_is_absolute(abstract_path)) {
572 ret = g_strdup(abstract_path);
574 const std::string apath(abstract_path);
575 const std::string path = Glib::build_filename(me->state_dir(), apath);
576 ret = g_strndup(path.c_str(), path.length());
579 DEBUG_TRACE(DEBUG::LV2, string_compose("absolute path %1 => %2\n",
580 abstract_path, ret));
586 LV2Plugin::lv2_state_make_path(LV2_State_Make_Path_Handle handle,
589 LV2Plugin* me = (LV2Plugin*)handle;
590 if (me->_insert_id == PBD::ID("0")) {
591 return g_strdup(path);
594 const std::string abs_path = Glib::build_filename(me->state_dir(), path);
595 const std::string dirname = Glib::path_get_dirname(abs_path);
597 g_mkdir_with_parents(dirname.c_str(), 0744);
599 DEBUG_TRACE(DEBUG::LV2, string_compose("new file path %1 => %2\n",
602 return g_strndup(abs_path.c_str(), abs_path.length());
606 remove_directory(const std::string& path)
608 if (!Glib::file_test(path, Glib::FILE_TEST_IS_DIR)) {
612 Glib::RefPtr<Gio::File> dir = Gio::File::create_for_path(path);
613 Glib::RefPtr<Gio::FileEnumerator> e = dir->enumerate_children();
614 Glib::RefPtr<Gio::FileInfo> fi;
615 while ((fi = e->next_file())) {
616 if (fi->get_type() == Gio::FILE_TYPE_DIRECTORY) {
617 remove_directory(fi->get_name());
619 dir->get_child(fi->get_name())->remove();
626 LV2Plugin::add_state(XMLNode* root) const
628 assert(_insert_id != PBD::ID("0"));
632 LocaleGuard lg(X_("POSIX"));
634 for (uint32_t i = 0; i < parameter_count(); ++i) {
635 if (parameter_is_input(i) && parameter_is_control(i)) {
636 child = new XMLNode("Port");
637 child->add_property("symbol", port_symbol(i));
638 snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
639 child->add_property("value", string(buf));
640 root->add_child_nocopy(*child);
644 if (_has_state_interface) {
645 // Create state directory for this plugin instance
646 cerr << "Create statefile name from ID " << _insert_id << endl;
647 const std::string state_filename = _insert_id.to_s() + ".rdff";
648 const std::string state_path = Glib::build_filename(
649 _session.plugins_dir(), state_filename);
651 cout << "Saving LV2 plugin state to " << state_path << endl;
653 // Get LV2 State extension data from plugin instance
654 LV2_State_Interface* state_iface = (LV2_State_Interface*)extension_data(
655 LV2_STATE_INTERFACE_URI);
657 warning << string_compose(
658 _("Plugin \"%1\% failed to return LV2 state interface"),
663 // Remove old state directory (FIXME: should this be preserved?)
664 remove_directory(state_dir());
666 // Save plugin state to state object
667 LV2State state(*this, _uri_map);
668 state_iface->save(_impl->instance->lv2_handle,
669 &LV2Plugin::lv2_state_store_callback,
671 LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE,
674 // Write state object to RDFF file
675 RDFF file = rdff_open(state_path.c_str(), true);
679 root->add_property("state-file", state_filename);
683 static inline const LilvNode*
684 get_value(LilvWorld* world, const LilvNode* subject, const LilvNode* predicate)
686 LilvNodes* vs = lilv_world_find_nodes(world, subject, predicate, NULL);
687 return vs ? lilv_nodes_get_first(vs) : NULL;
691 find_presets_helper(LilvWorld* world,
693 std::map<std::string, Plugin::PresetRecord>& out,
694 LilvNode* preset_pred,
695 LilvNode* title_pred)
697 LilvNodes* presets = lilv_plugin_get_value(plugin, preset_pred);
698 LILV_FOREACH(nodes, i, presets) {
699 const LilvNode* preset = lilv_nodes_get(presets, i);
700 const LilvNode* name = get_value(world, preset, title_pred);
702 out.insert(std::make_pair(lilv_node_as_string(preset),
703 Plugin::PresetRecord(
704 lilv_node_as_string(preset),
705 lilv_node_as_string(name))));
707 warning << string_compose(
708 _("Plugin \"%1\% preset \"%2%\" is missing a label\n"),
709 lilv_node_as_string(lilv_plugin_get_uri(plugin)),
710 lilv_node_as_string(preset));
713 lilv_nodes_free(presets);
717 LV2Plugin::find_presets()
719 LilvNode* dc_title = lilv_new_uri(_world.world, NS_DC "title");
720 LilvNode* oldpset_hasPreset = lilv_new_uri(_world.world, NS_OLDPSET "hasPreset");
721 LilvNode* pset_hasPreset = lilv_new_uri(_world.world, NS_PSET "hasPreset");
722 LilvNode* rdfs_label = lilv_new_uri(_world.world, NS_RDFS "label");
724 find_presets_helper(_world.world, _impl->plugin, _presets,
725 oldpset_hasPreset, dc_title);
727 find_presets_helper(_world.world, _impl->plugin, _presets,
728 pset_hasPreset, rdfs_label);
730 lilv_node_free(rdfs_label);
731 lilv_node_free(pset_hasPreset);
732 lilv_node_free(oldpset_hasPreset);
733 lilv_node_free(dc_title);
737 LV2Plugin::load_preset(PresetRecord r)
739 Plugin::load_preset(r);
741 LilvNode* lv2_port = lilv_new_uri(_world.world, NS_LV2 "port");
742 LilvNode* lv2_symbol = lilv_new_uri(_world.world, NS_LV2 "symbol");
743 LilvNode* oldpset_value = lilv_new_uri(_world.world, NS_OLDPSET "value");
744 LilvNode* preset = lilv_new_uri(_world.world, r.uri.c_str());
745 LilvNode* pset_value = lilv_new_uri(_world.world, NS_PSET "value");
747 LilvNodes* ports = lilv_world_find_nodes(_world.world, preset, lv2_port, NULL);
748 LILV_FOREACH(nodes, i, ports) {
749 const LilvNode* port = lilv_nodes_get(ports, i);
750 const LilvNode* symbol = get_value(_world.world, port, lv2_symbol);
751 const LilvNode* value = get_value(_world.world, port, pset_value);
753 value = get_value(_world.world, port, oldpset_value);
755 if (value && lilv_node_is_float(value)) {
756 set_parameter(_port_indices[lilv_node_as_string(symbol)],
757 lilv_node_as_float(value));
760 lilv_nodes_free(ports);
762 lilv_node_free(pset_value);
763 lilv_node_free(preset);
764 lilv_node_free(oldpset_value);
765 lilv_node_free(lv2_symbol);
766 lilv_node_free(lv2_port);
772 LV2Plugin::do_save_preset(string /*name*/)
778 LV2Plugin::do_remove_preset(string /*name*/)
782 LV2Plugin::has_editor() const
784 return _impl->ui != NULL;
788 LV2Plugin::set_insert_info(const PluginInsert* insert)
790 _insert_id = insert->id();
794 LV2Plugin::set_state(const XMLNode& node, int version)
797 const XMLProperty* prop;
798 XMLNodeConstIterator iter;
803 LocaleGuard lg(X_("POSIX"));
805 if (node.name() != state_node_name()) {
806 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
810 if (version < 3000) {
811 nodes = node.children("port");
813 nodes = node.children("Port");
816 for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
820 if ((prop = child->property("symbol")) != 0) {
821 sym = prop->value().c_str();
823 warning << _("LV2: port has no symbol, ignored") << endmsg;
827 map<string, uint32_t>::iterator i = _port_indices.find(sym);
829 if (i != _port_indices.end()) {
832 warning << _("LV2: port has unknown index, ignored") << endmsg;
836 if ((prop = child->property("value")) != 0) {
837 value = prop->value().c_str();
839 warning << _("LV2: port has no value, ignored") << endmsg;
843 set_parameter(port_id, atof(value));
846 if ((prop = node.property("state-file")) != 0) {
847 std::string state_path = Glib::build_filename(_session.plugins_dir(),
850 cout << "LV2 state path " << state_path << endl;
852 // Get LV2 State extension data from plugin instance
853 LV2_State_Interface* state_iface = (LV2_State_Interface*)extension_data(
854 LV2_STATE_INTERFACE_URI);
856 cout << "Loading LV2 state from " << state_path << endl;
857 RDFF file = rdff_open(state_path.c_str(), false);
858 LV2State state(*this, _uri_map);
860 state_iface->restore(_impl->instance->lv2_handle,
861 &LV2Plugin::lv2_state_retrieve_callback,
863 LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE,
867 warning << string_compose(
868 _("Plugin \"%1\% failed to return LV2 state interface"),
873 latency_compute_run();
875 return Plugin::set_state(node, version);
879 LV2Plugin::get_parameter_descriptor(uint32_t which, ParameterDescriptor& desc) const
881 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, which);
883 LilvNode *def, *min, *max;
884 lilv_port_get_range(_impl->plugin, port, &def, &min, &max);
886 desc.integer_step = lilv_port_has_property(_impl->plugin, port, _world.integer);
887 desc.toggled = lilv_port_has_property(_impl->plugin, port, _world.toggled);
888 desc.logarithmic = lilv_port_has_property(_impl->plugin, port, _world.logarithmic);
889 desc.sr_dependent = lilv_port_has_property(_impl->plugin, port, _world.srate);
890 desc.label = lilv_node_as_string(lilv_port_get_name(_impl->plugin, port));
891 desc.lower = min ? lilv_node_as_float(min) : 0.0f;
892 desc.upper = max ? lilv_node_as_float(max) : 1.0f;
893 if (desc.sr_dependent) {
894 desc.lower *= _session.frame_rate ();
895 desc.upper *= _session.frame_rate ();
898 desc.min_unbound = false; // TODO: LV2 extension required
899 desc.max_unbound = false; // TODO: LV2 extension required
901 if (desc.integer_step) {
903 desc.smallstep = 0.1;
904 desc.largestep = 10.0;
906 const float delta = desc.upper - desc.lower;
907 desc.step = delta / 1000.0f;
908 desc.smallstep = delta / 10000.0f;
909 desc.largestep = delta / 10.0f;
920 LV2Plugin::describe_parameter(Evoral::Parameter which)
922 if (( which.type() == PluginAutomation) && ( which.id() < parameter_count()) ) {
923 LilvNode* name = lilv_port_get_name(_impl->plugin,
924 lilv_plugin_get_port_by_index(_impl->plugin, which.id()));
925 string ret(lilv_node_as_string(name));
926 lilv_node_free(name);
934 LV2Plugin::signal_latency() const
936 if (_latency_control_port) {
937 return (framecnt_t)floor(*_latency_control_port);
943 set<Evoral::Parameter>
944 LV2Plugin::automatable() const
946 set<Evoral::Parameter> ret;
948 for (uint32_t i = 0; i < parameter_count(); ++i) {
949 if (parameter_is_input(i) && parameter_is_control(i)) {
950 ret.insert(ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
958 LV2Plugin::activate()
960 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 activate\n", name()));
962 if (!_was_activated) {
963 lilv_instance_activate(_impl->instance);
964 _was_activated = true;
969 LV2Plugin::deactivate()
971 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 deactivate\n", name()));
973 if (_was_activated) {
974 lilv_instance_deactivate(_impl->instance);
975 _was_activated = false;
982 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 cleanup\n", name()));
986 lilv_instance_free(_impl->instance);
987 _impl->instance = NULL;
991 LV2Plugin::connect_and_run(BufferSet& bufs,
992 ChanMapping in_map, ChanMapping out_map,
993 pframes_t nframes, framecnt_t offset)
995 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 run %2 offset %3\n", name(), nframes, offset));
996 Plugin::connect_and_run(bufs, in_map, out_map, nframes, offset);
998 cycles_t then = get_cycles();
1000 ChanCount bufs_count;
1001 bufs_count.set(DataType::AUDIO, 1);
1002 bufs_count.set(DataType::MIDI, 1);
1003 BufferSet& silent_bufs = _session.get_silent_buffers(bufs_count);
1004 BufferSet& scratch_bufs = _session.get_silent_buffers(bufs_count);
1006 uint32_t audio_in_index = 0;
1007 uint32_t audio_out_index = 0;
1008 uint32_t midi_in_index = 0;
1009 uint32_t midi_out_index = 0;
1011 for (uint32_t port_index = 0; port_index < parameter_count(); ++port_index) {
1012 if (parameter_is_audio(port_index)) {
1013 if (parameter_is_input(port_index)) {
1014 const uint32_t buf_index = in_map.get(DataType::AUDIO, audio_in_index++, &valid);
1015 lilv_instance_connect_port(_impl->instance, port_index,
1016 valid ? bufs.get_audio(buf_index).data(offset)
1017 : silent_bufs.get_audio(0).data(offset));
1018 } else if (parameter_is_output(port_index)) {
1019 const uint32_t buf_index = out_map.get(DataType::AUDIO, audio_out_index++, &valid);
1020 //cerr << port_index << " : " << " AUDIO OUT " << buf_index << endl;
1021 lilv_instance_connect_port(_impl->instance, port_index,
1022 valid ? bufs.get_audio(buf_index).data(offset)
1023 : scratch_bufs.get_audio(0).data(offset));
1025 } else if (parameter_is_midi(port_index)) {
1026 /* FIXME: The checks here for bufs.count().n_midi() > buf_index shouldn't
1027 be necessary, but the mapping is illegal in some cases. Ideally
1028 that should be fixed, but this is easier...
1030 if (parameter_is_input(port_index)) {
1031 const uint32_t buf_index = in_map.get(DataType::MIDI, midi_in_index++, &valid);
1032 if (valid && bufs.count().n_midi() > buf_index) {
1033 lilv_instance_connect_port(_impl->instance, port_index,
1034 bufs.get_lv2_midi(true, buf_index).data());
1036 lilv_instance_connect_port(_impl->instance, port_index,
1037 silent_bufs.get_lv2_midi(true, 0).data());
1039 } else if (parameter_is_output(port_index)) {
1040 const uint32_t buf_index = out_map.get(DataType::MIDI, midi_out_index++, &valid);
1041 if (valid && bufs.count().n_midi() > buf_index) {
1042 lilv_instance_connect_port(_impl->instance, port_index,
1043 bufs.get_lv2_midi(false, buf_index).data());
1045 lilv_instance_connect_port(_impl->instance, port_index,
1046 scratch_bufs.get_lv2_midi(true, 0).data());
1049 } else if (!parameter_is_control(port_index)) {
1050 // Optional port (it'd better be if we've made it this far...)
1051 lilv_instance_connect_port(_impl->instance, port_index, NULL);
1058 for (uint32_t port_index = 0; port_index < parameter_count(); ++port_index) {
1059 if (parameter_is_midi(port_index) && parameter_is_output(port_index)) {
1060 const uint32_t buf_index = out_map.get(DataType::MIDI, midi_out_index++, &valid);
1062 bufs.flush_lv2_midi(true, buf_index);
1067 cycles_t now = get_cycles();
1068 set_cycles((uint32_t)(now - then));
1074 LV2Plugin::parameter_is_control(uint32_t param) const
1076 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
1077 return lilv_port_is_a(_impl->plugin, port, _world.control_class);
1081 LV2Plugin::parameter_is_audio(uint32_t param) const
1083 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
1084 return lilv_port_is_a(_impl->plugin, port, _world.audio_class);
1088 LV2Plugin::parameter_is_midi(uint32_t param) const
1090 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
1091 return lilv_port_is_a(_impl->plugin, port, _world.event_class);
1092 // && lilv_port_supports_event(_impl->plugin, port, _world.midi_class);
1096 LV2Plugin::parameter_is_output(uint32_t param) const
1098 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
1099 return lilv_port_is_a(_impl->plugin, port, _world.output_class);
1103 LV2Plugin::parameter_is_input(uint32_t param) const
1105 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
1106 return lilv_port_is_a(_impl->plugin, port, _world.input_class);
1110 LV2Plugin::print_parameter(uint32_t param, char* buf, uint32_t len) const
1113 if (param < parameter_count()) {
1114 snprintf(buf, len, "%.3f", get_parameter(param));
1121 boost::shared_ptr<Plugin::ScalePoints>
1122 LV2Plugin::get_scale_points(uint32_t port_index) const
1124 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, port_index);
1125 LilvScalePoints* points = lilv_port_get_scale_points(_impl->plugin, port);
1127 boost::shared_ptr<Plugin::ScalePoints> ret;
1132 ret = boost::shared_ptr<Plugin::ScalePoints>(new ScalePoints());
1134 LILV_FOREACH(scale_points, i, points) {
1135 const LilvScalePoint* p = lilv_scale_points_get(points, i);
1136 const LilvNode* label = lilv_scale_point_get_label(p);
1137 const LilvNode* value = lilv_scale_point_get_value(p);
1138 if (label && (lilv_node_is_float(value) || lilv_node_is_int(value))) {
1139 ret->insert(make_pair(lilv_node_as_string(label),
1140 lilv_node_as_float(value)));
1144 lilv_scale_points_free(points);
1149 LV2Plugin::run(pframes_t nframes)
1151 for (uint32_t i = 0; i < parameter_count(); ++i) {
1152 if (parameter_is_control(i) && parameter_is_input(i)) {
1153 _control_data[i] = _shadow_data[i];
1157 lilv_instance_run(_impl->instance, nframes);
1161 LV2Plugin::latency_compute_run()
1163 if (!_latency_control_port) {
1167 // Run the plugin so that it can set its latency parameter
1171 uint32_t port_index = 0;
1172 uint32_t in_index = 0;
1173 uint32_t out_index = 0;
1175 const framecnt_t bufsize = 1024;
1176 float buffer[bufsize];
1178 memset(buffer, 0, sizeof(float) * bufsize);
1180 // FIXME: Ensure plugins can handle in-place processing
1184 while (port_index < parameter_count()) {
1185 if (parameter_is_audio(port_index)) {
1186 if (parameter_is_input(port_index)) {
1187 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1189 } else if (parameter_is_output(port_index)) {
1190 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1201 LV2World::LV2World()
1202 : world(lilv_world_new())
1204 lilv_world_load_all(world);
1205 input_class = lilv_new_uri(world, LILV_URI_INPUT_PORT);
1206 output_class = lilv_new_uri(world, LILV_URI_OUTPUT_PORT);
1207 control_class = lilv_new_uri(world, LILV_URI_CONTROL_PORT);
1208 audio_class = lilv_new_uri(world, LILV_URI_AUDIO_PORT);
1209 event_class = lilv_new_uri(world, LILV_URI_EVENT_PORT);
1210 midi_class = lilv_new_uri(world, LILV_URI_MIDI_EVENT);
1211 in_place_broken = lilv_new_uri(world, LILV_NS_LV2 "inPlaceBroken");
1212 integer = lilv_new_uri(world, LILV_NS_LV2 "integer");
1213 toggled = lilv_new_uri(world, LILV_NS_LV2 "toggled");
1214 srate = lilv_new_uri(world, LILV_NS_LV2 "sampleRate");
1215 gtk_gui = lilv_new_uri(world, NS_UI "GtkUI");
1216 external_gui = lilv_new_uri(world, NS_UI "external");
1217 logarithmic = lilv_new_uri(world, "http://lv2plug.in/ns/dev/extportinfo#logarithmic");
1220 LV2World::~LV2World()
1222 lilv_node_free(input_class);
1223 lilv_node_free(output_class);
1224 lilv_node_free(control_class);
1225 lilv_node_free(audio_class);
1226 lilv_node_free(event_class);
1227 lilv_node_free(midi_class);
1228 lilv_node_free(in_place_broken);
1231 LV2PluginInfo::LV2PluginInfo (void* c_plugin)
1232 : _c_plugin(c_plugin)
1237 LV2PluginInfo::~LV2PluginInfo()
1241 LV2PluginInfo::load(Session& session)
1246 plugin.reset(new LV2Plugin(session.engine(), session,
1247 (LilvPlugin*)_c_plugin,
1248 session.frame_rate()));
1250 plugin->set_info(PluginInfoPtr(new LV2PluginInfo(*this)));
1252 } catch (failed_constructor& err) {
1253 return PluginPtr((Plugin*)0);
1260 LV2PluginInfo::discover()
1262 PluginInfoList* plugs = new PluginInfoList;
1263 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
1265 cerr << "LV2: Discovering " << lilv_plugins_size(plugins) << " plugins" << endl;
1267 LILV_FOREACH(plugins, i, plugins) {
1268 const LilvPlugin* p = lilv_plugins_get(plugins, i);
1269 LV2PluginInfoPtr info(new LV2PluginInfo((void*)p));
1271 LilvNode* name = lilv_plugin_get_name(p);
1273 cerr << "LV2: invalid plugin\n";
1279 info->name = string(lilv_node_as_string(name));
1280 lilv_node_free(name);
1282 const LilvPluginClass* pclass = lilv_plugin_get_class(p);
1283 const LilvNode* label = lilv_plugin_class_get_label(pclass);
1284 info->category = lilv_node_as_string(label);
1286 LilvNode* author_name = lilv_plugin_get_author_name(p);
1287 info->creator = author_name ? string(lilv_node_as_string(author_name)) : "Unknown";
1288 lilv_node_free(author_name);
1290 info->path = "/NOPATH"; // Meaningless for LV2
1292 info->n_inputs.set_audio(
1293 lilv_plugin_get_num_ports_of_class(
1294 p, _world.input_class, _world.audio_class, NULL));
1295 info->n_inputs.set_midi(
1296 lilv_plugin_get_num_ports_of_class(
1297 p, _world.input_class, _world.event_class, NULL));
1299 info->n_outputs.set_audio(
1300 lilv_plugin_get_num_ports_of_class(
1301 p, _world.output_class, _world.audio_class, NULL));
1302 info->n_outputs.set_midi(
1303 lilv_plugin_get_num_ports_of_class(
1304 p, _world.output_class, _world.event_class, NULL));
1306 info->unique_id = lilv_node_as_uri(lilv_plugin_get_uri(p));
1307 info->index = 0; // Meaningless for LV2
1309 plugs->push_back(info);
1312 cerr << "Done LV2 discovery" << endl;