LV2/midnam tweaks - fix race conditions
[ardour.git] / libs / ardour / lv2_plugin.cc
1 /*
2     Copyright (C) 2008-2012 Paul Davis
3     Author: David Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <cctype>
21 #include <string>
22 #include <vector>
23 #include <limits>
24
25 #include <cmath>
26 #include <cstdlib>
27 #include <cstring>
28
29 #include "pbd/gstdio_compat.h"
30 #include <glib/gprintf.h>
31 #include <glibmm.h>
32
33 #include <boost/utility.hpp>
34
35 #include "pbd/file_utils.h"
36 #include "pbd/stl_delete.h"
37 #include "pbd/compose.h"
38 #include "pbd/error.h"
39 #include "pbd/locale_guard.h"
40 #include "pbd/replace_all.h"
41 #include "pbd/xml++.h"
42
43 #ifdef PLATFORM_WINDOWS
44 #include <shlobj.h> // CSIDL_*
45 #include "pbd/windows_special_dirs.h"
46 #endif
47
48 #include "libardour-config.h"
49
50 #include "ardour/audio_buffer.h"
51 #include "ardour/audioengine.h"
52 #include "ardour/debug.h"
53 #include "ardour/lv2_plugin.h"
54 #include "ardour/midi_patch_manager.h"
55 #include "ardour/session.h"
56 #include "ardour/tempo.h"
57 #include "ardour/types.h"
58 #include "ardour/utils.h"
59 #include "ardour/worker.h"
60 #include "ardour/search_paths.h"
61
62 #include "pbd/i18n.h"
63 #include <locale.h>
64
65 #include <lilv/lilv.h>
66
67 #include "lv2/lv2plug.in/ns/ext/atom/atom.h"
68 #include "lv2/lv2plug.in/ns/ext/atom/forge.h"
69 #include "lv2/lv2plug.in/ns/ext/log/log.h"
70 #include "lv2/lv2plug.in/ns/ext/midi/midi.h"
71 #include "lv2/lv2plug.in/ns/ext/port-props/port-props.h"
72 #include "lv2/lv2plug.in/ns/ext/presets/presets.h"
73 #include "lv2/lv2plug.in/ns/ext/state/state.h"
74 #include "lv2/lv2plug.in/ns/ext/time/time.h"
75 #include "lv2/lv2plug.in/ns/ext/worker/worker.h"
76 #include "lv2/lv2plug.in/ns/ext/resize-port/resize-port.h"
77 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
78 #include "lv2/lv2plug.in/ns/extensions/units/units.h"
79 #include "lv2/lv2plug.in/ns/ext/patch/patch.h"
80 #include "lv2/lv2plug.in/ns/ext/port-groups/port-groups.h"
81 #ifdef HAVE_LV2_1_2_0
82 #include "lv2/lv2plug.in/ns/ext/buf-size/buf-size.h"
83 #include "lv2/lv2plug.in/ns/ext/options/options.h"
84 #endif
85
86 #include "lv2_evbuf.h"
87
88 #ifdef HAVE_SUIL
89 #include <suil/suil.h>
90 #endif
91
92 // Compatibility for old LV2
93 #ifndef LV2_ATOM_CONTENTS_CONST
94 #define LV2_ATOM_CONTENTS_CONST(type, atom) \
95         ((const void*)((const uint8_t*)(atom) + sizeof(type)))
96 #endif
97 #ifndef LV2_ATOM_BODY_CONST
98 #define LV2_ATOM_BODY_CONST(atom) LV2_ATOM_CONTENTS_CONST(LV2_Atom, atom)
99 #endif
100 #ifndef LV2_PATCH__property
101 #define LV2_PATCH__property LV2_PATCH_PREFIX "property"
102 #endif
103 #ifndef LV2_PATCH__value
104 #define LV2_PATCH__value LV2_PATCH_PREFIX "value"
105 #endif
106 #ifndef LV2_PATCH__writable
107 #define LV2_PATCH__writable LV2_PATCH_PREFIX "writable"
108 #endif
109
110 /** The number of MIDI buffers that will fit in a UI/worker comm buffer.
111     This needs to be roughly the number of cycles the UI will get around to
112     actually processing the traffic.  Lower values are flakier but save memory.
113 */
114 static const size_t NBUFS = 4;
115
116 using namespace std;
117 using namespace ARDOUR;
118 using namespace PBD;
119
120 bool LV2Plugin::force_state_save = false;
121
122 class LV2World : boost::noncopyable {
123 public:
124         LV2World ();
125         ~LV2World ();
126
127         void load_bundled_plugins(bool verbose=false);
128
129         LilvWorld* world;
130
131         LilvNode* atom_AtomPort;
132         LilvNode* atom_Chunk;
133         LilvNode* atom_Sequence;
134         LilvNode* atom_bufferType;
135         LilvNode* atom_eventTransfer;
136         LilvNode* atom_supports;
137         LilvNode* ev_EventPort;
138         LilvNode* ext_logarithmic;
139         LilvNode* ext_notOnGUI;
140         LilvNode* ext_expensive;
141         LilvNode* ext_causesArtifacts;
142         LilvNode* ext_notAutomatic;
143         LilvNode* ext_rangeSteps;
144         LilvNode* groups_group;
145         LilvNode* groups_element;
146         LilvNode* lv2_AudioPort;
147         LilvNode* lv2_ControlPort;
148         LilvNode* lv2_InputPort;
149         LilvNode* lv2_OutputPort;
150         LilvNode* lv2_designation;
151         LilvNode* lv2_enumeration;
152         LilvNode* lv2_freewheeling;
153         LilvNode* lv2_inPlaceBroken;
154         LilvNode* lv2_isSideChain;
155         LilvNode* lv2_index;
156         LilvNode* lv2_integer;
157         LilvNode* lv2_default;
158         LilvNode* lv2_minimum;
159         LilvNode* lv2_maximum;
160         LilvNode* lv2_reportsLatency;
161         LilvNode* lv2_sampleRate;
162         LilvNode* lv2_toggled;
163         LilvNode* midi_MidiEvent;
164         LilvNode* rdfs_comment;
165         LilvNode* rdfs_label;
166         LilvNode* rdfs_range;
167         LilvNode* rsz_minimumSize;
168         LilvNode* time_Position;
169         LilvNode* ui_GtkUI;
170         LilvNode* ui_external;
171         LilvNode* ui_externalkx;
172         LilvNode* units_hz;
173         LilvNode* units_db;
174         LilvNode* units_unit;
175         LilvNode* units_render;
176         LilvNode* units_midiNote;
177         LilvNode* patch_writable;
178         LilvNode* patch_Message;
179 #ifdef HAVE_LV2_1_2_0
180         LilvNode* bufz_powerOf2BlockLength;
181         LilvNode* bufz_fixedBlockLength;
182         LilvNode* bufz_nominalBlockLength;
183         LilvNode* bufz_coarseBlockLength;
184 #endif
185
186 #ifdef HAVE_LV2_1_10_0
187         LilvNode* atom_int;
188         LilvNode* atom_float;
189         LilvNode* atom_object; // new in 1.8
190         LilvNode* atom_vector;
191 #endif
192 #ifdef LV2_EXTENDED
193         LilvNode* lv2_noSampleAccurateCtrl;
194         LilvNode* auto_can_write_automatation; // lv2:optionalFeature
195         LilvNode* auto_automation_control; // atom:supports
196         LilvNode* auto_automation_controlled; // lv2:portProperty
197         LilvNode* auto_automation_controller; // lv2:portProperty
198         LilvNode* inline_display_in_gui; // lv2:optionalFeature
199 #endif
200
201 private:
202         bool _bundle_checked;
203 };
204
205 static LV2World _world;
206
207 /* worker extension */
208
209 /** Called by the plugin to schedule non-RT work. */
210 static LV2_Worker_Status
211 work_schedule(LV2_Worker_Schedule_Handle handle,
212               uint32_t                   size,
213               const void*                data)
214 {
215         return (((Worker*)handle)->schedule(size, data)
216                 ? LV2_WORKER_SUCCESS
217                 : LV2_WORKER_ERR_UNKNOWN);
218 }
219
220 /** Called by the plugin to respond to non-RT work. */
221 static LV2_Worker_Status
222 work_respond(LV2_Worker_Respond_Handle handle,
223              uint32_t                  size,
224              const void*               data)
225 {
226         return (((Worker*)handle)->respond(size, data)
227                 ? LV2_WORKER_SUCCESS
228                 : LV2_WORKER_ERR_UNKNOWN);
229 }
230
231 #ifdef LV2_EXTENDED
232 /* inline display extension */
233 void
234 LV2Plugin::queue_draw (LV2_Inline_Display_Handle handle)
235 {
236         LV2Plugin* plugin = (LV2Plugin*)handle;
237         plugin->QueueDraw(); /* EMIT SIGNAL */
238 }
239
240 void
241 LV2Plugin::midnam_update (LV2_Midnam_Handle handle)
242 {
243         LV2Plugin* plugin = (LV2Plugin*)handle;
244         plugin->_midnam_dirty = true;
245         plugin->UpdateMidnam (); /* EMIT SIGNAL */
246 }
247
248 void
249 LV2Plugin::bankpatch_notify (LV2_BankPatch_Handle handle, uint8_t chn, uint32_t bank, uint8_t pgm)
250 {
251         LV2Plugin* plugin = (LV2Plugin*)handle;
252         if (chn > 15) {
253                 return;
254         }
255         plugin->seen_bankpatch = true;
256         if (pgm > 127 || bank > 16383) {
257                 plugin->_bankpatch[chn] = UINT32_MAX;
258         } else {
259                 plugin->_bankpatch[chn] = (bank << 7) | pgm;
260         }
261         plugin->BankPatchChange (chn); /* EMIT SIGNAL */
262 }
263 #endif
264
265 /* log extension */
266
267 static int
268 log_vprintf(LV2_Log_Handle /*handle*/,
269             LV2_URID       type,
270             const char*    fmt,
271             va_list        args)
272 {
273         char* str = NULL;
274         const int ret = g_vasprintf(&str, fmt, args);
275         /* strip trailing whitespace */
276         while (strlen (str) > 0 && isspace (str[strlen (str) - 1])) {
277                 str[strlen (str) - 1] = '\0';
278         }
279         if (strlen (str) == 0) {
280                 return 0;
281         }
282
283         if (type == URIMap::instance().urids.log_Error) {
284                 error << str << endmsg;
285         } else if (type == URIMap::instance().urids.log_Warning) {
286                 warning << str << endmsg;
287         } else if (type == URIMap::instance().urids.log_Note) {
288                 info << str << endmsg;
289         } else if (type == URIMap::instance().urids.log_Trace) {
290                 DEBUG_TRACE(DEBUG::LV2, str);
291         }
292         return ret;
293 }
294
295 static int
296 log_printf(LV2_Log_Handle handle,
297            LV2_URID       type,
298            const char*    fmt, ...)
299 {
300         va_list args;
301         va_start(args, fmt);
302         const int ret = log_vprintf(handle, type, fmt, args);
303         va_end(args);
304         return ret;
305 }
306
307 struct LV2Plugin::Impl {
308         Impl() : plugin(0), ui(0), ui_type(0), name(0), author(0), instance(0)
309                , work_iface(0)
310 #ifdef HAVE_LV2_1_2_0
311                , opts_iface(0)
312 #endif
313                , state(0)
314                , block_length(0)
315 #ifdef HAVE_LV2_1_2_0
316                , options(0)
317 #endif
318 #ifdef LV2_EXTENDED
319                , queue_draw(0)
320                , midnam(0)
321 #endif
322         {}
323
324         /** Find the LV2 input port with the given designation.
325          * If found, bufptrs[port_index] will be set to bufptr.
326          */
327         const LilvPort* designated_input (const char* uri, void** bufptrs[], void** bufptr);
328
329         const LilvPlugin*            plugin;
330         const LilvUI*                ui;
331         const LilvNode*              ui_type;
332         LilvNode*                    name;
333         LilvNode*                    author;
334         LilvInstance*                instance;
335         const LV2_Worker_Interface*  work_iface;
336 #ifdef HAVE_LV2_1_2_0
337         const LV2_Options_Interface* opts_iface;
338 #endif
339         LilvState*                   state;
340         LV2_Atom_Forge               forge;
341         LV2_Atom_Forge               ui_forge;
342         int32_t                      block_length;
343 #ifdef HAVE_LV2_1_2_0
344         LV2_Options_Option*          options;
345 #endif
346 #ifdef LV2_EXTENDED
347         LV2_Inline_Display*          queue_draw;
348         LV2_Midnam*                  midnam;
349         LV2_BankPatch*               bankpatch;
350 #endif
351 };
352
353 LV2Plugin::LV2Plugin (AudioEngine& engine,
354                       Session&     session,
355                       const void*  c_plugin,
356                       framecnt_t   rate)
357         : Plugin (engine, session)
358         , Workee ()
359         , _impl(new Impl())
360         , _features(NULL)
361         , _worker(NULL)
362         , _state_worker(NULL)
363         , _insert_id("0")
364         , _patch_port_in_index((uint32_t)-1)
365         , _patch_port_out_index((uint32_t)-1)
366         , _uri_map(URIMap::instance())
367         , _no_sample_accurate_ctrl (false)
368 {
369         init(c_plugin, rate);
370 }
371
372 LV2Plugin::LV2Plugin (const LV2Plugin& other)
373         : Plugin (other)
374         , Workee ()
375         , _impl(new Impl())
376         , _features(NULL)
377         , _worker(NULL)
378         , _state_worker(NULL)
379         , _insert_id(other._insert_id)
380         , _patch_port_in_index((uint32_t)-1)
381         , _patch_port_out_index((uint32_t)-1)
382         , _uri_map(URIMap::instance())
383         , _no_sample_accurate_ctrl (false)
384 {
385         init(other._impl->plugin, other._sample_rate);
386
387         for (uint32_t i = 0; i < parameter_count(); ++i) {
388                 _control_data[i] = other._shadow_data[i];
389                 _shadow_data[i]  = other._shadow_data[i];
390         }
391 }
392
393 void
394 LV2Plugin::init(const void* c_plugin, framecnt_t rate)
395 {
396         DEBUG_TRACE(DEBUG::LV2, "init\n");
397
398         _impl->plugin           = (const LilvPlugin*)c_plugin;
399         _impl->ui               = NULL;
400         _impl->ui_type          = NULL;
401         _to_ui                  = NULL;
402         _from_ui                = NULL;
403         _control_data           = 0;
404         _shadow_data            = 0;
405         _atom_ev_buffers        = 0;
406         _ev_buffers             = 0;
407         _bpm_control_port       = 0;
408         _freewheel_control_port = 0;
409         _latency_control_port   = 0;
410         _next_cycle_start       = std::numeric_limits<framepos_t>::max();
411         _next_cycle_speed       = 1.0;
412         _seq_size               = _engine.raw_buffer_size(DataType::MIDI);
413         _state_version          = 0;
414         _was_activated          = false;
415         _has_state_interface    = false;
416         _can_write_automation   = false;
417         _inline_display_in_gui  = false;
418         _max_latency            = 0;
419         _current_latency        = 0;
420         _impl->block_length     = _session.get_block_size();
421
422         _instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
423         _data_access_feature.URI     = "http://lv2plug.in/ns/ext/data-access";
424         _make_path_feature.URI       = LV2_STATE__makePath;
425         _log_feature.URI             = LV2_LOG__log;
426         _work_schedule_feature.URI   = LV2_WORKER__schedule;
427         _work_schedule_feature.data  = NULL;
428         _def_state_feature.URI       = LV2_STATE_PREFIX "loadDefaultState";  // Post LV2-1.2.0
429         _def_state_feature.data      = NULL;
430
431         const LilvPlugin* plugin = _impl->plugin;
432
433         LilvNode* state_iface_uri = lilv_new_uri(_world.world, LV2_STATE__interface);
434         LilvNode* state_uri       = lilv_new_uri(_world.world, LV2_STATE_URI);
435         _has_state_interface =
436                 // What plugins should have (lv2:extensionData state:Interface)
437                 lilv_plugin_has_extension_data(plugin, state_iface_uri)
438                 // What some outdated/incorrect ones have
439                 || lilv_plugin_has_feature(plugin, state_uri);
440         lilv_node_free(state_uri);
441         lilv_node_free(state_iface_uri);
442
443         _features    = (LV2_Feature**)calloc(14, sizeof(LV2_Feature*));
444         _features[0] = &_instance_access_feature;
445         _features[1] = &_data_access_feature;
446         _features[2] = &_make_path_feature;
447         _features[3] = _uri_map.uri_map_feature();
448         _features[4] = _uri_map.urid_map_feature();
449         _features[5] = _uri_map.urid_unmap_feature();
450         _features[6] = &_log_feature;
451
452         unsigned n_features = 7;
453 #ifdef HAVE_LV2_1_2_0
454         _features[n_features++] = &_def_state_feature;
455 #endif
456
457         lv2_atom_forge_init(&_impl->forge, _uri_map.urid_map());
458         lv2_atom_forge_init(&_impl->ui_forge, _uri_map.urid_map());
459
460 #ifdef LV2_EXTENDED
461         _impl->queue_draw = (LV2_Inline_Display*)
462                 malloc (sizeof(LV2_Inline_Display));
463         _impl->queue_draw->handle     = this;
464         _impl->queue_draw->queue_draw = queue_draw;
465
466         _queue_draw_feature.URI  = LV2_INLINEDISPLAY__queue_draw;
467         _queue_draw_feature.data = _impl->queue_draw;
468         _features[n_features++]  = &_queue_draw_feature;
469
470         _impl->midnam = (LV2_Midnam*)
471                 malloc (sizeof(LV2_Midnam));
472         _impl->midnam->handle = this;
473         _impl->midnam->update = midnam_update;
474
475         _midnam_feature.URI  = LV2_MIDNAM__update;
476         _midnam_feature.data = _impl->midnam;
477         _features[n_features++]  = &_midnam_feature;
478
479         _impl->bankpatch = (LV2_BankPatch*)
480                 malloc (sizeof(LV2_BankPatch));
481         _impl->bankpatch->handle = this;
482         _impl->bankpatch->notify = bankpatch_notify;
483
484         _bankpatch_feature.URI  = LV2_BANKPATCH__notify;
485         _bankpatch_feature.data = _impl->bankpatch;
486         _features[n_features++]  = &_bankpatch_feature;
487 #endif
488
489 #ifdef HAVE_LV2_1_2_0
490         LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
491         static const int32_t _min_block_length = 1;   // may happen during split-cycles
492         static const int32_t _max_block_length = 8192; // max possible (with all engines and during export)
493         /* Consider updating max-block-size whenever the buffersize changes.
494          * It requires re-instantiating the plugin (which is a non-realtime operation),
495          * so it should be done lightly and only for plugins that require it.
496          *
497          * given that the block-size can change at any time (split-cycles) ardour currently
498          * does not support plugins that require bufz_fixedBlockLength.
499          */
500         LV2_Options_Option options[] = {
501                 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__minBlockLength),
502                   sizeof(int32_t), atom_Int, &_min_block_length },
503                 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__maxBlockLength),
504                   sizeof(int32_t), atom_Int, &_max_block_length },
505                 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__sequenceSize),
506                   sizeof(int32_t), atom_Int, &_seq_size },
507                 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://lv2plug.in/ns/ext/buf-size#nominalBlockLength"),
508                   sizeof(int32_t), atom_Int, &_impl->block_length },
509                 { LV2_OPTIONS_INSTANCE, 0, 0, 0, 0, NULL }
510         };
511
512         _impl->options = (LV2_Options_Option*) malloc (sizeof (options));
513         memcpy ((void*) _impl->options, (void*) options, sizeof (options));
514
515         _options_feature.URI    = LV2_OPTIONS__options;
516         _options_feature.data   = _impl->options;
517         _features[n_features++] = &_options_feature;
518 #endif
519
520 #ifdef LV2_EXTENDED
521         seen_bankpatch = false;
522         for (uint32_t chn = 0; chn < 16; ++chn) {
523                 _bankpatch[chn] = UINT32_MAX;
524         }
525 #endif
526
527         LV2_State_Make_Path* make_path = (LV2_State_Make_Path*)malloc(
528                 sizeof(LV2_State_Make_Path));
529         make_path->handle = this;
530         make_path->path = &lv2_state_make_path;
531         _make_path_feature.data = make_path;
532
533         LV2_Log_Log* log = (LV2_Log_Log*)malloc(sizeof(LV2_Log_Log));
534         log->handle  = this;
535         log->printf  = &log_printf;
536         log->vprintf = &log_vprintf;
537         _log_feature.data = log;
538
539         const size_t ring_size = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
540         LilvNode* worker_schedule = lilv_new_uri(_world.world, LV2_WORKER__schedule);
541         if (lilv_plugin_has_feature(plugin, worker_schedule)) {
542                 LV2_Worker_Schedule* schedule = (LV2_Worker_Schedule*)malloc(
543                         sizeof(LV2_Worker_Schedule));
544                 _worker                     = new Worker(this, ring_size);
545                 schedule->handle            = _worker;
546                 schedule->schedule_work     = work_schedule;
547                 _work_schedule_feature.data = schedule;
548                 _features[n_features++]     = &_work_schedule_feature;
549         }
550         lilv_node_free(worker_schedule);
551
552         if (_has_state_interface) {
553                 // Create a non-threaded worker for use by state restore
554                 _state_worker = new Worker(this, ring_size, false);
555         }
556
557         _impl->instance = lilv_plugin_instantiate(plugin, rate, _features);
558         _impl->name     = lilv_plugin_get_name(plugin);
559         _impl->author   = lilv_plugin_get_author_name(plugin);
560
561         if (_impl->instance == 0) {
562                 error << _("LV2: Failed to instantiate plugin ") << uri() << endmsg;
563                 throw failed_constructor();
564         }
565
566         _instance_access_feature.data              = (void*)_impl->instance->lv2_handle;
567         _data_access_extension_data.extension_data = _impl->instance->lv2_descriptor->extension_data;
568         _data_access_feature.data                  = &_data_access_extension_data;
569
570         LilvNode* worker_iface_uri = lilv_new_uri(_world.world, LV2_WORKER__interface);
571         if (lilv_plugin_has_extension_data(plugin, worker_iface_uri)) {
572                 _impl->work_iface = (const LV2_Worker_Interface*)extension_data(
573                         LV2_WORKER__interface);
574         }
575         lilv_node_free(worker_iface_uri);
576
577
578 #ifdef HAVE_LV2_1_2_0
579         LilvNode* options_iface_uri = lilv_new_uri(_world.world, LV2_OPTIONS__interface);
580         if (lilv_plugin_has_extension_data(plugin, options_iface_uri)) {
581                 _impl->opts_iface = (const LV2_Options_Interface*)extension_data(
582                         LV2_OPTIONS__interface);
583         }
584         lilv_node_free(options_iface_uri);
585 #endif
586
587 #ifdef LV2_EXTENDED
588         _display_interface = (const LV2_Inline_Display_Interface*)
589                 extension_data (LV2_INLINEDISPLAY__interface);
590
591         _midname_interface = (const LV2_Midnam_Interface*)
592                 extension_data (LV2_MIDNAM__interface);
593         if (_midname_interface) {
594                 _midnam_dirty = true;
595                 read_midnam ();
596         }
597 #endif
598
599         if (lilv_plugin_has_feature(plugin, _world.lv2_inPlaceBroken)) {
600                 error << string_compose(
601                     _("LV2: \"%1\" cannot be used, since it cannot do inplace processing."),
602                     lilv_node_as_string(_impl->name)) << endmsg;
603                 lilv_node_free(_impl->name);
604                 lilv_node_free(_impl->author);
605                 throw failed_constructor();
606         }
607
608 #ifdef HAVE_LV2_1_2_0
609         LilvNodes *required_features = lilv_plugin_get_required_features (plugin);
610         if (lilv_nodes_contains (required_features, _world.bufz_powerOf2BlockLength) ||
611                         lilv_nodes_contains (required_features, _world.bufz_fixedBlockLength)
612            ) {
613                 error << string_compose(
614                     _("LV2: \"%1\" buffer-size requirements cannot be satisfied."),
615                     lilv_node_as_string(_impl->name)) << endmsg;
616                 lilv_node_free(_impl->name);
617                 lilv_node_free(_impl->author);
618                 lilv_nodes_free(required_features);
619                 throw failed_constructor();
620         }
621         lilv_nodes_free(required_features);
622 #endif
623
624         LilvNodes* optional_features = lilv_plugin_get_optional_features (plugin);
625 #ifdef HAVE_LV2_1_2_0
626         if (lilv_nodes_contains (optional_features, _world.bufz_coarseBlockLength)) {
627                 _no_sample_accurate_ctrl = true;
628         }
629 #endif
630 #ifdef LV2_EXTENDED
631         if (lilv_nodes_contains (optional_features, _world.lv2_noSampleAccurateCtrl)) {
632                 /* deprecated 2016-Sep-18 in favor of bufz_coarseBlockLength */
633                 _no_sample_accurate_ctrl = true;
634         }
635         if (lilv_nodes_contains (optional_features, _world.auto_can_write_automatation)) {
636                 _can_write_automation = true;
637         }
638         if (lilv_nodes_contains (optional_features, _world.inline_display_in_gui)) {
639                 _inline_display_in_gui = true;
640         }
641         lilv_nodes_free(optional_features);
642 #endif
643
644 #ifdef HAVE_LILV_0_16_0
645         // Load default state
646         if (_worker) {
647                 /* immediately schedule any work,
648                  * so that state restore later will not find a busy
649                  * worker.  latency_compute_run() flushes any replies
650                  */
651                 _worker->set_synchronous(true);
652         }
653         LilvState* state = lilv_state_new_from_world(
654                 _world.world, _uri_map.urid_map(), lilv_plugin_get_uri(_impl->plugin));
655         if (state && _has_state_interface) {
656                 lilv_state_restore(state, _impl->instance, NULL, NULL, 0, NULL);
657         }
658         lilv_state_free(state);
659 #endif
660
661         _sample_rate = rate;
662
663         const uint32_t num_ports = this->num_ports();
664         for (uint32_t i = 0; i < num_ports; ++i) {
665                 const LilvPort* port  = lilv_plugin_get_port_by_index(_impl->plugin, i);
666                 PortFlags       flags = 0;
667                 size_t          minimumSize = 0;
668
669                 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_OutputPort)) {
670                         flags |= PORT_OUTPUT;
671                 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_InputPort)) {
672                         flags |= PORT_INPUT;
673                 } else {
674                         error << string_compose(
675                                 "LV2: \"%1\" port %2 is neither input nor output",
676                                 lilv_node_as_string(_impl->name), i) << endmsg;
677                         throw failed_constructor();
678                 }
679
680                 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_ControlPort)) {
681                         flags |= PORT_CONTROL;
682                 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_AudioPort)) {
683                         flags |= PORT_AUDIO;
684                 } else if (lilv_port_is_a(_impl->plugin, port, _world.ev_EventPort)) {
685                         flags |= PORT_EVENT;
686                         flags |= PORT_MIDI;  // We assume old event API ports are for MIDI
687                 } else if (lilv_port_is_a(_impl->plugin, port, _world.atom_AtomPort)) {
688                         LilvNodes* buffer_types = lilv_port_get_value(
689                                 _impl->plugin, port, _world.atom_bufferType);
690                         LilvNodes* atom_supports = lilv_port_get_value(
691                                 _impl->plugin, port, _world.atom_supports);
692
693                         if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
694                                 flags |= PORT_SEQUENCE;
695                                 if (lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)) {
696                                         flags |= PORT_MIDI;
697                                 }
698                                 if (lilv_nodes_contains(atom_supports, _world.time_Position)) {
699                                         flags |= PORT_POSITION;
700                                 }
701 #ifdef LV2_EXTENDED
702                                 if (lilv_nodes_contains(atom_supports, _world.auto_automation_control)) {
703                                         flags |= PORT_AUTOCTRL;
704                                 }
705 #endif
706                                 if (lilv_nodes_contains(atom_supports, _world.patch_Message)) {
707                                         flags |= PORT_PATCHMSG;
708                                         if (flags & PORT_INPUT) {
709                                                 _patch_port_in_index = i;
710                                         } else {
711                                                 _patch_port_out_index = i;
712                                         }
713                                 }
714                         }
715                         LilvNodes* min_size_v = lilv_port_get_value(_impl->plugin, port, _world.rsz_minimumSize);
716                         LilvNode* min_size = min_size_v ? lilv_nodes_get_first(min_size_v) : NULL;
717                         if (min_size && lilv_node_is_int(min_size)) {
718                                 minimumSize = lilv_node_as_int(min_size);
719                         }
720                         lilv_nodes_free(min_size_v);
721                         lilv_nodes_free(buffer_types);
722                         lilv_nodes_free(atom_supports);
723                 } else {
724                         error << string_compose(
725                                 "LV2: \"%1\" port %2 has no known data type",
726                                 lilv_node_as_string(_impl->name), i) << endmsg;
727                         throw failed_constructor();
728                 }
729
730                 if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
731                         if (lilv_port_has_property(_impl->plugin, port, _world.ext_causesArtifacts)) {
732                                 flags |= PORT_NOAUTO;
733                         }
734                         if (lilv_port_has_property(_impl->plugin, port, _world.ext_notAutomatic)) {
735                                 flags |= PORT_NOAUTO;
736                         }
737                         if (lilv_port_has_property(_impl->plugin, port, _world.ext_expensive)) {
738                                 flags |= PORT_NOAUTO;
739                         }
740                 }
741 #ifdef LV2_EXTENDED
742                 if (lilv_port_has_property(_impl->plugin, port, _world.auto_automation_controlled)) {
743                         if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
744                                 flags |= PORT_CTRLED;
745                         }
746                 }
747                 if (lilv_port_has_property(_impl->plugin, port, _world.auto_automation_controller)) {
748                         if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
749                                 flags |= PORT_CTRLER;
750                         }
751                 }
752 #endif
753
754                 _port_flags.push_back(flags);
755                 _port_minimumSize.push_back(minimumSize);
756                 DEBUG_TRACE(DEBUG::LV2, string_compose("port %1 buffer %2 bytes\n", i, minimumSize));
757         }
758
759         _control_data = new float[num_ports];
760         _shadow_data  = new float[num_ports];
761         _defaults     = new float[num_ports];
762         _ev_buffers   = new LV2_Evbuf*[num_ports];
763         memset(_ev_buffers, 0, sizeof(LV2_Evbuf*) * num_ports);
764
765         const bool     latent        = lilv_plugin_has_latency(plugin);
766         const uint32_t latency_index = (latent)
767                 ? lilv_plugin_get_latency_port_index(plugin)
768                 : 0;
769
770         // Build an array of pointers to special parameter buffers
771         void*** params = new void**[num_ports];
772         for (uint32_t i = 0; i < num_ports; ++i) {
773                 params[i] = NULL;
774         }
775         _impl->designated_input (LV2_TIME__beatsPerMinute, params, (void**)&_bpm_control_port);
776         _impl->designated_input (LV2_CORE__freeWheeling, params, (void**)&_freewheel_control_port);
777
778         for (uint32_t i = 0; i < num_ports; ++i) {
779                 const LilvPort* port = lilv_plugin_get_port_by_index(plugin, i);
780                 const LilvNode* sym  = lilv_port_get_symbol(plugin, port);
781
782                 // Store index in map so we can look up index by symbol
783                 _port_indices.insert(std::make_pair(lilv_node_as_string(sym), i));
784
785                 // Get range and default value if applicable
786                 if (parameter_is_control(i)) {
787                         LilvNode* def;
788                         lilv_port_get_range(plugin, port, &def, NULL, NULL);
789                         _defaults[i] = def ? lilv_node_as_float(def) : 0.0f;
790                         if (lilv_port_has_property (plugin, port, _world.lv2_sampleRate)) {
791                                 _defaults[i] *= _session.frame_rate ();
792                         }
793                         lilv_node_free(def);
794
795                         lilv_instance_connect_port(_impl->instance, i, &_control_data[i]);
796
797                         if (latent && i == latency_index) {
798                                 LilvNode *max;
799                                 lilv_port_get_range(_impl->plugin, port, NULL, NULL, &max);
800                                 _max_latency = max ? lilv_node_as_float(max) : .02 * _sample_rate;
801                                 _latency_control_port  = &_control_data[i];
802                                 *_latency_control_port = 0;
803                         }
804
805                         if (parameter_is_input(i)) {
806                                 _shadow_data[i] = default_value(i);
807                                 if (params[i]) {
808                                         *params[i] = (void*)&_shadow_data[i];
809                                 }
810                         }
811                 } else {
812                         _defaults[i] = 0.0f;
813                 }
814         }
815
816         delete[] params;
817
818         LilvUIs* uis = lilv_plugin_get_uis(plugin);
819         if (lilv_uis_size(uis) > 0) {
820 #ifdef HAVE_SUIL
821                 // Look for embeddable UI
822                 LILV_FOREACH(uis, u, uis) {
823                         const LilvUI*   this_ui      = lilv_uis_get(uis, u);
824                         const LilvNode* this_ui_type = NULL;
825                         if (lilv_ui_is_supported(this_ui,
826                                                  suil_ui_supported,
827                                                  _world.ui_GtkUI,
828                                                  &this_ui_type)) {
829                                 // TODO: Multiple UI support
830                                 _impl->ui      = this_ui;
831                                 _impl->ui_type = this_ui_type;
832                                 break;
833                         }
834                 }
835 #else
836                 // Look for Gtk native UI
837                 LILV_FOREACH(uis, i, uis) {
838                         const LilvUI* ui = lilv_uis_get(uis, i);
839                         if (lilv_ui_is_a(ui, _world.ui_GtkUI)) {
840                                 _impl->ui      = ui;
841                                 _impl->ui_type = _world.ui_GtkUI;
842                                 break;
843                         }
844                 }
845 #endif
846
847                 // If Gtk UI is not available, try to find external UI
848                 if (!_impl->ui) {
849                         LILV_FOREACH(uis, i, uis) {
850                                 const LilvUI* ui = lilv_uis_get(uis, i);
851                                 if (lilv_ui_is_a(ui, _world.ui_externalkx)) {
852                                         _impl->ui      = ui;
853                                         _impl->ui_type = _world.ui_external;
854                                         break;
855                                 }
856                                 if (lilv_ui_is_a(ui, _world.ui_external)) {
857                                         _impl->ui      = ui;
858                                         _impl->ui_type = _world.ui_external;
859                                 }
860                         }
861                 }
862         }
863
864         load_supported_properties(_property_descriptors);
865         allocate_atom_event_buffers();
866         latency_compute_run();
867 }
868
869 int
870 LV2Plugin::set_block_size (pframes_t nframes)
871 {
872 #ifdef HAVE_LV2_1_2_0
873         if (_impl->opts_iface) {
874                 LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
875                 _impl->block_length = nframes;
876                 LV2_Options_Option block_size_option = {
877                         LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id ("http://lv2plug.in/ns/ext/buf-size#nominalBlockLength"),
878                         sizeof(int32_t), atom_Int, (void*)&_impl->block_length
879                 };
880                 _impl->opts_iface->set (_impl->instance->lv2_handle, &block_size_option);
881         }
882 #endif
883         return 0;
884 }
885
886 bool
887 LV2Plugin::requires_fixed_sized_buffers () const
888 {
889         /* This controls if Ardour will split the plugin's run()
890          * on automation events in order to pass sample-accurate automation
891          * via standard control-ports.
892          *
893          * When returning true Ardour will *not* sub-divide the process-cycle.
894          * Automation events that happen between cycle-start and cycle-end will be
895          * ignored (ctrl values are interpolated to cycle-start).
896          * NB. Atom Sequences are still sample accurate.
897          *
898          * Note: This does not guarantee a fixed block-size.
899          * e.g The process cycle may be split when looping, also
900          * the period-size may change any time: see set_block_size()
901          */
902         if (get_info()->n_inputs.n_midi() > 0) {
903                 /* we don't yet implement midi buffer offsets (for split cycles).
904                  * Also connect_and_run() also uses _session.transport_frame() directly
905                  * (for BBT) which is not offset for plugin cycle split.
906                  */
907                 return true;
908         }
909         return _no_sample_accurate_ctrl;
910 }
911
912 LV2Plugin::~LV2Plugin ()
913 {
914         DEBUG_TRACE(DEBUG::LV2, string_compose("%1 destroy\n", name()));
915
916         deactivate();
917         cleanup();
918
919 #ifdef LV2_EXTENDED
920         if (has_midnam ()) {
921                 std::stringstream ss;
922                 ss << (void*)this;
923                 ss << unique_id();
924                 MIDI::Name::MidiPatchManager::instance().remove_custom_midnam (ss.str());
925         }
926 #endif
927
928         lilv_instance_free(_impl->instance);
929         lilv_state_free(_impl->state);
930         lilv_node_free(_impl->name);
931         lilv_node_free(_impl->author);
932 #ifdef HAVE_LV2_1_2_0
933         free(_impl->options);
934 #endif
935 #ifdef LV2_EXTENDED
936         free(_impl->queue_draw);
937         free(_impl->midnam);
938         free(_impl->bankpatch);
939 #endif
940
941         free(_features);
942         free(_log_feature.data);
943         free(_make_path_feature.data);
944         free(_work_schedule_feature.data);
945
946         delete _to_ui;
947         delete _from_ui;
948         delete _worker;
949         delete _state_worker;
950
951         if (_atom_ev_buffers) {
952                 LV2_Evbuf**  b = _atom_ev_buffers;
953                 while (*b) {
954                         free(*b);
955                         b++;
956                 }
957                 free(_atom_ev_buffers);
958         }
959
960         delete [] _control_data;
961         delete [] _shadow_data;
962         delete [] _defaults;
963         delete [] _ev_buffers;
964         delete _impl;
965 }
966
967 bool
968 LV2Plugin::is_external_ui() const
969 {
970         if (!_impl->ui) {
971                 return false;
972         }
973         return lilv_ui_is_a(_impl->ui, _world.ui_external) || lilv_ui_is_a(_impl->ui, _world.ui_externalkx);
974 }
975
976 bool
977 LV2Plugin::is_external_kx() const
978 {
979         if (!_impl->ui) {
980                 return false;
981         }
982         return lilv_ui_is_a(_impl->ui, _world.ui_externalkx);
983 }
984
985 bool
986 LV2Plugin::ui_is_resizable () const
987 {
988         const LilvNode* s   = lilv_ui_get_uri(_impl->ui);
989         LilvNode*       p   = lilv_new_uri(_world.world, LV2_CORE__optionalFeature);
990         LilvNode*       fs  = lilv_new_uri(_world.world, LV2_UI__fixedSize);
991         LilvNode*       nrs = lilv_new_uri(_world.world, LV2_UI__noUserResize);
992
993         LilvNodes* fs_matches = lilv_world_find_nodes(_world.world, s, p, fs);
994         LilvNodes* nrs_matches = lilv_world_find_nodes(_world.world, s, p, nrs);
995
996         lilv_nodes_free(nrs_matches);
997         lilv_nodes_free(fs_matches);
998         lilv_node_free(nrs);
999         lilv_node_free(fs);
1000         lilv_node_free(p);
1001
1002         return !fs_matches && !nrs_matches;
1003 }
1004
1005 #ifdef LV2_EXTENDED
1006 bool
1007 LV2Plugin::has_inline_display () {
1008         return _display_interface ? true : false;
1009 }
1010
1011 bool
1012 LV2Plugin::inline_display_in_gui () {
1013         return _inline_display_in_gui;
1014 }
1015
1016 Plugin::Display_Image_Surface*
1017 LV2Plugin::render_inline_display (uint32_t w, uint32_t h) {
1018         if (_display_interface) {
1019                 /* Plugin::Display_Image_Surface is identical to
1020                  * LV2_Inline_Display_Image_Surface */
1021                 return (Plugin::Display_Image_Surface*) _display_interface->render ((void*)_impl->instance->lv2_handle, w, h);
1022         }
1023         return NULL;
1024 }
1025
1026 bool
1027 LV2Plugin::has_midnam () {
1028         return _midname_interface ? true : false;
1029 }
1030
1031 bool
1032 LV2Plugin::read_midnam () {
1033         bool rv = false;
1034         if (!_midname_interface || !_midnam_dirty) {
1035                 return rv;
1036         }
1037         char* midnam = _midname_interface->midnam ((void*)_impl->instance->lv2_handle);
1038         if (midnam) {
1039                 std::stringstream ss;
1040                 ss << (void*)this;
1041                 ss << unique_id();
1042                 rv = MIDI::Name::MidiPatchManager::instance().update_custom_midnam (ss.str(), midnam);
1043         }
1044 #ifndef NDEBUG
1045         if (rv) {
1046                 info << string_compose(_("LV2: update midnam for plugin '%1'"), name ()) << endmsg;
1047         } else {
1048                 warning << string_compose(_("LV2: Failed to parse midnam of plugin '%1'"), name ()) << endmsg;
1049         }
1050 #endif
1051         _midname_interface->free (midnam);
1052         if (rv) {
1053                 UpdatedMidnam ();
1054                 _midnam_dirty = false;
1055         }
1056         return rv;
1057 }
1058
1059 std::string
1060 LV2Plugin::midnam_model () {
1061         std::string rv;
1062         if (!_midname_interface) {
1063                 return rv;
1064         }
1065         char* model = _midname_interface->model ((void*)_impl->instance->lv2_handle);
1066         if (model) {
1067                 rv = model;
1068         }
1069         _midname_interface->free (model);
1070         return rv;
1071 }
1072 #endif
1073
1074 string
1075 LV2Plugin::unique_id() const
1076 {
1077         return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
1078 }
1079
1080 const char*
1081 LV2Plugin::uri() const
1082 {
1083         return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
1084 }
1085
1086 const char*
1087 LV2Plugin::label() const
1088 {
1089         return lilv_node_as_string(_impl->name);
1090 }
1091
1092 const char*
1093 LV2Plugin::name() const
1094 {
1095         return lilv_node_as_string(_impl->name);
1096 }
1097
1098 const char*
1099 LV2Plugin::maker() const
1100 {
1101         return _impl->author ? lilv_node_as_string (_impl->author) : "Unknown";
1102 }
1103
1104 uint32_t
1105 LV2Plugin::num_ports() const
1106 {
1107         return lilv_plugin_get_num_ports(_impl->plugin);
1108 }
1109
1110 uint32_t
1111 LV2Plugin::parameter_count() const
1112 {
1113         return lilv_plugin_get_num_ports(_impl->plugin);
1114 }
1115
1116 float
1117 LV2Plugin::default_value(uint32_t port)
1118 {
1119         return _defaults[port];
1120 }
1121
1122 const char*
1123 LV2Plugin::port_symbol(uint32_t index) const
1124 {
1125         const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, index);
1126         if (!port) {
1127                 error << name() << ": Invalid port index " << index << endmsg;
1128         }
1129
1130         const LilvNode* sym = lilv_port_get_symbol(_impl->plugin, port);
1131         return lilv_node_as_string(sym);
1132 }
1133
1134 uint32_t
1135 LV2Plugin::port_index (const char* symbol) const
1136 {
1137         const map<string, uint32_t>::const_iterator i = _port_indices.find(symbol);
1138         if (i != _port_indices.end()) {
1139                 return  i->second;
1140         } else {
1141                 warning << string_compose(_("LV2: Unknown port %1"), symbol) << endmsg;
1142                 return (uint32_t)-1;
1143         }
1144 }
1145
1146 void
1147 LV2Plugin::set_parameter(uint32_t which, float val)
1148 {
1149         DEBUG_TRACE(DEBUG::LV2, string_compose(
1150                             "%1 set parameter %2 to %3\n", name(), which, val));
1151
1152         if (which < lilv_plugin_get_num_ports(_impl->plugin)) {
1153                 if (get_parameter (which) == val) {
1154                         return;
1155                 }
1156
1157                 _shadow_data[which] = val;
1158         } else {
1159                 warning << string_compose(
1160                     _("Illegal parameter number used with plugin \"%1\". "
1161                       "This is a bug in either %2 or the LV2 plugin <%3>"),
1162                     name(), PROGRAM_NAME, unique_id()) << endmsg;
1163         }
1164
1165         Plugin::set_parameter(which, val);
1166 }
1167
1168 float
1169 LV2Plugin::get_parameter(uint32_t which) const
1170 {
1171         if (parameter_is_input(which)) {
1172                 return (float)_shadow_data[which];
1173         } else {
1174                 return (float)_control_data[which];
1175         }
1176         return 0.0f;
1177 }
1178
1179 std::string
1180 LV2Plugin::get_docs() const
1181 {
1182         LilvNodes* comments = lilv_plugin_get_value(_impl->plugin, _world.rdfs_comment);
1183         if (comments) {
1184                 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
1185                 lilv_nodes_free(comments);
1186                 return docs;
1187         }
1188
1189         return "";
1190 }
1191
1192 std::string
1193 LV2Plugin::get_parameter_docs(uint32_t which) const
1194 {
1195         LilvNodes* comments = lilv_port_get_value(
1196                 _impl->plugin,
1197                 lilv_plugin_get_port_by_index(_impl->plugin, which),
1198                 _world.rdfs_comment);
1199
1200         if (comments) {
1201                 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
1202                 lilv_nodes_free(comments);
1203                 return docs;
1204         }
1205
1206         return "";
1207 }
1208
1209 bool
1210 LV2Plugin::get_layout (uint32_t which, UILayoutHint& h) const
1211 {
1212         /// TODO lookup port-properties
1213         if (unique_id () != "urn:ardour:a-eq") {
1214                 return false;
1215         }
1216         h.knob = true;
1217         switch (which) {
1218                 case  0: h.x0 = 0; h.x1 = 1; h.y0 = 2; h.y1 = 3; break; // Frequency L
1219                 case  1: h.x0 = 0; h.x1 = 1; h.y0 = 0; h.y1 = 1; break; // Gain L
1220                 case 17: h.x0 = 0; h.x1 = 1; h.y0 = 5; h.y1 = 6; break; // enable L
1221
1222                 case  2: h.x0 = 1; h.x1 = 3; h.y0 = 2; h.y1 = 3; break; // Frequency 1
1223                 case  3: h.x0 = 1; h.x1 = 3; h.y0 = 0; h.y1 = 1; break; // Gain 1
1224                 case  4: h.x0 = 1; h.x1 = 3; h.y0 = 1; h.y1 = 2; break; // Bandwidth 1
1225                 case 18: h.x0 = 1; h.x1 = 4; h.y0 = 5; h.y1 = 6; break; // enable 1
1226
1227                 case  5: h.x0 = 4; h.x1 = 6; h.y0 = 2; h.y1 = 3; break; // Frequency 2
1228                 case  6: h.x0 = 4; h.x1 = 6; h.y0 = 0; h.y1 = 1; break; // Gain 2
1229                 case  7: h.x0 = 4; h.x1 = 6; h.y0 = 1; h.y1 = 2; break; // Bandwidth 2
1230                 case 19: h.x0 = 4; h.x1 = 7; h.y0 = 5; h.y1 = 6; break; // enable 2
1231
1232                 case  8: h.x0 = 7; h.x1 =  9; h.y0 = 2; h.y1 = 3; break; // Frequency 3
1233                 case  9: h.x0 = 7; h.x1 =  9; h.y0 = 0; h.y1 = 1; break; // Gain 3
1234                 case 10: h.x0 = 7; h.x1 =  9; h.y0 = 1; h.y1 = 2; break; // Bandwidth 3
1235                 case 20: h.x0 = 7; h.x1 = 10; h.y0 = 5; h.y1 = 6; break; // enable 3
1236
1237                 case 11: h.x0 = 10; h.x1 = 12; h.y0 = 2; h.y1 = 3; break; // Frequency 4
1238                 case 12: h.x0 = 10; h.x1 = 12; h.y0 = 0; h.y1 = 1; break; // Gain 4
1239                 case 13: h.x0 = 10; h.x1 = 12; h.y0 = 1; h.y1 = 2; break; // Bandwidth 4
1240                 case 21: h.x0 = 10; h.x1 = 13; h.y0 = 5; h.y1 = 6; break; // enable 4
1241
1242                 case 14: h.x0 = 13; h.x1 = 14; h.y0 = 2; h.y1 = 3; break; // Frequency H
1243                 case 15: h.x0 = 13; h.x1 = 14; h.y0 = 0; h.y1 = 1; break; // Gain H
1244                 case 22: h.x0 = 13; h.x1 = 14; h.y0 = 5; h.y1 = 6; break; // enable H
1245
1246                 case 16: h.x0 = 14; h.x1 = 15; h.y0 = 1; h.y1 = 3; break; // Master Gain
1247                 case 23: h.x0 = 14; h.x1 = 15; h.y0 = 5; h.y1 = 6; break; // Master Enable
1248                 default:
1249                         return false;
1250         }
1251         return true;
1252 }
1253
1254 uint32_t
1255 LV2Plugin::nth_parameter(uint32_t n, bool& ok) const
1256 {
1257         ok = false;
1258         for (uint32_t c = 0, x = 0; x < lilv_plugin_get_num_ports(_impl->plugin); ++x) {
1259                 if (parameter_is_control(x)) {
1260                         if (c++ == n) {
1261                                 ok = true;
1262                                 return x;
1263                         }
1264                 }
1265         }
1266
1267         return 0;
1268 }
1269
1270 const void*
1271 LV2Plugin::extension_data(const char* uri) const
1272 {
1273         return lilv_instance_get_extension_data(_impl->instance, uri);
1274 }
1275
1276 const void*
1277 LV2Plugin::c_plugin()
1278 {
1279         return _impl->plugin;
1280 }
1281
1282 const void*
1283 LV2Plugin::c_ui()
1284 {
1285         return (const void*)_impl->ui;
1286 }
1287
1288 const void*
1289 LV2Plugin::c_ui_type()
1290 {
1291         return (const void*)_impl->ui_type;
1292 }
1293
1294 /** Directory for all plugin state. */
1295 const std::string
1296 LV2Plugin::plugin_dir() const
1297 {
1298         if (!_plugin_state_dir.empty ()){
1299                 return Glib::build_filename(_plugin_state_dir, _insert_id.to_s());
1300         } else {
1301                 return Glib::build_filename(_session.plugins_dir(), _insert_id.to_s());
1302         }
1303 }
1304
1305 /** Directory for files created by the plugin (except during save). */
1306 const std::string
1307 LV2Plugin::scratch_dir() const
1308 {
1309         return Glib::build_filename(plugin_dir(), "scratch");
1310 }
1311
1312 /** Directory for snapshots of files in the scratch directory. */
1313 const std::string
1314 LV2Plugin::file_dir() const
1315 {
1316         return Glib::build_filename(plugin_dir(), "files");
1317 }
1318
1319 /** Directory to save state snapshot version @c num into. */
1320 const std::string
1321 LV2Plugin::state_dir(unsigned num) const
1322 {
1323         return Glib::build_filename(plugin_dir(), string("state") + PBD::to_string (num));
1324 }
1325
1326 /** Implementation of state:makePath for files created at instantiation time.
1327  * Note this is not used for files created at save time (Lilv deals with that).
1328  */
1329 char*
1330 LV2Plugin::lv2_state_make_path(LV2_State_Make_Path_Handle handle,
1331                                const char*                path)
1332 {
1333         LV2Plugin* me = (LV2Plugin*)handle;
1334         if (me->_insert_id == PBD::ID("0")) {
1335                 warning << string_compose(
1336                         "File path \"%1\" requested but LV2 %2 has no insert ID",
1337                         path, me->name()) << endmsg;
1338                 return g_strdup(path);
1339         }
1340
1341         const std::string abs_path = Glib::build_filename(me->scratch_dir(), path);
1342         const std::string dirname  = Glib::path_get_dirname(abs_path);
1343         g_mkdir_with_parents(dirname.c_str(), 0744);
1344
1345         DEBUG_TRACE(DEBUG::LV2, string_compose("new file path %1 => %2\n",
1346                                                path, abs_path));
1347
1348         return g_strndup(abs_path.c_str(), abs_path.length());
1349 }
1350
1351 void
1352 LV2Plugin::add_state(XMLNode* root) const
1353 {
1354         assert(_insert_id != PBD::ID("0"));
1355
1356         XMLNode*    child;
1357         LocaleGuard lg;
1358
1359         for (uint32_t i = 0; i < parameter_count(); ++i) {
1360                 if (parameter_is_input(i) && parameter_is_control(i)) {
1361                         child = new XMLNode("Port");
1362                         child->set_property("symbol", port_symbol(i));
1363                         child->set_property("value", _shadow_data[i]);
1364                         root->add_child_nocopy(*child);
1365                 }
1366         }
1367
1368         if (!_plugin_state_dir.empty()) {
1369                 root->set_property("template-dir", _plugin_state_dir);
1370         }
1371
1372         if (_has_state_interface) {
1373                 // Provisionally increment state version and create directory
1374                 const std::string new_dir = state_dir(++_state_version);
1375                 // and keep track of it (for templates & archive)
1376                 unsigned int saved_state = _state_version;;
1377                 g_mkdir_with_parents(new_dir.c_str(), 0744);
1378
1379                 LilvState* state = lilv_state_new_from_instance(
1380                         _impl->plugin,
1381                         _impl->instance,
1382                         _uri_map.urid_map(),
1383                         scratch_dir().c_str(),
1384                         file_dir().c_str(),
1385                         _session.externals_dir().c_str(),
1386                         new_dir.c_str(),
1387                         NULL,
1388                         const_cast<LV2Plugin*>(this),
1389                         0,
1390                         NULL);
1391
1392                 if (!_plugin_state_dir.empty() || force_state_save
1393                     || !_impl->state
1394                     || !lilv_state_equals(state, _impl->state)) {
1395                         lilv_state_save(_world.world,
1396                                         _uri_map.urid_map(),
1397                                         _uri_map.urid_unmap(),
1398                                         state,
1399                                         NULL,
1400                                         new_dir.c_str(),
1401                                         "state.ttl");
1402
1403                         if (force_state_save) {
1404                                 // archive or save-as
1405                                 lilv_state_free(state);
1406                                 --_state_version;
1407                         }
1408                         else if (_plugin_state_dir.empty()) {
1409                                 // normal session save
1410                                 lilv_state_free(_impl->state);
1411                                 _impl->state = state;
1412                         } else {
1413                                 // template save (dedicated state-dir)
1414                                 lilv_state_free(state);
1415                                 --_state_version;
1416                         }
1417                 } else {
1418                         // State is identical, decrement version and nuke directory
1419                         lilv_state_free(state);
1420                         PBD::remove_directory(new_dir);
1421                         --_state_version;
1422                         saved_state = _state_version;
1423                 }
1424
1425                 root->set_property("state-dir", string("state") + PBD::to_string (saved_state));
1426         }
1427 }
1428
1429 // TODO: Once we can rely on lilv 0.16.0, lilv_world_get can replace this
1430 static LilvNode*
1431 get_value(LilvWorld* world, const LilvNode* subject, const LilvNode* predicate)
1432 {
1433         LilvNodes* vs = lilv_world_find_nodes(world, subject, predicate, NULL);
1434         if (vs) {
1435                 LilvNode* node = lilv_node_duplicate(lilv_nodes_get_first(vs));
1436                 lilv_nodes_free(vs);
1437                 return node;
1438         }
1439         return NULL;
1440 }
1441
1442 void
1443 LV2Plugin::find_presets()
1444 {
1445         LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
1446         LilvNode* pset_Preset   = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
1447         LilvNode* rdfs_label    = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
1448
1449         LilvNodes* presets = lilv_plugin_get_related(_impl->plugin, pset_Preset);
1450         LILV_FOREACH(nodes, i, presets) {
1451                 const LilvNode* preset = lilv_nodes_get(presets, i);
1452                 lilv_world_load_resource(_world.world, preset);
1453                 LilvNode* name = get_value(_world.world, preset, rdfs_label);
1454                 bool userpreset = true; // TODO
1455                 if (name) {
1456                         _presets.insert(std::make_pair(lilv_node_as_string(preset),
1457                                                        Plugin::PresetRecord(
1458                                                                lilv_node_as_string(preset),
1459                                                                lilv_node_as_string(name),
1460                                                                userpreset)));
1461                         lilv_node_free(name);
1462                 } else {
1463                         warning << string_compose(
1464                             _("Plugin \"%1\" preset \"%2\" is missing a label\n"),
1465                             lilv_node_as_string(lilv_plugin_get_uri(_impl->plugin)),
1466                             lilv_node_as_string(preset)) << endmsg;
1467                 }
1468         }
1469         lilv_nodes_free(presets);
1470
1471         lilv_node_free(rdfs_label);
1472         lilv_node_free(pset_Preset);
1473         lilv_node_free(lv2_appliesTo);
1474 }
1475
1476 static void
1477 set_port_value(const char* port_symbol,
1478                void*       user_data,
1479                const void* value,
1480                uint32_t    /*size*/,
1481                uint32_t    type)
1482 {
1483         LV2Plugin* self = (LV2Plugin*)user_data;
1484         if (type != 0 && type != URIMap::instance().urids.atom_Float) {
1485                 return;  // TODO: Support non-float ports
1486         }
1487
1488         const uint32_t port_index = self->port_index(port_symbol);
1489         if (port_index != (uint32_t)-1) {
1490                 self->set_parameter(port_index, *(const float*)value);
1491                 self->PresetPortSetValue (port_index, *(const float*)value); /* EMIT SIGNAL */
1492         }
1493 }
1494
1495 bool
1496 LV2Plugin::load_preset(PresetRecord r)
1497 {
1498         LilvWorld* world = _world.world;
1499         LilvNode*  pset  = lilv_new_uri(world, r.uri.c_str());
1500         LilvState* state = lilv_state_new_from_world(world, _uri_map.urid_map(), pset);
1501
1502         const LV2_Feature*  state_features[2]   = { NULL, NULL };
1503         LV2_Worker_Schedule schedule            = { _state_worker, work_schedule };
1504         const LV2_Feature   state_sched_feature = { LV2_WORKER__schedule, &schedule };
1505         if (_state_worker) {
1506                 state_features[0] = &state_sched_feature;
1507         }
1508
1509         if (state) {
1510                 lilv_state_restore(state, _impl->instance, set_port_value, this, 0, state_features);
1511                 lilv_state_free(state);
1512                 Plugin::load_preset(r);
1513         }
1514
1515         lilv_node_free(pset);
1516         return state;
1517 }
1518
1519 const void*
1520 ARDOUR::lv2plugin_get_port_value(const char* port_symbol,
1521                                  void*       user_data,
1522                                  uint32_t*   size,
1523                                  uint32_t*   type)
1524 {
1525         LV2Plugin *plugin = (LV2Plugin *) user_data;
1526
1527         uint32_t index = plugin->port_index(port_symbol);
1528         if (index != (uint32_t) -1) {
1529                 if (plugin->parameter_is_input(index) && plugin->parameter_is_control(index)) {
1530                         float *value;
1531                         *size = sizeof(float);
1532                         *type = plugin->_uri_map.uri_to_id(LV2_ATOM__Float);
1533                         value = &plugin->_shadow_data[index];
1534
1535                         return value;
1536                 }
1537         }
1538
1539         *size = *type = 0;
1540         return NULL;
1541 }
1542
1543
1544 std::string
1545 LV2Plugin::do_save_preset(string name)
1546 {
1547         LilvNode*    plug_name = lilv_plugin_get_name(_impl->plugin);
1548         const string prefix    = legalize_for_uri(lilv_node_as_string(plug_name));
1549         const string base_name = legalize_for_uri(name);
1550         const string file_name = base_name + ".ttl";
1551 #ifdef PLATFORM_WINDOWS
1552         /* http://lv2plug.in/pages/filesystem-hierarchy-standard.html */
1553         std::string appdata = PBD::get_win_special_folder_path (CSIDL_APPDATA);
1554         if (appdata.empty ()) {
1555                 // TODO consider a fallback location
1556                 return "";
1557         }
1558         const string bundle = Glib::build_filename (
1559                         appdata, "LV2", prefix + "_" + base_name + ".lv2");
1560 #else
1561         /* while macOS/OSX user-specific path is
1562          *
1563          *   $HOME/Library/Audio/Plug-Ins/LV2/
1564          *
1565          * liblilv's LV2 search path on all unices does include ~/.lv2/
1566          * Ardour has been saving lv2 presets to ~/.lv2 for along time,
1567          * so just keep them there.
1568          */
1569         const string bundle    = Glib::build_filename(
1570                 Glib::get_home_dir(),
1571                 Glib::build_filename(".lv2", prefix + "_" + base_name + ".lv2"));
1572 #endif
1573
1574 #ifdef HAVE_LILV_0_21_3
1575         /* delete reference to old preset (if any) */
1576         const PresetRecord* r = preset_by_label(name);
1577         if (r) {
1578                 LilvNode*  pset  = lilv_new_uri (_world.world, r->uri.c_str());
1579                 if (pset) {
1580                         lilv_world_unload_resource (_world.world, pset);
1581                         lilv_node_free(pset);
1582                 }
1583         }
1584 #endif
1585
1586         LilvState* state = lilv_state_new_from_instance(
1587                 _impl->plugin,
1588                 _impl->instance,
1589                 _uri_map.urid_map(),
1590                 scratch_dir().c_str(),                   // file_dir
1591                 bundle.c_str(),                          // copy_dir
1592                 bundle.c_str(),                          // link_dir
1593                 bundle.c_str(),                          // save_dir
1594                 lv2plugin_get_port_value,                // get_value
1595                 (void*)this,                             // user_data
1596                 LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE,  // flags
1597                 _features                                // features
1598         );
1599
1600         lilv_state_set_label(state, name.c_str());
1601         lilv_state_save(
1602                 _world.world,           // world
1603                 _uri_map.urid_map(),    // map
1604                 _uri_map.urid_unmap(),  // unmap
1605                 state,                  // state
1606                 NULL,                   // uri (NULL = use file URI)
1607                 bundle.c_str(),         // dir
1608                 file_name.c_str()       // filename
1609         );
1610
1611         lilv_state_free(state);
1612
1613         std::string uri = Glib::filename_to_uri(Glib::build_filename(bundle, file_name));
1614         LilvNode *node_bundle = lilv_new_uri(_world.world, Glib::filename_to_uri(Glib::build_filename(bundle, "/")).c_str());
1615         LilvNode *node_preset = lilv_new_uri(_world.world, uri.c_str());
1616 #ifdef HAVE_LILV_0_21_3
1617         lilv_world_unload_resource(_world.world, node_preset);
1618         lilv_world_unload_bundle(_world.world, node_bundle);
1619 #endif
1620         lilv_world_load_bundle(_world.world, node_bundle);
1621         lilv_world_load_resource(_world.world, node_preset);
1622         lilv_node_free(node_bundle);
1623         lilv_node_free(node_preset);
1624         lilv_node_free(plug_name);
1625         return uri;
1626 }
1627
1628 void
1629 LV2Plugin::do_remove_preset(string name)
1630 {
1631 #ifdef HAVE_LILV_0_21_3
1632         /* Look up preset record by label (FIXME: ick, label as ID) */
1633         const PresetRecord* r = preset_by_label(name);
1634         if (!r) {
1635                 return;
1636         }
1637
1638         /* Load a LilvState for the preset. */
1639         LilvWorld* world = _world.world;
1640         LilvNode*  pset  = lilv_new_uri(world, r->uri.c_str());
1641         LilvState* state = lilv_state_new_from_world(world, _uri_map.urid_map(), pset);
1642         if (!state) {
1643                 lilv_node_free(pset);
1644                 return;
1645         }
1646
1647         /* Unload preset from world. */
1648         lilv_world_unload_resource(world, pset);
1649
1650         /* Delete it from the file system.  This will remove the preset file and the entry
1651            from the manifest.  If this results in an empty manifest (i.e. the
1652            preset is the only thing in the bundle), then the bundle is removed. */
1653         lilv_state_delete(world, state);
1654
1655         lilv_state_free(state);
1656         lilv_node_free(pset);
1657 #endif
1658         /* Without lilv_state_delete(), we could delete the preset file, but this
1659            would leave a broken bundle/manifest around, so the preset would still
1660            be visible, but broken.  Naively deleting a bundle is too dangerous, so
1661            we simply do not support preset deletion with older Lilv */
1662 }
1663
1664 bool
1665 LV2Plugin::has_editor() const
1666 {
1667         return _impl->ui != NULL;
1668 }
1669
1670 bool
1671 LV2Plugin::has_message_output() const
1672 {
1673         for (uint32_t i = 0; i < num_ports(); ++i) {
1674                 if ((_port_flags[i] & PORT_SEQUENCE) &&
1675                     (_port_flags[i] & PORT_OUTPUT)) {
1676                         return true;
1677                 }
1678         }
1679         return false;
1680 }
1681
1682 bool
1683 LV2Plugin::write_to(RingBuffer<uint8_t>* dest,
1684                     uint32_t             index,
1685                     uint32_t             protocol,
1686                     uint32_t             size,
1687                     const uint8_t*       body)
1688 {
1689         const uint32_t  buf_size = sizeof(UIMessage) + size;
1690         vector<uint8_t> buf(buf_size);
1691
1692         UIMessage* msg = (UIMessage*)&buf[0];
1693         msg->index    = index;
1694         msg->protocol = protocol;
1695         msg->size     = size;
1696         memcpy(msg + 1, body, size);
1697
1698         return (dest->write(&buf[0], buf_size) == buf_size);
1699 }
1700
1701 bool
1702 LV2Plugin::write_from_ui(uint32_t       index,
1703                          uint32_t       protocol,
1704                          uint32_t       size,
1705                          const uint8_t* body)
1706 {
1707         if (!_from_ui) {
1708                 size_t rbs = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
1709                 /* buffer data communication from plugin UI to plugin instance.
1710                  * this buffer needs to potentially hold
1711                  *   (port's minimumSize) * (audio-periods) / (UI-periods)
1712                  * bytes.
1713                  *
1714                  *  e.g 48kSPS / 128fpp -> audio-periods = 375 Hz
1715                  *  ui-periods = 25 Hz (SuperRapidScreenUpdate)
1716                  *  default minimumSize = 32K (see LV2Plugin::allocate_atom_event_buffers()
1717                  *
1718                  * it is NOT safe to overflow (msg.size will be misinterpreted)
1719                  */
1720                 uint32_t bufsiz = 32768;
1721                 if (_atom_ev_buffers && _atom_ev_buffers[0]) {
1722                         bufsiz =  lv2_evbuf_get_capacity(_atom_ev_buffers[0]);
1723                 }
1724                 int fact = ceilf(_session.frame_rate () / 3000.f);
1725                 rbs = max((size_t) bufsiz * std::max (8, fact), rbs);
1726                 _from_ui = new RingBuffer<uint8_t>(rbs);
1727         }
1728
1729         if (!write_to(_from_ui, index, protocol, size, body)) {
1730                 error << "Error writing from UI to plugin" << endmsg;
1731                 return false;
1732         }
1733         return true;
1734 }
1735
1736 bool
1737 LV2Plugin::write_to_ui(uint32_t       index,
1738                        uint32_t       protocol,
1739                        uint32_t       size,
1740                        const uint8_t* body)
1741 {
1742         if (!write_to(_to_ui, index, protocol, size, body)) {
1743                 error << "Error writing from plugin to UI" << endmsg;
1744                 return false;
1745         }
1746         return true;
1747 }
1748
1749 static void
1750 forge_variant(LV2_Atom_Forge* forge, const Variant& value)
1751 {
1752         switch (value.type()) {
1753         case Variant::NOTHING:
1754                 break;
1755         case Variant::BEATS:
1756                 // No atom type for this, just forge a double
1757                 lv2_atom_forge_double(forge, value.get_beats().to_double());
1758                 break;
1759         case Variant::BOOL:
1760                 lv2_atom_forge_bool(forge, value.get_bool());
1761                 break;
1762         case Variant::DOUBLE:
1763                 lv2_atom_forge_double(forge, value.get_double());
1764                 break;
1765         case Variant::FLOAT:
1766                 lv2_atom_forge_float(forge, value.get_float());
1767                 break;
1768         case Variant::INT:
1769                 lv2_atom_forge_int(forge, value.get_int());
1770                 break;
1771         case Variant::LONG:
1772                 lv2_atom_forge_long(forge, value.get_long());
1773                 break;
1774         case Variant::PATH:
1775                 lv2_atom_forge_path(
1776                         forge, value.get_path().c_str(), value.get_path().size());
1777                 break;
1778         case Variant::STRING:
1779                 lv2_atom_forge_string(
1780                         forge, value.get_string().c_str(), value.get_string().size());
1781                 break;
1782         case Variant::URI:
1783                 lv2_atom_forge_uri(
1784                         forge, value.get_uri().c_str(), value.get_uri().size());
1785                 break;
1786         }
1787 }
1788
1789 /** Get a variant type from a URI, return false iff no match found. */
1790 static bool
1791 uri_to_variant_type(const std::string& uri, Variant::Type& type)
1792 {
1793         if (uri == LV2_ATOM__Bool) {
1794                 type = Variant::BOOL;
1795         } else if (uri == LV2_ATOM__Double) {
1796                 type = Variant::DOUBLE;
1797         } else if (uri == LV2_ATOM__Float) {
1798                 type = Variant::FLOAT;
1799         } else if (uri == LV2_ATOM__Int) {
1800                 type = Variant::INT;
1801         } else if (uri == LV2_ATOM__Long) {
1802                 type = Variant::LONG;
1803         } else if (uri == LV2_ATOM__Path) {
1804                 type = Variant::PATH;
1805         } else if (uri == LV2_ATOM__String) {
1806                 type = Variant::STRING;
1807         } else if (uri == LV2_ATOM__URI) {
1808                 type = Variant::URI;
1809         } else {
1810                 return false;
1811         }
1812         return true;
1813 }
1814
1815 void
1816 LV2Plugin::set_property(uint32_t key, const Variant& value)
1817 {
1818         if (_patch_port_in_index == (uint32_t)-1) {
1819                 error << "LV2: set_property called with unset patch_port_in_index" << endmsg;
1820                 return;
1821         } else if (value.type() == Variant::NOTHING) {
1822                 error << "LV2: set_property called with void value" << endmsg;
1823                 return;
1824         }
1825
1826         // Set up forge to write to temporary buffer on the stack
1827         LV2_Atom_Forge*      forge = &_impl->ui_forge;
1828         LV2_Atom_Forge_Frame frame;
1829         uint8_t              buf[PATH_MAX];  // Ought to be enough for anyone...
1830
1831         lv2_atom_forge_set_buffer(forge, buf, sizeof(buf));
1832
1833         // Serialize patch:Set message to set property
1834 #ifdef HAVE_LV2_1_10_0
1835         lv2_atom_forge_object(forge, &frame, 0, _uri_map.urids.patch_Set);
1836         lv2_atom_forge_key(forge, _uri_map.urids.patch_property);
1837         lv2_atom_forge_urid(forge, key);
1838         lv2_atom_forge_key(forge, _uri_map.urids.patch_value);
1839 #else
1840         lv2_atom_forge_blank(forge, &frame, 0, _uri_map.urids.patch_Set);
1841         lv2_atom_forge_property_head(forge, _uri_map.urids.patch_property, 0);
1842         lv2_atom_forge_urid(forge, key);
1843         lv2_atom_forge_property_head(forge, _uri_map.urids.patch_value, 0);
1844 #endif
1845
1846         forge_variant(forge, value);
1847
1848         // Write message to UI=>Plugin ring
1849         const LV2_Atom* const atom = (const LV2_Atom*)buf;
1850         write_from_ui(_patch_port_in_index,
1851                       _uri_map.urids.atom_eventTransfer,
1852                       lv2_atom_total_size(atom),
1853                       (const uint8_t*)atom);
1854 }
1855
1856 const ParameterDescriptor&
1857 LV2Plugin::get_property_descriptor(uint32_t id) const
1858 {
1859         PropertyDescriptors::const_iterator p = _property_descriptors.find(id);
1860         if (p != _property_descriptors.end()) {
1861                 return p->second;
1862         }
1863         return Plugin::get_property_descriptor(id);
1864 }
1865
1866 static void
1867 load_parameter_descriptor_units(LilvWorld* lworld, ParameterDescriptor& desc, const LilvNodes* units)
1868 {
1869         if (lilv_nodes_contains(units, _world.units_midiNote)) {
1870                 desc.unit = ParameterDescriptor::MIDI_NOTE;
1871         } else if (lilv_nodes_contains(units, _world.units_db)) {
1872                 desc.unit = ParameterDescriptor::DB;
1873         } else if (lilv_nodes_contains(units, _world.units_hz)) {
1874                 desc.unit = ParameterDescriptor::HZ;
1875         }
1876         if (lilv_nodes_size(units) > 0) {
1877                 const LilvNode* unit = lilv_nodes_get_first(units);
1878                 LilvNode* render = get_value(lworld, unit, _world.units_render);
1879                 if (render) {
1880                         desc.print_fmt = lilv_node_as_string(render);
1881                         /* override lilv's default "%f" format */
1882                         if (desc.integer_step) {
1883                                 replace_all (desc.print_fmt, "%f", "%.0f");
1884                         } else if (desc.upper - desc.lower >= 1000) {
1885                                 replace_all (desc.print_fmt, "%f", "%.1f");
1886                         } else if (desc.upper - desc.lower >= 100) {
1887                                 replace_all (desc.print_fmt, "%f", "%.2f");
1888                         } else {
1889                                 replace_all (desc.print_fmt, "%f", "%.3f");
1890                         }
1891                         lilv_node_free(render);
1892                 }
1893         }
1894 }
1895
1896 static void
1897 load_parameter_descriptor(LV2World&            world,
1898                           ParameterDescriptor& desc,
1899                           Variant::Type        datatype,
1900                           const LilvNode*      subject)
1901 {
1902         LilvWorld* lworld  = _world.world;
1903         LilvNode*  label   = get_value(lworld, subject, _world.rdfs_label);
1904         LilvNode*  def     = get_value(lworld, subject, _world.lv2_default);
1905         LilvNode*  minimum = get_value(lworld, subject, _world.lv2_minimum);
1906         LilvNode*  maximum = get_value(lworld, subject, _world.lv2_maximum);
1907         LilvNodes* units   = lilv_world_find_nodes(lworld, subject, _world.units_unit, NULL);
1908         if (label) {
1909                 desc.label = lilv_node_as_string(label);
1910         }
1911         if (def) {
1912                 if (lilv_node_is_float(def)) {
1913                         desc.normal = lilv_node_as_float(def);
1914                 } else if (lilv_node_is_int(def)) {
1915                         desc.normal = lilv_node_as_int(def);
1916                 }
1917         }
1918         if (minimum) {
1919                 if (lilv_node_is_float(minimum)) {
1920                         desc.lower = lilv_node_as_float(minimum);
1921                 } else if (lilv_node_is_int(minimum)) {
1922                         desc.lower = lilv_node_as_int(minimum);
1923                 }
1924         }
1925         if (maximum) {
1926                 if (lilv_node_is_float(maximum)) {
1927                         desc.upper = lilv_node_as_float(maximum);
1928                 } else if (lilv_node_is_int(maximum)) {
1929                         desc.upper = lilv_node_as_int(maximum);
1930                 }
1931         }
1932         load_parameter_descriptor_units(lworld, desc, units);
1933         desc.datatype      = datatype;
1934         desc.toggled      |= datatype == Variant::BOOL;
1935         desc.integer_step |= datatype == Variant::INT || datatype == Variant::LONG;
1936         desc.update_steps();
1937
1938         lilv_nodes_free(units);
1939         lilv_node_free(label);
1940         lilv_node_free(def);
1941         lilv_node_free(minimum);
1942         lilv_node_free(maximum);
1943 }
1944
1945 void
1946 LV2Plugin::load_supported_properties(PropertyDescriptors& descs)
1947 {
1948         LilvWorld*       lworld     = _world.world;
1949         const LilvNode*  subject    = lilv_plugin_get_uri(_impl->plugin);
1950         LilvNodes*       properties = lilv_world_find_nodes(
1951                 lworld, subject, _world.patch_writable, NULL);
1952         LILV_FOREACH(nodes, p, properties) {
1953                 // Get label and range
1954                 const LilvNode* prop  = lilv_nodes_get(properties, p);
1955                 LilvNode*       range = get_value(lworld, prop, _world.rdfs_range);
1956                 if (!range) {
1957                         warning << string_compose(_("LV2: property <%1> has no range datatype, ignoring"),
1958                                                   lilv_node_as_uri(prop)) << endmsg;
1959                         continue;
1960                 }
1961
1962                 // Convert range to variant type (TODO: support for multiple range types)
1963                 Variant::Type datatype;
1964                 if (!uri_to_variant_type(lilv_node_as_uri(range), datatype)) {
1965                         error << string_compose(_("LV2: property <%1> has unsupported datatype <%1>"),
1966                                                 lilv_node_as_uri(prop), lilv_node_as_uri(range)) << endmsg;
1967                         continue;
1968                 }
1969
1970                 // Add description to result
1971                 ParameterDescriptor desc;
1972                 desc.key      = _uri_map.uri_to_id(lilv_node_as_uri(prop));
1973                 desc.datatype = datatype;
1974                 load_parameter_descriptor(_world, desc, datatype, prop);
1975                 descs.insert(std::make_pair(desc.key, desc));
1976
1977                 lilv_node_free(range);
1978         }
1979         lilv_nodes_free(properties);
1980 }
1981
1982 void
1983 LV2Plugin::announce_property_values()
1984 {
1985         if (_patch_port_in_index == (uint32_t)-1) {
1986                 return;
1987         }
1988
1989         // Set up forge to write to temporary buffer on the stack
1990         LV2_Atom_Forge*      forge = &_impl->ui_forge;
1991         LV2_Atom_Forge_Frame frame;
1992         uint8_t              buf[PATH_MAX];  // Ought to be enough for anyone...
1993
1994         lv2_atom_forge_set_buffer(forge, buf, sizeof(buf));
1995
1996         // Serialize patch:Get message with no subject (implicitly plugin instance)
1997 #ifdef HAVE_LV2_1_10_0
1998         lv2_atom_forge_object(forge, &frame, 0, _uri_map.urids.patch_Get);
1999 #else
2000         lv2_atom_forge_blank(forge, &frame, 0, _uri_map.urids.patch_Get);
2001 #endif
2002
2003         // Write message to UI=>Plugin ring
2004         const LV2_Atom* const atom = (const LV2_Atom*)buf;
2005         write_from_ui(_patch_port_in_index,
2006                       _uri_map.urids.atom_eventTransfer,
2007                       lv2_atom_total_size(atom),
2008                       (const uint8_t*)atom);
2009 }
2010
2011 void
2012 LV2Plugin::enable_ui_emission()
2013 {
2014         if (!_to_ui) {
2015                 /* see note in LV2Plugin::write_from_ui() */
2016                 uint32_t bufsiz = 32768;
2017                 if (_atom_ev_buffers && _atom_ev_buffers[0]) {
2018                         bufsiz =  lv2_evbuf_get_capacity(_atom_ev_buffers[0]);
2019                 }
2020                 size_t rbs = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
2021                 rbs = max((size_t) bufsiz * 8, rbs);
2022                 _to_ui = new RingBuffer<uint8_t>(rbs);
2023         }
2024 }
2025
2026 void
2027 LV2Plugin::emit_to_ui(void* controller, UIMessageSink sink)
2028 {
2029         if (!_to_ui) {
2030                 return;
2031         }
2032
2033         uint32_t read_space = _to_ui->read_space();
2034         while (read_space > sizeof(UIMessage)) {
2035                 UIMessage msg;
2036                 if (_to_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
2037                         error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
2038                         break;
2039                 }
2040                 vector<uint8_t> body(msg.size);
2041                 if (_to_ui->read(&body[0], msg.size) != msg.size) {
2042                         error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
2043                         break;
2044                 }
2045
2046                 sink(controller, msg.index, msg.size, msg.protocol, &body[0]);
2047
2048                 read_space -= sizeof(msg) + msg.size;
2049         }
2050 }
2051
2052 int
2053 LV2Plugin::work(Worker& worker, uint32_t size, const void* data)
2054 {
2055         Glib::Threads::Mutex::Lock lm(_work_mutex);
2056         return _impl->work_iface->work(
2057                 _impl->instance->lv2_handle, work_respond, &worker, size, data);
2058 }
2059
2060 int
2061 LV2Plugin::work_response(uint32_t size, const void* data)
2062 {
2063         return _impl->work_iface->work_response(
2064                 _impl->instance->lv2_handle, size, data);
2065 }
2066
2067 void
2068 LV2Plugin::set_insert_id(PBD::ID id)
2069 {
2070         if (_insert_id == "0") {
2071                 _insert_id = id;
2072         } else if (_insert_id != id) {
2073                 lilv_state_free(_impl->state);
2074                 _impl->state = NULL;
2075                 _insert_id   = id;
2076         }
2077 }
2078
2079 void
2080 LV2Plugin::set_state_dir (const std::string& d)
2081 {
2082         _plugin_state_dir = d;
2083 }
2084
2085 int
2086 LV2Plugin::set_state(const XMLNode& node, int version)
2087 {
2088         XMLNodeList          nodes;
2089         XMLNodeConstIterator iter;
2090         XMLNode*             child;
2091         LocaleGuard          lg;
2092
2093         if (node.name() != state_node_name()) {
2094                 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
2095                 return -1;
2096         }
2097
2098 #ifndef NO_PLUGIN_STATE
2099
2100         if (version < 3000) {
2101                 nodes = node.children("port");
2102         } else {
2103                 nodes = node.children("Port");
2104         }
2105
2106         for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
2107
2108                 child = *iter;
2109
2110                 std::string sym;
2111                 if (!child->get_property("symbol", sym)) {
2112                         warning << _("LV2: port has no symbol, ignored") << endmsg;
2113                         continue;
2114                 }
2115
2116                 map<string, uint32_t>::iterator i = _port_indices.find(sym);
2117
2118                 uint32_t port_id;
2119
2120                 if (i != _port_indices.end()) {
2121                         port_id = i->second;
2122                 } else {
2123                         warning << _("LV2: port has unknown index, ignored") << endmsg;
2124                         continue;
2125                 }
2126
2127                 float val;
2128                 if (!child->get_property("value", val)) {
2129                         warning << _("LV2: port has no value, ignored") << endmsg;
2130                         continue;
2131                 }
2132
2133                 set_parameter(port_id, val);
2134         }
2135
2136         std::string template_dir;
2137         if (node.get_property("template-dir", template_dir)) {
2138                 set_state_dir (template_dir);
2139         }
2140
2141         _state_version = 0;
2142         std::string state_dir;
2143         if (node.get_property("state-dir", state_dir) != 0) {
2144                 if (sscanf(state_dir.c_str(), "state%u", &_state_version) != 1) {
2145                         error << string_compose(
2146                                 "LV2: failed to parse state version from \"%1\"",
2147                                 state_dir) << endmsg;
2148                 }
2149
2150                 std::string state_file = Glib::build_filename(
2151                         plugin_dir(),
2152                         Glib::build_filename(state_dir, "state.ttl"));
2153
2154                 LilvState* state = lilv_state_new_from_file(
2155                         _world.world, _uri_map.urid_map(), NULL, state_file.c_str());
2156
2157                 lilv_state_restore(state, _impl->instance, NULL, NULL, 0, NULL);
2158                 lilv_state_free(_impl->state);
2159                 _impl->state = state;
2160         }
2161
2162         if (!_plugin_state_dir.empty ()) {
2163                 // force save with session, next time (increment counter)
2164                 lilv_state_free (_impl->state);
2165                 _impl->state = NULL;
2166                 set_state_dir ("");
2167         }
2168
2169         latency_compute_run();
2170 #endif
2171
2172         return Plugin::set_state(node, version);
2173 }
2174
2175 int
2176 LV2Plugin::get_parameter_descriptor(uint32_t which, ParameterDescriptor& desc) const
2177 {
2178         const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, which);
2179         if (!port) {
2180                 error << string_compose("LV2: get descriptor of non-existent port %1", which)
2181                       << endmsg;
2182                 return 1;
2183         }
2184
2185         LilvNodes* portunits;
2186         LilvNode *def, *min, *max;
2187         lilv_port_get_range(_impl->plugin, port, &def, &min, &max);
2188         portunits = lilv_port_get_value(_impl->plugin, port, _world.units_unit);
2189
2190         LilvNode* steps   = lilv_port_get(_impl->plugin, port, _world.ext_rangeSteps);
2191
2192         // TODO: Once we can rely on lilv 0.18.0 being present,
2193         // load_parameter_descriptor() can be used for ports as well
2194         desc.integer_step = lilv_port_has_property(_impl->plugin, port, _world.lv2_integer);
2195         desc.toggled      = lilv_port_has_property(_impl->plugin, port, _world.lv2_toggled);
2196         desc.logarithmic  = lilv_port_has_property(_impl->plugin, port, _world.ext_logarithmic);
2197         desc.sr_dependent = lilv_port_has_property(_impl->plugin, port, _world.lv2_sampleRate);
2198         desc.label        = lilv_node_as_string(lilv_port_get_name(_impl->plugin, port));
2199         desc.normal       = def ? lilv_node_as_float(def) : 0.0f;
2200         desc.lower        = min ? lilv_node_as_float(min) : 0.0f;
2201         desc.upper        = max ? lilv_node_as_float(max) : 1.0f;
2202         load_parameter_descriptor_units(_world.world, desc, portunits);
2203
2204         if (desc.sr_dependent) {
2205                 desc.lower *= _session.frame_rate ();
2206                 desc.upper *= _session.frame_rate ();
2207         }
2208
2209         desc.enumeration = lilv_port_has_property(_impl->plugin, port, _world.lv2_enumeration);
2210         desc.scale_points = get_scale_points(which);
2211
2212         if (steps) {
2213                 desc.rangesteps = lilv_node_as_float (steps);
2214         }
2215
2216         desc.update_steps();
2217
2218         lilv_node_free(def);
2219         lilv_node_free(min);
2220         lilv_node_free(max);
2221         lilv_node_free(steps);
2222         lilv_nodes_free(portunits);
2223
2224         return 0;
2225 }
2226
2227 Plugin::IOPortDescription
2228 LV2Plugin::describe_io_port (ARDOUR::DataType dt, bool input, uint32_t id) const
2229 {
2230         PortFlags match = 0;
2231         switch (dt) {
2232                 case DataType::AUDIO:
2233                         match = PORT_AUDIO;
2234                         break;
2235                 case DataType::MIDI:
2236                         match = PORT_SEQUENCE | PORT_MIDI; // ignore old PORT_EVENT
2237                         break;
2238                 default:
2239                         return Plugin::IOPortDescription ("?");
2240                         break;
2241         }
2242         if (input) {
2243                 match |= PORT_INPUT;
2244         } else {
2245                 match |= PORT_OUTPUT;
2246         }
2247
2248         uint32_t p = 0;
2249         uint32_t idx = UINT32_MAX;
2250
2251         uint32_t const num_ports = parameter_count();
2252         for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2253                 PortFlags flags = _port_flags[port_index];
2254                 if ((flags & match) == match) {
2255                         if (p == id) {
2256                                 idx = port_index;
2257                         }
2258                         ++p;
2259                 }
2260         }
2261         if (idx == UINT32_MAX) {
2262                 return Plugin::IOPortDescription ("?");
2263         }
2264
2265         const LilvPort* pport = lilv_plugin_get_port_by_index (_impl->plugin, idx);
2266
2267         LilvNode* name = lilv_port_get_name(_impl->plugin, pport);
2268         Plugin::IOPortDescription iod (lilv_node_as_string (name));
2269         lilv_node_free(name);
2270
2271         /* get the port's pg:group */
2272         LilvNodes* groups = lilv_port_get_value (_impl->plugin, pport, _world.groups_group);
2273         if (lilv_nodes_size (groups) > 0) {
2274                 const LilvNode* group = lilv_nodes_get_first (groups);
2275                 LilvNodes* grouplabel = lilv_world_find_nodes (_world.world, group, _world.rdfs_label, NULL);
2276
2277                 /* get the name of the port-group */
2278                 if (lilv_nodes_size (grouplabel) > 0) {
2279                         const LilvNode* grpname = lilv_nodes_get_first (grouplabel);
2280                         iod.group_name = lilv_node_as_string (grpname);
2281                 }
2282                 lilv_nodes_free (grouplabel);
2283
2284                 /* get all port designations.
2285                  * we're interested in e.g. lv2:designation pg:right */
2286                 LilvNodes* designations = lilv_port_get_value (_impl->plugin, pport, _world.lv2_designation);
2287                 if (lilv_nodes_size (designations) > 0) {
2288                         /* get all pg:elements of the pg:group */
2289                         LilvNodes* group_childs = lilv_world_find_nodes (_world.world, group, _world.groups_element, NULL);
2290                         if (lilv_nodes_size (group_childs) > 0) {
2291                                 /* iterate over all port designations .. */
2292                                 LILV_FOREACH (nodes, i, designations) {
2293                                         const LilvNode* designation = lilv_nodes_get (designations, i);
2294                                         /* match the lv2:designation's element against the port-group's element */
2295                                         LILV_FOREACH (nodes, j, group_childs) {
2296                                                 const LilvNode* group_element = lilv_nodes_get (group_childs, j);
2297                                                 LilvNodes* elem = lilv_world_find_nodes (_world.world, group_element, _world.lv2_designation, designation);
2298                                                 /* found it. Now look up the index (channel-number) of the pg:Element */
2299                                                 if (lilv_nodes_size (elem) > 0) {
2300                                                         LilvNodes* idx = lilv_world_find_nodes (_world.world, lilv_nodes_get_first (elem), _world.lv2_index, NULL);
2301                                                         if (lilv_node_is_int (lilv_nodes_get_first (idx))) {
2302                                                                 iod.group_channel = lilv_node_as_int(lilv_nodes_get_first (idx));
2303                                                         }
2304                                                 }
2305                                         }
2306                                 }
2307                         }
2308                 }
2309                 lilv_nodes_free (groups);
2310                 lilv_nodes_free (designations);
2311         }
2312
2313         if (lilv_port_has_property(_impl->plugin, pport, _world.lv2_isSideChain)) {
2314                 iod.is_sidechain = true;
2315         }
2316         return iod;
2317 }
2318
2319 string
2320 LV2Plugin::describe_parameter(Evoral::Parameter which)
2321 {
2322         if (( which.type() == PluginAutomation) && ( which.id() < parameter_count()) ) {
2323
2324                 if (lilv_port_has_property(_impl->plugin,
2325                                         lilv_plugin_get_port_by_index(_impl->plugin, which.id()), _world.ext_notOnGUI)) {
2326                         return X_("hidden");
2327                 }
2328
2329                 if (lilv_port_has_property(_impl->plugin,
2330                                         lilv_plugin_get_port_by_index(_impl->plugin, which.id()), _world.lv2_freewheeling)) {
2331                         return X_("hidden");
2332                 }
2333
2334                 if (lilv_port_has_property(_impl->plugin,
2335                                         lilv_plugin_get_port_by_index(_impl->plugin, which.id()), _world.lv2_reportsLatency)) {
2336                         return X_("latency");
2337                 }
2338
2339                 LilvNode* name = lilv_port_get_name(_impl->plugin,
2340                                                     lilv_plugin_get_port_by_index(_impl->plugin, which.id()));
2341                 string ret(lilv_node_as_string(name));
2342                 lilv_node_free(name);
2343                 return ret;
2344         } else {
2345                 return "??";
2346         }
2347 }
2348
2349 framecnt_t
2350 LV2Plugin::max_latency () const
2351 {
2352         return _max_latency;
2353 }
2354
2355 framecnt_t
2356 LV2Plugin::signal_latency() const
2357 {
2358         if (_latency_control_port) {
2359                 return (framecnt_t)floor(*_latency_control_port);
2360         } else {
2361                 return 0;
2362         }
2363 }
2364
2365 set<Evoral::Parameter>
2366 LV2Plugin::automatable() const
2367 {
2368         set<Evoral::Parameter> ret;
2369
2370         for (uint32_t i = 0; i < parameter_count(); ++i) {
2371                 if (parameter_is_input(i) && parameter_is_control(i) && !(_port_flags[i] & PORT_NOAUTO)) {
2372                         ret.insert(ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
2373                 }
2374         }
2375
2376         for (PropertyDescriptors::const_iterator p = _property_descriptors.begin();
2377              p != _property_descriptors.end();
2378              ++p) {
2379                 ret.insert(ret.end(), Evoral::Parameter(PluginPropertyAutomation, 0, p->first));
2380         }
2381         return ret;
2382 }
2383
2384 void
2385 LV2Plugin::set_automation_control (uint32_t i, boost::shared_ptr<AutomationControl> c)
2386 {
2387         if ((_port_flags[i] & (PORT_CTRLED | PORT_CTRLER))) {
2388                 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("Ctrl Port %1\n", i));
2389                 _ctrl_map [i] = AutomationCtrlPtr (new AutomationCtrl(c));
2390         }
2391 }
2392
2393 LV2Plugin::AutomationCtrlPtr
2394 LV2Plugin::get_automation_control (uint32_t i)
2395 {
2396         if (_ctrl_map.find (i) == _ctrl_map.end()) {
2397                 return AutomationCtrlPtr ();
2398         }
2399         return _ctrl_map[i];
2400 }
2401
2402 void
2403 LV2Plugin::activate()
2404 {
2405         DEBUG_TRACE(DEBUG::LV2, string_compose("%1 activate\n", name()));
2406
2407         if (!_was_activated) {
2408                 lilv_instance_activate(_impl->instance);
2409                 _was_activated = true;
2410         }
2411 }
2412
2413 void
2414 LV2Plugin::deactivate()
2415 {
2416         DEBUG_TRACE(DEBUG::LV2, string_compose("%1 deactivate\n", name()));
2417
2418         if (_was_activated) {
2419                 lilv_instance_deactivate(_impl->instance);
2420                 _was_activated = false;
2421         }
2422 }
2423
2424 void
2425 LV2Plugin::cleanup()
2426 {
2427         DEBUG_TRACE(DEBUG::LV2, string_compose("%1 cleanup\n", name()));
2428
2429         deactivate();
2430         lilv_instance_free(_impl->instance);
2431         _impl->instance = NULL;
2432 }
2433
2434 void
2435 LV2Plugin::allocate_atom_event_buffers()
2436 {
2437         /* reserve local scratch buffers for ATOM event-queues */
2438         const LilvPlugin* p = _impl->plugin;
2439
2440         /* count non-MIDI atom event-ports
2441          * TODO: nicely ask drobilla to make a lilv_ call for that
2442          */
2443         int count_atom_out = 0;
2444         int count_atom_in = 0;
2445         int minimumSize = 32768; // TODO use a per-port minimum-size
2446         for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
2447                 const LilvPort* port  = lilv_plugin_get_port_by_index(p, i);
2448                 if (lilv_port_is_a(p, port, _world.atom_AtomPort)) {
2449                         LilvNodes* buffer_types = lilv_port_get_value(
2450                                 p, port, _world.atom_bufferType);
2451                         LilvNodes* atom_supports = lilv_port_get_value(
2452                                 p, port, _world.atom_supports);
2453
2454                         if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
2455                                 if (lilv_port_is_a(p, port, _world.lv2_InputPort)) {
2456                                         count_atom_in++;
2457                                 }
2458                                 if (lilv_port_is_a(p, port, _world.lv2_OutputPort)) {
2459                                         count_atom_out++;
2460                                 }
2461                                 LilvNodes* min_size_v = lilv_port_get_value(_impl->plugin, port, _world.rsz_minimumSize);
2462                                 LilvNode* min_size = min_size_v ? lilv_nodes_get_first(min_size_v) : NULL;
2463                                 if (min_size && lilv_node_is_int(min_size)) {
2464                                         minimumSize = std::max(minimumSize, lilv_node_as_int(min_size));
2465                                 }
2466                                 lilv_nodes_free(min_size_v);
2467                         }
2468                         lilv_nodes_free(buffer_types);
2469                         lilv_nodes_free(atom_supports);
2470                 }
2471         }
2472
2473         DEBUG_TRACE(DEBUG::LV2, string_compose("%1 need buffers for %2 atom-in and %3 atom-out event-ports\n",
2474                                 name(), count_atom_in, count_atom_out));
2475
2476         const int total_atom_buffers = (count_atom_in + count_atom_out);
2477         if (_atom_ev_buffers || total_atom_buffers == 0) {
2478                 return;
2479         }
2480
2481         DEBUG_TRACE(DEBUG::LV2, string_compose("allocate %1 atom_ev_buffers of %2 bytes\n", total_atom_buffers, minimumSize));
2482         _atom_ev_buffers = (LV2_Evbuf**) malloc((total_atom_buffers + 1) * sizeof(LV2_Evbuf*));
2483         for (int i = 0; i < total_atom_buffers; ++i ) {
2484                 _atom_ev_buffers[i] = lv2_evbuf_new(minimumSize, LV2_EVBUF_ATOM,
2485                                 _uri_map.urids.atom_Chunk, _uri_map.urids.atom_Sequence);
2486         }
2487         _atom_ev_buffers[total_atom_buffers] = 0;
2488         return;
2489 }
2490
2491 /** Write an ardour position/time/tempo/meter as an LV2 event.
2492  * @return true on success.
2493  */
2494 static bool
2495 write_position(LV2_Atom_Forge*     forge,
2496                LV2_Evbuf*          buf,
2497                const TempoMetric&  t,
2498                Timecode::BBT_Time& bbt,
2499                double              speed,
2500                double              bpm,
2501                framepos_t          position,
2502                framecnt_t          offset)
2503 {
2504         const URIMap::URIDs& urids = URIMap::instance().urids;
2505
2506         uint8_t pos_buf[256];
2507         lv2_atom_forge_set_buffer(forge, pos_buf, sizeof(pos_buf));
2508         LV2_Atom_Forge_Frame frame;
2509 #ifdef HAVE_LV2_1_10_0
2510         lv2_atom_forge_object(forge, &frame, 0, urids.time_Position);
2511         lv2_atom_forge_key(forge, urids.time_frame);
2512         lv2_atom_forge_long(forge, position);
2513         lv2_atom_forge_key(forge, urids.time_speed);
2514         lv2_atom_forge_float(forge, speed);
2515         lv2_atom_forge_key(forge, urids.time_barBeat);
2516         lv2_atom_forge_float(forge, bbt.beats - 1 +
2517                              (bbt.ticks / Timecode::BBT_Time::ticks_per_beat));
2518         lv2_atom_forge_key(forge, urids.time_bar);
2519         lv2_atom_forge_long(forge, bbt.bars - 1);
2520         lv2_atom_forge_key(forge, urids.time_beatUnit);
2521         lv2_atom_forge_int(forge, t.meter().note_divisor());
2522         lv2_atom_forge_key(forge, urids.time_beatsPerBar);
2523         lv2_atom_forge_float(forge, t.meter().divisions_per_bar());
2524         lv2_atom_forge_key(forge, urids.time_beatsPerMinute);
2525         lv2_atom_forge_float(forge, bpm);
2526 #else
2527         lv2_atom_forge_blank(forge, &frame, 1, urids.time_Position);
2528         lv2_atom_forge_property_head(forge, urids.time_frame, 0);
2529         lv2_atom_forge_long(forge, position);
2530         lv2_atom_forge_property_head(forge, urids.time_speed, 0);
2531         lv2_atom_forge_float(forge, speed);
2532         lv2_atom_forge_property_head(forge, urids.time_barBeat, 0);
2533         lv2_atom_forge_float(forge, bbt.beats - 1 +
2534                              (bbt.ticks / Timecode::BBT_Time::ticks_per_beat));
2535         lv2_atom_forge_property_head(forge, urids.time_bar, 0);
2536         lv2_atom_forge_long(forge, bbt.bars - 1);
2537         lv2_atom_forge_property_head(forge, urids.time_beatUnit, 0);
2538         lv2_atom_forge_int(forge, t.meter().note_divisor());
2539         lv2_atom_forge_property_head(forge, urids.time_beatsPerBar, 0);
2540         lv2_atom_forge_float(forge, t.meter().divisions_per_bar());
2541         lv2_atom_forge_property_head(forge, urids.time_beatsPerMinute, 0);
2542         lv2_atom_forge_float(forge, bpm);
2543 #endif
2544
2545         LV2_Evbuf_Iterator    end  = lv2_evbuf_end(buf);
2546         const LV2_Atom* const atom = (const LV2_Atom*)pos_buf;
2547         return lv2_evbuf_write(&end, offset, 0, atom->type, atom->size,
2548                                (const uint8_t*)(atom + 1));
2549 }
2550
2551 int
2552 LV2Plugin::connect_and_run(BufferSet& bufs,
2553                 framepos_t start, framepos_t end, double speed,
2554                 ChanMapping in_map, ChanMapping out_map,
2555                 pframes_t nframes, framecnt_t offset)
2556 {
2557         DEBUG_TRACE(DEBUG::LV2, string_compose("%1 run %2 offset %3\n", name(), nframes, offset));
2558         Plugin::connect_and_run(bufs, start, end, speed, in_map, out_map, nframes, offset);
2559
2560         cycles_t then = get_cycles();
2561
2562         TempoMap&               tmap     = _session.tempo_map();
2563         Metrics::const_iterator metric_i = tmap.metrics_end();
2564         TempoMetric             tmetric  = tmap.metric_at(start, &metric_i);
2565
2566         if (_freewheel_control_port) {
2567                 *_freewheel_control_port = _session.engine().freewheeling() ? 1.f : 0.f;
2568         }
2569
2570         if (_bpm_control_port) {
2571                 *_bpm_control_port = tmap.tempo_at_frame (start).note_types_per_minute();
2572         }
2573
2574 #ifdef LV2_EXTENDED
2575         if (_can_write_automation && start != _next_cycle_start) {
2576                 // add guard-points after locating
2577                 for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2578                         i->second->guard = true;
2579                 }
2580         }
2581 #endif
2582
2583         ChanCount bufs_count;
2584         bufs_count.set(DataType::AUDIO, 1);
2585         bufs_count.set(DataType::MIDI, 1);
2586         BufferSet& silent_bufs  = _session.get_silent_buffers(bufs_count);
2587         BufferSet& scratch_bufs = _session.get_scratch_buffers(bufs_count);
2588         uint32_t const num_ports = parameter_count();
2589         uint32_t const nil_index = std::numeric_limits<uint32_t>::max();
2590
2591         uint32_t audio_in_index  = 0;
2592         uint32_t audio_out_index = 0;
2593         uint32_t midi_in_index   = 0;
2594         uint32_t midi_out_index  = 0;
2595         uint32_t atom_port_index = 0;
2596         for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2597                 void*     buf   = NULL;
2598                 uint32_t  index = nil_index;
2599                 PortFlags flags = _port_flags[port_index];
2600                 bool      valid = false;
2601                 if (flags & PORT_AUDIO) {
2602                         if (flags & PORT_INPUT) {
2603                                 index = in_map.get(DataType::AUDIO, audio_in_index++, &valid);
2604                                 buf = (valid)
2605                                         ? bufs.get_audio(index).data(offset)
2606                                         : silent_bufs.get_audio(0).data(offset);
2607                         } else {
2608                                 index = out_map.get(DataType::AUDIO, audio_out_index++, &valid);
2609                                 buf = (valid)
2610                                         ? bufs.get_audio(index).data(offset)
2611                                         : scratch_bufs.get_audio(0).data(offset);
2612                         }
2613                 } else if (flags & (PORT_EVENT|PORT_SEQUENCE)) {
2614                         /* FIXME: The checks here for bufs.count().n_midi() > index shouldn't
2615                            be necessary, but the mapping is illegal in some cases.  Ideally
2616                            that should be fixed, but this is easier...
2617                         */
2618                         if (flags & PORT_MIDI) {
2619                                 if (flags & PORT_INPUT) {
2620                                         index = in_map.get(DataType::MIDI, midi_in_index++, &valid);
2621                                 } else {
2622                                         index = out_map.get(DataType::MIDI, midi_out_index++, &valid);
2623                                 }
2624                                 if (valid && bufs.count().n_midi() > index) {
2625                                         /* Note, ensure_lv2_bufsize() is not RT safe!
2626                                          * However free()/alloc() is only called if a
2627                                          * plugin requires a rsz:minimumSize buffersize
2628                                          * and the existing buffer if smaller.
2629                                          */
2630                                         bufs.ensure_lv2_bufsize((flags & PORT_INPUT), index, _port_minimumSize[port_index]);
2631                                         _ev_buffers[port_index] = bufs.get_lv2_midi(
2632                                                 (flags & PORT_INPUT), index, (flags & PORT_EVENT));
2633                                 }
2634                         } else if ((flags & PORT_POSITION) && (flags & PORT_INPUT)) {
2635                                 lv2_evbuf_reset(_atom_ev_buffers[atom_port_index], true);
2636                                 _ev_buffers[port_index] = _atom_ev_buffers[atom_port_index++];
2637                                 valid                   = true;
2638                         }
2639
2640                         if (valid && (flags & PORT_INPUT)) {
2641                                 if ((flags & PORT_POSITION)) {
2642                                         Timecode::BBT_Time bbt (tmap.bbt_at_frame (start));
2643                                         double bpm = tmap.tempo_at_frame (start).note_types_per_minute();
2644                                         double beatpos = (bbt.bars - 1) * tmetric.meter().divisions_per_bar()
2645                                                        + (bbt.beats - 1)
2646                                                        + (bbt.ticks / Timecode::BBT_Time::ticks_per_beat);
2647                                         beatpos *= tmetric.meter().note_divisor() / 4.0;
2648                                         if (start != _next_cycle_start ||
2649                                                         speed != _next_cycle_speed ||
2650                                                         rint (1000 * beatpos) != rint(1000 * _next_cycle_beat) ||
2651                                                         bpm != _current_bpm) {
2652                                                 // Transport or Tempo has changed, write position at cycle start
2653                                                 write_position(&_impl->forge, _ev_buffers[port_index],
2654                                                                 tmetric, bbt, speed, bpm, start, 0);
2655                                         }
2656                                 }
2657
2658                                 // Get MIDI iterator range (empty range if no MIDI)
2659                                 MidiBuffer::iterator m = (index != nil_index)
2660                                         ? bufs.get_midi(index).begin()
2661                                         : silent_bufs.get_midi(0).end();
2662                                 MidiBuffer::iterator m_end = (index != nil_index)
2663                                         ? bufs.get_midi(index).end()
2664                                         : m;
2665
2666                                 // Now merge MIDI and any transport events into the buffer
2667                                 const uint32_t     type = _uri_map.urids.midi_MidiEvent;
2668                                 const framepos_t   tend = end;
2669                                 ++metric_i;
2670                                 while (m != m_end || (metric_i != tmap.metrics_end() &&
2671                                                       (*metric_i)->frame() < tend)) {
2672                                         MetricSection* metric = (metric_i != tmap.metrics_end())
2673                                                 ? *metric_i : NULL;
2674                                         if (m != m_end && (!metric || metric->frame() > (*m).time())) {
2675                                                 const Evoral::Event<framepos_t> ev(*m, false);
2676                                                 if (ev.time() < nframes) {
2677                                                         LV2_Evbuf_Iterator eend = lv2_evbuf_end(_ev_buffers[port_index]);
2678                                                         lv2_evbuf_write(&eend, ev.time(), 0, type, ev.size(), ev.buffer());
2679                                                 }
2680                                                 ++m;
2681                                         } else {
2682                                                 tmetric.set_metric(metric);
2683                                                 Timecode::BBT_Time bbt;
2684                                                 bbt = tmap.bbt_at_frame (metric->frame());
2685                                                 double bpm = tmap.tempo_at_frame (start/*XXX*/).note_types_per_minute();
2686                                                 write_position(&_impl->forge, _ev_buffers[port_index],
2687                                                                tmetric, bbt, speed, bpm,
2688                                                                metric->frame(),
2689                                                                metric->frame() - start);
2690                                                 ++metric_i;
2691                                         }
2692                                 }
2693                         } else if (!valid) {
2694                                 // Nothing we understand or care about, connect to scratch
2695                                 // see note for midi-buffer size above
2696                                 scratch_bufs.ensure_lv2_bufsize((flags & PORT_INPUT),
2697                                                 0, _port_minimumSize[port_index]);
2698                                 _ev_buffers[port_index] = scratch_bufs.get_lv2_midi(
2699                                         (flags & PORT_INPUT), 0, (flags & PORT_EVENT));
2700                         }
2701
2702                         buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
2703                 } else {
2704                         continue;  // Control port, leave buffer alone
2705                 }
2706                 lilv_instance_connect_port(_impl->instance, port_index, buf);
2707         }
2708
2709         // Read messages from UI and push into appropriate buffers
2710         if (_from_ui) {
2711                 uint32_t read_space = _from_ui->read_space();
2712                 while (read_space > sizeof(UIMessage)) {
2713                         UIMessage msg;
2714                         if (_from_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
2715                                 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
2716                                 break;
2717                         }
2718                         vector<uint8_t> body(msg.size);
2719                         if (_from_ui->read(&body[0], msg.size) != msg.size) {
2720                                 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
2721                                 break;
2722                         }
2723                         if (msg.protocol == URIMap::instance().urids.atom_eventTransfer) {
2724                                 LV2_Evbuf*            buf  = _ev_buffers[msg.index];
2725                                 LV2_Evbuf_Iterator    i    = lv2_evbuf_end(buf);
2726                                 const LV2_Atom* const atom = (const LV2_Atom*)&body[0];
2727                                 if (!lv2_evbuf_write(&i, nframes - 1, 0, atom->type, atom->size,
2728                                                 (const uint8_t*)(atom + 1))) {
2729                                         error << "Failed to write data to LV2 event buffer\n";
2730                                 }
2731                         } else {
2732                                 error << "Received unknown message type from UI" << endmsg;
2733                         }
2734                         read_space -= sizeof(UIMessage) + msg.size;
2735                 }
2736         }
2737
2738         run(nframes);
2739
2740         midi_out_index = 0;
2741         for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
2742                 PortFlags flags = _port_flags[port_index];
2743                 bool      valid = false;
2744
2745                 /* TODO ask drobilla about comment
2746                  * "Make Ardour event buffers generic so plugins can communicate"
2747                  * in libs/ardour/buffer_set.cc:310
2748                  *
2749                  * ideally the user could choose which of the following two modes
2750                  * to use (e.g. instrument/effect chains  MIDI OUT vs MIDI TRHU).
2751                  *
2752                  * This implementation follows the discussion on IRC Mar 16 2013 16:47 UTC
2753                  * 16:51 < drobilla> rgareus: [..] i.e always replace with MIDI output [of LV2 plugin] if it's there
2754                  * 16:52 < drobilla> rgareus: That would probably be good enough [..] to make users not complain
2755                  *                            for quite a while at least ;)
2756                  */
2757                 // copy output of LV2 plugin's MIDI port to Ardour MIDI buffers -- MIDI OUT
2758                 if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE|PORT_MIDI))) {
2759                         const uint32_t buf_index = out_map.get(
2760                                 DataType::MIDI, midi_out_index++, &valid);
2761                         if (valid) {
2762                                 bufs.forward_lv2_midi(_ev_buffers[port_index], buf_index);
2763                         }
2764                 }
2765                 // Flush MIDI (write back to Ardour MIDI buffers) -- MIDI THRU
2766                 else if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE))) {
2767                         const uint32_t buf_index = out_map.get(
2768                                 DataType::MIDI, midi_out_index++, &valid);
2769                         if (valid) {
2770                                 bufs.flush_lv2_midi(true, buf_index);
2771                         }
2772                 }
2773
2774                 // Write messages to UI
2775                 if ((_to_ui || _can_write_automation || _patch_port_out_index != (uint32_t)-1) &&
2776                     (flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_SEQUENCE))) {
2777                         LV2_Evbuf* buf = _ev_buffers[port_index];
2778                         for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(buf);
2779                              lv2_evbuf_is_valid(i);
2780                              i = lv2_evbuf_next(i)) {
2781                                 uint32_t frames, subframes, type, size;
2782                                 uint8_t* data;
2783                                 lv2_evbuf_get(i, &frames, &subframes, &type, &size, &data);
2784
2785 #ifdef LV2_EXTENDED
2786                                 // Intercept Automation Write Events
2787                                 if ((flags & PORT_AUTOCTRL)) {
2788                                         LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2789                                         if (atom->type == _uri_map.urids.atom_Blank ||
2790                                                         atom->type == _uri_map.urids.atom_Object) {
2791                                                 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2792                                                 if (obj->body.otype == _uri_map.urids.auto_event) {
2793                                                         // only if transport_rolling ??
2794                                                         const LV2_Atom* parameter = NULL;
2795                                                         const LV2_Atom* value    = NULL;
2796                                                         lv2_atom_object_get(obj,
2797                                                                             _uri_map.urids.auto_parameter, &parameter,
2798                                                                             _uri_map.urids.auto_value,     &value,
2799                                                                             0);
2800                                                         if (parameter && value) {
2801                                                                 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2802                                                                 const float v = ((const LV2_Atom_Float*)value)->body;
2803                                                                 // -> add automation event..
2804                                                                 DEBUG_TRACE(DEBUG::LV2Automate,
2805                                                                                 string_compose ("Event p: %1 t: %2 v: %3\n", p, frames, v));
2806                                                                 AutomationCtrlPtr c = get_automation_control (p);
2807                                                                 if (c &&
2808                                                                      (c->ac->automation_state() == Touch || c->ac->automation_state() == Write)
2809                                                                    ) {
2810                                                                         framepos_t when = std::max ((framepos_t) 0, start + frames - _current_latency);
2811                                                                         assert (start + frames - _current_latency >= 0);
2812                                                                         if (c->guard) {
2813                                                                                 c->guard = false;
2814                                                                                 c->ac->list()->add (when, v, true, true);
2815                                                                         } else {
2816                                                                                 c->ac->set_double (v, when, true);
2817                                                                         }
2818                                                                 }
2819                                                         }
2820                                                 }
2821                                                 else if (obj->body.otype == _uri_map.urids.auto_setup) {
2822                                                         // TODO optional arguments, for now we assume the plugin
2823                                                         // writes automation for its own inputs
2824                                                         // -> put them in "touch" mode (preferably "exclusive plugin touch(TM)"
2825                                                         for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2826                                                                 if (_port_flags[i->first] & PORT_CTRLED) {
2827                                                                         DEBUG_TRACE(DEBUG::LV2Automate,
2828                                                                                 string_compose ("Setup p: %1\n", i->first));
2829                                                                         i->second->ac->set_automation_state (Touch);
2830                                                                 }
2831                                                         }
2832                                                 }
2833                                                 else if (obj->body.otype == _uri_map.urids.auto_finalize) {
2834                                                         // set [touched] parameters to "play" ??
2835                                                         // allow plugin to change its mode (from analyze to apply)
2836                                                         const LV2_Atom* parameter = NULL;
2837                                                         const LV2_Atom* value    = NULL;
2838                                                         lv2_atom_object_get(obj,
2839                                                                             _uri_map.urids.auto_parameter, &parameter,
2840                                                                             _uri_map.urids.auto_value,     &value,
2841                                                                             0);
2842                                                         if (parameter && value) {
2843                                                                 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2844                                                                 const float v = ((const LV2_Atom_Float*)value)->body;
2845                                                                 AutomationCtrlPtr c = get_automation_control (p);
2846                                                                 DEBUG_TRACE(DEBUG::LV2Automate,
2847                                                                                 string_compose ("Finalize p: %1 v: %2\n", p, v));
2848                                                                 if (c && _port_flags[p] & PORT_CTRLER) {
2849                                                                         c->ac->set_value(v, Controllable::NoGroup);
2850                                                                 }
2851                                                         } else {
2852                                                                 DEBUG_TRACE(DEBUG::LV2Automate, "Finalize\n");
2853                                                         }
2854                                                         for (AutomationCtrlMap::iterator i = _ctrl_map.begin(); i != _ctrl_map.end(); ++i) {
2855                                                                 // guard will be false if an event was written
2856                                                                 if ((_port_flags[i->first] & PORT_CTRLED) && !i->second->guard) {
2857                                                                         DEBUG_TRACE(DEBUG::LV2Automate,
2858                                                                                 string_compose ("Thin p: %1\n", i->first));
2859                                                                         i->second->ac->alist ()->thin (20);
2860                                                                 }
2861                                                         }
2862                                                 }
2863                                                 else if (obj->body.otype == _uri_map.urids.auto_start) {
2864                                                         const LV2_Atom* parameter = NULL;
2865                                                         lv2_atom_object_get(obj,
2866                                                                             _uri_map.urids.auto_parameter, &parameter,
2867                                                                             0);
2868                                                         if (parameter) {
2869                                                                 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2870                                                                 AutomationCtrlPtr c = get_automation_control (p);
2871                                                                 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("Start Touch p: %1\n", p));
2872                                                                 if (c) {
2873                                                                         c->ac->start_touch (std::max ((framepos_t)0, start - _current_latency));
2874                                                                         c->guard = true;
2875                                                                 }
2876                                                         }
2877                                                 }
2878                                                 else if (obj->body.otype == _uri_map.urids.auto_end) {
2879                                                         const LV2_Atom* parameter = NULL;
2880                                                         lv2_atom_object_get(obj,
2881                                                                             _uri_map.urids.auto_parameter, &parameter,
2882                                                                             0);
2883                                                         if (parameter) {
2884                                                                 const uint32_t p = ((const LV2_Atom_Int*)parameter)->body;
2885                                                                 AutomationCtrlPtr c = get_automation_control (p);
2886                                                                 DEBUG_TRACE(DEBUG::LV2Automate, string_compose ("End Touch p: %1\n", p));
2887                                                                 if (c) {
2888                                                                         c->ac->stop_touch (std::max ((framepos_t)0, start - _current_latency));
2889                                                                 }
2890                                                         }
2891                                                 }
2892                                         }
2893                                 }
2894 #endif
2895                                 // Intercept state dirty message
2896                                 if (_has_state_interface /* && (flags & PORT_DIRTYMSG)*/) {
2897                                         LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2898                                         if (atom->type == _uri_map.urids.atom_Blank ||
2899                                             atom->type == _uri_map.urids.atom_Object) {
2900                                                 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2901                                                 if (obj->body.otype == _uri_map.urids.state_StateChanged) {
2902                                                         _session.set_dirty ();
2903                                                 }
2904                                         }
2905                                 }
2906
2907                                 // Intercept patch change messages to emit PropertyChanged signal
2908                                 if ((flags & PORT_PATCHMSG)) {
2909                                         LV2_Atom* atom = (LV2_Atom*)(data - sizeof(LV2_Atom));
2910                                         if (atom->type == _uri_map.urids.atom_Blank ||
2911                                             atom->type == _uri_map.urids.atom_Object) {
2912                                                 LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
2913                                                 if (obj->body.otype == _uri_map.urids.patch_Set) {
2914                                                         const LV2_Atom* property = NULL;
2915                                                         const LV2_Atom* value    = NULL;
2916                                                         lv2_atom_object_get(obj,
2917                                                                             _uri_map.urids.patch_property, &property,
2918                                                                             _uri_map.urids.patch_value,    &value,
2919                                                                             0);
2920
2921                                                         if (property && value &&
2922                                                             property->type == _uri_map.urids.atom_URID &&
2923                                                             value->type    == _uri_map.urids.atom_Path) {
2924                                                                 const uint32_t prop_id = ((const LV2_Atom_URID*)property)->body;
2925                                                                 const char*    path    = (const char*)LV2_ATOM_BODY_CONST(value);
2926
2927                                                                 // Emit PropertyChanged signal for UI
2928                                                                 // TODO: This should emit the control's Changed signal
2929                                                                 PropertyChanged(prop_id, Variant(Variant::PATH, path));
2930                                                         } else {
2931                                                                 std::cerr << "warning: patch:Set for unknown property" << std::endl;
2932                                                         }
2933                                                 }
2934                                         }
2935                                 }
2936
2937                                 if (!_to_ui) continue;
2938                                 write_to_ui(port_index, URIMap::instance().urids.atom_eventTransfer,
2939                                             size + sizeof(LV2_Atom),
2940                                             data - sizeof(LV2_Atom));
2941                         }
2942                 }
2943         }
2944
2945         cycles_t now = get_cycles();
2946         set_cycles((uint32_t)(now - then));
2947
2948         // Update expected transport information for next cycle so we can detect changes
2949         _next_cycle_speed = speed;
2950         _next_cycle_start = end;
2951
2952         {
2953                 /* keep track of lv2:timePosition like plugins can do.
2954                  * Note: for no-midi plugins, we only ever send information at cycle-start,
2955                  * so it needs to be realative to that.
2956                  */
2957                 TempoMetric t = tmap.metric_at(start);
2958                 _current_bpm = tmap.tempo_at_frame (start).note_types_per_minute();
2959                 Timecode::BBT_Time bbt (tmap.bbt_at_frame (start));
2960                 double beatpos = (bbt.bars - 1) * t.meter().divisions_per_bar()
2961                                + (bbt.beats - 1)
2962                                + (bbt.ticks / Timecode::BBT_Time::ticks_per_beat);
2963                 beatpos *= tmetric.meter().note_divisor() / 4.0;
2964                 _next_cycle_beat = beatpos + nframes * speed * _current_bpm / (60.f * _session.frame_rate());
2965         }
2966
2967         if (_latency_control_port) {
2968                 framecnt_t new_latency = signal_latency ();
2969                 _current_latency = new_latency;
2970         }
2971         return 0;
2972 }
2973
2974 bool
2975 LV2Plugin::parameter_is_control(uint32_t param) const
2976 {
2977         assert(param < _port_flags.size());
2978         return _port_flags[param] & PORT_CONTROL;
2979 }
2980
2981 bool
2982 LV2Plugin::parameter_is_audio(uint32_t param) const
2983 {
2984         assert(param < _port_flags.size());
2985         return _port_flags[param] & PORT_AUDIO;
2986 }
2987
2988 bool
2989 LV2Plugin::parameter_is_event(uint32_t param) const
2990 {
2991         assert(param < _port_flags.size());
2992         return _port_flags[param] & PORT_EVENT;
2993 }
2994
2995 bool
2996 LV2Plugin::parameter_is_output(uint32_t param) const
2997 {
2998         assert(param < _port_flags.size());
2999         return _port_flags[param] & PORT_OUTPUT;
3000 }
3001
3002 bool
3003 LV2Plugin::parameter_is_input(uint32_t param) const
3004 {
3005         assert(param < _port_flags.size());
3006         return _port_flags[param] & PORT_INPUT;
3007 }
3008
3009 uint32_t
3010 LV2Plugin::designated_bypass_port ()
3011 {
3012         const LilvPort* port = NULL;
3013         LilvNode* designation = lilv_new_uri (_world.world, LV2_CORE_PREFIX "enabled");
3014         port = lilv_plugin_get_port_by_designation (
3015                         _impl->plugin, _world.lv2_InputPort, designation);
3016         lilv_node_free(designation);
3017         if (port) {
3018                 return lilv_port_get_index (_impl->plugin, port);
3019         }
3020 #ifdef LV2_EXTENDED
3021         /* deprecated on 2016-Sep-18 in favor of lv2:enabled */
3022         designation = lilv_new_uri (_world.world, LV2_PROCESSING_URI__enable);
3023         port = lilv_plugin_get_port_by_designation (
3024                         _impl->plugin, _world.lv2_InputPort, designation);
3025         lilv_node_free(designation);
3026         if (port) {
3027                 return lilv_port_get_index (_impl->plugin, port);
3028         }
3029 #endif
3030         return UINT32_MAX;
3031 }
3032
3033 void
3034 LV2Plugin::print_parameter(uint32_t param, char* buf, uint32_t len) const
3035 {
3036         if (buf && len) {
3037                 if (param < parameter_count()) {
3038                         snprintf(buf, len, "%.3f", get_parameter(param));
3039                 } else {
3040                         strcat(buf, "0");
3041                 }
3042         }
3043 }
3044
3045 boost::shared_ptr<ScalePoints>
3046 LV2Plugin::get_scale_points(uint32_t port_index) const
3047 {
3048         const LilvPort*  port   = lilv_plugin_get_port_by_index(_impl->plugin, port_index);
3049         LilvScalePoints* points = lilv_port_get_scale_points(_impl->plugin, port);
3050
3051         boost::shared_ptr<ScalePoints> ret;
3052         if (!points) {
3053                 return ret;
3054         }
3055
3056         ret = boost::shared_ptr<ScalePoints>(new ScalePoints());
3057
3058         LILV_FOREACH(scale_points, i, points) {
3059                 const LilvScalePoint* p     = lilv_scale_points_get(points, i);
3060                 const LilvNode*       label = lilv_scale_point_get_label(p);
3061                 const LilvNode*       value = lilv_scale_point_get_value(p);
3062                 if (label && (lilv_node_is_float(value) || lilv_node_is_int(value))) {
3063                         ret->insert(make_pair(lilv_node_as_string(label),
3064                                               lilv_node_as_float(value)));
3065                 }
3066         }
3067
3068         lilv_scale_points_free(points);
3069         return ret;
3070 }
3071
3072 void
3073 LV2Plugin::run(pframes_t nframes, bool sync_work)
3074 {
3075         uint32_t const N = parameter_count();
3076         for (uint32_t i = 0; i < N; ++i) {
3077                 if (parameter_is_control(i) && parameter_is_input(i)) {
3078                         _control_data[i] = _shadow_data[i];
3079                 }
3080         }
3081
3082         if (_worker) {
3083                 // Execute work synchronously if we're freewheeling (export)
3084                 _worker->set_synchronous(sync_work || session().engine().freewheeling());
3085         }
3086
3087         // Run the plugin for this cycle
3088         lilv_instance_run(_impl->instance, nframes);
3089
3090         // Emit any queued worker responses (calls a plugin callback)
3091         if (_state_worker) {
3092                 _state_worker->emit_responses();
3093         }
3094         if (_worker) {
3095                 _worker->emit_responses();
3096         }
3097
3098         // Notify the plugin that a work run cycle is complete
3099         if (_impl->work_iface) {
3100                 if (_impl->work_iface->end_run) {
3101                         _impl->work_iface->end_run(_impl->instance->lv2_handle);
3102                 }
3103         }
3104 }
3105
3106 void
3107 LV2Plugin::latency_compute_run()
3108 {
3109         if (!_latency_control_port) {
3110                 return;
3111         }
3112
3113         // Run the plugin so that it can set its latency parameter
3114
3115         bool was_activated = _was_activated;
3116         activate();
3117
3118         uint32_t port_index = 0;
3119         uint32_t in_index   = 0;
3120         uint32_t out_index  = 0;
3121
3122         // this is done in the main thread. non realtime.
3123         const framecnt_t bufsize = _engine.samples_per_cycle();
3124         float            *buffer = (float*) malloc(_engine.samples_per_cycle() * sizeof(float));
3125
3126         memset(buffer, 0, sizeof(float) * bufsize);
3127
3128         // FIXME: Ensure plugins can handle in-place processing
3129
3130         port_index = 0;
3131
3132         while (port_index < parameter_count()) {
3133                 if (parameter_is_audio(port_index)) {
3134                         if (parameter_is_input(port_index)) {
3135                                 lilv_instance_connect_port(_impl->instance, port_index, buffer);
3136                                 in_index++;
3137                         } else if (parameter_is_output(port_index)) {
3138                                 lilv_instance_connect_port(_impl->instance, port_index, buffer);
3139                                 out_index++;
3140                         }
3141                 }
3142                 port_index++;
3143         }
3144
3145         run(bufsize, true);
3146         deactivate();
3147         if (was_activated) {
3148                 activate();
3149         }
3150         free(buffer);
3151 }
3152
3153 const LilvPort*
3154 LV2Plugin::Impl::designated_input (const char* uri, void** bufptrs[], void** bufptr)
3155 {
3156         const LilvPort* port = NULL;
3157         LilvNode* designation = lilv_new_uri(_world.world, uri);
3158         port = lilv_plugin_get_port_by_designation(
3159                 plugin, _world.lv2_InputPort, designation);
3160         lilv_node_free(designation);
3161         if (port) {
3162                 bufptrs[lilv_port_get_index(plugin, port)] = bufptr;
3163         }
3164         return port;
3165 }
3166
3167 static bool lv2_filter (const string& str, void* /*arg*/)
3168 {
3169         /* Not a dotfile, has a prefix before a period, suffix is "lv2" */
3170
3171         return str[0] != '.' && (str.length() > 3 && str.find (".lv2") == (str.length() - 4));
3172 }
3173
3174
3175 LV2World::LV2World()
3176         : world(lilv_world_new())
3177         , _bundle_checked(false)
3178 {
3179         atom_AtomPort      = lilv_new_uri(world, LV2_ATOM__AtomPort);
3180         atom_Chunk         = lilv_new_uri(world, LV2_ATOM__Chunk);
3181         atom_Sequence      = lilv_new_uri(world, LV2_ATOM__Sequence);
3182         atom_bufferType    = lilv_new_uri(world, LV2_ATOM__bufferType);
3183         atom_supports      = lilv_new_uri(world, LV2_ATOM__supports);
3184         atom_eventTransfer = lilv_new_uri(world, LV2_ATOM__eventTransfer);
3185         ev_EventPort       = lilv_new_uri(world, LILV_URI_EVENT_PORT);
3186         ext_logarithmic    = lilv_new_uri(world, LV2_PORT_PROPS__logarithmic);
3187         ext_notOnGUI       = lilv_new_uri(world, LV2_PORT_PROPS__notOnGUI);
3188         ext_expensive      = lilv_new_uri(world, LV2_PORT_PROPS__expensive);
3189         ext_causesArtifacts= lilv_new_uri(world, LV2_PORT_PROPS__causesArtifacts);
3190         ext_notAutomatic   = lilv_new_uri(world, LV2_PORT_PROPS__notAutomatic);
3191         ext_rangeSteps     = lilv_new_uri(world, LV2_PORT_PROPS__rangeSteps);
3192         groups_group       = lilv_new_uri(world, LV2_PORT_GROUPS__group);
3193         groups_element     = lilv_new_uri(world, LV2_PORT_GROUPS__element);
3194         lv2_AudioPort      = lilv_new_uri(world, LILV_URI_AUDIO_PORT);
3195         lv2_ControlPort    = lilv_new_uri(world, LILV_URI_CONTROL_PORT);
3196         lv2_InputPort      = lilv_new_uri(world, LILV_URI_INPUT_PORT);
3197         lv2_OutputPort     = lilv_new_uri(world, LILV_URI_OUTPUT_PORT);
3198         lv2_inPlaceBroken  = lilv_new_uri(world, LV2_CORE__inPlaceBroken);
3199         lv2_isSideChain    = lilv_new_uri(world, LV2_CORE_PREFIX "isSideChain");
3200         lv2_index          = lilv_new_uri(world, LV2_CORE__index);
3201         lv2_integer        = lilv_new_uri(world, LV2_CORE__integer);
3202         lv2_default        = lilv_new_uri(world, LV2_CORE__default);
3203         lv2_minimum        = lilv_new_uri(world, LV2_CORE__minimum);
3204         lv2_maximum        = lilv_new_uri(world, LV2_CORE__maximum);
3205         lv2_reportsLatency = lilv_new_uri(world, LV2_CORE__reportsLatency);
3206         lv2_sampleRate     = lilv_new_uri(world, LV2_CORE__sampleRate);
3207         lv2_toggled        = lilv_new_uri(world, LV2_CORE__toggled);
3208         lv2_designation    = lilv_new_uri(world, LV2_CORE__designation);
3209         lv2_enumeration    = lilv_new_uri(world, LV2_CORE__enumeration);
3210         lv2_freewheeling   = lilv_new_uri(world, LV2_CORE__freeWheeling);
3211         midi_MidiEvent     = lilv_new_uri(world, LILV_URI_MIDI_EVENT);
3212         rdfs_comment       = lilv_new_uri(world, LILV_NS_RDFS "comment");
3213         rdfs_label         = lilv_new_uri(world, LILV_NS_RDFS "label");
3214         rdfs_range         = lilv_new_uri(world, LILV_NS_RDFS "range");
3215         rsz_minimumSize    = lilv_new_uri(world, LV2_RESIZE_PORT__minimumSize);
3216         time_Position      = lilv_new_uri(world, LV2_TIME__Position);
3217         ui_GtkUI           = lilv_new_uri(world, LV2_UI__GtkUI);
3218         ui_external        = lilv_new_uri(world, "http://lv2plug.in/ns/extensions/ui#external");
3219         ui_externalkx      = lilv_new_uri(world, "http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget");
3220         units_unit         = lilv_new_uri(world, LV2_UNITS__unit);
3221         units_render       = lilv_new_uri(world, LV2_UNITS__render);
3222         units_hz           = lilv_new_uri(world, LV2_UNITS__hz);
3223         units_midiNote     = lilv_new_uri(world, LV2_UNITS__midiNote);
3224         units_db           = lilv_new_uri(world, LV2_UNITS__db);
3225         patch_writable     = lilv_new_uri(world, LV2_PATCH__writable);
3226         patch_Message      = lilv_new_uri(world, LV2_PATCH__Message);
3227 #ifdef LV2_EXTENDED
3228         lv2_noSampleAccurateCtrl    = lilv_new_uri(world, "http://ardour.org/lv2/ext#noSampleAccurateControls"); // deprecated 2016-09-18
3229         auto_can_write_automatation = lilv_new_uri(world, LV2_AUTOMATE_URI__can_write);
3230         auto_automation_control     = lilv_new_uri(world, LV2_AUTOMATE_URI__control);
3231         auto_automation_controlled  = lilv_new_uri(world, LV2_AUTOMATE_URI__controlled);
3232         auto_automation_controller  = lilv_new_uri(world, LV2_AUTOMATE_URI__controller);
3233         inline_display_in_gui       = lilv_new_uri(world, LV2_INLINEDISPLAY__in_gui);
3234 #endif
3235 #ifdef HAVE_LV2_1_2_0
3236         bufz_powerOf2BlockLength = lilv_new_uri(world, LV2_BUF_SIZE__powerOf2BlockLength);
3237         bufz_fixedBlockLength    = lilv_new_uri(world, LV2_BUF_SIZE__fixedBlockLength);
3238         bufz_nominalBlockLength  = lilv_new_uri(world, "http://lv2plug.in/ns/ext/buf-size#nominalBlockLength");
3239         bufz_coarseBlockLength   = lilv_new_uri(world, "http://lv2plug.in/ns/ext/buf-size#coarseBlockLength");
3240 #endif
3241
3242 }
3243
3244 LV2World::~LV2World()
3245 {
3246         if (!world) {
3247                 return;
3248         }
3249 #ifdef HAVE_LV2_1_2_0
3250         lilv_node_free(bufz_coarseBlockLength);
3251         lilv_node_free(bufz_nominalBlockLength);
3252         lilv_node_free(bufz_fixedBlockLength);
3253         lilv_node_free(bufz_powerOf2BlockLength);
3254 #endif
3255 #ifdef LV2_EXTENDED
3256         lilv_node_free(lv2_noSampleAccurateCtrl);
3257         lilv_node_free(auto_can_write_automatation);
3258         lilv_node_free(auto_automation_control);
3259         lilv_node_free(auto_automation_controlled);
3260         lilv_node_free(auto_automation_controller);
3261 #endif
3262         lilv_node_free(patch_Message);
3263         lilv_node_free(patch_writable);
3264         lilv_node_free(units_hz);
3265         lilv_node_free(units_midiNote);
3266         lilv_node_free(units_db);
3267         lilv_node_free(units_unit);
3268         lilv_node_free(units_render);
3269         lilv_node_free(ui_externalkx);
3270         lilv_node_free(ui_external);
3271         lilv_node_free(ui_GtkUI);
3272         lilv_node_free(time_Position);
3273         lilv_node_free(rsz_minimumSize);
3274         lilv_node_free(rdfs_comment);
3275         lilv_node_free(rdfs_label);
3276         lilv_node_free(rdfs_range);
3277         lilv_node_free(midi_MidiEvent);
3278         lilv_node_free(lv2_designation);
3279         lilv_node_free(lv2_enumeration);
3280         lilv_node_free(lv2_freewheeling);
3281         lilv_node_free(lv2_toggled);
3282         lilv_node_free(lv2_sampleRate);
3283         lilv_node_free(lv2_reportsLatency);
3284         lilv_node_free(lv2_index);
3285         lilv_node_free(lv2_integer);
3286         lilv_node_free(lv2_isSideChain);
3287         lilv_node_free(lv2_inPlaceBroken);
3288         lilv_node_free(lv2_OutputPort);
3289         lilv_node_free(lv2_InputPort);
3290         lilv_node_free(lv2_ControlPort);
3291         lilv_node_free(lv2_AudioPort);
3292         lilv_node_free(groups_group);
3293         lilv_node_free(groups_element);
3294         lilv_node_free(ext_rangeSteps);
3295         lilv_node_free(ext_notAutomatic);
3296         lilv_node_free(ext_causesArtifacts);
3297         lilv_node_free(ext_expensive);
3298         lilv_node_free(ext_notOnGUI);
3299         lilv_node_free(ext_logarithmic);
3300         lilv_node_free(ev_EventPort);
3301         lilv_node_free(atom_supports);
3302         lilv_node_free(atom_eventTransfer);
3303         lilv_node_free(atom_bufferType);
3304         lilv_node_free(atom_Sequence);
3305         lilv_node_free(atom_Chunk);
3306         lilv_node_free(atom_AtomPort);
3307         lilv_world_free(world);
3308         world = NULL;
3309 }
3310
3311 void
3312 LV2World::load_bundled_plugins(bool verbose)
3313 {
3314         if (!_bundle_checked) {
3315                 if (verbose) {
3316                         cout << "Scanning folders for bundled LV2s: " << ARDOUR::lv2_bundled_search_path().to_string() << endl;
3317                 }
3318
3319                 vector<string> plugin_objects;
3320                 find_paths_matching_filter (plugin_objects, ARDOUR::lv2_bundled_search_path(), lv2_filter, 0, true, true, true);
3321                 for ( vector<string>::iterator x = plugin_objects.begin(); x != plugin_objects.end (); ++x) {
3322 #ifdef PLATFORM_WINDOWS
3323                         string uri = "file:///" + *x + "/";
3324 #else
3325                         string uri = "file://" + *x + "/";
3326 #endif
3327                         LilvNode *node = lilv_new_uri(world, uri.c_str());
3328                         lilv_world_load_bundle(world, node);
3329                         lilv_node_free(node);
3330                 }
3331
3332                 lilv_world_load_all(world);
3333                 _bundle_checked = true;
3334         }
3335 }
3336
3337 LV2PluginInfo::LV2PluginInfo (const char* plugin_uri)
3338 {
3339         type = ARDOUR::LV2;
3340         _plugin_uri = strdup(plugin_uri);
3341 }
3342
3343 LV2PluginInfo::~LV2PluginInfo()
3344 {
3345         free(_plugin_uri);
3346         _plugin_uri = NULL;
3347 }
3348
3349 PluginPtr
3350 LV2PluginInfo::load(Session& session)
3351 {
3352         try {
3353                 PluginPtr plugin;
3354                 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
3355                 LilvNode* uri = lilv_new_uri(_world.world, _plugin_uri);
3356                 if (!uri) { throw failed_constructor(); }
3357                 const LilvPlugin* lp = lilv_plugins_get_by_uri(plugins, uri);
3358                 if (!lp) { throw failed_constructor(); }
3359                 plugin.reset(new LV2Plugin(session.engine(), session, lp, session.frame_rate()));
3360                 lilv_node_free(uri);
3361                 plugin->set_info(PluginInfoPtr(shared_from_this ()));
3362                 return plugin;
3363         } catch (failed_constructor& err) {
3364                 return PluginPtr((Plugin*)0);
3365         }
3366
3367         return PluginPtr();
3368 }
3369
3370 std::vector<Plugin::PresetRecord>
3371 LV2PluginInfo::get_presets (bool /*user_only*/) const
3372 {
3373         std::vector<Plugin::PresetRecord> p;
3374 #ifndef NO_PLUGIN_STATE
3375         const LilvPlugin* lp = NULL;
3376         try {
3377                 PluginPtr plugin;
3378                 const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
3379                 LilvNode* uri = lilv_new_uri(_world.world, _plugin_uri);
3380                 if (!uri) { throw failed_constructor(); }
3381                 lp = lilv_plugins_get_by_uri(plugins, uri);
3382                 if (!lp) { throw failed_constructor(); }
3383                 lilv_node_free(uri);
3384         } catch (failed_constructor& err) {
3385                 return p;
3386         }
3387         assert (lp);
3388         // see LV2Plugin::find_presets
3389         LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
3390         LilvNode* pset_Preset   = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
3391         LilvNode* rdfs_label    = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
3392
3393         LilvNodes* presets = lilv_plugin_get_related(lp, pset_Preset);
3394         LILV_FOREACH(nodes, i, presets) {
3395                 const LilvNode* preset = lilv_nodes_get(presets, i);
3396                 lilv_world_load_resource(_world.world, preset);
3397                 LilvNode* name = get_value(_world.world, preset, rdfs_label);
3398                 bool userpreset = true; // TODO
3399                 if (name) {
3400                         p.push_back (Plugin::PresetRecord (lilv_node_as_string(preset), lilv_node_as_string(name), userpreset));
3401                         lilv_node_free(name);
3402                 }
3403         }
3404         lilv_nodes_free(presets);
3405         lilv_node_free(rdfs_label);
3406         lilv_node_free(pset_Preset);
3407         lilv_node_free(lv2_appliesTo);
3408 #endif
3409         return p;
3410 }
3411
3412 bool
3413 LV2PluginInfo::in_category (const std::string &c) const
3414 {
3415         // TODO use untranslated lilv_plugin_get_class()
3416         // match gtk2_ardour/plugin_selector.cc
3417         return category == c;
3418 }
3419
3420 bool
3421 LV2PluginInfo::is_instrument () const
3422 {
3423         if (category == "Instrument") {
3424                 return true;
3425         }
3426 #if 1
3427         /* until we make sure that category remains untranslated in the lv2.ttl spec
3428          * and until most instruments also classify themselves as such, there's a 2nd check:
3429          */
3430         if (n_inputs.n_midi() > 0 && n_inputs.n_audio() == 0 && n_outputs.n_audio() > 0) {
3431                 return true;
3432         }
3433 #endif
3434         return false;
3435 }
3436
3437 PluginInfoList*
3438 LV2PluginInfo::discover()
3439 {
3440         LV2World world;
3441         world.load_bundled_plugins();
3442         _world.load_bundled_plugins(true);
3443
3444         PluginInfoList*    plugs   = new PluginInfoList;
3445         const LilvPlugins* plugins = lilv_world_get_all_plugins(world.world);
3446
3447         LILV_FOREACH(plugins, i, plugins) {
3448                 const LilvPlugin* p = lilv_plugins_get(plugins, i);
3449                 const LilvNode* pun = lilv_plugin_get_uri(p);
3450                 if (!pun) continue;
3451                 LV2PluginInfoPtr info(new LV2PluginInfo(lilv_node_as_string(pun)));
3452
3453                 LilvNode* name = lilv_plugin_get_name(p);
3454                 if (!name || !lilv_plugin_get_port_by_index(p, 0)) {
3455                         warning << "Ignoring invalid LV2 plugin "
3456                                 << lilv_node_as_string(lilv_plugin_get_uri(p))
3457                                 << endmsg;
3458                         continue;
3459                 }
3460
3461                 if (lilv_plugin_has_feature(p, world.lv2_inPlaceBroken)) {
3462                         warning << string_compose(
3463                             _("Ignoring LV2 plugin \"%1\" since it cannot do inplace processing."),
3464                             lilv_node_as_string(name)) << endmsg;
3465                         lilv_node_free(name);
3466                         continue;
3467                 }
3468
3469 #ifdef HAVE_LV2_1_2_0
3470                 LilvNodes *required_features = lilv_plugin_get_required_features (p);
3471                 if (lilv_nodes_contains (required_features, world.bufz_powerOf2BlockLength) ||
3472                                 lilv_nodes_contains (required_features, world.bufz_fixedBlockLength)
3473                    ) {
3474                         warning << string_compose(
3475                             _("Ignoring LV2 plugin \"%1\" because its buffer-size requirements cannot be satisfied."),
3476                             lilv_node_as_string(name)) << endmsg;
3477                         lilv_nodes_free(required_features);
3478                         lilv_node_free(name);
3479                         continue;
3480                 }
3481                 lilv_nodes_free(required_features);
3482 #endif
3483
3484                 info->type = LV2;
3485
3486                 info->name = string(lilv_node_as_string(name));
3487                 lilv_node_free(name);
3488                 ARDOUR::PluginScanMessage(_("LV2"), info->name, false);
3489
3490                 const LilvPluginClass* pclass = lilv_plugin_get_class(p);
3491                 const LilvNode*        label  = lilv_plugin_class_get_label(pclass);
3492                 info->category = lilv_node_as_string(label);
3493
3494                 LilvNode* author_name = lilv_plugin_get_author_name(p);
3495                 info->creator = author_name ? string(lilv_node_as_string(author_name)) : "Unknown";
3496                 lilv_node_free(author_name);
3497
3498                 info->path = "/NOPATH"; // Meaningless for LV2
3499
3500                 /* count atom-event-ports that feature
3501                  * atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent>
3502                  *
3503                  * TODO: nicely ask drobilla to make a lilv_ call for that
3504                  */
3505                 int count_midi_out = 0;
3506                 int count_midi_in = 0;
3507                 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
3508                         const LilvPort* port  = lilv_plugin_get_port_by_index(p, i);
3509                         if (lilv_port_is_a(p, port, world.atom_AtomPort)) {
3510                                 LilvNodes* buffer_types = lilv_port_get_value(
3511                                         p, port, world.atom_bufferType);
3512                                 LilvNodes* atom_supports = lilv_port_get_value(
3513                                         p, port, world.atom_supports);
3514
3515                                 if (lilv_nodes_contains(buffer_types, world.atom_Sequence)
3516                                                 && lilv_nodes_contains(atom_supports, world.midi_MidiEvent)) {
3517                                         if (lilv_port_is_a(p, port, world.lv2_InputPort)) {
3518                                                 count_midi_in++;
3519                                         }
3520                                         if (lilv_port_is_a(p, port, world.lv2_OutputPort)) {
3521                                                 count_midi_out++;
3522                                         }
3523                                 }
3524                                 lilv_nodes_free(buffer_types);
3525                                 lilv_nodes_free(atom_supports);
3526                         }
3527                 }
3528
3529                 info->n_inputs.set_audio(
3530                         lilv_plugin_get_num_ports_of_class(
3531                                 p, world.lv2_InputPort, world.lv2_AudioPort, NULL));
3532                 info->n_inputs.set_midi(
3533                         lilv_plugin_get_num_ports_of_class(
3534                                 p, world.lv2_InputPort, world.ev_EventPort, NULL)
3535                         + count_midi_in);
3536
3537                 info->n_outputs.set_audio(
3538                         lilv_plugin_get_num_ports_of_class(
3539                                 p, world.lv2_OutputPort, world.lv2_AudioPort, NULL));
3540                 info->n_outputs.set_midi(
3541                         lilv_plugin_get_num_ports_of_class(
3542                                 p, world.lv2_OutputPort, world.ev_EventPort, NULL)
3543                         + count_midi_out);
3544
3545                 info->unique_id = lilv_node_as_uri(lilv_plugin_get_uri(p));
3546                 info->index     = 0; // Meaningless for LV2
3547
3548                 plugs->push_back(info);
3549         }
3550
3551         return plugs;
3552 }