-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBirlPhysicalModel.cpp
More file actions
1083 lines (916 loc) · 36 KB
/
BirlPhysicalModel.cpp
File metadata and controls
1083 lines (916 loc) · 36 KB
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
//-----------------------------------------------------------------------------
// Entaro ChucK Developer!
// This is a Chugin boilerplate, generated by chugerate!
//-----------------------------------------------------------------------------
// this should align with the correct versions of these ChucK files
#include "chuck_dl.h"
#include "chuck_def.h"
// general includes
#include <stdio.h>
#include <limits.h>
// #include "ReedTable.h"
#include "OneZero.h"
// #include "PoleZero.h"
// #include "Noise.h"
#include "Birl.h"
#include "Birl_Filters.h"
#include "Birl_Tuning.h"
#include "Birl_Tube.h"
// declaration of chugin constructor
CK_DLL_CTOR(birlphysicalmodel_ctor);
// declaration of chugin desctructor
CK_DLL_DTOR(birlphysicalmodel_dtor);
CK_DLL_MFUN(birlphysicalmodel_setBreathPressure);
CK_DLL_MFUN(birlphysicalmodel_setLength);
CK_DLL_MFUN(birlphysicalmodel_setToneHole);
CK_DLL_MFUN(birlphysicalmodel_setToneHoleRadius);
CK_DLL_MFUN(birlphysicalmodel_setFundamental);
CK_DLL_MFUN(birlphysicalmodel_tweakBoreRadius);
CK_DLL_MFUN(birlphysicalmodel_tweakToneHoleRadius);
CK_DLL_MFUN(birlphysicalmodel_lpCutoff);
CK_DLL_MFUN(birlphysicalmodel_lpQ);
CK_DLL_MFUN(birlphysicalmodel_pfCutoff);
CK_DLL_MFUN(birlphysicalmodel_pfQ);
CK_DLL_MFUN(birlphysicalmodel_noiseBPCutoff);
CK_DLL_MFUN(birlphysicalmodel_noiseBPQ);
CK_DLL_MFUN(birlphysicalmodel_noiseGain);
CK_DLL_MFUN(birlphysicalmodel_shaper);
CK_DLL_MFUN(birlphysicalmodel_shaperMix);
CK_DLL_MFUN(birlphysicalmodel_setTuning);
CK_DLL_MFUN(birlphysicalmodel_setCustomTuning);
//CK_DLL_MFUN(birlphysicalmodel_setNumToneHoles);
// for Chugins extending UGen, this is mono synthesis function for 1 sample
CK_DLL_TICK(birlphysicalmodel_tick);
// this is a special offset reserved for Chugin internal data
t_CKINT birlphysicalmodel_data_offset = 0;
class BirlPhysicalModel
{
public:
void debug(std::string msg, double in) {
if (count % 44100 == 0) {
printf(msg.c_str(), in);
count = 0;
}
}
void debug(std::string msg, int in) {
if (count % 44100 == 0) {
printf(msg.c_str(), in);
count = 0;
}
}
void debug(std::string msg, int in0, double in1) {
if (count % 44100 == 0) {
printf(msg.c_str(), in0, in1);
count = 0;
}
}
void debug(std::string msg, double in0, double in1) {
if (count % 44100 == 0) {
printf(msg.c_str(), in0, in1);
count = 0;
}
}
void debug(std::string msg, double in0, int in1) {
if (count % 44100 == 0) {
printf(msg.c_str(), in0, in1);
count = 0;
}
}
void debug(std::string msg) {
if (count % 44100 == 0) {
printf(msg.c_str());
count = 0;
}
}
void debug(std::string msg, double in0, double in1, double in2, double in3) {
if (count % 44100 == 0) {
printf(msg.c_str(), in0, in1, in2, in3);
count = 0;
}
}
void checkClip(std::string msg, double input) {
if (input >= 1 || input <= -1) {
printf(msg.c_str());
printf(" is out of bounds!\n");
}
}
void tweakRb(int mult) {
if (originalrb_ * (mult * RB_TWEAK_FACTOR) < 0) {
return;
}
rb_ = originalrb_ + (mult * RB_TWEAK_FACTOR);
printf("new rb: %f\n", rb_);
calcTHCoeffs();
}
void tweakRth(int mult) {
int index = toneHoleIndex_;
if (originalRth_[index] * (mult * RTH_TWEAK_FACTOR) < 0) {
return;
}
rth_[index] = originalRth_[index] + (mult * RTH_TWEAK_FACTOR);
printf("new rth_[%d]: %f\n", toneHoleIndex_, rth_[index]);
calcTHCoeffs();
}
// void tune(double Fc) {
// // for dummy?
// // Fc = (tuning[NUM_NOTES-1]/tuning[NUM_NOTES]) * Fc;
// // printf("Fc: %f\n", Fc);
// double LS = calcLS(Fc);
// int LC = calcLC(LS);
// double d1 = calcd1(LC, LS);
// printf("LC: %d\n", LC);
// printf("d1: %f\n", d1);
// int prevlL = 0;
// int correction = 0;
// // Must handle dummy separately.
// for (int i = 0; i < numTubes_ - 1; i++) {
// printf("tubelength %d: %d\n", i, calclL(d1, i, LS) - prevlL);
// tubeLengths_[i] = calclL(d1, i, LS) - prevlL;
// if (i == 0) {
// tubeLengths_[i] -= correction;
// }
// if (tubeLengths_[i] == 0) {
// printf("ERROR: Integer delay line lengths clash!!!!! Use a different tuning or try oversampling.\n");
// return;
// }
// // if (tubes_[i] != NULL) {
// // printf("WANNA BE FREEEEEEE\n");
// // freeTube(tubes_[i]);
// // }
// if (i == 0) {
// tubes_[i] = initTube(tubeLengths_[i]);
// prevlL += tubeLengths_[i] + correction;
// } else {
// tubes_[i] = initTube(tubeLengths_[i]);
// prevlL += tubeLengths_[i];
// }
// printf("th %d: lL = %d\n", i, tubeLengths_[i]);
// }
// // Dummy
// // tubeLengths_[numTubes_-1] = (int) ((1.0/tuning[numTubes_-1]) * LS) - prevlL;
// // if (tubeLengths_[numTubes_-1] == 0) {
// // printf("ERROR: Integer delay line lengths clash!!!!! Use a different tuning or try oversampling.\n");
// // return;
// // }
// // tubes_[numTubes_-1] = initTube(tubeLengths_[numTubes_-1]);
// // printf("th %d: lL = %d\n", numTubes_-1, tubeLengths_[numTubes_-1]);
// originalrb_ = convertTocm(d1)/200.0; // main bore radius
// rb_ = originalrb_;
// int lL = tubeLengths_[0];
// for (int i = 0; i < numToneHoles_; i++) {
// originalRth_[i] = convertTocm(calcdH(i, d1, LS, lL))/200.0;
// rth_[i] = originalRth_[i];
// lL += tubeLengths_[i+1];
// }
// printf("rb: %f\n", rb_);
// // printf("tubeLengths:\n");
// // for (int i = 0; i < numTubes_; i++) {
// // printf("tube %d: %d\n", i, tubeLengths_[i]);
// // }
// printf("toneHole radii:\n");
// lL = 0.0;
// for (int i = 0; i < numToneHoles_; i++) {
// lL += tubeLengths_[i];
// double LSh = (1.0/tuning[i]) * LS;
// printf("th %d rth: %f m, output freq when open: %f\n", i, rth_[i], checkTuning(d1, convertToSamples(rth_[i]*200.0), LSh, lL, calcg(i)));
// }
// calcTHCoeffs();
// }
/* ------ Parameter accessors ----------------------------------------- */
// This method allows setting of the toneHole0 "open-ness" at
// any point between "Open" (newValue = 1) and "Closed"
// (newValue = 0).
void setToneHoleIndex(int index) {
if (index < 0 || index >= numToneHoles_) {
printf("index out of range: %d\n", index);
return;
}
toneHoleIndex_ = index;
}
void setToneHole(double newValue) {
double new_coeff;
int index = toneHoleIndex_;
double thCoeff = thCoeff_[index];
if ( newValue <= 0.0 )
new_coeff = 0.9995;
else if ( newValue >= 1.0 )
new_coeff = thCoeff;
else
new_coeff = ( newValue * (thCoeff - 0.9995) ) + 0.9995;
setA1PoleZero( toneHoles_[index], -new_coeff );
setB0PoleZero( toneHoles_[index], new_coeff );
}
void setToneHoleRadius(double newRadius) {
if (newRadius < MIN_TONEHOLE_RADIUS || newRadius > MAX_TONEHOLE_RADIUS) {
printf("Radius is too big or too small: %f\n", newRadius);
return;
}
int index = toneHoleIndex_;
rth_[index] = newRadius;
scatter_[index] = -pow(newRadius,2) / ( pow(newRadius,2) + 2*pow(rb_,2) );
// Calculate toneHole coefficients.
double te = newRadius; // effective length of the open hole
thCoeff_[index] = (te*2*(SRATE*OVERSAMPLE) - C_m) / (te*2*(SRATE*OVERSAMPLE) + C_m);
}
void setLengthIndex(int index) {
if (index < 0 || index >= numTubes_) {
printf("index out of range: %d\n", index);
return;
}
tubeIndex_ = index;
}
void setLength(int newlen) {
if (newlen < 0 || newlen >= MAX_TUBE_LENGTH) {
printf("length out of range: %d\n", newlen);
return;
}
int index = tubeIndex_;
tubeLengths_[index] = newlen;
tubes_[index] = initTube(newlen);
}
void setBreathPressure(double input) {
breathPressure_ = input;
}
void setLPCutoff(double cut) {
cut = clip(cut, 30.0, 16000.0);
printf("lp cutoff: %f\n", cut);
setCutoffSVF(lp_, cut);
setCutoffSVF(lp2_, cut);
}
void setLPQ(double Q) {
Q = clip(Q, 0.0, 1.0);
printf("lp Q: %f\n", Q);
setQSVF(lp_, Q);
setQSVF(lp2_, Q);
}
void setPFCutoff(double cut) {
cut = clip(cut, 30.0, 16000.0);
printf("pf cutoff: %f\n", cut);
setCutoffSVF(pf_, cut);
setCutoffSVF(pf2_, cut);
}
void setPFQ(double Q) {
Q = clip(Q, 0.0, 1.0);
printf("pf Q: %f\n", Q);
setQSVF(pf_, Q);
setQSVF(pf2_, Q);
}
void setNoiseBPCutoff(double cut) {
cut = clip(cut, 30.0, 16000.0);
printf("noiseBP cutoff: %f\n", cut);
setCutoffSVF(noiseBP_, cut);
}
void setNoiseGain(double gain) {
gain = clip(gain, 0.0, 1.0);
printf("noise gain: %f\n", gain);
noiseGain_ = gain;
}
void setNoiseBPQ(double Q) {
Q = clip(Q, 0.0, 1.0);
printf("noiseBP Q: %f\n", Q);
setQSVF(noiseBP_, Q);
}
void setShaperDrive(double d) {
d = clip(d, 0.0, 1.0);
printf("m_drive_: %f\n", d);
m_drive_ = d;
}
void setShaperMix(double d) {
d = clip(d, 0.0, 1.0);
printf("shaper mix: %f\n", shaperMix_);
shaperMix_ = d;
}
void setTuningWrapper(int t) {
setTuning((tuningSystem) t);
}
void setCustomTuning(double f9, double f8, double f7, double f6, double f5, double f4, double f3, double f2, double f1, double f0, double fn1) {
double freqs[] = {f9, f8, f7, f6, f5, f4, f3, f2, f1, f0, fn1};
populateCustomTuning(freqs);
tune(freqs[NUM_NOTES - 1]);
}
// old version (you can tell by the STK parts)
void calcTHCoeffs() {
// Calculate initial tone hole three-port scattering coefficients
for (int i = 0; i < MAX_TONEHOLES; i++) {
scatter_[i] = -pow(rth_[i],2) / ( pow(rth_[i],2) + 2*pow(rb_,2) );
// Calculate toneHole coefficients and set for initially open.
thCoeff_[i] = (rth_[i]*2*(SRATE*OVERSAMPLE) - C_m) / (rth_[i]*2*(SRATE*OVERSAMPLE) + C_m);
// Initialize tone holes.
toneHoles_[i] = initPoleZero();
setA1PoleZero(toneHoles_[i], -thCoeff_[i]);
setB0PoleZero(toneHoles_[i], thCoeff_[i]);
setB1PoleZero(toneHoles_[i], -1.0);
}
}
// void calcTHCoeffs() {
// // Calculate initial tone hole three-port scattering coefficients
// for (int i = 0; i < MAX_TONEHOLES; i++) {
// scatter_[i] = -pow(rth_[i],2) / ( pow(rth_[i],2) + 2*pow(rb_,2) );
// // Calculate toneHole coefficients and set for initially open.
// thCoeff_[i] = (rth_[i]*2*(SRATE*OVERSAMPLE) - C_m) / (rth_[i]*2*(SRATE*OVERSAMPLE) + C_m);
// // Initialize tone holes.
// toneHoles_[i] = initPoleZero();
// setA1PoleZero(toneHoles_[i], -thCoeff_[i]);
// setB0PoleZero(toneHoles_[i], thCoeff_[i]);
// setB1PoleZero(toneHoles_[i], -1.0);
// }
// }
void tune(double Fc) {
// Fc = (tuning[NUM_NOTES-1]/tuning[NUM_NOTES]) * Fc;
// printf("Fc: %f\n", Fc);
double LS = calcLS(Fc);
int LC = calcLC(LS);
double d1 = calcd1(LC, LS);
printf("LC: %d\n", LC);
printf("d1: %f\n", d1);
int prevlL = 0;
int correction = 0;
for (int i = 0; i < numTubes_; i++) {
printf("tubelength %d: %d\n", i, calclL(d1, i, LS) - prevlL);
tubeLengths_[i] = calclL(d1, i, LS) - prevlL;
// if (i == 0) {
// tubelengths_[i] -= correction;
// }
if (tubeLengths_[i] == 0) {
printf("ERROR: Integer delay line lengths clash!!!!! Use a different tuning or try oversampling.\n");
return;
}
// if (tubes_[i] != NULL) {
// printf("WANNA BE FREEEEEEE\n");
// freeTube(tubes_[i]);
// }
if (i == 0) {
tubes_[i] = initTube(tubeLengths_[i]);
// prevlL += tubeLengths_[i] + correction;
prevlL += tubeLengths_[i];
} else {
tubes_[i] = initTube(tubeLengths_[i]);
prevlL += tubeLengths_[i];
}
printf("th %d: lL = %d\n", i, tubeLengths_[i]);
}
// Dummy
// tubeLengths_[numTubes_-1] = (int) ((1.0/tuning[numTubes_-1]) * LS) - prevlL;
// if (tubeLengths_[numTubes_-1] == 0) {
// printf("ERROR: Integer delay line lengths clash!!!!! Use a different tuning or try oversampling.\n");
// return;
// }
// tubes_[numTubes_-1] = initTube(tubeLengths_[numTubes_-1]);
// printf("th %d: lL = %d\n", numTubes_-1, tubeLengths_[numTubes_-1]);
originalrb_ = convertTocm(d1)/200.0; // main bore radius
rb_ = originalrb_;
int lL = tubeLengths_[0];
for (int i = 0; i < numToneHoles_; i++) {
originalRth_[i] = convertTocm(calcdH(i, d1, LS, lL))/200.0;
rth_[i] = originalRth_[i];
lL += tubeLengths_[i+1];
}
printf("rb: %f\n", rb_);
// printf("tubeLengths:\n");
// for (int i = 0; i < numTubes_; i++) {
// printf("tube %d: %d\n", i, tubeLengths_[i]);
// }
printf("toneHole radii:\n");
lL = 0.0;
for (int i = 0; i < numToneHoles_; i++) {
lL += tubeLengths_[i];
double LSh = (1.0/tuning[i]) * LS;
printf("th %d rth: %f m, output freq when open: %f\n", i, rth_[i], checkTuning(d1, convertToSamples(rth_[i]*200.0), LSh, lL, calcg(i)));
}
calcTHCoeffs();
}
double interpolateLinear(double a, double b, double alpha) {
return (alpha * a) + ((1.0-alpha) * b);
}
SAMPLE tick(SAMPLE in)
{
double breathInterp[OVERSAMPLE];
for (int i = 0; i < OVERSAMPLE; i++) {
breathInterp[i] = interpolateLinear(breathPressure_, prevBreathPressure_, (double) (i+1) / (double) OVERSAMPLE);
}
prevBreathPressure_ = breathPressure_;
double pap;
double pbm;
double pthm;
double outsamp = 0.0;
double scatter;
double bellReflected;
for (int t = 0; t < OVERSAMPLE; t++) {
double breath = breathInterp[t];
double noise = ((double)rand()/(double)RAND_MAX);
noise = noiseGain_ * (inputSVFBand(noiseBP_, noise));
// double noise = noiseGain_ * rand();
breath += breath * noise;
// Calculate the differential pressure = reflected - mouthpiece pressures
double pressureDiff = accessDelayLine(tubes_[0]->lower) - breath;
double reedLookup = pressureDiff * reedTable( pressureDiff );
breath = tanhClip(breath + reedLookup);
if (breath >= 1 || breath <= -1) {
printf("breath going out of bounds of -1 to 1: %f\n", breath);
}
// debug("%d\n", tubeLengths_[0]);
// Helps reduce high-pitched noise.
// breath = inputBiquad(biquad_, breath);
breath = interpolateLinear(shaper(breath, m_drive_), breath, shaperMix_);
// breath = inputSVFPeak(pf_, breath);
// breath = inputSVFLP(lp_, breath);
// breath = inputDCFilter(dcBlocker_, breath);
for (int i = 0; i < numToneHoles_; i++) {
// Index in tubes_[] of tube positioned before toneHoles[i].
int a = i + FRONT_TUBES - 1;
// Index in tubes_[] of tube positioned after toneHoles[i].
int b = i + FRONT_TUBES;
// Three-port junction scattering.
pap = accessDelayLine(tubes_[a]->upper);
pbm = accessDelayLine(tubes_[b]->lower);
pthm = z1PoleZero(toneHoles_[i]);
scatter = scatter_[i] * (pap + pbm - (2 * pthm));
pbp_[i] = pap + scatter;
pam_[i] = pbm + scatter;
pthp_[i] = pap + scatter + pbm - pthm;
// Sample output at tubes_[SAMPLE_INDEX].
if (a == SAMPLE_INDEX) {
outsamp += pap + pam_[i];
}
// Bell reflection at last tube.
if (i == numToneHoles_ - 1) {
double bell = accessDelayLine(tubes_[b]->upper);
// double bell2 = shaper(bell1, m_drive_);
// double bell4 = inputSVFPeak(pf2_, bell1);
// bell = inputSVFLP(lp2_, bell);
// // Reflection = Inversion + gain reduction + lowpass filtering.
// bell = inputSVFLP(lp2_, bell);
// bell = inputDCFilter(dcBlocker2_, bell);
bellReflected = bell * -0.995;
// bellReflected = filter_.tick(bell * -0.995);
}
}
// Perform all inputs at the end so that we're not altering
// state prior to calculations.
for (int i = 0; i < numToneHoles_; i++) {
// Index in tubes_[] of tube positioned before toneHoles[i].
int a = i + FRONT_TUBES - 1;
// Index in tubes_[] of tube positioned after toneHoles[i].
int b = i + FRONT_TUBES;
inputPoleZero(toneHoles_[i], pthp_[i]);
// toneHoles_[i]->tick(pthp_[i]);
inputDelayLine(tubes_[a]->lower, pam_[i]);
inputDelayLine(tubes_[b]->upper, pbp_[i]);
}
inputDelayLine(tubes_[0]->upper, breath);
inputDelayLine(tubes_[numTubes_-1]->lower, bellReflected);
}
count++;
// debug("%.5f -0-> ", breath);
// for (int i = 0; i < numToneHoles_; i++) {
// debug("\t%.5f\t-%d->", pbp_[i], i+1);
// }
// debug("\n");
// for (int i = 0; i < numToneHoles_; i++) {
// debug("%.5f\t<-%d-\t", pam_[i], i);
// }
// debug("%.5f\n", bellReflected);
// debug("range: %f - %f\n", min, max);
// debug("\n");
// Clipping, account for oversampling, and gain.
outsamp /= (double) OVERSAMPLE;
outsamp = tanhClip(outsamp);
outsamp *= outputGain_;
return outsamp;
}
// constructor
BirlPhysicalModel(t_CKFLOAT lowestFrequency)
{
numToneHoles_ = 1;
numTubes_ = 2;
tubeIndex_ = 0;
setTuning(EQUAL_TEMPERED);
tune(440.0);
printf("\n\n");
// reedTable_.setOffset( 0.7 );
// reedTable_.setSlope( -0.3 );
dcBlocker_ = initDCFilter(0.995);
dcBlocker2_ = initDCFilter(0.995);
biquad_ = initBiquad();
biquadSetCoeffs(biquad_, 0.169301, 0.338601, 0.169301, -0.482013, 0.186622);
pf_ = initSVF(2000.0, 0.5);
lp_ = initSVF(5000.0, 0.5);
pf2_ = initSVF(1000.0, 1.0);
lp2_ = initSVF(5000.0, 0.5);
noiseBP_ = initSVF(16000.0, 1.0);
outputGain_ = 1.0;
noiseGain_ = 0.2;
breathPressure_ = 0.0;
prevBreathPressure_ = 0.0;
count = 0;
min = 0.0;
max = 0.0;
m_drive_ = 0.0;
shaperMix_ = 0.0;
srand((unsigned)time(NULL));
}
// // for Chugins extending UGen
// SAMPLE tick(SAMPLE in) {
// return 0.0;
// // return inputSVFLP(lp_, noise_.tick());
// }
// SAMPLE tick(SAMPLE in)
// {
// double breathInterp[OVERSAMPLE];
// for (int i = 0; i < OVERSAMPLE; i++) {
// breathInterp[i] = interpolateLinear(breathPressure_, prevBreathPressure_, (double) (i+1) / (double) OVERSAMPLE);
// }
// prevBreathPressure_ = breathPressure_;
// double pap;
// double pbm;
// double pthm;
// double outsamp = 0.0;
// double scatter;
// double bellReflected;
// for (int t = 0; t < OVERSAMPLE; t++) {
// double breath = breathInterp[t];
// double noise = noiseGain_ * (inputSVFBand(noiseBP_, noise_.tick()));
// breath += breath * noise;
// // Calculate the differential pressure = reflected - mouthpiece pressures
// double pressureDiff = accessDelayLine(tubes_[0]->lower) - breath;
// double reedLookup = pressureDiff * reedTable( pressureDiff );
// breath = tanhClip(breath + reedLookup);
// if (breath >= 1 || breath <= -1) {
// printf("breath going out of bounds of -1 to 1: %f\n", breath);
// }
// // debug("%d\n", tubeLengths_[0]);
// // Helps reduce high-pitched noise.
// // breath = inputBiquad(biquad_, breath);
// breath = interpolateLinear(shaper(breath, m_drive_), breath, shaperMix_);
// // breath = inputSVFPeak(pf_, breath);
// // breath = inputSVFLP(lp_, breath);
// // breath = inputDCFilter(dcBlocker_, breath);
// for (int i = 0; i < numToneHoles_; i++) {
// // Index in tubes_[] of tube positioned before toneHoles[i].
// int a = i + FRONT_TUBES - 1;
// // Index in tubes_[] of tube positioned after toneHoles[i].
// int b = i + FRONT_TUBES;
// // Three-port junction scattering.
// pap = accessDelayLine(tubes_[a]->upper);
// pbm = accessDelayLine(tubes_[b]->lower);
// pthm = toneHoles_[i]->lastOut();
// scatter = scatter_[i] * (pap + pbm - (2 * pthm));
// pbp_[i] = pap + scatter;
// pam_[i] = pbm + scatter;
// pthp_[i] = pap + scatter + pbm - pthm;
// // Sample output at tubes_[SAMPLE_INDEX].
// if (a == SAMPLE_INDEX) {
// outsamp += pap + pam_[i];
// }
// // Bell reflection at last tube.
// if (i == numToneHoles_ - 1) {
// double bell = accessDelayLine(tubes_[b]->upper);
// // double bell2 = shaper(bell1, m_drive_);
// // double bell4 = inputSVFPeak(pf2_, bell1);
// // bell = inputSVFLP(lp2_, bell);
// // // Reflection = Inversion + gain reduction + lowpass filtering.
// // bell = inputSVFLP(lp2_, bell);
// // bell = inputDCFilter(dcBlocker2_, bell);
// bellReflected = bell * -0.995;
// // bellReflected = filter_.tick(bell * -0.995);
// }
// }
// // Perform all inputs at the end so that we're not altering
// // state prior to calculations.
// for (int i = 0; i < numToneHoles_; i++) {
// // Index in tubes_[] of tube positioned before toneHoles[i].
// int a = i + FRONT_TUBES - 1;
// // Index in tubes_[] of tube positioned after toneHoles[i].
// int b = i + FRONT_TUBES;
// toneHoles_[i]->tick(pthp_[i]);
// inputDelayLine(tubes_[a]->lower, pam_[i]);
// inputDelayLine(tubes_[b]->upper, pbp_[i]);
// }
// inputDelayLine(tubes_[0]->upper, breath);
// inputDelayLine(tubes_[numTubes_-1]->lower, bellReflected);
// }
// count++;
// // debug("%.5f -0-> ", breath);
// // for (int i = 0; i < numToneHoles_; i++) {
// // debug("\t%.5f\t-%d->", pbp_[i], i+1);
// // }
// // debug("\n");
// // for (int i = 0; i < numToneHoles_; i++) {
// // debug("%.5f\t<-%d-\t", pam_[i], i);
// // }
// // debug("%.5f\n", bellReflected);
// // debug("range: %f - %f\n", min, max);
// // debug("\n");
// // Clipping, account for oversampling, and gain.
// outsamp /= (double) OVERSAMPLE;
// outsamp = tanhClip(outsamp);
// outsamp *= outputGain_;
// return outsamp;
// }
protected:
int numTubes_;
int tubeIndex_;
int numToneHoles_;
int toneHoleIndex_;
double pam_[MAX_TONEHOLES];
double pbp_[MAX_TONEHOLES];
double pthp_[MAX_TONEHOLES];
double scatter_[MAX_TONEHOLES];
double thCoeff_[MAX_TONEHOLES];
Tube *tubes_[MAX_TUBES];
PoleZero *toneHoles_[MAX_TONEHOLES];
// stk::ReedTable reedTable_;
stk::OneZero filter_;
// stk::Noise noise_;
DCFilter *dcBlocker_;
DCFilter *dcBlocker2_;
Biquad *biquad_;
Biquad *biquad2_;
SVF *pf_;
SVF *lp_;
SVF *pf2_;
SVF *lp2_;
SVF *noiseBP_;
double rb_;
double originalrb_;
double rhGain_;
double outputGain_;
double noiseGain_;
double m_drive_;
double shaperMix_;
double breathPressure_;
double prevBreathPressure_;
int count;
double min, max;
};
// query function: chuck calls this when loading the Chugin
// NOTE: developer will need to modify this function to
// add additional functions to this Chugin
CK_DLL_QUERY( BirlPhysicalModel )
{
// hmm, don't change this...
QUERY->setname(QUERY, "BirlPhysicalModel");
// begin the class definition
// can change the second argument to extend a different ChucK class
QUERY->begin_class(QUERY, "BirlPhysicalModel", "UGen");
// register the constructor (probably no need to change)
QUERY->add_ctor(QUERY, birlphysicalmodel_ctor);
// register the destructor (probably no need to change)
QUERY->add_dtor(QUERY, birlphysicalmodel_dtor);
// for UGen's only: add tick function
QUERY->add_ugen_func(QUERY, birlphysicalmodel_tick, NULL, 1, 1);
// NOTE: if this is to be a UGen with more than 1 channel,
// e.g., a multichannel UGen -- will need to use add_ugen_funcf()
// and declare a tickf function using CK_DLL_TICKF
// breathPressure_ getter/setter.
QUERY->add_mfun(QUERY, birlphysicalmodel_setBreathPressure, "float", "breathPressure");
QUERY->add_arg(QUERY, "float", "arg0");
// Delay line length getter/setter.
QUERY->add_mfun(QUERY, birlphysicalmodel_setLength, "int", "length");
QUERY->add_arg(QUERY, "int", "arg1");
QUERY->add_arg(QUERY, "int", "arg2");
// toneHole setter
QUERY->add_mfun(QUERY, birlphysicalmodel_setToneHole, "float", "toneHole");
QUERY->add_arg(QUERY, "int", "arg4");
QUERY->add_arg(QUERY, "float", "arg5");
// toneHole radius setter.
QUERY->add_mfun(QUERY, birlphysicalmodel_setToneHoleRadius, "float", "toneHoleRadius");
QUERY->add_arg(QUERY, "int", "arg6");
QUERY->add_arg(QUERY, "float", "arg7");
// tune function.
QUERY->add_mfun(QUERY, birlphysicalmodel_setFundamental, "float", "setFundamental");
QUERY->add_arg(QUERY, "float", "arg8");
// rb tweak.
QUERY->add_mfun(QUERY, birlphysicalmodel_tweakBoreRadius, "int", "tweakBoreRadius");
QUERY->add_arg(QUERY, "int", "arg9");
// rth tweak.
QUERY->add_mfun(QUERY, birlphysicalmodel_tweakToneHoleRadius, "int", "tweakToneHoleRadius");
QUERY->add_arg(QUERY, "int", "arg10");
QUERY->add_arg(QUERY, "int", "arg11");
// Filter cutoff
QUERY->add_mfun(QUERY, birlphysicalmodel_lpQ, "int", "LPQ");
QUERY->add_arg(QUERY, "float", "arg21");
// Filter Q
QUERY->add_mfun(QUERY, birlphysicalmodel_lpCutoff, "int", "LPcutoff");
QUERY->add_arg(QUERY, "float", "arg20");
// Filter cutoff
QUERY->add_mfun(QUERY, birlphysicalmodel_pfQ, "int", "PFQ");
QUERY->add_arg(QUERY, "float", "arg22");
// Filter Q
QUERY->add_mfun(QUERY, birlphysicalmodel_pfCutoff, "int", "PFcutoff");
QUERY->add_arg(QUERY, "float", "arg23");
// NoiseBP cutoff
QUERY->add_mfun(QUERY, birlphysicalmodel_noiseBPQ, "int", "noiseQ");
QUERY->add_arg(QUERY, "float", "noiseBPQ");
// NoiseBP Q
QUERY->add_mfun(QUERY, birlphysicalmodel_noiseBPCutoff, "int", "noiseCutoff");
QUERY->add_arg(QUERY, "float", "noiseBPCut");
// Noise gain
QUERY->add_mfun(QUERY, birlphysicalmodel_noiseGain, "int", "noiseGain");
QUERY->add_arg(QUERY, "float", "noiseGain");
// Shaper
QUERY->add_mfun(QUERY, birlphysicalmodel_shaper, "int", "shaper");
QUERY->add_arg(QUERY, "float", "arg15");
// Shaper mix
QUERY->add_mfun(QUERY, birlphysicalmodel_shaperMix, "int", "shaperMix");
QUERY->add_arg(QUERY, "float", "arg16");
// Tuning
QUERY->add_mfun(QUERY, birlphysicalmodel_setTuning, "int", "tuning");
QUERY->add_arg(QUERY, "int", "arg14");
QUERY->add_mfun(QUERY, birlphysicalmodel_setCustomTuning, "int", "setCustomTuning");
QUERY->add_arg(QUERY, "float", "f9");
QUERY->add_arg(QUERY, "float", "f8");
QUERY->add_arg(QUERY, "float", "f7");
QUERY->add_arg(QUERY, "float", "f6");
QUERY->add_arg(QUERY, "float", "f5");
QUERY->add_arg(QUERY, "float", "f4");
QUERY->add_arg(QUERY, "float", "f3");
QUERY->add_arg(QUERY, "float", "f2");
QUERY->add_arg(QUERY, "float", "f1");
QUERY->add_arg(QUERY, "float", "f0");
QUERY->add_arg(QUERY, "float", "fn1");
// this reserves a variable in the ChucK internal class to store
// referene to the c++ class we defined above
birlphysicalmodel_data_offset = QUERY->add_mvar(QUERY, "int", "@bhbp_data", false);
// end the class definition
// IMPORTANT: this MUST be called!
QUERY->end_class(QUERY);
// wasn't that a breeze?
return TRUE;
}
// implementation for the constructor
CK_DLL_CTOR(birlphysicalmodel_ctor)
{
// get the offset where we'll store our internal c++ class pointer
OBJ_MEMBER_INT(SELF, birlphysicalmodel_data_offset) = 0;
// instantiate our internal c++ class representation
BirlPhysicalModel * bcdata = new BirlPhysicalModel(API->vm->get_srate());
// store the pointer in the ChucK object member
OBJ_MEMBER_INT(SELF, birlphysicalmodel_data_offset) = (t_CKINT) bcdata;
}
// implementation for the destructor
CK_DLL_DTOR(birlphysicalmodel_dtor)
{
// get our c++ class pointer
BirlPhysicalModel * bcdata = (BirlPhysicalModel *) OBJ_MEMBER_INT(SELF, birlphysicalmodel_data_offset);
// check it
if( bcdata )
{
// clean up
delete bcdata;
OBJ_MEMBER_INT(SELF, birlphysicalmodel_data_offset) = 0;
bcdata = NULL;
}
}
// implementation for tick function
CK_DLL_TICK(birlphysicalmodel_tick)
{
// get our c++ class pointer
BirlPhysicalModel * c = (BirlPhysicalModel *) OBJ_MEMBER_INT(SELF, birlphysicalmodel_data_offset);
// invoke our tick function; store in the magical out variable
if(c) *out = c->tick(in);
// yes
return TRUE;
}
CK_DLL_MFUN(birlphysicalmodel_setBreathPressure)
{
// get our c++ class pointer
BirlPhysicalModel * bcdata = (BirlPhysicalModel *) OBJ_MEMBER_INT(SELF, birlphysicalmodel_data_offset);
// set the return value
bcdata->setBreathPressure(GET_NEXT_FLOAT(ARGS));
}
CK_DLL_MFUN(birlphysicalmodel_setLength)
{
// get our c++ class pointer
BirlPhysicalModel * bcdata = (BirlPhysicalModel *) OBJ_MEMBER_INT(SELF, birlphysicalmodel_data_offset);
// set the return value
bcdata->setLengthIndex(GET_NEXT_INT(ARGS));
bcdata->setLength(GET_NEXT_INT(ARGS));
}
CK_DLL_MFUN(birlphysicalmodel_setToneHole)
{
// get our c++ class pointer
BirlPhysicalModel * bcdata = (BirlPhysicalModel *) OBJ_MEMBER_INT(SELF, birlphysicalmodel_data_offset);
// set the return value
bcdata->setToneHoleIndex(GET_NEXT_INT(ARGS));
bcdata->setToneHole(GET_NEXT_FLOAT(ARGS));
}
CK_DLL_MFUN(birlphysicalmodel_setToneHoleRadius)
{
// get our c++ class pointer
BirlPhysicalModel * bcdata = (BirlPhysicalModel *) OBJ_MEMBER_INT(SELF, birlphysicalmodel_data_offset);
// set the return value
bcdata->setToneHoleIndex(GET_NEXT_INT(ARGS));
bcdata->setToneHoleRadius(GET_NEXT_FLOAT(ARGS));
}
CK_DLL_MFUN(birlphysicalmodel_setFundamental)
{
// get our c++ class pointer
BirlPhysicalModel * bcdata = (BirlPhysicalModel *) OBJ_MEMBER_INT(SELF, birlphysicalmodel_data_offset);
// set the return value
bcdata->tune(GET_NEXT_FLOAT(ARGS));
printf("\n\n");
}
CK_DLL_MFUN(birlphysicalmodel_tweakBoreRadius)
{
// get our c++ class pointer
BirlPhysicalModel * bcdata = (BirlPhysicalModel *) OBJ_MEMBER_INT(SELF, birlphysicalmodel_data_offset);
// set the return value
bcdata->tweakRb(GET_NEXT_INT(ARGS));
}
CK_DLL_MFUN(birlphysicalmodel_tweakToneHoleRadius)
{
// get our c++ class pointer
BirlPhysicalModel * bcdata = (BirlPhysicalModel *) OBJ_MEMBER_INT(SELF, birlphysicalmodel_data_offset);
// set the return value
bcdata->setToneHoleIndex(GET_NEXT_INT(ARGS));
bcdata->tweakRth(GET_NEXT_INT(ARGS));
}
CK_DLL_MFUN(birlphysicalmodel_lpCutoff)
{
// get our c++ class pointer
BirlPhysicalModel * bcdata = (BirlPhysicalModel *) OBJ_MEMBER_INT(SELF, birlphysicalmodel_data_offset);
// set the return value
bcdata->setLPCutoff(GET_NEXT_FLOAT(ARGS));
}
CK_DLL_MFUN(birlphysicalmodel_lpQ)
{
// get our c++ class pointer
BirlPhysicalModel * bcdata = (BirlPhysicalModel *) OBJ_MEMBER_INT(SELF, birlphysicalmodel_data_offset);
// set the return value
bcdata->setLPQ(GET_NEXT_FLOAT(ARGS));
}