2 Copyright (C) 2008-2012 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.
28 #include <giomm/file.h>
30 #include <boost/utility.hpp>
32 #include "pbd/compose.h"
33 #include "pbd/error.h"
34 #include "pbd/xml++.h"
36 #include "libardour-config.h"
38 #include "ardour/types.h"
39 #include "ardour/audio_buffer.h"
40 #include "ardour/audioengine.h"
41 #include "ardour/debug.h"
42 #include "ardour/lv2_plugin.h"
43 #include "ardour/session.h"
44 #include "ardour/tempo.h"
45 #include "ardour/worker.h"
50 #include <lilv/lilv.h>
52 #include "lv2/lv2plug.in/ns/ext/atom/atom.h"
53 #include "lv2/lv2plug.in/ns/ext/port-props/port-props.h"
54 #include "lv2/lv2plug.in/ns/ext/presets/presets.h"
55 #include "lv2/lv2plug.in/ns/ext/state/state.h"
56 #include "lv2/lv2plug.in/ns/ext/time/time.h"
57 #include "lv2/lv2plug.in/ns/ext/worker/worker.h"
58 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
60 #include "lv2_evbuf.h"
63 #include <suil/suil.h>
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/midi#MidiEvent");
73 uint32_t LV2Plugin::_chunk_type = _uri_map.uri_to_id(
75 uint32_t LV2Plugin::_sequence_type = _uri_map.uri_to_id(
77 uint32_t LV2Plugin::_event_transfer_type = _uri_map.uri_to_id(
78 LV2_ATOM__eventTransfer);
79 uint32_t LV2Plugin::_path_type = _uri_map.uri_to_id(
82 class LV2World : boost::noncopyable {
89 LilvNode* atom_AtomPort;
91 LilvNode* atom_Sequence;
92 LilvNode* atom_bufferType;
93 LilvNode* atom_eventTransfer;
94 LilvNode* ev_EventPort;
95 LilvNode* ext_logarithmic;
96 LilvNode* lv2_AudioPort;
97 LilvNode* lv2_ControlPort;
98 LilvNode* lv2_InputPort;
99 LilvNode* lv2_OutputPort;
100 LilvNode* lv2_enumeration;
101 LilvNode* lv2_inPlaceBroken;
102 LilvNode* lv2_integer;
103 LilvNode* lv2_sampleRate;
104 LilvNode* lv2_toggled;
105 LilvNode* midi_MidiEvent;
106 LilvNode* rdfs_comment;
108 LilvNode* ui_external;
111 static LV2World _world;
113 /** Called by the plugin to schedule non-RT work. */
114 static LV2_Worker_Status
115 work_schedule(LV2_Worker_Schedule_Handle handle,
119 LV2Plugin* plugin = (LV2Plugin*)handle;
120 if (plugin->session().engine().freewheeling()) {
121 // Freewheeling, do the work immediately in this (audio) thread
122 return (LV2_Worker_Status)plugin->work(size, data);
124 // Enqueue message for the worker thread
125 return plugin->worker()->schedule(size, data) ?
126 LV2_WORKER_SUCCESS : LV2_WORKER_ERR_UNKNOWN;
130 /** Called by the plugin to respond to non-RT work. */
131 static LV2_Worker_Status
132 work_respond(LV2_Worker_Respond_Handle handle,
136 LV2Plugin* plugin = (LV2Plugin*)handle;
137 if (plugin->session().engine().freewheeling()) {
138 // Freewheeling, respond immediately in this (audio) thread
139 return (LV2_Worker_Status)plugin->work_response(size, data);
141 // Enqueue response for the worker
142 return plugin->worker()->respond(size, data) ?
143 LV2_WORKER_SUCCESS : LV2_WORKER_ERR_UNKNOWN;
147 struct LV2Plugin::Impl {
148 Impl() : plugin(0), ui(0), ui_type(0), name(0), author(0), instance(0)
153 /** Find the LV2 input port with the given designation.
154 * If found, bufptrs[port_index] will be set to bufptr.
156 LilvPort* designated_input (const char* uri, void** bufptrs[], void** bufptr);
160 const LilvNode* ui_type;
163 LilvInstance* instance;
164 LV2_Worker_Interface* work_iface;
168 LV2Plugin::LV2Plugin (AudioEngine& engine,
172 : Plugin (engine, session)
179 init(c_plugin, rate);
182 LV2Plugin::LV2Plugin (const LV2Plugin& other)
188 , _insert_id(other._insert_id)
190 init(other._impl->plugin, other._sample_rate);
192 for (uint32_t i = 0; i < parameter_count(); ++i) {
193 _control_data[i] = other._shadow_data[i];
194 _shadow_data[i] = other._shadow_data[i];
199 LV2Plugin::init(void* c_plugin, framecnt_t rate)
201 DEBUG_TRACE(DEBUG::LV2, "init\n");
203 _impl->plugin = (LilvPlugin*)c_plugin;
205 _impl->ui_type = NULL;
211 _bpm_control_port = 0;
212 _freewheel_control_port = 0;
213 _latency_control_port = 0;
215 _was_activated = false;
216 _has_state_interface = false;
218 _instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
219 _data_access_feature.URI = "http://lv2plug.in/ns/ext/data-access";
220 _make_path_feature.URI = LV2_STATE__makePath;
221 _work_schedule_feature.URI = LV2_WORKER__schedule;
222 _work_schedule_feature.data = NULL;
224 LilvPlugin* plugin = _impl->plugin;
226 LilvNode* state_iface_uri = lilv_new_uri(_world.world, LV2_STATE__interface);
227 LilvNode* state_uri = lilv_new_uri(_world.world, LV2_STATE_URI);
228 _has_state_interface =
229 // What plugins should have (lv2:extensionData state:Interface)
230 lilv_plugin_has_extension_data(plugin, state_iface_uri)
231 // What some outdated/incorrect ones have
232 || lilv_plugin_has_feature(plugin, state_uri);
233 lilv_node_free(state_uri);
234 lilv_node_free(state_iface_uri);
236 _features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * 8);
237 _features[0] = &_instance_access_feature;
238 _features[1] = &_data_access_feature;
239 _features[2] = &_make_path_feature;
240 _features[3] = _uri_map.uri_map_feature();
241 _features[4] = _uri_map.urid_map_feature();
242 _features[5] = _uri_map.urid_unmap_feature();
246 LV2_State_Make_Path* make_path = (LV2_State_Make_Path*)malloc(
247 sizeof(LV2_State_Make_Path));
248 make_path->handle = this;
249 make_path->path = &lv2_state_make_path;
250 _make_path_feature.data = make_path;
252 LilvNode* worker_schedule = lilv_new_uri(_world.world, LV2_WORKER__schedule);
253 if (lilv_plugin_has_feature(plugin, worker_schedule)) {
254 LV2_Worker_Schedule* schedule = (LV2_Worker_Schedule*)malloc(
255 sizeof(LV2_Worker_Schedule));
256 _worker = new Worker(this, 4096);
257 schedule->handle = this;
258 schedule->schedule_work = work_schedule;
259 _work_schedule_feature.data = schedule;
260 _features[6] = &_work_schedule_feature;
262 lilv_node_free(worker_schedule);
264 _impl->instance = lilv_plugin_instantiate(plugin, rate, _features);
265 _impl->name = lilv_plugin_get_name(plugin);
266 _impl->author = lilv_plugin_get_author_name(plugin);
268 if (_impl->instance == 0) {
269 error << _("LV2: Failed to instantiate plugin ") << uri() << endmsg;
270 throw failed_constructor();
273 _instance_access_feature.data = (void*)_impl->instance->lv2_handle;
274 _data_access_extension_data.extension_data = _impl->instance->lv2_descriptor->extension_data;
275 _data_access_feature.data = &_data_access_extension_data;
277 _impl->work_iface = (LV2_Worker_Interface*)extension_data(LV2_WORKER__interface);
279 if (lilv_plugin_has_feature(plugin, _world.lv2_inPlaceBroken)) {
280 error << string_compose(
281 _("LV2: \"%1\" cannot be used, since it cannot do inplace processing"),
282 lilv_node_as_string(_impl->name)) << endmsg;
283 lilv_node_free(_impl->name);
284 lilv_node_free(_impl->author);
285 throw failed_constructor();
290 const uint32_t num_ports = this->num_ports();
291 for (uint32_t i = 0; i < num_ports; ++i) {
292 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, i);
295 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_OutputPort)) {
296 flags |= PORT_OUTPUT;
297 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_InputPort)) {
300 error << string_compose(
301 "LV2: \"%1\" port %2 is neither input nor output",
302 lilv_node_as_string(_impl->name), i) << endmsg;
303 throw failed_constructor();
306 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_ControlPort)) {
307 flags |= PORT_CONTROL;
308 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_AudioPort)) {
310 } else if (lilv_port_is_a(_impl->plugin, port, _world.ev_EventPort)) {
312 } else if (lilv_port_is_a(_impl->plugin, port, _world.atom_AtomPort)) {
313 LilvNodes* buffer_types = lilv_port_get_value(
314 _impl->plugin, port, _world.atom_bufferType);
315 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
316 flags |= PORT_MESSAGE;
318 lilv_nodes_free(buffer_types);
320 error << string_compose(
321 "LV2: \"%1\" port %2 has no known data type",
322 lilv_node_as_string(_impl->name), i) << endmsg;
323 throw failed_constructor();
326 _port_flags.push_back(flags);
329 _control_data = new float[num_ports];
330 _shadow_data = new float[num_ports];
331 _defaults = new float[num_ports];
332 _ev_buffers = new LV2_Evbuf*[num_ports];
333 memset(_ev_buffers, 0, sizeof(LV2_Evbuf*) * num_ports);
335 const bool latent = lilv_plugin_has_latency(plugin);
336 const uint32_t latency_index = (latent)
337 ? lilv_plugin_get_latency_port_index(plugin)
340 // Build an array of pointers to special parameter buffers
341 void*** params = new void**[num_ports];
342 for (uint32_t i = 0; i < num_ports; ++i) {
345 _impl->designated_input (LV2_TIME__beatsPerMinute, params, (void**)&_bpm_control_port);
346 _impl->designated_input (LV2_CORE__freeWheeling, params, (void**)&_freewheel_control_port);
348 for (uint32_t i = 0; i < num_ports; ++i) {
349 const LilvPort* port = lilv_plugin_get_port_by_index(plugin, i);
350 const LilvNode* sym = lilv_port_get_symbol(plugin, port);
352 // Store index in map so we can look up index by symbol
353 _port_indices.insert(std::make_pair(lilv_node_as_string(sym), i));
355 // Get range and default value if applicable
356 if (parameter_is_control(i)) {
358 lilv_port_get_range(plugin, port, &def, NULL, NULL);
359 _defaults[i] = def ? lilv_node_as_float(def) : 0.0f;
360 if (lilv_port_has_property (plugin, port, _world.lv2_sampleRate)) {
361 _defaults[i] *= _session.frame_rate ();
365 lilv_instance_connect_port(_impl->instance, i, &_control_data[i]);
367 if (latent && i == latency_index) {
368 _latency_control_port = &_control_data[i];
369 *_latency_control_port = 0;
372 if (parameter_is_input(i)) {
373 _shadow_data[i] = default_value(i);
375 *params[i] = (void*)&_shadow_data[i];
385 LilvUIs* uis = lilv_plugin_get_uis(plugin);
386 if (lilv_uis_size(uis) > 0) {
388 // Look for embeddable UI
389 LILV_FOREACH(uis, u, uis) {
390 const LilvUI* this_ui = lilv_uis_get(uis, u);
391 const LilvNode* this_ui_type = NULL;
392 if (lilv_ui_is_supported(this_ui,
396 // TODO: Multiple UI support
398 _impl->ui_type = this_ui_type;
403 // Look for Gtk native UI
404 LILV_FOREACH(uis, i, uis) {
405 const LilvUI* ui = lilv_uis_get(uis, i);
406 if (lilv_ui_is_a(ui, _world.ui_GtkUI)) {
408 _impl->ui_type = _world.ui_GtkUI;
414 // If Gtk UI is not available, try to find external UI
416 LILV_FOREACH(uis, i, uis) {
417 const LilvUI* ui = lilv_uis_get(uis, i);
418 if (lilv_ui_is_a(ui, _world.ui_external)) {
420 _impl->ui_type = _world.ui_external;
427 latency_compute_run();
430 LV2Plugin::~LV2Plugin ()
432 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 destroy\n", name()));
437 lilv_instance_free(_impl->instance);
438 lilv_node_free(_impl->name);
439 lilv_node_free(_impl->author);
442 free(_make_path_feature.data);
443 free(_work_schedule_feature.data);
448 delete [] _control_data;
449 delete [] _shadow_data;
450 delete [] _ev_buffers;
454 LV2Plugin::is_external_ui() const
459 return lilv_ui_is_a(_impl->ui, _world.ui_external);
463 LV2Plugin::ui_is_resizable () const
465 const LilvNode* s = lilv_ui_get_uri(_impl->ui);
466 LilvNode* p = lilv_new_uri(_world.world, LV2_CORE__optionalFeature);
467 LilvNode* fs = lilv_new_uri(_world.world, LV2_UI__fixedSize);
468 LilvNode* nrs = lilv_new_uri(_world.world, LV2_UI__noUserResize);
470 LilvNodes* fs_matches = lilv_world_find_nodes(_world.world, s, p, fs);
471 LilvNodes* nrs_matches = lilv_world_find_nodes(_world.world, s, p, nrs);
473 lilv_nodes_free(nrs_matches);
474 lilv_nodes_free(fs_matches);
479 return !fs_matches && !nrs_matches;
483 LV2Plugin::unique_id() const
485 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
489 LV2Plugin::uri() const
491 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
495 LV2Plugin::label() const
497 return lilv_node_as_string(_impl->name);
501 LV2Plugin::name() const
503 return lilv_node_as_string(_impl->name);
507 LV2Plugin::maker() const
509 return _impl->author ? lilv_node_as_string (_impl->author) : "Unknown";
513 LV2Plugin::num_ports() const
515 return lilv_plugin_get_num_ports(_impl->plugin);
519 LV2Plugin::parameter_count() const
521 return lilv_plugin_get_num_ports(_impl->plugin);
525 LV2Plugin::default_value(uint32_t port)
527 return _defaults[port];
531 LV2Plugin::port_symbol(uint32_t index) const
533 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, index);
535 error << name() << ": Invalid port index " << index << endmsg;
538 const LilvNode* sym = lilv_port_get_symbol(_impl->plugin, port);
539 return lilv_node_as_string(sym);
543 LV2Plugin::port_index (const char* symbol) const
545 const map<string, uint32_t>::const_iterator i = _port_indices.find(symbol);
546 if (i != _port_indices.end()) {
549 warning << string_compose(_("LV2: Unknown port %1"), symbol) << endmsg;
555 LV2Plugin::set_parameter(uint32_t which, float val)
557 DEBUG_TRACE(DEBUG::LV2, string_compose(
558 "%1 set parameter %2 to %3\n", name(), which, val));
560 if (which < lilv_plugin_get_num_ports(_impl->plugin)) {
561 _shadow_data[which] = val;
563 warning << string_compose(
564 _("Illegal parameter number used with plugin \"%1\". "
565 "This is a bug in either %2 or the LV2 plugin <%3>"),
566 name(), PROGRAM_NAME, unique_id()) << endmsg;
569 Plugin::set_parameter(which, val);
573 LV2Plugin::get_parameter(uint32_t which) const
575 if (parameter_is_input(which)) {
576 return (float)_shadow_data[which];
578 return (float)_control_data[which];
584 LV2Plugin::get_docs() const
586 LilvNodes* comments = lilv_plugin_get_value(_impl->plugin, _world.rdfs_comment);
588 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
589 lilv_nodes_free(comments);
597 LV2Plugin::get_parameter_docs(uint32_t which) const
599 LilvNodes* comments = lilv_port_get_value(
601 lilv_plugin_get_port_by_index(_impl->plugin, which),
602 _world.rdfs_comment);
605 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
606 lilv_nodes_free(comments);
614 LV2Plugin::nth_parameter(uint32_t n, bool& ok) const
617 for (uint32_t c = 0, x = 0; x < lilv_plugin_get_num_ports(_impl->plugin); ++x) {
618 if (parameter_is_control(x)) {
630 LV2Plugin::extension_data (const char* uri) const
632 return lilv_instance_get_extension_data(_impl->instance, uri);
636 LV2Plugin::c_plugin ()
638 return _impl->plugin;
644 return (void*)_impl->ui;
648 LV2Plugin::c_ui_type ()
650 return (void*)_impl->ui_type;
653 /** Directory for all plugin state. */
655 LV2Plugin::plugin_dir() const
657 return Glib::build_filename(_session.plugins_dir(), _insert_id.to_s());
660 /** Directory for files created by the plugin (except during save). */
662 LV2Plugin::scratch_dir() const
664 return Glib::build_filename(plugin_dir(), "scratch");
667 /** Directory for snapshots of files in the scratch directory. */
669 LV2Plugin::file_dir() const
671 return Glib::build_filename(plugin_dir(), "files");
674 /** Directory to save state snapshot version @c num into. */
676 LV2Plugin::state_dir(unsigned num) const
678 return Glib::build_filename(plugin_dir(), string_compose("state%1", num));
681 /** Implementation of state:makePath for files created at instantiation time.
682 * Note this is not used for files created at save time (Lilv deals with that).
685 LV2Plugin::lv2_state_make_path(LV2_State_Make_Path_Handle handle,
688 LV2Plugin* me = (LV2Plugin*)handle;
689 if (me->_insert_id == PBD::ID("0")) {
690 warning << string_compose(
691 "File path \"%1\" requested but LV2 %2 has no insert ID",
692 path, me->name()) << endmsg;
693 return g_strdup(path);
696 const std::string abs_path = Glib::build_filename(me->scratch_dir(), path);
697 const std::string dirname = Glib::path_get_dirname(abs_path);
698 g_mkdir_with_parents(dirname.c_str(), 0744);
700 DEBUG_TRACE(DEBUG::LV2, string_compose("new file path %1 => %2\n",
703 std::cerr << "MAKE PATH " << path
704 << " => " << g_strndup(abs_path.c_str(), abs_path.length())
706 return g_strndup(abs_path.c_str(), abs_path.length());
710 remove_directory(const std::string& path)
712 if (!Glib::file_test(path, Glib::FILE_TEST_IS_DIR)) {
713 warning << string_compose("\"%1\" is not a directory", path) << endmsg;
717 Glib::RefPtr<Gio::File> dir = Gio::File::create_for_path(path);
718 Glib::RefPtr<Gio::FileEnumerator> e = dir->enumerate_children();
719 Glib::RefPtr<Gio::FileInfo> fi;
720 while ((fi = e->next_file())) {
721 if (fi->get_type() == Gio::FILE_TYPE_DIRECTORY) {
722 remove_directory(fi->get_name());
724 dir->get_child(fi->get_name())->remove();
731 LV2Plugin::add_state(XMLNode* root) const
733 assert(_insert_id != PBD::ID("0"));
737 LocaleGuard lg(X_("POSIX"));
739 for (uint32_t i = 0; i < parameter_count(); ++i) {
740 if (parameter_is_input(i) && parameter_is_control(i)) {
741 child = new XMLNode("Port");
742 child->add_property("symbol", port_symbol(i));
743 snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
744 child->add_property("value", string(buf));
745 root->add_child_nocopy(*child);
749 if (_has_state_interface) {
750 cout << "LV2 " << name() << " has state interface" << endl;
751 // Provisionally increment state version and create directory
752 const std::string new_dir = state_dir(++_state_version);
753 g_mkdir_with_parents(new_dir.c_str(), 0744);
755 cout << "NEW DIR: " << new_dir << endl;
757 LilvState* state = lilv_state_new_from_instance(
761 scratch_dir().c_str(),
763 _session.externals_dir().c_str(),
770 if (!_impl->state || !lilv_state_equals(state, _impl->state)) {
771 lilv_state_save(_world.world,
773 _uri_map.urid_unmap(),
779 lilv_state_free(_impl->state);
780 _impl->state = state;
782 cout << "Saved LV2 state to " << state_dir(_state_version) << endl;
784 // State is identical, decrement version and nuke directory
785 cout << "LV2 state identical, not saving" << endl;
786 lilv_state_free(state);
787 remove_directory(new_dir);
791 root->add_property("state-dir", string_compose("state%1", _state_version));
793 cout << "LV2 " << name() << " has no state interface." << endl;
797 static inline const LilvNode*
798 get_value(LilvWorld* world, const LilvNode* subject, const LilvNode* predicate)
800 LilvNodes* vs = lilv_world_find_nodes(world, subject, predicate, NULL);
801 return vs ? lilv_nodes_get_first(vs) : NULL;
805 LV2Plugin::find_presets()
807 LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
808 LilvNode* pset_Preset = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
809 LilvNode* rdfs_label = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
811 LilvNodes* presets = lilv_plugin_get_related(_impl->plugin, pset_Preset);
812 LILV_FOREACH(nodes, i, presets) {
813 const LilvNode* preset = lilv_nodes_get(presets, i);
814 lilv_world_load_resource(_world.world, preset);
815 const LilvNode* name = get_value(_world.world, preset, rdfs_label);
817 _presets.insert(std::make_pair(lilv_node_as_string(preset),
818 Plugin::PresetRecord(
819 lilv_node_as_string(preset),
820 lilv_node_as_string(name))));
822 warning << string_compose(
823 _("Plugin \"%1\% preset \"%2%\" is missing a label\n"),
824 lilv_node_as_string(lilv_plugin_get_uri(_impl->plugin)),
825 lilv_node_as_string(preset)) << endmsg;
828 lilv_nodes_free(presets);
830 lilv_node_free(rdfs_label);
831 lilv_node_free(pset_Preset);
832 lilv_node_free(lv2_appliesTo);
836 LV2Plugin::load_preset(PresetRecord r)
838 Plugin::load_preset(r);
840 std::map<std::string,uint32_t>::iterator it;
842 LilvNode* lv2_port = lilv_new_uri(_world.world, LILV_NS_LV2 "port");
843 LilvNode* lv2_symbol = lilv_new_uri(_world.world, LILV_NS_LV2 "symbol");
844 LilvNode* preset = lilv_new_uri(_world.world, r.uri.c_str());
845 LilvNode* pset_value = lilv_new_uri(_world.world, LV2_PRESETS__value);
847 LilvNodes* ports = lilv_world_find_nodes(_world.world, preset, lv2_port, NULL);
848 LILV_FOREACH(nodes, i, ports) {
849 const LilvNode* port = lilv_nodes_get(ports, i);
850 const LilvNode* symbol = get_value(_world.world, port, lv2_symbol);
851 const LilvNode* value = get_value(_world.world, port, pset_value);
852 if (value && lilv_node_is_float(value)) {
853 it = _port_indices.find(lilv_node_as_string(symbol));
854 if (it != _port_indices.end())
855 set_parameter(it->second,lilv_node_as_float(value));
858 lilv_nodes_free(ports);
860 lilv_node_free(pset_value);
861 lilv_node_free(preset);
862 lilv_node_free(lv2_symbol);
863 lilv_node_free(lv2_port);
869 LV2Plugin::do_save_preset(string /*name*/)
875 LV2Plugin::do_remove_preset(string /*name*/)
879 LV2Plugin::has_editor() const
881 return _impl->ui != NULL;
885 LV2Plugin::has_message_output() const
887 for (uint32_t i = 0; i < num_ports(); ++i) {
888 if ((_port_flags[i] & PORT_MESSAGE) && _port_flags[i] & PORT_OUTPUT) {
896 LV2Plugin::atom_eventTransfer() const
898 return _event_transfer_type;
902 LV2Plugin::write_to(RingBuffer<uint8_t>* dest,
908 const uint32_t buf_size = sizeof(UIMessage) + size;
909 uint8_t buf[buf_size];
911 UIMessage* msg = (UIMessage*)buf;
913 msg->protocol = protocol;
915 memcpy(msg + 1, body, size);
917 if (dest->write(buf, buf_size) != buf_size) {
918 error << "Error writing to UI=>Plugin RingBuffer" << endmsg;
923 LV2Plugin::write_from_ui(uint32_t index,
929 _from_ui = new RingBuffer<uint8_t>(4096);
932 write_to(_from_ui, index, protocol, size, body);
936 LV2Plugin::write_to_ui(uint32_t index,
941 std::cerr << "WRITE TO UI" << std::endl;
942 write_to(_to_ui, index, protocol, size, body);
946 LV2Plugin::enable_ui_emmission()
949 _to_ui = new RingBuffer<uint8_t>(4096);
954 LV2Plugin::emit_to_ui(void* controller, UIMessageSink sink)
960 uint32_t read_space = _to_ui->read_space();
961 while (read_space > sizeof(UIMessage)) {
963 if (_to_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
964 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
967 uint8_t body[msg.size];
968 if (_to_ui->read(body, msg.size) != msg.size) {
969 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
973 sink(controller, msg.index, msg.size, msg.protocol, body);
975 read_space -= sizeof(msg) + msg.size;
980 LV2Plugin::work(uint32_t size, const void* data)
982 return _impl->work_iface->work(
983 _impl->instance->lv2_handle, work_respond, this, size, data);
987 LV2Plugin::work_response(uint32_t size, const void* data)
989 return _impl->work_iface->work_response(
990 _impl->instance->lv2_handle, size, data);
994 LV2Plugin::set_insert_info(const PluginInsert* insert)
996 _insert_id = insert->id();
1000 LV2Plugin::set_state(const XMLNode& node, int version)
1003 const XMLProperty* prop;
1004 XMLNodeConstIterator iter;
1009 LocaleGuard lg(X_("POSIX"));
1011 if (node.name() != state_node_name()) {
1012 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
1016 if (version < 3000) {
1017 nodes = node.children("port");
1019 nodes = node.children("Port");
1022 for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
1026 if ((prop = child->property("symbol")) != 0) {
1027 sym = prop->value().c_str();
1029 warning << _("LV2: port has no symbol, ignored") << endmsg;
1033 map<string, uint32_t>::iterator i = _port_indices.find(sym);
1035 if (i != _port_indices.end()) {
1036 port_id = i->second;
1038 warning << _("LV2: port has unknown index, ignored") << endmsg;
1042 if ((prop = child->property("value")) != 0) {
1043 value = prop->value().c_str();
1045 warning << _("LV2: port has no value, ignored") << endmsg;
1049 set_parameter(port_id, atof(value));
1053 if ((prop = node.property("state-dir")) != 0) {
1054 if (sscanf(prop->value().c_str(), "state%u", &_state_version) != 1) {
1055 error << string_compose(
1056 "LV2: failed to parse state version from \"%1\"",
1057 prop->value()) << endmsg;
1060 std::string state_file = Glib::build_filename(
1062 Glib::build_filename(prop->value(), "state.ttl"));
1064 cout << "Loading LV2 state from " << state_file << endl;
1065 LilvState* state = lilv_state_new_from_file(
1066 _world.world, _uri_map.urid_map(), NULL, state_file.c_str());
1068 lilv_state_restore(state, _impl->instance, NULL, NULL, 0, NULL);
1071 latency_compute_run();
1073 return Plugin::set_state(node, version);
1077 LV2Plugin::get_parameter_descriptor(uint32_t which, ParameterDescriptor& desc) const
1079 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, which);
1081 LilvNode *def, *min, *max;
1082 lilv_port_get_range(_impl->plugin, port, &def, &min, &max);
1084 desc.integer_step = lilv_port_has_property(_impl->plugin, port, _world.lv2_integer);
1085 desc.toggled = lilv_port_has_property(_impl->plugin, port, _world.lv2_toggled);
1086 desc.logarithmic = lilv_port_has_property(_impl->plugin, port, _world.ext_logarithmic);
1087 desc.sr_dependent = lilv_port_has_property(_impl->plugin, port, _world.lv2_sampleRate);
1088 desc.label = lilv_node_as_string(lilv_port_get_name(_impl->plugin, port));
1089 desc.lower = min ? lilv_node_as_float(min) : 0.0f;
1090 desc.upper = max ? lilv_node_as_float(max) : 1.0f;
1091 if (desc.sr_dependent) {
1092 desc.lower *= _session.frame_rate ();
1093 desc.upper *= _session.frame_rate ();
1096 desc.min_unbound = false; // TODO: LV2 extension required
1097 desc.max_unbound = false; // TODO: LV2 extension required
1099 if (desc.integer_step) {
1101 desc.smallstep = 0.1;
1102 desc.largestep = 10.0;
1104 const float delta = desc.upper - desc.lower;
1105 desc.step = delta / 1000.0f;
1106 desc.smallstep = delta / 10000.0f;
1107 desc.largestep = delta / 10.0f;
1110 desc.enumeration = lilv_port_has_property(_impl->plugin, port, _world.lv2_enumeration);
1112 lilv_node_free(def);
1113 lilv_node_free(min);
1114 lilv_node_free(max);
1120 LV2Plugin::describe_parameter(Evoral::Parameter which)
1122 if (( which.type() == PluginAutomation) && ( which.id() < parameter_count()) ) {
1123 LilvNode* name = lilv_port_get_name(_impl->plugin,
1124 lilv_plugin_get_port_by_index(_impl->plugin, which.id()));
1125 string ret(lilv_node_as_string(name));
1126 lilv_node_free(name);
1134 LV2Plugin::signal_latency() const
1136 if (_latency_control_port) {
1137 return (framecnt_t)floor(*_latency_control_port);
1143 set<Evoral::Parameter>
1144 LV2Plugin::automatable() const
1146 set<Evoral::Parameter> ret;
1148 for (uint32_t i = 0; i < parameter_count(); ++i) {
1149 if (parameter_is_input(i) && parameter_is_control(i)) {
1150 ret.insert(ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
1158 LV2Plugin::activate()
1160 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 activate\n", name()));
1162 if (!_was_activated) {
1163 lilv_instance_activate(_impl->instance);
1164 _was_activated = true;
1169 LV2Plugin::deactivate()
1171 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 deactivate\n", name()));
1173 if (_was_activated) {
1174 lilv_instance_deactivate(_impl->instance);
1175 _was_activated = false;
1180 LV2Plugin::cleanup()
1182 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 cleanup\n", name()));
1186 lilv_instance_free(_impl->instance);
1187 _impl->instance = NULL;
1191 LV2Plugin::connect_and_run(BufferSet& bufs,
1192 ChanMapping in_map, ChanMapping out_map,
1193 pframes_t nframes, framecnt_t offset)
1195 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 run %2 offset %3\n", name(), nframes, offset));
1196 Plugin::connect_and_run(bufs, in_map, out_map, nframes, offset);
1198 cycles_t then = get_cycles();
1200 if (_freewheel_control_port) {
1201 *_freewheel_control_port = _session.engine().freewheeling ();
1204 if (_bpm_control_port) {
1205 TempoMap& tmap (_session.tempo_map ());
1206 Tempo tempo = tmap.tempo_at (_session.transport_frame () + offset);
1207 *_bpm_control_port = tempo.beats_per_minute ();
1210 ChanCount bufs_count;
1211 bufs_count.set(DataType::AUDIO, 1);
1212 bufs_count.set(DataType::MIDI, 1);
1213 BufferSet& silent_bufs = _session.get_silent_buffers(bufs_count);
1214 BufferSet& scratch_bufs = _session.get_silent_buffers(bufs_count);
1215 uint32_t const num_ports = parameter_count();
1217 uint32_t audio_in_index = 0;
1218 uint32_t audio_out_index = 0;
1219 uint32_t midi_in_index = 0;
1220 uint32_t midi_out_index = 0;
1222 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
1225 PortFlags flags = _port_flags[port_index];
1226 if (flags & PORT_AUDIO) {
1227 if (flags & PORT_INPUT) {
1228 index = in_map.get(DataType::AUDIO, audio_in_index++, &valid);
1230 ? bufs.get_audio(index).data(offset)
1231 : silent_bufs.get_audio(0).data(offset);
1233 index = out_map.get(DataType::AUDIO, audio_out_index++, &valid);
1235 ? bufs.get_audio(index).data(offset)
1236 : scratch_bufs.get_audio(0).data(offset);
1238 } else if (flags & (PORT_EVENT|PORT_MESSAGE)) {
1239 /* FIXME: The checks here for bufs.count().n_midi() > index shouldn't
1240 be necessary, but the mapping is illegal in some cases. Ideally
1241 that should be fixed, but this is easier...
1243 if (flags & PORT_INPUT) {
1244 index = in_map.get(DataType::MIDI, midi_in_index++, &valid);
1245 _ev_buffers[port_index] = (valid && bufs.count().n_midi() > index)
1246 ? bufs.get_lv2_midi(true, index, flags & PORT_EVENT)
1247 : silent_bufs.get_lv2_midi(true, 0, flags & PORT_EVENT);
1248 buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
1250 index = out_map.get(DataType::MIDI, midi_out_index++, &valid);
1251 _ev_buffers[port_index] = (valid && bufs.count().n_midi() > index)
1252 ? bufs.get_lv2_midi(false, index, flags & PORT_EVENT)
1253 : scratch_bufs.get_lv2_midi(false, 0, flags & PORT_EVENT);
1254 buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
1257 continue; // Control port, leave buffer alone
1259 lilv_instance_connect_port(_impl->instance, port_index, buf);
1262 // Read messages from UI and push into appropriate buffers
1264 uint32_t read_space = _from_ui->read_space();
1265 while (read_space > sizeof(UIMessage)) {
1267 if (_from_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
1268 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
1271 uint8_t body[msg.size];
1272 if (_from_ui->read(body, msg.size) != msg.size) {
1273 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
1276 if (msg.protocol == _event_transfer_type) {
1277 LV2_Evbuf* buf = _ev_buffers[msg.index];
1278 LV2_Evbuf_Iterator i = lv2_evbuf_end(buf);
1279 const LV2_Atom* const atom = (const LV2_Atom*)body;
1280 lv2_evbuf_write(&i, nframes, 0, atom->type, atom->size,
1281 (const uint8_t*)LV2_ATOM_BODY(atom));
1283 error << "Received unknown message type from UI" << endmsg;
1285 read_space -= sizeof(UIMessage) + msg.size;
1292 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
1293 PortFlags flags = _port_flags[port_index];
1295 // Flush MIDI (write back to Ardour MIDI buffers)
1296 if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_MESSAGE))) {
1297 const uint32_t buf_index = out_map.get(
1298 DataType::MIDI, midi_out_index++, &valid);
1300 bufs.flush_lv2_midi(true, buf_index);
1304 // Write messages to UI
1305 if (_to_ui && (flags & PORT_OUTPUT) && (flags & PORT_MESSAGE)) {
1306 LV2_Evbuf* buf = _ev_buffers[port_index];
1307 for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(buf);
1308 lv2_evbuf_is_valid(i);
1309 i = lv2_evbuf_next(i)) {
1310 uint32_t frames, subframes, type, size;
1312 lv2_evbuf_get(i, &frames, &subframes, &type, &size, &data);
1313 write_to_ui(port_index, _event_transfer_type,
1314 size + sizeof(LV2_Atom),
1315 data - sizeof(LV2_Atom));
1320 cycles_t now = get_cycles();
1321 set_cycles((uint32_t)(now - then));
1327 LV2Plugin::parameter_is_control(uint32_t param) const
1329 assert(param < _port_flags.size());
1330 return _port_flags[param] & PORT_CONTROL;
1334 LV2Plugin::parameter_is_audio(uint32_t param) const
1336 assert(param < _port_flags.size());
1337 return _port_flags[param] & PORT_AUDIO;
1341 LV2Plugin::parameter_is_event(uint32_t param) const
1343 assert(param < _port_flags.size());
1344 return _port_flags[param] & PORT_EVENT;
1348 LV2Plugin::parameter_is_output(uint32_t param) const
1350 assert(param < _port_flags.size());
1351 return _port_flags[param] & PORT_OUTPUT;
1355 LV2Plugin::parameter_is_input(uint32_t param) const
1357 assert(param < _port_flags.size());
1358 return _port_flags[param] & PORT_INPUT;
1362 LV2Plugin::print_parameter(uint32_t param, char* buf, uint32_t len) const
1365 if (param < parameter_count()) {
1366 snprintf(buf, len, "%.3f", get_parameter(param));
1373 boost::shared_ptr<Plugin::ScalePoints>
1374 LV2Plugin::get_scale_points(uint32_t port_index) const
1376 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, port_index);
1377 LilvScalePoints* points = lilv_port_get_scale_points(_impl->plugin, port);
1379 boost::shared_ptr<Plugin::ScalePoints> ret;
1384 ret = boost::shared_ptr<Plugin::ScalePoints>(new ScalePoints());
1386 LILV_FOREACH(scale_points, i, points) {
1387 const LilvScalePoint* p = lilv_scale_points_get(points, i);
1388 const LilvNode* label = lilv_scale_point_get_label(p);
1389 const LilvNode* value = lilv_scale_point_get_value(p);
1390 if (label && (lilv_node_is_float(value) || lilv_node_is_int(value))) {
1391 ret->insert(make_pair(lilv_node_as_string(label),
1392 lilv_node_as_float(value)));
1396 lilv_scale_points_free(points);
1401 LV2Plugin::run(pframes_t nframes)
1403 uint32_t const N = parameter_count();
1404 for (uint32_t i = 0; i < N; ++i) {
1405 if (parameter_is_control(i) && parameter_is_input(i)) {
1406 _control_data[i] = _shadow_data[i];
1410 lilv_instance_run(_impl->instance, nframes);
1412 if (_impl->work_iface) {
1413 _worker->emit_responses();
1414 if (_impl->work_iface->end_run) {
1415 _impl->work_iface->end_run(_impl->instance->lv2_handle);
1421 LV2Plugin::latency_compute_run()
1423 if (!_latency_control_port) {
1427 // Run the plugin so that it can set its latency parameter
1431 uint32_t port_index = 0;
1432 uint32_t in_index = 0;
1433 uint32_t out_index = 0;
1435 const framecnt_t bufsize = 1024;
1436 float buffer[bufsize];
1438 memset(buffer, 0, sizeof(float) * bufsize);
1440 // FIXME: Ensure plugins can handle in-place processing
1444 while (port_index < parameter_count()) {
1445 if (parameter_is_audio(port_index)) {
1446 if (parameter_is_input(port_index)) {
1447 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1449 } else if (parameter_is_output(port_index)) {
1450 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1462 LV2Plugin::Impl::designated_input (const char* uri, void** bufptrs[], void** bufptr)
1464 LilvPort* port = NULL;
1465 LilvNode* designation = lilv_new_uri(_world.world, uri);
1466 port = lilv_plugin_get_port_by_designation(
1467 plugin, _world.lv2_InputPort, designation);
1468 lilv_node_free(designation);
1470 bufptrs[lilv_port_get_index(plugin, port)] = bufptr;
1475 LV2World::LV2World()
1476 : world(lilv_world_new())
1478 lilv_world_load_all(world);
1479 atom_AtomPort = lilv_new_uri(world, LV2_ATOM__AtomPort);
1480 atom_Chunk = lilv_new_uri(world, LV2_ATOM__Chunk);
1481 atom_Sequence = lilv_new_uri(world, LV2_ATOM__Sequence);
1482 atom_bufferType = lilv_new_uri(world, LV2_ATOM__bufferType);
1483 atom_eventTransfer = lilv_new_uri(world, LV2_ATOM__eventTransfer);
1484 ev_EventPort = lilv_new_uri(world, LILV_URI_EVENT_PORT);
1485 ext_logarithmic = lilv_new_uri(world, LV2_PORT_PROPS__logarithmic);
1486 lv2_AudioPort = lilv_new_uri(world, LILV_URI_AUDIO_PORT);
1487 lv2_ControlPort = lilv_new_uri(world, LILV_URI_CONTROL_PORT);
1488 lv2_InputPort = lilv_new_uri(world, LILV_URI_INPUT_PORT);
1489 lv2_OutputPort = lilv_new_uri(world, LILV_URI_OUTPUT_PORT);
1490 lv2_inPlaceBroken = lilv_new_uri(world, LV2_CORE__inPlaceBroken);
1491 lv2_integer = lilv_new_uri(world, LV2_CORE__integer);
1492 lv2_sampleRate = lilv_new_uri(world, LV2_CORE__sampleRate);
1493 lv2_toggled = lilv_new_uri(world, LV2_CORE__toggled);
1494 lv2_enumeration = lilv_new_uri(world, LV2_CORE__enumeration);
1495 midi_MidiEvent = lilv_new_uri(world, LILV_URI_MIDI_EVENT);
1496 rdfs_comment = lilv_new_uri(world, LILV_NS_RDFS "comment");
1497 ui_GtkUI = lilv_new_uri(world, LV2_UI__GtkUI);
1498 ui_external = lilv_new_uri(world, "http://lv2plug.in/ns/extensions/ui#external");
1501 LV2World::~LV2World()
1503 lilv_node_free(ui_external);
1504 lilv_node_free(ui_GtkUI);
1505 lilv_node_free(midi_MidiEvent);
1506 lilv_node_free(lv2_toggled);
1507 lilv_node_free(lv2_sampleRate);
1508 lilv_node_free(lv2_integer);
1509 lilv_node_free(lv2_inPlaceBroken);
1510 lilv_node_free(lv2_OutputPort);
1511 lilv_node_free(lv2_InputPort);
1512 lilv_node_free(lv2_ControlPort);
1513 lilv_node_free(lv2_AudioPort);
1514 lilv_node_free(ext_logarithmic);
1515 lilv_node_free(ev_EventPort);
1516 lilv_node_free(atom_eventTransfer);
1517 lilv_node_free(atom_bufferType);
1518 lilv_node_free(atom_Sequence);
1519 lilv_node_free(atom_Chunk);
1520 lilv_node_free(atom_AtomPort);
1523 LV2PluginInfo::LV2PluginInfo (void* c_plugin)
1524 : _c_plugin(c_plugin)
1529 LV2PluginInfo::~LV2PluginInfo()
1533 LV2PluginInfo::load(Session& session)
1538 plugin.reset(new LV2Plugin(session.engine(), session,
1539 (LilvPlugin*)_c_plugin,
1540 session.frame_rate()));
1542 plugin->set_info(PluginInfoPtr(new LV2PluginInfo(*this)));
1544 } catch (failed_constructor& err) {
1545 return PluginPtr((Plugin*)0);
1552 LV2PluginInfo::discover()
1554 PluginInfoList* plugs = new PluginInfoList;
1555 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
1557 cerr << "LV2: Discovering " << lilv_plugins_size(plugins) << " plugins" << endl;
1559 LILV_FOREACH(plugins, i, plugins) {
1560 const LilvPlugin* p = lilv_plugins_get(plugins, i);
1561 LV2PluginInfoPtr info(new LV2PluginInfo((void*)p));
1563 LilvNode* name = lilv_plugin_get_name(p);
1565 cerr << "LV2: invalid plugin\n";
1571 info->name = string(lilv_node_as_string(name));
1572 lilv_node_free(name);
1574 const LilvPluginClass* pclass = lilv_plugin_get_class(p);
1575 const LilvNode* label = lilv_plugin_class_get_label(pclass);
1576 info->category = lilv_node_as_string(label);
1578 LilvNode* author_name = lilv_plugin_get_author_name(p);
1579 info->creator = author_name ? string(lilv_node_as_string(author_name)) : "Unknown";
1580 lilv_node_free(author_name);
1582 info->path = "/NOPATH"; // Meaningless for LV2
1584 info->n_inputs.set_audio(
1585 lilv_plugin_get_num_ports_of_class(
1586 p, _world.lv2_InputPort, _world.lv2_AudioPort, NULL));
1587 info->n_inputs.set_midi(
1588 lilv_plugin_get_num_ports_of_class(
1589 p, _world.lv2_InputPort, _world.ev_EventPort, NULL)
1590 + lilv_plugin_get_num_ports_of_class(
1591 p, _world.lv2_InputPort, _world.atom_AtomPort, NULL));
1593 info->n_outputs.set_audio(
1594 lilv_plugin_get_num_ports_of_class(
1595 p, _world.lv2_OutputPort, _world.lv2_AudioPort, NULL));
1596 info->n_outputs.set_midi(
1597 lilv_plugin_get_num_ports_of_class(
1598 p, _world.lv2_OutputPort, _world.ev_EventPort, NULL)
1599 + lilv_plugin_get_num_ports_of_class(
1600 p, _world.lv2_OutputPort, _world.atom_AtomPort, NULL));
1602 info->unique_id = lilv_node_as_uri(lilv_plugin_get_uri(p));
1603 info->index = 0; // Meaningless for LV2
1605 plugs->push_back(info);
1608 cerr << "Done LV2 discovery" << endl;