Set up default and upper/lower limit correctly on LV2 ports
[ardour2.git] / libs / ardour / lv2_plugin.cc
blob5060756800784159f52f2f5efbd221c10676d7a5
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 <slv2/slv2.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 SLV2World world;
81 SLV2Value input_class; ///< Input port
82 SLV2Value output_class; ///< Output port
83 SLV2Value audio_class; ///< Audio port
84 SLV2Value control_class; ///< Control port
85 SLV2Value event_class; ///< Event port
86 SLV2Value midi_class; ///< MIDI event
87 SLV2Value in_place_broken;
88 SLV2Value integer;
89 SLV2Value toggled;
90 SLV2Value srate;
91 SLV2Value gtk_gui;
92 SLV2Value external_gui;
93 SLV2Value 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 SLV2Plugin plugin;
101 SLV2UI ui;
102 SLV2Value ui_type;
103 SLV2Value name;
104 SLV2Value author;
105 SLV2Instance 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 = (SLV2Plugin)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 SLV2Plugin plugin = _impl->plugin;
156 SLV2Value persist_uri = slv2_value_new_uri(_world.world, _persist_feature.URI);
157 _supports_persist = slv2_plugin_has_feature(plugin, persist_uri);
158 slv2_value_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 = slv2_plugin_instantiate(plugin, rate, _features);
183 _impl->name = slv2_plugin_get_name(plugin);
184 _impl->author = slv2_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 (slv2_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 slv2_value_as_string(_impl->name)) << endmsg;
199 slv2_value_free(_impl->name);
200 slv2_value_free(_impl->author);
201 throw failed_constructor();
204 _sample_rate = rate;
206 const uint32_t num_ports = this->num_ports();
207 const bool latent = slv2_plugin_has_latency(plugin);
208 const uint32_t latency_port = (latent)
209 ? slv2_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 SLV2Port port = slv2_plugin_get_port_by_index(plugin, i);
218 SLV2Value sym = slv2_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(slv2_value_as_string(sym), i));
223 // Get range and default value if applicable
224 if (parameter_is_control(i)) {
225 SLV2Value def;
226 slv2_port_get_range(plugin, port, &def, NULL, NULL);
227 _defaults[i] = def ? slv2_value_as_float(def) : 0.0f;
228 if (slv2_port_has_property(_impl->plugin, port, _world.srate)) {
229 _defaults[i] *= _session.frame_rate ();
231 slv2_value_free(def);
233 slv2_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 SLV2UIs uis = slv2_plugin_get_uis(plugin);
249 if (slv2_uis_size(uis) > 0) {
250 #if defined(HAVE_NEW_SLV2) and defined(HAVE_SUIL)
251 // Look for embeddable UI
252 SLV2Value ui_type = NULL;
253 SLV2_FOREACH(uis, u, uis) {
254 SLV2UI this_ui = slv2_uis_get(uis, u);
255 SLV2Value this_ui_type = NULL;
256 if (slv2_ui_is_supported(this_ui,
257 suil_ui_supported,
258 _world.gtk_gui,
259 &this_ui_type)) {
260 // TODO: Multiple UI support
261 _ui = this_ui;
262 _ui_type = this_ui_type;
263 break;
266 #else
267 // Look for Gtk native UI
268 for (unsigned i = 0; i < slv2_uis_size(uis); ++i) {
269 SLV2UI ui = slv2_uis_get_at(uis, i);
270 if (slv2_ui_is_a(ui, _world.gtk_gui)) {
271 _impl->ui = ui;
272 _impl->ui_type = _world.gtk_gui;
273 break;
276 #endif
278 // If Gtk UI is not available, try to find external UI
279 if (!_impl->ui) {
280 for (unsigned i = 0; i < slv2_uis_size(uis); ++i) {
281 SLV2UI ui = slv2_uis_get_at(uis, i);
282 if (slv2_ui_is_a(ui, _world.external_gui)) {
283 _impl->ui = ui;
284 _impl->ui_type = _world.external_gui;
285 break;
291 latency_compute_run();
294 LV2Plugin::~LV2Plugin ()
296 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 destroy\n", name()));
298 deactivate();
299 cleanup();
301 slv2_instance_free(_impl->instance);
302 slv2_value_free(_impl->name);
303 slv2_value_free(_impl->author);
304 #if defined(HAVE_NEW_SLV2) and defined(HAVE_SUIL)
305 slv2_value_free(_impl->ui_type);
306 #else
307 /* _ui_type points to a global, so leave it alone */
308 #endif
309 delete [] _control_data;
310 delete [] _shadow_data;
313 bool
314 LV2Plugin::is_external_ui() const
316 if (!_impl->ui) {
317 return false;
319 return slv2_ui_is_a(_impl->ui, _world.external_gui);
322 string
323 LV2Plugin::unique_id() const
325 return slv2_value_as_uri(slv2_plugin_get_uri(_impl->plugin));
328 const char*
329 LV2Plugin::uri() const
331 return slv2_value_as_uri(slv2_plugin_get_uri(_impl->plugin));
334 const char*
335 LV2Plugin::label() const
337 return slv2_value_as_string(_impl->name);
340 const char*
341 LV2Plugin::name() const
343 return slv2_value_as_string(_impl->name);
346 const char*
347 LV2Plugin::maker() const
349 return _impl->author ? slv2_value_as_string (_impl->author) : "Unknown";
352 uint32_t
353 LV2Plugin::num_ports() const
355 return slv2_plugin_get_num_ports(_impl->plugin);
358 uint32_t
359 LV2Plugin::parameter_count() const
361 return slv2_plugin_get_num_ports(_impl->plugin);
364 float
365 LV2Plugin::default_value(uint32_t port)
367 return _defaults[port];
370 const char*
371 LV2Plugin::port_symbol(uint32_t index) const
373 SLV2Port port = slv2_plugin_get_port_by_index(_impl->plugin, index);
374 if (!port) {
375 error << name() << ": Invalid port index " << index << endmsg;
378 SLV2Value sym = slv2_port_get_symbol(_impl->plugin, port);
379 return slv2_value_as_string(sym);
382 void
383 LV2Plugin::set_parameter(uint32_t which, float val)
385 DEBUG_TRACE(DEBUG::LV2, string_compose(
386 "%1 set parameter %2 to %3\n", name(), which, val));
388 if (which < slv2_plugin_get_num_ports(_impl->plugin)) {
389 _shadow_data[which] = val;
390 } else {
391 warning << string_compose(
392 _("Illegal parameter number used with plugin \"%1\". "
393 "This is a bug in either %2 or the LV2 plugin <%3>"),
394 name(), PROGRAM_NAME, unique_id()) << endmsg;
397 Plugin::set_parameter(which, val);
400 float
401 LV2Plugin::get_parameter(uint32_t which) const
403 if (parameter_is_input(which)) {
404 return (float)_shadow_data[which];
405 } else {
406 return (float)_control_data[which];
408 return 0.0f;
411 uint32_t
412 LV2Plugin::nth_parameter(uint32_t n, bool& ok) const
414 ok = false;
415 for (uint32_t c = 0, x = 0; x < slv2_plugin_get_num_ports(_impl->plugin); ++x) {
416 if (parameter_is_control(x)) {
417 if (c++ == n) {
418 ok = true;
419 return x;
424 return 0;
427 const void*
428 LV2Plugin::extension_data (const char* uri) const
430 return slv2_instance_get_extension_data(_impl->instance, uri);
433 void*
434 LV2Plugin::c_plugin ()
436 return _impl->plugin;
439 void*
440 LV2Plugin::c_ui ()
442 return _impl->ui;
445 void*
446 LV2Plugin::c_ui_type ()
448 return _impl->ui_type;
452 LV2Plugin::lv2_persist_store_callback(void* host_data,
453 uint32_t key,
454 const void* value,
455 size_t size,
456 uint32_t type,
457 uint32_t flags)
459 DEBUG_TRACE(DEBUG::LV2, string_compose(
460 "persist store %1 (size: %2, type: %3)\n",
461 _uri_map.id_to_uri(NULL, key),
462 size,
463 _uri_map.id_to_uri(NULL, type)));
465 LV2PersistState* state = (LV2PersistState*)host_data;
466 state->add_uri(key, _uri_map.id_to_uri(NULL, key));
467 state->add_uri(type, _uri_map.id_to_uri(NULL, type));
468 return state->add_value(key, value, size, type, flags);
471 const void*
472 LV2Plugin::lv2_persist_retrieve_callback(void* host_data,
473 uint32_t key,
474 size_t* size,
475 uint32_t* type,
476 uint32_t* flags)
478 LV2PersistState* state = (LV2PersistState*)host_data;
479 LV2PersistState::Values::const_iterator i = state->values.find(key);
480 if (i == state->values.end()) {
481 warning << "LV2 plugin attempted to retrieve nonexistent key: "
482 << _uri_map.id_to_uri(NULL, key) << endmsg;
483 return NULL;
485 *size = i->second.size;
486 *type = i->second.type;
487 *flags = LV2_PERSIST_IS_POD | LV2_PERSIST_IS_PORTABLE; // FIXME
488 DEBUG_TRACE(DEBUG::LV2, string_compose(
489 "persist retrieve %1 = %2 (size: %3, type: %4)\n",
490 _uri_map.id_to_uri(NULL, key),
491 i->second.value, *size, *type));
492 return i->second.value;
495 char*
496 LV2Plugin::lv2_files_abstract_path(LV2_Files_Host_Data host_data,
497 const char* absolute_path)
499 LV2Plugin* me = (LV2Plugin*)host_data;
500 if (me->_insert_id == PBD::ID("0")) {
501 return g_strdup(absolute_path);
504 const std::string state_dir = Glib::build_filename(me->_session.plugins_dir(),
505 me->_insert_id.to_s());
507 char* ret = NULL;
508 if (strncmp(absolute_path, state_dir.c_str(), state_dir.length())) {
509 ret = g_strdup(absolute_path);
510 } else {
511 const std::string path(absolute_path + state_dir.length() + 1);
512 ret = g_strndup(path.c_str(), path.length());
515 DEBUG_TRACE(DEBUG::LV2, string_compose("abstract path %1 => %2\n",
516 absolute_path, ret));
518 return ret;
521 char*
522 LV2Plugin::lv2_files_absolute_path(LV2_Files_Host_Data host_data,
523 const char* abstract_path)
525 LV2Plugin* me = (LV2Plugin*)host_data;
526 if (me->_insert_id == PBD::ID("0")) {
527 return g_strdup(abstract_path);
530 char* ret = NULL;
531 if (g_path_is_absolute(abstract_path)) {
532 ret = g_strdup(abstract_path);
533 } else {
534 const std::string apath(abstract_path);
535 const std::string state_dir = Glib::build_filename(me->_session.plugins_dir(),
536 me->_insert_id.to_s());
537 const std::string path = Glib::build_filename(state_dir,
538 apath);
539 ret = g_strndup(path.c_str(), path.length());
542 DEBUG_TRACE(DEBUG::LV2, string_compose("absolute path %1 => %2\n",
543 abstract_path, ret));
545 return ret;
548 char*
549 LV2Plugin::lv2_files_new_file_path(LV2_Files_Host_Data host_data,
550 const char* relative_path)
552 LV2Plugin* me = (LV2Plugin*)host_data;
553 if (me->_insert_id == PBD::ID("0")) {
554 return g_strdup(relative_path);
557 const std::string state_dir = Glib::build_filename(me->_session.plugins_dir(),
558 me->_insert_id.to_s());
559 const std::string path = Glib::build_filename(state_dir,
560 relative_path);
562 char* dirname = g_path_get_dirname(path.c_str());
563 g_mkdir_with_parents(dirname, 0744);
564 free(dirname);
566 DEBUG_TRACE(DEBUG::LV2, string_compose("new file path %1 => %2\n",
567 relative_path, path));
569 return g_strndup(path.c_str(), path.length());
572 void
573 LV2Plugin::add_state(XMLNode* root) const
575 assert(_insert_id != PBD::ID("0"));
577 XMLNode* child;
578 char buf[16];
579 LocaleGuard lg(X_("POSIX"));
581 for (uint32_t i = 0; i < parameter_count(); ++i) {
582 if (parameter_is_input(i) && parameter_is_control(i)) {
583 child = new XMLNode("Port");
584 child->add_property("symbol", port_symbol(i));
585 snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
586 child->add_property("value", string(buf));
587 root->add_child_nocopy(*child);
591 if (_supports_persist) {
592 // Create state directory for this plugin instance
593 const std::string state_filename = _insert_id.to_s() + ".rdff";
594 const std::string state_path = Glib::build_filename(
595 _session.plugins_dir(), state_filename);
597 cout << "Saving LV2 plugin state to " << state_path << endl;
599 // Get LV2 Persist extension data from plugin instance
600 LV2_Persist* persist = (LV2_Persist*)extension_data(
601 "http://lv2plug.in/ns/ext/persist");
602 if (!persist) {
603 warning << string_compose(
604 _("Plugin \"%1\% failed to return LV2 persist data"),
605 unique_id());
606 return;
609 // Save plugin state to state object
610 LV2PersistState state(_uri_map);
611 persist->save(_impl->instance->lv2_handle,
612 &LV2Plugin::lv2_persist_store_callback,
613 &state);
615 // Write state object to RDFF file
616 RDFF file = rdff_open(state_path.c_str(), true);
617 state.write(file);
618 rdff_close(file);
620 root->add_property("state-file", state_filename);
624 static inline SLV2Value
625 get_value(SLV2Plugin p, SLV2Value subject, SLV2Value predicate)
627 SLV2Values vs = slv2_plugin_get_value_for_subject(p, subject, predicate);
628 return vs ? slv2_values_get_at(vs, 0) : NULL;
631 void
632 LV2Plugin::find_presets()
634 SLV2Value dc_title = slv2_value_new_uri(_world.world, NS_DC "title");
635 SLV2Value pset_hasPreset = slv2_value_new_uri(_world.world, NS_PSET "hasPreset");
637 SLV2Values presets = slv2_plugin_get_value(_impl->plugin, pset_hasPreset);
638 for (unsigned i = 0; i < slv2_values_size(presets); ++i) {
639 SLV2Value preset = slv2_values_get_at(presets, i);
640 SLV2Value name = get_value(_impl->plugin, preset, dc_title);
641 if (name) {
642 _presets.insert(std::make_pair(slv2_value_as_string(preset),
643 PresetRecord(
644 slv2_value_as_string(preset),
645 slv2_value_as_string(name))));
646 } else {
647 warning << string_compose(
648 _("Plugin \"%1\% preset \"%2%\" is missing a dc:title\n"),
649 unique_id(), slv2_value_as_string(preset));
652 slv2_values_free(presets);
654 slv2_value_free(pset_hasPreset);
655 slv2_value_free(dc_title);
658 bool
659 LV2Plugin::load_preset(PresetRecord r)
661 Plugin::load_preset(r);
663 #ifdef HAVE_NEW_SLV2
664 // New (>= 0.7.0) slv2 no longer supports SPARQL, but exposes blank nodes
665 // so querying ports is possible with the simple/fast API
666 SLV2Value lv2_port = slv2_value_new_uri(_world.world, NS_LV2 "port");
667 SLV2Value lv2_symbol = slv2_value_new_uri(_world.world, NS_LV2 "symbol");
668 SLV2Value pset_value = slv2_value_new_uri(_world.world, NS_PSET "value");
669 SLV2Value preset = slv2_value_new_uri(_world.world, r.uri.c_str());
671 SLV2Values ports = slv2_plugin_get_value_for_subject(_impl->plugin, preset, lv2_port);
672 for (unsigned i = 0; i < slv2_values_size(ports); ++i) {
673 SLV2Value port = slv2_values_get_at(ports, i);
674 SLV2Value symbol = get_value(_impl->plugin, port, lv2_symbol);
675 SLV2Value value = get_value(_impl->plugin, port, pset_value);
676 if (value && slv2_value_is_float(value)) {
677 set_parameter(_port_indices[slv2_value_as_string(symbol)],
678 slv2_value_as_float(value));
681 slv2_values_free(ports);
683 slv2_value_free(preset);
684 slv2_value_free(pset_value);
685 slv2_value_free(lv2_symbol);
686 slv2_value_free(lv2_port);
687 #else
688 const string query = string(
689 "PREFIX lv2p: <http://lv2plug.in/ns/dev/presets#>\n"
690 "PREFIX dc: <http://dublincore.org/documents/dcmi-namespace/>\n"
691 "SELECT ?sym ?val WHERE { <") + r.uri + "> lv2:port ?port . "
692 " ?port lv2:symbol ?sym ; lv2p:value ?val . }";
693 SLV2Results values = slv2_plugin_query_sparql(_impl->plugin, query.c_str());
694 for (; !slv2_results_finished(values); slv2_results_next(values)) {
695 SLV2Value sym = slv2_results_get_binding_value(values, 0);
696 SLV2Value val = slv2_results_get_binding_value(values, 1);
697 if (slv2_value_is_float(val)) {
698 uint32_t index = _port_indices[slv2_value_as_string(sym)];
699 set_parameter(index, slv2_value_as_float(val));
702 slv2_results_free(values);
703 #endif
704 return true;
707 std::string
708 LV2Plugin::do_save_preset(string /*name*/)
710 return "";
713 void
714 LV2Plugin::do_remove_preset(string /*name*/)
717 bool
718 LV2Plugin::has_editor() const
720 return _impl->ui != NULL;
723 void
724 LV2Plugin::set_insert_info(const PluginInsert* insert)
726 _insert_id = insert->id();
730 LV2Plugin::set_state(const XMLNode& node, int version)
732 XMLNodeList nodes;
733 const XMLProperty* prop;
734 XMLNodeConstIterator iter;
735 XMLNode* child;
736 const char* sym;
737 const char* value;
738 uint32_t port_id;
739 LocaleGuard lg(X_("POSIX"));
741 if (node.name() != state_node_name()) {
742 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
743 return -1;
746 if (version < 3000) {
747 nodes = node.children("port");
748 } else {
749 nodes = node.children("Port");
752 for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
754 child = *iter;
756 if ((prop = child->property("symbol")) != 0) {
757 sym = prop->value().c_str();
758 } else {
759 warning << _("LV2: port has no symbol, ignored") << endmsg;
760 continue;
763 map<string, uint32_t>::iterator i = _port_indices.find(sym);
765 if (i != _port_indices.end()) {
766 port_id = i->second;
767 } else {
768 warning << _("LV2: port has unknown index, ignored") << endmsg;
769 continue;
772 if ((prop = child->property("value")) != 0) {
773 value = prop->value().c_str();
774 } else {
775 warning << _("LV2: port has no value, ignored") << endmsg;
776 continue;
779 set_parameter(port_id, atof(value));
782 if ((prop = node.property("state-file")) != 0) {
783 std::string state_path = Glib::build_filename(_session.plugins_dir(),
784 prop->value());
786 cout << "LV2 state path " << state_path << endl;
788 // Get LV2 Persist extension data from plugin instance
789 LV2_Persist* persist = (LV2_Persist*)extension_data(
790 "http://lv2plug.in/ns/ext/persist");
791 if (persist) {
792 cout << "Loading LV2 state from " << state_path << endl;
793 RDFF file = rdff_open(state_path.c_str(), false);
794 LV2PersistState state(_uri_map);
795 state.read(file);
796 persist->restore(_impl->instance->lv2_handle,
797 &LV2Plugin::lv2_persist_retrieve_callback,
798 &state);
799 rdff_close(file);
800 } else {
801 warning << string_compose(
802 _("Plugin \"%1\% failed to return LV2 persist data"),
803 unique_id());
807 latency_compute_run();
809 return Plugin::set_state(node, version);
813 LV2Plugin::get_parameter_descriptor(uint32_t which, ParameterDescriptor& desc) const
815 SLV2Port port = slv2_plugin_get_port_by_index(_impl->plugin, which);
817 SLV2Value def, min, max;
818 slv2_port_get_range(_impl->plugin, port, &def, &min, &max);
820 desc.integer_step = slv2_port_has_property(_impl->plugin, port, _world.integer);
821 desc.toggled = slv2_port_has_property(_impl->plugin, port, _world.toggled);
822 desc.logarithmic = slv2_port_has_property(_impl->plugin, port, _world.logarithmic);
823 desc.sr_dependent = slv2_port_has_property(_impl->plugin, port, _world.srate);
824 desc.label = slv2_value_as_string(slv2_port_get_name(_impl->plugin, port));
825 desc.lower = min ? slv2_value_as_float(min) : 0.0f;
826 desc.upper = max ? slv2_value_as_float(max) : 1.0f;
827 if (desc.sr_dependent) {
828 desc.lower *= _session.frame_rate ();
829 desc.upper *= _session.frame_rate ();
832 desc.min_unbound = false; // TODO: LV2 extension required
833 desc.max_unbound = false; // TODO: LV2 extension required
835 if (desc.integer_step) {
836 desc.step = 1.0;
837 desc.smallstep = 0.1;
838 desc.largestep = 10.0;
839 } else {
840 const float delta = desc.upper - desc.lower;
841 desc.step = delta / 1000.0f;
842 desc.smallstep = delta / 10000.0f;
843 desc.largestep = delta / 10.0f;
846 slv2_value_free(def);
847 slv2_value_free(min);
848 slv2_value_free(max);
850 return 0;
853 string
854 LV2Plugin::describe_parameter(Evoral::Parameter which)
856 if (( which.type() == PluginAutomation) && ( which.id() < parameter_count()) ) {
857 SLV2Value name = slv2_port_get_name(_impl->plugin,
858 slv2_plugin_get_port_by_index(_impl->plugin, which.id()));
859 string ret(slv2_value_as_string(name));
860 slv2_value_free(name);
861 return ret;
862 } else {
863 return "??";
867 framecnt_t
868 LV2Plugin::signal_latency() const
870 if (_latency_control_port) {
871 return (framecnt_t)floor(*_latency_control_port);
872 } else {
873 return 0;
877 set<Evoral::Parameter>
878 LV2Plugin::automatable() const
880 set<Evoral::Parameter> ret;
882 for (uint32_t i = 0; i < parameter_count(); ++i) {
883 if (parameter_is_input(i) && parameter_is_control(i)) {
884 ret.insert(ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
888 return ret;
891 void
892 LV2Plugin::activate()
894 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 activate\n", name()));
896 if (!_was_activated) {
897 slv2_instance_activate(_impl->instance);
898 _was_activated = true;
902 void
903 LV2Plugin::deactivate()
905 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 deactivate\n", name()));
907 if (_was_activated) {
908 slv2_instance_deactivate(_impl->instance);
909 _was_activated = false;
913 void
914 LV2Plugin::cleanup()
916 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 cleanup\n", name()));
918 activate();
919 deactivate();
920 slv2_instance_free(_impl->instance);
921 _impl->instance = NULL;
925 LV2Plugin::connect_and_run(BufferSet& bufs,
926 ChanMapping in_map, ChanMapping out_map,
927 pframes_t nframes, framecnt_t offset)
929 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 run %2 offset %3\n", name(), nframes, offset));
930 Plugin::connect_and_run(bufs, in_map, out_map, nframes, offset);
932 cycles_t then = get_cycles();
934 uint32_t audio_in_index = 0;
935 uint32_t audio_out_index = 0;
936 uint32_t midi_in_index = 0;
937 uint32_t midi_out_index = 0;
938 for (uint32_t port_index = 0; port_index < parameter_count(); ++port_index) {
939 if (parameter_is_audio(port_index)) {
940 if (parameter_is_input(port_index)) {
941 const uint32_t buf_index = in_map.get(DataType::AUDIO, audio_in_index++);
942 slv2_instance_connect_port(_impl->instance, port_index,
943 bufs.get_audio(buf_index).data(offset));
944 } else if (parameter_is_output(port_index)) {
945 const uint32_t buf_index = out_map.get(DataType::AUDIO, audio_out_index++);
946 //cerr << port_index << " : " << " AUDIO OUT " << buf_index << endl;
947 slv2_instance_connect_port(_impl->instance, port_index,
948 bufs.get_audio(buf_index).data(offset));
950 } else if (parameter_is_midi(port_index)) {
951 if (parameter_is_input(port_index)) {
952 const uint32_t buf_index = in_map.get(DataType::MIDI, midi_in_index++);
953 slv2_instance_connect_port(_impl->instance, port_index,
954 bufs.get_lv2_midi(true, buf_index).data());
955 } else if (parameter_is_output(port_index)) {
956 const uint32_t buf_index = out_map.get(DataType::MIDI, midi_out_index++);
957 slv2_instance_connect_port(_impl->instance, port_index,
958 bufs.get_lv2_midi(false, buf_index).data());
960 } else if (!parameter_is_control(port_index)) {
961 // Optional port (it'd better be if we've made it this far...)
962 slv2_instance_connect_port(_impl->instance, port_index, NULL);
966 run(nframes);
968 midi_out_index = 0;
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++);
972 bufs.flush_lv2_midi(true, buf_index);
976 cycles_t now = get_cycles();
977 set_cycles((uint32_t)(now - then));
979 return 0;
982 bool
983 LV2Plugin::parameter_is_control(uint32_t param) const
985 SLV2Port port = slv2_plugin_get_port_by_index(_impl->plugin, param);
986 return slv2_port_is_a(_impl->plugin, port, _world.control_class);
989 bool
990 LV2Plugin::parameter_is_audio(uint32_t param) const
992 SLV2Port port = slv2_plugin_get_port_by_index(_impl->plugin, param);
993 return slv2_port_is_a(_impl->plugin, port, _world.audio_class);
996 bool
997 LV2Plugin::parameter_is_midi(uint32_t param) const
999 SLV2Port port = slv2_plugin_get_port_by_index(_impl->plugin, param);
1000 return slv2_port_is_a(_impl->plugin, port, _world.event_class);
1001 // && slv2_port_supports_event(_impl->plugin, port, _world.midi_class);
1004 bool
1005 LV2Plugin::parameter_is_output(uint32_t param) const
1007 SLV2Port port = slv2_plugin_get_port_by_index(_impl->plugin, param);
1008 return slv2_port_is_a(_impl->plugin, port, _world.output_class);
1011 bool
1012 LV2Plugin::parameter_is_input(uint32_t param) const
1014 SLV2Port port = slv2_plugin_get_port_by_index(_impl->plugin, param);
1015 return slv2_port_is_a(_impl->plugin, port, _world.input_class);
1018 void
1019 LV2Plugin::print_parameter(uint32_t param, char* buf, uint32_t len) const
1021 if (buf && len) {
1022 if (param < parameter_count()) {
1023 snprintf(buf, len, "%.3f", get_parameter(param));
1024 } else {
1025 strcat(buf, "0");
1030 boost::shared_ptr<Plugin::ScalePoints>
1031 LV2Plugin::get_scale_points(uint32_t port_index) const
1033 SLV2Port port = slv2_plugin_get_port_by_index(_impl->plugin, port_index);
1034 SLV2ScalePoints points = slv2_port_get_scale_points(_impl->plugin, port);
1036 boost::shared_ptr<Plugin::ScalePoints> ret;
1037 if (!points) {
1038 return ret;
1041 ret = boost::shared_ptr<Plugin::ScalePoints>(new ScalePoints());
1043 for (unsigned i = 0; i < slv2_scale_points_size(points); ++i) {
1044 SLV2ScalePoint p = slv2_scale_points_get_at(points, i);
1045 SLV2Value label = slv2_scale_point_get_label(p);
1046 SLV2Value value = slv2_scale_point_get_value(p);
1047 if (label && (slv2_value_is_float(value) || slv2_value_is_int(value))) {
1048 ret->insert(make_pair(slv2_value_as_string(label),
1049 slv2_value_as_float(value)));
1053 slv2_scale_points_free(points);
1054 return ret;
1057 void
1058 LV2Plugin::run(pframes_t nframes)
1060 for (uint32_t i = 0; i < parameter_count(); ++i) {
1061 if (parameter_is_control(i) && parameter_is_input(i)) {
1062 _control_data[i] = _shadow_data[i];
1066 slv2_instance_run(_impl->instance, nframes);
1069 void
1070 LV2Plugin::latency_compute_run()
1072 if (!_latency_control_port) {
1073 return;
1076 // Run the plugin so that it can set its latency parameter
1078 activate();
1080 uint32_t port_index = 0;
1081 uint32_t in_index = 0;
1082 uint32_t out_index = 0;
1084 const framecnt_t bufsize = 1024;
1085 float buffer[bufsize];
1087 memset(buffer, 0, sizeof(float) * bufsize);
1089 // FIXME: Ensure plugins can handle in-place processing
1091 port_index = 0;
1093 while (port_index < parameter_count()) {
1094 if (parameter_is_audio(port_index)) {
1095 if (parameter_is_input(port_index)) {
1096 slv2_instance_connect_port(_impl->instance, port_index, buffer);
1097 in_index++;
1098 } else if (parameter_is_output(port_index)) {
1099 slv2_instance_connect_port(_impl->instance, port_index, buffer);
1100 out_index++;
1103 port_index++;
1106 run(bufsize);
1107 deactivate();
1110 LV2World::LV2World()
1111 : world(slv2_world_new())
1113 slv2_world_load_all(world);
1114 input_class = slv2_value_new_uri(world, SLV2_PORT_CLASS_INPUT);
1115 output_class = slv2_value_new_uri(world, SLV2_PORT_CLASS_OUTPUT);
1116 control_class = slv2_value_new_uri(world, SLV2_PORT_CLASS_CONTROL);
1117 audio_class = slv2_value_new_uri(world, SLV2_PORT_CLASS_AUDIO);
1118 event_class = slv2_value_new_uri(world, SLV2_PORT_CLASS_EVENT);
1119 midi_class = slv2_value_new_uri(world, SLV2_EVENT_CLASS_MIDI);
1120 in_place_broken = slv2_value_new_uri(world, SLV2_NAMESPACE_LV2 "inPlaceBroken");
1121 integer = slv2_value_new_uri(world, SLV2_NAMESPACE_LV2 "integer");
1122 toggled = slv2_value_new_uri(world, SLV2_NAMESPACE_LV2 "toggled");
1123 srate = slv2_value_new_uri(world, SLV2_NAMESPACE_LV2 "sampleRate");
1124 gtk_gui = slv2_value_new_uri(world, NS_UI "GtkUI");
1125 external_gui = slv2_value_new_uri(world, NS_UI "external");
1126 logarithmic = slv2_value_new_uri(world, "http://lv2plug.in/ns/dev/extportinfo#logarithmic");
1129 LV2World::~LV2World()
1131 slv2_value_free(input_class);
1132 slv2_value_free(output_class);
1133 slv2_value_free(control_class);
1134 slv2_value_free(audio_class);
1135 slv2_value_free(event_class);
1136 slv2_value_free(midi_class);
1137 slv2_value_free(in_place_broken);
1140 LV2PluginInfo::LV2PluginInfo (void* c_plugin)
1141 : _c_plugin(c_plugin)
1143 type = ARDOUR::LV2;
1146 LV2PluginInfo::~LV2PluginInfo()
1149 PluginPtr
1150 LV2PluginInfo::load(Session& session)
1152 try {
1153 PluginPtr plugin;
1155 plugin.reset(new LV2Plugin(session.engine(), session,
1156 (SLV2Plugin)_c_plugin,
1157 session.frame_rate()));
1159 plugin->set_info(PluginInfoPtr(new LV2PluginInfo(*this)));
1160 return plugin;
1161 } catch (failed_constructor& err) {
1162 return PluginPtr((Plugin*)0);
1165 return PluginPtr();
1168 PluginInfoList*
1169 LV2PluginInfo::discover()
1171 PluginInfoList* plugs = new PluginInfoList;
1172 SLV2Plugins plugins = slv2_world_get_all_plugins(_world.world);
1174 cerr << "LV2: Discovering " << slv2_plugins_size(plugins) << " plugins" << endl;
1176 for (unsigned i = 0; i < slv2_plugins_size(plugins); ++i) {
1177 SLV2Plugin p = slv2_plugins_get_at(plugins, i);
1178 LV2PluginInfoPtr info(new LV2PluginInfo(p));
1180 SLV2Value name = slv2_plugin_get_name(p);
1182 if (!name) {
1183 cerr << "LV2: invalid plugin\n";
1184 continue;
1187 info->type = LV2;
1189 info->name = string(slv2_value_as_string(name));
1190 slv2_value_free(name);
1192 SLV2PluginClass pclass = slv2_plugin_get_class(p);
1193 SLV2Value label = slv2_plugin_class_get_label(pclass);
1194 info->category = slv2_value_as_string(label);
1196 SLV2Value author_name = slv2_plugin_get_author_name(p);
1197 info->creator = author_name ? string(slv2_value_as_string(author_name)) : "Unknown";
1198 slv2_value_free(author_name);
1200 info->path = "/NOPATH"; // Meaningless for LV2
1202 info->n_inputs.set_audio(
1203 slv2_plugin_get_num_ports_of_class(
1204 p, _world.input_class, _world.audio_class, NULL));
1205 info->n_inputs.set_midi(
1206 slv2_plugin_get_num_ports_of_class(
1207 p, _world.input_class, _world.event_class, NULL));
1209 info->n_outputs.set_audio(
1210 slv2_plugin_get_num_ports_of_class(
1211 p, _world.output_class, _world.audio_class, NULL));
1212 info->n_outputs.set_midi(
1213 slv2_plugin_get_num_ports_of_class(
1214 p, _world.output_class, _world.event_class, NULL));
1216 info->unique_id = slv2_value_as_uri(slv2_plugin_get_uri(p));
1217 info->index = 0; // Meaningless for LV2
1219 plugs->push_back(info);
1222 cerr << "Done LV2 discovery" << endl;
1224 return plugs;