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/locale_guard.h"
40 #include "pbd/replace_all.h"
41 #include "pbd/xml++.h"
43 #include "libardour-config.h"
45 #include "ardour/audio_buffer.h"
46 #include "ardour/audioengine.h"
47 #include "ardour/debug.h"
48 #include "ardour/lv2_plugin.h"
49 #include "ardour/midi_patch_manager.h"
50 #include "ardour/session.h"
51 #include "ardour/tempo.h"
52 #include "ardour/types.h"
53 #include "ardour/utils.h"
54 #include "ardour/worker.h"
55 #include "ardour/search_paths.h"
60 #include <lilv/lilv.h>
62 #include "lv2/lv2plug.in/ns/ext/atom/atom.h"
63 #include "lv2/lv2plug.in/ns/ext/atom/forge.h"
64 #include "lv2/lv2plug.in/ns/ext/log/log.h"
65 #include "lv2/lv2plug.in/ns/ext/midi/midi.h"
66 #include "lv2/lv2plug.in/ns/ext/port-props/port-props.h"
67 #include "lv2/lv2plug.in/ns/ext/presets/presets.h"
68 #include "lv2/lv2plug.in/ns/ext/state/state.h"
69 #include "lv2/lv2plug.in/ns/ext/time/time.h"
70 #include "lv2/lv2plug.in/ns/ext/worker/worker.h"
71 #include "lv2/lv2plug.in/ns/ext/resize-port/resize-port.h"
72 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
73 #include "lv2/lv2plug.in/ns/extensions/units/units.h"
74 #include "lv2/lv2plug.in/ns/ext/patch/patch.h"
75 #include "lv2/lv2plug.in/ns/ext/port-groups/port-groups.h"
77 #include "lv2/lv2plug.in/ns/ext/buf-size/buf-size.h"
78 #include "lv2/lv2plug.in/ns/ext/options/options.h"
81 #include "lv2_evbuf.h"
84 #include <suil/suil.h>
87 // Compatibility for old LV2
88 #ifndef LV2_ATOM_CONTENTS_CONST
89 #define LV2_ATOM_CONTENTS_CONST(type, atom) \
90 ((const void*)((const uint8_t*)(atom) + sizeof(type)))
92 #ifndef LV2_ATOM_BODY_CONST
93 #define LV2_ATOM_BODY_CONST(atom) LV2_ATOM_CONTENTS_CONST(LV2_Atom, atom)
95 #ifndef LV2_PATCH__property
96 #define LV2_PATCH__property LV2_PATCH_PREFIX "property"
98 #ifndef LV2_PATCH__value
99 #define LV2_PATCH__value LV2_PATCH_PREFIX "value"
101 #ifndef LV2_PATCH__writable
102 #define LV2_PATCH__writable LV2_PATCH_PREFIX "writable"
105 /** The number of MIDI buffers that will fit in a UI/worker comm buffer.
106 This needs to be roughly the number of cycles the UI will get around to
107 actually processing the traffic. Lower values are flakier but save memory.
109 static const size_t NBUFS = 4;
112 using namespace ARDOUR;
115 bool LV2Plugin::force_state_save = false;
117 class LV2World : boost::noncopyable {
122 void load_bundled_plugins(bool verbose=false);
126 LilvNode* atom_AtomPort;
127 LilvNode* atom_Chunk;
128 LilvNode* atom_Sequence;
129 LilvNode* atom_bufferType;
130 LilvNode* atom_eventTransfer;
131 LilvNode* atom_supports;
132 LilvNode* ev_EventPort;
133 LilvNode* ext_logarithmic;
134 LilvNode* ext_notOnGUI;
135 LilvNode* ext_expensive;
136 LilvNode* ext_causesArtifacts;
137 LilvNode* ext_notAutomatic;
138 LilvNode* ext_rangeSteps;
139 LilvNode* groups_group;
140 LilvNode* groups_element;
141 LilvNode* lv2_AudioPort;
142 LilvNode* lv2_ControlPort;
143 LilvNode* lv2_InputPort;
144 LilvNode* lv2_OutputPort;
145 LilvNode* lv2_designation;
146 LilvNode* lv2_enumeration;
147 LilvNode* lv2_freewheeling;
148 LilvNode* lv2_inPlaceBroken;
149 LilvNode* lv2_isSideChain;
151 LilvNode* lv2_integer;
152 LilvNode* lv2_default;
153 LilvNode* lv2_minimum;
154 LilvNode* lv2_maximum;
155 LilvNode* lv2_reportsLatency;
156 LilvNode* lv2_sampleRate;
157 LilvNode* lv2_toggled;
158 LilvNode* midi_MidiEvent;
159 LilvNode* rdfs_comment;
160 LilvNode* rdfs_label;
161 LilvNode* rdfs_range;
162 LilvNode* rsz_minimumSize;
163 LilvNode* time_Position;
165 LilvNode* ui_external;
166 LilvNode* ui_externalkx;
169 LilvNode* units_unit;
170 LilvNode* units_render;
171 LilvNode* units_midiNote;
172 LilvNode* patch_writable;
173 LilvNode* patch_Message;
174 #ifdef HAVE_LV2_1_2_0
175 LilvNode* bufz_powerOf2BlockLength;
176 LilvNode* bufz_fixedBlockLength;
177 LilvNode* bufz_nominalBlockLength;
178 LilvNode* bufz_coarseBlockLength;
181 #ifdef HAVE_LV2_1_10_0
183 LilvNode* atom_float;
184 LilvNode* atom_object; // new in 1.8
185 LilvNode* atom_vector;
188 LilvNode* lv2_noSampleAccurateCtrl;
189 LilvNode* auto_can_write_automatation; // lv2:optionalFeature
190 LilvNode* auto_automation_control; // atom:supports
191 LilvNode* auto_automation_controlled; // lv2:portProperty
192 LilvNode* auto_automation_controller; // lv2:portProperty
193 LilvNode* inline_display_in_gui; // lv2:optionalFeature
197 bool _bundle_checked;
200 static LV2World _world;
202 /* worker extension */
204 /** Called by the plugin to schedule non-RT work. */
205 static LV2_Worker_Status
206 work_schedule(LV2_Worker_Schedule_Handle handle,
210 return (((Worker*)handle)->schedule(size, data)
212 : LV2_WORKER_ERR_UNKNOWN);
215 /** Called by the plugin to respond to non-RT work. */
216 static LV2_Worker_Status
217 work_respond(LV2_Worker_Respond_Handle handle,
221 return (((Worker*)handle)->respond(size, data)
223 : LV2_WORKER_ERR_UNKNOWN);
227 /* inline display extension */
229 queue_draw (LV2_Inline_Display_Handle handle)
231 LV2Plugin* plugin = (LV2Plugin*)handle;
232 plugin->QueueDraw(); /* EMIT SIGNAL */
236 midnam_update (LV2_Midnam_Handle handle)
238 LV2Plugin* plugin = (LV2Plugin*)handle;
239 plugin->UpdateMidnam (); /* EMIT SIGNAL */
246 log_vprintf(LV2_Log_Handle /*handle*/,
252 const int ret = g_vasprintf(&str, fmt, args);
253 /* strip trailing whitespace */
254 while (strlen (str) > 0 && isspace (str[strlen (str) - 1])) {
255 str[strlen (str) - 1] = '\0';
257 if (strlen (str) == 0) {
261 if (type == URIMap::instance().urids.log_Error) {
262 error << str << endmsg;
263 } else if (type == URIMap::instance().urids.log_Warning) {
264 warning << str << endmsg;
265 } else if (type == URIMap::instance().urids.log_Note) {
266 info << str << endmsg;
267 } else if (type == URIMap::instance().urids.log_Trace) {
268 DEBUG_TRACE(DEBUG::LV2, str);
274 log_printf(LV2_Log_Handle handle,
276 const char* fmt, ...)
280 const int ret = log_vprintf(handle, type, fmt, args);
285 struct LV2Plugin::Impl {
286 Impl() : plugin(0), ui(0), ui_type(0), name(0), author(0), instance(0)
288 #ifdef HAVE_LV2_1_2_0
293 #ifdef HAVE_LV2_1_2_0
302 /** Find the LV2 input port with the given designation.
303 * If found, bufptrs[port_index] will be set to bufptr.
305 const LilvPort* designated_input (const char* uri, void** bufptrs[], void** bufptr);
307 const LilvPlugin* plugin;
309 const LilvNode* ui_type;
312 LilvInstance* instance;
313 const LV2_Worker_Interface* work_iface;
314 #ifdef HAVE_LV2_1_2_0
315 const LV2_Options_Interface* opts_iface;
318 LV2_Atom_Forge forge;
319 LV2_Atom_Forge ui_forge;
320 int32_t block_length;
321 #ifdef HAVE_LV2_1_2_0
322 LV2_Options_Option* options;
325 LV2_Inline_Display* queue_draw;
330 LV2Plugin::LV2Plugin (AudioEngine& engine,
332 const void* c_plugin,
334 : Plugin (engine, session)
339 , _state_worker(NULL)
341 , _patch_port_in_index((uint32_t)-1)
342 , _patch_port_out_index((uint32_t)-1)
343 , _uri_map(URIMap::instance())
344 , _no_sample_accurate_ctrl (false)
346 init(c_plugin, rate);
349 LV2Plugin::LV2Plugin (const LV2Plugin& other)
355 , _state_worker(NULL)
356 , _insert_id(other._insert_id)
357 , _patch_port_in_index((uint32_t)-1)
358 , _patch_port_out_index((uint32_t)-1)
359 , _uri_map(URIMap::instance())
360 , _no_sample_accurate_ctrl (false)
362 init(other._impl->plugin, other._sample_rate);
364 for (uint32_t i = 0; i < parameter_count(); ++i) {
365 _control_data[i] = other._shadow_data[i];
366 _shadow_data[i] = other._shadow_data[i];
371 LV2Plugin::init(const void* c_plugin, framecnt_t rate)
373 DEBUG_TRACE(DEBUG::LV2, "init\n");
375 _impl->plugin = (const LilvPlugin*)c_plugin;
377 _impl->ui_type = NULL;
382 _atom_ev_buffers = 0;
384 _bpm_control_port = 0;
385 _freewheel_control_port = 0;
386 _latency_control_port = 0;
387 _next_cycle_start = std::numeric_limits<framepos_t>::max();
388 _next_cycle_speed = 1.0;
389 _seq_size = _engine.raw_buffer_size(DataType::MIDI);
391 _was_activated = false;
392 _has_state_interface = false;
393 _can_write_automation = false;
394 _inline_display_in_gui = false;
396 _current_latency = 0;
397 _impl->block_length = _session.get_block_size();
399 _instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
400 _data_access_feature.URI = "http://lv2plug.in/ns/ext/data-access";
401 _make_path_feature.URI = LV2_STATE__makePath;
402 _log_feature.URI = LV2_LOG__log;
403 _work_schedule_feature.URI = LV2_WORKER__schedule;
404 _work_schedule_feature.data = NULL;
405 _def_state_feature.URI = LV2_STATE_PREFIX "loadDefaultState"; // Post LV2-1.2.0
406 _def_state_feature.data = NULL;
408 const LilvPlugin* plugin = _impl->plugin;
410 LilvNode* state_iface_uri = lilv_new_uri(_world.world, LV2_STATE__interface);
411 LilvNode* state_uri = lilv_new_uri(_world.world, LV2_STATE_URI);
412 _has_state_interface =
413 // What plugins should have (lv2:extensionData state:Interface)
414 lilv_plugin_has_extension_data(plugin, state_iface_uri)
415 // What some outdated/incorrect ones have
416 || lilv_plugin_has_feature(plugin, state_uri);
417 lilv_node_free(state_uri);
418 lilv_node_free(state_iface_uri);
420 _features = (LV2_Feature**)calloc(13, sizeof(LV2_Feature*));
421 _features[0] = &_instance_access_feature;
422 _features[1] = &_data_access_feature;
423 _features[2] = &_make_path_feature;
424 _features[3] = _uri_map.uri_map_feature();
425 _features[4] = _uri_map.urid_map_feature();
426 _features[5] = _uri_map.urid_unmap_feature();
427 _features[6] = &_log_feature;
429 unsigned n_features = 7;
430 #ifdef HAVE_LV2_1_2_0
431 _features[n_features++] = &_def_state_feature;
434 lv2_atom_forge_init(&_impl->forge, _uri_map.urid_map());
435 lv2_atom_forge_init(&_impl->ui_forge, _uri_map.urid_map());
438 _impl->queue_draw = (LV2_Inline_Display*)
439 malloc (sizeof(LV2_Inline_Display));
440 _impl->queue_draw->handle = this;
441 _impl->queue_draw->queue_draw = queue_draw;
443 _queue_draw_feature.URI = LV2_INLINEDISPLAY__queue_draw;
444 _queue_draw_feature.data = _impl->queue_draw;
445 _features[n_features++] = &_queue_draw_feature;
447 _impl->midnam = (LV2_Midnam*)
448 malloc (sizeof(LV2_Midnam));
449 _impl->midnam->handle = this;
450 _impl->midnam->update = midnam_update;
452 _midnam_feature.URI = LV2_MIDNAM__update;
453 _midnam_feature.data = _impl->midnam;
454 _features[n_features++] = &_midnam_feature;
457 #ifdef HAVE_LV2_1_2_0
458 LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
459 static const int32_t _min_block_length = 1; // may happen during split-cycles
460 static const int32_t _max_block_length = 8192; // max possible (with all engines and during export)
461 /* Consider updating max-block-size whenever the buffersize changes.
462 * It requires re-instantiating the plugin (which is a non-realtime operation),
463 * so it should be done lightly and only for plugins that require it.
465 * given that the block-size can change at any time (split-cycles) ardour currently
466 * does not support plugins that require bufz_fixedBlockLength.
468 LV2_Options_Option options[] = {
469 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__minBlockLength),
470 sizeof(int32_t), atom_Int, &_min_block_length },
471 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__maxBlockLength),
472 sizeof(int32_t), atom_Int, &_max_block_length },
473 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__sequenceSize),
474 sizeof(int32_t), atom_Int, &_seq_size },
475 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://lv2plug.in/ns/ext/buf-size#nominalBlockLength"),
476 sizeof(int32_t), atom_Int, &_impl->block_length },
477 { LV2_OPTIONS_INSTANCE, 0, 0, 0, 0, NULL }
480 _impl->options = (LV2_Options_Option*) malloc (sizeof (options));
481 memcpy ((void*) _impl->options, (void*) options, sizeof (options));
483 _options_feature.URI = LV2_OPTIONS__options;
484 _options_feature.data = _impl->options;
485 _features[n_features++] = &_options_feature;
488 LV2_State_Make_Path* make_path = (LV2_State_Make_Path*)malloc(
489 sizeof(LV2_State_Make_Path));
490 make_path->handle = this;
491 make_path->path = &lv2_state_make_path;
492 _make_path_feature.data = make_path;
494 LV2_Log_Log* log = (LV2_Log_Log*)malloc(sizeof(LV2_Log_Log));
496 log->printf = &log_printf;
497 log->vprintf = &log_vprintf;
498 _log_feature.data = log;
500 const size_t ring_size = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
501 LilvNode* worker_schedule = lilv_new_uri(_world.world, LV2_WORKER__schedule);
502 if (lilv_plugin_has_feature(plugin, worker_schedule)) {
503 LV2_Worker_Schedule* schedule = (LV2_Worker_Schedule*)malloc(
504 sizeof(LV2_Worker_Schedule));
505 _worker = new Worker(this, ring_size);
506 schedule->handle = _worker;
507 schedule->schedule_work = work_schedule;
508 _work_schedule_feature.data = schedule;
509 _features[n_features++] = &_work_schedule_feature;
511 lilv_node_free(worker_schedule);
513 if (_has_state_interface) {
514 // Create a non-threaded worker for use by state restore
515 _state_worker = new Worker(this, ring_size, false);
518 _impl->instance = lilv_plugin_instantiate(plugin, rate, _features);
519 _impl->name = lilv_plugin_get_name(plugin);
520 _impl->author = lilv_plugin_get_author_name(plugin);
522 if (_impl->instance == 0) {
523 error << _("LV2: Failed to instantiate plugin ") << uri() << endmsg;
524 throw failed_constructor();
527 _instance_access_feature.data = (void*)_impl->instance->lv2_handle;
528 _data_access_extension_data.extension_data = _impl->instance->lv2_descriptor->extension_data;
529 _data_access_feature.data = &_data_access_extension_data;
531 LilvNode* worker_iface_uri = lilv_new_uri(_world.world, LV2_WORKER__interface);
532 if (lilv_plugin_has_extension_data(plugin, worker_iface_uri)) {
533 _impl->work_iface = (const LV2_Worker_Interface*)extension_data(
534 LV2_WORKER__interface);
536 lilv_node_free(worker_iface_uri);
539 #ifdef HAVE_LV2_1_2_0
540 LilvNode* options_iface_uri = lilv_new_uri(_world.world, LV2_OPTIONS__interface);
541 if (lilv_plugin_has_extension_data(plugin, options_iface_uri)) {
542 _impl->opts_iface = (const LV2_Options_Interface*)extension_data(
543 LV2_OPTIONS__interface);
545 lilv_node_free(options_iface_uri);
549 _display_interface = (const LV2_Inline_Display_Interface*)
550 extension_data (LV2_INLINEDISPLAY__interface);
552 _midname_interface = (const LV2_Midnam_Interface*)
553 extension_data (LV2_MIDNAM__interface);
554 if (_midname_interface) {
559 if (lilv_plugin_has_feature(plugin, _world.lv2_inPlaceBroken)) {
560 error << string_compose(
561 _("LV2: \"%1\" cannot be used, since it cannot do inplace processing."),
562 lilv_node_as_string(_impl->name)) << endmsg;
563 lilv_node_free(_impl->name);
564 lilv_node_free(_impl->author);
565 throw failed_constructor();
568 #ifdef HAVE_LV2_1_2_0
569 LilvNodes *required_features = lilv_plugin_get_required_features (plugin);
570 if (lilv_nodes_contains (required_features, _world.bufz_powerOf2BlockLength) ||
571 lilv_nodes_contains (required_features, _world.bufz_fixedBlockLength)
573 error << string_compose(
574 _("LV2: \"%1\" buffer-size requirements cannot be satisfied."),
575 lilv_node_as_string(_impl->name)) << endmsg;
576 lilv_node_free(_impl->name);
577 lilv_node_free(_impl->author);
578 lilv_nodes_free(required_features);
579 throw failed_constructor();
581 lilv_nodes_free(required_features);
584 LilvNodes* optional_features = lilv_plugin_get_optional_features (plugin);
585 #ifdef HAVE_LV2_1_2_0
586 if (lilv_nodes_contains (optional_features, _world.bufz_coarseBlockLength)) {
587 _no_sample_accurate_ctrl = true;
591 if (lilv_nodes_contains (optional_features, _world.lv2_noSampleAccurateCtrl)) {
592 /* deprecated 2016-Sep-18 in favor of bufz_coarseBlockLength */
593 _no_sample_accurate_ctrl = true;
595 if (lilv_nodes_contains (optional_features, _world.auto_can_write_automatation)) {
596 _can_write_automation = true;
598 if (lilv_nodes_contains (optional_features, _world.inline_display_in_gui)) {
599 _inline_display_in_gui = true;
601 lilv_nodes_free(optional_features);
604 #ifdef HAVE_LILV_0_16_0
605 // Load default state
607 /* immediately schedule any work,
608 * so that state restore later will not find a busy
609 * worker. latency_compute_run() flushes any replies
611 _worker->set_synchronous(true);
613 LilvState* state = lilv_state_new_from_world(
614 _world.world, _uri_map.urid_map(), lilv_plugin_get_uri(_impl->plugin));
615 if (state && _has_state_interface) {
616 lilv_state_restore(state, _impl->instance, NULL, NULL, 0, NULL);
618 lilv_state_free(state);
623 const uint32_t num_ports = this->num_ports();
624 for (uint32_t i = 0; i < num_ports; ++i) {
625 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, i);
627 size_t minimumSize = 0;
629 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_OutputPort)) {
630 flags |= PORT_OUTPUT;
631 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_InputPort)) {
634 error << string_compose(
635 "LV2: \"%1\" port %2 is neither input nor output",
636 lilv_node_as_string(_impl->name), i) << endmsg;
637 throw failed_constructor();
640 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_ControlPort)) {
641 flags |= PORT_CONTROL;
642 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_AudioPort)) {
644 } else if (lilv_port_is_a(_impl->plugin, port, _world.ev_EventPort)) {
646 flags |= PORT_MIDI; // We assume old event API ports are for MIDI
647 } else if (lilv_port_is_a(_impl->plugin, port, _world.atom_AtomPort)) {
648 LilvNodes* buffer_types = lilv_port_get_value(
649 _impl->plugin, port, _world.atom_bufferType);
650 LilvNodes* atom_supports = lilv_port_get_value(
651 _impl->plugin, port, _world.atom_supports);
653 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
654 flags |= PORT_SEQUENCE;
655 if (lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)) {
658 if (lilv_nodes_contains(atom_supports, _world.time_Position)) {
659 flags |= PORT_POSITION;
662 if (lilv_nodes_contains(atom_supports, _world.auto_automation_control)) {
663 flags |= PORT_AUTOCTRL;
666 if (lilv_nodes_contains(atom_supports, _world.patch_Message)) {
667 flags |= PORT_PATCHMSG;
668 if (flags & PORT_INPUT) {
669 _patch_port_in_index = i;
671 _patch_port_out_index = i;
675 LilvNodes* min_size_v = lilv_port_get_value(_impl->plugin, port, _world.rsz_minimumSize);
676 LilvNode* min_size = min_size_v ? lilv_nodes_get_first(min_size_v) : NULL;
677 if (min_size && lilv_node_is_int(min_size)) {
678 minimumSize = lilv_node_as_int(min_size);
680 lilv_nodes_free(min_size_v);
681 lilv_nodes_free(buffer_types);
682 lilv_nodes_free(atom_supports);
684 error << string_compose(
685 "LV2: \"%1\" port %2 has no known data type",
686 lilv_node_as_string(_impl->name), i) << endmsg;
687 throw failed_constructor();
690 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
691 if (lilv_port_has_property(_impl->plugin, port, _world.ext_causesArtifacts)) {
692 flags |= PORT_NOAUTO;
694 if (lilv_port_has_property(_impl->plugin, port, _world.ext_notAutomatic)) {
695 flags |= PORT_NOAUTO;
697 if (lilv_port_has_property(_impl->plugin, port, _world.ext_expensive)) {
698 flags |= PORT_NOAUTO;
702 if (lilv_port_has_property(_impl->plugin, port, _world.auto_automation_controlled)) {
703 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
704 flags |= PORT_CTRLED;
707 if (lilv_port_has_property(_impl->plugin, port, _world.auto_automation_controller)) {
708 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
709 flags |= PORT_CTRLER;
714 _port_flags.push_back(flags);
715 _port_minimumSize.push_back(minimumSize);
716 DEBUG_TRACE(DEBUG::LV2, string_compose("port %1 buffer %2 bytes\n", i, minimumSize));
719 _control_data = new float[num_ports];
720 _shadow_data = new float[num_ports];
721 _defaults = new float[num_ports];
722 _ev_buffers = new LV2_Evbuf*[num_ports];
723 memset(_ev_buffers, 0, sizeof(LV2_Evbuf*) * num_ports);
725 const bool latent = lilv_plugin_has_latency(plugin);
726 const uint32_t latency_index = (latent)
727 ? lilv_plugin_get_latency_port_index(plugin)
730 // Build an array of pointers to special parameter buffers
731 void*** params = new void**[num_ports];
732 for (uint32_t i = 0; i < num_ports; ++i) {
735 _impl->designated_input (LV2_TIME__beatsPerMinute, params, (void**)&_bpm_control_port);
736 _impl->designated_input (LV2_CORE__freeWheeling, params, (void**)&_freewheel_control_port);
738 for (uint32_t i = 0; i < num_ports; ++i) {
739 const LilvPort* port = lilv_plugin_get_port_by_index(plugin, i);
740 const LilvNode* sym = lilv_port_get_symbol(plugin, port);
742 // Store index in map so we can look up index by symbol
743 _port_indices.insert(std::make_pair(lilv_node_as_string(sym), i));
745 // Get range and default value if applicable
746 if (parameter_is_control(i)) {
748 lilv_port_get_range(plugin, port, &def, NULL, NULL);
749 _defaults[i] = def ? lilv_node_as_float(def) : 0.0f;
750 if (lilv_port_has_property (plugin, port, _world.lv2_sampleRate)) {
751 _defaults[i] *= _session.frame_rate ();
755 lilv_instance_connect_port(_impl->instance, i, &_control_data[i]);
757 if (latent && i == latency_index) {
759 lilv_port_get_range(_impl->plugin, port, NULL, NULL, &max);
760 _max_latency = max ? lilv_node_as_float(max) : .02 * _sample_rate;
761 _latency_control_port = &_control_data[i];
762 *_latency_control_port = 0;
765 if (parameter_is_input(i)) {
766 _shadow_data[i] = default_value(i);
768 *params[i] = (void*)&_shadow_data[i];
778 LilvUIs* uis = lilv_plugin_get_uis(plugin);
779 if (lilv_uis_size(uis) > 0) {
781 // Look for embeddable UI
782 LILV_FOREACH(uis, u, uis) {
783 const LilvUI* this_ui = lilv_uis_get(uis, u);
784 const LilvNode* this_ui_type = NULL;
785 if (lilv_ui_is_supported(this_ui,
789 // TODO: Multiple UI support
791 _impl->ui_type = this_ui_type;
796 // Look for Gtk native UI
797 LILV_FOREACH(uis, i, uis) {
798 const LilvUI* ui = lilv_uis_get(uis, i);
799 if (lilv_ui_is_a(ui, _world.ui_GtkUI)) {
801 _impl->ui_type = _world.ui_GtkUI;
807 // If Gtk UI is not available, try to find external UI
809 LILV_FOREACH(uis, i, uis) {
810 const LilvUI* ui = lilv_uis_get(uis, i);
811 if (lilv_ui_is_a(ui, _world.ui_externalkx)) {
813 _impl->ui_type = _world.ui_external;
816 if (lilv_ui_is_a(ui, _world.ui_external)) {
818 _impl->ui_type = _world.ui_external;
824 load_supported_properties(_property_descriptors);
825 allocate_atom_event_buffers();
826 latency_compute_run();
830 LV2Plugin::set_block_size (pframes_t nframes)
832 #ifdef HAVE_LV2_1_2_0
833 if (_impl->opts_iface) {
834 LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
835 _impl->block_length = nframes;
836 LV2_Options_Option block_size_option = {
837 LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id ("http://lv2plug.in/ns/ext/buf-size#nominalBlockLength"),
838 sizeof(int32_t), atom_Int, (void*)&_impl->block_length
840 _impl->opts_iface->set (_impl->instance->lv2_handle, &block_size_option);
847 LV2Plugin::requires_fixed_sized_buffers () const
849 /* This controls if Ardour will split the plugin's run()
850 * on automation events in order to pass sample-accurate automation
851 * via standard control-ports.
853 * When returning true Ardour will *not* sub-divide the process-cycle.
854 * Automation events that happen between cycle-start and cycle-end will be
855 * ignored (ctrl values are interpolated to cycle-start).
856 * NB. Atom Sequences are still sample accurate.
858 * Note: This does not guarantee a fixed block-size.
859 * e.g The process cycle may be split when looping, also
860 * the period-size may change any time: see set_block_size()
862 if (get_info()->n_inputs.n_midi() > 0) {
863 /* we don't yet implement midi buffer offsets (for split cycles).
864 * Also connect_and_run() also uses _session.transport_frame() directly
865 * (for BBT) which is not offset for plugin cycle split.
869 return _no_sample_accurate_ctrl;
872 LV2Plugin::~LV2Plugin ()
874 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 destroy\n", name()));
881 std::stringstream ss;
884 MIDI::Name::MidiPatchManager::instance().remove_custom_midnam (ss.str());
888 lilv_instance_free(_impl->instance);
889 lilv_state_free(_impl->state);
890 lilv_node_free(_impl->name);
891 lilv_node_free(_impl->author);
892 #ifdef HAVE_LV2_1_2_0
893 free(_impl->options);
896 free(_impl->queue_draw);
901 free(_log_feature.data);
902 free(_make_path_feature.data);
903 free(_work_schedule_feature.data);
908 delete _state_worker;
910 if (_atom_ev_buffers) {
911 LV2_Evbuf** b = _atom_ev_buffers;
916 free(_atom_ev_buffers);
919 delete [] _control_data;
920 delete [] _shadow_data;
922 delete [] _ev_buffers;
927 LV2Plugin::is_external_ui() const
932 return lilv_ui_is_a(_impl->ui, _world.ui_external) || lilv_ui_is_a(_impl->ui, _world.ui_externalkx);
936 LV2Plugin::is_external_kx() const
941 return lilv_ui_is_a(_impl->ui, _world.ui_externalkx);
945 LV2Plugin::ui_is_resizable () const
947 const LilvNode* s = lilv_ui_get_uri(_impl->ui);
948 LilvNode* p = lilv_new_uri(_world.world, LV2_CORE__optionalFeature);
949 LilvNode* fs = lilv_new_uri(_world.world, LV2_UI__fixedSize);
950 LilvNode* nrs = lilv_new_uri(_world.world, LV2_UI__noUserResize);
952 LilvNodes* fs_matches = lilv_world_find_nodes(_world.world, s, p, fs);
953 LilvNodes* nrs_matches = lilv_world_find_nodes(_world.world, s, p, nrs);
955 lilv_nodes_free(nrs_matches);
956 lilv_nodes_free(fs_matches);
961 return !fs_matches && !nrs_matches;
966 LV2Plugin::has_inline_display () {
967 return _display_interface ? true : false;
971 LV2Plugin::inline_display_in_gui () {
972 return _inline_display_in_gui;
975 Plugin::Display_Image_Surface*
976 LV2Plugin::render_inline_display (uint32_t w, uint32_t h) {
977 if (_display_interface) {
978 /* Plugin::Display_Image_Surface is identical to
979 * LV2_Inline_Display_Image_Surface */
980 return (Plugin::Display_Image_Surface*) _display_interface->render ((void*)_impl->instance->lv2_handle, w, h);
986 LV2Plugin::has_midnam () {
987 return _midname_interface ? true : false;
991 LV2Plugin::read_midnam () {
993 if (!_midname_interface) {
996 char* midnam = _midname_interface->midnam ((void*)_impl->instance->lv2_handle);
998 std::stringstream ss;
1001 rv = MIDI::Name::MidiPatchManager::instance().update_custom_midnam (ss.str(), midnam);
1005 info << string_compose(_("LV2: update midnam for plugin '%1'"), name ()) << endmsg;
1007 warning << string_compose(_("LV2: Failed to parse midnam of plugin '%1'"), name ()) << endmsg;
1010 _midname_interface->free (midnam);
1015 LV2Plugin::midnam_model () {
1017 if (!_midname_interface) {
1020 char* model = _midname_interface->model ((void*)_impl->instance->lv2_handle);
1024 _midname_interface->free (model);
1030 LV2Plugin::unique_id() const
1032 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
1036 LV2Plugin::uri() const
1038 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
1042 LV2Plugin::label() const
1044 return lilv_node_as_string(_impl->name);
1048 LV2Plugin::name() const
1050 return lilv_node_as_string(_impl->name);
1054 LV2Plugin::maker() const
1056 return _impl->author ? lilv_node_as_string (_impl->author) : "Unknown";
1060 LV2Plugin::num_ports() const
1062 return lilv_plugin_get_num_ports(_impl->plugin);
1066 LV2Plugin::parameter_count() const
1068 return lilv_plugin_get_num_ports(_impl->plugin);
1072 LV2Plugin::default_value(uint32_t port)
1074 return _defaults[port];
1078 LV2Plugin::port_symbol(uint32_t index) const
1080 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, index);
1082 error << name() << ": Invalid port index " << index << endmsg;
1085 const LilvNode* sym = lilv_port_get_symbol(_impl->plugin, port);
1086 return lilv_node_as_string(sym);
1090 LV2Plugin::port_index (const char* symbol) const
1092 const map<string, uint32_t>::const_iterator i = _port_indices.find(symbol);
1093 if (i != _port_indices.end()) {
1096 warning << string_compose(_("LV2: Unknown port %1"), symbol) << endmsg;
1097 return (uint32_t)-1;
1102 LV2Plugin::set_parameter(uint32_t which, float val)
1104 DEBUG_TRACE(DEBUG::LV2, string_compose(
1105 "%1 set parameter %2 to %3\n", name(), which, val));
1107 if (which < lilv_plugin_get_num_ports(_impl->plugin)) {
1108 if (get_parameter (which) == val) {
1112 _shadow_data[which] = val;
1114 warning << string_compose(
1115 _("Illegal parameter number used with plugin \"%1\". "
1116 "This is a bug in either %2 or the LV2 plugin <%3>"),
1117 name(), PROGRAM_NAME, unique_id()) << endmsg;
1120 Plugin::set_parameter(which, val);
1124 LV2Plugin::get_parameter(uint32_t which) const
1126 if (parameter_is_input(which)) {
1127 return (float)_shadow_data[which];
1129 return (float)_control_data[which];
1135 LV2Plugin::get_docs() const
1137 LilvNodes* comments = lilv_plugin_get_value(_impl->plugin, _world.rdfs_comment);
1139 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
1140 lilv_nodes_free(comments);
1148 LV2Plugin::get_parameter_docs(uint32_t which) const
1150 LilvNodes* comments = lilv_port_get_value(
1152 lilv_plugin_get_port_by_index(_impl->plugin, which),
1153 _world.rdfs_comment);
1156 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
1157 lilv_nodes_free(comments);
1165 LV2Plugin::get_layout (uint32_t which, UILayoutHint& h) const
1167 /// TODO lookup port-properties
1168 if (unique_id () != "urn:ardour:a-eq") {
1173 case 0: h.x0 = 0; h.x1 = 1; h.y0 = 2; h.y1 = 3; break; // Frequency L
1174 case 1: h.x0 = 0; h.x1 = 1; h.y0 = 0; h.y1 = 1; break; // Gain L
1175 case 17: h.x0 = 0; h.x1 = 1; h.y0 = 5; h.y1 = 6; break; // enable L
1177 case 2: h.x0 = 1; h.x1 = 3; h.y0 = 2; h.y1 = 3; break; // Frequency 1
1178 case 3: h.x0 = 1; h.x1 = 3; h.y0 = 0; h.y1 = 1; break; // Gain 1
1179 case 4: h.x0 = 1; h.x1 = 3; h.y0 = 1; h.y1 = 2; break; // Bandwidth 1
1180 case 18: h.x0 = 1; h.x1 = 4; h.y0 = 5; h.y1 = 6; break; // enable 1
1182 case 5: h.x0 = 4; h.x1 = 6; h.y0 = 2; h.y1 = 3; break; // Frequency 2
1183 case 6: h.x0 = 4; h.x1 = 6; h.y0 = 0; h.y1 = 1; break; // Gain 2
1184 case 7: h.x0 = 4; h.x1 = 6; h.y0 = 1; h.y1 = 2; break; // Bandwidth 2
1185 case 19: h.x0 = 4; h.x1 = 7; h.y0 = 5; h.y1 = 6; break; // enable 2
1187 case 8: h.x0 = 7; h.x1 = 9; h.y0 = 2; h.y1 = 3; break; // Frequency 3
1188 case 9: h.x0 = 7; h.x1 = 9; h.y0 = 0; h.y1 = 1; break; // Gain 3
1189 case 10: h.x0 = 7; h.x1 = 9; h.y0 = 1; h.y1 = 2; break; // Bandwidth 3
1190 case 20: h.x0 = 7; h.x1 = 10; h.y0 = 5; h.y1 = 6; break; // enable 3
1192 case 11: h.x0 = 10; h.x1 = 12; h.y0 = 2; h.y1 = 3; break; // Frequency 4
1193 case 12: h.x0 = 10; h.x1 = 12; h.y0 = 0; h.y1 = 1; break; // Gain 4
1194 case 13: h.x0 = 10; h.x1 = 12; h.y0 = 1; h.y1 = 2; break; // Bandwidth 4
1195 case 21: h.x0 = 10; h.x1 = 13; h.y0 = 5; h.y1 = 6; break; // enable 4
1197 case 14: h.x0 = 13; h.x1 = 14; h.y0 = 2; h.y1 = 3; break; // Frequency H
1198 case 15: h.x0 = 13; h.x1 = 14; h.y0 = 0; h.y1 = 1; break; // Gain H
1199 case 22: h.x0 = 13; h.x1 = 14; h.y0 = 5; h.y1 = 6; break; // enable H
1201 case 16: h.x0 = 14; h.x1 = 15; h.y0 = 1; h.y1 = 3; break; // Master Gain
1202 case 23: h.x0 = 14; h.x1 = 15; h.y0 = 5; h.y1 = 6; break; // Master Enable
1210 LV2Plugin::nth_parameter(uint32_t n, bool& ok) const
1213 for (uint32_t c = 0, x = 0; x < lilv_plugin_get_num_ports(_impl->plugin); ++x) {
1214 if (parameter_is_control(x)) {
1226 LV2Plugin::extension_data(const char* uri) const
1228 return lilv_instance_get_extension_data(_impl->instance, uri);
1232 LV2Plugin::c_plugin()
1234 return _impl->plugin;
1240 return (const void*)_impl->ui;
1244 LV2Plugin::c_ui_type()
1246 return (const void*)_impl->ui_type;
1249 /** Directory for all plugin state. */
1251 LV2Plugin::plugin_dir() const
1253 if (!_plugin_state_dir.empty ()){
1254 return Glib::build_filename(_plugin_state_dir, _insert_id.to_s());
1256 return Glib::build_filename(_session.plugins_dir(), _insert_id.to_s());
1260 /** Directory for files created by the plugin (except during save). */
1262 LV2Plugin::scratch_dir() const
1264 return Glib::build_filename(plugin_dir(), "scratch");
1267 /** Directory for snapshots of files in the scratch directory. */
1269 LV2Plugin::file_dir() const
1271 return Glib::build_filename(plugin_dir(), "files");
1274 /** Directory to save state snapshot version @c num into. */
1276 LV2Plugin::state_dir(unsigned num) const
1278 return Glib::build_filename(plugin_dir(), string("state") + PBD::to_string (num));
1281 /** Implementation of state:makePath for files created at instantiation time.
1282 * Note this is not used for files created at save time (Lilv deals with that).
1285 LV2Plugin::lv2_state_make_path(LV2_State_Make_Path_Handle handle,
1288 LV2Plugin* me = (LV2Plugin*)handle;
1289 if (me->_insert_id == PBD::ID("0")) {
1290 warning << string_compose(
1291 "File path \"%1\" requested but LV2 %2 has no insert ID",
1292 path, me->name()) << endmsg;
1293 return g_strdup(path);
1296 const std::string abs_path = Glib::build_filename(me->scratch_dir(), path);
1297 const std::string dirname = Glib::path_get_dirname(abs_path);
1298 g_mkdir_with_parents(dirname.c_str(), 0744);
1300 DEBUG_TRACE(DEBUG::LV2, string_compose("new file path %1 => %2\n",
1303 return g_strndup(abs_path.c_str(), abs_path.length());
1307 LV2Plugin::add_state(XMLNode* root) const
1309 assert(_insert_id != PBD::ID("0"));
1314 for (uint32_t i = 0; i < parameter_count(); ++i) {
1315 if (parameter_is_input(i) && parameter_is_control(i)) {
1316 child = new XMLNode("Port");
1317 child->set_property("symbol", port_symbol(i));
1318 child->set_property("value", _shadow_data[i]);
1319 root->add_child_nocopy(*child);
1323 if (!_plugin_state_dir.empty()) {
1324 root->set_property("template-dir", _plugin_state_dir);
1327 if (_has_state_interface) {
1328 // Provisionally increment state version and create directory
1329 const std::string new_dir = state_dir(++_state_version);
1330 // and keep track of it (for templates & archive)
1331 unsigned int saved_state = _state_version;;
1332 g_mkdir_with_parents(new_dir.c_str(), 0744);
1334 LilvState* state = lilv_state_new_from_instance(
1337 _uri_map.urid_map(),
1338 scratch_dir().c_str(),
1340 _session.externals_dir().c_str(),
1343 const_cast<LV2Plugin*>(this),
1347 if (!_plugin_state_dir.empty() || force_state_save
1349 || !lilv_state_equals(state, _impl->state)) {
1350 lilv_state_save(_world.world,
1351 _uri_map.urid_map(),
1352 _uri_map.urid_unmap(),
1358 if (force_state_save) {
1359 // archive or save-as
1360 lilv_state_free(state);
1363 else if (_plugin_state_dir.empty()) {
1364 // normal session save
1365 lilv_state_free(_impl->state);
1366 _impl->state = state;
1368 // template save (dedicated state-dir)
1369 lilv_state_free(state);
1373 // State is identical, decrement version and nuke directory
1374 lilv_state_free(state);
1375 PBD::remove_directory(new_dir);
1377 saved_state = _state_version;
1380 root->set_property("state-dir", string("state") + PBD::to_string (saved_state));
1384 // TODO: Once we can rely on lilv 0.16.0, lilv_world_get can replace this
1386 get_value(LilvWorld* world, const LilvNode* subject, const LilvNode* predicate)
1388 LilvNodes* vs = lilv_world_find_nodes(world, subject, predicate, NULL);
1390 LilvNode* node = lilv_node_duplicate(lilv_nodes_get_first(vs));
1391 lilv_nodes_free(vs);
1398 LV2Plugin::find_presets()
1400 LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
1401 LilvNode* pset_Preset = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
1402 LilvNode* rdfs_label = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
1404 LilvNodes* presets = lilv_plugin_get_related(_impl->plugin, pset_Preset);
1405 LILV_FOREACH(nodes, i, presets) {
1406 const LilvNode* preset = lilv_nodes_get(presets, i);
1407 lilv_world_load_resource(_world.world, preset);
1408 LilvNode* name = get_value(_world.world, preset, rdfs_label);
1409 bool userpreset = true; // TODO
1411 _presets.insert(std::make_pair(lilv_node_as_string(preset),
1412 Plugin::PresetRecord(
1413 lilv_node_as_string(preset),
1414 lilv_node_as_string(name),
1416 lilv_node_free(name);
1418 warning << string_compose(
1419 _("Plugin \"%1\" preset \"%2\" is missing a label\n"),
1420 lilv_node_as_string(lilv_plugin_get_uri(_impl->plugin)),
1421 lilv_node_as_string(preset)) << endmsg;
1424 lilv_nodes_free(presets);
1426 lilv_node_free(rdfs_label);
1427 lilv_node_free(pset_Preset);
1428 lilv_node_free(lv2_appliesTo);
1432 set_port_value(const char* port_symbol,
1438 LV2Plugin* self = (LV2Plugin*)user_data;
1439 if (type != 0 && type != URIMap::instance().urids.atom_Float) {
1440 return; // TODO: Support non-float ports
1443 const uint32_t port_index = self->port_index(port_symbol);
1444 if (port_index != (uint32_t)-1) {
1445 self->set_parameter(port_index, *(const float*)value);
1446 self->PresetPortSetValue (port_index, *(const float*)value); /* EMIT SIGNAL */
1451 LV2Plugin::load_preset(PresetRecord r)
1453 LilvWorld* world = _world.world;
1454 LilvNode* pset = lilv_new_uri(world, r.uri.c_str());
1455 LilvState* state = lilv_state_new_from_world(world, _uri_map.urid_map(), pset);
1457 const LV2_Feature* state_features[2] = { NULL, NULL };
1458 LV2_Worker_Schedule schedule = { _state_worker, work_schedule };
1459 const LV2_Feature state_sched_feature = { LV2_WORKER__schedule, &schedule };
1460 if (_state_worker) {
1461 state_features[0] = &state_sched_feature;
1465 lilv_state_restore(state, _impl->instance, set_port_value, this, 0, state_features);
1466 lilv_state_free(state);
1467 Plugin::load_preset(r);
1470 lilv_node_free(pset);
1475 ARDOUR::lv2plugin_get_port_value(const char* port_symbol,
1480 LV2Plugin *plugin = (LV2Plugin *) user_data;
1482 uint32_t index = plugin->port_index(port_symbol);
1483 if (index != (uint32_t) -1) {
1484 if (plugin->parameter_is_input(index) && plugin->parameter_is_control(index)) {
1486 *size = sizeof(float);
1487 *type = plugin->_uri_map.uri_to_id(LV2_ATOM__Float);
1488 value = &plugin->_shadow_data[index];
1500 LV2Plugin::do_save_preset(string name)
1502 LilvNode* plug_name = lilv_plugin_get_name(_impl->plugin);
1503 const string prefix = legalize_for_uri(lilv_node_as_string(plug_name));
1504 const string base_name = legalize_for_uri(name);
1505 const string file_name = base_name + ".ttl";
1506 const string bundle = Glib::build_filename(
1507 Glib::get_home_dir(),
1508 Glib::build_filename(".lv2", prefix + "_" + base_name + ".lv2"));
1510 #ifdef HAVE_LILV_0_21_3
1511 /* delete reference to old preset (if any) */
1512 const PresetRecord* r = preset_by_label(name);
1514 LilvNode* pset = lilv_new_uri (_world.world, r->uri.c_str());
1516 lilv_world_unload_resource (_world.world, pset);
1517 lilv_node_free(pset);
1522 LilvState* state = lilv_state_new_from_instance(
1525 _uri_map.urid_map(),
1526 scratch_dir().c_str(), // file_dir
1527 bundle.c_str(), // copy_dir
1528 bundle.c_str(), // link_dir
1529 bundle.c_str(), // save_dir
1530 lv2plugin_get_port_value, // get_value
1531 (void*)this, // user_data
1532 LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE, // flags
1533 _features // features
1536 lilv_state_set_label(state, name.c_str());
1538 _world.world, // world
1539 _uri_map.urid_map(), // map
1540 _uri_map.urid_unmap(), // unmap
1542 NULL, // uri (NULL = use file URI)
1543 bundle.c_str(), // dir
1544 file_name.c_str() // filename
1547 lilv_state_free(state);
1549 std::string uri = Glib::filename_to_uri(Glib::build_filename(bundle, file_name));
1550 LilvNode *node_bundle = lilv_new_uri(_world.world, Glib::filename_to_uri(Glib::build_filename(bundle, "/")).c_str());
1551 LilvNode *node_preset = lilv_new_uri(_world.world, uri.c_str());
1552 #ifdef HAVE_LILV_0_21_3
1553 lilv_world_unload_resource(_world.world, node_preset);
1554 lilv_world_unload_bundle(_world.world, node_bundle);
1556 lilv_world_load_bundle(_world.world, node_bundle);
1557 lilv_world_load_resource(_world.world, node_preset);
1558 lilv_node_free(node_bundle);
1559 lilv_node_free(node_preset);
1560 lilv_node_free(plug_name);
1565 LV2Plugin::do_remove_preset(string name)
1567 #ifdef HAVE_LILV_0_21_3
1568 /* Look up preset record by label (FIXME: ick, label as ID) */
1569 const PresetRecord* r = preset_by_label(name);
1574 /* Load a LilvState for the preset. */
1575 LilvWorld* world = _world.world;
1576 LilvNode* pset = lilv_new_uri(world, r->uri.c_str());
1577 LilvState* state = lilv_state_new_from_world(world, _uri_map.urid_map(), pset);
1579 lilv_node_free(pset);
1583 /* Unload preset from world. */
1584 lilv_world_unload_resource(world, pset);
1586 /* Delete it from the file system. This will remove the preset file and the entry
1587 from the manifest. If this results in an empty manifest (i.e. the
1588 preset is the only thing in the bundle), then the bundle is removed. */
1589 lilv_state_delete(world, state);
1591 lilv_state_free(state);
1592 lilv_node_free(pset);
1594 /* Without lilv_state_delete(), we could delete the preset file, but this
1595 would leave a broken bundle/manifest around, so the preset would still
1596 be visible, but broken. Naively deleting a bundle is too dangerous, so
1597 we simply do not support preset deletion with older Lilv */
1601 LV2Plugin::has_editor() const
1603 return _impl->ui != NULL;
1607 LV2Plugin::has_message_output() const
1609 for (uint32_t i = 0; i < num_ports(); ++i) {
1610 if ((_port_flags[i] & PORT_SEQUENCE) &&
1611 (_port_flags[i] & PORT_OUTPUT)) {
1619 LV2Plugin::write_to(RingBuffer<uint8_t>* dest,
1623 const uint8_t* body)
1625 const uint32_t buf_size = sizeof(UIMessage) + size;
1626 vector<uint8_t> buf(buf_size);
1628 UIMessage* msg = (UIMessage*)&buf[0];
1630 msg->protocol = protocol;
1632 memcpy(msg + 1, body, size);
1634 return (dest->write(&buf[0], buf_size) == buf_size);
1638 LV2Plugin::write_from_ui(uint32_t index,
1641 const uint8_t* body)
1644 size_t rbs = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
1645 /* buffer data communication from plugin UI to plugin instance.
1646 * this buffer needs to potentially hold
1647 * (port's minimumSize) * (audio-periods) / (UI-periods)
1650 * e.g 48kSPS / 128fpp -> audio-periods = 375 Hz
1651 * ui-periods = 25 Hz (SuperRapidScreenUpdate)
1652 * default minimumSize = 32K (see LV2Plugin::allocate_atom_event_buffers()
1654 * it is NOT safe to overflow (msg.size will be misinterpreted)
1656 uint32_t bufsiz = 32768;
1657 if (_atom_ev_buffers && _atom_ev_buffers[0]) {
1658 bufsiz = lv2_evbuf_get_capacity(_atom_ev_buffers[0]);
1660 int fact = ceilf(_session.frame_rate () / 3000.f);
1661 rbs = max((size_t) bufsiz * std::max (8, fact), rbs);
1662 _from_ui = new RingBuffer<uint8_t>(rbs);
1665 if (!write_to(_from_ui, index, protocol, size, body)) {
1666 error << "Error writing from UI to plugin" << endmsg;
1673 LV2Plugin::write_to_ui(uint32_t index,
1676 const uint8_t* body)
1678 if (!write_to(_to_ui, index, protocol, size, body)) {
1679 error << "Error writing from plugin to UI" << endmsg;
1686 forge_variant(LV2_Atom_Forge* forge, const Variant& value)
1688 switch (value.type()) {
1689 case Variant::NOTHING:
1691 case Variant::BEATS:
1692 // No atom type for this, just forge a double
1693 lv2_atom_forge_double(forge, value.get_beats().to_double());
1696 lv2_atom_forge_bool(forge, value.get_bool());
1698 case Variant::DOUBLE:
1699 lv2_atom_forge_double(forge, value.get_double());
1701 case Variant::FLOAT:
1702 lv2_atom_forge_float(forge, value.get_float());
1705 lv2_atom_forge_int(forge, value.get_int());
1708 lv2_atom_forge_long(forge, value.get_long());
1711 lv2_atom_forge_path(
1712 forge, value.get_path().c_str(), value.get_path().size());
1714 case Variant::STRING:
1715 lv2_atom_forge_string(
1716 forge, value.get_string().c_str(), value.get_string().size());
1720 forge, value.get_uri().c_str(), value.get_uri().size());
1725 /** Get a variant type from a URI, return false iff no match found. */
1727 uri_to_variant_type(const std::string& uri, Variant::Type& type)
1729 if (uri == LV2_ATOM__Bool) {
1730 type = Variant::BOOL;
1731 } else if (uri == LV2_ATOM__Double) {
1732 type = Variant::DOUBLE;
1733 } else if (uri == LV2_ATOM__Float) {
1734 type = Variant::FLOAT;
1735 } else if (uri == LV2_ATOM__Int) {
1736 type = Variant::INT;
1737 } else if (uri == LV2_ATOM__Long) {
1738 type = Variant::LONG;
1739 } else if (uri == LV2_ATOM__Path) {
1740 type = Variant::PATH;
1741 } else if (uri == LV2_ATOM__String) {
1742 type = Variant::STRING;
1743 } else if (uri == LV2_ATOM__URI) {
1744 type = Variant::URI;
1752 LV2Plugin::set_property(uint32_t key, const Variant& value)
1754 if (_patch_port_in_index == (uint32_t)-1) {
1755 error << "LV2: set_property called with unset patch_port_in_index" << endmsg;
1757 } else if (value.type() == Variant::NOTHING) {
1758 error << "LV2: set_property called with void value" << endmsg;
1762 // Set up forge to write to temporary buffer on the stack
1763 LV2_Atom_Forge* forge = &_impl->ui_forge;
1764 LV2_Atom_Forge_Frame frame;
1765 uint8_t buf[PATH_MAX]; // Ought to be enough for anyone...
1767 lv2_atom_forge_set_buffer(forge, buf, sizeof(buf));
1769 // Serialize patch:Set message to set property
1770 #ifdef HAVE_LV2_1_10_0
1771 lv2_atom_forge_object(forge, &frame, 0, _uri_map.urids.patch_Set);
1772 lv2_atom_forge_key(forge, _uri_map.urids.patch_property);
1773 lv2_atom_forge_urid(forge, key);
1774 lv2_atom_forge_key(forge, _uri_map.urids.patch_value);
1776 lv2_atom_forge_blank(forge, &frame, 0, _uri_map.urids.patch_Set);
1777 lv2_atom_forge_property_head(forge, _uri_map.urids.patch_property, 0);
1778 lv2_atom_forge_urid(forge, key);
1779 lv2_atom_forge_property_head(forge, _uri_map.urids.patch_value, 0);
1782 forge_variant(forge, value);
1784 // Write message to UI=>Plugin ring
1785 const LV2_Atom* const atom = (const LV2_Atom*)buf;
1786 write_from_ui(_patch_port_in_index,
1787 _uri_map.urids.atom_eventTransfer,
1788 lv2_atom_total_size(atom),
1789 (const uint8_t*)atom);
1792 const ParameterDescriptor&
1793 LV2Plugin::get_property_descriptor(uint32_t id) const
1795 PropertyDescriptors::const_iterator p = _property_descriptors.find(id);
1796 if (p != _property_descriptors.end()) {
1799 return Plugin::get_property_descriptor(id);
1803 load_parameter_descriptor_units(LilvWorld* lworld, ParameterDescriptor& desc, const LilvNodes* units)
1805 if (lilv_nodes_contains(units, _world.units_midiNote)) {
1806 desc.unit = ParameterDescriptor::MIDI_NOTE;
1807 } else if (lilv_nodes_contains(units, _world.units_db)) {
1808 desc.unit = ParameterDescriptor::DB;
1809 } else if (lilv_nodes_contains(units, _world.units_hz)) {
1810 desc.unit = ParameterDescriptor::HZ;
1812 if (lilv_nodes_size(units) > 0) {
1813 const LilvNode* unit = lilv_nodes_get_first(units);
1814 LilvNode* render = get_value(lworld, unit, _world.units_render);
1816 desc.print_fmt = lilv_node_as_string(render);
1817 /* override lilv's default "%f" format */
1818 if (desc.integer_step) {
1819 replace_all (desc.print_fmt, "%f", "%.0f");
1820 } else if (desc.upper - desc.lower >= 1000) {
1821 replace_all (desc.print_fmt, "%f", "%.1f");
1822 } else if (desc.upper - desc.lower >= 100) {
1823 replace_all (desc.print_fmt, "%f", "%.2f");
1825 replace_all (desc.print_fmt, "%f", "%.3f");
1827 lilv_node_free(render);
1833 load_parameter_descriptor(LV2World& world,
1834 ParameterDescriptor& desc,
1835 Variant::Type datatype,
1836 const LilvNode* subject)
1838 LilvWorld* lworld = _world.world;
1839 LilvNode* label = get_value(lworld, subject, _world.rdfs_label);
1840 LilvNode* def = get_value(lworld, subject, _world.lv2_default);
1841 LilvNode* minimum = get_value(lworld, subject, _world.lv2_minimum);
1842 LilvNode* maximum = get_value(lworld, subject, _world.lv2_maximum);
1843 LilvNodes* units = lilv_world_find_nodes(lworld, subject, _world.units_unit, NULL);
1845 desc.label = lilv_node_as_string(label);
1848 if (lilv_node_is_float(def)) {
1849 desc.normal = lilv_node_as_float(def);
1850 } else if (lilv_node_is_int(def)) {
1851 desc.normal = lilv_node_as_int(def);
1855 if (lilv_node_is_float(minimum)) {
1856 desc.lower = lilv_node_as_float(minimum);
1857 } else if (lilv_node_is_int(minimum)) {
1858 desc.lower = lilv_node_as_int(minimum);
1862 if (lilv_node_is_float(maximum)) {
1863 desc.upper = lilv_node_as_float(maximum);
1864 } else if (lilv_node_is_int(maximum)) {
1865 desc.upper = lilv_node_as_int(maximum);
1868 load_parameter_descriptor_units(lworld, desc, units);
1869 desc.datatype = datatype;
1870 desc.toggled |= datatype == Variant::BOOL;
1871 desc.integer_step |= datatype == Variant::INT || datatype == Variant::LONG;
1872 desc.update_steps();
1874 lilv_nodes_free(units);
1875 lilv_node_free(label);
1876 lilv_node_free(def);
1877 lilv_node_free(minimum);
1878 lilv_node_free(maximum);
1882 LV2Plugin::load_supported_properties(PropertyDescriptors& descs)
1884 LilvWorld* lworld = _world.world;
1885 const LilvNode* subject = lilv_plugin_get_uri(_impl->plugin);
1886 LilvNodes* properties = lilv_world_find_nodes(
1887 lworld, subject, _world.patch_writable, NULL);
1888 LILV_FOREACH(nodes, p, properties) {
1889 // Get label and range
1890 const LilvNode* prop = lilv_nodes_get(properties, p);
1891 LilvNode* range = get_value(lworld, prop, _world.rdfs_range);
1893 warning << string_compose(_("LV2: property <%1> has no range datatype, ignoring"),
1894 lilv_node_as_uri(prop)) << endmsg;
1898 // Convert range to variant type (TODO: support for multiple range types)
1899 Variant::Type datatype;
1900 if (!uri_to_variant_type(lilv_node_as_uri(range), datatype)) {
1901 error << string_compose(_("LV2: property <%1> has unsupported datatype <%1>"),
1902 lilv_node_as_uri(prop), lilv_node_as_uri(range)) << endmsg;
1906 // Add description to result
1907 ParameterDescriptor desc;
1908 desc.key = _uri_map.uri_to_id(lilv_node_as_uri(prop));
1909 desc.datatype = datatype;
1910 load_parameter_descriptor(_world, desc, datatype, prop);
1911 descs.insert(std::make_pair(desc.key, desc));
1913 lilv_node_free(range);
1915 lilv_nodes_free(properties);
1919 LV2Plugin::announce_property_values()
1921 if (_patch_port_in_index == (uint32_t)-1) {
1925 // Set up forge to write to temporary buffer on the stack
1926 LV2_Atom_Forge* forge = &_impl->ui_forge;
1927 LV2_Atom_Forge_Frame frame;
1928 uint8_t buf[PATH_MAX]; // Ought to be enough for anyone...
1930 lv2_atom_forge_set_buffer(forge, buf, sizeof(buf));
1932 // Serialize patch:Get message with no subject (implicitly plugin instance)
1933 #ifdef HAVE_LV2_1_10_0
1934 lv2_atom_forge_object(forge, &frame, 0, _uri_map.urids.patch_Get);
1936 lv2_atom_forge_blank(forge, &frame, 0, _uri_map.urids.patch_Get);
1939 // Write message to UI=>Plugin ring
1940 const LV2_Atom* const atom = (const LV2_Atom*)buf;
1941 write_from_ui(_patch_port_in_index,
1942 _uri_map.urids.atom_eventTransfer,
1943 lv2_atom_total_size(atom),
1944 (const uint8_t*)atom);
1948 LV2Plugin::enable_ui_emission()
1951 /* see note in LV2Plugin::write_from_ui() */
1952 uint32_t bufsiz = 32768;
1953 if (_atom_ev_buffers && _atom_ev_buffers[0]) {
1954 bufsiz = lv2_evbuf_get_capacity(_atom_ev_buffers[0]);
1956 size_t rbs = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
1957 rbs = max((size_t) bufsiz * 8, rbs);
1958 _to_ui = new RingBuffer<uint8_t>(rbs);
1963 LV2Plugin::emit_to_ui(void* controller, UIMessageSink sink)
1969 uint32_t read_space = _to_ui->read_space();
1970 while (read_space > sizeof(UIMessage)) {
1972 if (_to_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
1973 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
1976 vector<uint8_t> body(msg.size);
1977 if (_to_ui->read(&body[0], msg.size) != msg.size) {
1978 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
1982 sink(controller, msg.index, msg.size, msg.protocol, &body[0]);
1984 read_space -= sizeof(msg) + msg.size;
1989 LV2Plugin::work(Worker& worker, uint32_t size, const void* data)
1991 Glib::Threads::Mutex::Lock lm(_work_mutex);
1992 return _impl->work_iface->work(
1993 _impl->instance->lv2_handle, work_respond, &worker, size, data);
1997 LV2Plugin::work_response(uint32_t size, const void* data)
1999 return _impl->work_iface->work_response(
2000 _impl->instance->lv2_handle, size, data);
2004 LV2Plugin::set_insert_id(PBD::ID id)
2006 if (_insert_id == "0") {
2008 } else if (_insert_id != id) {
2009 lilv_state_free(_impl->state);
2010 _impl->state = NULL;
2016 LV2Plugin::set_state_dir (const std::string& d)
2018 _plugin_state_dir = d;
2022 LV2Plugin::set_state(const XMLNode& node, int version)
2025 XMLNodeConstIterator iter;
2029 if (node.name() != state_node_name()) {
2030 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
2034 #ifndef NO_PLUGIN_STATE
2036 if (version < 3000) {
2037 nodes = node.children("port");
2039 nodes = node.children("Port");
2042 for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
2047 if (!child->get_property("symbol", sym)) {
2048 warning << _("LV2: port has no symbol, ignored") << endmsg;
2052 map<string, uint32_t>::iterator i = _port_indices.find(sym);
2056 if (i != _port_indices.end()) {
2057 port_id = i->second;
2059 warning << _("LV2: port has unknown index, ignored") << endmsg;
2064 if (!child->get_property("value", val)) {
2065 warning << _("LV2: port has no value, ignored") << endmsg;
2069 set_parameter(port_id, val);
2072 std::string template_dir;
2073 if (node.get_property("template-dir", template_dir)) {
2074 set_state_dir (template_dir);
2078 std::string state_dir;
2079 if (node.get_property("state-dir", state_dir) != 0) {
2080 if (sscanf(state_dir.c_str(), "state%u", &_state_version) != 1) {
2081 error << string_compose(
2082 "LV2: failed to parse state version from \"%1\"",
2083 state_dir) << endmsg;
2086 std::string state_file = Glib::build_filename(
2088 Glib::build_filename(state_dir, "state.ttl"));
2090 LilvState* state = lilv_state_new_from_file(
2091 _world.world, _uri_map.urid_map(), NULL, state_file.c_str());
2093 lilv_state_restore(state, _impl->instance, NULL, NULL, 0, NULL);
2094 lilv_state_free(_impl->state);
2095 _impl->state = state;
2098 if (!_plugin_state_dir.empty ()) {
2099 // force save with session, next time (increment counter)
2100 lilv_state_free (_impl->state);
2101 _impl->state = NULL;
2105 latency_compute_run();
2108 return Plugin::set_state(node, version);
2112 LV2Plugin::get_parameter_descriptor(uint32_t which, ParameterDescriptor& desc) const
2114 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, which);
2116 error << string_compose("LV2: get descriptor of non-existent port %1", which)
2121 LilvNodes* portunits;
2122 LilvNode *def, *min, *max;
2123 lilv_port_get_range(_impl->plugin, port, &def, &min, &max);
2124 portunits = lilv_port_get_value(_impl->plugin, port, _world.units_unit);
2126 LilvNode* steps = lilv_port_get(_impl->plugin, port, _world.ext_rangeSteps);
2128 // TODO: Once we can rely on lilv 0.18.0 being present,
2129 // load_parameter_descriptor() can be used for ports as well
2130 desc.integer_step = lilv_port_has_property(_impl->plugin, port, _world.lv2_integer);
2131 desc.toggled = lilv_port_has_property(_impl->plugin, port, _world.lv2_toggled);
2132 desc.logarithmic = lilv_port_has_property(_impl->plugin, port, _world.ext_logarithmic);
2133 desc.sr_dependent = lilv_port_has_property(_impl->plugin, port, _world.lv2_sampleRate);
2134 desc.label = lilv_node_as_string(lilv_port_get_name(_impl->plugin, port));
2135 desc.normal = def ? lilv_node_as_float(def) : 0.0f;
2136 desc.lower = min ? lilv_node_as_float(min) : 0.0f;
2137 desc.upper = max ? lilv_node_as_float(max) : 1.0f;
2138 load_parameter_descriptor_units(_world.world, desc, portunits);
2140 if (desc.sr_dependent) {
2141 desc.lower *= _session.frame_rate ();
2142 desc.upper *= _session.frame_rate ();
2145 desc.enumeration = lilv_port_has_property(_impl->plugin, port, _world.lv2_enumeration);
2146 desc.scale_points = get_scale_points(which);
2149 desc.rangesteps = lilv_node_as_float (steps);
2152 desc.update_steps();
2154 lilv_node_free(def);
2155 lilv_node_free(min);
2156 lilv_node_free(max);
2157 lilv_node_free(steps);
2158 lilv_nodes_free(portunits);
2163 Plugin::IOPortDescription
2164 LV2Plugin::describe_io_port (ARDOUR::DataType dt, bool input, uint32_t id) const
2166 PortFlags match = 0;
2168 case DataType::AUDIO:
2171 case DataType::MIDI:
2172 match = PORT_SEQUENCE | PORT_MIDI; // ignore old PORT_EVENT
2175 return Plugin::IOPortDescription ("?");
2179 match |= PORT_INPUT;
2181 match |= PORT_OUTPUT;
2185 uint32_t idx = UINT32_MAX;
2187 uint32_t const num_ports = parameter_count();
2188 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2189 PortFlags flags = _port_flags[port_index];
2190 if ((flags & match) == match) {
2197 if (idx == UINT32_MAX) {
2198 return Plugin::IOPortDescription ("?");
2201 const LilvPort* pport = lilv_plugin_get_port_by_index (_impl->plugin, idx);
2203 LilvNode* name = lilv_port_get_name(_impl->plugin, pport);
2204 Plugin::IOPortDescription iod (lilv_node_as_string (name));
2205 lilv_node_free(name);
2207 /* get the port's pg:group */
2208 LilvNodes* groups = lilv_port_get_value (_impl->plugin, pport, _world.groups_group);
2209 if (lilv_nodes_size (groups) > 0) {
2210 const LilvNode* group = lilv_nodes_get_first (groups);
2211 LilvNodes* grouplabel = lilv_world_find_nodes (_world.world, group, _world.rdfs_label, NULL);
2213 /* get the name of the port-group */
2214 if (lilv_nodes_size (grouplabel) > 0) {
2215 const LilvNode* grpname = lilv_nodes_get_first (grouplabel);
2216 iod.group_name = lilv_node_as_string (grpname);
2218 lilv_nodes_free (grouplabel);
2220 /* get all port designations.
2221 * we're interested in e.g. lv2:designation pg:right */
2222 LilvNodes* designations = lilv_port_get_value (_impl->plugin, pport, _world.lv2_designation);
2223 if (lilv_nodes_size (designations) > 0) {
2224 /* get all pg:elements of the pg:group */
2225 LilvNodes* group_childs = lilv_world_find_nodes (_world.world, group, _world.groups_element, NULL);
2226 if (lilv_nodes_size (group_childs) > 0) {
2227 /* iterate over all port designations .. */
2228 LILV_FOREACH (nodes, i, designations) {
2229 const LilvNode* designation = lilv_nodes_get (designations, i);
2230 /* match the lv2:designation's element against the port-group's element */
2231 LILV_FOREACH (nodes, j, group_childs) {
2232 const LilvNode* group_element = lilv_nodes_get (group_childs, j);
2233 LilvNodes* elem = lilv_world_find_nodes (_world.world, group_element, _world.lv2_designation, designation);
2234 /* found it. Now look up the index (channel-number) of the pg:Element */
2235 if (lilv_nodes_size (elem) > 0) {
2236 LilvNodes* idx = lilv_world_find_nodes (_world.world, lilv_nodes_get_first (elem), _world.lv2_index, NULL);
2237 if (lilv_node_is_int (lilv_nodes_get_first (idx))) {
2238 iod.group_channel = lilv_node_as_int(lilv_nodes_get_first (idx));
2245 lilv_nodes_free (groups);
2246 lilv_nodes_free (designations);
2249 if (lilv_port_has_property(_impl->plugin, pport, _world.lv2_isSideChain)) {
2250 iod.is_sidechain = true;
2256 LV2Plugin::describe_parameter(Evoral::Parameter which)
2258 if (( which.type() == PluginAutomation) && ( which.id() < parameter_count()) ) {
2260 if (lilv_port_has_property(_impl->plugin,
2261 lilv_plugin_get_port_by_index(_impl->plugin, which.id()), _world.ext_notOnGUI)) {
2262 return X_("hidden");
2265 if (lilv_port_has_property(_impl->plugin,
2266 lilv_plugin_get_port_by_index(_impl->plugin, which.id()), _world.lv2_freewheeling)) {
2267 return X_("hidden");
2270 if (lilv_port_has_property(_impl->plugin,
2271 lilv_plugin_get_port_by_index(_impl->plugin, which.id()), _world.lv2_reportsLatency)) {
2272 return X_("latency");
2275 LilvNode* name = lilv_port_get_name(_impl->plugin,
2276 lilv_plugin_get_port_by_index(_impl->plugin, which.id()));
2277 string ret(lilv_node_as_string(name));
2278 lilv_node_free(name);
2286 LV2Plugin::max_latency () const
2288 return _max_latency;
2292 LV2Plugin::signal_latency() const
2294 if (_latency_control_port) {
2295 return (framecnt_t)floor(*_latency_control_port);
2301 set<Evoral::Parameter>
2302 LV2Plugin::automatable() const
2304 set<Evoral::Parameter> ret;
2306 for (uint32_t i = 0; i < parameter_count(); ++i) {
2307 if (parameter_is_input(i) && parameter_is_control(i) && !(_port_flags[i] & PORT_NOAUTO)) {
2308 ret.insert(ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
2312 for (PropertyDescriptors::const_iterator p = _property_descriptors.begin();
2313 p != _property_descriptors.end();
2315 ret.insert(ret.end(), Evoral::Parameter(PluginPropertyAutomation, 0, p->first));
2321 LV2Plugin::set_automation_control (uint32_t i, boost::shared_ptr<AutomationControl> c)
2323 if ((_port_flags[i] & (PORT_CTRLED | PORT_CTRLER))) {
2324 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("Ctrl Port %1\n", i));
2325 _ctrl_map [i] = AutomationCtrlPtr (new AutomationCtrl(c));
2329 LV2Plugin::AutomationCtrlPtr
2330 LV2Plugin::get_automation_control (uint32_t i)
2332 if (_ctrl_map.find (i) == _ctrl_map.end()) {
2333 return AutomationCtrlPtr ();
2335 return _ctrl_map[i];
2339 LV2Plugin::activate()
2341 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 activate\n", name()));
2343 if (!_was_activated) {
2344 lilv_instance_activate(_impl->instance);
2345 _was_activated = true;
2350 LV2Plugin::deactivate()
2352 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 deactivate\n", name()));
2354 if (_was_activated) {
2355 lilv_instance_deactivate(_impl->instance);
2356 _was_activated = false;
2361 LV2Plugin::cleanup()
2363 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 cleanup\n", name()));
2366 lilv_instance_free(_impl->instance);
2367 _impl->instance = NULL;
2371 LV2Plugin::allocate_atom_event_buffers()
2373 /* reserve local scratch buffers for ATOM event-queues */
2374 const LilvPlugin* p = _impl->plugin;
2376 /* count non-MIDI atom event-ports
2377 * TODO: nicely ask drobilla to make a lilv_ call for that
2379 int count_atom_out = 0;
2380 int count_atom_in = 0;
2381 int minimumSize = 32768; // TODO use a per-port minimum-size
2382 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
2383 const LilvPort* port = lilv_plugin_get_port_by_index(p, i);
2384 if (lilv_port_is_a(p, port, _world.atom_AtomPort)) {
2385 LilvNodes* buffer_types = lilv_port_get_value(
2386 p, port, _world.atom_bufferType);
2387 LilvNodes* atom_supports = lilv_port_get_value(
2388 p, port, _world.atom_supports);
2390 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
2391 if (lilv_port_is_a(p, port, _world.lv2_InputPort)) {
2394 if (lilv_port_is_a(p, port, _world.lv2_OutputPort)) {
2397 LilvNodes* min_size_v = lilv_port_get_value(_impl->plugin, port, _world.rsz_minimumSize);
2398 LilvNode* min_size = min_size_v ? lilv_nodes_get_first(min_size_v) : NULL;
2399 if (min_size && lilv_node_is_int(min_size)) {
2400 minimumSize = std::max(minimumSize, lilv_node_as_int(min_size));
2402 lilv_nodes_free(min_size_v);
2404 lilv_nodes_free(buffer_types);
2405 lilv_nodes_free(atom_supports);
2409 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 need buffers for %2 atom-in and %3 atom-out event-ports\n",
2410 name(), count_atom_in, count_atom_out));
2412 const int total_atom_buffers = (count_atom_in + count_atom_out);
2413 if (_atom_ev_buffers || total_atom_buffers == 0) {
2417 DEBUG_TRACE(DEBUG::LV2, string_compose("allocate %1 atom_ev_buffers of %2 bytes\n", total_atom_buffers, minimumSize));
2418 _atom_ev_buffers = (LV2_Evbuf**) malloc((total_atom_buffers + 1) * sizeof(LV2_Evbuf*));
2419 for (int i = 0; i < total_atom_buffers; ++i ) {
2420 _atom_ev_buffers[i] = lv2_evbuf_new(minimumSize, LV2_EVBUF_ATOM,
2421 _uri_map.urids.atom_Chunk, _uri_map.urids.atom_Sequence);
2423 _atom_ev_buffers[total_atom_buffers] = 0;
2427 /** Write an ardour position/time/tempo/meter as an LV2 event.
2428 * @return true on success.
2431 write_position(LV2_Atom_Forge* forge,
2433 const TempoMetric& t,
2434 Timecode::BBT_Time& bbt,
2437 framepos_t position,
2440 const URIMap::URIDs& urids = URIMap::instance().urids;
2442 uint8_t pos_buf[256];
2443 lv2_atom_forge_set_buffer(forge, pos_buf, sizeof(pos_buf));
2444 LV2_Atom_Forge_Frame frame;
2445 #ifdef HAVE_LV2_1_10_0
2446 lv2_atom_forge_object(forge, &frame, 0, urids.time_Position);
2447 lv2_atom_forge_key(forge, urids.time_frame);
2448 lv2_atom_forge_long(forge, position);
2449 lv2_atom_forge_key(forge, urids.time_speed);
2450 lv2_atom_forge_float(forge, speed);
2451 lv2_atom_forge_key(forge, urids.time_barBeat);
2452 lv2_atom_forge_float(forge, bbt.beats - 1 +
2453 (bbt.ticks / Timecode::BBT_Time::ticks_per_beat));
2454 lv2_atom_forge_key(forge, urids.time_bar);
2455 lv2_atom_forge_long(forge, bbt.bars - 1);
2456 lv2_atom_forge_key(forge, urids.time_beatUnit);
2457 lv2_atom_forge_int(forge, t.meter().note_divisor());
2458 lv2_atom_forge_key(forge, urids.time_beatsPerBar);
2459 lv2_atom_forge_float(forge, t.meter().divisions_per_bar());
2460 lv2_atom_forge_key(forge, urids.time_beatsPerMinute);
2461 lv2_atom_forge_float(forge, bpm);
2463 lv2_atom_forge_blank(forge, &frame, 1, urids.time_Position);
2464 lv2_atom_forge_property_head(forge, urids.time_frame, 0);
2465 lv2_atom_forge_long(forge, position);
2466 lv2_atom_forge_property_head(forge, urids.time_speed, 0);
2467 lv2_atom_forge_float(forge, speed);
2468 lv2_atom_forge_property_head(forge, urids.time_barBeat, 0);
2469 lv2_atom_forge_float(forge, bbt.beats - 1 +
2470 (bbt.ticks / Timecode::BBT_Time::ticks_per_beat));
2471 lv2_atom_forge_property_head(forge, urids.time_bar, 0);
2472 lv2_atom_forge_long(forge, bbt.bars - 1);
2473 lv2_atom_forge_property_head(forge, urids.time_beatUnit, 0);
2474 lv2_atom_forge_int(forge, t.meter().note_divisor());
2475 lv2_atom_forge_property_head(forge, urids.time_beatsPerBar, 0);
2476 lv2_atom_forge_float(forge, t.meter().divisions_per_bar());
2477 lv2_atom_forge_property_head(forge, urids.time_beatsPerMinute, 0);
2478 lv2_atom_forge_float(forge, bpm);
2481 LV2_Evbuf_Iterator end = lv2_evbuf_end(buf);
2482 const LV2_Atom* const atom = (const LV2_Atom*)pos_buf;
2483 return lv2_evbuf_write(&end, offset, 0, atom->type, atom->size,
2484 (const uint8_t*)(atom + 1));
2488 LV2Plugin::connect_and_run(BufferSet& bufs,
2489 framepos_t start, framepos_t end, double speed,
2490 ChanMapping in_map, ChanMapping out_map,
2491 pframes_t nframes, framecnt_t offset)
2493 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 run %2 offset %3\n", name(), nframes, offset));
2494 Plugin::connect_and_run(bufs, start, end, speed, in_map, out_map, nframes, offset);
2496 cycles_t then = get_cycles();
2498 TempoMap& tmap = _session.tempo_map();
2499 Metrics::const_iterator metric_i = tmap.metrics_end();
2500 TempoMetric tmetric = tmap.metric_at(start, &metric_i);
2502 if (_freewheel_control_port) {
2503 *_freewheel_control_port = _session.engine().freewheeling() ? 1.f : 0.f;
2506 if (_bpm_control_port) {
2507 *_bpm_control_port = tmap.tempo_at_frame (start).note_types_per_minute();
2511 if (_can_write_automation && start != _next_cycle_start) {
2512 // add guard-points after locating
2513 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2514 i->second->guard = true;
2519 ChanCount bufs_count;
2520 bufs_count.set(DataType::AUDIO, 1);
2521 bufs_count.set(DataType::MIDI, 1);
2522 BufferSet& silent_bufs = _session.get_silent_buffers(bufs_count);
2523 BufferSet& scratch_bufs = _session.get_scratch_buffers(bufs_count);
2524 uint32_t const num_ports = parameter_count();
2525 uint32_t const nil_index = std::numeric_limits<uint32_t>::max();
2527 uint32_t audio_in_index = 0;
2528 uint32_t audio_out_index = 0;
2529 uint32_t midi_in_index = 0;
2530 uint32_t midi_out_index = 0;
2531 uint32_t atom_port_index = 0;
2532 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2534 uint32_t index = nil_index;
2535 PortFlags flags = _port_flags[port_index];
2537 if (flags & PORT_AUDIO) {
2538 if (flags & PORT_INPUT) {
2539 index = in_map.get(DataType::AUDIO, audio_in_index++, &valid);
2541 ? bufs.get_audio(index).data(offset)
2542 : silent_bufs.get_audio(0).data(offset);
2544 index = out_map.get(DataType::AUDIO, audio_out_index++, &valid);
2546 ? bufs.get_audio(index).data(offset)
2547 : scratch_bufs.get_audio(0).data(offset);
2549 } else if (flags & (PORT_EVENT|PORT_SEQUENCE)) {
2550 /* FIXME: The checks here for bufs.count().n_midi() > index shouldn't
2551 be necessary, but the mapping is illegal in some cases. Ideally
2552 that should be fixed, but this is easier...
2554 if (flags & PORT_MIDI) {
2555 if (flags & PORT_INPUT) {
2556 index = in_map.get(DataType::MIDI, midi_in_index++, &valid);
2558 index = out_map.get(DataType::MIDI, midi_out_index++, &valid);
2560 if (valid && bufs.count().n_midi() > index) {
2561 /* Note, ensure_lv2_bufsize() is not RT safe!
2562 * However free()/alloc() is only called if a
2563 * plugin requires a rsz:minimumSize buffersize
2564 * and the existing buffer if smaller.
2566 bufs.ensure_lv2_bufsize((flags & PORT_INPUT), index, _port_minimumSize[port_index]);
2567 _ev_buffers[port_index] = bufs.get_lv2_midi(
2568 (flags & PORT_INPUT), index, (flags & PORT_EVENT));
2570 } else if ((flags & PORT_POSITION) && (flags & PORT_INPUT)) {
2571 lv2_evbuf_reset(_atom_ev_buffers[atom_port_index], true);
2572 _ev_buffers[port_index] = _atom_ev_buffers[atom_port_index++];
2576 if (valid && (flags & PORT_INPUT)) {
2577 if ((flags & PORT_POSITION)) {
2578 Timecode::BBT_Time bbt (tmap.bbt_at_frame (start));
2579 double bpm = tmap.tempo_at_frame (start).note_types_per_minute();
2580 double beatpos = (bbt.bars - 1) * tmetric.meter().divisions_per_bar()
2582 + (bbt.ticks / Timecode::BBT_Time::ticks_per_beat);
2583 beatpos *= tmetric.meter().note_divisor() / 4.0;
2584 if (start != _next_cycle_start ||
2585 speed != _next_cycle_speed ||
2586 rint (1000 * beatpos) != rint(1000 * _next_cycle_beat) ||
2587 bpm != _current_bpm) {
2588 // Transport or Tempo has changed, write position at cycle start
2589 write_position(&_impl->forge, _ev_buffers[port_index],
2590 tmetric, bbt, speed, bpm, start, 0);
2594 // Get MIDI iterator range (empty range if no MIDI)
2595 MidiBuffer::iterator m = (index != nil_index)
2596 ? bufs.get_midi(index).begin()
2597 : silent_bufs.get_midi(0).end();
2598 MidiBuffer::iterator m_end = (index != nil_index)
2599 ? bufs.get_midi(index).end()
2602 // Now merge MIDI and any transport events into the buffer
2603 const uint32_t type = _uri_map.urids.midi_MidiEvent;
2604 const framepos_t tend = end;
2606 while (m != m_end || (metric_i != tmap.metrics_end() &&
2607 (*metric_i)->frame() < tend)) {
2608 MetricSection* metric = (metric_i != tmap.metrics_end())
2610 if (m != m_end && (!metric || metric->frame() > (*m).time())) {
2611 const Evoral::Event<framepos_t> ev(*m, false);
2612 if (ev.time() < nframes) {
2613 LV2_Evbuf_Iterator eend = lv2_evbuf_end(_ev_buffers[port_index]);
2614 lv2_evbuf_write(&eend, ev.time(), 0, type, ev.size(), ev.buffer());
2618 tmetric.set_metric(metric);
2619 Timecode::BBT_Time bbt;
2620 bbt = tmap.bbt_at_frame (metric->frame());
2621 double bpm = tmap.tempo_at_frame (start/*XXX*/).note_types_per_minute();
2622 write_position(&_impl->forge, _ev_buffers[port_index],
2623 tmetric, bbt, speed, bpm,
2625 metric->frame() - start);
2629 } else if (!valid) {
2630 // Nothing we understand or care about, connect to scratch
2631 // see note for midi-buffer size above
2632 scratch_bufs.ensure_lv2_bufsize((flags & PORT_INPUT),
2633 0, _port_minimumSize[port_index]);
2634 _ev_buffers[port_index] = scratch_bufs.get_lv2_midi(
2635 (flags & PORT_INPUT), 0, (flags & PORT_EVENT));
2638 buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
2640 continue; // Control port, leave buffer alone
2642 lilv_instance_connect_port(_impl->instance, port_index, buf);
2645 // Read messages from UI and push into appropriate buffers
2647 uint32_t read_space = _from_ui->read_space();
2648 while (read_space > sizeof(UIMessage)) {
2650 if (_from_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
2651 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
2654 vector<uint8_t> body(msg.size);
2655 if (_from_ui->read(&body[0], msg.size) != msg.size) {
2656 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
2659 if (msg.protocol == URIMap::instance().urids.atom_eventTransfer) {
2660 LV2_Evbuf* buf = _ev_buffers[msg.index];
2661 LV2_Evbuf_Iterator i = lv2_evbuf_end(buf);
2662 const LV2_Atom* const atom = (const LV2_Atom*)&body[0];
2663 if (!lv2_evbuf_write(&i, nframes - 1, 0, atom->type, atom->size,
2664 (const uint8_t*)(atom + 1))) {
2665 error << "Failed to write data to LV2 event buffer\n";
2668 error << "Received unknown message type from UI" << endmsg;
2670 read_space -= sizeof(UIMessage) + msg.size;
2677 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2678 PortFlags flags = _port_flags[port_index];
2681 /* TODO ask drobilla about comment
2682 * "Make Ardour event buffers generic so plugins can communicate"
2683 * in libs/ardour/buffer_set.cc:310
2685 * ideally the user could choose which of the following two modes
2686 * to use (e.g. instrument/effect chains MIDI OUT vs MIDI TRHU).
2688 * This implementation follows the discussion on IRC Mar 16 2013 16:47 UTC
2689 * 16:51 < drobilla> rgareus: [..] i.e always replace with MIDI output [of LV2 plugin] if it's there
2690 * 16:52 < drobilla> rgareus: That would probably be good enough [..] to make users not complain
2691 * for quite a while at least ;)
2693 // copy output of LV2 plugin's MIDI port to Ardour MIDI buffers -- MIDI OUT
2694 if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE|PORT_MIDI))) {
2695 const uint32_t buf_index = out_map.get(
2696 DataType::MIDI, midi_out_index++, &valid);
2698 bufs.forward_lv2_midi(_ev_buffers[port_index], buf_index);
2701 // Flush MIDI (write back to Ardour MIDI buffers) -- MIDI THRU
2702 else if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE))) {
2703 const uint32_t buf_index = out_map.get(
2704 DataType::MIDI, midi_out_index++, &valid);
2706 bufs.flush_lv2_midi(true, buf_index);
2710 // Write messages to UI
2711 if ((_to_ui || _can_write_automation || _patch_port_out_index != (uint32_t)-1) &&
2712 (flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE))) {
2713 LV2_Evbuf* buf = _ev_buffers[port_index];
2714 for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(buf);
2715 lv2_evbuf_is_valid(i);
2716 i = lv2_evbuf_next(i)) {
2717 uint32_t frames, subframes, type, size;
2719 lv2_evbuf_get(i, &frames, &subframes, &type, &size, &data);
2722 // Intercept Automation Write Events
2723 if ((flags & PORT_AUTOCTRL)) {
2724 LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2725 if (atom->type == _uri_map.urids.atom_Blank ||
2726 atom->type == _uri_map.urids.atom_Object) {
2727 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2728 if (obj->body.otype == _uri_map.urids.auto_event) {
2729 // only if transport_rolling ??
2730 const LV2_Atom* parameter = NULL;
2731 const LV2_Atom* value = NULL;
2732 lv2_atom_object_get(obj,
2733 _uri_map.urids.auto_parameter, ¶meter,
2734 _uri_map.urids.auto_value, &value,
2736 if (parameter && value) {
2737 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2738 const float v = ((const LV2_Atom_Float*)value)->body;
2739 // -> add automation event..
2740 DEBUG_TRACE(DEBUG::LV2Automate,
2741 string_compose ("Event p: %1 t: %2 v: %3\n", p, frames, v));
2742 AutomationCtrlPtr c = get_automation_control (p);
2744 (c->ac->automation_state() == Touch || c->ac->automation_state() == Write)
2746 framepos_t when = std::max ((framepos_t) 0, start + frames - _current_latency);
2747 assert (start + frames - _current_latency >= 0);
2750 c->ac->list()->add (when, v, true, true);
2752 c->ac->set_double (v, when, true);
2757 else if (obj->body.otype == _uri_map.urids.auto_setup) {
2758 // TODO optional arguments, for now we assume the plugin
2759 // writes automation for its own inputs
2760 // -> put them in "touch" mode (preferably "exclusive plugin touch(TM)"
2761 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2762 if (_port_flags[i->first] & PORT_CTRLED) {
2763 DEBUG_TRACE(DEBUG::LV2Automate,
2764 string_compose ("Setup p: %1\n", i->first));
2765 i->second->ac->set_automation_state (Touch);
2769 else if (obj->body.otype == _uri_map.urids.auto_finalize) {
2770 // set [touched] parameters to "play" ??
2771 // allow plugin to change its mode (from analyze to apply)
2772 const LV2_Atom* parameter = NULL;
2773 const LV2_Atom* value = NULL;
2774 lv2_atom_object_get(obj,
2775 _uri_map.urids.auto_parameter, ¶meter,
2776 _uri_map.urids.auto_value, &value,
2778 if (parameter && value) {
2779 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2780 const float v = ((const LV2_Atom_Float*)value)->body;
2781 AutomationCtrlPtr c = get_automation_control (p);
2782 DEBUG_TRACE(DEBUG::LV2Automate,
2783 string_compose ("Finalize p: %1 v: %2\n", p, v));
2784 if (c && _port_flags[p] & PORT_CTRLER) {
2785 c->ac->set_value(v, Controllable::NoGroup);
2788 DEBUG_TRACE(DEBUG::LV2Automate, "Finalize\n");
2790 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2791 // guard will be false if an event was written
2792 if ((_port_flags[i->first] & PORT_CTRLED) && !i->second->guard) {
2793 DEBUG_TRACE(DEBUG::LV2Automate,
2794 string_compose ("Thin p: %1\n", i->first));
2795 i->second->ac->alist ()->thin (20);
2799 else if (obj->body.otype == _uri_map.urids.auto_start) {
2800 const LV2_Atom* parameter = NULL;
2801 lv2_atom_object_get(obj,
2802 _uri_map.urids.auto_parameter, ¶meter,
2805 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2806 AutomationCtrlPtr c = get_automation_control (p);
2807 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("Start Touch p: %1\n", p));
2809 c->ac->start_touch (std::max ((framepos_t)0, start - _current_latency));
2814 else if (obj->body.otype == _uri_map.urids.auto_end) {
2815 const LV2_Atom* parameter = NULL;
2816 lv2_atom_object_get(obj,
2817 _uri_map.urids.auto_parameter, ¶meter,
2820 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2821 AutomationCtrlPtr c = get_automation_control (p);
2822 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("End Touch p: %1\n", p));
2824 c->ac->stop_touch (std::max ((framepos_t)0, start - _current_latency));
2831 // Intercept state dirty message
2832 if (_has_state_interface /* && (flags & PORT_DIRTYMSG)*/) {
2833 LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2834 if (atom->type == _uri_map.urids.atom_Blank ||
2835 atom->type == _uri_map.urids.atom_Object) {
2836 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2837 if (obj->body.otype == _uri_map.urids.state_StateChanged) {
2838 _session.set_dirty ();
2843 // Intercept patch change messages to emit PropertyChanged signal
2844 if ((flags & PORT_PATCHMSG)) {
2845 LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2846 if (atom->type == _uri_map.urids.atom_Blank ||
2847 atom->type == _uri_map.urids.atom_Object) {
2848 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2849 if (obj->body.otype == _uri_map.urids.patch_Set) {
2850 const LV2_Atom* property = NULL;
2851 const LV2_Atom* value = NULL;
2852 lv2_atom_object_get(obj,
2853 _uri_map.urids.patch_property, &property,
2854 _uri_map.urids.patch_value, &value,
2857 if (property && value &&
2858 property->type == _uri_map.urids.atom_URID &&
2859 value->type == _uri_map.urids.atom_Path) {
2860 const uint32_t prop_id = ((const LV2_Atom_URID*)property)->body;
2861 const char* path = (const char*)LV2_ATOM_BODY_CONST(value);
2863 // Emit PropertyChanged signal for UI
2864 // TODO: This should emit the control's Changed signal
2865 PropertyChanged(prop_id, Variant(Variant::PATH, path));
2867 std::cerr << "warning: patch:Set for unknown property" << std::endl;
2873 if (!_to_ui) continue;
2874 write_to_ui(port_index, URIMap::instance().urids.atom_eventTransfer,
2875 size + sizeof(LV2_Atom),
2876 data - sizeof(LV2_Atom));
2881 cycles_t now = get_cycles();
2882 set_cycles((uint32_t)(now - then));
2884 // Update expected transport information for next cycle so we can detect changes
2885 _next_cycle_speed = speed;
2886 _next_cycle_start = end;
2889 /* keep track of lv2:timePosition like plugins can do.
2890 * Note: for no-midi plugins, we only ever send information at cycle-start,
2891 * so it needs to be realative to that.
2893 TempoMetric t = tmap.metric_at(start);
2894 _current_bpm = tmap.tempo_at_frame (start).note_types_per_minute();
2895 Timecode::BBT_Time bbt (tmap.bbt_at_frame (start));
2896 double beatpos = (bbt.bars - 1) * t.meter().divisions_per_bar()
2898 + (bbt.ticks / Timecode::BBT_Time::ticks_per_beat);
2899 beatpos *= tmetric.meter().note_divisor() / 4.0;
2900 _next_cycle_beat = beatpos + nframes * speed * _current_bpm / (60.f * _session.frame_rate());
2903 if (_latency_control_port) {
2904 framecnt_t new_latency = signal_latency ();
2905 _current_latency = new_latency;
2911 LV2Plugin::parameter_is_control(uint32_t param) const
2913 assert(param < _port_flags.size());
2914 return _port_flags[param] & PORT_CONTROL;
2918 LV2Plugin::parameter_is_audio(uint32_t param) const
2920 assert(param < _port_flags.size());
2921 return _port_flags[param] & PORT_AUDIO;
2925 LV2Plugin::parameter_is_event(uint32_t param) const
2927 assert(param < _port_flags.size());
2928 return _port_flags[param] & PORT_EVENT;
2932 LV2Plugin::parameter_is_output(uint32_t param) const
2934 assert(param < _port_flags.size());
2935 return _port_flags[param] & PORT_OUTPUT;
2939 LV2Plugin::parameter_is_input(uint32_t param) const
2941 assert(param < _port_flags.size());
2942 return _port_flags[param] & PORT_INPUT;
2946 LV2Plugin::designated_bypass_port ()
2948 const LilvPort* port = NULL;
2949 LilvNode* designation = lilv_new_uri (_world.world, LV2_CORE_PREFIX "enabled");
2950 port = lilv_plugin_get_port_by_designation (
2951 _impl->plugin, _world.lv2_InputPort, designation);
2952 lilv_node_free(designation);
2954 return lilv_port_get_index (_impl->plugin, port);
2957 /* deprecated on 2016-Sep-18 in favor of lv2:enabled */
2958 designation = lilv_new_uri (_world.world, LV2_PROCESSING_URI__enable);
2959 port = lilv_plugin_get_port_by_designation (
2960 _impl->plugin, _world.lv2_InputPort, designation);
2961 lilv_node_free(designation);
2963 return lilv_port_get_index (_impl->plugin, port);
2970 LV2Plugin::print_parameter(uint32_t param, char* buf, uint32_t len) const
2973 if (param < parameter_count()) {
2974 snprintf(buf, len, "%.3f", get_parameter(param));
2981 boost::shared_ptr<ScalePoints>
2982 LV2Plugin::get_scale_points(uint32_t port_index) const
2984 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, port_index);
2985 LilvScalePoints* points = lilv_port_get_scale_points(_impl->plugin, port);
2987 boost::shared_ptr<ScalePoints> ret;
2992 ret = boost::shared_ptr<ScalePoints>(new ScalePoints());
2994 LILV_FOREACH(scale_points, i, points) {
2995 const LilvScalePoint* p = lilv_scale_points_get(points, i);
2996 const LilvNode* label = lilv_scale_point_get_label(p);
2997 const LilvNode* value = lilv_scale_point_get_value(p);
2998 if (label && (lilv_node_is_float(value) || lilv_node_is_int(value))) {
2999 ret->insert(make_pair(lilv_node_as_string(label),
3000 lilv_node_as_float(value)));
3004 lilv_scale_points_free(points);
3009 LV2Plugin::run(pframes_t nframes, bool sync_work)
3011 uint32_t const N = parameter_count();
3012 for (uint32_t i = 0; i < N; ++i) {
3013 if (parameter_is_control(i) && parameter_is_input(i)) {
3014 _control_data[i] = _shadow_data[i];
3019 // Execute work synchronously if we're freewheeling (export)
3020 _worker->set_synchronous(sync_work || session().engine().freewheeling());
3023 // Run the plugin for this cycle
3024 lilv_instance_run(_impl->instance, nframes);
3026 // Emit any queued worker responses (calls a plugin callback)
3027 if (_state_worker) {
3028 _state_worker->emit_responses();
3031 _worker->emit_responses();
3034 // Notify the plugin that a work run cycle is complete
3035 if (_impl->work_iface) {
3036 if (_impl->work_iface->end_run) {
3037 _impl->work_iface->end_run(_impl->instance->lv2_handle);
3043 LV2Plugin::latency_compute_run()
3045 if (!_latency_control_port) {
3049 // Run the plugin so that it can set its latency parameter
3051 bool was_activated = _was_activated;
3054 uint32_t port_index = 0;
3055 uint32_t in_index = 0;
3056 uint32_t out_index = 0;
3058 // this is done in the main thread. non realtime.
3059 const framecnt_t bufsize = _engine.samples_per_cycle();
3060 float *buffer = (float*) malloc(_engine.samples_per_cycle() * sizeof(float));
3062 memset(buffer, 0, sizeof(float) * bufsize);
3064 // FIXME: Ensure plugins can handle in-place processing
3068 while (port_index < parameter_count()) {
3069 if (parameter_is_audio(port_index)) {
3070 if (parameter_is_input(port_index)) {
3071 lilv_instance_connect_port(_impl->instance, port_index, buffer);
3073 } else if (parameter_is_output(port_index)) {
3074 lilv_instance_connect_port(_impl->instance, port_index, buffer);
3083 if (was_activated) {
3090 LV2Plugin::Impl::designated_input (const char* uri, void** bufptrs[], void** bufptr)
3092 const LilvPort* port = NULL;
3093 LilvNode* designation = lilv_new_uri(_world.world, uri);
3094 port = lilv_plugin_get_port_by_designation(
3095 plugin, _world.lv2_InputPort, designation);
3096 lilv_node_free(designation);
3098 bufptrs[lilv_port_get_index(plugin, port)] = bufptr;
3103 static bool lv2_filter (const string& str, void* /*arg*/)
3105 /* Not a dotfile, has a prefix before a period, suffix is "lv2" */
3107 return str[0] != '.' && (str.length() > 3 && str.find (".lv2") == (str.length() - 4));
3111 LV2World::LV2World()
3112 : world(lilv_world_new())
3113 , _bundle_checked(false)
3115 atom_AtomPort = lilv_new_uri(world, LV2_ATOM__AtomPort);
3116 atom_Chunk = lilv_new_uri(world, LV2_ATOM__Chunk);
3117 atom_Sequence = lilv_new_uri(world, LV2_ATOM__Sequence);
3118 atom_bufferType = lilv_new_uri(world, LV2_ATOM__bufferType);
3119 atom_supports = lilv_new_uri(world, LV2_ATOM__supports);
3120 atom_eventTransfer = lilv_new_uri(world, LV2_ATOM__eventTransfer);
3121 ev_EventPort = lilv_new_uri(world, LILV_URI_EVENT_PORT);
3122 ext_logarithmic = lilv_new_uri(world, LV2_PORT_PROPS__logarithmic);
3123 ext_notOnGUI = lilv_new_uri(world, LV2_PORT_PROPS__notOnGUI);
3124 ext_expensive = lilv_new_uri(world, LV2_PORT_PROPS__expensive);
3125 ext_causesArtifacts= lilv_new_uri(world, LV2_PORT_PROPS__causesArtifacts);
3126 ext_notAutomatic = lilv_new_uri(world, LV2_PORT_PROPS__notAutomatic);
3127 ext_rangeSteps = lilv_new_uri(world, LV2_PORT_PROPS__rangeSteps);
3128 groups_group = lilv_new_uri(world, LV2_PORT_GROUPS__group);
3129 groups_element = lilv_new_uri(world, LV2_PORT_GROUPS__element);
3130 lv2_AudioPort = lilv_new_uri(world, LILV_URI_AUDIO_PORT);
3131 lv2_ControlPort = lilv_new_uri(world, LILV_URI_CONTROL_PORT);
3132 lv2_InputPort = lilv_new_uri(world, LILV_URI_INPUT_PORT);
3133 lv2_OutputPort = lilv_new_uri(world, LILV_URI_OUTPUT_PORT);
3134 lv2_inPlaceBroken = lilv_new_uri(world, LV2_CORE__inPlaceBroken);
3135 lv2_isSideChain = lilv_new_uri(world, LV2_CORE_PREFIX "isSideChain");
3136 lv2_index = lilv_new_uri(world, LV2_CORE__index);
3137 lv2_integer = lilv_new_uri(world, LV2_CORE__integer);
3138 lv2_default = lilv_new_uri(world, LV2_CORE__default);
3139 lv2_minimum = lilv_new_uri(world, LV2_CORE__minimum);
3140 lv2_maximum = lilv_new_uri(world, LV2_CORE__maximum);
3141 lv2_reportsLatency = lilv_new_uri(world, LV2_CORE__reportsLatency);
3142 lv2_sampleRate = lilv_new_uri(world, LV2_CORE__sampleRate);
3143 lv2_toggled = lilv_new_uri(world, LV2_CORE__toggled);
3144 lv2_designation = lilv_new_uri(world, LV2_CORE__designation);
3145 lv2_enumeration = lilv_new_uri(world, LV2_CORE__enumeration);
3146 lv2_freewheeling = lilv_new_uri(world, LV2_CORE__freeWheeling);
3147 midi_MidiEvent = lilv_new_uri(world, LILV_URI_MIDI_EVENT);
3148 rdfs_comment = lilv_new_uri(world, LILV_NS_RDFS "comment");
3149 rdfs_label = lilv_new_uri(world, LILV_NS_RDFS "label");
3150 rdfs_range = lilv_new_uri(world, LILV_NS_RDFS "range");
3151 rsz_minimumSize = lilv_new_uri(world, LV2_RESIZE_PORT__minimumSize);
3152 time_Position = lilv_new_uri(world, LV2_TIME__Position);
3153 ui_GtkUI = lilv_new_uri(world, LV2_UI__GtkUI);
3154 ui_external = lilv_new_uri(world, "http://lv2plug.in/ns/extensions/ui#external");
3155 ui_externalkx = lilv_new_uri(world, "http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget");
3156 units_unit = lilv_new_uri(world, LV2_UNITS__unit);
3157 units_render = lilv_new_uri(world, LV2_UNITS__render);
3158 units_hz = lilv_new_uri(world, LV2_UNITS__hz);
3159 units_midiNote = lilv_new_uri(world, LV2_UNITS__midiNote);
3160 units_db = lilv_new_uri(world, LV2_UNITS__db);
3161 patch_writable = lilv_new_uri(world, LV2_PATCH__writable);
3162 patch_Message = lilv_new_uri(world, LV2_PATCH__Message);
3164 lv2_noSampleAccurateCtrl = lilv_new_uri(world, "http://ardour.org/lv2/ext#noSampleAccurateControls"); // deprecated 2016-09-18
3165 auto_can_write_automatation = lilv_new_uri(world, LV2_AUTOMATE_URI__can_write);
3166 auto_automation_control = lilv_new_uri(world, LV2_AUTOMATE_URI__control);
3167 auto_automation_controlled = lilv_new_uri(world, LV2_AUTOMATE_URI__controlled);
3168 auto_automation_controller = lilv_new_uri(world, LV2_AUTOMATE_URI__controller);
3169 inline_display_in_gui = lilv_new_uri(world, LV2_INLINEDISPLAY__in_gui);
3171 #ifdef HAVE_LV2_1_2_0
3172 bufz_powerOf2BlockLength = lilv_new_uri(world, LV2_BUF_SIZE__powerOf2BlockLength);
3173 bufz_fixedBlockLength = lilv_new_uri(world, LV2_BUF_SIZE__fixedBlockLength);
3174 bufz_nominalBlockLength = lilv_new_uri(world, "http://lv2plug.in/ns/ext/buf-size#nominalBlockLength");
3175 bufz_coarseBlockLength = lilv_new_uri(world, "http://lv2plug.in/ns/ext/buf-size#coarseBlockLength");
3180 LV2World::~LV2World()
3185 #ifdef HAVE_LV2_1_2_0
3186 lilv_node_free(bufz_coarseBlockLength);
3187 lilv_node_free(bufz_nominalBlockLength);
3188 lilv_node_free(bufz_fixedBlockLength);
3189 lilv_node_free(bufz_powerOf2BlockLength);
3192 lilv_node_free(lv2_noSampleAccurateCtrl);
3193 lilv_node_free(auto_can_write_automatation);
3194 lilv_node_free(auto_automation_control);
3195 lilv_node_free(auto_automation_controlled);
3196 lilv_node_free(auto_automation_controller);
3198 lilv_node_free(patch_Message);
3199 lilv_node_free(patch_writable);
3200 lilv_node_free(units_hz);
3201 lilv_node_free(units_midiNote);
3202 lilv_node_free(units_db);
3203 lilv_node_free(units_unit);
3204 lilv_node_free(units_render);
3205 lilv_node_free(ui_externalkx);
3206 lilv_node_free(ui_external);
3207 lilv_node_free(ui_GtkUI);
3208 lilv_node_free(time_Position);
3209 lilv_node_free(rsz_minimumSize);
3210 lilv_node_free(rdfs_comment);
3211 lilv_node_free(rdfs_label);
3212 lilv_node_free(rdfs_range);
3213 lilv_node_free(midi_MidiEvent);
3214 lilv_node_free(lv2_designation);
3215 lilv_node_free(lv2_enumeration);
3216 lilv_node_free(lv2_freewheeling);
3217 lilv_node_free(lv2_toggled);
3218 lilv_node_free(lv2_sampleRate);
3219 lilv_node_free(lv2_reportsLatency);
3220 lilv_node_free(lv2_index);
3221 lilv_node_free(lv2_integer);
3222 lilv_node_free(lv2_isSideChain);
3223 lilv_node_free(lv2_inPlaceBroken);
3224 lilv_node_free(lv2_OutputPort);
3225 lilv_node_free(lv2_InputPort);
3226 lilv_node_free(lv2_ControlPort);
3227 lilv_node_free(lv2_AudioPort);
3228 lilv_node_free(groups_group);
3229 lilv_node_free(groups_element);
3230 lilv_node_free(ext_rangeSteps);
3231 lilv_node_free(ext_notAutomatic);
3232 lilv_node_free(ext_causesArtifacts);
3233 lilv_node_free(ext_expensive);
3234 lilv_node_free(ext_notOnGUI);
3235 lilv_node_free(ext_logarithmic);
3236 lilv_node_free(ev_EventPort);
3237 lilv_node_free(atom_supports);
3238 lilv_node_free(atom_eventTransfer);
3239 lilv_node_free(atom_bufferType);
3240 lilv_node_free(atom_Sequence);
3241 lilv_node_free(atom_Chunk);
3242 lilv_node_free(atom_AtomPort);
3243 lilv_world_free(world);
3248 LV2World::load_bundled_plugins(bool verbose)
3250 if (!_bundle_checked) {
3252 cout << "Scanning folders for bundled LV2s: " << ARDOUR::lv2_bundled_search_path().to_string() << endl;
3255 vector<string> plugin_objects;
3256 find_paths_matching_filter (plugin_objects, ARDOUR::lv2_bundled_search_path(), lv2_filter, 0, true, true, true);
3257 for ( vector<string>::iterator x = plugin_objects.begin(); x != plugin_objects.end (); ++x) {
3258 #ifdef PLATFORM_WINDOWS
3259 string uri = "file:///" + *x + "/";
3261 string uri = "file://" + *x + "/";
3263 LilvNode *node = lilv_new_uri(world, uri.c_str());
3264 lilv_world_load_bundle(world, node);
3265 lilv_node_free(node);
3268 lilv_world_load_all(world);
3269 _bundle_checked = true;
3273 LV2PluginInfo::LV2PluginInfo (const char* plugin_uri)
3276 _plugin_uri = strdup(plugin_uri);
3279 LV2PluginInfo::~LV2PluginInfo()
3286 LV2PluginInfo::load(Session& session)
3290 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
3291 LilvNode* uri = lilv_new_uri(_world.world, _plugin_uri);
3292 if (!uri) { throw failed_constructor(); }
3293 const LilvPlugin* lp = lilv_plugins_get_by_uri(plugins, uri);
3294 if (!lp) { throw failed_constructor(); }
3295 plugin.reset(new LV2Plugin(session.engine(), session, lp, session.frame_rate()));
3296 lilv_node_free(uri);
3297 plugin->set_info(PluginInfoPtr(shared_from_this ()));
3299 } catch (failed_constructor& err) {
3300 return PluginPtr((Plugin*)0);
3306 std::vector<Plugin::PresetRecord>
3307 LV2PluginInfo::get_presets (bool /*user_only*/) const
3309 std::vector<Plugin::PresetRecord> p;
3310 #ifndef NO_PLUGIN_STATE
3311 const LilvPlugin* lp = NULL;
3314 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
3315 LilvNode* uri = lilv_new_uri(_world.world, _plugin_uri);
3316 if (!uri) { throw failed_constructor(); }
3317 lp = lilv_plugins_get_by_uri(plugins, uri);
3318 if (!lp) { throw failed_constructor(); }
3319 lilv_node_free(uri);
3320 } catch (failed_constructor& err) {
3324 // see LV2Plugin::find_presets
3325 LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
3326 LilvNode* pset_Preset = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
3327 LilvNode* rdfs_label = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
3329 LilvNodes* presets = lilv_plugin_get_related(lp, pset_Preset);
3330 LILV_FOREACH(nodes, i, presets) {
3331 const LilvNode* preset = lilv_nodes_get(presets, i);
3332 lilv_world_load_resource(_world.world, preset);
3333 LilvNode* name = get_value(_world.world, preset, rdfs_label);
3334 bool userpreset = true; // TODO
3336 p.push_back (Plugin::PresetRecord (lilv_node_as_string(preset), lilv_node_as_string(name), userpreset));
3337 lilv_node_free(name);
3340 lilv_nodes_free(presets);
3341 lilv_node_free(rdfs_label);
3342 lilv_node_free(pset_Preset);
3343 lilv_node_free(lv2_appliesTo);
3349 LV2PluginInfo::in_category (const std::string &c) const
3351 // TODO use untranslated lilv_plugin_get_class()
3352 // match gtk2_ardour/plugin_selector.cc
3353 return category == c;
3357 LV2PluginInfo::is_instrument () const
3359 if (category == "Instrument") {
3363 /* until we make sure that category remains untranslated in the lv2.ttl spec
3364 * and until most instruments also classify themselves as such, there's a 2nd check:
3366 if (n_inputs.n_midi() > 0 && n_inputs.n_audio() == 0 && n_outputs.n_audio() > 0) {
3374 LV2PluginInfo::discover()
3377 world.load_bundled_plugins();
3378 _world.load_bundled_plugins(true);
3380 PluginInfoList* plugs = new PluginInfoList;
3381 const LilvPlugins* plugins = lilv_world_get_all_plugins(world.world);
3383 LILV_FOREACH(plugins, i, plugins) {
3384 const LilvPlugin* p = lilv_plugins_get(plugins, i);
3385 const LilvNode* pun = lilv_plugin_get_uri(p);
3387 LV2PluginInfoPtr info(new LV2PluginInfo(lilv_node_as_string(pun)));
3389 LilvNode* name = lilv_plugin_get_name(p);
3390 if (!name || !lilv_plugin_get_port_by_index(p, 0)) {
3391 warning << "Ignoring invalid LV2 plugin "
3392 << lilv_node_as_string(lilv_plugin_get_uri(p))
3397 if (lilv_plugin_has_feature(p, world.lv2_inPlaceBroken)) {
3398 warning << string_compose(
3399 _("Ignoring LV2 plugin \"%1\" since it cannot do inplace processing."),
3400 lilv_node_as_string(name)) << endmsg;
3401 lilv_node_free(name);
3405 #ifdef HAVE_LV2_1_2_0
3406 LilvNodes *required_features = lilv_plugin_get_required_features (p);
3407 if (lilv_nodes_contains (required_features, world.bufz_powerOf2BlockLength) ||
3408 lilv_nodes_contains (required_features, world.bufz_fixedBlockLength)
3410 warning << string_compose(
3411 _("Ignoring LV2 plugin \"%1\" because its buffer-size requirements cannot be satisfied."),
3412 lilv_node_as_string(name)) << endmsg;
3413 lilv_nodes_free(required_features);
3414 lilv_node_free(name);
3417 lilv_nodes_free(required_features);
3422 info->name = string(lilv_node_as_string(name));
3423 lilv_node_free(name);
3424 ARDOUR::PluginScanMessage(_("LV2"), info->name, false);
3426 const LilvPluginClass* pclass = lilv_plugin_get_class(p);
3427 const LilvNode* label = lilv_plugin_class_get_label(pclass);
3428 info->category = lilv_node_as_string(label);
3430 LilvNode* author_name = lilv_plugin_get_author_name(p);
3431 info->creator = author_name ? string(lilv_node_as_string(author_name)) : "Unknown";
3432 lilv_node_free(author_name);
3434 info->path = "/NOPATH"; // Meaningless for LV2
3436 /* count atom-event-ports that feature
3437 * atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent>
3439 * TODO: nicely ask drobilla to make a lilv_ call for that
3441 int count_midi_out = 0;
3442 int count_midi_in = 0;
3443 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
3444 const LilvPort* port = lilv_plugin_get_port_by_index(p, i);
3445 if (lilv_port_is_a(p, port, world.atom_AtomPort)) {
3446 LilvNodes* buffer_types = lilv_port_get_value(
3447 p, port, world.atom_bufferType);
3448 LilvNodes* atom_supports = lilv_port_get_value(
3449 p, port, world.atom_supports);
3451 if (lilv_nodes_contains(buffer_types, world.atom_Sequence)
3452 && lilv_nodes_contains(atom_supports, world.midi_MidiEvent)) {
3453 if (lilv_port_is_a(p, port, world.lv2_InputPort)) {
3456 if (lilv_port_is_a(p, port, world.lv2_OutputPort)) {
3460 lilv_nodes_free(buffer_types);
3461 lilv_nodes_free(atom_supports);
3465 info->n_inputs.set_audio(
3466 lilv_plugin_get_num_ports_of_class(
3467 p, world.lv2_InputPort, world.lv2_AudioPort, NULL));
3468 info->n_inputs.set_midi(
3469 lilv_plugin_get_num_ports_of_class(
3470 p, world.lv2_InputPort, world.ev_EventPort, NULL)
3473 info->n_outputs.set_audio(
3474 lilv_plugin_get_num_ports_of_class(
3475 p, world.lv2_OutputPort, world.lv2_AudioPort, NULL));
3476 info->n_outputs.set_midi(
3477 lilv_plugin_get_num_ports_of_class(
3478 p, world.lv2_OutputPort, world.ev_EventPort, NULL)
3481 info->unique_id = lilv_node_as_uri(lilv_plugin_get_uri(p));
3482 info->index = 0; // Meaningless for LV2
3484 plugs->push_back(info);