-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathcarla-vst.cpp
1310 lines (1074 loc) · 42.3 KB
/
carla-vst.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
/*
* Carla Native Plugins
* Copyright (C) 2013-2019 Filipe Coelho <[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 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.
*
* For a full copy of the GNU General Public License see the doc/GPL.txt file.
*/
#ifdef __WINE__
#error This file is not supposed to be built with wine!
#endif
#ifndef CARLA_PLUGIN_SYNTH
#error CARLA_PLUGIN_SYNTH undefined
#endif
#ifndef CARLA_VST_SHELL
#ifndef CARLA_PLUGIN_PATCHBAY
#error CARLA_PLUGIN_PATCHBAY undefined
#endif
#if defined(CARLA_PLUGIN_64CH) || defined(CARLA_PLUGIN_32CH) || defined(CARLA_PLUGIN_16CH)
#if ! CARLA_PLUGIN_SYNTH
#error CARLA_PLUGIN_16/32/64CH requires CARLA_PLUGIN_SYNTH
#endif
#endif
#endif
#define CARLA_NATIVE_PLUGIN_VST
#include "carla-base.cpp"
#include "carla-vst.hpp"
#include "water/files/File.h"
#include "CarlaMathUtils.hpp"
#include "CarlaVstUtils.hpp"
#ifdef USING_JUCE
# if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wconversion"
# pragma GCC diagnostic ignored "-Weffc++"
# pragma GCC diagnostic ignored "-Wsign-conversion"
# pragma GCC diagnostic ignored "-Wundef"
# pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
# endif
# include "AppConfig.h"
# include "juce_events/juce_events.h"
# if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
# pragma GCC diagnostic pop
# endif
#endif
static uint32_t d_lastBufferSize = 0;
static double d_lastSampleRate = 0.0;
static const int32_t kBaseUniqueID = CCONST('C', 'r', 'l', 'a');
static const int32_t kVstMidiEventSize = static_cast<int32_t>(sizeof(VstMidiEvent));
#ifdef CARLA_VST_SHELL
static const int32_t kShellUniqueID = CCONST('C', 'r', 'l', 's');
#else
static const int32_t kNumParameters = 100;
#endif
static const bool kIsUsingUILauncher = isUsingUILauncher();
// --------------------------------------------------------------------------------------------------------------------
// Carla Internal Plugin API exposed as VST plugin
class NativePlugin
{
public:
static const uint32_t kMaxMidiEvents = 512;
NativePlugin(AEffect* const effect, const NativePluginDescriptor* desc)
: fEffect(effect),
fHandle(nullptr),
fHost(),
fDescriptor(desc),
fBufferSize(d_lastBufferSize),
fSampleRate(d_lastSampleRate),
fIsActive(false),
fMidiEventCount(0),
fTimeInfo(),
fVstRect(),
fUiLauncher(nullptr),
fHostType(kHostTypeNull),
fMidiOutEvents(),
#ifdef USING_JUCE
fJuceInitialiser(),
#endif
fStateChunk(nullptr)
{
fHost.handle = this;
fHost.uiName = carla_strdup("CarlaVST");
fHost.uiParentId = 0;
std::memset(fProgramName, 0, sizeof(fProgramName));
std::strcpy(fProgramName, "Default");
// find resource dir
using water::File;
using water::String;
File curExe = File::getSpecialLocation(File::currentExecutableFile).getLinkedTarget();
File resDir = curExe.getSiblingFile("resources");
// FIXME: proper fallback path for other OSes
if (! resDir.exists())
resDir = File("/usr/local/share/carla/resources");
if (! resDir.exists())
resDir = File("/usr/share/carla/resources");
// find host type
const String hostFilename(File::getSpecialLocation(File::hostApplicationPath).getFileName());
/**/ if (hostFilename.startsWith("ardour"))
fHostType = kHostTypeArdour;
else if (hostFilename.startsWith("Bitwig"))
fHostType = kHostTypeBitwig;
fHost.resourceDir = carla_strdup(resDir.getFullPathName().toRawUTF8());
fHost.get_buffer_size = host_get_buffer_size;
fHost.get_sample_rate = host_get_sample_rate;
fHost.is_offline = host_is_offline;
fHost.get_time_info = host_get_time_info;
fHost.write_midi_event = host_write_midi_event;
fHost.ui_parameter_changed = host_ui_parameter_changed;
fHost.ui_custom_data_changed = host_ui_custom_data_changed;
fHost.ui_closed = host_ui_closed;
fHost.ui_open_file = host_ui_open_file;
fHost.ui_save_file = host_ui_save_file;
fHost.dispatcher = host_dispatcher;
fVstRect.top = 0;
fVstRect.left = 0;
if (kIsUsingUILauncher)
{
fVstRect.bottom = ui_launcher_res::carla_uiHeight;
fVstRect.right = ui_launcher_res::carla_uiWidth;
}
else
{
fVstRect.bottom = 712;
fVstRect.right = 1024;
}
init();
}
~NativePlugin()
{
if (fIsActive)
{
// host has not de-activated the plugin yet, nasty!
fIsActive = false;
if (fDescriptor->deactivate != nullptr)
fDescriptor->deactivate(fHandle);
}
if (fDescriptor->cleanup != nullptr && fHandle != nullptr)
fDescriptor->cleanup(fHandle);
fHandle = nullptr;
if (fStateChunk != nullptr)
{
std::free(fStateChunk);
fStateChunk = nullptr;
}
if (fHost.uiName != nullptr)
{
delete[] fHost.uiName;
fHost.uiName = nullptr;
}
if (fHost.resourceDir != nullptr)
{
delete[] fHost.resourceDir;
fHost.resourceDir = nullptr;
}
}
bool init()
{
if (fDescriptor->instantiate == nullptr || fDescriptor->process == nullptr)
{
carla_stderr("Plugin is missing something...");
return false;
}
fHandle = fDescriptor->instantiate(&fHost);
CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
carla_zeroStructs(fMidiEvents, kMaxMidiEvents);
carla_zeroStruct(fTimeInfo);
return true;
}
const NativePluginDescriptor* getDescriptor() const noexcept
{
return fDescriptor;
}
// -------------------------------------------------------------------
intptr_t vst_dispatcher(const int32_t opcode,
const int32_t index, const intptr_t value, void* const ptr, const float opt)
{
CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
intptr_t ret = 0;
switch (opcode)
{
case effGetProgram:
return 0;
case effSetProgramName:
if (char* const programName = (char*)ptr)
{
std::strncpy(fProgramName, programName, 32);
return 1;
}
break;
case effGetProgramName:
if (char* const programName = (char*)ptr)
{
std::strncpy(programName, fProgramName, 23);
programName[23] = '\0';
return 1;
}
break;
case effGetProgramNameIndexed:
if (char* const programName = (char*)ptr)
{
std::strncpy(programName, fProgramName, 23);
programName[23] = '\0';
return 1;
}
break;
case effGetParamDisplay:
CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
#ifndef CARLA_VST_SHELL
CARLA_SAFE_ASSERT_RETURN(index < kNumParameters, 0);
#endif
if (char* const cptr = (char*)ptr)
{
const uint32_t uindex = static_cast<uint32_t>(index);
CARLA_SAFE_ASSERT_RETURN(uindex < fDescriptor->paramIns, 0);
const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, uindex);
CARLA_SAFE_ASSERT_RETURN(param != nullptr, 0);
float paramValue = fDescriptor->get_parameter_value(fHandle, uindex);
if (param->hints & NATIVE_PARAMETER_IS_BOOLEAN)
{
const NativeParameterRanges& ranges(param->ranges);
const float midRange = ranges.min + (ranges.max - ranges.min) / 2.0f;
paramValue = paramValue > midRange ? ranges.max : ranges.min;
}
else if (param->hints & NATIVE_PARAMETER_IS_INTEGER)
{
paramValue = std::round(paramValue);
}
for (uint32_t i = 0; i < param->scalePointCount; ++i)
{
const NativeParameterScalePoint& scalePoint(param->scalePoints[uindex]);
if (carla_isNotEqual(paramValue, scalePoint.value))
continue;
std::strncpy(cptr, scalePoint.label, 23);
cptr[23] = '\0';
return 1;
}
if (param->hints & NATIVE_PARAMETER_IS_INTEGER)
{
std::snprintf(cptr, 23, "%d%s%s",
static_cast<int>(paramValue),
param->unit != nullptr && param->unit[0] != '\0' ? " " : "",
param->unit != nullptr && param->unit[0] != '\0' ? param->unit : "");
cptr[23] = '\0';
}
else
{
std::snprintf(cptr, 23, "%.12g%s%s",
static_cast<double>(paramValue),
param->unit != nullptr && param->unit[0] != '\0' ? " " : "",
param->unit != nullptr && param->unit[0] != '\0' ? param->unit : "");
cptr[23] = '\0';
}
return 1;
}
break;
case effGetParamName:
CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
#ifndef CARLA_VST_SHELL
CARLA_SAFE_ASSERT_RETURN(index < kNumParameters, 0);
#endif
if (char* const cptr = (char*)ptr)
{
const uint32_t uindex = static_cast<uint32_t>(index);
CARLA_SAFE_ASSERT_RETURN(uindex < fDescriptor->paramIns, 0);
const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, uindex);
CARLA_SAFE_ASSERT_RETURN(param != nullptr, 0);
std::strncpy(cptr, param->name, 15);
cptr[15] = '\0';
return 1;
}
return 0;
case effSetSampleRate:
CARLA_SAFE_ASSERT_RETURN(opt > 0.0f, 0);
if (carla_isEqual(fSampleRate, static_cast<double>(opt)))
return 0;
fSampleRate = opt;
if (fDescriptor->dispatcher != nullptr)
fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, opt);
break;
case effSetBlockSize:
CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
if (fBufferSize == static_cast<uint32_t>(value))
return 0;
fBufferSize = static_cast<uint32_t>(value);
if (fDescriptor->dispatcher != nullptr)
fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, value, nullptr, 0.0f);
break;
case effMainsChanged:
if (value != 0)
{
fMidiEventCount = 0;
carla_zeroStruct(fTimeInfo);
// tell host we want MIDI events
if (fDescriptor->midiIns > 0)
hostCallback(audioMasterWantMidi);
// deactivate for possible changes
if (fDescriptor->deactivate != nullptr && fIsActive)
fDescriptor->deactivate(fHandle);
// check if something changed
const uint32_t bufferSize = static_cast<uint32_t>(hostCallback(audioMasterGetBlockSize));
const double sampleRate = static_cast<double>(hostCallback(audioMasterGetSampleRate));
if (bufferSize != 0 && fBufferSize != bufferSize && (fHostType != kHostTypeArdour || fBufferSize == 0))
{
fBufferSize = bufferSize;
if (fDescriptor->dispatcher != nullptr)
fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, bufferSize, nullptr, 0.0f);
}
if (carla_isNotZero(sampleRate) && carla_isNotEqual(fSampleRate, sampleRate))
{
fSampleRate = sampleRate;
if (fDescriptor->dispatcher != nullptr)
fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, (float)sampleRate);
}
if (fDescriptor->activate != nullptr)
fDescriptor->activate(fHandle);
fIsActive = true;
}
else
{
CARLA_SAFE_ASSERT_BREAK(fIsActive);
if (fDescriptor->deactivate != nullptr)
fDescriptor->deactivate(fHandle);
fIsActive = false;
}
break;
case effEditGetRect:
*(ERect**)ptr = &fVstRect;
ret = 1;
break;
case effEditOpen:
if (fDescriptor->ui_show != nullptr)
{
if (kIsUsingUILauncher)
{
destoryUILauncher(fUiLauncher);
fUiLauncher = createUILauncher((intptr_t)ptr, fDescriptor, fHandle);
}
else
{
char strBuf[0xff+1];
std::snprintf(strBuf, 0xff, P_INTPTR, (intptr_t)ptr);
strBuf[0xff] = '\0';
// set CARLA_PLUGIN_EMBED_WINID for external process
carla_setenv("CARLA_PLUGIN_EMBED_WINID", strBuf);
// show UI now
fDescriptor->ui_show(fHandle, true);
// reset CARLA_PLUGIN_EMBED_WINID just in case
carla_setenv("CARLA_PLUGIN_EMBED_WINID", "0");
}
ret = 1;
}
break;
case effEditClose:
if (fDescriptor->ui_show != nullptr)
{
if (kIsUsingUILauncher)
{
destoryUILauncher(fUiLauncher);
fUiLauncher = nullptr;
}
else
{
fDescriptor->ui_show(fHandle, false);
}
ret = 1;
}
break;
case effEditIdle:
if (fUiLauncher != nullptr)
idleUILauncher(fUiLauncher);
if (fDescriptor->ui_idle != nullptr)
fDescriptor->ui_idle(fHandle);
break;
case effGetChunk:
if (ptr == nullptr || fDescriptor->get_state == nullptr)
return 0;
if (fStateChunk != nullptr)
std::free(fStateChunk);
fStateChunk = fDescriptor->get_state(fHandle);
if (fStateChunk == nullptr)
return 0;
ret = static_cast<intptr_t>(std::strlen(fStateChunk)+1);
*(void**)ptr = fStateChunk;
break;
case effSetChunk:
if (value <= 0 || fDescriptor->set_state == nullptr)
return 0;
if (value == 1)
return 1;
if (const char* const state = (const char*)ptr)
{
fDescriptor->set_state(fHandle, state);
ret = 1;
}
break;
case effProcessEvents:
if (! fIsActive)
{
// host has not activated the plugin yet, nasty!
vst_dispatcher(effMainsChanged, 0, 1, nullptr, 0.0f);
}
if (const VstEvents* const events = (const VstEvents*)ptr)
{
if (events->numEvents == 0)
break;
for (int i=0, count=events->numEvents; i < count; ++i)
{
const VstMidiEvent* const vstMidiEvent((const VstMidiEvent*)events->events[i]);
if (vstMidiEvent == nullptr)
break;
if (vstMidiEvent->type != kVstMidiType || vstMidiEvent->deltaFrames < 0)
continue;
if (fMidiEventCount >= kMaxMidiEvents)
break;
const uint32_t j(fMidiEventCount++);
fMidiEvents[j].port = 0;
fMidiEvents[j].time = static_cast<uint32_t>(vstMidiEvent->deltaFrames);
fMidiEvents[j].size = 3;
for (uint32_t k=0; k<3; ++k)
fMidiEvents[j].data[k] = static_cast<uint8_t>(vstMidiEvent->midiData[k]);
}
}
break;
case effCanBeAutomated:
ret = 1;
break;
case effCanDo:
if (const char* const canDo = (const char*)ptr)
{
if (std::strcmp(canDo, "receiveVstEvents") == 0 || std::strcmp(canDo, "receiveVstMidiEvent") == 0)
{
if (fDescriptor->midiIns == 0)
return -1;
return 1;
}
if (std::strcmp(canDo, "sendVstEvents") == 0 || std::strcmp(canDo, "sendVstMidiEvent") == 0)
{
if (fDescriptor->midiOuts == 0)
return -1;
return 1;
}
if (std::strcmp(canDo, "receiveVstTimeInfo") == 0)
return 1;
}
break;
}
return ret;
}
float vst_getParameter(const int32_t index)
{
CARLA_SAFE_ASSERT_RETURN(index >= 0, 0.0f);
const uint32_t uindex = static_cast<uint32_t>(index);
CARLA_SAFE_ASSERT_RETURN(uindex < fDescriptor->paramIns, 0.0f);
const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, uindex);
CARLA_SAFE_ASSERT_RETURN(param != nullptr, 0);
const float realValue = fDescriptor->get_parameter_value(fHandle, uindex);
return (realValue - param->ranges.min) / (param->ranges.max - param->ranges.min);
}
void vst_setParameter(const int32_t index, const float value)
{
CARLA_SAFE_ASSERT_RETURN(index >= 0,);
const uint32_t uindex = static_cast<uint32_t>(index);
CARLA_SAFE_ASSERT_RETURN(uindex < fDescriptor->paramIns,);
const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, uindex);
CARLA_SAFE_ASSERT_RETURN(param != nullptr,);
float realValue;
if (param->hints & NATIVE_PARAMETER_IS_BOOLEAN)
{
realValue = value > 0.5f ? param->ranges.max : param->ranges.min;
}
else
{
realValue = param->ranges.min + ((param->ranges.max - param->ranges.min) * value);
if (param->hints & NATIVE_PARAMETER_IS_INTEGER)
realValue = std::round(realValue);
}
fDescriptor->set_parameter_value(fHandle, uindex, realValue);
}
void vst_processReplacing(const float** const inputs, float** const outputs, const int32_t sampleFrames)
{
if (sampleFrames <= 0)
return;
if (fHostType == kHostTypeBitwig && static_cast<int32_t>(fBufferSize) != sampleFrames)
{
// deactivate first if needed
if (fIsActive && fDescriptor->deactivate != nullptr)
fDescriptor->deactivate(fHandle);
fBufferSize = static_cast<uint32_t>(sampleFrames);
if (fDescriptor->dispatcher != nullptr)
fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, sampleFrames, nullptr, 0.0f);
// activate again
if (fDescriptor->activate != nullptr)
fDescriptor->activate(fHandle);
fIsActive = true;
}
if (! fIsActive)
{
// host has not activated the plugin yet, nasty!
vst_dispatcher(effMainsChanged, 0, 1, nullptr, 0.0f);
}
static const int kWantVstTimeFlags = kVstTransportPlaying|kVstPpqPosValid|kVstTempoValid|kVstTimeSigValid;
if (const VstTimeInfo* const vstTimeInfo = (const VstTimeInfo*)hostCallback(audioMasterGetTime, 0, kWantVstTimeFlags))
{
fTimeInfo.frame = static_cast<uint64_t>(vstTimeInfo->samplePos);
fTimeInfo.playing = (vstTimeInfo->flags & kVstTransportPlaying);
fTimeInfo.bbt.valid = ((vstTimeInfo->flags & kVstTempoValid) != 0 || (vstTimeInfo->flags & kVstTimeSigValid) != 0);
// ticksPerBeat is not possible with VST
fTimeInfo.bbt.ticksPerBeat = 960.0;
if (vstTimeInfo->flags & kVstTempoValid)
fTimeInfo.bbt.beatsPerMinute = vstTimeInfo->tempo;
else
fTimeInfo.bbt.beatsPerMinute = 120.0;
if (vstTimeInfo->flags & (kVstPpqPosValid|kVstTimeSigValid))
{
const double ppqPos = std::abs(vstTimeInfo->ppqPos);
const int ppqPerBar = vstTimeInfo->timeSigNumerator * 4 / vstTimeInfo->timeSigDenominator;
const double barBeats = (std::fmod(ppqPos, ppqPerBar) / ppqPerBar) * vstTimeInfo->timeSigNumerator;
const double rest = std::fmod(barBeats, 1.0);
fTimeInfo.bbt.bar = static_cast<int32_t>(ppqPos) / ppqPerBar + 1;
fTimeInfo.bbt.beat = static_cast<int32_t>(barBeats - rest + 0.5) + 1;
fTimeInfo.bbt.tick = static_cast<int32_t>(rest * fTimeInfo.bbt.ticksPerBeat + 0.5);
fTimeInfo.bbt.beatsPerBar = static_cast<float>(vstTimeInfo->timeSigNumerator);
fTimeInfo.bbt.beatType = static_cast<float>(vstTimeInfo->timeSigDenominator);
if (vstTimeInfo->ppqPos < 0.0)
{
--fTimeInfo.bbt.bar;
fTimeInfo.bbt.beat = vstTimeInfo->timeSigNumerator - fTimeInfo.bbt.beat + 1;
fTimeInfo.bbt.tick = fTimeInfo.bbt.ticksPerBeat - fTimeInfo.bbt.tick - 1;
}
}
else
{
fTimeInfo.bbt.bar = 1;
fTimeInfo.bbt.beat = 1;
fTimeInfo.bbt.tick = 0;
fTimeInfo.bbt.beatsPerBar = 4.0f;
fTimeInfo.bbt.beatType = 4.0f;
}
fTimeInfo.bbt.barStartTick = fTimeInfo.bbt.ticksPerBeat *
static_cast<double>(fTimeInfo.bbt.beatsPerBar) *
(fTimeInfo.bbt.bar - 1);
}
fMidiOutEvents.numEvents = 0;
if (fHandle != nullptr)
// FIXME
fDescriptor->process(fHandle,
inputs, outputs, static_cast<uint32_t>(sampleFrames),
fMidiEvents, fMidiEventCount);
fMidiEventCount = 0;
if (fMidiOutEvents.numEvents > 0)
hostCallback(audioMasterProcessEvents, 0, 0, &fMidiOutEvents, 0.0f);
}
protected:
// -------------------------------------------------------------------
bool handleWriteMidiEvent(const NativeMidiEvent* const event)
{
CARLA_SAFE_ASSERT_RETURN(fDescriptor->midiOuts > 0, false);
CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
if (fMidiOutEvents.numEvents >= static_cast<int32_t>(kMaxMidiEvents))
{
// send current events
hostCallback(audioMasterProcessEvents, 0, 0, &fMidiOutEvents, 0.0f);
// clear
fMidiOutEvents.numEvents = 0;
}
VstMidiEvent& vstMidiEvent(fMidiOutEvents.mdata[fMidiOutEvents.numEvents++]);
vstMidiEvent.type = kVstMidiType;
vstMidiEvent.byteSize = kVstMidiEventSize;
uint8_t i=0;
for (; i<event->size; ++i)
vstMidiEvent.midiData[i] = static_cast<char>(event->data[i]);
for (; i<4; ++i)
vstMidiEvent.midiData[i] = 0;
return true;
}
void handleUiParameterChanged(const uint32_t index, const float value) const
{
const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, index);
CARLA_SAFE_ASSERT_RETURN(param != nullptr,);
const float normalizedValue = (value - param->ranges.min) / (param->ranges.max - param->ranges.min);
hostCallback(audioMasterAutomate, static_cast<int32_t>(index), 0, nullptr, normalizedValue);
}
void handleUiParameterTouch(const uint32_t index, const bool touch) const
{
hostCallback(touch ? audioMasterBeginEdit : audioMasterEndEdit, static_cast<int32_t>(index));
}
void handleUiCustomDataChanged(const char* const /*key*/, const char* const /*value*/) const
{
}
void handleUiClosed()
{
}
const char* handleUiOpenFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
{
// TODO
return nullptr;
}
const char* handleUiSaveFile(const bool /*isDir*/, const char* const /*title*/, const char* const /*filter*/) const
{
// TODO
return nullptr;
}
intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
{
carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)",
opcode, index, value, ptr, static_cast<double>(opt));
switch (opcode)
{
case NATIVE_HOST_OPCODE_NULL:
case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
case NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY:
case NATIVE_HOST_OPCODE_REQUEST_IDLE:
case NATIVE_HOST_OPCODE_GET_FILE_PATH:
// nothing
break;
case NATIVE_HOST_OPCODE_RELOAD_ALL:
hostCallback(audioMasterUpdateDisplay);
break;
case NATIVE_HOST_OPCODE_HOST_IDLE:
hostCallback(audioMasterIdle);
break;
case NATIVE_HOST_OPCODE_UI_TOUCH_PARAMETER:
CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
handleUiParameterTouch(static_cast<uint32_t>(index), value != 0);
break;
}
// unused for now
return 0;
(void)ptr; (void)opt;
}
private:
// VST stuff
AEffect* const fEffect;
// Native data
NativePluginHandle fHandle;
NativeHostDescriptor fHost;
const NativePluginDescriptor* const fDescriptor;
// VST host data
uint32_t fBufferSize;
double fSampleRate;
// Temporary data
bool fIsActive;
uint32_t fMidiEventCount;
NativeMidiEvent fMidiEvents[kMaxMidiEvents];
char fProgramName[32+1];
NativeTimeInfo fTimeInfo;
ERect fVstRect;
// UI button
CarlaUILauncher* fUiLauncher;
// Host data
enum HostType {
kHostTypeNull = 0,
kHostTypeArdour,
kHostTypeBitwig
};
HostType fHostType;
// host callback
intptr_t hostCallback(const int32_t opcode,
const int32_t index = 0,
const intptr_t value = 0,
void* const ptr = nullptr,
const float opt = 0.0f) const
{
return VSTAudioMaster(fEffect, opcode, index, value, ptr, opt);
}
struct FixedVstEvents {
int32_t numEvents;
intptr_t reserved;
VstEvent* data[kMaxMidiEvents];
VstMidiEvent mdata[kMaxMidiEvents];
FixedVstEvents()
: numEvents(0),
reserved(0)
{
for (uint32_t i=0; i<kMaxMidiEvents; ++i)
data[i] = (VstEvent*)&mdata[i];
carla_zeroStructs(mdata, kMaxMidiEvents);
}
CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents);
} fMidiOutEvents;
#ifdef USING_JUCE
juce::SharedResourcePointer<juce::ScopedJuceInitialiser_GUI> fJuceInitialiser;
#endif
char* fStateChunk;
// -------------------------------------------------------------------
#define handlePtr ((NativePlugin*)handle)
static uint32_t host_get_buffer_size(NativeHostHandle handle)
{
return handlePtr->fBufferSize;
}
static double host_get_sample_rate(NativeHostHandle handle)
{
return handlePtr->fSampleRate;
}
static bool host_is_offline(NativeHostHandle /*handle*/)
{
// TODO
return false;
}
static const NativeTimeInfo* host_get_time_info(NativeHostHandle handle)
{
return &(handlePtr->fTimeInfo);
}
static bool host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
{
return handlePtr->handleWriteMidiEvent(event);
}
static void host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
{
handlePtr->handleUiParameterChanged(index, value);
}
static void host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
{
handlePtr->handleUiCustomDataChanged(key, value);
}
static void host_ui_closed(NativeHostHandle handle)
{
handlePtr->handleUiClosed();
}
static const char* host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
{
return handlePtr->handleUiOpenFile(isDir, title, filter);
}
static const char* host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
{
return handlePtr->handleUiSaveFile(isDir, title, filter);
}
static intptr_t host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
{
return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
}
#undef handlePtr
CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
};
// -----------------------------------------------------------------------
#define validObject effect != nullptr && effect->object != nullptr
#define validPlugin effect != nullptr && effect->object != nullptr && ((VstObject*)effect->object)->plugin != nullptr
#define vstObjectPtr (VstObject*)effect->object
#define pluginPtr (vstObjectPtr)->plugin
intptr_t vst_dispatcherCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
{
// handle base opcodes
switch (opcode)
{
case effOpen:
if (VstObject* const obj = vstObjectPtr)
{
// this must always be valid
CARLA_SAFE_ASSERT_RETURN(obj->audioMaster != nullptr, 0);
// some hosts call effOpen twice
CARLA_SAFE_ASSERT_RETURN(obj->plugin == nullptr, 1);
d_lastBufferSize = static_cast<uint32_t>(VSTAudioMaster(effect, audioMasterGetBlockSize, 0, 0, nullptr, 0.0f));
d_lastSampleRate = static_cast<double>(VSTAudioMaster(effect, audioMasterGetSampleRate, 0, 0, nullptr, 0.0f));
// some hosts are not ready at this point or return 0 buffersize/samplerate
if (d_lastBufferSize == 0)
d_lastBufferSize = 2048;
if (d_lastSampleRate <= 0.0)
d_lastSampleRate = 44100.0;
const NativePluginDescriptor* pluginDesc = nullptr;
PluginListManager& plm(PluginListManager::getInstance());
#ifdef CARLA_VST_SHELL
if (effect->uniqueID == 0)
effect->uniqueID = kShellUniqueID;
if (effect->uniqueID == kShellUniqueID)
{
// first open for discovery, nothing to do
effect->numParams = 0;
effect->numPrograms = 0;
effect->numInputs = 0;
effect->numOutputs = 0;
return 1;
}
const int32_t plugIndex = effect->uniqueID - kShellUniqueID - 1;
CARLA_SAFE_ASSERT_RETURN(plugIndex >= 0, 0);
pluginDesc = plm.descs.getAt(static_cast<size_t>(plugIndex), nullptr);
#else // CARLA_VST_SHELL
# if defined(CARLA_PLUGIN_64CH)
const char* const pluginLabel = "carlapatchbay64";
# elif defined(CARLA_PLUGIN_32CH)
const char* const pluginLabel = "carlapatchbay32";
# elif defined(CARLA_PLUGIN_16CH)
const char* const pluginLabel = "carlapatchbay16";
# elif CARLA_PLUGIN_PATCHBAY
const char* const pluginLabel = "carlapatchbay";
# else
const char* const pluginLabel = "carlarack";
# endif
for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
{