-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpluginobjects.cpp
2342 lines (1913 loc) · 66 KB
/
pluginobjects.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
#include "pluginconstants.h"
// This file contains the object implementations for the objects declared in
// "pluginconstants.h"
//
// Note about Helper Objects: DO NOT MODIFY THESE OBJECTS. If you need to do so,
// create a derived class and modify it. These objects may be updated from time to
// time so they need to be left alone.
// CEnvelopeDetector Implementation ----------------------------------------------------------------
//
CEnvelopeDetector::CEnvelopeDetector(void)
{
m_fAttackTime_mSec = 0.0;
m_fReleaseTime_mSec = 0.0;
m_fAttackTime = 0.0;
m_fReleaseTime = 0.0;
m_fSampleRate = 44100;
m_fEnvelope = 0.0;
m_uDetectMode = 0;
m_nSample = 0;
m_bAnalogTC = false;
m_bLogDetector = false;
}
CEnvelopeDetector::~CEnvelopeDetector(void)
{
}
void CEnvelopeDetector::prepareForPlay()
{
m_fEnvelope = 0.0;
m_nSample = 0;
}
void CEnvelopeDetector::init(float samplerate, float attack_in_ms, float release_in_ms, bool bAnalogTC, UINT uDetect, bool bLogDetector)
{
m_fEnvelope = 0.0;
m_fSampleRate = samplerate;
m_bAnalogTC = bAnalogTC;
m_fAttackTime_mSec = attack_in_ms;
m_fReleaseTime_mSec = release_in_ms;
m_uDetectMode = uDetect;
m_bLogDetector = bLogDetector;
// set themm_uDetectMode = uDetect;
setAttackTime(attack_in_ms);
setReleaseTime(release_in_ms);
}
void CEnvelopeDetector::setAttackTime(float attack_in_ms)
{
m_fAttackTime_mSec = attack_in_ms;
if(m_bAnalogTC)
m_fAttackTime = exp(ANALOG_TC/( attack_in_ms * m_fSampleRate * 0.001));
else
m_fAttackTime = exp(DIGITAL_TC/( attack_in_ms * m_fSampleRate * 0.001));
}
void CEnvelopeDetector::setReleaseTime(float release_in_ms)
{
m_fReleaseTime_mSec = release_in_ms;
if(m_bAnalogTC)
m_fReleaseTime = exp(ANALOG_TC/( release_in_ms * m_fSampleRate * 0.001));
else
m_fReleaseTime = exp(DIGITAL_TC/( release_in_ms * m_fSampleRate * 0.001));
}
void CEnvelopeDetector::setTCModeAnalog(bool bAnalogTC)
{
m_bAnalogTC = bAnalogTC;
setAttackTime(m_fAttackTime_mSec);
setReleaseTime(m_fReleaseTime_mSec);
}
/*
SEE:
http://c4dm.eecs.qmul.ac.uk/audioengineering/compressors/documents/report.pdf
for RMS Detector, the sqrt operation is AFTER the detector
input -> x^2 -> RC Detector -> sqrt(out) -> out
*/
float CEnvelopeDetector::detect(float fInput)
{
switch(m_uDetectMode)
{
case 0:
fInput = fabs(fInput);
break;
case 1:
case 2: // --- both MS and RMS require squaring the input
fInput = fabs(fInput) * fabs(fInput);
break;
default:
fInput = (float)fabs(fInput);
break;
}
float fCurrEnvelope = 0.0;
if(fInput> m_fEnvelope)
fCurrEnvelope = m_fAttackTime * (m_fEnvelope - fInput) + fInput;
else
fCurrEnvelope = m_fReleaseTime * (m_fEnvelope - fInput) + fInput;
if(fCurrEnvelope > 0.0 && fCurrEnvelope < FLT_MIN_PLUS) fCurrEnvelope = 0;
if(fCurrEnvelope < 0.0 && fCurrEnvelope > FLT_MIN_MINUS) fCurrEnvelope = 0;
// --- bound them; can happen when using pre-detector gains of more than 1.0
fCurrEnvelope = fmin(fCurrEnvelope, (float)1.0);
fCurrEnvelope = fmax(fCurrEnvelope, (float)0.0);
// --- store envelope prior to sqrt for RMS version
m_fEnvelope = fCurrEnvelope;
// --- if RMS, do the SQRT
if(m_uDetectMode == 2)
fCurrEnvelope = pow(fCurrEnvelope, (float)0.5);
// --- 16-bit scaling!
if(m_bLogDetector)
{
if(fCurrEnvelope <= 0)
return -96.0; // 16 bit noise floor
return 20*log10(fCurrEnvelope);
}
return fCurrEnvelope;
}
// CWaveTable Implementation ----------------------------------------------------------------
//
CWaveTable::CWaveTable()
{
// non inveting unless user sets it
m_bInvert = false;
// slope and y-intercept values for the
// Triangle Wave
// rising edge1:
float mt1 = 1.0/256.0;
float bt1 = 0.0;
// rising edge2:
float mt2 = 1.0/256.0;
float bt2 = -1.0;
// falling edge:
float mtf2 = -2.0/512.0;
float btf2 = +1.0;
// Sawtooth
// rising edge1:
float ms1 = 1.0/512.0;
float bs1 = 0.0;
// rising edge2:
float ms2 = 1.0/512.0;
float bs2 = -1.0;
float fMaxTri = 0;
float fMaxSaw = 0;
float fMaxSqr = 0;
// setup arrays
for(int i = 0; i < 1024; i++)
{
// sample the sinusoid, 1024 points
// sin(wnT) = sin(2pi*i/1024)
m_SinArray[i] = sin( ((float)i/1024.0)*(2*pi) );
// saw, triangle and square are just logic mechanisms
// can you figure them out?
// Sawtooth
m_SawtoothArray[i] = i < 512 ? ms1*i + bs1 : ms2*(i-511) + bs2;
// Triangle
if(i < 256)
m_TriangleArray[i] = mt1*i + bt1; // mx + b; rising edge 1
else if (i >= 256 && i < 768)
m_TriangleArray[i] = mtf2*(i-256) + btf2; // mx + b; falling edge
else
m_TriangleArray[i] = mt2*(i-768) + bt2; // mx + b; rising edge 2
// square
m_SquareArray[i] = i < 512 ? +1.0 : -1.0;
// zero to start, then loops build the rest
m_SawtoothArray_BL5[i] = 0.0;
m_SquareArray_BL5[i] = 0.0;
m_TriangleArray_BL5[i] = 0.0;
// sawtooth: += (-1)^g+1(1/g)sin(wnT)
for(int g=1; g<=6; g++)
{
double n = double(g);
m_SawtoothArray_BL5[i] += pow((float)-1.0,(float)(g+1))*(1.0/n)*sin(2.0*pi*i*n/1024.0);
// down saw m_SawtoothArray_BL5[i] += (1.0/n)*sin(2.0*pi*i*n/1024.0);
}
// triangle: += (-1)^g(1/(2g+1+^2)sin(w(2n+1)T)
for(int g=0; g<=3; g++)
{
double n = double(g);
m_TriangleArray_BL5[i] += pow((float)-1.0, (float)n)*(1.0/pow((float)(2*n + 1), (float)2.0))*sin(2.0*pi*(2.0*n + 1)*i/1024.0);
}
// square: += (1/g)sin(wnT)
for(int g=1; g<8; g+=2)
{
double n = double(g);
m_SquareArray_BL5[i] += (1.0/n)*sin(2.0*pi*i*n/1024.0);
}
// store the max values
if(i == 0)
{
fMaxSaw = m_SawtoothArray_BL5[i];
fMaxTri = m_TriangleArray_BL5[i];
fMaxSqr = m_SquareArray_BL5[i];
}
else
{
// test and store
if(m_SawtoothArray_BL5[i] > fMaxSaw)
fMaxSaw = m_SawtoothArray_BL5[i];
if(m_TriangleArray_BL5[i] > fMaxTri)
fMaxTri = m_TriangleArray_BL5[i];
if(m_SquareArray_BL5[i] > fMaxSqr)
fMaxSqr = m_SquareArray_BL5[i];
}
}
// normalize the bandlimited tables
for(int i = 0; i < 1024; i++)
{
// normalize it
m_SawtoothArray_BL5[i] /= fMaxSaw;
m_TriangleArray_BL5[i] /= fMaxTri;
m_SquareArray_BL5[i] /= fMaxSqr;
}
// clear variables
m_fReadIndex = 0.0;
m_fQuadPhaseReadIndex = 0.0;
m_f_inc = 0.0;
// initialize inc
reset();
m_fFrequency_Hz = 440;
m_uOscType = 0;
m_uTableMode = 0;
m_uPolarity = 0;
cookFrequency();
}
/* destructor()
Destroy variables allocated in the contructor()
*/
CWaveTable::~CWaveTable(void)
{
}
/* prepareForPlay()
Called by the client after Play() is initiated but before audio streams
NOTE: if you allocte memory in this function, destroy it in ::destructor() above
*/
bool CWaveTable::prepareForPlay()
{
// reset the index
reset();
// cook curent frequency
cookFrequency();
return true;
}
// The Oscillate Function!
void CWaveTable::doOscillate(float* pYn, float* pYqn)
{
// our output value for this cycle
float fOutSample = 0;
float fQuadPhaseOutSample = 0;
// get INT part
int nReadIndex = (int)m_fReadIndex;
int nQuadPhaseReadIndex = (int)m_fQuadPhaseReadIndex;
// get FRAC part
float fFrac = m_fReadIndex - nReadIndex;
float fQuadFrac = m_fQuadPhaseReadIndex - nQuadPhaseReadIndex;
// setup second index for interpolation; wrap the buffer if needed
int nReadIndexNext = nReadIndex + 1 > 1023 ? 0 : nReadIndex + 1;
int nQuadPhaseReadIndexNext = nQuadPhaseReadIndex + 1 > 1023 ? 0 : nQuadPhaseReadIndex + 1;
// interpolate the output
switch(m_uOscType)
{
case sine:
fOutSample = dLinTerp(0, 1, m_SinArray[nReadIndex], m_SinArray[nReadIndexNext], fFrac);
fQuadPhaseOutSample = dLinTerp(0, 1, m_SinArray[nQuadPhaseReadIndex], m_SinArray[nQuadPhaseReadIndexNext], fQuadFrac);
break;
case saw:
if(m_uTableMode == normal) // normal
{
fOutSample = dLinTerp(0, 1, m_SawtoothArray[nReadIndex], m_SawtoothArray[nReadIndexNext], fFrac);
fQuadPhaseOutSample = dLinTerp(0, 1, m_SawtoothArray[nQuadPhaseReadIndex], m_SawtoothArray[nQuadPhaseReadIndexNext], fQuadFrac);
}
else // bandlimited
{
fOutSample = dLinTerp(0, 1, m_SawtoothArray_BL5[nReadIndex], m_SawtoothArray_BL5[nReadIndexNext], fFrac);
fQuadPhaseOutSample = dLinTerp(0, 1, m_SawtoothArray_BL5[nQuadPhaseReadIndex], m_SawtoothArray_BL5[nQuadPhaseReadIndexNext], fQuadFrac);
}
break;
case tri:
if(m_uTableMode == normal) // normal
{
fOutSample = dLinTerp(0, 1, m_TriangleArray[nReadIndex], m_TriangleArray[nReadIndexNext], fFrac);
fQuadPhaseOutSample = dLinTerp(0, 1, m_TriangleArray[nQuadPhaseReadIndex], m_TriangleArray[nQuadPhaseReadIndexNext], fQuadFrac);
}
else // bandlimited
{
fOutSample = dLinTerp(0, 1, m_TriangleArray_BL5[nReadIndex], m_TriangleArray_BL5[nReadIndexNext], fFrac);
fQuadPhaseOutSample = dLinTerp(0, 1, m_TriangleArray_BL5[nQuadPhaseReadIndex], m_TriangleArray_BL5[nQuadPhaseReadIndexNext], fQuadFrac);
}
break;
case square:
if(m_uTableMode == normal) // normal
{
fOutSample = dLinTerp(0, 1, m_SquareArray[nReadIndex], m_SquareArray[nReadIndexNext], fFrac);
fQuadPhaseOutSample = dLinTerp(0, 1, m_SquareArray[nQuadPhaseReadIndex], m_SquareArray[nQuadPhaseReadIndexNext], fQuadFrac);
}
else // bandlimited
{
fOutSample = dLinTerp(0, 1, m_SquareArray_BL5[nReadIndex], m_SquareArray_BL5[nReadIndexNext], fFrac);
fQuadPhaseOutSample = dLinTerp(0, 1, m_SquareArray_BL5[nQuadPhaseReadIndex], m_SquareArray_BL5[nQuadPhaseReadIndexNext], fQuadFrac);
}
break;
// always need a default
default:
fOutSample = dLinTerp(0, 1, m_SinArray[nReadIndex], m_SinArray[nReadIndexNext], fFrac);
fQuadPhaseOutSample = dLinTerp(0, 1, m_SinArray[nQuadPhaseReadIndex], m_SinArray[nQuadPhaseReadIndexNext], fQuadFrac);
break;
}
// add the increment for next time
m_fReadIndex += m_f_inc;
m_fQuadPhaseReadIndex += m_f_inc;
// check for wrap
if(m_fReadIndex >= 1024)
m_fReadIndex = m_fReadIndex - 1024;
if(m_fQuadPhaseReadIndex >= 1024)
m_fQuadPhaseReadIndex = m_fQuadPhaseReadIndex - 1024;
// write out
*pYn = fOutSample;
*pYqn = fQuadPhaseOutSample;
if(m_bInvert)
{
*pYn *= -1.0;
*pYqn *= -1.0;
}
if(m_uPolarity == unipolar)
{
*pYn /= 2.0;
*pYn += 0.5;
*pYqn /= 2.0;
*pYqn += 0.5;
}
}
// CBiQuad Implementation ----------------------------------------------------------------
//
CBiQuad::CBiQuad(void)
{
}
CBiQuad::~CBiQuad(void)
{
}
// CJoystickProgram Implementation ----------------------------------------------------------------
//
CJoystickProgram::CJoystickProgram(float* pJSProgramTable, UINT uMode)
{
m_bRunning = false;
m_uSampleCount = 0;
m_pJSProgramTable = pJSProgramTable;
m_fTimerDurationMSec = 0;
m_nCurrentProgramStep = 0;
m_fA_Mix = 0.25;
m_fB_Mix = 0.25;
m_fC_Mix = 0.25;
m_fD_Mix = 0.25;
m_fAC_Mix = 0.0;
m_fBD_Mix = 0.0;
m_uJSMode = uMode;
m_bDirInc = true;
}
void CJoystickProgram::setJSMode(UINT uMode)
{
m_uJSMode = uMode;
m_bDirInc = true;
}
void CJoystickProgram::setSampleRate(int nSampleRate)
{
m_fSampleRate = (float)nSampleRate;
}
CJoystickProgram::~CJoystickProgram(void)
{
}
void CJoystickProgram::incTimer()
{
if(!m_bRunning)
return;
m_uSampleCount++;
calculateCurrentVectorMix();
if(m_uSampleCount > (UINT)m_nTimerDurationSamples)
{
// goto the next step
if(m_bDirInc)
m_nCurrentProgramStep++;
else
m_nCurrentProgramStep--;
if(m_nCurrentProgramStep > m_nNumSteps || m_nCurrentProgramStep < 0)
{
if(m_uJSMode == JS_ONESHOT)
{
reset();
return;
}
else if(m_uJSMode == JS_LOOP)
{
m_nCurrentProgramStep = 0;
}
else if(m_uJSMode == JS_LOOP_BACKANDFORTH)
{
m_bDirInc = !m_bDirInc;
if(m_bDirInc)
m_nCurrentProgramStep +=2;
else
m_nCurrentProgramStep -= 2;
}
else if(m_uJSMode == JS_SUSTAIN)
{
m_bRunning = false;
return; // just return
}
}
else if(m_uJSMode == JS_SUSTAIN && m_nCurrentProgramStep == m_nNumSteps)
{
pauseProgram(); // until user restarts with a note-off event
}
// setup for next step
m_uSampleCount = 0;
m_fStartA_Mix = m_fEndA_Mix;
m_fStartB_Mix = m_fEndB_Mix;
m_fStartC_Mix = m_fEndC_Mix;
m_fStartD_Mix = m_fEndD_Mix;
m_fStartAC_Mix = m_fEndAC_Mix;
m_fStartBD_Mix = m_fEndBD_Mix;
m_fEndA_Mix = m_pJSProgramTable[JS_PROG_INDEX(m_nCurrentProgramStep,0)];
m_fEndB_Mix = m_pJSProgramTable[JS_PROG_INDEX(m_nCurrentProgramStep,1)];
m_fEndC_Mix = m_pJSProgramTable[JS_PROG_INDEX(m_nCurrentProgramStep,2)];
m_fEndD_Mix = m_pJSProgramTable[JS_PROG_INDEX(m_nCurrentProgramStep,3)];
m_fEndAC_Mix = m_pJSProgramTable[JS_PROG_INDEX(m_nCurrentProgramStep,5)];
m_fEndBD_Mix = m_pJSProgramTable[JS_PROG_INDEX(m_nCurrentProgramStep,6)];
if(m_bDirInc)
m_fTimerDurationMSec = m_nCurrentProgramStep > 0 ? m_pJSProgramTable[JS_PROG_INDEX(m_nCurrentProgramStep-1,4)] : m_pJSProgramTable[JS_PROG_INDEX(m_nNumSteps,4)];
else
m_fTimerDurationMSec = m_pJSProgramTable[JS_PROG_INDEX(m_nCurrentProgramStep,4)];
m_nTimerDurationSamples = (int)((m_fTimerDurationMSec/1000.0)*(m_fSampleRate));
}
}
void CJoystickProgram::calculateCurrentVectorMix()
{
float m = (m_fEndA_Mix - m_fStartA_Mix)/(float)m_nTimerDurationSamples;
m_fA_Mix = m*(float)m_uSampleCount + m_fStartA_Mix;
m = (m_fEndB_Mix - m_fStartB_Mix)/(float)m_nTimerDurationSamples;
m_fB_Mix = m*(float)m_uSampleCount + m_fStartB_Mix;
m = (m_fEndC_Mix - m_fStartC_Mix)/(float)m_nTimerDurationSamples;
m_fC_Mix = m*(float)m_uSampleCount + m_fStartC_Mix;
m = (m_fEndD_Mix - m_fStartD_Mix)/(float)m_nTimerDurationSamples;
m_fD_Mix = m*(float)m_uSampleCount + m_fStartD_Mix;
m = (m_fEndAC_Mix - m_fStartAC_Mix)/(float)m_nTimerDurationSamples;
m_fAC_Mix = m*(float)m_uSampleCount + m_fStartAC_Mix;
m = (m_fEndBD_Mix - m_fStartBD_Mix)/(float)m_nTimerDurationSamples;
m_fBD_Mix = m*(float)m_uSampleCount + m_fStartBD_Mix;
}
void CJoystickProgram::startProgram()
{
m_nNumSteps = 0;
for(int i=1; i<MAX_JS_PROGRAM_STEPS; i++)
{
if(m_pJSProgramTable[JS_PROG_INDEX(i,4)] > 0)
m_nNumSteps++;
}
if(m_nNumSteps == 0)
return;
m_bDirInc = true;
m_nCurrentProgramStep = 1;
m_fStartA_Mix = m_pJSProgramTable[JS_PROG_INDEX(0,0)];
m_fStartB_Mix = m_pJSProgramTable[JS_PROG_INDEX(0,1)];
m_fStartC_Mix = m_pJSProgramTable[JS_PROG_INDEX(0,2)];
m_fStartD_Mix = m_pJSProgramTable[JS_PROG_INDEX(0,3)];
m_fStartAC_Mix = m_pJSProgramTable[JS_PROG_INDEX(0,5)];
m_fStartBD_Mix = m_pJSProgramTable[JS_PROG_INDEX(0,6)];
m_fEndA_Mix = m_pJSProgramTable[JS_PROG_INDEX(1,0)];
m_fEndB_Mix = m_pJSProgramTable[JS_PROG_INDEX(1,1)];
m_fEndC_Mix = m_pJSProgramTable[JS_PROG_INDEX(1,2)];
m_fEndD_Mix = m_pJSProgramTable[JS_PROG_INDEX(1,3)];
m_fEndAC_Mix = m_pJSProgramTable[JS_PROG_INDEX(1,5)];
m_fEndBD_Mix = m_pJSProgramTable[JS_PROG_INDEX(1,6)];
m_fTimerDurationMSec = m_pJSProgramTable[JS_PROG_INDEX(0,4)];
m_nTimerDurationSamples = (int)((m_fTimerDurationMSec/1000.0)*(m_fSampleRate));
m_bRunning = true;
}
// CWaveData Implementation ----------------------------------------------------------------
//
#if defined _WINDOWS || defined _WINDLL
typedef struct {
UCHAR IdentifierString[4];
DWORD dwLength;
} RIFF_CHUNK, *PRIFF_CHUNK;
typedef struct {
WORD wFormatTag; // Format category
WORD wChannels; // Number of channels
DWORD dwSamplesPerSec; // Sampling rate
DWORD dwAvgBytesPerSec; // For buffer estimation
WORD wBlockAlign; // Data block size
WORD wBitsPerSample;
} WAVE_FILE_HEADER, *PWAVE_FILE_HEADER;
typedef struct _wave_sample {
WAVEFORMATEX WaveFormatEx;
char *pSampleData;
char *pFXSampleData;
UINT Index;
UINT FXIndex;
UINT Size;
DWORD dwId;
DWORD bPlaying;
struct _wave_sample *pNext;
} WAVE_SAMPLE, *PWAVE_SAMPLE;
// union for data conversion
union UWaveData
{
float f;
double d;
int n;
unsigned int u;
unsigned long long u64;
};
// CWaveData
CWaveData::CWaveData(char* pFilePath)
{
m_bWaveLoaded = false;
m_pWaveBuffer = NULL;
m_uLoopCount = 0;
m_uLoopStartIndex = 0;
m_uLoopEndIndex = 0;
m_uLoopType = 0;
m_uMIDINote = 0;
m_uMIDIPitchFraction = 0;
m_uSMPTEFormat = 0;
m_uSMPTEOffset = 0;
if(pFilePath)
m_bWaveLoaded = readWaveFile(pFilePath);
}
CWaveData::~CWaveData()
{
if(m_pWaveBuffer)
delete [] m_pWaveBuffer;
}
// prompts with file open dialog, returns TRUE if successfuly
// opened and parsed the file into the member m_pWaveBuffer
bool CWaveData::initWithUserWAVFile(char* pInitDir)
{
OPENFILENAME ofn; // common dialog box structure
char szFile[260]; // buffer for file name
// open a file name
ZeroMemory( &ofn , sizeof( ofn));
ofn.lStructSize = sizeof ( ofn );
ofn.hwndOwner = NULL ;
ofn.lpstrFile = szFile ;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "Wave\0*.WAV\0";
ofn.nFilterIndex =1;
ofn.lpstrFileTitle = NULL ;
ofn.nMaxFileTitle = 0 ;
ofn.lpstrInitialDir=pInitDir ; // you can default to a directory here
ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST ;
GetOpenFileName( &ofn );
if(!ofn.lpstrFile)
return false;
m_bWaveLoaded = readWaveFile(ofn.lpstrFile);
//// Now simply display the file name
// MessageBox ( NULL , ofn.lpstrFile , "File Name" , MB_OK);
return m_bWaveLoaded;
}
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
// THE FOLLOWING TYPES ARE SUPPORTED:
//
// WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE
//
// 16-BIT Signed Integer PCM
// 24-BIT Signed Integer PCM 3-ByteAlign
// 24-BIT Signed Integer PCM 4-ByteAlign
// 32-BIT Signed Integer PCM
// 32-BIT Floating Point
// 64-BIT Floating Point
bool CWaveData::readWaveFile(char* pFilePath)
{
bool bSampleLoaded = false;
bool bFailed = false;
RIFF_CHUNK RiffChunk = {{0}};
WAVE_FILE_HEADER WaveFileHeader;
DWORD dwIncrementBytes;
WAVE_SAMPLE WaveSample;
m_uNumChannels = 0;
m_uSampleRate = 0;
m_uSampleCount = 0;
// --- create filestream from open binary file
ifstream inFile;
inFile.open(pFilePath, std::ifstream::binary);
// --- OK?
bool bIsOpen = inFile.is_open();
if(!bIsOpen)
return false;
// --- this is used to identify each chunk
char szIdentifier[5] = {0};
// --- advance 12 chars
inFile.seekg(12);
// --- read first RiffChunk and the WaveFileHeader
inFile.read((char*)(&RiffChunk), sizeof(RiffChunk));
inFile.read((char*)(&WaveFileHeader), sizeof(WaveFileHeader));
// --- set the waveformatex
WaveSample.WaveFormatEx.wFormatTag = WaveFileHeader.wFormatTag;
WaveSample.WaveFormatEx.nChannels = WaveFileHeader.wChannels;
WaveSample.WaveFormatEx.nSamplesPerSec = WaveFileHeader.dwSamplesPerSec;
WaveSample.WaveFormatEx.nAvgBytesPerSec = WaveFileHeader.dwAvgBytesPerSec;
WaveSample.WaveFormatEx.nBlockAlign = WaveFileHeader.wBlockAlign;
WaveSample.WaveFormatEx.wBitsPerSample = WaveFileHeader.wBitsPerSample;
WaveSample.WaveFormatEx.cbSize = 0;
// --- I don't support these types (compressed, uLaw/aLaw, etc..)
if(WaveSample.WaveFormatEx.wFormatTag != 1 && WaveSample.WaveFormatEx.wFormatTag != 3)
{
inFile.close();
return false;
}
// --- for backing up the first seek
dwIncrementBytes = sizeof(WaveFileHeader);
do {
// RiffChunk.dwLength - dwIncrementBytes sets the file pointer to position 0 first time through
// advance by RiffChunk.dwLength - dwIncrementBytes
int position = inFile.tellg();
inFile.seekg(position + (RiffChunk.dwLength - dwIncrementBytes));
// --- advanced past end of file?
bFailed = inFile.fail();
if(!bFailed)
{
// --- read the RiffChunk
inFile.read((char*)(&RiffChunk), sizeof(RiffChunk));
// --- this now makes the seekg() advance in RiffChunk.dwLength chunks
// which vary with the type of chunk
dwIncrementBytes = 0;
// --- extract the chunk identifier
memcpy(szIdentifier, RiffChunk.IdentifierString, 4);
}
} while(strcmp(szIdentifier, "data") && !bFailed);
// --- AUDIO DATA CHUNK data
// --- 16 bit
if(!bFailed && WaveSample.WaveFormatEx.wBitsPerSample == 16)
{
WaveSample.pSampleData = (char *)LocalAlloc(LMEM_ZEROINIT, RiffChunk.dwLength);
WaveSample.Size = RiffChunk.dwLength;
inFile.read(WaveSample.pSampleData, RiffChunk.dwLength);
UINT nSampleCount = (float)RiffChunk.dwLength/(float)(WaveSample.WaveFormatEx.wBitsPerSample/8.0);
m_uSampleCount = nSampleCount;
m_uNumChannels = WaveSample.WaveFormatEx.nChannels;
m_uSampleRate = WaveSample.WaveFormatEx.nSamplesPerSec;
if(m_pWaveBuffer)
delete [] m_pWaveBuffer;
m_pWaveBuffer = new float[nSampleCount];
short* pShorts = new short[nSampleCount];
memset(pShorts, 0, nSampleCount*sizeof(short));
int m = 0;
for(UINT i=0; i<nSampleCount; i++)
{
// MSB
pShorts[i] = (unsigned char)WaveSample.pSampleData[m+1];
pShorts[i] <<= 8;
// LSB
short lsb = (unsigned char)WaveSample.pSampleData[m];
// in case top of lsb is bad
lsb &= 0x00FF;
pShorts[i] |= lsb;
m+=2;
}
// convet to float -1.0 -> +1.0
for(UINT i = 0; i < nSampleCount; i++)
{
m_pWaveBuffer[i] = ((float)pShorts[i])/32768.0;
}
delete [] pShorts;
LocalFree(WaveSample.pSampleData);
bSampleLoaded = true;
}
// --- 24 bits
else if(!bFailed && WaveSample.WaveFormatEx.wBitsPerSample == 24)
{
WaveSample.pSampleData = (char *)LocalAlloc(LMEM_ZEROINIT, RiffChunk.dwLength);
WaveSample.Size = RiffChunk.dwLength;
inFile.read(WaveSample.pSampleData, RiffChunk.dwLength);
UINT nSampleCount = (float)RiffChunk.dwLength/(float)(WaveSample.WaveFormatEx.wBitsPerSample/8.0);
m_uSampleCount = nSampleCount;
m_uNumChannels = WaveSample.WaveFormatEx.nChannels;
m_uSampleRate = WaveSample.WaveFormatEx.nSamplesPerSec;
if(m_pWaveBuffer)
delete [] m_pWaveBuffer;
// our buffer gets created
m_pWaveBuffer = new float[nSampleCount];
int* pSignedInts = new int[nSampleCount];
memset(pSignedInts, 0, nSampleCount*sizeof(long));
int m = 0;
int mask = 0x000000FF;
// 24-bits in 3-byte packs
if(WaveSample.WaveFormatEx.nBlockAlign/WaveSample.WaveFormatEx.nChannels == 3)
{
for(UINT i=0; i<nSampleCount; i++)
{
// MSB
pSignedInts[i] = (unsigned char)WaveSample.pSampleData[m+2];
pSignedInts[i] <<= 24;
// NSB
int nsb = (int)WaveSample.pSampleData[m+1];
// in case top of nsb is bad
nsb &= mask;
nsb <<= 16;
pSignedInts[i] |= nsb;
// LSB
int lsb = (int)WaveSample.pSampleData[m];
// in case top of lsb is bad
lsb &= mask;
lsb <<= 8;
pSignedInts[i] |= lsb;
m+=3;
}
}
// --- 24-bits in 4-byte packs
if(WaveSample.WaveFormatEx.nBlockAlign/WaveSample.WaveFormatEx.nChannels == 4)
{
for(UINT i=0; i<nSampleCount; i++)
{
// MSB
pSignedInts[i] = (unsigned char)WaveSample.pSampleData[m+3];
pSignedInts[i] <<= 24;
// NSB
int nsb = (int)WaveSample.pSampleData[m+2];
// in case top of nsb is bad
nsb &= mask;
nsb <<= 16;
pSignedInts[i] |= nsb;
// NSB2
int nsb2 = (int)WaveSample.pSampleData[m+1];
// in case top of nsb is bad
nsb2 &= mask;
nsb2 <<= 8;
pSignedInts[i] |= nsb2;
// LSB
int lsb = (int)WaveSample.pSampleData[m];
// in case top of lsb is bad
lsb &= mask;
pSignedInts[i] |= lsb;
m+=4;
}
}
// convet to float -1.0 -> +1.0
for(UINT i = 0; i < nSampleCount; i++)
{
m_pWaveBuffer[i] = ((float)pSignedInts[i])/2147483648.0; // 2147483648.0 = 1/2 of 2^32
}
delete [] pSignedInts;
LocalFree(WaveSample.pSampleData);
bSampleLoaded = true;
}
// --- 32 bits
else if(!bFailed && WaveSample.WaveFormatEx.wBitsPerSample == 32)
{
WaveSample.pSampleData = (char *)LocalAlloc(LMEM_ZEROINIT, RiffChunk.dwLength);
WaveSample.Size = RiffChunk.dwLength;
inFile.read(WaveSample.pSampleData, RiffChunk.dwLength);
UINT nSampleCount = (float)RiffChunk.dwLength/(float)(WaveSample.WaveFormatEx.wBitsPerSample/8.0);
m_uSampleCount = nSampleCount;
m_uNumChannels = WaveSample.WaveFormatEx.nChannels;
m_uSampleRate = WaveSample.WaveFormatEx.nSamplesPerSec;
if(m_pWaveBuffer)
delete [] m_pWaveBuffer;
// our buffer gets created
m_pWaveBuffer = new float[nSampleCount];
if(WaveSample.WaveFormatEx.wFormatTag == 1)
{
int* pSignedInts = new int[nSampleCount];
memset(pSignedInts, 0, nSampleCount*sizeof(int));
int m = 0;
int mask = 0x000000FF;
for(UINT i=0; i<nSampleCount; i++)
{
// MSB
pSignedInts[i] = (unsigned char)WaveSample.pSampleData[m+3];
pSignedInts[i] <<= 24;
// NSB
int nsb = (int)WaveSample.pSampleData[m+2];
// in case top of nsb is bad
nsb &= mask;
nsb <<= 16;
pSignedInts[i] |= nsb;
// NSB2
int nsb2 = (int)WaveSample.pSampleData[m+1];
// in case top of nsb is bad
nsb2 &= mask;
nsb2 <<= 8;
pSignedInts[i] |= nsb2;
// LSB
int lsb = (int)WaveSample.pSampleData[m];
// in case top of lsb is bad
lsb &= mask;
pSignedInts[i] |= lsb;
m+=4;
}
// convet to float -1.0 -> +1.0
for(UINT i = 0; i < nSampleCount; i++)
{
m_pWaveBuffer[i] = ((float)pSignedInts[i])/2147483648.0; // 2147483648.0 = 1/2 of 2^32
}
delete [] pSignedInts;
// free(WaveSample.pSampleData);
bSampleLoaded = true;
}
else if(WaveSample.WaveFormatEx.wFormatTag == 3) // float
{
unsigned int* pUSignedInts = new unsigned int[nSampleCount];
memset(pUSignedInts, 0, nSampleCount*sizeof(unsigned int));
int m = 0;
int mask = 0x000000FF;
for(UINT i=0; i<nSampleCount; i++)
{
// MSB
pUSignedInts[i] = (unsigned char)WaveSample.pSampleData[m+3];
pUSignedInts[i] <<= 24;
// NSB
int nsb = (unsigned int)WaveSample.pSampleData[m+2];
// in case top of nsb is bad
nsb &= mask;
nsb <<= 16;
pUSignedInts[i] |= nsb;