-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathCarlaPluginVST3.cpp
4316 lines (3533 loc) · 152 KB
/
CarlaPluginVST3.cpp
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
// SPDX-FileCopyrightText: 2011-2024 Filipe Coelho <[email protected]>
// SPDX-License-Identifier: GPL-2.0-or-later
/* TODO list
* noexcept safe calls
* paramId vs index
*/
#include "CarlaPluginInternal.hpp"
#include "CarlaEngine.hpp"
#include "CarlaBackendUtils.hpp"
#include "CarlaVst3Utils.hpp"
#include "CarlaPluginUI.hpp"
#ifdef CARLA_OS_MAC
# include "CarlaMacUtils.hpp"
# import <Foundation/Foundation.h>
#endif
#include "water/files/File.h"
#include "water/misc/Time.h"
#if defined(V3_VIEW_PLATFORM_TYPE_NATIVE) && defined(_POSIX_VERSION)
# ifdef CARLA_OS_LINUX
# define CARLA_VST3_POSIX_EPOLL
# include <sys/epoll.h>
# else
# include <sys/event.h>
# include <sys/types.h>
# endif
#endif
#include <atomic>
#include <unordered_map>
CARLA_BACKEND_START_NAMESPACE
// --------------------------------------------------------------------------------------------------------------------
static inline
size_t strlen_utf16(const int16_t* const str)
{
size_t i = 0;
while (str[i] != 0)
++i;
return i;
}
// --------------------------------------------------------------------------------------------------------------------
static inline
void strncpy_utf8(char* const dst, const int16_t* const src, const size_t length)
{
CARLA_SAFE_ASSERT_RETURN(length > 0,);
if (const size_t len = std::min(strlen_utf16(src), length-1U))
{
for (size_t i=0; i<len; ++i)
{
// skip non-ascii chars, unsupported
if (src[i] >= 0x80)
continue;
dst[i] = static_cast<char>(src[i]);
}
dst[len] = 0;
}
else
{
dst[0] = 0;
}
}
// --------------------------------------------------------------------------------------------------------------------
struct v3HostCallback {
virtual ~v3HostCallback() {}
// v3_component_handler
virtual v3_result v3BeginEdit(v3_param_id) = 0;
virtual v3_result v3PerformEdit(v3_param_id, double) = 0;
virtual v3_result v3EndEdit(v3_param_id) = 0;
virtual v3_result v3RestartComponent(int32_t) = 0;
#ifdef V3_VIEW_PLATFORM_TYPE_NATIVE
// v3_plugin_frame
virtual v3_result v3ResizeView(struct v3_plugin_view**, struct v3_view_rect*) = 0;
#endif
};
// --------------------------------------------------------------------------------------------------------------------
struct v3_var {
char type;
uint32_t size;
union {
int64_t i;
double f;
int16_t* s;
void* b;
} value;
};
static void v3_var_cleanup(v3_var& var)
{
switch (var.type)
{
case 's':
std::free(var.value.s);
break;
case 'b':
std::free(var.value.b);
break;
}
carla_zeroStruct(var);
}
struct carla_v3_attribute_list : v3_attribute_list_cpp {
// std::atomic<int> refcounter;
std::unordered_map<std::string, v3_var> vars;
carla_v3_attribute_list()
// : refcounter(1)
{
query_interface = v3_query_interface_static<v3_attribute_list_iid>;
ref = v3_ref_static;
unref = v3_unref_static;
attrlist.set_int = set_int;
attrlist.get_int = get_int;
attrlist.set_float = set_float;
attrlist.get_float = get_float;
attrlist.set_string = set_string;
attrlist.get_string = get_string;
attrlist.set_binary = set_binary;
attrlist.get_binary = get_binary;
}
~carla_v3_attribute_list()
{
for (std::unordered_map<std::string, v3_var>::iterator it = vars.begin(); it != vars.end(); ++it)
v3_var_cleanup(it->second);
}
v3_result add(const char* const id, const v3_var& var)
{
const std::string sid(id);
for (std::unordered_map<std::string, v3_var>::iterator it = vars.begin(); it != vars.end(); ++it)
{
if (it->first == sid)
{
v3_var_cleanup(it->second);
break;
}
}
vars[sid] = var;
return V3_OK;
}
bool get(const char* const id, v3_var& var)
{
const std::string sid(id);
for (std::unordered_map<std::string, v3_var>::iterator it = vars.begin(); it != vars.end(); ++it)
{
if (it->first == sid)
{
var = it->second;
return true;
}
}
return false;
}
private:
static v3_result V3_API set_int(void* const self, const char* const id, const int64_t value)
{
CARLA_SAFE_ASSERT_RETURN(id != nullptr, V3_INVALID_ARG);
carla_v3_attribute_list* const attrlist = *static_cast<carla_v3_attribute_list**>(self);
v3_var var = {};
var.type = 'i';
var.value.i = value;
return attrlist->add(id, var);
}
static v3_result V3_API get_int(void* const self, const char* const id, int64_t* const value)
{
CARLA_SAFE_ASSERT_RETURN(id != nullptr, V3_INVALID_ARG);
carla_v3_attribute_list* const attrlist = *static_cast<carla_v3_attribute_list**>(self);
v3_var var = {};
if (attrlist->get(id, var))
{
*value = var.value.i;
return V3_OK;
}
return V3_INVALID_ARG;
}
static v3_result V3_API set_float(void* const self, const char* const id, const double value)
{
CARLA_SAFE_ASSERT_RETURN(id != nullptr, V3_INVALID_ARG);
carla_v3_attribute_list* const attrlist = *static_cast<carla_v3_attribute_list**>(self);
v3_var var = {};
var.type = 'f';
var.value.f = value;
return attrlist->add(id, var);
}
static v3_result V3_API get_float(void* const self, const char* const id, double* const value)
{
CARLA_SAFE_ASSERT_RETURN(id != nullptr, V3_INVALID_ARG);
carla_v3_attribute_list* const attrlist = *static_cast<carla_v3_attribute_list**>(self);
v3_var var = {};
if (attrlist->get(id, var))
{
*value = var.value.f;
return V3_OK;
}
return V3_INVALID_ARG;
}
static v3_result V3_API set_string(void* const self, const char* const id, const int16_t* const string)
{
CARLA_SAFE_ASSERT_RETURN(id != nullptr, V3_INVALID_ARG);
CARLA_SAFE_ASSERT_RETURN(string != nullptr, V3_INVALID_ARG);
carla_v3_attribute_list* const attrlist = *static_cast<carla_v3_attribute_list**>(self);
const size_t size = sizeof(int16_t) * (strlen_utf16(string) + 1);
int16_t* const s = static_cast<int16_t*>(std::malloc(size));
CARLA_SAFE_ASSERT_RETURN(s != nullptr, V3_NOMEM);
std::memcpy(s, string, size);
v3_var var = {};
var.type = 's';
var.size = size;
var.value.s = s;
return attrlist->add(id, var);
}
static v3_result V3_API get_string(void* const self, const char* const id,
int16_t* const string, const uint32_t size)
{
CARLA_SAFE_ASSERT_RETURN(id != nullptr, V3_INVALID_ARG);
CARLA_SAFE_ASSERT_RETURN(string != nullptr, V3_INVALID_ARG);
CARLA_SAFE_ASSERT_RETURN(size != 0, V3_INVALID_ARG);
carla_v3_attribute_list* const attrlist = *static_cast<carla_v3_attribute_list**>(self);
v3_var var = {};
if (attrlist->get(id, var))
{
CARLA_SAFE_ASSERT_UINT2_RETURN(var.size >= size, var.size, size, V3_INVALID_ARG);
std::memcpy(string, var.value.s, size);
return V3_OK;
}
return V3_INVALID_ARG;
}
static v3_result V3_API set_binary(void* const self, const char* const id,
const void* const data, const uint32_t size)
{
CARLA_SAFE_ASSERT_RETURN(id != nullptr, V3_INVALID_ARG);
CARLA_SAFE_ASSERT_RETURN(data != nullptr, V3_INVALID_ARG);
CARLA_SAFE_ASSERT_RETURN(size != 0, V3_INVALID_ARG);
carla_v3_attribute_list* const attrlist = *static_cast<carla_v3_attribute_list**>(self);
void* const b = std::malloc(size);
CARLA_SAFE_ASSERT_RETURN(b != nullptr, V3_NOMEM);
std::memcpy(b, data, size);
v3_var var = {};
var.type = 'b';
var.size = size;
var.value.b = b;
return attrlist->add(id, var);
}
static v3_result V3_API get_binary(void* const self, const char* const id,
const void** const data, uint32_t* const size)
{
CARLA_SAFE_ASSERT_RETURN(id != nullptr, V3_INVALID_ARG);
carla_v3_attribute_list* const attrlist = *static_cast<carla_v3_attribute_list**>(self);
v3_var var = {};
if (attrlist->get(id, var))
{
*data = var.value.b;
*size = var.size;
return V3_OK;
}
return V3_INVALID_ARG;
}
CARLA_DECLARE_NON_COPYABLE(carla_v3_attribute_list)
CARLA_PREVENT_HEAP_ALLOCATION
};
struct carla_v3_message : v3_message_cpp {
std::atomic<int> refcounter;
carla_v3_attribute_list attrlist;
carla_v3_attribute_list* attrlistptr;
const char* msgId;
carla_v3_message()
: refcounter(1),
attrlistptr(&attrlist),
msgId(nullptr)
{
query_interface = v3_query_interface<carla_v3_message, v3_message_iid>;
ref = v3_ref<carla_v3_message>;
unref = v3_unref<carla_v3_message>;
msg.get_message_id = get_message_id;
msg.set_message_id = set_message_id;
msg.get_attributes = get_attributes;
}
~carla_v3_message()
{
delete[] msgId;
}
private:
static const char* V3_API get_message_id(void* const self)
{
carla_v3_message* const msg = *static_cast<carla_v3_message**>(self);
return msg->msgId;
}
static void V3_API set_message_id(void* const self, const char* const id)
{
carla_v3_message* const msg = *static_cast<carla_v3_message**>(self);
delete[] msg->msgId;
msg->msgId = id != nullptr ? carla_strdup(id) : nullptr;
}
static v3_attribute_list** V3_API get_attributes(void* const self)
{
carla_v3_message* const msg = *static_cast<carla_v3_message**>(self);
return (v3_attribute_list**)&msg->attrlistptr;
}
CARLA_DECLARE_NON_COPYABLE(carla_v3_message)
};
// --------------------------------------------------------------------------------------------------------------------
struct carla_v3_bstream : v3_bstream_cpp {
// to be filled by class producer
void* buffer;
int64_t size;
bool canRead, canWrite;
// used by class consumer
int64_t readPos;
carla_v3_bstream()
: buffer(nullptr),
size(0),
canRead(false),
canWrite(false),
readPos(0)
{
query_interface = v3_query_interface_static<v3_bstream_iid>;
ref = v3_ref_static;
unref = v3_unref_static;
stream.read = read;
stream.write = write;
stream.seek = seek;
stream.tell = tell;
}
private:
static v3_result V3_API read(void* const self, void* const buffer, int32_t num_bytes, int32_t* const bytes_read)
{
carla_v3_bstream* const stream = *static_cast<carla_v3_bstream**>(self);
CARLA_SAFE_ASSERT_RETURN(buffer != nullptr, V3_INVALID_ARG);
CARLA_SAFE_ASSERT_RETURN(num_bytes > 0, V3_INVALID_ARG);
CARLA_SAFE_ASSERT_RETURN(stream->canRead, V3_INVALID_ARG);
if (stream->readPos + num_bytes > stream->size)
num_bytes = stream->size - stream->readPos;
std::memcpy(buffer, static_cast<uint8_t*>(stream->buffer) + stream->readPos, num_bytes);
stream->readPos += num_bytes;
// this is nasty, some plugins do not care about incomplete reads!
if (bytes_read != nullptr)
*bytes_read = num_bytes;
return V3_OK;
}
static v3_result V3_API write(void* const self,
void* const buffer, const int32_t num_bytes, int32_t* const bytes_read)
{
carla_v3_bstream* const stream = *static_cast<carla_v3_bstream**>(self);
CARLA_SAFE_ASSERT_RETURN(buffer != nullptr, V3_INVALID_ARG);
CARLA_SAFE_ASSERT_RETURN(num_bytes > 0, V3_INVALID_ARG);
CARLA_SAFE_ASSERT_RETURN(stream->canWrite, V3_INVALID_ARG);
void* const newbuffer = std::realloc(stream->buffer, stream->size + num_bytes);
CARLA_SAFE_ASSERT_RETURN(newbuffer != nullptr, V3_NOMEM);
std::memcpy(static_cast<uint8_t*>(newbuffer) + stream->size, buffer, num_bytes);
stream->buffer = newbuffer;
stream->size += num_bytes;
// this is nasty, some plugins do not care about incomplete writes!
if (bytes_read != nullptr)
*bytes_read = num_bytes;
return V3_OK;
}
static v3_result V3_API seek(void* const self, const int64_t pos, const int32_t seek_mode, int64_t* const result)
{
carla_v3_bstream* const stream = *static_cast<carla_v3_bstream**>(self);
CARLA_SAFE_ASSERT_RETURN(stream->canRead, V3_INVALID_ARG);
switch (seek_mode)
{
case V3_SEEK_SET:
CARLA_SAFE_ASSERT_INT2_RETURN(pos <= stream->size, pos, stream->size, V3_INVALID_ARG);
stream->readPos = pos;
break;
case V3_SEEK_CUR:
CARLA_SAFE_ASSERT_INT2_RETURN(stream->readPos + pos <= stream->size, pos, stream->size, V3_INVALID_ARG);
stream->readPos = stream->readPos + pos;
break;
case V3_SEEK_END:
CARLA_SAFE_ASSERT_INT2_RETURN(pos <= stream->size, pos, stream->size, V3_INVALID_ARG);
stream->readPos = stream->size - pos;
break;
default:
return V3_INVALID_ARG;
}
if (result != nullptr)
*result = stream->readPos;
return V3_OK;
}
static v3_result V3_API tell(void* const self, int64_t* const pos)
{
carla_v3_bstream* const stream = *static_cast<carla_v3_bstream**>(self);
CARLA_SAFE_ASSERT_RETURN(pos != nullptr, V3_INVALID_ARG);
CARLA_SAFE_ASSERT_RETURN(stream->canRead, V3_INVALID_ARG);
*pos = stream->readPos;
return V3_OK;
}
CARLA_DECLARE_NON_COPYABLE(carla_v3_bstream)
CARLA_PREVENT_HEAP_ALLOCATION
};
// --------------------------------------------------------------------------------------------------------------------
struct carla_v3_host_application : v3_host_application_cpp {
carla_v3_host_application()
{
query_interface = v3_query_interface_static<v3_host_application_iid>;
ref = v3_ref_static;
unref = v3_unref_static;
app.get_name = get_name;
app.create_instance = create_instance;
}
private:
static v3_result V3_API get_name(void*, v3_str_128 name)
{
static const char hostname[] = "Carla\0";
for (size_t i=0; i<sizeof(hostname); ++i)
name[i] = hostname[i];
return V3_OK;
}
static v3_result V3_API create_instance(void*, v3_tuid cid, v3_tuid iid, void** const obj)
{
if (v3_tuid_match(cid, v3_message_iid) && (v3_tuid_match(iid, v3_message_iid) ||
v3_tuid_match(iid, v3_funknown_iid)))
{
*obj = v3_create_class_ptr<carla_v3_message>();
return V3_OK;
}
carla_stdout("TODO carla_create_instance %s", tuid2str(cid));
return V3_NOT_IMPLEMENTED;
}
CARLA_DECLARE_NON_COPYABLE(carla_v3_host_application)
CARLA_PREVENT_HEAP_ALLOCATION
};
// --------------------------------------------------------------------------------------------------------------------
struct carla_v3_input_param_value_queue : v3_param_value_queue_cpp {
const v3_param_id paramId;
int8_t numUsed;
struct Point {
int32_t offset;
float value;
} points[32];
carla_v3_input_param_value_queue(const v3_param_id pId)
: paramId(pId),
numUsed(0)
{
query_interface = v3_query_interface_static<v3_param_value_queue_iid>;
ref = v3_ref_static;
unref = v3_unref_static;
queue.get_param_id = get_param_id;
queue.get_point_count = get_point_count;
queue.get_point = get_point;
queue.add_point = add_point;
}
private:
static v3_param_id V3_API get_param_id(void* self)
{
carla_v3_input_param_value_queue* const me = *static_cast<carla_v3_input_param_value_queue**>(self);
return me->paramId;
}
static int32_t V3_API get_point_count(void* self)
{
carla_v3_input_param_value_queue* const me = *static_cast<carla_v3_input_param_value_queue**>(self);
return me->numUsed;
}
static v3_result V3_API get_point(void* const self,
const int32_t idx, int32_t* const sample_offset, double* const value)
{
carla_v3_input_param_value_queue* const me = *static_cast<carla_v3_input_param_value_queue**>(self);
CARLA_SAFE_ASSERT_INT2_RETURN(idx < me->numUsed, idx, me->numUsed, V3_INVALID_ARG);
*sample_offset = me->points[idx].offset;
*value = me->points[idx].value;
return V3_OK;
}
static v3_result V3_API add_point(void*, int32_t, double, int32_t*)
{
// there is nothing here for input parameters, plugins are not meant to call this!
return V3_NOT_IMPLEMENTED;
}
CARLA_DECLARE_NON_COPYABLE(carla_v3_input_param_value_queue)
};
struct carla_v3_input_param_changes : v3_param_changes_cpp {
const uint32_t paramCount;
struct UpdatedParam {
bool updated;
float value;
}* const updatedParams;
carla_v3_input_param_value_queue** const queue;
// data given to plugins
v3_param_value_queue*** pluginExposedQueue;
int32_t pluginExposedCount;
carla_v3_input_param_changes(const PluginParameterData& paramData)
: paramCount(paramData.count),
updatedParams(new UpdatedParam[paramData.count]),
queue(new carla_v3_input_param_value_queue*[paramData.count]),
pluginExposedQueue(new v3_param_value_queue**[paramData.count]),
pluginExposedCount(0)
{
query_interface = v3_query_interface_static<v3_param_changes_iid>;
ref = v3_ref_static;
unref = v3_unref_static;
changes.get_param_count = get_param_count;
changes.get_param_data = get_param_data;
changes.add_param_data = add_param_data;
CARLA_ASSERT(paramCount != 0);
carla_zeroStructs(updatedParams, paramCount);
for (uint32_t i=0; i<paramCount; ++i)
queue[i] = new carla_v3_input_param_value_queue(static_cast<v3_param_id>(paramData.data[i].rindex));
}
~carla_v3_input_param_changes()
{
for (uint32_t i=0; i<paramCount; ++i)
delete queue[i];
delete[] updatedParams;
delete[] pluginExposedQueue;
delete[] queue;
}
// called during start of process, gathering all parameter update requests so far
void init()
{
for (uint32_t i=0; i<paramCount; ++i)
{
if (updatedParams[i].updated)
{
queue[i]->numUsed = 1;
queue[i]->points[0].offset = 0;
queue[i]->points[0].value = updatedParams[i].value;
}
else
{
queue[i]->numUsed = 0;
}
}
}
// called just before plugin processing, creating local queue
void prepare()
{
int32_t count = 0;
for (uint32_t i=0; i<paramCount; ++i)
{
if (queue[i]->numUsed)
pluginExposedQueue[count++] = (v3_param_value_queue**)&queue[i];
}
pluginExposedCount = count;
}
// called when a parameter is set from non-rt thread
void setParamValue(const uint32_t index, const float value) noexcept
{
updatedParams[index].value = value;
updatedParams[index].updated = true;
}
// called as response to MIDI CC
void setParamValueRT(const uint32_t index, const int32_t offset, const float value) noexcept
{
static constexpr const int8_t kQueuePointSize = sizeof(queue[0]->points)/sizeof(queue[0]->points[0]);
if (queue[index]->numUsed < kQueuePointSize)
{
// still has space, add in queue
carla_v3_input_param_value_queue::Point& point(queue[index]->points[queue[index]->numUsed++]);
point.offset = offset;
point.value = value;
}
else
{
// points are full, replace last one
carla_v3_input_param_value_queue::Point& point(queue[index]->points[queue[index]->numUsed - 1]);
point.offset = offset;
point.value = value;
}
}
private:
static int32_t V3_API get_param_count(void* const self)
{
carla_v3_input_param_changes* const me = *static_cast<carla_v3_input_param_changes**>(self);
return me->pluginExposedCount;
}
static v3_param_value_queue** V3_API get_param_data(void* const self, const int32_t index)
{
carla_v3_input_param_changes* const me = *static_cast<carla_v3_input_param_changes**>(self);
return me->pluginExposedQueue[index];
}
static v3_param_value_queue** V3_API add_param_data(void*, const v3_param_id*, int32_t*)
{
// there is nothing here for input parameters, plugins are not meant to call this!
return nullptr;
}
CARLA_DECLARE_NON_COPYABLE(carla_v3_input_param_changes)
};
// --------------------------------------------------------------------------------------------------------------------
struct carla_v3_output_param_value_queue : v3_param_value_queue_cpp {
const v3_param_id paramId;
bool used;
int32_t offset;
double value;
carla_v3_output_param_value_queue(const v3_param_id pId)
: paramId(pId),
used(false),
offset(0),
value(0.0)
{
query_interface = v3_query_interface_static<v3_param_value_queue_iid>;
ref = v3_ref_static;
unref = v3_unref_static;
queue.get_param_id = get_param_id;
queue.get_point_count = get_point_count;
queue.get_point = get_point;
queue.add_point = add_point;
}
void init()
{
used = false;
offset = 0;
value = 0.0;
}
private:
static v3_param_id V3_API get_param_id(void* self)
{
carla_v3_output_param_value_queue* const me = *static_cast<carla_v3_output_param_value_queue**>(self);
return me->paramId;
}
static int32_t V3_API get_point_count(void* self)
{
carla_v3_output_param_value_queue* const me = *static_cast<carla_v3_output_param_value_queue**>(self);
return me->used ? 1 : 0;
}
static v3_result V3_API get_point(void* const self,
const int32_t index, int32_t* const sample_offset, double* const value)
{
carla_v3_output_param_value_queue* const me = *static_cast<carla_v3_output_param_value_queue**>(self);
CARLA_SAFE_ASSERT_RETURN(me->used, V3_INVALID_ARG);
CARLA_SAFE_ASSERT_INT_RETURN(index == 0, index, V3_INVALID_ARG);
*sample_offset = me->offset;
*value = me->value;
return V3_OK;
}
static v3_result V3_API add_point(void* const self,
const int32_t sample_offset, const double value, int32_t* const index)
{
carla_v3_output_param_value_queue* const me = *static_cast<carla_v3_output_param_value_queue**>(self);
CARLA_SAFE_ASSERT_INT_RETURN(sample_offset >= 0, sample_offset, V3_INVALID_ARG);
CARLA_SAFE_ASSERT_RETURN(value >= 0 && value <= 1, V3_INVALID_ARG);
CARLA_SAFE_ASSERT_RETURN(index != nullptr, V3_INVALID_ARG);
me->offset = sample_offset;
me->value = value;
*index = 0;
return V3_OK;
}
CARLA_DECLARE_NON_COPYABLE(carla_v3_output_param_value_queue)
};
struct carla_v3_output_param_changes : v3_param_changes_cpp {
const uint32_t numParameters;
int32_t numParametersUsed;
bool* const parametersUsed;
carla_v3_output_param_value_queue** const queue;
std::unordered_map<v3_param_id, int32_t> paramIds;
carla_v3_output_param_changes(const PluginParameterData& paramData)
: numParameters(paramData.count),
numParametersUsed(0),
parametersUsed(new bool[paramData.count]),
queue(new carla_v3_output_param_value_queue*[paramData.count])
{
query_interface = v3_query_interface_static<v3_param_changes_iid>;
ref = v3_ref_static;
unref = v3_unref_static;
changes.get_param_count = get_param_count;
changes.get_param_data = get_param_data;
changes.add_param_data = add_param_data;
carla_zeroStructs(parametersUsed, numParameters);
for (uint32_t i=0; i<numParameters; ++i)
{
const v3_param_id paramId = paramData.data[i].rindex;
queue[i] = new carla_v3_output_param_value_queue(paramId);
paramIds[paramId] = i;
}
}
~carla_v3_output_param_changes()
{
for (uint32_t i=0; i<numParameters; ++i)
delete queue[i];
delete[] parametersUsed;
delete[] queue;
}
void prepare()
{
numParametersUsed = 0;
carla_zeroStructs(parametersUsed, numParameters);
}
private:
static int32_t V3_API get_param_count(void*)
{
// there is nothing here for output parameters, plugins are not meant to call this!
return 0;
}
static v3_param_value_queue** V3_API get_param_data(void*, int32_t)
{
// there is nothing here for output parameters, plugins are not meant to call this!
return nullptr;
}
static v3_param_value_queue** V3_API add_param_data(void* const self,
const v3_param_id* const paramIdPtr,
int32_t* const index)
{
carla_v3_output_param_changes* const me = *static_cast<carla_v3_output_param_changes**>(self);
CARLA_SAFE_ASSERT_RETURN(paramIdPtr != nullptr, nullptr);
const v3_param_id paramId = *paramIdPtr;
if (me->paramIds.find(paramId) == me->paramIds.end())
return nullptr;
const int32_t paramIndex = me->paramIds[paramId];
CARLA_SAFE_ASSERT_RETURN(!me->parametersUsed[paramIndex], nullptr);
*index = me->numParametersUsed++;
me->parametersUsed[paramIndex] = true;
me->queue[paramIndex]->init();
return (v3_param_value_queue**)&me->queue[paramIndex];
}
CARLA_DECLARE_NON_COPYABLE(carla_v3_output_param_changes)
};
// --------------------------------------------------------------------------------------------------------------------
struct carla_v3_input_event_list : v3_event_list_cpp {
v3_event* const events;
uint16_t numEvents;
carla_v3_input_event_list()
: events(new v3_event[kPluginMaxMidiEvents]),
numEvents(0)
{
query_interface = v3_query_interface_static<v3_event_list_iid>;
ref = v3_ref_static;
unref = v3_unref_static;
list.get_event_count = get_event_count;
list.get_event = get_event;
list.add_event = add_event;
}
~carla_v3_input_event_list()
{
delete[] events;
}
private:
static uint32_t V3_API get_event_count(void* const self)
{
const carla_v3_input_event_list* const me = *static_cast<const carla_v3_input_event_list**>(self);
return me->numEvents;
}
static v3_result V3_API get_event(void* const self, const int32_t index, v3_event* const event)
{
const carla_v3_input_event_list* const me = *static_cast<const carla_v3_input_event_list**>(self);
CARLA_SAFE_ASSERT_RETURN(index < static_cast<int32_t>(me->numEvents), V3_INVALID_ARG);
std::memcpy(event, &me->events[index], sizeof(v3_event));
return V3_OK;
}
static v3_result V3_API add_event(void*, v3_event*)
{
// there is nothing here for input events, plugins are not meant to call this!
return V3_NOT_IMPLEMENTED;
}
CARLA_DECLARE_NON_COPYABLE(carla_v3_input_event_list)
};
// --------------------------------------------------------------------------------------------------------------------
struct carla_v3_output_event_list : v3_event_list_cpp {
carla_v3_output_event_list()
{
query_interface = v3_query_interface_static<v3_event_list_iid>;
ref = v3_ref_static;
unref = v3_unref_static;
list.get_event_count = get_event_count;
list.get_event = get_event;
list.add_event = add_event;
}
private:
static uint32_t V3_API get_event_count(void*)
{
carla_debug("TODO %s", __PRETTY_FUNCTION__);
// there is nothing here for output events, plugins are not meant to call this!
return 0;
}
static v3_result V3_API get_event(void*, int32_t, v3_event*)
{
carla_debug("TODO %s", __PRETTY_FUNCTION__);
// there is nothing here for output events, plugins are not meant to call this!
return V3_NOT_IMPLEMENTED;
}
static v3_result V3_API add_event(void*, v3_event*)
{
carla_debug("TODO %s", __PRETTY_FUNCTION__);
return V3_NOT_IMPLEMENTED;
}
CARLA_DECLARE_NON_COPYABLE(carla_v3_output_event_list)
};
// --------------------------------------------------------------------------------------------------------------------
#ifdef V3_VIEW_PLATFORM_TYPE_NATIVE
struct HostTimer {
v3_timer_handler** handler;
uint64_t periodInMs;
uint64_t lastCallTimeInMs;
};
static constexpr const HostTimer kTimerFallback = { nullptr, 0, 0 };
static /* */ HostTimer kTimerFallbackNC = { nullptr, 0, 0 };
#ifdef _POSIX_VERSION
struct HostPosixFileDescriptor {
v3_event_handler** handler;
int hostfd;
int pluginfd;
};
static constexpr const HostPosixFileDescriptor kPosixFileDescriptorFallback = { nullptr, -1, -1 };
static /* */ HostPosixFileDescriptor kPosixFileDescriptorFallbackNC = { nullptr, -1, -1 };
#endif
struct carla_v3_run_loop : v3_run_loop_cpp {
LinkedList<HostTimer> timers;
#ifdef _POSIX_VERSION
LinkedList<HostPosixFileDescriptor> posixfds;
#endif
carla_v3_run_loop()
{
query_interface = v3_query_interface_static<v3_run_loop_iid>;
ref = v3_ref_static;
unref = v3_unref_static;
loop.register_event_handler = register_event_handler;
loop.unregister_event_handler = unregister_event_handler;
loop.register_timer = register_timer;
loop.unregister_timer = unregister_timer;
}
private:
static v3_result V3_API register_event_handler(void* const self, v3_event_handler** const handler, const int fd)
{
#ifdef _POSIX_VERSION
carla_v3_run_loop* const loop = *static_cast<carla_v3_run_loop**>(self);
#ifdef CARLA_VST3_POSIX_EPOLL
const int hostfd = ::epoll_create1(0);
#else
const int hostfd = ::kqueue();
#endif
CARLA_SAFE_ASSERT_RETURN(hostfd >= 0, V3_INTERNAL_ERR);
#ifdef CARLA_VST3_POSIX_EPOLL
struct ::epoll_event ev = {};
ev.events = EPOLLIN|EPOLLOUT;
ev.data.fd = fd;
if (::epoll_ctl(hostfd, EPOLL_CTL_ADD, fd, &ev) < 0)
{
::close(hostfd);
return V3_INTERNAL_ERR;
}
#endif
const HostPosixFileDescriptor posixfd = { handler, hostfd, fd };
return loop->posixfds.append(posixfd) ? V3_OK : V3_NOMEM;
#else
return V3_NOT_IMPLEMENTED;