2 Copyright (C) 2008-2012 Paul Davis
3 Author: David Robillard
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include "pbd/gstdio_compat.h"
30 #include <glib/gprintf.h>
33 #include <boost/utility.hpp>
35 #include "pbd/file_utils.h"
36 #include "pbd/stl_delete.h"
37 #include "pbd/compose.h"
38 #include "pbd/error.h"
39 #include "pbd/replace_all.h"
40 #include "pbd/xml++.h"
42 #include "libardour-config.h"
44 #include "ardour/audio_buffer.h"
45 #include "ardour/audioengine.h"
46 #include "ardour/debug.h"
47 #include "ardour/lv2_plugin.h"
48 #include "ardour/session.h"
49 #include "ardour/tempo.h"
50 #include "ardour/types.h"
51 #include "ardour/utils.h"
52 #include "ardour/worker.h"
53 #include "ardour/search_paths.h"
58 #include <lilv/lilv.h>
60 #include "lv2/lv2plug.in/ns/ext/atom/atom.h"
61 #include "lv2/lv2plug.in/ns/ext/atom/forge.h"
62 #include "lv2/lv2plug.in/ns/ext/log/log.h"
63 #include "lv2/lv2plug.in/ns/ext/midi/midi.h"
64 #include "lv2/lv2plug.in/ns/ext/port-props/port-props.h"
65 #include "lv2/lv2plug.in/ns/ext/presets/presets.h"
66 #include "lv2/lv2plug.in/ns/ext/state/state.h"
67 #include "lv2/lv2plug.in/ns/ext/time/time.h"
68 #include "lv2/lv2plug.in/ns/ext/worker/worker.h"
69 #include "lv2/lv2plug.in/ns/ext/resize-port/resize-port.h"
70 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
71 #include "lv2/lv2plug.in/ns/extensions/units/units.h"
72 #include "lv2/lv2plug.in/ns/ext/patch/patch.h"
74 #include "lv2/lv2plug.in/ns/ext/buf-size/buf-size.h"
75 #include "lv2/lv2plug.in/ns/ext/options/options.h"
78 #include "lv2_evbuf.h"
81 #include <suil/suil.h>
84 // Compatibility for old LV2
85 #ifndef LV2_ATOM_CONTENTS_CONST
86 #define LV2_ATOM_CONTENTS_CONST(type, atom) \
87 ((const void*)((const uint8_t*)(atom) + sizeof(type)))
89 #ifndef LV2_ATOM_BODY_CONST
90 #define LV2_ATOM_BODY_CONST(atom) LV2_ATOM_CONTENTS_CONST(LV2_Atom, atom)
92 #ifndef LV2_PATCH__property
93 #define LV2_PATCH__property LV2_PATCH_PREFIX "property"
95 #ifndef LV2_PATCH__value
96 #define LV2_PATCH__value LV2_PATCH_PREFIX "value"
98 #ifndef LV2_PATCH__writable
99 #define LV2_PATCH__writable LV2_PATCH_PREFIX "writable"
102 /** The number of MIDI buffers that will fit in a UI/worker comm buffer.
103 This needs to be roughly the number of cycles the UI will get around to
104 actually processing the traffic. Lower values are flakier but save memory.
106 static const size_t NBUFS = 4;
109 using namespace ARDOUR;
112 class LV2World : boost::noncopyable {
117 void load_bundled_plugins(bool verbose=false);
121 LilvNode* atom_AtomPort;
122 LilvNode* atom_Chunk;
123 LilvNode* atom_Sequence;
124 LilvNode* atom_bufferType;
125 LilvNode* atom_eventTransfer;
126 LilvNode* atom_supports;
127 LilvNode* ev_EventPort;
128 LilvNode* ext_logarithmic;
129 LilvNode* ext_notOnGUI;
130 LilvNode* ext_expensive;
131 LilvNode* ext_causesArtifacts;
132 LilvNode* ext_notAutomatic;
133 LilvNode* ext_rangeSteps;
134 LilvNode* lv2_AudioPort;
135 LilvNode* lv2_ControlPort;
136 LilvNode* lv2_InputPort;
137 LilvNode* lv2_OutputPort;
138 LilvNode* lv2_enumeration;
139 LilvNode* lv2_freewheeling;
140 LilvNode* lv2_inPlaceBroken;
141 LilvNode* lv2_isSideChain;
142 LilvNode* lv2_integer;
143 LilvNode* lv2_default;
144 LilvNode* lv2_minimum;
145 LilvNode* lv2_maximum;
146 LilvNode* lv2_reportsLatency;
147 LilvNode* lv2_sampleRate;
148 LilvNode* lv2_toggled;
149 LilvNode* midi_MidiEvent;
150 LilvNode* rdfs_comment;
151 LilvNode* rdfs_label;
152 LilvNode* rdfs_range;
153 LilvNode* rsz_minimumSize;
154 LilvNode* time_Position;
156 LilvNode* ui_external;
157 LilvNode* ui_externalkx;
160 LilvNode* units_unit;
161 LilvNode* units_render;
162 LilvNode* units_midiNote;
163 LilvNode* patch_writable;
164 LilvNode* patch_Message;
165 #ifdef HAVE_LV2_1_2_0
166 LilvNode* bufz_powerOf2BlockLength;
167 LilvNode* bufz_fixedBlockLength;
168 LilvNode* bufz_nominalBlockLength;
171 #ifdef HAVE_LV2_1_10_0
173 LilvNode* atom_float;
174 LilvNode* atom_object; // new in 1.8
175 LilvNode* atom_vector;
178 LilvNode* lv2_noSampleAccurateCtrl;
179 LilvNode* auto_can_write_automatation; // lv2:optionalFeature
180 LilvNode* auto_automation_control; // atom:supports
181 LilvNode* auto_automation_controlled; // lv2:portProperty
182 LilvNode* auto_automation_controller; // lv2:portProperty
186 bool _bundle_checked;
189 static LV2World _world;
191 /* worker extension */
193 /** Called by the plugin to schedule non-RT work. */
194 static LV2_Worker_Status
195 work_schedule(LV2_Worker_Schedule_Handle handle,
199 return (((Worker*)handle)->schedule(size, data)
201 : LV2_WORKER_ERR_UNKNOWN);
204 /** Called by the plugin to respond to non-RT work. */
205 static LV2_Worker_Status
206 work_respond(LV2_Worker_Respond_Handle handle,
210 return (((Worker*)handle)->respond(size, data)
212 : LV2_WORKER_ERR_UNKNOWN);
216 /* inline display extension */
218 queue_draw (LV2_Inline_Display_Handle handle)
220 LV2Plugin* plugin = (LV2Plugin*)handle;
221 plugin->QueueDraw(); /* EMIT SIGNAL */
228 log_vprintf(LV2_Log_Handle /*handle*/,
234 const int ret = g_vasprintf(&str, fmt, args);
235 /* strip trailing whitespace */
236 while (strlen (str) > 0 && isspace (str[strlen (str) - 1])) {
237 str[strlen (str) - 1] = '\0';
239 if (strlen (str) == 0) {
243 if (type == URIMap::instance().urids.log_Error) {
244 error << str << endmsg;
245 } else if (type == URIMap::instance().urids.log_Warning) {
246 warning << str << endmsg;
247 } else if (type == URIMap::instance().urids.log_Note) {
248 info << str << endmsg;
249 } else if (type == URIMap::instance().urids.log_Trace) {
250 DEBUG_TRACE(DEBUG::LV2, str);
256 log_printf(LV2_Log_Handle handle,
258 const char* fmt, ...)
262 const int ret = log_vprintf(handle, type, fmt, args);
267 struct LV2Plugin::Impl {
268 Impl() : plugin(0), ui(0), ui_type(0), name(0), author(0), instance(0)
270 #ifdef HAVE_LV2_1_2_0
275 #ifdef HAVE_LV2_1_2_0
283 /** Find the LV2 input port with the given designation.
284 * If found, bufptrs[port_index] will be set to bufptr.
286 const LilvPort* designated_input (const char* uri, void** bufptrs[], void** bufptr);
288 const LilvPlugin* plugin;
290 const LilvNode* ui_type;
293 LilvInstance* instance;
294 const LV2_Worker_Interface* work_iface;
295 #ifdef HAVE_LV2_1_2_0
296 const LV2_Options_Interface* opts_iface;
299 LV2_Atom_Forge forge;
300 LV2_Atom_Forge ui_forge;
301 int32_t block_length;
302 #ifdef HAVE_LV2_1_2_0
303 LV2_Options_Option* options;
306 LV2_Inline_Display* queue_draw;
310 LV2Plugin::LV2Plugin (AudioEngine& engine,
312 const void* c_plugin,
314 : Plugin (engine, session)
319 , _state_worker(NULL)
321 , _patch_port_in_index((uint32_t)-1)
322 , _patch_port_out_index((uint32_t)-1)
323 , _uri_map(URIMap::instance())
324 , _no_sample_accurate_ctrl (false)
326 init(c_plugin, rate);
329 LV2Plugin::LV2Plugin (const LV2Plugin& other)
335 , _state_worker(NULL)
336 , _insert_id(other._insert_id)
337 , _patch_port_in_index((uint32_t)-1)
338 , _patch_port_out_index((uint32_t)-1)
339 , _uri_map(URIMap::instance())
340 , _no_sample_accurate_ctrl (false)
342 init(other._impl->plugin, other._sample_rate);
344 for (uint32_t i = 0; i < parameter_count(); ++i) {
345 _control_data[i] = other._shadow_data[i];
346 _shadow_data[i] = other._shadow_data[i];
351 LV2Plugin::init(const void* c_plugin, framecnt_t rate)
353 DEBUG_TRACE(DEBUG::LV2, "init\n");
355 _impl->plugin = (const LilvPlugin*)c_plugin;
357 _impl->ui_type = NULL;
362 _atom_ev_buffers = 0;
364 _bpm_control_port = 0;
365 _freewheel_control_port = 0;
366 _latency_control_port = 0;
367 _next_cycle_start = std::numeric_limits<framepos_t>::max();
368 _next_cycle_speed = 1.0;
369 _seq_size = _engine.raw_buffer_size(DataType::MIDI);
371 _was_activated = false;
372 _has_state_interface = false;
373 _can_write_automation = false;
375 _current_latency = 0;
376 _impl->block_length = _session.get_block_size();
378 _instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
379 _data_access_feature.URI = "http://lv2plug.in/ns/ext/data-access";
380 _make_path_feature.URI = LV2_STATE__makePath;
381 _log_feature.URI = LV2_LOG__log;
382 _work_schedule_feature.URI = LV2_WORKER__schedule;
383 _work_schedule_feature.data = NULL;
384 _def_state_feature.URI = LV2_STATE_PREFIX "loadDefaultState"; // Post LV2-1.2.0
385 _def_state_feature.data = NULL;
387 const LilvPlugin* plugin = _impl->plugin;
389 LilvNode* state_iface_uri = lilv_new_uri(_world.world, LV2_STATE__interface);
390 LilvNode* state_uri = lilv_new_uri(_world.world, LV2_STATE_URI);
391 _has_state_interface =
392 // What plugins should have (lv2:extensionData state:Interface)
393 lilv_plugin_has_extension_data(plugin, state_iface_uri)
394 // What some outdated/incorrect ones have
395 || lilv_plugin_has_feature(plugin, state_uri);
396 lilv_node_free(state_uri);
397 lilv_node_free(state_iface_uri);
399 _features = (LV2_Feature**)calloc(12, sizeof(LV2_Feature*));
400 _features[0] = &_instance_access_feature;
401 _features[1] = &_data_access_feature;
402 _features[2] = &_make_path_feature;
403 _features[3] = _uri_map.uri_map_feature();
404 _features[4] = _uri_map.urid_map_feature();
405 _features[5] = _uri_map.urid_unmap_feature();
406 _features[6] = &_log_feature;
408 unsigned n_features = 7;
409 #ifdef HAVE_LV2_1_2_0
410 _features[n_features++] = &_def_state_feature;
413 lv2_atom_forge_init(&_impl->forge, _uri_map.urid_map());
414 lv2_atom_forge_init(&_impl->ui_forge, _uri_map.urid_map());
417 _impl->queue_draw = (LV2_Inline_Display*)
418 malloc (sizeof(LV2_Inline_Display));
419 _impl->queue_draw->handle = this;
420 _impl->queue_draw->queue_draw = queue_draw;
422 _queue_draw_feature.URI = LV2_INLINEDISPLAY__queue_draw;
423 _queue_draw_feature.data = _impl->queue_draw;
424 _features[n_features++] = &_queue_draw_feature;
427 #ifdef HAVE_LV2_1_2_0
428 LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
429 static const int32_t _min_block_length = 1; // may happen during split-cycles
430 static const int32_t _max_block_length = 8192; // max possible (with all engines and during export)
431 /* Consider updating max-block-size whenever the buffersize changes.
432 * It requires re-instantiating the plugin (which is a non-realtime operation),
433 * so it should be done lightly and only for plugins that require it.
435 * given that the block-size can change at any time (split-cycles) ardour currently
436 * does not support plugins that require bufz_fixedBlockLength.
438 LV2_Options_Option options[] = {
439 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__minBlockLength),
440 sizeof(int32_t), atom_Int, &_min_block_length },
441 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__maxBlockLength),
442 sizeof(int32_t), atom_Int, &_max_block_length },
443 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__sequenceSize),
444 sizeof(int32_t), atom_Int, &_seq_size },
445 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://lv2plug.in/ns/ext/buf-size#nominalBlockLength"),
446 sizeof(int32_t), atom_Int, &_impl->block_length },
447 { LV2_OPTIONS_INSTANCE, 0, 0, 0, 0, NULL }
450 _impl->options = (LV2_Options_Option*) malloc (sizeof (options));
451 memcpy ((void*) _impl->options, (void*) options, sizeof (options));
453 _options_feature.URI = LV2_OPTIONS__options;
454 _options_feature.data = _impl->options;
455 _features[n_features++] = &_options_feature;
458 LV2_State_Make_Path* make_path = (LV2_State_Make_Path*)malloc(
459 sizeof(LV2_State_Make_Path));
460 make_path->handle = this;
461 make_path->path = &lv2_state_make_path;
462 _make_path_feature.data = make_path;
464 LV2_Log_Log* log = (LV2_Log_Log*)malloc(sizeof(LV2_Log_Log));
466 log->printf = &log_printf;
467 log->vprintf = &log_vprintf;
468 _log_feature.data = log;
470 const size_t ring_size = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
471 LilvNode* worker_schedule = lilv_new_uri(_world.world, LV2_WORKER__schedule);
472 if (lilv_plugin_has_feature(plugin, worker_schedule)) {
473 LV2_Worker_Schedule* schedule = (LV2_Worker_Schedule*)malloc(
474 sizeof(LV2_Worker_Schedule));
475 _worker = new Worker(this, ring_size);
476 schedule->handle = _worker;
477 schedule->schedule_work = work_schedule;
478 _work_schedule_feature.data = schedule;
479 _features[n_features++] = &_work_schedule_feature;
481 lilv_node_free(worker_schedule);
483 if (_has_state_interface) {
484 // Create a non-threaded worker for use by state restore
485 _state_worker = new Worker(this, ring_size, false);
488 _impl->instance = lilv_plugin_instantiate(plugin, rate, _features);
489 _impl->name = lilv_plugin_get_name(plugin);
490 _impl->author = lilv_plugin_get_author_name(plugin);
492 if (_impl->instance == 0) {
493 error << _("LV2: Failed to instantiate plugin ") << uri() << endmsg;
494 throw failed_constructor();
497 _instance_access_feature.data = (void*)_impl->instance->lv2_handle;
498 _data_access_extension_data.extension_data = _impl->instance->lv2_descriptor->extension_data;
499 _data_access_feature.data = &_data_access_extension_data;
501 LilvNode* worker_iface_uri = lilv_new_uri(_world.world, LV2_WORKER__interface);
502 if (lilv_plugin_has_extension_data(plugin, worker_iface_uri)) {
503 _impl->work_iface = (const LV2_Worker_Interface*)extension_data(
504 LV2_WORKER__interface);
506 lilv_node_free(worker_iface_uri);
509 #ifdef HAVE_LV2_1_2_0
510 LilvNode* options_iface_uri = lilv_new_uri(_world.world, LV2_OPTIONS__interface);
511 if (lilv_plugin_has_extension_data(plugin, options_iface_uri)) {
512 _impl->opts_iface = (const LV2_Options_Interface*)extension_data(
513 LV2_OPTIONS__interface);
515 lilv_node_free(options_iface_uri);
519 _display_interface = (const LV2_Inline_Display_Interface*)
520 extension_data (LV2_INLINEDISPLAY__interface);
523 if (lilv_plugin_has_feature(plugin, _world.lv2_inPlaceBroken)) {
524 error << string_compose(
525 _("LV2: \"%1\" cannot be used, since it cannot do inplace processing."),
526 lilv_node_as_string(_impl->name)) << endmsg;
527 lilv_node_free(_impl->name);
528 lilv_node_free(_impl->author);
529 throw failed_constructor();
532 #ifdef HAVE_LV2_1_2_0
533 LilvNodes *required_features = lilv_plugin_get_required_features (plugin);
534 if (lilv_nodes_contains (required_features, _world.bufz_powerOf2BlockLength) ||
535 lilv_nodes_contains (required_features, _world.bufz_fixedBlockLength)
537 error << string_compose(
538 _("LV2: \"%1\" buffer-size requirements cannot be satisfied."),
539 lilv_node_as_string(_impl->name)) << endmsg;
540 lilv_node_free(_impl->name);
541 lilv_node_free(_impl->author);
542 lilv_nodes_free(required_features);
543 throw failed_constructor();
545 lilv_nodes_free(required_features);
549 LilvNodes* optional_features = lilv_plugin_get_optional_features (plugin);
550 if (lilv_nodes_contains (optional_features, _world.lv2_noSampleAccurateCtrl)) {
551 _no_sample_accurate_ctrl = true;
553 if (lilv_nodes_contains (optional_features, _world.auto_can_write_automatation)) {
554 _can_write_automation = true;
556 lilv_nodes_free(optional_features);
559 #ifdef HAVE_LILV_0_16_0
560 // Load default state
561 LilvState* state = lilv_state_new_from_world(
562 _world.world, _uri_map.urid_map(), lilv_plugin_get_uri(_impl->plugin));
563 if (state && _has_state_interface) {
564 lilv_state_restore(state, _impl->instance, NULL, NULL, 0, NULL);
566 lilv_state_free(state);
571 const uint32_t num_ports = this->num_ports();
572 for (uint32_t i = 0; i < num_ports; ++i) {
573 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, i);
575 size_t minimumSize = 0;
577 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_OutputPort)) {
578 flags |= PORT_OUTPUT;
579 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_InputPort)) {
582 error << string_compose(
583 "LV2: \"%1\" port %2 is neither input nor output",
584 lilv_node_as_string(_impl->name), i) << endmsg;
585 throw failed_constructor();
588 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_ControlPort)) {
589 flags |= PORT_CONTROL;
590 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_AudioPort)) {
592 } else if (lilv_port_is_a(_impl->plugin, port, _world.ev_EventPort)) {
594 flags |= PORT_MIDI; // We assume old event API ports are for MIDI
595 } else if (lilv_port_is_a(_impl->plugin, port, _world.atom_AtomPort)) {
596 LilvNodes* buffer_types = lilv_port_get_value(
597 _impl->plugin, port, _world.atom_bufferType);
598 LilvNodes* atom_supports = lilv_port_get_value(
599 _impl->plugin, port, _world.atom_supports);
601 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
602 flags |= PORT_SEQUENCE;
603 if (lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)) {
606 if (lilv_nodes_contains(atom_supports, _world.time_Position)) {
607 flags |= PORT_POSITION;
610 if (lilv_nodes_contains(atom_supports, _world.auto_automation_control)) {
611 flags |= PORT_AUTOCTRL;
614 if (lilv_nodes_contains(atom_supports, _world.patch_Message)) {
615 flags |= PORT_PATCHMSG;
616 if (flags & PORT_INPUT) {
617 _patch_port_in_index = i;
619 _patch_port_out_index = i;
623 LilvNodes* min_size_v = lilv_port_get_value(_impl->plugin, port, _world.rsz_minimumSize);
624 LilvNode* min_size = min_size_v ? lilv_nodes_get_first(min_size_v) : NULL;
625 if (min_size && lilv_node_is_int(min_size)) {
626 minimumSize = lilv_node_as_int(min_size);
628 lilv_nodes_free(min_size_v);
629 lilv_nodes_free(buffer_types);
630 lilv_nodes_free(atom_supports);
632 error << string_compose(
633 "LV2: \"%1\" port %2 has no known data type",
634 lilv_node_as_string(_impl->name), i) << endmsg;
635 throw failed_constructor();
638 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
639 if (lilv_port_has_property(_impl->plugin, port, _world.ext_causesArtifacts)) {
640 flags |= PORT_NOAUTO;
642 if (lilv_port_has_property(_impl->plugin, port, _world.ext_notAutomatic)) {
643 flags |= PORT_NOAUTO;
645 if (lilv_port_has_property(_impl->plugin, port, _world.ext_expensive)) {
646 flags |= PORT_NOAUTO;
650 if (lilv_port_has_property(_impl->plugin, port, _world.auto_automation_controlled)) {
651 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
652 flags |= PORT_CTRLED;
655 if (lilv_port_has_property(_impl->plugin, port, _world.auto_automation_controller)) {
656 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
657 flags |= PORT_CTRLER;
662 _port_flags.push_back(flags);
663 _port_minimumSize.push_back(minimumSize);
664 DEBUG_TRACE(DEBUG::LV2, string_compose("port %1 buffer %2 bytes\n", i, minimumSize));
667 _control_data = new float[num_ports];
668 _shadow_data = new float[num_ports];
669 _defaults = new float[num_ports];
670 _ev_buffers = new LV2_Evbuf*[num_ports];
671 memset(_ev_buffers, 0, sizeof(LV2_Evbuf*) * num_ports);
673 const bool latent = lilv_plugin_has_latency(plugin);
674 const uint32_t latency_index = (latent)
675 ? lilv_plugin_get_latency_port_index(plugin)
678 // Build an array of pointers to special parameter buffers
679 void*** params = new void**[num_ports];
680 for (uint32_t i = 0; i < num_ports; ++i) {
683 _impl->designated_input (LV2_TIME__beatsPerMinute, params, (void**)&_bpm_control_port);
684 _impl->designated_input (LV2_CORE__freeWheeling, params, (void**)&_freewheel_control_port);
686 for (uint32_t i = 0; i < num_ports; ++i) {
687 const LilvPort* port = lilv_plugin_get_port_by_index(plugin, i);
688 const LilvNode* sym = lilv_port_get_symbol(plugin, port);
690 // Store index in map so we can look up index by symbol
691 _port_indices.insert(std::make_pair(lilv_node_as_string(sym), i));
693 // Get range and default value if applicable
694 if (parameter_is_control(i)) {
696 lilv_port_get_range(plugin, port, &def, NULL, NULL);
697 _defaults[i] = def ? lilv_node_as_float(def) : 0.0f;
698 if (lilv_port_has_property (plugin, port, _world.lv2_sampleRate)) {
699 _defaults[i] *= _session.frame_rate ();
703 lilv_instance_connect_port(_impl->instance, i, &_control_data[i]);
705 if (latent && i == latency_index) {
707 lilv_port_get_range(_impl->plugin, port, NULL, NULL, &max);
708 _max_latency = max ? lilv_node_as_float(max) : .02 * _sample_rate;
709 _latency_control_port = &_control_data[i];
710 *_latency_control_port = 0;
713 if (parameter_is_input(i)) {
714 _shadow_data[i] = default_value(i);
716 *params[i] = (void*)&_shadow_data[i];
726 LilvUIs* uis = lilv_plugin_get_uis(plugin);
727 if (lilv_uis_size(uis) > 0) {
729 // Look for embeddable UI
730 LILV_FOREACH(uis, u, uis) {
731 const LilvUI* this_ui = lilv_uis_get(uis, u);
732 const LilvNode* this_ui_type = NULL;
733 if (lilv_ui_is_supported(this_ui,
737 // TODO: Multiple UI support
739 _impl->ui_type = this_ui_type;
744 // Look for Gtk native UI
745 LILV_FOREACH(uis, i, uis) {
746 const LilvUI* ui = lilv_uis_get(uis, i);
747 if (lilv_ui_is_a(ui, _world.ui_GtkUI)) {
749 _impl->ui_type = _world.ui_GtkUI;
755 // If Gtk UI is not available, try to find external UI
757 LILV_FOREACH(uis, i, uis) {
758 const LilvUI* ui = lilv_uis_get(uis, i);
759 if (lilv_ui_is_a(ui, _world.ui_externalkx)) {
761 _impl->ui_type = _world.ui_external;
764 if (lilv_ui_is_a(ui, _world.ui_external)) {
766 _impl->ui_type = _world.ui_external;
772 load_supported_properties(_property_descriptors);
773 allocate_atom_event_buffers();
774 latency_compute_run();
778 LV2Plugin::set_block_size (pframes_t nframes)
780 #ifdef HAVE_LV2_1_2_0
781 if (_impl->opts_iface) {
782 LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
783 _impl->block_length = nframes;
784 LV2_Options_Option block_size_option = {
785 LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id ("http://lv2plug.in/ns/ext/buf-size#nominalBlockLength"),
786 sizeof(int32_t), atom_Int, (void*)&_impl->block_length
788 _impl->opts_iface->set (_impl->instance->lv2_handle, &block_size_option);
795 LV2Plugin::requires_fixed_sized_buffers () const
797 /* This controls if Ardour will split the plugin's run()
798 * on automation events in order to pass sample-accurate automation
799 * via standard control-ports.
801 * When returning true Ardour will *not* sub-divide the process-cycle.
802 * Automation events that happen between cycle-start and cycle-end will be
803 * ignored (ctrl values are interpolated to cycle-start).
804 * NB. Atom Sequences are still sample accurate.
806 * Note: This does not guarantee a fixed block-size.
807 * e.g The process cycle may be split when looping, also
808 * the period-size may change any time: see set_block_size()
810 if (get_info()->n_inputs.n_midi() > 0) {
811 /* we don't yet implement midi buffer offsets (for split cycles).
812 * Also connect_and_run() also uses _session.transport_frame() directly
813 * (for BBT) which is not offset for plugin cycle split.
817 return _no_sample_accurate_ctrl;
820 LV2Plugin::~LV2Plugin ()
822 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 destroy\n", name()));
827 lilv_instance_free(_impl->instance);
828 lilv_state_free(_impl->state);
829 lilv_node_free(_impl->name);
830 lilv_node_free(_impl->author);
831 #ifdef HAVE_LV2_1_2_0
832 free(_impl->options);
835 free(_impl->queue_draw);
839 free(_log_feature.data);
840 free(_make_path_feature.data);
841 free(_work_schedule_feature.data);
846 delete _state_worker;
848 if (_atom_ev_buffers) {
849 LV2_Evbuf** b = _atom_ev_buffers;
854 free(_atom_ev_buffers);
857 delete [] _control_data;
858 delete [] _shadow_data;
860 delete [] _ev_buffers;
865 LV2Plugin::is_external_ui() const
870 return lilv_ui_is_a(_impl->ui, _world.ui_external) || lilv_ui_is_a(_impl->ui, _world.ui_externalkx);
874 LV2Plugin::is_external_kx() const
879 return lilv_ui_is_a(_impl->ui, _world.ui_externalkx);
883 LV2Plugin::ui_is_resizable () const
885 const LilvNode* s = lilv_ui_get_uri(_impl->ui);
886 LilvNode* p = lilv_new_uri(_world.world, LV2_CORE__optionalFeature);
887 LilvNode* fs = lilv_new_uri(_world.world, LV2_UI__fixedSize);
888 LilvNode* nrs = lilv_new_uri(_world.world, LV2_UI__noUserResize);
890 LilvNodes* fs_matches = lilv_world_find_nodes(_world.world, s, p, fs);
891 LilvNodes* nrs_matches = lilv_world_find_nodes(_world.world, s, p, nrs);
893 lilv_nodes_free(nrs_matches);
894 lilv_nodes_free(fs_matches);
899 return !fs_matches && !nrs_matches;
904 LV2Plugin::has_inline_display () {
905 return _display_interface ? true : false;
908 Plugin::Display_Image_Surface*
909 LV2Plugin::render_inline_display (uint32_t w, uint32_t h) {
910 if (_display_interface) {
911 /* Plugin::Display_Image_Surface is identical to
912 * LV2_Inline_Display_Image_Surface */
913 return (Plugin::Display_Image_Surface*) _display_interface->render ((void*)_impl->instance->lv2_handle, w, h);
920 LV2Plugin::unique_id() const
922 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
926 LV2Plugin::uri() const
928 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
932 LV2Plugin::label() const
934 return lilv_node_as_string(_impl->name);
938 LV2Plugin::name() const
940 return lilv_node_as_string(_impl->name);
944 LV2Plugin::maker() const
946 return _impl->author ? lilv_node_as_string (_impl->author) : "Unknown";
950 LV2Plugin::num_ports() const
952 return lilv_plugin_get_num_ports(_impl->plugin);
956 LV2Plugin::parameter_count() const
958 return lilv_plugin_get_num_ports(_impl->plugin);
962 LV2Plugin::default_value(uint32_t port)
964 return _defaults[port];
968 LV2Plugin::port_symbol(uint32_t index) const
970 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, index);
972 error << name() << ": Invalid port index " << index << endmsg;
975 const LilvNode* sym = lilv_port_get_symbol(_impl->plugin, port);
976 return lilv_node_as_string(sym);
980 LV2Plugin::port_index (const char* symbol) const
982 const map<string, uint32_t>::const_iterator i = _port_indices.find(symbol);
983 if (i != _port_indices.end()) {
986 warning << string_compose(_("LV2: Unknown port %1"), symbol) << endmsg;
992 LV2Plugin::set_parameter(uint32_t which, float val)
994 DEBUG_TRACE(DEBUG::LV2, string_compose(
995 "%1 set parameter %2 to %3\n", name(), which, val));
997 if (which < lilv_plugin_get_num_ports(_impl->plugin)) {
998 if (get_parameter (which) == val) {
1002 _shadow_data[which] = val;
1004 warning << string_compose(
1005 _("Illegal parameter number used with plugin \"%1\". "
1006 "This is a bug in either %2 or the LV2 plugin <%3>"),
1007 name(), PROGRAM_NAME, unique_id()) << endmsg;
1010 Plugin::set_parameter(which, val);
1014 LV2Plugin::get_parameter(uint32_t which) const
1016 if (parameter_is_input(which)) {
1017 return (float)_shadow_data[which];
1019 return (float)_control_data[which];
1025 LV2Plugin::get_docs() const
1027 LilvNodes* comments = lilv_plugin_get_value(_impl->plugin, _world.rdfs_comment);
1029 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
1030 lilv_nodes_free(comments);
1038 LV2Plugin::get_parameter_docs(uint32_t which) const
1040 LilvNodes* comments = lilv_port_get_value(
1042 lilv_plugin_get_port_by_index(_impl->plugin, which),
1043 _world.rdfs_comment);
1046 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
1047 lilv_nodes_free(comments);
1055 LV2Plugin::get_layout (uint32_t which, UILayoutHint& h) const
1057 /// TODO lookup port-properties
1058 if (unique_id () != "urn:ardour:a-eq") {
1063 case 0: h.x0 = 0; h.x1 = 1; h.y0 = 2; h.y1 = 3; break; // Frequency L
1064 case 1: h.x0 = 0; h.x1 = 1; h.y0 = 0; h.y1 = 1; break; // Gain L
1065 case 17: h.x0 = 0; h.x1 = 1; h.y0 = 5; h.y1 = 6; break; // enable L
1067 case 2: h.x0 = 1; h.x1 = 3; h.y0 = 2; h.y1 = 3; break; // Frequency 1
1068 case 3: h.x0 = 1; h.x1 = 3; h.y0 = 0; h.y1 = 1; break; // Gain 1
1069 case 4: h.x0 = 2; h.x1 = 4; h.y0 = 1; h.y1 = 2; break; // Bandwidth 1
1070 case 18: h.x0 = 1; h.x1 = 4; h.y0 = 5; h.y1 = 6; break; // enable 1
1072 case 5: h.x0 = 4; h.x1 = 6; h.y0 = 2; h.y1 = 3; break; // Frequency 2
1073 case 6: h.x0 = 4; h.x1 = 6; h.y0 = 0; h.y1 = 1; break; // Gain 2
1074 case 7: h.x0 = 5; h.x1 = 7; h.y0 = 1; h.y1 = 2; break; // Bandwidth 2
1075 case 19: h.x0 = 4; h.x1 = 7; h.y0 = 5; h.y1 = 6; break; // enable 2
1077 case 8: h.x0 = 7; h.x1 = 9; h.y0 = 2; h.y1 = 3; break; // Frequency 3
1078 case 9: h.x0 = 7; h.x1 = 9; h.y0 = 0; h.y1 = 1; break; // Gain 3
1079 case 10: h.x0 = 8; h.x1 = 10; h.y0 = 1; h.y1 = 2; break; // Bandwidth 3
1080 case 20: h.x0 = 7; h.x1 = 10; h.y0 = 5; h.y1 = 6; break; // enable 3
1082 case 11: h.x0 = 10; h.x1 = 12; h.y0 = 2; h.y1 = 3; break; // Frequency 4
1083 case 12: h.x0 = 10; h.x1 = 12; h.y0 = 0; h.y1 = 1; break; // Gain 4
1084 case 13: h.x0 = 11; h.x1 = 13; h.y0 = 1; h.y1 = 2; break; // Bandwidth 4
1085 case 21: h.x0 = 10; h.x1 = 13; h.y0 = 5; h.y1 = 6; break; // enable 4
1087 case 14: h.x0 = 13; h.x1 = 14; h.y0 = 2; h.y1 = 3; break; // Frequency H
1088 case 15: h.x0 = 13; h.x1 = 14; h.y0 = 0; h.y1 = 1; break; // Gain H
1089 case 22: h.x0 = 13; h.x1 = 14; h.y0 = 5; h.y1 = 6; break; // enable H
1092 case 16: h.x0 = 14; h.x1 = 15; h.y0 = 4; h.y1 = 6; break; // Master Gain
1100 LV2Plugin::nth_parameter(uint32_t n, bool& ok) const
1103 for (uint32_t c = 0, x = 0; x < lilv_plugin_get_num_ports(_impl->plugin); ++x) {
1104 if (parameter_is_control(x)) {
1116 LV2Plugin::extension_data(const char* uri) const
1118 return lilv_instance_get_extension_data(_impl->instance, uri);
1122 LV2Plugin::c_plugin()
1124 return _impl->plugin;
1130 return (const void*)_impl->ui;
1134 LV2Plugin::c_ui_type()
1136 return (const void*)_impl->ui_type;
1139 /** Directory for all plugin state. */
1141 LV2Plugin::plugin_dir() const
1143 if (!_plugin_state_dir.empty ()){
1144 return Glib::build_filename(_plugin_state_dir, _insert_id.to_s());
1146 return Glib::build_filename(_session.plugins_dir(), _insert_id.to_s());
1150 /** Directory for files created by the plugin (except during save). */
1152 LV2Plugin::scratch_dir() const
1154 return Glib::build_filename(plugin_dir(), "scratch");
1157 /** Directory for snapshots of files in the scratch directory. */
1159 LV2Plugin::file_dir() const
1161 return Glib::build_filename(plugin_dir(), "files");
1164 /** Directory to save state snapshot version @c num into. */
1166 LV2Plugin::state_dir(unsigned num) const
1168 return Glib::build_filename(plugin_dir(), string_compose("state%1", num));
1171 /** Implementation of state:makePath for files created at instantiation time.
1172 * Note this is not used for files created at save time (Lilv deals with that).
1175 LV2Plugin::lv2_state_make_path(LV2_State_Make_Path_Handle handle,
1178 LV2Plugin* me = (LV2Plugin*)handle;
1179 if (me->_insert_id == PBD::ID("0")) {
1180 warning << string_compose(
1181 "File path \"%1\" requested but LV2 %2 has no insert ID",
1182 path, me->name()) << endmsg;
1183 return g_strdup(path);
1186 const std::string abs_path = Glib::build_filename(me->scratch_dir(), path);
1187 const std::string dirname = Glib::path_get_dirname(abs_path);
1188 g_mkdir_with_parents(dirname.c_str(), 0744);
1190 DEBUG_TRACE(DEBUG::LV2, string_compose("new file path %1 => %2\n",
1193 return g_strndup(abs_path.c_str(), abs_path.length());
1197 LV2Plugin::add_state(XMLNode* root) const
1199 assert(_insert_id != PBD::ID("0"));
1205 for (uint32_t i = 0; i < parameter_count(); ++i) {
1206 if (parameter_is_input(i) && parameter_is_control(i)) {
1207 child = new XMLNode("Port");
1208 child->add_property("symbol", port_symbol(i));
1209 snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
1210 child->add_property("value", string(buf));
1211 root->add_child_nocopy(*child);
1215 if (!_plugin_state_dir.empty()) {
1216 root->add_property("template-dir", _plugin_state_dir);
1219 if (_has_state_interface) {
1220 // Provisionally increment state version and create directory
1221 const std::string new_dir = state_dir(++_state_version);
1222 g_mkdir_with_parents(new_dir.c_str(), 0744);
1224 LilvState* state = lilv_state_new_from_instance(
1227 _uri_map.urid_map(),
1228 scratch_dir().c_str(),
1230 _session.externals_dir().c_str(),
1233 const_cast<LV2Plugin*>(this),
1237 if (!_plugin_state_dir.empty()
1239 || !lilv_state_equals(state, _impl->state)) {
1240 lilv_state_save(_world.world,
1241 _uri_map.urid_map(),
1242 _uri_map.urid_unmap(),
1248 if (_plugin_state_dir.empty()) {
1249 // normal session save
1250 lilv_state_free(_impl->state);
1251 _impl->state = state;
1253 // template save (dedicated state-dir)
1254 lilv_state_free(state);
1257 // State is identical, decrement version and nuke directory
1258 lilv_state_free(state);
1259 PBD::remove_directory(new_dir);
1263 root->add_property("state-dir", string_compose("state%1", _state_version));
1267 // TODO: Once we can rely on lilv 0.16.0, lilv_world_get can replace this
1269 get_value(LilvWorld* world, const LilvNode* subject, const LilvNode* predicate)
1271 LilvNodes* vs = lilv_world_find_nodes(world, subject, predicate, NULL);
1273 LilvNode* node = lilv_node_duplicate(lilv_nodes_get_first(vs));
1274 lilv_nodes_free(vs);
1281 LV2Plugin::find_presets()
1283 LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
1284 LilvNode* pset_Preset = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
1285 LilvNode* rdfs_label = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
1287 LilvNodes* presets = lilv_plugin_get_related(_impl->plugin, pset_Preset);
1288 LILV_FOREACH(nodes, i, presets) {
1289 const LilvNode* preset = lilv_nodes_get(presets, i);
1290 lilv_world_load_resource(_world.world, preset);
1291 LilvNode* name = get_value(_world.world, preset, rdfs_label);
1292 bool userpreset = true; // TODO
1294 _presets.insert(std::make_pair(lilv_node_as_string(preset),
1295 Plugin::PresetRecord(
1296 lilv_node_as_string(preset),
1297 lilv_node_as_string(name),
1299 lilv_node_free(name);
1301 warning << string_compose(
1302 _("Plugin \"%1\" preset \"%2\" is missing a label\n"),
1303 lilv_node_as_string(lilv_plugin_get_uri(_impl->plugin)),
1304 lilv_node_as_string(preset)) << endmsg;
1307 lilv_nodes_free(presets);
1309 lilv_node_free(rdfs_label);
1310 lilv_node_free(pset_Preset);
1311 lilv_node_free(lv2_appliesTo);
1315 set_port_value(const char* port_symbol,
1321 LV2Plugin* self = (LV2Plugin*)user_data;
1322 if (type != 0 && type != URIMap::instance().urids.atom_Float) {
1323 return; // TODO: Support non-float ports
1326 const uint32_t port_index = self->port_index(port_symbol);
1327 if (port_index != (uint32_t)-1) {
1328 self->set_parameter(port_index, *(const float*)value);
1333 LV2Plugin::load_preset(PresetRecord r)
1335 LilvWorld* world = _world.world;
1336 LilvNode* pset = lilv_new_uri(world, r.uri.c_str());
1337 LilvState* state = lilv_state_new_from_world(world, _uri_map.urid_map(), pset);
1339 const LV2_Feature* state_features[2] = { NULL, NULL };
1340 LV2_Worker_Schedule schedule = { _state_worker, work_schedule };
1341 const LV2_Feature state_sched_feature = { LV2_WORKER__schedule, &schedule };
1342 if (_state_worker) {
1343 state_features[0] = &state_sched_feature;
1347 lilv_state_restore(state, _impl->instance, set_port_value, this, 0, state_features);
1348 lilv_state_free(state);
1349 Plugin::load_preset(r);
1352 lilv_node_free(pset);
1357 ARDOUR::lv2plugin_get_port_value(const char* port_symbol,
1362 LV2Plugin *plugin = (LV2Plugin *) user_data;
1364 uint32_t index = plugin->port_index(port_symbol);
1365 if (index != (uint32_t) -1) {
1366 if (plugin->parameter_is_input(index) && plugin->parameter_is_control(index)) {
1368 *size = sizeof(float);
1369 *type = plugin->_uri_map.uri_to_id(LV2_ATOM__Float);
1370 value = &plugin->_shadow_data[index];
1382 LV2Plugin::do_save_preset(string name)
1384 LilvNode* plug_name = lilv_plugin_get_name(_impl->plugin);
1385 const string prefix = legalize_for_uri(lilv_node_as_string(plug_name));
1386 const string base_name = legalize_for_uri(name);
1387 const string file_name = base_name + ".ttl";
1388 const string bundle = Glib::build_filename(
1389 Glib::get_home_dir(),
1390 Glib::build_filename(".lv2", prefix + "_" + base_name + ".lv2"));
1392 #ifdef HAVE_LILV_0_21_3
1393 /* delete reference to old preset (if any) */
1394 const PresetRecord* r = preset_by_label(name);
1396 LilvNode* pset = lilv_new_uri (_world.world, r->uri.c_str());
1398 lilv_world_unload_resource (_world.world, pset);
1399 lilv_node_free(pset);
1404 LilvState* state = lilv_state_new_from_instance(
1407 _uri_map.urid_map(),
1408 scratch_dir().c_str(), // file_dir
1409 bundle.c_str(), // copy_dir
1410 bundle.c_str(), // link_dir
1411 bundle.c_str(), // save_dir
1412 lv2plugin_get_port_value, // get_value
1413 (void*)this, // user_data
1414 LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE, // flags
1415 _features // features
1418 lilv_state_set_label(state, name.c_str());
1420 _world.world, // world
1421 _uri_map.urid_map(), // map
1422 _uri_map.urid_unmap(), // unmap
1424 NULL, // uri (NULL = use file URI)
1425 bundle.c_str(), // dir
1426 file_name.c_str() // filename
1429 lilv_state_free(state);
1431 std::string uri = Glib::filename_to_uri(Glib::build_filename(bundle, file_name));
1432 LilvNode *node_bundle = lilv_new_uri(_world.world, Glib::filename_to_uri(Glib::build_filename(bundle, "/")).c_str());
1433 LilvNode *node_preset = lilv_new_uri(_world.world, uri.c_str());
1434 #ifdef HAVE_LILV_0_21_3
1435 lilv_world_unload_resource(_world.world, node_preset);
1436 lilv_world_unload_bundle(_world.world, node_bundle);
1438 lilv_world_load_bundle(_world.world, node_bundle);
1439 lilv_world_load_resource(_world.world, node_preset);
1440 lilv_node_free(node_bundle);
1441 lilv_node_free(node_preset);
1442 lilv_node_free(plug_name);
1447 LV2Plugin::do_remove_preset(string name)
1449 #ifdef HAVE_LILV_0_21_3
1450 /* Look up preset record by label (FIXME: ick, label as ID) */
1451 const PresetRecord* r = preset_by_label(name);
1456 /* Load a LilvState for the preset. */
1457 LilvWorld* world = _world.world;
1458 LilvNode* pset = lilv_new_uri(world, r->uri.c_str());
1459 LilvState* state = lilv_state_new_from_world(world, _uri_map.urid_map(), pset);
1461 lilv_node_free(pset);
1465 /* Unload preset from world. */
1466 lilv_world_unload_resource(world, pset);
1468 /* Delete it from the file system. This will remove the preset file and the entry
1469 from the manifest. If this results in an empty manifest (i.e. the
1470 preset is the only thing in the bundle), then the bundle is removed. */
1471 lilv_state_delete(world, state);
1473 lilv_state_free(state);
1474 lilv_node_free(pset);
1476 /* Without lilv_state_delete(), we could delete the preset file, but this
1477 would leave a broken bundle/manifest around, so the preset would still
1478 be visible, but broken. Naively deleting a bundle is too dangerous, so
1479 we simply do not support preset deletion with older Lilv */
1483 LV2Plugin::has_editor() const
1485 return _impl->ui != NULL;
1489 LV2Plugin::has_message_output() const
1491 for (uint32_t i = 0; i < num_ports(); ++i) {
1492 if ((_port_flags[i] & PORT_SEQUENCE) &&
1493 (_port_flags[i] & PORT_OUTPUT)) {
1501 LV2Plugin::write_to(RingBuffer<uint8_t>* dest,
1505 const uint8_t* body)
1507 const uint32_t buf_size = sizeof(UIMessage) + size;
1508 vector<uint8_t> buf(buf_size);
1510 UIMessage* msg = (UIMessage*)&buf[0];
1512 msg->protocol = protocol;
1514 memcpy(msg + 1, body, size);
1516 return (dest->write(&buf[0], buf_size) == buf_size);
1520 LV2Plugin::write_from_ui(uint32_t index,
1523 const uint8_t* body)
1526 size_t rbs = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
1527 /* buffer data communication from plugin UI to plugin instance.
1528 * this buffer needs to potentially hold
1529 * (port's minimumSize) * (audio-periods) / (UI-periods)
1532 * e.g 48kSPS / 128fpp -> audio-periods = 375 Hz
1533 * ui-periods = 25 Hz (SuperRapidScreenUpdate)
1534 * default minimumSize = 32K (see LV2Plugin::allocate_atom_event_buffers()
1536 * it is NOT safe to overflow (msg.size will be misinterpreted)
1538 uint32_t bufsiz = 32768;
1539 if (_atom_ev_buffers && _atom_ev_buffers[0]) {
1540 bufsiz = lv2_evbuf_get_capacity(_atom_ev_buffers[0]);
1542 rbs = max((size_t) bufsiz * 8, rbs);
1543 _from_ui = new RingBuffer<uint8_t>(rbs);
1546 if (!write_to(_from_ui, index, protocol, size, body)) {
1547 error << "Error writing from UI to plugin" << endmsg;
1554 LV2Plugin::write_to_ui(uint32_t index,
1557 const uint8_t* body)
1559 if (!write_to(_to_ui, index, protocol, size, body)) {
1560 error << "Error writing from plugin to UI" << endmsg;
1567 forge_variant(LV2_Atom_Forge* forge, const Variant& value)
1569 switch (value.type()) {
1570 case Variant::NOTHING:
1572 case Variant::BEATS:
1573 // No atom type for this, just forge a double
1574 lv2_atom_forge_double(forge, value.get_beats().to_double());
1577 lv2_atom_forge_bool(forge, value.get_bool());
1579 case Variant::DOUBLE:
1580 lv2_atom_forge_double(forge, value.get_double());
1582 case Variant::FLOAT:
1583 lv2_atom_forge_float(forge, value.get_float());
1586 lv2_atom_forge_int(forge, value.get_int());
1589 lv2_atom_forge_long(forge, value.get_long());
1592 lv2_atom_forge_path(
1593 forge, value.get_path().c_str(), value.get_path().size());
1595 case Variant::STRING:
1596 lv2_atom_forge_string(
1597 forge, value.get_string().c_str(), value.get_string().size());
1601 forge, value.get_uri().c_str(), value.get_uri().size());
1606 /** Get a variant type from a URI, return false iff no match found. */
1608 uri_to_variant_type(const std::string& uri, Variant::Type& type)
1610 if (uri == LV2_ATOM__Bool) {
1611 type = Variant::BOOL;
1612 } else if (uri == LV2_ATOM__Double) {
1613 type = Variant::DOUBLE;
1614 } else if (uri == LV2_ATOM__Float) {
1615 type = Variant::FLOAT;
1616 } else if (uri == LV2_ATOM__Int) {
1617 type = Variant::INT;
1618 } else if (uri == LV2_ATOM__Long) {
1619 type = Variant::LONG;
1620 } else if (uri == LV2_ATOM__Path) {
1621 type = Variant::PATH;
1622 } else if (uri == LV2_ATOM__String) {
1623 type = Variant::STRING;
1624 } else if (uri == LV2_ATOM__URI) {
1625 type = Variant::URI;
1633 LV2Plugin::set_property(uint32_t key, const Variant& value)
1635 if (_patch_port_in_index == (uint32_t)-1) {
1636 error << "LV2: set_property called with unset patch_port_in_index" << endmsg;
1638 } else if (value.type() == Variant::NOTHING) {
1639 error << "LV2: set_property called with void value" << endmsg;
1643 // Set up forge to write to temporary buffer on the stack
1644 LV2_Atom_Forge* forge = &_impl->ui_forge;
1645 LV2_Atom_Forge_Frame frame;
1646 uint8_t buf[PATH_MAX]; // Ought to be enough for anyone...
1648 lv2_atom_forge_set_buffer(forge, buf, sizeof(buf));
1650 // Serialize patch:Set message to set property
1651 #ifdef HAVE_LV2_1_10_0
1652 lv2_atom_forge_object(forge, &frame, 1, _uri_map.urids.patch_Set);
1653 lv2_atom_forge_key(forge, _uri_map.urids.patch_property);
1654 lv2_atom_forge_urid(forge, key);
1655 lv2_atom_forge_key(forge, _uri_map.urids.patch_value);
1657 lv2_atom_forge_blank(forge, &frame, 1, _uri_map.urids.patch_Set);
1658 lv2_atom_forge_property_head(forge, _uri_map.urids.patch_property, 0);
1659 lv2_atom_forge_urid(forge, key);
1660 lv2_atom_forge_property_head(forge, _uri_map.urids.patch_value, 0);
1663 forge_variant(forge, value);
1665 // Write message to UI=>Plugin ring
1666 const LV2_Atom* const atom = (const LV2_Atom*)buf;
1667 write_from_ui(_patch_port_in_index,
1668 _uri_map.urids.atom_eventTransfer,
1669 lv2_atom_total_size(atom),
1670 (const uint8_t*)atom);
1673 const ParameterDescriptor&
1674 LV2Plugin::get_property_descriptor(uint32_t id) const
1676 PropertyDescriptors::const_iterator p = _property_descriptors.find(id);
1677 if (p != _property_descriptors.end()) {
1680 return Plugin::get_property_descriptor(id);
1684 load_parameter_descriptor_units(LilvWorld* lworld, ParameterDescriptor& desc, const LilvNodes* units)
1686 if (lilv_nodes_contains(units, _world.units_midiNote)) {
1687 desc.unit = ParameterDescriptor::MIDI_NOTE;
1688 } else if (lilv_nodes_contains(units, _world.units_db)) {
1689 desc.unit = ParameterDescriptor::DB;
1690 } else if (lilv_nodes_contains(units, _world.units_hz)) {
1691 desc.unit = ParameterDescriptor::HZ;
1693 if (lilv_nodes_size(units) > 0) {
1694 const LilvNode* unit = lilv_nodes_get_first(units);
1695 LilvNode* render = get_value(lworld, unit, _world.units_render);
1697 desc.print_fmt = lilv_node_as_string(render);
1698 replace_all (desc.print_fmt, "%f", "%.2f");
1699 lilv_node_free(render);
1705 load_parameter_descriptor(LV2World& world,
1706 ParameterDescriptor& desc,
1707 Variant::Type datatype,
1708 const LilvNode* subject)
1710 LilvWorld* lworld = _world.world;
1711 LilvNode* label = get_value(lworld, subject, _world.rdfs_label);
1712 LilvNode* def = get_value(lworld, subject, _world.lv2_default);
1713 LilvNode* minimum = get_value(lworld, subject, _world.lv2_minimum);
1714 LilvNode* maximum = get_value(lworld, subject, _world.lv2_maximum);
1715 LilvNodes* units = lilv_world_find_nodes(lworld, subject, _world.units_unit, NULL);
1717 desc.label = lilv_node_as_string(label);
1719 if (def && lilv_node_is_float(def)) {
1720 desc.normal = lilv_node_as_float(def);
1722 if (minimum && lilv_node_is_float(minimum)) {
1723 desc.lower = lilv_node_as_float(minimum);
1725 if (maximum && lilv_node_is_float(maximum)) {
1726 desc.upper = lilv_node_as_float(maximum);
1728 load_parameter_descriptor_units(lworld, desc, units);
1729 desc.datatype = datatype;
1730 desc.toggled |= datatype == Variant::BOOL;
1731 desc.integer_step |= datatype == Variant::INT || datatype == Variant::LONG;
1732 desc.update_steps();
1734 lilv_nodes_free(units);
1735 lilv_node_free(label);
1736 lilv_node_free(def);
1737 lilv_node_free(minimum);
1738 lilv_node_free(maximum);
1742 LV2Plugin::load_supported_properties(PropertyDescriptors& descs)
1744 LilvWorld* lworld = _world.world;
1745 const LilvNode* subject = lilv_plugin_get_uri(_impl->plugin);
1746 LilvNodes* properties = lilv_world_find_nodes(
1747 lworld, subject, _world.patch_writable, NULL);
1748 LILV_FOREACH(nodes, p, properties) {
1749 // Get label and range
1750 const LilvNode* prop = lilv_nodes_get(properties, p);
1751 LilvNode* range = get_value(lworld, prop, _world.rdfs_range);
1753 warning << string_compose(_("LV2: property <%1> has no range datatype, ignoring"),
1754 lilv_node_as_uri(prop)) << endmsg;
1758 // Convert range to variant type (TODO: support for multiple range types)
1759 Variant::Type datatype;
1760 if (!uri_to_variant_type(lilv_node_as_uri(range), datatype)) {
1761 error << string_compose(_("LV2: property <%1> has unsupported datatype <%1>"),
1762 lilv_node_as_uri(prop), lilv_node_as_uri(range)) << endmsg;
1766 // Add description to result
1767 ParameterDescriptor desc;
1768 desc.key = _uri_map.uri_to_id(lilv_node_as_uri(prop));
1769 desc.datatype = datatype;
1770 load_parameter_descriptor(_world, desc, datatype, prop);
1771 descs.insert(std::make_pair(desc.key, desc));
1773 lilv_node_free(range);
1775 lilv_nodes_free(properties);
1779 LV2Plugin::announce_property_values()
1781 if (_patch_port_in_index == (uint32_t)-1) {
1785 // Set up forge to write to temporary buffer on the stack
1786 LV2_Atom_Forge* forge = &_impl->ui_forge;
1787 LV2_Atom_Forge_Frame frame;
1788 uint8_t buf[PATH_MAX]; // Ought to be enough for anyone...
1790 lv2_atom_forge_set_buffer(forge, buf, sizeof(buf));
1792 // Serialize patch:Get message with no subject (implicitly plugin instance)
1793 #ifdef HAVE_LV2_1_10_0
1794 lv2_atom_forge_object(forge, &frame, 1, _uri_map.urids.patch_Get);
1796 lv2_atom_forge_blank(forge, &frame, 1, _uri_map.urids.patch_Get);
1799 // Write message to UI=>Plugin ring
1800 const LV2_Atom* const atom = (const LV2_Atom*)buf;
1801 write_from_ui(_patch_port_in_index,
1802 _uri_map.urids.atom_eventTransfer,
1803 lv2_atom_total_size(atom),
1804 (const uint8_t*)atom);
1808 LV2Plugin::enable_ui_emission()
1811 /* see note in LV2Plugin::write_from_ui() */
1812 uint32_t bufsiz = 32768;
1813 if (_atom_ev_buffers && _atom_ev_buffers[0]) {
1814 bufsiz = lv2_evbuf_get_capacity(_atom_ev_buffers[0]);
1816 size_t rbs = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
1817 rbs = max((size_t) bufsiz * 8, rbs);
1818 _to_ui = new RingBuffer<uint8_t>(rbs);
1823 LV2Plugin::emit_to_ui(void* controller, UIMessageSink sink)
1829 uint32_t read_space = _to_ui->read_space();
1830 while (read_space > sizeof(UIMessage)) {
1832 if (_to_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
1833 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
1836 vector<uint8_t> body(msg.size);
1837 if (_to_ui->read(&body[0], msg.size) != msg.size) {
1838 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
1842 sink(controller, msg.index, msg.size, msg.protocol, &body[0]);
1844 read_space -= sizeof(msg) + msg.size;
1849 LV2Plugin::work(Worker& worker, uint32_t size, const void* data)
1851 Glib::Threads::Mutex::Lock lm(_work_mutex);
1852 return _impl->work_iface->work(
1853 _impl->instance->lv2_handle, work_respond, &worker, size, data);
1857 LV2Plugin::work_response(uint32_t size, const void* data)
1859 return _impl->work_iface->work_response(
1860 _impl->instance->lv2_handle, size, data);
1864 LV2Plugin::set_insert_id(PBD::ID id)
1866 if (_insert_id == "0") {
1868 } else if (_insert_id != id) {
1869 lilv_state_free(_impl->state);
1870 _impl->state = NULL;
1876 LV2Plugin::set_state_dir (const std::string& d)
1878 _plugin_state_dir = d;
1882 LV2Plugin::set_state(const XMLNode& node, int version)
1885 XMLProperty const * prop;
1886 XMLNodeConstIterator iter;
1893 if (node.name() != state_node_name()) {
1894 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
1898 #ifndef NO_PLUGIN_STATE
1900 if (version < 3000) {
1901 nodes = node.children("port");
1903 nodes = node.children("Port");
1906 for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
1910 if ((prop = child->property("symbol")) != 0) {
1911 sym = prop->value().c_str();
1913 warning << _("LV2: port has no symbol, ignored") << endmsg;
1917 map<string, uint32_t>::iterator i = _port_indices.find(sym);
1919 if (i != _port_indices.end()) {
1920 port_id = i->second;
1922 warning << _("LV2: port has unknown index, ignored") << endmsg;
1926 if ((prop = child->property("value")) != 0) {
1927 value = prop->value().c_str();
1929 warning << _("LV2: port has no value, ignored") << endmsg;
1933 set_parameter(port_id, atof(value));
1936 if ((prop = node.property("template-dir")) != 0) {
1937 set_state_dir (prop->value ());
1941 if ((prop = node.property("state-dir")) != 0) {
1942 if (sscanf(prop->value().c_str(), "state%u", &_state_version) != 1) {
1943 error << string_compose(
1944 "LV2: failed to parse state version from \"%1\"",
1945 prop->value()) << endmsg;
1948 std::string state_file = Glib::build_filename(
1950 Glib::build_filename(prop->value(), "state.ttl"));
1952 LilvState* state = lilv_state_new_from_file(
1953 _world.world, _uri_map.urid_map(), NULL, state_file.c_str());
1955 lilv_state_restore(state, _impl->instance, NULL, NULL, 0, NULL);
1956 lilv_state_free(_impl->state);
1957 _impl->state = state;
1960 if (!_plugin_state_dir.empty ()) {
1961 // force save with session, next time (increment counter)
1962 lilv_state_free (_impl->state);
1963 _impl->state = NULL;
1967 latency_compute_run();
1970 return Plugin::set_state(node, version);
1974 LV2Plugin::get_parameter_descriptor(uint32_t which, ParameterDescriptor& desc) const
1976 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, which);
1978 error << string_compose("LV2: get descriptor of non-existent port %1", which)
1983 LilvNodes* portunits;
1984 LilvNode *def, *min, *max;
1985 lilv_port_get_range(_impl->plugin, port, &def, &min, &max);
1986 portunits = lilv_port_get_value(_impl->plugin, port, _world.units_unit);
1988 LilvNode* steps = lilv_port_get(_impl->plugin, port, _world.ext_rangeSteps);
1990 // TODO: Once we can rely on lilv 0.18.0 being present,
1991 // load_parameter_descriptor() can be used for ports as well
1992 desc.integer_step = lilv_port_has_property(_impl->plugin, port, _world.lv2_integer);
1993 desc.toggled = lilv_port_has_property(_impl->plugin, port, _world.lv2_toggled);
1994 desc.logarithmic = lilv_port_has_property(_impl->plugin, port, _world.ext_logarithmic);
1995 desc.sr_dependent = lilv_port_has_property(_impl->plugin, port, _world.lv2_sampleRate);
1996 desc.label = lilv_node_as_string(lilv_port_get_name(_impl->plugin, port));
1997 desc.normal = def ? lilv_node_as_float(def) : 0.0f;
1998 desc.lower = min ? lilv_node_as_float(min) : 0.0f;
1999 desc.upper = max ? lilv_node_as_float(max) : 1.0f;
2000 load_parameter_descriptor_units(_world.world, desc, portunits);
2002 if (desc.sr_dependent) {
2003 desc.lower *= _session.frame_rate ();
2004 desc.upper *= _session.frame_rate ();
2007 desc.min_unbound = false; // TODO: LV2 extension required
2008 desc.max_unbound = false; // TODO: LV2 extension required
2010 desc.enumeration = lilv_port_has_property(_impl->plugin, port, _world.lv2_enumeration);
2011 desc.scale_points = get_scale_points(which);
2013 desc.update_steps();
2016 //override auto-calculated steps in update_steps()
2017 float s = lilv_node_as_float (steps);
2018 const float delta = desc.upper - desc.lower;
2020 desc.step = desc.smallstep = (delta / s);
2021 desc.largestep = std::min ((delta / 5.0f), 10.f * desc.smallstep);
2023 if (desc.logarithmic) {
2024 // TODO marry AutomationControl::internal_to_interface () with
2025 // http://lv2plug.in/ns/ext/port-props/#rangeSteps
2026 desc.smallstep = desc.smallstep / logf(s);
2027 desc.step = desc.step / logf(s);
2028 desc.largestep = desc.largestep / logf(s);
2029 } else if (desc.integer_step) {
2030 desc.smallstep = 1.0;
2031 desc.step = std::max(1.f, rintf (desc.step));
2032 desc.largestep = std::max(1.f, rintf (desc.largestep));
2034 DEBUG_TRACE(DEBUG::LV2, string_compose("parameter %1 small: %2, step: %3 largestep: %4\n",
2035 which, desc.smallstep, desc.step, desc.largestep));
2039 lilv_node_free(def);
2040 lilv_node_free(min);
2041 lilv_node_free(max);
2042 lilv_node_free(steps);
2043 lilv_nodes_free(portunits);
2048 Plugin::IOPortDescription
2049 LV2Plugin::describe_io_port (ARDOUR::DataType dt, bool input, uint32_t id) const
2051 PortFlags match = 0;
2053 case DataType::AUDIO:
2056 case DataType::MIDI:
2057 match = PORT_SEQUENCE | PORT_MIDI; // ignore old PORT_EVENT
2060 return Plugin::IOPortDescription ("?");
2064 match |= PORT_INPUT;
2066 match |= PORT_OUTPUT;
2070 uint32_t idx = UINT32_MAX;
2072 uint32_t const num_ports = parameter_count();
2073 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2074 PortFlags flags = _port_flags[port_index];
2075 if ((flags & match) == match) {
2082 if (idx == UINT32_MAX) {
2083 return Plugin::IOPortDescription ("?");
2086 LilvNode* name = lilv_port_get_name(_impl->plugin,
2087 lilv_plugin_get_port_by_index(_impl->plugin, idx));
2088 Plugin::IOPortDescription iod (lilv_node_as_string (name));
2089 lilv_node_free(name);
2091 if (lilv_port_has_property(_impl->plugin,
2092 lilv_plugin_get_port_by_index(_impl->plugin, idx), _world.lv2_isSideChain)) {
2093 iod.is_sidechain = true;
2099 LV2Plugin::describe_parameter(Evoral::Parameter which)
2101 if (( which.type() == PluginAutomation) && ( which.id() < parameter_count()) ) {
2103 if (lilv_port_has_property(_impl->plugin,
2104 lilv_plugin_get_port_by_index(_impl->plugin, which.id()), _world.ext_notOnGUI)) {
2105 return X_("hidden");
2108 if (lilv_port_has_property(_impl->plugin,
2109 lilv_plugin_get_port_by_index(_impl->plugin, which.id()), _world.lv2_freewheeling)) {
2110 return X_("hidden");
2113 if (lilv_port_has_property(_impl->plugin,
2114 lilv_plugin_get_port_by_index(_impl->plugin, which.id()), _world.lv2_reportsLatency)) {
2115 return X_("latency");
2118 LilvNode* name = lilv_port_get_name(_impl->plugin,
2119 lilv_plugin_get_port_by_index(_impl->plugin, which.id()));
2120 string ret(lilv_node_as_string(name));
2121 lilv_node_free(name);
2129 LV2Plugin::max_latency () const
2131 return _max_latency;
2135 LV2Plugin::signal_latency() const
2137 if (_latency_control_port) {
2138 return (framecnt_t)floor(*_latency_control_port);
2144 set<Evoral::Parameter>
2145 LV2Plugin::automatable() const
2147 set<Evoral::Parameter> ret;
2149 for (uint32_t i = 0; i < parameter_count(); ++i) {
2150 if (parameter_is_input(i) && parameter_is_control(i) && !(_port_flags[i] & PORT_NOAUTO)) {
2151 ret.insert(ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
2155 for (PropertyDescriptors::const_iterator p = _property_descriptors.begin();
2156 p != _property_descriptors.end();
2158 ret.insert(ret.end(), Evoral::Parameter(PluginPropertyAutomation, 0, p->first));
2164 LV2Plugin::set_automation_control (uint32_t i, boost::shared_ptr<AutomationControl> c)
2166 if ((_port_flags[i] & (PORT_CTRLED | PORT_CTRLER))) {
2167 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("Ctrl Port %1\n", i));
2168 _ctrl_map [i] = AutomationCtrlPtr (new AutomationCtrl(c));
2172 LV2Plugin::AutomationCtrlPtr
2173 LV2Plugin::get_automation_control (uint32_t i)
2175 if (_ctrl_map.find (i) == _ctrl_map.end()) {
2176 return AutomationCtrlPtr ();
2178 return _ctrl_map[i];
2182 LV2Plugin::activate()
2184 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 activate\n", name()));
2186 if (!_was_activated) {
2187 lilv_instance_activate(_impl->instance);
2188 _was_activated = true;
2193 LV2Plugin::deactivate()
2195 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 deactivate\n", name()));
2197 if (_was_activated) {
2198 lilv_instance_deactivate(_impl->instance);
2199 _was_activated = false;
2204 LV2Plugin::cleanup()
2206 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 cleanup\n", name()));
2209 lilv_instance_free(_impl->instance);
2210 _impl->instance = NULL;
2214 LV2Plugin::allocate_atom_event_buffers()
2216 /* reserve local scratch buffers for ATOM event-queues */
2217 const LilvPlugin* p = _impl->plugin;
2219 /* count non-MIDI atom event-ports
2220 * TODO: nicely ask drobilla to make a lilv_ call for that
2222 int count_atom_out = 0;
2223 int count_atom_in = 0;
2224 int minimumSize = 32768; // TODO use a per-port minimum-size
2225 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
2226 const LilvPort* port = lilv_plugin_get_port_by_index(p, i);
2227 if (lilv_port_is_a(p, port, _world.atom_AtomPort)) {
2228 LilvNodes* buffer_types = lilv_port_get_value(
2229 p, port, _world.atom_bufferType);
2230 LilvNodes* atom_supports = lilv_port_get_value(
2231 p, port, _world.atom_supports);
2233 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
2234 if (lilv_port_is_a(p, port, _world.lv2_InputPort)) {
2237 if (lilv_port_is_a(p, port, _world.lv2_OutputPort)) {
2240 LilvNodes* min_size_v = lilv_port_get_value(_impl->plugin, port, _world.rsz_minimumSize);
2241 LilvNode* min_size = min_size_v ? lilv_nodes_get_first(min_size_v) : NULL;
2242 if (min_size && lilv_node_is_int(min_size)) {
2243 minimumSize = std::max(minimumSize, lilv_node_as_int(min_size));
2245 lilv_nodes_free(min_size_v);
2247 lilv_nodes_free(buffer_types);
2248 lilv_nodes_free(atom_supports);
2252 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 need buffers for %2 atom-in and %3 atom-out event-ports\n",
2253 name(), count_atom_in, count_atom_out));
2255 const int total_atom_buffers = (count_atom_in + count_atom_out);
2256 if (_atom_ev_buffers || total_atom_buffers == 0) {
2260 DEBUG_TRACE(DEBUG::LV2, string_compose("allocate %1 atom_ev_buffers of %2 bytes\n", total_atom_buffers, minimumSize));
2261 _atom_ev_buffers = (LV2_Evbuf**) malloc((total_atom_buffers + 1) * sizeof(LV2_Evbuf*));
2262 for (int i = 0; i < total_atom_buffers; ++i ) {
2263 _atom_ev_buffers[i] = lv2_evbuf_new(minimumSize, LV2_EVBUF_ATOM,
2264 _uri_map.urids.atom_Chunk, _uri_map.urids.atom_Sequence);
2266 _atom_ev_buffers[total_atom_buffers] = 0;
2270 /** Write an ardour position/time/tempo/meter as an LV2 event.
2271 * @return true on success.
2274 write_position(LV2_Atom_Forge* forge,
2276 const TempoMetric& t,
2277 Timecode::BBT_Time& bbt,
2279 framepos_t position,
2282 const URIMap::URIDs& urids = URIMap::instance().urids;
2284 uint8_t pos_buf[256];
2285 lv2_atom_forge_set_buffer(forge, pos_buf, sizeof(pos_buf));
2286 LV2_Atom_Forge_Frame frame;
2287 #ifdef HAVE_LV2_1_10_0
2288 lv2_atom_forge_object(forge, &frame, 1, urids.time_Position);
2289 lv2_atom_forge_key(forge, urids.time_frame);
2290 lv2_atom_forge_long(forge, position);
2291 lv2_atom_forge_key(forge, urids.time_speed);
2292 lv2_atom_forge_float(forge, speed);
2293 lv2_atom_forge_key(forge, urids.time_barBeat);
2294 lv2_atom_forge_float(forge, bbt.beats - 1 +
2295 (bbt.ticks / Timecode::BBT_Time::ticks_per_beat));
2296 lv2_atom_forge_key(forge, urids.time_bar);
2297 lv2_atom_forge_long(forge, bbt.bars - 1);
2298 lv2_atom_forge_key(forge, urids.time_beatUnit);
2299 lv2_atom_forge_int(forge, t.meter().note_divisor());
2300 lv2_atom_forge_key(forge, urids.time_beatsPerBar);
2301 lv2_atom_forge_float(forge, t.meter().divisions_per_bar());
2302 lv2_atom_forge_key(forge, urids.time_beatsPerMinute);
2303 lv2_atom_forge_float(forge, t.tempo().beats_per_minute());
2305 lv2_atom_forge_blank(forge, &frame, 1, urids.time_Position);
2306 lv2_atom_forge_property_head(forge, urids.time_frame, 0);
2307 lv2_atom_forge_long(forge, position);
2308 lv2_atom_forge_property_head(forge, urids.time_speed, 0);
2309 lv2_atom_forge_float(forge, speed);
2310 lv2_atom_forge_property_head(forge, urids.time_barBeat, 0);
2311 lv2_atom_forge_float(forge, bbt.beats - 1 +
2312 (bbt.ticks / Timecode::BBT_Time::ticks_per_beat));
2313 lv2_atom_forge_property_head(forge, urids.time_bar, 0);
2314 lv2_atom_forge_long(forge, bbt.bars - 1);
2315 lv2_atom_forge_property_head(forge, urids.time_beatUnit, 0);
2316 lv2_atom_forge_int(forge, t.meter().note_divisor());
2317 lv2_atom_forge_property_head(forge, urids.time_beatsPerBar, 0);
2318 lv2_atom_forge_float(forge, t.meter().divisions_per_bar());
2319 lv2_atom_forge_property_head(forge, urids.time_beatsPerMinute, 0);
2320 lv2_atom_forge_float(forge, t.tempo().beats_per_minute());
2323 LV2_Evbuf_Iterator end = lv2_evbuf_end(buf);
2324 const LV2_Atom* const atom = (const LV2_Atom*)pos_buf;
2325 return lv2_evbuf_write(&end, offset, 0, atom->type, atom->size,
2326 (const uint8_t*)(atom + 1));
2330 LV2Plugin::connect_and_run(BufferSet& bufs,
2331 framepos_t start, framepos_t end, double speed,
2332 ChanMapping in_map, ChanMapping out_map,
2333 pframes_t nframes, framecnt_t offset)
2335 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 run %2 offset %3\n", name(), nframes, offset));
2336 Plugin::connect_and_run(bufs, start, end, speed, in_map, out_map, nframes, offset);
2338 cycles_t then = get_cycles();
2340 TempoMap& tmap = _session.tempo_map();
2341 Metrics::const_iterator metric_i = tmap.metrics_end();
2342 TempoMetric tmetric = tmap.metric_at(start, &metric_i);
2344 if (_freewheel_control_port) {
2345 *_freewheel_control_port = _session.engine().freewheeling() ? 1.f : 0.f;
2348 if (_bpm_control_port) {
2349 *_bpm_control_port = tmetric.tempo().beats_per_minute();
2353 if (_can_write_automation && start != _next_cycle_start) {
2354 // add guard-points after locating
2355 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2356 i->second->guard = true;
2361 ChanCount bufs_count;
2362 bufs_count.set(DataType::AUDIO, 1);
2363 bufs_count.set(DataType::MIDI, 1);
2364 BufferSet& silent_bufs = _session.get_silent_buffers(bufs_count);
2365 BufferSet& scratch_bufs = _session.get_scratch_buffers(bufs_count);
2366 uint32_t const num_ports = parameter_count();
2367 uint32_t const nil_index = std::numeric_limits<uint32_t>::max();
2369 uint32_t audio_in_index = 0;
2370 uint32_t audio_out_index = 0;
2371 uint32_t midi_in_index = 0;
2372 uint32_t midi_out_index = 0;
2373 uint32_t atom_port_index = 0;
2374 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2376 uint32_t index = nil_index;
2377 PortFlags flags = _port_flags[port_index];
2379 if (flags & PORT_AUDIO) {
2380 if (flags & PORT_INPUT) {
2381 index = in_map.get(DataType::AUDIO, audio_in_index++, &valid);
2383 ? bufs.get_audio(index).data(offset)
2384 : silent_bufs.get_audio(0).data(offset);
2386 index = out_map.get(DataType::AUDIO, audio_out_index++, &valid);
2388 ? bufs.get_audio(index).data(offset)
2389 : scratch_bufs.get_audio(0).data(offset);
2391 } else if (flags & (PORT_EVENT|PORT_SEQUENCE)) {
2392 /* FIXME: The checks here for bufs.count().n_midi() > index shouldn't
2393 be necessary, but the mapping is illegal in some cases. Ideally
2394 that should be fixed, but this is easier...
2396 if (flags & PORT_MIDI) {
2397 if (flags & PORT_INPUT) {
2398 index = in_map.get(DataType::MIDI, midi_in_index++, &valid);
2400 index = out_map.get(DataType::MIDI, midi_out_index++, &valid);
2402 if (valid && bufs.count().n_midi() > index) {
2403 /* Note, ensure_lv2_bufsize() is not RT safe!
2404 * However free()/alloc() is only called if a
2405 * plugin requires a rsz:minimumSize buffersize
2406 * and the existing buffer if smaller.
2408 bufs.ensure_lv2_bufsize((flags & PORT_INPUT), index, _port_minimumSize[port_index]);
2409 _ev_buffers[port_index] = bufs.get_lv2_midi(
2410 (flags & PORT_INPUT), index, (flags & PORT_EVENT));
2412 } else if ((flags & PORT_POSITION) && (flags & PORT_INPUT)) {
2413 lv2_evbuf_reset(_atom_ev_buffers[atom_port_index], true);
2414 _ev_buffers[port_index] = _atom_ev_buffers[atom_port_index++];
2418 if (valid && (flags & PORT_INPUT)) {
2419 if ((flags & PORT_POSITION)) {
2420 Timecode::BBT_Time bbt (tmap.bbt_at_frame (start));
2421 double bpm = tmetric.tempo().beats_per_minute();
2422 double beatpos = (bbt.bars - 1) * tmetric.meter().divisions_per_bar()
2424 + (bbt.ticks / Timecode::BBT_Time::ticks_per_beat);
2425 beatpos *= tmetric.meter().note_divisor() / 4.0;
2427 // XXX BUG beats_per_minute() is most recent fixed tempo -- not ramped
2428 printf("POS:%ld <> %ld SPD: %f <> %f BBT: %f <> %f BPM: %f <> %f\n",
2429 start, _next_cycle_start,
2430 speed, _next_cycle_speed,
2431 beatpos, _next_cycle_beat,
2434 if (start != _next_cycle_start ||
2435 speed != _next_cycle_speed ||
2436 rint (1000 * beatpos) != rint(1000 * _next_cycle_beat) ||
2437 bpm != _current_bpm) {
2438 // Transport or Tempo has changed, write position at cycle start
2439 write_position(&_impl->forge, _ev_buffers[port_index],
2440 tmetric, bbt, speed, start, 0);
2444 // Get MIDI iterator range (empty range if no MIDI)
2445 MidiBuffer::iterator m = (index != nil_index)
2446 ? bufs.get_midi(index).begin()
2447 : silent_bufs.get_midi(0).end();
2448 MidiBuffer::iterator m_end = (index != nil_index)
2449 ? bufs.get_midi(index).end()
2452 // Now merge MIDI and any transport events into the buffer
2453 const uint32_t type = _uri_map.urids.midi_MidiEvent;
2454 const framepos_t tend = end;
2456 while (m != m_end || (metric_i != tmap.metrics_end() &&
2457 (*metric_i)->frame() < tend)) {
2458 MetricSection* metric = (metric_i != tmap.metrics_end())
2460 if (m != m_end && (!metric || metric->frame() > (*m).time())) {
2461 const Evoral::MIDIEvent<framepos_t> ev(*m, false);
2462 if (ev.time() < nframes) {
2463 LV2_Evbuf_Iterator eend = lv2_evbuf_end(_ev_buffers[port_index]);
2464 lv2_evbuf_write(&eend, ev.time(), 0, type, ev.size(), ev.buffer());
2468 tmetric.set_metric(metric);
2469 Timecode::BBT_Time bbt;
2470 bbt = tmap.bbt_at_pulse (metric->pulse());
2471 write_position(&_impl->forge, _ev_buffers[port_index],
2472 tmetric, bbt, speed,
2474 metric->frame() - start);
2478 } else if (!valid) {
2479 // Nothing we understand or care about, connect to scratch
2480 // see note for midi-buffer size above
2481 scratch_bufs.ensure_lv2_bufsize((flags & PORT_INPUT),
2482 0, _port_minimumSize[port_index]);
2483 _ev_buffers[port_index] = scratch_bufs.get_lv2_midi(
2484 (flags & PORT_INPUT), 0, (flags & PORT_EVENT));
2487 buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
2489 continue; // Control port, leave buffer alone
2491 lilv_instance_connect_port(_impl->instance, port_index, buf);
2494 // Read messages from UI and push into appropriate buffers
2496 uint32_t read_space = _from_ui->read_space();
2497 while (read_space > sizeof(UIMessage)) {
2499 if (_from_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
2500 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
2503 vector<uint8_t> body(msg.size);
2504 if (_from_ui->read(&body[0], msg.size) != msg.size) {
2505 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
2508 if (msg.protocol == URIMap::instance().urids.atom_eventTransfer) {
2509 LV2_Evbuf* buf = _ev_buffers[msg.index];
2510 LV2_Evbuf_Iterator i = lv2_evbuf_end(buf);
2511 const LV2_Atom* const atom = (const LV2_Atom*)&body[0];
2512 if (!lv2_evbuf_write(&i, nframes - 1, 0, atom->type, atom->size,
2513 (const uint8_t*)(atom + 1))) {
2514 error << "Failed to write data to LV2 event buffer\n";
2517 error << "Received unknown message type from UI" << endmsg;
2519 read_space -= sizeof(UIMessage) + msg.size;
2526 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2527 PortFlags flags = _port_flags[port_index];
2530 /* TODO ask drobilla about comment
2531 * "Make Ardour event buffers generic so plugins can communicate"
2532 * in libs/ardour/buffer_set.cc:310
2534 * ideally the user could choose which of the following two modes
2535 * to use (e.g. instrument/effect chains MIDI OUT vs MIDI TRHU).
2537 * This implementation follows the discussion on IRC Mar 16 2013 16:47 UTC
2538 * 16:51 < drobilla> rgareus: [..] i.e always replace with MIDI output [of LV2 plugin] if it's there
2539 * 16:52 < drobilla> rgareus: That would probably be good enough [..] to make users not complain
2540 * for quite a while at least ;)
2542 // copy output of LV2 plugin's MIDI port to Ardour MIDI buffers -- MIDI OUT
2543 if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE|PORT_MIDI))) {
2544 const uint32_t buf_index = out_map.get(
2545 DataType::MIDI, midi_out_index++, &valid);
2547 bufs.forward_lv2_midi(_ev_buffers[port_index], buf_index);
2550 // Flush MIDI (write back to Ardour MIDI buffers) -- MIDI THRU
2551 else if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE))) {
2552 const uint32_t buf_index = out_map.get(
2553 DataType::MIDI, midi_out_index++, &valid);
2555 bufs.flush_lv2_midi(true, buf_index);
2559 // Write messages to UI
2560 if ((_to_ui || _can_write_automation || _patch_port_out_index != (uint32_t)-1) &&
2561 (flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE))) {
2562 LV2_Evbuf* buf = _ev_buffers[port_index];
2563 for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(buf);
2564 lv2_evbuf_is_valid(i);
2565 i = lv2_evbuf_next(i)) {
2566 uint32_t frames, subframes, type, size;
2568 lv2_evbuf_get(i, &frames, &subframes, &type, &size, &data);
2571 // Intercept Automation Write Events
2572 if ((flags & PORT_AUTOCTRL)) {
2573 LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2574 if (atom->type == _uri_map.urids.atom_Blank ||
2575 atom->type == _uri_map.urids.atom_Object) {
2576 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2577 if (obj->body.otype == _uri_map.urids.auto_event) {
2578 // only if transport_rolling ??
2579 const LV2_Atom* parameter = NULL;
2580 const LV2_Atom* value = NULL;
2581 lv2_atom_object_get(obj,
2582 _uri_map.urids.auto_parameter, ¶meter,
2583 _uri_map.urids.auto_value, &value,
2585 if (parameter && value) {
2586 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2587 const float v = ((const LV2_Atom_Float*)value)->body;
2588 // -> add automation event..
2589 DEBUG_TRACE(DEBUG::LV2Automate,
2590 string_compose ("Event p: %1 t: %2 v: %3\n", p, frames, v));
2591 AutomationCtrlPtr c = get_automation_control (p);
2593 (c->ac->automation_state() == Touch || c->ac->automation_state() == Write)
2595 framepos_t when = std::max ((framepos_t) 0, start + frames - _current_latency);
2596 assert (start + frames - _current_latency >= 0);
2599 c->ac->list()->add (when, v, true, true);
2601 c->ac->set_double (v, when, true);
2606 else if (obj->body.otype == _uri_map.urids.auto_setup) {
2607 // TODO optional arguments, for now we assume the plugin
2608 // writes automation for its own inputs
2609 // -> put them in "touch" mode (preferably "exclusive plugin touch(TM)"
2610 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2611 if (_port_flags[i->first] & PORT_CTRLED) {
2612 DEBUG_TRACE(DEBUG::LV2Automate,
2613 string_compose ("Setup p: %1\n", i->first));
2614 i->second->ac->set_automation_state (Touch);
2618 else if (obj->body.otype == _uri_map.urids.auto_finalize) {
2619 // set [touched] parameters to "play" ??
2620 // allow plugin to change its mode (from analyze to apply)
2621 const LV2_Atom* parameter = NULL;
2622 const LV2_Atom* value = NULL;
2623 lv2_atom_object_get(obj,
2624 _uri_map.urids.auto_parameter, ¶meter,
2625 _uri_map.urids.auto_value, &value,
2627 if (parameter && value) {
2628 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2629 const float v = ((const LV2_Atom_Float*)value)->body;
2630 AutomationCtrlPtr c = get_automation_control (p);
2631 DEBUG_TRACE(DEBUG::LV2Automate,
2632 string_compose ("Finalize p: %1 v: %2\n", p, v));
2633 if (c && _port_flags[p] & PORT_CTRLER) {
2634 c->ac->set_value(v, Controllable::NoGroup);
2637 DEBUG_TRACE(DEBUG::LV2Automate, "Finalize\n");
2639 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2640 // guard will be false if an event was written
2641 if ((_port_flags[i->first] & PORT_CTRLED) && !i->second->guard) {
2642 DEBUG_TRACE(DEBUG::LV2Automate,
2643 string_compose ("Thin p: %1\n", i->first));
2644 i->second->ac->alist ()->thin (20);
2648 else if (obj->body.otype == _uri_map.urids.auto_start) {
2649 const LV2_Atom* parameter = NULL;
2650 lv2_atom_object_get(obj,
2651 _uri_map.urids.auto_parameter, ¶meter,
2654 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2655 AutomationCtrlPtr c = get_automation_control (p);
2656 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("Start Touch p: %1\n", p));
2658 c->ac->start_touch (std::max ((framepos_t)0, start - _current_latency));
2663 else if (obj->body.otype == _uri_map.urids.auto_end) {
2664 const LV2_Atom* parameter = NULL;
2665 lv2_atom_object_get(obj,
2666 _uri_map.urids.auto_parameter, ¶meter,
2669 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2670 AutomationCtrlPtr c = get_automation_control (p);
2671 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("End Touch p: %1\n", p));
2673 c->ac->stop_touch (true, std::max ((framepos_t)0, start - _current_latency));
2681 // Intercept patch change messages to emit PropertyChanged signal
2682 if ((flags & PORT_PATCHMSG)) {
2683 LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2684 if (atom->type == _uri_map.urids.atom_Blank ||
2685 atom->type == _uri_map.urids.atom_Object) {
2686 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2687 if (obj->body.otype == _uri_map.urids.patch_Set) {
2688 const LV2_Atom* property = NULL;
2689 const LV2_Atom* value = NULL;
2690 lv2_atom_object_get(obj,
2691 _uri_map.urids.patch_property, &property,
2692 _uri_map.urids.patch_value, &value,
2695 if (property && value &&
2696 property->type == _uri_map.urids.atom_URID &&
2697 value->type == _uri_map.urids.atom_Path) {
2698 const uint32_t prop_id = ((const LV2_Atom_URID*)property)->body;
2699 const char* path = (const char*)LV2_ATOM_BODY_CONST(value);
2701 // Emit PropertyChanged signal for UI
2702 // TODO: This should emit the control's Changed signal
2703 PropertyChanged(prop_id, Variant(Variant::PATH, path));
2705 std::cerr << "warning: patch:Set for unknown property" << std::endl;
2711 if (!_to_ui) continue;
2712 write_to_ui(port_index, URIMap::instance().urids.atom_eventTransfer,
2713 size + sizeof(LV2_Atom),
2714 data - sizeof(LV2_Atom));
2719 cycles_t now = get_cycles();
2720 set_cycles((uint32_t)(now - then));
2722 // Update expected transport information for next cycle so we can detect changes
2723 _next_cycle_speed = speed;
2724 _next_cycle_start = end;
2727 /* keep track of lv2:timePosition like plugins can do.
2728 * Note: for no-midi plugins, we only ever send information at cycle-start,
2729 * so it needs to be realative to that.
2731 TempoMetric t = tmap.metric_at(start);
2732 _current_bpm = t.tempo().beats_per_minute();
2733 Timecode::BBT_Time bbt (tmap.bbt_at_frame (start));
2734 double beatpos = (bbt.bars - 1) * t.meter().divisions_per_bar()
2736 + (bbt.ticks / Timecode::BBT_Time::ticks_per_beat);
2737 beatpos *= tmetric.meter().note_divisor() / 4.0;
2738 _next_cycle_beat = beatpos + nframes * speed * _current_bpm / (60.f * _session.frame_rate());
2741 if (_latency_control_port) {
2742 framecnt_t new_latency = signal_latency ();
2743 _current_latency = new_latency;
2749 LV2Plugin::parameter_is_control(uint32_t param) const
2751 assert(param < _port_flags.size());
2752 return _port_flags[param] & PORT_CONTROL;
2756 LV2Plugin::parameter_is_audio(uint32_t param) const
2758 assert(param < _port_flags.size());
2759 return _port_flags[param] & PORT_AUDIO;
2763 LV2Plugin::parameter_is_event(uint32_t param) const
2765 assert(param < _port_flags.size());
2766 return _port_flags[param] & PORT_EVENT;
2770 LV2Plugin::parameter_is_output(uint32_t param) const
2772 assert(param < _port_flags.size());
2773 return _port_flags[param] & PORT_OUTPUT;
2777 LV2Plugin::parameter_is_input(uint32_t param) const
2779 assert(param < _port_flags.size());
2780 return _port_flags[param] & PORT_INPUT;
2784 LV2Plugin::designated_bypass_port ()
2787 const LilvPort* port = NULL;
2788 LilvNode* designation = lilv_new_uri (_world.world, LV2_PROCESSING_URI__enable);
2789 port = lilv_plugin_get_port_by_designation (
2790 _impl->plugin, _world.lv2_InputPort, designation);
2791 lilv_node_free(designation);
2793 return lilv_port_get_index (_impl->plugin, port);
2800 LV2Plugin::print_parameter(uint32_t param, char* buf, uint32_t len) const
2803 if (param < parameter_count()) {
2804 snprintf(buf, len, "%.3f", get_parameter(param));
2811 boost::shared_ptr<ScalePoints>
2812 LV2Plugin::get_scale_points(uint32_t port_index) const
2814 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, port_index);
2815 LilvScalePoints* points = lilv_port_get_scale_points(_impl->plugin, port);
2817 boost::shared_ptr<ScalePoints> ret;
2822 ret = boost::shared_ptr<ScalePoints>(new ScalePoints());
2824 LILV_FOREACH(scale_points, i, points) {
2825 const LilvScalePoint* p = lilv_scale_points_get(points, i);
2826 const LilvNode* label = lilv_scale_point_get_label(p);
2827 const LilvNode* value = lilv_scale_point_get_value(p);
2828 if (label && (lilv_node_is_float(value) || lilv_node_is_int(value))) {
2829 ret->insert(make_pair(lilv_node_as_string(label),
2830 lilv_node_as_float(value)));
2834 lilv_scale_points_free(points);
2839 LV2Plugin::run(pframes_t nframes)
2841 uint32_t const N = parameter_count();
2842 for (uint32_t i = 0; i < N; ++i) {
2843 if (parameter_is_control(i) && parameter_is_input(i)) {
2844 _control_data[i] = _shadow_data[i];
2849 // Execute work synchronously if we're freewheeling (export)
2850 _worker->set_synchronous(session().engine().freewheeling());
2853 // Run the plugin for this cycle
2854 lilv_instance_run(_impl->instance, nframes);
2856 // Emit any queued worker responses (calls a plugin callback)
2857 if (_state_worker) {
2858 _state_worker->emit_responses();
2861 _worker->emit_responses();
2864 // Notify the plugin that a work run cycle is complete
2865 if (_impl->work_iface) {
2866 if (_impl->work_iface->end_run) {
2867 _impl->work_iface->end_run(_impl->instance->lv2_handle);
2873 LV2Plugin::latency_compute_run()
2875 if (!_latency_control_port) {
2879 // Run the plugin so that it can set its latency parameter
2881 bool was_activated = _was_activated;
2884 uint32_t port_index = 0;
2885 uint32_t in_index = 0;
2886 uint32_t out_index = 0;
2888 // this is done in the main thread. non realtime.
2889 const framecnt_t bufsize = _engine.samples_per_cycle();
2890 float *buffer = (float*) malloc(_engine.samples_per_cycle() * sizeof(float));
2892 memset(buffer, 0, sizeof(float) * bufsize);
2894 // FIXME: Ensure plugins can handle in-place processing
2898 while (port_index < parameter_count()) {
2899 if (parameter_is_audio(port_index)) {
2900 if (parameter_is_input(port_index)) {
2901 lilv_instance_connect_port(_impl->instance, port_index, buffer);
2903 } else if (parameter_is_output(port_index)) {
2904 lilv_instance_connect_port(_impl->instance, port_index, buffer);
2913 if (was_activated) {
2920 LV2Plugin::Impl::designated_input (const char* uri, void** bufptrs[], void** bufptr)
2922 const LilvPort* port = NULL;
2923 LilvNode* designation = lilv_new_uri(_world.world, uri);
2924 port = lilv_plugin_get_port_by_designation(
2925 plugin, _world.lv2_InputPort, designation);
2926 lilv_node_free(designation);
2928 bufptrs[lilv_port_get_index(plugin, port)] = bufptr;
2933 static bool lv2_filter (const string& str, void* /*arg*/)
2935 /* Not a dotfile, has a prefix before a period, suffix is "lv2" */
2937 return str[0] != '.' && (str.length() > 3 && str.find (".lv2") == (str.length() - 4));
2941 LV2World::LV2World()
2942 : world(lilv_world_new())
2943 , _bundle_checked(false)
2945 atom_AtomPort = lilv_new_uri(world, LV2_ATOM__AtomPort);
2946 atom_Chunk = lilv_new_uri(world, LV2_ATOM__Chunk);
2947 atom_Sequence = lilv_new_uri(world, LV2_ATOM__Sequence);
2948 atom_bufferType = lilv_new_uri(world, LV2_ATOM__bufferType);
2949 atom_supports = lilv_new_uri(world, LV2_ATOM__supports);
2950 atom_eventTransfer = lilv_new_uri(world, LV2_ATOM__eventTransfer);
2951 ev_EventPort = lilv_new_uri(world, LILV_URI_EVENT_PORT);
2952 ext_logarithmic = lilv_new_uri(world, LV2_PORT_PROPS__logarithmic);
2953 ext_notOnGUI = lilv_new_uri(world, LV2_PORT_PROPS__notOnGUI);
2954 ext_expensive = lilv_new_uri(world, LV2_PORT_PROPS__expensive);
2955 ext_causesArtifacts= lilv_new_uri(world, LV2_PORT_PROPS__causesArtifacts);
2956 ext_notAutomatic = lilv_new_uri(world, LV2_PORT_PROPS__notAutomatic);
2957 ext_rangeSteps = lilv_new_uri(world, LV2_PORT_PROPS__rangeSteps);
2958 lv2_AudioPort = lilv_new_uri(world, LILV_URI_AUDIO_PORT);
2959 lv2_ControlPort = lilv_new_uri(world, LILV_URI_CONTROL_PORT);
2960 lv2_InputPort = lilv_new_uri(world, LILV_URI_INPUT_PORT);
2961 lv2_OutputPort = lilv_new_uri(world, LILV_URI_OUTPUT_PORT);
2962 lv2_inPlaceBroken = lilv_new_uri(world, LV2_CORE__inPlaceBroken);
2963 lv2_isSideChain = lilv_new_uri(world, LV2_CORE_PREFIX "isSideChain");
2964 lv2_integer = lilv_new_uri(world, LV2_CORE__integer);
2965 lv2_default = lilv_new_uri(world, LV2_CORE__default);
2966 lv2_minimum = lilv_new_uri(world, LV2_CORE__minimum);
2967 lv2_maximum = lilv_new_uri(world, LV2_CORE__maximum);
2968 lv2_reportsLatency = lilv_new_uri(world, LV2_CORE__reportsLatency);
2969 lv2_sampleRate = lilv_new_uri(world, LV2_CORE__sampleRate);
2970 lv2_toggled = lilv_new_uri(world, LV2_CORE__toggled);
2971 lv2_enumeration = lilv_new_uri(world, LV2_CORE__enumeration);
2972 lv2_freewheeling = lilv_new_uri(world, LV2_CORE__freeWheeling);
2973 midi_MidiEvent = lilv_new_uri(world, LILV_URI_MIDI_EVENT);
2974 rdfs_comment = lilv_new_uri(world, LILV_NS_RDFS "comment");
2975 rdfs_label = lilv_new_uri(world, LILV_NS_RDFS "label");
2976 rdfs_range = lilv_new_uri(world, LILV_NS_RDFS "range");
2977 rsz_minimumSize = lilv_new_uri(world, LV2_RESIZE_PORT__minimumSize);
2978 time_Position = lilv_new_uri(world, LV2_TIME__Position);
2979 ui_GtkUI = lilv_new_uri(world, LV2_UI__GtkUI);
2980 ui_external = lilv_new_uri(world, "http://lv2plug.in/ns/extensions/ui#external");
2981 ui_externalkx = lilv_new_uri(world, "http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget");
2982 units_unit = lilv_new_uri(world, LV2_UNITS__unit);
2983 units_render = lilv_new_uri(world, LV2_UNITS__render);
2984 units_hz = lilv_new_uri(world, LV2_UNITS__hz);
2985 units_midiNote = lilv_new_uri(world, LV2_UNITS__midiNote);
2986 units_db = lilv_new_uri(world, LV2_UNITS__db);
2987 patch_writable = lilv_new_uri(world, LV2_PATCH__writable);
2988 patch_Message = lilv_new_uri(world, LV2_PATCH__Message);
2990 lv2_noSampleAccurateCtrl = lilv_new_uri(world, "http://ardour.org/lv2/ext#noSampleAccurateControls");
2991 auto_can_write_automatation = lilv_new_uri(world, LV2_AUTOMATE_URI__can_write);
2992 auto_automation_control = lilv_new_uri(world, LV2_AUTOMATE_URI__control);
2993 auto_automation_controlled = lilv_new_uri(world, LV2_AUTOMATE_URI__controlled);
2994 auto_automation_controller = lilv_new_uri(world, LV2_AUTOMATE_URI__controller);
2996 #ifdef HAVE_LV2_1_2_0
2997 bufz_powerOf2BlockLength = lilv_new_uri(world, LV2_BUF_SIZE__powerOf2BlockLength);
2998 bufz_fixedBlockLength = lilv_new_uri(world, LV2_BUF_SIZE__fixedBlockLength);
2999 bufz_nominalBlockLength = lilv_new_uri(world, "http://lv2plug.in/ns/ext/buf-size#nominalBlockLength");
3004 LV2World::~LV2World()
3009 #ifdef HAVE_LV2_1_2_0
3010 lilv_node_free(bufz_nominalBlockLength);
3011 lilv_node_free(bufz_fixedBlockLength);
3012 lilv_node_free(bufz_powerOf2BlockLength);
3015 lilv_node_free(lv2_noSampleAccurateCtrl);
3016 lilv_node_free(auto_can_write_automatation);
3017 lilv_node_free(auto_automation_control);
3018 lilv_node_free(auto_automation_controlled);
3019 lilv_node_free(auto_automation_controller);
3021 lilv_node_free(patch_Message);
3022 lilv_node_free(patch_writable);
3023 lilv_node_free(units_hz);
3024 lilv_node_free(units_midiNote);
3025 lilv_node_free(units_db);
3026 lilv_node_free(units_unit);
3027 lilv_node_free(units_render);
3028 lilv_node_free(ui_externalkx);
3029 lilv_node_free(ui_external);
3030 lilv_node_free(ui_GtkUI);
3031 lilv_node_free(time_Position);
3032 lilv_node_free(rsz_minimumSize);
3033 lilv_node_free(rdfs_comment);
3034 lilv_node_free(rdfs_label);
3035 lilv_node_free(rdfs_range);
3036 lilv_node_free(midi_MidiEvent);
3037 lilv_node_free(lv2_enumeration);
3038 lilv_node_free(lv2_freewheeling);
3039 lilv_node_free(lv2_toggled);
3040 lilv_node_free(lv2_sampleRate);
3041 lilv_node_free(lv2_reportsLatency);
3042 lilv_node_free(lv2_integer);
3043 lilv_node_free(lv2_isSideChain);
3044 lilv_node_free(lv2_inPlaceBroken);
3045 lilv_node_free(lv2_OutputPort);
3046 lilv_node_free(lv2_InputPort);
3047 lilv_node_free(lv2_ControlPort);
3048 lilv_node_free(lv2_AudioPort);
3049 lilv_node_free(ext_rangeSteps);
3050 lilv_node_free(ext_notAutomatic);
3051 lilv_node_free(ext_causesArtifacts);
3052 lilv_node_free(ext_expensive);
3053 lilv_node_free(ext_notOnGUI);
3054 lilv_node_free(ext_logarithmic);
3055 lilv_node_free(ev_EventPort);
3056 lilv_node_free(atom_supports);
3057 lilv_node_free(atom_eventTransfer);
3058 lilv_node_free(atom_bufferType);
3059 lilv_node_free(atom_Sequence);
3060 lilv_node_free(atom_Chunk);
3061 lilv_node_free(atom_AtomPort);
3062 lilv_world_free(world);
3067 LV2World::load_bundled_plugins(bool verbose)
3069 if (!_bundle_checked) {
3071 cout << "Scanning folders for bundled LV2s: " << ARDOUR::lv2_bundled_search_path().to_string() << endl;
3074 vector<string> plugin_objects;
3075 find_paths_matching_filter (plugin_objects, ARDOUR::lv2_bundled_search_path(), lv2_filter, 0, true, true, true);
3076 for ( vector<string>::iterator x = plugin_objects.begin(); x != plugin_objects.end (); ++x) {
3077 #ifdef PLATFORM_WINDOWS
3078 string uri = "file:///" + *x + "/";
3080 string uri = "file://" + *x + "/";
3082 LilvNode *node = lilv_new_uri(world, uri.c_str());
3083 lilv_world_load_bundle(world, node);
3084 lilv_node_free(node);
3087 lilv_world_load_all(world);
3088 _bundle_checked = true;
3092 LV2PluginInfo::LV2PluginInfo (const char* plugin_uri)
3095 _plugin_uri = strdup(plugin_uri);
3098 LV2PluginInfo::~LV2PluginInfo()
3105 LV2PluginInfo::load(Session& session)
3109 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
3110 LilvNode* uri = lilv_new_uri(_world.world, _plugin_uri);
3111 if (!uri) { throw failed_constructor(); }
3112 const LilvPlugin* lp = lilv_plugins_get_by_uri(plugins, uri);
3113 if (!lp) { throw failed_constructor(); }
3114 plugin.reset(new LV2Plugin(session.engine(), session, lp, session.frame_rate()));
3115 lilv_node_free(uri);
3116 plugin->set_info(PluginInfoPtr(shared_from_this ()));
3118 } catch (failed_constructor& err) {
3119 return PluginPtr((Plugin*)0);
3125 std::vector<Plugin::PresetRecord>
3126 LV2PluginInfo::get_presets (bool /*user_only*/) const
3128 std::vector<Plugin::PresetRecord> p;
3129 #ifndef NO_PLUGIN_STATE
3130 const LilvPlugin* lp = NULL;
3133 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
3134 LilvNode* uri = lilv_new_uri(_world.world, _plugin_uri);
3135 if (!uri) { throw failed_constructor(); }
3136 lp = lilv_plugins_get_by_uri(plugins, uri);
3137 if (!lp) { throw failed_constructor(); }
3138 lilv_node_free(uri);
3139 } catch (failed_constructor& err) {
3143 // see LV2Plugin::find_presets
3144 LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
3145 LilvNode* pset_Preset = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
3146 LilvNode* rdfs_label = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
3148 LilvNodes* presets = lilv_plugin_get_related(lp, pset_Preset);
3149 LILV_FOREACH(nodes, i, presets) {
3150 const LilvNode* preset = lilv_nodes_get(presets, i);
3151 lilv_world_load_resource(_world.world, preset);
3152 LilvNode* name = get_value(_world.world, preset, rdfs_label);
3153 bool userpreset = true; // TODO
3155 p.push_back (Plugin::PresetRecord (lilv_node_as_string(preset), lilv_node_as_string(name), userpreset));
3156 lilv_node_free(name);
3159 lilv_nodes_free(presets);
3160 lilv_node_free(rdfs_label);
3161 lilv_node_free(pset_Preset);
3162 lilv_node_free(lv2_appliesTo);
3168 LV2PluginInfo::in_category (const std::string &c) const
3170 // TODO use untranslated lilv_plugin_get_class()
3171 // match gtk2_ardour/plugin_selector.cc
3172 if (category == c) {
3179 LV2PluginInfo::is_instrument () const
3181 if (category == "Instrument") {
3185 /* until we make sure that category remains untranslated in the lv2.ttl spec
3186 * and until most instruments also classify themselves as such, there's a 2nd check:
3188 if (n_inputs.n_midi() > 0 && n_inputs.n_audio() == 0 && n_outputs.n_audio() > 0) {
3196 LV2PluginInfo::discover()
3199 world.load_bundled_plugins();
3200 _world.load_bundled_plugins(true);
3202 PluginInfoList* plugs = new PluginInfoList;
3203 const LilvPlugins* plugins = lilv_world_get_all_plugins(world.world);
3205 LILV_FOREACH(plugins, i, plugins) {
3206 const LilvPlugin* p = lilv_plugins_get(plugins, i);
3207 const LilvNode* pun = lilv_plugin_get_uri(p);
3209 LV2PluginInfoPtr info(new LV2PluginInfo(lilv_node_as_string(pun)));
3211 LilvNode* name = lilv_plugin_get_name(p);
3212 if (!name || !lilv_plugin_get_port_by_index(p, 0)) {
3213 warning << "Ignoring invalid LV2 plugin "
3214 << lilv_node_as_string(lilv_plugin_get_uri(p))
3219 if (lilv_plugin_has_feature(p, world.lv2_inPlaceBroken)) {
3220 warning << string_compose(
3221 _("Ignoring LV2 plugin \"%1\" since it cannot do inplace processing."),
3222 lilv_node_as_string(name)) << endmsg;
3223 lilv_node_free(name);
3227 #ifdef HAVE_LV2_1_2_0
3228 LilvNodes *required_features = lilv_plugin_get_required_features (p);
3229 if (lilv_nodes_contains (required_features, world.bufz_powerOf2BlockLength) ||
3230 lilv_nodes_contains (required_features, world.bufz_fixedBlockLength)
3232 warning << string_compose(
3233 _("Ignoring LV2 plugin \"%1\" because its buffer-size requirements cannot be satisfied."),
3234 lilv_node_as_string(name)) << endmsg;
3235 lilv_nodes_free(required_features);
3236 lilv_node_free(name);
3239 lilv_nodes_free(required_features);
3244 info->name = string(lilv_node_as_string(name));
3245 lilv_node_free(name);
3246 ARDOUR::PluginScanMessage(_("LV2"), info->name, false);
3248 const LilvPluginClass* pclass = lilv_plugin_get_class(p);
3249 const LilvNode* label = lilv_plugin_class_get_label(pclass);
3250 info->category = lilv_node_as_string(label);
3252 LilvNode* author_name = lilv_plugin_get_author_name(p);
3253 info->creator = author_name ? string(lilv_node_as_string(author_name)) : "Unknown";
3254 lilv_node_free(author_name);
3256 info->path = "/NOPATH"; // Meaningless for LV2
3258 /* count atom-event-ports that feature
3259 * atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent>
3261 * TODO: nicely ask drobilla to make a lilv_ call for that
3263 int count_midi_out = 0;
3264 int count_midi_in = 0;
3265 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
3266 const LilvPort* port = lilv_plugin_get_port_by_index(p, i);
3267 if (lilv_port_is_a(p, port, world.atom_AtomPort)) {
3268 LilvNodes* buffer_types = lilv_port_get_value(
3269 p, port, world.atom_bufferType);
3270 LilvNodes* atom_supports = lilv_port_get_value(
3271 p, port, world.atom_supports);
3273 if (lilv_nodes_contains(buffer_types, world.atom_Sequence)
3274 && lilv_nodes_contains(atom_supports, world.midi_MidiEvent)) {
3275 if (lilv_port_is_a(p, port, world.lv2_InputPort)) {
3278 if (lilv_port_is_a(p, port, world.lv2_OutputPort)) {
3282 lilv_nodes_free(buffer_types);
3283 lilv_nodes_free(atom_supports);
3287 info->n_inputs.set_audio(
3288 lilv_plugin_get_num_ports_of_class(
3289 p, world.lv2_InputPort, world.lv2_AudioPort, NULL));
3290 info->n_inputs.set_midi(
3291 lilv_plugin_get_num_ports_of_class(
3292 p, world.lv2_InputPort, world.ev_EventPort, NULL)
3295 info->n_outputs.set_audio(
3296 lilv_plugin_get_num_ports_of_class(
3297 p, world.lv2_OutputPort, world.lv2_AudioPort, NULL));
3298 info->n_outputs.set_midi(
3299 lilv_plugin_get_num_ports_of_class(
3300 p, world.lv2_OutputPort, world.ev_EventPort, NULL)
3303 info->unique_id = lilv_node_as_uri(lilv_plugin_get_uri(p));
3304 info->index = 0; // Meaningless for LV2
3306 plugs->push_back(info);