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 bool LV2Plugin::force_state_save = false;
114 class LV2World : boost::noncopyable {
119 void load_bundled_plugins(bool verbose=false);
123 LilvNode* atom_AtomPort;
124 LilvNode* atom_Chunk;
125 LilvNode* atom_Sequence;
126 LilvNode* atom_bufferType;
127 LilvNode* atom_eventTransfer;
128 LilvNode* atom_supports;
129 LilvNode* ev_EventPort;
130 LilvNode* ext_logarithmic;
131 LilvNode* ext_notOnGUI;
132 LilvNode* ext_expensive;
133 LilvNode* ext_causesArtifacts;
134 LilvNode* ext_notAutomatic;
135 LilvNode* ext_rangeSteps;
136 LilvNode* lv2_AudioPort;
137 LilvNode* lv2_ControlPort;
138 LilvNode* lv2_InputPort;
139 LilvNode* lv2_OutputPort;
140 LilvNode* lv2_enumeration;
141 LilvNode* lv2_freewheeling;
142 LilvNode* lv2_inPlaceBroken;
143 LilvNode* lv2_isSideChain;
144 LilvNode* lv2_integer;
145 LilvNode* lv2_default;
146 LilvNode* lv2_minimum;
147 LilvNode* lv2_maximum;
148 LilvNode* lv2_reportsLatency;
149 LilvNode* lv2_sampleRate;
150 LilvNode* lv2_toggled;
151 LilvNode* midi_MidiEvent;
152 LilvNode* rdfs_comment;
153 LilvNode* rdfs_label;
154 LilvNode* rdfs_range;
155 LilvNode* rsz_minimumSize;
156 LilvNode* time_Position;
158 LilvNode* ui_external;
159 LilvNode* ui_externalkx;
162 LilvNode* units_unit;
163 LilvNode* units_render;
164 LilvNode* units_midiNote;
165 LilvNode* patch_writable;
166 LilvNode* patch_Message;
167 #ifdef HAVE_LV2_1_2_0
168 LilvNode* bufz_powerOf2BlockLength;
169 LilvNode* bufz_fixedBlockLength;
170 LilvNode* bufz_nominalBlockLength;
171 LilvNode* bufz_coarseBlockLength;
174 #ifdef HAVE_LV2_1_10_0
176 LilvNode* atom_float;
177 LilvNode* atom_object; // new in 1.8
178 LilvNode* atom_vector;
181 LilvNode* lv2_noSampleAccurateCtrl;
182 LilvNode* auto_can_write_automatation; // lv2:optionalFeature
183 LilvNode* auto_automation_control; // atom:supports
184 LilvNode* auto_automation_controlled; // lv2:portProperty
185 LilvNode* auto_automation_controller; // lv2:portProperty
189 bool _bundle_checked;
192 static LV2World _world;
194 /* worker extension */
196 /** Called by the plugin to schedule non-RT work. */
197 static LV2_Worker_Status
198 work_schedule(LV2_Worker_Schedule_Handle handle,
202 return (((Worker*)handle)->schedule(size, data)
204 : LV2_WORKER_ERR_UNKNOWN);
207 /** Called by the plugin to respond to non-RT work. */
208 static LV2_Worker_Status
209 work_respond(LV2_Worker_Respond_Handle handle,
213 return (((Worker*)handle)->respond(size, data)
215 : LV2_WORKER_ERR_UNKNOWN);
219 /* inline display extension */
221 queue_draw (LV2_Inline_Display_Handle handle)
223 LV2Plugin* plugin = (LV2Plugin*)handle;
224 plugin->QueueDraw(); /* EMIT SIGNAL */
231 log_vprintf(LV2_Log_Handle /*handle*/,
237 const int ret = g_vasprintf(&str, fmt, args);
238 /* strip trailing whitespace */
239 while (strlen (str) > 0 && isspace (str[strlen (str) - 1])) {
240 str[strlen (str) - 1] = '\0';
242 if (strlen (str) == 0) {
246 if (type == URIMap::instance().urids.log_Error) {
247 error << str << endmsg;
248 } else if (type == URIMap::instance().urids.log_Warning) {
249 warning << str << endmsg;
250 } else if (type == URIMap::instance().urids.log_Note) {
251 info << str << endmsg;
252 } else if (type == URIMap::instance().urids.log_Trace) {
253 DEBUG_TRACE(DEBUG::LV2, str);
259 log_printf(LV2_Log_Handle handle,
261 const char* fmt, ...)
265 const int ret = log_vprintf(handle, type, fmt, args);
270 struct LV2Plugin::Impl {
271 Impl() : plugin(0), ui(0), ui_type(0), name(0), author(0), instance(0)
273 #ifdef HAVE_LV2_1_2_0
278 #ifdef HAVE_LV2_1_2_0
286 /** Find the LV2 input port with the given designation.
287 * If found, bufptrs[port_index] will be set to bufptr.
289 const LilvPort* designated_input (const char* uri, void** bufptrs[], void** bufptr);
291 const LilvPlugin* plugin;
293 const LilvNode* ui_type;
296 LilvInstance* instance;
297 const LV2_Worker_Interface* work_iface;
298 #ifdef HAVE_LV2_1_2_0
299 const LV2_Options_Interface* opts_iface;
302 LV2_Atom_Forge forge;
303 LV2_Atom_Forge ui_forge;
304 int32_t block_length;
305 #ifdef HAVE_LV2_1_2_0
306 LV2_Options_Option* options;
309 LV2_Inline_Display* queue_draw;
313 LV2Plugin::LV2Plugin (AudioEngine& engine,
315 const void* c_plugin,
317 : Plugin (engine, session)
322 , _state_worker(NULL)
324 , _patch_port_in_index((uint32_t)-1)
325 , _patch_port_out_index((uint32_t)-1)
326 , _uri_map(URIMap::instance())
327 , _no_sample_accurate_ctrl (false)
329 init(c_plugin, rate);
332 LV2Plugin::LV2Plugin (const LV2Plugin& other)
338 , _state_worker(NULL)
339 , _insert_id(other._insert_id)
340 , _patch_port_in_index((uint32_t)-1)
341 , _patch_port_out_index((uint32_t)-1)
342 , _uri_map(URIMap::instance())
343 , _no_sample_accurate_ctrl (false)
345 init(other._impl->plugin, other._sample_rate);
347 for (uint32_t i = 0; i < parameter_count(); ++i) {
348 _control_data[i] = other._shadow_data[i];
349 _shadow_data[i] = other._shadow_data[i];
354 LV2Plugin::init(const void* c_plugin, framecnt_t rate)
356 DEBUG_TRACE(DEBUG::LV2, "init\n");
358 _impl->plugin = (const LilvPlugin*)c_plugin;
360 _impl->ui_type = NULL;
365 _atom_ev_buffers = 0;
367 _bpm_control_port = 0;
368 _freewheel_control_port = 0;
369 _latency_control_port = 0;
370 _next_cycle_start = std::numeric_limits<framepos_t>::max();
371 _next_cycle_speed = 1.0;
372 _seq_size = _engine.raw_buffer_size(DataType::MIDI);
374 _was_activated = false;
375 _has_state_interface = false;
376 _can_write_automation = false;
378 _current_latency = 0;
379 _impl->block_length = _session.get_block_size();
381 _instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
382 _data_access_feature.URI = "http://lv2plug.in/ns/ext/data-access";
383 _make_path_feature.URI = LV2_STATE__makePath;
384 _log_feature.URI = LV2_LOG__log;
385 _work_schedule_feature.URI = LV2_WORKER__schedule;
386 _work_schedule_feature.data = NULL;
387 _def_state_feature.URI = LV2_STATE_PREFIX "loadDefaultState"; // Post LV2-1.2.0
388 _def_state_feature.data = NULL;
390 const LilvPlugin* plugin = _impl->plugin;
392 LilvNode* state_iface_uri = lilv_new_uri(_world.world, LV2_STATE__interface);
393 LilvNode* state_uri = lilv_new_uri(_world.world, LV2_STATE_URI);
394 _has_state_interface =
395 // What plugins should have (lv2:extensionData state:Interface)
396 lilv_plugin_has_extension_data(plugin, state_iface_uri)
397 // What some outdated/incorrect ones have
398 || lilv_plugin_has_feature(plugin, state_uri);
399 lilv_node_free(state_uri);
400 lilv_node_free(state_iface_uri);
402 _features = (LV2_Feature**)calloc(12, sizeof(LV2_Feature*));
403 _features[0] = &_instance_access_feature;
404 _features[1] = &_data_access_feature;
405 _features[2] = &_make_path_feature;
406 _features[3] = _uri_map.uri_map_feature();
407 _features[4] = _uri_map.urid_map_feature();
408 _features[5] = _uri_map.urid_unmap_feature();
409 _features[6] = &_log_feature;
411 unsigned n_features = 7;
412 #ifdef HAVE_LV2_1_2_0
413 _features[n_features++] = &_def_state_feature;
416 lv2_atom_forge_init(&_impl->forge, _uri_map.urid_map());
417 lv2_atom_forge_init(&_impl->ui_forge, _uri_map.urid_map());
420 _impl->queue_draw = (LV2_Inline_Display*)
421 malloc (sizeof(LV2_Inline_Display));
422 _impl->queue_draw->handle = this;
423 _impl->queue_draw->queue_draw = queue_draw;
425 _queue_draw_feature.URI = LV2_INLINEDISPLAY__queue_draw;
426 _queue_draw_feature.data = _impl->queue_draw;
427 _features[n_features++] = &_queue_draw_feature;
430 #ifdef HAVE_LV2_1_2_0
431 LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
432 static const int32_t _min_block_length = 1; // may happen during split-cycles
433 static const int32_t _max_block_length = 8192; // max possible (with all engines and during export)
434 /* Consider updating max-block-size whenever the buffersize changes.
435 * It requires re-instantiating the plugin (which is a non-realtime operation),
436 * so it should be done lightly and only for plugins that require it.
438 * given that the block-size can change at any time (split-cycles) ardour currently
439 * does not support plugins that require bufz_fixedBlockLength.
441 LV2_Options_Option options[] = {
442 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__minBlockLength),
443 sizeof(int32_t), atom_Int, &_min_block_length },
444 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__maxBlockLength),
445 sizeof(int32_t), atom_Int, &_max_block_length },
446 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__sequenceSize),
447 sizeof(int32_t), atom_Int, &_seq_size },
448 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://lv2plug.in/ns/ext/buf-size#nominalBlockLength"),
449 sizeof(int32_t), atom_Int, &_impl->block_length },
450 { LV2_OPTIONS_INSTANCE, 0, 0, 0, 0, NULL }
453 _impl->options = (LV2_Options_Option*) malloc (sizeof (options));
454 memcpy ((void*) _impl->options, (void*) options, sizeof (options));
456 _options_feature.URI = LV2_OPTIONS__options;
457 _options_feature.data = _impl->options;
458 _features[n_features++] = &_options_feature;
461 LV2_State_Make_Path* make_path = (LV2_State_Make_Path*)malloc(
462 sizeof(LV2_State_Make_Path));
463 make_path->handle = this;
464 make_path->path = &lv2_state_make_path;
465 _make_path_feature.data = make_path;
467 LV2_Log_Log* log = (LV2_Log_Log*)malloc(sizeof(LV2_Log_Log));
469 log->printf = &log_printf;
470 log->vprintf = &log_vprintf;
471 _log_feature.data = log;
473 const size_t ring_size = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
474 LilvNode* worker_schedule = lilv_new_uri(_world.world, LV2_WORKER__schedule);
475 if (lilv_plugin_has_feature(plugin, worker_schedule)) {
476 LV2_Worker_Schedule* schedule = (LV2_Worker_Schedule*)malloc(
477 sizeof(LV2_Worker_Schedule));
478 _worker = new Worker(this, ring_size);
479 schedule->handle = _worker;
480 schedule->schedule_work = work_schedule;
481 _work_schedule_feature.data = schedule;
482 _features[n_features++] = &_work_schedule_feature;
484 lilv_node_free(worker_schedule);
486 if (_has_state_interface) {
487 // Create a non-threaded worker for use by state restore
488 _state_worker = new Worker(this, ring_size, false);
491 _impl->instance = lilv_plugin_instantiate(plugin, rate, _features);
492 _impl->name = lilv_plugin_get_name(plugin);
493 _impl->author = lilv_plugin_get_author_name(plugin);
495 if (_impl->instance == 0) {
496 error << _("LV2: Failed to instantiate plugin ") << uri() << endmsg;
497 throw failed_constructor();
500 _instance_access_feature.data = (void*)_impl->instance->lv2_handle;
501 _data_access_extension_data.extension_data = _impl->instance->lv2_descriptor->extension_data;
502 _data_access_feature.data = &_data_access_extension_data;
504 LilvNode* worker_iface_uri = lilv_new_uri(_world.world, LV2_WORKER__interface);
505 if (lilv_plugin_has_extension_data(plugin, worker_iface_uri)) {
506 _impl->work_iface = (const LV2_Worker_Interface*)extension_data(
507 LV2_WORKER__interface);
509 lilv_node_free(worker_iface_uri);
512 #ifdef HAVE_LV2_1_2_0
513 LilvNode* options_iface_uri = lilv_new_uri(_world.world, LV2_OPTIONS__interface);
514 if (lilv_plugin_has_extension_data(plugin, options_iface_uri)) {
515 _impl->opts_iface = (const LV2_Options_Interface*)extension_data(
516 LV2_OPTIONS__interface);
518 lilv_node_free(options_iface_uri);
522 _display_interface = (const LV2_Inline_Display_Interface*)
523 extension_data (LV2_INLINEDISPLAY__interface);
526 if (lilv_plugin_has_feature(plugin, _world.lv2_inPlaceBroken)) {
527 error << string_compose(
528 _("LV2: \"%1\" cannot be used, since it cannot do inplace processing."),
529 lilv_node_as_string(_impl->name)) << endmsg;
530 lilv_node_free(_impl->name);
531 lilv_node_free(_impl->author);
532 throw failed_constructor();
535 #ifdef HAVE_LV2_1_2_0
536 LilvNodes *required_features = lilv_plugin_get_required_features (plugin);
537 if (lilv_nodes_contains (required_features, _world.bufz_powerOf2BlockLength) ||
538 lilv_nodes_contains (required_features, _world.bufz_fixedBlockLength)
540 error << string_compose(
541 _("LV2: \"%1\" buffer-size requirements cannot be satisfied."),
542 lilv_node_as_string(_impl->name)) << endmsg;
543 lilv_node_free(_impl->name);
544 lilv_node_free(_impl->author);
545 lilv_nodes_free(required_features);
546 throw failed_constructor();
548 lilv_nodes_free(required_features);
551 LilvNodes* optional_features = lilv_plugin_get_optional_features (plugin);
552 #ifdef HAVE_LV2_1_2_0
553 if (lilv_nodes_contains (optional_features, _world.bufz_coarseBlockLength)) {
554 _no_sample_accurate_ctrl = true;
558 if (lilv_nodes_contains (optional_features, _world.lv2_noSampleAccurateCtrl)) {
559 /* deprecated 2016-Sep-18 in favor of bufz_coarseBlockLength */
560 _no_sample_accurate_ctrl = true;
562 if (lilv_nodes_contains (optional_features, _world.auto_can_write_automatation)) {
563 _can_write_automation = true;
565 lilv_nodes_free(optional_features);
568 #ifdef HAVE_LILV_0_16_0
569 // Load default state
571 /* immediately schedule any work,
572 * so that state restore later will not find a busy
573 * worker. latency_compute_run() flushes any replies
575 _worker->set_synchronous(true);
577 LilvState* state = lilv_state_new_from_world(
578 _world.world, _uri_map.urid_map(), lilv_plugin_get_uri(_impl->plugin));
579 if (state && _has_state_interface) {
580 lilv_state_restore(state, _impl->instance, NULL, NULL, 0, NULL);
582 lilv_state_free(state);
587 const uint32_t num_ports = this->num_ports();
588 for (uint32_t i = 0; i < num_ports; ++i) {
589 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, i);
591 size_t minimumSize = 0;
593 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_OutputPort)) {
594 flags |= PORT_OUTPUT;
595 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_InputPort)) {
598 error << string_compose(
599 "LV2: \"%1\" port %2 is neither input nor output",
600 lilv_node_as_string(_impl->name), i) << endmsg;
601 throw failed_constructor();
604 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_ControlPort)) {
605 flags |= PORT_CONTROL;
606 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_AudioPort)) {
608 } else if (lilv_port_is_a(_impl->plugin, port, _world.ev_EventPort)) {
610 flags |= PORT_MIDI; // We assume old event API ports are for MIDI
611 } else if (lilv_port_is_a(_impl->plugin, port, _world.atom_AtomPort)) {
612 LilvNodes* buffer_types = lilv_port_get_value(
613 _impl->plugin, port, _world.atom_bufferType);
614 LilvNodes* atom_supports = lilv_port_get_value(
615 _impl->plugin, port, _world.atom_supports);
617 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
618 flags |= PORT_SEQUENCE;
619 if (lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)) {
622 if (lilv_nodes_contains(atom_supports, _world.time_Position)) {
623 flags |= PORT_POSITION;
626 if (lilv_nodes_contains(atom_supports, _world.auto_automation_control)) {
627 flags |= PORT_AUTOCTRL;
630 if (lilv_nodes_contains(atom_supports, _world.patch_Message)) {
631 flags |= PORT_PATCHMSG;
632 if (flags & PORT_INPUT) {
633 _patch_port_in_index = i;
635 _patch_port_out_index = i;
639 LilvNodes* min_size_v = lilv_port_get_value(_impl->plugin, port, _world.rsz_minimumSize);
640 LilvNode* min_size = min_size_v ? lilv_nodes_get_first(min_size_v) : NULL;
641 if (min_size && lilv_node_is_int(min_size)) {
642 minimumSize = lilv_node_as_int(min_size);
644 lilv_nodes_free(min_size_v);
645 lilv_nodes_free(buffer_types);
646 lilv_nodes_free(atom_supports);
648 error << string_compose(
649 "LV2: \"%1\" port %2 has no known data type",
650 lilv_node_as_string(_impl->name), i) << endmsg;
651 throw failed_constructor();
654 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
655 if (lilv_port_has_property(_impl->plugin, port, _world.ext_causesArtifacts)) {
656 flags |= PORT_NOAUTO;
658 if (lilv_port_has_property(_impl->plugin, port, _world.ext_notAutomatic)) {
659 flags |= PORT_NOAUTO;
661 if (lilv_port_has_property(_impl->plugin, port, _world.ext_expensive)) {
662 flags |= PORT_NOAUTO;
666 if (lilv_port_has_property(_impl->plugin, port, _world.auto_automation_controlled)) {
667 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
668 flags |= PORT_CTRLED;
671 if (lilv_port_has_property(_impl->plugin, port, _world.auto_automation_controller)) {
672 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
673 flags |= PORT_CTRLER;
678 _port_flags.push_back(flags);
679 _port_minimumSize.push_back(minimumSize);
680 DEBUG_TRACE(DEBUG::LV2, string_compose("port %1 buffer %2 bytes\n", i, minimumSize));
683 _control_data = new float[num_ports];
684 _shadow_data = new float[num_ports];
685 _defaults = new float[num_ports];
686 _ev_buffers = new LV2_Evbuf*[num_ports];
687 memset(_ev_buffers, 0, sizeof(LV2_Evbuf*) * num_ports);
689 const bool latent = lilv_plugin_has_latency(plugin);
690 const uint32_t latency_index = (latent)
691 ? lilv_plugin_get_latency_port_index(plugin)
694 // Build an array of pointers to special parameter buffers
695 void*** params = new void**[num_ports];
696 for (uint32_t i = 0; i < num_ports; ++i) {
699 _impl->designated_input (LV2_TIME__beatsPerMinute, params, (void**)&_bpm_control_port);
700 _impl->designated_input (LV2_CORE__freeWheeling, params, (void**)&_freewheel_control_port);
702 for (uint32_t i = 0; i < num_ports; ++i) {
703 const LilvPort* port = lilv_plugin_get_port_by_index(plugin, i);
704 const LilvNode* sym = lilv_port_get_symbol(plugin, port);
706 // Store index in map so we can look up index by symbol
707 _port_indices.insert(std::make_pair(lilv_node_as_string(sym), i));
709 // Get range and default value if applicable
710 if (parameter_is_control(i)) {
712 lilv_port_get_range(plugin, port, &def, NULL, NULL);
713 _defaults[i] = def ? lilv_node_as_float(def) : 0.0f;
714 if (lilv_port_has_property (plugin, port, _world.lv2_sampleRate)) {
715 _defaults[i] *= _session.frame_rate ();
719 lilv_instance_connect_port(_impl->instance, i, &_control_data[i]);
721 if (latent && i == latency_index) {
723 lilv_port_get_range(_impl->plugin, port, NULL, NULL, &max);
724 _max_latency = max ? lilv_node_as_float(max) : .02 * _sample_rate;
725 _latency_control_port = &_control_data[i];
726 *_latency_control_port = 0;
729 if (parameter_is_input(i)) {
730 _shadow_data[i] = default_value(i);
732 *params[i] = (void*)&_shadow_data[i];
742 LilvUIs* uis = lilv_plugin_get_uis(plugin);
743 if (lilv_uis_size(uis) > 0) {
745 // Look for embeddable UI
746 LILV_FOREACH(uis, u, uis) {
747 const LilvUI* this_ui = lilv_uis_get(uis, u);
748 const LilvNode* this_ui_type = NULL;
749 if (lilv_ui_is_supported(this_ui,
753 // TODO: Multiple UI support
755 _impl->ui_type = this_ui_type;
760 // Look for Gtk native 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_GtkUI)) {
765 _impl->ui_type = _world.ui_GtkUI;
771 // If Gtk UI is not available, try to find external UI
773 LILV_FOREACH(uis, i, uis) {
774 const LilvUI* ui = lilv_uis_get(uis, i);
775 if (lilv_ui_is_a(ui, _world.ui_externalkx)) {
777 _impl->ui_type = _world.ui_external;
780 if (lilv_ui_is_a(ui, _world.ui_external)) {
782 _impl->ui_type = _world.ui_external;
788 load_supported_properties(_property_descriptors);
789 allocate_atom_event_buffers();
790 latency_compute_run();
794 LV2Plugin::set_block_size (pframes_t nframes)
796 #ifdef HAVE_LV2_1_2_0
797 if (_impl->opts_iface) {
798 LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
799 _impl->block_length = nframes;
800 LV2_Options_Option block_size_option = {
801 LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id ("http://lv2plug.in/ns/ext/buf-size#nominalBlockLength"),
802 sizeof(int32_t), atom_Int, (void*)&_impl->block_length
804 _impl->opts_iface->set (_impl->instance->lv2_handle, &block_size_option);
811 LV2Plugin::requires_fixed_sized_buffers () const
813 /* This controls if Ardour will split the plugin's run()
814 * on automation events in order to pass sample-accurate automation
815 * via standard control-ports.
817 * When returning true Ardour will *not* sub-divide the process-cycle.
818 * Automation events that happen between cycle-start and cycle-end will be
819 * ignored (ctrl values are interpolated to cycle-start).
820 * NB. Atom Sequences are still sample accurate.
822 * Note: This does not guarantee a fixed block-size.
823 * e.g The process cycle may be split when looping, also
824 * the period-size may change any time: see set_block_size()
826 if (get_info()->n_inputs.n_midi() > 0) {
827 /* we don't yet implement midi buffer offsets (for split cycles).
828 * Also connect_and_run() also uses _session.transport_frame() directly
829 * (for BBT) which is not offset for plugin cycle split.
833 return _no_sample_accurate_ctrl;
836 LV2Plugin::~LV2Plugin ()
838 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 destroy\n", name()));
843 lilv_instance_free(_impl->instance);
844 lilv_state_free(_impl->state);
845 lilv_node_free(_impl->name);
846 lilv_node_free(_impl->author);
847 #ifdef HAVE_LV2_1_2_0
848 free(_impl->options);
851 free(_impl->queue_draw);
855 free(_log_feature.data);
856 free(_make_path_feature.data);
857 free(_work_schedule_feature.data);
862 delete _state_worker;
864 if (_atom_ev_buffers) {
865 LV2_Evbuf** b = _atom_ev_buffers;
870 free(_atom_ev_buffers);
873 delete [] _control_data;
874 delete [] _shadow_data;
876 delete [] _ev_buffers;
881 LV2Plugin::is_external_ui() const
886 return lilv_ui_is_a(_impl->ui, _world.ui_external) || lilv_ui_is_a(_impl->ui, _world.ui_externalkx);
890 LV2Plugin::is_external_kx() const
895 return lilv_ui_is_a(_impl->ui, _world.ui_externalkx);
899 LV2Plugin::ui_is_resizable () const
901 const LilvNode* s = lilv_ui_get_uri(_impl->ui);
902 LilvNode* p = lilv_new_uri(_world.world, LV2_CORE__optionalFeature);
903 LilvNode* fs = lilv_new_uri(_world.world, LV2_UI__fixedSize);
904 LilvNode* nrs = lilv_new_uri(_world.world, LV2_UI__noUserResize);
906 LilvNodes* fs_matches = lilv_world_find_nodes(_world.world, s, p, fs);
907 LilvNodes* nrs_matches = lilv_world_find_nodes(_world.world, s, p, nrs);
909 lilv_nodes_free(nrs_matches);
910 lilv_nodes_free(fs_matches);
915 return !fs_matches && !nrs_matches;
920 LV2Plugin::has_inline_display () {
921 return _display_interface ? true : false;
924 Plugin::Display_Image_Surface*
925 LV2Plugin::render_inline_display (uint32_t w, uint32_t h) {
926 if (_display_interface) {
927 /* Plugin::Display_Image_Surface is identical to
928 * LV2_Inline_Display_Image_Surface */
929 return (Plugin::Display_Image_Surface*) _display_interface->render ((void*)_impl->instance->lv2_handle, w, h);
936 LV2Plugin::unique_id() const
938 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
942 LV2Plugin::uri() const
944 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
948 LV2Plugin::label() const
950 return lilv_node_as_string(_impl->name);
954 LV2Plugin::name() const
956 return lilv_node_as_string(_impl->name);
960 LV2Plugin::maker() const
962 return _impl->author ? lilv_node_as_string (_impl->author) : "Unknown";
966 LV2Plugin::num_ports() const
968 return lilv_plugin_get_num_ports(_impl->plugin);
972 LV2Plugin::parameter_count() const
974 return lilv_plugin_get_num_ports(_impl->plugin);
978 LV2Plugin::default_value(uint32_t port)
980 return _defaults[port];
984 LV2Plugin::port_symbol(uint32_t index) const
986 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, index);
988 error << name() << ": Invalid port index " << index << endmsg;
991 const LilvNode* sym = lilv_port_get_symbol(_impl->plugin, port);
992 return lilv_node_as_string(sym);
996 LV2Plugin::port_index (const char* symbol) const
998 const map<string, uint32_t>::const_iterator i = _port_indices.find(symbol);
999 if (i != _port_indices.end()) {
1002 warning << string_compose(_("LV2: Unknown port %1"), symbol) << endmsg;
1003 return (uint32_t)-1;
1008 LV2Plugin::set_parameter(uint32_t which, float val)
1010 DEBUG_TRACE(DEBUG::LV2, string_compose(
1011 "%1 set parameter %2 to %3\n", name(), which, val));
1013 if (which < lilv_plugin_get_num_ports(_impl->plugin)) {
1014 if (get_parameter (which) == val) {
1018 _shadow_data[which] = val;
1020 warning << string_compose(
1021 _("Illegal parameter number used with plugin \"%1\". "
1022 "This is a bug in either %2 or the LV2 plugin <%3>"),
1023 name(), PROGRAM_NAME, unique_id()) << endmsg;
1026 Plugin::set_parameter(which, val);
1030 LV2Plugin::get_parameter(uint32_t which) const
1032 if (parameter_is_input(which)) {
1033 return (float)_shadow_data[which];
1035 return (float)_control_data[which];
1041 LV2Plugin::get_docs() const
1043 LilvNodes* comments = lilv_plugin_get_value(_impl->plugin, _world.rdfs_comment);
1045 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
1046 lilv_nodes_free(comments);
1054 LV2Plugin::get_parameter_docs(uint32_t which) const
1056 LilvNodes* comments = lilv_port_get_value(
1058 lilv_plugin_get_port_by_index(_impl->plugin, which),
1059 _world.rdfs_comment);
1062 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
1063 lilv_nodes_free(comments);
1071 LV2Plugin::get_layout (uint32_t which, UILayoutHint& h) const
1073 /// TODO lookup port-properties
1074 if (unique_id () != "urn:ardour:a-eq") {
1079 case 0: h.x0 = 0; h.x1 = 1; h.y0 = 2; h.y1 = 3; break; // Frequency L
1080 case 1: h.x0 = 0; h.x1 = 1; h.y0 = 0; h.y1 = 1; break; // Gain L
1081 case 17: h.x0 = 0; h.x1 = 1; h.y0 = 5; h.y1 = 6; break; // enable L
1083 case 2: h.x0 = 1; h.x1 = 3; h.y0 = 2; h.y1 = 3; break; // Frequency 1
1084 case 3: h.x0 = 1; h.x1 = 3; h.y0 = 0; h.y1 = 1; break; // Gain 1
1085 case 4: h.x0 = 2; h.x1 = 4; h.y0 = 1; h.y1 = 2; break; // Bandwidth 1
1086 case 18: h.x0 = 1; h.x1 = 4; h.y0 = 5; h.y1 = 6; break; // enable 1
1088 case 5: h.x0 = 4; h.x1 = 6; h.y0 = 2; h.y1 = 3; break; // Frequency 2
1089 case 6: h.x0 = 4; h.x1 = 6; h.y0 = 0; h.y1 = 1; break; // Gain 2
1090 case 7: h.x0 = 5; h.x1 = 7; h.y0 = 1; h.y1 = 2; break; // Bandwidth 2
1091 case 19: h.x0 = 4; h.x1 = 7; h.y0 = 5; h.y1 = 6; break; // enable 2
1093 case 8: h.x0 = 7; h.x1 = 9; h.y0 = 2; h.y1 = 3; break; // Frequency 3
1094 case 9: h.x0 = 7; h.x1 = 9; h.y0 = 0; h.y1 = 1; break; // Gain 3
1095 case 10: h.x0 = 8; h.x1 = 10; h.y0 = 1; h.y1 = 2; break; // Bandwidth 3
1096 case 20: h.x0 = 7; h.x1 = 10; h.y0 = 5; h.y1 = 6; break; // enable 3
1098 case 11: h.x0 = 10; h.x1 = 12; h.y0 = 2; h.y1 = 3; break; // Frequency 4
1099 case 12: h.x0 = 10; h.x1 = 12; h.y0 = 0; h.y1 = 1; break; // Gain 4
1100 case 13: h.x0 = 11; h.x1 = 13; h.y0 = 1; h.y1 = 2; break; // Bandwidth 4
1101 case 21: h.x0 = 10; h.x1 = 13; h.y0 = 5; h.y1 = 6; break; // enable 4
1103 case 14: h.x0 = 13; h.x1 = 14; h.y0 = 2; h.y1 = 3; break; // Frequency H
1104 case 15: h.x0 = 13; h.x1 = 14; h.y0 = 0; h.y1 = 1; break; // Gain H
1105 case 22: h.x0 = 13; h.x1 = 14; h.y0 = 5; h.y1 = 6; break; // enable H
1107 case 16: h.x0 = 14; h.x1 = 15; h.y0 = 1; h.y1 = 3; break; // Master Gain
1108 case 23: h.x0 = 14; h.x1 = 15; h.y0 = 5; h.y1 = 6; break; // Master Enable
1116 LV2Plugin::nth_parameter(uint32_t n, bool& ok) const
1119 for (uint32_t c = 0, x = 0; x < lilv_plugin_get_num_ports(_impl->plugin); ++x) {
1120 if (parameter_is_control(x)) {
1132 LV2Plugin::extension_data(const char* uri) const
1134 return lilv_instance_get_extension_data(_impl->instance, uri);
1138 LV2Plugin::c_plugin()
1140 return _impl->plugin;
1146 return (const void*)_impl->ui;
1150 LV2Plugin::c_ui_type()
1152 return (const void*)_impl->ui_type;
1155 /** Directory for all plugin state. */
1157 LV2Plugin::plugin_dir() const
1159 if (!_plugin_state_dir.empty ()){
1160 return Glib::build_filename(_plugin_state_dir, _insert_id.to_s());
1162 return Glib::build_filename(_session.plugins_dir(), _insert_id.to_s());
1166 /** Directory for files created by the plugin (except during save). */
1168 LV2Plugin::scratch_dir() const
1170 return Glib::build_filename(plugin_dir(), "scratch");
1173 /** Directory for snapshots of files in the scratch directory. */
1175 LV2Plugin::file_dir() const
1177 return Glib::build_filename(plugin_dir(), "files");
1180 /** Directory to save state snapshot version @c num into. */
1182 LV2Plugin::state_dir(unsigned num) const
1184 return Glib::build_filename(plugin_dir(), string_compose("state%1", num));
1187 /** Implementation of state:makePath for files created at instantiation time.
1188 * Note this is not used for files created at save time (Lilv deals with that).
1191 LV2Plugin::lv2_state_make_path(LV2_State_Make_Path_Handle handle,
1194 LV2Plugin* me = (LV2Plugin*)handle;
1195 if (me->_insert_id == PBD::ID("0")) {
1196 warning << string_compose(
1197 "File path \"%1\" requested but LV2 %2 has no insert ID",
1198 path, me->name()) << endmsg;
1199 return g_strdup(path);
1202 const std::string abs_path = Glib::build_filename(me->scratch_dir(), path);
1203 const std::string dirname = Glib::path_get_dirname(abs_path);
1204 g_mkdir_with_parents(dirname.c_str(), 0744);
1206 DEBUG_TRACE(DEBUG::LV2, string_compose("new file path %1 => %2\n",
1209 return g_strndup(abs_path.c_str(), abs_path.length());
1213 LV2Plugin::add_state(XMLNode* root) const
1215 assert(_insert_id != PBD::ID("0"));
1221 for (uint32_t i = 0; i < parameter_count(); ++i) {
1222 if (parameter_is_input(i) && parameter_is_control(i)) {
1223 child = new XMLNode("Port");
1224 child->add_property("symbol", port_symbol(i));
1225 snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
1226 child->add_property("value", string(buf));
1227 root->add_child_nocopy(*child);
1231 if (!_plugin_state_dir.empty()) {
1232 root->add_property("template-dir", _plugin_state_dir);
1235 if (_has_state_interface) {
1236 // Provisionally increment state version and create directory
1237 const std::string new_dir = state_dir(++_state_version);
1238 g_mkdir_with_parents(new_dir.c_str(), 0744);
1240 LilvState* state = lilv_state_new_from_instance(
1243 _uri_map.urid_map(),
1244 scratch_dir().c_str(),
1246 _session.externals_dir().c_str(),
1249 const_cast<LV2Plugin*>(this),
1253 if (!_plugin_state_dir.empty() || force_state_save
1255 || !lilv_state_equals(state, _impl->state)) {
1256 lilv_state_save(_world.world,
1257 _uri_map.urid_map(),
1258 _uri_map.urid_unmap(),
1264 if (force_state_save) {
1265 // archive or save-as
1266 lilv_state_free(state);
1269 else if (_plugin_state_dir.empty()) {
1270 // normal session save
1271 lilv_state_free(_impl->state);
1272 _impl->state = state;
1274 // template save (dedicated state-dir)
1275 lilv_state_free(state);
1279 // State is identical, decrement version and nuke directory
1280 lilv_state_free(state);
1281 PBD::remove_directory(new_dir);
1285 root->add_property("state-dir", string_compose("state%1", _state_version));
1289 // TODO: Once we can rely on lilv 0.16.0, lilv_world_get can replace this
1291 get_value(LilvWorld* world, const LilvNode* subject, const LilvNode* predicate)
1293 LilvNodes* vs = lilv_world_find_nodes(world, subject, predicate, NULL);
1295 LilvNode* node = lilv_node_duplicate(lilv_nodes_get_first(vs));
1296 lilv_nodes_free(vs);
1303 LV2Plugin::find_presets()
1305 LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
1306 LilvNode* pset_Preset = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
1307 LilvNode* rdfs_label = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
1309 LilvNodes* presets = lilv_plugin_get_related(_impl->plugin, pset_Preset);
1310 LILV_FOREACH(nodes, i, presets) {
1311 const LilvNode* preset = lilv_nodes_get(presets, i);
1312 lilv_world_load_resource(_world.world, preset);
1313 LilvNode* name = get_value(_world.world, preset, rdfs_label);
1314 bool userpreset = true; // TODO
1316 _presets.insert(std::make_pair(lilv_node_as_string(preset),
1317 Plugin::PresetRecord(
1318 lilv_node_as_string(preset),
1319 lilv_node_as_string(name),
1321 lilv_node_free(name);
1323 warning << string_compose(
1324 _("Plugin \"%1\" preset \"%2\" is missing a label\n"),
1325 lilv_node_as_string(lilv_plugin_get_uri(_impl->plugin)),
1326 lilv_node_as_string(preset)) << endmsg;
1329 lilv_nodes_free(presets);
1331 lilv_node_free(rdfs_label);
1332 lilv_node_free(pset_Preset);
1333 lilv_node_free(lv2_appliesTo);
1337 set_port_value(const char* port_symbol,
1343 LV2Plugin* self = (LV2Plugin*)user_data;
1344 if (type != 0 && type != URIMap::instance().urids.atom_Float) {
1345 return; // TODO: Support non-float ports
1348 const uint32_t port_index = self->port_index(port_symbol);
1349 if (port_index != (uint32_t)-1) {
1350 self->set_parameter(port_index, *(const float*)value);
1355 LV2Plugin::load_preset(PresetRecord r)
1357 LilvWorld* world = _world.world;
1358 LilvNode* pset = lilv_new_uri(world, r.uri.c_str());
1359 LilvState* state = lilv_state_new_from_world(world, _uri_map.urid_map(), pset);
1361 const LV2_Feature* state_features[2] = { NULL, NULL };
1362 LV2_Worker_Schedule schedule = { _state_worker, work_schedule };
1363 const LV2_Feature state_sched_feature = { LV2_WORKER__schedule, &schedule };
1364 if (_state_worker) {
1365 state_features[0] = &state_sched_feature;
1369 lilv_state_restore(state, _impl->instance, set_port_value, this, 0, state_features);
1370 lilv_state_free(state);
1371 Plugin::load_preset(r);
1374 lilv_node_free(pset);
1379 ARDOUR::lv2plugin_get_port_value(const char* port_symbol,
1384 LV2Plugin *plugin = (LV2Plugin *) user_data;
1386 uint32_t index = plugin->port_index(port_symbol);
1387 if (index != (uint32_t) -1) {
1388 if (plugin->parameter_is_input(index) && plugin->parameter_is_control(index)) {
1390 *size = sizeof(float);
1391 *type = plugin->_uri_map.uri_to_id(LV2_ATOM__Float);
1392 value = &plugin->_shadow_data[index];
1404 LV2Plugin::do_save_preset(string name)
1406 LilvNode* plug_name = lilv_plugin_get_name(_impl->plugin);
1407 const string prefix = legalize_for_uri(lilv_node_as_string(plug_name));
1408 const string base_name = legalize_for_uri(name);
1409 const string file_name = base_name + ".ttl";
1410 const string bundle = Glib::build_filename(
1411 Glib::get_home_dir(),
1412 Glib::build_filename(".lv2", prefix + "_" + base_name + ".lv2"));
1414 #ifdef HAVE_LILV_0_21_3
1415 /* delete reference to old preset (if any) */
1416 const PresetRecord* r = preset_by_label(name);
1418 LilvNode* pset = lilv_new_uri (_world.world, r->uri.c_str());
1420 lilv_world_unload_resource (_world.world, pset);
1421 lilv_node_free(pset);
1426 LilvState* state = lilv_state_new_from_instance(
1429 _uri_map.urid_map(),
1430 scratch_dir().c_str(), // file_dir
1431 bundle.c_str(), // copy_dir
1432 bundle.c_str(), // link_dir
1433 bundle.c_str(), // save_dir
1434 lv2plugin_get_port_value, // get_value
1435 (void*)this, // user_data
1436 LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE, // flags
1437 _features // features
1440 lilv_state_set_label(state, name.c_str());
1442 _world.world, // world
1443 _uri_map.urid_map(), // map
1444 _uri_map.urid_unmap(), // unmap
1446 NULL, // uri (NULL = use file URI)
1447 bundle.c_str(), // dir
1448 file_name.c_str() // filename
1451 lilv_state_free(state);
1453 std::string uri = Glib::filename_to_uri(Glib::build_filename(bundle, file_name));
1454 LilvNode *node_bundle = lilv_new_uri(_world.world, Glib::filename_to_uri(Glib::build_filename(bundle, "/")).c_str());
1455 LilvNode *node_preset = lilv_new_uri(_world.world, uri.c_str());
1456 #ifdef HAVE_LILV_0_21_3
1457 lilv_world_unload_resource(_world.world, node_preset);
1458 lilv_world_unload_bundle(_world.world, node_bundle);
1460 lilv_world_load_bundle(_world.world, node_bundle);
1461 lilv_world_load_resource(_world.world, node_preset);
1462 lilv_node_free(node_bundle);
1463 lilv_node_free(node_preset);
1464 lilv_node_free(plug_name);
1469 LV2Plugin::do_remove_preset(string name)
1471 #ifdef HAVE_LILV_0_21_3
1472 /* Look up preset record by label (FIXME: ick, label as ID) */
1473 const PresetRecord* r = preset_by_label(name);
1478 /* Load a LilvState for the preset. */
1479 LilvWorld* world = _world.world;
1480 LilvNode* pset = lilv_new_uri(world, r->uri.c_str());
1481 LilvState* state = lilv_state_new_from_world(world, _uri_map.urid_map(), pset);
1483 lilv_node_free(pset);
1487 /* Unload preset from world. */
1488 lilv_world_unload_resource(world, pset);
1490 /* Delete it from the file system. This will remove the preset file and the entry
1491 from the manifest. If this results in an empty manifest (i.e. the
1492 preset is the only thing in the bundle), then the bundle is removed. */
1493 lilv_state_delete(world, state);
1495 lilv_state_free(state);
1496 lilv_node_free(pset);
1498 /* Without lilv_state_delete(), we could delete the preset file, but this
1499 would leave a broken bundle/manifest around, so the preset would still
1500 be visible, but broken. Naively deleting a bundle is too dangerous, so
1501 we simply do not support preset deletion with older Lilv */
1505 LV2Plugin::has_editor() const
1507 return _impl->ui != NULL;
1511 LV2Plugin::has_message_output() const
1513 for (uint32_t i = 0; i < num_ports(); ++i) {
1514 if ((_port_flags[i] & PORT_SEQUENCE) &&
1515 (_port_flags[i] & PORT_OUTPUT)) {
1523 LV2Plugin::write_to(RingBuffer<uint8_t>* dest,
1527 const uint8_t* body)
1529 const uint32_t buf_size = sizeof(UIMessage) + size;
1530 vector<uint8_t> buf(buf_size);
1532 UIMessage* msg = (UIMessage*)&buf[0];
1534 msg->protocol = protocol;
1536 memcpy(msg + 1, body, size);
1538 return (dest->write(&buf[0], buf_size) == buf_size);
1542 LV2Plugin::write_from_ui(uint32_t index,
1545 const uint8_t* body)
1548 size_t rbs = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
1549 /* buffer data communication from plugin UI to plugin instance.
1550 * this buffer needs to potentially hold
1551 * (port's minimumSize) * (audio-periods) / (UI-periods)
1554 * e.g 48kSPS / 128fpp -> audio-periods = 375 Hz
1555 * ui-periods = 25 Hz (SuperRapidScreenUpdate)
1556 * default minimumSize = 32K (see LV2Plugin::allocate_atom_event_buffers()
1558 * it is NOT safe to overflow (msg.size will be misinterpreted)
1560 uint32_t bufsiz = 32768;
1561 if (_atom_ev_buffers && _atom_ev_buffers[0]) {
1562 bufsiz = lv2_evbuf_get_capacity(_atom_ev_buffers[0]);
1564 rbs = max((size_t) bufsiz * 8, rbs);
1565 _from_ui = new RingBuffer<uint8_t>(rbs);
1568 if (!write_to(_from_ui, index, protocol, size, body)) {
1569 error << "Error writing from UI to plugin" << endmsg;
1576 LV2Plugin::write_to_ui(uint32_t index,
1579 const uint8_t* body)
1581 if (!write_to(_to_ui, index, protocol, size, body)) {
1582 error << "Error writing from plugin to UI" << endmsg;
1589 forge_variant(LV2_Atom_Forge* forge, const Variant& value)
1591 switch (value.type()) {
1592 case Variant::NOTHING:
1594 case Variant::BEATS:
1595 // No atom type for this, just forge a double
1596 lv2_atom_forge_double(forge, value.get_beats().to_double());
1599 lv2_atom_forge_bool(forge, value.get_bool());
1601 case Variant::DOUBLE:
1602 lv2_atom_forge_double(forge, value.get_double());
1604 case Variant::FLOAT:
1605 lv2_atom_forge_float(forge, value.get_float());
1608 lv2_atom_forge_int(forge, value.get_int());
1611 lv2_atom_forge_long(forge, value.get_long());
1614 lv2_atom_forge_path(
1615 forge, value.get_path().c_str(), value.get_path().size());
1617 case Variant::STRING:
1618 lv2_atom_forge_string(
1619 forge, value.get_string().c_str(), value.get_string().size());
1623 forge, value.get_uri().c_str(), value.get_uri().size());
1628 /** Get a variant type from a URI, return false iff no match found. */
1630 uri_to_variant_type(const std::string& uri, Variant::Type& type)
1632 if (uri == LV2_ATOM__Bool) {
1633 type = Variant::BOOL;
1634 } else if (uri == LV2_ATOM__Double) {
1635 type = Variant::DOUBLE;
1636 } else if (uri == LV2_ATOM__Float) {
1637 type = Variant::FLOAT;
1638 } else if (uri == LV2_ATOM__Int) {
1639 type = Variant::INT;
1640 } else if (uri == LV2_ATOM__Long) {
1641 type = Variant::LONG;
1642 } else if (uri == LV2_ATOM__Path) {
1643 type = Variant::PATH;
1644 } else if (uri == LV2_ATOM__String) {
1645 type = Variant::STRING;
1646 } else if (uri == LV2_ATOM__URI) {
1647 type = Variant::URI;
1655 LV2Plugin::set_property(uint32_t key, const Variant& value)
1657 if (_patch_port_in_index == (uint32_t)-1) {
1658 error << "LV2: set_property called with unset patch_port_in_index" << endmsg;
1660 } else if (value.type() == Variant::NOTHING) {
1661 error << "LV2: set_property called with void value" << endmsg;
1665 // Set up forge to write to temporary buffer on the stack
1666 LV2_Atom_Forge* forge = &_impl->ui_forge;
1667 LV2_Atom_Forge_Frame frame;
1668 uint8_t buf[PATH_MAX]; // Ought to be enough for anyone...
1670 lv2_atom_forge_set_buffer(forge, buf, sizeof(buf));
1672 // Serialize patch:Set message to set property
1673 #ifdef HAVE_LV2_1_10_0
1674 lv2_atom_forge_object(forge, &frame, 1, _uri_map.urids.patch_Set);
1675 lv2_atom_forge_key(forge, _uri_map.urids.patch_property);
1676 lv2_atom_forge_urid(forge, key);
1677 lv2_atom_forge_key(forge, _uri_map.urids.patch_value);
1679 lv2_atom_forge_blank(forge, &frame, 1, _uri_map.urids.patch_Set);
1680 lv2_atom_forge_property_head(forge, _uri_map.urids.patch_property, 0);
1681 lv2_atom_forge_urid(forge, key);
1682 lv2_atom_forge_property_head(forge, _uri_map.urids.patch_value, 0);
1685 forge_variant(forge, value);
1687 // Write message to UI=>Plugin ring
1688 const LV2_Atom* const atom = (const LV2_Atom*)buf;
1689 write_from_ui(_patch_port_in_index,
1690 _uri_map.urids.atom_eventTransfer,
1691 lv2_atom_total_size(atom),
1692 (const uint8_t*)atom);
1695 const ParameterDescriptor&
1696 LV2Plugin::get_property_descriptor(uint32_t id) const
1698 PropertyDescriptors::const_iterator p = _property_descriptors.find(id);
1699 if (p != _property_descriptors.end()) {
1702 return Plugin::get_property_descriptor(id);
1706 load_parameter_descriptor_units(LilvWorld* lworld, ParameterDescriptor& desc, const LilvNodes* units)
1708 if (lilv_nodes_contains(units, _world.units_midiNote)) {
1709 desc.unit = ParameterDescriptor::MIDI_NOTE;
1710 } else if (lilv_nodes_contains(units, _world.units_db)) {
1711 desc.unit = ParameterDescriptor::DB;
1712 } else if (lilv_nodes_contains(units, _world.units_hz)) {
1713 desc.unit = ParameterDescriptor::HZ;
1715 if (lilv_nodes_size(units) > 0) {
1716 const LilvNode* unit = lilv_nodes_get_first(units);
1717 LilvNode* render = get_value(lworld, unit, _world.units_render);
1719 desc.print_fmt = lilv_node_as_string(render);
1720 replace_all (desc.print_fmt, "%f", "%.2f");
1721 lilv_node_free(render);
1727 load_parameter_descriptor(LV2World& world,
1728 ParameterDescriptor& desc,
1729 Variant::Type datatype,
1730 const LilvNode* subject)
1732 LilvWorld* lworld = _world.world;
1733 LilvNode* label = get_value(lworld, subject, _world.rdfs_label);
1734 LilvNode* def = get_value(lworld, subject, _world.lv2_default);
1735 LilvNode* minimum = get_value(lworld, subject, _world.lv2_minimum);
1736 LilvNode* maximum = get_value(lworld, subject, _world.lv2_maximum);
1737 LilvNodes* units = lilv_world_find_nodes(lworld, subject, _world.units_unit, NULL);
1739 desc.label = lilv_node_as_string(label);
1741 if (def && lilv_node_is_float(def)) {
1742 desc.normal = lilv_node_as_float(def);
1744 if (minimum && lilv_node_is_float(minimum)) {
1745 desc.lower = lilv_node_as_float(minimum);
1747 if (maximum && lilv_node_is_float(maximum)) {
1748 desc.upper = lilv_node_as_float(maximum);
1750 load_parameter_descriptor_units(lworld, desc, units);
1751 desc.datatype = datatype;
1752 desc.toggled |= datatype == Variant::BOOL;
1753 desc.integer_step |= datatype == Variant::INT || datatype == Variant::LONG;
1754 desc.update_steps();
1756 lilv_nodes_free(units);
1757 lilv_node_free(label);
1758 lilv_node_free(def);
1759 lilv_node_free(minimum);
1760 lilv_node_free(maximum);
1764 LV2Plugin::load_supported_properties(PropertyDescriptors& descs)
1766 LilvWorld* lworld = _world.world;
1767 const LilvNode* subject = lilv_plugin_get_uri(_impl->plugin);
1768 LilvNodes* properties = lilv_world_find_nodes(
1769 lworld, subject, _world.patch_writable, NULL);
1770 LILV_FOREACH(nodes, p, properties) {
1771 // Get label and range
1772 const LilvNode* prop = lilv_nodes_get(properties, p);
1773 LilvNode* range = get_value(lworld, prop, _world.rdfs_range);
1775 warning << string_compose(_("LV2: property <%1> has no range datatype, ignoring"),
1776 lilv_node_as_uri(prop)) << endmsg;
1780 // Convert range to variant type (TODO: support for multiple range types)
1781 Variant::Type datatype;
1782 if (!uri_to_variant_type(lilv_node_as_uri(range), datatype)) {
1783 error << string_compose(_("LV2: property <%1> has unsupported datatype <%1>"),
1784 lilv_node_as_uri(prop), lilv_node_as_uri(range)) << endmsg;
1788 // Add description to result
1789 ParameterDescriptor desc;
1790 desc.key = _uri_map.uri_to_id(lilv_node_as_uri(prop));
1791 desc.datatype = datatype;
1792 load_parameter_descriptor(_world, desc, datatype, prop);
1793 descs.insert(std::make_pair(desc.key, desc));
1795 lilv_node_free(range);
1797 lilv_nodes_free(properties);
1801 LV2Plugin::announce_property_values()
1803 if (_patch_port_in_index == (uint32_t)-1) {
1807 // Set up forge to write to temporary buffer on the stack
1808 LV2_Atom_Forge* forge = &_impl->ui_forge;
1809 LV2_Atom_Forge_Frame frame;
1810 uint8_t buf[PATH_MAX]; // Ought to be enough for anyone...
1812 lv2_atom_forge_set_buffer(forge, buf, sizeof(buf));
1814 // Serialize patch:Get message with no subject (implicitly plugin instance)
1815 #ifdef HAVE_LV2_1_10_0
1816 lv2_atom_forge_object(forge, &frame, 1, _uri_map.urids.patch_Get);
1818 lv2_atom_forge_blank(forge, &frame, 1, _uri_map.urids.patch_Get);
1821 // Write message to UI=>Plugin ring
1822 const LV2_Atom* const atom = (const LV2_Atom*)buf;
1823 write_from_ui(_patch_port_in_index,
1824 _uri_map.urids.atom_eventTransfer,
1825 lv2_atom_total_size(atom),
1826 (const uint8_t*)atom);
1830 LV2Plugin::enable_ui_emission()
1833 /* see note in LV2Plugin::write_from_ui() */
1834 uint32_t bufsiz = 32768;
1835 if (_atom_ev_buffers && _atom_ev_buffers[0]) {
1836 bufsiz = lv2_evbuf_get_capacity(_atom_ev_buffers[0]);
1838 size_t rbs = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
1839 rbs = max((size_t) bufsiz * 8, rbs);
1840 _to_ui = new RingBuffer<uint8_t>(rbs);
1845 LV2Plugin::emit_to_ui(void* controller, UIMessageSink sink)
1851 uint32_t read_space = _to_ui->read_space();
1852 while (read_space > sizeof(UIMessage)) {
1854 if (_to_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
1855 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
1858 vector<uint8_t> body(msg.size);
1859 if (_to_ui->read(&body[0], msg.size) != msg.size) {
1860 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
1864 sink(controller, msg.index, msg.size, msg.protocol, &body[0]);
1866 read_space -= sizeof(msg) + msg.size;
1871 LV2Plugin::work(Worker& worker, uint32_t size, const void* data)
1873 Glib::Threads::Mutex::Lock lm(_work_mutex);
1874 return _impl->work_iface->work(
1875 _impl->instance->lv2_handle, work_respond, &worker, size, data);
1879 LV2Plugin::work_response(uint32_t size, const void* data)
1881 return _impl->work_iface->work_response(
1882 _impl->instance->lv2_handle, size, data);
1886 LV2Plugin::set_insert_id(PBD::ID id)
1888 if (_insert_id == "0") {
1890 } else if (_insert_id != id) {
1891 lilv_state_free(_impl->state);
1892 _impl->state = NULL;
1898 LV2Plugin::set_state_dir (const std::string& d)
1900 _plugin_state_dir = d;
1904 LV2Plugin::set_state(const XMLNode& node, int version)
1907 XMLProperty const * prop;
1908 XMLNodeConstIterator iter;
1915 if (node.name() != state_node_name()) {
1916 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
1920 #ifndef NO_PLUGIN_STATE
1922 if (version < 3000) {
1923 nodes = node.children("port");
1925 nodes = node.children("Port");
1928 for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
1932 if ((prop = child->property("symbol")) != 0) {
1933 sym = prop->value().c_str();
1935 warning << _("LV2: port has no symbol, ignored") << endmsg;
1939 map<string, uint32_t>::iterator i = _port_indices.find(sym);
1941 if (i != _port_indices.end()) {
1942 port_id = i->second;
1944 warning << _("LV2: port has unknown index, ignored") << endmsg;
1948 if ((prop = child->property("value")) != 0) {
1949 value = prop->value().c_str();
1951 warning << _("LV2: port has no value, ignored") << endmsg;
1955 set_parameter(port_id, atof(value));
1958 if ((prop = node.property("template-dir")) != 0) {
1959 set_state_dir (prop->value ());
1963 if ((prop = node.property("state-dir")) != 0) {
1964 if (sscanf(prop->value().c_str(), "state%u", &_state_version) != 1) {
1965 error << string_compose(
1966 "LV2: failed to parse state version from \"%1\"",
1967 prop->value()) << endmsg;
1970 std::string state_file = Glib::build_filename(
1972 Glib::build_filename(prop->value(), "state.ttl"));
1974 LilvState* state = lilv_state_new_from_file(
1975 _world.world, _uri_map.urid_map(), NULL, state_file.c_str());
1977 lilv_state_restore(state, _impl->instance, NULL, NULL, 0, NULL);
1978 lilv_state_free(_impl->state);
1979 _impl->state = state;
1982 if (!_plugin_state_dir.empty ()) {
1983 // force save with session, next time (increment counter)
1984 lilv_state_free (_impl->state);
1985 _impl->state = NULL;
1989 latency_compute_run();
1992 return Plugin::set_state(node, version);
1996 LV2Plugin::get_parameter_descriptor(uint32_t which, ParameterDescriptor& desc) const
1998 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, which);
2000 error << string_compose("LV2: get descriptor of non-existent port %1", which)
2005 LilvNodes* portunits;
2006 LilvNode *def, *min, *max;
2007 lilv_port_get_range(_impl->plugin, port, &def, &min, &max);
2008 portunits = lilv_port_get_value(_impl->plugin, port, _world.units_unit);
2010 LilvNode* steps = lilv_port_get(_impl->plugin, port, _world.ext_rangeSteps);
2012 // TODO: Once we can rely on lilv 0.18.0 being present,
2013 // load_parameter_descriptor() can be used for ports as well
2014 desc.integer_step = lilv_port_has_property(_impl->plugin, port, _world.lv2_integer);
2015 desc.toggled = lilv_port_has_property(_impl->plugin, port, _world.lv2_toggled);
2016 desc.logarithmic = lilv_port_has_property(_impl->plugin, port, _world.ext_logarithmic);
2017 desc.sr_dependent = lilv_port_has_property(_impl->plugin, port, _world.lv2_sampleRate);
2018 desc.label = lilv_node_as_string(lilv_port_get_name(_impl->plugin, port));
2019 desc.normal = def ? lilv_node_as_float(def) : 0.0f;
2020 desc.lower = min ? lilv_node_as_float(min) : 0.0f;
2021 desc.upper = max ? lilv_node_as_float(max) : 1.0f;
2022 load_parameter_descriptor_units(_world.world, desc, portunits);
2024 if (desc.sr_dependent) {
2025 desc.lower *= _session.frame_rate ();
2026 desc.upper *= _session.frame_rate ();
2029 desc.min_unbound = false; // TODO: LV2 extension required
2030 desc.max_unbound = false; // TODO: LV2 extension required
2032 desc.enumeration = lilv_port_has_property(_impl->plugin, port, _world.lv2_enumeration);
2033 desc.scale_points = get_scale_points(which);
2035 desc.update_steps();
2038 //override auto-calculated steps in update_steps()
2039 float s = lilv_node_as_float (steps);
2040 const float delta = desc.upper - desc.lower;
2042 desc.step = desc.smallstep = (delta / s);
2043 desc.largestep = std::min ((delta / 5.0f), 10.f * desc.smallstep);
2045 if (desc.logarithmic) {
2046 // TODO marry AutomationControl::internal_to_interface () with
2047 // http://lv2plug.in/ns/ext/port-props/#rangeSteps
2048 desc.smallstep = desc.smallstep / logf(s);
2049 desc.step = desc.step / logf(s);
2050 desc.largestep = desc.largestep / logf(s);
2051 } else if (desc.integer_step) {
2052 desc.smallstep = 1.0;
2053 desc.step = std::max(1.f, rintf (desc.step));
2054 desc.largestep = std::max(1.f, rintf (desc.largestep));
2056 DEBUG_TRACE(DEBUG::LV2, string_compose("parameter %1 small: %2, step: %3 largestep: %4\n",
2057 which, desc.smallstep, desc.step, desc.largestep));
2061 lilv_node_free(def);
2062 lilv_node_free(min);
2063 lilv_node_free(max);
2064 lilv_node_free(steps);
2065 lilv_nodes_free(portunits);
2070 Plugin::IOPortDescription
2071 LV2Plugin::describe_io_port (ARDOUR::DataType dt, bool input, uint32_t id) const
2073 PortFlags match = 0;
2075 case DataType::AUDIO:
2078 case DataType::MIDI:
2079 match = PORT_SEQUENCE | PORT_MIDI; // ignore old PORT_EVENT
2082 return Plugin::IOPortDescription ("?");
2086 match |= PORT_INPUT;
2088 match |= PORT_OUTPUT;
2092 uint32_t idx = UINT32_MAX;
2094 uint32_t const num_ports = parameter_count();
2095 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2096 PortFlags flags = _port_flags[port_index];
2097 if ((flags & match) == match) {
2104 if (idx == UINT32_MAX) {
2105 return Plugin::IOPortDescription ("?");
2108 LilvNode* name = lilv_port_get_name(_impl->plugin,
2109 lilv_plugin_get_port_by_index(_impl->plugin, idx));
2110 Plugin::IOPortDescription iod (lilv_node_as_string (name));
2111 lilv_node_free(name);
2113 if (lilv_port_has_property(_impl->plugin,
2114 lilv_plugin_get_port_by_index(_impl->plugin, idx), _world.lv2_isSideChain)) {
2115 iod.is_sidechain = true;
2121 LV2Plugin::describe_parameter(Evoral::Parameter which)
2123 if (( which.type() == PluginAutomation) && ( which.id() < parameter_count()) ) {
2125 if (lilv_port_has_property(_impl->plugin,
2126 lilv_plugin_get_port_by_index(_impl->plugin, which.id()), _world.ext_notOnGUI)) {
2127 return X_("hidden");
2130 if (lilv_port_has_property(_impl->plugin,
2131 lilv_plugin_get_port_by_index(_impl->plugin, which.id()), _world.lv2_freewheeling)) {
2132 return X_("hidden");
2135 if (lilv_port_has_property(_impl->plugin,
2136 lilv_plugin_get_port_by_index(_impl->plugin, which.id()), _world.lv2_reportsLatency)) {
2137 return X_("latency");
2140 LilvNode* name = lilv_port_get_name(_impl->plugin,
2141 lilv_plugin_get_port_by_index(_impl->plugin, which.id()));
2142 string ret(lilv_node_as_string(name));
2143 lilv_node_free(name);
2151 LV2Plugin::max_latency () const
2153 return _max_latency;
2157 LV2Plugin::signal_latency() const
2159 if (_latency_control_port) {
2160 return (framecnt_t)floor(*_latency_control_port);
2166 set<Evoral::Parameter>
2167 LV2Plugin::automatable() const
2169 set<Evoral::Parameter> ret;
2171 for (uint32_t i = 0; i < parameter_count(); ++i) {
2172 if (parameter_is_input(i) && parameter_is_control(i) && !(_port_flags[i] & PORT_NOAUTO)) {
2173 ret.insert(ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
2177 for (PropertyDescriptors::const_iterator p = _property_descriptors.begin();
2178 p != _property_descriptors.end();
2180 ret.insert(ret.end(), Evoral::Parameter(PluginPropertyAutomation, 0, p->first));
2186 LV2Plugin::set_automation_control (uint32_t i, boost::shared_ptr<AutomationControl> c)
2188 if ((_port_flags[i] & (PORT_CTRLED | PORT_CTRLER))) {
2189 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("Ctrl Port %1\n", i));
2190 _ctrl_map [i] = AutomationCtrlPtr (new AutomationCtrl(c));
2194 LV2Plugin::AutomationCtrlPtr
2195 LV2Plugin::get_automation_control (uint32_t i)
2197 if (_ctrl_map.find (i) == _ctrl_map.end()) {
2198 return AutomationCtrlPtr ();
2200 return _ctrl_map[i];
2204 LV2Plugin::activate()
2206 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 activate\n", name()));
2208 if (!_was_activated) {
2209 lilv_instance_activate(_impl->instance);
2210 _was_activated = true;
2215 LV2Plugin::deactivate()
2217 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 deactivate\n", name()));
2219 if (_was_activated) {
2220 lilv_instance_deactivate(_impl->instance);
2221 _was_activated = false;
2226 LV2Plugin::cleanup()
2228 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 cleanup\n", name()));
2231 lilv_instance_free(_impl->instance);
2232 _impl->instance = NULL;
2236 LV2Plugin::allocate_atom_event_buffers()
2238 /* reserve local scratch buffers for ATOM event-queues */
2239 const LilvPlugin* p = _impl->plugin;
2241 /* count non-MIDI atom event-ports
2242 * TODO: nicely ask drobilla to make a lilv_ call for that
2244 int count_atom_out = 0;
2245 int count_atom_in = 0;
2246 int minimumSize = 32768; // TODO use a per-port minimum-size
2247 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
2248 const LilvPort* port = lilv_plugin_get_port_by_index(p, i);
2249 if (lilv_port_is_a(p, port, _world.atom_AtomPort)) {
2250 LilvNodes* buffer_types = lilv_port_get_value(
2251 p, port, _world.atom_bufferType);
2252 LilvNodes* atom_supports = lilv_port_get_value(
2253 p, port, _world.atom_supports);
2255 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
2256 if (lilv_port_is_a(p, port, _world.lv2_InputPort)) {
2259 if (lilv_port_is_a(p, port, _world.lv2_OutputPort)) {
2262 LilvNodes* min_size_v = lilv_port_get_value(_impl->plugin, port, _world.rsz_minimumSize);
2263 LilvNode* min_size = min_size_v ? lilv_nodes_get_first(min_size_v) : NULL;
2264 if (min_size && lilv_node_is_int(min_size)) {
2265 minimumSize = std::max(minimumSize, lilv_node_as_int(min_size));
2267 lilv_nodes_free(min_size_v);
2269 lilv_nodes_free(buffer_types);
2270 lilv_nodes_free(atom_supports);
2274 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 need buffers for %2 atom-in and %3 atom-out event-ports\n",
2275 name(), count_atom_in, count_atom_out));
2277 const int total_atom_buffers = (count_atom_in + count_atom_out);
2278 if (_atom_ev_buffers || total_atom_buffers == 0) {
2282 DEBUG_TRACE(DEBUG::LV2, string_compose("allocate %1 atom_ev_buffers of %2 bytes\n", total_atom_buffers, minimumSize));
2283 _atom_ev_buffers = (LV2_Evbuf**) malloc((total_atom_buffers + 1) * sizeof(LV2_Evbuf*));
2284 for (int i = 0; i < total_atom_buffers; ++i ) {
2285 _atom_ev_buffers[i] = lv2_evbuf_new(minimumSize, LV2_EVBUF_ATOM,
2286 _uri_map.urids.atom_Chunk, _uri_map.urids.atom_Sequence);
2288 _atom_ev_buffers[total_atom_buffers] = 0;
2292 /** Write an ardour position/time/tempo/meter as an LV2 event.
2293 * @return true on success.
2296 write_position(LV2_Atom_Forge* forge,
2298 const TempoMetric& t,
2299 Timecode::BBT_Time& bbt,
2302 framepos_t position,
2305 const URIMap::URIDs& urids = URIMap::instance().urids;
2307 uint8_t pos_buf[256];
2308 lv2_atom_forge_set_buffer(forge, pos_buf, sizeof(pos_buf));
2309 LV2_Atom_Forge_Frame frame;
2310 #ifdef HAVE_LV2_1_10_0
2311 lv2_atom_forge_object(forge, &frame, 1, urids.time_Position);
2312 lv2_atom_forge_key(forge, urids.time_frame);
2313 lv2_atom_forge_long(forge, position);
2314 lv2_atom_forge_key(forge, urids.time_speed);
2315 lv2_atom_forge_float(forge, speed);
2316 lv2_atom_forge_key(forge, urids.time_barBeat);
2317 lv2_atom_forge_float(forge, bbt.beats - 1 +
2318 (bbt.ticks / Timecode::BBT_Time::ticks_per_beat));
2319 lv2_atom_forge_key(forge, urids.time_bar);
2320 lv2_atom_forge_long(forge, bbt.bars - 1);
2321 lv2_atom_forge_key(forge, urids.time_beatUnit);
2322 lv2_atom_forge_int(forge, t.meter().note_divisor());
2323 lv2_atom_forge_key(forge, urids.time_beatsPerBar);
2324 lv2_atom_forge_float(forge, t.meter().divisions_per_bar());
2325 lv2_atom_forge_key(forge, urids.time_beatsPerMinute);
2326 lv2_atom_forge_float(forge, bpm);
2328 lv2_atom_forge_blank(forge, &frame, 1, urids.time_Position);
2329 lv2_atom_forge_property_head(forge, urids.time_frame, 0);
2330 lv2_atom_forge_long(forge, position);
2331 lv2_atom_forge_property_head(forge, urids.time_speed, 0);
2332 lv2_atom_forge_float(forge, speed);
2333 lv2_atom_forge_property_head(forge, urids.time_barBeat, 0);
2334 lv2_atom_forge_float(forge, bbt.beats - 1 +
2335 (bbt.ticks / Timecode::BBT_Time::ticks_per_beat));
2336 lv2_atom_forge_property_head(forge, urids.time_bar, 0);
2337 lv2_atom_forge_long(forge, bbt.bars - 1);
2338 lv2_atom_forge_property_head(forge, urids.time_beatUnit, 0);
2339 lv2_atom_forge_int(forge, t.meter().note_divisor());
2340 lv2_atom_forge_property_head(forge, urids.time_beatsPerBar, 0);
2341 lv2_atom_forge_float(forge, t.meter().divisions_per_bar());
2342 lv2_atom_forge_property_head(forge, urids.time_beatsPerMinute, 0);
2343 lv2_atom_forge_float(forge, bpm);
2346 LV2_Evbuf_Iterator end = lv2_evbuf_end(buf);
2347 const LV2_Atom* const atom = (const LV2_Atom*)pos_buf;
2348 return lv2_evbuf_write(&end, offset, 0, atom->type, atom->size,
2349 (const uint8_t*)(atom + 1));
2353 LV2Plugin::connect_and_run(BufferSet& bufs,
2354 framepos_t start, framepos_t end, double speed,
2355 ChanMapping in_map, ChanMapping out_map,
2356 pframes_t nframes, framecnt_t offset)
2358 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 run %2 offset %3\n", name(), nframes, offset));
2359 Plugin::connect_and_run(bufs, start, end, speed, in_map, out_map, nframes, offset);
2361 cycles_t then = get_cycles();
2363 TempoMap& tmap = _session.tempo_map();
2364 Metrics::const_iterator metric_i = tmap.metrics_end();
2365 TempoMetric tmetric = tmap.metric_at(start, &metric_i);
2367 if (_freewheel_control_port) {
2368 *_freewheel_control_port = _session.engine().freewheeling() ? 1.f : 0.f;
2371 if (_bpm_control_port) {
2372 *_bpm_control_port = tmap.tempo_at_frame (start).beats_per_minute();
2376 if (_can_write_automation && start != _next_cycle_start) {
2377 // add guard-points after locating
2378 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2379 i->second->guard = true;
2384 ChanCount bufs_count;
2385 bufs_count.set(DataType::AUDIO, 1);
2386 bufs_count.set(DataType::MIDI, 1);
2387 BufferSet& silent_bufs = _session.get_silent_buffers(bufs_count);
2388 BufferSet& scratch_bufs = _session.get_scratch_buffers(bufs_count);
2389 uint32_t const num_ports = parameter_count();
2390 uint32_t const nil_index = std::numeric_limits<uint32_t>::max();
2392 uint32_t audio_in_index = 0;
2393 uint32_t audio_out_index = 0;
2394 uint32_t midi_in_index = 0;
2395 uint32_t midi_out_index = 0;
2396 uint32_t atom_port_index = 0;
2397 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2399 uint32_t index = nil_index;
2400 PortFlags flags = _port_flags[port_index];
2402 if (flags & PORT_AUDIO) {
2403 if (flags & PORT_INPUT) {
2404 index = in_map.get(DataType::AUDIO, audio_in_index++, &valid);
2406 ? bufs.get_audio(index).data(offset)
2407 : silent_bufs.get_audio(0).data(offset);
2409 index = out_map.get(DataType::AUDIO, audio_out_index++, &valid);
2411 ? bufs.get_audio(index).data(offset)
2412 : scratch_bufs.get_audio(0).data(offset);
2414 } else if (flags & (PORT_EVENT|PORT_SEQUENCE)) {
2415 /* FIXME: The checks here for bufs.count().n_midi() > index shouldn't
2416 be necessary, but the mapping is illegal in some cases. Ideally
2417 that should be fixed, but this is easier...
2419 if (flags & PORT_MIDI) {
2420 if (flags & PORT_INPUT) {
2421 index = in_map.get(DataType::MIDI, midi_in_index++, &valid);
2423 index = out_map.get(DataType::MIDI, midi_out_index++, &valid);
2425 if (valid && bufs.count().n_midi() > index) {
2426 /* Note, ensure_lv2_bufsize() is not RT safe!
2427 * However free()/alloc() is only called if a
2428 * plugin requires a rsz:minimumSize buffersize
2429 * and the existing buffer if smaller.
2431 bufs.ensure_lv2_bufsize((flags & PORT_INPUT), index, _port_minimumSize[port_index]);
2432 _ev_buffers[port_index] = bufs.get_lv2_midi(
2433 (flags & PORT_INPUT), index, (flags & PORT_EVENT));
2435 } else if ((flags & PORT_POSITION) && (flags & PORT_INPUT)) {
2436 lv2_evbuf_reset(_atom_ev_buffers[atom_port_index], true);
2437 _ev_buffers[port_index] = _atom_ev_buffers[atom_port_index++];
2441 if (valid && (flags & PORT_INPUT)) {
2442 if ((flags & PORT_POSITION)) {
2443 Timecode::BBT_Time bbt (tmap.bbt_at_frame (start));
2444 double bpm = tmap.tempo_at_frame (start).beats_per_minute();
2445 double beatpos = (bbt.bars - 1) * tmetric.meter().divisions_per_bar()
2447 + (bbt.ticks / Timecode::BBT_Time::ticks_per_beat);
2448 beatpos *= tmetric.meter().note_divisor() / 4.0;
2449 if (start != _next_cycle_start ||
2450 speed != _next_cycle_speed ||
2451 rint (1000 * beatpos) != rint(1000 * _next_cycle_beat) ||
2452 bpm != _current_bpm) {
2453 // Transport or Tempo has changed, write position at cycle start
2454 write_position(&_impl->forge, _ev_buffers[port_index],
2455 tmetric, bbt, speed, bpm, start, 0);
2459 // Get MIDI iterator range (empty range if no MIDI)
2460 MidiBuffer::iterator m = (index != nil_index)
2461 ? bufs.get_midi(index).begin()
2462 : silent_bufs.get_midi(0).end();
2463 MidiBuffer::iterator m_end = (index != nil_index)
2464 ? bufs.get_midi(index).end()
2467 // Now merge MIDI and any transport events into the buffer
2468 const uint32_t type = _uri_map.urids.midi_MidiEvent;
2469 const framepos_t tend = end;
2471 while (m != m_end || (metric_i != tmap.metrics_end() &&
2472 (*metric_i)->frame() < tend)) {
2473 MetricSection* metric = (metric_i != tmap.metrics_end())
2475 if (m != m_end && (!metric || metric->frame() > (*m).time())) {
2476 const Evoral::MIDIEvent<framepos_t> ev(*m, false);
2477 if (ev.time() < nframes) {
2478 LV2_Evbuf_Iterator eend = lv2_evbuf_end(_ev_buffers[port_index]);
2479 lv2_evbuf_write(&eend, ev.time(), 0, type, ev.size(), ev.buffer());
2483 tmetric.set_metric(metric);
2484 Timecode::BBT_Time bbt;
2485 bbt = tmap.bbt_at_pulse (metric->pulse());
2486 double bpm = tmap.tempo_at_frame (start/*XXX*/).beats_per_minute();
2487 write_position(&_impl->forge, _ev_buffers[port_index],
2488 tmetric, bbt, speed, bpm,
2490 metric->frame() - start);
2494 } else if (!valid) {
2495 // Nothing we understand or care about, connect to scratch
2496 // see note for midi-buffer size above
2497 scratch_bufs.ensure_lv2_bufsize((flags & PORT_INPUT),
2498 0, _port_minimumSize[port_index]);
2499 _ev_buffers[port_index] = scratch_bufs.get_lv2_midi(
2500 (flags & PORT_INPUT), 0, (flags & PORT_EVENT));
2503 buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
2505 continue; // Control port, leave buffer alone
2507 lilv_instance_connect_port(_impl->instance, port_index, buf);
2510 // Read messages from UI and push into appropriate buffers
2512 uint32_t read_space = _from_ui->read_space();
2513 while (read_space > sizeof(UIMessage)) {
2515 if (_from_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
2516 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
2519 vector<uint8_t> body(msg.size);
2520 if (_from_ui->read(&body[0], msg.size) != msg.size) {
2521 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
2524 if (msg.protocol == URIMap::instance().urids.atom_eventTransfer) {
2525 LV2_Evbuf* buf = _ev_buffers[msg.index];
2526 LV2_Evbuf_Iterator i = lv2_evbuf_end(buf);
2527 const LV2_Atom* const atom = (const LV2_Atom*)&body[0];
2528 if (!lv2_evbuf_write(&i, nframes - 1, 0, atom->type, atom->size,
2529 (const uint8_t*)(atom + 1))) {
2530 error << "Failed to write data to LV2 event buffer\n";
2533 error << "Received unknown message type from UI" << endmsg;
2535 read_space -= sizeof(UIMessage) + msg.size;
2542 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2543 PortFlags flags = _port_flags[port_index];
2546 /* TODO ask drobilla about comment
2547 * "Make Ardour event buffers generic so plugins can communicate"
2548 * in libs/ardour/buffer_set.cc:310
2550 * ideally the user could choose which of the following two modes
2551 * to use (e.g. instrument/effect chains MIDI OUT vs MIDI TRHU).
2553 * This implementation follows the discussion on IRC Mar 16 2013 16:47 UTC
2554 * 16:51 < drobilla> rgareus: [..] i.e always replace with MIDI output [of LV2 plugin] if it's there
2555 * 16:52 < drobilla> rgareus: That would probably be good enough [..] to make users not complain
2556 * for quite a while at least ;)
2558 // copy output of LV2 plugin's MIDI port to Ardour MIDI buffers -- MIDI OUT
2559 if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE|PORT_MIDI))) {
2560 const uint32_t buf_index = out_map.get(
2561 DataType::MIDI, midi_out_index++, &valid);
2563 bufs.forward_lv2_midi(_ev_buffers[port_index], buf_index);
2566 // Flush MIDI (write back to Ardour MIDI buffers) -- MIDI THRU
2567 else if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE))) {
2568 const uint32_t buf_index = out_map.get(
2569 DataType::MIDI, midi_out_index++, &valid);
2571 bufs.flush_lv2_midi(true, buf_index);
2575 // Write messages to UI
2576 if ((_to_ui || _can_write_automation || _patch_port_out_index != (uint32_t)-1) &&
2577 (flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE))) {
2578 LV2_Evbuf* buf = _ev_buffers[port_index];
2579 for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(buf);
2580 lv2_evbuf_is_valid(i);
2581 i = lv2_evbuf_next(i)) {
2582 uint32_t frames, subframes, type, size;
2584 lv2_evbuf_get(i, &frames, &subframes, &type, &size, &data);
2587 // Intercept Automation Write Events
2588 if ((flags & PORT_AUTOCTRL)) {
2589 LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2590 if (atom->type == _uri_map.urids.atom_Blank ||
2591 atom->type == _uri_map.urids.atom_Object) {
2592 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2593 if (obj->body.otype == _uri_map.urids.auto_event) {
2594 // only if transport_rolling ??
2595 const LV2_Atom* parameter = NULL;
2596 const LV2_Atom* value = NULL;
2597 lv2_atom_object_get(obj,
2598 _uri_map.urids.auto_parameter, ¶meter,
2599 _uri_map.urids.auto_value, &value,
2601 if (parameter && value) {
2602 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2603 const float v = ((const LV2_Atom_Float*)value)->body;
2604 // -> add automation event..
2605 DEBUG_TRACE(DEBUG::LV2Automate,
2606 string_compose ("Event p: %1 t: %2 v: %3\n", p, frames, v));
2607 AutomationCtrlPtr c = get_automation_control (p);
2609 (c->ac->automation_state() == Touch || c->ac->automation_state() == Write)
2611 framepos_t when = std::max ((framepos_t) 0, start + frames - _current_latency);
2612 assert (start + frames - _current_latency >= 0);
2615 c->ac->list()->add (when, v, true, true);
2617 c->ac->set_double (v, when, true);
2622 else if (obj->body.otype == _uri_map.urids.auto_setup) {
2623 // TODO optional arguments, for now we assume the plugin
2624 // writes automation for its own inputs
2625 // -> put them in "touch" mode (preferably "exclusive plugin touch(TM)"
2626 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2627 if (_port_flags[i->first] & PORT_CTRLED) {
2628 DEBUG_TRACE(DEBUG::LV2Automate,
2629 string_compose ("Setup p: %1\n", i->first));
2630 i->second->ac->set_automation_state (Touch);
2634 else if (obj->body.otype == _uri_map.urids.auto_finalize) {
2635 // set [touched] parameters to "play" ??
2636 // allow plugin to change its mode (from analyze to apply)
2637 const LV2_Atom* parameter = NULL;
2638 const LV2_Atom* value = NULL;
2639 lv2_atom_object_get(obj,
2640 _uri_map.urids.auto_parameter, ¶meter,
2641 _uri_map.urids.auto_value, &value,
2643 if (parameter && value) {
2644 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2645 const float v = ((const LV2_Atom_Float*)value)->body;
2646 AutomationCtrlPtr c = get_automation_control (p);
2647 DEBUG_TRACE(DEBUG::LV2Automate,
2648 string_compose ("Finalize p: %1 v: %2\n", p, v));
2649 if (c && _port_flags[p] & PORT_CTRLER) {
2650 c->ac->set_value(v, Controllable::NoGroup);
2653 DEBUG_TRACE(DEBUG::LV2Automate, "Finalize\n");
2655 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2656 // guard will be false if an event was written
2657 if ((_port_flags[i->first] & PORT_CTRLED) && !i->second->guard) {
2658 DEBUG_TRACE(DEBUG::LV2Automate,
2659 string_compose ("Thin p: %1\n", i->first));
2660 i->second->ac->alist ()->thin (20);
2664 else if (obj->body.otype == _uri_map.urids.auto_start) {
2665 const LV2_Atom* parameter = NULL;
2666 lv2_atom_object_get(obj,
2667 _uri_map.urids.auto_parameter, ¶meter,
2670 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2671 AutomationCtrlPtr c = get_automation_control (p);
2672 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("Start Touch p: %1\n", p));
2674 c->ac->start_touch (std::max ((framepos_t)0, start - _current_latency));
2679 else if (obj->body.otype == _uri_map.urids.auto_end) {
2680 const LV2_Atom* parameter = NULL;
2681 lv2_atom_object_get(obj,
2682 _uri_map.urids.auto_parameter, ¶meter,
2685 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2686 AutomationCtrlPtr c = get_automation_control (p);
2687 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("End Touch p: %1\n", p));
2689 c->ac->stop_touch (true, std::max ((framepos_t)0, start - _current_latency));
2696 // Intercept state dirty message
2697 if (_has_state_interface /* && (flags & PORT_DIRTYMSG)*/) {
2698 LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2699 if (atom->type == _uri_map.urids.atom_Blank ||
2700 atom->type == _uri_map.urids.atom_Object) {
2701 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2702 if (obj->body.otype == _uri_map.urids.state_Dirty) {
2703 _session.set_dirty ();
2709 // Intercept patch change messages to emit PropertyChanged signal
2710 if ((flags & PORT_PATCHMSG)) {
2711 LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2712 if (atom->type == _uri_map.urids.atom_Blank ||
2713 atom->type == _uri_map.urids.atom_Object) {
2714 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2715 if (obj->body.otype == _uri_map.urids.patch_Set) {
2716 const LV2_Atom* property = NULL;
2717 const LV2_Atom* value = NULL;
2718 lv2_atom_object_get(obj,
2719 _uri_map.urids.patch_property, &property,
2720 _uri_map.urids.patch_value, &value,
2723 if (property && value &&
2724 property->type == _uri_map.urids.atom_URID &&
2725 value->type == _uri_map.urids.atom_Path) {
2726 const uint32_t prop_id = ((const LV2_Atom_URID*)property)->body;
2727 const char* path = (const char*)LV2_ATOM_BODY_CONST(value);
2729 // Emit PropertyChanged signal for UI
2730 // TODO: This should emit the control's Changed signal
2731 PropertyChanged(prop_id, Variant(Variant::PATH, path));
2733 std::cerr << "warning: patch:Set for unknown property" << std::endl;
2739 if (!_to_ui) continue;
2740 write_to_ui(port_index, URIMap::instance().urids.atom_eventTransfer,
2741 size + sizeof(LV2_Atom),
2742 data - sizeof(LV2_Atom));
2747 cycles_t now = get_cycles();
2748 set_cycles((uint32_t)(now - then));
2750 // Update expected transport information for next cycle so we can detect changes
2751 _next_cycle_speed = speed;
2752 _next_cycle_start = end;
2755 /* keep track of lv2:timePosition like plugins can do.
2756 * Note: for no-midi plugins, we only ever send information at cycle-start,
2757 * so it needs to be realative to that.
2759 TempoMetric t = tmap.metric_at(start);
2760 _current_bpm = tmap.tempo_at_frame (start).beats_per_minute();
2761 Timecode::BBT_Time bbt (tmap.bbt_at_frame (start));
2762 double beatpos = (bbt.bars - 1) * t.meter().divisions_per_bar()
2764 + (bbt.ticks / Timecode::BBT_Time::ticks_per_beat);
2765 beatpos *= tmetric.meter().note_divisor() / 4.0;
2766 _next_cycle_beat = beatpos + nframes * speed * _current_bpm / (60.f * _session.frame_rate());
2769 if (_latency_control_port) {
2770 framecnt_t new_latency = signal_latency ();
2771 _current_latency = new_latency;
2777 LV2Plugin::parameter_is_control(uint32_t param) const
2779 assert(param < _port_flags.size());
2780 return _port_flags[param] & PORT_CONTROL;
2784 LV2Plugin::parameter_is_audio(uint32_t param) const
2786 assert(param < _port_flags.size());
2787 return _port_flags[param] & PORT_AUDIO;
2791 LV2Plugin::parameter_is_event(uint32_t param) const
2793 assert(param < _port_flags.size());
2794 return _port_flags[param] & PORT_EVENT;
2798 LV2Plugin::parameter_is_output(uint32_t param) const
2800 assert(param < _port_flags.size());
2801 return _port_flags[param] & PORT_OUTPUT;
2805 LV2Plugin::parameter_is_input(uint32_t param) const
2807 assert(param < _port_flags.size());
2808 return _port_flags[param] & PORT_INPUT;
2812 LV2Plugin::designated_bypass_port ()
2814 const LilvPort* port = NULL;
2815 LilvNode* designation = lilv_new_uri (_world.world, LV2_CORE_PREFIX "enabled");
2816 port = lilv_plugin_get_port_by_designation (
2817 _impl->plugin, _world.lv2_InputPort, designation);
2818 lilv_node_free(designation);
2820 return lilv_port_get_index (_impl->plugin, port);
2823 /* deprecated on 2016-Sep-18 in favor of lv2:enabled */
2824 designation = lilv_new_uri (_world.world, LV2_PROCESSING_URI__enable);
2825 port = lilv_plugin_get_port_by_designation (
2826 _impl->plugin, _world.lv2_InputPort, designation);
2827 lilv_node_free(designation);
2829 return lilv_port_get_index (_impl->plugin, port);
2836 LV2Plugin::print_parameter(uint32_t param, char* buf, uint32_t len) const
2839 if (param < parameter_count()) {
2840 snprintf(buf, len, "%.3f", get_parameter(param));
2847 boost::shared_ptr<ScalePoints>
2848 LV2Plugin::get_scale_points(uint32_t port_index) const
2850 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, port_index);
2851 LilvScalePoints* points = lilv_port_get_scale_points(_impl->plugin, port);
2853 boost::shared_ptr<ScalePoints> ret;
2858 ret = boost::shared_ptr<ScalePoints>(new ScalePoints());
2860 LILV_FOREACH(scale_points, i, points) {
2861 const LilvScalePoint* p = lilv_scale_points_get(points, i);
2862 const LilvNode* label = lilv_scale_point_get_label(p);
2863 const LilvNode* value = lilv_scale_point_get_value(p);
2864 if (label && (lilv_node_is_float(value) || lilv_node_is_int(value))) {
2865 ret->insert(make_pair(lilv_node_as_string(label),
2866 lilv_node_as_float(value)));
2870 lilv_scale_points_free(points);
2875 LV2Plugin::run(pframes_t nframes, bool sync_work)
2877 uint32_t const N = parameter_count();
2878 for (uint32_t i = 0; i < N; ++i) {
2879 if (parameter_is_control(i) && parameter_is_input(i)) {
2880 _control_data[i] = _shadow_data[i];
2885 // Execute work synchronously if we're freewheeling (export)
2886 _worker->set_synchronous(sync_work || session().engine().freewheeling());
2889 // Run the plugin for this cycle
2890 lilv_instance_run(_impl->instance, nframes);
2892 // Emit any queued worker responses (calls a plugin callback)
2893 if (_state_worker) {
2894 _state_worker->emit_responses();
2897 _worker->emit_responses();
2900 // Notify the plugin that a work run cycle is complete
2901 if (_impl->work_iface) {
2902 if (_impl->work_iface->end_run) {
2903 _impl->work_iface->end_run(_impl->instance->lv2_handle);
2909 LV2Plugin::latency_compute_run()
2911 if (!_latency_control_port) {
2915 // Run the plugin so that it can set its latency parameter
2917 bool was_activated = _was_activated;
2920 uint32_t port_index = 0;
2921 uint32_t in_index = 0;
2922 uint32_t out_index = 0;
2924 // this is done in the main thread. non realtime.
2925 const framecnt_t bufsize = _engine.samples_per_cycle();
2926 float *buffer = (float*) malloc(_engine.samples_per_cycle() * sizeof(float));
2928 memset(buffer, 0, sizeof(float) * bufsize);
2930 // FIXME: Ensure plugins can handle in-place processing
2934 while (port_index < parameter_count()) {
2935 if (parameter_is_audio(port_index)) {
2936 if (parameter_is_input(port_index)) {
2937 lilv_instance_connect_port(_impl->instance, port_index, buffer);
2939 } else if (parameter_is_output(port_index)) {
2940 lilv_instance_connect_port(_impl->instance, port_index, buffer);
2949 if (was_activated) {
2956 LV2Plugin::Impl::designated_input (const char* uri, void** bufptrs[], void** bufptr)
2958 const LilvPort* port = NULL;
2959 LilvNode* designation = lilv_new_uri(_world.world, uri);
2960 port = lilv_plugin_get_port_by_designation(
2961 plugin, _world.lv2_InputPort, designation);
2962 lilv_node_free(designation);
2964 bufptrs[lilv_port_get_index(plugin, port)] = bufptr;
2969 static bool lv2_filter (const string& str, void* /*arg*/)
2971 /* Not a dotfile, has a prefix before a period, suffix is "lv2" */
2973 return str[0] != '.' && (str.length() > 3 && str.find (".lv2") == (str.length() - 4));
2977 LV2World::LV2World()
2978 : world(lilv_world_new())
2979 , _bundle_checked(false)
2981 atom_AtomPort = lilv_new_uri(world, LV2_ATOM__AtomPort);
2982 atom_Chunk = lilv_new_uri(world, LV2_ATOM__Chunk);
2983 atom_Sequence = lilv_new_uri(world, LV2_ATOM__Sequence);
2984 atom_bufferType = lilv_new_uri(world, LV2_ATOM__bufferType);
2985 atom_supports = lilv_new_uri(world, LV2_ATOM__supports);
2986 atom_eventTransfer = lilv_new_uri(world, LV2_ATOM__eventTransfer);
2987 ev_EventPort = lilv_new_uri(world, LILV_URI_EVENT_PORT);
2988 ext_logarithmic = lilv_new_uri(world, LV2_PORT_PROPS__logarithmic);
2989 ext_notOnGUI = lilv_new_uri(world, LV2_PORT_PROPS__notOnGUI);
2990 ext_expensive = lilv_new_uri(world, LV2_PORT_PROPS__expensive);
2991 ext_causesArtifacts= lilv_new_uri(world, LV2_PORT_PROPS__causesArtifacts);
2992 ext_notAutomatic = lilv_new_uri(world, LV2_PORT_PROPS__notAutomatic);
2993 ext_rangeSteps = lilv_new_uri(world, LV2_PORT_PROPS__rangeSteps);
2994 lv2_AudioPort = lilv_new_uri(world, LILV_URI_AUDIO_PORT);
2995 lv2_ControlPort = lilv_new_uri(world, LILV_URI_CONTROL_PORT);
2996 lv2_InputPort = lilv_new_uri(world, LILV_URI_INPUT_PORT);
2997 lv2_OutputPort = lilv_new_uri(world, LILV_URI_OUTPUT_PORT);
2998 lv2_inPlaceBroken = lilv_new_uri(world, LV2_CORE__inPlaceBroken);
2999 lv2_isSideChain = lilv_new_uri(world, LV2_CORE_PREFIX "isSideChain");
3000 lv2_integer = lilv_new_uri(world, LV2_CORE__integer);
3001 lv2_default = lilv_new_uri(world, LV2_CORE__default);
3002 lv2_minimum = lilv_new_uri(world, LV2_CORE__minimum);
3003 lv2_maximum = lilv_new_uri(world, LV2_CORE__maximum);
3004 lv2_reportsLatency = lilv_new_uri(world, LV2_CORE__reportsLatency);
3005 lv2_sampleRate = lilv_new_uri(world, LV2_CORE__sampleRate);
3006 lv2_toggled = lilv_new_uri(world, LV2_CORE__toggled);
3007 lv2_enumeration = lilv_new_uri(world, LV2_CORE__enumeration);
3008 lv2_freewheeling = lilv_new_uri(world, LV2_CORE__freeWheeling);
3009 midi_MidiEvent = lilv_new_uri(world, LILV_URI_MIDI_EVENT);
3010 rdfs_comment = lilv_new_uri(world, LILV_NS_RDFS "comment");
3011 rdfs_label = lilv_new_uri(world, LILV_NS_RDFS "label");
3012 rdfs_range = lilv_new_uri(world, LILV_NS_RDFS "range");
3013 rsz_minimumSize = lilv_new_uri(world, LV2_RESIZE_PORT__minimumSize);
3014 time_Position = lilv_new_uri(world, LV2_TIME__Position);
3015 ui_GtkUI = lilv_new_uri(world, LV2_UI__GtkUI);
3016 ui_external = lilv_new_uri(world, "http://lv2plug.in/ns/extensions/ui#external");
3017 ui_externalkx = lilv_new_uri(world, "http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget");
3018 units_unit = lilv_new_uri(world, LV2_UNITS__unit);
3019 units_render = lilv_new_uri(world, LV2_UNITS__render);
3020 units_hz = lilv_new_uri(world, LV2_UNITS__hz);
3021 units_midiNote = lilv_new_uri(world, LV2_UNITS__midiNote);
3022 units_db = lilv_new_uri(world, LV2_UNITS__db);
3023 patch_writable = lilv_new_uri(world, LV2_PATCH__writable);
3024 patch_Message = lilv_new_uri(world, LV2_PATCH__Message);
3026 lv2_noSampleAccurateCtrl = lilv_new_uri(world, "http://ardour.org/lv2/ext#noSampleAccurateControls"); // deprecated 2016-09-18
3027 auto_can_write_automatation = lilv_new_uri(world, LV2_AUTOMATE_URI__can_write);
3028 auto_automation_control = lilv_new_uri(world, LV2_AUTOMATE_URI__control);
3029 auto_automation_controlled = lilv_new_uri(world, LV2_AUTOMATE_URI__controlled);
3030 auto_automation_controller = lilv_new_uri(world, LV2_AUTOMATE_URI__controller);
3032 #ifdef HAVE_LV2_1_2_0
3033 bufz_powerOf2BlockLength = lilv_new_uri(world, LV2_BUF_SIZE__powerOf2BlockLength);
3034 bufz_fixedBlockLength = lilv_new_uri(world, LV2_BUF_SIZE__fixedBlockLength);
3035 bufz_nominalBlockLength = lilv_new_uri(world, "http://lv2plug.in/ns/ext/buf-size#nominalBlockLength");
3036 bufz_coarseBlockLength = lilv_new_uri(world, "http://lv2plug.in/ns/ext/buf-size#coarseBlockLength");
3041 LV2World::~LV2World()
3046 #ifdef HAVE_LV2_1_2_0
3047 lilv_node_free(bufz_coarseBlockLength);
3048 lilv_node_free(bufz_nominalBlockLength);
3049 lilv_node_free(bufz_fixedBlockLength);
3050 lilv_node_free(bufz_powerOf2BlockLength);
3053 lilv_node_free(lv2_noSampleAccurateCtrl);
3054 lilv_node_free(auto_can_write_automatation);
3055 lilv_node_free(auto_automation_control);
3056 lilv_node_free(auto_automation_controlled);
3057 lilv_node_free(auto_automation_controller);
3059 lilv_node_free(patch_Message);
3060 lilv_node_free(patch_writable);
3061 lilv_node_free(units_hz);
3062 lilv_node_free(units_midiNote);
3063 lilv_node_free(units_db);
3064 lilv_node_free(units_unit);
3065 lilv_node_free(units_render);
3066 lilv_node_free(ui_externalkx);
3067 lilv_node_free(ui_external);
3068 lilv_node_free(ui_GtkUI);
3069 lilv_node_free(time_Position);
3070 lilv_node_free(rsz_minimumSize);
3071 lilv_node_free(rdfs_comment);
3072 lilv_node_free(rdfs_label);
3073 lilv_node_free(rdfs_range);
3074 lilv_node_free(midi_MidiEvent);
3075 lilv_node_free(lv2_enumeration);
3076 lilv_node_free(lv2_freewheeling);
3077 lilv_node_free(lv2_toggled);
3078 lilv_node_free(lv2_sampleRate);
3079 lilv_node_free(lv2_reportsLatency);
3080 lilv_node_free(lv2_integer);
3081 lilv_node_free(lv2_isSideChain);
3082 lilv_node_free(lv2_inPlaceBroken);
3083 lilv_node_free(lv2_OutputPort);
3084 lilv_node_free(lv2_InputPort);
3085 lilv_node_free(lv2_ControlPort);
3086 lilv_node_free(lv2_AudioPort);
3087 lilv_node_free(ext_rangeSteps);
3088 lilv_node_free(ext_notAutomatic);
3089 lilv_node_free(ext_causesArtifacts);
3090 lilv_node_free(ext_expensive);
3091 lilv_node_free(ext_notOnGUI);
3092 lilv_node_free(ext_logarithmic);
3093 lilv_node_free(ev_EventPort);
3094 lilv_node_free(atom_supports);
3095 lilv_node_free(atom_eventTransfer);
3096 lilv_node_free(atom_bufferType);
3097 lilv_node_free(atom_Sequence);
3098 lilv_node_free(atom_Chunk);
3099 lilv_node_free(atom_AtomPort);
3100 lilv_world_free(world);
3105 LV2World::load_bundled_plugins(bool verbose)
3107 if (!_bundle_checked) {
3109 cout << "Scanning folders for bundled LV2s: " << ARDOUR::lv2_bundled_search_path().to_string() << endl;
3112 vector<string> plugin_objects;
3113 find_paths_matching_filter (plugin_objects, ARDOUR::lv2_bundled_search_path(), lv2_filter, 0, true, true, true);
3114 for ( vector<string>::iterator x = plugin_objects.begin(); x != plugin_objects.end (); ++x) {
3115 #ifdef PLATFORM_WINDOWS
3116 string uri = "file:///" + *x + "/";
3118 string uri = "file://" + *x + "/";
3120 LilvNode *node = lilv_new_uri(world, uri.c_str());
3121 lilv_world_load_bundle(world, node);
3122 lilv_node_free(node);
3125 lilv_world_load_all(world);
3126 _bundle_checked = true;
3130 LV2PluginInfo::LV2PluginInfo (const char* plugin_uri)
3133 _plugin_uri = strdup(plugin_uri);
3136 LV2PluginInfo::~LV2PluginInfo()
3143 LV2PluginInfo::load(Session& session)
3147 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
3148 LilvNode* uri = lilv_new_uri(_world.world, _plugin_uri);
3149 if (!uri) { throw failed_constructor(); }
3150 const LilvPlugin* lp = lilv_plugins_get_by_uri(plugins, uri);
3151 if (!lp) { throw failed_constructor(); }
3152 plugin.reset(new LV2Plugin(session.engine(), session, lp, session.frame_rate()));
3153 lilv_node_free(uri);
3154 plugin->set_info(PluginInfoPtr(shared_from_this ()));
3156 } catch (failed_constructor& err) {
3157 return PluginPtr((Plugin*)0);
3163 std::vector<Plugin::PresetRecord>
3164 LV2PluginInfo::get_presets (bool /*user_only*/) const
3166 std::vector<Plugin::PresetRecord> p;
3167 #ifndef NO_PLUGIN_STATE
3168 const LilvPlugin* lp = NULL;
3171 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
3172 LilvNode* uri = lilv_new_uri(_world.world, _plugin_uri);
3173 if (!uri) { throw failed_constructor(); }
3174 lp = lilv_plugins_get_by_uri(plugins, uri);
3175 if (!lp) { throw failed_constructor(); }
3176 lilv_node_free(uri);
3177 } catch (failed_constructor& err) {
3181 // see LV2Plugin::find_presets
3182 LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
3183 LilvNode* pset_Preset = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
3184 LilvNode* rdfs_label = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
3186 LilvNodes* presets = lilv_plugin_get_related(lp, pset_Preset);
3187 LILV_FOREACH(nodes, i, presets) {
3188 const LilvNode* preset = lilv_nodes_get(presets, i);
3189 lilv_world_load_resource(_world.world, preset);
3190 LilvNode* name = get_value(_world.world, preset, rdfs_label);
3191 bool userpreset = true; // TODO
3193 p.push_back (Plugin::PresetRecord (lilv_node_as_string(preset), lilv_node_as_string(name), userpreset));
3194 lilv_node_free(name);
3197 lilv_nodes_free(presets);
3198 lilv_node_free(rdfs_label);
3199 lilv_node_free(pset_Preset);
3200 lilv_node_free(lv2_appliesTo);
3206 LV2PluginInfo::in_category (const std::string &c) const
3208 // TODO use untranslated lilv_plugin_get_class()
3209 // match gtk2_ardour/plugin_selector.cc
3210 if (category == c) {
3217 LV2PluginInfo::is_instrument () const
3219 if (category == "Instrument") {
3223 /* until we make sure that category remains untranslated in the lv2.ttl spec
3224 * and until most instruments also classify themselves as such, there's a 2nd check:
3226 if (n_inputs.n_midi() > 0 && n_inputs.n_audio() == 0 && n_outputs.n_audio() > 0) {
3234 LV2PluginInfo::discover()
3237 world.load_bundled_plugins();
3238 _world.load_bundled_plugins(true);
3240 PluginInfoList* plugs = new PluginInfoList;
3241 const LilvPlugins* plugins = lilv_world_get_all_plugins(world.world);
3243 LILV_FOREACH(plugins, i, plugins) {
3244 const LilvPlugin* p = lilv_plugins_get(plugins, i);
3245 const LilvNode* pun = lilv_plugin_get_uri(p);
3247 LV2PluginInfoPtr info(new LV2PluginInfo(lilv_node_as_string(pun)));
3249 LilvNode* name = lilv_plugin_get_name(p);
3250 if (!name || !lilv_plugin_get_port_by_index(p, 0)) {
3251 warning << "Ignoring invalid LV2 plugin "
3252 << lilv_node_as_string(lilv_plugin_get_uri(p))
3257 if (lilv_plugin_has_feature(p, world.lv2_inPlaceBroken)) {
3258 warning << string_compose(
3259 _("Ignoring LV2 plugin \"%1\" since it cannot do inplace processing."),
3260 lilv_node_as_string(name)) << endmsg;
3261 lilv_node_free(name);
3265 #ifdef HAVE_LV2_1_2_0
3266 LilvNodes *required_features = lilv_plugin_get_required_features (p);
3267 if (lilv_nodes_contains (required_features, world.bufz_powerOf2BlockLength) ||
3268 lilv_nodes_contains (required_features, world.bufz_fixedBlockLength)
3270 warning << string_compose(
3271 _("Ignoring LV2 plugin \"%1\" because its buffer-size requirements cannot be satisfied."),
3272 lilv_node_as_string(name)) << endmsg;
3273 lilv_nodes_free(required_features);
3274 lilv_node_free(name);
3277 lilv_nodes_free(required_features);
3282 info->name = string(lilv_node_as_string(name));
3283 lilv_node_free(name);
3284 ARDOUR::PluginScanMessage(_("LV2"), info->name, false);
3286 const LilvPluginClass* pclass = lilv_plugin_get_class(p);
3287 const LilvNode* label = lilv_plugin_class_get_label(pclass);
3288 info->category = lilv_node_as_string(label);
3290 LilvNode* author_name = lilv_plugin_get_author_name(p);
3291 info->creator = author_name ? string(lilv_node_as_string(author_name)) : "Unknown";
3292 lilv_node_free(author_name);
3294 info->path = "/NOPATH"; // Meaningless for LV2
3296 /* count atom-event-ports that feature
3297 * atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent>
3299 * TODO: nicely ask drobilla to make a lilv_ call for that
3301 int count_midi_out = 0;
3302 int count_midi_in = 0;
3303 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
3304 const LilvPort* port = lilv_plugin_get_port_by_index(p, i);
3305 if (lilv_port_is_a(p, port, world.atom_AtomPort)) {
3306 LilvNodes* buffer_types = lilv_port_get_value(
3307 p, port, world.atom_bufferType);
3308 LilvNodes* atom_supports = lilv_port_get_value(
3309 p, port, world.atom_supports);
3311 if (lilv_nodes_contains(buffer_types, world.atom_Sequence)
3312 && lilv_nodes_contains(atom_supports, world.midi_MidiEvent)) {
3313 if (lilv_port_is_a(p, port, world.lv2_InputPort)) {
3316 if (lilv_port_is_a(p, port, world.lv2_OutputPort)) {
3320 lilv_nodes_free(buffer_types);
3321 lilv_nodes_free(atom_supports);
3325 info->n_inputs.set_audio(
3326 lilv_plugin_get_num_ports_of_class(
3327 p, world.lv2_InputPort, world.lv2_AudioPort, NULL));
3328 info->n_inputs.set_midi(
3329 lilv_plugin_get_num_ports_of_class(
3330 p, world.lv2_InputPort, world.ev_EventPort, NULL)
3333 info->n_outputs.set_audio(
3334 lilv_plugin_get_num_ports_of_class(
3335 p, world.lv2_OutputPort, world.lv2_AudioPort, NULL));
3336 info->n_outputs.set_midi(
3337 lilv_plugin_get_num_ports_of_class(
3338 p, world.lv2_OutputPort, world.ev_EventPort, NULL)
3341 info->unique_id = lilv_node_as_uri(lilv_plugin_get_uri(p));
3342 info->index = 0; // Meaningless for LV2
3344 plugs->push_back(info);