-
Notifications
You must be signed in to change notification settings - Fork 14
/
SmpVSTHost.cpp
1411 lines (1275 loc) · 49.4 KB
/
SmpVSTHost.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
/*****************************************************************************/
/* SmpVSTHost.cpp: CSmpVSTHost / CSmpEffect implementation */
/*****************************************************************************/
/******************************************************************************
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
******************************************************************************/
#include "stdafx.h"
#include "ChildFrm.h"
#include "EffectWnd.h"
#include "EffSecWnd.h"
#include "specmidi.h"
#include "specwave.h"
#include "vstsysex.h"
#include "SmpVSTHost.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#pragma intrinsic(memcpy,memset)
/*===========================================================================*/
/* CSmpVSTHost class members */
/*===========================================================================*/
static const int nMaxMidi = 2000; /* max. # MIDI messages buffered for */
/* EffProcess() */
static const int nMaxSysEx = 100; /* max. # MIDI SysEx messages */
/* buffered for EffProcess() */
/*****************************************************************************/
/* CSmpVSTHost : constructor */
/*****************************************************************************/
CSmpVSTHost::CSmpVSTHost()
{
// for this sample project, I've assumed a maximum buffer size
// of 1/3 second at 96KHz - presumably, this is MUCH too much
// and should be tweaked to more reasonable values, since it
// requires a machine with lots of main memory this way...
// user configurability would be nice, of course.
for (int i = 0; i < 2; i++)
pOutputs[i] = new float[32000];
pMidis = new DWORD[nMaxMidi * 2];
nMidis = 0;
pSysEx = new CMidiMsg[nMaxSysEx];
nSysEx = 0;
InitializeCriticalSection(&cs);
}
/*****************************************************************************/
/* ~CSmpVSTHost : destructor */
/*****************************************************************************/
CSmpVSTHost::~CSmpVSTHost()
{
for (int i = 0; i < 2; i++)
delete[] pOutputs[i];
if (pMidis)
delete[] pMidis;
if (pSysEx)
delete[] pSysEx;
}
/*****************************************************************************/
/* OnAudioMasterCallback : redefine the callback a bit */
/*****************************************************************************/
long CSmpVSTHost::OnAudioMasterCallback
(
int nEffect,
long opcode,
long index,
long value,
void *ptr,
float opt
)
{
#if defined(_DEBUG) || defined(_DEBUGFILE)
char szDebug[1024];
int nLen;
switch (opcode)
{
case audioMasterAutomate :
nLen = sprintf(szDebug, "callback: %d audioMasterAutomate %d, %f", nEffect, index, opt);
break;
case audioMasterVersion :
nLen = sprintf(szDebug, "callback: %d audioMasterVersion", nEffect);
break;
case audioMasterCurrentId :
nLen = sprintf(szDebug, "callback: %d audioMasterCurrentId", nEffect);
break;
case audioMasterIdle :
nLen = sprintf(szDebug, "callback: %d audioMasterIdle", nEffect);
break;
case audioMasterPinConnected :
nLen = sprintf(szDebug, "callback: %d audioMasterPinConnected %d", nEffect, index);
break;
/* VST 2.0 additions... */
case audioMasterWantMidi :
nLen = sprintf(szDebug, "callback: %d audioMasterWantMidi %d", nEffect, value);
break;
case audioMasterGetTime :
nLen = sprintf(szDebug, "callback: %d audioMasterGetTime %d", nEffect, value);
break;
case audioMasterProcessEvents :
nLen = sprintf(szDebug, "callback: %d audioMasterProcessEvents %d", nEffect, ((VstEvents *)ptr)->numEvents);
break;
case audioMasterSetTime :
nLen = sprintf(szDebug, "callback: %d audioMasterSetTime %d", nEffect, value);
break;
case audioMasterTempoAt :
nLen = sprintf(szDebug, "callback: %d audioMasterTempoAt %d", nEffect, value);
break;
case audioMasterGetNumAutomatableParameters :
nLen = sprintf(szDebug, "callback: %d audioMasterGetNumAutomatableParameters", nEffect);
break;
case audioMasterGetParameterQuantization :
nLen = sprintf(szDebug, "callback: %d audioMasterGetParameterQuantization", nEffect);
break;
case audioMasterIOChanged :
nLen = sprintf(szDebug, "callback: %d audioMasterIOChanged", nEffect);
break;
case audioMasterNeedIdle :
nLen = sprintf(szDebug, "callback: %d audioMasterNeedIdle", nEffect);
break;
case audioMasterSizeWindow :
nLen = sprintf(szDebug, "callback: %d audioMasterSizeWindow %d,%d", nEffect, index, value);
break;
case audioMasterGetSampleRate :
nLen = sprintf(szDebug, "callback: %d audioMasterGetSampleRate", nEffect);
break;
case audioMasterGetBlockSize :
nLen = sprintf(szDebug, "callback: %d audioMasterGetBlockSize", nEffect);
break;
case audioMasterGetInputLatency :
nLen = sprintf(szDebug, "callback: %d audioMasterGetInputLatency", nEffect);
break;
case audioMasterGetOutputLatency :
nLen = sprintf(szDebug, "callback: %d audioMasterGetOutputLatency", nEffect);
break;
case audioMasterGetPreviousPlug :
nLen = sprintf(szDebug, "callback: %d audioMasterGetPreviousPlug", nEffect);
break;
case audioMasterGetNextPlug :
nLen = sprintf(szDebug, "callback: %d audioMasterGetNextPlug", nEffect);
break;
case audioMasterWillReplaceOrAccumulate :
nLen = sprintf(szDebug, "callback: %d audioMasterWillReplaceOrAccumulate", nEffect);
break;
case audioMasterGetCurrentProcessLevel :
nLen = sprintf(szDebug, "callback: %d audioMasterGetCurrentProcessLevel", nEffect);
break;
case audioMasterGetAutomationState :
nLen = sprintf(szDebug, "callback: %d audioMasterGetAutomationState", nEffect);
break;
case audioMasterOfflineStart :
nLen = sprintf(szDebug, "callback: %d audioMasterOfflineStart %d,%d", nEffect, value, index);
break;
case audioMasterOfflineRead :
nLen = sprintf(szDebug, "callback: %d audioMasterOfflineRead %d,%d", nEffect, value, index);
break;
case audioMasterOfflineWrite :
nLen = sprintf(szDebug, "callback: %d audioMasterOfflineWrite %d", nEffect, value);
break;
case audioMasterOfflineGetCurrentPass :
nLen = sprintf(szDebug, "callback: %d audioMasterOfflineGetCurrentPass", nEffect);
break;
case audioMasterOfflineGetCurrentMetaPass :
nLen = sprintf(szDebug, "callback: %d audioMasterOfflineGetCurrentMetaPass", nEffect);
break;
case audioMasterSetOutputSampleRate :
nLen = sprintf(szDebug, "callback: %d audioMasterSetOutputSampleRate %f", nEffect, opt);
break;
#ifdef VST_2_3_EXTENSIONS
case audioMasterGetOutputSpeakerArrangement :
nLen = sprintf(szDebug, "callback: %d audioMasterGetOutputSpeakerArrangement", nEffect);
break;
#else
case audioMasterGetSpeakerArrangement :
nLen = sprintf(szDebug, "callback: %d audioMasterGetSpeakerArrangement", nEffect);
break;
#endif
case audioMasterGetVendorString :
nLen = sprintf(szDebug, "callback: %d audioMasterGetVendorString", nEffect);
break;
case audioMasterGetProductString :
nLen = sprintf(szDebug, "callback: %d audioMasterGetProductString", nEffect);
break;
case audioMasterGetVendorVersion :
nLen = sprintf(szDebug, "callback: %d audioMasterGetVendorVersion", nEffect);
break;
case audioMasterVendorSpecific :
nLen = sprintf(szDebug, "callback: %d audioMasterVendorSpecific %d,%d,%d,%08lX,%f", nEffect, opcode, index, value, ptr, opt);
break;
case audioMasterSetIcon :
nLen = sprintf(szDebug, "callback: %d audioMasterSetIcon", nEffect);
break;
case audioMasterCanDo :
nLen = sprintf(szDebug, "callback: %d audioMasterCanDo \"%s\"", nEffect, ptr);
break;
case audioMasterGetLanguage :
nLen = sprintf(szDebug, "callback: %d audioMasterGetLanguage", nEffect);
break;
case audioMasterOpenWindow :
nLen = sprintf(szDebug, "callback: %d audioMasterOpenWindow", nEffect);
break;
case audioMasterCloseWindow :
nLen = sprintf(szDebug, "callback: %d audioMasterCloseWindow", nEffect);
break;
case audioMasterGetDirectory :
nLen = sprintf(szDebug, "callback: %d audioMasterGetDirectory", nEffect);
break;
case audioMasterUpdateDisplay :
nLen = sprintf(szDebug, "callback: %d audioMasterUpdateDisplay", nEffect);
break;
//---from here VST 2.1 extension opcodes------------------------------------------------------
#ifdef VST_2_1_EXTENSIONS
case audioMasterBeginEdit :
nLen = sprintf(szDebug, "callback: %d audioMasterBeginEdit Parameter %d", nEffect, index);
break;
case audioMasterEndEdit :
nLen = sprintf(szDebug, "callback: %d audioMasterEndEdit Parameter %d", nEffect, index);
break;
case audioMasterOpenFileSelector :
nLen = sprintf(szDebug, "callback: %d audioMasterOpenFileSelector", nEffect);
break;
#endif
//---from here VST 2.2 extension opcodes------------------------------------------------------
#ifdef VST_2_2_EXTENSIONS
case audioMasterCloseFileSelector :
nLen = sprintf(szDebug, "callback: %d audioMasterCloseFileSelector", nEffect);
break;
case audioMasterEditFile :
nLen = sprintf(szDebug, "callback: %d audioMasterEditFile \"%s\"", nEffect, ptr);
break;
case audioMasterGetChunkFile :
nLen = sprintf(szDebug, "callback: %d audioMasterGetChunkFile", nEffect);
break;
#endif
//---from here VST 2.3 extension opcodes------------------------------------------------------
#ifdef VST_2_1_EXTENSIONS
case audioMasterGetInputSpeakerArrangement :
nLen = sprintf(szDebug, "callback: %d audioMasterGetInputSpeakerArrangement", nEffect);
break;
#endif
}
#endif
long lRC = CVSTHost::OnAudioMasterCallback(nEffect, opcode, index, value, ptr, opt);
#if defined(_DEBUG) || defined(_DEBUGFILE)
switch (opcode)
{
case audioMasterGetChunkFile :
nLen += sprintf(szDebug + nLen, " \"%s\"", ptr);
break;
}
sprintf(szDebug + nLen, " RC=%d", lRC);
TRACE1("%s\n", szDebug);
#endif
return lRC;
}
/*****************************************************************************/
/* OnSetParameterAutomated : make sure parameter changes are reflected */
/*****************************************************************************/
bool CSmpVSTHost::OnSetParameterAutomated(int nEffect, long index, float value)
{
CSmpEffect *pEffect = (CSmpEffect *)GetAt(nEffect);
if (pEffect)
pEffect->OnSetParameterAutomated(index, value);
return CVSTHost::OnSetParameterAutomated(nEffect, index, value);
}
/*****************************************************************************/
/* OnMidiIn : called when a MIDI message comes in */
/*****************************************************************************/
void CSmpVSTHost::OnMidiIn(CMidiMsg &msg)
{
if (msg[0] > 0xf0) /* VSTInstruments don't expect */
return; /* realtime messages! */
else if (msg[0] == 0xf0) /* but SysEx are processed */
{
EnterCriticalSection(&cs); /* must not interfere! */
if ((pSysEx) && /* if allocated, and */
(nSysEx < nMaxSysEx)) /* if still space for another one */
pSysEx[nSysEx++] = msg; /* copy it in */
LeaveCriticalSection(&cs);
}
else
{
EnterCriticalSection(&cs); /* must not interfere! */
if (nMidis < 2 * nMaxMidi) /* if enough place to do it, */
{
pMidis[nMidis++] = msg; /* buffer the message */
pMidis[nMidis++] = msg.GetStamp(); /* and its timestamp */
// TRACE2("MIDI Message %08lX at %d\n", (DWORD)msg, msg.GetStamp());
}
LeaveCriticalSection(&cs);
}
}
/*****************************************************************************/
/* OnProcessEvents : called when an effect sends events */
/*****************************************************************************/
bool CSmpVSTHost::OnProcessEvents(int nEffect, VstEvents *events)
{
int i;
CMidiMsg msg;
bool bErr = false;
for (i = 0; i < events->numEvents; i++) /* process all sent MIDI events */
{
switch (events->events[i]->type)
{
case kVstMidiType : /* normal MIDI message? */
msg.Set(((VstMidiEvent *)(events->events[i]))->midiData, 3);
bErr |= !MidiOut.Output(msg); /* send it to MIDI Out */
break;
case kVstSysExType : /* SysEx message ? */
msg.Set(((VstMidiSysexEvent *)(events->events[i]))->sysexDump,
((VstMidiSysexEvent *)(events->events[i]))->dumpBytes);
bErr |= !MidiOut.Output(msg); /* send it to MIDI Out */
break;
#if 0 // silently ignore all non-MIDI events
default : /* anything else? */
bErr = true;
break;
#endif
}
}
return !bErr;
}
/*****************************************************************************/
/* CreateMIDIEvents : creates a set of VST MIDI events */
/*****************************************************************************/
BYTE * CSmpVSTHost::CreateMIDIEvents
(
int nLength, /* # samples */
DWORD dwStamp, /* timestamp of 1st sample */
int nm, /* # normal MIDI messages */
int ns /* # sysex messages */
)
{
if ((!nm) && (!ns)) /* if neither normal nor sysex there */
return NULL; /* nothing to do in here */
BYTE *pEvData;
int nHdrLen, nTotLen;
int i;
DWORD dwDelta;
VstMidiEvent *pEv;
VstMidiSysexEvent *pSEv;
int nSysBase;
nHdrLen = sizeof(VstEvents) + /* calculate header length */
((nm - 1) * sizeof(VstMidiEvent *)) +
(ns * sizeof(VstMidiSysexEvent *));
nTotLen = nHdrLen + /* calculate total events length */
(nm * sizeof(VstMidiEvent)) +
(ns * sizeof(VstMidiSysexEvent));
pEvData = new BYTE[nTotLen]; /* allocate space for events */
if (pEvData) /* if that worked, */
{ /* copy in normal and SysEx */
VstEvents *pEvents = (VstEvents *) pEvData;
memset(pEvents, 0, nTotLen);
pEvents->numEvents = (nm / 2) + ns;
for (i = 0; i < nm; i += 2) /* copy in all normal MIDI messages */
{
pEv = ((VstMidiEvent *)(pEvData + nHdrLen)) + (i / 2);
pEvents->events[i / 2] = (VstEvent *)pEv;
pEv->type = kVstMidiType;
pEv->byteSize = sizeof(VstMidiEvent);
memcpy(pEv->midiData, pMidis + i, sizeof(DWORD));
// offset assumes a fixed rate of 44.1kHz and drops 1 percent accuracy
dwDelta = (pMidis[i + 1] - dwStamp) * 44;
if (dwDelta > (DWORD)nLength)
dwDelta = nLength - 1;
pEv->deltaFrames = dwDelta;
/* ALL VSTHost events are realtime */
pEv->flags = kVstMidiEventIsRealtime;
}
nm >>= 1;
nSysBase = nHdrLen + (sizeof(VstMidiEvent) * nm);
for (i = 0; i < ns; i++) /* copy in all SysEx messages */
{
pSEv = ((VstMidiSysexEvent *)(pEvData + nSysBase)) + i;
pEvents->events[nm + i] = (VstEvent *)pSEv;
pSEv->type = kVstSysExType;
pSEv->byteSize = sizeof(VstMidiSysexEvent);
pSEv->deltaFrames = pSysEx[i].GetStamp() - dwStamp;
pSEv->dumpBytes = pSysEx[i].Length();
pSEv->sysexDump = pSysEx[i];
pSEv->flags = pSEv->resvd1 = pSEv->resvd2 = 0;
}
}
return pEvData;
}
/*****************************************************************************/
/* CreateMIDISubset : creates an event subset for a specific effect */
/*****************************************************************************/
BYTE * CSmpVSTHost::CreateMIDISubset(void *pEvData, unsigned short wChnMask)
{
if (!pEvData)
return NULL;
VstEvents *pEvents = (VstEvents *) pEvData;
VstMidiEvent *pEv;
int i, nMatching = 0;
for (i = pEvents->numEvents - 1; i >= 0; i--)
{
pEv = (VstMidiEvent *)pEvents->events[i];
if ((pEv->type == kVstSysExType) || /* SysEx goes to all, */
(wChnMask & /* for normal MIDI channel has to fit*/
(1 << (pEv->midiData[0] & 0x0F))))
nMatching++;
}
if (!nMatching)
return NULL;
int nHdrLen = sizeof(VstEvents) +
((nMatching - 1) * sizeof(VstMidiEvent *));
BYTE * pEffEvData = new BYTE[nHdrLen];
VstEvents *pEffEvents = (VstEvents *) pEffEvData;
pEffEvents->numEvents = nMatching;
nMatching = 0;
for (i = 0; i < pEvents->numEvents; i++)
{
pEv = (VstMidiEvent *)pEvents->events[i];
if ((pEv->type == kVstSysExType) || /* SysEx goes to all, */
(wChnMask & /* for normal MIDI channel has to fit*/
(1 << (pEv->midiData[0] & 0x0F))))
pEffEvents->events[nMatching++] = pEvents->events[i];
}
return pEffEvData;
}
/*****************************************************************************/
/* PassThruEffect : passes the sample data through one of the effects */
/*****************************************************************************/
void CSmpVSTHost::PassThruEffect
(
CSmpEffect *pEff, /* effect to call */
float **pBuffer, /* input buffers */
int nLength, /* length of these */
int nChannels, /* # channels */
BYTE *pEvData, /* Midi events during this period */
bool bReplacing
)
{
BYTE *pEffEvData = CreateMIDISubset(pEvData, pEff->GetChnMask());
float *pBuf1;
int j; /* loop counters */
pEff->EnterCritical(); /* make sure we don't get killed now */
if (pEff->bWantMidi) /* if effect wants MIDI events */
{
static VstEvents noEvData = {0}; /* necessary for Reaktor ... */
try
{
pEff->EffProcessEvents((pEffEvData) ? (VstEvents *)pEffEvData : &noEvData);
}
catch(...)
{
TRACE0("Serious error in pEff->EffProcessEvents()!\n");
}
}
float *pEmpty = GetApp()->GetEmptyBuffer();
for (j = 0; j < nChannels; j++)
pEff->SetInputBuffer(j, pBuffer[j]);
for (; j < pEff->pEffect->numInputs; j++)
pEff->SetInputBuffer(j, pEmpty);
/* then process the buffer itself */
/* clean all output buffers */
// NB: this has to be done even in EffProcessReplacing() since some VSTIs
// (most notably those from Steinberg... hehe) obviously don't implement
// processReplacing() as a separate function but rather use process()
for (j = 0; j < pEff->pEffect->numOutputs; j++)
{
pBuf1 = pEff->GetOutputBuffer(j);
if (pBuf1)
memcpy(pBuf1, pEmpty, nLength * sizeof(float));
}
/* if replacing implemented */
if ((bReplacing) && /* and wanted */
(pEff->pEffect->flags & effFlagsCanReplacing))
pEff->EffProcessReplacing(nLength); /* do processReplacing */
else /* otherwise */
pEff->EffProcess(nLength); /* call process */
pEff->LeaveCritical(); /* reallow killing */
if (pEffEvData) /* if there are event data for this */
delete pEffEvData; /* delete 'em */
}
/*****************************************************************************/
/* OnSamples : called when sample data come in */
/*****************************************************************************/
void CSmpVSTHost::OnSamples
(
float **pBuffer,
int nLength,
int nChannels,
DWORD dwStamp
)
{
int nEffs = 0; /* # contributing effects */
int i, j, k; /* loop counters */
int nSize = GetSize(); /* get # affected effects */
int nm = nMidis;
int ns = nSysEx;
CSmpEffect *pEff, *pNextEff;
float *pBuf1, *pBuf2;
CVsthostApp *pApp = GetApp();
float *pEmpty = pApp->GetEmptyBuffer();
// NO optimizations in here, as well as NO checks for overruns etc...
if (nLength > lBlockSize) /* prevent buffer overflow! */
nLength = lBlockSize;
vstTimeInfo.nanoSeconds = (double)timeGetTime() * 1000000.0L;
CalcTimeInfo();
BYTE *pEvData = CreateMIDIEvents(nLength, dwStamp, nm, ns);
for (i = 0; i < nSize; i++) /* pass them through all interested */
{ /* effects */
pEff = (CSmpEffect *)GetAt(i);
if ((!pEff) || /* if no effect at this position */
(pEff->GetPrev())) /* or it's part of a chain */
continue; /* skip this one */
/* pass through the effect */
PassThruEffect(pEff,
pBuffer, nLength, nChannels,
pEvData,
true);
while ((pNextEff = pEff->GetNext())) /* while more in chain */
{
PassThruEffect(pNextEff, /* pass these through next effect */
pEff->GetOutputBuffers(), nLength, nChannels,
pEvData,
true);
pEff = pNextEff; /* and advance to next in chain */
}
nEffs++; /* increment #participating effects */
}
if (pEvData) /* delete eventual MIDI event data */
delete pEvData;
/* make sure we don't lose new events*/
EnterCriticalSection(&cs);
if (nm < nMidis)
for (i = nm; i < nMidis; i++)
pMidis[i - nm] = pMidis[i];
nMidis -= nm;
if (ns < nSysEx)
for (i = ns; i < nSysEx; i++)
pSysEx[i - ns] = pSysEx[i];
nSysEx -= ns;
LeaveCriticalSection(&cs);
if (!nEffs) /* if no effects there */
{ /* just mute input */
for (j = 0; j < nChannels; j++)
{
pBuf1 = pBuffer[j];
memcpy(pBuf1, pEmpty, nLength * sizeof(float));
}
pApp->SendResult(pBuffer, nLength); /* and blow out some silence */
}
else /* otherwise */
{
int nOut = 0;
// this is a hard-wired mixer - should be replaced with a "real" mixer
float fMult = (float)sqrt(1.0f / nEffs);
for (i = 0; i < GetSize(); i++) /* merge all generated outputs */
{
// VERY simple version... all loaded effects' outputs are merged
// into a parallelized stream of stereo samples; no chaining, fixed mixing;
// no sample rate conversion or the like;
// the # effects determines a single effect's contribution to the output.
pEff = (CSmpEffect *)GetAt(i);
if ((!pEff) || /* if no effect at this position */
(pEff->GetPrev())) /* or it's part of a chain */
continue;
while ((pNextEff = pEff->GetNext())) /* walk to last in chain */
pEff = pNextEff; /* which has the final buffers */
pEff->EnterCritical(); /* make sure we don't get killed now */
for (j = 0; j < 2; j++) /* do for our 2 outputs */
{
pBuf1 = pOutputs[j];
pBuf2 = pEff->GetOutputBuffer(j);
if ((!pBuf1) || (!pBuf2))
break;
if (!nOut)
for (k = 0; k < nLength; k++) /* loop through the samples */
*pBuf1++ = *pBuf2++ *fMult;
else
for (k = 0; k < nLength; k++) /* loop through the samples */
*pBuf1++ += *pBuf2++ * fMult;
}
pEff->LeaveCritical(); /* reallow killing */
nOut++;
}
// now that we've got a populated output buffer set, send it to
// the Wave Output device.
pApp->SendResult(pOutputs, nLength);
}
vstTimeInfo.samplePos += (float)nLength;
/* in any case, reset "changed" flag */
vstTimeInfo.flags &= ~kVstTransportChanged;
}
/*****************************************************************************/
/* OnCanDo : plugin wants to know whether host can do... */
/*****************************************************************************/
bool CSmpVSTHost::OnCanDo(const char *ptr)
{
if ((!strcmp(ptr, "openFileSelector")) ||
(!strcmp(ptr, "closeFileSelector")) ||
// (!strcmp(ptr, "supportShell")) || /* old-style Waves plugin shell */
(!strcmp(ptr, "shellCategory")) ||
(!strcmp(ptr, "supplyIdle")) )
return true;
return CVSTHost::OnCanDo(ptr);
}
/*****************************************************************************/
/* OnOpenFileSelector : called when an effect needs a file selector */
/*****************************************************************************/
static int CALLBACK BrowseCallbackProc
(
HWND hwnd,
UINT uMsg,
LPARAM lParam,
LPARAM lpData
)
{
if (uMsg == BFFM_INITIALIZED)
{
if (lpData && *((LPCSTR)lpData))
::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
}
return 0;
}
bool CSmpVSTHost::OnOpenFileSelector (int nEffect, VstFileSelect *ptr)
{
#if defined(VST_2_1_EXTENSIONS)
ptr->reserved = 0;
if (ptr->command == kVstMultipleFilesLoad)
return false; // not yet
else if ((ptr->command == kVstFileLoad) ||
(ptr->command == kVstFileSave))
{
CString sFilter;
for (int i = 0; i < ptr->nbFileTypes; i++)
{
sFilter += CString(ptr->fileTypes[i].name) +
" (*." +
ptr->fileTypes[i].dosType +
")|*." +
ptr->fileTypes[i].dosType +
"|";
}
if (!sFilter.IsEmpty())
sFilter += "|";
CFileDialog dlg((ptr->command != kVstFileSave),
NULL, /* default extension */
ptr->initialPath, /* filename */
OFN_HIDEREADONLY | /* flags */
OFN_OVERWRITEPROMPT
#if 0
// not yet
| ((ptr->command == kVstMultipleFilesLoad) ? OFN_ALLOWMULTISELECT : 0)
#endif
,
sFilter, /* filter */
NULL); /* parent window */
dlg.m_ofn.lpstrTitle = ptr->title;
if (dlg.DoModal() == IDOK)
{
if (!ptr->returnPath)
{
ptr->returnPath = new char[dlg.GetPathName().GetLength() + 1];
ptr->reserved = 1;
}
strcpy(ptr->returnPath, dlg.GetPathName());
ptr->nbReturnPath = 1;
return true;
}
}
else if (ptr->command == kVstDirectorySelect)
{
char szDisplayName[_MAX_PATH];
BROWSEINFO bi = {0};
bi.hwndOwner = AfxGetMainWnd()->GetSafeHwnd();
bi.pszDisplayName = szDisplayName;
bi.lpszTitle = ptr->title;
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpfn = BrowseCallbackProc;
bi.lParam = (LPARAM)ptr->initialPath;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if (pidl)
{
if (SHGetPathFromIDList(pidl, szDisplayName))
{
if (!ptr->returnPath)
{
ptr->returnPath = new char[strlen(szDisplayName) + 1];
ptr->reserved = 1;
}
strcpy(ptr->returnPath, szDisplayName);
ptr->nbReturnPath = 1;
return true;
}
}
}
#endif
return false;
}
/*****************************************************************************/
/* OnCloseFileSelector : called when an effect closes a file selector */
/*****************************************************************************/
bool CSmpVSTHost::OnCloseFileSelector (int nEffect, VstFileSelect *ptr)
{
if (ptr->reserved == 1)
{
delete[] ptr->returnPath;
ptr->returnPath = 0;
ptr->reserved = 0;
}
return false;
}
/*****************************************************************************/
/* OnGetChunkFile : returns current .fxb / .fxp name */
/*****************************************************************************/
bool CSmpVSTHost::OnGetChunkFile(int nEffect, void * nativePath)
{
CSmpEffect *pEffect = (CSmpEffect *)GetAt(nEffect);
if (pEffect && pEffect->GetChunkFile().GetLength())
{
strcpy((char *)nativePath, pEffect->GetChunkFile());
return true;
}
return false;
}
/*****************************************************************************/
/* OnIdle : idle processing */
/*****************************************************************************/
long CSmpVSTHost::OnIdle
(
int nEffect /* effect ID that triggered this */
)
{
GetApp()->OnEffIdle(nEffect);
return 0;
}
/*===========================================================================*/
/* CSmpEffect class members */
/*===========================================================================*/
/*****************************************************************************/
/* CSmpEffect : constructor */
/*****************************************************************************/
CSmpEffect::CSmpEffect(CVSTHost *pHost)
: CEffect(pHost)
{
InitializeCriticalSection(&cs);
pFrameWnd = 0;
pEditWnd = pParmWnd = 0;
inBufs = outBufs = 0;
nAllocatedInbufs = nAllocatedOutbufs = 0;
wChnMask = 0xffff;
pPrev = pNext = NULL;
nLoadNum = -1;
}
/*****************************************************************************/
/* ~CSmpEffect : destructor */
/*****************************************************************************/
CSmpEffect::~CSmpEffect()
{
int i;
RemoveFromChain(); /* remove from chain (if any) */
EnterCritical(); /* inhibit others! */
if (inBufs)
{
#if 0
// these are set by VST Host; it would ruin performance
// if we copied data into the effect every time
for (i = 0; i < nAllocatedInbufs; i++)
if (inBufs[i])
delete[] inBufs[i];
#endif
delete[] inBufs;
}
if (outBufs)
{
// these are managed by the effect object, so we
// have to delete them here
for (i = nAllocatedOutbufs - 1; i >= 0; i--)
if ((outBufs[i]) &&
((!i) || (outBufs[i] != outBufs[0])))
delete[] outBufs[i];
delete[] outBufs;
}
LeaveCritical(); /* reallow others */
DeleteCriticalSection(&cs);
}
/*****************************************************************************/
/* Load : loads a plugin */
/*****************************************************************************/
bool CSmpEffect::Load(const char *name)
{
if (!CEffect::Load(name)) /* do basic stuff */
return false;
int i, j; /* loop counter */
if (pEffect->numInputs) /* allocate input pointers */
{
inBufs = new float *[pEffect->numInputs];
if (!inBufs)
return false;
for (i = 0; i < pEffect->numInputs; i++)
inBufs[i] = 0;
nAllocatedInbufs = pEffect->numInputs;
}
if (pEffect->numOutputs) /* allocate output pointers */
{
int nAlloc;
if (pEffect->numOutputs < 2)
nAlloc = 2;
else
nAlloc = pEffect->numOutputs;
outBufs = new float *[nAlloc];
if (!outBufs)
return false;
/* and the buffers pointed to */
for (i = 0; i < pEffect->numOutputs; i++)
{
// for this sample project, I've assumed a maximum buffer size
// of 1/4 second at 96KHz - presumably, this is MUCH too much
// and should be tweaked to more reasonable values, since it
// requires a machine with lots of main memory this way...
// user configurability would be nice, of course.
outBufs[i] = new float[24000];
if (!outBufs[i])
return false;
for (j = 0; j < 24000; j++)
outBufs[i][j] = 0.f;
}
for (; i < nAlloc; i++) /* if less than 2, fill up */
outBufs[i] = outBufs[0]; /* by replicating buffer 0 pointer */
nAllocatedOutbufs = nAlloc;
}
return true;
}
/*****************************************************************************/
/* Unload : removes an effect from memory */
/*****************************************************************************/
bool CSmpEffect::Unload()
{
bool bRC = false;
//__try
try
{
bRC = CEffect::Unload();
}
//__except (EvalException(GetExceptionCode()))
catch(...)
{
// no code here; isn't executed
}
return bRC;
}
/*****************************************************************************/
/* OnOpenWindow : called to open yet another window */
/*****************************************************************************/
void * CSmpEffect::OnOpenWindow(VstWindow* window)
{
CChildFrame *pFrame = GetFrameWnd();
if (pFrame)
{
CEffectWnd *pWnd;
pWnd = (CEffectWnd *)GetApp()->CreateChild(pEditWnd ?
RUNTIME_CLASS(CEffSecWnd) :
RUNTIME_CLASS(CEffectWnd),
IDR_EFFECTTYPE,
pFrame->GetEditMenu());
if (pWnd)
{
HWND hWnd = pWnd->GetSafeHwnd();
// ignored at the moment: style, position
ERect rc = {0};
rc.right = window->width;
rc.bottom = window->height;
pWnd->ShowWindow(SW_SHOWNORMAL);
pWnd->SetEffect(pFrame->GetEffect());
pWnd->SetMain(pFrame);
pWnd->SetEffSize(&rc);
pWnd->SetupTitleText(window->title);
if (!pEditWnd)
{
pFrame->SetEditWnd(pWnd);
SetEditWnd(pWnd);
// EffEditOpen(hWnd);
}
pWnd->SetupTitle();
window->winHandle = hWnd;
return pWnd->GetSafeHwnd();
}
}
return 0; /* no effect - no window. */
}