-
Notifications
You must be signed in to change notification settings - Fork 716
/
Copy pathlv2_plugin.cc
4048 lines (3593 loc) · 128 KB
/
lv2_plugin.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008-2012 Carl Hetherington <[email protected]>
* Copyright (C) 2008-2017 Paul Davis <[email protected]>
* Copyright (C) 2008-2019 David Robillard <[email protected]>
* Copyright (C) 2012-2023 Robin Gareus <[email protected]>
* Copyright (C) 2013-2018 John Emmas <[email protected]>
* Copyright (C) 2013 Michael R. Fisher <[email protected]>
* Copyright (C) 2014-2016 Tim Mayberry <[email protected]>
* Copyright (C) 2016-2017 Damien Zammit <[email protected]>
* Copyright (C) 2016 Nick Mainsbridge <[email protected]>
* Copyright (C) 2017 Johannes Mueller <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <cctype>
#include <string>
#include <vector>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include "pbd/gstdio_compat.h"
#include <glib/gprintf.h>
#include <glibmm.h>
#include "pbd/file_utils.h"
#include "pbd/stl_delete.h"
#include "pbd/compose.h"
#include "pbd/error.h"
#include "pbd/locale_guard.h"
#include "pbd/pthread_utils.h"
#include "pbd/replace_all.h"
#include "pbd/xml++.h"
#ifdef PLATFORM_WINDOWS
#include <shlobj.h> // CSIDL_*
#include "pbd/windows_special_dirs.h"
#endif
#include "temporal/superclock.h"
#ifdef WAF_BUILD
#include "libardour-config.h"
#endif
#include "ardour/audio_buffer.h"
#include "ardour/audioengine.h"
#include "ardour/directory_names.h"
#include "ardour/debug.h"
#include "ardour/lv2_evbuf.h"
#include "ardour/lv2_plugin.h"
#include "ardour/midi_patch_manager.h"
#include "ardour/session.h"
#include "ardour/tempo.h"
#include "ardour/types.h"
#include "ardour/utils.h"
#include "ardour/worker.h"
#include "ardour/search_paths.h"
#include "pbd/i18n.h"
#include <locale.h>
#include <lilv/lilv.h>
#ifdef HAVE_LV2_1_18_6
#include <lv2/atom/atom.h>
#include <lv2/atom/forge.h>
#include <lv2/log/log.h>
#include <lv2/midi/midi.h>
#include <lv2/port-props/port-props.h>
#include <lv2/presets/presets.h>
#include <lv2/state/state.h>
#include <lv2/time/time.h>
#include <lv2/worker/worker.h>
#include <lv2/resize-port/resize-port.h>
#include <lv2/ui/ui.h>
#include <lv2/units/units.h>
#include <lv2/patch/patch.h>
#include <lv2/port-groups/port-groups.h>
#include <lv2/parameters/parameters.h>
#include <lv2/buf-size/buf-size.h>
#include <lv2/options/options.h>
#else
#include <lv2/lv2plug.in/ns/ext/atom/atom.h>
#include <lv2/lv2plug.in/ns/ext/atom/forge.h>
#include <lv2/lv2plug.in/ns/ext/log/log.h>
#include <lv2/lv2plug.in/ns/ext/midi/midi.h>
#include <lv2/lv2plug.in/ns/ext/port-props/port-props.h>
#include <lv2/lv2plug.in/ns/ext/presets/presets.h>
#include <lv2/lv2plug.in/ns/ext/state/state.h>
#include <lv2/lv2plug.in/ns/ext/time/time.h>
#include <lv2/lv2plug.in/ns/ext/worker/worker.h>
#include <lv2/lv2plug.in/ns/ext/resize-port/resize-port.h>
#include <lv2/lv2plug.in/ns/extensions/ui/ui.h>
#include <lv2/lv2plug.in/ns/extensions/units/units.h>
#include <lv2/lv2plug.in/ns/ext/patch/patch.h>
#include <lv2/lv2plug.in/ns/ext/port-groups/port-groups.h>
#include <lv2/lv2plug.in/ns/ext/parameters/parameters.h>
#include <lv2/lv2plug.in/ns/ext/buf-size/buf-size.h>
#include <lv2/lv2plug.in/ns/ext/options/options.h>
#endif
#ifdef HAVE_SUIL
#include <suil/suil.h>
#endif
// Compatibility for old LV2
#ifndef LV2_ATOM_CONTENTS_CONST
#define LV2_ATOM_CONTENTS_CONST(type, atom) \
((const void*)((const uint8_t*)(atom) + sizeof(type)))
#endif
#ifndef LV2_ATOM_BODY_CONST
#define LV2_ATOM_BODY_CONST(atom) LV2_ATOM_CONTENTS_CONST(LV2_Atom, atom)
#endif
#ifndef LV2_PATCH__property
#define LV2_PATCH__property LV2_PATCH_PREFIX "property"
#endif
#ifndef LV2_PATCH__value
#define LV2_PATCH__value LV2_PATCH_PREFIX "value"
#endif
#ifndef LV2_PATCH__writable
#define LV2_PATCH__writable LV2_PATCH_PREFIX "writable"
#endif
/** The number of MIDI buffers that will fit in a UI/worker comm buffer.
This needs to be roughly the number of cycles the UI will get around to
actually processing the traffic. Lower values are flakier but save memory.
*/
static const size_t NBUFS = 4;
using namespace std;
using namespace ARDOUR;
using namespace PBD;
using namespace Temporal;
bool LV2Plugin::force_state_save = false;
int32_t LV2Plugin::_ui_style_flat = 0;
int32_t LV2Plugin::_ui_style_boxy = 0;
uint32_t LV2Plugin::_ui_background_color = 0x000000ff; // RGBA
uint32_t LV2Plugin::_ui_foreground_color = 0xffffffff; // RGBA
uint32_t LV2Plugin::_ui_contrasting_color = 0x33ff33ff; // RGBA
unsigned long LV2Plugin::_ui_transient_win_id = 0;
class LV2World : boost::noncopyable {
public:
LV2World ();
~LV2World ();
void load_bundled_plugins(bool verbose=false);
LilvWorld* world;
LilvNode* atom_AtomPort;
LilvNode* atom_Chunk;
LilvNode* atom_Sequence;
LilvNode* atom_bufferType;
LilvNode* atom_eventTransfer;
LilvNode* atom_supports;
LilvNode* ev_EventPort;
LilvNode* ext_logarithmic;
LilvNode* ext_notOnGUI;
LilvNode* ext_expensive;
LilvNode* ext_causesArtifacts;
LilvNode* ext_notAutomatic;
LilvNode* ext_rangeSteps;
LilvNode* ext_displayPriority;
LilvNode* groups_group;
LilvNode* groups_element;
LilvNode* lv2_AudioPort;
LilvNode* lv2_ControlPort;
LilvNode* lv2_InputPort;
LilvNode* lv2_OutputPort;
LilvNode* lv2_designation;
LilvNode* lv2_enumeration;
LilvNode* lv2_freewheeling;
LilvNode* lv2_inPlaceBroken;
LilvNode* lv2_isSideChain;
LilvNode* lv2_index;
LilvNode* lv2_integer;
LilvNode* lv2_default;
LilvNode* lv2_minimum;
LilvNode* lv2_maximum;
LilvNode* lv2_reportsLatency;
LilvNode* lv2_sampleRate;
LilvNode* lv2_toggled;
LilvNode* midi_MidiEvent;
LilvNode* rdfs_comment;
LilvNode* rdfs_label;
LilvNode* rdfs_range;
LilvNode* rsz_minimumSize;
LilvNode* time_Position;
LilvNode* time_beatsPerMin;
LilvNode* ui_GtkUI;
LilvNode* ui_X11UI;
LilvNode* ui_external;
LilvNode* ui_externalkx;
LilvNode* units_hz;
LilvNode* units_db;
LilvNode* units_unit;
LilvNode* units_render;
LilvNode* units_midiNote;
LilvNode* patch_writable;
LilvNode* patch_Message;
LilvNode* opts_requiredOptions;
LilvNode* bufz_powerOf2BlockLength;
LilvNode* bufz_fixedBlockLength;
LilvNode* bufz_nominalBlockLength;
LilvNode* bufz_coarseBlockLength;
LilvNode* state_loadDefaultState;
#ifdef LV2_EXTENDED
LilvNode* lv2_noSampleAccurateCtrl;
LilvNode* routing_connectAllOutputs; // lv2:optionalFeature
LilvNode* auto_can_write_automatation; // lv2:optionalFeature
LilvNode* auto_automation_control; // atom:supports
LilvNode* auto_automation_controlled; // lv2:portProperty
LilvNode* auto_automation_controller; // lv2:portProperty
LilvNode* inline_display_interface; // lv2:extensionData
LilvNode* inline_display_in_gui; // lv2:optionalFeature
LilvNode* inline_mixer_control; // lv2:PortProperty
LilvNode* export_interface; // lv2:extensionData
#endif
private:
bool _bundle_checked;
};
static LV2World _world;
static bool
is_inside_bundle (std::string const& bundle_dir, std::string path)
{
assert (Glib::file_test (bundle_dir, Glib::FILE_TEST_IS_DIR | Glib::FILE_TEST_EXISTS));
if (bundle_dir.size () > path.size ()) {
return false;
}
if (bundle_dir == path) {
return true;
}
if (path == Glib::path_get_dirname (path)) {
/* path must be root */
return false;
}
while (bundle_dir.size () < path.size ()) {
/* step up, compare with bundle_dir */
path = Glib::path_get_dirname (path);
if (bundle_dir == path) {
return true;
}
}
return false;
}
/* worker extension */
/** Called by the plugin to schedule non-RT work. */
static LV2_Worker_Status
work_schedule(LV2_Worker_Schedule_Handle handle,
uint32_t size,
const void* data)
{
return (((Worker*)handle)->schedule(size, data)
? LV2_WORKER_SUCCESS
: LV2_WORKER_ERR_UNKNOWN);
}
/** Called by the plugin to respond to non-RT work. */
static LV2_Worker_Status
work_respond(LV2_Worker_Respond_Handle handle,
uint32_t size,
const void* data)
{
return (((Worker*)handle)->respond(size, data)
? LV2_WORKER_SUCCESS
: LV2_WORKER_ERR_UNKNOWN);
}
static void
set_port_value(const char* port_symbol,
void* user_data,
const void* value,
uint32_t /*size*/,
uint32_t type)
{
LV2Plugin* self = (LV2Plugin*)user_data;
float fvalue = 0.0f;
if (type == URIMap::instance ().urids.atom_Float) {
fvalue = *(const float*)value;
} else if (type == URIMap::instance ().urids.atom_Double) {
fvalue = static_cast<float> (*(const double*)value);
} else if (type == URIMap::instance ().urids.atom_Int) {
fvalue = static_cast<float> (*(const int32_t*)value);
} else if (type == URIMap::instance ().urids.atom_Long) {
fvalue = static_cast<float> (*(const int64_t*)value);
} else {
error << string_compose (
_("LV2<%1>: Preset value for port '%2' has unsupported datatype <%3>"),
self->name (),
port_symbol,
self->uri_map ().id_to_uri(type));
}
const uint32_t port_index = self->port_index(port_symbol);
if (port_index != (uint32_t)-1) {
self->set_parameter(port_index, fvalue, 0);
self->PresetPortSetValue (port_index, fvalue); /* EMIT SIGNAL */
}
}
#ifdef LV2_EXTENDED
/* inline display extension */
void
LV2Plugin::queue_draw (LV2_Inline_Display_Handle handle)
{
LV2Plugin* plugin = (LV2Plugin*)handle;
plugin->QueueDraw(); /* EMIT SIGNAL */
}
void
LV2Plugin::midnam_update (LV2_Midnam_Handle handle)
{
LV2Plugin* plugin = (LV2Plugin*)handle;
plugin->_midnam_dirty = true;
plugin->UpdateMidnam (); /* EMIT SIGNAL */
}
void
LV2Plugin::bankpatch_notify (LV2_BankPatch_Handle handle, uint8_t chn, uint32_t bank, uint8_t pgm)
{
LV2Plugin* plugin = (LV2Plugin*)handle;
if (chn > 15) {
return;
}
plugin->seen_bankpatch = true;
if (pgm > 127 || bank > 16383) {
plugin->_bankpatch[chn] = UINT32_MAX;
} else {
plugin->_bankpatch[chn] = (bank << 7) | pgm;
}
plugin->BankPatchChange (chn); /* EMIT SIGNAL */
}
#endif
/* log extension */
static int
log_vprintf(LV2_Log_Handle /*handle*/,
LV2_URID type,
const char* fmt,
va_list args)
{
char* str = NULL;
const int ret = g_vasprintf(&str, fmt, args);
/* strip trailing whitespace */
while (strlen (str) > 0 && isspace (str[strlen (str) - 1])) {
str[strlen (str) - 1] = '\0';
}
if (strlen (str) == 0) {
return 0;
}
if (type == URIMap::instance().urids.log_Error) {
error << str << endmsg;
} else if (type == URIMap::instance().urids.log_Warning) {
warning << str << endmsg;
} else if (type == URIMap::instance().urids.log_Note) {
info << str << endmsg;
} else if (type == URIMap::instance().urids.log_Trace) {
DEBUG_TRACE(DEBUG::LV2, str);
}
return ret;
}
static int
log_printf(LV2_Log_Handle handle,
LV2_URID type,
const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
const int ret = log_vprintf(handle, type, fmt, args);
va_end(args);
return ret;
}
struct LV2Plugin::Impl {
Impl() : plugin(0), ui(0), ui_type(0), name(0), author(0), instance(0)
, work_iface(0)
, opts_iface(0)
, state(0)
, block_length(0)
, options(0)
#ifdef LV2_EXTENDED
, queue_draw(0)
, midnam(0)
#endif
{}
/** Find the LV2 input port with the given designation.
* If found, bufptrs[port_index] will be set to bufptr.
*/
const LilvPort* designated_input (const char* uri, void** bufptrs[], void** bufptr);
const LilvPlugin* plugin;
const LilvUI* ui;
const LilvNode* ui_type;
LilvNode* name;
LilvNode* author;
LilvInstance* instance;
const LV2_Worker_Interface* work_iface;
const LV2_Options_Interface* opts_iface;
LilvState* state;
LV2_Atom_Forge forge;
LV2_Atom_Forge ui_forge;
int32_t block_length;
LV2_Options_Option* options;
#ifdef LV2_EXTENDED
LV2_Inline_Display* queue_draw;
LV2_Midnam* midnam;
LV2_BankPatch* bankpatch;
#endif
};
LV2Plugin::LV2Plugin (AudioEngine& engine,
Session& session,
const void* c_plugin,
samplecnt_t rate)
: Plugin (engine, session)
, Workee ()
, _impl(new Impl())
, _features(NULL)
, _worker(NULL)
, _state_worker(NULL)
, _insert_id("0")
, _non_realtime (false)
, _bpm_control_port_index((uint32_t)-1)
, _patch_port_in_index((uint32_t)-1)
, _patch_port_out_index((uint32_t)-1)
, _uri_map(URIMap::instance())
, _no_sample_accurate_ctrl (false)
, _connect_all_audio_outputs (false)
{
init(c_plugin, rate);
latency_compute_run();
}
LV2Plugin::LV2Plugin (const LV2Plugin& other)
: Plugin (other)
, Workee ()
, _impl(new Impl())
, _features(NULL)
, _worker(NULL)
, _state_worker(NULL)
, _insert_id(other._insert_id)
, _non_realtime (other._non_realtime)
, _bpm_control_port_index((uint32_t)-1)
, _patch_port_in_index((uint32_t)-1)
, _patch_port_out_index((uint32_t)-1)
, _uri_map(URIMap::instance())
, _no_sample_accurate_ctrl (false)
, _connect_all_audio_outputs (false)
{
init(other._impl->plugin, other._sample_rate);
XMLNode root (other.state_node_name ());
other.add_state (&root);
set_state (root, Stateful::current_state_version);
for (uint32_t i = 0; i < parameter_count(); ++i) {
_control_data[i] = other._shadow_data[i];
_shadow_data[i] = other._shadow_data[i];
}
latency_compute_run();
}
void
LV2Plugin::init(const void* c_plugin, samplecnt_t rate)
{
DEBUG_TRACE(DEBUG::LV2, "init\n");
_impl->plugin = (const LilvPlugin*)c_plugin;
_impl->ui = NULL;
_impl->ui_type = NULL;
_to_ui = NULL;
_from_ui = NULL;
_control_data = 0;
_shadow_data = 0;
_atom_ev_buffers = 0;
_ev_buffers = 0;
_bpm_control_port = 0;
_freewheel_control_port = 0;
_latency_control_port = 0;
_next_cycle_start = std::numeric_limits<samplepos_t>::max();
_next_cycle_speed = 1.0;
_next_cycle_beat = 0.0;
_current_bpm = 0.0;
_prev_time_scale = 0.0;
_seq_size = _engine.raw_buffer_size(DataType::MIDI);
_state_version = 0;
_was_activated = false;
_has_state_interface = false;
_can_write_automation = false;
#ifdef LV2_EXTENDED
_display_interface = 0;
_export_interface = 0;
_inline_display_in_gui = false;
#endif
_max_latency = 0;
_current_latency = 0;
_impl->block_length = _session.get_block_size();
_sample_rate = rate;
_fsample_rate = rate;
_instance_access_feature.URI = "http://lv2plug.in/ns/ext/instance-access";
_data_access_feature.URI = "http://lv2plug.in/ns/ext/data-access";
_make_path_feature.URI = LV2_STATE__makePath;
_log_feature.URI = LV2_LOG__log;
_work_schedule_feature.URI = LV2_WORKER__schedule;
_work_schedule_feature.data = NULL;
_def_state_feature.URI = LV2_STATE_PREFIX "loadDefaultState"; // Post LV2-1.2.0
_def_state_feature.data = NULL;
_block_length_feature.URI = LV2_BUF_SIZE__boundedBlockLength;
_block_length_feature.data = NULL;
const LilvPlugin* plugin = _impl->plugin;
LilvNode* state_iface_uri = lilv_new_uri(_world.world, LV2_STATE__interface);
LilvNode* state_uri = lilv_new_uri(_world.world, LV2_STATE_URI);
_has_state_interface =
// What plugins should have (lv2:extensionData state:Interface)
lilv_plugin_has_extension_data(plugin, state_iface_uri)
// What some outdated/incorrect ones have
|| lilv_plugin_has_feature(plugin, state_uri);
lilv_node_free(state_uri);
lilv_node_free(state_iface_uri);
_features = (LV2_Feature**)calloc (15, sizeof(LV2_Feature*));
_features[0] = &_instance_access_feature;
_features[1] = &_data_access_feature;
_features[2] = &_make_path_feature;
_features[3] = _uri_map.urid_map_feature();
_features[4] = _uri_map.urid_unmap_feature();
_features[5] = &_log_feature;
_features[6] = &_def_state_feature;
_features[7] = &_block_length_feature;
unsigned n_features = 8;
lv2_atom_forge_init(&_impl->forge, _uri_map.urid_map());
lv2_atom_forge_init(&_impl->ui_forge, _uri_map.urid_map());
#ifdef LV2_EXTENDED
_impl->queue_draw = (LV2_Inline_Display*)
malloc (sizeof(LV2_Inline_Display));
_impl->queue_draw->handle = this;
_impl->queue_draw->queue_draw = queue_draw;
_queue_draw_feature.URI = LV2_INLINEDISPLAY__queue_draw;
_queue_draw_feature.data = _impl->queue_draw;
_features[n_features++] = &_queue_draw_feature;
_impl->midnam = (LV2_Midnam*)
malloc (sizeof(LV2_Midnam));
_impl->midnam->handle = this;
_impl->midnam->update = midnam_update;
_midnam_feature.URI = LV2_MIDNAM__update;
_midnam_feature.data = _impl->midnam;
_features[n_features++] = &_midnam_feature;
_impl->bankpatch = (LV2_BankPatch*)
malloc (sizeof(LV2_BankPatch));
_impl->bankpatch->handle = this;
_impl->bankpatch->notify = bankpatch_notify;
_bankpatch_feature.URI = LV2_BANKPATCH__notify;
_bankpatch_feature.data = _impl->bankpatch;
_features[n_features++] = &_bankpatch_feature;
#endif
LV2_URID atom_Int = _uri_map.uri_to_id(LV2_ATOM__Int);
LV2_URID atom_Bool = _uri_map.uri_to_id(LV2_ATOM__Bool);
LV2_URID atom_Long = _uri_map.uri_to_id(LV2_ATOM__Long);
LV2_URID atom_Float = _uri_map.uri_to_id(LV2_ATOM__Float);
static const int32_t _min_block_length = 1; // may happen during split-cycles
static const int32_t _max_block_length = 8192; // max possible (with all engines and during export)
static const int32_t rt_policy = PBD_SCHED_FIFO;
static const int32_t rt_priority = pbd_absolute_rt_priority (PBD_SCHED_FIFO, AudioEngine::instance()->client_real_time_priority () - 1);
static const int32_t hw_concurrency = how_many_dsp_threads ();
/* Consider updating max-block-size whenever the buffersize changes.
* It requires re-instantiating the plugin (which is a non-realtime operation),
* so it should be done lightly and only for plugins that require it.
*
* given that the block-size can change at any time (split-cycles) ardour currently
* does not support plugins that require bufz_fixedBlockLength.
*/
LV2_Options_Option options[] = {
{ LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__minBlockLength),
sizeof(int32_t), atom_Int, &_min_block_length },
{ LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__maxBlockLength),
sizeof(int32_t), atom_Int, &_max_block_length },
{ LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_BUF_SIZE__sequenceSize),
sizeof(int32_t), atom_Int, &_seq_size },
{ LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id(LV2_PARAMETERS__sampleRate),
sizeof(float), atom_Float, &_fsample_rate },
{ LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://lv2plug.in/ns/ext/buf-size#nominalBlockLength"),
sizeof(int32_t), atom_Int, &_impl->block_length },
{ LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://ardour.org/lv2/threads/#schedPolicy"),
sizeof(int32_t), atom_Int, &rt_policy },
{ LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://ardour.org/lv2/threads/#schedPriority"),
sizeof(int32_t), atom_Int, &rt_priority },
{ LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://ardour.org/lv2/threads/#concurrency"),
sizeof(int32_t), atom_Int, &hw_concurrency },
{ LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://lv2plug.in/ns/extensions/ui#backgroundColor"),
sizeof(int32_t), atom_Int, &_ui_background_color },
{ LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://lv2plug.in/ns/extensions/ui#foregroundColor"),
sizeof(int32_t), atom_Int, &_ui_foreground_color },
{ LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://lv2plug.in/ns/extensions/ui#contrastingColor"),
sizeof(int32_t), atom_Int, &_ui_contrasting_color },
{ LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://lv2plug.in/ns/extensions/ui#scaleFactor"),
sizeof(float), atom_Float, &ARDOUR::ui_scale_factor },
{ LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://ardour.org/lv2/theme/#styleBoxy"),
sizeof(int32_t), atom_Bool, &_ui_style_boxy },
{ LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://ardour.org/lv2/theme/#styleFlat"),
sizeof(int32_t), atom_Bool, &_ui_style_flat },
{ LV2_OPTIONS_INSTANCE, 0, _uri_map.uri_to_id("http://kxstudio.sf.net/ns/lv2ext/props#TransientWindowId"),
sizeof(int32_t), atom_Long, &_ui_transient_win_id },
{ LV2_OPTIONS_INSTANCE, 0, 0, 0, 0, NULL }
};
_impl->options = (LV2_Options_Option*) malloc (sizeof (options));
memcpy ((void*) _impl->options, (void*) options, sizeof (options));
_options_feature.URI = LV2_OPTIONS__options;
_options_feature.data = _impl->options;
_features[n_features++] = &_options_feature;
#ifdef LV2_EXTENDED
seen_bankpatch = false;
for (uint32_t chn = 0; chn < 16; ++chn) {
_bankpatch[chn] = UINT32_MAX;
}
#endif
LV2_State_Make_Path* make_path = (LV2_State_Make_Path*)malloc(
sizeof(LV2_State_Make_Path));
make_path->handle = this;
make_path->path = &lv2_state_make_path;
_make_path_feature.data = make_path;
LV2_Log_Log* log = (LV2_Log_Log*)malloc(sizeof(LV2_Log_Log));
log->handle = this;
log->printf = &log_printf;
log->vprintf = &log_vprintf;
_log_feature.data = log;
const size_t ring_size = _session.engine().raw_buffer_size(DataType::MIDI) * NBUFS;
LilvNode* worker_schedule = lilv_new_uri(_world.world, LV2_WORKER__schedule);
if (lilv_plugin_has_feature(plugin, worker_schedule)) {
LV2_Worker_Schedule* schedule = (LV2_Worker_Schedule*)malloc(
sizeof(LV2_Worker_Schedule));
_worker = new Worker(this, ring_size);
schedule->handle = _worker;
schedule->schedule_work = work_schedule;
_work_schedule_feature.data = schedule;
_features[n_features++] = &_work_schedule_feature;
}
lilv_node_free(worker_schedule);
if (_has_state_interface) {
// Create a non-threaded worker for use by state restore
_state_worker = new Worker(this, ring_size, false);
}
_impl->instance = lilv_plugin_instantiate(plugin, rate, _features);
_impl->name = lilv_plugin_get_name(plugin);
_impl->author = lilv_plugin_get_author_name(plugin);
if (_impl->instance == 0) {
error << _("LV2: Failed to instantiate plugin ") << uri() << endmsg;
throw failed_constructor();
}
_instance_access_feature.data = (void*)_impl->instance->lv2_handle;
_data_access_extension_data.extension_data = _impl->instance->lv2_descriptor->extension_data;
_data_access_feature.data = &_data_access_extension_data;
LilvNode* worker_iface_uri = lilv_new_uri(_world.world, LV2_WORKER__interface);
if (lilv_plugin_has_extension_data(plugin, worker_iface_uri)) {
_impl->work_iface = (const LV2_Worker_Interface*)extension_data(
LV2_WORKER__interface);
}
lilv_node_free(worker_iface_uri);
LilvNode* options_iface_uri = lilv_new_uri(_world.world, LV2_OPTIONS__interface);
if (lilv_plugin_has_extension_data(plugin, options_iface_uri)) {
_impl->opts_iface = (const LV2_Options_Interface*)extension_data(
LV2_OPTIONS__interface);
}
lilv_node_free(options_iface_uri);
#ifdef LV2_EXTENDED
_midname_interface = (const LV2_Midnam_Interface*)
extension_data (LV2_MIDNAM__interface);
if (_midname_interface) {
_midnam_dirty = true;
read_midnam ();
}
#endif
if (lilv_plugin_has_feature(plugin, _world.lv2_inPlaceBroken)) {
error << string_compose(
_("LV2: \"%1\" cannot be used, since it cannot do inplace processing."),
lilv_node_as_string(_impl->name)) << endmsg;
lilv_node_free(_impl->name);
lilv_node_free(_impl->author);
throw failed_constructor();
}
LilvNodes* optional_features = lilv_plugin_get_optional_features (plugin);
if (lilv_nodes_contains (optional_features, _world.bufz_coarseBlockLength)) {
_no_sample_accurate_ctrl = true;
}
#ifdef LV2_EXTENDED
if (lilv_nodes_contains (optional_features, _world.lv2_noSampleAccurateCtrl)) {
/* deprecated 2016-Sep-18 in favor of bufz_coarseBlockLength */
_no_sample_accurate_ctrl = true;
}
if (lilv_nodes_contains (optional_features, _world.routing_connectAllOutputs)) {
_connect_all_audio_outputs = true;
}
if (lilv_nodes_contains (optional_features, _world.auto_can_write_automatation)) {
_can_write_automation = true;
}
if (lilv_plugin_has_extension_data(plugin, _world.inline_display_interface)) {
_display_interface = (const LV2_Inline_Display_Interface*) extension_data (LV2_INLINEDISPLAY__interface);
}
#ifndef NDEBUG
else if (extension_data (LV2_INLINEDISPLAY__interface)) {
warning << string_compose(_("LV2: Plugin '%1' has inline-display extension without lv2:extensionData meta-data "), name ()) << endmsg;
}
#endif
if (lilv_nodes_contains (optional_features, _world.inline_display_in_gui)) {
_inline_display_in_gui = true;
}
if (lilv_plugin_has_extension_data(plugin, _world.export_interface)) {
_export_interface = (const LV2_Export_Interface*) extension_data (LV2_EXPORT__interface);
}
#endif
lilv_nodes_free(optional_features);
/* Snapshot default state -- https://lv2plug.in/ns/ext/state#loadDefaultState */
LilvState* state = NULL;
LilvNodes* required_features = lilv_plugin_get_required_features (plugin);
if (lilv_nodes_contains (required_features, _world.state_loadDefaultState)) {
state = lilv_state_new_from_world(_world.world, _uri_map.urid_map(), lilv_plugin_get_uri(_impl->plugin));
}
lilv_nodes_free (required_features);
const uint32_t num_ports = this->num_ports();
for (uint32_t i = 0; i < num_ports; ++i) {
const LilvPort* port = lilv_plugin_get_port_by_index(_impl->plugin, i);
PortFlags flags = 0;
size_t minimumSize = 0;
if (lilv_port_is_a(_impl->plugin, port, _world.lv2_OutputPort)) {
flags |= PORT_OUTPUT;
} else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_InputPort)) {
flags |= PORT_INPUT;
} else {
error << string_compose(
"LV2: \"%1\" port %2 is neither input nor output",
lilv_node_as_string(_impl->name), i) << endmsg;
throw failed_constructor();
}
if (lilv_port_is_a(_impl->plugin, port, _world.lv2_ControlPort)) {
flags |= PORT_CONTROL;
} else if (lilv_port_is_a(_impl->plugin, port, _world.lv2_AudioPort)) {
flags |= PORT_AUDIO;
} else if (lilv_port_is_a(_impl->plugin, port, _world.atom_AtomPort)) {
LilvNodes* buffer_types = lilv_port_get_value(
_impl->plugin, port, _world.atom_bufferType);
LilvNodes* atom_supports = lilv_port_get_value(
_impl->plugin, port, _world.atom_supports);
if (lilv_nodes_contains(buffer_types, _world.atom_Sequence)) {
flags |= PORT_SEQUENCE;
if (lilv_nodes_contains(atom_supports, _world.midi_MidiEvent)) {
flags |= PORT_MIDI;
}
if (lilv_nodes_contains(atom_supports, _world.time_Position)) {
flags |= PORT_POSITION;
}
#ifdef LV2_EXTENDED
if (lilv_nodes_contains(atom_supports, _world.auto_automation_control)) {
flags |= PORT_AUTOCTRL;
}
#endif
if (lilv_nodes_contains(atom_supports, _world.patch_Message)) {
flags |= PORT_PATCHMSG;
if (flags & PORT_INPUT) {
_patch_port_in_index = i;
} else {
_patch_port_out_index = i;
}
}
}
LilvNodes* min_size_v = lilv_port_get_value(_impl->plugin, port, _world.rsz_minimumSize);
LilvNode* min_size = min_size_v ? lilv_nodes_get_first(min_size_v) : NULL;
if (min_size && lilv_node_is_int(min_size)) {
minimumSize = lilv_node_as_int(min_size);
}
lilv_nodes_free(min_size_v);
lilv_nodes_free(buffer_types);
lilv_nodes_free(atom_supports);
} else {
error << string_compose(
"LV2: \"%1\" port %2 has no known data type",
lilv_node_as_string(_impl->name), i) << endmsg;
throw failed_constructor();
}
if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
if (lilv_port_has_property(_impl->plugin, port, _world.ext_causesArtifacts)) {
flags |= PORT_NOAUTO;
}
if (lilv_port_has_property(_impl->plugin, port, _world.ext_notAutomatic)) {
flags |= PORT_NOAUTO;
}
if (lilv_port_has_property(_impl->plugin, port, _world.ext_expensive)) {
flags |= PORT_NOAUTO;
}
}
#ifdef LV2_EXTENDED
if (lilv_port_has_property(_impl->plugin, port, _world.auto_automation_controlled)) {
if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
flags |= PORT_CTRLED;
}
}
if (lilv_port_has_property(_impl->plugin, port, _world.auto_automation_controller)) {
if ((flags & PORT_INPUT) && (flags & PORT_CONTROL)) {
flags |= PORT_CTRLER;
}
}
#endif
_port_flags.push_back(flags);
_port_minimumSize.push_back(minimumSize);
DEBUG_TRACE(DEBUG::LV2, string_compose("port %1 buffer %2 bytes\n", i, minimumSize));
}
_control_data = new float[num_ports];
_shadow_data = new float[num_ports];
_defaults = new float[num_ports];
_ev_buffers = new LV2_Evbuf*[num_ports];
memset(_ev_buffers, 0, sizeof(LV2_Evbuf*) * num_ports);
const bool latent = lilv_plugin_has_latency(plugin);
const uint32_t latency_index = (latent)
? lilv_plugin_get_latency_port_index(plugin)
: 0;
// Build an array of pointers to special parameter buffers
void*** params = new void**[num_ports];
for (uint32_t i = 0; i < num_ports; ++i) {
params[i] = NULL;
}
_impl->designated_input (LV2_TIME__beatsPerMinute, params, (void**)&_bpm_control_port);
_impl->designated_input (LV2_CORE__freeWheeling, params, (void**)&_freewheel_control_port);
const LilvPort* bpmport = lilv_plugin_get_port_by_designation(plugin, _world.lv2_InputPort, _world.time_beatsPerMin);
if (bpmport) {
_bpm_control_port_index = lilv_port_get_index (plugin, bpmport);
}
for (uint32_t i = 0; i < num_ports; ++i) {
const LilvPort* port = lilv_plugin_get_port_by_index(plugin, i);
const LilvNode* sym = lilv_port_get_symbol(plugin, port);
// Store index in map so we can look up index by symbol
_port_indices.insert(std::make_pair(lilv_node_as_string(sym), i));
// Get range and default value if applicable
if (parameter_is_control(i)) {
LilvNode* def;
lilv_port_get_range(plugin, port, &def, NULL, NULL);
_defaults[i] = def ? lilv_node_as_float(def) : 0.0f;
if (lilv_port_has_property (plugin, port, _world.lv2_sampleRate)) {
_defaults[i] *= _session.sample_rate ();
}
lilv_node_free(def);
lilv_instance_connect_port(_impl->instance, i, &_control_data[i]);
if (latent && i == latency_index) {
LilvNode *max;
lilv_port_get_range(_impl->plugin, port, NULL, NULL, &max);
_max_latency = max ? lilv_node_as_float(max) : .02 * _sample_rate;
_latency_control_port = &_control_data[i];
*_latency_control_port = 0;
}
if (parameter_is_input(i)) {
_shadow_data[i] = default_value(i);
if (params[i]) {
*params[i] = (void*)&_shadow_data[i];
}
} else {
_shadow_data[i] = 0;
}
_control_data[i] = _shadow_data[i];
} else {
_defaults[i] = 0.0f;
}
}
delete[] params;
LilvUIs* uis = lilv_plugin_get_uis(plugin);
if (lilv_uis_size(uis) > 0) {
#ifdef HAVE_SUIL
const LilvUI* this_ui = NULL;
const LilvNode* this_ui_type = NULL;
#if ! (defined(__APPLE__) || defined(PLATFORM_WINDOWS))
// Always prefer X11 UIs...
LILV_FOREACH(uis, i, uis) {
const LilvUI* ui = lilv_uis_get(uis, i);
if (lilv_ui_is_a(ui, _world.ui_X11UI) &&
lilv_ui_is_supported (ui,
suil_ui_supported,
_world.ui_GtkUI,
&this_ui_type)) {
this_ui = ui;
break;
}
}
#endif
// Then anything else...
if (this_ui == NULL) {
LILV_FOREACH(uis, i, uis) {
const LilvUI* ui = lilv_uis_get(uis, i);
if (lilv_ui_is_supported (ui,
suil_ui_supported,
_world.ui_GtkUI,
&this_ui_type)) {
this_ui = ui;
break;
}
}
}
// Found one that is supported by SUIL?...
if (this_ui != NULL) {
_impl->ui = this_ui;
_impl->ui_type = this_ui_type;
}
#else
// Look for Gtk native UI
LILV_FOREACH(uis, i, uis) {
const LilvUI* ui = lilv_uis_get(uis, i);
if (lilv_ui_is_a(ui, _world.ui_GtkUI)) {
_impl->ui = ui;
_impl->ui_type = _world.ui_GtkUI;
break;
}
}
#endif
// If Gtk UI is not available, try to find external UI
if (!_impl->ui) {
LILV_FOREACH(uis, i, uis) {
const LilvUI* ui = lilv_uis_get(uis, i);
if (lilv_ui_is_a(ui, _world.ui_externalkx)) {
_impl->ui = ui;
_impl->ui_type = _world.ui_external;
break;