Gracefully ignore plugins with no ports, or invalid ports.
[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 <string>
21 #include <vector>
22 #include <limits>
23
24 #include <cmath>
25 #include <cstdlib>
26 #include <cstring>
27
28 #include <giomm/file.h>
29 #include <glib/gprintf.h>
30 #include <glibmm.h>
31
32 #include <boost/utility.hpp>
33
34 #include "pbd/compose.h"
35 #include "pbd/error.h"
36 #include "pbd/xml++.h"
37
38 #include "libardour-config.h"
39
40 #include "ardour/types.h"
41 #include "ardour/audio_buffer.h"
42 #include "ardour/audioengine.h"
43 #include "ardour/debug.h"
44 #include "ardour/lv2_plugin.h"
45 #include "ardour/session.h"
46 #include "ardour/tempo.h"
47 #include "ardour/worker.h"
48
49 #include "i18n.h"
50 #include <locale.h>
51
52 #include <lilv/lilv.h>
53
54 #include "lv2/lv2plug.in/ns/ext/atom/atom.h"
55 #include "lv2/lv2plug.in/ns/ext/atom/forge.h"
56 #include "lv2/lv2plug.in/ns/ext/log/log.h"
57 #include "lv2/lv2plug.in/ns/ext/midi/midi.h"
58 #include "lv2/lv2plug.in/ns/ext/port-props/port-props.h"
59 #include "lv2/lv2plug.in/ns/ext/presets/presets.h"
60 #include "lv2/lv2plug.in/ns/ext/state/state.h"
61 #include "lv2/lv2plug.in/ns/ext/time/time.h"
62 #include "lv2/lv2plug.in/ns/ext/worker/worker.h"
63 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
64 #ifdef HAVE_NEW_LV2
65 #include "lv2/lv2plug.in/ns/ext/buf-size/buf-size.h"
66 #include "lv2/lv2plug.in/ns/ext/options/options.h"
67 #endif
68
69 #include "lv2_evbuf.h"
70
71 #ifdef HAVE_SUIL
72 #include <suil/suil.h>
73 #endif
74
75 /** The number of MIDI buffers that will fit in a UI/worker comm buffer.
76     This needs to be roughly the number of cycles the UI will get around to
77     actually processing the traffic.  Lower values are flakier but save memory.
78 */
79 static const size_t NBUFS = 4;
80
81 using namespace std;
82 using namespace ARDOUR;
83 using namespace PBD;
84
85 URIMap LV2Plugin::_uri_map;
86
87 LV2Plugin::URIDs LV2Plugin::urids = {
88         _uri_map.uri_to_id(LV2_ATOM__Chunk),
89         _uri_map.uri_to_id(LV2_ATOM__Path),
90         _uri_map.uri_to_id(LV2_ATOM__Sequence),
91         _uri_map.uri_to_id(LV2_ATOM__eventTransfer),
92         _uri_map.uri_to_id(LV2_LOG__Error),
93         _uri_map.uri_to_id(LV2_LOG__Note),
94         _uri_map.uri_to_id(LV2_LOG__Warning),
95         _uri_map.uri_to_id(LV2_MIDI__MidiEvent),
96         _uri_map.uri_to_id(LV2_TIME__Position),
97         _uri_map.uri_to_id(LV2_TIME__bar),
98         _uri_map.uri_to_id(LV2_TIME__barBeat),
99         _uri_map.uri_to_id(LV2_TIME__beatUnit),
100         _uri_map.uri_to_id(LV2_TIME__beatsPerBar),
101         _uri_map.uri_to_id(LV2_TIME__beatsPerMinute),
102         _uri_map.uri_to_id(LV2_TIME__frame),
103         _uri_map.uri_to_id(LV2_TIME__speed)
104 };
105
106 class LV2World : boost::noncopyable {
107 public:
108         LV2World ();
109         ~LV2World ();
110
111         LilvWorld* world;
112
113         LilvNode* atom_AtomPort;
114         LilvNode* atom_Chunk;
115         LilvNode* atom_Sequence;
116         LilvNode* atom_bufferType;
117         LilvNode* atom_eventTransfer;
118         LilvNode* atom_supports;
119         LilvNode* ev_EventPort;
120         LilvNode* ext_logarithmic;
121         LilvNode* lv2_AudioPort;
122         LilvNode* lv2_ControlPort;
123         LilvNode* lv2_InputPort;
124         LilvNode* lv2_OutputPort;
125         LilvNode* lv2_enumeration;
126         LilvNode* lv2_inPlaceBroken;
127         LilvNode* lv2_integer;
128         LilvNode* lv2_sampleRate;
129         LilvNode* lv2_toggled;
130         LilvNode* midi_MidiEvent;
131         LilvNode* rdfs_comment;
132         LilvNode* time_Position;
133         LilvNode* ui_GtkUI;
134         LilvNode* ui_external;
135 };
136
137 static LV2World _world;
138
139 /* worker extension */
140
141 /** Called by the plugin to schedule non-RT work. */
142 static LV2_Worker_Status
143 work_schedule(LV2_Worker_Schedule_Handle handle,
144               uint32_t                   size,
145               const void*                data)
146 {
147         LV2Plugin* plugin = (LV2Plugin*)handle;
148         if (plugin->session().engine().freewheeling()) {
149                 // Freewheeling, do the work immediately in this (audio) thread
150                 return (LV2_Worker_Status)plugin->work(size, data);
151         } else {
152                 // Enqueue message for the worker thread
153                 return plugin->worker()->schedule(size, data) ?
154                         LV2_WORKER_SUCCESS : LV2_WORKER_ERR_UNKNOWN;
155         }
156 }
157
158 /** Called by the plugin to respond to non-RT work. */
159 static LV2_Worker_Status
160 work_respond(LV2_Worker_Respond_Handle handle,
161              uint32_t                  size,
162              const void*               data)
163 {
164         LV2Plugin* plugin = (LV2Plugin*)handle;
165         if (plugin->session().engine().freewheeling()) {
166                 // Freewheeling, respond immediately in this (audio) thread
167                 return (LV2_Worker_Status)plugin->work_response(size, data);
168         } else {
169                 // Enqueue response for the worker
170                 return plugin->worker()->respond(size, data) ?
171                         LV2_WORKER_SUCCESS : LV2_WORKER_ERR_UNKNOWN;
172         }
173 }
174
175 /* log extension */
176
177 static int
178 log_vprintf(LV2_Log_Handle handle,
179             LV2_URID       type,
180             const char*    fmt,
181             va_list        args)
182 {
183         char* str = NULL;
184         const int ret = g_vasprintf(&str, fmt, args);
185         if (type == LV2Plugin::urids.log_Error) {
186                 error << str << endmsg;
187         } else if (type == LV2Plugin::urids.log_Warning) {
188                 warning << str << endmsg;
189         } else if (type == LV2Plugin::urids.log_Note) {
190                 info << str << endmsg;
191         }
192         // TODO: Toggleable log:Trace message support
193         return ret;
194 }
195
196 static int
197 log_printf(LV2_Log_Handle handle,
198            LV2_URID       type,
199            const char*    fmt, ...)
200 {
201         va_list args;
202         va_start(args, fmt);
203         const int ret = log_vprintf(handle, type, fmt, args);
204         va_end(args);
205         return ret;
206 }
207
208 struct LV2Plugin::Impl {
209         Impl() : plugin(0), ui(0), ui_type(0), name(0), author(0), instance(0)
210                , work_iface(0)
211                , state(0)
212         {}
213
214         /** Find the LV2 input port with the given designation.
215          * If found, bufptrs[port_index] will be set to bufptr.
216          */
217         LilvPort* designated_input (const char* uri, void** bufptrs[], void** bufptr);
218
219         const LilvPlugin*           plugin;
220         const LilvUI*               ui;
221         const LilvNode*             ui_type;
222         LilvNode*                   name;
223         LilvNode*                   author;
224         LilvInstance*               instance;
225         const LV2_Worker_Interface* work_iface;
226         LilvState*                  state;
227         LV2_Atom_Forge              forge;
228 };
229
230 LV2Plugin::LV2Plugin (AudioEngine& engine,
231                       Session&     session,
232                       const void*  c_plugin,
233                       framecnt_t   rate)
234         : Plugin (engine, session)
235         , Workee ()
236         , _impl(new Impl())
237         , _features(NULL)
238         , _worker(NULL)
239         , _insert_id("0")
240 {
241         init(c_plugin, rate);
242 }
243
244 LV2Plugin::LV2Plugin (const LV2Plugin& other)
245         : Plugin (other)
246         , Workee ()
247         , _impl(new Impl())
248         , _features(NULL)
249         , _worker(NULL)
250         , _insert_id(other._insert_id)
251 {
252         init(other._impl->plugin, other._sample_rate);
253
254         for (uint32_t i = 0; i < parameter_count(); ++i) {
255                 _control_data[i] = other._shadow_data[i];
256                 _shadow_data[i]  = other._shadow_data[i];
257         }
258 }
259
260 void
261 LV2Plugin::init(const void* c_plugin, framecnt_t rate)
262 {
263         DEBUG_TRACE(DEBUG::LV2, "init\n");
264
265         _impl->plugin           = (const LilvPlugin*)c_plugin;
266         _impl->ui               = NULL;
267         _impl->ui_type          = NULL;
268         _to_ui                  = NULL;
269         _from_ui                = NULL;
270         _control_data           = 0;
271         _shadow_data            = 0;
272         _atom_ev_buffers        = 0;
273         _ev_buffers             = 0;
274         _bpm_control_port       = 0;
275         _freewheel_control_port = 0;
276         _latency_control_port   = 0;
277         _position_seq_port_idx  = std::numeric_limits<uint32_t>::max();
278         _next_cycle_start       = std::numeric_limits<framepos_t>::max();
279         _next_cycle_speed       = 1.0;
280         _block_length           = _engine.frames_per_cycle();
281         _seq_size               = _engine.raw_buffer_size(DataType::MIDI);
282         _state_version          = 0;
283         _was_activated          = false;
284         _has_state_interface    = false;
285
286         _instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
287         _data_access_feature.URI     = "http://lv2plug.in/ns/ext/data-access";
288         _make_path_feature.URI       = LV2_STATE__makePath;
289         _log_feature.URI             = LV2_LOG__log;
290         _work_schedule_feature.URI   = LV2_WORKER__schedule;
291         _work_schedule_feature.data  = NULL;
292
293         const LilvPlugin* plugin = _impl->plugin;
294
295         LilvNode* state_iface_uri = lilv_new_uri(_world.world, LV2_STATE__interface);
296         LilvNode* state_uri       = lilv_new_uri(_world.world, LV2_STATE_URI);
297         _has_state_interface =
298                 // What plugins should have (lv2:extensionData state:Interface)
299                 lilv_plugin_has_extension_data(plugin, state_iface_uri)
300                 // What some outdated/incorrect ones have
301                 || lilv_plugin_has_feature(plugin, state_uri);
302         lilv_node_free(state_uri);
303         lilv_node_free(state_iface_uri);
304
305         _features    = (LV2_Feature**)calloc(10, sizeof(LV2_Feature*));
306         _features[0] = &_instance_access_feature;
307         _features[1] = &_data_access_feature;
308         _features[2] = &_make_path_feature;
309         _features[3] = _uri_map.uri_map_feature();
310         _features[4] = _uri_map.urid_map_feature();
311         _features[5] = _uri_map.urid_unmap_feature();
312         _features[6] = &_log_feature;
313
314         lv2_atom_forge_init(&_impl->forge, _uri_map.urid_map());
315
316         unsigned n_features = 7;
317 #ifdef HAVE_NEW_LV2
318         LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
319         LV2_Options_Option options[] = {
320                 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__minBlockLength),
321                   sizeof(int32_t), atom_Int, &_block_length },
322                 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__maxBlockLength),
323                   sizeof(int32_t), atom_Int, &_block_length },
324                 { LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__sequenceSize),
325                   sizeof(int32_t), atom_Int, &_seq_size },
326                 { LV2_OPTIONS_INSTANCE, 0, 0, 0, 0, NULL }
327         };
328
329         _options_feature.URI    = LV2_OPTIONS__options;
330         _options_feature.data   = options;
331         _features[n_features++] = &_options_feature;
332 #endif
333
334         LV2_State_Make_Path* make_path = (LV2_State_Make_Path*)malloc(
335                 sizeof(LV2_State_Make_Path));
336         make_path->handle = this;
337         make_path->path = &lv2_state_make_path;
338         _make_path_feature.data = make_path;
339
340         LV2_Log_Log* log = (LV2_Log_Log*)malloc(sizeof(LV2_Log_Log));
341         log->handle  = this;
342         log->printf  = &log_printf;
343         log->vprintf = &log_vprintf;
344         _log_feature.data = log;
345
346         LilvNode* worker_schedule = lilv_new_uri(_world.world, LV2_WORKER__schedule);
347         if (lilv_plugin_has_feature(plugin, worker_schedule)) {
348                 LV2_Worker_Schedule* schedule = (LV2_Worker_Schedule*)malloc(
349                         sizeof(LV2_Worker_Schedule));
350                 size_t buf_size = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
351                 _worker                     = new Worker(this, buf_size);
352                 schedule->handle            = this;
353                 schedule->schedule_work     = work_schedule;
354                 _work_schedule_feature.data = schedule;
355                 _features[n_features++]     = &_work_schedule_feature;
356         }
357         lilv_node_free(worker_schedule);
358
359         _impl->instance = lilv_plugin_instantiate(plugin, rate, _features);
360         _impl->name     = lilv_plugin_get_name(plugin);
361         _impl->author   = lilv_plugin_get_author_name(plugin);
362
363         if (_impl->instance == 0) {
364                 error << _("LV2: Failed to instantiate plugin ") << uri() << endmsg;
365                 throw failed_constructor();
366         }
367
368         _instance_access_feature.data              = (void*)_impl->instance->lv2_handle;
369         _data_access_extension_data.extension_data = _impl->instance->lv2_descriptor->extension_data;
370         _data_access_feature.data                  = &_data_access_extension_data;
371
372         _impl->work_iface = (const LV2_Worker_Interface*)extension_data(
373                 LV2_WORKER__interface);
374
375         if (lilv_plugin_has_feature(plugin, _world.lv2_inPlaceBroken)) {
376                 error << string_compose(
377                     _("LV2: \"%1\" cannot be used, since it cannot do inplace processing"),
378                     lilv_node_as_string(_impl->name)) << endmsg;
379                 lilv_node_free(_impl->name);
380                 lilv_node_free(_impl->author);
381                 throw failed_constructor();
382         }
383
384         _sample_rate = rate;
385
386         const uint32_t num_ports = this->num_ports();
387         for (uint32_t i = 0; i < num_ports; ++i) {
388                 const LilvPort* port  = lilv_plugin_get_port_by_index(_impl->plugin, i);
389                 PortFlags       flags = 0;
390
391                 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_OutputPort)) {
392                         flags |= PORT_OUTPUT;
393                 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_InputPort)) {
394                         flags |= PORT_INPUT;
395                 } else {
396                         error << string_compose(
397                                 "LV2: \"%1\" port %2 is neither input nor output",
398                                 lilv_node_as_string(_impl->name), i) << endmsg;
399                         throw failed_constructor();
400                 }
401
402                 if (lilv_port_is_a(_impl->plugin, port, _world.lv2_ControlPort)) {
403                         flags |= PORT_CONTROL;
404                 } else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_AudioPort)) {
405                         flags |= PORT_AUDIO;
406                 } else if (lilv_port_is_a(_impl->plugin, port, _world.ev_EventPort)) {
407                         flags |= PORT_EVENT;
408                 } else if (lilv_port_is_a(_impl->plugin, port, _world.atom_AtomPort)) {
409                         LilvNodes* buffer_types = lilv_port_get_value(
410                                 _impl->plugin, port, _world.atom_bufferType);
411                         LilvNodes* atom_supports = lilv_port_get_value(
412                                 _impl->plugin, port, _world.atom_supports);
413
414                         if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
415                                 if (lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)) {
416                                         flags |= PORT_MESSAGE;
417                                 } else {
418                                         flags |= PORT_ATOM;
419                                 }
420                                 if (lilv_nodes_contains(atom_supports, _world.time_Position)) {
421                                         _position_seq_port_idx = i;
422                                 }
423                         }
424                         lilv_nodes_free(buffer_types);
425                         lilv_nodes_free(atom_supports);
426                 } else {
427                         error << string_compose(
428                                 "LV2: \"%1\" port %2 has no known data type",
429                                 lilv_node_as_string(_impl->name), i) << endmsg;
430                         throw failed_constructor();
431                 }
432
433                 _port_flags.push_back(flags);
434         }
435
436         _control_data = new float[num_ports];
437         _shadow_data  = new float[num_ports];
438         _defaults     = new float[num_ports];
439         _ev_buffers   = new LV2_Evbuf*[num_ports];
440         memset(_ev_buffers, 0, sizeof(LV2_Evbuf*) * num_ports);
441
442         const bool     latent        = lilv_plugin_has_latency(plugin);
443         const uint32_t latency_index = (latent)
444                 ? lilv_plugin_get_latency_port_index(plugin)
445                 : 0;
446
447         // Build an array of pointers to special parameter buffers
448         void*** params = new void**[num_ports];
449         for (uint32_t i = 0; i < num_ports; ++i) {
450                 params[i] = NULL;
451         }
452         _impl->designated_input (LV2_TIME__beatsPerMinute, params, (void**)&_bpm_control_port);
453         _impl->designated_input (LV2_CORE__freeWheeling, params, (void**)&_freewheel_control_port);
454
455         for (uint32_t i = 0; i < num_ports; ++i) {
456                 const LilvPort* port = lilv_plugin_get_port_by_index(plugin, i);
457                 const LilvNode* sym  = lilv_port_get_symbol(plugin, port);
458
459                 // Store index in map so we can look up index by symbol
460                 _port_indices.insert(std::make_pair(lilv_node_as_string(sym), i));
461
462                 // Get range and default value if applicable
463                 if (parameter_is_control(i)) {
464                         LilvNode* def;
465                         lilv_port_get_range(plugin, port, &def, NULL, NULL);
466                         _defaults[i] = def ? lilv_node_as_float(def) : 0.0f;
467                         if (lilv_port_has_property (plugin, port, _world.lv2_sampleRate)) {
468                                 _defaults[i] *= _session.frame_rate ();
469                         }
470                         lilv_node_free(def);
471
472                         lilv_instance_connect_port(_impl->instance, i, &_control_data[i]);
473
474                         if (latent && i == latency_index) {
475                                 _latency_control_port  = &_control_data[i];
476                                 *_latency_control_port = 0;
477                         }
478
479                         if (parameter_is_input(i)) {
480                                 _shadow_data[i] = default_value(i);
481                                 if (params[i]) {
482                                         *params[i] = (void*)&_shadow_data[i];
483                                 }
484                         }
485                 } else {
486                         _defaults[i] = 0.0f;
487                 }
488         }
489
490         delete[] params;
491
492         LilvUIs* uis = lilv_plugin_get_uis(plugin);
493         if (lilv_uis_size(uis) > 0) {
494 #ifdef HAVE_SUIL
495                 // Look for embeddable UI
496                 LILV_FOREACH(uis, u, uis) {
497                         const LilvUI*   this_ui      = lilv_uis_get(uis, u);
498                         const LilvNode* this_ui_type = NULL;
499                         if (lilv_ui_is_supported(this_ui,
500                                                  suil_ui_supported,
501                                                  _world.ui_GtkUI,
502                                                  &this_ui_type)) {
503                                 // TODO: Multiple UI support
504                                 _impl->ui      = this_ui;
505                                 _impl->ui_type = this_ui_type;
506                                 break;
507                         }
508                 }
509 #else
510                 // Look for Gtk native UI
511                 LILV_FOREACH(uis, i, uis) {
512                         const LilvUI* ui = lilv_uis_get(uis, i);
513                         if (lilv_ui_is_a(ui, _world.ui_GtkUI)) {
514                                 _impl->ui      = ui;
515                                 _impl->ui_type = _world.ui_GtkUI;
516                                 break;
517                         }
518                 }
519 #endif
520
521                 // If Gtk UI is not available, try to find external UI
522                 if (!_impl->ui) {
523                         LILV_FOREACH(uis, i, uis) {
524                                 const LilvUI* ui = lilv_uis_get(uis, i);
525                                 if (lilv_ui_is_a(ui, _world.ui_external)) {
526                                         _impl->ui      = ui;
527                                         _impl->ui_type = _world.ui_external;
528                                         break;
529                                 }
530                         }
531                 }
532         }
533
534         allocate_atom_event_buffers();
535         latency_compute_run();
536 }
537
538 LV2Plugin::~LV2Plugin ()
539 {
540         DEBUG_TRACE(DEBUG::LV2, string_compose("%1 destroy\n", name()));
541
542         deactivate();
543         cleanup();
544
545         lilv_instance_free(_impl->instance);
546         lilv_node_free(_impl->name);
547         lilv_node_free(_impl->author);
548
549         free(_features);
550         free(_make_path_feature.data);
551         free(_work_schedule_feature.data);
552
553         delete _to_ui;
554         delete _from_ui;
555         delete _worker;
556
557         if (_atom_ev_buffers) {
558                 LV2_Evbuf**  b = _atom_ev_buffers;
559                 while (*b) {
560                         free(*b);
561                         b++;
562                 }
563                 free(_atom_ev_buffers);
564         }
565
566         delete [] _control_data;
567         delete [] _shadow_data;
568         delete [] _ev_buffers;
569 }
570
571 bool
572 LV2Plugin::is_external_ui() const
573 {
574         if (!_impl->ui) {
575                 return false;
576         }
577         return lilv_ui_is_a(_impl->ui, _world.ui_external);
578 }
579
580 bool
581 LV2Plugin::ui_is_resizable () const
582 {
583         const LilvNode* s   = lilv_ui_get_uri(_impl->ui);
584         LilvNode*       p   = lilv_new_uri(_world.world, LV2_CORE__optionalFeature);
585         LilvNode*       fs  = lilv_new_uri(_world.world, LV2_UI__fixedSize);
586         LilvNode*       nrs = lilv_new_uri(_world.world, LV2_UI__noUserResize);
587
588         LilvNodes* fs_matches = lilv_world_find_nodes(_world.world, s, p, fs);
589         LilvNodes* nrs_matches = lilv_world_find_nodes(_world.world, s, p, nrs);
590
591         lilv_nodes_free(nrs_matches);
592         lilv_nodes_free(fs_matches);
593         lilv_node_free(nrs);
594         lilv_node_free(fs);
595         lilv_node_free(p);
596
597         return !fs_matches && !nrs_matches;
598 }
599
600 string
601 LV2Plugin::unique_id() const
602 {
603         return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
604 }
605
606 const char*
607 LV2Plugin::uri() const
608 {
609         return lilv_node_as_uri(lilv_plugin_get_uri(_impl->plugin));
610 }
611
612 const char*
613 LV2Plugin::label() const
614 {
615         return lilv_node_as_string(_impl->name);
616 }
617
618 const char*
619 LV2Plugin::name() const
620 {
621         return lilv_node_as_string(_impl->name);
622 }
623
624 const char*
625 LV2Plugin::maker() const
626 {
627         return _impl->author ? lilv_node_as_string (_impl->author) : "Unknown";
628 }
629
630 uint32_t
631 LV2Plugin::num_ports() const
632 {
633         return lilv_plugin_get_num_ports(_impl->plugin);
634 }
635
636 uint32_t
637 LV2Plugin::parameter_count() const
638 {
639         return lilv_plugin_get_num_ports(_impl->plugin);
640 }
641
642 float
643 LV2Plugin::default_value(uint32_t port)
644 {
645         return _defaults[port];
646 }
647
648 const char*
649 LV2Plugin::port_symbol(uint32_t index) const
650 {
651         const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, index);
652         if (!port) {
653                 error << name() << ": Invalid port index " << index << endmsg;
654         }
655
656         const LilvNode* sym = lilv_port_get_symbol(_impl->plugin, port);
657         return lilv_node_as_string(sym);
658 }
659
660 uint32_t
661 LV2Plugin::port_index (const char* symbol) const
662 {
663         const map<string, uint32_t>::const_iterator i = _port_indices.find(symbol);
664         if (i != _port_indices.end()) {
665                 return  i->second;
666         } else {
667                 warning << string_compose(_("LV2: Unknown port %1"), symbol) << endmsg;
668                 return (uint32_t)-1;
669         }
670 }
671
672 void
673 LV2Plugin::set_parameter(uint32_t which, float val)
674 {
675         DEBUG_TRACE(DEBUG::LV2, string_compose(
676                             "%1 set parameter %2 to %3\n", name(), which, val));
677
678         if (which < lilv_plugin_get_num_ports(_impl->plugin)) {
679                 if (get_parameter (which) == val) {
680                         return;
681                 }
682
683                 _shadow_data[which] = val;
684         } else {
685                 warning << string_compose(
686                     _("Illegal parameter number used with plugin \"%1\". "
687                       "This is a bug in either %2 or the LV2 plugin <%3>"),
688                     name(), PROGRAM_NAME, unique_id()) << endmsg;
689         }
690
691         Plugin::set_parameter(which, val);
692 }
693
694 float
695 LV2Plugin::get_parameter(uint32_t which) const
696 {
697         if (parameter_is_input(which)) {
698                 return (float)_shadow_data[which];
699         } else {
700                 return (float)_control_data[which];
701         }
702         return 0.0f;
703 }
704
705 std::string
706 LV2Plugin::get_docs() const
707 {
708         LilvNodes* comments = lilv_plugin_get_value(_impl->plugin, _world.rdfs_comment);
709         if (comments) {
710                 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
711                 lilv_nodes_free(comments);
712                 return docs;
713         }
714
715         return "";
716 }
717
718 std::string
719 LV2Plugin::get_parameter_docs(uint32_t which) const
720 {
721         LilvNodes* comments = lilv_port_get_value(
722                 _impl->plugin,
723                 lilv_plugin_get_port_by_index(_impl->plugin, which),
724                 _world.rdfs_comment);
725
726         if (comments) {
727                 const std::string docs(lilv_node_as_string(lilv_nodes_get_first(comments)));
728                 lilv_nodes_free(comments);
729                 return docs;
730         }
731
732         return "";
733 }
734
735 uint32_t
736 LV2Plugin::nth_parameter(uint32_t n, bool& ok) const
737 {
738         ok = false;
739         for (uint32_t c = 0, x = 0; x < lilv_plugin_get_num_ports(_impl->plugin); ++x) {
740                 if (parameter_is_control(x)) {
741                         if (c++ == n) {
742                                 ok = true;
743                                 return x;
744                         }
745                 }
746         }
747
748         return 0;
749 }
750
751 const void*
752 LV2Plugin::extension_data(const char* uri) const
753 {
754         return lilv_instance_get_extension_data(_impl->instance, uri);
755 }
756
757 const void*
758 LV2Plugin::c_plugin()
759 {
760         return _impl->plugin;
761 }
762
763 const void*
764 LV2Plugin::c_ui()
765 {
766         return (const void*)_impl->ui;
767 }
768
769 const void*
770 LV2Plugin::c_ui_type()
771 {
772         return (const void*)_impl->ui_type;
773 }
774
775 /** Directory for all plugin state. */
776 const std::string
777 LV2Plugin::plugin_dir() const
778 {
779         return Glib::build_filename(_session.plugins_dir(), _insert_id.to_s());
780 }
781
782 /** Directory for files created by the plugin (except during save). */
783 const std::string
784 LV2Plugin::scratch_dir() const
785 {
786         return Glib::build_filename(plugin_dir(), "scratch");
787 }
788
789 /** Directory for snapshots of files in the scratch directory. */
790 const std::string
791 LV2Plugin::file_dir() const
792 {
793         return Glib::build_filename(plugin_dir(), "files");
794 }
795
796 /** Directory to save state snapshot version @c num into. */
797 const std::string
798 LV2Plugin::state_dir(unsigned num) const
799 {
800         return Glib::build_filename(plugin_dir(), string_compose("state%1", num));
801 }
802
803 /** Implementation of state:makePath for files created at instantiation time.
804  * Note this is not used for files created at save time (Lilv deals with that).
805  */
806 char*
807 LV2Plugin::lv2_state_make_path(LV2_State_Make_Path_Handle handle,
808                                const char*                path)
809 {
810         LV2Plugin* me = (LV2Plugin*)handle;
811         if (me->_insert_id == PBD::ID("0")) {
812                 warning << string_compose(
813                         "File path \"%1\" requested but LV2 %2 has no insert ID",
814                         path, me->name()) << endmsg;
815                 return g_strdup(path);
816         }
817
818         const std::string abs_path = Glib::build_filename(me->scratch_dir(), path);
819         const std::string dirname  = Glib::path_get_dirname(abs_path);
820         g_mkdir_with_parents(dirname.c_str(), 0744);
821
822         DEBUG_TRACE(DEBUG::LV2, string_compose("new file path %1 => %2\n",
823                                                path, abs_path));
824
825         return g_strndup(abs_path.c_str(), abs_path.length());
826 }
827
828 static void
829 remove_directory(const std::string& path)
830 {
831         if (!Glib::file_test(path, Glib::FILE_TEST_IS_DIR)) {
832                 warning << string_compose("\"%1\" is not a directory", path) << endmsg;
833                 return;
834         }
835
836         Glib::RefPtr<Gio::File>           dir = Gio::File::create_for_path(path);
837         Glib::RefPtr<Gio::FileEnumerator> e   = dir->enumerate_children();
838         Glib::RefPtr<Gio::FileInfo>       fi;
839         while ((fi = e->next_file())) {
840                 if (fi->get_type() == Gio::FILE_TYPE_DIRECTORY) {
841                         remove_directory(fi->get_name());
842                 } else {
843                         dir->get_child(fi->get_name())->remove();
844                 }
845         }
846         dir->remove();
847 }
848
849 void
850 LV2Plugin::add_state(XMLNode* root) const
851 {
852         assert(_insert_id != PBD::ID("0"));
853
854         XMLNode*    child;
855         char        buf[16];
856         LocaleGuard lg(X_("POSIX"));
857
858         for (uint32_t i = 0; i < parameter_count(); ++i) {
859                 if (parameter_is_input(i) && parameter_is_control(i)) {
860                         child = new XMLNode("Port");
861                         child->add_property("symbol", port_symbol(i));
862                         snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
863                         child->add_property("value", string(buf));
864                         root->add_child_nocopy(*child);
865                 }
866         }
867
868         if (_has_state_interface) {
869                 // Provisionally increment state version and create directory
870                 const std::string new_dir = state_dir(++_state_version);
871                 g_mkdir_with_parents(new_dir.c_str(), 0744);
872
873                 LilvState* state = lilv_state_new_from_instance(
874                         _impl->plugin,
875                         _impl->instance,
876                         _uri_map.urid_map(),
877                         scratch_dir().c_str(),
878                         file_dir().c_str(),
879                         _session.externals_dir().c_str(),
880                         new_dir.c_str(),
881                         NULL,
882                         const_cast<LV2Plugin*>(this),
883                         0,
884                         NULL);
885
886                 if (!_impl->state || !lilv_state_equals(state, _impl->state)) {
887                         lilv_state_save(_world.world,
888                                         _uri_map.urid_map(),
889                                         _uri_map.urid_unmap(),
890                                         state,
891                                         NULL,
892                                         new_dir.c_str(),
893                                         "state.ttl");
894
895                         lilv_state_free(_impl->state);
896                         _impl->state = state;
897                 } else {
898                         // State is identical, decrement version and nuke directory
899                         lilv_state_free(state);
900                         remove_directory(new_dir);
901                         --_state_version;
902                 }
903
904                 root->add_property("state-dir", string_compose("state%1", _state_version));
905         }
906 }
907
908 static inline const LilvNode*
909 get_value(LilvWorld* world, const LilvNode* subject, const LilvNode* predicate)
910 {
911         LilvNodes* vs = lilv_world_find_nodes(world, subject, predicate, NULL);
912         return vs ? lilv_nodes_get_first(vs) : NULL;
913 }
914
915 void
916 LV2Plugin::find_presets()
917 {
918         LilvNode* lv2_appliesTo = lilv_new_uri(_world.world, LV2_CORE__appliesTo);
919         LilvNode* pset_Preset   = lilv_new_uri(_world.world, LV2_PRESETS__Preset);
920         LilvNode* rdfs_label    = lilv_new_uri(_world.world, LILV_NS_RDFS "label");
921
922         LilvNodes* presets = lilv_plugin_get_related(_impl->plugin, pset_Preset);
923         LILV_FOREACH(nodes, i, presets) {
924                 const LilvNode* preset = lilv_nodes_get(presets, i);
925                 lilv_world_load_resource(_world.world, preset);
926                 const LilvNode* name = get_value(_world.world, preset, rdfs_label);
927                 if (name) {
928                         _presets.insert(std::make_pair(lilv_node_as_string(preset),
929                                                        Plugin::PresetRecord(
930                                                                lilv_node_as_string(preset),
931                                                                lilv_node_as_string(name))));
932                 } else {
933                         warning << string_compose(
934                             _("Plugin \"%1\% preset \"%2%\" is missing a label\n"),
935                             lilv_node_as_string(lilv_plugin_get_uri(_impl->plugin)),
936                             lilv_node_as_string(preset)) << endmsg;
937                 }
938         }
939         lilv_nodes_free(presets);
940
941         lilv_node_free(rdfs_label);
942         lilv_node_free(pset_Preset);
943         lilv_node_free(lv2_appliesTo);
944 }
945
946 bool
947 LV2Plugin::load_preset(PresetRecord r)
948 {
949         std::map<std::string,uint32_t>::iterator it;
950
951         LilvNode* lv2_port   = lilv_new_uri(_world.world, LILV_NS_LV2 "port");
952         LilvNode* lv2_symbol = lilv_new_uri(_world.world, LILV_NS_LV2 "symbol");
953         LilvNode* preset     = lilv_new_uri(_world.world, r.uri.c_str());
954         LilvNode* pset_value = lilv_new_uri(_world.world, LV2_PRESETS__value);
955
956         LilvNodes* ports = lilv_world_find_nodes(_world.world, preset, lv2_port, NULL);
957         LILV_FOREACH(nodes, i, ports) {
958                 const LilvNode* port   = lilv_nodes_get(ports, i);
959                 const LilvNode* symbol = get_value(_world.world, port, lv2_symbol);
960                 const LilvNode* value  = get_value(_world.world, port, pset_value);
961                 if (value && lilv_node_is_float(value)) {
962                         it = _port_indices.find(lilv_node_as_string(symbol));
963                         if (it != _port_indices.end()) {
964                                 set_parameter(it->second,lilv_node_as_float(value));
965                         }
966                 }
967         }
968         lilv_nodes_free(ports);
969
970         lilv_node_free(pset_value);
971         lilv_node_free(preset);
972         lilv_node_free(lv2_symbol);
973         lilv_node_free(lv2_port);
974
975         Plugin::load_preset(r);
976
977         return true;
978 }
979
980 const void*
981 ARDOUR::lv2plugin_get_port_value(const char* port_symbol,
982                                  void*       user_data,
983                                  uint32_t*   size,
984                                  uint32_t*   type)
985 {
986         LV2Plugin *plugin = (LV2Plugin *) user_data;
987
988         uint32_t index = plugin->port_index(port_symbol);
989         if (index != (uint32_t) -1) {
990                 if (plugin->parameter_is_input(index) && plugin->parameter_is_control(index)) {
991                         float *value;
992                         *size = sizeof(float);
993                         *type = plugin->_uri_map.uri_to_id(LV2_ATOM__Float);
994                         value = &plugin->_shadow_data[index];
995
996                         return value;
997                 }
998         }
999
1000         *size = *type = 0;
1001         return NULL;
1002 }
1003
1004
1005 std::string
1006 LV2Plugin::do_save_preset(string name)
1007 {
1008         string pset_uri = uri();
1009         pset_uri += "#";
1010         pset_uri += name;
1011
1012         string save_dir = Glib::build_filename(
1013                 Glib::get_home_dir(),
1014                 Glib::build_filename(".lv2", "presets")
1015         );
1016
1017         LilvState* state = lilv_state_new_from_instance(
1018                 _impl->plugin,
1019                 _impl->instance,
1020                 _uri_map.urid_map(),
1021                 scratch_dir().c_str(),                  // file_dir
1022                 NULL,                                   // copy_dir
1023                 NULL,                                   // link_dir
1024                 save_dir.c_str(),                       // save_dir
1025                 lv2plugin_get_port_value,               // get_value
1026                 (void*) this,                           // user_data
1027                 LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE, // flags
1028                 _features                               // features
1029         );
1030
1031         lilv_state_set_label(state, name.c_str());
1032         lilv_state_save(
1033                 _world.world,           // world
1034                 _uri_map.urid_map(),    // map
1035                 _uri_map.urid_unmap(),  // unmap
1036                 state,                  // state
1037                 pset_uri.c_str(),       // uri
1038                 save_dir.c_str(),       // dir
1039                 (name + ".ttl").c_str() // filename
1040         );
1041
1042         lilv_state_free(state);
1043         return pset_uri;
1044 }
1045
1046 void
1047 LV2Plugin::do_remove_preset(string name)
1048 {
1049         string preset_file = Glib::build_filename(
1050                 Glib::get_home_dir(),
1051                 Glib::build_filename(
1052                         Glib::build_filename(".lv2", "presets"),
1053                         name + ".ttl"
1054                 )
1055         );
1056         unlink(preset_file.c_str());
1057 }
1058
1059 bool
1060 LV2Plugin::has_editor() const
1061 {
1062         return _impl->ui != NULL;
1063 }
1064
1065 bool
1066 LV2Plugin::has_message_output() const
1067 {
1068         for (uint32_t i = 0; i < num_ports(); ++i) {
1069                 if ((_port_flags[i] & (PORT_MESSAGE|PORT_ATOM)) && _port_flags[i] & PORT_OUTPUT) {
1070                         return true;
1071                 }
1072         }
1073         return false;
1074 }
1075
1076 bool
1077 LV2Plugin::write_to(RingBuffer<uint8_t>* dest,
1078                     uint32_t             index,
1079                     uint32_t             protocol,
1080                     uint32_t             size,
1081                     const uint8_t*       body)
1082 {
1083         const uint32_t buf_size = sizeof(UIMessage) + size;
1084         uint8_t        buf[buf_size];
1085
1086         UIMessage* msg = (UIMessage*)buf;
1087         msg->index    = index;
1088         msg->protocol = protocol;
1089         msg->size     = size;
1090         memcpy(msg + 1, body, size);
1091
1092         return (dest->write(buf, buf_size) == buf_size);
1093 }
1094
1095 bool
1096 LV2Plugin::write_from_ui(uint32_t       index,
1097                          uint32_t       protocol,
1098                          uint32_t       size,
1099                          const uint8_t* body)
1100 {
1101         if (!_from_ui) {
1102                 _from_ui = new RingBuffer<uint8_t>(
1103                         _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS);
1104         }
1105
1106         if (!write_to(_from_ui, index, protocol, size, body)) {
1107                 error << "Error writing from UI to plugin" << endmsg;
1108                 return false;
1109         }
1110         return true;
1111 }
1112
1113 bool
1114 LV2Plugin::write_to_ui(uint32_t       index,
1115                        uint32_t       protocol,
1116                        uint32_t       size,
1117                        const uint8_t* body)
1118 {
1119         if (!write_to(_to_ui, index, protocol, size, body)) {
1120                 error << "Error writing from plugin to UI" << endmsg;
1121                 return false;
1122         }
1123         return true;
1124 }
1125
1126 void
1127 LV2Plugin::enable_ui_emmission()
1128 {
1129         if (!_to_ui) {
1130                 _to_ui = new RingBuffer<uint8_t>(
1131                         _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS);
1132         }
1133 }
1134
1135 void
1136 LV2Plugin::emit_to_ui(void* controller, UIMessageSink sink)
1137 {
1138         if (!_to_ui) {
1139                 return;
1140         }
1141
1142         uint32_t read_space = _to_ui->read_space();
1143         while (read_space > sizeof(UIMessage)) {
1144                 UIMessage msg;
1145                 if (_to_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
1146                         error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
1147                         break;
1148                 }
1149                 uint8_t body[msg.size];
1150                 if (_to_ui->read(body, msg.size) != msg.size) {
1151                         error << "Error reading from Plugin=>UI RingBuffer" << endmsg;
1152                         break;
1153                 }
1154
1155                 sink(controller, msg.index, msg.size, msg.protocol, body);
1156
1157                 read_space -= sizeof(msg) + msg.size;
1158         }
1159 }
1160
1161 int
1162 LV2Plugin::work(uint32_t size, const void* data)
1163 {
1164         return _impl->work_iface->work(
1165                 _impl->instance->lv2_handle, work_respond, this, size, data);
1166 }
1167
1168 int
1169 LV2Plugin::work_response(uint32_t size, const void* data)
1170 {
1171         return _impl->work_iface->work_response(
1172                 _impl->instance->lv2_handle, size, data);
1173 }
1174
1175 void
1176 LV2Plugin::set_insert_info(const PluginInsert* insert)
1177 {
1178         _insert_id = insert->id();
1179 }
1180
1181 int
1182 LV2Plugin::set_state(const XMLNode& node, int version)
1183 {
1184         XMLNodeList          nodes;
1185         const XMLProperty*   prop;
1186         XMLNodeConstIterator iter;
1187         XMLNode*             child;
1188         const char*          sym;
1189         const char*          value;
1190         uint32_t             port_id;
1191         LocaleGuard          lg(X_("POSIX"));
1192
1193         if (node.name() != state_node_name()) {
1194                 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
1195                 return -1;
1196         }
1197
1198         if (version < 3000) {
1199                 nodes = node.children("port");
1200         } else {
1201                 nodes = node.children("Port");
1202         }
1203
1204         for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
1205
1206                 child = *iter;
1207
1208                 if ((prop = child->property("symbol")) != 0) {
1209                         sym = prop->value().c_str();
1210                 } else {
1211                         warning << _("LV2: port has no symbol, ignored") << endmsg;
1212                         continue;
1213                 }
1214
1215                 map<string, uint32_t>::iterator i = _port_indices.find(sym);
1216
1217                 if (i != _port_indices.end()) {
1218                         port_id = i->second;
1219                 } else {
1220                         warning << _("LV2: port has unknown index, ignored") << endmsg;
1221                         continue;
1222                 }
1223
1224                 if ((prop = child->property("value")) != 0) {
1225                         value = prop->value().c_str();
1226                 } else {
1227                         warning << _("LV2: port has no value, ignored") << endmsg;
1228                         continue;
1229                 }
1230
1231                 set_parameter(port_id, atof(value));
1232         }
1233
1234         _state_version = 0;
1235         if ((prop = node.property("state-dir")) != 0) {
1236                 if (sscanf(prop->value().c_str(), "state%u", &_state_version) != 1) {
1237                         error << string_compose(
1238                                 "LV2: failed to parse state version from \"%1\"",
1239                                 prop->value()) << endmsg;
1240                 }
1241
1242                 std::string state_file = Glib::build_filename(
1243                         plugin_dir(),
1244                         Glib::build_filename(prop->value(), "state.ttl"));
1245
1246                 LilvState* state = lilv_state_new_from_file(
1247                         _world.world, _uri_map.urid_map(), NULL, state_file.c_str());
1248
1249                 lilv_state_restore(state, _impl->instance, NULL, NULL, 0, NULL);
1250         }
1251
1252         latency_compute_run();
1253
1254         return Plugin::set_state(node, version);
1255 }
1256
1257 int
1258 LV2Plugin::get_parameter_descriptor(uint32_t which, ParameterDescriptor& desc) const
1259 {
1260         const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, which);
1261
1262         LilvNode *def, *min, *max;
1263         lilv_port_get_range(_impl->plugin, port, &def, &min, &max);
1264
1265         desc.integer_step = lilv_port_has_property(_impl->plugin, port, _world.lv2_integer);
1266         desc.toggled      = lilv_port_has_property(_impl->plugin, port, _world.lv2_toggled);
1267         desc.logarithmic  = lilv_port_has_property(_impl->plugin, port, _world.ext_logarithmic);
1268         desc.sr_dependent = lilv_port_has_property(_impl->plugin, port, _world.lv2_sampleRate);
1269         desc.label        = lilv_node_as_string(lilv_port_get_name(_impl->plugin, port));
1270         desc.lower        = min ? lilv_node_as_float(min) : 0.0f;
1271         desc.upper        = max ? lilv_node_as_float(max) : 1.0f;
1272         if (desc.sr_dependent) {
1273                 desc.lower *= _session.frame_rate ();
1274                 desc.upper *= _session.frame_rate ();
1275         }
1276
1277         desc.min_unbound  = false; // TODO: LV2 extension required
1278         desc.max_unbound  = false; // TODO: LV2 extension required
1279
1280         if (desc.integer_step) {
1281                 desc.step      = 1.0;
1282                 desc.smallstep = 0.1;
1283                 desc.largestep = 10.0;
1284         } else {
1285                 const float delta = desc.upper - desc.lower;
1286                 desc.step      = delta / 1000.0f;
1287                 desc.smallstep = delta / 10000.0f;
1288                 desc.largestep = delta / 10.0f;
1289         }
1290
1291         desc.enumeration = lilv_port_has_property(_impl->plugin, port, _world.lv2_enumeration);
1292
1293         lilv_node_free(def);
1294         lilv_node_free(min);
1295         lilv_node_free(max);
1296
1297         return 0;
1298 }
1299
1300 string
1301 LV2Plugin::describe_parameter(Evoral::Parameter which)
1302 {
1303         if (( which.type() == PluginAutomation) && ( which.id() < parameter_count()) ) {
1304                 LilvNode* name = lilv_port_get_name(_impl->plugin,
1305                                                     lilv_plugin_get_port_by_index(_impl->plugin, which.id()));
1306                 string ret(lilv_node_as_string(name));
1307                 lilv_node_free(name);
1308                 return ret;
1309         } else {
1310                 return "??";
1311         }
1312 }
1313
1314 framecnt_t
1315 LV2Plugin::signal_latency() const
1316 {
1317         if (_latency_control_port) {
1318                 return (framecnt_t)floor(*_latency_control_port);
1319         } else {
1320                 return 0;
1321         }
1322 }
1323
1324 set<Evoral::Parameter>
1325 LV2Plugin::automatable() const
1326 {
1327         set<Evoral::Parameter> ret;
1328
1329         for (uint32_t i = 0; i < parameter_count(); ++i) {
1330                 if (parameter_is_input(i) && parameter_is_control(i)) {
1331                         ret.insert(ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
1332                 }
1333         }
1334
1335         return ret;
1336 }
1337
1338 void
1339 LV2Plugin::activate()
1340 {
1341         DEBUG_TRACE(DEBUG::LV2, string_compose("%1 activate\n", name()));
1342
1343         if (!_was_activated) {
1344                 lilv_instance_activate(_impl->instance);
1345                 _was_activated = true;
1346         }
1347 }
1348
1349 void
1350 LV2Plugin::deactivate()
1351 {
1352         DEBUG_TRACE(DEBUG::LV2, string_compose("%1 deactivate\n", name()));
1353
1354         if (_was_activated) {
1355                 lilv_instance_deactivate(_impl->instance);
1356                 _was_activated = false;
1357         }
1358 }
1359
1360 void
1361 LV2Plugin::cleanup()
1362 {
1363         DEBUG_TRACE(DEBUG::LV2, string_compose("%1 cleanup\n", name()));
1364
1365         activate();
1366         deactivate();
1367         lilv_instance_free(_impl->instance);
1368         _impl->instance = NULL;
1369 }
1370
1371 void
1372 LV2Plugin::allocate_atom_event_buffers()
1373 {
1374         /* reserve local scratch buffers for ATOM event-queues */
1375         const LilvPlugin* p = _impl->plugin;
1376
1377         /* count non-MIDI atom event-ports
1378          * TODO: nicely ask drobilla to make a lilv_ call for that
1379          */
1380         int count_atom_out = 0;
1381         int count_atom_in = 0;
1382         for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
1383                 const LilvPort* port  = lilv_plugin_get_port_by_index(p, i);
1384                 if (lilv_port_is_a(p, port, _world.atom_AtomPort)) {
1385                         LilvNodes* buffer_types = lilv_port_get_value(
1386                                 p, port, _world.atom_bufferType);
1387                         LilvNodes* atom_supports = lilv_port_get_value(
1388                                 p, port, _world.atom_supports);
1389
1390                         if (!lilv_nodes_contains(buffer_types, _world.atom_Sequence)
1391                                         || !lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)) {
1392                                 if (lilv_port_is_a(p, port, _world.lv2_InputPort)) {
1393                                         count_atom_in++;
1394                                 }
1395                                 if (lilv_port_is_a(p, port, _world.lv2_OutputPort)) {
1396                                         count_atom_out++;
1397                                 }
1398                         }
1399                         lilv_nodes_free(buffer_types);
1400                         lilv_nodes_free(atom_supports);
1401                 }
1402         }
1403
1404         DEBUG_TRACE(DEBUG::LV2, string_compose("%1 need buffers for %2 atom-in and %3 atom-out event-ports\n",
1405                                 name(), count_atom_in, count_atom_out));
1406
1407         const int total_atom_buffers = (count_atom_in + count_atom_out);
1408         if (_atom_ev_buffers || total_atom_buffers == 0) {
1409                 return;
1410         }
1411
1412         DEBUG_TRACE(DEBUG::LV2, string_compose("allocate %1 atom_ev_buffers\n", total_atom_buffers));
1413         _atom_ev_buffers = (LV2_Evbuf**) malloc((total_atom_buffers + 1) * sizeof(LV2_Evbuf*));
1414         for (int i = 0; i < total_atom_buffers; ++i ) {
1415                 _atom_ev_buffers[i] = lv2_evbuf_new(32768, LV2_EVBUF_ATOM,
1416                                 LV2Plugin::urids.atom_Chunk, LV2Plugin::urids.atom_Sequence);
1417         }
1418         _atom_ev_buffers[total_atom_buffers] = 0;
1419         return;
1420 }
1421
1422 /** Write an ardour position/time/tempo/meter as an LV2 event.
1423  * @return true on success.
1424  */
1425 static bool
1426 write_position(LV2_Atom_Forge*     forge,
1427                LV2_Evbuf*          buf,
1428                const TempoMetric&  t,
1429                Timecode::BBT_Time& bbt,
1430                double              speed,
1431                framepos_t          position,
1432                framecnt_t          offset)
1433 {
1434         uint8_t pos_buf[256];
1435         lv2_atom_forge_set_buffer(forge, pos_buf, sizeof(pos_buf));
1436         LV2_Atom_Forge_Frame frame;
1437         lv2_atom_forge_blank(forge, &frame, 1, LV2Plugin::urids.time_Position);
1438         lv2_atom_forge_property_head(forge, LV2Plugin::urids.time_frame, 0);
1439         lv2_atom_forge_long(forge, position);
1440         lv2_atom_forge_property_head(forge, LV2Plugin::urids.time_speed, 0);
1441         lv2_atom_forge_float(forge, speed);
1442         lv2_atom_forge_property_head(forge, LV2Plugin::urids.time_barBeat, 0);
1443         lv2_atom_forge_float(forge, bbt.beats - 1 +
1444                              (bbt.ticks / Timecode::BBT_Time::ticks_per_beat));
1445         lv2_atom_forge_property_head(forge, LV2Plugin::urids.time_bar, 0);
1446         lv2_atom_forge_float(forge, bbt.bars - 1);
1447         lv2_atom_forge_property_head(forge, LV2Plugin::urids.time_beatUnit, 0);
1448         lv2_atom_forge_float(forge, t.meter().note_divisor());
1449         lv2_atom_forge_property_head(forge, LV2Plugin::urids.time_beatsPerBar, 0);
1450         lv2_atom_forge_float(forge, t.meter().divisions_per_bar());
1451         lv2_atom_forge_property_head(forge, LV2Plugin::urids.time_beatsPerMinute, 0);
1452         lv2_atom_forge_float(forge, t.tempo().beats_per_minute());
1453
1454         LV2_Evbuf_Iterator    end  = lv2_evbuf_end(buf);
1455         const LV2_Atom* const atom = (const LV2_Atom*)pos_buf;
1456         return lv2_evbuf_write(&end, offset, 0, atom->type, atom->size,
1457                                (const uint8_t*)(atom + 1));
1458 }
1459
1460 int
1461 LV2Plugin::connect_and_run(BufferSet& bufs,
1462         ChanMapping in_map, ChanMapping out_map,
1463         pframes_t nframes, framecnt_t offset)
1464 {
1465         DEBUG_TRACE(DEBUG::LV2, string_compose("%1 run %2 offset %3\n", name(), nframes, offset));
1466         Plugin::connect_and_run(bufs, in_map, out_map, nframes, offset);
1467
1468         cycles_t then = get_cycles();
1469
1470         TempoMap&               tmap     = _session.tempo_map();
1471         Metrics::const_iterator metric_i = tmap.metrics_end();
1472         TempoMetric             tmetric  = tmap.metric_at(_session.transport_frame(), &metric_i);
1473
1474         if (_freewheel_control_port) {
1475                 *_freewheel_control_port = _session.engine().freewheeling();
1476         }
1477
1478         if (_bpm_control_port) {
1479                 *_bpm_control_port = tmetric.tempo().beats_per_minute();
1480         }
1481
1482         ChanCount bufs_count;
1483         bufs_count.set(DataType::AUDIO, 1);
1484         bufs_count.set(DataType::MIDI, 1);
1485         BufferSet& silent_bufs  = _session.get_silent_buffers(bufs_count);
1486         BufferSet& scratch_bufs = _session.get_scratch_buffers(bufs_count);
1487         uint32_t const num_ports = parameter_count();
1488
1489         uint32_t audio_in_index  = 0;
1490         uint32_t audio_out_index = 0;
1491         uint32_t midi_in_index   = 0;
1492         uint32_t midi_out_index  = 0;
1493         uint32_t atom_port_index = 0;
1494         bool valid;
1495         for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
1496                 void*     buf   = NULL;
1497                 uint32_t  index = 0;
1498                 PortFlags flags = _port_flags[port_index];
1499                 if (flags & PORT_AUDIO) {
1500                         if (flags & PORT_INPUT) {
1501                                 index = in_map.get(DataType::AUDIO, audio_in_index++, &valid);
1502                                 buf = (valid)
1503                                         ? bufs.get_audio(index).data(offset)
1504                                         : silent_bufs.get_audio(0).data(offset);
1505                         } else {
1506                                 index = out_map.get(DataType::AUDIO, audio_out_index++, &valid);
1507                                 buf = (valid)
1508                                         ? bufs.get_audio(index).data(offset)
1509                                         : scratch_bufs.get_audio(0).data(offset);
1510                         }
1511                 } else if (flags & (PORT_EVENT|PORT_MESSAGE)) {
1512                         /* FIXME: The checks here for bufs.count().n_midi() > index shouldn't
1513                            be necessary, but the mapping is illegal in some cases.  Ideally
1514                            that should be fixed, but this is easier...
1515                         */
1516                         if (flags & PORT_INPUT) {
1517                                 index = in_map.get(DataType::MIDI, midi_in_index++, &valid);
1518                                 _ev_buffers[port_index] = (valid && bufs.count().n_midi() > index)
1519                                         ? bufs.get_lv2_midi(true, index, flags & PORT_EVENT)
1520                                         : silent_bufs.get_lv2_midi(true, 0, flags & PORT_EVENT);
1521                                 buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
1522                         } else {
1523                                 index = out_map.get(DataType::MIDI, midi_out_index++, &valid);
1524                                 _ev_buffers[port_index] = (valid && bufs.count().n_midi() > index)
1525                                         ? bufs.get_lv2_midi(false, index, flags & PORT_EVENT)
1526                                         : scratch_bufs.get_lv2_midi(false, 0, flags & PORT_EVENT);
1527                                 buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
1528                         }
1529                 } else if (flags & (PORT_ATOM)) {
1530                         assert(_atom_ev_buffers && _atom_ev_buffers[atom_port_index]);
1531                         if (flags & PORT_INPUT) {
1532                                 lv2_evbuf_reset(_atom_ev_buffers[atom_port_index], true);
1533                                 _ev_buffers[port_index] = _atom_ev_buffers[atom_port_index++];
1534
1535                                 if (port_index == _position_seq_port_idx) {
1536                                         Timecode::BBT_Time bbt;
1537                                         if (_session.transport_frame() != _next_cycle_start ||
1538                                             _session.transport_speed() != _next_cycle_speed) {
1539                                                 // Something has changed, write the position at cycle start
1540                                                 tmap.bbt_time(_session.transport_frame(), bbt);
1541                                                 write_position(&_impl->forge, _ev_buffers[port_index],
1542                                                                tmetric, bbt, _session.transport_speed(),
1543                                                                _session.transport_frame(), 0);
1544                                         }
1545
1546                                         // Write a position event for every metric change within this cycle
1547                                         while (++metric_i != tmap.metrics_end() &&
1548                                                (*metric_i)->frame() < _session.transport_frame() + nframes) {
1549                                                 MetricSection* section = *metric_i;
1550                                                 tmetric.set_metric(section);
1551                                                 bbt = section->start();
1552                                                 write_position(&_impl->forge, _ev_buffers[port_index],
1553                                                                tmetric, bbt, _session.transport_speed(),
1554                                                                section->frame(),
1555                                                                section->frame() - _session.transport_frame());
1556                                         }
1557                                 }
1558                         } else {
1559                                 lv2_evbuf_reset(_atom_ev_buffers[atom_port_index], false);
1560                                 _ev_buffers[port_index] = _atom_ev_buffers[atom_port_index++];
1561                         }
1562                         buf = lv2_evbuf_get_buffer(_ev_buffers[port_index]);
1563                         assert(buf);
1564                 } else {
1565                         continue;  // Control port, leave buffer alone
1566                 }
1567                 lilv_instance_connect_port(_impl->instance, port_index, buf);
1568         }
1569
1570         // Read messages from UI and push into appropriate buffers
1571         if (_from_ui) {
1572                 uint32_t read_space = _from_ui->read_space();
1573                 while (read_space > sizeof(UIMessage)) {
1574                         UIMessage msg;
1575                         if (_from_ui->read((uint8_t*)&msg, sizeof(msg)) != sizeof(msg)) {
1576                                 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
1577                                 break;
1578                         }
1579                         uint8_t body[msg.size];
1580                         if (_from_ui->read(body, msg.size) != msg.size) {
1581                                 error << "Error reading from UI=>Plugin RingBuffer" << endmsg;
1582                                 break;
1583                         }
1584                         if (msg.protocol == urids.atom_eventTransfer) {
1585                                 LV2_Evbuf*            buf  = _ev_buffers[msg.index];
1586                                 LV2_Evbuf_Iterator    i    = lv2_evbuf_end(buf);
1587                                 const LV2_Atom* const atom = (const LV2_Atom*)body;
1588                                 if (!lv2_evbuf_write(&i, nframes, 0, atom->type, atom->size,
1589                                                 (const uint8_t*)(atom + 1))) {
1590                                         error << "Failed to write data to LV2 event buffer\n";
1591                                 }
1592                         } else {
1593                                 error << "Received unknown message type from UI" << endmsg;
1594                         }
1595                         read_space -= sizeof(UIMessage) + msg.size;
1596                 }
1597         }
1598
1599         run(nframes);
1600
1601         midi_out_index = 0;
1602         for (uint32_t port_index = 0; port_index < num_ports; ++port_index) {
1603                 PortFlags flags = _port_flags[port_index];
1604
1605                 // Flush MIDI (write back to Ardour MIDI buffers)
1606                 if ((flags & PORT_OUTPUT) && (flags & (PORT_EVENT|PORT_MESSAGE))) {
1607                         const uint32_t buf_index = out_map.get(
1608                                 DataType::MIDI, midi_out_index++, &valid);
1609                         if (valid) {
1610                                 bufs.flush_lv2_midi(true, buf_index);
1611                         }
1612                 }
1613
1614                 // Write messages to UI
1615                 if (_to_ui && (flags & PORT_OUTPUT) && (flags & (PORT_MESSAGE|PORT_ATOM))) {
1616                         LV2_Evbuf* buf = _ev_buffers[port_index];
1617                         for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(buf);
1618                              lv2_evbuf_is_valid(i);
1619                              i = lv2_evbuf_next(i)) {
1620                                 uint32_t frames, subframes, type, size;
1621                                 uint8_t* data;
1622                                 lv2_evbuf_get(i, &frames, &subframes, &type, &size, &data);
1623                                 write_to_ui(port_index, urids.atom_eventTransfer,
1624                                             size + sizeof(LV2_Atom),
1625                                             data - sizeof(LV2_Atom));
1626                         }
1627                 }
1628         }
1629
1630         cycles_t now = get_cycles();
1631         set_cycles((uint32_t)(now - then));
1632
1633         // Update expected transport information for next cycle so we can detect changes
1634         _next_cycle_speed = _session.transport_speed();
1635         _next_cycle_start = _session.transport_frame() + (nframes * _next_cycle_speed);
1636
1637         return 0;
1638 }
1639
1640 bool
1641 LV2Plugin::parameter_is_control(uint32_t param) const
1642 {
1643         assert(param < _port_flags.size());
1644         return _port_flags[param] & PORT_CONTROL;
1645 }
1646
1647 bool
1648 LV2Plugin::parameter_is_audio(uint32_t param) const
1649 {
1650         assert(param < _port_flags.size());
1651         return _port_flags[param] & PORT_AUDIO;
1652 }
1653
1654 bool
1655 LV2Plugin::parameter_is_event(uint32_t param) const
1656 {
1657         assert(param < _port_flags.size());
1658         return _port_flags[param] & PORT_EVENT;
1659 }
1660
1661 bool
1662 LV2Plugin::parameter_is_output(uint32_t param) const
1663 {
1664         assert(param < _port_flags.size());
1665         return _port_flags[param] & PORT_OUTPUT;
1666 }
1667
1668 bool
1669 LV2Plugin::parameter_is_input(uint32_t param) const
1670 {
1671         assert(param < _port_flags.size());
1672         return _port_flags[param] & PORT_INPUT;
1673 }
1674
1675 void
1676 LV2Plugin::print_parameter(uint32_t param, char* buf, uint32_t len) const
1677 {
1678         if (buf && len) {
1679                 if (param < parameter_count()) {
1680                         snprintf(buf, len, "%.3f", get_parameter(param));
1681                 } else {
1682                         strcat(buf, "0");
1683                 }
1684         }
1685 }
1686
1687 boost::shared_ptr<Plugin::ScalePoints>
1688 LV2Plugin::get_scale_points(uint32_t port_index) const
1689 {
1690         const LilvPort*  port   = lilv_plugin_get_port_by_index(_impl->plugin, port_index);
1691         LilvScalePoints* points = lilv_port_get_scale_points(_impl->plugin, port);
1692
1693         boost::shared_ptr<Plugin::ScalePoints> ret;
1694         if (!points) {
1695                 return ret;
1696         }
1697
1698         ret = boost::shared_ptr<Plugin::ScalePoints>(new ScalePoints());
1699
1700         LILV_FOREACH(scale_points, i, points) {
1701                 const LilvScalePoint* p     = lilv_scale_points_get(points, i);
1702                 const LilvNode*       label = lilv_scale_point_get_label(p);
1703                 const LilvNode*       value = lilv_scale_point_get_value(p);
1704                 if (label && (lilv_node_is_float(value) || lilv_node_is_int(value))) {
1705                         ret->insert(make_pair(lilv_node_as_string(label),
1706                                               lilv_node_as_float(value)));
1707                 }
1708         }
1709
1710         lilv_scale_points_free(points);
1711         return ret;
1712 }
1713
1714 void
1715 LV2Plugin::run(pframes_t nframes)
1716 {
1717         uint32_t const N = parameter_count();
1718         for (uint32_t i = 0; i < N; ++i) {
1719                 if (parameter_is_control(i) && parameter_is_input(i)) {
1720                         _control_data[i] = _shadow_data[i];
1721                 }
1722         }
1723
1724         lilv_instance_run(_impl->instance, nframes);
1725
1726         if (_impl->work_iface) {
1727                 _worker->emit_responses();
1728                 if (_impl->work_iface->end_run) {
1729                         _impl->work_iface->end_run(_impl->instance->lv2_handle);
1730                 }
1731         }
1732 }
1733
1734 void
1735 LV2Plugin::latency_compute_run()
1736 {
1737         if (!_latency_control_port) {
1738                 return;
1739         }
1740
1741         // Run the plugin so that it can set its latency parameter
1742
1743         activate();
1744
1745         uint32_t port_index = 0;
1746         uint32_t in_index   = 0;
1747         uint32_t out_index  = 0;
1748
1749         const framecnt_t bufsize = 1024;
1750         float            buffer[bufsize];
1751
1752         memset(buffer, 0, sizeof(float) * bufsize);
1753
1754         // FIXME: Ensure plugins can handle in-place processing
1755
1756         port_index = 0;
1757
1758         while (port_index < parameter_count()) {
1759                 if (parameter_is_audio(port_index)) {
1760                         if (parameter_is_input(port_index)) {
1761                                 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1762                                 in_index++;
1763                         } else if (parameter_is_output(port_index)) {
1764                                 lilv_instance_connect_port(_impl->instance, port_index, buffer);
1765                                 out_index++;
1766                         }
1767                 }
1768                 port_index++;
1769         }
1770
1771         run(bufsize);
1772         deactivate();
1773 }
1774
1775 LilvPort*
1776 LV2Plugin::Impl::designated_input (const char* uri, void** bufptrs[], void** bufptr)
1777 {
1778         LilvPort* port        = NULL;
1779         LilvNode* designation = lilv_new_uri(_world.world, uri);
1780         port = lilv_plugin_get_port_by_designation(
1781                 plugin, _world.lv2_InputPort, designation);
1782         lilv_node_free(designation);
1783         if (port) {
1784                 bufptrs[lilv_port_get_index(plugin, port)] = bufptr;
1785         }
1786         return port;
1787 }
1788
1789 LV2World::LV2World()
1790         : world(lilv_world_new())
1791 {
1792         lilv_world_load_all(world);
1793         atom_AtomPort      = lilv_new_uri(world, LV2_ATOM__AtomPort);
1794         atom_Chunk         = lilv_new_uri(world, LV2_ATOM__Chunk);
1795         atom_Sequence      = lilv_new_uri(world, LV2_ATOM__Sequence);
1796         atom_bufferType    = lilv_new_uri(world, LV2_ATOM__bufferType);
1797         atom_supports      = lilv_new_uri(world, LV2_ATOM__supports);
1798         atom_eventTransfer = lilv_new_uri(world, LV2_ATOM__eventTransfer);
1799         ev_EventPort       = lilv_new_uri(world, LILV_URI_EVENT_PORT);
1800         ext_logarithmic    = lilv_new_uri(world, LV2_PORT_PROPS__logarithmic);
1801         lv2_AudioPort      = lilv_new_uri(world, LILV_URI_AUDIO_PORT);
1802         lv2_ControlPort    = lilv_new_uri(world, LILV_URI_CONTROL_PORT);
1803         lv2_InputPort      = lilv_new_uri(world, LILV_URI_INPUT_PORT);
1804         lv2_OutputPort     = lilv_new_uri(world, LILV_URI_OUTPUT_PORT);
1805         lv2_inPlaceBroken  = lilv_new_uri(world, LV2_CORE__inPlaceBroken);
1806         lv2_integer        = lilv_new_uri(world, LV2_CORE__integer);
1807         lv2_sampleRate     = lilv_new_uri(world, LV2_CORE__sampleRate);
1808         lv2_toggled        = lilv_new_uri(world, LV2_CORE__toggled);
1809         lv2_enumeration    = lilv_new_uri(world, LV2_CORE__enumeration);
1810         midi_MidiEvent     = lilv_new_uri(world, LILV_URI_MIDI_EVENT);
1811         rdfs_comment       = lilv_new_uri(world, LILV_NS_RDFS "comment");
1812         time_Position      = lilv_new_uri(world, LV2_TIME__Position);
1813         ui_GtkUI           = lilv_new_uri(world, LV2_UI__GtkUI);
1814         ui_external        = lilv_new_uri(world, "http://lv2plug.in/ns/extensions/ui#external");
1815 }
1816
1817 LV2World::~LV2World()
1818 {
1819         lilv_node_free(ui_external);
1820         lilv_node_free(ui_GtkUI);
1821         lilv_node_free(midi_MidiEvent);
1822         lilv_node_free(lv2_toggled);
1823         lilv_node_free(lv2_sampleRate);
1824         lilv_node_free(lv2_integer);
1825         lilv_node_free(lv2_inPlaceBroken);
1826         lilv_node_free(lv2_OutputPort);
1827         lilv_node_free(lv2_InputPort);
1828         lilv_node_free(lv2_ControlPort);
1829         lilv_node_free(lv2_AudioPort);
1830         lilv_node_free(ext_logarithmic);
1831         lilv_node_free(ev_EventPort);
1832         lilv_node_free(atom_eventTransfer);
1833         lilv_node_free(atom_bufferType);
1834         lilv_node_free(atom_Sequence);
1835         lilv_node_free(atom_Chunk);
1836         lilv_node_free(atom_AtomPort);
1837 }
1838
1839 LV2PluginInfo::LV2PluginInfo (const void* c_plugin)
1840         : _c_plugin(c_plugin)
1841 {
1842         type = ARDOUR::LV2;
1843 }
1844
1845 LV2PluginInfo::~LV2PluginInfo()
1846 {}
1847
1848 PluginPtr
1849 LV2PluginInfo::load(Session& session)
1850 {
1851         try {
1852                 PluginPtr plugin;
1853
1854                 plugin.reset(new LV2Plugin(session.engine(), session,
1855                                            (const LilvPlugin*)_c_plugin,
1856                                            session.frame_rate()));
1857
1858                 plugin->set_info(PluginInfoPtr(new LV2PluginInfo(*this)));
1859                 return plugin;
1860         } catch (failed_constructor& err) {
1861                 return PluginPtr((Plugin*)0);
1862         }
1863
1864         return PluginPtr();
1865 }
1866
1867 PluginInfoList*
1868 LV2PluginInfo::discover()
1869 {
1870         PluginInfoList*    plugs   = new PluginInfoList;
1871         const LilvPlugins* plugins = lilv_world_get_all_plugins(_world.world);
1872
1873         info << "LV2: Discovering " << lilv_plugins_size(plugins) << " plugins" << endmsg;
1874
1875         LILV_FOREACH(plugins, i, plugins) {
1876                 const LilvPlugin* p = lilv_plugins_get(plugins, i);
1877                 LV2PluginInfoPtr  info(new LV2PluginInfo((const void*)p));
1878
1879                 LilvNode* name = lilv_plugin_get_name(p);
1880                 if (!name || !lilv_plugin_get_port_by_index(p, 0)) {
1881                         warning << "Ignoring invalid LV2 plugin "
1882                                 << lilv_node_as_string(lilv_plugin_get_uri(p))
1883                                 << endmsg;
1884                         continue;
1885                 }
1886
1887                 info->type = LV2;
1888
1889                 info->name = string(lilv_node_as_string(name));
1890                 lilv_node_free(name);
1891
1892                 const LilvPluginClass* pclass = lilv_plugin_get_class(p);
1893                 const LilvNode*        label  = lilv_plugin_class_get_label(pclass);
1894                 info->category = lilv_node_as_string(label);
1895
1896                 LilvNode* author_name = lilv_plugin_get_author_name(p);
1897                 info->creator = author_name ? string(lilv_node_as_string(author_name)) : "Unknown";
1898                 lilv_node_free(author_name);
1899
1900                 info->path = "/NOPATH"; // Meaningless for LV2
1901
1902                 /* count atom-event-ports that feature
1903                  * atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent>
1904                  *
1905                  * TODO: nicely ask drobilla to make a lilv_ call for that
1906                  */
1907                 int count_midi_out = 0;
1908                 int count_midi_in = 0;
1909                 for (uint32_t i = 0; i < lilv_plugin_get_num_ports(p); ++i) {
1910                         const LilvPort* port  = lilv_plugin_get_port_by_index(p, i);
1911                         if (lilv_port_is_a(p, port, _world.atom_AtomPort)) {
1912                                 LilvNodes* buffer_types = lilv_port_get_value(
1913                                         p, port, _world.atom_bufferType);
1914                                 LilvNodes* atom_supports = lilv_port_get_value(
1915                                         p, port, _world.atom_supports);
1916
1917                                 if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)
1918                                                 && lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)) {
1919                                         if (lilv_port_is_a(p, port, _world.lv2_InputPort)) {
1920                                                 count_midi_in++;
1921                                         }
1922                                         if (lilv_port_is_a(p, port, _world.lv2_OutputPort)) {
1923                                                 count_midi_out++;
1924                                         }
1925                                 }
1926                                 lilv_nodes_free(buffer_types);
1927                                 lilv_nodes_free(atom_supports);
1928                         }
1929                 }
1930
1931                 info->n_inputs.set_audio(
1932                         lilv_plugin_get_num_ports_of_class(
1933                                 p, _world.lv2_InputPort, _world.lv2_AudioPort, NULL));
1934                 info->n_inputs.set_midi(
1935                         lilv_plugin_get_num_ports_of_class(
1936                                 p, _world.lv2_InputPort, _world.ev_EventPort, NULL)
1937                         + count_midi_in);
1938
1939                 info->n_outputs.set_audio(
1940                         lilv_plugin_get_num_ports_of_class(
1941                                 p, _world.lv2_OutputPort, _world.lv2_AudioPort, NULL));
1942                 info->n_outputs.set_midi(
1943                         lilv_plugin_get_num_ports_of_class(
1944                                 p, _world.lv2_OutputPort, _world.ev_EventPort, NULL)
1945                         + count_midi_out);
1946
1947                 info->unique_id = lilv_node_as_uri(lilv_plugin_get_uri(p));
1948                 info->index     = 0; // Meaningless for LV2
1949
1950                 plugs->push_back(info);
1951         }
1952
1953         return plugs;
1954 }