1 /*
2 * Copyright (C) 2018-2021 Alexandros Theodotou <alex at zrythm dot org>
3 *
4 * This file is part of Zrythm
5 *
6 * Zrythm is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Zrythm is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with Zrythm. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * This file incorporates work covered by the following copyright and
20 * permission notices:
21 *
22 Copyright 2007-2016 David Robillard <http://drobilla.net>
23
24 Permission to use, copy, modify, and/or distribute this software for any
25 purpose with or without fee is hereby granted, provided that the above
26 copyright notice and this permission notice appear in all copies.
27
28 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
29 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
30 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
31 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
32 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
33 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
34 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35
36 * Copyright (C) 2008-2012 Carl Hetherington <carl@carlh.net>
37 * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
38 * Copyright (C) 2008-2019 David Robillard <d@drobilla.net>
39 * Copyright (C) 2012-2019 Robin Gareus <robin@gareus.org>
40 * Copyright (C) 2013-2018 John Emmas <john@creativepost.co.uk>
41 * Copyright (C) 2013 Michael R. Fisher <mfisher@bketech.com>
42 * Copyright (C) 2014-2016 Tim Mayberry <mojofunk@gmail.com>
43 * Copyright (C) 2016-2017 Damien Zammit <damien@zamaudio.com>
44 * Copyright (C) 2016 Nick Mainsbridge <mainsbridge@gmail.com>
45 * Copyright (C) 2017 Johannes Mueller <github@johannes-mueller.org>
46 *
47 * This program is free software; you can redistribute it and/or modify
48 * it under the terms of the GNU General Public License as published by
49 * the Free Software Foundation; either version 2 of the License, or
50 * (at your option) any later version.
51 *
52 * This program is distributed in the hope that it will be useful,
53 * but WITHOUT ANY WARRANTY; without even the implied warranty of
54 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55 * GNU General Public License for more details.
56 *
57 * You should have received a copy of the GNU General Public License
58 * along with this program. If not, see <https://www.gnu.org/licenses/>.
59 */
60
61 #include "zrythm-config.h"
62
63 /* for dlinfo */
64 #define _GNU_SOURCE
65 #if !defined (_WOE32) && !defined (__APPLE__)
66 #include <link.h>
67 #include <dlfcn.h>
68 #endif
69
70 #include <assert.h>
71 #include <inttypes.h>
72 #include <math.h>
73 #include <signal.h>
74 #include <stdbool.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <sys/stat.h>
79 #include <sys/types.h>
80 #include <unistd.h>
81
82 #include "audio/engine.h"
83 #include "audio/midi_event.h"
84 #include "audio/tempo_track.h"
85 #include "audio/transport.h"
86 #include "gui/backend/event.h"
87 #include "gui/backend/event_manager.h"
88 #include "gui/widgets/main_window.h"
89 #include "plugins/lv2_plugin.h"
90 #include "plugins/lv2/lv2_log.h"
91 #include "plugins/lv2/lv2_evbuf.h"
92 #include "plugins/lv2/lv2_gtk.h"
93 #include "plugins/lv2/lv2_state.h"
94 #include "plugins/lv2/lv2_ui.h"
95 #include "plugins/lv2/lv2_worker.h"
96 #include "plugins/plugin.h"
97 #include "plugins/plugin_manager.h"
98 #include "project.h"
99 #include "settings/settings.h"
100 #include "utils/error.h"
101 #include "utils/flags.h"
102 #include "utils/gtk.h"
103 #include "utils/io.h"
104 #include "utils/math.h"
105 #include "utils/objects.h"
106 #include "utils/string.h"
107 #include "utils/ui.h"
108 #include "zrythm_app.h"
109
110 #include <gtk/gtk.h>
111 #include <glib/gi18n.h>
112
113 #include <lv2/buf-size/buf-size.h>
114 #include <lv2/event/event.h>
115 #include <lv2/instance-access/instance-access.h>
116 #include <lv2/midi/midi.h>
117 #include <lv2/patch/patch.h>
118 #include <lv2/port-groups/port-groups.h>
119 #include <lv2/port-props/port-props.h>
120 #include <lv2/presets/presets.h>
121 #include <lv2/resize-port/resize-port.h>
122 #include <lv2/time/time.h>
123 #include <lv2/units/units.h>
124
125 #include <lilv/lilv.h>
126 #include <sratom/sratom.h>
127 #include <suil/suil.h>
128
129 typedef enum
130 {
131 Z_PLUGINS_LV2_PLUGIN_ERROR_FAILED,
132 Z_PLUGINS_LV2_PLUGIN_ERROR_CREATION_FAILED,
133 Z_PLUGINS_LV2_PLUGIN_ERROR_INSTANTIATION_FAILED,
134 Z_PLUGINS_LV2_PLUGIN_ERROR_NO_UI,
135 Z_PLUGINS_LV2_PLUGIN_ERROR_CANNOT_FIND_URI,
136 } ZPluginsLv2PluginError;
137
138 #define Z_PLUGINS_LV2_PLUGIN_ERROR \
139 z_plugins_lv2_plugin_error_quark ()
140 GQuark z_plugins_lv2_plugin_error_quark (void);
141 G_DEFINE_QUARK (
142 z-plugins-lv2-plugin-error-quark, z_plugins_lv2_plugin_error)
143
144 /**
145 * Size factor for UI ring buffers. The ring size
146 * is a few times the size of an event output to
147 * give the UI a chance to keep up. Experiments
148 * with Ingen, which can highly saturate its event
149 * output, led me to this value (4). It really
150 * ought to be enough for anybody(TM).
151 *
152 * The number of MIDI buffers that will fit in a
153 * UI/worker comm buffer.
154 * This needs to be roughly the number of cycles
155 * the UI will get around to actually processing
156 * the traffic. Lower values are flakier but save
157 * memory.
158 */
159 #define N_BUFFER_CYCLES 4
160
161 /**
162 * Returns whether Zrythm supports the given
163 * option.
164 */
165 static bool
option_is_supported(Lv2Plugin * self,const char * uri)166 option_is_supported (
167 Lv2Plugin * self,
168 const char * uri)
169 {
170 size_t num_opts =
171 sizeof (self->options) /
172 sizeof (self->options[0]);
173 for (size_t i = 0; i < num_opts; i++)
174 {
175 LV2_Options_Option * opt = &self->options[i];
176 if (lv2_urid_map_uri (self, uri) == opt->key)
177 {
178 return true;
179 }
180 }
181 return false;
182 }
183
184 /**
185 * Returns whether Zrythm supports the given
186 * feature.
187 */
188 static bool
feature_is_supported(Lv2Plugin * self,const char * uri)189 feature_is_supported (
190 Lv2Plugin * self,
191 const char * uri)
192 {
193 if (string_is_equal (uri, LV2_CORE__isLive))
194 return true;
195
196 /* handled by lilv */
197 if (string_is_equal (uri, LV2_STATE__mapPath))
198 return true;
199
200 for (const LV2_Feature * const * f =
201 self->features; *f; ++f)
202 {
203 if (string_is_equal (uri, (*f)->URI))
204 {
205 return true;
206 }
207 }
208 return false;
209 }
210
211 static char *
get_port_group_label(const LilvNode * group)212 get_port_group_label (
213 const LilvNode * group)
214 {
215 LilvNode * group_label =
216 lilv_world_get (
217 LILV_WORLD, group,
218 PM_GET_NODE (LILV_NS_RDFS "label"),
219 NULL);
220 char * ret = NULL;
221 if (group_label)
222 {
223 const char * group_name_str =
224 lilv_node_as_string (group_label);
225 ret = g_strdup (group_name_str);
226 lilv_node_free (group_label);
227 }
228
229 return ret;
230 }
231
232 static bool
param_has_range(Lv2Plugin * plugin,const LilvNode * subject,const char * range_uri)233 param_has_range (
234 Lv2Plugin * plugin,
235 const LilvNode * subject,
236 const char * range_uri)
237 {
238 LilvNode * range =
239 lilv_new_uri (LILV_WORLD, range_uri);
240 const bool result =
241 lilv_world_ask (
242 LILV_WORLD, subject,
243 PM_GET_NODE (LILV_NS_RDFS "range"),
244 range);
245 lilv_node_free (range);
246
247 return result;
248 }
249
250 /**
251 * Create a port structure from data description.
252 *
253 * This is called before plugin and Jack
254 * instantiation. The remaining instance-specific
255 * setup (e.g. buffers) is done later in
256 * connect_port().
257 *
258 * @param param If non-null, this port is for a
259 * parameter (set via atom messages).
260 * @param port_exists If zrythm port exists (when
261 * loading)
262 *
263 * @return Non-zero if fail.
264 */
265 static Port *
create_port(Lv2Plugin * self,const uint32_t lv2_port_index,Lv2Parameter * param,const float default_value,const int port_exists)266 create_port (
267 Lv2Plugin * self,
268 const uint32_t lv2_port_index,
269 Lv2Parameter * param,
270 const float default_value,
271 const int port_exists)
272 {
273 Plugin * pl = self->plugin;
274 bool is_project =
275 lv2_plugin_is_in_active_project (self);
276 const LilvPlugin * lilv_plugin =
277 self->lilv_plugin;
278 LV2_Atom_Forge * forge =
279 &self->main_forge;
280 Port * port = NULL;
281 if (port_exists && is_project)
282 {
283 if (param)
284 {
285 port =
286 plugin_get_port_by_param_uri (
287 pl,
288 lv2_urid_unmap_uri (
289 self, param->urid));
290 }
291 else
292 {
293 const LilvPort * lilv_port =
294 lilv_plugin_get_port_by_index (
295 lilv_plugin, lv2_port_index);
296 const LilvNode * sym_node =
297 lilv_port_get_symbol (
298 lilv_plugin, lilv_port);
299 const char * sym =
300 lilv_node_as_string (sym_node);
301 port =
302 plugin_get_port_by_symbol (pl, sym);
303 }
304 g_return_val_if_fail (port, NULL);
305 }
306
307 if (param)
308 {
309 if (!port_exists || !is_project)
310 {
311 port =
312 port_new_with_type (
313 TYPE_CONTROL, FLOW_INPUT,
314 param->label);
315 }
316 PortIdentifier * pi = &port->id;
317
318 pi->uri =
319 g_strdup (
320 lv2_urid_unmap_uri (NULL, param->urid));
321 pi->flags |= PORT_FLAG_IS_PROPERTY;
322 pi->sym = g_strdup (param->symbol);
323 g_return_val_if_fail (
324 IS_PORT_AND_NONNULL (port), NULL);
325 port->value_type =
326 param->value_type_urid;
327 pi->comment = g_strdup (param->comment);
328
329 if (param->has_range)
330 {
331 port->minf = param->minf;
332 port->maxf = param->maxf;
333 port->deff = param->deff;
334 }
335
336 if (param->value_type_urid == forge->Bool)
337 {
338 pi->flags |= PORT_FLAG_TOGGLE;
339 }
340 if (param->value_type_urid == forge->Int ||
341 param->value_type_urid == forge->Long)
342 {
343 pi->flags |= PORT_FLAG_INTEGER;
344 }
345 if (param->value_type_urid == forge->URI)
346 {
347 pi->flags2 |= PORT_FLAG2_URI_PARAM;
348 }
349
350 if (param->value_type_urid == forge->Int ||
351 param->value_type_urid == forge->Long ||
352 param->value_type_urid == forge->Float ||
353 param->value_type_urid == forge->Double ||
354 param->value_type_urid == forge->Bool)
355 {
356 pi->flags |= PORT_FLAG_AUTOMATABLE;
357 }
358
359 port->lilv_port_index = -1;
360 }
361 else
362 {
363 const LilvPort * lilv_port =
364 lilv_plugin_get_port_by_index (
365 lilv_plugin, lv2_port_index);
366 const LilvNode * lilv_port_node =
367 lilv_port_get_node (
368 lilv_plugin, lilv_port);
369
370 LilvNode * def, * min, * max;
371 lilv_port_get_range (
372 lilv_plugin, lilv_port, &def, &min, &max);
373
374 #define PORT_IS_A(x) \
375 lilv_port_is_a ( \
376 lilv_plugin, lilv_port, PM_GET_NODE (x))
377
378 bool is_atom = PORT_IS_A (LV2_ATOM__AtomPort);
379 bool is_control =
380 PORT_IS_A (LV2_CORE__ControlPort);
381 bool is_input = PORT_IS_A (LV2_CORE__InputPort);
382 bool is_output =
383 PORT_IS_A (LV2_CORE__OutputPort);
384 bool is_audio = PORT_IS_A (LV2_CORE__AudioPort);
385 bool is_cv = PORT_IS_A (LV2_CORE__CVPort);
386 bool is_event = PORT_IS_A (LV2_EVENT__EventPort);
387
388 #undef PORT_IS_A
389
390 LilvNode * name_node =
391 lilv_port_get_name (
392 lilv_plugin, lilv_port);
393 const char * name_str =
394 lilv_node_as_string (name_node);
395 if (port_exists && is_project)
396 {
397 g_return_val_if_fail (
398 IS_PORT_AND_NONNULL (port), NULL);
399 port->buf =
400 g_realloc (
401 port->buf,
402 (size_t) AUDIO_ENGINE->block_length *
403 sizeof (float));
404 }
405 else
406 {
407 PortType type = 0;
408 PortFlow flow = 0;
409
410 /* Set the lv2_port flow (input or output) */
411 if (is_input)
412 {
413 flow = FLOW_INPUT;
414 }
415 else if (is_output)
416 {
417 flow = FLOW_OUTPUT;
418 }
419
420 /* Set type */
421 if (is_control)
422 {
423 type = TYPE_CONTROL;
424 }
425 else if (is_audio)
426 {
427 type = TYPE_AUDIO;
428 }
429 else if (is_cv)
430 {
431 type = TYPE_CV;
432 }
433 else if (is_event || is_atom)
434 {
435 type = TYPE_EVENT;
436 }
437
438 port =
439 port_new_with_type_and_owner (
440 type, flow, name_str,
441 PORT_OWNER_TYPE_PLUGIN, self->plugin);
442 }
443 lilv_node_free (name_node);
444
445 g_return_val_if_fail (
446 IS_PORT_AND_NONNULL (port), NULL);
447 port->evbuf = NULL;
448 port->lilv_port_index = (int) lv2_port_index;
449 port->control = 0.0f;
450 port->unsnapped_control = 0.0f;
451 port->value_type = forge->Float;
452
453 #define HAS_PROPERTY(x) \
454 lilv_port_has_property ( \
455 lilv_plugin, lilv_port, PM_GET_NODE (x))
456
457 const bool optional =
458 HAS_PROPERTY (LV2_CORE__connectionOptional);
459
460 PortIdentifier * pi = &port->id;
461
462 /* set the symbol */
463 const LilvNode * sym_node =
464 lilv_port_get_symbol (
465 lilv_plugin, lilv_port);
466 pi->sym =
467 g_strdup (lilv_node_as_string (sym_node));
468
469 /* set the comment */
470 LilvNode * comment =
471 lilv_world_get (
472 LILV_WORLD, lilv_port_node,
473 PM_GET_NODE (LILV_NS_RDFS "comment"),
474 NULL);
475 pi->comment =
476 g_strdup (lilv_node_as_string (comment));
477 lilv_node_free (comment);
478
479 /* Set the lv2_port flow (input or output) */
480 if (is_input)
481 {
482 pi->flow = FLOW_INPUT;
483 }
484 else if (is_output)
485 {
486 pi->flow = FLOW_OUTPUT;
487 }
488 else if (!optional)
489 {
490 g_warning (
491 "Mandatory lv2_port at %d has "
492 "unknown type"
493 " (neither input nor output)",
494 lv2_port_index);
495 return NULL;
496 }
497
498 /* Set control values */
499 if (is_control)
500 {
501 pi->type = TYPE_CONTROL;
502 port->control =
503 isnan (default_value) ?
504 0.0f : default_value;
505 port->unsnapped_control = port->control;
506
507 /* set port flags */
508 if (HAS_PROPERTY (
509 LV2_PORT_PROPS__trigger))
510 {
511 pi->flags |= PORT_FLAG_TRIGGER;
512 }
513 if (HAS_PROPERTY (
514 LV2_CORE__freeWheeling))
515 {
516 pi->flags |= PORT_FLAG_FREEWHEEL;
517 }
518 if (self->enabled_in ==
519 (int) lv2_port_index)
520 {
521 pi->flags |=
522 PORT_FLAG_PLUGIN_ENABLED;
523 pl->own_enabled_port = port;
524 }
525 if (HAS_PROPERTY (
526 LV2_CORE__reportsLatency))
527 {
528 pi->flags |=
529 PORT_FLAG_REPORTS_LATENCY;
530 }
531 if (HAS_PROPERTY (
532 LV2_PORT_PROPS__logarithmic))
533 {
534 pi->flags |= PORT_FLAG_LOGARITHMIC;
535 }
536 if (HAS_PROPERTY (LV2_CORE__enumeration))
537 {
538 pi->flags2 |= PORT_FLAG2_ENUMERATION;
539 }
540 if (HAS_PROPERTY (LV2_CORE__toggled))
541 {
542 pi->flags |= PORT_FLAG_TOGGLE;
543 }
544 else if (HAS_PROPERTY (LV2_CORE__integer))
545 {
546 pi->flags |= PORT_FLAG_INTEGER;
547 }
548
549 /* TODO enumeration / scale points */
550
551 if (pi->flow == FLOW_INPUT &&
552 !HAS_PROPERTY (
553 LV2_CORE__freeWheeling))
554 {
555 pi->flags |= PORT_FLAG_AUTOMATABLE;
556 }
557
558 port->maxf = 1.f;
559 port->minf = 0.f;
560 if (max)
561 {
562 port->maxf = lilv_node_as_float (max);
563 }
564 if (min)
565 {
566 port->minf =
567 lilv_node_as_float (min);
568 }
569 if (def)
570 {
571 port->deff =
572 lilv_node_as_float (def);
573 }
574 else
575 {
576 port->deff = port->minf;
577 }
578 port->zerof = port->minf;
579
580 #define FIND_OBJECTS(x) \
581 lilv_world_find_nodes ( \
582 LILV_WORLD, lilv_port_node, \
583 PM_GET_NODE (x), NULL)
584
585 /* set unit */
586 LilvNodes * units =
587 FIND_OBJECTS (LV2_UNITS__unit);
588
589 #define SET_UNIT(caps,sc) \
590 if (lilv_nodes_contains ( \
591 units, \
592 PM_GET_NODE (LV2_UNITS__##sc))) \
593 { \
594 port->id.unit = PORT_UNIT_##caps; \
595 }
596
597 SET_UNIT (DB, db);
598 SET_UNIT (DEGREES, degree);
599 SET_UNIT (HZ, hz);
600 SET_UNIT (MHZ, mhz);
601 SET_UNIT (MS, ms);
602 SET_UNIT (SECONDS, s);
603
604 #undef SET_UNIT
605
606 lilv_nodes_free (units);
607 }
608 else if (is_audio)
609 {
610 pi->type = TYPE_AUDIO;
611 }
612 else if (is_cv)
613 {
614 port->maxf = 1.f;
615 port->minf = -1.f;
616 if (max)
617 {
618 port->maxf =
619 lilv_node_as_float (max);
620 }
621 if (min)
622 {
623 port->minf =
624 lilv_node_as_float (min);
625 }
626 if (def)
627 {
628 port->deff =
629 lilv_node_as_float (def);
630 }
631 else
632 {
633 port->deff = port->minf;
634 }
635 port->zerof = 0.f;
636 pi->type = TYPE_CV;
637 }
638 else if (is_event)
639 {
640 pi->type = TYPE_EVENT;
641 pi->flags2 |= PORT_FLAG2_SUPPORTS_MIDI;
642 if (!port->midi_events)
643 {
644 port->midi_events = midi_events_new ();
645 }
646 port->old_api = true;
647 }
648 else if (is_atom)
649 {
650 pi->type = TYPE_EVENT;
651 if (!port->midi_events)
652 {
653 port->midi_events =
654 midi_events_new ();
655 }
656 port->old_api = false;
657
658 LilvNodes * buffer_types =
659 lilv_port_get_value (
660 lilv_plugin, lilv_port,
661 PM_GET_NODE (LV2_ATOM__bufferType));
662 LilvNodes* atom_supports =
663 lilv_port_get_value (
664 lilv_plugin, lilv_port,
665 PM_GET_NODE (LV2_ATOM__supports));
666
667 if (lilv_nodes_contains (
668 buffer_types,
669 PM_GET_NODE (LV2_ATOM__Sequence)))
670 {
671 pi->flags2 |= PORT_FLAG2_SEQUENCE;
672
673 if (lilv_nodes_contains (
674 atom_supports,
675 PM_GET_NODE (
676 LV2_TIME__Position)))
677 {
678 pi->flags |=
679 PORT_FLAG_WANT_POSITION;
680 self->want_position = true;
681 }
682 if (lilv_nodes_contains (
683 atom_supports,
684 PM_GET_NODE (
685 LV2_MIDI__MidiEvent)))
686 {
687 pi->flags2 |=
688 PORT_FLAG2_SUPPORTS_MIDI;
689 }
690 if (lilv_nodes_contains (
691 atom_supports,
692 PM_GET_NODE (
693 LV2_PATCH__Message)))
694 {
695 pi->flags2 |=
696 PORT_FLAG2_SUPPORTS_PATCH_MESSAGE;
697 }
698 }
699 lilv_nodes_free (buffer_types);
700 lilv_nodes_free (atom_supports);
701 }
702 else if (!optional)
703 {
704 g_warning (
705 "Mandatory lv2_port at %d has unknown "
706 "data type",
707 lv2_port_index);
708 return NULL;
709 }
710 pi->flags |= PORT_FLAG_PLUGIN_CONTROL;
711
712 if (HAS_PROPERTY (LV2_PORT_PROPS__notOnGUI))
713 {
714 pi->flags |= PORT_FLAG_NOT_ON_GUI;
715 }
716
717 if (HAS_PROPERTY (LV2_CORE__sampleRate))
718 {
719 /* Adjust range for lv2:sampleRate
720 * controls */
721 if (lilv_node_is_float (min) ||
722 lilv_node_is_int (min))
723 {
724 port->minf =
725 lilv_node_as_float (min) *
726 AUDIO_ENGINE->sample_rate;
727 }
728 if (lilv_node_is_float (max) ||
729 lilv_node_is_int (max))
730 {
731 port->maxf =
732 lilv_node_as_float (max) *
733 AUDIO_ENGINE->sample_rate;
734 }
735 }
736
737 /* Get scale points */
738 LilvScalePoints * sp =
739 lilv_port_get_scale_points (
740 lilv_plugin, lilv_port);
741 if (sp)
742 {
743 /* clear previous scale points */
744 for (int i = 0;
745 i < port->num_scale_points; i++)
746 {
747 object_free_w_func_and_null (
748 port_scale_point_free,
749 port->scale_points[i]);
750 }
751 port->num_scale_points = 0;
752
753 size_t num_scale_points =
754 lilv_scale_points_size (sp);
755 port->scale_points =
756 object_new_n (
757 num_scale_points, PortScalePoint *);
758
759 LILV_FOREACH (scale_points, s, sp)
760 {
761 const LilvScalePoint* p =
762 lilv_scale_points_get (sp, s);
763 if (lilv_node_is_float (
764 lilv_scale_point_get_value (
765 p)) ||
766 lilv_node_is_int (
767 lilv_scale_point_get_value (
768 p)))
769 {
770 port->scale_points[
771 port->num_scale_points++] =
772 port_scale_point_new (
773 lilv_node_as_float(
774 lilv_scale_point_get_value (
775 p)),
776 lilv_node_as_string (
777 lilv_scale_point_get_label(
778 p)));
779 }
780
781 /* TODO: Non-float scale points */
782 }
783
784 qsort (
785 port->scale_points,
786 (size_t) port->num_scale_points,
787 sizeof (PortScalePoint *),
788 port_scale_point_cmp);
789 lilv_scale_points_free (sp);
790 }
791
792 if (HAS_PROPERTY (LV2_CORE__isSideChain))
793 {
794 g_debug ("%s is sidechain", pi->label);
795 pi->flags |= PORT_FLAG_SIDECHAIN;
796 }
797 LilvNodes * groups =
798 lilv_port_get_value (
799 lilv_plugin, lilv_port,
800 PM_GET_NODE (
801 LV2_PORT_GROUPS__group));
802 if (lilv_nodes_size (groups) > 0)
803 {
804 const LilvNode * group =
805 lilv_nodes_get_first (groups);
806
807 /* get the name of the port group */
808 pi->port_group =
809 get_port_group_label (group);
810 g_debug (
811 "'%s' has port group '%s'",
812 pi->label, pi->port_group);
813
814 /* get sideChainOf */
815 LilvNode * side_chain_of =
816 lilv_world_get (
817 LILV_WORLD, group,
818 PM_GET_NODE (
819 LV2_PORT_GROUPS__sideChainOf),
820 NULL);
821 if (side_chain_of)
822 {
823 g_debug (
824 "'%s' is sidechain", pi->label);
825 lilv_node_free (side_chain_of);
826 pi->flags |= PORT_FLAG_SIDECHAIN;
827 }
828
829 /* get all designations we are interested
830 * in (e.g., "right") */
831 LilvNodes * designations =
832 lilv_port_get_value (
833 lilv_plugin, lilv_port,
834 PM_GET_NODE (LV2_CORE__designation));
835 /* get all pg:elements of the pg:group */
836 LilvNodes * group_childs =
837 lilv_world_find_nodes (
838 LILV_WORLD, group,
839 PM_GET_NODE (
840 LV2_PORT_GROUPS__element), NULL);
841 if (lilv_nodes_size (designations) > 0)
842 {
843 /* iterate over all port
844 * designations */
845 LILV_FOREACH (nodes, i, designations)
846 {
847 const LilvNode * designation =
848 lilv_nodes_get (
849 designations, i);
850 const char * designation_str =
851 lilv_node_as_uri (
852 designation);
853 g_debug (
854 "'%s' designation: <%s>",
855 pi->label, designation_str);
856 if (lilv_node_equals (
857 designation,
858 PM_GET_NODE (
859 LV2_PORT_GROUPS__left)))
860 {
861 pi->flags |=
862 PORT_FLAG_STEREO_L;
863 g_debug ("left port");
864 }
865 if (lilv_node_equals (
866 designation,
867 PM_GET_NODE (
868 LV2_PORT_GROUPS__right)))
869 {
870 pi->flags |=
871 PORT_FLAG_STEREO_R;
872 g_debug ("right port");
873 }
874
875 if (lilv_nodes_size (group_childs) >
876 0)
877 {
878 /* match the lv2:designation's
879 * element against the
880 * port-group's element */
881 LILV_FOREACH (
882 nodes, j, group_childs)
883 {
884 const LilvNode * group_el =
885 lilv_nodes_get (
886 group_childs, j);
887 LilvNodes * elem =
888 lilv_world_find_nodes (
889 LILV_WORLD, group_el,
890 PM_GET_NODE (
891 LV2_CORE__designation),
892 designation);
893
894 /*if found, look up the
895 * index (channel number of
896 * the pg:Element */
897 if (lilv_nodes_size (elem) >
898 0)
899 {
900 LilvNodes * idx =
901 lilv_world_find_nodes (
902 LILV_WORLD,
903 lilv_nodes_get_first (
904 elem),
905 PM_GET_NODE (
906 LV2_CORE__index),
907 NULL);
908 if (lilv_node_is_int (
909 lilv_nodes_get_first (idx)))
910 {
911 int group_channel =
912 lilv_node_as_int (
913 lilv_nodes_get_first (idx));
914 g_debug (
915 "group chan %d",
916 group_channel);
917 }
918 }
919 }
920 }
921 }
922 }
923 lilv_nodes_free (designations);
924 lilv_nodes_free (groups);
925 }
926
927 LilvNode * min_size =
928 lilv_port_get (
929 lilv_plugin, lilv_port,
930 PM_GET_NODE (
931 LV2_RESIZE_PORT__minimumSize));
932 if (min_size)
933 {
934 if (lilv_node_is_int (min_size))
935 {
936 port->min_buf_size =
937 (size_t) lilv_node_as_int (min_size);
938 }
939 lilv_node_free (min_size);
940 }
941
942 lilv_node_free (max);
943 lilv_node_free (min);
944 lilv_node_free (def);
945 } /* endif control port */
946
947 #undef HAS_PROPERTY
948
949 g_return_val_if_fail (
950 IS_PORT_AND_NONNULL (port), NULL);
951
952 return port;
953 }
954
955 void
lv2_plugin_init_loaded(Lv2Plugin * self)956 lv2_plugin_init_loaded (
957 Lv2Plugin * self)
958 {
959 self->magic = LV2_PLUGIN_MAGIC;
960 }
961
962 /**
963 * Comparator.
964 */
965 static int
param_port_cmp(const void * _a,const void * _b)966 param_port_cmp (
967 const void * _a,
968 const void * _b)
969 {
970 const Lv2Parameter * a =
971 (const Lv2Parameter *) _a;
972 const Lv2Parameter * b =
973 (const Lv2Parameter *) _b;
974 return (int) a->urid - (int) b->urid;
975 }
976
977 /**
978 * Returns all writable/readable parameters
979 * declared by the plugin.
980 */
981 static void
lv2_plugin_get_parameters(Lv2Plugin * self,GArray * params_array)982 lv2_plugin_get_parameters (
983 Lv2Plugin * self,
984 GArray * params_array)
985 {
986 const LilvNode* patch_writable =
987 PM_GET_NODE (LV2_PATCH__writable);
988 const LilvNode* patch_readable =
989 PM_GET_NODE (LV2_PATCH__readable);
990
991 for (int i = 0; i < 2; i++)
992 {
993 bool writable = i == 0;
994
995 /* get all parameters declared in the ttl
996 * (myplugin:myparameter a lv2:Parameter)
997 * marked as either patch:readable or
998 * patch:writable */
999 LilvNodes * properties =
1000 lilv_world_find_nodes (
1001 LILV_WORLD,
1002 lilv_plugin_get_uri (self->lilv_plugin),
1003 writable ? patch_writable : patch_readable,
1004 NULL);
1005 LILV_FOREACH (nodes, p, properties)
1006 {
1007 const LilvNode* property =
1008 lilv_nodes_get (properties, p);
1009 const char * property_uri =
1010 lilv_node_as_uri (property);
1011 LV2_URID property_urid =
1012 lv2_urid_map_uri (NULL, property_uri);
1013 g_message (
1014 "%s parameter found: <%s>",
1015 writable ? "writable" : "readable",
1016 property_uri);
1017
1018 bool found = false;
1019
1020 /* if searching for readable params and
1021 * existing writable parameter exists,
1022 * also mark it as readable and skip
1023 * creation */
1024 if (!writable &&
1025 lilv_world_ask (
1026 LILV_WORLD,
1027 lilv_plugin_get_uri (
1028 self->lilv_plugin),
1029 patch_writable,
1030 property))
1031 {
1032 for (guint j = 0;
1033 j < params_array->len; j++)
1034 {
1035 Lv2Parameter * param =
1036 &g_array_index (
1037 params_array, Lv2Parameter,
1038 j);
1039 LV2_URID cur_param_urid =
1040 param->urid;
1041 #if 0
1042 const char * cur_param_uri =
1043 lv2_urid_unmap_uri (
1044 NULL, cur_param_urid);
1045 g_message (
1046 "param URI %s", cur_param_uri);
1047 #endif
1048
1049 if (property_urid ==
1050 cur_param_urid)
1051 {
1052 found = true;
1053 param->readable = true;
1054
1055 /* break writable param
1056 * search */
1057 break;
1058 }
1059 }
1060
1061 if (found)
1062 {
1063 #if 0
1064 g_messsage (
1065 "existing writable param "
1066 "found for this property, "
1067 "skipping creation and "
1068 "continuing to next "
1069 "property (if any)");
1070 #endif
1071
1072 /* continue LILV_FOREACH */
1073 continue;
1074 }
1075 }
1076
1077 /* create new param */
1078 Lv2Parameter param;
1079 memset (¶m, 0, sizeof (Lv2Parameter));
1080 param.urid = property_urid;
1081 param.readable = !writable;
1082 param.writable = writable;
1083
1084 LilvNode * symbol =
1085 lilv_world_get_symbol (
1086 LILV_WORLD, property);
1087 const char * symbol_str =
1088 lilv_node_as_string (symbol);
1089 strncpy (
1090 param.symbol, symbol_str,
1091 MIN (
1092 strlen (symbol_str) + 1,
1093 LV2_PARAM_MAX_STR_LEN));
1094 lilv_node_free (symbol);
1095
1096 LilvNode * label =
1097 lilv_world_get (
1098 LILV_WORLD, property,
1099 PM_GET_NODE (LILV_NS_RDFS "label"),
1100 NULL);
1101 if (label)
1102 {
1103 const char * label_str =
1104 lilv_node_as_string (label);
1105 strncpy (
1106 param.label, label_str,
1107 MIN (
1108 strlen (label_str) + 1,
1109 LV2_PARAM_MAX_STR_LEN));
1110 lilv_node_free (label);
1111 }
1112
1113 LilvNode * comment =
1114 lilv_world_get (
1115 LILV_WORLD, property,
1116 PM_GET_NODE (LILV_NS_RDFS "comment"),
1117 NULL);
1118 if (comment)
1119 {
1120 const char * comment_str =
1121 lilv_node_as_string (comment);
1122 strncpy (
1123 param.comment, comment_str,
1124 MIN (
1125 strlen (comment_str) + 1,
1126 LV2_PARAM_MAX_STR_LEN));
1127 lilv_node_free (comment);
1128 }
1129
1130 LilvNode * min =
1131 lilv_world_get (
1132 LILV_WORLD, property,
1133 PM_GET_NODE (LV2_CORE__minimum),
1134 NULL);
1135 if (lilv_node_is_int (min) ||
1136 lilv_node_is_float (min))
1137 {
1138 param.has_range = true;
1139 param.minf = lilv_node_as_float (min);
1140 LilvNode * max =
1141 lilv_world_get (
1142 LILV_WORLD, property,
1143 PM_GET_NODE (LV2_CORE__maximum),
1144 NULL);
1145 param.maxf =
1146 lilv_node_as_float (max);
1147 lilv_node_free (max);
1148
1149 LilvNode * def =
1150 lilv_world_get (
1151 LILV_WORLD, property,
1152 PM_GET_NODE (LV2_CORE__default),
1153 NULL);
1154 param.deff =
1155 lilv_node_as_float (def);
1156 lilv_node_free (def);
1157 }
1158 else
1159 {
1160 param.has_range = false;
1161 }
1162 lilv_node_free (min);
1163
1164 const char* const types[] = {
1165 LV2_ATOM__Int,
1166 LV2_ATOM__Long,
1167 LV2_ATOM__Float,
1168 LV2_ATOM__Double,
1169 LV2_ATOM__Bool,
1170 LV2_ATOM__String,
1171 LV2_ATOM__Path,
1172 LV2_ATOM__URI,
1173 NULL
1174 };
1175
1176 for (const char *const * t = types; *t;
1177 t++)
1178 {
1179 if (param_has_range (
1180 self, property, *t))
1181 {
1182 param.value_type_urid =
1183 lv2_urid_map_uri (NULL, *t);
1184 break;
1185 }
1186 }
1187
1188 if (param.value_type_urid <= 0)
1189 {
1190 g_warning (
1191 "Unknown value type for property "
1192 "<%s>, skipping",
1193 lilv_node_as_string (property));
1194 continue;
1195 }
1196
1197 g_array_append_val (
1198 params_array, param);
1199 } /* end foreach property */
1200 lilv_nodes_free (properties);
1201 } /* end loop 2 */
1202
1203 /* sort parameters */
1204 g_array_sort (params_array, param_port_cmp);
1205 }
1206
1207 /**
1208 * Create port structures from data (via
1209 * create_port()) for all ports.
1210 */
1211 NONNULL
1212 static int
lv2_create_or_init_ports_and_parameters(Lv2Plugin * self)1213 lv2_create_or_init_ports_and_parameters (
1214 Lv2Plugin* self)
1215 {
1216 g_return_val_if_fail (
1217 IS_PLUGIN_AND_NONNULL (self->plugin) &&
1218 self->lilv_plugin, -1);
1219
1220 Plugin * pl = self->plugin;
1221
1222 /* zrythm ports exist when loading a
1223 * project since they are serialized */
1224 int ports_exist =
1225 /* excluding enabled/gain created by zrythm */
1226 pl->num_in_ports > 2 ||
1227 pl->num_out_ports > 0;
1228
1229 GArray * params_array =
1230 g_array_new (
1231 F_NOT_ZERO_TERMINATED, F_CLEAR,
1232 sizeof (Lv2Parameter));
1233 lv2_plugin_get_parameters (
1234 self, params_array);
1235
1236 int num_lilv_ports =
1237 (int)
1238 lilv_plugin_get_num_ports (self->lilv_plugin);
1239
1240 if (ports_exist)
1241 {
1242 #if 0
1243 /* TODO check number of ports is correct */
1244 g_warn_if_fail (
1245 pl->num_ports ==
1246 (int)
1247 /* lilv ports + params + zrythm provided
1248 * ports */
1249 num_lilv_ports + num_params + 2);
1250 #endif
1251 }
1252
1253 float * default_values =
1254 object_new_n ((size_t) num_lilv_ports, float);
1255 lilv_plugin_get_port_ranges_float (
1256 self->lilv_plugin,
1257 NULL, NULL, default_values);
1258
1259 /* set control input index */
1260 const LilvPort* control_input =
1261 lilv_plugin_get_port_by_designation (
1262 self->lilv_plugin,
1263 PM_GET_NODE (LV2_CORE__InputPort),
1264 PM_GET_NODE (LV2_CORE__control));
1265 if (control_input)
1266 {
1267 self->control_in =
1268 (int)
1269 lilv_port_get_index (
1270 self->lilv_plugin, control_input);
1271 }
1272
1273 pl->num_lilv_ports = num_lilv_ports;
1274 pl->lilv_ports =
1275 object_new_n ((size_t) num_lilv_ports, Port *);
1276
1277 /* set enabled port index */
1278 const LilvPort * enabled_port =
1279 lilv_plugin_get_port_by_designation (
1280 self->lilv_plugin,
1281 PM_GET_NODE (LV2_CORE__InputPort),
1282 PM_GET_NODE (LV2_CORE__enabled));
1283 if (enabled_port)
1284 {
1285 self->enabled_in =
1286 (int)
1287 lilv_port_get_index (
1288 self->lilv_plugin, enabled_port);
1289 }
1290
1291 /* create and add zrythm ports for lilv ports
1292 * and parameters */
1293 for (int i = 0;
1294 i < num_lilv_ports +
1295 (int) params_array->len;
1296 i++)
1297 {
1298 Lv2Parameter * param =
1299 &g_array_index (
1300 params_array, Lv2Parameter,
1301 i - num_lilv_ports);
1302 Port * port =
1303 create_port (
1304 self, (uint32_t) i,
1305 (i < num_lilv_ports) ?
1306 NULL : param,
1307 i < num_lilv_ports ?
1308 default_values[i] : 0, ports_exist);
1309 g_return_val_if_fail (
1310 IS_PORT_AND_NONNULL (port), -1);
1311
1312 if (i < num_lilv_ports)
1313 {
1314 pl->lilv_ports[i] = port;
1315 }
1316
1317 if (!ports_exist)
1318 {
1319 port->tmp_plugin = pl;
1320 if (port->id.flow == FLOW_INPUT)
1321 {
1322 plugin_add_in_port (pl, port);
1323 }
1324 else if (port->id.flow == FLOW_OUTPUT)
1325 {
1326 plugin_add_out_port (pl, port);
1327 }
1328 else
1329 {
1330 g_return_val_if_reached (-1);
1331 }
1332 }
1333 }
1334
1335 free (default_values);
1336 g_array_free (params_array, F_FREE);
1337
1338 return 0;
1339 }
1340
1341 /**
1342 * Allocate port buffers (only necessary for MIDI).
1343 */
1344 void
lv2_plugin_allocate_port_buffers(Lv2Plugin * lv2_plugin)1345 lv2_plugin_allocate_port_buffers (
1346 Lv2Plugin * lv2_plugin)
1347 {
1348 Plugin * pl = lv2_plugin->plugin;
1349 g_return_if_fail (pl);
1350 for (int i = 0; i < pl->num_lilv_ports; ++i)
1351 {
1352 Port * port = pl->lilv_ports[i];
1353
1354 if (port->id.type != TYPE_EVENT)
1355 continue;
1356
1357 g_debug (
1358 "allocating event buf for port %s",
1359 port->id.sym);
1360
1361 object_free_w_func_and_null (
1362 lv2_evbuf_free, port->evbuf);
1363 const size_t buf_size =
1364 MAX (
1365 port->min_buf_size,
1366 AUDIO_ENGINE->midi_buf_size);
1367 g_return_if_fail (
1368 buf_size > 0 && lv2_plugin->map.map);
1369 g_debug ("buf size: %zu", buf_size);
1370
1371 port->evbuf =
1372 lv2_evbuf_new (
1373 (uint32_t) buf_size,
1374 lv2_urid_map_uri (
1375 lv2_plugin, LV2_ATOM__Chunk),
1376 lv2_urid_map_uri (
1377 lv2_plugin, LV2_ATOM__Sequence));
1378 g_return_if_fail (port->evbuf);
1379
1380 /* reset the evbuf - needed if output since
1381 * it is reset after the run cycle in
1382 * lv2_plugin_process() */
1383 lv2_evbuf_reset (
1384 port->evbuf, port->id.flow == FLOW_INPUT);
1385
1386 lilv_instance_connect_port (
1387 lv2_plugin->instance,
1388 (uint32_t) i,
1389 lv2_evbuf_get_buffer (port->evbuf));
1390 }
1391 }
1392
1393 /**
1394 * Returns the property port matching the given
1395 * property URID.
1396 */
1397 Port *
lv2_plugin_get_property_port(Lv2Plugin * self,LV2_URID property)1398 lv2_plugin_get_property_port (
1399 Lv2Plugin * self,
1400 LV2_URID property)
1401 {
1402 Plugin * pl = self->plugin;
1403 for (int i = 0; i < pl->num_in_ports; i++)
1404 {
1405 Port * port = pl->in_ports[i];
1406 if (port->id.type != TYPE_CONTROL ||
1407 !(port->id.flags &
1408 PORT_FLAG_IS_PROPERTY))
1409 continue;
1410
1411 if (lv2_urid_map_uri (self, port->id.uri) ==
1412 property)
1413 {
1414 return port;
1415 }
1416 }
1417
1418 return NULL;
1419 }
1420
1421 /**
1422 * Function to get a port value.
1423 *
1424 * Used when saving the state.
1425 * This function MUST set size and type
1426 * appropriately.
1427 */
1428 const void *
lv2_plugin_get_port_value(const char * port_sym,void * user_data,uint32_t * size,uint32_t * type)1429 lv2_plugin_get_port_value (
1430 const char * port_sym,
1431 void * user_data,
1432 uint32_t * size,
1433 uint32_t * type)
1434 {
1435 Lv2Plugin * lv2_plugin = (Lv2Plugin *) user_data;
1436
1437 Plugin * pl = lv2_plugin->plugin;
1438 g_return_val_if_fail (
1439 IS_PLUGIN_AND_NONNULL (pl), NULL);
1440
1441 Port * port =
1442 plugin_get_port_by_symbol (pl, port_sym);
1443 *size = 0;
1444 *type = 0;
1445
1446 if (port)
1447 {
1448 *size = sizeof (float);
1449 *type = PM_URIDS.atom_Float;
1450 return (const void *) &port->control;
1451 }
1452
1453 return NULL;
1454 }
1455
1456 /**
1457 * Runs the plugin for this cycle.
1458 */
1459 static int
run(Lv2Plugin * plugin,const nframes_t nframes)1460 run (
1461 Lv2Plugin* plugin,
1462 const nframes_t nframes)
1463 {
1464 /* Read and apply control change events from UI */
1465 if (plugin->plugin->visible)
1466 lv2_ui_read_and_apply_events (plugin, nframes);
1467
1468 /* Run plugin for this cycle */
1469 lilv_instance_run (plugin->instance, nframes);
1470
1471 /* Process any worker replies. */
1472 lv2_worker_emit_responses (
1473 &plugin->state_worker, plugin->instance);
1474 lv2_worker_emit_responses (
1475 &plugin->worker, plugin->instance);
1476
1477 /* Notify the plugin the run() cycle is
1478 * finished */
1479 if (plugin->worker.iface &&
1480 plugin->worker.iface->end_run)
1481 {
1482 plugin->worker.iface->end_run (
1483 plugin->instance->lv2_handle);
1484 }
1485
1486 bool have_custom_ui =
1487 !plugin->plugin->setting->force_generic_ui;
1488
1489 /* Check if it's time to send updates to the UI */
1490 plugin->event_delta_t += nframes;
1491 bool send_ui_updates = false;
1492 uint32_t update_frames =
1493 (uint32_t)
1494 ((float) AUDIO_ENGINE->sample_rate /
1495 plugin->plugin->ui_update_hz);
1496 if (have_custom_ui &&
1497 plugin->plugin->visible &&
1498 (plugin->event_delta_t > update_frames))
1499 {
1500 send_ui_updates = true;
1501 plugin->event_delta_t = 0;
1502 }
1503
1504 return send_ui_updates;
1505 }
1506
1507 /**
1508 * Connect the port to its buffer.
1509 */
1510 NONNULL
1511 static void
connect_port(Lv2Plugin * lv2_plugin,uint32_t port_index)1512 connect_port (
1513 Lv2Plugin * lv2_plugin,
1514 uint32_t port_index)
1515 {
1516 Plugin * pl = lv2_plugin->plugin;
1517 g_return_if_fail (IS_PLUGIN_AND_NONNULL (pl));
1518
1519 g_return_if_fail (
1520 (int) port_index < pl->num_lilv_ports);
1521 Port * port = pl->lilv_ports[port_index];
1522
1523 /* Connect unsupported ports to NULL (known to
1524 * be optional by this point) */
1525 if (port->id.flow == FLOW_UNKNOWN ||
1526 port->id.type == TYPE_UNKNOWN)
1527 {
1528 lilv_instance_connect_port(
1529 lv2_plugin->instance,
1530 port_index, NULL);
1531 return;
1532 }
1533
1534 /* Connect the port based on its type */
1535 switch (port->id.type)
1536 {
1537 case TYPE_CONTROL:
1538 lilv_instance_connect_port (
1539 lv2_plugin->instance,
1540 port_index, &port->control);
1541 break;
1542 case TYPE_AUDIO:
1543 case TYPE_CV:
1544 /* connect lv2 ports to plugin port
1545 * buffers */
1546 lilv_instance_connect_port (
1547 lv2_plugin->instance,
1548 port_index, port->buf);
1549 break;
1550 case TYPE_EVENT:
1551 /* already connected to port */
1552 g_return_if_fail (port->evbuf);
1553 break;
1554 default:
1555 break;
1556 }
1557 }
1558
1559 /**
1560 * Initializes the plugin features.
1561 *
1562 * This is called on instantiation.
1563 */
1564 static void
set_features(Lv2Plugin * self)1565 set_features (
1566 Lv2Plugin * self)
1567 {
1568 #define INIT_FEATURE(x,uri,_data) \
1569 self->x.URI = uri; \
1570 self->x.data = _data
1571
1572 self->ext_data_feature.data_access = NULL;
1573
1574 INIT_FEATURE (
1575 map_feature, LV2_URID__map, &self->map);
1576 INIT_FEATURE (
1577 unmap_feature, LV2_URID__unmap, &self->unmap);
1578 INIT_FEATURE (
1579 make_path_temp_feature, LV2_STATE__makePath,
1580 &self->make_path_temp);
1581 INIT_FEATURE (
1582 sched_feature, LV2_WORKER__schedule,
1583 &self->sched);
1584 INIT_FEATURE (
1585 state_sched_feature, LV2_WORKER__schedule,
1586 &self->ssched);
1587 INIT_FEATURE (
1588 safe_restore_feature,
1589 LV2_STATE__threadSafeRestore, NULL);
1590 INIT_FEATURE (
1591 log_feature, LV2_LOG__log, &self->llog);
1592 INIT_FEATURE (
1593 options_feature, LV2_OPTIONS__options,
1594 self->options);
1595 INIT_FEATURE (
1596 def_state_feature, LV2_STATE__loadDefaultState,
1597 NULL);
1598 INIT_FEATURE (
1599 hard_rt_capable_feature,
1600 LV2_CORE__hardRTCapable, NULL);
1601 INIT_FEATURE (
1602 data_access_feature,
1603 LV2_DATA_ACCESS_URI, NULL);
1604 INIT_FEATURE (
1605 instance_access_feature,
1606 LV2_INSTANCE_ACCESS_URI, NULL);
1607 INIT_FEATURE (
1608 bounded_block_length_feature,
1609 LV2_BUF_SIZE__boundedBlockLength, NULL);
1610
1611 #undef INIT_FEATURE
1612
1613 self->features[0] = &self->map_feature;
1614 self->features[1] = &self->unmap_feature;
1615 self->features[2] = &self->sched_feature;
1616 self->features[3] = &self->log_feature;
1617 self->features[4] = &self->options_feature;
1618 self->features[5] = &self->def_state_feature;
1619 self->features[6] = &self->make_path_temp_feature;
1620 self->features[7] =
1621 &self->safe_restore_feature;
1622 self->features[8] =
1623 &self->hard_rt_capable_feature;
1624 self->features[9] =
1625 &self->data_access_feature;
1626 self->features[10] =
1627 &self->instance_access_feature;
1628 self->features[11] =
1629 &self->bounded_block_length_feature;
1630 self->features[12] = NULL;
1631
1632 self->state_features[0] =
1633 &self->map_feature;
1634 self->state_features[1] =
1635 &self->unmap_feature;
1636 self->state_features[2] =
1637 &self->state_sched_feature;
1638 self->state_features[3] =
1639 &self->safe_restore_feature;
1640 self->state_features[4] =
1641 &self->log_feature;
1642 self->state_features[5] =
1643 &self->options_feature;
1644 self->state_features[6] = NULL;
1645 }
1646
1647 /**
1648 * Returns whether the plugin contains a port
1649 * that reports latency.
1650 */
1651 NONNULL
1652 static bool
reports_latency(Lv2Plugin * self)1653 reports_latency (
1654 Lv2Plugin * self)
1655 {
1656 Plugin * pl = self->plugin;
1657 g_return_val_if_fail (
1658 IS_PLUGIN_AND_NONNULL (pl), false);
1659
1660 if (pl->instantiation_failed)
1661 {
1662 /* ignore */
1663 return false;
1664 }
1665
1666 for (int i = 0; i < pl->num_lilv_ports; i++)
1667 {
1668 Port * port = pl->lilv_ports[i];
1669
1670 if (port->id.flags &
1671 PORT_FLAG_REPORTS_LATENCY)
1672 {
1673 return true;
1674 }
1675 }
1676
1677 return false;
1678 }
1679
1680 /**
1681 * Returns the plugin's latency in samples.
1682 *
1683 * Searches for a port marked as reportsLatency
1684 * and gets it value when a 0-size buffer is
1685 * passed.
1686 */
1687 nframes_t
lv2_plugin_get_latency(Lv2Plugin * self)1688 lv2_plugin_get_latency (
1689 Lv2Plugin * self)
1690 {
1691 if (reports_latency (self))
1692 {
1693 lv2_plugin_process (
1694 self, PLAYHEAD->frames, 0, 0);
1695 /*AUDIO_ENGINE->block_length);*/
1696 }
1697
1698 return self->plugin->latency;
1699 }
1700
1701 /**
1702 * Returns a newly allocated plugin descriptor for
1703 * the given LilvPlugin
1704 * if it can be hosted, otherwise NULL.
1705 */
1706 PluginDescriptor *
lv2_plugin_create_descriptor_from_lilv(const LilvPlugin * lp)1707 lv2_plugin_create_descriptor_from_lilv (
1708 const LilvPlugin * lp)
1709 {
1710 const LilvNode* lv2_uri =
1711 lilv_plugin_get_uri (lp);
1712 const char * uri_str =
1713 lilv_node_as_string (lv2_uri);
1714 if (!string_is_ascii (uri_str))
1715 {
1716 g_warning ("Invalid plugin URI (not ascii),"
1717 " skipping");
1718 return NULL;
1719 }
1720
1721 LilvNode* name =
1722 lilv_plugin_get_name(lp);
1723
1724 /* check if we can host the Plugin */
1725 if (!lv2_uri)
1726 {
1727 g_warning ("Plugin URI not found, try lv2ls "
1728 "to get a list of all plugins");
1729 lilv_node_free (name);
1730 return NULL;
1731 }
1732 if (!name ||
1733 !lilv_plugin_get_port_by_index(lp, 0))
1734 {
1735 g_message (
1736 "Ignoring invalid LV2 plugin %s",
1737 lilv_node_as_string (
1738 lilv_plugin_get_uri(lp)));
1739 lilv_node_free(name);
1740 return NULL;
1741 }
1742
1743 /* check if shared lib exists */
1744 const LilvNode * lib_uri =
1745 lilv_plugin_get_library_uri (lp);
1746 char * path =
1747 lilv_file_uri_parse (
1748 lilv_node_as_string (lib_uri),
1749 NULL);
1750 if (access (path, F_OK) == -1)
1751 {
1752 g_warning ("%s not found, skipping %s",
1753 path, lilv_node_as_string (name));
1754 lilv_node_free(name);
1755 return NULL;
1756 }
1757 lilv_free (path);
1758
1759 if (lilv_plugin_has_feature (
1760 lp,
1761 PM_GET_NODE (LV2_CORE__inPlaceBroken)))
1762 {
1763 g_warning ("Ignoring LV2 plugin \"%s\" "
1764 "since it cannot do inplace "
1765 "processing.",
1766 lilv_node_as_string(name));
1767 lilv_node_free(name);
1768 return NULL;
1769 }
1770
1771 /* Zrythm splits the processing cycle so
1772 * powerof2 and fixed block length are not
1773 * supported */
1774 LilvNodes *required_features =
1775 lilv_plugin_get_required_features (lp);
1776 if (lilv_nodes_contains (
1777 required_features,
1778 PM_GET_NODE (
1779 LV2_BUF_SIZE__powerOf2BlockLength)))
1780 {
1781 g_message (
1782 "Ignoring LV2 Plugin %s because "
1783 "it requires a power of 2 block length",
1784 lilv_node_as_string (name));
1785 lilv_nodes_free (required_features);
1786 lilv_node_free (name);
1787 return NULL;
1788 }
1789 if (lilv_nodes_contains (
1790 required_features,
1791 PM_GET_NODE (
1792 LV2_BUF_SIZE__fixedBlockLength)))
1793 {
1794 g_message (
1795 "Ignoring LV2 Plugin %s because "
1796 "it requires fixed block length",
1797 lilv_node_as_string (name));
1798 lilv_nodes_free (required_features);
1799 lilv_node_free (name);
1800 return NULL;
1801 }
1802 lilv_nodes_free (required_features);
1803
1804 /* set descriptor info */
1805 PluginDescriptor * pd = plugin_descriptor_new ();
1806 pd->protocol = PROT_LV2;
1807 pd->arch = ARCH_64;
1808 const char * str = lilv_node_as_string (name);
1809 pd->name = g_strdup (str);
1810 lilv_node_free (name);
1811 LilvNode * author =
1812 lilv_plugin_get_author_name (lp);
1813 str = lilv_node_as_string (author);
1814 pd->author = g_strdup (str);
1815 lilv_node_free (author);
1816 LilvNode * website =
1817 lilv_plugin_get_author_homepage (lp);
1818 str = lilv_node_as_string (website);
1819 pd->website = g_strdup (str);
1820 lilv_node_free (website);
1821 const LilvPluginClass* pclass =
1822 lilv_plugin_get_class(lp);
1823 const LilvNode * label =
1824 lilv_plugin_class_get_label(pclass);
1825 str = lilv_node_as_string (label);
1826 if (str)
1827 {
1828 pd->category_str =
1829 string_get_substr_before_suffix (
1830 str, " Plugin");
1831 }
1832 else
1833 {
1834 pd->category_str = g_strdup ("Plugin");
1835 }
1836
1837 pd->category =
1838 plugin_descriptor_string_to_category (
1839 pd->category_str);
1840
1841 /* count atom-event-ports that feature
1842 * atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent>
1843 */
1844 int count_midi_out = 0;
1845 int count_midi_in = 0;
1846 for (uint32_t i = 0;
1847 i < lilv_plugin_get_num_ports (lp);
1848 ++i)
1849 {
1850 const LilvPort* port =
1851 lilv_plugin_get_port_by_index (lp, i);
1852 if (lilv_port_is_a (
1853 lp, port,
1854 PM_GET_NODE (LV2_ATOM__AtomPort)))
1855 {
1856 LilvNodes* buffer_types =
1857 lilv_port_get_value (
1858 lp, port,
1859 PM_GET_NODE (LV2_ATOM__bufferType));
1860 LilvNodes* atom_supports =
1861 lilv_port_get_value (
1862 lp, port,
1863 PM_GET_NODE (LV2_ATOM__supports));
1864
1865 if (lilv_nodes_contains (
1866 buffer_types,
1867 PM_GET_NODE (
1868 LV2_ATOM__Sequence)) &&
1869 lilv_nodes_contains (
1870 atom_supports,
1871 PM_GET_NODE (LV2_MIDI__MidiEvent)))
1872 {
1873 if (lilv_port_is_a (
1874 lp, port,
1875 PM_GET_NODE (
1876 LV2_CORE__InputPort)))
1877 {
1878 count_midi_in++;
1879 }
1880 if (lilv_port_is_a (
1881 lp, port,
1882 PM_GET_NODE (
1883 LV2_CORE__OutputPort)))
1884 {
1885 count_midi_out++;
1886 }
1887 }
1888 lilv_nodes_free(buffer_types);
1889 lilv_nodes_free(atom_supports);
1890 }
1891 }
1892
1893 pd->num_audio_ins =
1894 (int)
1895 lilv_plugin_get_num_ports_of_class (
1896 lp, PM_GET_NODE (LV2_CORE__InputPort),
1897 PM_GET_NODE (LV2_CORE__AudioPort), NULL);
1898 pd->num_midi_ins =
1899 (int)
1900 lilv_plugin_get_num_ports_of_class (
1901 lp, PM_GET_NODE (LV2_CORE__InputPort),
1902 PM_GET_NODE (LV2_EVENT__EventPort), NULL) +
1903 count_midi_in;
1904 pd->num_audio_outs =
1905 (int)
1906 lilv_plugin_get_num_ports_of_class (
1907 lp, PM_GET_NODE (LV2_CORE__OutputPort),
1908 PM_GET_NODE (LV2_CORE__AudioPort), NULL);
1909 pd->num_midi_outs =
1910 (int)
1911 lilv_plugin_get_num_ports_of_class(
1912 lp, PM_GET_NODE (LV2_CORE__OutputPort),
1913 PM_GET_NODE (LV2_EVENT__EventPort), NULL) +
1914 count_midi_out;
1915 pd->num_ctrl_ins =
1916 (int)
1917 lilv_plugin_get_num_ports_of_class (
1918 lp, PM_GET_NODE (LV2_CORE__InputPort),
1919 PM_GET_NODE (LV2_CORE__ControlPort), NULL);
1920 pd->num_ctrl_outs =
1921 (int)
1922 lilv_plugin_get_num_ports_of_class (
1923 lp, PM_GET_NODE (LV2_CORE__OutputPort),
1924 PM_GET_NODE (LV2_CORE__ControlPort), NULL);
1925 pd->num_cv_ins =
1926 (int)
1927 lilv_plugin_get_num_ports_of_class (
1928 lp, PM_GET_NODE (LV2_CORE__InputPort),
1929 PM_GET_NODE (LV2_CORE__CVPort), NULL);
1930 pd->num_cv_outs =
1931 (int)
1932 lilv_plugin_get_num_ports_of_class (
1933 lp, PM_GET_NODE (LV2_CORE__OutputPort),
1934 PM_GET_NODE (LV2_CORE__CVPort), NULL);
1935
1936 pd->uri = g_strdup (uri_str);
1937 pd->min_bridge_mode =
1938 plugin_descriptor_get_min_bridge_mode (pd);
1939 pd->has_custom_ui =
1940 plugin_descriptor_has_custom_ui (pd);
1941
1942 return pd;
1943 }
1944
1945 /**
1946 * Creates an LV2 plugin from given uri.
1947 *
1948 * Note that this does not instantiate the plugin.
1949 * For instantiating the plugin using a preset or
1950 * state file, see lv2_plugin_instantiate.
1951 *
1952 * @param pl A newly created Plugin with its
1953 * descriptor filled in.
1954 * @param uri The URI.
1955 */
1956 Lv2Plugin *
lv2_plugin_new_from_uri(Plugin * pl,const char * uri,GError ** error)1957 lv2_plugin_new_from_uri (
1958 Plugin * pl,
1959 const char * uri,
1960 GError ** error)
1961 {
1962 g_message ("Creating from uri: %s...", uri);
1963
1964 LilvNode * lv2_uri =
1965 lilv_new_uri (LILV_WORLD, uri);
1966 const LilvPlugin * lilv_plugin =
1967 lilv_plugins_get_by_uri (
1968 LILV_PLUGINS, lv2_uri);
1969 lilv_node_free (lv2_uri);
1970
1971 if (!lilv_plugin)
1972 {
1973 g_set_error (
1974 error, Z_PLUGINS_LV2_PLUGIN_ERROR,
1975 Z_PLUGINS_LV2_PLUGIN_ERROR_CREATION_FAILED,
1976 _("Failed to get LV2 plugin from URI "
1977 "<%s>"),
1978 uri);
1979 return NULL;
1980 }
1981 Lv2Plugin * lv2_plugin = lv2_plugin_new (pl);
1982
1983 lv2_plugin->lilv_plugin = lilv_plugin;
1984
1985 g_message ("done");
1986
1987 return lv2_plugin;
1988 }
1989
1990 /**
1991 * Creates a new LV2 plugin using the given Plugin
1992 * instance.
1993 *
1994 * The given plugin instance must be a newly
1995 * allocated one.
1996 *
1997 * @param plugin A newly allocated Plugin instance.
1998 */
1999 Lv2Plugin *
lv2_plugin_new(Plugin * plugin)2000 lv2_plugin_new (
2001 Plugin * plugin)
2002 {
2003 Lv2Plugin * self = object_new (Lv2Plugin);
2004
2005 self->magic = LV2_PLUGIN_MAGIC;
2006
2007 /* set pointers to each other */
2008 self->plugin = plugin;
2009 plugin->lv2 = self;
2010
2011 return self;
2012 }
2013
2014 bool
lv2_plugin_ui_type_is_external(const LilvNode * ui_type)2015 lv2_plugin_ui_type_is_external (
2016 const LilvNode * ui_type)
2017 {
2018 return
2019 lilv_node_equals (
2020 ui_type,
2021 PM_GET_NODE (LV2_KX__externalUi));
2022 }
2023
2024 /**
2025 * Returns whether the plugin has a custom UI that
2026 * is deprecated (GtkUI, QtUI, etc.).
2027 *
2028 * @return If the plugin has a deprecated UI,
2029 * returns the UI URI, otherwise NULL.
2030 */
2031 char *
lv2_plugin_has_deprecated_ui(const char * uri)2032 lv2_plugin_has_deprecated_ui (
2033 const char * uri)
2034 {
2035 LilvNode * lv2_uri =
2036 lilv_new_uri (LILV_WORLD, uri);
2037 const LilvPlugin * lilv_plugin =
2038 lilv_plugins_get_by_uri (
2039 LILV_PLUGINS, lv2_uri);
2040 lilv_node_free (lv2_uri);
2041 const LilvUI * ui;
2042 const LilvNode * ui_type;
2043 LilvUIs * uis =
2044 lilv_plugin_get_uis (lilv_plugin);
2045 bool ui_picked =
2046 lv2_plugin_pick_ui (
2047 uis, LV2_PLUGIN_UI_WRAPPABLE,
2048 &ui, &ui_type);
2049 if (!ui_picked)
2050 {
2051 ui_picked =
2052 lv2_plugin_pick_ui (
2053 uis, LV2_PLUGIN_UI_EXTERNAL,
2054 &ui, &ui_type);
2055 }
2056 if (!ui_picked)
2057 {
2058 ui_picked =
2059 lv2_plugin_pick_ui (
2060 uis, LV2_PLUGIN_UI_FOR_BRIDGING,
2061 &ui, &ui_type);
2062 }
2063 if (!ui_picked)
2064 {
2065 return false;
2066 }
2067
2068 const char * ui_type_str =
2069 lilv_node_as_string (ui_type);
2070 char * ret = NULL;
2071 if (lilv_node_equals (
2072 ui_type, PM_GET_NODE (LV2_UI__GtkUI)) ||
2073 lilv_node_equals (
2074 ui_type, PM_GET_NODE (LV2_UI__Gtk3UI)) ||
2075 lilv_node_equals (
2076 ui_type, PM_GET_NODE (LV2_UI__Qt5UI)) ||
2077 lilv_node_equals (
2078 ui_type, PM_GET_NODE (LV2_UI__Qt4UI)))
2079 {
2080 ret = g_strdup (ui_type_str);
2081 }
2082 lilv_uis_free (uis);
2083
2084 return ret;
2085 }
2086
2087 /**
2088 * Returns whether the given UI uri is supported
2089 * for non-bridged plugins.
2090 */
2091 bool
lv2_plugin_is_ui_supported(const char * pl_uri,const char * ui_uri)2092 lv2_plugin_is_ui_supported (
2093 const char * pl_uri,
2094 const char * ui_uri)
2095 {
2096 char * ui_class =
2097 lv2_plugin_get_ui_class (
2098 pl_uri, ui_uri);
2099 if (!ui_class)
2100 {
2101 g_warning (
2102 "failed to get UI class for <%s>:<%s>",
2103 pl_uri, ui_uri);
2104 return false;
2105 }
2106
2107 if (suil_ui_supported (
2108 LV2_UI__Gtk3UI, ui_class) ||
2109 string_is_equal (
2110 ui_class, LV2_KX__externalUi))
2111 {
2112 g_free (ui_class);
2113 return true;
2114 }
2115
2116 return false;
2117 }
2118
2119 /**
2120 * Returns the UI URIs that this plugin has.
2121 */
2122 void
lv2_plugin_get_uis(const char * pl_uri,char ** uris,int * num_uris)2123 lv2_plugin_get_uis (
2124 const char * pl_uri,
2125 char ** uris,
2126 int * num_uris)
2127 {
2128 *num_uris = 0;
2129
2130 LilvNode * lv2_uri =
2131 lilv_new_uri (LILV_WORLD, pl_uri);
2132 const LilvPlugin * lilv_plugin =
2133 lilv_plugins_get_by_uri (
2134 LILV_PLUGINS, lv2_uri);
2135 lilv_node_free (lv2_uri);
2136 LilvUIs * uis =
2137 lilv_plugin_get_uis (lilv_plugin);
2138 LILV_FOREACH (uis, u, uis)
2139 {
2140 const LilvUI * cur_ui =
2141 lilv_uis_get (uis, u);
2142 const LilvNode * ui_uri =
2143 lilv_ui_get_uri (cur_ui);
2144 uris[(*num_uris)++] =
2145 g_strdup (lilv_node_as_uri (ui_uri));
2146 }
2147
2148 lilv_uis_free (uis);
2149 }
2150
2151 char *
lv2_plugin_get_ui_class(const char * pl_uri,const char * ui_uri)2152 lv2_plugin_get_ui_class (
2153 const char * pl_uri,
2154 const char * ui_uri)
2155 {
2156 LilvNode * lv2_uri =
2157 lilv_new_uri (LILV_WORLD, pl_uri);
2158 const LilvPlugin * lilv_plugin =
2159 lilv_plugins_get_by_uri (
2160 LILV_PLUGINS, lv2_uri);
2161 lilv_node_free (lv2_uri);
2162 g_return_val_if_fail (lilv_plugin, NULL);
2163 LilvUIs * uis =
2164 lilv_plugin_get_uis (lilv_plugin);
2165 LilvNode * ui_uri_node =
2166 lilv_new_uri (LILV_WORLD, ui_uri);
2167 const LilvUI * ui =
2168 lilv_uis_get_by_uri (uis, ui_uri_node);
2169
2170 const LilvNodes * ui_classes =
2171 lilv_ui_get_classes (ui);
2172 char * ui_type_uri = NULL;
2173 LILV_FOREACH (nodes, t, ui_classes)
2174 {
2175 const LilvNode * ui_type =
2176 lilv_nodes_get (ui_classes, t);
2177 ui_type_uri =
2178 g_strdup (
2179 lilv_node_as_uri (ui_type));
2180 break;
2181 }
2182
2183 lilv_node_free (ui_uri_node);
2184 lilv_uis_free (uis);
2185
2186 return ui_type_uri;
2187 }
2188
2189 /**
2190 * Returns the bundle path of the UI as a URI.
2191 */
2192 char *
lv2_plugin_get_ui_bundle_uri(const char * pl_uri,const char * ui_uri)2193 lv2_plugin_get_ui_bundle_uri (
2194 const char * pl_uri,
2195 const char * ui_uri)
2196 {
2197 LilvNode * lv2_uri =
2198 lilv_new_uri (LILV_WORLD, pl_uri);
2199 const LilvPlugin * lilv_plugin =
2200 lilv_plugins_get_by_uri (
2201 LILV_PLUGINS, lv2_uri);
2202 lilv_node_free (lv2_uri);
2203 LilvUIs * uis =
2204 lilv_plugin_get_uis (lilv_plugin);
2205 LilvNode * ui_uri_node =
2206 lilv_new_uri (LILV_WORLD, ui_uri);
2207 g_return_val_if_fail (ui_uri_node, NULL);
2208 const LilvUI * ui =
2209 lilv_uis_get_by_uri (uis, ui_uri_node);
2210
2211 const LilvNode * bundle_uri_node =
2212 lilv_ui_get_bundle_uri (ui);
2213 char * bundle_uri =
2214 g_strdup (
2215 lilv_node_as_uri (bundle_uri_node));
2216
2217 lilv_node_free (ui_uri_node);
2218 lilv_uis_free (uis);
2219
2220 return bundle_uri;
2221 }
2222
2223 /**
2224 * Returns the binary path of the UI as a URI.
2225 */
2226 char *
lv2_plugin_get_ui_binary_uri(const char * pl_uri,const char * ui_uri)2227 lv2_plugin_get_ui_binary_uri (
2228 const char * pl_uri,
2229 const char * ui_uri)
2230 {
2231 LilvNode * lv2_uri =
2232 lilv_new_uri (LILV_WORLD, pl_uri);
2233 const LilvPlugin * lilv_plugin =
2234 lilv_plugins_get_by_uri (
2235 LILV_PLUGINS, lv2_uri);
2236 lilv_node_free (lv2_uri);
2237 LilvUIs * uis =
2238 lilv_plugin_get_uis (lilv_plugin);
2239 LilvNode * ui_uri_node =
2240 lilv_new_uri (LILV_WORLD, ui_uri);
2241 g_return_val_if_fail (ui_uri_node, NULL);
2242 const LilvUI * ui =
2243 lilv_uis_get_by_uri (uis, ui_uri_node);
2244
2245 const LilvNode * binary_uri_node =
2246 lilv_ui_get_binary_uri (ui);
2247 char * binary_uri =
2248 g_strdup (
2249 lilv_node_as_uri (binary_uri_node));
2250
2251 lilv_node_free (ui_uri_node);
2252 lilv_uis_free (uis);
2253
2254 return binary_uri;
2255 }
2256
2257 bool
lv2_plugin_is_ui_external(const char * uri,const char * ui_uri,GError ** error)2258 lv2_plugin_is_ui_external (
2259 const char * uri,
2260 const char * ui_uri,
2261 GError ** error)
2262 {
2263 LilvNode * lv2_uri =
2264 lilv_new_uri (LILV_WORLD, uri);
2265 const LilvPlugin * lilv_plugin =
2266 lilv_plugins_get_by_uri (
2267 LILV_PLUGINS, lv2_uri);
2268 lilv_node_free (lv2_uri);
2269 LilvUIs * uis =
2270 lilv_plugin_get_uis (lilv_plugin);
2271 LilvNode * ui_uri_node =
2272 lilv_new_uri (LILV_WORLD, ui_uri);
2273 const LilvUI * ui =
2274 lilv_uis_get_by_uri (uis, ui_uri_node);
2275 if (!ui)
2276 {
2277 g_set_error (
2278 error, Z_PLUGINS_LV2_PLUGIN_ERROR,
2279 Z_PLUGINS_LV2_PLUGIN_ERROR_NO_UI,
2280 _("Could not find UI by URI <%s>"),
2281 ui_uri);
2282 return false;
2283 }
2284
2285 const LilvNodes * ui_classes =
2286 lilv_ui_get_classes (ui);
2287 LILV_FOREACH (nodes, t, ui_classes)
2288 {
2289 const LilvNode * ui_type =
2290 lilv_nodes_get (ui_classes, t);
2291
2292 if (lilv_node_equals (
2293 ui_type,
2294 PM_GET_NODE (LV2_KX__externalUi)))
2295 {
2296 lilv_node_free (ui_uri_node);
2297 lilv_uis_free (uis);
2298 return true;
2299 }
2300 }
2301
2302 lilv_node_free (ui_uri_node);
2303 lilv_uis_free (uis);
2304
2305 return false;
2306 }
2307
2308 /**
2309 * Pick the most preferable UI for non-bridged
2310 * plugins.
2311 *
2312 * Calls lv2_plugin_pick_ui().
2313 *
2314 * @param[out] ui (Output) UI of the specific
2315 * plugin.
2316 * @param[out] ui_type UI type (eg, X11).
2317 *
2318 * @return Whether a UI was picked.
2319 */
2320 bool
lv2_plugin_pick_most_preferable_ui(const char * plugin_uri,char ** out_ui_str,char ** out_ui_type_str,bool allow_bridged)2321 lv2_plugin_pick_most_preferable_ui (
2322 const char * plugin_uri,
2323 char ** out_ui_str,
2324 char ** out_ui_type_str,
2325 bool allow_bridged)
2326 {
2327 LilvNode * uri =
2328 lilv_new_uri (LILV_WORLD, plugin_uri);
2329 const LilvPlugin * lilv_pl =
2330 lilv_plugins_get_by_uri (
2331 LILV_PLUGINS, uri);
2332 lilv_node_free (uri);
2333 g_return_val_if_fail (lilv_pl, false);
2334
2335 LilvUIs * uis =
2336 lilv_plugin_get_uis (lilv_pl);
2337 const LilvUI * out_ui;
2338 const LilvNode * out_ui_type;
2339
2340 /* get wrappable UI */
2341 bool ui_picked =
2342 lv2_plugin_pick_ui (
2343 uis, LV2_PLUGIN_UI_WRAPPABLE,
2344 &out_ui, &out_ui_type);
2345 g_debug ("wrappable UI found: %d", ui_picked);
2346
2347 #ifdef HAVE_CARLA
2348 /* try a bridged UI */
2349 if (!ui_picked && allow_bridged)
2350 {
2351 ui_picked =
2352 lv2_plugin_pick_ui (
2353 uis, LV2_PLUGIN_UI_FOR_BRIDGING,
2354 &out_ui, &out_ui_type);
2355 g_debug ("bridgable UI found: %d", ui_picked);
2356 }
2357 #endif
2358
2359 /* try an external UI */
2360 if (!ui_picked)
2361 {
2362 ui_picked =
2363 lv2_plugin_pick_ui (
2364 uis, LV2_PLUGIN_UI_EXTERNAL,
2365 &out_ui, &out_ui_type);
2366 }
2367 g_debug ("external UI found: %d", ui_picked);
2368
2369 if (out_ui_str)
2370 {
2371 const LilvNode * out_ui_uri =
2372 lilv_ui_get_uri (out_ui);
2373 *out_ui_str =
2374 g_strdup (
2375 lilv_node_as_uri (out_ui_uri));
2376 }
2377 if (out_ui_type_str)
2378 {
2379 *out_ui_type_str =
2380 g_strdup (
2381 lilv_node_as_uri (out_ui_type));
2382 }
2383
2384 lilv_uis_free (uis);
2385
2386 return ui_picked;
2387 }
2388
2389 /**
2390 * Pick the most preferable UI for the given flag.
2391 *
2392 * @param[out] ui (Output) UI of the specific
2393 * plugin.
2394 * @param[out] ui_type UI type (eg, X11).
2395 *
2396 * @return Whether a UI was picked.
2397 */
2398 bool
lv2_plugin_pick_ui(const LilvUIs * uis,Lv2PluginPickUiFlag flag,const LilvUI ** out_ui,const LilvNode ** out_ui_type)2399 lv2_plugin_pick_ui (
2400 const LilvUIs * uis,
2401 Lv2PluginPickUiFlag flag,
2402 const LilvUI ** out_ui,
2403 const LilvNode ** out_ui_type)
2404 {
2405 LILV_FOREACH (uis, u, uis)
2406 {
2407 const LilvUI* cur_ui =
2408 lilv_uis_get (uis, u);
2409 const LilvNodes * ui_types =
2410 lilv_ui_get_classes (cur_ui);
2411 g_message (
2412 "Checking UI: %s",
2413 lilv_node_as_uri (
2414 lilv_ui_get_uri (cur_ui)));
2415 LILV_FOREACH (nodes, t, ui_types)
2416 {
2417 const LilvNode * ui_type =
2418 lilv_nodes_get (ui_types, t);
2419 const char * ui_type_uri =
2420 lilv_node_as_uri (ui_type);
2421 g_message (
2422 "Found UI type: %s", ui_type_uri);
2423
2424 bool acceptable = false;
2425 switch (flag)
2426 {
2427 case LV2_PLUGIN_UI_WRAPPABLE:
2428 acceptable =
2429 (bool)
2430 suil_ui_supported (
2431 LV2_UI__Gtk3UI, ui_type_uri);
2432 if (acceptable)
2433 {
2434 if (out_ui_type)
2435 *out_ui_type = ui_type;
2436 g_message (
2437 "Wrappable UI accepted");
2438 }
2439 break;
2440 case LV2_PLUGIN_UI_EXTERNAL:
2441 if (string_is_equal (
2442 ui_type_uri,
2443 LV2_KX__externalUi))
2444 {
2445 acceptable = true;
2446 if (out_ui_type)
2447 {
2448 *out_ui_type =
2449 PM_GET_NODE (
2450 LV2_KX__externalUi);
2451 }
2452 g_message (
2453 "External KX UI accepted");
2454 }
2455 break;
2456 case LV2_PLUGIN_UI_FOR_BRIDGING:
2457 if (
2458 #ifdef HAVE_CARLA_BRIDGE_LV2_GTK2
2459 lilv_node_equals (
2460 ui_type,
2461 PM_GET_NODE (LV2_UI__GtkUI)) ||
2462 #endif
2463 #ifdef HAVE_CARLA_BRIDGE_LV2_GTK3
2464 lilv_node_equals (
2465 ui_type,
2466 PM_GET_NODE (
2467 LV2_UI__Gtk3UI)) ||
2468 #endif
2469 #ifdef HAVE_CARLA_BRIDGE_LV2_QT5
2470 lilv_node_equals (
2471 ui_type,
2472 PM_GET_NODE (
2473 LV2_UI__Qt5UI)) ||
2474 #endif
2475 #ifdef HAVE_CARLA_BRIDGE_LV2_QT4
2476 lilv_node_equals (
2477 ui_type,
2478 PM_GET_NODE (
2479 LV2_UI__Qt4UI)) ||
2480 #endif
2481 /*lilv_node_equals (*/
2482 /*ui_type,*/
2483 /*PM_LILV_NODES.ui_externalkx) ||*/
2484 false
2485 )
2486 {
2487 acceptable = true;
2488 if (out_ui_type)
2489 {
2490 *out_ui_type = ui_type;
2491 }
2492 g_message (
2493 "UI %s accepted for "
2494 "bridging",
2495 lilv_node_as_string (
2496 ui_type));
2497 }
2498 break;
2499 }
2500
2501 if (acceptable)
2502 {
2503 if (out_ui)
2504 {
2505 *out_ui = cur_ui;
2506 }
2507 return true;
2508 }
2509 }
2510 }
2511
2512 return false;
2513 }
2514
2515 /**
2516 * Returns the library path.
2517 *
2518 * Must be free'd with free().
2519 */
2520 char *
lv2_plugin_get_library_path(Lv2Plugin * self)2521 lv2_plugin_get_library_path (
2522 Lv2Plugin * self)
2523 {
2524 const LilvNode * library_uri =
2525 lilv_plugin_get_library_uri (self->lilv_plugin);
2526 char * library_path =
2527 lilv_node_get_path (library_uri, NULL);
2528 g_warn_if_fail (library_path);
2529
2530 return library_path;
2531 }
2532
2533 char *
lv2_plugin_get_abs_state_file_path(Lv2Plugin * self,bool is_backup)2534 lv2_plugin_get_abs_state_file_path (
2535 Lv2Plugin * self,
2536 bool is_backup)
2537 {
2538 char * abs_state_dir =
2539 plugin_get_abs_state_dir (
2540 self->plugin, is_backup);
2541 char * state_file_abs_path =
2542 g_build_filename (
2543 abs_state_dir, "state.ttl", NULL);
2544
2545 g_free (abs_state_dir);
2546
2547 return state_file_abs_path;
2548 }
2549
2550 /**
2551 * Instantiate the plugin.
2552 *
2553 * All of the actual initialization is done here.
2554 * If this is a new plugin, preset_uri should be
2555 * empty. If the project is being loaded, preset
2556 * uri should be the state file path.
2557 *
2558 * @param self Plugin to instantiate.
2559 * @param use_state_file Whether to use the plugin's
2560 * state file to instantiate the plugin.
2561 * @param preset_uri URI of preset to load.
2562 * @param state State to load, if loading from
2563 * a state. This is used when cloning plugins
2564 * for example. The state of the original plugin
2565 * is passed here.
2566 *
2567 * @return 0 if OK, non-zero if error.
2568 */
2569 int
lv2_plugin_instantiate(Lv2Plugin * self,bool use_state_file,char * preset_uri,LilvState * state,GError ** error)2570 lv2_plugin_instantiate (
2571 Lv2Plugin * self,
2572 bool use_state_file,
2573 char * preset_uri,
2574 LilvState * state,
2575 GError ** error)
2576 {
2577 g_message (
2578 "Instantiating... uri: %s, "
2579 "preset_uri: %s, use_state_file: %d"
2580 "state: %p",
2581 self->plugin->setting->descr->uri,
2582 preset_uri, use_state_file, state);
2583
2584 Plugin * pl = self->plugin;
2585 g_return_val_if_fail (
2586 IS_PLUGIN_AND_NONNULL (pl), -1);
2587
2588 const PluginDescriptor * descr =
2589 pl->setting->descr;
2590
2591 set_features (self);
2592
2593 zix_sem_init (&self->work_lock, 1);
2594
2595 self->map.handle = self;
2596 self->map.map = lv2_urid_map_uri;
2597
2598 self->worker.plugin = self;
2599 self->state_worker.plugin = self;
2600
2601 self->unmap.handle = self;
2602 self->unmap.unmap = lv2_urid_unmap_uri;
2603
2604 lv2_atom_forge_init (
2605 &self->main_forge, &self->map);
2606 lv2_atom_forge_init (
2607 &self->dsp_forge, &self->map);
2608
2609 self->env = serd_env_new (NULL);
2610 serd_env_set_prefix_from_strings (
2611 self->env,
2612 (const uint8_t*) "patch",
2613 (const uint8_t*) LV2_PATCH_PREFIX);
2614 serd_env_set_prefix_from_strings (
2615 self->env, (const uint8_t*) "time",
2616 (const uint8_t*) LV2_TIME_PREFIX);
2617 serd_env_set_prefix_from_strings (
2618 self->env, (const uint8_t*) "xsd",
2619 (const uint8_t*) LILV_NS_XSD);
2620
2621 self->sratom = sratom_new (&self->map);
2622 self->ui_sratom = sratom_new (&self->map);
2623 sratom_set_env (self->sratom, self->env);
2624 sratom_set_env (self->ui_sratom, self->env);
2625
2626 GError * err = NULL;
2627 char * templ =
2628 g_dir_make_tmp ("lv2_self_XXXXXX", &err);
2629 if (!templ)
2630 {
2631 PROPAGATE_PREFIXED_ERROR (
2632 error, err, "%s",
2633 _("Failed to make temporary dir"));
2634 return -1;
2635 }
2636 self->temp_dir =
2637 g_strjoin (NULL, templ, "/", NULL);
2638 g_free (templ);
2639
2640 self->make_path_temp.handle = self;
2641 self->make_path_temp.path =
2642 lv2_state_make_path_temp;
2643
2644 self->sched.handle = &self->worker;
2645 self->sched.schedule_work = lv2_worker_schedule;
2646
2647 self->ssched.handle = &self->state_worker;
2648 self->ssched.schedule_work = lv2_worker_schedule;
2649
2650 self->llog.handle = self;
2651 lv2_log_set_printf_funcs (&self->llog);
2652
2653 /*zix_sem_init (&self->exit_sem, 0);*/
2654
2655 zix_sem_init(&self->worker.sem, 0);
2656
2657 /* Load preset, if specified */
2658 if (!state)
2659 {
2660 if (preset_uri)
2661 {
2662 LilvNode* preset =
2663 lilv_new_uri (
2664 LILV_WORLD, preset_uri);
2665
2666 lv2_state_load_presets (
2667 self, NULL, NULL);
2668 state =
2669 lilv_state_new_from_world (
2670 LILV_WORLD, &self->map, preset);
2671 self->preset = state;
2672 lilv_node_free(preset);
2673 if (!state)
2674 {
2675 g_set_error (
2676 error,
2677 Z_PLUGINS_LV2_PLUGIN_ERROR,
2678 Z_PLUGINS_LV2_PLUGIN_ERROR_CANNOT_FIND_URI,
2679 _("Failed to find preset with URI "
2680 "<%s>"),
2681 preset_uri);
2682 return -1;
2683 }
2684 } /* end if preset uri */
2685 else if (use_state_file)
2686 {
2687 char * state_file_path =
2688 lv2_plugin_get_abs_state_file_path (
2689 self, PROJECT->loading_from_backup);
2690 state =
2691 lilv_state_new_from_file (
2692 LILV_WORLD, &self->map, NULL,
2693 state_file_path);
2694 if (!state)
2695 {
2696 g_set_error (
2697 error,
2698 Z_PLUGINS_LV2_PLUGIN_ERROR,
2699 Z_PLUGINS_LV2_PLUGIN_ERROR_FAILED,
2700 _("Failed to load state from %s"),
2701 state_file_path);
2702 return -1;
2703 }
2704 g_free (state_file_path);
2705
2706 LilvNode * lv2_uri =
2707 lilv_node_duplicate (
2708 lilv_state_get_plugin_uri (
2709 state));
2710
2711 if (!lv2_uri)
2712 {
2713 g_warning (
2714 "Missing plugin URI, try lv2ls"
2715 " to list plugins");
2716 }
2717
2718 /* Find plugin */
2719 const char * lv2_uri_str =
2720 lilv_node_as_string (lv2_uri);
2721 g_message (
2722 "Plugin URI: %s", lv2_uri_str);
2723 g_return_val_if_fail (
2724 LILV_PLUGINS, -1);
2725 self->lilv_plugin =
2726 lilv_plugins_get_by_uri (
2727 LILV_PLUGINS, lv2_uri);
2728 if (!self->lilv_plugin)
2729 {
2730 g_set_error (
2731 error,
2732 Z_PLUGINS_LV2_PLUGIN_ERROR,
2733 Z_PLUGINS_LV2_PLUGIN_ERROR_CANNOT_FIND_URI,
2734 _("Failed to find plugin with URI "
2735 "<%s>"),
2736 lv2_uri_str);
2737 lilv_node_free (lv2_uri);
2738 return -1;
2739 }
2740 lilv_node_free (lv2_uri);
2741 } /* end if use state file */
2742 }
2743 g_warn_if_fail (self->lilv_plugin);
2744
2745 int ret = 0;
2746 #if !defined (_WOE32) && !defined (__APPLE__)
2747 /* check that plugin .so doesn't contain illegal
2748 * dynamic dependencies */
2749 char * library_path =
2750 lv2_plugin_get_library_path (self);
2751 void * handle = dlopen (library_path, RTLD_LAZY);
2752 if (!handle)
2753 {
2754 g_set_error (
2755 error,
2756 Z_PLUGINS_LV2_PLUGIN_ERROR,
2757 Z_PLUGINS_LV2_PLUGIN_ERROR_FAILED,
2758 _("Failed to dlopen %s: %s"),
2759 library_path, dlerror ());
2760 return -1;
2761 }
2762 struct link_map * lm;
2763 ret = dlinfo (handle, RTLD_DI_LINKMAP, &lm);
2764 if (!lm || ret != 0)
2765 {
2766 g_set_error (
2767 error,
2768 Z_PLUGINS_LV2_PLUGIN_ERROR,
2769 Z_PLUGINS_LV2_PLUGIN_ERROR_FAILED,
2770 _("Failed to get dlinfo for %s"),
2771 library_path);
2772 return -1;
2773 }
2774 if (ZRYTHM_HAVE_UI)
2775 {
2776 while (lm)
2777 {
2778 g_message (
2779 " - %s (0x%016" PRIX64 ")",
2780 lm->l_name, lm->l_addr);
2781 if (string_contains_substr (
2782 lm->l_name, "Qt5Widgets.so") ||
2783 string_contains_substr (
2784 lm->l_name, "libgtk-3.so") ||
2785 string_contains_substr (
2786 lm->l_name, "libgtk"))
2787 {
2788 char * basename =
2789 io_path_get_basename (lm->l_name);
2790 char msg[1200];
2791 sprintf (
2792 msg,
2793 _("%s <%s> contains a reference to "
2794 "%s, which may cause issues.\n"
2795 "If the plugin does not load, please "
2796 "try instantiating the plugin in full-"
2797 "bridged mode, and report this to the "
2798 "plugin distributor and/or author:\n"
2799 "%s <%s>"),
2800 descr->name, descr->uri,
2801 basename, descr->author,
2802 descr->website);
2803 ui_show_error_message (
2804 MAIN_WINDOW, msg);
2805 g_free (basename);
2806 }
2807
2808 lm = lm->l_next;
2809 }
2810 }
2811 dlclose (handle);
2812 #endif
2813
2814 self->control_in = -1;
2815 self->enabled_in = -1;
2816
2817 /* Set default values for all ports */
2818 ret =
2819 lv2_create_or_init_ports_and_parameters (
2820 self);
2821 if (ret != 0)
2822 {
2823 g_set_error (
2824 error,
2825 Z_PLUGINS_LV2_PLUGIN_ERROR,
2826 Z_PLUGINS_LV2_PLUGIN_ERROR_FAILED,
2827 "%s",
2828 _("Failed to create or init ports and "
2829 "parameters"));
2830 return -1;
2831 }
2832
2833 /* --- Check for required features --- */
2834
2835 /* check that any required features are
2836 * supported */
2837 g_message ("checking required features");
2838 LilvNodes* req_feats =
2839 lilv_plugin_get_required_features (
2840 self->lilv_plugin);
2841 LILV_FOREACH (nodes, f, req_feats)
2842 {
2843 const char* uri =
2844 lilv_node_as_uri (
2845 lilv_nodes_get (req_feats, f));
2846 if (feature_is_supported (self, uri))
2847 {
2848 g_message (
2849 "Required feature %s is supported",
2850 uri);
2851 }
2852 else
2853 {
2854 g_set_error (
2855 error,
2856 Z_PLUGINS_LV2_PLUGIN_ERROR,
2857 Z_PLUGINS_LV2_PLUGIN_ERROR_FAILED,
2858 _("Required feature %s is not supported"),
2859 uri);
2860 lilv_nodes_free (req_feats);
2861 return -1;
2862 }
2863 }
2864 lilv_nodes_free (req_feats);
2865
2866 g_message ("checking optional features");
2867 LilvNodes* optional_features =
2868 lilv_plugin_get_optional_features (
2869 self->lilv_plugin);
2870 LILV_FOREACH (nodes, f, optional_features)
2871 {
2872 const char* uri =
2873 lilv_node_as_uri (
2874 lilv_nodes_get (optional_features, f));
2875 if (feature_is_supported (self, uri))
2876 {
2877 g_message (
2878 "Optional feature %s is supported",
2879 uri);
2880 }
2881 else
2882 {
2883 g_message (
2884 "Optional feature %s is not supported",
2885 uri);
2886 }
2887 }
2888 lilv_nodes_free (optional_features);
2889
2890 g_message ("checking extension data");
2891 LilvNodes* ext_data =
2892 lilv_plugin_get_extension_data (
2893 self->lilv_plugin);
2894 LILV_FOREACH (nodes, f, ext_data)
2895 {
2896 const char* uri =
2897 lilv_node_as_uri (
2898 lilv_nodes_get (ext_data, f));
2899 g_message (
2900 "Plugin has extension data: %s", uri);
2901 }
2902 lilv_nodes_free (ext_data);
2903
2904 /* Check for thread-safe state restore()
2905 * method. */
2906 if (lilv_plugin_has_feature (
2907 self->lilv_plugin,
2908 PM_GET_NODE (
2909 LV2_STATE__threadSafeRestore)))
2910 {
2911 g_message (
2912 "plugin has thread safe restore feature");
2913 self->safe_restore = true;
2914 }
2915
2916 /* Check for default state feature */
2917 if (lilv_plugin_has_feature (
2918 self->lilv_plugin,
2919 PM_GET_NODE (
2920 LV2_STATE__loadDefaultState)))
2921 {
2922 g_message (
2923 "plugin has default state feature");
2924 self->has_default_state = true;
2925 }
2926
2927 if (!state)
2928 {
2929 /* Not restoring state, load the plugin as a
2930 * preset to get default - see
2931 * http://lv2plug.in/ns/ext/state/#loadDefaultState */
2932 state =
2933 lilv_state_new_from_world (
2934 LILV_WORLD, &self->map,
2935 lilv_plugin_get_uri (self->lilv_plugin));
2936 }
2937
2938 const char * pl_uri =
2939 self->plugin->setting->descr->uri;
2940 const char * ui_uri =
2941 self->plugin->setting->ui_uri;
2942 if (ui_uri)
2943 {
2944 /* set whether the UI is external */
2945 bool is_ui_external =
2946 lv2_plugin_is_ui_external (
2947 pl_uri, ui_uri, &err);
2948 if (err)
2949 {
2950 PROPAGATE_PREFIXED_ERROR (
2951 error, err, "%s",
2952 _("Failed checking whether lv2 plugin "
2953 "UI is external"));
2954 return -1;
2955 }
2956 else if (is_ui_external)
2957 {
2958 self->has_external_ui = true;
2959 }
2960
2961 char * class =
2962 lv2_plugin_get_ui_class (
2963 pl_uri, ui_uri);
2964 g_message (
2965 "Selected UI: %s (type: %s)",
2966 ui_uri, class);
2967 g_free (class);
2968 }
2969 else
2970 {
2971 g_message ("Selected UI: Generic");
2972 }
2973
2974 /* The UI ring is fed by self->output
2975 * ports (usually one), and the UI
2976 * updates roughly once per cycle.
2977 * The ring size is a few times the
2978 * size of the MIDI output to give the UI
2979 * a chance to keep up. The UI
2980 * should be able to keep up with 4 cycles,
2981 * and tests show this works
2982 * for me, but this value might need
2983 * increasing to avoid overflows.
2984 */
2985 self->comm_buffer_size =
2986 (uint32_t)
2987 (AUDIO_ENGINE->midi_buf_size *
2988 N_BUFFER_CYCLES);
2989
2990 /* buffer data communication from plugin UI to plugin instance.
2991 * this buffer needs to potentially hold
2992 * (port's minimumSize) * (audio-periods) / (UI-periods)
2993 * bytes.
2994 *
2995 * e.g 48kSPS / 128fpp -> audio-periods = 375 Hz
2996 * ui-periods = 25 Hz (SuperRapidScreenUpdate)
2997 * default minimumSize = 32K (see LV2Plugin::allocate_atom_event_buffers()
2998 *
2999 * it is NOT safe to overflow (msg.size will be misinterpreted)
3000 */
3001 uint32_t bufsiz = 32768;
3002 int fact =
3003 (int)
3004 ceilf (
3005 AUDIO_ENGINE->sample_rate / 3000.f);
3006 self->comm_buffer_size =
3007 MAX (
3008 (uint32_t) bufsiz * MAX (8, (uint32_t) fact),
3009 self->comm_buffer_size);
3010
3011 g_message ("==========================");
3012 g_message (
3013 "Comm buffers: %d bytes",
3014 self->comm_buffer_size);
3015 g_message (
3016 "Update rate: %.01f Hz",
3017 (double) self->plugin->ui_update_hz);
3018 g_message (
3019 "Scale factor: %.01f",
3020 (double) self->plugin->ui_scale_factor);
3021 g_message ("==========================");
3022
3023 static float samplerate = 0.f;
3024 static int nominal_blocklength = 0;
3025 static int min_blocklength = 0;
3026 static int max_blocklength = 4096;
3027 static int midi_buf_size = 0;
3028 static const char * prog_name = PROGRAM_NAME;
3029
3030 samplerate =
3031 (float) AUDIO_ENGINE->sample_rate;
3032 nominal_blocklength =
3033 (int) AUDIO_ENGINE->block_length;
3034 midi_buf_size =
3035 (int) AUDIO_ENGINE->midi_buf_size;
3036
3037 /* Build options array to pass to plugin */
3038 const LV2_Options_Option options[] =
3039 {
3040 { LV2_OPTIONS_INSTANCE, 0,
3041 PM_URIDS.param_sampleRate,
3042 sizeof (float),
3043 PM_URIDS.atom_Float, &samplerate },
3044 { LV2_OPTIONS_INSTANCE, 0,
3045 PM_URIDS.bufsz_minBlockLength,
3046 sizeof (int32_t),
3047 PM_URIDS.atom_Int, &min_blocklength },
3048 { LV2_OPTIONS_INSTANCE, 0,
3049 PM_URIDS.bufsz_maxBlockLength,
3050 sizeof (int32_t),
3051 PM_URIDS.atom_Int, &max_blocklength },
3052 { LV2_OPTIONS_INSTANCE, 0,
3053 PM_URIDS.bufsz_nominalBlockLength,
3054 sizeof (int32_t),
3055 PM_URIDS.atom_Int, &nominal_blocklength },
3056 { LV2_OPTIONS_INSTANCE, 0,
3057 PM_URIDS.bufsz_sequenceSize,
3058 sizeof (int32_t),
3059 PM_URIDS.atom_Int, &midi_buf_size },
3060 { LV2_OPTIONS_INSTANCE, 0,
3061 PM_URIDS.ui_updateRate,
3062 sizeof (float),
3063 PM_URIDS.atom_Float,
3064 &self->plugin->ui_update_hz },
3065 #ifdef HAVE_LV2_1_18
3066 { LV2_OPTIONS_INSTANCE, 0,
3067 PM_URIDS.ui_scaleFactor,
3068 sizeof (float),
3069 PM_URIDS.atom_Float,
3070 &self->plugin->ui_scale_factor },
3071 #endif
3072 { LV2_OPTIONS_INSTANCE, 0,
3073 PM_URIDS.z_hostInfo_name,
3074 sizeof (const char *),
3075 PM_URIDS.atom_String,
3076 &prog_name },
3077 { LV2_OPTIONS_INSTANCE, 0,
3078 PM_URIDS.z_hostInfo_version,
3079 sizeof (const char *),
3080 PM_URIDS.atom_String,
3081 &ZRYTHM->version },
3082 { LV2_OPTIONS_INSTANCE, 0, 0, 0, 0, NULL },
3083 };
3084 g_return_val_if_fail (
3085 (sizeof (options) / sizeof (options[0])) <=
3086 (sizeof (self->options) /
3087 sizeof (self->options[0])),
3088 -1);
3089 memcpy (
3090 self->options, options, sizeof (options));
3091
3092 /* --- Check for required options --- */
3093
3094 /* check for required options */
3095 g_message ("checking required options");
3096 LilvNodes * required_options =
3097 lilv_world_find_nodes (
3098 LILV_WORLD,
3099 lilv_plugin_get_uri (self->lilv_plugin),
3100 PM_GET_NODE (LV2_OPTIONS__requiredOption),
3101 NULL);
3102 if (required_options)
3103 {
3104 LILV_FOREACH (nodes, i, required_options)
3105 {
3106 const char * uri =
3107 lilv_node_as_uri (
3108 lilv_nodes_get (required_options, i));
3109
3110 if (option_is_supported (self, uri))
3111 {
3112 g_message (
3113 "Required option %s is supported",
3114 uri);
3115 }
3116 else
3117 {
3118 g_set_error (
3119 error,
3120 Z_PLUGINS_LV2_PLUGIN_ERROR,
3121 Z_PLUGINS_LV2_PLUGIN_ERROR_FAILED,
3122 _("Required option %s is not supported"),
3123 uri);
3124 lilv_nodes_free (required_options);
3125 return -1;
3126 }
3127 }
3128 }
3129 lilv_nodes_free (required_options);
3130
3131 /* check for optional options */
3132 g_message ("checking supported options");
3133 LilvNodes * supported_options =
3134 lilv_world_find_nodes (
3135 LILV_WORLD,
3136 lilv_plugin_get_uri (self->lilv_plugin),
3137 PM_GET_NODE (LV2_OPTIONS__supportedOption),
3138 NULL);
3139 if (supported_options)
3140 {
3141 LILV_FOREACH (nodes, i, supported_options)
3142 {
3143 const char * uri =
3144 lilv_node_as_uri (
3145 lilv_nodes_get (supported_options, i));
3146
3147 if (option_is_supported (self, uri))
3148 {
3149 g_message (
3150 "Optional option %s is supported",
3151 uri);
3152 }
3153 else
3154 {
3155 g_message (
3156 "Optional option %s is not supported",
3157 uri);
3158 }
3159 }
3160 }
3161 lilv_nodes_free (supported_options);
3162
3163 /* Create Plugin <=> UI communication
3164 * buffers */
3165 self->ui_to_plugin_events =
3166 zix_ring_new (self->comm_buffer_size);
3167 self->plugin_to_ui_events =
3168 zix_ring_new (self->comm_buffer_size);
3169 zix_ring_mlock (self->ui_to_plugin_events);
3170 zix_ring_mlock (self->plugin_to_ui_events);
3171
3172 /* Instantiate the plugin */
3173 self->instance =
3174 lilv_plugin_instantiate (
3175 self->lilv_plugin,
3176 AUDIO_ENGINE->sample_rate,
3177 self->features);
3178 if (!self->instance)
3179 {
3180 g_set_error_literal (
3181 error,
3182 Z_PLUGINS_LV2_PLUGIN_ERROR,
3183 Z_PLUGINS_LV2_PLUGIN_ERROR_INSTANTIATION_FAILED,
3184 _("lilv_plugin_instantiate() failed"));
3185 return -1;
3186 }
3187 g_message ("Lilv plugin instantiated");
3188
3189 /* --- Handle extension data --- */
3190
3191 /* prepare the LV2_Extension_Data_Feature to pass
3192 * to the plugin UI */
3193 const LV2_Descriptor * lv2_descriptor =
3194 lilv_instance_get_descriptor (self->instance);
3195 self->ext_data_feature.data_access =
3196 lv2_descriptor->extension_data;
3197
3198 lv2_plugin_allocate_port_buffers (self);
3199
3200 /* Create workers if necessary */
3201 if (lilv_plugin_has_extension_data (
3202 self->lilv_plugin,
3203 PM_GET_NODE (LV2_WORKER__interface)))
3204 {
3205 g_message ("Instantiating workers");
3206
3207 const LV2_Worker_Interface* iface =
3208 (const LV2_Worker_Interface*)
3209 lilv_instance_get_extension_data (
3210 self->instance, LV2_WORKER__interface);
3211
3212 lv2_worker_init (
3213 self, &self->worker, iface, true);
3214 if (self->safe_restore)
3215 {
3216 lv2_worker_init (
3217 self, &self->state_worker,
3218 iface, false);
3219 }
3220 }
3221
3222 /* create options interface */
3223 if (lilv_plugin_has_extension_data (
3224 self->lilv_plugin,
3225 PM_GET_NODE (LV2_OPTIONS__interface)))
3226 {
3227 self->options_iface =
3228 (const LV2_Options_Interface *)
3229 lilv_instance_get_extension_data (
3230 self->instance, LV2_OPTIONS__interface);
3231 }
3232
3233 /* --- Apply state --- */
3234
3235 /* apply loaded state to plugin instance if
3236 * necessary */
3237 if (state)
3238 {
3239 g_message ("applying state");
3240 lv2_state_apply_state (self, state);
3241 }
3242 else
3243 {
3244 g_message ("no state to apply");
3245 }
3246
3247 /* --- Connect ports --- */
3248
3249 /* connect ports to buffers */
3250 g_message (
3251 "connecting %d LV2 (lilv) ports to buffers",
3252 pl->num_lilv_ports);
3253 for (int i = 0; i < pl->num_lilv_ports; ++i)
3254 {
3255 connect_port (self, (uint32_t) i);
3256 }
3257
3258 /* Print initial control values */
3259 if (DEBUGGING)
3260 {
3261 for (int i = 0; i < pl->num_lilv_ports; i++)
3262 {
3263 Port * port = pl->lilv_ports[i];
3264 g_return_val_if_fail (
3265 IS_PORT_AND_NONNULL (port), -1);
3266 g_message (
3267 "%s = %f", port->id.sym,
3268 (double) port->control);
3269 }
3270 }
3271
3272 g_message ("%s: done", __func__);
3273
3274 return 0;
3275 }
3276
3277 int
lv2_plugin_activate(Lv2Plugin * self,bool activate)3278 lv2_plugin_activate (
3279 Lv2Plugin * self,
3280 bool activate)
3281 {
3282 /* Activate plugin */
3283 g_message (
3284 "%s lilv instance...",
3285 activate ? "Activating" :"Deactivating");
3286 if (activate && !self->plugin->activated)
3287 {
3288 if (!self->plugin->instantiated)
3289 {
3290 g_critical ("plugin not instantiated");
3291 return -1;
3292 }
3293 lilv_instance_activate (self->instance);
3294 }
3295 else if (!activate && self->plugin->activated)
3296 {
3297 lilv_instance_deactivate (self->instance);
3298 }
3299
3300 self->plugin->activated = activate;
3301
3302 return 0;
3303 }
3304
3305 /**
3306 * Returns whether the plugin can be cleaned up
3307 * (some plugins crash on cleanup).
3308 */
3309 static bool
can_cleanup(Lv2Plugin * self)3310 can_cleanup (
3311 Lv2Plugin * self)
3312 {
3313 /* helm hangs/deadlocks on free sometimes */
3314 if (string_is_equal (
3315 self->plugin->setting->descr->uri,
3316 "http://tytel.org/helm") ||
3317 /* swh-plugins crashes on free
3318 * https://github.com/swh/lv2/issues/13 */
3319 g_str_has_prefix (
3320 self->plugin->setting->descr->uri,
3321 "http://plugin.org.uk/swh-plugins"))
3322 return false;
3323
3324 return true;
3325 }
3326
3327 int
lv2_plugin_cleanup(Lv2Plugin * self)3328 lv2_plugin_cleanup (
3329 Lv2Plugin * self)
3330 {
3331 g_message (
3332 "Cleaning up LV2 plugin %s (%p)...",
3333 self->plugin->setting->descr->name, self);
3334
3335 if (self->plugin->activated)
3336 {
3337 lv2_plugin_activate (self, false);
3338 }
3339
3340 if (self->plugin->instantiated)
3341 {
3342 if (can_cleanup (self))
3343 {
3344 object_free_w_func_and_null (
3345 lilv_instance_free, self->instance);
3346 }
3347 self->plugin->instantiated = false;
3348 }
3349
3350 g_message ("done");
3351
3352 return 0;
3353 }
3354
3355 /**
3356 * Processes the plugin for this cycle.
3357 *
3358 * @param g_start_frames The global start frames.
3359 * @param nframes The number of frames to process.
3360 */
3361 void
lv2_plugin_process(Lv2Plugin * self,const long g_start_frames,const nframes_t local_offset,const nframes_t nframes)3362 lv2_plugin_process (
3363 Lv2Plugin * self,
3364 const long g_start_frames,
3365 const nframes_t local_offset,
3366 const nframes_t nframes)
3367 {
3368 Plugin * pl = self->plugin;
3369 g_return_if_fail (
3370 IS_PLUGIN_AND_NONNULL (pl));
3371
3372 if (pl->instantiation_failed)
3373 {
3374 /* ignore */
3375 return;
3376 }
3377
3378 g_return_if_fail (
3379 pl->instantiated && pl->activated);
3380
3381 /* If transport state is not as expected, then
3382 * something has changed */
3383 const bool xport_changed =
3384 self->rolling != (TRANSPORT_IS_ROLLING) ||
3385 self->gframes != g_start_frames ||
3386 !math_floats_equal (
3387 self->bpm,
3388 tempo_track_get_current_bpm (P_TEMPO_TRACK));
3389 # if 0
3390 if (xport_changed)
3391 {
3392 g_message (
3393 "xport changed lv2_plugin_rolling %d, "
3394 "self gframes vs g start frames %ld %ld, "
3395 "bpm %f %f",
3396 self->rolling,
3397 self->gframes, g_start_frames,
3398 (double) self->bpm,
3399 (double)
3400 tempo_track_get_current_bpm (
3401 P_TEMPO_TRACK));
3402 }
3403 #endif
3404
3405 /* let the plugin know if transport state
3406 * changed */
3407 uint8_t pos_buf[256];
3408 LV2_Atom * lv2_pos = (LV2_Atom*) pos_buf;
3409 if (xport_changed && self->want_position)
3410 {
3411 /* Build an LV2 position object to report
3412 * change to plugin */
3413 Position start_pos;
3414 position_from_frames (
3415 &start_pos, g_start_frames);
3416 LV2_Atom_Forge * forge = &self->dsp_forge;
3417 lv2_atom_forge_set_buffer (
3418 forge, pos_buf, sizeof(pos_buf));
3419 LV2_Atom_Forge_Frame frame;
3420 lv2_atom_forge_object (
3421 forge, &frame, 0,
3422 PM_URIDS.time_Position);
3423 lv2_atom_forge_key (
3424 forge, PM_URIDS.time_frame);
3425 lv2_atom_forge_long (
3426 forge, g_start_frames);
3427 lv2_atom_forge_key (
3428 forge, PM_URIDS.time_speed);
3429 lv2_atom_forge_float (
3430 forge,
3431 TRANSPORT->play_state == PLAYSTATE_ROLLING ?
3432 1.0 : 0.0);
3433 lv2_atom_forge_key (
3434 forge, PM_URIDS.time_barBeat);
3435 int bars =
3436 position_get_bars (&start_pos, true);
3437 int beats =
3438 position_get_beats (&start_pos, true);
3439 double ticks =
3440 position_get_ticks (&start_pos);
3441 lv2_atom_forge_float (
3442 forge,
3443 ((float) beats - 1) +
3444 ((float) ticks /
3445 (float) TRANSPORT->ticks_per_beat));
3446 lv2_atom_forge_key (
3447 forge, PM_URIDS.time_bar);
3448 lv2_atom_forge_long (
3449 forge, bars - 1);
3450 lv2_atom_forge_key (
3451 forge, PM_URIDS.time_beatUnit);
3452 int beats_per_bar =
3453 tempo_track_get_beats_per_bar (
3454 P_TEMPO_TRACK);
3455 int beat_unit =
3456 tempo_track_get_beat_unit (P_TEMPO_TRACK);
3457 lv2_atom_forge_int (forge, beat_unit);
3458 lv2_atom_forge_key (
3459 forge, PM_URIDS.time_beatsPerBar);
3460 lv2_atom_forge_float (
3461 forge, (float) beats_per_bar);
3462 lv2_atom_forge_key (
3463 forge, PM_URIDS.time_beatsPerMinute);
3464 lv2_atom_forge_float (
3465 forge,
3466 tempo_track_get_current_bpm (
3467 P_TEMPO_TRACK));
3468 }
3469
3470 /* Update transport state to expected values for
3471 * next cycle */
3472 if (TRANSPORT_IS_ROLLING)
3473 {
3474 Position gpos;
3475 position_from_frames (
3476 &gpos, g_start_frames + nframes);
3477 self->gframes = gpos.frames;
3478 self->rolling = 1;
3479 }
3480 else
3481 {
3482 self->gframes = g_start_frames;
3483 self->rolling = 0;
3484 }
3485 self->bpm =
3486 tempo_track_get_current_bpm (P_TEMPO_TRACK);
3487
3488 /* Prepare port buffers */
3489 for (int p = 0; p < pl->num_lilv_ports; ++p)
3490 {
3491 Port * port = pl->lilv_ports[p];
3492 g_return_if_fail (
3493 IS_PORT_AND_NONNULL (port));
3494
3495 PortIdentifier * id = &port->id;
3496 if (id->type == TYPE_AUDIO
3497 || id->type == TYPE_CV)
3498 {
3499 /* connect buffer */
3500 lilv_instance_connect_port (
3501 self->instance,
3502 (uint32_t) p, &port->buf[local_offset]);
3503 }
3504 else if (id->type == TYPE_EVENT
3505 && id->flow == FLOW_INPUT)
3506 {
3507 if (G_UNLIKELY (port->evbuf == NULL))
3508 {
3509 g_critical (
3510 "evbuf is NULL for %s",
3511 pl->setting->descr->uri);
3512 return;
3513 }
3514 lv2_evbuf_reset (port->evbuf, true);
3515
3516 /* Write transport change event if
3517 * applicable */
3518 LV2_Evbuf_Iterator iter =
3519 lv2_evbuf_begin (port->evbuf);
3520 if (xport_changed &&
3521 id->flags & PORT_FLAG_WANT_POSITION)
3522 {
3523 lv2_evbuf_write (
3524 &iter, 0, 0,
3525 lv2_pos->type, lv2_pos->size,
3526 (const uint8_t*)
3527 LV2_ATOM_BODY (lv2_pos));
3528 }
3529
3530 if (self->request_update)
3531 {
3532 /* Plugin state has changed, request
3533 * an update */
3534 const LV2_Atom_Object get = {
3535 { sizeof(LV2_Atom_Object_Body),
3536 PM_URIDS.atom_Object },
3537 { 0, PM_URIDS.patch_Get } };
3538 lv2_evbuf_write (
3539 &iter, 0, 0,
3540 get.atom.type, get.atom.size,
3541 (const uint8_t*)
3542 LV2_ATOM_BODY (&get));
3543 }
3544
3545 if (port->midi_events->num_events > 0)
3546 {
3547 int num_events_written = 0;
3548
3549 /* Write MIDI input */
3550 for (int i = 0;
3551 i <
3552 port->midi_events->num_events;
3553 i++)
3554 {
3555 MidiEvent * ev =
3556 &port->midi_events->events[i];
3557 if (ev->time < local_offset ||
3558 ev->time >=
3559 local_offset + nframes)
3560 {
3561 /* skip events scheduled
3562 * for another split within
3563 * the processing cycle */
3564 continue;
3565 }
3566
3567 if (ZRYTHM_TESTING)
3568 {
3569 g_message (
3570 "writing plugin input "
3571 "event %d at time %u - "
3572 "local frames %u nframes "
3573 "%u",
3574 num_events_written,
3575 ev->time - local_offset,
3576 local_offset, nframes);
3577 midi_event_print (ev);
3578 }
3579
3580 lv2_evbuf_write (
3581 &iter,
3582 /* event time is relative to
3583 * the current zrythm full
3584 * cycle (not split). it
3585 * needs to be made relative
3586 * to the current split */
3587 ev->time - local_offset, 0,
3588 PM_URIDS.midi_MidiEvent,
3589 3, ev->raw_buffer);
3590
3591 num_events_written++;
3592 }
3593 }
3594 }
3595 else if (id->type == TYPE_CONTROL &&
3596 id->flow == FLOW_INPUT)
3597 {
3598 /* let the plugin know if freewheeling */
3599 if (id->flags & PORT_FLAG_FREEWHEEL)
3600 {
3601 if (AUDIO_ENGINE->exporting)
3602 {
3603 port->control = port->maxf;
3604 }
3605 else
3606 {
3607 port->control = port->minf;
3608 }
3609 }
3610 }
3611 }
3612 self->request_update = false;
3613
3614 /* Run plugin for this cycle */
3615 const bool send_ui_updates =
3616 run (self, nframes) &&
3617 !AUDIO_ENGINE->exporting &&
3618 self->plugin->ui_instantiated;
3619
3620 /* Deliver MIDI output and UI events */
3621 for (int p = 0; p < pl->num_lilv_ports; ++p)
3622 {
3623 Port * port = pl->lilv_ports[p];
3624 PortIdentifier * pi = &port->id;
3625 switch (pi->type)
3626 {
3627 case TYPE_CONTROL:
3628 if (G_UNLIKELY (pi->flow == FLOW_OUTPUT))
3629 {
3630 /* if latency changed, recalc graph */
3631 if (G_UNLIKELY (pi->flags &
3632 PORT_FLAG_REPORTS_LATENCY &&
3633 self->plugin->latency !=
3634 (nframes_t)
3635 port->control))
3636 {
3637 g_message (
3638 "%s: latency changed from %d "
3639 "to %f",
3640 pi->label, pl->latency,
3641 (double) port->control);
3642 EVENTS_PUSH (
3643 ET_PLUGIN_LATENCY_CHANGED,
3644 NULL);
3645 pl->latency =
3646 (nframes_t)
3647 port->control;
3648 }
3649
3650 /* if UI is instantiated */
3651 if (G_UNLIKELY (send_ui_updates &&
3652 pl->visible &&
3653 !port->received_ui_event &&
3654 !math_floats_equal (
3655 port->control,
3656 port->last_sent_control)))
3657 {
3658 /* forward event to UI */
3659 lv2_ui_send_control_val_event_from_plugin_to_ui (
3660 self, port);
3661 }
3662 }
3663 if (send_ui_updates)
3664 {
3665 /* ignore ports that received a UI
3666 * event at the start of a cycle
3667 * (otherwise these causes trembling
3668 * while changing them) */
3669 if (port->received_ui_event)
3670 {
3671 port->received_ui_event = 0;
3672 continue;
3673 }
3674 }
3675 break;
3676 case TYPE_EVENT:
3677 if (pi->flow == FLOW_OUTPUT)
3678 {
3679 for (LV2_Evbuf_Iterator iter =
3680 lv2_evbuf_begin (
3681 port->evbuf);
3682 lv2_evbuf_is_valid(iter);
3683 iter = lv2_evbuf_next (iter))
3684 {
3685 // Get event from LV2 buffer
3686 uint32_t frames, subframes,
3687 type, size;
3688 uint8_t* body;
3689 lv2_evbuf_get (
3690 iter, &frames, &subframes,
3691 &type, &size, &body);
3692
3693 /* if midi event */
3694 if (body && type ==
3695 PM_URIDS.
3696 midi_MidiEvent)
3697 {
3698 if (size != 3)
3699 {
3700 g_message (
3701 "unhandled event from "
3702 "port %s of size %"
3703 PRIu32,
3704 pi->label, size);
3705 }
3706 else
3707 {
3708 /* Write MIDI event to port */
3709 midi_events_add_event_from_buf (
3710 port->midi_events,
3711 frames, body,
3712 (int) size, 0);
3713 }
3714 }
3715
3716 /* if UI is instantiated */
3717 if (pl->visible &&
3718 !port->old_api)
3719 {
3720 /* forward event to UI */
3721 lv2_ui_send_event_from_plugin_to_ui (
3722 self, (uint32_t) p,
3723 type, size, body);
3724 }
3725 }
3726
3727 /* Clear event output for plugin to
3728 * write to next cycle */
3729 lv2_evbuf_reset (
3730 port->evbuf, false);
3731 }
3732 default:
3733 break;
3734 }
3735 }
3736 }
3737
3738 /**
3739 * Populates the banks in the plugin instance.
3740 */
3741 void
lv2_plugin_populate_banks(Lv2Plugin * self)3742 lv2_plugin_populate_banks (
3743 Lv2Plugin * self)
3744 {
3745 /* add default bank and preset */
3746 PluginBank * pl_def_bank =
3747 plugin_add_bank_if_not_exists (
3748 self->plugin,
3749 LV2_ZRYTHM__defaultBank,
3750 _("Default bank"));
3751 PluginPreset * pl_def_preset =
3752 plugin_preset_new ();
3753 pl_def_preset->uri =
3754 g_strdup (LV2_ZRYTHM__initPreset);
3755 pl_def_preset->name = g_strdup (_("Init"));
3756 plugin_add_preset_to_bank (
3757 self->plugin, pl_def_bank, pl_def_preset);
3758
3759 /*GString * presets_str = g_string_new (NULL);*/
3760
3761 LilvNodes * presets =
3762 lilv_plugin_get_related (
3763 self->lilv_plugin,
3764 PM_GET_NODE (LV2_PRESETS__Preset));
3765 const LilvNode * preset_bank =
3766 PM_GET_NODE (LV2_PRESETS__bank);
3767 const LilvNode * rdfs_label =
3768 PM_GET_NODE (LILV_NS_RDFS "label");
3769 int count = 0;
3770 LILV_FOREACH (nodes, i, presets)
3771 {
3772 const LilvNode* preset =
3773 lilv_nodes_get (presets, i);
3774 lilv_world_load_resource (LILV_WORLD, preset);
3775
3776 LilvNodes * banks =
3777 lilv_world_find_nodes (
3778 LILV_WORLD, preset, preset_bank, NULL);
3779 PluginBank * pl_bank = NULL;
3780 if (banks)
3781 {
3782 const LilvNode * bank =
3783 lilv_nodes_get_first (banks);
3784 const LilvNode * bank_label =
3785 lilv_world_get (
3786 LILV_WORLD, bank,
3787 rdfs_label, NULL);
3788 pl_bank =
3789 plugin_add_bank_if_not_exists (
3790 self->plugin,
3791 lilv_node_as_string (bank),
3792 bank_label ?
3793 lilv_node_as_string (bank_label) :
3794 _("Unnamed bank"));
3795 lilv_nodes_free (banks);
3796 }
3797 else
3798 {
3799 pl_bank = pl_def_bank;
3800 }
3801
3802 LilvNodes * labels =
3803 lilv_world_find_nodes (
3804 LILV_WORLD, preset, rdfs_label, NULL);
3805 if (labels)
3806 {
3807 const LilvNode* label =
3808 lilv_nodes_get_first(labels);
3809 PluginPreset * pl_preset =
3810 plugin_preset_new ();
3811 pl_preset->uri =
3812 g_strdup (lilv_node_as_string (preset));
3813 pl_preset->name =
3814 g_strdup (lilv_node_as_string (label));
3815 plugin_add_preset_to_bank (
3816 self->plugin, pl_bank, pl_preset);
3817 lilv_nodes_free (labels);
3818
3819 #if 0
3820 g_string_append_printf (
3821 presets_str,
3822 "found preset %s (<%s>)\n",
3823 pl_preset->name, pl_preset->uri);
3824 #endif
3825 count++;
3826 }
3827 else
3828 {
3829 g_message (
3830 "Skipping preset <%s> because it has "
3831 "no rdfs:label",
3832 lilv_node_as_string (preset));
3833 }
3834 /* some plugins (Helm LV2) get stuck in an
3835 * infinite loop when calling this */
3836 /*lilv_world_unload_resource (*/
3837 /*LILV_WORLD, preset);*/
3838 }
3839 lilv_nodes_free (presets);
3840
3841 g_message ("found %d presets", count);
3842
3843 #if 0
3844 char * str = g_string_free (presets_str, false);
3845 g_message ("%s", str);
3846 g_free (str);
3847 #endif
3848 }
3849
3850 /**
3851 * Frees the Lv2Plugin and all its components.
3852 */
3853 void
lv2_plugin_free(Lv2Plugin * self)3854 lv2_plugin_free (
3855 Lv2Plugin * self)
3856 {
3857 /* Wait for finish signal from UI or signal
3858 * handler */
3859 /*zix_sem_wait (&self->exit_sem);*/
3860 self->exit = true;
3861
3862 /* Terminate the worker */
3863 lv2_worker_finish (&self->worker);
3864
3865 /* Deactivate suil instance */
3866 object_free_w_func_and_null (
3867 suil_instance_free, self->suil_instance);
3868
3869 if (self->instance && self->plugin->activated)
3870 {
3871 lilv_instance_deactivate (self->instance);
3872 self->plugin->activated = false;
3873 }
3874
3875 if (can_cleanup (self))
3876 {
3877 g_debug (
3878 "attempting to free lilv instance for %s",
3879 self->plugin->setting->descr->uri);
3880 object_free_w_func_and_null (
3881 lilv_instance_free, self->instance);
3882 }
3883
3884 /* Clean up */
3885 object_free_w_func_and_null (
3886 zix_ring_free, self->ui_to_plugin_events);
3887 object_free_w_func_and_null (
3888 zix_ring_free, self->plugin_to_ui_events);
3889 object_free_w_func_and_null (
3890 suil_host_free, self->suil_host);
3891 object_free_w_func_and_null (
3892 sratom_free, self->sratom);
3893 object_free_w_func_and_null (
3894 sratom_free, self->ui_sratom);
3895
3896 /*zix_sem_destroy (&self->exit_sem);*/
3897
3898 remove (self->temp_dir);
3899 object_zero_and_free_if_nonnull (self->temp_dir);
3900
3901 object_free_w_func_and_null (
3902 free, self->ui_event_buf);
3903
3904 if (self->extui.plugin_human_id)
3905 {
3906 g_free ((char *) self->extui.plugin_human_id);
3907 self->extui.plugin_human_id = NULL;
3908 }
3909
3910 object_zero_and_free (self);
3911 }
3912