forked from dliganov/Chaotic-DAW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CVSTHost.cpp
1610 lines (1362 loc) · 52 KB
/
CVSTHost.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
/*****************************************************************************/
/* CVSTHost.cpp: implementation of the CVSTHost class. */
/*****************************************************************************/
/******************************************************************************
Copyright (C) 2006 Hermann Seib
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#ifdef WIN32
#include <windows.h> /* Windows header files */
#elif MAC
#endif
#include <stdio.h> /* file I/O prototypes */
#include <math.h> /* math prototypes */
#include "CVSTHost.h" /* private prototypes */
/*****************************************************************************/
/* Global Data */
/*****************************************************************************/
const int MyVersion = 1; /* highest known VST FX version */
/*===========================================================================*/
/* CFxBase class members */
/*===========================================================================*/
static char szChnk[] = "CcnK"; /* set up swapping flag */
static long lChnk = 'CcnK';
bool CFxBase::NeedsBSwap = !!memcmp(szChnk, &lChnk, 4);
/*****************************************************************************/
/* SwapBytes : swaps bytes for big/little-endian difference */
/*****************************************************************************/
void CFxBase::SwapBytes(long &l)
{
unsigned char *b = (unsigned char *)&l;
long intermediate = ((long)b[0] << 24) |
((long)b[1] << 16) |
((long)b[2] << 8) |
(long)b[3];
l = intermediate;
}
void CFxBase::SwapBytes(float &f)
{
long *pl = (long *)&f;
SwapBytes(*pl);
}
#if defined(VST_2_4_EXTENSIONS)
void CFxBase::SwapBytes(VstInt32 &vi)
{
unsigned char *b = (unsigned char *)&vi;
VstInt32 intermediate = ((VstInt32)b[0] << 24) |
((VstInt32)b[1] << 16) |
((VstInt32)b[2] << 8) |
(VstInt32)b[3];
vi = intermediate;
}
#endif
/*===========================================================================*/
/* CFxBank class members */
/*===========================================================================*/
/*****************************************************************************/
/* CFxBank : constructor */
/*****************************************************************************/
CFxBank::CFxBank(char *pszFile)
{
Init(); /* initialize data areas */
if (pszFile) /* if a file name has been passed */
LoadBank(pszFile); /* load the corresponding bank */
}
CFxBank::CFxBank(int nPrograms, int nParams)
{
Init(); /* initialize data areas */
SetSize(nPrograms, nParams); /* set new size */
}
CFxBank::CFxBank(int nChunkSize)
{
Init(); /* initialize data areas */
SetSize(nChunkSize); /* set new size */
}
/*****************************************************************************/
/* Init : initializes all data areas */
/*****************************************************************************/
void CFxBank::Init()
{
bBank = NULL; /* no bank data loaded */
Unload(); /* reset all parameters */
}
/*****************************************************************************/
/* ~CFxBank : destructor */
/*****************************************************************************/
CFxBank::~CFxBank()
{
Unload(); /* unload all data */
}
/*****************************************************************************/
/* DoCopy : combined for copy constructor and assignment operator */
/*****************************************************************************/
CFxBank & CFxBank::DoCopy(const CFxBank &org)
{
unsigned char *nBank = NULL;
if (org.nBankLen)
{
unsigned char *nBank = new unsigned char[org.nBankLen];
if (!nBank)
throw(int)1;
memcpy(nBank, org.bBank, org.nBankLen);
}
Unload(); /* remove previous data */
bBank = nBank; /* and copy in the other one's */
bChunk = org.bChunk;
nBankLen = org.nBankLen;
strcpy(szFileName, org.szFileName);
return *this;
}
/*****************************************************************************/
/* SetSize : sets new size */
/*****************************************************************************/
bool CFxBank::SetSize(int nPrograms, int nParams)
{
int nTotLen = sizeof(SFxBankBase);
int nProgLen = sizeof(SFxProgramBase) + nParams * sizeof(float);
nTotLen += nPrograms * nProgLen;
unsigned char *nBank = new unsigned char[nTotLen];
if (!nBank)
return false;
Unload();
bBank = nBank;
nBankLen = nTotLen;
bChunk = false;
memset(nBank, 0, nTotLen); /* initialize new bank */
SFxBank *pBank = (SFxBank *)bBank;
pBank->chunkMagic = cMagic;
pBank->byteSize = 0;
pBank->fxMagic = bankMagic;
pBank->version = MyVersion;
pBank->numPrograms = nPrograms;
unsigned char *bProg = (unsigned char *)pBank->programs;
for (int i = 0; i < nPrograms; i++)
{
SFxProgram * pProg = (SFxProgram *)(bProg + i * nProgLen);
pProg->chunkMagic = cMagic;
pProg->byteSize = 0;
pProg->fxMagic = fMagic;
pProg->version = 1;
pProg->numParams = nParams;
for (int j = 0; j < nParams; j++)
pProg->params[j] = 0.f;
}
return true;
}
bool CFxBank::SetSize(int nChunkSize)
{
int nTotLen = ((int)((SFxBankChunk *)0)->chunk) + nChunkSize;
unsigned char *nBank = new unsigned char[nTotLen];
if (!nBank)
return false;
Unload();
bBank = nBank;
nBankLen = nTotLen;
bChunk = true;
memset(nBank, 0, nTotLen); /* initialize new bank */
SFxBankChunk *pBank = (SFxBankChunk *)bBank;
pBank->chunkMagic = cMagic;
pBank->fxMagic = chunkBankMagic;
pBank->version = MyVersion;
pBank->numPrograms = 1;
pBank->size = nChunkSize;
return true;
}
/*****************************************************************************/
/* LoadBank : loads a bank file */
/*****************************************************************************/
bool CFxBank::LoadBank(char *pszFile)
{
FILE *fp = fopen(pszFile, "rb"); /* try to open the file */
if (!fp) /* upon error */
return false; /* return an error */
bool brc = true; /* default to OK */
unsigned char *nBank = NULL;
try
{
fseek(fp, 0, SEEK_END); /* get file size */
size_t tLen = (size_t)ftell(fp);
rewind(fp);
nBank = new unsigned char[tLen]; /* allocate storage */
if (!nBank)
throw (int)1;
/* read chunk set to determine cnt. */
if (fread(nBank, 1, tLen, fp) != tLen)
throw (int)1;
/* position on bank */
SFxBankBase *pBank = (SFxBankBase *)nBank;
if (NeedsBSwap) /* eventually swap necessary bytes */
{
SwapBytes(pBank->chunkMagic);
SwapBytes(pBank->byteSize);
SwapBytes(pBank->fxMagic);
SwapBytes(pBank->version);
SwapBytes(pBank->fxID);
SwapBytes(pBank->fxVersion);
SwapBytes(pBank->numPrograms);
}
if ((pBank->chunkMagic != cMagic) || /* if erroneous data in there */
(pBank->version > MyVersion) ||
((pBank->fxMagic != bankMagic) &&
(pBank->fxMagic != chunkBankMagic)))
throw (int)1; /* get out */
if (pBank->fxMagic == bankMagic) /* if this is a bank of parameters */
{
/* position on 1st program */
SFxProgram * pProg = ((SFxBank *)pBank)->programs;
int nProg = 0;
while (nProg < pBank->numPrograms) /* walk program list */
{
if (NeedsBSwap) /* eventually swap necessary bytes */
{
SwapBytes(pProg->chunkMagic);
SwapBytes(pProg->byteSize);
SwapBytes(pProg->fxMagic);
SwapBytes(pProg->version);
SwapBytes(pProg->fxID);
SwapBytes(pProg->fxVersion);
SwapBytes(pProg->numParams);
}
/* if erroneous data */
if ((pProg->chunkMagic != cMagic)||
(pProg->fxMagic != fMagic))
throw (int)1; /* get out */
if (NeedsBSwap) /* if necessary */
{ /* swap all parameter bytes */
int j;
for (j = 0; j < pProg->numParams; j++)
SwapBytes(pProg->params[j]);
}
unsigned char *pNext = (unsigned char *)pProg;
pNext += sizeof(SFxProgramBase);
pNext += (sizeof(float) * pProg->numParams);
if (pNext > nBank + tLen) /* VERY simple fuse */
throw (int)1;
pProg = (SFxProgram *)pNext;
nProg++;
}
}
/* if it's a chunk file */
else if (pBank->fxMagic == chunkBankMagic)
{
if (NeedsBSwap) /* eventually swap necessary bytes */
{
SFxBankChunk *pChunk = (SFxBankChunk *)pBank;
SwapBytes(pChunk->size);
/* size check - must not be too large*/
if (pChunk->size + ((size_t)((SFxBankChunk *)0)->chunk) > tLen)
throw (int)1;
}
}
Unload(); /* otherwise remove eventual old data*/
bBank = nBank; /* and put in new data */
nBankLen = (int)tLen;
bChunk = (pBank->fxMagic == chunkBankMagic);
}
catch(...)
{
brc = false; /* if any error occured, say NOPE */
if (nBank) /* and remove loaded data */
delete[] nBank;
}
fclose(fp); /* close the file */
return brc; /* and return */
}
/*****************************************************************************/
/* SaveBank : save bank to file */
/*****************************************************************************/
bool CFxBank::SaveBank(char *pszFile)
{
if (!IsLoaded())
return false;
/* create internal copy for mod */
unsigned char *nBank = new unsigned char[nBankLen];
if (!nBank) /* if impossible */
return false;
memcpy(nBank, bBank, nBankLen);
/* position on bank */
SFxBankBase *pBank = (SFxBankBase *)nBank;
int numPrograms = pBank->numPrograms;
if (NeedsBSwap) /* if byte-swapping needed */
{
SwapBytes(pBank->chunkMagic);
SwapBytes(pBank->byteSize);
SwapBytes(pBank->fxMagic);
SwapBytes(pBank->version);
SwapBytes(pBank->fxID);
SwapBytes(pBank->fxVersion);
SwapBytes(pBank->numPrograms);
}
if (bChunk)
{
if (NeedsBSwap) /* if byte-swapping needed */
SwapBytes(((SFxBankChunk *)pBank)->size);
}
else
{
/* position on 1st program */
SFxProgram * pProg = ((SFxBank *)pBank)->programs;
int numParams = pProg->numParams;
int nProg = 0;
while (nProg < numPrograms) /* walk program list */
{
if (NeedsBSwap) /* eventually swap all necessary */
{
SwapBytes(pProg->chunkMagic);
SwapBytes(pProg->byteSize);
SwapBytes(pProg->fxMagic);
SwapBytes(pProg->version);
SwapBytes(pProg->fxID);
SwapBytes(pProg->fxVersion);
SwapBytes(pProg->numParams);
for (int j = 0; j < numParams; j++)
SwapBytes(pProg->params[j]);
}
unsigned char *pNext = (unsigned char *)pProg;
pNext += sizeof(SFxProgramBase);
pNext += (sizeof(float) * numParams);
if (pNext > nBank + nBankLen) /* VERY simple fuse */
break;
pProg = (SFxProgram *)pNext;
nProg++;
}
}
bool brc = true; /* default to OK */
FILE *fp = NULL;
try
{
fp = fopen(pszFile, "wb"); /* try to open the file */
if (!fp) /* upon error */
throw (int)1; /* return an error */
if (fwrite(nBank, 1, nBankLen, fp) != (size_t)nBankLen)
throw (int)1;
}
catch(...)
{
brc = false;
}
if (fp)
fclose(fp);
delete[] nBank;
return brc;
}
/*****************************************************************************/
/* Unload : removes a loaded bank from memory */
/*****************************************************************************/
void CFxBank::Unload()
{
if (bBank)
delete[] bBank;
*szFileName = '\0'; /* reset file name */
bBank = NULL; /* reset bank pointer */
nBankLen = 0; /* reset bank length */
bChunk = false; /* and of course it's no chunk. */
}
/*****************************************************************************/
/* GetProgram : returns pointer to one of the loaded programs */
/*****************************************************************************/
SFxProgram * CFxBank::GetProgram(int nProgNum)
{
if ((!IsLoaded()) || (bChunk)) /* if nothing loaded or chunk file */
return NULL; /* return OUCH */
SFxBank *pBank = (SFxBank *)bBank; /* position on 1st program */
SFxProgram * pProg = pBank->programs;
int nProgLen = sizeof(SFxProgramBase) +
pProg->numParams * sizeof(float);
unsigned char *pThatProg = ((unsigned char *)pProg) + (nProgNum * nProgLen);
pProg = (SFxProgram *)pThatProg;
return pProg;
}
/*===========================================================================*/
/* CEffect class members */
/*===========================================================================*/
/*****************************************************************************/
/* CEffect : constructor */
/*****************************************************************************/
CEffect::CEffect(CVSTHost *pHost)
{
this->pHost = pHost;
pEffect = NULL;
sName = NULL;
bEditOpen = false;
bNeedIdle = false;
bInEditIdle = false;
bWantMidi = false;
bInSetProgram = false;
nIndex = -1;
nUniqueId = 0;
pMasterEffect = NULL;
#ifdef WIN32
hModule = NULL;
sDir = NULL;
#elif MAC
// yet to be done
#endif
}
/*****************************************************************************/
/* ~CEffect : destructor */
/*****************************************************************************/
CEffect::~CEffect()
{
Unload();
#ifdef WIN32
#elif MAC
#endif
}
/*****************************************************************************/
/* Load : loads the effect module */
/*****************************************************************************/
bool CEffect::Load(const char *name)
{
if (!pHost) /* if VST Host undefined */
return false; /* return without action */
#ifdef WIN32
#elif MAC
#endif
Unload(); /* make sure nothing else is loaded */
/* pointer to main function */
AEffect *(*pMain)(long (*audioMaster)(AEffect *effect,
long opcode,
long index,
long value,
void *ptr,
float opt)) = 0;
#ifdef WIN32
hModule = LoadLibrary(name); /* try to load the DLL */
if (hModule) /* if there, get its main() function */
{
pMain = (AEffect * (*)(long (*)(AEffect *,long,long,long,void *,float)))
::GetProcAddress(hModule, "VSTPluginMain");
if (!pMain)
pMain = (AEffect * (*)(long (*)(AEffect *,long,long,long,void *,float)))
::GetProcAddress(hModule, "main");
}
if (pMain) /* initialize effect */
{
__try
{
pEffect = pMain(pHost->AudioMasterCallback);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
pEffect = NULL;
}
if (pEffect == NULL)
{
FreeLibrary(hModule);
hModule = NULL;
}
}
#elif MAC
// yet to be done
if (pMain) /* initialize effect */
pEffect = pMain(pHost->AudioMasterCallback);
#endif
/* check for correctness */
if (pEffect && (pEffect->magic != kEffectMagic))
pEffect = NULL;
if (pEffect)
{
unsigned int len = strlen(name);
sName = new char[len + 1];
if (sName)
strcpy(sName, name);
sName[len] = 0;
#ifdef WIN32
const char *p = strrchr(name, '\\');
if (p)
{
sDir = new char[p - name + 1];
if (sDir)
{
memcpy(sDir, name, p - name);
sDir[p - name] = '\0';
}
}
#elif MAC
// yet to be done
#endif
}
#ifdef WIN32
#elif MAC
#endif
return !!pEffect;
}
/*****************************************************************************/
/* Unload : unloads effect module */
/*****************************************************************************/
bool CEffect::Unload()
{
if (!pHost) /* if no VST Host there */
return false;
EffClose(); /* make sure it's closed */
pEffect = NULL; /* and reset the pointer */
#ifdef WIN32
if (hModule) /* if DLL instance available */
{
::FreeLibrary(hModule); /* remove it. */
hModule = NULL; /* and reset the handle */
}
if (sDir) /* reset module directory */
{
delete[] sDir;
sDir = NULL;
}
#elif MAC
// yet to be done!
#endif
if (sName) /* reset module name */
{
delete[] sName;
sName = NULL;
}
return true;
}
/*****************************************************************************/
/* LoadBank : loads a .fxb file ... IF it's for this effect */
/*****************************************************************************/
bool CEffect::LoadBank(char *name)
{
try
{
CFxBank fx(name); /* load the bank */
if (!fx.IsLoaded()) /* if error loading */
throw (int)1;
}
catch(...) /* if any error occured */
{
return false; /* return NOT!!! */
}
return true; /* pass back OK */
}
/*****************************************************************************/
/* SaveBank : saves current sound bank to a .fxb file */
/*****************************************************************************/
bool CEffect::SaveBank(char *name)
{
return false; /* return error for now */
}
/*****************************************************************************/
/* OnGetDirectory : returns the plug's directory (char* on pc, FSSpec on mac)*/
/*****************************************************************************/
void * CEffect::OnGetDirectory()
{
#ifdef WIN32
return sDir;
#elif MAC
// yet to be done
#endif
}
/*****************************************************************************/
/* EffDispatch : calls an effect's dispatcher */
/*****************************************************************************/
long CEffect::EffDispatch
(
long opCode,
long index,
long value,
void *ptr,
float opt
)
{
long ret = 0;
if (pEffect)
{
__try
{
ret = pEffect->dispatcher(pEffect, opCode, index, value, ptr, opt);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
ret = 0;
}
}
return ret;
}
/*****************************************************************************/
/* EffProcess : calles an effect's process() function */
/*****************************************************************************/
void CEffect::EffProcess(float **inputs, float **outputs, long sampleframes)
{
if (!pEffect)
return;
pEffect->process(pEffect, inputs, outputs, sampleframes);
}
/*****************************************************************************/
/* EffProcessReplacing : calls an effect's processReplacing() function */
/*****************************************************************************/
void CEffect::EffProcessReplacing(float **inputs, float **outputs, long sampleframes)
{
if ((!pEffect) ||
(!(pEffect->flags & effFlagsCanReplacing)))
return;
pEffect->processReplacing(pEffect, inputs, outputs, sampleframes);
}
/*****************************************************************************/
/* EffProcessDoubleReplacing : calls an effect's processDoubleReplacing() f. */
/*****************************************************************************/
void CEffect::EffProcessDoubleReplacing(double **inputs, double **outputs, long sampleFrames)
{
if ((!pEffect) ||
(!(pEffect->flags & effFlagsCanDoubleReplacing)))
return;
#if defined(VST_2_4_EXTENSIONS)
pEffect->processDoubleReplacing(pEffect, inputs, outputs, sampleFrames);
#endif
}
/*****************************************************************************/
/* EffSetParameter : calls an effect's setParameter() function */
/*****************************************************************************/
void CEffect::EffSetParameter(long index, float parameter)
{
if (!pEffect)
return;
pEffect->setParameter(pEffect, index, parameter);
}
/*****************************************************************************/
/* EffGetParameter : calls an effect's getParameter() function */
/*****************************************************************************/
float CEffect::EffGetParameter(long index)
{
if (!pEffect)
return 0.;
float frc = pEffect->getParameter(pEffect, index);
return frc;
}
/*****************************************************************************/
/* EffUsesChunks : Uses chunks or misuses chunks */
/*****************************************************************************/
bool CEffect::EffUsesChunks()
{
if (!pEffect)
return false;
return (pEffect->flags & effFlagsProgramChunks) != 0;
}
void CEffect::EnterCritical()
{
EnterCriticalSection(&cs); /* inhibit others! */
}
void CEffect::LeaveCritical()
{
LeaveCriticalSection(&cs); /* reallow others */
}
/*===========================================================================*/
/* CVSTHost class members */
/*===========================================================================*/
CVSTHost * CVSTHost::pHost = NULL; /* pointer to the one and only host */
/*****************************************************************************/
/* CVSTHost : constructor */
/*****************************************************************************/
CVSTHost::CVSTHost()
{
if (pHost) /* disallow more than one host! */
throw((int)1);
fSampleRate = 44100.; /* initialize to sensible values */
lBlockSize = 1024;
vstTimeInfo.samplePos = 0.0;
vstTimeInfo.sampleRate = fSampleRate;
vstTimeInfo.nanoSeconds = 0.0;
vstTimeInfo.ppqPos = 0.0;
vstTimeInfo.tempo = 120.0;
vstTimeInfo.barStartPos = 0.0;
vstTimeInfo.cycleStartPos = 0.0;
vstTimeInfo.cycleEndPos = 0.0;
vstTimeInfo.timeSigNumerator = 4;
vstTimeInfo.timeSigDenominator = 4;
vstTimeInfo.smpteOffset = 0;
vstTimeInfo.smpteFrameRate = 1;
vstTimeInfo.samplesToNextClock = 0;
vstTimeInfo.flags = 0;
aEffects = 0; /* no effects loaded */
naEffects = nmaEffects = 0;
pLoading = NULL;
pHost = this; /* install this instance as the one */
}
/*****************************************************************************/
/* ~CVSTHost : destructor */
/*****************************************************************************/
CVSTHost::~CVSTHost()
{
RemoveAll(); /* remove all loaded effects */
if (aEffects) /* if effects pointer array there */
delete[] aEffects; /* delete it */
if (pHost == this) /* if we're the chosen one */
pHost = NULL; /* remove ourselves from pointer */
}
/*****************************************************************************/
/* CalcTimeInfo : calculates time info from nanoSeconds */
/*****************************************************************************/
void CVSTHost::CalcTimeInfo(long lMask)
{
// we don't care for the mask in here
static double fSmpteDiv[] =
{
24.f,
25.f,
24.f,
30.f,
29.97f,
30.f
};
double dPos = vstTimeInfo.samplePos / vstTimeInfo.sampleRate;
vstTimeInfo.ppqPos = dPos * vstTimeInfo.tempo / 60.L;
/* offset in fractions of a second */
double dOffsetInSecond = dPos - floor(dPos);
vstTimeInfo.smpteOffset = (long)(dOffsetInSecond *
fSmpteDiv[vstTimeInfo.smpteFrameRate] *
80.L);
}
/*****************************************************************************/
/* AudioMasterCallback : callback to be called by plugins */
/*****************************************************************************/
long VSTCALLBACK CVSTHost::AudioMasterCallback
(
AEffect *effect,
long opcode,
long index,
long value,
void *ptr,
float opt
)
{
if (!pHost)
return 0;
return pHost->OnAudioMasterCallback(pHost->Search(effect),
opcode,
index,
value,
ptr,
opt);
}
/*****************************************************************************/
/* OnAudioMasterCallback : callback dispatcher */
/*****************************************************************************/
long CVSTHost::OnAudioMasterCallback
(
int nEffect,
long opcode,
long index,
long value,
void *ptr,
float opt
)
{
switch (opcode)
{
case audioMasterAutomate :
return OnSetParameterAutomated(nEffect, index, opt);
case audioMasterVersion :
return OnGetVersion(nEffect);
case audioMasterCurrentId :
return OnGetCurrentUniqueId(nEffect);
case audioMasterIdle :
return OnIdle(nEffect);
case audioMasterPinConnected :
return !((value) ?
OnOutputConnected(nEffect, index) :
OnInputConnected(nEffect, index));
/* VST 2.0 additions... */
case audioMasterWantMidi :
return OnWantEvents(nEffect, value);
case audioMasterGetTime :
return (long)OnGetTime(nEffect, value);
case audioMasterProcessEvents :
return OnProcessEvents(nEffect, (VstEvents *)ptr);
case audioMasterSetTime :
return OnSetTime(nEffect, value, (VstTimeInfo *)ptr);
case audioMasterTempoAt :
return OnTempoAt(nEffect, value);
case audioMasterGetNumAutomatableParameters :
return OnGetNumAutomatableParameters(nEffect);
case audioMasterGetParameterQuantization :
return OnGetParameterQuantization(nEffect);
case audioMasterIOChanged :
return OnIoChanged(nEffect);
case audioMasterNeedIdle :
return OnNeedIdle(nEffect);
case audioMasterSizeWindow :
return OnSizeWindow(nEffect, index, value);
case audioMasterGetSampleRate :
return OnUpdateSampleRate(nEffect);
case audioMasterGetBlockSize :
return OnUpdateBlockSize(nEffect);
case audioMasterGetInputLatency :
return OnGetInputLatency(nEffect);
case audioMasterGetOutputLatency :
return OnGetOutputLatency(nEffect);
case audioMasterGetPreviousPlug :
return GetPreviousPlugIn(nEffect);
case audioMasterGetNextPlug :
return GetNextPlugIn(nEffect);
case audioMasterWillReplaceOrAccumulate :
return OnWillProcessReplacing(nEffect);
case audioMasterGetCurrentProcessLevel :
return OnGetCurrentProcessLevel(nEffect);
case audioMasterGetAutomationState :
return OnGetAutomationState(nEffect);
case audioMasterOfflineStart :
return OnOfflineStart(nEffect,
(VstAudioFile *)ptr,
value,
index);
break;
case audioMasterOfflineRead :
return OnOfflineRead(nEffect,
(VstOfflineTask *)ptr,
(VstOfflineOption)value,
!!index);
break;
case audioMasterOfflineWrite :
return OnOfflineWrite(nEffect,
(VstOfflineTask *)ptr,
(VstOfflineOption)value);
break;
case audioMasterOfflineGetCurrentPass :
return OnOfflineGetCurrentPass(nEffect);
case audioMasterOfflineGetCurrentMetaPass :
return OnOfflineGetCurrentMetaPass(nEffect);
case audioMasterSetOutputSampleRate :
OnSetOutputSampleRate(nEffect, opt);
return 1;
#ifdef VST_2_4_EXTENSIONS
case audioMasterGetOutputSpeakerArrangement :
#else
case audioMasterGetSpeakerArrangement :
#endif
return OnGetOutputSpeakerArrangement(nEffect,
(VstSpeakerArrangement *)value,
(VstSpeakerArrangement *)ptr);
case audioMasterGetVendorString :