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.
29 #include "pbd/gstdio_compat.h"
30 #include <glib/gprintf.h>
33 #include <boost/utility.hpp>
35 #include "pbd/file_utils.h"
36 #include "pbd/stl_delete.h"
37 #include "pbd/compose.h"
38 #include "pbd/error.h"
39 #include "pbd/replace_all.h"
40 #include "pbd/xml++.h"
42 #include "libardour-config.h"
44 #include "ardour/audio_buffer.h"
45 #include "ardour/audioengine.h"
46 #include "ardour/debug.h"
47 #include "ardour/lv2_plugin.h"
48 #include "ardour/session.h"
49 #include "ardour/tempo.h"
50 #include "ardour/types.h"
51 #include "ardour/utils.h"
52 #include "ardour/worker.h"
53 #include "ardour/search_paths.h"
58 #include <lilv/lilv.h>
60 #include "lv2/lv2plug.in/ns/ext/atom/atom.h"
61 #include "lv2/lv2plug.in/ns/ext/atom/forge.h"
62 #include "lv2/lv2plug.in/ns/ext/log/log.h"
63 #include "lv2/lv2plug.in/ns/ext/midi/midi.h"
64 #include "lv2/lv2plug.in/ns/ext/port-props/port-props.h"
65 #include "lv2/lv2plug.in/ns/ext/presets/presets.h"
66 #include "lv2/lv2plug.in/ns/ext/state/state.h"
67 #include "lv2/lv2plug.in/ns/ext/time/time.h"
68 #include "lv2/lv2plug.in/ns/ext/worker/worker.h"
69 #include "lv2/lv2plug.in/ns/ext/resize-port/resize-port.h"
70 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
71 #include "lv2/lv2plug.in/ns/extensions/units/units.h"
72 #include "lv2/lv2plug.in/ns/ext/patch/patch.h"
74 #include "lv2/lv2plug.in/ns/ext/buf-size/buf-size.h"
75 #include "lv2/lv2plug.in/ns/ext/options/options.h"
78 #include "lv2_evbuf.h"
81 #include <suil/suil.h>
84 // Compatibility for old LV2
85 #ifndef LV2_ATOM_CONTENTS_CONST
86 #define LV2_ATOM_CONTENTS_CONST(type, atom) \
87 ((const void*)((const uint8_t*)(atom) + sizeof(type)))
89 #ifndef LV2_ATOM_BODY_CONST
90 #define LV2_ATOM_BODY_CONST(atom) LV2_ATOM_CONTENTS_CONST(LV2_Atom, atom)
92 #ifndef LV2_PATCH__property
93 #define LV2_PATCH__property LV2_PATCH_PREFIX "property"
95 #ifndef LV2_PATCH__value
96 #define LV2_PATCH__value LV2_PATCH_PREFIX "value"
98 #ifndef LV2_PATCH__writable
99 #define LV2_PATCH__writable LV2_PATCH_PREFIX "writable"
102 /** The number of MIDI buffers that will fit in a UI/worker comm buffer.
103 This needs to be roughly the number of cycles the UI will get around to
104 actually processing the traffic. Lower values are flakier but save memory.
106 static const size_t NBUFS = 4;
109 using namespace ARDOUR;
112 class LV2World : boost::noncopyable {
117 void load_bundled_plugins(bool verbose=false);
121 LilvNode* atom_AtomPort;
122 LilvNode* atom_Chunk;
123 LilvNode* atom_Sequence;
124 LilvNode* atom_bufferType;
125 LilvNode* atom_eventTransfer;
126 LilvNode* atom_supports;
127 LilvNode* ev_EventPort;
128 LilvNode* ext_logarithmic;
129 LilvNode* ext_notOnGUI;
130 LilvNode* ext_expensive;
131 LilvNode* ext_causesArtifacts;
132 LilvNode* ext_notAutomatic;
133 LilvNode* ext_rangeSteps;
134 LilvNode* lv2_AudioPort;
135 LilvNode* lv2_ControlPort;
136 LilvNode* lv2_InputPort;
137 LilvNode* lv2_OutputPort;
138 LilvNode* lv2_enumeration;
139 LilvNode* lv2_freewheeling;
140 LilvNode* lv2_inPlaceBroken;
141 LilvNode* lv2_isSideChain;
142 LilvNode* lv2_integer;
143 LilvNode* lv2_default;
144 LilvNode* lv2_minimum;
145 LilvNode* lv2_maximum;
146 LilvNode* lv2_reportsLatency;
147 LilvNode* lv2_sampleRate;
148 LilvNode* lv2_toggled;
149 LilvNode* midi_MidiEvent;
150 LilvNode* rdfs_comment;
151 LilvNode* rdfs_label;
152 LilvNode* rdfs_range;
153 LilvNode* rsz_minimumSize;
154 LilvNode* time_Position;
156 LilvNode* ui_external;
157 LilvNode* ui_externalkx;
160 LilvNode* units_unit;
161 LilvNode* units_render;
162 LilvNode* units_midiNote;
163 LilvNode* patch_writable;
164 LilvNode* patch_Message;
165 #ifdef HAVE_LV2_1_2_0
166 LilvNode* bufz_powerOf2BlockLength;
167 LilvNode* bufz_fixedBlockLength;
168 LilvNode* bufz_nominalBlockLength;
171 #ifdef HAVE_LV2_1_10_0
173 LilvNode* atom_float;
174 LilvNode* atom_object; // new in 1.8
175 LilvNode* atom_vector;
178 LilvNode* lv2_noSampleAccurateCtrl;
179 LilvNode* auto_can_write_automatation; // lv2:optionalFeature
180 LilvNode* auto_automation_control; // atom:supports
181 LilvNode* auto_automation_controlled; // lv2:portProperty
182 LilvNode* auto_automation_controller; // lv2:portProperty
186 bool _bundle_checked;
189 static LV2World _world;
191 /* worker extension */
193 /** Called by the plugin to schedule non-RT work. */
194 static LV2_Worker_Status
195 work_schedule(LV2_Worker_Schedule_Handle handle,
199 LV2Plugin* plugin = (LV2Plugin*)handle;
200 if (plugin->session().engine().freewheeling()) {
201 // Freewheeling, do the work immediately in this (audio) thread
202 return (LV2_Worker_Status)plugin->work(size, data);
204 // Enqueue message for the worker thread
205 return plugin->worker()->schedule(size, data) ?
206 LV2_WORKER_SUCCESS : LV2_WORKER_ERR_UNKNOWN;
210 /** Called by the plugin to respond to non-RT work. */
211 static LV2_Worker_Status
212 work_respond(LV2_Worker_Respond_Handle handle,
216 LV2Plugin* plugin = (LV2Plugin*)handle;
217 if (plugin->session().engine().freewheeling()) {
218 // Freewheeling, respond immediately in this (audio) thread
219 return (LV2_Worker_Status)plugin->work_response(size, data);
221 // Enqueue response for the worker
222 return plugin->worker()->respond(size, data) ?
223 LV2_WORKER_SUCCESS : LV2_WORKER_ERR_UNKNOWN;
228 /* inline display extension */
230 queue_draw (LV2_Inline_Display_Handle handle)
232 LV2Plugin* plugin = (LV2Plugin*)handle;
233 plugin->QueueDraw(); /* EMIT SIGNAL */
240 log_vprintf(LV2_Log_Handle /*handle*/,
246 const int ret = g_vasprintf(&str, fmt, args);
247 /* strip trailing whitespace */
248 while (strlen (str) > 0 && isspace (str[strlen (str) - 1])) {
249 str[strlen (str) - 1] = '\0';
251 if (strlen (str) == 0) {
255 if (type == URIMap::instance().urids.log_Error) {
256 error << str << endmsg;
257 } else if (type == URIMap::instance().urids.log_Warning) {
258 warning << str << endmsg;
259 } else if (type == URIMap::instance().urids.log_Note) {
260 info << str << endmsg;
262 // TODO: Toggleable log:Trace message support
267 log_printf(LV2_Log_Handle handle,
269 const char* fmt, ...)
273 const int ret = log_vprintf(handle, type, fmt, args);
278 struct LV2Plugin::Impl {
279 Impl() : plugin(0), ui(0), ui_type(0), name(0), author(0), instance(0)
281 #ifdef HAVE_LV2_1_2_0
286 #ifdef HAVE_LV2_1_2_0
294 /** Find the LV2 input port with the given designation.
295 * If found, bufptrs[port_index] will be set to bufptr.
297 const LilvPort* designated_input (const char* uri, void** bufptrs[], void** bufptr);
299 const LilvPlugin* plugin;
301 const LilvNode* ui_type;
304 LilvInstance* instance;
305 const LV2_Worker_Interface* work_iface;
306 #ifdef HAVE_LV2_1_2_0
307 const LV2_Options_Interface* opts_iface;
310 LV2_Atom_Forge forge;
311 LV2_Atom_Forge ui_forge;
312 int32_t block_length;
313 #ifdef HAVE_LV2_1_2_0
314 LV2_Options_Option* options;
317 LV2_Inline_Display* queue_draw;
321 LV2Plugin::LV2Plugin (AudioEngine& engine,
323 const void* c_plugin,
325 : Plugin (engine, session)
331 , _patch_port_in_index((uint32_t)-1)
332 , _patch_port_out_index((uint32_t)-1)
333 , _uri_map(URIMap::instance())
334 , _no_sample_accurate_ctrl (false)
336 init(c_plugin, rate);
339 LV2Plugin::LV2Plugin (const LV2Plugin& other)
345 , _insert_id(other._insert_id)
346 , _patch_port_in_index((uint32_t)-1)
347 , _patch_port_out_index((uint32_t)-1)
348 , _uri_map(URIMap::instance())
349 , _no_sample_accurate_ctrl (false)
351 init(other._impl->plugin, other._sample_rate);
353 for (uint32_t i = 0; i < parameter_count(); ++i) {
354 _control_data[i] = other._shadow_data[i];
355 _shadow_data[i] = other._shadow_data[i];
360 LV2Plugin::init(const void* c_plugin, framecnt_t rate)
362 DEBUG_TRACE(DEBUG::LV2, "init\n");
364 _impl->plugin = (const LilvPlugin*)c_plugin;
366 _impl->ui_type = NULL;
371 _atom_ev_buffers = 0;
373 _bpm_control_port = 0;
374 _freewheel_control_port = 0;
375 _latency_control_port = 0;
376 _next_cycle_start = std::numeric_limits<framepos_t>::max();
377 _next_cycle_speed = 1.0;
378 _seq_size = _engine.raw_buffer_size(DataType::MIDI);
380 _was_activated = false;
381 _has_state_interface = false;
382 _can_write_automation = false;
384 _current_latency = 0;
385 _impl->block_length = _session.get_block_size();
387 _instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
388 _data_access_feature.URI = "http://lv2plug.in/ns/ext/data-access";
389 _make_path_feature.URI = LV2_STATE__makePath;
390 _log_feature.URI = LV2_LOG__log;
391 _work_schedule_feature.URI = LV2_WORKER__schedule;
392 _work_schedule_feature.data = NULL;
393 _def_state_feature.URI = LV2_STATE_PREFIX "loadDefaultState"; // Post LV2-1.2.0
394 _def_state_feature.data = NULL;
396 const LilvPlugin* plugin = _impl->plugin;
398 LilvNode* state_iface_uri = lilv_new_uri(_world.world, LV2_STATE__interface);
399 LilvNode* state_uri = lilv_new_uri(_world.world, LV2_STATE_URI);
400 _has_state_interface =
401 // What plugins should have (lv2:extensionData state:Interface)
402 lilv_plugin_has_extension_data(plugin, state_iface_uri)
403 // What some outdated/incorrect ones have
404 || lilv_plugin_has_feature(plugin, state_uri);
405 lilv_node_free(state_uri);
406 lilv_node_free(state_iface_uri);
408 _features = (LV2_Feature**)calloc(12, sizeof(LV2_Feature*));
409 _features[0] = &_instance_access_feature;
410 _features[1] = &_data_access_feature;
411 _features[2] = &_make_path_feature;
412 _features[3] = _uri_map.uri_map_feature();
413 _features[4] = _uri_map.urid_map_feature();
414 _features[5] = _uri_map.urid_unmap_feature();
415 _features[6] = &_log_feature;
417 unsigned n_features = 7;
418 #ifdef HAVE_LV2_1_2_0
419 _features[n_features++] = &_def_state_feature;
422 lv2_atom_forge_init(&_impl->forge, _uri_map.urid_map());
423 lv2_atom_forge_init(&_impl->ui_forge, _uri_map.urid_map());
426 _impl->queue_draw = (LV2_Inline_Display*)
427 malloc (sizeof(LV2_Inline_Display));
428 _impl->queue_draw->handle = this;
429 _impl->queue_draw->queue_draw = queue_draw;
431 _queue_draw_feature.URI = LV2_INLINEDISPLAY__queue_draw;
432 _queue_draw_feature.data = _impl->queue_draw;
433 _features[n_features++] = &_queue_draw_feature;
436 #ifdef HAVE_LV2_1_2_0
437 LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
438 static const int32_t _min_block_length = 1; // may happen during split-cycles
439 static const int32_t _max_block_length = 8192; // max possible (with all engines and during export)
440 /* Consider updating max-block-size whenever the buffersize changes.
441 * It requires re-instantiating the plugin (which is a non-realtime operation),
442 * so it should be done lightly and only for plugins that require it.
444 * given that the block-size can change at any time (split-cycles) ardour currently
445 * does not support plugins that require bufz_fixedBlockLength.
447 LV2_Options_Option options[] = {
448 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__minBlockLength),
449 sizeof(int32_t), atom_Int, &_min_block_length },
450 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__maxBlockLength),
451 sizeof(int32_t), atom_Int, &_max_block_length },
452 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__sequenceSize),
453 sizeof(int32_t), atom_Int, &_seq_size },
454 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://lv2plug.in/ns/ext/buf-size#nominalBlockLength"),
455 sizeof(int32_t), atom_Int, &_impl->block_length },
456 { LV2_OPTIONS_INSTANCE, 0, 0, 0, 0, NULL }
459 _impl->options = (LV2_Options_Option*) malloc (sizeof (options));
460 memcpy ((void*) _impl->options, (void*) options, sizeof (options));
462 _options_feature.URI = LV2_OPTIONS__options;
463 _options_feature.data = _impl->options;
464 _features[n_features++] = &_options_feature;
467 LV2_State_Make_Path* make_path = (LV2_State_Make_Path*)malloc(
468 sizeof(LV2_State_Make_Path));
469 make_path->handle = this;
470 make_path->path = &lv2_state_make_path;
471 _make_path_feature.data = make_path;
473 LV2_Log_Log* log = (LV2_Log_Log*)malloc(sizeof(LV2_Log_Log));
475 log->printf = &log_printf;
476 log->vprintf = &log_vprintf;
477 _log_feature.data = log;
479 LilvNode* worker_schedule = lilv_new_uri(_world.world, LV2_WORKER__schedule);
480 if (lilv_plugin_has_feature(plugin, worker_schedule)) {
481 LV2_Worker_Schedule* schedule = (LV2_Worker_Schedule*)malloc(
482 sizeof(LV2_Worker_Schedule));
483 size_t buf_size = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
484 _worker = new Worker(this, buf_size);
485 schedule->handle = this;
486 schedule->schedule_work = work_schedule;
487 _work_schedule_feature.data = schedule;
488 _features[n_features++] = &_work_schedule_feature;
490 lilv_node_free(worker_schedule);
492 _impl->instance = lilv_plugin_instantiate(plugin, rate, _features);
493 _impl->name = lilv_plugin_get_name(plugin);
494 _impl->author = lilv_plugin_get_author_name(plugin);
496 if (_impl->instance == 0) {
497 error << _("LV2: Failed to instantiate plugin ") << uri() << endmsg;
498 throw failed_constructor();
501 _instance_access_feature.data = (void*)_impl->instance->lv2_handle;
502 _data_access_extension_data.extension_data = _impl->instance->lv2_descriptor->extension_data;
503 _data_access_feature.data = &_data_access_extension_data;
505 LilvNode* worker_iface_uri = lilv_new_uri(_world.world, LV2_WORKER__interface);
506 if (lilv_plugin_has_extension_data(plugin, worker_iface_uri)) {
507 _impl->work_iface = (const LV2_Worker_Interface*)extension_data(
508 LV2_WORKER__interface);
510 lilv_node_free(worker_iface_uri);
513 #ifdef HAVE_LV2_1_2_0
514 LilvNode* options_iface_uri = lilv_new_uri(_world.world, LV2_OPTIONS__interface);
515 if (lilv_plugin_has_extension_data(plugin, options_iface_uri)) {
516 _impl->opts_iface = (const LV2_Options_Interface*)extension_data(
517 LV2_OPTIONS__interface);
519 lilv_node_free(options_iface_uri);
523 _display_interface = (const LV2_Inline_Display_Interface*)
524 extension_data (LV2_INLINEDISPLAY__interface);
527 if (lilv_plugin_has_feature(plugin, _world.lv2_inPlaceBroken)) {
528 error << string_compose(
529 _("LV2: \"%1\" cannot be used, since it cannot do inplace processing."),
530 lilv_node_as_string(_impl->name)) << endmsg;
531 lilv_node_free(_impl->name);
532 lilv_node_free(_impl->author);
533 throw failed_constructor();
536 #ifdef HAVE_LV2_1_2_0
537 LilvNodes *required_features = lilv_plugin_get_required_features (plugin);
538 if (lilv_nodes_contains (required_features, _world.bufz_powerOf2BlockLength) ||
539 lilv_nodes_contains (required_features, _world.bufz_fixedBlockLength)
541 error << string_compose(
542 _("LV2: \"%1\" buffer-size requirements cannot be satisfied."),
543 lilv_node_as_string(_impl->name)) << endmsg;
544 lilv_node_free(_impl->name);
545 lilv_node_free(_impl->author);
546 lilv_nodes_free(required_features);
547 throw failed_constructor();
549 lilv_nodes_free(required_features);
553 LilvNodes* optional_features = lilv_plugin_get_optional_features (plugin);
554 if (lilv_nodes_contains (optional_features, _world.lv2_noSampleAccurateCtrl)) {
555 _no_sample_accurate_ctrl = true;
557 if (lilv_nodes_contains (optional_features, _world.auto_can_write_automatation)) {
558 _can_write_automation = true;
560 lilv_nodes_free(optional_features);
563 #ifdef HAVE_LILV_0_16_0
564 // Load default state
565 LilvState* state = lilv_state_new_from_world(
566 _world.world, _uri_map.urid_map(), lilv_plugin_get_uri(_impl->plugin));
567 if (state && _has_state_interface) {
568 lilv_state_restore(state, _impl->instance, NULL, NULL, 0, NULL);
570 lilv_state_free(state);
575 const uint32_t num_ports = this->num_ports();
576 for (uint32_t i = 0; i < num_ports; ++i) {
577 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, i);
579 size_t minimumSize = 0;
581 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_OutputPort)) {
582 flags |= PORT_OUTPUT;
583 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_InputPort)) {
586 error << string_compose(
587 "LV2: \"%1\" port %2 is neither input nor output",
588 lilv_node_as_string(_impl->name), i) << endmsg;
589 throw failed_constructor();
592 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_ControlPort)) {
593 flags |= PORT_CONTROL;
594 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_AudioPort)) {
596 } else if (lilv_port_is_a(_impl->plugin, port, _world.ev_EventPort)) {
598 flags |= PORT_MIDI; // We assume old event API ports are for MIDI
599 } else if (lilv_port_is_a(_impl->plugin, port, _world.atom_AtomPort)) {
600 LilvNodes* buffer_types = lilv_port_get_value(
601 _impl->plugin, port, _world.atom_bufferType);
602 LilvNodes* atom_supports = lilv_port_get_value(
603 _impl->plugin, port, _world.atom_supports);
605 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
606 flags |= PORT_SEQUENCE;
607 if (lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)) {
610 if (lilv_nodes_contains(atom_supports, _world.time_Position)) {
611 flags |= PORT_POSITION;
614 if (lilv_nodes_contains(atom_supports, _world.auto_automation_control)) {
615 flags |= PORT_AUTOCTRL;
618 if (lilv_nodes_contains(atom_supports, _world.patch_Message)) {
619 flags |= PORT_PATCHMSG;
620 if (flags & PORT_INPUT) {
621 _patch_port_in_index = i;
623 _patch_port_out_index = i;
627 LilvNodes* min_size_v = lilv_port_get_value(_impl->plugin, port, _world.rsz_minimumSize);
628 LilvNode* min_size = min_size_v ? lilv_nodes_get_first(min_size_v) : NULL;
629 if (min_size && lilv_node_is_int(min_size)) {
630 minimumSize = lilv_node_as_int(min_size);
632 lilv_nodes_free(min_size_v);
633 lilv_nodes_free(buffer_types);
634 lilv_nodes_free(atom_supports);
636 error << string_compose(
637 "LV2: \"%1\" port %2 has no known data type",
638 lilv_node_as_string(_impl->name), i) << endmsg;
639 throw failed_constructor();
642 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
643 if (lilv_port_has_property(_impl->plugin, port, _world.ext_causesArtifacts)) {
644 flags |= PORT_NOAUTO;
646 if (lilv_port_has_property(_impl->plugin, port, _world.ext_notAutomatic)) {
647 flags |= PORT_NOAUTO;
649 if (lilv_port_has_property(_impl->plugin, port, _world.ext_expensive)) {
650 flags |= PORT_NOAUTO;
654 if (lilv_port_has_property(_impl->plugin, port, _world.auto_automation_controlled)) {
655 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
656 flags |= PORT_CTRLED;
659 if (lilv_port_has_property(_impl->plugin, port, _world.auto_automation_controller)) {
660 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
661 flags |= PORT_CTRLER;
666 _port_flags.push_back(flags);
667 _port_minimumSize.push_back(minimumSize);
668 DEBUG_TRACE(DEBUG::LV2, string_compose("port %1 buffer %2 bytes\n", i, minimumSize));
671 _control_data = new float[num_ports];
672 _shadow_data = new float[num_ports];
673 _defaults = new float[num_ports];
674 _ev_buffers = new LV2_Evbuf*[num_ports];
675 memset(_ev_buffers, 0, sizeof(LV2_Evbuf*) * num_ports);
677 const bool latent = lilv_plugin_has_latency(plugin);
678 const uint32_t latency_index = (latent)
679 ? lilv_plugin_get_latency_port_index(plugin)
682 // Build an array of pointers to special parameter buffers
683 void*** params = new void**[num_ports];
684 for (uint32_t i = 0; i < num_ports; ++i) {
687 _impl->designated_input (LV2_TIME__beatsPerMinute, params, (void**)&_bpm_control_port);
688 _impl->designated_input (LV2_CORE__freeWheeling, params, (void**)&_freewheel_control_port);
690 for (uint32_t i = 0; i < num_ports; ++i) {
691 const LilvPort* port = lilv_plugin_get_port_by_index(plugin, i);
692 const LilvNode* sym = lilv_port_get_symbol(plugin, port);
694 // Store index in map so we can look up index by symbol
695 _port_indices.insert(std::make_pair(lilv_node_as_string(sym), i));
697 // Get range and default value if applicable
698 if (parameter_is_control(i)) {
700 lilv_port_get_range(plugin, port, &def, NULL, NULL);
701 _defaults[i] = def ? lilv_node_as_float(def) : 0.0f;
702 if (lilv_port_has_property (plugin, port, _world.lv2_sampleRate)) {
703 _defaults[i] *= _session.frame_rate ();
707 lilv_instance_connect_port(_impl->instance, i, &_control_data[i]);
709 if (latent && i == latency_index) {
711 lilv_port_get_range(_impl->plugin, port, NULL, NULL, &max);
712 _max_latency = max ? lilv_node_as_float(max) : .02 * _sample_rate;
713 _latency_control_port = &_control_data[i];
714 *_latency_control_port = 0;
717 if (parameter_is_input(i)) {
718 _shadow_data[i] = default_value(i);
720 *params[i] = (void*)&_shadow_data[i];
730 LilvUIs* uis = lilv_plugin_get_uis(plugin);
731 if (lilv_uis_size(uis) > 0) {
733 // Look for embeddable UI
734 LILV_FOREACH(uis, u, uis) {
735 const LilvUI* this_ui = lilv_uis_get(uis, u);
736 const LilvNode* this_ui_type = NULL;
737 if (lilv_ui_is_supported(this_ui,
741 // TODO: Multiple UI support
743 _impl->ui_type = this_ui_type;
748 // Look for Gtk native UI
749 LILV_FOREACH(uis, i, uis) {
750 const LilvUI* ui = lilv_uis_get(uis, i);
751 if (lilv_ui_is_a(ui, _world.ui_GtkUI)) {
753 _impl->ui_type = _world.ui_GtkUI;
759 // If Gtk UI is not available, try to find external UI
761 LILV_FOREACH(uis, i, uis) {
762 const LilvUI* ui = lilv_uis_get(uis, i);
763 if (lilv_ui_is_a(ui, _world.ui_externalkx)) {
765 _impl->ui_type = _world.ui_external;
768 if (lilv_ui_is_a(ui, _world.ui_external)) {
770 _impl->ui_type = _world.ui_external;
776 load_supported_properties(_property_descriptors);
777 allocate_atom_event_buffers();
778 latency_compute_run();
782 LV2Plugin::set_block_size (pframes_t nframes)
784 #ifdef HAVE_LV2_1_2_0
785 if (_impl->opts_iface) {
786 LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
787 _impl->block_length = nframes;
788 LV2_Options_Option block_size_option = {
789 LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id ("http://lv2plug.in/ns/ext/buf-size#nominalBlockLength"),
790 sizeof(int32_t), atom_Int, (void*)&_impl->block_length
792 _impl->opts_iface->set (_impl->instance->lv2_handle, &block_size_option);
799 LV2Plugin::requires_fixed_sized_buffers () const
801 /* This controls if Ardour will split the plugin's run()
802 * on automation events in order to pass sample-accurate automation
803 * via standard control-ports.
805 * When returning true Ardour will *not* sub-divide the process-cycle.
806 * Automation events that happen between cycle-start and cycle-end will be
807 * ignored (ctrl values are interpolated to cycle-start).
808 * NB. Atom Sequences are still sample accurate.
810 * Note: This does not guarantee a fixed block-size.
811 * e.g The process cycle may be split when looping, also
812 * the period-size may change any time: see set_block_size()
814 if (get_info()->n_inputs.n_midi() > 0) {
815 /* we don't yet implement midi buffer offsets (for split cycles).
816 * Also connect_and_run() also uses _session.transport_frame() directly
817 * (for BBT) which is not offset for plugin cycle split.
821 return _no_sample_accurate_ctrl;
824 LV2Plugin::~LV2Plugin ()
826 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 destroy\n", name()));
831 lilv_instance_free(_impl->instance);
832 lilv_state_free(_impl->state);
833 lilv_node_free(_impl->name);
834 lilv_node_free(_impl->author);
835 #ifdef HAVE_LV2_1_2_0
836 free(_impl->options);
839 free(_impl->queue_draw);
843 free(_log_feature.data);
844 free(_make_path_feature.data);
845 free(_work_schedule_feature.data);
851 if (_atom_ev_buffers) {
852 LV2_Evbuf** b = _atom_ev_buffers;
857 free(_atom_ev_buffers);
860 delete [] _control_data;
861 delete [] _shadow_data;
863 delete [] _ev_buffers;
868 LV2Plugin::is_external_ui() const
873 return lilv_ui_is_a(_impl->ui, _world.ui_external) || lilv_ui_is_a(_impl->ui, _world.ui_externalkx);
877 LV2Plugin::is_external_kx() const
882 return lilv_ui_is_a(_impl->ui, _world.ui_externalkx);
886 LV2Plugin::ui_is_resizable () const
888 const LilvNode* s = lilv_ui_get_uri(_impl->ui);
889 LilvNode* p = lilv_new_uri(_world.world, LV2_CORE__optionalFeature);
890 LilvNode* fs = lilv_new_uri(_world.world, LV2_UI__fixedSize);
891 LilvNode* nrs = lilv_new_uri(_world.world, LV2_UI__noUserResize);
893 LilvNodes* fs_matches = lilv_world_find_nodes(_world.world, s, p, fs);
894 LilvNodes* nrs_matches = lilv_world_find_nodes(_world.world, s, p, nrs);
896 lilv_nodes_free(nrs_matches);
897 lilv_nodes_free(fs_matches);
902 return !fs_matches && !nrs_matches;
907 LV2Plugin::has_inline_display () {
908 return _display_interface ? true : false;
911 Plugin::Display_Image_Surface*
912 LV2Plugin::render_inline_display (uint32_t w, uint32_t h) {
913 if (_display_interface) {
914 /* Plugin::Display_Image_Surface is identical to
915 * LV2_Inline_Display_Image_Surface */
916 return (Plugin::Display_Image_Surface*) _display_interface->render ((void*)_impl->instance->lv2_handle, w, h);
923 LV2Plugin::unique_id() const
925 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
929 LV2Plugin::uri() const
931 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
935 LV2Plugin::label() const
937 return lilv_node_as_string(_impl->name);
941 LV2Plugin::name() const
943 return lilv_node_as_string(_impl->name);
947 LV2Plugin::maker() const
949 return _impl->author ? lilv_node_as_string (_impl->author) : "Unknown";
953 LV2Plugin::num_ports() const
955 return lilv_plugin_get_num_ports(_impl->plugin);
959 LV2Plugin::parameter_count() const
961 return lilv_plugin_get_num_ports(_impl->plugin);
965 LV2Plugin::default_value(uint32_t port)
967 return _defaults[port];
971 LV2Plugin::port_symbol(uint32_t index) const
973 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, index);
975 error << name() << ": Invalid port index " << index << endmsg;
978 const LilvNode* sym = lilv_port_get_symbol(_impl->plugin, port);
979 return lilv_node_as_string(sym);
983 LV2Plugin::port_index (const char* symbol) const
985 const map<string, uint32_t>::const_iterator i = _port_indices.find(symbol);
986 if (i != _port_indices.end()) {
989 warning << string_compose(_("LV2: Unknown port %1"), symbol) << endmsg;
995 LV2Plugin::set_parameter(uint32_t which, float val)
997 DEBUG_TRACE(DEBUG::LV2, string_compose(
998 "%1 set parameter %2 to %3\n", name(), which, val));
1000 if (which < lilv_plugin_get_num_ports(_impl->plugin)) {
1001 if (get_parameter (which) == val) {
1005 _shadow_data[which] = val;
1007 warning << string_compose(
1008 _("Illegal parameter number used with plugin \"%1\". "
1009 "This is a bug in either %2 or the LV2 plugin <%3>"),
1010 name(), PROGRAM_NAME, unique_id()) << endmsg;
1013 Plugin::set_parameter(which, val);
1017 LV2Plugin::get_parameter(uint32_t which) const
1019 if (parameter_is_input(which)) {
1020 return (float)_shadow_data[which];
1022 return (float)_control_data[which];
1028 LV2Plugin::get_docs() const
1030 LilvNodes* comments = lilv_plugin_get_value(_impl->plugin, _world.rdfs_comment);
1032 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
1033 lilv_nodes_free(comments);
1041 LV2Plugin::get_parameter_docs(uint32_t which) const
1043 LilvNodes* comments = lilv_port_get_value(
1045 lilv_plugin_get_port_by_index(_impl->plugin, which),
1046 _world.rdfs_comment);
1049 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
1050 lilv_nodes_free(comments);
1058 LV2Plugin::get_layout (uint32_t which, UILayoutHint& h) const
1060 /// TODO lookup port-properties
1061 if (unique_id () != "urn:ardour:a-eq") {
1066 case 0: h.x0 = 0; h.x1 = 1; h.y0 = 4; h.y1 = 5; break; // Shelf toggle L
1067 case 1: h.x0 = 0; h.x1 = 1; h.y0 = 2; h.y1 = 3; break; // Frequency L
1068 case 2: h.x0 = 0; h.x1 = 1; h.y0 = 0; h.y1 = 1; break; // Gain L
1069 case 19: h.x0 = 0; h.x1 = 1; h.y0 = 5; h.y1 = 6; break; // enable L
1071 case 3: h.x0 = 1; h.x1 = 3; h.y0 = 2; h.y1 = 3; break; // Frequency 1
1072 case 4: h.x0 = 1; h.x1 = 3; h.y0 = 0; h.y1 = 1; break; // Gain 1
1073 case 5: h.x0 = 2; h.x1 = 4; h.y0 = 1; h.y1 = 2; break; // Bandwidth 1
1074 case 20: h.x0 = 1; h.x1 = 4; h.y0 = 5; h.y1 = 6; break; // enable 1
1076 case 6: h.x0 = 4; h.x1 = 6; h.y0 = 2; h.y1 = 3; break; // Frequency 2
1077 case 7: h.x0 = 4; h.x1 = 6; h.y0 = 0; h.y1 = 1; break; // Gain 2
1078 case 8: h.x0 = 5; h.x1 = 7; h.y0 = 1; h.y1 = 2; break; // Bandwidth 2
1079 case 21: h.x0 = 4; h.x1 = 7; h.y0 = 5; h.y1 = 6; break; // enable 2
1081 case 9: h.x0 = 7; h.x1 = 9; h.y0 = 2; h.y1 = 3; break; // Frequency 3
1082 case 10: h.x0 = 7; h.x1 = 9; h.y0 = 0; h.y1 = 1; break; // Gain 3
1083 case 11: h.x0 = 8; h.x1 = 10; h.y0 = 1; h.y1 = 2; break; // Bandwidth 3
1084 case 22: h.x0 = 7; h.x1 = 10; h.y0 = 5; h.y1 = 6; break; // enable 3
1086 case 12: h.x0 = 10; h.x1 = 12; h.y0 = 2; h.y1 = 3; break; // Frequency 4
1087 case 13: h.x0 = 10; h.x1 = 12; h.y0 = 0; h.y1 = 1; break; // Gain 4
1088 case 14: h.x0 = 11; h.x1 = 13; h.y0 = 1; h.y1 = 2; break; // Bandwidth 4
1089 case 23: h.x0 = 10; h.x1 = 13; h.y0 = 5; h.y1 = 6; break; // enable 4
1091 case 15: h.x0 = 13; h.x1 = 14; h.y0 = 4; h.y1 = 5; break; // Shelf toggle H
1092 case 16: h.x0 = 13; h.x1 = 14; h.y0 = 2; h.y1 = 3; break; // Frequency H
1093 case 17: h.x0 = 13; h.x1 = 14; h.y0 = 0; h.y1 = 1; break; // Gain H
1094 case 24: h.x0 = 13; h.x1 = 14; h.y0 = 5; h.y1 = 6; break; // enable H
1097 case 18: h.x0 = 14; h.x1 = 15; h.y0 = 4; h.y1 = 6; break; // Master Gain
1105 LV2Plugin::nth_parameter(uint32_t n, bool& ok) const
1108 for (uint32_t c = 0, x = 0; x < lilv_plugin_get_num_ports(_impl->plugin); ++x) {
1109 if (parameter_is_control(x)) {
1121 LV2Plugin::extension_data(const char* uri) const
1123 return lilv_instance_get_extension_data(_impl->instance, uri);
1127 LV2Plugin::c_plugin()
1129 return _impl->plugin;
1135 return (const void*)_impl->ui;
1139 LV2Plugin::c_ui_type()
1141 return (const void*)_impl->ui_type;
1144 /** Directory for all plugin state. */
1146 LV2Plugin::plugin_dir() const
1148 if (!_plugin_state_dir.empty ()){
1149 return Glib::build_filename(_plugin_state_dir, _insert_id.to_s());
1151 return Glib::build_filename(_session.plugins_dir(), _insert_id.to_s());
1155 /** Directory for files created by the plugin (except during save). */
1157 LV2Plugin::scratch_dir() const
1159 return Glib::build_filename(plugin_dir(), "scratch");
1162 /** Directory for snapshots of files in the scratch directory. */
1164 LV2Plugin::file_dir() const
1166 return Glib::build_filename(plugin_dir(), "files");
1169 /** Directory to save state snapshot version @c num into. */
1171 LV2Plugin::state_dir(unsigned num) const
1173 return Glib::build_filename(plugin_dir(), string_compose("state%1", num));
1176 /** Implementation of state:makePath for files created at instantiation time.
1177 * Note this is not used for files created at save time (Lilv deals with that).
1180 LV2Plugin::lv2_state_make_path(LV2_State_Make_Path_Handle handle,
1183 LV2Plugin* me = (LV2Plugin*)handle;
1184 if (me->_insert_id == PBD::ID("0")) {
1185 warning << string_compose(
1186 "File path \"%1\" requested but LV2 %2 has no insert ID",
1187 path, me->name()) << endmsg;
1188 return g_strdup(path);
1191 const std::string abs_path = Glib::build_filename(me->scratch_dir(), path);
1192 const std::string dirname = Glib::path_get_dirname(abs_path);
1193 g_mkdir_with_parents(dirname.c_str(), 0744);
1195 DEBUG_TRACE(DEBUG::LV2, string_compose("new file path %1 => %2\n",
1198 return g_strndup(abs_path.c_str(), abs_path.length());
1202 LV2Plugin::add_state(XMLNode* root) const
1204 assert(_insert_id != PBD::ID("0"));
1210 for (uint32_t i = 0; i < parameter_count(); ++i) {
1211 if (parameter_is_input(i) && parameter_is_control(i)) {
1212 child = new XMLNode("Port");
1213 child->add_property("symbol", port_symbol(i));
1214 snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
1215 child->add_property("value", string(buf));
1216 root->add_child_nocopy(*child);
1220 if (!_plugin_state_dir.empty()) {
1221 root->add_property("template-dir", _plugin_state_dir);
1224 if (_has_state_interface) {
1225 // Provisionally increment state version and create directory
1226 const std::string new_dir = state_dir(++_state_version);
1227 g_mkdir_with_parents(new_dir.c_str(), 0744);
1229 LilvState* state = lilv_state_new_from_instance(
1232 _uri_map.urid_map(),
1233 scratch_dir().c_str(),
1235 _session.externals_dir().c_str(),
1238 const_cast<LV2Plugin*>(this),
1242 if (!_plugin_state_dir.empty()
1244 || !lilv_state_equals(state, _impl->state)) {
1245 lilv_state_save(_world.world,
1246 _uri_map.urid_map(),
1247 _uri_map.urid_unmap(),
1253 if (_plugin_state_dir.empty()) {
1254 // normal session save
1255 lilv_state_free(_impl->state);
1256 _impl->state = state;
1258 // template save (dedicated state-dir)
1259 lilv_state_free(state);
1262 // State is identical, decrement version and nuke directory
1263 lilv_state_free(state);
1264 PBD::remove_directory(new_dir);
1268 root->add_property("state-dir", string_compose("state%1", _state_version));
1272 // TODO: Once we can rely on lilv 0.16.0, lilv_world_get can replace this
1274 get_value(LilvWorld* world, const LilvNode* subject, const LilvNode* predicate)
1276 LilvNodes* vs = lilv_world_find_nodes(world, subject, predicate, NULL);
1278 LilvNode* node = lilv_node_duplicate(lilv_nodes_get_first(vs));
1279 lilv_nodes_free(vs);
1286 LV2Plugin::find_presets()
1288 LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
1289 LilvNode* pset_Preset = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
1290 LilvNode* rdfs_label = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
1292 LilvNodes* presets = lilv_plugin_get_related(_impl->plugin, pset_Preset);
1293 LILV_FOREACH(nodes, i, presets) {
1294 const LilvNode* preset = lilv_nodes_get(presets, i);
1295 lilv_world_load_resource(_world.world, preset);
1296 LilvNode* name = get_value(_world.world, preset, rdfs_label);
1297 bool userpreset = true; // TODO
1299 _presets.insert(std::make_pair(lilv_node_as_string(preset),
1300 Plugin::PresetRecord(
1301 lilv_node_as_string(preset),
1302 lilv_node_as_string(name),
1304 lilv_node_free(name);
1306 warning << string_compose(
1307 _("Plugin \"%1\" preset \"%2\" is missing a label\n"),
1308 lilv_node_as_string(lilv_plugin_get_uri(_impl->plugin)),
1309 lilv_node_as_string(preset)) << endmsg;
1312 lilv_nodes_free(presets);
1314 lilv_node_free(rdfs_label);
1315 lilv_node_free(pset_Preset);
1316 lilv_node_free(lv2_appliesTo);
1320 set_port_value(const char* port_symbol,
1326 LV2Plugin* self = (LV2Plugin*)user_data;
1327 if (type != 0 && type != URIMap::instance().urids.atom_Float) {
1328 return; // TODO: Support non-float ports
1331 const uint32_t port_index = self->port_index(port_symbol);
1332 if (port_index != (uint32_t)-1) {
1333 self->set_parameter(port_index, *(const float*)value);
1338 LV2Plugin::load_preset(PresetRecord r)
1340 LilvWorld* world = _world.world;
1341 LilvNode* pset = lilv_new_uri(world, r.uri.c_str());
1342 LilvState* state = lilv_state_new_from_world(world, _uri_map.urid_map(), pset);
1345 lilv_state_restore(state, _impl->instance, set_port_value, this, 0, NULL);
1346 lilv_state_free(state);
1347 Plugin::load_preset(r);
1350 lilv_node_free(pset);
1355 ARDOUR::lv2plugin_get_port_value(const char* port_symbol,
1360 LV2Plugin *plugin = (LV2Plugin *) user_data;
1362 uint32_t index = plugin->port_index(port_symbol);
1363 if (index != (uint32_t) -1) {
1364 if (plugin->parameter_is_input(index) && plugin->parameter_is_control(index)) {
1366 *size = sizeof(float);
1367 *type = plugin->_uri_map.uri_to_id(LV2_ATOM__Float);
1368 value = &plugin->_shadow_data[index];
1380 LV2Plugin::do_save_preset(string name)
1382 LilvNode* plug_name = lilv_plugin_get_name(_impl->plugin);
1383 const string prefix = legalize_for_uri(lilv_node_as_string(plug_name));
1384 const string base_name = legalize_for_uri(name);
1385 const string file_name = base_name + ".ttl";
1386 const string bundle = Glib::build_filename(
1387 Glib::get_home_dir(),
1388 Glib::build_filename(".lv2", prefix + "_" + base_name + ".lv2"));
1390 #ifdef HAVE_LILV_0_21_3
1391 /* delete reference to old preset (if any) */
1392 const PresetRecord* r = preset_by_label(name);
1394 LilvNode* pset = lilv_new_uri (_world.world, r->uri.c_str());
1396 lilv_world_unload_resource (_world.world, pset);
1397 lilv_node_free(pset);
1402 LilvState* state = lilv_state_new_from_instance(
1405 _uri_map.urid_map(),
1406 scratch_dir().c_str(), // file_dir
1407 bundle.c_str(), // copy_dir
1408 bundle.c_str(), // link_dir
1409 bundle.c_str(), // save_dir
1410 lv2plugin_get_port_value, // get_value
1411 (void*)this, // user_data
1412 LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE, // flags
1413 _features // features
1416 lilv_state_set_label(state, name.c_str());
1418 _world.world, // world
1419 _uri_map.urid_map(), // map
1420 _uri_map.urid_unmap(), // unmap
1422 NULL, // uri (NULL = use file URI)
1423 bundle.c_str(), // dir
1424 file_name.c_str() // filename
1427 lilv_state_free(state);
1429 std::string uri = Glib::filename_to_uri(Glib::build_filename(bundle, file_name));
1430 LilvNode *node_bundle = lilv_new_uri(_world.world, Glib::filename_to_uri(Glib::build_filename(bundle, "/")).c_str());
1431 LilvNode *node_preset = lilv_new_uri(_world.world, uri.c_str());
1432 #ifdef HAVE_LILV_0_21_3
1433 lilv_world_unload_resource(_world.world, node_preset);
1434 lilv_world_unload_bundle(_world.world, node_bundle);
1436 lilv_world_load_bundle(_world.world, node_bundle);
1437 lilv_world_load_resource(_world.world, node_preset);
1438 lilv_node_free(node_bundle);
1439 lilv_node_free(node_preset);
1440 lilv_node_free(plug_name);
1445 LV2Plugin::do_remove_preset(string name)
1447 #ifdef HAVE_LILV_0_21_3
1448 /* Look up preset record by label (FIXME: ick, label as ID) */
1449 const PresetRecord* r = preset_by_label(name);
1454 /* Load a LilvState for the preset. */
1455 LilvWorld* world = _world.world;
1456 LilvNode* pset = lilv_new_uri(world, r->uri.c_str());
1457 LilvState* state = lilv_state_new_from_world(world, _uri_map.urid_map(), pset);
1459 lilv_node_free(pset);
1463 /* Unload preset from world. */
1464 lilv_world_unload_resource(world, pset);
1466 /* Delete it from the file system. This will remove the preset file and the entry
1467 from the manifest. If this results in an empty manifest (i.e. the
1468 preset is the only thing in the bundle), then the bundle is removed. */
1469 lilv_state_delete(world, state);
1471 lilv_state_free(state);
1472 lilv_node_free(pset);
1474 /* Without lilv_state_delete(), we could delete the preset file, but this
1475 would leave a broken bundle/manifest around, so the preset would still
1476 be visible, but broken. Naively deleting a bundle is too dangerous, so
1477 we simply do not support preset deletion with older Lilv */
1481 LV2Plugin::has_editor() const
1483 return _impl->ui != NULL;
1487 LV2Plugin::has_message_output() const
1489 for (uint32_t i = 0; i < num_ports(); ++i) {
1490 if ((_port_flags[i] & PORT_SEQUENCE) &&
1491 (_port_flags[i] & PORT_OUTPUT)) {
1499 LV2Plugin::write_to(RingBuffer<uint8_t>* dest,
1503 const uint8_t* body)
1505 const uint32_t buf_size = sizeof(UIMessage) + size;
1506 vector<uint8_t> buf(buf_size);
1508 UIMessage* msg = (UIMessage*)&buf[0];
1510 msg->protocol = protocol;
1512 memcpy(msg + 1, body, size);
1514 return (dest->write(&buf[0], buf_size) == buf_size);
1518 LV2Plugin::write_from_ui(uint32_t index,
1521 const uint8_t* body)
1524 size_t rbs = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
1525 /* buffer data communication from plugin UI to plugin instance.
1526 * this buffer needs to potentially hold
1527 * (port's minimumSize) * (audio-periods) / (UI-periods)
1530 * e.g 48kSPS / 128fpp -> audio-periods = 375 Hz
1531 * ui-periods = 25 Hz (SuperRapidScreenUpdate)
1532 * default minimumSize = 32K (see LV2Plugin::allocate_atom_event_buffers()
1534 * it is NOT safe to overflow (msg.size will be misinterpreted)
1536 uint32_t bufsiz = 32768;
1537 if (_atom_ev_buffers && _atom_ev_buffers[0]) {
1538 bufsiz = lv2_evbuf_get_capacity(_atom_ev_buffers[0]);
1540 rbs = max((size_t) bufsiz * 8, rbs);
1541 _from_ui = new RingBuffer<uint8_t>(rbs);
1544 if (!write_to(_from_ui, index, protocol, size, body)) {
1545 error << "Error writing from UI to plugin" << endmsg;
1552 LV2Plugin::write_to_ui(uint32_t index,
1555 const uint8_t* body)
1557 if (!write_to(_to_ui, index, protocol, size, body)) {
1558 error << "Error writing from plugin to UI" << endmsg;
1565 forge_variant(LV2_Atom_Forge* forge, const Variant& value)
1567 switch (value.type()) {
1568 case Variant::NOTHING:
1570 case Variant::BEATS:
1571 // No atom type for this, just forge a double
1572 lv2_atom_forge_double(forge, value.get_beats().to_double());
1575 lv2_atom_forge_bool(forge, value.get_bool());
1577 case Variant::DOUBLE:
1578 lv2_atom_forge_double(forge, value.get_double());
1580 case Variant::FLOAT:
1581 lv2_atom_forge_float(forge, value.get_float());
1584 lv2_atom_forge_int(forge, value.get_int());
1587 lv2_atom_forge_long(forge, value.get_long());
1590 lv2_atom_forge_path(
1591 forge, value.get_path().c_str(), value.get_path().size());
1593 case Variant::STRING:
1594 lv2_atom_forge_string(
1595 forge, value.get_string().c_str(), value.get_string().size());
1599 forge, value.get_uri().c_str(), value.get_uri().size());
1604 /** Get a variant type from a URI, return false iff no match found. */
1606 uri_to_variant_type(const std::string& uri, Variant::Type& type)
1608 if (uri == LV2_ATOM__Bool) {
1609 type = Variant::BOOL;
1610 } else if (uri == LV2_ATOM__Double) {
1611 type = Variant::DOUBLE;
1612 } else if (uri == LV2_ATOM__Float) {
1613 type = Variant::FLOAT;
1614 } else if (uri == LV2_ATOM__Int) {
1615 type = Variant::INT;
1616 } else if (uri == LV2_ATOM__Long) {
1617 type = Variant::LONG;
1618 } else if (uri == LV2_ATOM__Path) {
1619 type = Variant::PATH;
1620 } else if (uri == LV2_ATOM__String) {
1621 type = Variant::STRING;
1622 } else if (uri == LV2_ATOM__URI) {
1623 type = Variant::URI;
1631 LV2Plugin::set_property(uint32_t key, const Variant& value)
1633 if (_patch_port_in_index == (uint32_t)-1) {
1634 error << "LV2: set_property called with unset patch_port_in_index" << endmsg;
1636 } else if (value.type() == Variant::NOTHING) {
1637 error << "LV2: set_property called with void value" << endmsg;
1641 // Set up forge to write to temporary buffer on the stack
1642 LV2_Atom_Forge* forge = &_impl->ui_forge;
1643 LV2_Atom_Forge_Frame frame;
1644 uint8_t buf[PATH_MAX]; // Ought to be enough for anyone...
1646 lv2_atom_forge_set_buffer(forge, buf, sizeof(buf));
1648 // Serialize patch:Set message to set property
1649 #ifdef HAVE_LV2_1_10_0
1650 lv2_atom_forge_object(forge, &frame, 1, _uri_map.urids.patch_Set);
1651 lv2_atom_forge_key(forge, _uri_map.urids.patch_property);
1652 lv2_atom_forge_urid(forge, key);
1653 lv2_atom_forge_key(forge, _uri_map.urids.patch_value);
1655 lv2_atom_forge_blank(forge, &frame, 1, _uri_map.urids.patch_Set);
1656 lv2_atom_forge_property_head(forge, _uri_map.urids.patch_property, 0);
1657 lv2_atom_forge_urid(forge, key);
1658 lv2_atom_forge_property_head(forge, _uri_map.urids.patch_value, 0);
1661 forge_variant(forge, value);
1663 // Write message to UI=>Plugin ring
1664 const LV2_Atom* const atom = (const LV2_Atom*)buf;
1665 write_from_ui(_patch_port_in_index,
1666 _uri_map.urids.atom_eventTransfer,
1667 lv2_atom_total_size(atom),
1668 (const uint8_t*)atom);
1671 const ParameterDescriptor&
1672 LV2Plugin::get_property_descriptor(uint32_t id) const
1674 PropertyDescriptors::const_iterator p = _property_descriptors.find(id);
1675 if (p != _property_descriptors.end()) {
1678 return Plugin::get_property_descriptor(id);
1682 load_parameter_descriptor_units(LilvWorld* lworld, ParameterDescriptor& desc, const LilvNodes* units)
1684 if (lilv_nodes_contains(units, _world.units_midiNote)) {
1685 desc.unit = ParameterDescriptor::MIDI_NOTE;
1686 } else if (lilv_nodes_contains(units, _world.units_db)) {
1687 desc.unit = ParameterDescriptor::DB;
1688 } else if (lilv_nodes_contains(units, _world.units_hz)) {
1689 desc.unit = ParameterDescriptor::HZ;
1691 if (lilv_nodes_size(units) > 0) {
1692 const LilvNode* unit = lilv_nodes_get_first(units);
1693 LilvNode* render = get_value(lworld, unit, _world.units_render);
1695 desc.print_fmt = lilv_node_as_string(render);
1696 replace_all (desc.print_fmt, "%f", "%.2f");
1697 lilv_node_free(render);
1703 load_parameter_descriptor(LV2World& world,
1704 ParameterDescriptor& desc,
1705 Variant::Type datatype,
1706 const LilvNode* subject)
1708 LilvWorld* lworld = _world.world;
1709 LilvNode* label = get_value(lworld, subject, _world.rdfs_label);
1710 LilvNode* def = get_value(lworld, subject, _world.lv2_default);
1711 LilvNode* minimum = get_value(lworld, subject, _world.lv2_minimum);
1712 LilvNode* maximum = get_value(lworld, subject, _world.lv2_maximum);
1713 LilvNodes* units = lilv_world_find_nodes(lworld, subject, _world.units_unit, NULL);
1715 desc.label = lilv_node_as_string(label);
1717 if (def && lilv_node_is_float(def)) {
1718 desc.normal = lilv_node_as_float(def);
1720 if (minimum && lilv_node_is_float(minimum)) {
1721 desc.lower = lilv_node_as_float(minimum);
1723 if (maximum && lilv_node_is_float(maximum)) {
1724 desc.upper = lilv_node_as_float(maximum);
1726 load_parameter_descriptor_units(lworld, desc, units);
1727 desc.datatype = datatype;
1728 desc.toggled |= datatype == Variant::BOOL;
1729 desc.integer_step |= datatype == Variant::INT || datatype == Variant::LONG;
1730 desc.update_steps();
1732 lilv_nodes_free(units);
1733 lilv_node_free(label);
1734 lilv_node_free(def);
1735 lilv_node_free(minimum);
1736 lilv_node_free(maximum);
1740 LV2Plugin::load_supported_properties(PropertyDescriptors& descs)
1742 LilvWorld* lworld = _world.world;
1743 const LilvNode* subject = lilv_plugin_get_uri(_impl->plugin);
1744 LilvNodes* properties = lilv_world_find_nodes(
1745 lworld, subject, _world.patch_writable, NULL);
1746 LILV_FOREACH(nodes, p, properties) {
1747 // Get label and range
1748 const LilvNode* prop = lilv_nodes_get(properties, p);
1749 LilvNode* range = get_value(lworld, prop, _world.rdfs_range);
1751 warning << string_compose(_("LV2: property <%1> has no range datatype, ignoring"),
1752 lilv_node_as_uri(prop)) << endmsg;
1756 // Convert range to variant type (TODO: support for multiple range types)
1757 Variant::Type datatype;
1758 if (!uri_to_variant_type(lilv_node_as_uri(range), datatype)) {
1759 error << string_compose(_("LV2: property <%1> has unsupported datatype <%1>"),
1760 lilv_node_as_uri(prop), lilv_node_as_uri(range)) << endmsg;
1764 // Add description to result
1765 ParameterDescriptor desc;
1766 desc.key = _uri_map.uri_to_id(lilv_node_as_uri(prop));
1767 desc.datatype = datatype;
1768 load_parameter_descriptor(_world, desc, datatype, prop);
1769 descs.insert(std::make_pair(desc.key, desc));
1771 lilv_node_free(range);
1773 lilv_nodes_free(properties);
1777 LV2Plugin::announce_property_values()
1779 if (_patch_port_in_index == (uint32_t)-1) {
1783 // Set up forge to write to temporary buffer on the stack
1784 LV2_Atom_Forge* forge = &_impl->ui_forge;
1785 LV2_Atom_Forge_Frame frame;
1786 uint8_t buf[PATH_MAX]; // Ought to be enough for anyone...
1788 lv2_atom_forge_set_buffer(forge, buf, sizeof(buf));
1790 // Serialize patch:Get message with no subject (implicitly plugin instance)
1791 #ifdef HAVE_LV2_1_10_0
1792 lv2_atom_forge_object(forge, &frame, 1, _uri_map.urids.patch_Get);
1794 lv2_atom_forge_blank(forge, &frame, 1, _uri_map.urids.patch_Get);
1797 // Write message to UI=>Plugin ring
1798 const LV2_Atom* const atom = (const LV2_Atom*)buf;
1799 write_from_ui(_patch_port_in_index,
1800 _uri_map.urids.atom_eventTransfer,
1801 lv2_atom_total_size(atom),
1802 (const uint8_t*)atom);
1806 LV2Plugin::enable_ui_emission()
1809 /* see note in LV2Plugin::write_from_ui() */
1810 uint32_t bufsiz = 32768;
1811 if (_atom_ev_buffers && _atom_ev_buffers[0]) {
1812 bufsiz = lv2_evbuf_get_capacity(_atom_ev_buffers[0]);
1814 size_t rbs = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
1815 rbs = max((size_t) bufsiz * 8, rbs);
1816 _to_ui = new RingBuffer<uint8_t>(rbs);
1821 LV2Plugin::emit_to_ui(void* controller, UIMessageSink sink)
1827 uint32_t read_space = _to_ui->read_space();
1828 while (read_space > sizeof(UIMessage)) {
1830 if (_to_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
1831 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
1834 vector<uint8_t> body(msg.size);
1835 if (_to_ui->read(&body[0], msg.size) != msg.size) {
1836 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
1840 sink(controller, msg.index, msg.size, msg.protocol, &body[0]);
1842 read_space -= sizeof(msg) + msg.size;
1847 LV2Plugin::work(uint32_t size, const void* data)
1849 return _impl->work_iface->work(
1850 _impl->instance->lv2_handle, work_respond, this, size, data);
1854 LV2Plugin::work_response(uint32_t size, const void* data)
1856 return _impl->work_iface->work_response(
1857 _impl->instance->lv2_handle, size, data);
1861 LV2Plugin::set_insert_id(PBD::ID id)
1863 if (_insert_id == "0") {
1865 } else if (_insert_id != id) {
1866 lilv_state_free(_impl->state);
1867 _impl->state = NULL;
1873 LV2Plugin::set_state_dir (const std::string& d)
1875 _plugin_state_dir = d;
1879 LV2Plugin::set_state(const XMLNode& node, int version)
1882 XMLProperty const * prop;
1883 XMLNodeConstIterator iter;
1890 if (node.name() != state_node_name()) {
1891 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
1895 #ifndef NO_PLUGIN_STATE
1897 if (version < 3000) {
1898 nodes = node.children("port");
1900 nodes = node.children("Port");
1903 for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
1907 if ((prop = child->property("symbol")) != 0) {
1908 sym = prop->value().c_str();
1910 warning << _("LV2: port has no symbol, ignored") << endmsg;
1914 map<string, uint32_t>::iterator i = _port_indices.find(sym);
1916 if (i != _port_indices.end()) {
1917 port_id = i->second;
1919 warning << _("LV2: port has unknown index, ignored") << endmsg;
1923 if ((prop = child->property("value")) != 0) {
1924 value = prop->value().c_str();
1926 warning << _("LV2: port has no value, ignored") << endmsg;
1930 set_parameter(port_id, atof(value));
1933 if ((prop = node.property("template-dir")) != 0) {
1934 set_state_dir (prop->value ());
1938 if ((prop = node.property("state-dir")) != 0) {
1939 if (sscanf(prop->value().c_str(), "state%u", &_state_version) != 1) {
1940 error << string_compose(
1941 "LV2: failed to parse state version from \"%1\"",
1942 prop->value()) << endmsg;
1945 std::string state_file = Glib::build_filename(
1947 Glib::build_filename(prop->value(), "state.ttl"));
1949 LilvState* state = lilv_state_new_from_file(
1950 _world.world, _uri_map.urid_map(), NULL, state_file.c_str());
1952 lilv_state_restore(state, _impl->instance, NULL, NULL, 0, NULL);
1953 lilv_state_free(_impl->state);
1954 _impl->state = state;
1957 if (!_plugin_state_dir.empty ()) {
1958 // force save with session, next time (increment counter)
1959 lilv_state_free (_impl->state);
1960 _impl->state = NULL;
1964 latency_compute_run();
1967 return Plugin::set_state(node, version);
1971 LV2Plugin::get_parameter_descriptor(uint32_t which, ParameterDescriptor& desc) const
1973 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, which);
1975 error << string_compose("LV2: get descriptor of non-existent port %1", which)
1980 LilvNodes* portunits;
1981 LilvNode *def, *min, *max;
1982 lilv_port_get_range(_impl->plugin, port, &def, &min, &max);
1983 portunits = lilv_port_get_value(_impl->plugin, port, _world.units_unit);
1985 LilvNode* steps = lilv_port_get(_impl->plugin, port, _world.ext_rangeSteps);
1987 // TODO: Once we can rely on lilv 0.18.0 being present,
1988 // load_parameter_descriptor() can be used for ports as well
1989 desc.integer_step = lilv_port_has_property(_impl->plugin, port, _world.lv2_integer);
1990 desc.toggled = lilv_port_has_property(_impl->plugin, port, _world.lv2_toggled);
1991 desc.logarithmic = lilv_port_has_property(_impl->plugin, port, _world.ext_logarithmic);
1992 desc.sr_dependent = lilv_port_has_property(_impl->plugin, port, _world.lv2_sampleRate);
1993 desc.label = lilv_node_as_string(lilv_port_get_name(_impl->plugin, port));
1994 desc.normal = def ? lilv_node_as_float(def) : 0.0f;
1995 desc.lower = min ? lilv_node_as_float(min) : 0.0f;
1996 desc.upper = max ? lilv_node_as_float(max) : 1.0f;
1997 load_parameter_descriptor_units(_world.world, desc, portunits);
1999 if (desc.sr_dependent) {
2000 desc.lower *= _session.frame_rate ();
2001 desc.upper *= _session.frame_rate ();
2004 desc.min_unbound = false; // TODO: LV2 extension required
2005 desc.max_unbound = false; // TODO: LV2 extension required
2007 desc.enumeration = lilv_port_has_property(_impl->plugin, port, _world.lv2_enumeration);
2008 desc.scale_points = get_scale_points(which);
2010 desc.update_steps();
2013 //override auto-calculated steps in update_steps()
2014 float s = lilv_node_as_float (steps);
2015 const float delta = desc.upper - desc.lower;
2017 desc.step = desc.smallstep = (delta / s);
2018 desc.largestep = std::min ((delta / 5.0f), 10.f * desc.smallstep);
2020 if (desc.logarithmic) {
2021 // TODO marry AutomationControl::internal_to_interface () with
2022 // http://lv2plug.in/ns/ext/port-props/#rangeSteps
2023 desc.smallstep = desc.smallstep / logf(s);
2024 desc.step = desc.step / logf(s);
2025 desc.largestep = desc.largestep / logf(s);
2026 } else if (desc.integer_step) {
2027 desc.smallstep = 1.0;
2028 desc.step = std::max(1.f, rintf (desc.step));
2029 desc.largestep = std::max(1.f, rintf (desc.largestep));
2031 DEBUG_TRACE(DEBUG::LV2, string_compose("parameter %1 small: %2, step: %3 largestep: %4\n",
2032 which, desc.smallstep, desc.step, desc.largestep));
2036 lilv_node_free(def);
2037 lilv_node_free(min);
2038 lilv_node_free(max);
2039 lilv_node_free(steps);
2040 lilv_nodes_free(portunits);
2045 Plugin::IOPortDescription
2046 LV2Plugin::describe_io_port (ARDOUR::DataType dt, bool input, uint32_t id) const
2048 PortFlags match = 0;
2050 case DataType::AUDIO:
2053 case DataType::MIDI:
2054 match = PORT_SEQUENCE | PORT_MIDI; // ignore old PORT_EVENT
2057 return Plugin::IOPortDescription ("?");
2061 match |= PORT_INPUT;
2063 match |= PORT_OUTPUT;
2067 uint32_t idx = UINT32_MAX;
2069 uint32_t const num_ports = parameter_count();
2070 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2071 PortFlags flags = _port_flags[port_index];
2072 if ((flags & match) == match) {
2079 if (idx == UINT32_MAX) {
2080 return Plugin::IOPortDescription ("?");
2083 LilvNode* name = lilv_port_get_name(_impl->plugin,
2084 lilv_plugin_get_port_by_index(_impl->plugin, idx));
2085 Plugin::IOPortDescription iod (lilv_node_as_string (name));
2086 lilv_node_free(name);
2088 if (lilv_port_has_property(_impl->plugin,
2089 lilv_plugin_get_port_by_index(_impl->plugin, idx), _world.lv2_isSideChain)) {
2090 iod.is_sidechain = true;
2096 LV2Plugin::describe_parameter(Evoral::Parameter which)
2098 if (( which.type() == PluginAutomation) && ( which.id() < parameter_count()) ) {
2100 if (lilv_port_has_property(_impl->plugin,
2101 lilv_plugin_get_port_by_index(_impl->plugin, which.id()), _world.ext_notOnGUI)) {
2102 return X_("hidden");
2105 if (lilv_port_has_property(_impl->plugin,
2106 lilv_plugin_get_port_by_index(_impl->plugin, which.id()), _world.lv2_freewheeling)) {
2107 return X_("hidden");
2110 if (lilv_port_has_property(_impl->plugin,
2111 lilv_plugin_get_port_by_index(_impl->plugin, which.id()), _world.lv2_reportsLatency)) {
2112 return X_("latency");
2115 LilvNode* name = lilv_port_get_name(_impl->plugin,
2116 lilv_plugin_get_port_by_index(_impl->plugin, which.id()));
2117 string ret(lilv_node_as_string(name));
2118 lilv_node_free(name);
2126 LV2Plugin::max_latency () const
2128 return _max_latency;
2132 LV2Plugin::signal_latency() const
2134 if (_latency_control_port) {
2135 return (framecnt_t)floor(*_latency_control_port);
2141 set<Evoral::Parameter>
2142 LV2Plugin::automatable() const
2144 set<Evoral::Parameter> ret;
2146 for (uint32_t i = 0; i < parameter_count(); ++i) {
2147 if (parameter_is_input(i) && parameter_is_control(i) && !(_port_flags[i] & PORT_NOAUTO)) {
2148 ret.insert(ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
2152 for (PropertyDescriptors::const_iterator p = _property_descriptors.begin();
2153 p != _property_descriptors.end();
2155 ret.insert(ret.end(), Evoral::Parameter(PluginPropertyAutomation, 0, p->first));
2161 LV2Plugin::set_automation_control (uint32_t i, boost::shared_ptr<AutomationControl> c)
2163 if ((_port_flags[i] & (PORT_CTRLED | PORT_CTRLER))) {
2164 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("Ctrl Port %1\n", i));
2165 _ctrl_map [i] = AutomationCtrlPtr (new AutomationCtrl(c));
2169 LV2Plugin::AutomationCtrlPtr
2170 LV2Plugin::get_automation_control (uint32_t i)
2172 if (_ctrl_map.find (i) == _ctrl_map.end()) {
2173 return AutomationCtrlPtr ();
2175 return _ctrl_map[i];
2179 LV2Plugin::activate()
2181 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 activate\n", name()));
2183 if (!_was_activated) {
2184 lilv_instance_activate(_impl->instance);
2185 _was_activated = true;
2190 LV2Plugin::deactivate()
2192 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 deactivate\n", name()));
2194 if (_was_activated) {
2195 lilv_instance_deactivate(_impl->instance);
2196 _was_activated = false;
2201 LV2Plugin::cleanup()
2203 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 cleanup\n", name()));
2206 lilv_instance_free(_impl->instance);
2207 _impl->instance = NULL;
2211 LV2Plugin::allocate_atom_event_buffers()
2213 /* reserve local scratch buffers for ATOM event-queues */
2214 const LilvPlugin* p = _impl->plugin;
2216 /* count non-MIDI atom event-ports
2217 * TODO: nicely ask drobilla to make a lilv_ call for that
2219 int count_atom_out = 0;
2220 int count_atom_in = 0;
2221 int minimumSize = 32768; // TODO use a per-port minimum-size
2222 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
2223 const LilvPort* port = lilv_plugin_get_port_by_index(p, i);
2224 if (lilv_port_is_a(p, port, _world.atom_AtomPort)) {
2225 LilvNodes* buffer_types = lilv_port_get_value(
2226 p, port, _world.atom_bufferType);
2227 LilvNodes* atom_supports = lilv_port_get_value(
2228 p, port, _world.atom_supports);
2230 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
2231 if (lilv_port_is_a(p, port, _world.lv2_InputPort)) {
2234 if (lilv_port_is_a(p, port, _world.lv2_OutputPort)) {
2237 LilvNodes* min_size_v = lilv_port_get_value(_impl->plugin, port, _world.rsz_minimumSize);
2238 LilvNode* min_size = min_size_v ? lilv_nodes_get_first(min_size_v) : NULL;
2239 if (min_size && lilv_node_is_int(min_size)) {
2240 minimumSize = std::max(minimumSize, lilv_node_as_int(min_size));
2242 lilv_nodes_free(min_size_v);
2244 lilv_nodes_free(buffer_types);
2245 lilv_nodes_free(atom_supports);
2249 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 need buffers for %2 atom-in and %3 atom-out event-ports\n",
2250 name(), count_atom_in, count_atom_out));
2252 const int total_atom_buffers = (count_atom_in + count_atom_out);
2253 if (_atom_ev_buffers || total_atom_buffers == 0) {
2257 DEBUG_TRACE(DEBUG::LV2, string_compose("allocate %1 atom_ev_buffers of %2 bytes\n", total_atom_buffers, minimumSize));
2258 _atom_ev_buffers = (LV2_Evbuf**) malloc((total_atom_buffers + 1) * sizeof(LV2_Evbuf*));
2259 for (int i = 0; i < total_atom_buffers; ++i ) {
2260 _atom_ev_buffers[i] = lv2_evbuf_new(minimumSize, LV2_EVBUF_ATOM,
2261 _uri_map.urids.atom_Chunk, _uri_map.urids.atom_Sequence);
2263 _atom_ev_buffers[total_atom_buffers] = 0;
2267 /** Write an ardour position/time/tempo/meter as an LV2 event.
2268 * @return true on success.
2271 write_position(LV2_Atom_Forge* forge,
2273 const TempoMetric& t,
2274 Timecode::BBT_Time& bbt,
2276 framepos_t position,
2279 const URIMap::URIDs& urids = URIMap::instance().urids;
2281 uint8_t pos_buf[256];
2282 lv2_atom_forge_set_buffer(forge, pos_buf, sizeof(pos_buf));
2283 LV2_Atom_Forge_Frame frame;
2284 #ifdef HAVE_LV2_1_10_0
2285 lv2_atom_forge_object(forge, &frame, 1, urids.time_Position);
2286 lv2_atom_forge_key(forge, urids.time_frame);
2287 lv2_atom_forge_long(forge, position);
2288 lv2_atom_forge_key(forge, urids.time_speed);
2289 lv2_atom_forge_float(forge, speed);
2290 lv2_atom_forge_key(forge, urids.time_barBeat);
2291 lv2_atom_forge_float(forge, bbt.beats - 1 +
2292 (bbt.ticks / Timecode::BBT_Time::ticks_per_beat));
2293 lv2_atom_forge_key(forge, urids.time_bar);
2294 lv2_atom_forge_long(forge, bbt.bars - 1);
2295 lv2_atom_forge_key(forge, urids.time_beatUnit);
2296 lv2_atom_forge_int(forge, t.meter().note_divisor());
2297 lv2_atom_forge_key(forge, urids.time_beatsPerBar);
2298 lv2_atom_forge_float(forge, t.meter().divisions_per_bar());
2299 lv2_atom_forge_key(forge, urids.time_beatsPerMinute);
2300 lv2_atom_forge_float(forge, t.tempo().beats_per_minute());
2302 lv2_atom_forge_blank(forge, &frame, 1, urids.time_Position);
2303 lv2_atom_forge_property_head(forge, urids.time_frame, 0);
2304 lv2_atom_forge_long(forge, position);
2305 lv2_atom_forge_property_head(forge, urids.time_speed, 0);
2306 lv2_atom_forge_float(forge, speed);
2307 lv2_atom_forge_property_head(forge, urids.time_barBeat, 0);
2308 lv2_atom_forge_float(forge, bbt.beats - 1 +
2309 (bbt.ticks / Timecode::BBT_Time::ticks_per_beat));
2310 lv2_atom_forge_property_head(forge, urids.time_bar, 0);
2311 lv2_atom_forge_long(forge, bbt.bars - 1);
2312 lv2_atom_forge_property_head(forge, urids.time_beatUnit, 0);
2313 lv2_atom_forge_int(forge, t.meter().note_divisor());
2314 lv2_atom_forge_property_head(forge, urids.time_beatsPerBar, 0);
2315 lv2_atom_forge_float(forge, t.meter().divisions_per_bar());
2316 lv2_atom_forge_property_head(forge, urids.time_beatsPerMinute, 0);
2317 lv2_atom_forge_float(forge, t.tempo().beats_per_minute());
2320 LV2_Evbuf_Iterator end = lv2_evbuf_end(buf);
2321 const LV2_Atom* const atom = (const LV2_Atom*)pos_buf;
2322 return lv2_evbuf_write(&end, offset, 0, atom->type, atom->size,
2323 (const uint8_t*)(atom + 1));
2327 LV2Plugin::connect_and_run(BufferSet& bufs,
2328 framepos_t start, framepos_t end, double speed,
2329 ChanMapping in_map, ChanMapping out_map,
2330 pframes_t nframes, framecnt_t offset)
2332 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 run %2 offset %3\n", name(), nframes, offset));
2333 Plugin::connect_and_run(bufs, start, end, speed, in_map, out_map, nframes, offset);
2335 cycles_t then = get_cycles();
2337 TempoMap& tmap = _session.tempo_map();
2338 Metrics::const_iterator metric_i = tmap.metrics_end();
2339 TempoMetric tmetric = tmap.metric_at(start, &metric_i);
2341 if (_freewheel_control_port) {
2342 *_freewheel_control_port = _session.engine().freewheeling() ? 1.f : 0.f;
2345 if (_bpm_control_port) {
2346 *_bpm_control_port = tmetric.tempo().beats_per_minute();
2350 if (_can_write_automation && start != _next_cycle_start) {
2351 // add guard-points after locating
2352 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2353 i->second->guard = true;
2358 ChanCount bufs_count;
2359 bufs_count.set(DataType::AUDIO, 1);
2360 bufs_count.set(DataType::MIDI, 1);
2361 BufferSet& silent_bufs = _session.get_silent_buffers(bufs_count);
2362 BufferSet& scratch_bufs = _session.get_scratch_buffers(bufs_count);
2363 uint32_t const num_ports = parameter_count();
2364 uint32_t const nil_index = std::numeric_limits<uint32_t>::max();
2366 uint32_t audio_in_index = 0;
2367 uint32_t audio_out_index = 0;
2368 uint32_t midi_in_index = 0;
2369 uint32_t midi_out_index = 0;
2370 uint32_t atom_port_index = 0;
2371 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2373 uint32_t index = nil_index;
2374 PortFlags flags = _port_flags[port_index];
2376 if (flags & PORT_AUDIO) {
2377 if (flags & PORT_INPUT) {
2378 index = in_map.get(DataType::AUDIO, audio_in_index++, &valid);
2380 ? bufs.get_audio(index).data(offset)
2381 : silent_bufs.get_audio(0).data(offset);
2383 index = out_map.get(DataType::AUDIO, audio_out_index++, &valid);
2385 ? bufs.get_audio(index).data(offset)
2386 : scratch_bufs.get_audio(0).data(offset);
2388 } else if (flags & (PORT_EVENT|PORT_SEQUENCE)) {
2389 /* FIXME: The checks here for bufs.count().n_midi() > index shouldn't
2390 be necessary, but the mapping is illegal in some cases. Ideally
2391 that should be fixed, but this is easier...
2393 if (flags & PORT_MIDI) {
2394 if (flags & PORT_INPUT) {
2395 index = in_map.get(DataType::MIDI, midi_in_index++, &valid);
2397 index = out_map.get(DataType::MIDI, midi_out_index++, &valid);
2399 if (valid && bufs.count().n_midi() > index) {
2400 /* Note, ensure_lv2_bufsize() is not RT safe!
2401 * However free()/alloc() is only called if a
2402 * plugin requires a rsz:minimumSize buffersize
2403 * and the existing buffer if smaller.
2405 bufs.ensure_lv2_bufsize((flags & PORT_INPUT), index, _port_minimumSize[port_index]);
2406 _ev_buffers[port_index] = bufs.get_lv2_midi(
2407 (flags & PORT_INPUT), index, (flags & PORT_EVENT));
2409 } else if ((flags & PORT_POSITION) && (flags & PORT_INPUT)) {
2410 lv2_evbuf_reset(_atom_ev_buffers[atom_port_index], true);
2411 _ev_buffers[port_index] = _atom_ev_buffers[atom_port_index++];
2415 if (valid && (flags & PORT_INPUT)) {
2416 Timecode::BBT_Time bbt;
2417 if ((flags & PORT_POSITION)) {
2418 if (start != _next_cycle_start ||
2419 speed != _next_cycle_speed) {
2420 // Transport has changed, write position at cycle start
2421 bbt = tmap.bbt_at_frame (start);
2422 write_position(&_impl->forge, _ev_buffers[port_index],
2423 tmetric, bbt, speed, start, 0);
2427 // Get MIDI iterator range (empty range if no MIDI)
2428 MidiBuffer::iterator m = (index != nil_index)
2429 ? bufs.get_midi(index).begin()
2430 : silent_bufs.get_midi(0).end();
2431 MidiBuffer::iterator m_end = (index != nil_index)
2432 ? bufs.get_midi(index).end()
2435 // Now merge MIDI and any transport events into the buffer
2436 const uint32_t type = _uri_map.urids.midi_MidiEvent;
2437 const framepos_t tend = end;
2439 while (m != m_end || (metric_i != tmap.metrics_end() &&
2440 (*metric_i)->frame() < tend)) {
2441 MetricSection* metric = (metric_i != tmap.metrics_end())
2443 if (m != m_end && (!metric || metric->frame() > (*m).time())) {
2444 const Evoral::MIDIEvent<framepos_t> ev(*m, false);
2445 if (ev.time() < nframes) {
2446 LV2_Evbuf_Iterator eend = lv2_evbuf_end(_ev_buffers[port_index]);
2447 lv2_evbuf_write(&eend, ev.time(), 0, type, ev.size(), ev.buffer());
2451 tmetric.set_metric(metric);
2452 bbt = tmap.bbt_at_pulse (metric->pulse());
2453 write_position(&_impl->forge, _ev_buffers[port_index],
2454 tmetric, bbt, speed,
2456 metric->frame() - start);
2460 } else if (!valid) {
2461 // Nothing we understand or care about, connect to scratch
2462 // see note for midi-buffer size above
2463 scratch_bufs.ensure_lv2_bufsize((flags & PORT_INPUT),
2464 0, _port_minimumSize[port_index]);
2465 _ev_buffers[port_index] = scratch_bufs.get_lv2_midi(
2466 (flags & PORT_INPUT), 0, (flags & PORT_EVENT));
2469 buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
2471 continue; // Control port, leave buffer alone
2473 lilv_instance_connect_port(_impl->instance, port_index, buf);
2476 // Read messages from UI and push into appropriate buffers
2478 uint32_t read_space = _from_ui->read_space();
2479 while (read_space > sizeof(UIMessage)) {
2481 if (_from_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
2482 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
2485 vector<uint8_t> body(msg.size);
2486 if (_from_ui->read(&body[0], msg.size) != msg.size) {
2487 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
2490 if (msg.protocol == URIMap::instance().urids.atom_eventTransfer) {
2491 LV2_Evbuf* buf = _ev_buffers[msg.index];
2492 LV2_Evbuf_Iterator i = lv2_evbuf_end(buf);
2493 const LV2_Atom* const atom = (const LV2_Atom*)&body[0];
2494 if (!lv2_evbuf_write(&i, nframes - 1, 0, atom->type, atom->size,
2495 (const uint8_t*)(atom + 1))) {
2496 error << "Failed to write data to LV2 event buffer\n";
2499 error << "Received unknown message type from UI" << endmsg;
2501 read_space -= sizeof(UIMessage) + msg.size;
2508 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2509 PortFlags flags = _port_flags[port_index];
2512 /* TODO ask drobilla about comment
2513 * "Make Ardour event buffers generic so plugins can communicate"
2514 * in libs/ardour/buffer_set.cc:310
2516 * ideally the user could choose which of the following two modes
2517 * to use (e.g. instrument/effect chains MIDI OUT vs MIDI TRHU).
2519 * This implementation follows the discussion on IRC Mar 16 2013 16:47 UTC
2520 * 16:51 < drobilla> rgareus: [..] i.e always replace with MIDI output [of LV2 plugin] if it's there
2521 * 16:52 < drobilla> rgareus: That would probably be good enough [..] to make users not complain
2522 * for quite a while at least ;)
2524 // copy output of LV2 plugin's MIDI port to Ardour MIDI buffers -- MIDI OUT
2525 if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE|PORT_MIDI))) {
2526 const uint32_t buf_index = out_map.get(
2527 DataType::MIDI, midi_out_index++, &valid);
2529 bufs.forward_lv2_midi(_ev_buffers[port_index], buf_index);
2532 // Flush MIDI (write back to Ardour MIDI buffers) -- MIDI THRU
2533 else if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE))) {
2534 const uint32_t buf_index = out_map.get(
2535 DataType::MIDI, midi_out_index++, &valid);
2537 bufs.flush_lv2_midi(true, buf_index);
2541 // Write messages to UI
2542 if ((_to_ui || _can_write_automation || _patch_port_out_index != (uint32_t)-1) &&
2543 (flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE))) {
2544 LV2_Evbuf* buf = _ev_buffers[port_index];
2545 for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(buf);
2546 lv2_evbuf_is_valid(i);
2547 i = lv2_evbuf_next(i)) {
2548 uint32_t frames, subframes, type, size;
2550 lv2_evbuf_get(i, &frames, &subframes, &type, &size, &data);
2553 // Intercept Automation Write Events
2554 if ((flags & PORT_AUTOCTRL)) {
2555 LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2556 if (atom->type == _uri_map.urids.atom_Blank ||
2557 atom->type == _uri_map.urids.atom_Object) {
2558 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2559 if (obj->body.otype == _uri_map.urids.auto_event) {
2560 // only if transport_rolling ??
2561 const LV2_Atom* parameter = NULL;
2562 const LV2_Atom* value = NULL;
2563 lv2_atom_object_get(obj,
2564 _uri_map.urids.auto_parameter, ¶meter,
2565 _uri_map.urids.auto_value, &value,
2567 if (parameter && value) {
2568 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2569 const float v = ((const LV2_Atom_Float*)value)->body;
2570 // -> add automation event..
2571 DEBUG_TRACE(DEBUG::LV2Automate,
2572 string_compose ("Event p: %1 t: %2 v: %3\n", p, frames, v));
2573 AutomationCtrlPtr c = get_automation_control (p);
2575 (c->ac->automation_state() == Touch || c->ac->automation_state() == Write)
2577 framepos_t when = std::max ((framepos_t) 0, start + frames - _current_latency);
2578 assert (start + frames - _current_latency >= 0);
2581 c->ac->list()->add (when, v, true, true);
2583 c->ac->set_double (v, when, true);
2588 else if (obj->body.otype == _uri_map.urids.auto_setup) {
2589 // TODO optional arguments, for now we assume the plugin
2590 // writes automation for its own inputs
2591 // -> put them in "touch" mode (preferably "exclusive plugin touch(TM)"
2592 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2593 if (_port_flags[i->first] & PORT_CTRLED) {
2594 DEBUG_TRACE(DEBUG::LV2Automate,
2595 string_compose ("Setup p: %1\n", i->first));
2596 i->second->ac->set_automation_state (Touch);
2600 else if (obj->body.otype == _uri_map.urids.auto_finalize) {
2601 // set [touched] parameters to "play" ??
2602 // allow plugin to change its mode (from analyze to apply)
2603 const LV2_Atom* parameter = NULL;
2604 const LV2_Atom* value = NULL;
2605 lv2_atom_object_get(obj,
2606 _uri_map.urids.auto_parameter, ¶meter,
2607 _uri_map.urids.auto_value, &value,
2609 if (parameter && value) {
2610 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2611 const float v = ((const LV2_Atom_Float*)value)->body;
2612 AutomationCtrlPtr c = get_automation_control (p);
2613 DEBUG_TRACE(DEBUG::LV2Automate,
2614 string_compose ("Finalize p: %1 v: %2\n", p, v));
2615 if (c && _port_flags[p] & PORT_CTRLER) {
2616 c->ac->set_value(v, Controllable::NoGroup);
2619 DEBUG_TRACE(DEBUG::LV2Automate, "Finalize\n");
2621 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2622 // guard will be false if an event was written
2623 if ((_port_flags[i->first] & PORT_CTRLED) && !i->second->guard) {
2624 DEBUG_TRACE(DEBUG::LV2Automate,
2625 string_compose ("Thin p: %1\n", i->first));
2626 i->second->ac->alist ()->thin (20);
2630 else if (obj->body.otype == _uri_map.urids.auto_start) {
2631 const LV2_Atom* parameter = NULL;
2632 lv2_atom_object_get(obj,
2633 _uri_map.urids.auto_parameter, ¶meter,
2636 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2637 AutomationCtrlPtr c = get_automation_control (p);
2638 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("Start Touch p: %1\n", p));
2640 c->ac->start_touch (std::max ((framepos_t)0, start - _current_latency));
2645 else if (obj->body.otype == _uri_map.urids.auto_end) {
2646 const LV2_Atom* parameter = NULL;
2647 lv2_atom_object_get(obj,
2648 _uri_map.urids.auto_parameter, ¶meter,
2651 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2652 AutomationCtrlPtr c = get_automation_control (p);
2653 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("End Touch p: %1\n", p));
2655 c->ac->stop_touch (true, std::max ((framepos_t)0, start - _current_latency));
2663 // Intercept patch change messages to emit PropertyChanged signal
2664 if ((flags & PORT_PATCHMSG)) {
2665 LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2666 if (atom->type == _uri_map.urids.atom_Blank ||
2667 atom->type == _uri_map.urids.atom_Object) {
2668 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2669 if (obj->body.otype == _uri_map.urids.patch_Set) {
2670 const LV2_Atom* property = NULL;
2671 const LV2_Atom* value = NULL;
2672 lv2_atom_object_get(obj,
2673 _uri_map.urids.patch_property, &property,
2674 _uri_map.urids.patch_value, &value,
2677 if (property && value &&
2678 property->type == _uri_map.urids.atom_URID &&
2679 value->type == _uri_map.urids.atom_Path) {
2680 const uint32_t prop_id = ((const LV2_Atom_URID*)property)->body;
2681 const char* path = (const char*)LV2_ATOM_BODY_CONST(value);
2683 // Emit PropertyChanged signal for UI
2684 // TODO: This should emit the control's Changed signal
2685 PropertyChanged(prop_id, Variant(Variant::PATH, path));
2687 std::cerr << "warning: patch:Set for unknown property" << std::endl;
2693 if (!_to_ui) continue;
2694 write_to_ui(port_index, URIMap::instance().urids.atom_eventTransfer,
2695 size + sizeof(LV2_Atom),
2696 data - sizeof(LV2_Atom));
2701 cycles_t now = get_cycles();
2702 set_cycles((uint32_t)(now - then));
2704 // Update expected transport information for next cycle so we can detect changes
2705 _next_cycle_speed = speed;
2706 _next_cycle_start = end;
2708 if (_latency_control_port) {
2709 framecnt_t new_latency = signal_latency ();
2710 _current_latency = new_latency;
2716 LV2Plugin::parameter_is_control(uint32_t param) const
2718 assert(param < _port_flags.size());
2719 return _port_flags[param] & PORT_CONTROL;
2723 LV2Plugin::parameter_is_audio(uint32_t param) const
2725 assert(param < _port_flags.size());
2726 return _port_flags[param] & PORT_AUDIO;
2730 LV2Plugin::parameter_is_event(uint32_t param) const
2732 assert(param < _port_flags.size());
2733 return _port_flags[param] & PORT_EVENT;
2737 LV2Plugin::parameter_is_output(uint32_t param) const
2739 assert(param < _port_flags.size());
2740 return _port_flags[param] & PORT_OUTPUT;
2744 LV2Plugin::parameter_is_input(uint32_t param) const
2746 assert(param < _port_flags.size());
2747 return _port_flags[param] & PORT_INPUT;
2751 LV2Plugin::designated_bypass_port ()
2754 const LilvPort* port = NULL;
2755 LilvNode* designation = lilv_new_uri (_world.world, LV2_PROCESSING_URI__enable);
2756 port = lilv_plugin_get_port_by_designation (
2757 _impl->plugin, _world.lv2_InputPort, designation);
2758 lilv_node_free(designation);
2760 return lilv_port_get_index (_impl->plugin, port);
2767 LV2Plugin::print_parameter(uint32_t param, char* buf, uint32_t len) const
2770 if (param < parameter_count()) {
2771 snprintf(buf, len, "%.3f", get_parameter(param));
2778 boost::shared_ptr<ScalePoints>
2779 LV2Plugin::get_scale_points(uint32_t port_index) const
2781 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, port_index);
2782 LilvScalePoints* points = lilv_port_get_scale_points(_impl->plugin, port);
2784 boost::shared_ptr<ScalePoints> ret;
2789 ret = boost::shared_ptr<ScalePoints>(new ScalePoints());
2791 LILV_FOREACH(scale_points, i, points) {
2792 const LilvScalePoint* p = lilv_scale_points_get(points, i);
2793 const LilvNode* label = lilv_scale_point_get_label(p);
2794 const LilvNode* value = lilv_scale_point_get_value(p);
2795 if (label && (lilv_node_is_float(value) || lilv_node_is_int(value))) {
2796 ret->insert(make_pair(lilv_node_as_string(label),
2797 lilv_node_as_float(value)));
2801 lilv_scale_points_free(points);
2806 LV2Plugin::run(pframes_t nframes)
2808 uint32_t const N = parameter_count();
2809 for (uint32_t i = 0; i < N; ++i) {
2810 if (parameter_is_control(i) && parameter_is_input(i)) {
2811 _control_data[i] = _shadow_data[i];
2815 lilv_instance_run(_impl->instance, nframes);
2817 if (_impl->work_iface) {
2818 _worker->emit_responses();
2819 if (_impl->work_iface->end_run) {
2820 _impl->work_iface->end_run(_impl->instance->lv2_handle);
2826 LV2Plugin::latency_compute_run()
2828 if (!_latency_control_port) {
2832 // Run the plugin so that it can set its latency parameter
2834 bool was_activated = _was_activated;
2837 uint32_t port_index = 0;
2838 uint32_t in_index = 0;
2839 uint32_t out_index = 0;
2841 // this is done in the main thread. non realtime.
2842 const framecnt_t bufsize = _engine.samples_per_cycle();
2843 float *buffer = (float*) malloc(_engine.samples_per_cycle() * sizeof(float));
2845 memset(buffer, 0, sizeof(float) * bufsize);
2847 // FIXME: Ensure plugins can handle in-place processing
2851 while (port_index < parameter_count()) {
2852 if (parameter_is_audio(port_index)) {
2853 if (parameter_is_input(port_index)) {
2854 lilv_instance_connect_port(_impl->instance, port_index, buffer);
2856 } else if (parameter_is_output(port_index)) {
2857 lilv_instance_connect_port(_impl->instance, port_index, buffer);
2866 if (was_activated) {
2873 LV2Plugin::Impl::designated_input (const char* uri, void** bufptrs[], void** bufptr)
2875 const LilvPort* port = NULL;
2876 LilvNode* designation = lilv_new_uri(_world.world, uri);
2877 port = lilv_plugin_get_port_by_designation(
2878 plugin, _world.lv2_InputPort, designation);
2879 lilv_node_free(designation);
2881 bufptrs[lilv_port_get_index(plugin, port)] = bufptr;
2886 static bool lv2_filter (const string& str, void* /*arg*/)
2888 /* Not a dotfile, has a prefix before a period, suffix is "lv2" */
2890 return str[0] != '.' && (str.length() > 3 && str.find (".lv2") == (str.length() - 4));
2894 LV2World::LV2World()
2895 : world(lilv_world_new())
2896 , _bundle_checked(false)
2898 atom_AtomPort = lilv_new_uri(world, LV2_ATOM__AtomPort);
2899 atom_Chunk = lilv_new_uri(world, LV2_ATOM__Chunk);
2900 atom_Sequence = lilv_new_uri(world, LV2_ATOM__Sequence);
2901 atom_bufferType = lilv_new_uri(world, LV2_ATOM__bufferType);
2902 atom_supports = lilv_new_uri(world, LV2_ATOM__supports);
2903 atom_eventTransfer = lilv_new_uri(world, LV2_ATOM__eventTransfer);
2904 ev_EventPort = lilv_new_uri(world, LILV_URI_EVENT_PORT);
2905 ext_logarithmic = lilv_new_uri(world, LV2_PORT_PROPS__logarithmic);
2906 ext_notOnGUI = lilv_new_uri(world, LV2_PORT_PROPS__notOnGUI);
2907 ext_expensive = lilv_new_uri(world, LV2_PORT_PROPS__expensive);
2908 ext_causesArtifacts= lilv_new_uri(world, LV2_PORT_PROPS__causesArtifacts);
2909 ext_notAutomatic = lilv_new_uri(world, LV2_PORT_PROPS__notAutomatic);
2910 ext_rangeSteps = lilv_new_uri(world, LV2_PORT_PROPS__rangeSteps);
2911 lv2_AudioPort = lilv_new_uri(world, LILV_URI_AUDIO_PORT);
2912 lv2_ControlPort = lilv_new_uri(world, LILV_URI_CONTROL_PORT);
2913 lv2_InputPort = lilv_new_uri(world, LILV_URI_INPUT_PORT);
2914 lv2_OutputPort = lilv_new_uri(world, LILV_URI_OUTPUT_PORT);
2915 lv2_inPlaceBroken = lilv_new_uri(world, LV2_CORE__inPlaceBroken);
2916 lv2_isSideChain = lilv_new_uri(world, LV2_CORE_PREFIX "isSideChain");
2917 lv2_integer = lilv_new_uri(world, LV2_CORE__integer);
2918 lv2_default = lilv_new_uri(world, LV2_CORE__default);
2919 lv2_minimum = lilv_new_uri(world, LV2_CORE__minimum);
2920 lv2_maximum = lilv_new_uri(world, LV2_CORE__maximum);
2921 lv2_reportsLatency = lilv_new_uri(world, LV2_CORE__reportsLatency);
2922 lv2_sampleRate = lilv_new_uri(world, LV2_CORE__sampleRate);
2923 lv2_toggled = lilv_new_uri(world, LV2_CORE__toggled);
2924 lv2_enumeration = lilv_new_uri(world, LV2_CORE__enumeration);
2925 lv2_freewheeling = lilv_new_uri(world, LV2_CORE__freeWheeling);
2926 midi_MidiEvent = lilv_new_uri(world, LILV_URI_MIDI_EVENT);
2927 rdfs_comment = lilv_new_uri(world, LILV_NS_RDFS "comment");
2928 rdfs_label = lilv_new_uri(world, LILV_NS_RDFS "label");
2929 rdfs_range = lilv_new_uri(world, LILV_NS_RDFS "range");
2930 rsz_minimumSize = lilv_new_uri(world, LV2_RESIZE_PORT__minimumSize);
2931 time_Position = lilv_new_uri(world, LV2_TIME__Position);
2932 ui_GtkUI = lilv_new_uri(world, LV2_UI__GtkUI);
2933 ui_external = lilv_new_uri(world, "http://lv2plug.in/ns/extensions/ui#external");
2934 ui_externalkx = lilv_new_uri(world, "http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget");
2935 units_unit = lilv_new_uri(world, LV2_UNITS__unit);
2936 units_render = lilv_new_uri(world, LV2_UNITS__render);
2937 units_hz = lilv_new_uri(world, LV2_UNITS__hz);
2938 units_midiNote = lilv_new_uri(world, LV2_UNITS__midiNote);
2939 units_db = lilv_new_uri(world, LV2_UNITS__db);
2940 patch_writable = lilv_new_uri(world, LV2_PATCH__writable);
2941 patch_Message = lilv_new_uri(world, LV2_PATCH__Message);
2943 lv2_noSampleAccurateCtrl = lilv_new_uri(world, "http://ardour.org/lv2/ext#noSampleAccurateControls");
2944 auto_can_write_automatation = lilv_new_uri(world, LV2_AUTOMATE_URI__can_write);
2945 auto_automation_control = lilv_new_uri(world, LV2_AUTOMATE_URI__control);
2946 auto_automation_controlled = lilv_new_uri(world, LV2_AUTOMATE_URI__controlled);
2947 auto_automation_controller = lilv_new_uri(world, LV2_AUTOMATE_URI__controller);
2949 #ifdef HAVE_LV2_1_2_0
2950 bufz_powerOf2BlockLength = lilv_new_uri(world, LV2_BUF_SIZE__powerOf2BlockLength);
2951 bufz_fixedBlockLength = lilv_new_uri(world, LV2_BUF_SIZE__fixedBlockLength);
2952 bufz_nominalBlockLength = lilv_new_uri(world, "http://lv2plug.in/ns/ext/buf-size#nominalBlockLength");
2957 LV2World::~LV2World()
2962 #ifdef HAVE_LV2_1_2_0
2963 lilv_node_free(bufz_nominalBlockLength);
2964 lilv_node_free(bufz_fixedBlockLength);
2965 lilv_node_free(bufz_powerOf2BlockLength);
2968 lilv_node_free(lv2_noSampleAccurateCtrl);
2969 lilv_node_free(auto_can_write_automatation);
2970 lilv_node_free(auto_automation_control);
2971 lilv_node_free(auto_automation_controlled);
2972 lilv_node_free(auto_automation_controller);
2974 lilv_node_free(patch_Message);
2975 lilv_node_free(patch_writable);
2976 lilv_node_free(units_hz);
2977 lilv_node_free(units_midiNote);
2978 lilv_node_free(units_db);
2979 lilv_node_free(units_unit);
2980 lilv_node_free(units_render);
2981 lilv_node_free(ui_externalkx);
2982 lilv_node_free(ui_external);
2983 lilv_node_free(ui_GtkUI);
2984 lilv_node_free(time_Position);
2985 lilv_node_free(rsz_minimumSize);
2986 lilv_node_free(rdfs_comment);
2987 lilv_node_free(rdfs_label);
2988 lilv_node_free(rdfs_range);
2989 lilv_node_free(midi_MidiEvent);
2990 lilv_node_free(lv2_enumeration);
2991 lilv_node_free(lv2_freewheeling);
2992 lilv_node_free(lv2_toggled);
2993 lilv_node_free(lv2_sampleRate);
2994 lilv_node_free(lv2_reportsLatency);
2995 lilv_node_free(lv2_integer);
2996 lilv_node_free(lv2_isSideChain);
2997 lilv_node_free(lv2_inPlaceBroken);
2998 lilv_node_free(lv2_OutputPort);
2999 lilv_node_free(lv2_InputPort);
3000 lilv_node_free(lv2_ControlPort);
3001 lilv_node_free(lv2_AudioPort);
3002 lilv_node_free(ext_rangeSteps);
3003 lilv_node_free(ext_notAutomatic);
3004 lilv_node_free(ext_causesArtifacts);
3005 lilv_node_free(ext_expensive);
3006 lilv_node_free(ext_notOnGUI);
3007 lilv_node_free(ext_logarithmic);
3008 lilv_node_free(ev_EventPort);
3009 lilv_node_free(atom_supports);
3010 lilv_node_free(atom_eventTransfer);
3011 lilv_node_free(atom_bufferType);
3012 lilv_node_free(atom_Sequence);
3013 lilv_node_free(atom_Chunk);
3014 lilv_node_free(atom_AtomPort);
3015 lilv_world_free(world);
3020 LV2World::load_bundled_plugins(bool verbose)
3022 if (!_bundle_checked) {
3024 cout << "Scanning folders for bundled LV2s: " << ARDOUR::lv2_bundled_search_path().to_string() << endl;
3027 vector<string> plugin_objects;
3028 find_paths_matching_filter (plugin_objects, ARDOUR::lv2_bundled_search_path(), lv2_filter, 0, true, true, true);
3029 for ( vector<string>::iterator x = plugin_objects.begin(); x != plugin_objects.end (); ++x) {
3030 #ifdef PLATFORM_WINDOWS
3031 string uri = "file:///" + *x + "/";
3033 string uri = "file://" + *x + "/";
3035 LilvNode *node = lilv_new_uri(world, uri.c_str());
3036 lilv_world_load_bundle(world, node);
3037 lilv_node_free(node);
3040 lilv_world_load_all(world);
3041 _bundle_checked = true;
3045 LV2PluginInfo::LV2PluginInfo (const char* plugin_uri)
3048 _plugin_uri = strdup(plugin_uri);
3051 LV2PluginInfo::~LV2PluginInfo()
3058 LV2PluginInfo::load(Session& session)
3062 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
3063 LilvNode* uri = lilv_new_uri(_world.world, _plugin_uri);
3064 if (!uri) { throw failed_constructor(); }
3065 const LilvPlugin* lp = lilv_plugins_get_by_uri(plugins, uri);
3066 if (!lp) { throw failed_constructor(); }
3067 plugin.reset(new LV2Plugin(session.engine(), session, lp, session.frame_rate()));
3068 lilv_node_free(uri);
3069 plugin->set_info(PluginInfoPtr(shared_from_this ()));
3071 } catch (failed_constructor& err) {
3072 return PluginPtr((Plugin*)0);
3078 std::vector<Plugin::PresetRecord>
3079 LV2PluginInfo::get_presets (bool /*user_only*/) const
3081 std::vector<Plugin::PresetRecord> p;
3082 #ifndef NO_PLUGIN_STATE
3083 const LilvPlugin* lp = NULL;
3086 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
3087 LilvNode* uri = lilv_new_uri(_world.world, _plugin_uri);
3088 if (!uri) { throw failed_constructor(); }
3089 lp = lilv_plugins_get_by_uri(plugins, uri);
3090 if (!lp) { throw failed_constructor(); }
3091 lilv_node_free(uri);
3092 } catch (failed_constructor& err) {
3096 // see LV2Plugin::find_presets
3097 LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
3098 LilvNode* pset_Preset = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
3099 LilvNode* rdfs_label = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
3101 LilvNodes* presets = lilv_plugin_get_related(lp, pset_Preset);
3102 LILV_FOREACH(nodes, i, presets) {
3103 const LilvNode* preset = lilv_nodes_get(presets, i);
3104 lilv_world_load_resource(_world.world, preset);
3105 LilvNode* name = get_value(_world.world, preset, rdfs_label);
3106 bool userpreset = true; // TODO
3108 p.push_back (Plugin::PresetRecord (lilv_node_as_string(preset), lilv_node_as_string(name), userpreset));
3109 lilv_node_free(name);
3112 lilv_nodes_free(presets);
3113 lilv_node_free(rdfs_label);
3114 lilv_node_free(pset_Preset);
3115 lilv_node_free(lv2_appliesTo);
3121 LV2PluginInfo::in_category (const std::string &c) const
3123 // TODO use untranslated lilv_plugin_get_class()
3124 // match gtk2_ardour/plugin_selector.cc
3125 if (category == c) {
3132 LV2PluginInfo::is_instrument () const
3134 if (category == "Instrument") {
3138 /* until we make sure that category remains untranslated in the lv2.ttl spec
3139 * and until most instruments also classify themselves as such, there's a 2nd check:
3141 if (n_inputs.n_midi() > 0 && n_inputs.n_audio() == 0 && n_outputs.n_audio() > 0) {
3149 LV2PluginInfo::discover()
3152 world.load_bundled_plugins();
3153 _world.load_bundled_plugins(true);
3155 PluginInfoList* plugs = new PluginInfoList;
3156 const LilvPlugins* plugins = lilv_world_get_all_plugins(world.world);
3158 LILV_FOREACH(plugins, i, plugins) {
3159 const LilvPlugin* p = lilv_plugins_get(plugins, i);
3160 const LilvNode* pun = lilv_plugin_get_uri(p);
3162 LV2PluginInfoPtr info(new LV2PluginInfo(lilv_node_as_string(pun)));
3164 LilvNode* name = lilv_plugin_get_name(p);
3165 if (!name || !lilv_plugin_get_port_by_index(p, 0)) {
3166 warning << "Ignoring invalid LV2 plugin "
3167 << lilv_node_as_string(lilv_plugin_get_uri(p))
3172 if (lilv_plugin_has_feature(p, world.lv2_inPlaceBroken)) {
3173 warning << string_compose(
3174 _("Ignoring LV2 plugin \"%1\" since it cannot do inplace processing."),
3175 lilv_node_as_string(name)) << endmsg;
3176 lilv_node_free(name);
3180 #ifdef HAVE_LV2_1_2_0
3181 LilvNodes *required_features = lilv_plugin_get_required_features (p);
3182 if (lilv_nodes_contains (required_features, world.bufz_powerOf2BlockLength) ||
3183 lilv_nodes_contains (required_features, world.bufz_fixedBlockLength)
3185 warning << string_compose(
3186 _("Ignoring LV2 plugin \"%1\" because its buffer-size requirements cannot be satisfied."),
3187 lilv_node_as_string(name)) << endmsg;
3188 lilv_nodes_free(required_features);
3189 lilv_node_free(name);
3192 lilv_nodes_free(required_features);
3197 info->name = string(lilv_node_as_string(name));
3198 lilv_node_free(name);
3199 ARDOUR::PluginScanMessage(_("LV2"), info->name, false);
3201 const LilvPluginClass* pclass = lilv_plugin_get_class(p);
3202 const LilvNode* label = lilv_plugin_class_get_label(pclass);
3203 info->category = lilv_node_as_string(label);
3205 LilvNode* author_name = lilv_plugin_get_author_name(p);
3206 info->creator = author_name ? string(lilv_node_as_string(author_name)) : "Unknown";
3207 lilv_node_free(author_name);
3209 info->path = "/NOPATH"; // Meaningless for LV2
3211 /* count atom-event-ports that feature
3212 * atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent>
3214 * TODO: nicely ask drobilla to make a lilv_ call for that
3216 int count_midi_out = 0;
3217 int count_midi_in = 0;
3218 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
3219 const LilvPort* port = lilv_plugin_get_port_by_index(p, i);
3220 if (lilv_port_is_a(p, port, world.atom_AtomPort)) {
3221 LilvNodes* buffer_types = lilv_port_get_value(
3222 p, port, world.atom_bufferType);
3223 LilvNodes* atom_supports = lilv_port_get_value(
3224 p, port, world.atom_supports);
3226 if (lilv_nodes_contains(buffer_types, world.atom_Sequence)
3227 && lilv_nodes_contains(atom_supports, world.midi_MidiEvent)) {
3228 if (lilv_port_is_a(p, port, world.lv2_InputPort)) {
3231 if (lilv_port_is_a(p, port, world.lv2_OutputPort)) {
3235 lilv_nodes_free(buffer_types);
3236 lilv_nodes_free(atom_supports);
3240 info->n_inputs.set_audio(
3241 lilv_plugin_get_num_ports_of_class(
3242 p, world.lv2_InputPort, world.lv2_AudioPort, NULL));
3243 info->n_inputs.set_midi(
3244 lilv_plugin_get_num_ports_of_class(
3245 p, world.lv2_InputPort, world.ev_EventPort, NULL)
3248 info->n_outputs.set_audio(
3249 lilv_plugin_get_num_ports_of_class(
3250 p, world.lv2_OutputPort, world.lv2_AudioPort, NULL));
3251 info->n_outputs.set_midi(
3252 lilv_plugin_get_num_ports_of_class(
3253 p, world.lv2_OutputPort, world.ev_EventPort, NULL)
3256 info->unique_id = lilv_node_as_uri(lilv_plugin_get_uri(p));
3257 info->index = 0; // Meaningless for LV2
3259 plugs->push_back(info);