/
FilterUGens.cpp
5492 lines (4461 loc) · 170 KB
/
FilterUGens.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
/*
SuperCollider real time audio synthesis system
Copyright (c) 2002 James McCartney. All rights reserved.
http://www.audiosynth.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "SC_PlugIn.h"
#include <limits>
// NaNs are not equal to any floating point number
static const float uninitializedControl = std::numeric_limits<float>::quiet_NaN();
#define PI 3.1415926535898f
#define PUSH_LOOPVALS \
int tmp_floops = unit->mRate->mFilterLoops; \
int tmp_fremain = unit->mRate->mFilterRemain; \
unit->mRate->mFilterLoops = 0; \
unit->mRate->mFilterRemain = 1;
#define POP_LOOPVALS \
unit->mRate->mFilterLoops = tmp_floops; \
unit->mRate->mFilterRemain = tmp_fremain;
using namespace std; // for math functions
static InterfaceTable* ft;
struct Ramp : public Unit {
double m_level, m_slope;
int m_counter;
};
struct Lag : public Unit {
float m_lag;
double m_b1, m_y1;
};
struct Lag2 : public Unit {
float m_lag;
double m_b1, m_y1a, m_y1b;
};
struct Lag3 : public Unit {
float m_lag;
double m_b1, m_y1a, m_y1b, m_y1c;
};
struct LagUD : public Unit {
float m_lagu, m_lagd;
double m_b1u, m_b1d, m_y1;
};
struct Lag2UD : public Unit {
float m_lagu, m_lagd;
double m_b1u, m_b1d, m_y1a, m_y1b;
};
struct Lag3UD : public Unit {
float m_lagu, m_lagd;
double m_b1u, m_b1d, m_y1a, m_y1b, m_y1c;
};
struct VarLag : public Unit {
double m_level, m_slope;
int m_counter;
float m_in, m_lagTime;
};
struct OnePole : public Unit {
double m_b1, m_y1;
};
struct OneZero : public Unit {
double m_b1, m_x1;
};
struct Integrator : public Unit {
double m_b1, m_y1;
};
struct Decay : public Unit {
float m_decayTime;
double m_y1, m_b1;
};
struct Decay2 : public Unit {
float m_attackTime;
double m_y1a, m_b1a;
float m_decayTime;
double m_y1b, m_b1b;
};
struct LeakDC : public Unit {
double m_b1, m_x1, m_y1;
};
struct TwoPole : public Unit {
float m_freq, m_reson;
double m_y1, m_y2, m_b1, m_b2;
};
struct APF : public Unit {
float m_freq, m_reson;
double m_y1, m_y2, m_x1, m_x2, m_b1, m_b2;
};
struct TwoZero : public Unit {
float m_freq, m_reson;
double m_x1, m_x2, m_b1, m_b2;
};
struct LPZ1 : public Unit {
double m_x1;
};
struct HPZ1 : public Unit {
double m_x1;
};
struct HPZ2 : public Unit {
double m_x1, m_x2;
};
struct BPZ2 : public Unit {
double m_x1, m_x2;
};
struct BRZ2 : public Unit {
double m_x1, m_x2;
};
struct LPZ2 : public Unit {
double m_x1, m_x2;
};
struct Flip : public Unit {};
struct Delay1 : public Unit {
float m_x1;
};
struct Delay2 : public Unit {
float m_x1, m_x2;
};
struct Slope : public Unit {
double m_x1;
};
struct Slew : public Unit {
double mLevel;
};
struct RLPF : public Unit {
float m_freq, m_reson;
double m_y1, m_y2, m_a0, m_b1, m_b2;
};
struct RHPF : public Unit {
float m_freq, m_reson;
double m_y1, m_y2, m_a0, m_b1, m_b2;
};
struct LPF : public Unit {
float m_freq;
double m_y1, m_y2, m_a0, m_b1, m_b2;
};
struct HPF : public Unit {
float m_freq;
double m_y1, m_y2, m_a0, m_b1, m_b2;
};
struct BPF : public Unit {
float m_freq, m_bw;
double m_y1, m_y2, m_a0, m_b1, m_b2;
};
struct BRF : public Unit {
float m_freq, m_bw;
double m_y1, m_y2, m_a0, m_a1, m_b2;
};
struct MidEQ : public Unit {
float m_freq, m_bw, m_db;
double m_y1, m_y2, m_a0, m_b1, m_b2;
};
struct Resonz : public Unit {
float m_freq, m_rq;
double m_y1, m_y2, m_a0, m_b1, m_b2;
};
struct Ringz : public Unit {
float m_freq, m_decayTime;
double m_y1, m_y2, m_b1, m_b2;
};
struct FOS : public Unit {
double m_y1, m_a0, m_a1, m_b1;
};
struct SOS : public Unit {
double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
};
struct Formlet : public Unit {
float m_freq, m_decayTime, m_attackTime;
double m_y01, m_y02, m_b01, m_b02;
double m_y11, m_y12, m_b11, m_b12;
};
const int kMAXMEDIANSIZE = 32;
struct Median : public Unit {
float m_medianValue[kMAXMEDIANSIZE];
long m_medianAge[kMAXMEDIANSIZE];
long m_medianSize, m_medianIndex;
};
struct Compander : public Unit {
float m_prevmaxval, m_gain, m_clamp, m_clampcoef, m_relax, m_relaxcoef;
};
struct Normalizer : public Unit {
float *m_table, *m_xinbuf, *m_xoutbuf, *m_xmidbuf;
long m_flips, m_pos, m_bufsize;
float m_slope, m_level, m_curmaxval, m_prevmaxval, m_slopefactor;
};
struct Limiter : public Unit {
float *m_table, *m_xinbuf, *m_xoutbuf, *m_xmidbuf;
long m_flips, m_pos, m_bufsize;
float m_slope, m_level, m_curmaxval, m_prevmaxval, m_slopefactor;
};
struct Amplitude : public Unit {
float m_previn, m_clampcoef, m_relaxcoef, m_clamp_in, m_relax_in;
};
struct DetectSilence : public Unit {
float mThresh;
int32 mCounter, mEndCounter;
};
struct Hilbert : public Unit {
double m_coefs[12];
double m_y1[12];
};
struct FreqShift : public Unit {
float m_coefs[12];
float m_y1[12];
int32 m_phase;
int32 m_phaseoffset, m_lomask;
double m_cpstoinc, m_radtoinc, m_phasein;
};
struct MoogFF : public Unit {
float m_freq, m_k;
double m_b0, m_a1; // Resonant freq and corresponding vals; stored because we need to compare against prev vals
double m_s1, m_s2, m_s3, m_s4; // 1st order filter states
};
//////////////////////////////////////////////////////////////////////////////////////////////////
extern "C" {
void Ramp_next(Ramp* unit, int inNumSamples);
void Ramp_next_1(Ramp* unit, int inNumSamples);
void Ramp_Ctor(Ramp* unit);
void Lag_next(Lag* unit, int inNumSamples);
void Lag_Ctor(Lag* unit);
void Lag2_Ctor(Lag2* unit);
void Lag3_next(Lag3* unit, int inNumSamples);
void Lag3_Ctor(Lag3* unit);
void LagUD_next(LagUD* unit, int inNumSamples);
void LagUD_Ctor(LagUD* unit);
void Lag2UD_next(Lag2UD* unit, int inNumSamples);
void Lag2UD_Ctor(Lag2UD* unit);
void Lag3UD_next(Lag3UD* unit, int inNumSamples);
void Lag3UD_Ctor(Lag3UD* unit);
void VarLag_next(VarLag* unit, int inNumSamples);
void VarLag_Ctor(VarLag* unit);
void OnePole_next_a(OnePole* unit, int inNumSamples);
void OnePole_next_k(OnePole* unit, int inNumSamples);
void OnePole_Ctor(OnePole* unit);
void OneZero_next(OneZero* unit, int inNumSamples);
void OneZero_Ctor(OneZero* unit);
void Integrator_next(Integrator* unit, int inNumSamples);
void Integrator_next_i(Integrator* unit, int inNumSamples);
void Integrator_Ctor(Integrator* unit);
void Decay_next(Decay* unit, int inNumSamples);
void Decay_Ctor(Decay* unit);
void Decay2_next(Decay2* unit, int inNumSamples);
void Decay2_Ctor(Decay2* unit);
void LeakDC_next(LeakDC* unit, int inNumSamples);
void LeakDC_next_1(LeakDC* unit, int inNumSamples);
void LeakDC_Ctor(LeakDC* unit);
void TwoPole_next(TwoPole* unit, int inNumSamples);
void TwoPole_Ctor(TwoPole* unit);
void TwoZero_next(TwoZero* unit, int inNumSamples);
void TwoZero_Ctor(TwoZero* unit);
void APF_next(APF* unit, int inNumSamples);
void APF_Ctor(APF* unit);
void LPZ1_next(LPZ1* unit, int inNumSamples);
void LPZ1_Ctor(LPZ1* unit);
void HPZ1_next(HPZ1* unit, int inNumSamples);
void HPZ1_Ctor(HPZ1* unit);
void Slope_next(Slope* unit, int inNumSamples);
void Slope_Ctor(Slope* unit);
void Delay1_next(Delay1* unit, int inNumSamples);
void Delay1_Ctor(Delay1* unit);
void Flip_Ctor(Flip* unit);
void Flip_next_even(Flip* unit, int inNumSamples);
void Flip_next_odd(Flip* unit, int inNumSamples);
void Delay2_next(Delay2* unit, int inNumSamples);
void Delay2_Ctor(Delay2* unit);
void LPZ2_next(LPZ2* unit, int inNumSamples);
void LPZ2_Ctor(LPZ2* unit);
void HPZ2_next(HPZ2* unit, int inNumSamples);
void HPZ2_Ctor(HPZ2* unit);
void BPZ2_next(BPZ2* unit, int inNumSamples);
void BPZ2_Ctor(BPZ2* unit);
void BRZ2_next(BRZ2* unit, int inNumSamples);
void BRZ2_Ctor(BRZ2* unit);
void Slew_next(Slew* unit, int inNumSamples);
void Slew_Ctor(Slew* unit);
void RLPF_next(RLPF* unit, int inNumSamples);
void RLPF_next_1(RLPF* unit, int inNumSamples);
void RLPF_Ctor(RLPF* unit);
void RHPF_next(RHPF* unit, int inNumSamples);
void RHPF_next_1(RHPF* unit, int inNumSamples);
void RHPF_Ctor(RHPF* unit);
void LPF_next(LPF* unit, int inNumSamples);
void LPF_next_1(LPF* unit, int inNumSamples);
void LPF_Ctor(LPF* unit);
void HPF_next(HPF* unit, int inNumSamples);
void HPF_next_1(HPF* unit, int inNumSamples);
void HPF_Ctor(HPF* unit);
void BPF_next(BPF* unit, int inNumSamples);
void BPF_next_1(BPF* unit, int inNumSamples);
void BPF_Ctor(BPF* unit);
void BRF_next(BRF* unit, int inNumSamples);
void BRF_next_1(BRF* unit, int inNumSamples);
void BRF_Ctor(BRF* unit);
void Median_next(Median* unit, int inNumSamples);
void Median_Ctor(Median* unit);
void MidEQ_next(MidEQ* unit, int inNumSamples);
void MidEQ_Ctor(MidEQ* unit);
void Resonz_next(Resonz* unit, int inNumSamples);
void Resonz_Ctor(Resonz* unit);
void Ringz_next(Ringz* unit, int inNumSamples);
void Ringz_Ctor(Ringz* unit);
void Formlet_next(Formlet* unit, int inNumSamples);
void Formlet_next_1(Formlet* unit, int inNumSamples);
void Formlet_Ctor(Formlet* unit);
void FOS_next_k(FOS* unit, int inNumSamples);
void FOS_next_a(FOS* unit, int inNumSamples);
void FOS_next_1(FOS* unit, int inNumSamples);
void FOS_Ctor(FOS* unit);
void SOS_next_i(SOS* unit, int inNumSamples);
void SOS_next_k(SOS* unit, int inNumSamples);
void SOS_next_a(SOS* unit, int inNumSamples);
void SOS_next_1(SOS* unit, int inNumSamples);
void SOS_Ctor(SOS* unit);
void Normalizer_next(Normalizer* unit, int inNumSamples);
void Normalizer_Ctor(Normalizer* unit);
void Normalizer_Dtor(Normalizer* unit);
void Limiter_next(Limiter* unit, int inNumSamples);
void Limiter_Ctor(Limiter* unit);
void Limiter_Dtor(Limiter* unit);
void Compander_next(Compander* unit, int inNumSamples);
void Compander_Ctor(Compander* unit);
void Amplitude_next(Amplitude* unit, int inNumSamples);
void Amplitude_next_kk(Amplitude* unit, int inNumSamples);
void Amplitude_next_atok(Amplitude* unit, int inNumSamples);
void Amplitude_next_atok_kk(Amplitude* unit, int inNumSamples);
void Amplitude_Ctor(Amplitude* unit);
void DetectSilence_next(DetectSilence* unit, int inNumSamples);
void DetectSilence_next_k(DetectSilence* unit, int inNumSamples);
// void DetectSilence_done(DetectSilence *unit, int inNumSamples);
void DetectSilence_Ctor(DetectSilence* unit);
void Hilbert_Ctor(Hilbert* unit);
void Hilbert_next(Hilbert* unit, int inNumSamples);
void FreqShift_Ctor(FreqShift* unit);
void FreqShift_next_kk(FreqShift* unit, int inNumSamples);
void FreqShift_next_aa(FreqShift* unit, int inNumSamples);
void FreqShift_next_ak(FreqShift* unit, int inNumSamples);
void FreqShift_next_ka(FreqShift* unit, int inNumSamples);
void MoogFF_next(MoogFF* unit, int inNumSamples);
void MoogFF_Ctor(MoogFF* unit);
/*
void Lag_next(Lag *unit, int inNumSamples);
void Lag_Ctor(Lag* unit);
void Lag_next(Lag *unit, int inNumSamples);
void Lag_Ctor(Lag* unit);
*/
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void Ramp_next(Ramp* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = IN(0);
float period = ZIN0(1);
double slope = unit->m_slope;
double level = unit->m_level;
int counter = unit->m_counter;
int remain = inNumSamples;
while (remain) {
int nsmps = sc_min(remain, counter);
LOOP(nsmps, ZXP(out) = level; level += slope;);
in += nsmps;
counter -= nsmps;
remain -= nsmps;
if (counter <= 0) {
counter = (int)(period * SAMPLERATE);
counter = sc_max(1, counter);
slope = (*in - level) / counter;
}
}
unit->m_level = level;
unit->m_slope = slope;
unit->m_counter = counter;
}
void Ramp_next_1(Ramp* unit, int inNumSamples) {
float* out = OUT(0);
*out = unit->m_level;
unit->m_level += unit->m_slope;
if (--unit->m_counter <= 0) {
float in = ZIN0(0);
float period = ZIN0(1);
int counter = (int)(period * SAMPLERATE);
unit->m_counter = counter = sc_max(1, counter);
unit->m_slope = (in - unit->m_level) / counter;
}
}
void Ramp_Ctor(Ramp* unit) {
if (BUFLENGTH == 1) {
SETCALC(Ramp_next_1);
} else {
SETCALC(Ramp_next);
}
unit->m_counter = 1;
unit->m_level = ZIN0(0);
unit->m_slope = 0.f;
ZOUT0(0) = unit->m_level;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void Lag_next(Lag* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float lag = ZIN0(1);
double y1 = unit->m_y1;
double b1 = unit->m_b1;
if (lag == unit->m_lag) {
LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * (y1 - y0););
} else {
unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
double b1_slope = CALCSLOPE(unit->m_b1, b1);
unit->m_lag = lag;
LOOP1(inNumSamples, b1 += b1_slope; double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * (y1 - y0););
}
unit->m_y1 = zapgremlins(y1);
}
void Lag_next_1(Lag* unit, int inNumSamples) {
float* out = OUT(0);
float* in = IN(0);
float lag = IN0(1);
double y1 = unit->m_y1;
double b1 = unit->m_b1;
if (lag == unit->m_lag) {
double y0 = *in;
*out = y1 = y0 + b1 * (y1 - y0);
} else {
unit->m_b1 = b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
unit->m_lag = lag;
double y0 = *in;
*out = y1 = y0 + b1 * (y1 - y0);
}
unit->m_y1 = zapgremlins(y1);
}
void Lag_Ctor(Lag* unit) {
if (BUFLENGTH == 1)
SETCALC(Lag_next_1);
else
SETCALC(Lag_next);
unit->m_lag = uninitializedControl;
unit->m_b1 = 0.f;
unit->m_y1 = ZIN0(0);
Lag_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void LagUD_next(LagUD* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float lagu = ZIN0(1);
float lagd = ZIN0(2);
double y1 = unit->m_y1;
double b1u = unit->m_b1u;
double b1d = unit->m_b1d;
if ((lagu == unit->m_lagu) && (lagd == unit->m_lagd)) {
LOOP1(inNumSamples, double y0 = ZXP(in); if (y0 > y1) ZXP(out) = y1 = y0 + b1u * (y1 - y0);
else ZXP(out) = y1 = y0 + b1d * (y1 - y0););
} else {
unit->m_b1u = lagu == 0.f ? 0.f : exp(log001 / (lagu * unit->mRate->mSampleRate));
double b1u_slope = CALCSLOPE(unit->m_b1u, b1u);
unit->m_lagu = lagu;
unit->m_b1d = lagd == 0.f ? 0.f : exp(log001 / (lagd * unit->mRate->mSampleRate));
double b1d_slope = CALCSLOPE(unit->m_b1d, b1d);
unit->m_lagd = lagd;
LOOP1(inNumSamples, b1u += b1u_slope; b1d += b1d_slope; double y0 = ZXP(in);
if (y0 > y1) ZXP(out) = y1 = y0 + b1u * (y1 - y0); else ZXP(out) = y1 = y0 + b1d * (y1 - y0););
}
unit->m_y1 = zapgremlins(y1);
}
void LagUD_Ctor(LagUD* unit) {
SETCALC(LagUD_next);
unit->m_lagu = uninitializedControl;
unit->m_lagd = uninitializedControl;
unit->m_b1u = 0.f;
unit->m_b1d = 0.f;
unit->m_y1 = ZIN0(0);
LagUD_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
static void Lag2_next_k(Lag2* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float lag = ZIN0(1);
double y1a = unit->m_y1a;
double y1b = unit->m_y1b;
double b1 = unit->m_b1;
if (lag == unit->m_lag) {
LOOP1(inNumSamples, double y0a = ZXP(in); y1a = y0a + b1 * (y1a - y0a); y1b = y1a + b1 * (y1b - y1a);
ZXP(out) = y1b;);
} else {
unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
double b1_slope = CALCSLOPE(unit->m_b1, b1);
unit->m_lag = lag;
LOOP1(inNumSamples, b1 += b1_slope; double y0a = ZXP(in); y1a = y0a + b1 * (y1a - y0a);
y1b = y1a + b1 * (y1b - y1a); ZXP(out) = y1b;);
}
unit->m_y1a = zapgremlins(y1a);
unit->m_y1b = zapgremlins(y1b);
}
static void Lag2_next_i(Lag2* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
double y1a = unit->m_y1a;
double y1b = unit->m_y1b;
double b1 = unit->m_b1;
LOOP1(inNumSamples, double y0a = ZXP(in); y1a = y0a + b1 * (y1a - y0a); y1b = y1a + b1 * (y1b - y1a);
ZXP(out) = y1b;);
unit->m_y1a = zapgremlins(y1a);
unit->m_y1b = zapgremlins(y1b);
}
static void Lag2_next_1_i(Lag2* unit, int inNumSamples) {
double y1a = unit->m_y1a;
double y1b = unit->m_y1b;
double b1 = unit->m_b1;
float y0a = ZIN0(0);
y1a = y0a + b1 * (y1a - y0a);
y1b = y1a + b1 * (y1b - y1a);
ZOUT0(0) = y1b;
unit->m_y1a = zapgremlins(y1a);
unit->m_y1b = zapgremlins(y1b);
}
void Lag2_Ctor(Lag2* unit) {
switch (INRATE(1)) {
case calc_FullRate:
case calc_BufRate:
SETCALC(Lag2_next_k);
break;
default:
if (BUFLENGTH == 1)
SETCALC(Lag2_next_1_i);
else
SETCALC(Lag2_next_i);
break;
}
unit->m_lag = uninitializedControl;
unit->m_b1 = 0.f;
unit->m_y1a = unit->m_y1b = ZIN0(0);
Lag2_next_k(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void Lag2UD_next(Lag2UD* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float lagu = ZIN0(1);
float lagd = ZIN0(2);
double y1a = unit->m_y1a;
double y1b = unit->m_y1b;
double b1u = unit->m_b1u;
double b1d = unit->m_b1d;
if ((lagu == unit->m_lagu) && (lagd == unit->m_lagd)) {
LOOP1(
inNumSamples, double y0a = ZXP(in); if (y0a > y1a) { y1a = y0a + b1u * (y1a - y0a); } else {
y1a = y0a + b1d * (y1a - y0a);
} if (y1a > y1b) y1b = y1a + b1u * (y1b - y1a);
else y1b = y1a + b1d * (y1b - y1a); ZXP(out) = y1b;);
} else {
unit->m_b1u = lagu == 0.f ? 0.f : exp(log001 / (lagu * unit->mRate->mSampleRate));
double b1u_slope = CALCSLOPE(unit->m_b1u, b1u);
unit->m_lagu = lagu;
unit->m_b1d = lagd == 0.f ? 0.f : exp(log001 / (lagd * unit->mRate->mSampleRate));
double b1d_slope = CALCSLOPE(unit->m_b1d, b1d);
unit->m_lagd = lagd;
LOOP1(
inNumSamples, b1u += b1u_slope; b1d += b1d_slope; double y0a = ZXP(in); if (y0a > y1a) {
y1a = y0a + b1u * (y1a - y0a);
} else { y1a = y0a + b1d * (y1a - y0a); } if (y1a > y1b) y1b = y1a + b1u * (y1b - y1a);
else y1b = y1a + b1d * (y1b - y1a); ZXP(out) = y1b;);
}
unit->m_y1a = zapgremlins(y1a);
unit->m_y1b = zapgremlins(y1b);
}
void Lag2UD_Ctor(Lag2UD* unit) {
SETCALC(Lag2UD_next);
unit->m_lagu = 0.f;
unit->m_lagd = 0.f;
unit->m_b1u = 0.f;
unit->m_b1d = 0.f;
unit->m_y1a = unit->m_y1b = ZIN0(0);
Lag2UD_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void Lag3_next(Lag3* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float lag = ZIN0(1);
double y1a = unit->m_y1a;
double y1b = unit->m_y1b;
double y1c = unit->m_y1c;
double b1 = unit->m_b1;
if (lag == unit->m_lag) {
LOOP1(inNumSamples, double y0a = ZXP(in); y1a = y0a + b1 * (y1a - y0a); y1b = y1a + b1 * (y1b - y1a);
y1c = y1b + b1 * (y1c - y1b); ZXP(out) = y1c;);
} else {
unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
double b1_slope = CALCSLOPE(unit->m_b1, b1);
unit->m_lag = lag;
LOOP1(inNumSamples, b1 += b1_slope; double y0a = ZXP(in); y1a = y0a + b1 * (y1a - y0a);
y1b = y1a + b1 * (y1b - y1a); y1c = y1b + b1 * (y1c - y1b); ZXP(out) = y1c;);
}
unit->m_y1a = zapgremlins(y1a);
unit->m_y1b = zapgremlins(y1b);
unit->m_y1c = zapgremlins(y1c);
}
static void Lag3_next_1_i(Lag3* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
double y1a = unit->m_y1a;
double y1b = unit->m_y1b;
double y1c = unit->m_y1c;
double b1 = unit->m_b1;
double y0a = ZXP(in);
y1a = y0a + b1 * (y1a - y0a);
y1b = y1a + b1 * (y1b - y1a);
y1c = y1b + b1 * (y1c - y1b);
ZXP(out) = y1c;
unit->m_y1a = zapgremlins(y1a);
unit->m_y1b = zapgremlins(y1b);
unit->m_y1c = zapgremlins(y1c);
}
void Lag3_Ctor(Lag3* unit) {
switch (INRATE(1)) {
case calc_FullRate:
case calc_BufRate:
SETCALC(Lag3_next);
break;
default:
if (BUFLENGTH == 1)
SETCALC(Lag3_next_1_i);
else
SETCALC(Lag3_next);
break;
}
unit->m_lag = uninitializedControl;
unit->m_b1 = 0.f;
unit->m_y1a = unit->m_y1b = unit->m_y1c = ZIN0(0);
Lag3_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void Lag3UD_next(Lag3UD* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float lagu = ZIN0(1);
float lagd = ZIN0(2);
double y1a = unit->m_y1a;
double y1b = unit->m_y1b;
double y1c = unit->m_y1c;
double b1u = unit->m_b1u;
double b1d = unit->m_b1d;
if ((lagu == unit->m_lagu) && (lagd == unit->m_lagd)) {
LOOP1(
inNumSamples, double y0a = ZXP(in); if (y0a > y1a) { y1a = y0a + b1u * (y1a - y0a); } else {
y1a = y0a + b1d * (y1a - y0a);
} if (y1a > y1b) { y1b = y1a + b1u * (y1b - y1a); } else { y1b = y1a + b1d * (y1b - y1a); } if (y1a > y1b) {
y1c = y1b + b1u * (y1c - y1b);
} else { y1c = y1b + b1d * (y1c - y1b); } ZXP(out) = y1c;);
} else {
unit->m_b1u = lagu == 0.f ? 0.f : exp(log001 / (lagu * unit->mRate->mSampleRate));
double b1u_slope = CALCSLOPE(unit->m_b1u, b1u);
unit->m_lagu = lagu;
unit->m_b1d = lagd == 0.f ? 0.f : exp(log001 / (lagd * unit->mRate->mSampleRate));
double b1d_slope = CALCSLOPE(unit->m_b1d, b1d);
unit->m_lagd = lagd;
LOOP1(
inNumSamples, b1u += b1u_slope; b1d += b1d_slope; double y0a = ZXP(in);
if (y0a > y1a) { y1a = y0a + b1u * (y1a - y0a); } else { y1a = y0a + b1d * (y1a - y0a); } if (y1a > y1b) {
y1b = y1a + b1u * (y1b - y1a);
} else { y1b = y1a + b1d * (y1b - y1a); } if (y1a > y1b) { y1c = y1b + b1u * (y1c - y1b); } else {
y1c = y1b + b1d * (y1c - y1b);
} ZXP(out) = y1c;);
}
unit->m_y1a = zapgremlins(y1a);
unit->m_y1b = zapgremlins(y1b);
unit->m_y1c = zapgremlins(y1c);
}
void Lag3UD_Ctor(Lag3UD* unit) {
SETCALC(Lag3UD_next);
unit->m_lagu = uninitializedControl;
unit->m_lagd = uninitializedControl;
unit->m_b1u = 0.f;
unit->m_b1d = 0.f;
unit->m_y1a = unit->m_y1b = unit->m_y1c = ZIN0(0);
Lag3UD_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void VarLag_next(VarLag* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = IN(0);
float lagTime = ZIN0(1);
double slope = unit->m_slope;
double level = unit->m_level;
int counter = unit->m_counter;
int remain = inNumSamples;
if (*in != unit->m_in) {
counter = (int)(lagTime * SAMPLERATE);
counter = unit->m_counter = sc_max(1, counter);
slope = unit->m_slope = (*in - unit->m_level) / counter;
unit->m_in = *in;
unit->m_lagTime = lagTime;
} else {
if (lagTime != unit->m_lagTime) {
float scaleFactor = lagTime / unit->m_lagTime;
counter = (int)(unit->m_counter * scaleFactor);
counter = unit->m_counter = sc_max(1, counter);
slope = unit->m_slope / scaleFactor;
unit->m_lagTime = lagTime;
}
}
if (counter > 0) {
LOOP(
remain, ZXP(out) = level; if (counter > 0) {
level += slope;
--counter;
} else { level = unit->m_in; };)
} else {
LOOP(remain, ZXP(out) = level);
}
unit->m_level = level;
unit->m_slope = slope;
unit->m_counter = counter;
}
void VarLag_next_1(VarLag* unit, int inNumSamples) {
float* out = OUT(0);
float in = *IN(0);
float lagTime = ZIN0(1);
int counter = unit->m_counter;
if (in != unit->m_in) {
counter = (int)(lagTime * SAMPLERATE);
unit->m_counter = counter = sc_max(1, counter);
unit->m_slope = (in - unit->m_level) / counter;
unit->m_in = in;
unit->m_lagTime = lagTime;
}
{
if (lagTime != unit->m_lagTime) {
if (counter != 0) {
double scaleFactor = lagTime / unit->m_lagTime;
counter = (int)(unit->m_counter * scaleFactor);
unit->m_counter = counter = sc_max(1, counter);
unit->m_slope = unit->m_slope / scaleFactor;
}
unit->m_lagTime = lagTime;
}
}
*out = unit->m_level;
if (unit->m_counter > 0) {
unit->m_level += unit->m_slope;
--unit->m_counter;
} else {
unit->m_level = unit->m_in;
}
}
void VarLag_Ctor(VarLag* unit) {
if (BUFLENGTH == 1) {
SETCALC(VarLag_next_1);
} else {
SETCALC(VarLag_next);
}
float in = *IN(0);
float lagTime = ZIN0(1);
unit->m_level = ZIN0(2);
int counter = (int)(lagTime * SAMPLERATE);
unit->m_counter = counter = sc_max(1, counter);
unit->m_slope = (in - unit->m_level) / counter;
unit->m_in = in;
unit->m_lagTime = lagTime;
ZOUT0(0) = unit->m_level;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void OnePole_next_a(OnePole* unit, int inNumSamples) {
// printf("OnePole_next_a\n");
float* out = ZOUT(0);
float* in = ZIN(0);
float* b1p = ZIN(1);
double y1 = unit->m_y1;
LOOP1(inNumSamples, double y0 = ZXP(in); double b1 = ZXP(b1p); ZXP(out) = y1 = y0 + b1 * (y1 - y0););
unit->m_y1 = zapgremlins(y1);
}
void OnePole_next_k(OnePole* unit, int inNumSamples) {
// printf("OnePole_next_a\n");
float* out = ZOUT(0);
float* in = ZIN(0);
double b1 = unit->m_b1;
unit->m_b1 = ZIN0(1);
double y1 = unit->m_y1;
if (b1 == unit->m_b1) {
if (b1 >= 0.f) {
LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * (y1 - y0););
} else {
LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * (y1 + y0););
}
} else {
double b1_slope = CALCSLOPE(unit->m_b1, b1);
if (b1 >= 0.f && unit->m_b1 >= 0) {
LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * (y1 - y0); b1 += b1_slope;);
} else if (b1 <= 0.f && unit->m_b1 <= 0) {
LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * (y1 + y0); b1 += b1_slope;);
} else {
LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = (1.f - std::abs(b1)) * y0 + b1 * y1;
b1 += b1_slope;);
}
}
unit->m_y1 = zapgremlins(y1);
}
void OnePole_Ctor(OnePole* unit) {
if (INRATE(1) == calc_FullRate) {
SETCALC(OnePole_next_a);
} else {