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 "lv2/lv2plug.in/ns/ext/files/files.h"
55 #include "lv2/lv2plug.in/ns/ext/persist/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;
228 if (lilv_port_has_property (plugin, port, _world.srate)) {
229 _defaults[i] *= _session.frame_rate ();
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);
248 LilvUIs* uis = lilv_plugin_get_uis(plugin);
249 if (lilv_uis_size(uis) > 0) {
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,
259 // TODO: Multiple UI support
261 _impl->ui_type = this_ui_type;
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)) {
271 _impl->ui_type = _world.gtk_gui;
277 // If Gtk UI is not available, try to find external 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)) {
283 _impl->ui_type = _world.external_gui;
290 latency_compute_run();
293 LV2Plugin::~LV2Plugin ()
295 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 destroy\n", name()));
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;
309 LV2Plugin::is_external_ui() const
314 return lilv_ui_is_a(_impl->ui, _world.external_gui);
318 LV2Plugin::unique_id() const
320 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
324 LV2Plugin::uri() const
326 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
330 LV2Plugin::label() const
332 return lilv_node_as_string(_impl->name);
336 LV2Plugin::name() const
338 return lilv_node_as_string(_impl->name);
342 LV2Plugin::maker() const
344 return _impl->author ? lilv_node_as_string (_impl->author) : "Unknown";
348 LV2Plugin::num_ports() const
350 return lilv_plugin_get_num_ports(_impl->plugin);
354 LV2Plugin::parameter_count() const
356 return lilv_plugin_get_num_ports(_impl->plugin);
360 LV2Plugin::default_value(uint32_t port)
362 return _defaults[port];
366 LV2Plugin::port_symbol(uint32_t index) const
368 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, index);
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);
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;
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);
396 LV2Plugin::get_parameter(uint32_t which) const
398 if (parameter_is_input(which)) {
399 return (float)_shadow_data[which];
401 return (float)_control_data[which];
407 LV2Plugin::nth_parameter(uint32_t n, bool& ok) const
410 for (uint32_t c = 0, x = 0; x < lilv_plugin_get_num_ports(_impl->plugin); ++x) {
411 if (parameter_is_control(x)) {
423 LV2Plugin::extension_data (const char* uri) const
425 return lilv_instance_get_extension_data(_impl->instance, uri);
429 LV2Plugin::c_plugin ()
431 return _impl->plugin;
437 return (void*)_impl->ui;
441 LV2Plugin::c_ui_type ()
443 return (void*)_impl->ui_type;
447 LV2Plugin::lv2_persist_store_callback(void* host_data,
454 DEBUG_TRACE(DEBUG::LV2, string_compose(
455 "persist store %1 (size: %2, type: %3)\n",
456 _uri_map.id_to_uri(NULL, key),
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);
467 LV2Plugin::lv2_persist_retrieve_callback(void* host_data,
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;
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;
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());
503 if (strncmp(absolute_path, state_dir.c_str(), state_dir.length())) {
504 ret = g_strdup(absolute_path);
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));
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);
526 if (g_path_is_absolute(abstract_path)) {
527 ret = g_strdup(abstract_path);
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,
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));
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,
557 char* dirname = g_path_get_dirname(path.c_str());
558 g_mkdir_with_parents(dirname, 0744);
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());
568 LV2Plugin::add_state(XMLNode* root) const
570 assert(_insert_id != PBD::ID("0"));
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");
599 warning << string_compose(
600 _("Plugin \"%1\% failed to return LV2 persist data"),
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,
611 // Write state object to RDFF file
612 RDFF file = rdff_open(state_path.c_str(), true);
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;
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);
638 _presets.insert(std::make_pair(lilv_node_as_string(preset),
640 lilv_node_as_string(preset),
641 lilv_node_as_string(name))));
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);
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);
685 LV2Plugin::do_save_preset(string /*name*/)
691 LV2Plugin::do_remove_preset(string /*name*/)
695 LV2Plugin::has_editor() const
697 return _impl->ui != NULL;
701 LV2Plugin::set_insert_info(const PluginInsert* insert)
703 _insert_id = insert->id();
707 LV2Plugin::set_state(const XMLNode& node, int version)
710 const XMLProperty* prop;
711 XMLNodeConstIterator iter;
716 LocaleGuard lg(X_("POSIX"));
718 if (node.name() != state_node_name()) {
719 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
723 if (version < 3000) {
724 nodes = node.children("port");
726 nodes = node.children("Port");
729 for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
733 if ((prop = child->property("symbol")) != 0) {
734 sym = prop->value().c_str();
736 warning << _("LV2: port has no symbol, ignored") << endmsg;
740 map<string, uint32_t>::iterator i = _port_indices.find(sym);
742 if (i != _port_indices.end()) {
745 warning << _("LV2: port has unknown index, ignored") << endmsg;
749 if ((prop = child->property("value")) != 0) {
750 value = prop->value().c_str();
752 warning << _("LV2: port has no value, ignored") << endmsg;
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(),
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");
769 cout << "Loading LV2 state from " << state_path << endl;
770 RDFF file = rdff_open(state_path.c_str(), false);
771 LV2PersistState state(_uri_map);
773 persist->restore(_impl->instance->lv2_handle,
774 &LV2Plugin::lv2_persist_retrieve_callback,
778 warning << string_compose(
779 _("Plugin \"%1\% failed to return LV2 persist data"),
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) {
814 desc.smallstep = 0.1;
815 desc.largestep = 10.0;
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;
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);
845 LV2Plugin::signal_latency() const
847 if (_latency_control_port) {
848 return (framecnt_t)floor(*_latency_control_port);
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));
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;
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;
893 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 cleanup\n", name()));
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 ChanCount bufs_count;
912 bufs_count.set(DataType::AUDIO, 1);
913 bufs_count.set(DataType::MIDI, 1);
914 BufferSet& silent_bufs = _session.get_silent_buffers(bufs_count);
915 BufferSet& scratch_bufs = _session.get_silent_buffers(bufs_count);
917 uint32_t audio_in_index = 0;
918 uint32_t audio_out_index = 0;
919 uint32_t midi_in_index = 0;
920 uint32_t midi_out_index = 0;
922 for (uint32_t port_index = 0; port_index < parameter_count(); ++port_index) {
923 if (parameter_is_audio(port_index)) {
924 if (parameter_is_input(port_index)) {
925 const uint32_t buf_index = in_map.get(DataType::AUDIO, audio_in_index++, &valid);
926 lilv_instance_connect_port(_impl->instance, port_index,
927 valid ? bufs.get_audio(buf_index).data(offset)
928 : silent_bufs.get_audio(0).data(offset));
929 } else if (parameter_is_output(port_index)) {
930 const uint32_t buf_index = out_map.get(DataType::AUDIO, audio_out_index++, &valid);
931 //cerr << port_index << " : " << " AUDIO OUT " << buf_index << endl;
932 lilv_instance_connect_port(_impl->instance, port_index,
933 valid ? bufs.get_audio(buf_index).data(offset)
934 : scratch_bufs.get_audio(0).data(offset));
936 } else if (parameter_is_midi(port_index)) {
937 /* FIXME: The checks here for bufs.count().n_midi() > buf_index shouldn't
938 be necessary, but the mapping is illegal in some cases. Ideally
939 that should be fixed, but this is easier...
941 if (parameter_is_input(port_index)) {
942 const uint32_t buf_index = in_map.get(DataType::MIDI, midi_in_index++, &valid);
943 if (valid && bufs.count().n_midi() > buf_index) {
944 lilv_instance_connect_port(_impl->instance, port_index,
945 bufs.get_lv2_midi(true, buf_index).data());
947 lilv_instance_connect_port(_impl->instance, port_index,
948 silent_bufs.get_lv2_midi(true, 0).data());
950 } else if (parameter_is_output(port_index)) {
951 const uint32_t buf_index = out_map.get(DataType::MIDI, midi_out_index++, &valid);
952 if (valid && bufs.count().n_midi() > buf_index) {
953 lilv_instance_connect_port(_impl->instance, port_index,
954 bufs.get_lv2_midi(false, buf_index).data());
956 lilv_instance_connect_port(_impl->instance, port_index,
957 scratch_bufs.get_lv2_midi(true, 0).data());
960 } else if (!parameter_is_control(port_index)) {
961 // Optional port (it'd better be if we've made it this far...)
962 lilv_instance_connect_port(_impl->instance, port_index, NULL);
969 for (uint32_t port_index = 0; port_index < parameter_count(); ++port_index) {
970 if (parameter_is_midi(port_index) && parameter_is_output(port_index)) {
971 const uint32_t buf_index = out_map.get(DataType::MIDI, midi_out_index++, &valid);
973 bufs.flush_lv2_midi(true, buf_index);
978 cycles_t now = get_cycles();
979 set_cycles((uint32_t)(now - then));
985 LV2Plugin::parameter_is_control(uint32_t param) const
987 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
988 return lilv_port_is_a(_impl->plugin, port, _world.control_class);
992 LV2Plugin::parameter_is_audio(uint32_t param) const
994 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
995 return lilv_port_is_a(_impl->plugin, port, _world.audio_class);
999 LV2Plugin::parameter_is_midi(uint32_t param) const
1001 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
1002 return lilv_port_is_a(_impl->plugin, port, _world.event_class);
1003 // && lilv_port_supports_event(_impl->plugin, port, _world.midi_class);
1007 LV2Plugin::parameter_is_output(uint32_t param) const
1009 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
1010 return lilv_port_is_a(_impl->plugin, port, _world.output_class);
1014 LV2Plugin::parameter_is_input(uint32_t param) const
1016 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, param);
1017 return lilv_port_is_a(_impl->plugin, port, _world.input_class);
1021 LV2Plugin::print_parameter(uint32_t param, char* buf, uint32_t len) const
1024 if (param < parameter_count()) {
1025 snprintf(buf, len, "%.3f", get_parameter(param));
1032 boost::shared_ptr<Plugin::ScalePoints>
1033 LV2Plugin::get_scale_points(uint32_t port_index) const
1035 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, port_index);
1036 LilvScalePoints* points = lilv_port_get_scale_points(_impl->plugin, port);
1038 boost::shared_ptr<Plugin::ScalePoints> ret;
1043 ret = boost::shared_ptr<Plugin::ScalePoints>(new ScalePoints());
1045 LILV_FOREACH(scale_points, i, points) {
1046 const LilvScalePoint* p = lilv_scale_points_get(points, i);
1047 const LilvNode* label = lilv_scale_point_get_label(p);
1048 const LilvNode* value = lilv_scale_point_get_value(p);
1049 if (label && (lilv_node_is_float(value) || lilv_node_is_int(value))) {
1050 ret->insert(make_pair(lilv_node_as_string(label),
1051 lilv_node_as_float(value)));
1055 lilv_scale_points_free(points);
1060 LV2Plugin::run(pframes_t nframes)
1062 for (uint32_t i = 0; i < parameter_count(); ++i) {
1063 if (parameter_is_control(i) && parameter_is_input(i)) {
1064 _control_data[i] = _shadow_data[i];
1068 lilv_instance_run(_impl->instance, nframes);
1072 LV2Plugin::latency_compute_run()
1074 if (!_latency_control_port) {
1078 // Run the plugin so that it can set its latency parameter
1082 uint32_t port_index = 0;
1083 uint32_t in_index = 0;
1084 uint32_t out_index = 0;
1086 const framecnt_t bufsize = 1024;
1087 float buffer[bufsize];
1089 memset(buffer, 0, sizeof(float) * bufsize);
1091 // FIXME: Ensure plugins can handle in-place processing
1095 while (port_index < parameter_count()) {
1096 if (parameter_is_audio(port_index)) {
1097 if (parameter_is_input(port_index)) {
1098 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1100 } else if (parameter_is_output(port_index)) {
1101 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1112 LV2World::LV2World()
1113 : world(lilv_world_new())
1115 lilv_world_load_all(world);
1116 input_class = lilv_new_uri(world, LILV_URI_INPUT_PORT);
1117 output_class = lilv_new_uri(world, LILV_URI_OUTPUT_PORT);
1118 control_class = lilv_new_uri(world, LILV_URI_CONTROL_PORT);
1119 audio_class = lilv_new_uri(world, LILV_URI_AUDIO_PORT);
1120 event_class = lilv_new_uri(world, LILV_URI_EVENT_PORT);
1121 midi_class = lilv_new_uri(world, LILV_URI_MIDI_EVENT);
1122 in_place_broken = lilv_new_uri(world, LILV_NS_LV2 "inPlaceBroken");
1123 integer = lilv_new_uri(world, LILV_NS_LV2 "integer");
1124 toggled = lilv_new_uri(world, LILV_NS_LV2 "toggled");
1125 srate = lilv_new_uri(world, LILV_NS_LV2 "sampleRate");
1126 gtk_gui = lilv_new_uri(world, NS_UI "GtkUI");
1127 external_gui = lilv_new_uri(world, NS_UI "external");
1128 logarithmic = lilv_new_uri(world, "http://lv2plug.in/ns/dev/extportinfo#logarithmic");
1131 LV2World::~LV2World()
1133 lilv_node_free(input_class);
1134 lilv_node_free(output_class);
1135 lilv_node_free(control_class);
1136 lilv_node_free(audio_class);
1137 lilv_node_free(event_class);
1138 lilv_node_free(midi_class);
1139 lilv_node_free(in_place_broken);
1142 LV2PluginInfo::LV2PluginInfo (void* c_plugin)
1143 : _c_plugin(c_plugin)
1148 LV2PluginInfo::~LV2PluginInfo()
1152 LV2PluginInfo::load(Session& session)
1157 plugin.reset(new LV2Plugin(session.engine(), session,
1158 (LilvPlugin*)_c_plugin,
1159 session.frame_rate()));
1161 plugin->set_info(PluginInfoPtr(new LV2PluginInfo(*this)));
1163 } catch (failed_constructor& err) {
1164 return PluginPtr((Plugin*)0);
1171 LV2PluginInfo::discover()
1173 PluginInfoList* plugs = new PluginInfoList;
1174 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
1176 cerr << "LV2: Discovering " << lilv_plugins_size(plugins) << " plugins" << endl;
1178 LILV_FOREACH(plugins, i, plugins) {
1179 const LilvPlugin* p = lilv_plugins_get(plugins, i);
1180 LV2PluginInfoPtr info(new LV2PluginInfo((void*)p));
1182 LilvNode* name = lilv_plugin_get_name(p);
1184 cerr << "LV2: invalid plugin\n";
1190 info->name = string(lilv_node_as_string(name));
1191 lilv_node_free(name);
1193 const LilvPluginClass* pclass = lilv_plugin_get_class(p);
1194 const LilvNode* label = lilv_plugin_class_get_label(pclass);
1195 info->category = lilv_node_as_string(label);
1197 LilvNode* author_name = lilv_plugin_get_author_name(p);
1198 info->creator = author_name ? string(lilv_node_as_string(author_name)) : "Unknown";
1199 lilv_node_free(author_name);
1201 info->path = "/NOPATH"; // Meaningless for LV2
1203 info->n_inputs.set_audio(
1204 lilv_plugin_get_num_ports_of_class(
1205 p, _world.input_class, _world.audio_class, NULL));
1206 info->n_inputs.set_midi(
1207 lilv_plugin_get_num_ports_of_class(
1208 p, _world.input_class, _world.event_class, NULL));
1210 info->n_outputs.set_audio(
1211 lilv_plugin_get_num_ports_of_class(
1212 p, _world.output_class, _world.audio_class, NULL));
1213 info->n_outputs.set_midi(
1214 lilv_plugin_get_num_ports_of_class(
1215 p, _world.output_class, _world.event_class, NULL));
1217 info->unique_id = lilv_node_as_uri(lilv_plugin_get_uri(p));
1218 info->index = 0; // Meaningless for LV2
1220 plugs->push_back(info);
1223 cerr << "Done LV2 discovery" << endl;