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.
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"
52 #include <lilv/lilv.h>
54 #include "lv2ext/lv2_files.h"
55 #include "lv2ext/lv2_persist.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_PSET "http://lv2plug.in/ns/dev/presets#"
64 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
67 using namespace ARDOUR;
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 {
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;
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) {}
102 const LilvNode* ui_type;
105 LilvInstance* instance;
108 LV2Plugin::LV2Plugin (AudioEngine& engine,
112 : Plugin(engine, session)
117 init(c_plugin, rate);
120 LV2Plugin::LV2Plugin (const LV2Plugin& other)
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];
135 LV2Plugin::init(void* c_plugin, framecnt_t rate)
137 DEBUG_TRACE(DEBUG::LV2, "init\n");
139 _impl->plugin = (LilvPlugin*)c_plugin;
141 _impl->ui_type = NULL;
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();
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();
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)
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)) {
226 lilv_port_get_range(plugin, port, &def, NULL, NULL);
227 _defaults[i] = def ? lilv_node_as_float(def) : 0.0f;
230 lilv_instance_connect_port(_impl->instance, i, &_control_data[i]);
232 if (latent && ( i == latency_port) ) {
233 _latency_control_port = &_control_data[i];
234 *_latency_control_port = 0;
237 if (parameter_is_input(i)) {
238 _shadow_data[i] = default_value(i);
245 LilvUIs* uis = lilv_plugin_get_uis(plugin);
246 if (lilv_uis_size(uis) > 0) {
248 // Look for embeddable UI
249 LILV_FOREACH(uis, u, uis) {
250 const LilvUI* this_ui = lilv_uis_get(uis, u);
251 const LilvNode* this_ui_type = NULL;
252 if (lilv_ui_is_supported(this_ui,
256 // TODO: Multiple UI support
258 _impl->ui_type = this_ui_type;
263 // Look for Gtk native UI
264 LILV_FOREACH(uis, i, uis) {
265 const LilvUI* ui = lilv_uis_get(uis, i);
266 if (lilv_ui_is_a(ui, _world.gtk_gui)) {
268 _impl->ui_type = _world.gtk_gui;
274 // If Gtk UI is not available, try to find external UI
276 LILV_FOREACH(uis, i, uis) {
277 const LilvUI* ui = lilv_uis_get(uis, i);
278 if (lilv_ui_is_a(ui, _world.external_gui)) {
280 _impl->ui_type = _world.external_gui;
287 latency_compute_run();
290 LV2Plugin::~LV2Plugin ()
292 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 destroy\n", name()));
297 lilv_instance_free(_impl->instance);
298 lilv_node_free(_impl->name);
299 lilv_node_free(_impl->author);
301 delete [] _control_data;
302 delete [] _shadow_data;
306 LV2Plugin::is_external_ui() const
311 return lilv_ui_is_a(_impl->ui, _world.external_gui);
315 LV2Plugin::unique_id() const
317 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
321 LV2Plugin::uri() const
323 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
327 LV2Plugin::label() const
329 return lilv_node_as_string(_impl->name);
333 LV2Plugin::name() const
335 return lilv_node_as_string(_impl->name);
339 LV2Plugin::maker() const
341 return _impl->author ? lilv_node_as_string (_impl->author) : "Unknown";
345 LV2Plugin::num_ports() const
347 return lilv_plugin_get_num_ports(_impl->plugin);
351 LV2Plugin::parameter_count() const
353 return lilv_plugin_get_num_ports(_impl->plugin);
357 LV2Plugin::default_value(uint32_t port)
359 return _defaults[port];
363 LV2Plugin::port_symbol(uint32_t index) const
365 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, index);
367 error << name() << ": Invalid port index " << index << endmsg;
370 const LilvNode* sym = lilv_port_get_symbol(_impl->plugin, port);
371 return lilv_node_as_string(sym);
375 LV2Plugin::set_parameter(uint32_t which, float val)
377 DEBUG_TRACE(DEBUG::LV2, string_compose(
378 "%1 set parameter %2 to %3\n", name(), which, val));
380 if (which < lilv_plugin_get_num_ports(_impl->plugin)) {
381 _shadow_data[which] = val;
383 warning << string_compose(
384 _("Illegal parameter number used with plugin \"%1\". "
385 "This is a bug in either %2 or the LV2 plugin <%3>"),
386 name(), PROGRAM_NAME, unique_id()) << endmsg;
389 Plugin::set_parameter(which, val);
393 LV2Plugin::get_parameter(uint32_t which) const
395 if (parameter_is_input(which)) {
396 return (float)_shadow_data[which];
398 return (float)_control_data[which];
404 LV2Plugin::nth_parameter(uint32_t n, bool& ok) const
407 for (uint32_t c = 0, x = 0; x < lilv_plugin_get_num_ports(_impl->plugin); ++x) {
408 if (parameter_is_control(x)) {
420 LV2Plugin::extension_data (const char* uri) const
422 return lilv_instance_get_extension_data(_impl->instance, uri);
426 LV2Plugin::c_plugin ()
428 return _impl->plugin;
434 return (void*)_impl->ui;
438 LV2Plugin::c_ui_type ()
440 return (void*)_impl->ui_type;
444 LV2Plugin::lv2_persist_store_callback(void* host_data,
451 DEBUG_TRACE(DEBUG::LV2, string_compose(
452 "persist store %1 (size: %2, type: %3)\n",
453 _uri_map.id_to_uri(NULL, key),
455 _uri_map.id_to_uri(NULL, type)));
457 LV2PersistState* state = (LV2PersistState*)host_data;
458 state->add_uri(key, _uri_map.id_to_uri(NULL, key));
459 state->add_uri(type, _uri_map.id_to_uri(NULL, type));
460 return state->add_value(key, value, size, type, flags);
464 LV2Plugin::lv2_persist_retrieve_callback(void* host_data,
470 LV2PersistState* state = (LV2PersistState*)host_data;
471 LV2PersistState::Values::const_iterator i = state->values.find(key);
472 if (i == state->values.end()) {
473 warning << "LV2 plugin attempted to retrieve nonexistent key: "
474 << _uri_map.id_to_uri(NULL, key) << endmsg;
477 *size = i->second.size;
478 *type = i->second.type;
479 *flags = LV2_PERSIST_IS_POD | LV2_PERSIST_IS_PORTABLE; // FIXME
480 DEBUG_TRACE(DEBUG::LV2, string_compose(
481 "persist retrieve %1 = %2 (size: %3, type: %4)\n",
482 _uri_map.id_to_uri(NULL, key),
483 i->second.value, *size, *type));
484 return i->second.value;
488 LV2Plugin::lv2_files_abstract_path(LV2_Files_Host_Data host_data,
489 const char* absolute_path)
491 LV2Plugin* me = (LV2Plugin*)host_data;
492 if (me->_insert_id == PBD::ID("0")) {
493 return g_strdup(absolute_path);
496 const std::string state_dir = Glib::build_filename(me->_session.plugins_dir(),
497 me->_insert_id.to_s());
500 if (strncmp(absolute_path, state_dir.c_str(), state_dir.length())) {
501 ret = g_strdup(absolute_path);
503 const std::string path(absolute_path + state_dir.length() + 1);
504 ret = g_strndup(path.c_str(), path.length());
507 DEBUG_TRACE(DEBUG::LV2, string_compose("abstract path %1 => %2\n",
508 absolute_path, ret));
514 LV2Plugin::lv2_files_absolute_path(LV2_Files_Host_Data host_data,
515 const char* abstract_path)
517 LV2Plugin* me = (LV2Plugin*)host_data;
518 if (me->_insert_id == PBD::ID("0")) {
519 return g_strdup(abstract_path);
523 if (g_path_is_absolute(abstract_path)) {
524 ret = g_strdup(abstract_path);
526 const std::string apath(abstract_path);
527 const std::string state_dir = Glib::build_filename(me->_session.plugins_dir(),
528 me->_insert_id.to_s());
529 const std::string path = Glib::build_filename(state_dir,
531 ret = g_strndup(path.c_str(), path.length());
534 DEBUG_TRACE(DEBUG::LV2, string_compose("absolute path %1 => %2\n",
535 abstract_path, ret));
541 LV2Plugin::lv2_files_new_file_path(LV2_Files_Host_Data host_data,
542 const char* relative_path)
544 LV2Plugin* me = (LV2Plugin*)host_data;
545 if (me->_insert_id == PBD::ID("0")) {
546 return g_strdup(relative_path);
549 const std::string state_dir = Glib::build_filename(me->_session.plugins_dir(),
550 me->_insert_id.to_s());
551 const std::string path = Glib::build_filename(state_dir,
554 char* dirname = g_path_get_dirname(path.c_str());
555 g_mkdir_with_parents(dirname, 0744);
558 DEBUG_TRACE(DEBUG::LV2, string_compose("new file path %1 => %2\n",
559 relative_path, path));
561 return g_strndup(path.c_str(), path.length());
565 LV2Plugin::add_state(XMLNode* root) const
567 assert(_insert_id != PBD::ID("0"));
571 LocaleGuard lg(X_("POSIX"));
573 for (uint32_t i = 0; i < parameter_count(); ++i) {
574 if (parameter_is_input(i) && parameter_is_control(i)) {
575 child = new XMLNode("Port");
576 child->add_property("symbol", port_symbol(i));
577 snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
578 child->add_property("value", string(buf));
579 root->add_child_nocopy(*child);
583 if (_supports_persist) {
584 // Create state directory for this plugin instance
585 cerr << "Create statefile name from ID " << _insert_id << endl;
586 const std::string state_filename = _insert_id.to_s() + ".rdff";
587 const std::string state_path = Glib::build_filename(
588 _session.plugins_dir(), state_filename);
590 cout << "Saving LV2 plugin state to " << state_path << endl;
592 // Get LV2 Persist extension data from plugin instance
593 LV2_Persist* persist = (LV2_Persist*)extension_data(
594 "http://lv2plug.in/ns/ext/persist");
596 warning << string_compose(
597 _("Plugin \"%1\% failed to return LV2 persist data"),
602 // Save plugin state to state object
603 LV2PersistState state(_uri_map);
604 persist->save(_impl->instance->lv2_handle,
605 &LV2Plugin::lv2_persist_store_callback,
608 // Write state object to RDFF file
609 RDFF file = rdff_open(state_path.c_str(), true);
613 root->add_property("state-file", state_filename);
617 static inline const LilvNode*
618 get_value(LilvWorld* world, const LilvNode* subject, const LilvNode* predicate)
620 LilvNodes* vs = lilv_world_find_nodes(world, subject, predicate, NULL);
621 return vs ? lilv_nodes_get_first(vs) : NULL;
625 LV2Plugin::find_presets()
627 LilvNode* dc_title = lilv_new_uri(_world.world, NS_DC "title");
628 LilvNode* pset_hasPreset = lilv_new_uri(_world.world, NS_PSET "hasPreset");
630 LilvNodes* presets = lilv_plugin_get_value(_impl->plugin, pset_hasPreset);
631 LILV_FOREACH(nodes, i, presets) {
632 const LilvNode* preset = lilv_nodes_get(presets, i);
633 const LilvNode* name = get_value(_world.world, preset, dc_title);
635 _presets.insert(std::make_pair(lilv_node_as_string(preset),
637 lilv_node_as_string(preset),
638 lilv_node_as_string(name))));
640 warning << string_compose(
641 _("Plugin \"%1\% preset \"%2%\" is missing a dc:title\n"),
642 unique_id(), lilv_node_as_string(preset));
645 lilv_nodes_free(presets);
647 lilv_node_free(pset_hasPreset);
648 lilv_node_free(dc_title);
652 LV2Plugin::load_preset(PresetRecord r)
654 Plugin::load_preset(r);
656 LilvNode* lv2_port = lilv_new_uri(_world.world, NS_LV2 "port");
657 LilvNode* lv2_symbol = lilv_new_uri(_world.world, NS_LV2 "symbol");
658 LilvNode* pset_value = lilv_new_uri(_world.world, NS_PSET "value");
659 LilvNode* preset = lilv_new_uri(_world.world, r.uri.c_str());
661 LilvNodes* ports = lilv_world_find_nodes(_world.world, preset, lv2_port, NULL);
662 LILV_FOREACH(nodes, i, ports) {
663 const LilvNode* port = lilv_nodes_get(ports, i);
664 const LilvNode* symbol = get_value(_world.world, port, lv2_symbol);
665 const LilvNode* value = get_value(_world.world, port, pset_value);
666 if (value && lilv_node_is_float(value)) {
667 set_parameter(_port_indices[lilv_node_as_string(symbol)],
668 lilv_node_as_float(value));
671 lilv_nodes_free(ports);
673 lilv_node_free(preset);
674 lilv_node_free(pset_value);
675 lilv_node_free(lv2_symbol);
676 lilv_node_free(lv2_port);
682 LV2Plugin::do_save_preset(string /*name*/)
688 LV2Plugin::do_remove_preset(string /*name*/)
692 LV2Plugin::has_editor() const
694 return _impl->ui != NULL;
698 LV2Plugin::set_insert_info(const PluginInsert* insert)
700 _insert_id = insert->id();
704 LV2Plugin::set_state(const XMLNode& node, int version)
707 const XMLProperty* prop;
708 XMLNodeConstIterator iter;
713 LocaleGuard lg(X_("POSIX"));
715 if (node.name() != state_node_name()) {
716 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
720 if (version < 3000) {
721 nodes = node.children("port");
723 nodes = node.children("Port");
726 for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
730 if ((prop = child->property("symbol")) != 0) {
731 sym = prop->value().c_str();
733 warning << _("LV2: port has no symbol, ignored") << endmsg;
737 map<string, uint32_t>::iterator i = _port_indices.find(sym);
739 if (i != _port_indices.end()) {
742 warning << _("LV2: port has unknown index, ignored") << endmsg;
746 if ((prop = child->property("value")) != 0) {
747 value = prop->value().c_str();
749 warning << _("LV2: port has no value, ignored") << endmsg;
753 set_parameter(port_id, atof(value));
756 if ((prop = node.property("state-file")) != 0) {
757 std::string state_path = Glib::build_filename(_session.plugins_dir(),
760 cout << "LV2 state path " << state_path << endl;
762 // Get LV2 Persist extension data from plugin instance
763 LV2_Persist* persist = (LV2_Persist*)extension_data(
764 "http://lv2plug.in/ns/ext/persist");
766 cout << "Loading LV2 state from " << state_path << endl;
767 RDFF file = rdff_open(state_path.c_str(), false);
768 LV2PersistState state(_uri_map);
770 persist->restore(_impl->instance->lv2_handle,
771 &LV2Plugin::lv2_persist_retrieve_callback,
775 warning << string_compose(
776 _("Plugin \"%1\% failed to return LV2 persist data"),
781 latency_compute_run();
783 return Plugin::set_state(node, version);
787 LV2Plugin::get_parameter_descriptor(uint32_t which, ParameterDescriptor& desc) const
789 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, which);
791 LilvNode *def, *min, *max;
792 lilv_port_get_range(_impl->plugin, port, &def, &min, &max);
794 desc.integer_step = lilv_port_has_property(_impl->plugin, port, _world.integer);
795 desc.toggled = lilv_port_has_property(_impl->plugin, port, _world.toggled);
796 desc.logarithmic = lilv_port_has_property(_impl->plugin, port, _world.logarithmic);
797 desc.sr_dependent = lilv_port_has_property(_impl->plugin, port, _world.srate);
798 desc.label = lilv_node_as_string(lilv_port_get_name(_impl->plugin, port));
799 desc.lower = min ? lilv_node_as_float(min) : 0.0f;
800 desc.upper = max ? lilv_node_as_float(max) : 1.0f;
801 desc.min_unbound = false; // TODO: LV2 extension required
802 desc.max_unbound = false; // TODO: LV2 extension required
804 if (desc.integer_step) {
806 desc.smallstep = 0.1;
807 desc.largestep = 10.0;
809 const float delta = desc.upper - desc.lower;
810 desc.step = delta / 1000.0f;
811 desc.smallstep = delta / 10000.0f;
812 desc.largestep = delta / 10.0f;
823 LV2Plugin::describe_parameter(Evoral::Parameter which)
825 if (( which.type() == PluginAutomation) && ( which.id() < parameter_count()) ) {
826 LilvNode* name = lilv_port_get_name(_impl->plugin,
827 lilv_plugin_get_port_by_index(_impl->plugin, which.id()));
828 string ret(lilv_node_as_string(name));
829 lilv_node_free(name);
837 LV2Plugin::signal_latency() const
839 if (_latency_control_port) {
840 return (framecnt_t)floor(*_latency_control_port);
846 set<Evoral::Parameter>
847 LV2Plugin::automatable() const
849 set<Evoral::Parameter> ret;
851 for (uint32_t i = 0; i < parameter_count(); ++i) {
852 if (parameter_is_input(i) && parameter_is_control(i)) {
853 ret.insert(ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
861 LV2Plugin::activate()
863 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 activate\n", name()));
865 if (!_was_activated) {
866 lilv_instance_activate(_impl->instance);
867 _was_activated = true;
872 LV2Plugin::deactivate()
874 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 deactivate\n", name()));
876 if (_was_activated) {
877 lilv_instance_deactivate(_impl->instance);
878 _was_activated = false;
885 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 cleanup\n", name()));
889 lilv_instance_free(_impl->instance);
890 _impl->instance = NULL;
894 LV2Plugin::connect_and_run(BufferSet& bufs,
895 ChanMapping in_map, ChanMapping out_map,
896 pframes_t nframes, framecnt_t offset)
898 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 run %2 offset %3\n", name(), nframes, offset));
899 Plugin::connect_and_run(bufs, in_map, out_map, nframes, offset);
901 cycles_t then = get_cycles();
903 uint32_t audio_in_index = 0;
904 uint32_t audio_out_index = 0;
905 uint32_t midi_in_index = 0;
906 uint32_t midi_out_index = 0;
907 for (uint32_t port_index = 0; port_index < parameter_count(); ++port_index) {
908 if (parameter_is_audio(port_index)) {
909 if (parameter_is_input(port_index)) {
910 const uint32_t buf_index = in_map.get(DataType::AUDIO, audio_in_index++);
911 lilv_instance_connect_port(_impl->instance, port_index,
912 bufs.get_audio(buf_index).data(offset));
913 } else if (parameter_is_output(port_index)) {
914 const uint32_t buf_index = out_map.get(DataType::AUDIO, audio_out_index++);
915 //cerr << port_index << " : " << " AUDIO OUT " << buf_index << endl;
916 lilv_instance_connect_port(_impl->instance, port_index,
917 bufs.get_audio(buf_index).data(offset));
919 } else if (parameter_is_midi(port_index)) {
920 if (parameter_is_input(port_index)) {
921 const uint32_t buf_index = in_map.get(DataType::MIDI, midi_in_index++);
922 lilv_instance_connect_port(_impl->instance, port_index,
923 bufs.get_lv2_midi(true, buf_index).data());
924 } else if (parameter_is_output(port_index)) {
925 const uint32_t buf_index = out_map.get(DataType::MIDI, midi_out_index++);
926 lilv_instance_connect_port(_impl->instance, port_index,
927 bufs.get_lv2_midi(false, buf_index).data());
929 } else if (!parameter_is_control(port_index)) {
930 // Optional port (it'd better be if we've made it this far...)
931 lilv_instance_connect_port(_impl->instance, port_index, NULL);
938 for (uint32_t port_index = 0; port_index < parameter_count(); ++port_index) {
939 if (parameter_is_midi(port_index) && parameter_is_output(port_index)) {
940 const uint32_t buf_index = out_map.get(DataType::MIDI, midi_out_index++);
941 bufs.flush_lv2_midi(true, buf_index);
945 cycles_t now = get_cycles();
946 set_cycles((uint32_t)(now - then));
952 LV2Plugin::parameter_is_control(uint32_t param) const
954 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
955 return lilv_port_is_a(_impl->plugin, port, _world.control_class);
959 LV2Plugin::parameter_is_audio(uint32_t param) const
961 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
962 return lilv_port_is_a(_impl->plugin, port, _world.audio_class);
966 LV2Plugin::parameter_is_midi(uint32_t param) const
968 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
969 return lilv_port_is_a(_impl->plugin, port, _world.event_class);
970 // && lilv_port_supports_event(_impl->plugin, port, _world.midi_class);
974 LV2Plugin::parameter_is_output(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.output_class);
981 LV2Plugin::parameter_is_input(uint32_t param) const
983 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
984 return lilv_port_is_a(_impl->plugin, port, _world.input_class);
988 LV2Plugin::print_parameter(uint32_t param, char* buf, uint32_t len) const
991 if (param < parameter_count()) {
992 snprintf(buf, len, "%.3f", get_parameter(param));
999 boost::shared_ptr<Plugin::ScalePoints>
1000 LV2Plugin::get_scale_points(uint32_t port_index) const
1002 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, port_index);
1003 LilvScalePoints* points = lilv_port_get_scale_points(_impl->plugin, port);
1005 boost::shared_ptr<Plugin::ScalePoints> ret;
1010 ret = boost::shared_ptr<Plugin::ScalePoints>(new ScalePoints());
1012 LILV_FOREACH(scale_points, i, points) {
1013 const LilvScalePoint* p = lilv_scale_points_get(points, i);
1014 const LilvNode* label = lilv_scale_point_get_label(p);
1015 const LilvNode* value = lilv_scale_point_get_value(p);
1016 if (label && (lilv_node_is_float(value) || lilv_node_is_int(value))) {
1017 ret->insert(make_pair(lilv_node_as_string(label),
1018 lilv_node_as_float(value)));
1022 lilv_scale_points_free(points);
1027 LV2Plugin::run(pframes_t nframes)
1029 for (uint32_t i = 0; i < parameter_count(); ++i) {
1030 if (parameter_is_control(i) && parameter_is_input(i)) {
1031 _control_data[i] = _shadow_data[i];
1035 lilv_instance_run(_impl->instance, nframes);
1039 LV2Plugin::latency_compute_run()
1041 if (!_latency_control_port) {
1045 // Run the plugin so that it can set its latency parameter
1049 uint32_t port_index = 0;
1050 uint32_t in_index = 0;
1051 uint32_t out_index = 0;
1053 const framecnt_t bufsize = 1024;
1054 float buffer[bufsize];
1056 memset(buffer, 0, sizeof(float) * bufsize);
1058 // FIXME: Ensure plugins can handle in-place processing
1062 while (port_index < parameter_count()) {
1063 if (parameter_is_audio(port_index)) {
1064 if (parameter_is_input(port_index)) {
1065 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1067 } else if (parameter_is_output(port_index)) {
1068 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1079 LV2World::LV2World()
1080 : world(lilv_world_new())
1082 lilv_world_load_all(world);
1083 input_class = lilv_new_uri(world, LILV_URI_INPUT_PORT);
1084 output_class = lilv_new_uri(world, LILV_URI_OUTPUT_PORT);
1085 control_class = lilv_new_uri(world, LILV_URI_CONTROL_PORT);
1086 audio_class = lilv_new_uri(world, LILV_URI_AUDIO_PORT);
1087 event_class = lilv_new_uri(world, LILV_URI_EVENT_PORT);
1088 midi_class = lilv_new_uri(world, LILV_URI_MIDI_EVENT);
1089 in_place_broken = lilv_new_uri(world, LILV_NS_LV2 "inPlaceBroken");
1090 integer = lilv_new_uri(world, LILV_NS_LV2 "integer");
1091 toggled = lilv_new_uri(world, LILV_NS_LV2 "toggled");
1092 srate = lilv_new_uri(world, LILV_NS_LV2 "sampleRate");
1093 gtk_gui = lilv_new_uri(world, NS_UI "GtkUI");
1094 external_gui = lilv_new_uri(world, NS_UI "external");
1095 logarithmic = lilv_new_uri(world, "http://lv2plug.in/ns/dev/extportinfo#logarithmic");
1098 LV2World::~LV2World()
1100 lilv_node_free(input_class);
1101 lilv_node_free(output_class);
1102 lilv_node_free(control_class);
1103 lilv_node_free(audio_class);
1104 lilv_node_free(event_class);
1105 lilv_node_free(midi_class);
1106 lilv_node_free(in_place_broken);
1109 LV2PluginInfo::LV2PluginInfo (void* c_plugin)
1110 : _c_plugin(c_plugin)
1115 LV2PluginInfo::~LV2PluginInfo()
1119 LV2PluginInfo::load(Session& session)
1124 plugin.reset(new LV2Plugin(session.engine(), session,
1125 (LilvPlugin*)_c_plugin,
1126 session.frame_rate()));
1128 plugin->set_info(PluginInfoPtr(new LV2PluginInfo(*this)));
1130 } catch (failed_constructor& err) {
1131 return PluginPtr((Plugin*)0);
1138 LV2PluginInfo::discover()
1140 PluginInfoList* plugs = new PluginInfoList;
1141 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
1143 cerr << "LV2: Discovering " << lilv_plugins_size(plugins) << " plugins" << endl;
1145 LILV_FOREACH(plugins, i, plugins) {
1146 const LilvPlugin* p = lilv_plugins_get(plugins, i);
1147 LV2PluginInfoPtr info(new LV2PluginInfo((void*)p));
1149 LilvNode* name = lilv_plugin_get_name(p);
1151 cerr << "LV2: invalid plugin\n";
1157 info->name = string(lilv_node_as_string(name));
1158 lilv_node_free(name);
1160 const LilvPluginClass* pclass = lilv_plugin_get_class(p);
1161 const LilvNode* label = lilv_plugin_class_get_label(pclass);
1162 info->category = lilv_node_as_string(label);
1164 LilvNode* author_name = lilv_plugin_get_author_name(p);
1165 info->creator = author_name ? string(lilv_node_as_string(author_name)) : "Unknown";
1166 lilv_node_free(author_name);
1168 info->path = "/NOPATH"; // Meaningless for LV2
1170 info->n_inputs.set_audio(
1171 lilv_plugin_get_num_ports_of_class(
1172 p, _world.input_class, _world.audio_class, NULL));
1173 info->n_inputs.set_midi(
1174 lilv_plugin_get_num_ports_of_class(
1175 p, _world.input_class, _world.event_class, NULL));
1177 info->n_outputs.set_audio(
1178 lilv_plugin_get_num_ports_of_class(
1179 p, _world.output_class, _world.audio_class, NULL));
1180 info->n_outputs.set_midi(
1181 lilv_plugin_get_num_ports_of_class(
1182 p, _world.output_class, _world.event_class, NULL));
1184 info->unique_id = lilv_node_as_uri(lilv_plugin_get_uri(p));
1185 info->index = 0; // Meaningless for LV2
1187 plugs->push_back(info);
1190 cerr << "Done LV2 discovery" << endl;