2 * Copyright (C) 2008-2012 Carl Hetherington <carl@carlh.net>
3 * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2008-2019 David Robillard <d@drobilla.net>
5 * Copyright (C) 2012-2019 Robin Gareus <robin@gareus.org>
6 * Copyright (C) 2013-2018 John Emmas <john@creativepost.co.uk>
7 * Copyright (C) 2013 Michael R. Fisher <mfisher@bketech.com>
8 * Copyright (C) 2014-2016 Tim Mayberry <mojofunk@gmail.com>
9 * Copyright (C) 2016-2017 Damien Zammit <damien@zamaudio.com>
10 * Copyright (C) 2016 Nick Mainsbridge <mainsbridge@gmail.com>
11 * Copyright (C) 2017 Johannes Mueller <github@johannes-mueller.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
37 #include "pbd/gstdio_compat.h"
38 #include <glib/gprintf.h>
41 #include <boost/utility.hpp>
43 #include "pbd/file_utils.h"
44 #include "pbd/stl_delete.h"
45 #include "pbd/compose.h"
46 #include "pbd/error.h"
47 #include "pbd/locale_guard.h"
48 #include "pbd/pthread_utils.h"
49 #include "pbd/replace_all.h"
50 #include "pbd/xml++.h"
52 #ifdef PLATFORM_WINDOWS
53 #include <shlobj.h> // CSIDL_*
54 #include "pbd/windows_special_dirs.h"
58 #include "libardour-config.h"
61 #include "ardour/audio_buffer.h"
62 #include "ardour/audioengine.h"
63 #include "ardour/directory_names.h"
64 #include "ardour/debug.h"
65 #include "ardour/lv2_plugin.h"
66 #include "ardour/midi_patch_manager.h"
67 #include "ardour/session.h"
68 #include "ardour/tempo.h"
69 #include "ardour/types.h"
70 #include "ardour/utils.h"
71 #include "ardour/worker.h"
72 #include "ardour/search_paths.h"
77 #include <lilv/lilv.h>
79 #include "lv2/lv2plug.in/ns/ext/atom/atom.h"
80 #include "lv2/lv2plug.in/ns/ext/atom/forge.h"
81 #include "lv2/lv2plug.in/ns/ext/log/log.h"
82 #include "lv2/lv2plug.in/ns/ext/midi/midi.h"
83 #include "lv2/lv2plug.in/ns/ext/port-props/port-props.h"
84 #include "lv2/lv2plug.in/ns/ext/presets/presets.h"
85 #include "lv2/lv2plug.in/ns/ext/state/state.h"
86 #include "lv2/lv2plug.in/ns/ext/time/time.h"
87 #include "lv2/lv2plug.in/ns/ext/worker/worker.h"
88 #include "lv2/lv2plug.in/ns/ext/resize-port/resize-port.h"
89 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
90 #include "lv2/lv2plug.in/ns/extensions/units/units.h"
91 #include "lv2/lv2plug.in/ns/ext/patch/patch.h"
92 #include "lv2/lv2plug.in/ns/ext/port-groups/port-groups.h"
93 #include "lv2/lv2plug.in/ns/ext/parameters/parameters.h"
94 #include "lv2/lv2plug.in/ns/ext/buf-size/buf-size.h"
95 #include "lv2/lv2plug.in/ns/ext/options/options.h"
97 #include "lv2_evbuf.h"
100 #include <suil/suil.h>
103 // Compatibility for old LV2
104 #ifndef LV2_ATOM_CONTENTS_CONST
105 #define LV2_ATOM_CONTENTS_CONST(type, atom) \
106 ((const void*)((const uint8_t*)(atom) + sizeof(type)))
108 #ifndef LV2_ATOM_BODY_CONST
109 #define LV2_ATOM_BODY_CONST(atom) LV2_ATOM_CONTENTS_CONST(LV2_Atom, atom)
111 #ifndef LV2_PATCH__property
112 #define LV2_PATCH__property LV2_PATCH_PREFIX "property"
114 #ifndef LV2_PATCH__value
115 #define LV2_PATCH__value LV2_PATCH_PREFIX "value"
117 #ifndef LV2_PATCH__writable
118 #define LV2_PATCH__writable LV2_PATCH_PREFIX "writable"
121 /** The number of MIDI buffers that will fit in a UI/worker comm buffer.
122 This needs to be roughly the number of cycles the UI will get around to
123 actually processing the traffic. Lower values are flakier but save memory.
125 static const size_t NBUFS = 4;
128 using namespace ARDOUR;
131 bool LV2Plugin::force_state_save = false;
132 uint32_t LV2Plugin::_ui_background_color = 0x000000ff; // RGBA
133 uint32_t LV2Plugin::_ui_foreground_color = 0xffffffff; // RGBA
134 float LV2Plugin::_ui_scale_factor = 1.0;
136 class LV2World : boost::noncopyable {
141 void load_bundled_plugins(bool verbose=false);
145 LilvNode* atom_AtomPort;
146 LilvNode* atom_Chunk;
147 LilvNode* atom_Sequence;
148 LilvNode* atom_bufferType;
149 LilvNode* atom_eventTransfer;
150 LilvNode* atom_supports;
151 LilvNode* ev_EventPort;
152 LilvNode* ext_logarithmic;
153 LilvNode* ext_notOnGUI;
154 LilvNode* ext_expensive;
155 LilvNode* ext_causesArtifacts;
156 LilvNode* ext_notAutomatic;
157 LilvNode* ext_rangeSteps;
158 LilvNode* groups_group;
159 LilvNode* groups_element;
160 LilvNode* lv2_AudioPort;
161 LilvNode* lv2_ControlPort;
162 LilvNode* lv2_InputPort;
163 LilvNode* lv2_OutputPort;
164 LilvNode* lv2_designation;
165 LilvNode* lv2_enumeration;
166 LilvNode* lv2_freewheeling;
167 LilvNode* lv2_inPlaceBroken;
168 LilvNode* lv2_isSideChain;
170 LilvNode* lv2_integer;
171 LilvNode* lv2_default;
172 LilvNode* lv2_minimum;
173 LilvNode* lv2_maximum;
174 LilvNode* lv2_reportsLatency;
175 LilvNode* lv2_sampleRate;
176 LilvNode* lv2_toggled;
177 LilvNode* midi_MidiEvent;
178 LilvNode* rdfs_comment;
179 LilvNode* rdfs_label;
180 LilvNode* rdfs_range;
181 LilvNode* rsz_minimumSize;
182 LilvNode* time_Position;
183 LilvNode* time_beatsPerMin;
185 LilvNode* ui_external;
186 LilvNode* ui_externalkx;
189 LilvNode* units_unit;
190 LilvNode* units_render;
191 LilvNode* units_midiNote;
192 LilvNode* patch_writable;
193 LilvNode* patch_Message;
194 LilvNode* opts_requiredOptions;
195 LilvNode* bufz_powerOf2BlockLength;
196 LilvNode* bufz_fixedBlockLength;
197 LilvNode* bufz_nominalBlockLength;
198 LilvNode* bufz_coarseBlockLength;
200 #ifdef HAVE_LV2_1_10_0
202 LilvNode* atom_float;
203 LilvNode* atom_object; // new in 1.8
204 LilvNode* atom_vector;
207 LilvNode* lv2_noSampleAccurateCtrl;
208 LilvNode* routing_connectAllOutputs; // lv2:optionalFeature
209 LilvNode* auto_can_write_automatation; // lv2:optionalFeature
210 LilvNode* auto_automation_control; // atom:supports
211 LilvNode* auto_automation_controlled; // lv2:portProperty
212 LilvNode* auto_automation_controller; // lv2:portProperty
213 LilvNode* inline_display_in_gui; // lv2:optionalFeature
217 bool _bundle_checked;
220 static LV2World _world;
222 /* worker extension */
224 /** Called by the plugin to schedule non-RT work. */
225 static LV2_Worker_Status
226 work_schedule(LV2_Worker_Schedule_Handle handle,
230 return (((Worker*)handle)->schedule(size, data)
232 : LV2_WORKER_ERR_UNKNOWN);
235 /** Called by the plugin to respond to non-RT work. */
236 static LV2_Worker_Status
237 work_respond(LV2_Worker_Respond_Handle handle,
241 return (((Worker*)handle)->respond(size, data)
243 : LV2_WORKER_ERR_UNKNOWN);
247 set_port_value(const char* port_symbol,
253 LV2Plugin* self = (LV2Plugin*)user_data;
254 if (type != 0 && type != URIMap::instance().urids.atom_Float) {
255 return; // TODO: Support non-float ports
258 const uint32_t port_index = self->port_index(port_symbol);
259 if (port_index != (uint32_t)-1) {
260 self->set_parameter(port_index, *(const float*)value);
261 self->PresetPortSetValue (port_index, *(const float*)value); /* EMIT SIGNAL */
266 /* inline display extension */
268 LV2Plugin::queue_draw (LV2_Inline_Display_Handle handle)
270 LV2Plugin* plugin = (LV2Plugin*)handle;
271 plugin->QueueDraw(); /* EMIT SIGNAL */
275 LV2Plugin::midnam_update (LV2_Midnam_Handle handle)
277 LV2Plugin* plugin = (LV2Plugin*)handle;
278 plugin->_midnam_dirty = true;
279 plugin->UpdateMidnam (); /* EMIT SIGNAL */
283 LV2Plugin::bankpatch_notify (LV2_BankPatch_Handle handle, uint8_t chn, uint32_t bank, uint8_t pgm)
285 LV2Plugin* plugin = (LV2Plugin*)handle;
289 plugin->seen_bankpatch = true;
290 if (pgm > 127 || bank > 16383) {
291 plugin->_bankpatch[chn] = UINT32_MAX;
293 plugin->_bankpatch[chn] = (bank << 7) | pgm;
295 plugin->BankPatchChange (chn); /* EMIT SIGNAL */
302 log_vprintf(LV2_Log_Handle /*handle*/,
308 const int ret = g_vasprintf(&str, fmt, args);
309 /* strip trailing whitespace */
310 while (strlen (str) > 0 && isspace (str[strlen (str) - 1])) {
311 str[strlen (str) - 1] = '\0';
313 if (strlen (str) == 0) {
317 if (type == URIMap::instance().urids.log_Error) {
318 error << str << endmsg;
319 } else if (type == URIMap::instance().urids.log_Warning) {
320 warning << str << endmsg;
321 } else if (type == URIMap::instance().urids.log_Note) {
322 info << str << endmsg;
323 } else if (type == URIMap::instance().urids.log_Trace) {
324 DEBUG_TRACE(DEBUG::LV2, str);
330 log_printf(LV2_Log_Handle handle,
332 const char* fmt, ...)
336 const int ret = log_vprintf(handle, type, fmt, args);
341 struct LV2Plugin::Impl {
342 Impl() : plugin(0), ui(0), ui_type(0), name(0), author(0), instance(0)
354 /** Find the LV2 input port with the given designation.
355 * If found, bufptrs[port_index] will be set to bufptr.
357 const LilvPort* designated_input (const char* uri, void** bufptrs[], void** bufptr);
359 const LilvPlugin* plugin;
361 const LilvNode* ui_type;
364 LilvInstance* instance;
365 const LV2_Worker_Interface* work_iface;
366 const LV2_Options_Interface* opts_iface;
368 LV2_Atom_Forge forge;
369 LV2_Atom_Forge ui_forge;
370 int32_t block_length;
371 LV2_Options_Option* options;
373 LV2_Inline_Display* queue_draw;
375 LV2_BankPatch* bankpatch;
379 LV2Plugin::LV2Plugin (AudioEngine& engine,
381 const void* c_plugin,
383 : Plugin (engine, session)
388 , _state_worker(NULL)
390 , _bpm_control_port_index((uint32_t)-1)
391 , _patch_port_in_index((uint32_t)-1)
392 , _patch_port_out_index((uint32_t)-1)
393 , _uri_map(URIMap::instance())
394 , _no_sample_accurate_ctrl (false)
395 , _connect_all_audio_outputs (false)
397 init(c_plugin, rate);
398 latency_compute_run();
401 LV2Plugin::LV2Plugin (const LV2Plugin& other)
407 , _state_worker(NULL)
408 , _insert_id(other._insert_id)
409 , _bpm_control_port_index((uint32_t)-1)
410 , _patch_port_in_index((uint32_t)-1)
411 , _patch_port_out_index((uint32_t)-1)
412 , _uri_map(URIMap::instance())
413 , _no_sample_accurate_ctrl (false)
414 , _connect_all_audio_outputs (false)
416 init(other._impl->plugin, other._sample_rate);
418 XMLNode root (other.state_node_name ());
419 other.add_state (&root);
420 set_state (root, Stateful::loading_state_version);
422 for (uint32_t i = 0; i < parameter_count(); ++i) {
423 _control_data[i] = other._shadow_data[i];
424 _shadow_data[i] = other._shadow_data[i];
427 latency_compute_run();
431 LV2Plugin::init(const void* c_plugin, samplecnt_t rate)
433 DEBUG_TRACE(DEBUG::LV2, "init\n");
435 _impl->plugin = (const LilvPlugin*)c_plugin;
437 _impl->ui_type = NULL;
442 _atom_ev_buffers = 0;
444 _bpm_control_port = 0;
445 _freewheel_control_port = 0;
446 _latency_control_port = 0;
447 _next_cycle_start = std::numeric_limits<samplepos_t>::max();
448 _next_cycle_speed = 1.0;
449 _seq_size = _engine.raw_buffer_size(DataType::MIDI);
451 _was_activated = false;
452 _has_state_interface = false;
453 _can_write_automation = false;
455 _inline_display_in_gui = false;
458 _current_latency = 0;
459 _impl->block_length = _session.get_block_size();
462 _fsample_rate = rate;
464 _instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
465 _data_access_feature.URI = "http://lv2plug.in/ns/ext/data-access";
466 _make_path_feature.URI = LV2_STATE__makePath;
467 _log_feature.URI = LV2_LOG__log;
468 _work_schedule_feature.URI = LV2_WORKER__schedule;
469 _work_schedule_feature.data = NULL;
470 _def_state_feature.URI = LV2_STATE_PREFIX "loadDefaultState"; // Post LV2-1.2.0
471 _def_state_feature.data = NULL;
473 const LilvPlugin* plugin = _impl->plugin;
475 LilvNode* state_iface_uri = lilv_new_uri(_world.world, LV2_STATE__interface);
476 LilvNode* state_uri = lilv_new_uri(_world.world, LV2_STATE_URI);
477 _has_state_interface =
478 // What plugins should have (lv2:extensionData state:Interface)
479 lilv_plugin_has_extension_data(plugin, state_iface_uri)
480 // What some outdated/incorrect ones have
481 || lilv_plugin_has_feature(plugin, state_uri);
482 lilv_node_free(state_uri);
483 lilv_node_free(state_iface_uri);
485 _features = (LV2_Feature**)calloc(14, sizeof(LV2_Feature*));
486 _features[0] = &_instance_access_feature;
487 _features[1] = &_data_access_feature;
488 _features[2] = &_make_path_feature;
489 _features[3] = _uri_map.uri_map_feature();
490 _features[4] = _uri_map.urid_map_feature();
491 _features[5] = _uri_map.urid_unmap_feature();
492 _features[6] = &_log_feature;
494 unsigned n_features = 7;
495 _features[n_features++] = &_def_state_feature;
497 lv2_atom_forge_init(&_impl->forge, _uri_map.urid_map());
498 lv2_atom_forge_init(&_impl->ui_forge, _uri_map.urid_map());
501 _impl->queue_draw = (LV2_Inline_Display*)
502 malloc (sizeof(LV2_Inline_Display));
503 _impl->queue_draw->handle = this;
504 _impl->queue_draw->queue_draw = queue_draw;
506 _queue_draw_feature.URI = LV2_INLINEDISPLAY__queue_draw;
507 _queue_draw_feature.data = _impl->queue_draw;
508 _features[n_features++] = &_queue_draw_feature;
510 _impl->midnam = (LV2_Midnam*)
511 malloc (sizeof(LV2_Midnam));
512 _impl->midnam->handle = this;
513 _impl->midnam->update = midnam_update;
515 _midnam_feature.URI = LV2_MIDNAM__update;
516 _midnam_feature.data = _impl->midnam;
517 _features[n_features++] = &_midnam_feature;
519 _impl->bankpatch = (LV2_BankPatch*)
520 malloc (sizeof(LV2_BankPatch));
521 _impl->bankpatch->handle = this;
522 _impl->bankpatch->notify = bankpatch_notify;
524 _bankpatch_feature.URI = LV2_BANKPATCH__notify;
525 _bankpatch_feature.data = _impl->bankpatch;
526 _features[n_features++] = &_bankpatch_feature;
529 LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
530 LV2_URID atom_Float = _uri_map.uri_to_id(LV2_ATOM__Float);
532 static const int32_t _min_block_length = 1; // may happen during split-cycles
533 static const int32_t _max_block_length = 8192; // max possible (with all engines and during export)
534 static const int32_t rt_policy = PBD_SCHED_FIFO;
535 static const int32_t rt_priority = pbd_absolute_rt_priority (PBD_SCHED_FIFO, AudioEngine::instance()->client_real_time_priority () - 2);
536 /* Consider updating max-block-size whenever the buffersize changes.
537 * It requires re-instantiating the plugin (which is a non-realtime operation),
538 * so it should be done lightly and only for plugins that require it.
540 * given that the block-size can change at any time (split-cycles) ardour currently
541 * does not support plugins that require bufz_fixedBlockLength.
543 LV2_Options_Option options[] = {
544 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__minBlockLength),
545 sizeof(int32_t), atom_Int, &_min_block_length },
546 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__maxBlockLength),
547 sizeof(int32_t), atom_Int, &_max_block_length },
548 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__sequenceSize),
549 sizeof(int32_t), atom_Int, &_seq_size },
550 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_PARAMETERS__sampleRate),
551 sizeof(float), atom_Float, &_fsample_rate },
552 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://lv2plug.in/ns/ext/buf-size#nominalBlockLength"),
553 sizeof(int32_t), atom_Int, &_impl->block_length },
554 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://ardour.org/lv2/threads/#schedPolicy"),
555 sizeof(int32_t), atom_Int, &rt_policy },
556 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://ardour.org/lv2/threads/#schedPriority"),
557 sizeof(int32_t), atom_Int, &rt_priority },
558 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://lv2plug.in/ns/extensions/ui#backgroundColor"),
559 sizeof(int32_t), atom_Int, &_ui_background_color },
560 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://lv2plug.in/ns/extensions/ui#foregroundColor"),
561 sizeof(int32_t), atom_Int, &_ui_foreground_color },
562 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://lv2plug.in/ns/extensions/ui#scaleFactor"),
563 sizeof(float), atom_Float, &_ui_scale_factor },
564 { LV2_OPTIONS_INSTANCE, 0, 0, 0, 0, NULL }
567 _impl->options = (LV2_Options_Option*) malloc (sizeof (options));
568 memcpy ((void*) _impl->options, (void*) options, sizeof (options));
570 _options_feature.URI = LV2_OPTIONS__options;
571 _options_feature.data = _impl->options;
572 _features[n_features++] = &_options_feature;
575 seen_bankpatch = false;
576 for (uint32_t chn = 0; chn < 16; ++chn) {
577 _bankpatch[chn] = UINT32_MAX;
581 LV2_State_Make_Path* make_path = (LV2_State_Make_Path*)malloc(
582 sizeof(LV2_State_Make_Path));
583 make_path->handle = this;
584 make_path->path = &lv2_state_make_path;
585 _make_path_feature.data = make_path;
587 LV2_Log_Log* log = (LV2_Log_Log*)malloc(sizeof(LV2_Log_Log));
589 log->printf = &log_printf;
590 log->vprintf = &log_vprintf;
591 _log_feature.data = log;
593 const size_t ring_size = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
594 LilvNode* worker_schedule = lilv_new_uri(_world.world, LV2_WORKER__schedule);
595 if (lilv_plugin_has_feature(plugin, worker_schedule)) {
596 LV2_Worker_Schedule* schedule = (LV2_Worker_Schedule*)malloc(
597 sizeof(LV2_Worker_Schedule));
598 _worker = new Worker(this, ring_size);
599 schedule->handle = _worker;
600 schedule->schedule_work = work_schedule;
601 _work_schedule_feature.data = schedule;
602 _features[n_features++] = &_work_schedule_feature;
604 lilv_node_free(worker_schedule);
606 if (_has_state_interface) {
607 // Create a non-threaded worker for use by state restore
608 _state_worker = new Worker(this, ring_size, false);
611 _impl->instance = lilv_plugin_instantiate(plugin, rate, _features);
612 _impl->name = lilv_plugin_get_name(plugin);
613 _impl->author = lilv_plugin_get_author_name(plugin);
615 if (_impl->instance == 0) {
616 error << _("LV2: Failed to instantiate plugin ") << uri() << endmsg;
617 throw failed_constructor();
620 _instance_access_feature.data = (void*)_impl->instance->lv2_handle;
621 _data_access_extension_data.extension_data = _impl->instance->lv2_descriptor->extension_data;
622 _data_access_feature.data = &_data_access_extension_data;
624 LilvNode* worker_iface_uri = lilv_new_uri(_world.world, LV2_WORKER__interface);
625 if (lilv_plugin_has_extension_data(plugin, worker_iface_uri)) {
626 _impl->work_iface = (const LV2_Worker_Interface*)extension_data(
627 LV2_WORKER__interface);
629 lilv_node_free(worker_iface_uri);
631 LilvNode* options_iface_uri = lilv_new_uri(_world.world, LV2_OPTIONS__interface);
632 if (lilv_plugin_has_extension_data(plugin, options_iface_uri)) {
633 _impl->opts_iface = (const LV2_Options_Interface*)extension_data(
634 LV2_OPTIONS__interface);
636 lilv_node_free(options_iface_uri);
639 _display_interface = (const LV2_Inline_Display_Interface*)
640 extension_data (LV2_INLINEDISPLAY__interface);
642 _midname_interface = (const LV2_Midnam_Interface*)
643 extension_data (LV2_MIDNAM__interface);
644 if (_midname_interface) {
645 _midnam_dirty = true;
650 if (lilv_plugin_has_feature(plugin, _world.lv2_inPlaceBroken)) {
651 error << string_compose(
652 _("LV2: \"%1\" cannot be used, since it cannot do inplace processing."),
653 lilv_node_as_string(_impl->name)) << endmsg;
654 lilv_node_free(_impl->name);
655 lilv_node_free(_impl->author);
656 throw failed_constructor();
659 LilvNodes* optional_features = lilv_plugin_get_optional_features (plugin);
660 if (lilv_nodes_contains (optional_features, _world.bufz_coarseBlockLength)) {
661 _no_sample_accurate_ctrl = true;
665 if (lilv_nodes_contains (optional_features, _world.lv2_noSampleAccurateCtrl)) {
666 /* deprecated 2016-Sep-18 in favor of bufz_coarseBlockLength */
667 _no_sample_accurate_ctrl = true;
669 if (lilv_nodes_contains (optional_features, _world.routing_connectAllOutputs)) {
670 _connect_all_audio_outputs = true;
672 if (lilv_nodes_contains (optional_features, _world.auto_can_write_automatation)) {
673 _can_write_automation = true;
675 if (lilv_nodes_contains (optional_features, _world.inline_display_in_gui)) {
676 _inline_display_in_gui = true;
678 lilv_nodes_free(optional_features);
681 /* Snapshot default state -- http://lv2plug.in/ns/ext/state/#loadDefaultState */
682 LilvState* state = lilv_state_new_from_world(
683 _world.world, _uri_map.urid_map(), lilv_plugin_get_uri(_impl->plugin));
685 const uint32_t num_ports = this->num_ports();
686 for (uint32_t i = 0; i < num_ports; ++i) {
687 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, i);
689 size_t minimumSize = 0;
691 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_OutputPort)) {
692 flags |= PORT_OUTPUT;
693 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_InputPort)) {
696 error << string_compose(
697 "LV2: \"%1\" port %2 is neither input nor output",
698 lilv_node_as_string(_impl->name), i) << endmsg;
699 throw failed_constructor();
702 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_ControlPort)) {
703 flags |= PORT_CONTROL;
704 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_AudioPort)) {
706 } else if (lilv_port_is_a(_impl->plugin, port, _world.ev_EventPort)) {
708 flags |= PORT_MIDI; // We assume old event API ports are for MIDI
709 } else if (lilv_port_is_a(_impl->plugin, port, _world.atom_AtomPort)) {
710 LilvNodes* buffer_types = lilv_port_get_value(
711 _impl->plugin, port, _world.atom_bufferType);
712 LilvNodes* atom_supports = lilv_port_get_value(
713 _impl->plugin, port, _world.atom_supports);
715 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
716 flags |= PORT_SEQUENCE;
717 if (lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)) {
720 if (lilv_nodes_contains(atom_supports, _world.time_Position)) {
721 flags |= PORT_POSITION;
724 if (lilv_nodes_contains(atom_supports, _world.auto_automation_control)) {
725 flags |= PORT_AUTOCTRL;
728 if (lilv_nodes_contains(atom_supports, _world.patch_Message)) {
729 flags |= PORT_PATCHMSG;
730 if (flags & PORT_INPUT) {
731 _patch_port_in_index = i;
733 _patch_port_out_index = i;
737 LilvNodes* min_size_v = lilv_port_get_value(_impl->plugin, port, _world.rsz_minimumSize);
738 LilvNode* min_size = min_size_v ? lilv_nodes_get_first(min_size_v) : NULL;
739 if (min_size && lilv_node_is_int(min_size)) {
740 minimumSize = lilv_node_as_int(min_size);
742 lilv_nodes_free(min_size_v);
743 lilv_nodes_free(buffer_types);
744 lilv_nodes_free(atom_supports);
746 error << string_compose(
747 "LV2: \"%1\" port %2 has no known data type",
748 lilv_node_as_string(_impl->name), i) << endmsg;
749 throw failed_constructor();
752 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
753 if (lilv_port_has_property(_impl->plugin, port, _world.ext_causesArtifacts)) {
754 flags |= PORT_NOAUTO;
756 if (lilv_port_has_property(_impl->plugin, port, _world.ext_notAutomatic)) {
757 flags |= PORT_NOAUTO;
759 if (lilv_port_has_property(_impl->plugin, port, _world.ext_expensive)) {
760 flags |= PORT_NOAUTO;
764 if (lilv_port_has_property(_impl->plugin, port, _world.auto_automation_controlled)) {
765 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
766 flags |= PORT_CTRLED;
769 if (lilv_port_has_property(_impl->plugin, port, _world.auto_automation_controller)) {
770 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
771 flags |= PORT_CTRLER;
776 _port_flags.push_back(flags);
777 _port_minimumSize.push_back(minimumSize);
778 DEBUG_TRACE(DEBUG::LV2, string_compose("port %1 buffer %2 bytes\n", i, minimumSize));
781 _control_data = new float[num_ports];
782 _shadow_data = new float[num_ports];
783 _defaults = new float[num_ports];
784 _ev_buffers = new LV2_Evbuf*[num_ports];
785 memset(_ev_buffers, 0, sizeof(LV2_Evbuf*) * num_ports);
787 const bool latent = lilv_plugin_has_latency(plugin);
788 const uint32_t latency_index = (latent)
789 ? lilv_plugin_get_latency_port_index(plugin)
792 // Build an array of pointers to special parameter buffers
793 void*** params = new void**[num_ports];
794 for (uint32_t i = 0; i < num_ports; ++i) {
797 _impl->designated_input (LV2_TIME__beatsPerMinute, params, (void**)&_bpm_control_port);
798 _impl->designated_input (LV2_CORE__freeWheeling, params, (void**)&_freewheel_control_port);
800 const LilvPort* bpmport = lilv_plugin_get_port_by_designation(plugin, _world.lv2_InputPort, _world.time_beatsPerMin);
802 _bpm_control_port_index = lilv_port_get_index (plugin, bpmport);
805 for (uint32_t i = 0; i < num_ports; ++i) {
806 const LilvPort* port = lilv_plugin_get_port_by_index(plugin, i);
807 const LilvNode* sym = lilv_port_get_symbol(plugin, port);
809 // Store index in map so we can look up index by symbol
810 _port_indices.insert(std::make_pair(lilv_node_as_string(sym), i));
812 // Get range and default value if applicable
813 if (parameter_is_control(i)) {
815 lilv_port_get_range(plugin, port, &def, NULL, NULL);
816 _defaults[i] = def ? lilv_node_as_float(def) : 0.0f;
817 if (lilv_port_has_property (plugin, port, _world.lv2_sampleRate)) {
818 _defaults[i] *= _session.sample_rate ();
822 lilv_instance_connect_port(_impl->instance, i, &_control_data[i]);
824 if (latent && i == latency_index) {
826 lilv_port_get_range(_impl->plugin, port, NULL, NULL, &max);
827 _max_latency = max ? lilv_node_as_float(max) : .02 * _sample_rate;
828 _latency_control_port = &_control_data[i];
829 *_latency_control_port = 0;
832 if (parameter_is_input(i)) {
833 _shadow_data[i] = default_value(i);
835 *params[i] = (void*)&_shadow_data[i];
847 LilvUIs* uis = lilv_plugin_get_uis(plugin);
848 if (lilv_uis_size(uis) > 0) {
850 // Look for embeddable UI
851 LILV_FOREACH(uis, u, uis) {
852 const LilvUI* this_ui = lilv_uis_get(uis, u);
853 const LilvNode* this_ui_type = NULL;
854 if (lilv_ui_is_supported(this_ui,
858 // TODO: Multiple UI support
860 _impl->ui_type = this_ui_type;
865 // Look for Gtk native UI
866 LILV_FOREACH(uis, i, uis) {
867 const LilvUI* ui = lilv_uis_get(uis, i);
868 if (lilv_ui_is_a(ui, _world.ui_GtkUI)) {
870 _impl->ui_type = _world.ui_GtkUI;
876 // If Gtk UI is not available, try to find external UI
878 LILV_FOREACH(uis, i, uis) {
879 const LilvUI* ui = lilv_uis_get(uis, i);
880 if (lilv_ui_is_a(ui, _world.ui_externalkx)) {
882 _impl->ui_type = _world.ui_external;
885 if (lilv_ui_is_a(ui, _world.ui_external)) {
887 _impl->ui_type = _world.ui_external;
893 load_supported_properties(_property_descriptors);
894 allocate_atom_event_buffers();
896 /* Load default state */
898 /* immediately schedule any work,
899 * so that state restore later will not find a busy
900 * worker. latency_compute_run() flushes any replies
902 _worker->set_synchronous(true);
905 lilv_state_restore (state, _impl->instance, set_port_value, this, 0, _features);
906 lilv_state_free(state);
911 LV2Plugin::set_block_size (pframes_t nframes)
913 if (_impl->opts_iface) {
914 LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
915 _impl->block_length = nframes;
916 LV2_Options_Option block_size_option = {
917 LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id ("http://lv2plug.in/ns/ext/buf-size#nominalBlockLength"),
918 sizeof(int32_t), atom_Int, (void*)&_impl->block_length
920 _impl->opts_iface->set (_impl->instance->lv2_handle, &block_size_option);
927 LV2Plugin::requires_fixed_sized_buffers () const
929 /* This controls if Ardour will split the plugin's run()
930 * on automation events in order to pass sample-accurate automation
931 * via standard control-ports.
933 * When returning true Ardour will *not* sub-divide the process-cycle.
934 * Automation events that happen between cycle-start and cycle-end will be
935 * ignored (ctrl values are interpolated to cycle-start).
936 * NB. Atom Sequences are still sample accurate.
938 * Note: This does not guarantee a fixed block-size.
939 * e.g The process cycle may be split when looping, also
940 * the period-size may change any time: see set_block_size()
942 if (get_info()->n_inputs.n_midi() > 0) {
943 /* we don't yet implement midi buffer offsets (for split cycles).
944 * Also connect_and_run() also uses _session.transport_sample() directly
945 * (for BBT) which is not offset for plugin cycle split.
949 return _no_sample_accurate_ctrl;
953 LV2Plugin::connect_all_audio_outputs () const
955 return _connect_all_audio_outputs;
958 LV2Plugin::~LV2Plugin ()
960 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 destroy\n", name()));
967 std::stringstream ss;
970 MIDI::Name::MidiPatchManager::instance().remove_custom_midnam (ss.str());
974 lilv_instance_free(_impl->instance);
975 lilv_state_free(_impl->state);
976 lilv_node_free(_impl->name);
977 lilv_node_free(_impl->author);
978 free(_impl->options);
980 free(_impl->queue_draw);
982 free(_impl->bankpatch);
986 free(_log_feature.data);
987 free(_make_path_feature.data);
988 free(_work_schedule_feature.data);
993 delete _state_worker;
995 if (_atom_ev_buffers) {
996 LV2_Evbuf** b = _atom_ev_buffers;
1001 free(_atom_ev_buffers);
1004 delete [] _control_data;
1005 delete [] _shadow_data;
1006 delete [] _defaults;
1007 delete [] _ev_buffers;
1012 LV2Plugin::is_external_ui() const
1014 return _impl->ui && (lilv_ui_is_a(_impl->ui, _world.ui_external) ||
1015 lilv_ui_is_a(_impl->ui, _world.ui_externalkx));
1019 LV2Plugin::is_external_kx() const
1021 return _impl->ui && lilv_ui_is_a(_impl->ui, _world.ui_externalkx);
1025 LV2Plugin::ui_is_resizable () const
1027 const LilvNode* s = lilv_ui_get_uri(_impl->ui);
1028 LilvNode* p = lilv_new_uri(_world.world, LV2_CORE__optionalFeature);
1029 LilvNode* fs = lilv_new_uri(_world.world, LV2_UI__fixedSize);
1030 LilvNode* nrs = lilv_new_uri(_world.world, LV2_UI__noUserResize);
1032 LilvNodes* fs_matches = lilv_world_find_nodes(_world.world, s, p, fs);
1033 LilvNodes* nrs_matches = lilv_world_find_nodes(_world.world, s, p, nrs);
1035 lilv_nodes_free(nrs_matches);
1036 lilv_nodes_free(fs_matches);
1037 lilv_node_free(nrs);
1041 return !fs_matches && !nrs_matches;
1046 LV2Plugin::has_inline_display () {
1047 return _display_interface ? true : false;
1051 LV2Plugin::inline_display_in_gui () {
1052 return _inline_display_in_gui;
1055 Plugin::Display_Image_Surface*
1056 LV2Plugin::render_inline_display (uint32_t w, uint32_t h) {
1057 if (_display_interface) {
1058 /* Plugin::Display_Image_Surface is identical to
1059 * LV2_Inline_Display_Image_Surface */
1060 return (Plugin::Display_Image_Surface*) _display_interface->render ((void*)_impl->instance->lv2_handle, w, h);
1066 LV2Plugin::has_midnam () {
1067 return _midname_interface ? true : false;
1071 LV2Plugin::read_midnam () {
1073 if (!_midname_interface || !_midnam_dirty) {
1076 char* midnam = _midname_interface->midnam ((void*)_impl->instance->lv2_handle);
1078 std::stringstream ss;
1081 rv = MIDI::Name::MidiPatchManager::instance().update_custom_midnam (ss.str(), midnam);
1085 info << string_compose(_("LV2: update midnam for plugin '%1'"), name ()) << endmsg;
1087 warning << string_compose(_("LV2: Failed to parse midnam of plugin '%1'"), name ()) << endmsg;
1090 _midname_interface->free (midnam);
1093 _midnam_dirty = false;
1099 LV2Plugin::midnam_model () {
1101 if (!_midname_interface) {
1104 char* model = _midname_interface->model ((void*)_impl->instance->lv2_handle);
1108 _midname_interface->free (model);
1114 LV2Plugin::unique_id() const
1116 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
1120 LV2Plugin::uri() const
1122 return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
1126 LV2Plugin::label() const
1128 return lilv_node_as_string(_impl->name);
1132 LV2Plugin::name() const
1134 return lilv_node_as_string(_impl->name);
1138 LV2Plugin::maker() const
1140 return _impl->author ? lilv_node_as_string (_impl->author) : "Unknown";
1144 LV2Plugin::num_ports() const
1146 return lilv_plugin_get_num_ports(_impl->plugin);
1150 LV2Plugin::parameter_count() const
1152 return lilv_plugin_get_num_ports(_impl->plugin);
1156 LV2Plugin::default_value(uint32_t port)
1158 return _defaults[port];
1162 LV2Plugin::port_symbol(uint32_t index) const
1164 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, index);
1166 error << name() << ": Invalid port index " << index << endmsg;
1169 const LilvNode* sym = lilv_port_get_symbol(_impl->plugin, port);
1170 return lilv_node_as_string(sym);
1174 LV2Plugin::port_index (const char* symbol) const
1176 const map<string, uint32_t>::const_iterator i = _port_indices.find(symbol);
1177 if (i != _port_indices.end()) {
1180 warning << string_compose(_("LV2: Unknown port %1"), symbol) << endmsg;
1181 return (uint32_t)-1;
1186 LV2Plugin::set_parameter(uint32_t which, float val)
1188 DEBUG_TRACE(DEBUG::LV2, string_compose(
1189 "%1 set parameter %2 to %3\n", name(), which, val));
1191 if (which < lilv_plugin_get_num_ports(_impl->plugin)) {
1192 if (get_parameter (which) == val) {
1196 _shadow_data[which] = val;
1198 warning << string_compose(
1199 _("Illegal parameter number used with plugin \"%1\". "
1200 "This is a bug in either %2 or the LV2 plugin <%3>"),
1201 name(), PROGRAM_NAME, unique_id()) << endmsg;
1204 Plugin::set_parameter(which, val);
1208 LV2Plugin::get_parameter(uint32_t which) const
1210 if (parameter_is_input(which)) {
1211 return (float)_shadow_data[which];
1213 return (float)_control_data[which];
1219 LV2Plugin::get_docs() const
1221 LilvNodes* comments = lilv_plugin_get_value(_impl->plugin, _world.rdfs_comment);
1223 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
1224 lilv_nodes_free(comments);
1232 LV2Plugin::get_parameter_docs(uint32_t which) const
1234 LilvNodes* comments = lilv_port_get_value(
1236 lilv_plugin_get_port_by_index(_impl->plugin, which),
1237 _world.rdfs_comment);
1240 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
1241 lilv_nodes_free(comments);
1249 LV2Plugin::get_layout (uint32_t which, UILayoutHint& h) const
1251 /// TODO lookup port-properties
1252 if (unique_id () != "urn:ardour:a-eq") {
1257 case 0: h.x0 = 0; h.x1 = 1; h.y0 = 2; h.y1 = 3; break; // Frequency L
1258 case 1: h.x0 = 0; h.x1 = 1; h.y0 = 0; h.y1 = 1; break; // Gain L
1259 case 17: h.x0 = 0; h.x1 = 1; h.y0 = 5; h.y1 = 6; break; // enable L
1261 case 2: h.x0 = 1; h.x1 = 3; h.y0 = 2; h.y1 = 3; break; // Frequency 1
1262 case 3: h.x0 = 1; h.x1 = 3; h.y0 = 0; h.y1 = 1; break; // Gain 1
1263 case 4: h.x0 = 1; h.x1 = 3; h.y0 = 1; h.y1 = 2; break; // Bandwidth 1
1264 case 18: h.x0 = 1; h.x1 = 4; h.y0 = 5; h.y1 = 6; break; // enable 1
1266 case 5: h.x0 = 4; h.x1 = 6; h.y0 = 2; h.y1 = 3; break; // Frequency 2
1267 case 6: h.x0 = 4; h.x1 = 6; h.y0 = 0; h.y1 = 1; break; // Gain 2
1268 case 7: h.x0 = 4; h.x1 = 6; h.y0 = 1; h.y1 = 2; break; // Bandwidth 2
1269 case 19: h.x0 = 4; h.x1 = 7; h.y0 = 5; h.y1 = 6; break; // enable 2
1271 case 8: h.x0 = 7; h.x1 = 9; h.y0 = 2; h.y1 = 3; break; // Frequency 3
1272 case 9: h.x0 = 7; h.x1 = 9; h.y0 = 0; h.y1 = 1; break; // Gain 3
1273 case 10: h.x0 = 7; h.x1 = 9; h.y0 = 1; h.y1 = 2; break; // Bandwidth 3
1274 case 20: h.x0 = 7; h.x1 = 10; h.y0 = 5; h.y1 = 6; break; // enable 3
1276 case 11: h.x0 = 10; h.x1 = 12; h.y0 = 2; h.y1 = 3; break; // Frequency 4
1277 case 12: h.x0 = 10; h.x1 = 12; h.y0 = 0; h.y1 = 1; break; // Gain 4
1278 case 13: h.x0 = 10; h.x1 = 12; h.y0 = 1; h.y1 = 2; break; // Bandwidth 4
1279 case 21: h.x0 = 10; h.x1 = 13; h.y0 = 5; h.y1 = 6; break; // enable 4
1281 case 14: h.x0 = 13; h.x1 = 14; h.y0 = 2; h.y1 = 3; break; // Frequency H
1282 case 15: h.x0 = 13; h.x1 = 14; h.y0 = 0; h.y1 = 1; break; // Gain H
1283 case 22: h.x0 = 13; h.x1 = 14; h.y0 = 5; h.y1 = 6; break; // enable H
1285 case 16: h.x0 = 14; h.x1 = 15; h.y0 = 1; h.y1 = 3; break; // Master Gain
1286 case 23: h.x0 = 14; h.x1 = 15; h.y0 = 5; h.y1 = 6; break; // Master Enable
1294 LV2Plugin::nth_parameter(uint32_t n, bool& ok) const
1297 for (uint32_t c = 0, x = 0; x < lilv_plugin_get_num_ports(_impl->plugin); ++x) {
1298 if (parameter_is_control(x)) {
1310 LV2Plugin::extension_data(const char* uri) const
1312 return lilv_instance_get_extension_data(_impl->instance, uri);
1316 LV2Plugin::c_plugin()
1318 return _impl->plugin;
1324 return (const void*)_impl->ui;
1328 LV2Plugin::c_ui_type()
1330 return (const void*)_impl->ui_type;
1333 /** Directory for all plugin state. */
1335 LV2Plugin::plugin_dir() const
1337 if (!_plugin_state_dir.empty ()){
1338 return Glib::build_filename(_plugin_state_dir, _insert_id.to_s());
1340 return Glib::build_filename(_session.plugins_dir(), _insert_id.to_s());
1344 /** Directory for files created by the plugin (except during save). */
1346 LV2Plugin::scratch_dir() const
1348 return Glib::build_filename(plugin_dir(), "scratch");
1351 /** Directory for snapshots of files in the scratch directory. */
1353 LV2Plugin::file_dir() const
1355 return Glib::build_filename(plugin_dir(), "files");
1358 /** Directory to save state snapshot version @c num into. */
1360 LV2Plugin::state_dir(unsigned num) const
1362 return Glib::build_filename(plugin_dir(), string("state") + PBD::to_string (num));
1365 /** Implementation of state:makePath for files created at instantiation time.
1366 * Note this is not used for files created at save time (Lilv deals with that).
1369 LV2Plugin::lv2_state_make_path(LV2_State_Make_Path_Handle handle,
1372 LV2Plugin* me = (LV2Plugin*)handle;
1373 if (me->_insert_id == PBD::ID("0")) {
1374 warning << string_compose(
1375 "File path \"%1\" requested but LV2 %2 has no insert ID",
1376 path, me->name()) << endmsg;
1377 return g_strdup(path);
1380 const std::string abs_path = Glib::build_filename(me->scratch_dir(), path);
1381 const std::string dirname = Glib::path_get_dirname(abs_path);
1382 g_mkdir_with_parents(dirname.c_str(), 0744);
1384 DEBUG_TRACE(DEBUG::LV2, string_compose("new file path %1 => %2\n",
1387 return g_strndup(abs_path.c_str(), abs_path.length());
1391 LV2Plugin::add_state(XMLNode* root) const
1393 assert(_insert_id != PBD::ID("0"));
1398 for (uint32_t i = 0; i < parameter_count(); ++i) {
1399 if (parameter_is_input(i) && parameter_is_control(i)) {
1400 child = new XMLNode("Port");
1401 child->set_property("symbol", port_symbol(i));
1402 child->set_property("value", _shadow_data[i]);
1403 root->add_child_nocopy(*child);
1407 if (!_plugin_state_dir.empty()) {
1408 root->set_property("template-dir", _plugin_state_dir);
1411 if (_has_state_interface) {
1412 // Provisionally increment state version and create directory
1413 const std::string new_dir = state_dir(++_state_version);
1414 // and keep track of it (for templates & archive)
1415 unsigned int saved_state = _state_version;;
1416 g_mkdir_with_parents(new_dir.c_str(), 0744);
1418 std::string xternal_dir = _session.externals_dir ();
1420 if (!_plugin_state_dir.empty()) {
1421 xternal_dir = Glib::build_filename (_plugin_state_dir, externals_dir_name);
1422 g_mkdir_with_parents(xternal_dir.c_str(), 0744);
1425 LilvState* state = lilv_state_new_from_instance(
1428 _uri_map.urid_map(),
1429 scratch_dir().c_str(),
1431 xternal_dir.c_str(),
1434 const_cast<LV2Plugin*>(this),
1438 if (!_plugin_state_dir.empty() || force_state_save
1440 || !lilv_state_equals(state, _impl->state)) {
1441 lilv_state_save(_world.world,
1442 _uri_map.urid_map(),
1443 _uri_map.urid_unmap(),
1449 if (force_state_save) {
1450 // archive or save-as
1451 lilv_state_free(state);
1454 else if (_plugin_state_dir.empty()) {
1455 // normal session save
1456 lilv_state_free(_impl->state);
1457 _impl->state = state;
1459 // template save (dedicated state-dir)
1460 lilv_state_free(state);
1461 g_rmdir (xternal_dir.c_str()); // try remove unused dir
1465 // State is identical, decrement version and nuke directory
1466 lilv_state_free(state);
1467 PBD::remove_directory(new_dir);
1469 saved_state = _state_version;
1472 root->set_property("state-dir", string("state") + PBD::to_string (saved_state));
1477 get_value(LilvWorld* world, const LilvNode* subject, const LilvNode* predicate)
1479 return lilv_world_get(world, subject, predicate, NULL);
1483 LV2Plugin::find_presets()
1485 /* see also LV2PluginInfo::get_presets */
1486 LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
1487 LilvNode* pset_Preset = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
1488 LilvNode* rdfs_label = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
1489 LilvNode* rdfs_comment = lilv_new_uri(_world.world, LILV_NS_RDFS "comment");
1491 LilvNodes* presets = lilv_plugin_get_related(_impl->plugin, pset_Preset);
1492 LILV_FOREACH(nodes, i, presets) {
1493 const LilvNode* preset = lilv_nodes_get(presets, i);
1494 lilv_world_load_resource(_world.world, preset);
1495 LilvNode* name = get_value(_world.world, preset, rdfs_label);
1496 LilvNode* comment = get_value(_world.world, preset, rdfs_comment);
1497 /* TODO properly identify user vs factory presets.
1498 * here's an indirect condition: only factory presets can have comments
1500 bool userpreset = comment ? false : true;
1502 _presets.insert(std::make_pair(lilv_node_as_string(preset),
1503 Plugin::PresetRecord(
1504 lilv_node_as_string(preset),
1505 lilv_node_as_string(name),
1507 comment ? lilv_node_as_string (comment) : ""
1509 lilv_node_free(name);
1511 warning << string_compose(
1512 _("Plugin \"%1\" preset \"%2\" is missing a label\n"),
1513 lilv_node_as_string(lilv_plugin_get_uri(_impl->plugin)),
1514 lilv_node_as_string(preset)) << endmsg;
1517 lilv_node_free(comment);
1520 lilv_nodes_free(presets);
1522 lilv_node_free(rdfs_comment);
1523 lilv_node_free(rdfs_label);
1524 lilv_node_free(pset_Preset);
1525 lilv_node_free(lv2_appliesTo);
1529 LV2Plugin::load_preset(PresetRecord r)
1531 LilvWorld* world = _world.world;
1532 LilvNode* pset = lilv_new_uri(world, r.uri.c_str());
1533 LilvState* state = lilv_state_new_from_world(world, _uri_map.urid_map(), pset);
1535 const LV2_Feature* state_features[2] = { NULL, NULL };
1536 LV2_Worker_Schedule schedule = { _state_worker, work_schedule };
1537 const LV2_Feature state_sched_feature = { LV2_WORKER__schedule, &schedule };
1538 if (_state_worker) {
1539 state_features[0] = &state_sched_feature;
1543 lilv_state_restore(state, _impl->instance, set_port_value, this, 0, state_features);
1544 lilv_state_free(state);
1545 Plugin::load_preset(r);
1548 lilv_node_free(pset);
1553 ARDOUR::lv2plugin_get_port_value(const char* port_symbol,
1558 LV2Plugin *plugin = (LV2Plugin *) user_data;
1560 uint32_t index = plugin->port_index(port_symbol);
1561 if (index != (uint32_t) -1) {
1562 if (plugin->parameter_is_input(index) && plugin->parameter_is_control(index)) {
1564 *size = sizeof(float);
1565 *type = plugin->_uri_map.uri_to_id(LV2_ATOM__Float);
1566 value = &plugin->_shadow_data[index];
1578 LV2Plugin::do_save_preset(string name)
1580 LilvNode* plug_name = lilv_plugin_get_name(_impl->plugin);
1581 const string prefix = legalize_for_uri(lilv_node_as_string(plug_name));
1582 const string base_name = legalize_for_uri(name);
1583 const string file_name = base_name + ".ttl";
1584 #ifdef PLATFORM_WINDOWS
1585 /* http://lv2plug.in/pages/filesystem-hierarchy-standard.html */
1586 std::string appdata = PBD::get_win_special_folder_path (CSIDL_APPDATA);
1587 if (appdata.empty ()) {
1588 // TODO consider a fallback location
1591 const string bundle = Glib::build_filename (
1592 appdata, "LV2", prefix + "_" + base_name + ".lv2");
1594 /* while macOS/OSX user-specific path is
1596 * $HOME/Library/Audio/Plug-Ins/LV2/
1598 * liblilv's LV2 search path on all unices does include ~/.lv2/
1599 * Ardour has been saving lv2 presets to ~/.lv2 for along time,
1600 * so just keep them there.
1602 const string bundle = Glib::build_filename(
1603 Glib::get_home_dir(),
1604 Glib::build_filename(".lv2", prefix + "_" + base_name + ".lv2"));
1607 /* delete reference to old preset (if any) */
1608 const PresetRecord* r = preset_by_label(name);
1610 LilvNode* pset = lilv_new_uri (_world.world, r->uri.c_str());
1612 lilv_world_unload_resource (_world.world, pset);
1613 lilv_node_free(pset);
1617 LilvState* state = lilv_state_new_from_instance(
1620 _uri_map.urid_map(),
1621 scratch_dir().c_str(), // file_dir
1622 bundle.c_str(), // copy_dir
1623 bundle.c_str(), // link_dir
1624 bundle.c_str(), // save_dir
1625 lv2plugin_get_port_value, // get_value
1626 (void*)this, // user_data
1627 LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE, // flags
1628 _features // features
1631 lilv_state_set_label(state, name.c_str());
1633 _world.world, // world
1634 _uri_map.urid_map(), // map
1635 _uri_map.urid_unmap(), // unmap
1637 NULL, // uri (NULL = use file URI)
1638 bundle.c_str(), // dir
1639 file_name.c_str() // filename
1642 lilv_state_free(state);
1644 std::string uri = Glib::filename_to_uri(Glib::build_filename(bundle, file_name));
1645 LilvNode *node_bundle = lilv_new_uri(_world.world, Glib::filename_to_uri(Glib::build_filename(bundle, "/")).c_str());
1646 LilvNode *node_preset = lilv_new_uri(_world.world, uri.c_str());
1647 lilv_world_unload_resource(_world.world, node_preset);
1648 lilv_world_unload_bundle(_world.world, node_bundle);
1649 lilv_world_load_bundle(_world.world, node_bundle);
1650 lilv_world_load_resource(_world.world, node_preset);
1651 lilv_node_free(node_bundle);
1652 lilv_node_free(node_preset);
1653 lilv_node_free(plug_name);
1658 LV2Plugin::do_remove_preset(string name)
1660 /* Look up preset record by label (FIXME: ick, label as ID) */
1661 const PresetRecord* r = preset_by_label(name);
1666 /* Load a LilvState for the preset. */
1667 LilvWorld* world = _world.world;
1668 LilvNode* pset = lilv_new_uri(world, r->uri.c_str());
1669 LilvState* state = lilv_state_new_from_world(world, _uri_map.urid_map(), pset);
1671 lilv_node_free(pset);
1675 /* Unload preset from world. */
1676 lilv_world_unload_resource(world, pset);
1678 /* Delete it from the file system. This will remove the preset file and the entry
1679 from the manifest. If this results in an empty manifest (i.e. the
1680 preset is the only thing in the bundle), then the bundle is removed. */
1681 lilv_state_delete(world, state);
1683 lilv_state_free(state);
1684 lilv_node_free(pset);
1688 LV2Plugin::has_editor() const
1690 return _impl->ui != NULL;
1694 LV2Plugin::has_message_output() const
1696 for (uint32_t i = 0; i < num_ports(); ++i) {
1697 if ((_port_flags[i] & PORT_SEQUENCE) &&
1698 (_port_flags[i] & PORT_OUTPUT)) {
1706 LV2Plugin::write_to(RingBuffer<uint8_t>* dest,
1710 const uint8_t* body)
1712 const uint32_t buf_size = sizeof(UIMessage) + size;
1713 vector<uint8_t> buf(buf_size);
1715 UIMessage* msg = (UIMessage*)&buf[0];
1717 msg->protocol = protocol;
1719 memcpy(msg + 1, body, size);
1721 return (dest->write(&buf[0], buf_size) == buf_size);
1725 LV2Plugin::write_from_ui(uint32_t index,
1728 const uint8_t* body)
1731 size_t rbs = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
1732 /* buffer data communication from plugin UI to plugin instance.
1733 * this buffer needs to potentially hold
1734 * (port's minimumSize) * (audio-periods) / (UI-periods)
1737 * e.g 48kSPS / 128fpp -> audio-periods = 375 Hz
1738 * ui-periods = 25 Hz (SuperRapidScreenUpdate)
1739 * default minimumSize = 32K (see LV2Plugin::allocate_atom_event_buffers()
1741 * it is NOT safe to overflow (msg.size will be misinterpreted)
1743 uint32_t bufsiz = 32768;
1744 if (_atom_ev_buffers && _atom_ev_buffers[0]) {
1745 bufsiz = lv2_evbuf_get_capacity(_atom_ev_buffers[0]);
1747 int fact = ceilf(_session.sample_rate () / 3000.f);
1748 rbs = max((size_t) bufsiz * std::max (8, fact), rbs);
1749 _from_ui = new RingBuffer<uint8_t>(rbs);
1752 if (!write_to(_from_ui, index, protocol, size, body)) {
1753 error << "Error writing from UI to plugin" << endmsg;
1760 LV2Plugin::write_to_ui(uint32_t index,
1763 const uint8_t* body)
1765 if (!write_to(_to_ui, index, protocol, size, body)) {
1766 error << "Error writing from plugin to UI" << endmsg;
1773 forge_variant(LV2_Atom_Forge* forge, const Variant& value)
1775 switch (value.type()) {
1776 case Variant::NOTHING:
1778 case Variant::BEATS:
1779 // No atom type for this, just forge a double
1780 lv2_atom_forge_double(forge, value.get_beats().to_double());
1783 lv2_atom_forge_bool(forge, value.get_bool());
1785 case Variant::DOUBLE:
1786 lv2_atom_forge_double(forge, value.get_double());
1788 case Variant::FLOAT:
1789 lv2_atom_forge_float(forge, value.get_float());
1792 lv2_atom_forge_int(forge, value.get_int());
1795 lv2_atom_forge_long(forge, value.get_long());
1798 lv2_atom_forge_path(
1799 forge, value.get_path().c_str(), value.get_path().size());
1801 case Variant::STRING:
1802 lv2_atom_forge_string(
1803 forge, value.get_string().c_str(), value.get_string().size());
1807 forge, value.get_uri().c_str(), value.get_uri().size());
1812 /** Get a variant type from a URI, return false iff no match found. */
1814 uri_to_variant_type(const std::string& uri, Variant::Type& type)
1816 if (uri == LV2_ATOM__Bool) {
1817 type = Variant::BOOL;
1818 } else if (uri == LV2_ATOM__Double) {
1819 type = Variant::DOUBLE;
1820 } else if (uri == LV2_ATOM__Float) {
1821 type = Variant::FLOAT;
1822 } else if (uri == LV2_ATOM__Int) {
1823 type = Variant::INT;
1824 } else if (uri == LV2_ATOM__Long) {
1825 type = Variant::LONG;
1826 } else if (uri == LV2_ATOM__Path) {
1827 type = Variant::PATH;
1828 } else if (uri == LV2_ATOM__String) {
1829 type = Variant::STRING;
1830 } else if (uri == LV2_ATOM__URI) {
1831 type = Variant::URI;
1839 LV2Plugin::set_property(uint32_t key, const Variant& value)
1841 if (_patch_port_in_index == (uint32_t)-1) {
1842 error << "LV2: set_property called with unset patch_port_in_index" << endmsg;
1844 } else if (value.type() == Variant::NOTHING) {
1845 error << "LV2: set_property called with void value" << endmsg;
1849 // Set up forge to write to temporary buffer on the stack
1850 LV2_Atom_Forge* forge = &_impl->ui_forge;
1851 LV2_Atom_Forge_Frame frame;
1852 uint8_t buf[PATH_MAX]; // Ought to be enough for anyone...
1854 lv2_atom_forge_set_buffer(forge, buf, sizeof(buf));
1856 // Serialize patch:Set message to set property
1857 #ifdef HAVE_LV2_1_10_0
1858 lv2_atom_forge_object(forge, &frame, 0, _uri_map.urids.patch_Set);
1859 lv2_atom_forge_key(forge, _uri_map.urids.patch_property);
1860 lv2_atom_forge_urid(forge, key);
1861 lv2_atom_forge_key(forge, _uri_map.urids.patch_value);
1863 lv2_atom_forge_blank(forge, &frame, 0, _uri_map.urids.patch_Set);
1864 lv2_atom_forge_property_head(forge, _uri_map.urids.patch_property, 0);
1865 lv2_atom_forge_urid(forge, key);
1866 lv2_atom_forge_property_head(forge, _uri_map.urids.patch_value, 0);
1869 forge_variant(forge, value);
1871 // Write message to UI=>Plugin ring
1872 const LV2_Atom* const atom = (const LV2_Atom*)buf;
1873 write_from_ui(_patch_port_in_index,
1874 _uri_map.urids.atom_eventTransfer,
1875 lv2_atom_total_size(atom),
1876 (const uint8_t*)atom);
1879 const ParameterDescriptor&
1880 LV2Plugin::get_property_descriptor(uint32_t id) const
1882 PropertyDescriptors::const_iterator p = _property_descriptors.find(id);
1883 if (p != _property_descriptors.end()) {
1886 return Plugin::get_property_descriptor(id);
1890 load_parameter_descriptor_units(LilvWorld* lworld, ParameterDescriptor& desc, const LilvNodes* units)
1892 if (lilv_nodes_contains(units, _world.units_midiNote)) {
1893 desc.unit = ParameterDescriptor::MIDI_NOTE;
1894 } else if (lilv_nodes_contains(units, _world.units_db)) {
1895 desc.unit = ParameterDescriptor::DB;
1896 } else if (lilv_nodes_contains(units, _world.units_hz)) {
1897 desc.unit = ParameterDescriptor::HZ;
1899 if (lilv_nodes_size(units) > 0) {
1900 const LilvNode* unit = lilv_nodes_get_first(units);
1901 LilvNode* render = get_value(lworld, unit, _world.units_render);
1903 desc.print_fmt = lilv_node_as_string(render);
1904 /* override lilv's default "%f" format */
1905 if (desc.integer_step) {
1906 replace_all (desc.print_fmt, "%f", "%.0f");
1907 } else if (desc.upper - desc.lower >= 1000) {
1908 replace_all (desc.print_fmt, "%f", "%.1f");
1909 } else if (desc.upper - desc.lower >= 100) {
1910 replace_all (desc.print_fmt, "%f", "%.2f");
1912 replace_all (desc.print_fmt, "%f", "%.3f");
1914 lilv_node_free(render);
1920 load_parameter_descriptor(LV2World& world,
1921 ParameterDescriptor& desc,
1922 Variant::Type datatype,
1923 const LilvNode* subject)
1925 LilvWorld* lworld = _world.world;
1926 LilvNode* label = get_value(lworld, subject, _world.rdfs_label);
1927 LilvNode* def = get_value(lworld, subject, _world.lv2_default);
1928 LilvNode* minimum = get_value(lworld, subject, _world.lv2_minimum);
1929 LilvNode* maximum = get_value(lworld, subject, _world.lv2_maximum);
1930 LilvNodes* units = lilv_world_find_nodes(lworld, subject, _world.units_unit, NULL);
1932 desc.label = lilv_node_as_string(label);
1935 if (lilv_node_is_float(def)) {
1936 desc.normal = lilv_node_as_float(def);
1937 } else if (lilv_node_is_int(def)) {
1938 desc.normal = lilv_node_as_int(def);
1942 if (lilv_node_is_float(minimum)) {
1943 desc.lower = lilv_node_as_float(minimum);
1944 } else if (lilv_node_is_int(minimum)) {
1945 desc.lower = lilv_node_as_int(minimum);
1949 if (lilv_node_is_float(maximum)) {
1950 desc.upper = lilv_node_as_float(maximum);
1951 } else if (lilv_node_is_int(maximum)) {
1952 desc.upper = lilv_node_as_int(maximum);
1955 load_parameter_descriptor_units(lworld, desc, units);
1956 desc.datatype = datatype;
1957 desc.toggled |= datatype == Variant::BOOL;
1958 desc.integer_step |= datatype == Variant::INT || datatype == Variant::LONG;
1959 desc.update_steps();
1961 lilv_nodes_free(units);
1962 lilv_node_free(label);
1963 lilv_node_free(def);
1964 lilv_node_free(minimum);
1965 lilv_node_free(maximum);
1969 LV2Plugin::load_supported_properties(PropertyDescriptors& descs)
1971 LilvWorld* lworld = _world.world;
1972 const LilvNode* subject = lilv_plugin_get_uri(_impl->plugin);
1973 LilvNodes* properties = lilv_world_find_nodes(
1974 lworld, subject, _world.patch_writable, NULL);
1975 LILV_FOREACH(nodes, p, properties) {
1976 // Get label and range
1977 const LilvNode* prop = lilv_nodes_get(properties, p);
1978 LilvNode* range = get_value(lworld, prop, _world.rdfs_range);
1980 warning << string_compose(_("LV2: property <%1> has no range datatype, ignoring"),
1981 lilv_node_as_uri(prop)) << endmsg;
1985 // Convert range to variant type (TODO: support for multiple range types)
1986 Variant::Type datatype;
1987 if (!uri_to_variant_type(lilv_node_as_uri(range), datatype)) {
1988 error << string_compose(_("LV2: property <%1> has unsupported datatype <%1>"),
1989 lilv_node_as_uri(prop), lilv_node_as_uri(range)) << endmsg;
1993 // Add description to result
1994 ParameterDescriptor desc;
1995 desc.key = _uri_map.uri_to_id(lilv_node_as_uri(prop));
1996 desc.datatype = datatype;
1997 load_parameter_descriptor(_world, desc, datatype, prop);
1998 descs.insert(std::make_pair(desc.key, desc));
2000 lilv_node_free(range);
2002 lilv_nodes_free(properties);
2006 LV2Plugin::get_property_value (uint32_t prop_id) const
2008 std::map<uint32_t, Variant>::const_iterator it;
2009 if ((it = _property_values.find (prop_id)) == _property_values.end()) {
2016 LV2Plugin::announce_property_values()
2018 if (_patch_port_in_index == (uint32_t)-1) {
2022 // Set up forge to write to temporary buffer on the stack
2023 LV2_Atom_Forge* forge = &_impl->ui_forge;
2024 LV2_Atom_Forge_Frame frame;
2025 uint8_t buf[PATH_MAX]; // Ought to be enough for anyone...
2027 lv2_atom_forge_set_buffer(forge, buf, sizeof(buf));
2029 // Serialize patch:Get message with no subject (implicitly plugin instance)
2030 #ifdef HAVE_LV2_1_10_0
2031 lv2_atom_forge_object(forge, &frame, 0, _uri_map.urids.patch_Get);
2033 lv2_atom_forge_blank(forge, &frame, 0, _uri_map.urids.patch_Get);
2036 // Write message to UI=>Plugin ring
2037 const LV2_Atom* const atom = (const LV2_Atom*)buf;
2038 write_from_ui(_patch_port_in_index,
2039 _uri_map.urids.atom_eventTransfer,
2040 lv2_atom_total_size(atom),
2041 (const uint8_t*)atom);
2045 LV2Plugin::enable_ui_emission()
2048 /* see note in LV2Plugin::write_from_ui() */
2049 uint32_t bufsiz = 32768;
2050 if (_atom_ev_buffers && _atom_ev_buffers[0]) {
2051 bufsiz = lv2_evbuf_get_capacity(_atom_ev_buffers[0]);
2053 size_t rbs = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
2054 rbs = max((size_t) bufsiz * 8, rbs);
2055 _to_ui = new RingBuffer<uint8_t>(rbs);
2060 LV2Plugin::emit_to_ui(void* controller, UIMessageSink sink)
2066 uint32_t read_space = _to_ui->read_space();
2067 while (read_space > sizeof(UIMessage)) {
2069 if (_to_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
2070 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
2073 vector<uint8_t> body(msg.size);
2074 if (_to_ui->read(&body[0], msg.size) != msg.size) {
2075 error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
2079 sink(controller, msg.index, msg.size, msg.protocol, &body[0]);
2081 read_space -= sizeof(msg) + msg.size;
2086 LV2Plugin::work(Worker& worker, uint32_t size, const void* data)
2088 Glib::Threads::Mutex::Lock lm(_work_mutex);
2089 return _impl->work_iface->work(
2090 _impl->instance->lv2_handle, work_respond, &worker, size, data);
2094 LV2Plugin::work_response(uint32_t size, const void* data)
2096 return _impl->work_iface->work_response(
2097 _impl->instance->lv2_handle, size, data);
2101 LV2Plugin::set_insert_id(PBD::ID id)
2103 if (_insert_id == "0") {
2105 } else if (_insert_id != id) {
2106 lilv_state_free(_impl->state);
2107 _impl->state = NULL;
2113 LV2Plugin::set_state_dir (const std::string& d)
2115 _plugin_state_dir = d;
2119 LV2Plugin::set_state(const XMLNode& node, int version)
2122 XMLNodeConstIterator iter;
2126 if (node.name() != state_node_name()) {
2127 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
2131 if (version < 3000) {
2132 nodes = node.children("port");
2134 nodes = node.children("Port");
2137 for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
2142 if (!child->get_property("symbol", sym)) {
2143 warning << _("LV2: port has no symbol, ignored") << endmsg;
2147 map<string, uint32_t>::iterator i = _port_indices.find(sym);
2151 if (i != _port_indices.end()) {
2152 port_id = i->second;
2154 warning << _("LV2: port has unknown index, ignored") << endmsg;
2159 if (!child->get_property("value", val)) {
2160 warning << _("LV2: port has no value, ignored") << endmsg;
2164 set_parameter(port_id, val);
2167 std::string template_dir;
2168 if (node.get_property("template-dir", template_dir)) {
2169 set_state_dir (template_dir);
2173 std::string state_dir;
2174 if (node.get_property("state-dir", state_dir) != 0) {
2175 if (sscanf(state_dir.c_str(), "state%u", &_state_version) != 1) {
2176 error << string_compose(
2177 "LV2: failed to parse state version from \"%1\"",
2178 state_dir) << endmsg;
2181 std::string state_file = Glib::build_filename(
2183 Glib::build_filename(state_dir, "state.ttl"));
2185 LilvState* state = lilv_state_new_from_file(
2186 _world.world, _uri_map.urid_map(), NULL, state_file.c_str());
2188 lilv_state_restore(state, _impl->instance, NULL, NULL, 0, NULL);
2189 lilv_state_free(_impl->state);
2190 _impl->state = state;
2193 if (!_plugin_state_dir.empty ()) {
2194 // force save with session, next time (increment counter)
2195 lilv_state_free (_impl->state);
2196 _impl->state = NULL;
2200 /* Do not call latency_compute_run() concurrently with connect_and_run().
2201 * So far this can only guarnteed when the session is loading,
2202 * and the plugin has not been added to the processor chain.
2204 * Ideally this would clso be called when copying a plugin from another track,
2205 * but NOT when copying the state from a plugin to another (active) plugin
2208 if (_session.loading ()) {
2209 latency_compute_run();
2212 return Plugin::set_state(node, version);
2216 LV2Plugin::get_parameter_descriptor(uint32_t which, ParameterDescriptor& desc) const
2218 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, which);
2220 error << string_compose("LV2: get descriptor of non-existent port %1", which)
2225 LilvNodes* portunits;
2226 LilvNode *def, *min, *max;
2227 lilv_port_get_range(_impl->plugin, port, &def, &min, &max);
2228 portunits = lilv_port_get_value(_impl->plugin, port, _world.units_unit);
2230 LilvNode* steps = lilv_port_get(_impl->plugin, port, _world.ext_rangeSteps);
2232 // TODO: Once we can rely on lilv 0.18.0 being present,
2233 // load_parameter_descriptor() can be used for ports as well
2234 desc.integer_step = lilv_port_has_property(_impl->plugin, port, _world.lv2_integer);
2235 desc.toggled = lilv_port_has_property(_impl->plugin, port, _world.lv2_toggled);
2236 desc.logarithmic = lilv_port_has_property(_impl->plugin, port, _world.ext_logarithmic);
2237 desc.sr_dependent = lilv_port_has_property(_impl->plugin, port, _world.lv2_sampleRate);
2238 desc.label = lilv_node_as_string(lilv_port_get_name(_impl->plugin, port));
2239 desc.normal = def ? lilv_node_as_float(def) : 0.0f;
2240 desc.lower = min ? lilv_node_as_float(min) : 0.0f;
2241 desc.upper = max ? lilv_node_as_float(max) : 1.0f;
2242 load_parameter_descriptor_units(_world.world, desc, portunits);
2244 if (desc.sr_dependent) {
2245 desc.lower *= _session.sample_rate ();
2246 desc.upper *= _session.sample_rate ();
2249 desc.enumeration = lilv_port_has_property(_impl->plugin, port, _world.lv2_enumeration);
2250 desc.scale_points = get_scale_points(which);
2253 desc.rangesteps = lilv_node_as_float (steps);
2256 desc.update_steps();
2258 lilv_node_free(def);
2259 lilv_node_free(min);
2260 lilv_node_free(max);
2261 lilv_node_free(steps);
2262 lilv_nodes_free(portunits);
2267 Plugin::IOPortDescription
2268 LV2Plugin::describe_io_port (ARDOUR::DataType dt, bool input, uint32_t id) const
2270 PortFlags match = 0;
2272 case DataType::AUDIO:
2275 case DataType::MIDI:
2276 match = PORT_SEQUENCE | PORT_MIDI; // ignore old PORT_EVENT
2279 return Plugin::IOPortDescription ("?");
2283 match |= PORT_INPUT;
2285 match |= PORT_OUTPUT;
2289 uint32_t idx = UINT32_MAX;
2291 uint32_t const num_ports = parameter_count();
2292 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2293 PortFlags flags = _port_flags[port_index];
2294 if ((flags & match) == match) {
2301 if (idx == UINT32_MAX) {
2302 return Plugin::IOPortDescription ("?");
2305 const LilvPort* pport = lilv_plugin_get_port_by_index (_impl->plugin, idx);
2307 LilvNode* name = lilv_port_get_name(_impl->plugin, pport);
2308 Plugin::IOPortDescription iod (lilv_node_as_string (name));
2309 lilv_node_free(name);
2311 /* get the port's pg:group */
2312 LilvNodes* groups = lilv_port_get_value (_impl->plugin, pport, _world.groups_group);
2313 if (lilv_nodes_size (groups) > 0) {
2314 const LilvNode* group = lilv_nodes_get_first (groups);
2315 LilvNodes* grouplabel = lilv_world_find_nodes (_world.world, group, _world.rdfs_label, NULL);
2317 /* get the name of the port-group */
2318 if (lilv_nodes_size (grouplabel) > 0) {
2319 const LilvNode* grpname = lilv_nodes_get_first (grouplabel);
2320 iod.group_name = lilv_node_as_string (grpname);
2322 lilv_nodes_free (grouplabel);
2324 /* get all port designations.
2325 * we're interested in e.g. lv2:designation pg:right */
2326 LilvNodes* designations = lilv_port_get_value (_impl->plugin, pport, _world.lv2_designation);
2327 if (lilv_nodes_size (designations) > 0) {
2328 /* get all pg:elements of the pg:group */
2329 LilvNodes* group_childs = lilv_world_find_nodes (_world.world, group, _world.groups_element, NULL);
2330 if (lilv_nodes_size (group_childs) > 0) {
2331 /* iterate over all port designations .. */
2332 LILV_FOREACH (nodes, i, designations) {
2333 const LilvNode* designation = lilv_nodes_get (designations, i);
2334 /* match the lv2:designation's element against the port-group's element */
2335 LILV_FOREACH (nodes, j, group_childs) {
2336 const LilvNode* group_element = lilv_nodes_get (group_childs, j);
2337 LilvNodes* elem = lilv_world_find_nodes (_world.world, group_element, _world.lv2_designation, designation);
2338 /* found it. Now look up the index (channel-number) of the pg:Element */
2339 if (lilv_nodes_size (elem) > 0) {
2340 LilvNodes* idx = lilv_world_find_nodes (_world.world, lilv_nodes_get_first (elem), _world.lv2_index, NULL);
2341 if (lilv_node_is_int (lilv_nodes_get_first (idx))) {
2342 iod.group_channel = lilv_node_as_int(lilv_nodes_get_first (idx));
2349 lilv_nodes_free (groups);
2350 lilv_nodes_free (designations);
2353 if (lilv_port_has_property(_impl->plugin, pport, _world.lv2_isSideChain)) {
2354 iod.is_sidechain = true;
2360 LV2Plugin::describe_parameter(Evoral::Parameter which)
2362 if (( which.type() == PluginAutomation) && ( which.id() < parameter_count()) ) {
2364 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, which.id());
2366 if (lilv_port_has_property(_impl->plugin, port, _world.ext_notOnGUI)) {
2367 return X_("hidden");
2370 const LilvPort* fwport = lilv_plugin_get_port_by_designation(_impl->plugin, _world.lv2_InputPort, _world.lv2_freewheeling);
2371 if (fwport && fwport == port) {
2372 return X_("hidden");
2375 const LilvPort* bpmport = lilv_plugin_get_port_by_designation(_impl->plugin, _world.lv2_InputPort, _world.time_beatsPerMin);
2376 if (bpmport && bpmport == port) {
2377 return X_("hidden");
2380 if (lilv_port_has_property(_impl->plugin, port, _world.lv2_freewheeling)) {
2381 return X_("hidden");
2384 if (lilv_port_has_property(_impl->plugin, port, _world.lv2_reportsLatency)) {
2385 return X_("latency");
2388 LilvNode* name = lilv_port_get_name(_impl->plugin,
2389 lilv_plugin_get_port_by_index(_impl->plugin, which.id()));
2390 string ret(lilv_node_as_string(name));
2391 lilv_node_free(name);
2399 LV2Plugin::max_latency () const
2401 return _max_latency;
2405 LV2Plugin::plugin_latency() const
2407 if (_latency_control_port) {
2408 return (samplecnt_t)floor(*_latency_control_port);
2414 set<Evoral::Parameter>
2415 LV2Plugin::automatable() const
2417 set<Evoral::Parameter> ret;
2419 for (uint32_t i = 0; i < parameter_count(); ++i) {
2420 if (parameter_is_input(i) && parameter_is_control(i) && !(_port_flags[i] & PORT_NOAUTO)) {
2421 ret.insert(ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
2425 for (PropertyDescriptors::const_iterator p = _property_descriptors.begin();
2426 p != _property_descriptors.end();
2428 ret.insert(ret.end(), Evoral::Parameter(PluginPropertyAutomation, 0, p->first));
2434 LV2Plugin::set_automation_control (uint32_t i, boost::shared_ptr<AutomationControl> c)
2436 if ((_port_flags[i] & (PORT_CTRLED | PORT_CTRLER))) {
2437 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("Ctrl Port %1\n", i));
2438 _ctrl_map [i] = AutomationCtrlPtr (new AutomationCtrl(c));
2440 else if (i == _bpm_control_port_index) {
2441 _ctrl_map [i] = AutomationCtrlPtr (new AutomationCtrl(c));
2445 LV2Plugin::AutomationCtrlPtr
2446 LV2Plugin::get_automation_control (uint32_t i)
2448 if (_ctrl_map.find (i) == _ctrl_map.end()) {
2449 return AutomationCtrlPtr ();
2451 return _ctrl_map[i];
2455 LV2Plugin::activate()
2457 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 activate\n", name()));
2459 if (!_was_activated) {
2460 lilv_instance_activate(_impl->instance);
2461 _was_activated = true;
2466 LV2Plugin::deactivate()
2468 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 deactivate\n", name()));
2470 if (_was_activated) {
2471 lilv_instance_deactivate(_impl->instance);
2472 _was_activated = false;
2477 LV2Plugin::cleanup()
2479 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 cleanup\n", name()));
2482 lilv_instance_free(_impl->instance);
2483 _impl->instance = NULL;
2487 LV2Plugin::allocate_atom_event_buffers()
2489 /* reserve local scratch buffers for ATOM event-queues */
2490 const LilvPlugin* p = _impl->plugin;
2492 /* count non-MIDI atom event-ports
2493 * TODO: nicely ask drobilla to make a lilv_ call for that
2495 int count_atom_out = 0;
2496 int count_atom_in = 0;
2497 int minimumSize = 32768; // TODO use a per-port minimum-size
2498 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
2499 const LilvPort* port = lilv_plugin_get_port_by_index(p, i);
2500 if (lilv_port_is_a(p, port, _world.atom_AtomPort)) {
2501 LilvNodes* buffer_types = lilv_port_get_value(
2502 p, port, _world.atom_bufferType);
2503 LilvNodes* atom_supports = lilv_port_get_value(
2504 p, port, _world.atom_supports);
2506 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
2507 if (lilv_port_is_a(p, port, _world.lv2_InputPort)) {
2510 if (lilv_port_is_a(p, port, _world.lv2_OutputPort)) {
2513 LilvNodes* min_size_v = lilv_port_get_value(_impl->plugin, port, _world.rsz_minimumSize);
2514 LilvNode* min_size = min_size_v ? lilv_nodes_get_first(min_size_v) : NULL;
2515 if (min_size && lilv_node_is_int(min_size)) {
2516 minimumSize = std::max(minimumSize, lilv_node_as_int(min_size));
2518 lilv_nodes_free(min_size_v);
2520 lilv_nodes_free(buffer_types);
2521 lilv_nodes_free(atom_supports);
2525 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 need buffers for %2 atom-in and %3 atom-out event-ports\n",
2526 name(), count_atom_in, count_atom_out));
2528 const int total_atom_buffers = (count_atom_in + count_atom_out);
2529 if (_atom_ev_buffers || total_atom_buffers == 0) {
2533 DEBUG_TRACE(DEBUG::LV2, string_compose("allocate %1 atom_ev_buffers of %2 bytes\n", total_atom_buffers, minimumSize));
2534 _atom_ev_buffers = (LV2_Evbuf**) malloc((total_atom_buffers + 1) * sizeof(LV2_Evbuf*));
2535 for (int i = 0; i < total_atom_buffers; ++i ) {
2536 _atom_ev_buffers[i] = lv2_evbuf_new(minimumSize, LV2_EVBUF_ATOM,
2537 _uri_map.urids.atom_Chunk, _uri_map.urids.atom_Sequence);
2539 _atom_ev_buffers[total_atom_buffers] = 0;
2543 /** Write an ardour position/time/tempo/meter as an LV2 event.
2544 * @return true on success.
2547 write_position(LV2_Atom_Forge* forge,
2549 const TempoMetric& t,
2550 Timecode::BBT_Time& bbt,
2553 samplepos_t position,
2556 const URIMap::URIDs& urids = URIMap::instance().urids;
2558 uint8_t pos_buf[256];
2559 lv2_atom_forge_set_buffer(forge, pos_buf, sizeof(pos_buf));
2560 LV2_Atom_Forge_Frame frame;
2561 #ifdef HAVE_LV2_1_10_0
2562 lv2_atom_forge_object(forge, &frame, 0, urids.time_Position);
2563 lv2_atom_forge_key(forge, urids.time_frame);
2564 lv2_atom_forge_long(forge, position);
2565 lv2_atom_forge_key(forge, urids.time_speed);
2566 lv2_atom_forge_float(forge, speed);
2567 lv2_atom_forge_key(forge, urids.time_barBeat);
2568 lv2_atom_forge_float(forge, bbt.beats - 1 +
2569 (bbt.ticks / Timecode::BBT_Time::ticks_per_beat));
2570 lv2_atom_forge_key(forge, urids.time_bar);
2571 lv2_atom_forge_long(forge, bbt.bars - 1);
2572 lv2_atom_forge_key(forge, urids.time_beatUnit);
2573 lv2_atom_forge_int(forge, t.meter().note_divisor());
2574 lv2_atom_forge_key(forge, urids.time_beatsPerBar);
2575 lv2_atom_forge_float(forge, t.meter().divisions_per_bar());
2576 lv2_atom_forge_key(forge, urids.time_beatsPerMinute);
2577 lv2_atom_forge_float(forge, bpm);
2579 lv2_atom_forge_blank(forge, &frame, 1, urids.time_Position);
2580 lv2_atom_forge_property_head(forge, urids.time_frame, 0);
2581 lv2_atom_forge_long(forge, position);
2582 lv2_atom_forge_property_head(forge, urids.time_speed, 0);
2583 lv2_atom_forge_float(forge, speed);
2584 lv2_atom_forge_property_head(forge, urids.time_barBeat, 0);
2585 lv2_atom_forge_float(forge, bbt.beats - 1 +
2586 (bbt.ticks / Timecode::BBT_Time::ticks_per_beat));
2587 lv2_atom_forge_property_head(forge, urids.time_bar, 0);
2588 lv2_atom_forge_long(forge, bbt.bars - 1);
2589 lv2_atom_forge_property_head(forge, urids.time_beatUnit, 0);
2590 lv2_atom_forge_int(forge, t.meter().note_divisor());
2591 lv2_atom_forge_property_head(forge, urids.time_beatsPerBar, 0);
2592 lv2_atom_forge_float(forge, t.meter().divisions_per_bar());
2593 lv2_atom_forge_property_head(forge, urids.time_beatsPerMinute, 0);
2594 lv2_atom_forge_float(forge, bpm);
2597 LV2_Evbuf_Iterator end = lv2_evbuf_end(buf);
2598 const LV2_Atom* const atom = (const LV2_Atom*)pos_buf;
2599 return lv2_evbuf_write(&end, offset, 0, atom->type, atom->size,
2600 (const uint8_t*)(atom + 1));
2604 LV2Plugin::connect_and_run(BufferSet& bufs,
2605 samplepos_t start, samplepos_t end, double speed,
2606 ChanMapping const& in_map, ChanMapping const& out_map,
2607 pframes_t nframes, samplecnt_t offset)
2609 DEBUG_TRACE(DEBUG::LV2, string_compose("%1 run %2 offset %3\n", name(), nframes, offset));
2610 Plugin::connect_and_run(bufs, start, end, speed, in_map, out_map, nframes, offset);
2612 cycles_t then = get_cycles();
2614 TempoMap& tmap = _session.tempo_map();
2615 Metrics::const_iterator metric_i = tmap.metrics_end();
2616 TempoMetric tmetric = tmap.metric_at(start, &metric_i);
2618 if (_freewheel_control_port) {
2619 *_freewheel_control_port = _session.engine().freewheeling() ? 1.f : 0.f;
2622 if (_bpm_control_port) {
2623 float bpm = tmap.tempo_at_sample (start).note_types_per_minute();
2624 if (*_bpm_control_port != bpm) {
2625 AutomationCtrlPtr c = get_automation_control (_bpm_control_port_index);
2627 /* may be NULL for replicated instances - only one custom UI/ctrl */
2628 c->ac->Changed (false, Controllable::NoGroup); /* EMIT SIGNAL */
2631 *_bpm_control_port = bpm;
2635 if (_can_write_automation && start != _next_cycle_start) {
2636 // add guard-points after locating
2637 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2638 i->second->guard = true;
2643 ChanCount bufs_count;
2644 bufs_count.set(DataType::AUDIO, 1);
2645 bufs_count.set(DataType::MIDI, 1);
2646 BufferSet& silent_bufs = _session.get_silent_buffers(bufs_count);
2647 BufferSet& scratch_bufs = _session.get_scratch_buffers(bufs_count);
2648 uint32_t const num_ports = parameter_count();
2649 uint32_t const nil_index = std::numeric_limits<uint32_t>::max();
2651 uint32_t audio_in_index = 0;
2652 uint32_t audio_out_index = 0;
2653 uint32_t midi_in_index = 0;
2654 uint32_t midi_out_index = 0;
2655 uint32_t atom_port_index = 0;
2656 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2658 uint32_t index = nil_index;
2659 PortFlags flags = _port_flags[port_index];
2661 if (flags & PORT_AUDIO) {
2662 if (flags & PORT_INPUT) {
2663 index = in_map.get(DataType::AUDIO, audio_in_index++, &valid);
2665 ? bufs.get_audio(index).data(offset)
2666 : silent_bufs.get_audio(0).data(offset);
2668 index = out_map.get(DataType::AUDIO, audio_out_index++, &valid);
2670 ? bufs.get_audio(index).data(offset)
2671 : scratch_bufs.get_audio(0).data(offset);
2673 } else if (flags & (PORT_EVENT|PORT_SEQUENCE)) {
2674 /* FIXME: The checks here for bufs.count().n_midi() > index shouldn't
2675 be necessary, but the mapping is illegal in some cases. Ideally
2676 that should be fixed, but this is easier...
2678 if (flags & PORT_MIDI) {
2679 if (flags & PORT_INPUT) {
2680 index = in_map.get(DataType::MIDI, midi_in_index++, &valid);
2682 index = out_map.get(DataType::MIDI, midi_out_index++, &valid);
2684 if (valid && bufs.count().n_midi() > index) {
2685 /* Note, ensure_lv2_bufsize() is not RT safe!
2686 * However free()/alloc() is only called if a
2687 * plugin requires a rsz:minimumSize buffersize
2688 * and the existing buffer if smaller.
2690 bufs.ensure_lv2_bufsize((flags & PORT_INPUT), index, _port_minimumSize[port_index]);
2691 _ev_buffers[port_index] = bufs.get_lv2_midi(
2692 (flags & PORT_INPUT), index, (flags & PORT_EVENT));
2694 } else if ((flags & PORT_POSITION) && (flags & PORT_INPUT)) {
2695 lv2_evbuf_reset(_atom_ev_buffers[atom_port_index], true);
2696 _ev_buffers[port_index] = _atom_ev_buffers[atom_port_index++];
2700 if (valid && (flags & PORT_INPUT)) {
2701 if ((flags & PORT_POSITION)) {
2702 Timecode::BBT_Time bbt (tmap.bbt_at_sample (start));
2703 double bpm = tmap.tempo_at_sample (start).note_types_per_minute();
2704 double beatpos = (bbt.bars - 1) * tmetric.meter().divisions_per_bar()
2706 + (bbt.ticks / Timecode::BBT_Time::ticks_per_beat);
2707 beatpos *= tmetric.meter().note_divisor() / 4.0;
2708 if (start != _next_cycle_start ||
2709 speed != _next_cycle_speed ||
2710 rint (1000 * beatpos) != rint(1000 * _next_cycle_beat) ||
2711 bpm != _current_bpm) {
2712 // Transport or Tempo has changed, write position at cycle start
2713 write_position(&_impl->forge, _ev_buffers[port_index],
2714 tmetric, bbt, speed, bpm, start, 0);
2718 // Get MIDI iterator range (empty range if no MIDI)
2719 MidiBuffer::iterator m = (index != nil_index)
2720 ? bufs.get_midi(index).begin()
2721 : silent_bufs.get_midi(0).end();
2722 MidiBuffer::iterator m_end = (index != nil_index)
2723 ? bufs.get_midi(index).end()
2726 // Now merge MIDI and any transport events into the buffer
2727 const uint32_t type = _uri_map.urids.midi_MidiEvent;
2728 const samplepos_t tend = end;
2730 while (m != m_end || (metric_i != tmap.metrics_end() &&
2731 (*metric_i)->sample() < tend)) {
2732 MetricSection* metric = (metric_i != tmap.metrics_end())
2734 if (m != m_end && (!metric || metric->sample() > (*m).time())) {
2735 const Evoral::Event<samplepos_t> ev(*m, false);
2736 if (ev.time() < nframes) {
2737 LV2_Evbuf_Iterator eend = lv2_evbuf_end(_ev_buffers[port_index]);
2738 lv2_evbuf_write(&eend, ev.time(), 0, type, ev.size(), ev.buffer());
2743 tmetric.set_metric(metric);
2744 Timecode::BBT_Time bbt;
2745 bbt = tmap.bbt_at_sample (metric->sample());
2746 double bpm = tmap.tempo_at_sample (start/*XXX*/).note_types_per_minute();
2747 write_position(&_impl->forge, _ev_buffers[port_index],
2748 tmetric, bbt, speed, bpm,
2750 metric->sample() - start);
2754 } else if (!valid) {
2755 // Nothing we understand or care about, connect to scratch
2756 // see note for midi-buffer size above
2757 scratch_bufs.ensure_lv2_bufsize((flags & PORT_INPUT),
2758 0, _port_minimumSize[port_index]);
2759 _ev_buffers[port_index] = scratch_bufs.get_lv2_midi(
2760 (flags & PORT_INPUT), 0, (flags & PORT_EVENT));
2763 buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
2765 continue; // Control port, leave buffer alone
2767 lilv_instance_connect_port(_impl->instance, port_index, buf);
2770 // Read messages from UI and push into appropriate buffers
2772 uint32_t read_space = _from_ui->read_space();
2773 while (read_space > sizeof(UIMessage)) {
2775 if (_from_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
2776 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
2779 vector<uint8_t> body(msg.size);
2780 if (_from_ui->read(&body[0], msg.size) != msg.size) {
2781 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
2784 if (msg.protocol == URIMap::instance().urids.atom_eventTransfer) {
2785 LV2_Evbuf* buf = _ev_buffers[msg.index];
2786 LV2_Evbuf_Iterator i = lv2_evbuf_end(buf);
2787 const LV2_Atom* const atom = (const LV2_Atom*)&body[0];
2788 if (!lv2_evbuf_write(&i, nframes - 1, 0, atom->type, atom->size,
2789 (const uint8_t*)(atom + 1))) {
2790 error << "Failed to write data to LV2 event buffer\n";
2793 error << "Received unknown message type from UI" << endmsg;
2795 read_space -= sizeof(UIMessage) + msg.size;
2802 for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2803 PortFlags flags = _port_flags[port_index];
2806 /* TODO ask drobilla about comment
2807 * "Make Ardour event buffers generic so plugins can communicate"
2808 * in libs/ardour/buffer_set.cc:310
2810 * ideally the user could choose which of the following two modes
2811 * to use (e.g. instrument/effect chains MIDI OUT vs MIDI TRHU).
2813 * This implementation follows the discussion on IRC Mar 16 2013 16:47 UTC
2814 * 16:51 < drobilla> rgareus: [..] i.e always replace with MIDI output [of LV2 plugin] if it's there
2815 * 16:52 < drobilla> rgareus: That would probably be good enough [..] to make users not complain
2816 * for quite a while at least ;)
2818 // copy output of LV2 plugin's MIDI port to Ardour MIDI buffers -- MIDI OUT
2819 if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE|PORT_MIDI))) {
2820 const uint32_t buf_index = out_map.get(
2821 DataType::MIDI, midi_out_index++, &valid);
2823 bufs.forward_lv2_midi(_ev_buffers[port_index], buf_index);
2826 // Flush MIDI (write back to Ardour MIDI buffers) -- MIDI THRU
2827 else if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE))) {
2828 const uint32_t buf_index = out_map.get(
2829 DataType::MIDI, midi_out_index++, &valid);
2831 bufs.flush_lv2_midi(true, buf_index);
2835 // Write messages to UI
2836 if ((_to_ui || _can_write_automation || _patch_port_out_index != (uint32_t)-1) &&
2837 (flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE))) {
2838 LV2_Evbuf* buf = _ev_buffers[port_index];
2839 for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(buf);
2840 lv2_evbuf_is_valid(i);
2841 i = lv2_evbuf_next(i)) {
2842 uint32_t samples, subframes, type, size;
2844 lv2_evbuf_get(i, &samples, &subframes, &type, &size, &data);
2847 // Intercept Automation Write Events
2848 if ((flags & PORT_AUTOCTRL)) {
2849 LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2850 if (atom->type == _uri_map.urids.atom_Blank ||
2851 atom->type == _uri_map.urids.atom_Object) {
2852 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2853 if (obj->body.otype == _uri_map.urids.auto_event) {
2854 // only if transport_rolling ??
2855 const LV2_Atom* parameter = NULL;
2856 const LV2_Atom* value = NULL;
2857 lv2_atom_object_get(obj,
2858 _uri_map.urids.auto_parameter, ¶meter,
2859 _uri_map.urids.auto_value, &value,
2861 if (parameter && value) {
2862 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2863 const float v = ((const LV2_Atom_Float*)value)->body;
2864 // -> add automation event..
2865 DEBUG_TRACE(DEBUG::LV2Automate,
2866 string_compose ("Event p: %1 t: %2 v: %3\n", p, samples, v));
2867 AutomationCtrlPtr c = get_automation_control (p);
2869 (c->ac->automation_state() == Touch || c->ac->automation_state() == Write)
2871 samplepos_t when = std::max ((samplepos_t) 0, start + samples - _current_latency);
2872 assert (start + samples - _current_latency >= 0);
2875 c->ac->list()->add (when, v, true, true);
2877 c->ac->set_double (v, when, true);
2882 else if (obj->body.otype == _uri_map.urids.auto_setup) {
2883 // TODO optional arguments, for now we assume the plugin
2884 // writes automation for its own inputs
2885 // -> put them in "touch" mode (preferably "exclusive plugin touch(TM)"
2886 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2887 if (_port_flags[i->first] & PORT_CTRLED) {
2888 DEBUG_TRACE(DEBUG::LV2Automate,
2889 string_compose ("Setup p: %1\n", i->first));
2890 i->second->ac->set_automation_state (Touch);
2894 else if (obj->body.otype == _uri_map.urids.auto_finalize) {
2895 // set [touched] parameters to "play" ??
2896 // allow plugin to change its mode (from analyze to apply)
2897 const LV2_Atom* parameter = NULL;
2898 const LV2_Atom* value = NULL;
2899 lv2_atom_object_get(obj,
2900 _uri_map.urids.auto_parameter, ¶meter,
2901 _uri_map.urids.auto_value, &value,
2903 if (parameter && value) {
2904 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2905 const float v = ((const LV2_Atom_Float*)value)->body;
2906 AutomationCtrlPtr c = get_automation_control (p);
2907 DEBUG_TRACE(DEBUG::LV2Automate,
2908 string_compose ("Finalize p: %1 v: %2\n", p, v));
2909 if (c && _port_flags[p] & PORT_CTRLER) {
2910 c->ac->set_value(v, Controllable::NoGroup);
2913 DEBUG_TRACE(DEBUG::LV2Automate, "Finalize\n");
2915 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2916 // guard will be false if an event was written
2917 if ((_port_flags[i->first] & PORT_CTRLED) && !i->second->guard) {
2918 DEBUG_TRACE(DEBUG::LV2Automate,
2919 string_compose ("Thin p: %1\n", i->first));
2920 i->second->ac->alist ()->thin (20);
2924 else if (obj->body.otype == _uri_map.urids.auto_start) {
2925 const LV2_Atom* parameter = NULL;
2926 lv2_atom_object_get(obj,
2927 _uri_map.urids.auto_parameter, ¶meter,
2930 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2931 AutomationCtrlPtr c = get_automation_control (p);
2932 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("Start Touch p: %1\n", p));
2934 c->ac->start_touch (std::max ((samplepos_t)0, start - _current_latency));
2939 else if (obj->body.otype == _uri_map.urids.auto_end) {
2940 const LV2_Atom* parameter = NULL;
2941 lv2_atom_object_get(obj,
2942 _uri_map.urids.auto_parameter, ¶meter,
2945 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2946 AutomationCtrlPtr c = get_automation_control (p);
2947 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("End Touch p: %1\n", p));
2949 c->ac->stop_touch (std::max ((samplepos_t)0, start - _current_latency));
2956 // Intercept state dirty message
2957 if (_has_state_interface /* && (flags & PORT_DIRTYMSG)*/) {
2958 LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2959 if (atom->type == _uri_map.urids.atom_Blank ||
2960 atom->type == _uri_map.urids.atom_Object) {
2961 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2962 if (obj->body.otype == _uri_map.urids.state_StateChanged) {
2963 _session.set_dirty ();
2968 // Intercept patch change messages to emit PropertyChanged signal
2969 if ((flags & PORT_PATCHMSG)) {
2970 LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2971 if (atom->type == _uri_map.urids.atom_Blank ||
2972 atom->type == _uri_map.urids.atom_Object) {
2973 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2974 if (obj->body.otype == _uri_map.urids.patch_Set) {
2975 const LV2_Atom* property = NULL;
2976 const LV2_Atom* value = NULL;
2977 lv2_atom_object_get(obj,
2978 _uri_map.urids.patch_property, &property,
2979 _uri_map.urids.patch_value, &value,
2982 if (property && value &&
2983 property->type == _uri_map.urids.atom_URID &&
2984 value->type == _uri_map.urids.atom_Path) {
2985 const uint32_t prop_id = ((const LV2_Atom_URID*)property)->body;
2986 const char* path = (const char*)LV2_ATOM_BODY_CONST(value);
2988 // Emit PropertyChanged signal for UI
2989 // TODO: This should emit the control's Changed signal
2990 PropertyChanged(prop_id, Variant(Variant::PATH, path));
2991 _property_values[prop_id] = Variant(Variant::PATH, path);
2993 std::cerr << "warning: patch:Set for unknown property" << std::endl;
2999 if (!_to_ui) continue;
3000 write_to_ui(port_index, URIMap::instance().urids.atom_eventTransfer,
3001 size + sizeof(LV2_Atom),
3002 data - sizeof(LV2_Atom));
3007 cycles_t now = get_cycles();
3008 set_cycles((uint32_t)(now - then));
3010 // Update expected transport information for next cycle so we can detect changes
3011 _next_cycle_speed = speed;
3012 _next_cycle_start = end;
3015 /* keep track of lv2:timePosition like plugins can do.
3016 * Note: for no-midi plugins, we only ever send information at cycle-start,
3017 * so it needs to be realative to that.
3019 TempoMetric t = tmap.metric_at(start);
3020 _current_bpm = tmap.tempo_at_sample (start).note_types_per_minute();
3021 Timecode::BBT_Time bbt (tmap.bbt_at_sample (start));
3022 double beatpos = (bbt.bars - 1) * t.meter().divisions_per_bar()
3024 + (bbt.ticks / Timecode::BBT_Time::ticks_per_beat);
3025 beatpos *= tmetric.meter().note_divisor() / 4.0;
3026 _next_cycle_beat = beatpos + nframes * speed * _current_bpm / (60.f * _session.sample_rate());
3029 if (_latency_control_port) {
3030 samplecnt_t new_latency = signal_latency ();
3031 _current_latency = new_latency;
3037 LV2Plugin::parameter_is_control(uint32_t param) const
3039 assert(param < _port_flags.size());
3040 return _port_flags[param] & PORT_CONTROL;
3044 LV2Plugin::parameter_is_audio(uint32_t param) const
3046 assert(param < _port_flags.size());
3047 return _port_flags[param] & PORT_AUDIO;
3051 LV2Plugin::parameter_is_event(uint32_t param) const
3053 assert(param < _port_flags.size());
3054 return _port_flags[param] & PORT_EVENT;
3058 LV2Plugin::parameter_is_output(uint32_t param) const
3060 assert(param < _port_flags.size());
3061 return _port_flags[param] & PORT_OUTPUT;
3065 LV2Plugin::parameter_is_input(uint32_t param) const
3067 assert(param < _port_flags.size());
3068 return _port_flags[param] & PORT_INPUT;
3072 LV2Plugin::designated_bypass_port ()
3074 const LilvPort* port = NULL;
3075 LilvNode* designation = lilv_new_uri (_world.world, LV2_CORE_PREFIX "enabled");
3076 port = lilv_plugin_get_port_by_designation (
3077 _impl->plugin, _world.lv2_InputPort, designation);
3078 lilv_node_free(designation);
3080 return lilv_port_get_index (_impl->plugin, port);
3083 /* deprecated on 2016-Sep-18 in favor of lv2:enabled */
3084 designation = lilv_new_uri (_world.world, LV2_PROCESSING_URI__enable);
3085 port = lilv_plugin_get_port_by_designation (
3086 _impl->plugin, _world.lv2_InputPort, designation);
3087 lilv_node_free(designation);
3089 return lilv_port_get_index (_impl->plugin, port);
3095 boost::shared_ptr<ScalePoints>
3096 LV2Plugin::get_scale_points(uint32_t port_index) const
3098 const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, port_index);
3099 LilvScalePoints* points = lilv_port_get_scale_points(_impl->plugin, port);
3101 boost::shared_ptr<ScalePoints> ret;
3106 ret = boost::shared_ptr<ScalePoints>(new ScalePoints());
3108 LILV_FOREACH(scale_points, i, points) {
3109 const LilvScalePoint* p = lilv_scale_points_get(points, i);
3110 const LilvNode* label = lilv_scale_point_get_label(p);
3111 const LilvNode* value = lilv_scale_point_get_value(p);
3112 if (label && (lilv_node_is_float(value) || lilv_node_is_int(value))) {
3113 ret->insert(make_pair(lilv_node_as_string(label),
3114 lilv_node_as_float(value)));
3118 lilv_scale_points_free(points);
3123 LV2Plugin::run(pframes_t nframes, bool sync_work)
3125 uint32_t const N = parameter_count();
3126 for (uint32_t i = 0; i < N; ++i) {
3127 if (parameter_is_control(i) && parameter_is_input(i)) {
3128 _control_data[i] = _shadow_data[i];
3133 // Execute work synchronously if we're freewheeling (export)
3134 _worker->set_synchronous(sync_work || session().engine().freewheeling());
3137 // Run the plugin for this cycle
3138 lilv_instance_run(_impl->instance, nframes);
3140 // Emit any queued worker responses (calls a plugin callback)
3141 if (_state_worker) {
3142 _state_worker->emit_responses();
3145 _worker->emit_responses();
3148 // Notify the plugin that a work run cycle is complete
3149 if (_impl->work_iface) {
3150 if (_impl->work_iface->end_run) {
3151 _impl->work_iface->end_run(_impl->instance->lv2_handle);
3157 LV2Plugin::latency_compute_run()
3159 if (!_latency_control_port) {
3163 // Run the plugin so that it can set its latency parameter
3165 bool was_activated = _was_activated;
3168 uint32_t port_index = 0;
3169 uint32_t in_index = 0;
3170 uint32_t out_index = 0;
3172 // this is done in the main thread. non realtime.
3173 const samplecnt_t bufsize = _engine.samples_per_cycle();
3174 float* buffer = (float*) malloc(_engine.samples_per_cycle() * sizeof(float));
3176 memset(buffer, 0, sizeof(float) * bufsize);
3178 // FIXME: Ensure plugins can handle in-place processing
3182 while (port_index < parameter_count()) {
3183 if (parameter_is_audio(port_index)) {
3184 if (parameter_is_input(port_index)) {
3185 lilv_instance_connect_port(_impl->instance, port_index, buffer);
3187 } else if (parameter_is_output(port_index)) {
3188 lilv_instance_connect_port(_impl->instance, port_index, buffer);
3197 if (was_activated) {
3204 LV2Plugin::Impl::designated_input (const char* uri, void** bufptrs[], void** bufptr)
3206 const LilvPort* port = NULL;
3207 LilvNode* designation = lilv_new_uri(_world.world, uri);
3208 port = lilv_plugin_get_port_by_designation(
3209 plugin, _world.lv2_InputPort, designation);
3210 lilv_node_free(designation);
3212 bufptrs[lilv_port_get_index(plugin, port)] = bufptr;
3217 static bool lv2_filter (const string& str, void* /*arg*/)
3219 /* Not a dotfile, has a prefix before a period, suffix is "lv2" */
3221 return str[0] != '.' && (str.length() > 3 && str.find (".lv2") == (str.length() - 4));
3225 LV2World::LV2World()
3226 : world(lilv_world_new())
3227 , _bundle_checked(false)
3229 atom_AtomPort = lilv_new_uri(world, LV2_ATOM__AtomPort);
3230 atom_Chunk = lilv_new_uri(world, LV2_ATOM__Chunk);
3231 atom_Sequence = lilv_new_uri(world, LV2_ATOM__Sequence);
3232 atom_bufferType = lilv_new_uri(world, LV2_ATOM__bufferType);
3233 atom_supports = lilv_new_uri(world, LV2_ATOM__supports);
3234 atom_eventTransfer = lilv_new_uri(world, LV2_ATOM__eventTransfer);
3235 ev_EventPort = lilv_new_uri(world, LILV_URI_EVENT_PORT);
3236 ext_logarithmic = lilv_new_uri(world, LV2_PORT_PROPS__logarithmic);
3237 ext_notOnGUI = lilv_new_uri(world, LV2_PORT_PROPS__notOnGUI);
3238 ext_expensive = lilv_new_uri(world, LV2_PORT_PROPS__expensive);
3239 ext_causesArtifacts= lilv_new_uri(world, LV2_PORT_PROPS__causesArtifacts);
3240 ext_notAutomatic = lilv_new_uri(world, LV2_PORT_PROPS__notAutomatic);
3241 ext_rangeSteps = lilv_new_uri(world, LV2_PORT_PROPS__rangeSteps);
3242 groups_group = lilv_new_uri(world, LV2_PORT_GROUPS__group);
3243 groups_element = lilv_new_uri(world, LV2_PORT_GROUPS__element);
3244 lv2_AudioPort = lilv_new_uri(world, LILV_URI_AUDIO_PORT);
3245 lv2_ControlPort = lilv_new_uri(world, LILV_URI_CONTROL_PORT);
3246 lv2_InputPort = lilv_new_uri(world, LILV_URI_INPUT_PORT);
3247 lv2_OutputPort = lilv_new_uri(world, LILV_URI_OUTPUT_PORT);
3248 lv2_inPlaceBroken = lilv_new_uri(world, LV2_CORE__inPlaceBroken);
3249 lv2_isSideChain = lilv_new_uri(world, LV2_CORE_PREFIX "isSideChain");
3250 lv2_index = lilv_new_uri(world, LV2_CORE__index);
3251 lv2_integer = lilv_new_uri(world, LV2_CORE__integer);
3252 lv2_default = lilv_new_uri(world, LV2_CORE__default);
3253 lv2_minimum = lilv_new_uri(world, LV2_CORE__minimum);
3254 lv2_maximum = lilv_new_uri(world, LV2_CORE__maximum);
3255 lv2_reportsLatency = lilv_new_uri(world, LV2_CORE__reportsLatency);
3256 lv2_sampleRate = lilv_new_uri(world, LV2_CORE__sampleRate);
3257 lv2_toggled = lilv_new_uri(world, LV2_CORE__toggled);
3258 lv2_designation = lilv_new_uri(world, LV2_CORE__designation);
3259 lv2_enumeration = lilv_new_uri(world, LV2_CORE__enumeration);
3260 lv2_freewheeling = lilv_new_uri(world, LV2_CORE__freeWheeling);
3261 midi_MidiEvent = lilv_new_uri(world, LILV_URI_MIDI_EVENT);
3262 rdfs_comment = lilv_new_uri(world, LILV_NS_RDFS "comment");
3263 rdfs_label = lilv_new_uri(world, LILV_NS_RDFS "label");
3264 rdfs_range = lilv_new_uri(world, LILV_NS_RDFS "range");
3265 rsz_minimumSize = lilv_new_uri(world, LV2_RESIZE_PORT__minimumSize);
3266 time_Position = lilv_new_uri(world, LV2_TIME__Position);
3267 time_beatsPerMin = lilv_new_uri(world, LV2_TIME__beatsPerMinute);
3268 ui_GtkUI = lilv_new_uri(world, LV2_UI__GtkUI);
3269 ui_external = lilv_new_uri(world, "http://lv2plug.in/ns/extensions/ui#external");
3270 ui_externalkx = lilv_new_uri(world, "http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget");
3271 units_unit = lilv_new_uri(world, LV2_UNITS__unit);
3272 units_render = lilv_new_uri(world, LV2_UNITS__render);
3273 units_hz = lilv_new_uri(world, LV2_UNITS__hz);
3274 units_midiNote = lilv_new_uri(world, LV2_UNITS__midiNote);
3275 units_db = lilv_new_uri(world, LV2_UNITS__db);
3276 patch_writable = lilv_new_uri(world, LV2_PATCH__writable);
3277 patch_Message = lilv_new_uri(world, LV2_PATCH__Message);
3278 opts_requiredOptions = lilv_new_uri(world, LV2_OPTIONS__requiredOption);
3280 lv2_noSampleAccurateCtrl = lilv_new_uri(world, "http://ardour.org/lv2/ext#noSampleAccurateControls"); // deprecated 2016-09-18
3281 routing_connectAllOutputs = lilv_new_uri(world, LV2_ROUTING__connectAllOutputs);
3282 auto_can_write_automatation = lilv_new_uri(world, LV2_AUTOMATE_URI__can_write);
3283 auto_automation_control = lilv_new_uri(world, LV2_AUTOMATE_URI__control);
3284 auto_automation_controlled = lilv_new_uri(world, LV2_AUTOMATE_URI__controlled);
3285 auto_automation_controller = lilv_new_uri(world, LV2_AUTOMATE_URI__controller);
3286 inline_display_in_gui = lilv_new_uri(world, LV2_INLINEDISPLAY__in_gui);
3288 bufz_powerOf2BlockLength = lilv_new_uri(world, LV2_BUF_SIZE__powerOf2BlockLength);
3289 bufz_fixedBlockLength = lilv_new_uri(world, LV2_BUF_SIZE__fixedBlockLength);
3290 bufz_nominalBlockLength = lilv_new_uri(world, "http://lv2plug.in/ns/ext/buf-size#nominalBlockLength");
3291 bufz_coarseBlockLength = lilv_new_uri(world, "http://lv2plug.in/ns/ext/buf-size#coarseBlockLength");
3294 LV2World::~LV2World()
3299 lilv_node_free(bufz_coarseBlockLength);
3300 lilv_node_free(bufz_nominalBlockLength);
3301 lilv_node_free(bufz_fixedBlockLength);
3302 lilv_node_free(bufz_powerOf2BlockLength);
3304 lilv_node_free(lv2_noSampleAccurateCtrl);
3305 lilv_node_free(routing_connectAllOutputs);
3306 lilv_node_free(auto_can_write_automatation);
3307 lilv_node_free(auto_automation_control);
3308 lilv_node_free(auto_automation_controlled);
3309 lilv_node_free(auto_automation_controller);
3311 lilv_node_free(patch_Message);
3312 lilv_node_free(opts_requiredOptions);
3313 lilv_node_free(patch_writable);
3314 lilv_node_free(units_hz);
3315 lilv_node_free(units_midiNote);
3316 lilv_node_free(units_db);
3317 lilv_node_free(units_unit);
3318 lilv_node_free(units_render);
3319 lilv_node_free(ui_externalkx);
3320 lilv_node_free(ui_external);
3321 lilv_node_free(ui_GtkUI);
3322 lilv_node_free(time_beatsPerMin);
3323 lilv_node_free(time_Position);
3324 lilv_node_free(rsz_minimumSize);
3325 lilv_node_free(rdfs_comment);
3326 lilv_node_free(rdfs_label);
3327 lilv_node_free(rdfs_range);
3328 lilv_node_free(midi_MidiEvent);
3329 lilv_node_free(lv2_designation);
3330 lilv_node_free(lv2_enumeration);
3331 lilv_node_free(lv2_freewheeling);
3332 lilv_node_free(lv2_toggled);
3333 lilv_node_free(lv2_sampleRate);
3334 lilv_node_free(lv2_reportsLatency);
3335 lilv_node_free(lv2_index);
3336 lilv_node_free(lv2_integer);
3337 lilv_node_free(lv2_isSideChain);
3338 lilv_node_free(lv2_inPlaceBroken);
3339 lilv_node_free(lv2_OutputPort);
3340 lilv_node_free(lv2_InputPort);
3341 lilv_node_free(lv2_ControlPort);
3342 lilv_node_free(lv2_AudioPort);
3343 lilv_node_free(groups_group);
3344 lilv_node_free(groups_element);
3345 lilv_node_free(ext_rangeSteps);
3346 lilv_node_free(ext_notAutomatic);
3347 lilv_node_free(ext_causesArtifacts);
3348 lilv_node_free(ext_expensive);
3349 lilv_node_free(ext_notOnGUI);
3350 lilv_node_free(ext_logarithmic);
3351 lilv_node_free(ev_EventPort);
3352 lilv_node_free(atom_supports);
3353 lilv_node_free(atom_eventTransfer);
3354 lilv_node_free(atom_bufferType);
3355 lilv_node_free(atom_Sequence);
3356 lilv_node_free(atom_Chunk);
3357 lilv_node_free(atom_AtomPort);
3358 lilv_world_free(world);
3363 LV2World::load_bundled_plugins(bool verbose)
3365 if (!_bundle_checked) {
3367 cout << "Scanning folders for bundled LV2s: " << ARDOUR::lv2_bundled_search_path().to_string() << endl;
3370 vector<string> plugin_objects;
3371 find_paths_matching_filter (plugin_objects, ARDOUR::lv2_bundled_search_path(), lv2_filter, 0, true, true, true);
3372 for ( vector<string>::iterator x = plugin_objects.begin(); x != plugin_objects.end (); ++x) {
3373 #ifdef PLATFORM_WINDOWS
3374 string uri = "file:///" + *x + "/";
3376 string uri = "file://" + *x + "/";
3378 LilvNode *node = lilv_new_uri(world, uri.c_str());
3379 lilv_world_load_bundle(world, node);
3380 lilv_node_free(node);
3383 lilv_world_load_all(world);
3384 _bundle_checked = true;
3388 LV2PluginInfo::LV2PluginInfo (const char* plugin_uri)
3391 _plugin_uri = strdup(plugin_uri);
3394 LV2PluginInfo::~LV2PluginInfo()
3401 LV2PluginInfo::load(Session& session)
3405 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
3406 LilvNode* uri = lilv_new_uri(_world.world, _plugin_uri);
3407 if (!uri) { throw failed_constructor(); }
3408 const LilvPlugin* lp = lilv_plugins_get_by_uri(plugins, uri);
3409 if (!lp) { throw failed_constructor(); }
3410 plugin.reset(new LV2Plugin(session.engine(), session, lp, session.sample_rate()));
3411 lilv_node_free(uri);
3412 plugin->set_info(PluginInfoPtr(shared_from_this ()));
3414 } catch (failed_constructor& err) {
3415 return PluginPtr((Plugin*)0);
3421 std::vector<Plugin::PresetRecord>
3422 LV2PluginInfo::get_presets (bool /*user_only*/) const
3424 std::vector<Plugin::PresetRecord> p;
3426 const LilvPlugin* lp = NULL;
3429 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
3430 LilvNode* uri = lilv_new_uri(_world.world, _plugin_uri);
3431 if (!uri) { throw failed_constructor(); }
3432 lp = lilv_plugins_get_by_uri(plugins, uri);
3433 if (!lp) { throw failed_constructor(); }
3434 lilv_node_free(uri);
3435 } catch (failed_constructor& err) {
3439 // see LV2Plugin::find_presets
3440 LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
3441 LilvNode* pset_Preset = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
3442 LilvNode* rdfs_label = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
3443 LilvNode* rdfs_comment = lilv_new_uri(_world.world, LILV_NS_RDFS "comment");
3445 LilvNodes* presets = lilv_plugin_get_related(lp, pset_Preset);
3446 LILV_FOREACH(nodes, i, presets) {
3447 const LilvNode* preset = lilv_nodes_get(presets, i);
3448 lilv_world_load_resource(_world.world, preset);
3449 LilvNode* name = get_value(_world.world, preset, rdfs_label);
3450 LilvNode* comment = get_value(_world.world, preset, rdfs_comment);
3451 /* TODO properly identify user vs factory presets.
3452 * here's an indirect condition: only factory presets can have comments
3454 bool userpreset = comment ? false : true;
3456 p.push_back (Plugin::PresetRecord (lilv_node_as_string(preset), lilv_node_as_string(name), userpreset, comment ? lilv_node_as_string (comment) : ""));
3457 lilv_node_free(name);
3460 lilv_node_free(comment);
3463 lilv_nodes_free(presets);
3464 lilv_node_free(rdfs_comment);
3465 lilv_node_free(rdfs_label);
3466 lilv_node_free(pset_Preset);
3467 lilv_node_free(lv2_appliesTo);
3473 LV2PluginInfo::discover()
3476 world.load_bundled_plugins();
3477 _world.load_bundled_plugins(true);
3479 PluginInfoList* plugs = new PluginInfoList;
3480 const LilvPlugins* plugins = lilv_world_get_all_plugins(world.world);
3482 LILV_FOREACH(plugins, i, plugins) {
3483 const LilvPlugin* p = lilv_plugins_get(plugins, i);
3484 const LilvNode* pun = lilv_plugin_get_uri(p);
3486 LV2PluginInfoPtr info(new LV2PluginInfo(lilv_node_as_string(pun)));
3488 LilvNode* name = lilv_plugin_get_name(p);
3489 if (!name || !lilv_plugin_get_port_by_index(p, 0)) {
3490 warning << "Ignoring invalid LV2 plugin "
3491 << lilv_node_as_string(lilv_plugin_get_uri(p))
3496 if (lilv_plugin_has_feature(p, world.lv2_inPlaceBroken)) {
3497 warning << string_compose(
3498 _("Ignoring LV2 plugin \"%1\" since it cannot do inplace processing."),
3499 lilv_node_as_string(name)) << endmsg;
3500 lilv_node_free(name);
3505 LilvNodes* required_features = lilv_plugin_get_required_features (p);
3506 LILV_FOREACH(nodes, i, required_features) {
3507 const char* rf = lilv_node_as_uri (lilv_nodes_get (required_features, i));
3509 if (!strcmp (rf, "http://lv2plug.in/ns/ext/instance-access")) { ok = true; }
3510 if (!strcmp (rf, "http://lv2plug.in/ns/ext/data-access")) { ok = true; }
3511 if (!strcmp (rf, LV2_STATE__makePath)) { ok = true; }
3512 if (!strcmp (rf, LV2_LOG__log)) { ok = true; }
3513 if (!strcmp (rf, LV2_WORKER__schedule)) { ok = true; }
3514 if (!strcmp (rf, LV2_STATE_PREFIX "loadDefaultState")) { ok = true; }
3515 if (!strcmp (rf, LV2_URID_MAP_URI)) { ok = true; }
3516 if (!strcmp (rf, LV2_URID_UNMAP_URI)) { ok = true; }
3517 if (!strcmp (rf, "http://lv2plug.in/ns/lv2core#isLive")) { ok = true; }
3518 if (!strcmp (rf, LV2_BUF_SIZE__boundedBlockLength)) { ok = true; }
3519 if (!strcmp (rf, "http://lv2plug.in/ns/ext/buf-size#coarseBlockLength" /*LV2_BUF_SIZE__coarseBlockLength*/)) { ok = true; }
3520 if (!strcmp (rf, LV2_OPTIONS__options)) { ok = true; }
3522 if (!strcmp (rf, LV2_INLINEDISPLAY__queue_draw)) { ok = true; }
3523 if (!strcmp (rf, LV2_MIDNAM__update)) { ok = true; }
3524 if (!strcmp (rf, LV2_BANKPATCH__notify)) { ok = true; }
3527 warning << string_compose (
3528 _("Unsupported required LV2 feature: '%1' in '%2'."),
3529 rf, lilv_node_as_string(name)) << endmsg;
3538 lilv_nodes_free (required_features);
3540 LilvNodes* required_options = lilv_world_find_nodes (world.world, lilv_plugin_get_uri (p), world.opts_requiredOptions, NULL);
3541 if (required_options) {
3542 LILV_FOREACH(nodes, i, required_options) {
3543 const char* ro = lilv_node_as_uri (lilv_nodes_get (required_options, i));
3545 if (!strcmp (ro, LV2_PARAMETERS__sampleRate)) { ok = true; }
3546 if (!strcmp (ro, LV2_BUF_SIZE__minBlockLength)) { ok = true; }
3547 if (!strcmp (ro, LV2_BUF_SIZE__maxBlockLength)) { ok = true; }
3548 if (!strcmp (ro, LV2_BUF_SIZE__sequenceSize)) { ok = true; }
3550 warning << string_compose (
3551 _("Unsupported required LV2 option: '%1' in '%2'."),
3552 ro, lilv_node_as_string(name)) << endmsg;
3557 lilv_nodes_free(required_options);
3565 info->name = string(lilv_node_as_string(name));
3566 lilv_node_free(name);
3567 ARDOUR::PluginScanMessage(_("LV2"), info->name, false);
3569 const LilvPluginClass* pclass = lilv_plugin_get_class(p);
3570 const LilvNode* label = lilv_plugin_class_get_label(pclass);
3571 info->category = lilv_node_as_string(label);
3573 LilvNode* author_name = lilv_plugin_get_author_name(p);
3574 info->creator = author_name ? string(lilv_node_as_string(author_name)) : "Unknown";
3575 lilv_node_free(author_name);
3577 info->path = "/NOPATH"; // Meaningless for LV2
3579 /* count atom-event-ports that feature
3580 * atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent>
3582 * TODO: nicely ask drobilla to make a lilv_ call for that
3584 int count_midi_out = 0;
3585 int count_midi_in = 0;
3586 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
3587 const LilvPort* port = lilv_plugin_get_port_by_index(p, i);
3588 if (lilv_port_is_a(p, port, world.atom_AtomPort)) {
3589 LilvNodes* buffer_types = lilv_port_get_value(
3590 p, port, world.atom_bufferType);
3591 LilvNodes* atom_supports = lilv_port_get_value(
3592 p, port, world.atom_supports);
3594 if (lilv_nodes_contains(buffer_types, world.atom_Sequence)
3595 && lilv_nodes_contains(atom_supports, world.midi_MidiEvent)) {
3596 if (lilv_port_is_a(p, port, world.lv2_InputPort)) {
3599 if (lilv_port_is_a(p, port, world.lv2_OutputPort)) {
3603 lilv_nodes_free(buffer_types);
3604 lilv_nodes_free(atom_supports);
3608 info->n_inputs.set_audio(
3609 lilv_plugin_get_num_ports_of_class(
3610 p, world.lv2_InputPort, world.lv2_AudioPort, NULL));
3611 info->n_inputs.set_midi(
3612 lilv_plugin_get_num_ports_of_class(
3613 p, world.lv2_InputPort, world.ev_EventPort, NULL)
3616 info->n_outputs.set_audio(
3617 lilv_plugin_get_num_ports_of_class(
3618 p, world.lv2_OutputPort, world.lv2_AudioPort, NULL));
3619 info->n_outputs.set_midi(
3620 lilv_plugin_get_num_ports_of_class(
3621 p, world.lv2_OutputPort, world.ev_EventPort, NULL)
3624 info->unique_id = lilv_node_as_uri(lilv_plugin_get_uri(p));
3625 info->index = 0; // Meaningless for LV2
3627 plugs->push_back(info);