forked from juce-framework/JUCE
/
juce_VSTPluginFormat.cpp
3777 lines (3043 loc) · 129 KB
/
juce_VSTPluginFormat.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
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
End User License Agreement: www.juce.com/juce-6-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
#if JUCE_PLUGINHOST_VST
//==============================================================================
#undef PRAGMA_ALIGN_SUPPORTED
#if ! JUCE_MINGW && ! JUCE_MSVC
#define __cdecl
#endif
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wzero-as-null-pointer-constant")
JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4996)
#define VST_FORCE_DEPRECATED 0
#define JUCE_VSTINTERFACE_H_INCLUDED 1
namespace Vst2
{
#include <vestige/vestige.h>
}
#include "juce_VSTCommon.h"
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
JUCE_END_IGNORE_WARNINGS_MSVC
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated-declarations")
JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4355)
#include "juce_VSTMidiEventList.h"
#if JUCE_MINGW
#ifndef WM_APPCOMMAND
#define WM_APPCOMMAND 0x0319
#endif
extern "C" void _fpreset();
extern "C" void _clearfp();
#elif ! JUCE_WINDOWS
static void _fpreset() {}
static void _clearfp() {}
#endif
#ifndef JUCE_VST_WRAPPER_LOAD_CUSTOM_MAIN
#define JUCE_VST_WRAPPER_LOAD_CUSTOM_MAIN
#endif
#ifndef JUCE_VST_WRAPPER_INVOKE_MAIN
#define JUCE_VST_WRAPPER_INVOKE_MAIN effect = module->moduleMain ((Vst2::audioMasterCallback) &audioMaster);
#endif
//==============================================================================
namespace juce
{
//==============================================================================
namespace
{
const int fxbVersionNum = 1;
struct fxProgram
{
int32 chunkMagic; // 'CcnK'
int32 byteSize; // of this chunk, excl. magic + byteSize
int32 fxMagic; // 'FxCk'
int32 version;
int32 fxID; // fx unique id
int32 fxVersion;
int32 numParams;
char prgName[28];
float params[1]; // variable no. of parameters
};
struct fxSet
{
int32 chunkMagic; // 'CcnK'
int32 byteSize; // of this chunk, excl. magic + byteSize
int32 fxMagic; // 'FxBk'
int32 version;
int32 fxID; // fx unique id
int32 fxVersion;
int32 numPrograms;
char future[128];
fxProgram programs[1]; // variable no. of programs
};
struct fxChunkSet
{
int32 chunkMagic; // 'CcnK'
int32 byteSize; // of this chunk, excl. magic + byteSize
int32 fxMagic; // 'FxCh', 'FPCh', or 'FBCh'
int32 version;
int32 fxID; // fx unique id
int32 fxVersion;
int32 numPrograms;
char future[128];
int32 chunkSize;
char chunk[8]; // variable
};
struct fxProgramSet
{
int32 chunkMagic; // 'CcnK'
int32 byteSize; // of this chunk, excl. magic + byteSize
int32 fxMagic; // 'FxCh', 'FPCh', or 'FBCh'
int32 version;
int32 fxID; // fx unique id
int32 fxVersion;
int32 numPrograms;
char name[28];
int32 chunkSize;
char chunk[8]; // variable
};
// Compares a magic value in either endianness.
static bool compareMagic (int32 magic, const char* name) noexcept
{
return magic == (int32) ByteOrder::littleEndianInt (name)
|| magic == (int32) ByteOrder::bigEndianInt (name);
}
static int32 fxbName (const char* name) noexcept { return (int32) ByteOrder::littleEndianInt (name); }
static int32 fxbSwap (int32 x) noexcept { return (int32) ByteOrder::swapIfLittleEndian ((uint32) x); }
static float fxbSwapFloat (const float x) noexcept
{
#ifdef JUCE_LITTLE_ENDIAN
union { uint32 asInt; float asFloat; } n;
n.asFloat = x;
n.asInt = ByteOrder::swap (n.asInt);
return n.asFloat;
#else
return x;
#endif
}
}
//==============================================================================
namespace
{
static double getVSTHostTimeNanoseconds() noexcept
{
#if JUCE_WINDOWS
return timeGetTime() * 1000000.0;
#elif JUCE_LINUX || JUCE_BSD || JUCE_IOS || JUCE_ANDROID
timeval micro;
gettimeofday (µ, nullptr);
return (double) micro.tv_usec * 1000.0;
#elif JUCE_MAC
UnsignedWide micro;
Microseconds (µ);
return micro.lo * 1000.0;
#endif
}
static int shellUIDToCreate = 0;
static int insideVSTCallback = 0;
struct IdleCallRecursionPreventer
{
IdleCallRecursionPreventer() : isMessageThread (MessageManager::getInstance()->isThisTheMessageThread())
{
if (isMessageThread)
++insideVSTCallback;
}
~IdleCallRecursionPreventer()
{
if (isMessageThread)
--insideVSTCallback;
}
const bool isMessageThread;
JUCE_DECLARE_NON_COPYABLE (IdleCallRecursionPreventer)
};
#if JUCE_MAC
static bool makeFSRefFromPath (FSRef* destFSRef, const String& path)
{
return FSPathMakeRef (reinterpret_cast<const UInt8*> (path.toRawUTF8()), destFSRef, nullptr) == noErr;
}
#endif
}
//==============================================================================
typedef Vst2::AEffect* (VSTCALLBACK *MainCall) (Vst2::audioMasterCallback);
static pointer_sized_int VSTCALLBACK audioMaster (Vst2::AEffect*, int32, int32, pointer_sized_int, void*, float);
//==============================================================================
// Change this to disable logging of various VST activities
#ifndef VST_LOGGING
#define VST_LOGGING 1
#endif
#if VST_LOGGING
#define JUCE_VST_LOG(a) Logger::writeToLog(a);
#else
#define JUCE_VST_LOG(a)
#endif
//==============================================================================
#if JUCE_LINUX || JUCE_BSD
namespace
{
using EventProcPtr = void (*)(XEvent*);
Window getChildWindow (Window windowToCheck)
{
Window rootWindow, parentWindow;
Window* childWindows;
unsigned int numChildren = 0;
X11Symbols::getInstance()->xQueryTree (XWindowSystem::getInstance()->getDisplay(),
windowToCheck, &rootWindow, &parentWindow, &childWindows, &numChildren);
if (numChildren > 0)
return childWindows [0];
return 0;
}
}
#endif
//==============================================================================
class VSTXMLInfo
{
public:
static VSTXMLInfo* createFor (const juce::XmlElement& xml)
{
if (xml.hasTagName ("VSTParametersStructure"))
return new VSTXMLInfo (xml);
if (const auto* x = xml.getChildByName ("VSTParametersStructure"))
return new VSTXMLInfo (*x);
return nullptr;
}
struct Group;
struct Base
{
Base() noexcept {}
virtual ~Base() {}
Group* parent = nullptr;
};
struct Param : public Base
{
int paramID;
juce::String expr, name, label;
juce::StringArray shortNames;
juce::String type;
int numberOfStates;
float defaultValue;
};
struct Group : public Base
{
juce::String name;
juce::OwnedArray<Base> paramTree;
};
struct Range
{
Range() noexcept {}
Range (const juce::String& s) { set (s); }
void set (const juce::String& s)
{
inclusiveLow = s.startsWithChar ('[');
inclusiveHigh = s.endsWithChar (']');
auto str = s.removeCharacters ("[]");
low = str.upToFirstOccurrenceOf (",", false, false).getFloatValue();
high = str.fromLastOccurrenceOf (",", false, false).getFloatValue();
}
bool contains (float f) const noexcept
{
return (inclusiveLow ? (f >= low) : (f > low))
&& (inclusiveHigh ? (f <= high) : (f < high));
}
float low = 0;
float high = 0;
bool inclusiveLow = false;
bool inclusiveHigh = false;
};
struct Entry
{
juce::String name;
Range range;
};
struct ValueType
{
juce::String name, label;
juce::OwnedArray<Entry> entries;
};
struct Template
{
juce::String name;
juce::OwnedArray<Param> params;
};
const Param* getParamForID (const int paramID, const Group* const grp) const
{
for (auto item : (grp != nullptr ? grp->paramTree : paramTree))
{
if (auto param = dynamic_cast<const Param*> (item))
if (param->paramID == paramID)
return param;
if (auto group = dynamic_cast<const Group*> (item))
if (auto res = getParamForID (paramID, group))
return res;
}
return nullptr;
}
const ValueType* getValueType (const juce::String& name) const
{
for (auto v : valueTypes)
if (v->name == name)
return v;
return nullptr;
}
juce::OwnedArray<Base> paramTree;
juce::OwnedArray<ValueType> valueTypes;
juce::OwnedArray<Template> templates;
ValueType switchValueType;
private:
VSTXMLInfo (const juce::XmlElement& xml)
{
switchValueType.entries.add (new Entry({ TRANS("Off"), Range ("[0, 0.5[") }));
switchValueType.entries.add (new Entry({ TRANS("On"), Range ("[0.5, 1]") }));
for (auto* item : xml.getChildIterator())
{
if (item->hasTagName ("Param")) parseParam (*item, nullptr, nullptr);
else if (item->hasTagName ("ValueType")) parseValueType (*item);
else if (item->hasTagName ("Template")) parseTemplate (*item);
else if (item->hasTagName ("Group")) parseGroup (*item, nullptr);
}
}
void parseParam (const juce::XmlElement& item, Group* group, Template* temp)
{
auto param = new Param();
if (temp != nullptr)
param->expr = item.getStringAttribute ("id");
else
param->paramID = item.getIntAttribute ("id");
param->name = item.getStringAttribute ("name");
param->label = item.getStringAttribute ("label");
param->type = item.getStringAttribute ("type");
param->numberOfStates = item.getIntAttribute ("numberOfStates");
param->defaultValue = (float) item.getDoubleAttribute ("defaultValue");
param->shortNames.addTokens (item.getStringAttribute ("shortName"), ",", juce::StringRef());
param->shortNames.trim();
param->shortNames.removeEmptyStrings();
if (group != nullptr)
{
group->paramTree.add (param);
param->parent = group;
}
else if (temp != nullptr)
{
temp->params.add (param);
}
else
{
paramTree.add (param);
}
}
void parseValueType (const juce::XmlElement& item)
{
auto vt = new ValueType();
valueTypes.add (vt);
vt->name = item.getStringAttribute ("name");
vt->label = item.getStringAttribute ("label");
int curEntry = 0;
const int numEntries = item.getNumChildElements();
for (auto* entryXml : item.getChildWithTagNameIterator ("Entry"))
{
auto entry = new Entry();
entry->name = entryXml->getStringAttribute ("name");
if (entryXml->hasAttribute ("value"))
{
entry->range.set(entryXml->getStringAttribute ("value"));
}
else
{
entry->range.low = (float) curEntry / (float) numEntries;
entry->range.high = (float) (curEntry + 1) / (float) numEntries;
entry->range.inclusiveLow = true;
entry->range.inclusiveHigh = (curEntry == numEntries - 1);
}
vt->entries.add (entry);
++curEntry;
}
}
void parseTemplate (const juce::XmlElement& item)
{
auto temp = new Template();
templates.add (temp);
temp->name = item.getStringAttribute ("name");
for (auto* param : item.getChildIterator())
parseParam (*param, nullptr, temp);
}
void parseGroup (const juce::XmlElement& item, Group* parentGroup)
{
auto group = new Group();
if (parentGroup)
{
parentGroup->paramTree.add (group);
group->parent = parentGroup;
}
else
{
paramTree.add (group);
}
group->name = item.getStringAttribute ("name");
if (item.hasAttribute ("template"))
{
juce::StringArray variables;
variables.addTokens (item.getStringAttribute ("values"), ";", juce::StringRef());
variables.trim();
for (auto temp : templates)
{
if (temp->name == item.getStringAttribute ("template"))
{
for (int i = 0; i < temp->params.size(); ++i)
{
auto param = new Param();
group->paramTree.add (param);
param->parent = group;
param->paramID = evaluate (temp->params[i]->expr, variables);
param->defaultValue = temp->params[i]->defaultValue;
param->label = temp->params[i]->label;
param->name = temp->params[i]->name;
param->numberOfStates = temp->params[i]->numberOfStates;
param->shortNames = temp->params[i]->shortNames;
param->type = temp->params[i]->type;
}
}
}
}
else
{
for (auto* subItem : item.getChildIterator())
{
if (subItem->hasTagName ("Param")) parseParam (*subItem, group, nullptr);
else if (subItem->hasTagName ("Group")) parseGroup (*subItem, group);
}
}
}
int evaluate (juce::String expr, const juce::StringArray& variables) const
{
juce::StringArray names;
juce::Array<int> vals;
for (auto& v : variables)
{
if (v.contains ("="))
{
names.add (v.upToFirstOccurrenceOf ("=", false, false));
vals.add (v.fromFirstOccurrenceOf ("=", false, false).getIntValue());
}
}
for (int i = 0; i < names.size(); ++i)
{
for (;;)
{
const int idx = expr.indexOfWholeWord (names[i]);
if (idx < 0)
break;
expr = expr.replaceSection (idx, names[i].length(), juce::String (vals[i]));
}
}
expr = expr.retainCharacters ("01234567890-+")
.replace ("+", " + ")
.replace ("-", " - ");
juce::StringArray tokens;
tokens.addTokens (expr, " ", juce::StringRef());
bool add = true;
int val = 0;
for (const auto& s : tokens)
{
if (s == "+")
{
add = true;
}
else if (s == "-")
{
add = false;
}
else
{
if (add)
val += s.getIntValue();
else
val -= s.getIntValue();
}
}
return val;
}
};
//==============================================================================
struct ModuleHandle : public ReferenceCountedObject
{
File file;
MainCall moduleMain, customMain = {};
String pluginName;
std::unique_ptr<XmlElement> vstXml;
using Ptr = ReferenceCountedObjectPtr<ModuleHandle>;
static Array<ModuleHandle*>& getActiveModules()
{
static Array<ModuleHandle*> activeModules;
return activeModules;
}
//==============================================================================
static Ptr findOrCreateModule (const File& file)
{
for (auto* module : getActiveModules())
if (module->file == file)
return module;
const IdleCallRecursionPreventer icrp;
shellUIDToCreate = 0;
_fpreset();
JUCE_VST_LOG ("Attempting to load VST: " + file.getFullPathName());
Ptr m = new ModuleHandle (file, nullptr);
if (m->open())
{
_fpreset();
return m;
}
return {};
}
//==============================================================================
ModuleHandle (const File& f, MainCall customMainCall)
: file (f), moduleMain (customMainCall)
{
getActiveModules().add (this);
#if JUCE_WINDOWS || JUCE_LINUX || JUCE_BSD || JUCE_IOS || JUCE_ANDROID
fullParentDirectoryPathName = f.getParentDirectory().getFullPathName();
#elif JUCE_MAC
FSRef ref;
makeFSRefFromPath (&ref, f.getParentDirectory().getFullPathName());
FSGetCatalogInfo (&ref, kFSCatInfoNone, nullptr, nullptr, &parentDirFSSpec, nullptr);
#endif
}
~ModuleHandle()
{
getActiveModules().removeFirstMatchingValue (this);
close();
}
//==============================================================================
#if ! JUCE_MAC
String fullParentDirectoryPathName;
#endif
#if JUCE_WINDOWS || JUCE_LINUX || JUCE_BSD || JUCE_ANDROID
DynamicLibrary module;
bool open()
{
if (moduleMain != nullptr)
return true;
pluginName = file.getFileNameWithoutExtension();
module.open (file.getFullPathName());
moduleMain = (MainCall) module.getFunction ("VSTPluginMain");
if (moduleMain == nullptr)
moduleMain = (MainCall) module.getFunction ("main");
JUCE_VST_WRAPPER_LOAD_CUSTOM_MAIN
if (moduleMain != nullptr)
{
vstXml = parseXML (file.withFileExtension ("vstxml"));
#if JUCE_WINDOWS
if (vstXml == nullptr)
vstXml = parseXML (getDLLResource (file, "VSTXML", 1));
#endif
}
return moduleMain != nullptr;
}
void close()
{
_fpreset(); // (doesn't do any harm)
module.close();
}
void closeEffect (Vst2::AEffect* eff)
{
eff->dispatcher (eff, Vst2::effClose, 0, 0, nullptr, 0);
}
#if JUCE_WINDOWS
static String getDLLResource (const File& dllFile, const String& type, int resID)
{
DynamicLibrary dll (dllFile.getFullPathName());
auto dllModule = (HMODULE) dll.getNativeHandle();
if (dllModule != INVALID_HANDLE_VALUE)
{
if (auto res = FindResource (dllModule, MAKEINTRESOURCE (resID), type.toWideCharPointer()))
{
if (auto hGlob = LoadResource (dllModule, res))
{
auto* data = static_cast<const char*> (LockResource (hGlob));
return String::fromUTF8 (data, SizeofResource (dllModule, res));
}
}
}
return {};
}
#endif
#else
Handle resHandle = {};
CFUniquePtr<CFBundleRef> bundleRef;
#if JUCE_MAC
CFBundleRefNum resFileId = {};
FSSpec parentDirFSSpec;
#endif
bool open()
{
if (moduleMain != nullptr)
return true;
bool ok = false;
if (file.hasFileExtension (".vst"))
{
auto* utf8 = file.getFullPathName().toRawUTF8();
if (auto url = CFUniquePtr<CFURLRef> (CFURLCreateFromFileSystemRepresentation (nullptr, (const UInt8*) utf8,
(CFIndex) strlen (utf8), file.isDirectory())))
{
bundleRef.reset (CFBundleCreate (kCFAllocatorDefault, url.get()));
if (bundleRef != nullptr)
{
if (CFBundleLoadExecutable (bundleRef.get()))
{
moduleMain = (MainCall) CFBundleGetFunctionPointerForName (bundleRef.get(), CFSTR("main_macho"));
if (moduleMain == nullptr)
moduleMain = (MainCall) CFBundleGetFunctionPointerForName (bundleRef.get(), CFSTR("VSTPluginMain"));
JUCE_VST_WRAPPER_LOAD_CUSTOM_MAIN
if (moduleMain != nullptr)
{
if (CFTypeRef name = CFBundleGetValueForInfoDictionaryKey (bundleRef.get(), CFSTR("CFBundleName")))
{
if (CFGetTypeID (name) == CFStringGetTypeID())
{
char buffer[1024];
if (CFStringGetCString ((CFStringRef) name, buffer, sizeof (buffer), CFStringGetSystemEncoding()))
pluginName = buffer;
}
}
if (pluginName.isEmpty())
pluginName = file.getFileNameWithoutExtension();
#if JUCE_MAC
resFileId = CFBundleOpenBundleResourceMap (bundleRef.get());
#endif
ok = true;
auto vstXmlFiles = file
#if JUCE_MAC
.getChildFile ("Contents")
.getChildFile ("Resources")
#endif
.findChildFiles (File::findFiles, false, "*.vstxml");
if (! vstXmlFiles.isEmpty())
vstXml = parseXML (vstXmlFiles.getReference(0));
}
}
if (! ok)
{
CFBundleUnloadExecutable (bundleRef.get());
bundleRef = nullptr;
}
}
}
}
return ok;
}
void close()
{
if (bundleRef != nullptr)
{
#if JUCE_MAC
CFBundleCloseBundleResourceMap (bundleRef.get(), resFileId);
#endif
if (CFGetRetainCount (bundleRef.get()) == 1)
CFBundleUnloadExecutable (bundleRef.get());
if (CFGetRetainCount (bundleRef.get()) > 0)
bundleRef = nullptr;
}
}
void closeEffect (Vst2::AEffect* eff)
{
eff->dispatcher (eff, Vst2::effClose, 0, 0, nullptr, 0);
}
#endif
private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ModuleHandle)
};
static const int defaultVSTSampleRateValue = 44100;
static const int defaultVSTBlockSizeValue = 512;
JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4996)
//==============================================================================
struct VSTPluginInstance : public AudioPluginInstance,
private Timer,
private AsyncUpdater
{
struct VSTParameter final : public Parameter
{
VSTParameter (VSTPluginInstance& parent,
const String& paramName,
const Array<String>& shortParamNames,
float paramDefaultValue,
const String& paramLabel,
bool paramIsAutomatable,
bool paramIsDiscrete,
int numParamSteps,
bool isBoolSwitch,
const StringArray& paramValueStrings,
const VSTXMLInfo::ValueType* paramValueType)
: pluginInstance (parent),
name (paramName),
shortNames (shortParamNames),
defaultValue (paramDefaultValue),
label (paramLabel),
automatable (paramIsAutomatable),
discrete (paramIsDiscrete),
numSteps (numParamSteps),
isSwitch (isBoolSwitch),
vstValueStrings (paramValueStrings),
valueType (paramValueType)
{
}
float getValue() const override
{
if (auto* effect = pluginInstance.vstEffect)
{
const ScopedLock sl (pluginInstance.lock);
return effect->getParameter (effect, getParameterIndex());
}
return 0.0f;
}
void setValue (float newValue) override
{
if (auto* effect = pluginInstance.vstEffect)
{
const ScopedLock sl (pluginInstance.lock);
if (effect->getParameter (effect, getParameterIndex()) != newValue)
effect->setParameter (effect, getParameterIndex(), newValue);
}
}
String getText (float value, int maximumStringLength) const override
{
if (valueType != nullptr)
{
for (auto& v : valueType->entries)
if (v->range.contains (value))
return v->name;
}
return Parameter::getText (value, maximumStringLength);
}
float getValueForText (const String& text) const override
{
if (valueType != nullptr)
{
for (auto& v : valueType->entries)
if (v->name == text)
return (v->range.high + v->range.low) / 2.0f;
}
return Parameter::getValueForText (text);
}
String getCurrentValueAsText() const override
{
if (valueType != nullptr || ! vstValueStrings.isEmpty())
return getText (getValue(), 1024);
return pluginInstance.getTextForOpcode (getParameterIndex(), Vst2::effGetParamDisplay);
}
float getDefaultValue() const override
{
return defaultValue;
}
String getName (int maximumStringLength) const override
{
if (name.isEmpty())
return pluginInstance.getTextForOpcode (getParameterIndex(),
Vst2::effGetParamName);
if (name.length() <= maximumStringLength)
return name;
if (! shortNames.isEmpty())
{
for (auto& n : shortNames)
if (n.length() <= maximumStringLength)
return n;
return shortNames.getLast();
}
return name;
}
String getLabel() const override
{
return label.isEmpty() ? pluginInstance.getTextForOpcode (getParameterIndex(),
Vst2::effGetParamLabel)
: label;
}
bool isAutomatable() const override
{
return automatable;
}
bool isDiscrete() const override
{
return discrete;
}
bool isBoolean() const override
{
return isSwitch;
}
int getNumSteps() const override
{
return numSteps;
}
StringArray getAllValueStrings() const override
{
return vstValueStrings;
}
VSTPluginInstance& pluginInstance;
const String name;
const Array<String> shortNames;
const float defaultValue;
const String label;
const bool automatable, discrete;
const int numSteps;
const bool isSwitch;
const StringArray vstValueStrings;
const VSTXMLInfo::ValueType* const valueType;
};
VSTPluginInstance (const ModuleHandle::Ptr& mh, const BusesProperties& ioConfig, Vst2::AEffect* effect,
double sampleRateToUse, int blockSizeToUse)
: AudioPluginInstance (ioConfig),
vstEffect (effect),
vstModule (mh),
name (mh->pluginName),
bypassParam (new VST2BypassParameter (*this))
{
jassert (vstEffect != nullptr);
if (auto* xml = vstModule->vstXml.get())
xmlInfo.reset (VSTXMLInfo::createFor (*xml));
refreshParameterList();
vstSupportsBypass = (pluginCanDo ("bypass") > 0);
setRateAndBufferSizeDetails (sampleRateToUse, blockSizeToUse);
}
void refreshParameterList() override
{
AudioProcessorParameterGroup newParameterTree;