1 /*
2     SuperCollider real time audio synthesis system
3     Copyright (c) 2002 James McCartney. All rights reserved.
4     http://www.audiosynth.com
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19 */
20 
21 
22 #include "SC_PlugIn.h"
23 
24 #include <limits>
25 
26 // NaNs are not equal to any floating point number
27 static const float uninitializedControl = std::numeric_limits<float>::quiet_NaN();
28 
29 #define PI 3.1415926535898f
30 
31 #define PUSH_LOOPVALS                                                                                                  \
32     int tmp_floops = unit->mRate->mFilterLoops;                                                                        \
33     int tmp_fremain = unit->mRate->mFilterRemain;                                                                      \
34     unit->mRate->mFilterLoops = 0;                                                                                     \
35     unit->mRate->mFilterRemain = 1;
36 
37 #define POP_LOOPVALS                                                                                                   \
38     unit->mRate->mFilterLoops = tmp_floops;                                                                            \
39     unit->mRate->mFilterRemain = tmp_fremain;
40 
41 using namespace std; // for math functions
42 
43 static InterfaceTable* ft;
44 
45 struct Ramp : public Unit {
46     double m_level, m_slope;
47     int m_counter;
48 };
49 
50 struct Lag : public Unit {
51     float m_lag;
52     double m_b1, m_y1;
53 };
54 
55 struct Lag2 : public Unit {
56     float m_lag;
57     double m_b1, m_y1a, m_y1b;
58 };
59 
60 struct Lag3 : public Unit {
61     float m_lag;
62     double m_b1, m_y1a, m_y1b, m_y1c;
63 };
64 
65 struct LagUD : public Unit {
66     float m_lagu, m_lagd;
67     double m_b1u, m_b1d, m_y1;
68 };
69 
70 struct Lag2UD : public Unit {
71     float m_lagu, m_lagd;
72     double m_b1u, m_b1d, m_y1a, m_y1b;
73 };
74 
75 struct Lag3UD : public Unit {
76     float m_lagu, m_lagd;
77     double m_b1u, m_b1d, m_y1a, m_y1b, m_y1c;
78 };
79 
80 struct VarLag : public Unit {
81     double m_level, m_slope;
82     int m_counter;
83     float m_in, m_lagTime;
84 };
85 
86 struct OnePole : public Unit {
87     double m_b1, m_y1;
88 };
89 
90 struct OneZero : public Unit {
91     double m_b1, m_x1;
92 };
93 
94 struct Integrator : public Unit {
95     double m_b1, m_y1;
96 };
97 
98 struct Decay : public Unit {
99     float m_decayTime;
100     double m_y1, m_b1;
101 };
102 
103 struct Decay2 : public Unit {
104     float m_attackTime;
105     double m_y1a, m_b1a;
106     float m_decayTime;
107     double m_y1b, m_b1b;
108 };
109 
110 struct LeakDC : public Unit {
111     double m_b1, m_x1, m_y1;
112 };
113 
114 struct TwoPole : public Unit {
115     float m_freq, m_reson;
116     double m_y1, m_y2, m_b1, m_b2;
117 };
118 
119 struct APF : public Unit {
120     float m_freq, m_reson;
121     double m_y1, m_y2, m_x1, m_x2, m_b1, m_b2;
122 };
123 
124 struct TwoZero : public Unit {
125     float m_freq, m_reson;
126     double m_x1, m_x2, m_b1, m_b2;
127 };
128 
129 struct LPZ1 : public Unit {
130     double m_x1;
131 };
132 
133 struct HPZ1 : public Unit {
134     double m_x1;
135 };
136 
137 struct HPZ2 : public Unit {
138     double m_x1, m_x2;
139 };
140 
141 struct BPZ2 : public Unit {
142     double m_x1, m_x2;
143 };
144 
145 struct BRZ2 : public Unit {
146     double m_x1, m_x2;
147 };
148 
149 struct LPZ2 : public Unit {
150     double m_x1, m_x2;
151 };
152 
153 struct Flip : public Unit {};
154 
155 struct Delay1 : public Unit {
156     float m_x1;
157 };
158 
159 struct Delay2 : public Unit {
160     float m_x1, m_x2;
161 };
162 
163 struct Slope : public Unit {
164     double m_x1;
165 };
166 
167 struct Slew : public Unit {
168     double mLevel;
169 };
170 
171 struct RLPF : public Unit {
172     float m_freq, m_reson;
173     double m_y1, m_y2, m_a0, m_b1, m_b2;
174 };
175 
176 struct RHPF : public Unit {
177     float m_freq, m_reson;
178     double m_y1, m_y2, m_a0, m_b1, m_b2;
179 };
180 
181 struct LPF : public Unit {
182     float m_freq;
183     double m_y1, m_y2, m_a0, m_b1, m_b2;
184 };
185 
186 struct HPF : public Unit {
187     float m_freq;
188     double m_y1, m_y2, m_a0, m_b1, m_b2;
189 };
190 
191 struct BPF : public Unit {
192     float m_freq, m_bw;
193     double m_y1, m_y2, m_a0, m_b1, m_b2;
194 };
195 
196 struct BRF : public Unit {
197     float m_freq, m_bw;
198     double m_y1, m_y2, m_a0, m_a1, m_b2;
199 };
200 
201 struct MidEQ : public Unit {
202     float m_freq, m_bw, m_db;
203     double m_y1, m_y2, m_a0, m_b1, m_b2;
204 };
205 
206 struct Resonz : public Unit {
207     float m_freq, m_rq;
208     double m_y1, m_y2, m_a0, m_b1, m_b2;
209 };
210 
211 struct Ringz : public Unit {
212     float m_freq, m_decayTime;
213     double m_y1, m_y2, m_b1, m_b2;
214 };
215 
216 struct FOS : public Unit {
217     double m_y1, m_a0, m_a1, m_b1;
218 };
219 
220 struct SOS : public Unit {
221     double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
222 };
223 
224 struct Formlet : public Unit {
225     float m_freq, m_decayTime, m_attackTime;
226     double m_y01, m_y02, m_b01, m_b02;
227     double m_y11, m_y12, m_b11, m_b12;
228 };
229 
230 const int kMAXMEDIANSIZE = 32;
231 
232 struct Median : public Unit {
233     float m_medianValue[kMAXMEDIANSIZE];
234     long m_medianAge[kMAXMEDIANSIZE];
235     long m_medianSize, m_medianIndex;
236 };
237 
238 struct Compander : public Unit {
239     float m_prevmaxval, m_gain, m_clamp, m_clampcoef, m_relax, m_relaxcoef;
240 };
241 
242 struct Normalizer : public Unit {
243     float *m_table, *m_xinbuf, *m_xoutbuf, *m_xmidbuf;
244     long m_flips, m_pos, m_bufsize;
245     float m_slope, m_level, m_curmaxval, m_prevmaxval, m_slopefactor;
246 };
247 
248 struct Limiter : public Unit {
249     float *m_table, *m_xinbuf, *m_xoutbuf, *m_xmidbuf;
250     long m_flips, m_pos, m_bufsize;
251     float m_slope, m_level, m_curmaxval, m_prevmaxval, m_slopefactor;
252 };
253 
254 
255 struct Amplitude : public Unit {
256     float m_previn, m_clampcoef, m_relaxcoef, m_clamp_in, m_relax_in;
257 };
258 
259 struct DetectSilence : public Unit {
260     float mThresh;
261     int32 mCounter, mEndCounter;
262 };
263 
264 struct Hilbert : public Unit {
265     double m_coefs[12];
266     double m_y1[12];
267 };
268 
269 struct FreqShift : public Unit {
270     float m_coefs[12];
271     float m_y1[12];
272     int32 m_phase;
273     int32 m_phaseoffset, m_lomask;
274     double m_cpstoinc, m_radtoinc, m_phasein;
275 };
276 
277 struct MoogFF : public Unit {
278     float m_freq, m_k;
279     double m_b0, m_a1; // Resonant freq and corresponding vals; stored because we need to compare against prev vals
280 
281     double m_s1, m_s2, m_s3, m_s4; // 1st order filter states
282 };
283 
284 //////////////////////////////////////////////////////////////////////////////////////////////////
285 
286 
287 extern "C" {
288 
289 void Ramp_next(Ramp* unit, int inNumSamples);
290 void Ramp_next_1(Ramp* unit, int inNumSamples);
291 void Ramp_Ctor(Ramp* unit);
292 
293 void Lag_next(Lag* unit, int inNumSamples);
294 void Lag_Ctor(Lag* unit);
295 
296 void Lag2_Ctor(Lag2* unit);
297 
298 void Lag3_next(Lag3* unit, int inNumSamples);
299 void Lag3_Ctor(Lag3* unit);
300 
301 void LagUD_next(LagUD* unit, int inNumSamples);
302 void LagUD_Ctor(LagUD* unit);
303 
304 void Lag2UD_next(Lag2UD* unit, int inNumSamples);
305 void Lag2UD_Ctor(Lag2UD* unit);
306 
307 void Lag3UD_next(Lag3UD* unit, int inNumSamples);
308 void Lag3UD_Ctor(Lag3UD* unit);
309 
310 void VarLag_next(VarLag* unit, int inNumSamples);
311 void VarLag_Ctor(VarLag* unit);
312 
313 void OnePole_next_a(OnePole* unit, int inNumSamples);
314 void OnePole_next_k(OnePole* unit, int inNumSamples);
315 void OnePole_Ctor(OnePole* unit);
316 
317 void OneZero_next(OneZero* unit, int inNumSamples);
318 void OneZero_Ctor(OneZero* unit);
319 
320 void Integrator_next(Integrator* unit, int inNumSamples);
321 void Integrator_next_i(Integrator* unit, int inNumSamples);
322 void Integrator_Ctor(Integrator* unit);
323 
324 void Decay_next(Decay* unit, int inNumSamples);
325 void Decay_Ctor(Decay* unit);
326 
327 void Decay2_next(Decay2* unit, int inNumSamples);
328 void Decay2_Ctor(Decay2* unit);
329 
330 void LeakDC_next(LeakDC* unit, int inNumSamples);
331 void LeakDC_next_1(LeakDC* unit, int inNumSamples);
332 void LeakDC_Ctor(LeakDC* unit);
333 
334 void TwoPole_next(TwoPole* unit, int inNumSamples);
335 void TwoPole_Ctor(TwoPole* unit);
336 
337 void TwoZero_next(TwoZero* unit, int inNumSamples);
338 void TwoZero_Ctor(TwoZero* unit);
339 
340 void APF_next(APF* unit, int inNumSamples);
341 void APF_Ctor(APF* unit);
342 
343 void LPZ1_next(LPZ1* unit, int inNumSamples);
344 void LPZ1_Ctor(LPZ1* unit);
345 
346 void HPZ1_next(HPZ1* unit, int inNumSamples);
347 void HPZ1_Ctor(HPZ1* unit);
348 
349 void Slope_next(Slope* unit, int inNumSamples);
350 void Slope_Ctor(Slope* unit);
351 
352 void Delay1_next(Delay1* unit, int inNumSamples);
353 void Delay1_Ctor(Delay1* unit);
354 
355 void Flip_Ctor(Flip* unit);
356 void Flip_next_even(Flip* unit, int inNumSamples);
357 void Flip_next_odd(Flip* unit, int inNumSamples);
358 
359 void Delay2_next(Delay2* unit, int inNumSamples);
360 void Delay2_Ctor(Delay2* unit);
361 
362 void LPZ2_next(LPZ2* unit, int inNumSamples);
363 void LPZ2_Ctor(LPZ2* unit);
364 
365 void HPZ2_next(HPZ2* unit, int inNumSamples);
366 void HPZ2_Ctor(HPZ2* unit);
367 
368 void BPZ2_next(BPZ2* unit, int inNumSamples);
369 void BPZ2_Ctor(BPZ2* unit);
370 
371 void BRZ2_next(BRZ2* unit, int inNumSamples);
372 void BRZ2_Ctor(BRZ2* unit);
373 
374 void Slew_next(Slew* unit, int inNumSamples);
375 void Slew_Ctor(Slew* unit);
376 
377 void RLPF_next(RLPF* unit, int inNumSamples);
378 void RLPF_next_1(RLPF* unit, int inNumSamples);
379 void RLPF_Ctor(RLPF* unit);
380 
381 void RHPF_next(RHPF* unit, int inNumSamples);
382 void RHPF_next_1(RHPF* unit, int inNumSamples);
383 void RHPF_Ctor(RHPF* unit);
384 
385 void LPF_next(LPF* unit, int inNumSamples);
386 void LPF_next_1(LPF* unit, int inNumSamples);
387 void LPF_Ctor(LPF* unit);
388 
389 void HPF_next(HPF* unit, int inNumSamples);
390 void HPF_next_1(HPF* unit, int inNumSamples);
391 void HPF_Ctor(HPF* unit);
392 
393 void BPF_next(BPF* unit, int inNumSamples);
394 void BPF_next_1(BPF* unit, int inNumSamples);
395 void BPF_Ctor(BPF* unit);
396 
397 void BRF_next(BRF* unit, int inNumSamples);
398 void BRF_next_1(BRF* unit, int inNumSamples);
399 void BRF_Ctor(BRF* unit);
400 
401 void Median_next(Median* unit, int inNumSamples);
402 void Median_Ctor(Median* unit);
403 
404 void MidEQ_next(MidEQ* unit, int inNumSamples);
405 void MidEQ_Ctor(MidEQ* unit);
406 
407 void Resonz_next(Resonz* unit, int inNumSamples);
408 void Resonz_Ctor(Resonz* unit);
409 
410 void Ringz_next(Ringz* unit, int inNumSamples);
411 void Ringz_Ctor(Ringz* unit);
412 
413 void Formlet_next(Formlet* unit, int inNumSamples);
414 void Formlet_next_1(Formlet* unit, int inNumSamples);
415 void Formlet_Ctor(Formlet* unit);
416 
417 void FOS_next_k(FOS* unit, int inNumSamples);
418 void FOS_next_a(FOS* unit, int inNumSamples);
419 void FOS_next_1(FOS* unit, int inNumSamples);
420 void FOS_Ctor(FOS* unit);
421 
422 void SOS_next_i(SOS* unit, int inNumSamples);
423 void SOS_next_k(SOS* unit, int inNumSamples);
424 void SOS_next_a(SOS* unit, int inNumSamples);
425 void SOS_next_1(SOS* unit, int inNumSamples);
426 void SOS_Ctor(SOS* unit);
427 
428 void Normalizer_next(Normalizer* unit, int inNumSamples);
429 void Normalizer_Ctor(Normalizer* unit);
430 void Normalizer_Dtor(Normalizer* unit);
431 
432 void Limiter_next(Limiter* unit, int inNumSamples);
433 void Limiter_Ctor(Limiter* unit);
434 void Limiter_Dtor(Limiter* unit);
435 
436 void Compander_next(Compander* unit, int inNumSamples);
437 void Compander_Ctor(Compander* unit);
438 
439 void Amplitude_next(Amplitude* unit, int inNumSamples);
440 void Amplitude_next_kk(Amplitude* unit, int inNumSamples);
441 void Amplitude_next_atok(Amplitude* unit, int inNumSamples);
442 void Amplitude_next_atok_kk(Amplitude* unit, int inNumSamples);
443 void Amplitude_Ctor(Amplitude* unit);
444 
445 void DetectSilence_next(DetectSilence* unit, int inNumSamples);
446 void DetectSilence_next_k(DetectSilence* unit, int inNumSamples);
447 // void DetectSilence_done(DetectSilence *unit, int inNumSamples);
448 void DetectSilence_Ctor(DetectSilence* unit);
449 
450 void Hilbert_Ctor(Hilbert* unit);
451 void Hilbert_next(Hilbert* unit, int inNumSamples);
452 
453 void FreqShift_Ctor(FreqShift* unit);
454 void FreqShift_next_kk(FreqShift* unit, int inNumSamples);
455 void FreqShift_next_aa(FreqShift* unit, int inNumSamples);
456 void FreqShift_next_ak(FreqShift* unit, int inNumSamples);
457 void FreqShift_next_ka(FreqShift* unit, int inNumSamples);
458 
459 void MoogFF_next(MoogFF* unit, int inNumSamples);
460 void MoogFF_Ctor(MoogFF* unit);
461 
462 /*
463     void Lag_next(Lag *unit, int inNumSamples);
464     void Lag_Ctor(Lag* unit);
465 
466     void Lag_next(Lag *unit, int inNumSamples);
467     void Lag_Ctor(Lag* unit);
468 */
469 }
470 
471 //////////////////////////////////////////////////////////////////////////////////////////////////
472 
Ramp_next(Ramp * unit,int inNumSamples)473 void Ramp_next(Ramp* unit, int inNumSamples) {
474     float* out = ZOUT(0);
475     float* in = IN(0);
476     float period = ZIN0(1);
477 
478     double slope = unit->m_slope;
479     double level = unit->m_level;
480     int counter = unit->m_counter;
481     int remain = inNumSamples;
482     while (remain) {
483         int nsmps = sc_min(remain, counter);
484         LOOP(nsmps, ZXP(out) = level; level += slope;);
485         in += nsmps;
486         counter -= nsmps;
487         remain -= nsmps;
488         if (counter <= 0) {
489             counter = (int)(period * SAMPLERATE);
490             counter = sc_max(1, counter);
491             slope = (*in - level) / counter;
492         }
493     }
494     unit->m_level = level;
495     unit->m_slope = slope;
496     unit->m_counter = counter;
497 }
498 
Ramp_next_1(Ramp * unit,int inNumSamples)499 void Ramp_next_1(Ramp* unit, int inNumSamples) {
500     float* out = OUT(0);
501 
502     *out = unit->m_level;
503     unit->m_level += unit->m_slope;
504     if (--unit->m_counter <= 0) {
505         float in = ZIN0(0);
506         float period = ZIN0(1);
507         int counter = (int)(period * SAMPLERATE);
508         unit->m_counter = counter = sc_max(1, counter);
509         unit->m_slope = (in - unit->m_level) / counter;
510     }
511 }
512 
Ramp_Ctor(Ramp * unit)513 void Ramp_Ctor(Ramp* unit) {
514     if (BUFLENGTH == 1) {
515         SETCALC(Ramp_next_1);
516     } else {
517         SETCALC(Ramp_next);
518     }
519 
520     unit->m_counter = 1;
521     unit->m_level = ZIN0(0);
522     unit->m_slope = 0.f;
523     ZOUT0(0) = unit->m_level;
524 }
525 
526 //////////////////////////////////////////////////////////////////////////////////////////////////
527 
Lag_next(Lag * unit,int inNumSamples)528 void Lag_next(Lag* unit, int inNumSamples) {
529     float* out = ZOUT(0);
530     float* in = ZIN(0);
531     float lag = ZIN0(1);
532 
533     double y1 = unit->m_y1;
534     double b1 = unit->m_b1;
535 
536     if (lag == unit->m_lag) {
537         LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * (y1 - y0););
538     } else {
539         unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
540         double b1_slope = CALCSLOPE(unit->m_b1, b1);
541         unit->m_lag = lag;
542         LOOP1(inNumSamples, b1 += b1_slope; double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * (y1 - y0););
543     }
544     unit->m_y1 = zapgremlins(y1);
545 }
546 
Lag_next_1(Lag * unit,int inNumSamples)547 void Lag_next_1(Lag* unit, int inNumSamples) {
548     float* out = OUT(0);
549     float* in = IN(0);
550     float lag = IN0(1);
551 
552     double y1 = unit->m_y1;
553     double b1 = unit->m_b1;
554 
555     if (lag == unit->m_lag) {
556         double y0 = *in;
557         *out = y1 = y0 + b1 * (y1 - y0);
558     } else {
559         unit->m_b1 = b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
560         unit->m_lag = lag;
561         double y0 = *in;
562         *out = y1 = y0 + b1 * (y1 - y0);
563     }
564     unit->m_y1 = zapgremlins(y1);
565 }
566 
Lag_Ctor(Lag * unit)567 void Lag_Ctor(Lag* unit) {
568     if (BUFLENGTH == 1)
569         SETCALC(Lag_next_1);
570     else
571         SETCALC(Lag_next);
572 
573     unit->m_lag = uninitializedControl;
574     unit->m_b1 = 0.f;
575     unit->m_y1 = ZIN0(0);
576     Lag_next(unit, 1);
577 }
578 
579 //////////////////////////////////////////////////////////////////////////////////////////////////
LagUD_next(LagUD * unit,int inNumSamples)580 void LagUD_next(LagUD* unit, int inNumSamples) {
581     float* out = ZOUT(0);
582     float* in = ZIN(0);
583     float lagu = ZIN0(1);
584     float lagd = ZIN0(2);
585 
586     double y1 = unit->m_y1;
587     double b1u = unit->m_b1u;
588     double b1d = unit->m_b1d;
589 
590     if ((lagu == unit->m_lagu) && (lagd == unit->m_lagd)) {
591         LOOP1(inNumSamples, double y0 = ZXP(in); if (y0 > y1) ZXP(out) = y1 = y0 + b1u * (y1 - y0);
592               else ZXP(out) = y1 = y0 + b1d * (y1 - y0););
593     } else {
594         unit->m_b1u = lagu == 0.f ? 0.f : exp(log001 / (lagu * unit->mRate->mSampleRate));
595         double b1u_slope = CALCSLOPE(unit->m_b1u, b1u);
596         unit->m_lagu = lagu;
597         unit->m_b1d = lagd == 0.f ? 0.f : exp(log001 / (lagd * unit->mRate->mSampleRate));
598         double b1d_slope = CALCSLOPE(unit->m_b1d, b1d);
599         unit->m_lagd = lagd;
600         LOOP1(inNumSamples, b1u += b1u_slope; b1d += b1d_slope; double y0 = ZXP(in);
601               if (y0 > y1) ZXP(out) = y1 = y0 + b1u * (y1 - y0); else ZXP(out) = y1 = y0 + b1d * (y1 - y0););
602     }
603     unit->m_y1 = zapgremlins(y1);
604 }
605 
LagUD_Ctor(LagUD * unit)606 void LagUD_Ctor(LagUD* unit) {
607     SETCALC(LagUD_next);
608 
609     unit->m_lagu = uninitializedControl;
610     unit->m_lagd = uninitializedControl;
611     unit->m_b1u = 0.f;
612     unit->m_b1d = 0.f;
613     unit->m_y1 = ZIN0(0);
614     LagUD_next(unit, 1);
615 }
616 
617 //////////////////////////////////////////////////////////////////////////////////////////////////
618 
Lag2_next_k(Lag2 * unit,int inNumSamples)619 static void Lag2_next_k(Lag2* unit, int inNumSamples) {
620     float* out = ZOUT(0);
621     float* in = ZIN(0);
622     float lag = ZIN0(1);
623 
624     double y1a = unit->m_y1a;
625     double y1b = unit->m_y1b;
626     double b1 = unit->m_b1;
627 
628     if (lag == unit->m_lag) {
629         LOOP1(inNumSamples, double y0a = ZXP(in); y1a = y0a + b1 * (y1a - y0a); y1b = y1a + b1 * (y1b - y1a);
630               ZXP(out) = y1b;);
631     } else {
632         unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
633         double b1_slope = CALCSLOPE(unit->m_b1, b1);
634         unit->m_lag = lag;
635         LOOP1(inNumSamples, b1 += b1_slope; double y0a = ZXP(in); y1a = y0a + b1 * (y1a - y0a);
636               y1b = y1a + b1 * (y1b - y1a); ZXP(out) = y1b;);
637     }
638     unit->m_y1a = zapgremlins(y1a);
639     unit->m_y1b = zapgremlins(y1b);
640 }
641 
Lag2_next_i(Lag2 * unit,int inNumSamples)642 static void Lag2_next_i(Lag2* unit, int inNumSamples) {
643     float* out = ZOUT(0);
644     float* in = ZIN(0);
645 
646     double y1a = unit->m_y1a;
647     double y1b = unit->m_y1b;
648     double b1 = unit->m_b1;
649 
650     LOOP1(inNumSamples, double y0a = ZXP(in); y1a = y0a + b1 * (y1a - y0a); y1b = y1a + b1 * (y1b - y1a);
651           ZXP(out) = y1b;);
652     unit->m_y1a = zapgremlins(y1a);
653     unit->m_y1b = zapgremlins(y1b);
654 }
655 
Lag2_next_1_i(Lag2 * unit,int inNumSamples)656 static void Lag2_next_1_i(Lag2* unit, int inNumSamples) {
657     double y1a = unit->m_y1a;
658     double y1b = unit->m_y1b;
659     double b1 = unit->m_b1;
660 
661     float y0a = ZIN0(0);
662     y1a = y0a + b1 * (y1a - y0a);
663     y1b = y1a + b1 * (y1b - y1a);
664     ZOUT0(0) = y1b;
665 
666     unit->m_y1a = zapgremlins(y1a);
667     unit->m_y1b = zapgremlins(y1b);
668 }
669 
Lag2_Ctor(Lag2 * unit)670 void Lag2_Ctor(Lag2* unit) {
671     switch (INRATE(1)) {
672     case calc_FullRate:
673     case calc_BufRate:
674         SETCALC(Lag2_next_k);
675         break;
676 
677     default:
678         if (BUFLENGTH == 1)
679             SETCALC(Lag2_next_1_i);
680         else
681             SETCALC(Lag2_next_i);
682         break;
683     }
684 
685     unit->m_lag = uninitializedControl;
686     unit->m_b1 = 0.f;
687     unit->m_y1a = unit->m_y1b = ZIN0(0);
688     Lag2_next_k(unit, 1);
689 }
690 
691 //////////////////////////////////////////////////////////////////////////////////////////////////
692 
Lag2UD_next(Lag2UD * unit,int inNumSamples)693 void Lag2UD_next(Lag2UD* unit, int inNumSamples) {
694     float* out = ZOUT(0);
695     float* in = ZIN(0);
696     float lagu = ZIN0(1);
697     float lagd = ZIN0(2);
698 
699     double y1a = unit->m_y1a;
700     double y1b = unit->m_y1b;
701     double b1u = unit->m_b1u;
702     double b1d = unit->m_b1d;
703 
704     if ((lagu == unit->m_lagu) && (lagd == unit->m_lagd)) {
705         LOOP1(
706             inNumSamples, double y0a = ZXP(in); if (y0a > y1a) { y1a = y0a + b1u * (y1a - y0a); } else {
707                 y1a = y0a + b1d * (y1a - y0a);
708             } if (y1a > y1b) y1b = y1a + b1u * (y1b - y1a);
709             else y1b = y1a + b1d * (y1b - y1a); ZXP(out) = y1b;);
710     } else {
711         unit->m_b1u = lagu == 0.f ? 0.f : exp(log001 / (lagu * unit->mRate->mSampleRate));
712         double b1u_slope = CALCSLOPE(unit->m_b1u, b1u);
713         unit->m_lagu = lagu;
714         unit->m_b1d = lagd == 0.f ? 0.f : exp(log001 / (lagd * unit->mRate->mSampleRate));
715         double b1d_slope = CALCSLOPE(unit->m_b1d, b1d);
716         unit->m_lagd = lagd;
717         LOOP1(
718             inNumSamples, b1u += b1u_slope; b1d += b1d_slope; double y0a = ZXP(in); if (y0a > y1a) {
719                 y1a = y0a + b1u * (y1a - y0a);
720             } else { y1a = y0a + b1d * (y1a - y0a); } if (y1a > y1b) y1b = y1a + b1u * (y1b - y1a);
721             else y1b = y1a + b1d * (y1b - y1a); ZXP(out) = y1b;);
722     }
723     unit->m_y1a = zapgremlins(y1a);
724     unit->m_y1b = zapgremlins(y1b);
725 }
726 
Lag2UD_Ctor(Lag2UD * unit)727 void Lag2UD_Ctor(Lag2UD* unit) {
728     SETCALC(Lag2UD_next);
729 
730     unit->m_lagu = 0.f;
731     unit->m_lagd = 0.f;
732     unit->m_b1u = 0.f;
733     unit->m_b1d = 0.f;
734     unit->m_y1a = unit->m_y1b = ZIN0(0);
735     Lag2UD_next(unit, 1);
736 }
737 
738 //////////////////////////////////////////////////////////////////////////////////////////////////
739 
Lag3_next(Lag3 * unit,int inNumSamples)740 void Lag3_next(Lag3* unit, int inNumSamples) {
741     float* out = ZOUT(0);
742     float* in = ZIN(0);
743     float lag = ZIN0(1);
744 
745     double y1a = unit->m_y1a;
746     double y1b = unit->m_y1b;
747     double y1c = unit->m_y1c;
748     double b1 = unit->m_b1;
749 
750     if (lag == unit->m_lag) {
751         LOOP1(inNumSamples, double y0a = ZXP(in); y1a = y0a + b1 * (y1a - y0a); y1b = y1a + b1 * (y1b - y1a);
752               y1c = y1b + b1 * (y1c - y1b); ZXP(out) = y1c;);
753     } else {
754         unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
755         double b1_slope = CALCSLOPE(unit->m_b1, b1);
756         unit->m_lag = lag;
757         LOOP1(inNumSamples, b1 += b1_slope; double y0a = ZXP(in); y1a = y0a + b1 * (y1a - y0a);
758               y1b = y1a + b1 * (y1b - y1a); y1c = y1b + b1 * (y1c - y1b); ZXP(out) = y1c;);
759     }
760     unit->m_y1a = zapgremlins(y1a);
761     unit->m_y1b = zapgremlins(y1b);
762     unit->m_y1c = zapgremlins(y1c);
763 }
764 
Lag3_next_1_i(Lag3 * unit,int inNumSamples)765 static void Lag3_next_1_i(Lag3* unit, int inNumSamples) {
766     float* out = ZOUT(0);
767     float* in = ZIN(0);
768 
769     double y1a = unit->m_y1a;
770     double y1b = unit->m_y1b;
771     double y1c = unit->m_y1c;
772     double b1 = unit->m_b1;
773 
774     double y0a = ZXP(in);
775     y1a = y0a + b1 * (y1a - y0a);
776     y1b = y1a + b1 * (y1b - y1a);
777     y1c = y1b + b1 * (y1c - y1b);
778     ZXP(out) = y1c;
779 
780     unit->m_y1a = zapgremlins(y1a);
781     unit->m_y1b = zapgremlins(y1b);
782     unit->m_y1c = zapgremlins(y1c);
783 }
784 
Lag3_Ctor(Lag3 * unit)785 void Lag3_Ctor(Lag3* unit) {
786     switch (INRATE(1)) {
787     case calc_FullRate:
788     case calc_BufRate:
789         SETCALC(Lag3_next);
790         break;
791 
792     default:
793         if (BUFLENGTH == 1)
794             SETCALC(Lag3_next_1_i);
795         else
796             SETCALC(Lag3_next);
797         break;
798     }
799 
800     unit->m_lag = uninitializedControl;
801     unit->m_b1 = 0.f;
802     unit->m_y1a = unit->m_y1b = unit->m_y1c = ZIN0(0);
803     Lag3_next(unit, 1);
804 }
805 
806 //////////////////////////////////////////////////////////////////////////////////////////////////
807 
Lag3UD_next(Lag3UD * unit,int inNumSamples)808 void Lag3UD_next(Lag3UD* unit, int inNumSamples) {
809     float* out = ZOUT(0);
810     float* in = ZIN(0);
811     float lagu = ZIN0(1);
812     float lagd = ZIN0(2);
813 
814     double y1a = unit->m_y1a;
815     double y1b = unit->m_y1b;
816     double y1c = unit->m_y1c;
817     double b1u = unit->m_b1u;
818     double b1d = unit->m_b1d;
819 
820     if ((lagu == unit->m_lagu) && (lagd == unit->m_lagd)) {
821         LOOP1(
822             inNumSamples, double y0a = ZXP(in); if (y0a > y1a) { y1a = y0a + b1u * (y1a - y0a); } else {
823                 y1a = y0a + b1d * (y1a - y0a);
824             } if (y1a > y1b) { y1b = y1a + b1u * (y1b - y1a); } else { y1b = y1a + b1d * (y1b - y1a); } if (y1a > y1b) {
825                 y1c = y1b + b1u * (y1c - y1b);
826             } else { y1c = y1b + b1d * (y1c - y1b); } ZXP(out) = y1c;);
827     } else {
828         unit->m_b1u = lagu == 0.f ? 0.f : exp(log001 / (lagu * unit->mRate->mSampleRate));
829         double b1u_slope = CALCSLOPE(unit->m_b1u, b1u);
830         unit->m_lagu = lagu;
831         unit->m_b1d = lagd == 0.f ? 0.f : exp(log001 / (lagd * unit->mRate->mSampleRate));
832         double b1d_slope = CALCSLOPE(unit->m_b1d, b1d);
833         unit->m_lagd = lagd;
834         LOOP1(
835             inNumSamples, b1u += b1u_slope; b1d += b1d_slope; double y0a = ZXP(in);
836             if (y0a > y1a) { y1a = y0a + b1u * (y1a - y0a); } else { y1a = y0a + b1d * (y1a - y0a); } if (y1a > y1b) {
837                 y1b = y1a + b1u * (y1b - y1a);
838             } else { y1b = y1a + b1d * (y1b - y1a); } if (y1a > y1b) { y1c = y1b + b1u * (y1c - y1b); } else {
839                 y1c = y1b + b1d * (y1c - y1b);
840             } ZXP(out) = y1c;);
841     }
842     unit->m_y1a = zapgremlins(y1a);
843     unit->m_y1b = zapgremlins(y1b);
844     unit->m_y1c = zapgremlins(y1c);
845 }
846 
Lag3UD_Ctor(Lag3UD * unit)847 void Lag3UD_Ctor(Lag3UD* unit) {
848     SETCALC(Lag3UD_next);
849 
850     unit->m_lagu = uninitializedControl;
851     unit->m_lagd = uninitializedControl;
852     unit->m_b1u = 0.f;
853     unit->m_b1d = 0.f;
854 
855     unit->m_y1a = unit->m_y1b = unit->m_y1c = ZIN0(0);
856     Lag3UD_next(unit, 1);
857 }
858 
859 //////////////////////////////////////////////////////////////////////////////////////////////////
860 
VarLag_next(VarLag * unit,int inNumSamples)861 void VarLag_next(VarLag* unit, int inNumSamples) {
862     float* out = ZOUT(0);
863     float* in = IN(0);
864     float lagTime = ZIN0(1);
865     double slope = unit->m_slope;
866     double level = unit->m_level;
867     int counter = unit->m_counter;
868     int remain = inNumSamples;
869 
870     if (*in != unit->m_in) {
871         counter = (int)(lagTime * SAMPLERATE);
872         counter = unit->m_counter = sc_max(1, counter);
873         slope = unit->m_slope = (*in - unit->m_level) / counter;
874         unit->m_in = *in;
875         unit->m_lagTime = lagTime;
876     } else {
877         if (lagTime != unit->m_lagTime) {
878             float scaleFactor = lagTime / unit->m_lagTime;
879             counter = (int)(unit->m_counter * scaleFactor);
880             counter = unit->m_counter = sc_max(1, counter);
881             slope = unit->m_slope / scaleFactor;
882             unit->m_lagTime = lagTime;
883         }
884     }
885     if (counter > 0) {
886         LOOP(
887             remain, ZXP(out) = level; if (counter > 0) {
888                 level += slope;
889                 --counter;
890             } else { level = unit->m_in; };)
891     } else {
892         LOOP(remain, ZXP(out) = level);
893     }
894 
895     unit->m_level = level;
896     unit->m_slope = slope;
897     unit->m_counter = counter;
898 }
899 
900 
VarLag_next_1(VarLag * unit,int inNumSamples)901 void VarLag_next_1(VarLag* unit, int inNumSamples) {
902     float* out = OUT(0);
903     float in = *IN(0);
904     float lagTime = ZIN0(1);
905     int counter = unit->m_counter;
906     if (in != unit->m_in) {
907         counter = (int)(lagTime * SAMPLERATE);
908         unit->m_counter = counter = sc_max(1, counter);
909         unit->m_slope = (in - unit->m_level) / counter;
910         unit->m_in = in;
911         unit->m_lagTime = lagTime;
912     }
913     {
914         if (lagTime != unit->m_lagTime) {
915             if (counter != 0) {
916                 double scaleFactor = lagTime / unit->m_lagTime;
917                 counter = (int)(unit->m_counter * scaleFactor);
918                 unit->m_counter = counter = sc_max(1, counter);
919                 unit->m_slope = unit->m_slope / scaleFactor;
920             }
921             unit->m_lagTime = lagTime;
922         }
923     }
924     *out = unit->m_level;
925 
926     if (unit->m_counter > 0) {
927         unit->m_level += unit->m_slope;
928         --unit->m_counter;
929     } else {
930         unit->m_level = unit->m_in;
931     }
932 }
933 
VarLag_Ctor(VarLag * unit)934 void VarLag_Ctor(VarLag* unit) {
935     if (BUFLENGTH == 1) {
936         SETCALC(VarLag_next_1);
937     } else {
938         SETCALC(VarLag_next);
939     }
940     float in = *IN(0);
941     float lagTime = ZIN0(1);
942     unit->m_level = ZIN0(2);
943     int counter = (int)(lagTime * SAMPLERATE);
944     unit->m_counter = counter = sc_max(1, counter);
945     unit->m_slope = (in - unit->m_level) / counter;
946     unit->m_in = in;
947     unit->m_lagTime = lagTime;
948     ZOUT0(0) = unit->m_level;
949 }
950 
951 
952 //////////////////////////////////////////////////////////////////////////////////////////////////
953 
OnePole_next_a(OnePole * unit,int inNumSamples)954 void OnePole_next_a(OnePole* unit, int inNumSamples) {
955     // printf("OnePole_next_a\n");
956 
957     float* out = ZOUT(0);
958     float* in = ZIN(0);
959     float* b1p = ZIN(1);
960 
961     double y1 = unit->m_y1;
962 
963     LOOP1(inNumSamples, double y0 = ZXP(in); double b1 = ZXP(b1p); ZXP(out) = y1 = y0 + b1 * (y1 - y0););
964     unit->m_y1 = zapgremlins(y1);
965 }
966 
OnePole_next_k(OnePole * unit,int inNumSamples)967 void OnePole_next_k(OnePole* unit, int inNumSamples) {
968     // printf("OnePole_next_a\n");
969 
970     float* out = ZOUT(0);
971     float* in = ZIN(0);
972     double b1 = unit->m_b1;
973     unit->m_b1 = ZIN0(1);
974 
975     double y1 = unit->m_y1;
976 
977     if (b1 == unit->m_b1) {
978         if (b1 >= 0.f) {
979             LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * (y1 - y0););
980         } else {
981             LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * (y1 + y0););
982         }
983     } else {
984         double b1_slope = CALCSLOPE(unit->m_b1, b1);
985         if (b1 >= 0.f && unit->m_b1 >= 0) {
986             LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * (y1 - y0); b1 += b1_slope;);
987         } else if (b1 <= 0.f && unit->m_b1 <= 0) {
988             LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * (y1 + y0); b1 += b1_slope;);
989         } else {
990             LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = (1.f - std::abs(b1)) * y0 + b1 * y1;
991                   b1 += b1_slope;);
992         }
993     }
994     unit->m_y1 = zapgremlins(y1);
995 }
996 
OnePole_Ctor(OnePole * unit)997 void OnePole_Ctor(OnePole* unit) {
998     if (INRATE(1) == calc_FullRate) {
999         SETCALC(OnePole_next_a);
1000     } else {
1001         SETCALC(OnePole_next_k);
1002     }
1003     unit->m_b1 = ZIN0(1);
1004     unit->m_y1 = 0.f;
1005     OnePole_next_a(unit, 1);
1006 }
1007 
1008 //////////////////////////////////////////////////////////////////////////////////////////////////
1009 
OneZero_Ctor(OneZero * unit)1010 void OneZero_Ctor(OneZero* unit) {
1011     SETCALC(OneZero_next);
1012     unit->m_b1 = ZIN0(1);
1013     unit->m_x1 = ZIN0(0);
1014     OneZero_next(unit, 1);
1015 }
1016 
1017 
OneZero_next(OneZero * unit,int inNumSamples)1018 void OneZero_next(OneZero* unit, int inNumSamples) {
1019     // printf("OneZero::next\n");
1020 
1021     float* out = ZOUT(0);
1022     float* in = ZIN(0);
1023     double b1 = unit->m_b1;
1024     unit->m_b1 = ZIN0(1);
1025 
1026     double x1 = unit->m_x1;
1027     if (b1 == unit->m_b1) {
1028         if (b1 >= 0.f) {
1029             LOOP1(inNumSamples, double x0 = ZXP(in); ZXP(out) = x0 + b1 * (x1 - x0); x1 = x0;);
1030         } else {
1031             LOOP1(inNumSamples, double x0 = ZXP(in); ZXP(out) = x0 + b1 * (x1 + x0); x1 = x0;);
1032         }
1033     } else {
1034         double b1_slope = CALCSLOPE(unit->m_b1, b1);
1035         if (b1 >= 0.f && unit->m_b1 >= 0) {
1036             LOOP1(inNumSamples, double x0 = ZXP(in); ZXP(out) = x0 + b1 * (x1 - x0); x1 = x0; b1 += b1_slope;);
1037         } else if (b1 <= 0.f && unit->m_b1 <= 0) {
1038             LOOP1(inNumSamples, double x0 = ZXP(in); ZXP(out) = x0 + b1 * (x1 + x0); x1 = x0; b1 += b1_slope;);
1039         } else {
1040             LOOP1(inNumSamples, double x0 = ZXP(in); ZXP(out) = (1.f - std::abs(b1)) * x0 + b1 * x1; x1 = x0;
1041                   b1 += b1_slope;);
1042         }
1043     }
1044     unit->m_x1 = x1;
1045 }
1046 
1047 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1048 
Integrator_Ctor(Integrator * unit)1049 void Integrator_Ctor(Integrator* unit) {
1050     if (INRATE(1) == calc_ScalarRate)
1051         SETCALC(Integrator_next_i);
1052     else
1053         SETCALC(Integrator_next);
1054     unit->m_b1 = ZIN0(1);
1055     unit->m_y1 = 0.f;
1056     Integrator_next(unit, 1);
1057 }
1058 
Integrator_next_i(Integrator * unit,int inNumSamples)1059 void Integrator_next_i(Integrator* unit, int inNumSamples) {
1060     float* out = ZOUT(0);
1061     float* in = ZIN(0);
1062 
1063     double b1 = unit->m_b1;
1064     double y1 = unit->m_y1;
1065 
1066     if (b1 == 1.f) {
1067         LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + y1;);
1068     } else if (b1 == 0.f) {
1069         LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0;);
1070     } else {
1071         LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * y1;);
1072     }
1073     unit->m_y1 = zapgremlins(y1);
1074 }
1075 
Integrator_next(Integrator * unit,int inNumSamples)1076 void Integrator_next(Integrator* unit, int inNumSamples) {
1077     float* out = ZOUT(0);
1078     float* in = ZIN(0);
1079     double newB1 = ZIN0(1);
1080 
1081     double b1 = unit->m_b1;
1082     double y1 = unit->m_y1;
1083 
1084     if (b1 == newB1) {
1085         if (b1 == 1.f) {
1086             LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + y1;);
1087         } else if (b1 == 0.f) {
1088             LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0;);
1089         } else {
1090             LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * y1;);
1091         }
1092     } else {
1093         double b1_slope = CALCSLOPE(newB1, b1);
1094         LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * y1; b1 += b1_slope;);
1095 
1096         unit->m_b1 = newB1;
1097     }
1098     unit->m_y1 = zapgremlins(y1);
1099 }
1100 
1101 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1102 
Decay_Ctor(Decay * unit)1103 void Decay_Ctor(Decay* unit) {
1104     SETCALC(Decay_next);
1105     unit->m_decayTime = uninitializedControl;
1106     unit->m_b1 = 0.f;
1107     unit->m_y1 = 0.f;
1108     Decay_next(unit, 1);
1109 }
1110 
Decay_next(Decay * unit,int inNumSamples)1111 void Decay_next(Decay* unit, int inNumSamples) {
1112     // printf("Decay_next_a\n");
1113 
1114     float* out = ZOUT(0);
1115     float* in = ZIN(0);
1116     float decayTime = ZIN0(1);
1117 
1118     double y1 = unit->m_y1;
1119     double b1 = unit->m_b1;
1120     if (decayTime == unit->m_decayTime) {
1121         if (b1 == 0.f) {
1122             LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0;);
1123         } else {
1124             LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * y1;);
1125         }
1126     } else {
1127         unit->m_b1 = decayTime == 0.f ? 0.f : exp(log001 / (decayTime * SAMPLERATE));
1128         unit->m_decayTime = decayTime;
1129         double b1_slope = CALCSLOPE(unit->m_b1, b1);
1130         // printf("decayTime %g  %g %g\n", unit->m_decayTime, next_b1, b1);
1131         LOOP1(inNumSamples, double y0 = ZXP(in); ZXP(out) = y1 = y0 + b1 * y1; b1 += b1_slope;);
1132     }
1133     unit->m_y1 = zapgremlins(y1);
1134 }
1135 
1136 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1137 
1138 
Decay2_Ctor(Decay2 * unit)1139 void Decay2_Ctor(Decay2* unit) {
1140     SETCALC(Decay2_next);
1141 
1142     float attackTime = ZIN0(1);
1143     float decayTime = ZIN0(2);
1144     unit->m_b1a = decayTime == 0.f ? 0.f : exp(log001 / (decayTime * SAMPLERATE));
1145     unit->m_b1b = attackTime == 0.f ? 0.f : exp(log001 / (attackTime * SAMPLERATE));
1146     unit->m_decayTime = decayTime;
1147     unit->m_attackTime = attackTime;
1148 
1149     float y0 = ZIN0(0);
1150     unit->m_y1a = y0;
1151     unit->m_y1b = y0;
1152     ZOUT0(0) = 0.f;
1153 }
1154 
Decay2_next(Decay2 * unit,int inNumSamples)1155 void Decay2_next(Decay2* unit, int inNumSamples) {
1156     // printf("Decay2_next_a\n");
1157 
1158     float* out = ZOUT(0);
1159     float* in = ZIN(0);
1160     float attackTime = ZIN0(1);
1161     float decayTime = ZIN0(2);
1162 
1163     double y1a = unit->m_y1a;
1164     double y1b = unit->m_y1b;
1165     double b1a = unit->m_b1a;
1166     double b1b = unit->m_b1b;
1167     if (decayTime == unit->m_decayTime && attackTime == unit->m_attackTime) {
1168         LOOP1(inNumSamples, double y0 = ZXP(in); y1a = y0 + b1a * y1a; y1b = y0 + b1b * y1b; ZXP(out) = y1a - y1b;);
1169     } else {
1170         unit->m_decayTime = decayTime;
1171         unit->m_attackTime = attackTime;
1172         double next_b1a = decayTime == 0.f ? 0.f : exp(log001 / (decayTime * SAMPLERATE));
1173         double next_b1b = attackTime == 0.f ? 0.f : exp(log001 / (attackTime * SAMPLERATE));
1174         unit->m_decayTime = decayTime;
1175         double b1a_slope = CALCSLOPE(next_b1a, b1a);
1176         double b1b_slope = CALCSLOPE(next_b1b, b1b);
1177         unit->m_b1a = next_b1a;
1178         unit->m_b1b = next_b1b;
1179         LOOP1(inNumSamples, double y0 = ZXP(in); y1a = y0 + b1a * y1a; y1b = y0 + b1b * y1b; ZXP(out) = y1a - y1b;
1180               b1a += b1a_slope; b1b += b1b_slope;);
1181     }
1182     unit->m_y1a = y1a;
1183     unit->m_y1b = y1b;
1184     unit->m_b1a = b1a;
1185     unit->m_b1b = b1b;
1186 }
1187 
1188 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1189 
LeakDC_next_i_4(LeakDC * unit,int inNumSamples)1190 void LeakDC_next_i_4(LeakDC* unit, int inNumSamples) {
1191     float* out = ZOUT(0);
1192     float* in = ZIN(0);
1193     double b1 = unit->m_b1;
1194     double y1 = unit->m_y1;
1195     double x1 = unit->m_x1;
1196 
1197     LOOP1(inNumSamples / 4, double x00 = ZXP(in); double x01 = ZXP(in); double x02 = ZXP(in); double x03 = ZXP(in);
1198           float out0 = y1 = x00 - x1 + b1 * y1; float out1 = y1 = x01 - x00 + b1 * y1;
1199           float out2 = y1 = x02 - x01 + b1 * y1; float out3 = y1 = x03 - x02 + b1 * y1;
1200 
1201           ZXP(out) = out0; ZXP(out) = out1; ZXP(out) = out2; ZXP(out) = out3;
1202 
1203           x1 = x03;);
1204     unit->m_x1 = x1;
1205     unit->m_y1 = zapgremlins(y1);
1206 }
1207 
LeakDC_next_i(LeakDC * unit,int inNumSamples)1208 void LeakDC_next_i(LeakDC* unit, int inNumSamples) {
1209     float* out = ZOUT(0);
1210     float* in = ZIN(0);
1211     double b1 = unit->m_b1;
1212     double y1 = unit->m_y1;
1213     double x1 = unit->m_x1;
1214 
1215     LOOP1(inNumSamples, double x0 = ZXP(in); ZXP(out) = y1 = x0 - x1 + b1 * y1; x1 = x0;);
1216     unit->m_x1 = x1;
1217     unit->m_y1 = zapgremlins(y1);
1218 }
1219 
LeakDC_next(LeakDC * unit,int inNumSamples)1220 void LeakDC_next(LeakDC* unit, int inNumSamples) {
1221     if (ZIN0(1) == unit->m_b1) {
1222         if ((inNumSamples & 3) == 0)
1223             LeakDC_next_i_4(unit, inNumSamples);
1224         else
1225             LeakDC_next_i(unit, inNumSamples);
1226     } else {
1227         float* out = ZOUT(0);
1228         float* in = ZIN(0);
1229         double b1 = unit->m_b1;
1230         unit->m_b1 = ZIN0(1);
1231 
1232         double y1 = unit->m_y1;
1233         double x1 = unit->m_x1;
1234 
1235         double b1_slope = CALCSLOPE(unit->m_b1, b1);
1236         LOOP1(inNumSamples, double x0 = ZXP(in); ZXP(out) = y1 = x0 - x1 + b1 * y1; x1 = x0; b1 += b1_slope;);
1237         unit->m_x1 = x1;
1238         unit->m_y1 = zapgremlins(y1);
1239     }
1240 }
1241 
1242 
LeakDC_next_1(LeakDC * unit,int inNumSamples)1243 void LeakDC_next_1(LeakDC* unit, int inNumSamples) {
1244     double b1 = unit->m_b1 = ZIN0(1);
1245 
1246     double y1 = unit->m_y1;
1247     double x1 = unit->m_x1;
1248 
1249     double x0 = ZIN0(0);
1250     ZOUT0(0) = y1 = x0 - x1 + b1 * y1;
1251     x1 = x0;
1252 
1253     unit->m_x1 = x1;
1254     unit->m_y1 = zapgremlins(y1);
1255 }
1256 
LeakDC_Ctor(LeakDC * unit)1257 void LeakDC_Ctor(LeakDC* unit) {
1258     // printf("LeakDC_Ctor\n");
1259     if (BUFLENGTH == 1)
1260         SETCALC(LeakDC_next_1);
1261     else {
1262         if (INRATE(1) == calc_ScalarRate) {
1263             if ((BUFLENGTH & 3) == 0)
1264                 SETCALC(LeakDC_next_i_4);
1265             else
1266                 SETCALC(LeakDC_next_i);
1267         } else
1268             SETCALC(LeakDC_next);
1269     }
1270     unit->m_b1 = 0.0;
1271     unit->m_x1 = ZIN0(0);
1272     unit->m_y1 = 0.0;
1273     LeakDC_next_1(unit, 1);
1274 }
1275 
1276 
1277 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1278 
TwoPole_Ctor(TwoPole * unit)1279 void TwoPole_Ctor(TwoPole* unit) {
1280     // printf("TwoPole_Reset\n");
1281     SETCALC(TwoPole_next);
1282     unit->m_b1 = 0.f;
1283     unit->m_b2 = 0.f;
1284     unit->m_y1 = 0.f;
1285     unit->m_y2 = 0.f;
1286     unit->m_freq = uninitializedControl;
1287     unit->m_reson = uninitializedControl;
1288     PUSH_LOOPVALS
1289     TwoPole_next(unit, 1);
1290     POP_LOOPVALS
1291 }
1292 
1293 
TwoPole_next(TwoPole * unit,int inNumSamples)1294 void TwoPole_next(TwoPole* unit, int inNumSamples) {
1295     // printf("TwoPole_next_a\n");
1296 
1297     float* out = ZOUT(0);
1298     float* in = ZIN(0);
1299     float freq = ZIN0(1);
1300     float reson = ZIN0(2);
1301 
1302     double y0;
1303     double y1 = unit->m_y1;
1304     double y2 = unit->m_y2;
1305 
1306     if (freq != unit->m_freq || reson != unit->m_reson) {
1307         double b1 = unit->m_b1;
1308         double b2 = unit->m_b2;
1309         double b1_next = 2 * reson * cos(freq * unit->mRate->mRadiansPerSample);
1310         double b2_next = -(reson * reson);
1311         double b1_slope = (b1_next - b1) * unit->mRate->mFilterSlope;
1312         double b2_slope = (b2_next - b2) * unit->mRate->mFilterSlope;
1313         LOOP(unit->mRate->mFilterLoops, ZXP(out) = y0 = ZXP(in) + b1 * y1 + b2 * y2;
1314              ZXP(out) = y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = y1 = ZXP(in) + b1 * y2 + b2 * y0;
1315 
1316              b1 += b1_slope; b2 += b2_slope;);
1317         LOOP(unit->mRate->mFilterRemain, ZXP(out) = y0 = ZXP(in) + b1 * y1 + b2 * y2; y2 = y1; y1 = y0;);
1318 
1319         unit->m_freq = freq;
1320         unit->m_reson = reson;
1321         unit->m_b1 = b1_next;
1322         unit->m_b2 = b2_next;
1323     } else {
1324         double b1 = unit->m_b1;
1325         double b2 = unit->m_b2;
1326         LOOP(unit->mRate->mFilterLoops, ZXP(out) = y0 = ZXP(in) + b1 * y1 + b2 * y2;
1327              ZXP(out) = y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = y1 = ZXP(in) + b1 * y2 + b2 * y0;);
1328         LOOP(unit->mRate->mFilterRemain, ZXP(out) = y0 = ZXP(in) + b1 * y1 + b2 * y2; y2 = y1; y1 = y0;);
1329     }
1330     unit->m_y1 = zapgremlins(y1);
1331     unit->m_y2 = zapgremlins(y2);
1332 }
1333 
1334 
1335 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1336 
1337 
TwoZero_Ctor(TwoZero * unit)1338 void TwoZero_Ctor(TwoZero* unit) {
1339     // printf("TwoZero_Reset\n");
1340     SETCALC(TwoZero_next);
1341     unit->m_b1 = 0.f;
1342     unit->m_b2 = 0.f;
1343     unit->m_x1 = 0.f;
1344     unit->m_x2 = 0.f;
1345     unit->m_freq = uninitializedControl;
1346     unit->m_reson = uninitializedControl;
1347     PUSH_LOOPVALS
1348     TwoZero_next(unit, 1);
1349     POP_LOOPVALS
1350 }
1351 
TwoZero_next(TwoZero * unit,int inNumSamples)1352 void TwoZero_next(TwoZero* unit, int inNumSamples) {
1353     // printf("TwoZero_next\n");
1354 
1355     float* out = ZOUT(0);
1356     float* in = ZIN(0);
1357     float freq = ZIN0(1);
1358     float reson = ZIN0(2);
1359 
1360     double x0;
1361     double x1 = unit->m_x1;
1362     double x2 = unit->m_x2;
1363 
1364     if (freq != unit->m_freq || reson != unit->m_reson) {
1365         double b1 = unit->m_b1;
1366         double b2 = unit->m_b2;
1367         double b1_next = -2 * reson * cos(freq * unit->mRate->mRadiansPerSample);
1368         double b2_next = (reson * reson);
1369         double b1_slope = (b1_next - b1) * unit->mRate->mFilterSlope;
1370         double b2_slope = (b2_next - b2) * unit->mRate->mFilterSlope;
1371         LOOP(unit->mRate->mFilterLoops, x0 = ZXP(in); ZXP(out) = x0 + b1 * x1 + b2 * x2; x2 = ZXP(in);
1372              ZXP(out) = x2 + b1 * x0 + b2 * x1; x1 = ZXP(in); ZXP(out) = x1 + b1 * x2 + b2 * x0;
1373 
1374              b1 += b1_slope; b2 += b2_slope;);
1375         LOOP(unit->mRate->mFilterRemain, x0 = ZXP(in); ZXP(out) = x0 + b1 * x1 + b2 * x2; x2 = x1; x1 = x0;);
1376 
1377         unit->m_freq = freq;
1378         unit->m_reson = reson;
1379         unit->m_b1 = b1_next;
1380         unit->m_b2 = b2_next;
1381     } else {
1382         double b1 = unit->m_b1;
1383         double b2 = unit->m_b2;
1384         LOOP(unit->mRate->mFilterLoops, x0 = ZXP(in); ZXP(out) = x0 + b1 * x1 + b2 * x2; x2 = ZXP(in);
1385              ZXP(out) = x2 + b1 * x0 + b2 * x1; x1 = ZXP(in); ZXP(out) = x1 + b1 * x2 + b2 * x0;);
1386         LOOP(unit->mRate->mFilterRemain, x0 = ZXP(in); ZXP(out) = x0 + b1 * x1 + b2 * x2; x2 = x1; x1 = x0;);
1387     }
1388     unit->m_x1 = x1;
1389     unit->m_x2 = x2;
1390 }
1391 
1392 
1393 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1394 
1395 
APF_Ctor(APF * unit)1396 void APF_Ctor(APF* unit) {
1397     // printf("APF_Reset\n");
1398     SETCALC(APF_next);
1399     unit->m_b1 = 0.f;
1400     unit->m_b2 = 0.f;
1401     unit->m_y1 = 0.f;
1402     unit->m_y2 = 0.f;
1403     unit->m_x1 = 0.f;
1404     unit->m_x2 = 0.f;
1405     unit->m_freq = uninitializedControl;
1406     unit->m_reson = uninitializedControl;
1407     PUSH_LOOPVALS
1408     APF_next(unit, 1);
1409     POP_LOOPVALS
1410 }
1411 
1412 
APF_next(APF * unit,int inNumSamples)1413 void APF_next(APF* unit, int inNumSamples) {
1414     // printf("APF_next_a\n");
1415 
1416     float* out = ZOUT(0);
1417     float* in = ZIN(0);
1418     float freq = ZIN0(1);
1419     float reson = ZIN0(2);
1420 
1421     double x0, y0;
1422     double y1 = unit->m_y1;
1423     double y2 = unit->m_y2;
1424     double x1 = unit->m_x1;
1425     double x2 = unit->m_x2;
1426 
1427     if (freq != unit->m_freq || reson != unit->m_reson) {
1428         double b1 = unit->m_b1;
1429         double b2 = unit->m_b2;
1430         double b1_next = 2.f * reson * cos(freq * unit->mRate->mRadiansPerSample);
1431         double b2_next = -(reson * reson);
1432         double b1_slope = (b1_next - b1) * unit->mRate->mFilterSlope;
1433         double b2_slope = (b2_next - b2) * unit->mRate->mFilterSlope;
1434         LOOP(unit->mRate->mFilterLoops, x0 = ZXP(in); ZXP(out) = y0 = x0 + b1 * (y1 - x1) + b2 * (y2 - x2);
1435              x2 = ZXP(in); ZXP(out) = y2 = x2 + b1 * (y0 - x0) + b2 * (y2 - x1); x1 = ZXP(in);
1436              ZXP(out) = y1 = x1 + b1 * (y2 - x2) + b2 * (y2 - x0);
1437 
1438              b1 += b1_slope; b2 += b2_slope;);
1439         LOOP(unit->mRate->mFilterRemain, x0 = ZXP(in); ZXP(out) = y0 = x0 + b1 * (y1 - x1) + b2 * (y2 - x2); y2 = y1;
1440              y1 = y0; x2 = x1; x1 = x0;);
1441 
1442         unit->m_freq = freq;
1443         unit->m_reson = reson;
1444         unit->m_b1 = b1_next;
1445         unit->m_b2 = b2_next;
1446     } else {
1447         double b1 = unit->m_b1;
1448         double b2 = unit->m_b2;
1449         LOOP(unit->mRate->mFilterLoops, float in0 = ZXP(in); float in1 = ZXP(in); float in2 = ZXP(in); x0 = in0;
1450              float out0 = y0 = x0 + b1 * (y1 - x1) + b2 * (y2 - x2); x2 = in1;
1451              float out1 = y2 = x2 + b1 * (y0 - x0) + b2 * (y2 - x1); x1 = in2;
1452              float out2 = y1 = x1 + b1 * (y2 - x2) + b2 * (y2 - x0); ZXP(out) = out0; ZXP(out) = out1;
1453              ZXP(out) = out2;);
1454         LOOP(unit->mRate->mFilterRemain, x0 = ZXP(in); ZXP(out) = y0 = x0 + b1 * (y1 - x1) + b2 * (y2 - x2); y2 = y1;
1455              y1 = y0; x2 = x1; x1 = x0;);
1456     }
1457     unit->m_y1 = zapgremlins(y1);
1458     unit->m_y2 = zapgremlins(y2);
1459     unit->m_x1 = x1;
1460     unit->m_x2 = x2;
1461 }
1462 
1463 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1464 
LPZ1_Ctor(LPZ1 * unit)1465 void LPZ1_Ctor(LPZ1* unit) {
1466     // printf("LPZ1_Reset\n");
1467     SETCALC(LPZ1_next);
1468     unit->m_x1 = ZIN0(0);
1469     LPZ1_next(unit, 1);
1470 }
1471 
1472 
LPZ1_next(LPZ1 * unit,int inNumSamples)1473 void LPZ1_next(LPZ1* unit, int inNumSamples) {
1474     // printf("LPZ1_next_a\n");
1475 
1476     float* out = ZOUT(0);
1477     float* in = ZIN(0);
1478 
1479     double x0;
1480     double x1 = unit->m_x1;
1481 
1482     LOOP(inNumSamples >> 2, x0 = ZXP(in); float out0 = 0.5 * (x0 + x1); x1 = ZXP(in); float out1 = 0.5 * (x1 + x0);
1483          x0 = ZXP(in); float out2 = 0.5 * (x0 + x1); x1 = ZXP(in); float out3 = 0.5 * (x1 + x0);
1484 
1485          ZXP(out) = out0; ZXP(out) = out1; ZXP(out) = out2; ZXP(out) = out3;);
1486     LOOP(inNumSamples & 3, x0 = ZXP(in); ZXP(out) = 0.5 * (x0 + x1); x1 = x0;);
1487 
1488     unit->m_x1 = x1;
1489 }
1490 
1491 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1492 
1493 
HPZ1_Ctor(HPZ1 * unit)1494 void HPZ1_Ctor(HPZ1* unit) {
1495     // printf("HPZ1_Reset\n");
1496     SETCALC(HPZ1_next);
1497     unit->m_x1 = ZIN0(0);
1498     HPZ1_next(unit, 1);
1499 }
1500 
1501 
HPZ1_next(HPZ1 * unit,int inNumSamples)1502 void HPZ1_next(HPZ1* unit, int inNumSamples) {
1503     // printf("HPZ1_next\n");
1504 
1505     float* out = ZOUT(0);
1506     float* in = ZIN(0);
1507 
1508     double x0;
1509     double x1 = unit->m_x1;
1510 
1511     LOOP(inNumSamples >> 2, x0 = ZXP(in); float out0 = 0.5 * (x0 - x1); x1 = ZXP(in); float out1 = 0.5 * (x1 - x0);
1512          x0 = ZXP(in); float out2 = 0.5 * (x0 - x1); x1 = ZXP(in); float out3 = 0.5 * (x1 - x0);
1513 
1514          ZXP(out) = out0; ZXP(out) = out1; ZXP(out) = out2; ZXP(out) = out3;);
1515     LOOP(inNumSamples & 3, x0 = ZXP(in);
1516          // printf("%d %d %g %g\n", this, inNumSamples, x0, x1);
1517          ZXP(out) = 0.5f * (x0 - x1);
1518          x1 = x0;);
1519 
1520     unit->m_x1 = x1;
1521 }
1522 
1523 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1524 
Slope_Ctor(Slope * unit)1525 void Slope_Ctor(Slope* unit) {
1526     // printf("Slope_Reset\n");
1527     SETCALC(Slope_next);
1528     unit->m_x1 = ZIN0(0);
1529     // printf("Slope_Reset %g\n", unit->m_x1);
1530     Slope_next(unit, 1);
1531 }
1532 
1533 
Slope_next(Slope * unit,int inNumSamples)1534 void Slope_next(Slope* unit, int inNumSamples) {
1535     // printf("Slope_next_a %g\n", unit->m_x1);
1536 
1537     float* out = ZOUT(0);
1538     float* in = ZIN(0);
1539 
1540     double x1 = unit->m_x1;
1541 
1542     double sr = SAMPLERATE;
1543     LOOP1(inNumSamples, double x0 = ZXP(in); ZXP(out) = sr * (x0 - x1); x1 = x0;);
1544 
1545     unit->m_x1 = x1;
1546 }
1547 
1548 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1549 
Delay1_Ctor(Delay1 * unit)1550 void Delay1_Ctor(Delay1* unit) {
1551     // printf("Delay1_Reset\n");
1552     SETCALC(Delay1_next);
1553     unit->m_x1 = ZIN0(0);
1554     Delay1_next(unit, 1);
1555 }
1556 
1557 
Delay1_next(Delay1 * unit,int inNumSamples)1558 void Delay1_next(Delay1* unit, int inNumSamples) {
1559     // printf("Delay1_next_a\n");
1560 
1561     float* out = ZOUT(0);
1562     float* in = ZIN(0);
1563 
1564     float x0;
1565     float x1 = unit->m_x1;
1566 
1567     LOOP(inNumSamples >> 2, x0 = ZXP(in); ZXP(out) = x1; x1 = ZXP(in); ZXP(out) = x0; x0 = ZXP(in); ZXP(out) = x1;
1568          x1 = ZXP(in); ZXP(out) = x0;);
1569     LOOP(inNumSamples & 3, x0 = ZXP(in); ZXP(out) = x1; x1 = x0;);
1570 
1571     unit->m_x1 = x1;
1572 }
1573 
1574 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1575 
Flip_Ctor(Flip * unit)1576 void Flip_Ctor(Flip* unit) {
1577     if (unit->mBufLength & 1) {
1578         SETCALC(Flip_next_odd);
1579         Flip_next_odd(unit, 1);
1580     } else {
1581         SETCALC(Flip_next_even);
1582         ZOUT0(0) = ZIN0(0);
1583     }
1584 }
1585 
1586 
Flip_next_even(Flip * unit,int inNumSamples)1587 void Flip_next_even(Flip* unit, int inNumSamples) {
1588     float* out = ZOUT(0);
1589     float* in = ZIN(0);
1590 
1591     LOOP(inNumSamples >> 1, ZXP(out) = -ZXP(in); ZXP(out) = ZXP(in););
1592 }
1593 
Flip_next_odd(Flip * unit,int inNumSamples)1594 void Flip_next_odd(Flip* unit, int inNumSamples) {
1595     float* out = ZOUT(0);
1596     float* in = ZIN(0);
1597 
1598     if (unit->mWorld->mBufCounter & 1) {
1599         ZXP(out) = ZXP(in);
1600         LOOP(inNumSamples >> 1, ZXP(out) = -ZXP(in); ZXP(out) = ZXP(in););
1601     } else {
1602         LOOP(inNumSamples >> 1, ZXP(out) = -ZXP(in); ZXP(out) = ZXP(in););
1603         ZXP(out) = -ZXP(in);
1604     }
1605 }
1606 
1607 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1608 
Delay2_Ctor(Delay2 * unit)1609 void Delay2_Ctor(Delay2* unit) {
1610     SETCALC(Delay2_next);
1611     unit->m_x1 = ZIN0(0);
1612     ZOUT0(0) = 0.f;
1613 }
1614 
1615 
Delay2_next(Delay2 * unit,int inNumSamples)1616 void Delay2_next(Delay2* unit, int inNumSamples) {
1617     float* out = ZOUT(0);
1618     float* in = ZIN(0);
1619 
1620     double x0;
1621     double x1 = unit->m_x1;
1622     double x2 = unit->m_x2;
1623 
1624     LOOP(unit->mRate->mFilterLoops, x0 = ZXP(in); ZXP(out) = x2; x2 = ZXP(in); ZXP(out) = x1; x1 = ZXP(in);
1625          ZXP(out) = x0;);
1626     LOOP(unit->mRate->mFilterRemain, x0 = ZXP(in); ZXP(out) = x2; x2 = x1; x1 = x0;);
1627 
1628     unit->m_x1 = x1;
1629     unit->m_x2 = x2;
1630 }
1631 
1632 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1633 
1634 
LPZ2_Ctor(LPZ2 * unit)1635 void LPZ2_Ctor(LPZ2* unit) {
1636     // printf("LPZ2_Reset\n");
1637     SETCALC(LPZ2_next);
1638     unit->m_x1 = unit->m_x2 = ZIN0(0);
1639     PUSH_LOOPVALS
1640     LPZ2_next(unit, 1);
1641     POP_LOOPVALS
1642 }
1643 
1644 
LPZ2_next(LPZ2 * unit,int inNumSamples)1645 void LPZ2_next(LPZ2* unit, int inNumSamples) {
1646     // printf("LPZ2_next_a\n");
1647 
1648     float* out = ZOUT(0);
1649     float* in = ZIN(0);
1650 
1651     double x0;
1652     double x1 = unit->m_x1;
1653     double x2 = unit->m_x2;
1654 
1655     LOOP(unit->mRate->mFilterLoops, x0 = ZXP(in); float out0 = (x0 + 2.0 * x1 + x2) * 0.25; x2 = ZXP(in);
1656          float out1 = (x2 + 2.0 * x0 + x1) * 0.25; x1 = ZXP(in); float out2 = (x1 + 2.0 * x2 + x0) * 0.25;
1657 
1658          ZXP(out) = out0; ZXP(out) = out1; ZXP(out) = out2;);
1659     LOOP(unit->mRate->mFilterRemain, x0 = ZXP(in); ZXP(out) = (x0 + 2. * x1 + x2) * 0.25; x2 = x1; x1 = x0;);
1660 
1661     unit->m_x1 = x1;
1662     unit->m_x2 = x2;
1663 }
1664 
1665 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1666 
HPZ2_Ctor(HPZ2 * unit)1667 void HPZ2_Ctor(HPZ2* unit) {
1668     // printf("HPZ2_Reset\n");
1669     SETCALC(HPZ2_next);
1670     unit->m_x1 = unit->m_x2 = ZIN0(0);
1671     PUSH_LOOPVALS
1672     HPZ2_next(unit, 1);
1673     POP_LOOPVALS
1674 }
1675 
1676 
HPZ2_next(HPZ2 * unit,int inNumSamples)1677 void HPZ2_next(HPZ2* unit, int inNumSamples) {
1678     // printf("HPZ2_next_a\n");
1679 
1680     float* out = ZOUT(0);
1681     float* in = ZIN(0);
1682 
1683     double x0;
1684     double x1 = unit->m_x1;
1685     double x2 = unit->m_x2;
1686 
1687     LOOP(unit->mRate->mFilterLoops, x0 = ZXP(in); float out0 = (x0 - 2.0 * x1 + x2) * 0.25; x2 = ZXP(in);
1688          float out1 = (x2 - 2.0 * x0 + x1) * 0.25; x1 = ZXP(in); float out2 = (x1 - 2.0 * x2 + x0) * 0.25;
1689          ZXP(out) = out0; ZXP(out) = out1; ZXP(out) = out2;);
1690     LOOP(unit->mRate->mFilterRemain, x0 = ZXP(in); ZXP(out) = (x0 - 2.0 * x1 + x2) * 0.25; x2 = x1; x1 = x0;);
1691 
1692     unit->m_x1 = x1;
1693     unit->m_x2 = x2;
1694 }
1695 
1696 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1697 
BPZ2_Ctor(BPZ2 * unit)1698 void BPZ2_Ctor(BPZ2* unit) {
1699     // printf("BPZ2_Reset\n");
1700     SETCALC(BPZ2_next);
1701     unit->m_x1 = unit->m_x2 = ZIN0(0);
1702     PUSH_LOOPVALS
1703     BPZ2_next(unit, 1);
1704     POP_LOOPVALS
1705 }
1706 
1707 
BPZ2_next(BPZ2 * unit,int inNumSamples)1708 void BPZ2_next(BPZ2* unit, int inNumSamples) {
1709     // printf("BPZ2_next_a\n");
1710 
1711     float* out = ZOUT(0);
1712     float* in = ZIN(0);
1713 
1714     double x0;
1715     double x1 = unit->m_x1;
1716     double x2 = unit->m_x2;
1717 
1718     LOOP(unit->mRate->mFilterLoops, x0 = ZXP(in); ZXP(out) = (x0 - x2) * 0.5; x2 = ZXP(in); ZXP(out) = (x2 - x1) * 0.5;
1719          x1 = ZXP(in); ZXP(out) = (x1 - x0) * 0.5;);
1720     LOOP(unit->mRate->mFilterRemain, x0 = ZXP(in); ZXP(out) = (x0 - x2) * 0.5; x2 = x1; x1 = x0;);
1721 
1722     unit->m_x1 = x1;
1723     unit->m_x2 = x2;
1724 }
1725 
1726 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1727 
BRZ2_Ctor(BRZ2 * unit)1728 void BRZ2_Ctor(BRZ2* unit) {
1729     // printf("BRZ2_Reset\n");
1730     SETCALC(BRZ2_next);
1731     unit->m_x1 = unit->m_x2 = ZIN0(0);
1732     PUSH_LOOPVALS
1733     BRZ2_next(unit, 1);
1734     POP_LOOPVALS
1735 }
1736 
1737 
BRZ2_next(BRZ2 * unit,int inNumSamples)1738 void BRZ2_next(BRZ2* unit, int inNumSamples) {
1739     // printf("BRZ2_next_a\n");
1740 
1741     float* out = ZOUT(0);
1742     float* in = ZIN(0);
1743 
1744     double x0;
1745     double x1 = unit->m_x1;
1746     double x2 = unit->m_x2;
1747 
1748     LOOP(unit->mRate->mFilterLoops, x0 = ZXP(in); ZXP(out) = (x0 + x2) * 0.5; x2 = ZXP(in); ZXP(out) = (x2 + x1) * 0.5;
1749          x1 = ZXP(in); ZXP(out) = (x1 + x0) * 0.5;);
1750     LOOP(unit->mRate->mFilterRemain, x0 = ZXP(in); ZXP(out) = (x0 + x2) * 0.5; x2 = x1; x1 = x0;);
1751 
1752     unit->m_x1 = x1;
1753     unit->m_x2 = x2;
1754 }
1755 
1756 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1757 
Slew_Ctor(Slew * unit)1758 void Slew_Ctor(Slew* unit) {
1759     // printf("Slew_Reset\n");
1760     SETCALC(Slew_next);
1761     unit->mLevel = ZIN0(0);
1762     Slew_next(unit, 1);
1763 }
1764 
1765 
Slew_next(Slew * unit,int inNumSamples)1766 void Slew_next(Slew* unit, int inNumSamples) {
1767     // printf("Slew_next_a\n");
1768 
1769     float sampleDur = unit->mRate->mSampleDur;
1770     float* out = ZOUT(0);
1771     float* in = ZIN(0);
1772     float upf = ZIN0(1) * sampleDur;
1773     float dnf = 0.f - ZIN0(2) * sampleDur;
1774     double level = unit->mLevel;
1775     LOOP1(inNumSamples, double slope = ZXP(in) - level; level += sc_clip(slope, dnf, upf); ZXP(out) = level;);
1776     unit->mLevel = level;
1777 }
1778 
1779 /*void Slew_next_1(Slew* unit, int inNumSamples)
1780 {
1781     //printf("Slew_next_a\n");
1782 
1783     float *out = ZOUT(0);
1784     float in = ZIN0(0);
1785     float upf = ZIN0(1);
1786     float dnf = ZIN0(2);
1787     float level = unit->mLevel;
1788     float slope = ZXP(in) - level;
1789     if (slope > upf) slope = upf;
1790     else if (slope < dnf) slope = dnf;
1791     level += slope;
1792     ZXP(out) = level;
1793     unit->mLevel = level;
1794 
1795 }*/
1796 
1797 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1798 
RLPF_Ctor(RLPF * unit)1799 void RLPF_Ctor(RLPF* unit) {
1800     // printf("RLPF_Reset\n");
1801     if (unit->mBufLength == 1) {
1802         SETCALC(RLPF_next_1);
1803     } else {
1804         SETCALC(RLPF_next);
1805     }
1806 
1807     unit->m_a0 = 0.f;
1808     unit->m_b1 = 0.f;
1809     unit->m_b2 = 0.f;
1810     unit->m_y1 = 0.f;
1811     unit->m_y2 = 0.f;
1812     unit->m_freq = uninitializedControl;
1813     unit->m_reson = uninitializedControl;
1814     RLPF_next_1(unit, 1);
1815 }
1816 
1817 
RLPF_next(RLPF * unit,int inNumSamples)1818 void RLPF_next(RLPF* unit, int inNumSamples) {
1819     // printf("RLPF_next\n");
1820 
1821     float* out = ZOUT(0);
1822     float* in = ZIN(0);
1823     float freq = ZIN0(1);
1824     float reson = ZIN0(2);
1825 
1826     double y0;
1827     double y1 = unit->m_y1;
1828     double y2 = unit->m_y2;
1829     double a0 = unit->m_a0;
1830     double b1 = unit->m_b1;
1831     double b2 = unit->m_b2;
1832 
1833     if (freq != unit->m_freq || reson != unit->m_reson) {
1834         double qres = sc_max(0.001, reson);
1835         double pfreq = freq * unit->mRate->mRadiansPerSample;
1836 
1837         double D = tan(pfreq * qres * 0.5);
1838         double C = ((1.0 - D) / (1.0 + D));
1839         double cosf = cos(pfreq);
1840 
1841         double next_b1 = (1.0 + C) * cosf;
1842         double next_b2 = -C;
1843         double next_a0 = (1.0 + C - next_b1) * .25;
1844         // post("%g %g %g   %g %g   %g %g %g   %g %g\n", *freq, pfreq, qres, D, C, cosf, next_b1, next_b2, next_a0, y1,
1845         // y2);
1846 
1847         double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
1848         double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
1849         double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
1850         LOOP(unit->mRate->mFilterLoops, y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = y0 + 2.0 * y1 + y2;
1851 
1852              y2 = a0 * ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = y2 + 2.0 * y0 + y1;
1853 
1854              y1 = a0 * ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = y1 + 2.0 * y2 + y0;
1855 
1856              a0 += a0_slope; b1 += b1_slope; b2 += b2_slope;);
1857         LOOP(unit->mRate->mFilterRemain, y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = y0 + 2.0 * y1 + y2; y2 = y1;
1858              y1 = y0;);
1859 
1860         unit->m_freq = freq;
1861         unit->m_reson = reson;
1862         unit->m_a0 = next_a0;
1863         unit->m_b1 = next_b1;
1864         unit->m_b2 = next_b2;
1865     } else {
1866         LOOP(unit->mRate->mFilterLoops, y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = y0 + 2.0 * y1 + y2;
1867 
1868              y2 = a0 * ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = y2 + 2.0 * y0 + y1;
1869 
1870              y1 = a0 * ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = y1 + 2.0 * y2 + y0;);
1871         LOOP(unit->mRate->mFilterRemain, y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = y0 + 2.0 * y1 + y2; y2 = y1;
1872              y1 = y0;);
1873     }
1874     unit->m_y1 = zapgremlins(y1);
1875     unit->m_y2 = zapgremlins(y2);
1876 }
1877 
1878 
RLPF_next_1(RLPF * unit,int inNumSamples)1879 void RLPF_next_1(RLPF* unit, int inNumSamples) {
1880     // printf("RLPF_next_1\n");
1881 
1882     float in = ZIN0(0);
1883     float freq = ZIN0(1);
1884     float reson = ZIN0(2);
1885 
1886     double y0;
1887     double y1 = unit->m_y1;
1888     double y2 = unit->m_y2;
1889     double a0 = unit->m_a0;
1890     double b1 = unit->m_b1;
1891     double b2 = unit->m_b2;
1892 
1893     if (freq != unit->m_freq || reson != unit->m_reson) {
1894         double qres = sc_max(0.001f, reson);
1895         double pfreq = freq * unit->mRate->mRadiansPerSample;
1896 
1897         double D = tan(pfreq * qres * 0.5f);
1898         double C = ((1.f - D) / (1.f + D));
1899         double cosf = cos(pfreq);
1900 
1901         b1 = (1.0 + C) * cosf;
1902         b2 = -C;
1903         a0 = (1.0 + C - b1) * .25;
1904 
1905         y0 = a0 * in + b1 * y1 + b2 * y2;
1906         ZOUT0(0) = y0 + 2.0 * y1 + y2;
1907         y2 = y1;
1908         y1 = y0;
1909 
1910         unit->m_freq = freq;
1911         unit->m_reson = reson;
1912         unit->m_a0 = a0;
1913         unit->m_b1 = b1;
1914         unit->m_b2 = b2;
1915     } else {
1916         y0 = a0 * in + b1 * y1 + b2 * y2;
1917         ZOUT0(0) = y0 + 2.0 * y1 + y2;
1918         y2 = y1;
1919         y1 = y0;
1920     }
1921     unit->m_y1 = zapgremlins(y1);
1922     unit->m_y2 = zapgremlins(y2);
1923 }
1924 
1925 
1926 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1927 
RHPF_Ctor(RHPF * unit)1928 void RHPF_Ctor(RHPF* unit) {
1929     if (unit->mBufLength == 1)
1930         SETCALC(RHPF_next_1);
1931     else
1932         SETCALC(RHPF_next);
1933 
1934     unit->m_a0 = 0.;
1935     unit->m_b1 = 0.;
1936     unit->m_b2 = 0.;
1937     unit->m_y1 = 0.;
1938     unit->m_y2 = 0.;
1939     unit->m_freq = uninitializedControl;
1940     unit->m_reson = uninitializedControl;
1941     RHPF_next_1(unit, 1);
1942 }
1943 
1944 
RHPF_next(RHPF * unit,int inNumSamples)1945 void RHPF_next(RHPF* unit, int inNumSamples) {
1946     // printf("RHPFs_next\n");
1947 
1948     float* out = ZOUT(0);
1949     float* in = ZIN(0);
1950     float freq = ZIN0(1);
1951     float reson = ZIN0(2);
1952 
1953     double y1 = unit->m_y1;
1954     double y2 = unit->m_y2;
1955     double a0 = unit->m_a0;
1956     double b1 = unit->m_b1;
1957     double b2 = unit->m_b2;
1958 
1959     if (freq != unit->m_freq || reson != unit->m_reson) {
1960         double qres = sc_max(0.001f, reson);
1961         double pfreq = freq * unit->mRate->mRadiansPerSample;
1962 
1963         double D = tan(pfreq * qres * 0.5f);
1964         double C = ((1.0 - D) / (1.0 + D));
1965         double cosf = cos(pfreq);
1966 
1967         double next_b1 = (1.0 + C) * cosf;
1968         double next_b2 = -C;
1969         double next_a0 = (1.0 + C + next_b1) * .25;
1970 
1971         // post("%g %g %g   %g %g   %g %g %g   %g %g\n", *freq, pfreq, qres, D, C, cosf, next_b1, next_b2, next_a0, y1,
1972         // y2);
1973 
1974         double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
1975         double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
1976         double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
1977         LOOP(unit->mRate->mFilterLoops, double y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = y0 - 2.0 * y1 + y2;
1978 
1979              y2 = a0 * ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = y2 - 2.0 * y0 + y1;
1980 
1981              y1 = a0 * ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = y1 - 2.0 * y2 + y0;
1982 
1983              a0 += a0_slope; b1 += b1_slope; b2 += b2_slope;);
1984         LOOP(unit->mRate->mFilterRemain, double y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = y0 - 2.0 * y1 + y2;
1985              y2 = y1; y1 = y0;);
1986 
1987         unit->m_freq = freq;
1988         unit->m_reson = reson;
1989         unit->m_a0 = next_a0;
1990         unit->m_b1 = next_b1;
1991         unit->m_b2 = next_b2;
1992     } else {
1993         LOOP(unit->mRate->mFilterLoops, double y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = y0 - 2.0 * y1 + y2;
1994 
1995              y2 = a0 * ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = y2 - 2.0 * y0 + y1;
1996 
1997              y1 = a0 * ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = y1 - 2.0 * y2 + y0;);
1998         LOOP(unit->mRate->mFilterRemain, double y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = y0 - 2.0 * y1 + y2;
1999              y2 = y1; y1 = y0;);
2000     }
2001     unit->m_y1 = zapgremlins(y1);
2002     unit->m_y2 = zapgremlins(y2);
2003 }
2004 
RHPF_next_1(RHPF * unit,int inNumSamples)2005 void RHPF_next_1(RHPF* unit, int inNumSamples) {
2006     // printf("RHPFs_next_1\n");
2007 
2008     float in = ZIN0(0);
2009     float freq = ZIN0(1);
2010     float reson = ZIN0(2);
2011 
2012     double y1 = unit->m_y1;
2013     double y2 = unit->m_y2;
2014     double a0 = unit->m_a0;
2015     double b1 = unit->m_b1;
2016     double b2 = unit->m_b2;
2017 
2018     if (freq != unit->m_freq || reson != unit->m_reson) {
2019         double qres = sc_max(0.001f, reson);
2020         double pfreq = freq * unit->mRate->mRadiansPerSample;
2021 
2022         double D = tan(pfreq * qres * 0.5f);
2023         double C = ((1. - D) / (1. + D));
2024         double cosf = cos(pfreq);
2025 
2026         b1 = (1. + C) * cosf;
2027         b2 = -C;
2028         a0 = (1. + C + b1) * .25;
2029 
2030         double y0 = a0 * in + b1 * y1 + b2 * y2;
2031         ZOUT0(0) = y0 - 2.0 * y1 + y2;
2032         y2 = y1;
2033         y1 = y0;
2034 
2035         unit->m_freq = freq;
2036         unit->m_reson = reson;
2037         unit->m_a0 = a0;
2038         unit->m_b1 = b1;
2039         unit->m_b2 = b2;
2040     } else {
2041         double y0 = a0 * in + b1 * y1 + b2 * y2;
2042         ZOUT0(0) = y0 - 2.0 * y1 + y2;
2043         y2 = y1;
2044         y1 = y0;
2045     }
2046     unit->m_y1 = zapgremlins(y1);
2047     unit->m_y2 = zapgremlins(y2);
2048 }
2049 
2050 
2051 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2052 
LPF_Ctor(LPF * unit)2053 void LPF_Ctor(LPF* unit) {
2054     if (unit->mBufLength == 1)
2055         SETCALC(LPF_next_1);
2056     else
2057         SETCALC(LPF_next);
2058 
2059     unit->m_a0 = 0.f;
2060     unit->m_b1 = 0.f;
2061     unit->m_b2 = 0.f;
2062     unit->m_y1 = 0.f;
2063     unit->m_y2 = 0.f;
2064     unit->m_freq = uninitializedControl;
2065     LPF_next_1(unit, 1);
2066 }
2067 
2068 
LPF_next(LPF * unit,int inNumSamples)2069 void LPF_next(LPF* unit, int inNumSamples) {
2070     // printf("LPF_next\n");
2071 
2072     float* out = ZOUT(0);
2073     float* in = ZIN(0);
2074     float freq = ZIN0(1);
2075 
2076     double y0;
2077     double y1 = unit->m_y1;
2078     double y2 = unit->m_y2;
2079     double a0 = unit->m_a0;
2080     double b1 = unit->m_b1;
2081     double b2 = unit->m_b2;
2082 
2083     if (freq != unit->m_freq) {
2084         double pfreq = freq * unit->mRate->mRadiansPerSample * 0.5;
2085 
2086         double C = 1.f / tan(pfreq);
2087         double C2 = C * C;
2088         double sqrt2C = C * sqrt2_f;
2089         double next_a0 = 1.f / (1.f + sqrt2C + C2);
2090         double next_b1 = -2.f * (1.f - C2) * next_a0;
2091         double next_b2 = -(1.f - sqrt2C + C2) * next_a0;
2092 
2093         // post("%g %g %g   %g %g   %g %g %g   %g %g\n", *freq, pfreq, qres, D, C, cosf, next_b1, next_b2, next_a0, y1,
2094         // y2);
2095 
2096         double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
2097         double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
2098         double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
2099         LOOP(unit->mRate->mFilterLoops, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 + 2. * y1 + y2);
2100 
2101              y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = a0 * (y2 + 2. * y0 + y1);
2102 
2103              y1 = ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = a0 * (y1 + 2.f * y2 + y0);
2104 
2105              a0 += a0_slope; b1 += b1_slope; b2 += b2_slope;);
2106         LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 + 2. * y1 + y2); y2 = y1;
2107              y1 = y0;);
2108 
2109         unit->m_freq = freq;
2110         unit->m_a0 = next_a0;
2111         unit->m_b1 = next_b1;
2112         unit->m_b2 = next_b2;
2113     } else {
2114         LOOP(unit->mRate->mFilterLoops, y0 = ZXP(in) + b1 * y1 + b2 * y2; float out0 = a0 * (y0 + 2. * y1 + y2);
2115 
2116              y2 = ZXP(in) + b1 * y0 + b2 * y1; float out1 = a0 * (y2 + 2. * y0 + y1);
2117 
2118              y1 = ZXP(in) + b1 * y2 + b2 * y0; float out2 = a0 * (y1 + 2. * y2 + y0); ZXP(out) = out0; ZXP(out) = out1;
2119              ZXP(out) = out2;);
2120         LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 + 2. * y1 + y2); y2 = y1;
2121              y1 = y0;);
2122     }
2123     unit->m_y1 = zapgremlins(y1);
2124     unit->m_y2 = zapgremlins(y2);
2125 }
2126 
LPF_next_1(LPF * unit,int inNumSamples)2127 void LPF_next_1(LPF* unit, int inNumSamples) {
2128     // printf("LPF_next\n");
2129     float in = ZIN0(0);
2130     float freq = ZIN0(1);
2131 
2132     double y0;
2133     double y1 = unit->m_y1;
2134     double y2 = unit->m_y2;
2135     double a0 = unit->m_a0;
2136     double b1 = unit->m_b1;
2137     double b2 = unit->m_b2;
2138 
2139     if (freq != unit->m_freq) {
2140         double pfreq = freq * unit->mRate->mRadiansPerSample * 0.5;
2141 
2142         double C = 1.f / tan(pfreq);
2143         double C2 = C * C;
2144         double sqrt2C = C * sqrt2_f;
2145         a0 = 1.f / (1.f + sqrt2C + C2);
2146         b1 = -2.f * (1.f - C2) * a0;
2147         b2 = -(1.f - sqrt2C + C2) * a0;
2148 
2149         y0 = in + b1 * y1 + b2 * y2;
2150         ZOUT0(0) = a0 * (y0 + 2. * y1 + y2);
2151         y2 = y1;
2152         y1 = y0;
2153 
2154         unit->m_freq = freq;
2155         unit->m_a0 = a0;
2156         unit->m_b1 = b1;
2157         unit->m_b2 = b2;
2158     } else {
2159         y0 = in + b1 * y1 + b2 * y2;
2160         ZOUT0(0) = a0 * (y0 + 2. * y1 + y2);
2161         y2 = y1;
2162         y1 = y0;
2163     }
2164     unit->m_y1 = zapgremlins(y1);
2165     unit->m_y2 = zapgremlins(y2);
2166 }
2167 
2168 
2169 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2170 
HPF_Ctor(HPF * unit)2171 void HPF_Ctor(HPF* unit) {
2172     if (unit->mBufLength == 1)
2173         SETCALC(HPF_next_1);
2174     else
2175         SETCALC(HPF_next);
2176     unit->m_a0 = 0.;
2177     unit->m_b1 = 0.;
2178     unit->m_b2 = 0.;
2179     unit->m_y1 = 0.;
2180     unit->m_y2 = 0.;
2181     unit->m_freq = uninitializedControl;
2182 
2183     HPF_next_1(unit, 1);
2184 }
2185 
2186 
HPF_next(HPF * unit,int inNumSamples)2187 void HPF_next(HPF* unit, int inNumSamples) {
2188     float* out = ZOUT(0);
2189     float* in = ZIN(0);
2190     float freq = ZIN0(1);
2191 
2192     double y1 = unit->m_y1;
2193     double y2 = unit->m_y2;
2194     double a0 = unit->m_a0;
2195     double b1 = unit->m_b1;
2196     double b2 = unit->m_b2;
2197 
2198     if (freq != unit->m_freq) {
2199         double pfreq = freq * unit->mRate->mRadiansPerSample * 0.5;
2200 
2201         double C = tan(pfreq);
2202         double C2 = C * C;
2203         double sqrt2C = C * sqrt2_f;
2204         double next_a0 = 1. / (1. + sqrt2C + C2);
2205         double next_b1 = 2. * (1. - C2) * next_a0;
2206         double next_b2 = -(1. - sqrt2C + C2) * next_a0;
2207 
2208         // post("%g %g %g   %g %g   %g %g %g   %g %g\n", *freq, pfreq, qres, D, C, cosf, next_b1, next_b2, next_a0, y1,
2209         // y2);
2210 
2211         double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
2212         double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
2213         double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
2214         LOOP(unit->mRate->mFilterLoops, double in0 = ZXP(in); double in1 = ZXP(in); double in2 = ZXP(in);
2215 
2216              double y0 = in0 + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - 2. * y1 + y2);
2217 
2218              y2 = in1 + b1 * y0 + b2 * y1; ZXP(out) = a0 * (y2 - 2. * y0 + y1);
2219 
2220              y1 = in2 + b1 * y2 + b2 * y0; ZXP(out) = a0 * (y1 - 2. * y2 + y0);
2221 
2222              a0 += a0_slope; b1 += b1_slope; b2 += b2_slope;);
2223         LOOP(unit->mRate->mFilterRemain, double y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - 2. * y1 + y2);
2224              y2 = y1; y1 = y0;);
2225 
2226         unit->m_freq = freq;
2227         unit->m_a0 = next_a0;
2228         unit->m_b1 = next_b1;
2229         unit->m_b2 = next_b2;
2230     } else {
2231         LOOP(unit->mRate->mFilterLoops, double in0 = ZXP(in); double in1 = ZXP(in); double in2 = ZXP(in);
2232 
2233              double y0 = in0 + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - 2. * y1 + y2);
2234 
2235              y2 = in1 + b1 * y0 + b2 * y1; ZXP(out) = a0 * (y2 - 2. * y0 + y1);
2236 
2237              y1 = in2 + b1 * y2 + b2 * y0; ZXP(out) = a0 * (y1 - 2. * y2 + y0););
2238         LOOP(unit->mRate->mFilterRemain, double y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - 2. * y1 + y2);
2239              y2 = y1; y1 = y0;);
2240     }
2241     unit->m_y1 = zapgremlins(y1);
2242     unit->m_y2 = zapgremlins(y2);
2243 }
2244 
HPF_next_1(HPF * unit,int inNumSamples)2245 void HPF_next_1(HPF* unit, int inNumSamples) {
2246     double in = ZIN0(0);
2247     double freq = ZIN0(1);
2248 
2249     double y1 = unit->m_y1;
2250     double y2 = unit->m_y2;
2251     double a0 = unit->m_a0;
2252     double b1 = unit->m_b1;
2253     double b2 = unit->m_b2;
2254 
2255     if (freq != unit->m_freq) {
2256         double pfreq = freq * unit->mRate->mRadiansPerSample * 0.5f;
2257 
2258         double C = tan(pfreq);
2259         double C2 = C * C;
2260         double sqrt2C = C * sqrt2_f;
2261         a0 = 1. / (1. + sqrt2C + C2);
2262         b1 = 2. * (1. - C2) * a0;
2263         b2 = -(1. - sqrt2C + C2) * a0;
2264 
2265         double y0 = in + b1 * y1 + b2 * y2;
2266         ZOUT0(0) = a0 * (y0 - 2. * y1 + y2);
2267         y2 = y1;
2268         y1 = y0;
2269 
2270         unit->m_freq = freq;
2271         unit->m_a0 = a0;
2272         unit->m_b1 = b1;
2273         unit->m_b2 = b2;
2274     } else {
2275         double y0 = in + b1 * y1 + b2 * y2;
2276         ZOUT0(0) = a0 * (y0 - 2. * y1 + y2);
2277         y2 = y1;
2278         y1 = y0;
2279     }
2280 
2281     unit->m_y1 = zapgremlins(y1);
2282     unit->m_y2 = zapgremlins(y2);
2283 }
2284 
2285 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2286 
BPF_Ctor(BPF * unit)2287 void BPF_Ctor(BPF* unit) {
2288     // printf("BPF_Reset\n");
2289     if (unit->mBufLength == 1) {
2290         SETCALC(BPF_next_1);
2291     } else {
2292         SETCALC(BPF_next);
2293     };
2294     unit->m_a0 = 0.f;
2295     unit->m_b1 = 0.f;
2296     unit->m_b2 = 0.f;
2297     unit->m_y1 = 0.f;
2298     unit->m_y2 = 0.f;
2299     unit->m_freq = uninitializedControl;
2300     unit->m_bw = uninitializedControl;
2301 
2302     BPF_next_1(unit, 1);
2303 }
2304 
BPF_next(BPF * unit,int inNumSamples)2305 void BPF_next(BPF* unit, int inNumSamples) {
2306     // printf("BPF_next\n");
2307 
2308     float* out = ZOUT(0);
2309     float* in = ZIN(0);
2310     float freq = ZIN0(1);
2311     float bw = ZIN0(2);
2312 
2313     double y0;
2314     double y1 = unit->m_y1;
2315     double y2 = unit->m_y2;
2316     double a0 = unit->m_a0;
2317     double b1 = unit->m_b1;
2318     double b2 = unit->m_b2;
2319 
2320     if (freq != unit->m_freq || bw != unit->m_bw) {
2321         double pfreq = freq * unit->mRate->mRadiansPerSample;
2322         double pbw = bw * pfreq * 0.5f;
2323 
2324         double C = 1.f / tan(pbw);
2325         double D = 2.f * cos(pfreq);
2326 
2327         double next_a0 = 1.f / (1.f + C);
2328         double next_b1 = C * D * next_a0;
2329         double next_b2 = (1.f - C) * next_a0;
2330 
2331         double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
2332         double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
2333         double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
2334         LOOP(unit->mRate->mFilterLoops, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - y2);
2335 
2336              y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = a0 * (y2 - y1);
2337 
2338              y1 = ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = a0 * (y1 - y0);
2339 
2340              a0 += a0_slope; b1 += b1_slope; b2 += b2_slope;);
2341         LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - y2); y2 = y1;
2342              y1 = y0;);
2343 
2344         unit->m_freq = freq;
2345         unit->m_bw = bw;
2346         unit->m_a0 = next_a0;
2347         unit->m_b1 = next_b1;
2348         unit->m_b2 = next_b2;
2349     } else {
2350         LOOP(unit->mRate->mFilterLoops, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - y2);
2351 
2352              y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = a0 * (y2 - y1);
2353 
2354              y1 = ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = a0 * (y1 - y0););
2355         LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - y2); y2 = y1;
2356              y1 = y0;);
2357     }
2358     unit->m_y1 = zapgremlins(y1);
2359     unit->m_y2 = zapgremlins(y2);
2360 }
2361 
BPF_next_1(BPF * unit,int inNumSamples)2362 void BPF_next_1(BPF* unit, int inNumSamples) {
2363     // printf("BPF_next_1\n");
2364 
2365     float in = ZIN0(0);
2366     float freq = ZIN0(1);
2367     float bw = ZIN0(2);
2368 
2369     double y0;
2370     double y1 = unit->m_y1;
2371     double y2 = unit->m_y2;
2372     double a0 = unit->m_a0;
2373     double b1 = unit->m_b1;
2374     double b2 = unit->m_b2;
2375 
2376     if (freq != unit->m_freq || bw != unit->m_bw) {
2377         double pfreq = freq * unit->mRate->mRadiansPerSample;
2378         double pbw = bw * pfreq * 0.5;
2379 
2380         double C = 1.f / tan(pbw);
2381         double D = 2.f * cos(pfreq);
2382 
2383         double a0 = 1.f / (1.f + C);
2384         double b1 = C * D * a0;
2385         double b2 = (1.f - C) * a0;
2386 
2387         y0 = in + b1 * y1 + b2 * y2;
2388         ZOUT0(0) = a0 * (y0 - y2);
2389         y2 = y1;
2390         y1 = y0;
2391 
2392         unit->m_freq = freq;
2393         unit->m_bw = bw;
2394         unit->m_a0 = a0;
2395         unit->m_b1 = b1;
2396         unit->m_b2 = b2;
2397     } else {
2398         y0 = in + b1 * y1 + b2 * y2;
2399         ZOUT0(0) = a0 * (y0 - y2);
2400         y2 = y1;
2401         y1 = y0;
2402     }
2403     unit->m_y1 = zapgremlins(y1);
2404     unit->m_y2 = zapgremlins(y2);
2405 }
2406 
2407 
2408 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2409 
BRF_Ctor(BRF * unit)2410 void BRF_Ctor(BRF* unit) {
2411     // printf("BRF_Reset\n");
2412     if (unit->mBufLength == 1) {
2413         SETCALC(BRF_next_1);
2414     } else {
2415         SETCALC(BRF_next);
2416     };
2417     unit->m_a0 = 0.f;
2418     unit->m_a1 = 0.f;
2419     unit->m_b2 = 0.f;
2420     unit->m_y1 = 0.f;
2421     unit->m_y2 = 0.f;
2422     unit->m_freq = uninitializedControl;
2423     unit->m_bw = uninitializedControl;
2424     BRF_next_1(unit, 1);
2425 }
2426 
2427 
BRF_next(BRF * unit,int inNumSamples)2428 void BRF_next(BRF* unit, int inNumSamples) {
2429     // printf("BRF_next\n");
2430 
2431     float* out = ZOUT(0);
2432     float* in = ZIN(0);
2433     float freq = ZIN0(1);
2434     float bw = ZIN0(2);
2435 
2436     double ay;
2437     double y0;
2438     double y1 = unit->m_y1;
2439     double y2 = unit->m_y2;
2440     double a0 = unit->m_a0;
2441     double a1 = unit->m_a1;
2442     double b2 = unit->m_b2;
2443 
2444     if (freq != unit->m_freq || bw != unit->m_bw) {
2445         double pfreq = freq * unit->mRate->mRadiansPerSample;
2446         double pbw = bw * pfreq * 0.5f;
2447 
2448         double C = tan(pbw);
2449         double D = 2.f * cos(pfreq);
2450 
2451         double next_a0 = 1.f / (1.f + C);
2452         double next_a1 = -D * next_a0;
2453         double next_b2 = (1.f - C) * next_a0;
2454 
2455         double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
2456         double a1_slope = (next_a1 - a1) * unit->mRate->mFilterSlope;
2457         double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
2458 
2459         LOOP(unit->mRate->mFilterLoops, ay = a1 * y1; y0 = ZXP(in) - ay - b2 * y2; ZXP(out) = a0 * (y0 + y2) + ay;
2460 
2461              ay = a1 * y0; y2 = ZXP(in) - ay - b2 * y1; ZXP(out) = a0 * (y2 + y1) + ay;
2462 
2463              ay = a1 * y2; y1 = ZXP(in) - ay - b2 * y0; ZXP(out) = a0 * (y1 + y0) + ay;
2464 
2465              a0 += a0_slope; a1 += a1_slope; b2 += b2_slope;);
2466         LOOP(unit->mRate->mFilterRemain, ay = a1 * y1; y0 = ZXP(in) - ay - b2 * y2; ZXP(out) = a0 * (y0 + y2) + ay;
2467              y2 = y1; y1 = y0;);
2468 
2469         unit->m_freq = freq;
2470         unit->m_bw = bw;
2471         unit->m_a0 = next_a0;
2472         unit->m_a1 = next_a1;
2473         unit->m_b2 = next_b2;
2474     } else {
2475         LOOP(unit->mRate->mFilterLoops, ay = a1 * y1; y0 = ZXP(in) - ay - b2 * y2; ZXP(out) = a0 * (y0 + y2) + ay;
2476 
2477              ay = a1 * y0; y2 = ZXP(in) - ay - b2 * y1; ZXP(out) = a0 * (y2 + y1) + ay;
2478 
2479              ay = a1 * y2; y1 = ZXP(in) - ay - b2 * y0; ZXP(out) = a0 * (y1 + y0) + ay;);
2480         LOOP(unit->mRate->mFilterRemain, ay = a1 * y1; y0 = ZXP(in) - ay - b2 * y2; ZXP(out) = a0 * (y0 + y2) + ay;
2481              y2 = y1; y1 = y0;);
2482     }
2483     unit->m_y1 = zapgremlins(y1);
2484     unit->m_y2 = zapgremlins(y2);
2485 }
2486 
2487 
BRF_next_1(BRF * unit,int inNumSamples)2488 void BRF_next_1(BRF* unit, int inNumSamples) {
2489     // printf("BRF_next_1\n");
2490 
2491     float in = ZIN0(0);
2492     float freq = ZIN0(1);
2493     float bw = ZIN0(2);
2494 
2495     double ay;
2496     double y0;
2497     double y1 = unit->m_y1;
2498     double y2 = unit->m_y2;
2499     double a0 = unit->m_a0;
2500     double a1 = unit->m_a1;
2501     double b2 = unit->m_b2;
2502 
2503     if (freq != unit->m_freq || bw != unit->m_bw) {
2504         double pfreq = freq * unit->mRate->mRadiansPerSample;
2505         double pbw = bw * pfreq * 0.5f;
2506 
2507         double C = tan(pbw);
2508         double D = 2.f * cos(pfreq);
2509 
2510         double a0 = 1.f / (1.f + C);
2511         double a1 = -D * a0;
2512         double b2 = (1.f - C) * a0;
2513 
2514         ay = a1 * y1;
2515         y0 = in - ay - b2 * y2;
2516         ZOUT0(0) = a0 * (y0 + y2) + ay;
2517         y2 = y1;
2518         y1 = y0;
2519 
2520         unit->m_freq = freq;
2521         unit->m_bw = bw;
2522         unit->m_a0 = a0;
2523         unit->m_a1 = a1;
2524         unit->m_b2 = b2;
2525     } else {
2526         ay = a1 * y1;
2527         y0 = in - ay - b2 * y2;
2528         ZOUT0(0) = a0 * (y0 + y2) + ay;
2529         y2 = y1;
2530         y1 = y0;
2531     }
2532     unit->m_y1 = zapgremlins(y1);
2533     unit->m_y2 = zapgremlins(y2);
2534 }
2535 
2536 
2537 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2538 
MidEQ_Ctor(MidEQ * unit)2539 void MidEQ_Ctor(MidEQ* unit) {
2540     // printf("MidEQ_Reset\n");
2541     SETCALC(MidEQ_next);
2542     unit->m_a0 = 0.f;
2543     unit->m_b1 = 0.f;
2544     unit->m_b2 = 0.f;
2545     unit->m_y1 = 0.f;
2546     unit->m_y2 = 0.f;
2547     unit->m_freq = uninitializedControl;
2548     unit->m_bw = uninitializedControl;
2549     unit->m_db = uninitializedControl;
2550     PUSH_LOOPVALS
2551     MidEQ_next(unit, 1);
2552     POP_LOOPVALS
2553 }
2554 
2555 
MidEQ_next(MidEQ * unit,int inNumSamples)2556 void MidEQ_next(MidEQ* unit, int inNumSamples) {
2557     // printf("MidEQ_next\n");
2558 
2559     float* out = ZOUT(0);
2560     float* in = ZIN(0);
2561     float freq = ZIN0(1);
2562     float bw = ZIN0(2);
2563     float db = ZIN0(3);
2564 
2565     double y0;
2566     double y1 = unit->m_y1;
2567     double y2 = unit->m_y2;
2568     double a0 = unit->m_a0;
2569     double b1 = unit->m_b1;
2570     double b2 = unit->m_b2;
2571     if (freq != unit->m_freq || bw != unit->m_bw || db != unit->m_db) {
2572         double amp = sc_dbamp(db) - 1.0f;
2573         double pfreq = freq * unit->mRate->mRadiansPerSample;
2574         double pbw = bw * pfreq * 0.5f;
2575 
2576         double C = 1.f / tan(pbw);
2577         double D = 2.f * cos(pfreq);
2578 
2579         double next_a0 = 1.f / (1.f + C);
2580         double next_b1 = C * D * next_a0;
2581         double next_b2 = (1.f - C) * next_a0;
2582         next_a0 *= amp;
2583         double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
2584         double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
2585         double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
2586         double zin;
2587         LOOP(unit->mRate->mFilterLoops, zin = ZXP(in); y0 = zin + b1 * y1 + b2 * y2; ZXP(out) = zin + a0 * (y0 - y2);
2588 
2589              zin = ZXP(in); y2 = zin + b1 * y0 + b2 * y1; ZXP(out) = zin + a0 * (y2 - y1);
2590 
2591              zin = ZXP(in); y1 = zin + b1 * y2 + b2 * y0; ZXP(out) = zin + a0 * (y1 - y0);
2592 
2593              a0 += a0_slope; b1 += b1_slope; b2 += b2_slope;);
2594         LOOP(unit->mRate->mFilterRemain, zin = ZXP(in); y0 = zin + b1 * y1 + b2 * y2; ZXP(out) = zin + a0 * (y0 - y2);
2595              y2 = y1; y1 = y0;);
2596 
2597         unit->m_freq = freq;
2598         unit->m_bw = bw;
2599         unit->m_db = db;
2600         unit->m_a0 = next_a0;
2601         unit->m_b1 = next_b1;
2602         unit->m_b2 = next_b2;
2603     } else {
2604         double zin;
2605         LOOP(unit->mRate->mFilterLoops, zin = ZXP(in); y0 = zin + b1 * y1 + b2 * y2; ZXP(out) = zin + a0 * (y0 - y2);
2606 
2607              zin = ZXP(in); y2 = zin + b1 * y0 + b2 * y1; ZXP(out) = zin + a0 * (y2 - y1);
2608 
2609              zin = ZXP(in); y1 = zin + b1 * y2 + b2 * y0; ZXP(out) = zin + a0 * (y1 - y0););
2610         LOOP(unit->mRate->mFilterRemain, zin = ZXP(in); y0 = zin + b1 * y1 + b2 * y2; ZXP(out) = zin + a0 * (y0 - y2);
2611              y2 = y1; y1 = y0;);
2612     }
2613     unit->m_y1 = zapgremlins(y1);
2614     unit->m_y2 = zapgremlins(y2);
2615 }
2616 
2617 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2618 
2619 static void Median_InitMedian(Median* unit, long size, float value);
2620 static float Median_InsertMedian(Median* unit, float value);
2621 
Median_Ctor(Median * unit)2622 void Median_Ctor(Median* unit) {
2623     // printf("Median_Reset\n");
2624     SETCALC(Median_next);
2625     float in = ZIN0(1);
2626     unit->m_medianSize = sc_clip((int)ZIN0(0), 0, kMAXMEDIANSIZE);
2627     Median_InitMedian(unit, unit->m_medianSize, in);
2628     ZOUT0(0) = Median_InsertMedian(unit, in);
2629 }
2630 
2631 
Median_InsertMedian(Median * unit,float value)2632 float Median_InsertMedian(Median* unit, float value) {
2633     long pos = -1;
2634 
2635     // keeps a sorted list of the previous n=size values
2636     // the oldest is removed and the newest is inserted.
2637     // values between the oldest and the newest are shifted over by one.
2638 
2639     // values and ages are both arrays that are 'size' long.
2640     // the median value is always values[size>>1]
2641 
2642     long last = unit->m_medianSize - 1;
2643     // find oldest bin and age the other bins.
2644     for (int i = 0; i < unit->m_medianSize; ++i) {
2645         if (unit->m_medianAge[i] == last) { // is it the oldest bin ?
2646             pos = i;
2647         } else {
2648             unit->m_medianAge[i]++; // age the bin
2649         }
2650     }
2651     // move values to fill in place of the oldest and make a space for the newest
2652     // search lower if value is too small for the open space
2653     while (pos != 0 && value < unit->m_medianValue[pos - 1]) {
2654         unit->m_medianValue[pos] = unit->m_medianValue[pos - 1];
2655         unit->m_medianAge[pos] = unit->m_medianAge[pos - 1];
2656         pos--;
2657     }
2658     // search higher if value is too big for the open space
2659     while (pos != last && value > unit->m_medianValue[pos + 1]) {
2660         unit->m_medianValue[pos] = unit->m_medianValue[pos + 1];
2661         unit->m_medianAge[pos] = unit->m_medianAge[pos + 1];
2662         pos++;
2663     }
2664     unit->m_medianValue[pos] = value;
2665     unit->m_medianAge[pos] = 0; // this is the newest bin, age = 0
2666     return unit->m_medianValue[unit->m_medianSize >> 1];
2667 }
2668 
Median_InitMedian(Median * unit,long size,float value)2669 void Median_InitMedian(Median* unit, long size, float value) {
2670     // initialize the arrays with the first value
2671     unit->m_medianSize = size;
2672     for (int i = 0; i < size; ++i) {
2673         unit->m_medianValue[i] = value;
2674         unit->m_medianAge[i] = i;
2675     }
2676 }
2677 
Median_next(Median * unit,int inNumSamples)2678 void Median_next(Median* unit, int inNumSamples) {
2679     // printf("Median_next_a\n");
2680 
2681     float* out = ZOUT(0);
2682     float* in = ZIN(1);
2683 
2684     LOOP1(inNumSamples, ZXP(out) = Median_InsertMedian(unit, ZXP(in)););
2685 }
2686 
2687 
2688 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2689 
Resonz_Ctor(Resonz * unit)2690 void Resonz_Ctor(Resonz* unit) {
2691     // printf("Resonz_Reset\n");
2692     SETCALC(Resonz_next);
2693     unit->m_a0 = 0.f;
2694     unit->m_b1 = 0.f;
2695     unit->m_b2 = 0.f;
2696     unit->m_y1 = 0.f;
2697     unit->m_y2 = 0.f;
2698     unit->m_freq = uninitializedControl;
2699     unit->m_rq = 0.f;
2700     PUSH_LOOPVALS
2701     Resonz_next(unit, 1);
2702     POP_LOOPVALS
2703 }
2704 
2705 
Resonz_next(Resonz * unit,int inNumSamples)2706 void Resonz_next(Resonz* unit, int inNumSamples) {
2707     // printf("Resonz_next\n");
2708 
2709     float* out = ZOUT(0);
2710     float* in = ZIN(0);
2711     float freq = ZIN0(1);
2712     float rq = ZIN0(2);
2713 
2714     double y0;
2715     double y1 = unit->m_y1;
2716     double y2 = unit->m_y2;
2717     double a0 = unit->m_a0;
2718     double b1 = unit->m_b1;
2719     double b2 = unit->m_b2;
2720 
2721     if (freq != unit->m_freq || rq != unit->m_rq) {
2722         double ffreq = freq * unit->mRate->mRadiansPerSample;
2723         double B = ffreq * rq;
2724         double R = 1.f - B * 0.5f;
2725         double twoR = 2.f * R;
2726         double R2 = R * R;
2727         double cost = (twoR * cos(ffreq)) / (1.f + R2);
2728         double b1_next = twoR * cost;
2729         double b2_next = -R2;
2730         double a0_next = (1.f - R2) * 0.5f;
2731         double a0_slope = (a0_next - a0) * unit->mRate->mFilterSlope;
2732         double b1_slope = (b1_next - b1) * unit->mRate->mFilterSlope;
2733         double b2_slope = (b2_next - b2) * unit->mRate->mFilterSlope;
2734         LOOP(unit->mRate->mFilterLoops, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - y2);
2735 
2736              y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = a0 * (y2 - y1);
2737 
2738              y1 = ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = a0 * (y1 - y0);
2739 
2740              a0 += a0_slope; b1 += b1_slope; b2 += b2_slope;);
2741         LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - y2); y2 = y1;
2742              y1 = y0;);
2743 
2744         unit->m_freq = freq;
2745         unit->m_rq = rq;
2746         unit->m_a0 = a0_next;
2747         unit->m_b1 = b1_next;
2748         unit->m_b2 = b2_next;
2749     } else {
2750         LOOP(unit->mRate->mFilterLoops, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - y2);
2751 
2752              y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = a0 * (y2 - y1);
2753 
2754              y1 = ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = a0 * (y1 - y0););
2755         LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - y2); y2 = y1;
2756              y1 = y0;);
2757     }
2758     unit->m_y1 = zapgremlins(y1);
2759     unit->m_y2 = zapgremlins(y2);
2760 }
2761 
2762 
2763 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2764 
Ringz_Ctor(Ringz * unit)2765 void Ringz_Ctor(Ringz* unit) {
2766     // printf("Ringz_ctor\n");
2767     SETCALC(Ringz_next);
2768     unit->m_b1 = 0.f;
2769     unit->m_b2 = 0.f;
2770     unit->m_y1 = 0.f;
2771     unit->m_y2 = 0.f;
2772     unit->m_freq = uninitializedControl;
2773     unit->m_decayTime = 0.f;
2774     PUSH_LOOPVALS
2775     Ringz_next(unit, 1);
2776     POP_LOOPVALS
2777 }
2778 
2779 
Ringz_next(Ringz * unit,int inNumSamples)2780 void Ringz_next(Ringz* unit, int inNumSamples) {
2781     // printf("Ringz_next\n");
2782 
2783     float* out = ZOUT(0);
2784     float* in = ZIN(0);
2785     float freq = ZIN0(1);
2786     float decayTime = ZIN0(2);
2787 
2788     double y0;
2789     double y1 = unit->m_y1;
2790     double y2 = unit->m_y2;
2791     double a0 = 0.5f;
2792     double b1 = unit->m_b1;
2793     double b2 = unit->m_b2;
2794 
2795     if (freq != unit->m_freq || decayTime != unit->m_decayTime) {
2796         double ffreq = freq * unit->mRate->mRadiansPerSample;
2797         double R = decayTime == 0.f ? 0.f : exp(log001 / (decayTime * SAMPLERATE));
2798         double twoR = 2.f * R;
2799         double R2 = R * R;
2800         double cost = (twoR * cos(ffreq)) / (1.f + R2);
2801         double b1_next = twoR * cost;
2802         double b2_next = -R2;
2803         double b1_slope = (b1_next - b1) * unit->mRate->mFilterSlope;
2804         double b2_slope = (b2_next - b2) * unit->mRate->mFilterSlope;
2805         LOOP(unit->mRate->mFilterLoops, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - y2);
2806 
2807              y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = a0 * (y2 - y1);
2808 
2809              y1 = ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = a0 * (y1 - y0);
2810 
2811              b1 += b1_slope; b2 += b2_slope;);
2812         LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - y2); y2 = y1;
2813              y1 = y0;);
2814 
2815         unit->m_freq = freq;
2816         unit->m_decayTime = decayTime;
2817         unit->m_b1 = b1_next;
2818         unit->m_b2 = b2_next;
2819     } else {
2820         LOOP(unit->mRate->mFilterLoops, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - y2);
2821 
2822              y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = a0 * (y2 - y1);
2823 
2824              y1 = ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = a0 * (y1 - y0););
2825         LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * (y0 - y2); y2 = y1;
2826              y1 = y0;);
2827     }
2828     unit->m_y1 = zapgremlins(y1);
2829     unit->m_y2 = zapgremlins(y2);
2830 }
2831 
2832 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2833 
Formlet_Ctor(Formlet * unit)2834 void Formlet_Ctor(Formlet* unit) {
2835     // printf("Formlet_Reset\n");
2836     if (unit->mBufLength == 1) {
2837         SETCALC(Formlet_next_1);
2838     } else {
2839         SETCALC(Formlet_next);
2840     };
2841     unit->m_b01 = 0.f;
2842     unit->m_b02 = 0.f;
2843     unit->m_y01 = 0.f;
2844     unit->m_y02 = 0.f;
2845     unit->m_b11 = 0.f;
2846     unit->m_b12 = 0.f;
2847     unit->m_y11 = 0.f;
2848     unit->m_y12 = 0.f;
2849     unit->m_freq = uninitializedControl;
2850     unit->m_attackTime = uninitializedControl;
2851     unit->m_decayTime = uninitializedControl;
2852     Formlet_next_1(unit, 1);
2853 }
2854 
Formlet_next(Formlet * unit,int inNumSamples)2855 void Formlet_next(Formlet* unit, int inNumSamples) {
2856     // printf("Formlet_next\n");
2857 
2858     float* out = ZOUT(0);
2859     float* in = ZIN(0);
2860     float freq = ZIN0(1);
2861     float attackTime = ZIN0(2);
2862     float decayTime = ZIN0(3);
2863 
2864     double y00;
2865     double y10;
2866     double y01 = unit->m_y01;
2867     double y11 = unit->m_y11;
2868     double y02 = unit->m_y02;
2869     double y12 = unit->m_y12;
2870 
2871     double b01 = unit->m_b01;
2872     double b11 = unit->m_b11;
2873     double b02 = unit->m_b02;
2874     double b12 = unit->m_b12;
2875     double ain;
2876 
2877     if (freq != unit->m_freq || decayTime != unit->m_decayTime || attackTime != unit->m_attackTime) {
2878         double ffreq = freq * unit->mRate->mRadiansPerSample;
2879 
2880         double R = decayTime == 0.f ? 0.f : exp(log001 / (decayTime * SAMPLERATE));
2881         double twoR = 2.f * R;
2882         double R2 = R * R;
2883         double cost = (twoR * cos(ffreq)) / (1.f + R2);
2884         double b01_next = twoR * cost;
2885         double b02_next = -R2;
2886         double b01_slope = (b01_next - b01) * unit->mRate->mFilterSlope;
2887         double b02_slope = (b02_next - b02) * unit->mRate->mFilterSlope;
2888 
2889         R = attackTime == 0.f ? 0.f : exp(log001 / (attackTime * SAMPLERATE));
2890         twoR = 2.f * R;
2891         R2 = R * R;
2892         cost = (twoR * cos(ffreq)) / (1.f + R2);
2893         double b11_next = twoR * cost;
2894         double b12_next = -R2;
2895         double b11_slope = (b11_next - b11) * unit->mRate->mFilterSlope;
2896         double b12_slope = (b12_next - b12) * unit->mRate->mFilterSlope;
2897 
2898         LOOP(unit->mRate->mFilterLoops, ain = ZXP(in); y00 = ain + b01 * y01 + b02 * y02;
2899              y10 = ain + b11 * y11 + b12 * y12; ZXP(out) = 0.25 * ((y00 - y02) - (y10 - y12));
2900 
2901              ain = ZXP(in); y02 = ain + b01 * y00 + b02 * y01; y12 = ain + b11 * y10 + b12 * y11;
2902              ZXP(out) = 0.25 * ((y02 - y01) - (y12 - y11));
2903 
2904              ain = ZXP(in); y01 = ain + b01 * y02 + b02 * y00; y11 = ain + b11 * y12 + b12 * y10;
2905              ZXP(out) = 0.25 * ((y01 - y00) - (y11 - y10));
2906 
2907              b01 += b01_slope; b02 += b02_slope; b11 += b11_slope; b12 += b12_slope;);
2908         LOOP(unit->mRate->mFilterRemain, ain = ZXP(in); y00 = ain + b01 * y01 + b02 * y02;
2909              y10 = ain + b11 * y11 + b12 * y12; ZXP(out) = 0.25 * ((y00 - y02) - (y10 - y12)); y02 = y01; y01 = y00;
2910              y12 = y11; y11 = y10;);
2911 
2912         unit->m_freq = freq;
2913         unit->m_attackTime = attackTime;
2914         unit->m_decayTime = decayTime;
2915         unit->m_b01 = b01_next;
2916         unit->m_b02 = b02_next;
2917         unit->m_b11 = b11_next;
2918         unit->m_b12 = b12_next;
2919     } else {
2920         LOOP(unit->mRate->mFilterLoops, ain = ZXP(in); y00 = ain + b01 * y01 + b02 * y02;
2921              y10 = ain + b11 * y11 + b12 * y12; ZXP(out) = 0.25 * ((y00 - y02) - (y10 - y12));
2922 
2923              ain = ZXP(in); y02 = ain + b01 * y00 + b02 * y01; y12 = ain + b11 * y10 + b12 * y11;
2924              ZXP(out) = 0.25 * ((y02 - y01) - (y12 - y11));
2925 
2926              ain = ZXP(in); y01 = ain + b01 * y02 + b02 * y00; y11 = ain + b11 * y12 + b12 * y10;
2927              ZXP(out) = 0.25 * ((y01 - y00) - (y11 - y10)););
2928         LOOP(unit->mRate->mFilterRemain, ain = ZXP(in); y00 = ain + b01 * y01 + b02 * y02;
2929              y10 = ain + b11 * y11 + b12 * y12; ZXP(out) = 0.25 * ((y00 - y02) - (y10 - y12)); y02 = y01; y01 = y00;
2930              y12 = y11; y11 = y10;);
2931     }
2932     unit->m_y01 = y01;
2933     unit->m_y02 = y02;
2934     unit->m_y11 = y11;
2935     unit->m_y12 = y12;
2936 }
2937 
Formlet_next_1(Formlet * unit,int inNumSamples)2938 void Formlet_next_1(Formlet* unit, int inNumSamples) {
2939     // printf("Formlet_next\n");
2940 
2941     float in = ZIN0(0);
2942     float freq = ZIN0(1);
2943     float attackTime = ZIN0(2);
2944     float decayTime = ZIN0(3);
2945 
2946     double y00;
2947     double y10;
2948     double y01 = unit->m_y01;
2949     double y11 = unit->m_y11;
2950     double y02 = unit->m_y02;
2951     double y12 = unit->m_y12;
2952 
2953     double b01 = unit->m_b01;
2954     double b11 = unit->m_b11;
2955     double b02 = unit->m_b02;
2956     double b12 = unit->m_b12;
2957     double ain;
2958 
2959     if (freq != unit->m_freq || decayTime != unit->m_decayTime || attackTime != unit->m_attackTime) {
2960         double ffreq = freq * unit->mRate->mRadiansPerSample;
2961 
2962         double R = decayTime == 0.f ? 0.f : exp(log001 / (decayTime * SAMPLERATE));
2963         double twoR = 2.f * R;
2964         double R2 = R * R;
2965         double cost = (twoR * cos(ffreq)) / (1.f + R2);
2966         b01 = twoR * cost;
2967         b02 = -R2;
2968 
2969         R = attackTime == 0.f ? 0.f : exp(log001 / (attackTime * SAMPLERATE));
2970         twoR = 2.f * R;
2971         R2 = R * R;
2972         cost = (twoR * cos(ffreq)) / (1.f + R2);
2973         b11 = twoR * cost;
2974         b12 = -R2;
2975 
2976         ain = in;
2977         y00 = ain + b01 * y01 + b02 * y02;
2978         y10 = ain + b11 * y11 + b12 * y12;
2979         ZOUT0(0) = 0.25f * ((y00 - y02) - (y10 - y12));
2980 
2981         y02 = y01;
2982         y01 = y00;
2983         y12 = y11;
2984         y11 = y10;
2985 
2986         unit->m_freq = freq;
2987         unit->m_attackTime = attackTime;
2988         unit->m_decayTime = decayTime;
2989         unit->m_b01 = b01;
2990         unit->m_b02 = b02;
2991         unit->m_b11 = b11;
2992         unit->m_b12 = b12;
2993     } else {
2994         ain = in;
2995         y00 = ain + b01 * y01 + b02 * y02;
2996         y10 = ain + b11 * y11 + b12 * y12;
2997         ZOUT0(0) = 0.25f * ((y00 - y02) - (y10 - y12));
2998 
2999         y02 = y01;
3000         y01 = y00;
3001         y12 = y11;
3002         y11 = y10;
3003     }
3004     unit->m_y01 = y01;
3005     unit->m_y02 = y02;
3006     unit->m_y11 = y11;
3007     unit->m_y12 = y12;
3008 }
3009 
3010 
3011 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3012 
FOS_Ctor(FOS * unit)3013 void FOS_Ctor(FOS* unit) {
3014     // printf("FOS_Reset\n");
3015     if (unit->mBufLength == 1) {
3016         SETCALC(FOS_next_1);
3017     } else {
3018         if (INRATE(1) == calc_FullRate && INRATE(2) == calc_FullRate && INRATE(3) == calc_FullRate) {
3019             SETCALC(FOS_next_a);
3020         } else {
3021             SETCALC(FOS_next_k);
3022         }
3023     };
3024     unit->m_y1 = 0.f;
3025     unit->m_a0 = ZIN0(1);
3026     unit->m_a1 = ZIN0(2);
3027     unit->m_b1 = ZIN0(3);
3028     FOS_next_1(unit, 1);
3029 }
3030 
FOS_next_a(FOS * unit,int inNumSamples)3031 void FOS_next_a(FOS* unit, int inNumSamples) {
3032     float* out = ZOUT(0);
3033     float* in = ZIN(0);
3034     float* a0 = ZIN(1);
3035     float* a1 = ZIN(2);
3036     float* b1 = ZIN(3);
3037 
3038     double y1 = unit->m_y1;
3039     LOOP1(inNumSamples, double y0 = ZXP(in) + ZXP(b1) * y1; ZXP(out) = ZXP(a0) * y0 + ZXP(a1) * y1; y1 = y0;);
3040     unit->m_y1 = zapgremlins(y1);
3041 }
3042 
FOS_next_1(FOS * unit,int inNumSamples)3043 void FOS_next_1(FOS* unit, int inNumSamples) {
3044     float in = ZIN0(0);
3045     float a0 = ZIN0(1);
3046     float a1 = ZIN0(2);
3047     float b1 = ZIN0(3);
3048 
3049     double y1 = unit->m_y1;
3050 
3051     double y0 = in + b1 * y1;
3052     ZOUT0(0) = a0 * y0 + a1 * y1;
3053     y1 = y0;
3054 
3055     unit->m_y1 = zapgremlins(y1);
3056 }
3057 
3058 
FOS_next_k(FOS * unit,int inNumSamples)3059 void FOS_next_k(FOS* unit, int inNumSamples) {
3060     float* out = ZOUT(0);
3061     float* in = ZIN(0);
3062     float next_a0 = ZIN0(1);
3063     float next_a1 = ZIN0(2);
3064     float next_b1 = ZIN0(3);
3065 
3066     double y1 = unit->m_y1;
3067     double a0 = unit->m_a0;
3068     double a1 = unit->m_a1;
3069     double b1 = unit->m_b1;
3070     double a0_slope = CALCSLOPE(next_a0, a0);
3071     double a1_slope = CALCSLOPE(next_a1, a1);
3072     double b1_slope = CALCSLOPE(next_b1, b1);
3073     LOOP1(inNumSamples, double y0 = ZXP(in) + b1 * y1; ZXP(out) = a0 * y0 + a1 * y1; y1 = y0;
3074 
3075           a0 += a0_slope; a1 += a1_slope; b1 += b1_slope;);
3076     unit->m_y1 = zapgremlins(y1);
3077     unit->m_a0 = next_a0;
3078     unit->m_a1 = next_a1;
3079     unit->m_b1 = next_b1;
3080 }
3081 
3082 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3083 
SOS_Ctor(SOS * unit)3084 void SOS_Ctor(SOS* unit) {
3085     // printf("SOS_Reset\n");
3086     if (unit->mBufLength != 1) {
3087         if (INRATE(1) == calc_FullRate && INRATE(2) == calc_FullRate && INRATE(3) == calc_FullRate
3088             && INRATE(4) == calc_FullRate && INRATE(5) == calc_FullRate) {
3089             SETCALC(SOS_next_a);
3090         } else if (INRATE(1) == calc_ScalarRate && INRATE(2) == calc_ScalarRate && INRATE(3) == calc_ScalarRate
3091                    && INRATE(4) == calc_ScalarRate && INRATE(5) == calc_ScalarRate) {
3092             SETCALC(SOS_next_i);
3093         } else {
3094             SETCALC(SOS_next_k);
3095         }
3096     } else {
3097         SETCALC(SOS_next_1);
3098         //	printf("SOS_next_1\n");
3099     }
3100     unit->m_y1 = 0.f;
3101     unit->m_y2 = 0.f;
3102     unit->m_a0 = ZIN0(1);
3103     unit->m_a1 = ZIN0(2);
3104     unit->m_a2 = ZIN0(3);
3105     unit->m_b1 = ZIN0(4);
3106     unit->m_b2 = ZIN0(5);
3107     SOS_next_1(unit, 1);
3108 }
3109 
SOS_next_a(SOS * unit,int inNumSamples)3110 void SOS_next_a(SOS* unit, int inNumSamples) {
3111     float* out = ZOUT(0);
3112     float* in = ZIN(0);
3113     float* a0 = ZIN(1);
3114     float* a1 = ZIN(2);
3115     float* a2 = ZIN(3);
3116     float* b1 = ZIN(4);
3117     float* b2 = ZIN(5);
3118 
3119     double y0;
3120     double y1 = unit->m_y1;
3121     double y2 = unit->m_y2;
3122     LOOP(unit->mRate->mFilterLoops, y0 = ZXP(in) + ZXP(b1) * y1 + ZXP(b2) * y2;
3123          ZXP(out) = ZXP(a0) * y0 + ZXP(a1) * y1 + ZXP(a2) * y2;
3124 
3125          y2 = ZXP(in) + ZXP(b1) * y0 + ZXP(b2) * y1; ZXP(out) = ZXP(a0) * y2 + ZXP(a1) * y0 + ZXP(a2) * y1;
3126 
3127          y1 = ZXP(in) + ZXP(b1) * y2 + ZXP(b2) * y0; ZXP(out) = ZXP(a0) * y1 + ZXP(a1) * y2 + ZXP(a2) * y0;);
3128     LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + ZXP(b1) * y1 + ZXP(b2) * y2;
3129          ZXP(out) = ZXP(a0) * y0 + ZXP(a1) * y1 + ZXP(a2) * y2; y2 = y1; y1 = y0;);
3130 
3131     unit->m_y1 = zapgremlins(y1);
3132     unit->m_y2 = zapgremlins(y2);
3133 }
3134 
SOS_next_k(SOS * unit,int inNumSamples)3135 void SOS_next_k(SOS* unit, int inNumSamples) {
3136     float* out = ZOUT(0);
3137     float* in = ZIN(0);
3138     float next_a0 = ZIN0(1);
3139     float next_a1 = ZIN0(2);
3140     float next_a2 = ZIN0(3);
3141     float next_b1 = ZIN0(4);
3142     float next_b2 = ZIN0(5);
3143 
3144     double y0;
3145     double y1 = unit->m_y1;
3146     double y2 = unit->m_y2;
3147     double a0 = unit->m_a0;
3148     double a1 = unit->m_a1;
3149     double a2 = unit->m_a2;
3150     double b1 = unit->m_b1;
3151     double b2 = unit->m_b2;
3152     double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
3153     double a1_slope = (next_a1 - a1) * unit->mRate->mFilterSlope;
3154     double a2_slope = (next_a2 - a2) * unit->mRate->mFilterSlope;
3155     double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
3156     double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
3157     LOOP(unit->mRate->mFilterLoops, float in0 = ZXP(in); float in1 = ZXP(in); float in2 = ZXP(in);
3158          y0 = in0 + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
3159 
3160          y2 = in1 + b1 * y0 + b2 * y1; ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
3161 
3162          y1 = in2 + b1 * y2 + b2 * y0; ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
3163 
3164          a0 += a0_slope; a1 += a1_slope; a2 += a2_slope; b1 += b1_slope; b2 += b2_slope;);
3165     LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2; y2 = y1;
3166          y1 = y0;);
3167 
3168     unit->m_a0 = a0;
3169     unit->m_a1 = a1;
3170     unit->m_a2 = a2;
3171     unit->m_b1 = b1;
3172     unit->m_b2 = b2;
3173     unit->m_y1 = zapgremlins(y1);
3174     unit->m_y2 = zapgremlins(y2);
3175 }
3176 
SOS_next_i(SOS * unit,int inNumSamples)3177 void SOS_next_i(SOS* unit, int inNumSamples) {
3178     float* out = ZOUT(0);
3179     float* in = ZIN(0);
3180 
3181     double y0;
3182     double y1 = unit->m_y1;
3183     double y2 = unit->m_y2;
3184     double a0 = unit->m_a0;
3185     double a1 = unit->m_a1;
3186     double a2 = unit->m_a2;
3187     double b1 = unit->m_b1;
3188     double b2 = unit->m_b2;
3189     LOOP(unit->mRate->mFilterLoops, float in0 = ZXP(in); float in1 = ZXP(in); float in2 = ZXP(in);
3190          y0 = in0 + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
3191 
3192          y2 = in1 + b1 * y0 + b2 * y1; ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
3193 
3194          y1 = in2 + b1 * y2 + b2 * y0; ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;);
3195     LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2; y2 = y1;
3196          y1 = y0;);
3197 
3198     unit->m_y1 = zapgremlins(y1);
3199     unit->m_y2 = zapgremlins(y2);
3200 }
3201 
3202 
SOS_next_1(SOS * unit,int inNumSamples)3203 void SOS_next_1(SOS* unit, int inNumSamples) // optimized for SOS.kr
3204 {
3205     float in = ZIN0(0);
3206     float a0 = ZIN0(1);
3207     float a1 = ZIN0(2);
3208     float a2 = ZIN0(3);
3209     float b1 = ZIN0(4);
3210     float b2 = ZIN0(5);
3211 
3212     double y0;
3213     double y1 = unit->m_y1;
3214     double y2 = unit->m_y2;
3215 
3216     y0 = in + b1 * y1 + b2 * y2;
3217     ZOUT0(0) = a0 * y0 + a1 * y1 + a2 * y2;
3218     y2 = y1;
3219     y1 = y0;
3220 
3221     unit->m_y1 = zapgremlins(y1);
3222     unit->m_y2 = zapgremlins(y2);
3223 }
3224 
3225 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3226 
Compander_Ctor(Compander * unit)3227 void Compander_Ctor(Compander* unit) {
3228     // printf("Compander_Reset\n");
3229     SETCALC(Compander_next);
3230     unit->m_clamp = 0.f;
3231     unit->m_clampcoef = 0.f;
3232     unit->m_relax = 0.f;
3233     unit->m_relaxcoef = 0.f;
3234     unit->m_prevmaxval = 0.f;
3235     unit->m_gain = 0.f;
3236     Compander_next(unit, 1);
3237 }
3238 
Compander_next(Compander * unit,int inNumSamples)3239 void Compander_next(Compander* unit, int inNumSamples) {
3240     float* out = ZOUT(0);
3241     float* in = ZIN(0);
3242     float* control = ZIN(1);
3243     float thresh = ZIN0(2);
3244     float slope_below = ZIN0(3);
3245     float slope_above = ZIN0(4);
3246     float clamp = ZIN0(5);
3247     float relax = ZIN0(6);
3248 
3249     if (clamp != unit->m_clamp) {
3250         unit->m_clampcoef = clamp == 0.0 ? 0.0 : exp(log1 / (clamp * SAMPLERATE));
3251         unit->m_clamp = clamp;
3252     }
3253     if (relax != unit->m_relax) {
3254         unit->m_relaxcoef = relax == 0.0 ? 0.0 : exp(log1 / (relax * SAMPLERATE));
3255         unit->m_relax = relax;
3256     }
3257 
3258     float gain = unit->m_gain;
3259 
3260     float relaxcoef = unit->m_relaxcoef;
3261     float clampcoef = unit->m_clampcoef;
3262 
3263     float prevmaxval = unit->m_prevmaxval;
3264 
3265     float val;
3266     LOOP1(
3267         inNumSamples, val = std::abs(ZXP(control)); if (val < prevmaxval) {
3268             val = val + (prevmaxval - val) * relaxcoef;
3269         } else { val = val + (prevmaxval - val) * clampcoef; } prevmaxval = val;);
3270 
3271     unit->m_prevmaxval = prevmaxval;
3272 
3273     float next_gain; //,absx;
3274     if (prevmaxval < thresh) {
3275         if (slope_below == 1.f) {
3276             next_gain = 1.f;
3277         } else {
3278             next_gain = pow(prevmaxval / thresh, slope_below - 1.f);
3279             // blows up here
3280             float32 absx = std::abs(next_gain);
3281             // zap gremlins, but returns 0. if gain is too small and 1. if gain is too big
3282             next_gain = (absx < (float32)1e-15) ? (float32)0. : (absx > (float32)1e15) ? (float32)1. : next_gain;
3283         }
3284     } else {
3285         if (slope_above == 1.f) {
3286             next_gain = 1.f;
3287         } else {
3288             next_gain = pow(prevmaxval / thresh, slope_above - 1.f);
3289         }
3290     }
3291 
3292     float gain_slope = CALCSLOPE(next_gain, gain);
3293     LOOP1(inNumSamples, ZXP(out) = ZXP(in) * gain; gain += gain_slope;);
3294     unit->m_gain = gain;
3295 }
3296 
3297 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3298 
3299 
Normalizer_Dtor(Normalizer * unit)3300 void Normalizer_Dtor(Normalizer* unit) { RTFree(unit->mWorld, unit->m_table); }
3301 
Normalizer_Ctor(Normalizer * unit)3302 void Normalizer_Ctor(Normalizer* unit) {
3303     SETCALC(Normalizer_next);
3304     // printf("Normalizer_Reset\n");
3305 
3306     float dur = ZIN0(2);
3307     unit->m_bufsize = (long)(ceil(dur * SAMPLERATE));
3308     long allocsize = unit->m_bufsize * 3;
3309     // allocsize = NEXTPOWEROFTWO(allocsize);
3310 
3311     unit->m_table = (float*)RTAlloc(unit->mWorld, allocsize * sizeof(float));
3312 
3313     unit->m_pos = 0;
3314     unit->m_flips = 0;
3315     unit->m_level = 1.f;
3316     unit->m_slope = 0.f;
3317     unit->m_prevmaxval = 0.0;
3318     unit->m_curmaxval = 0.0;
3319     unit->m_slopefactor = 1.f / unit->m_bufsize;
3320 
3321     unit->m_xinbuf = unit->m_table - ZOFF;
3322     unit->m_xmidbuf = unit->m_xinbuf + unit->m_bufsize;
3323     unit->m_xoutbuf = unit->m_xmidbuf + unit->m_bufsize;
3324     Normalizer_next(unit, 1);
3325 }
3326 
3327 
Normalizer_next(Normalizer * unit,int inNumSamples)3328 void Normalizer_next(Normalizer* unit, int inNumSamples) {
3329     float* out = ZOUT(0);
3330     float* in = ZIN(0);
3331     float amp = ZIN0(1);
3332 
3333     long pos = unit->m_pos;
3334     float slope = unit->m_slope;
3335     float level = unit->m_level;
3336     float curmaxval = unit->m_curmaxval;
3337     float val;
3338 
3339     long bufsize = unit->m_bufsize;
3340     long buf_remain = bufsize - pos;
3341 
3342     long remain = inNumSamples;
3343     while (remain > 0) {
3344         long nsmps = sc_min(remain, buf_remain);
3345         float* xinbuf = unit->m_xinbuf + pos;
3346         float* xoutbuf = unit->m_xoutbuf + pos;
3347         if (unit->m_flips >= 2) {
3348             LOOP(nsmps, ZXP(xinbuf) = val = ZXP(in); ZXP(out) = level * ZXP(xoutbuf); level += slope;
3349                  val = std::abs(val); if (val > curmaxval) curmaxval = val;);
3350         } else {
3351             LOOP(nsmps, ZXP(xinbuf) = val = ZXP(in); ZXP(out) = 0.f; level += slope; val = std::abs(val);
3352                  if (val > curmaxval) curmaxval = val;);
3353         }
3354         pos += nsmps;
3355         if (pos >= bufsize) {
3356             pos = 0;
3357             buf_remain = bufsize;
3358 
3359             float maxval2 = sc_max(unit->m_prevmaxval, curmaxval);
3360             unit->m_prevmaxval = curmaxval;
3361             unit->m_curmaxval = curmaxval = 0.f;
3362 
3363             float next_level;
3364             if (maxval2 <= 0.00001f)
3365                 next_level = 100000.f * amp;
3366             else
3367                 next_level = amp / maxval2;
3368 
3369             slope = unit->m_slope = (next_level - level) * unit->m_slopefactor;
3370 
3371             float* temp = unit->m_xoutbuf;
3372             unit->m_xoutbuf = unit->m_xmidbuf;
3373             unit->m_xmidbuf = unit->m_xinbuf;
3374             unit->m_xinbuf = temp;
3375 
3376             unit->m_flips++;
3377         }
3378         remain -= nsmps;
3379     }
3380 
3381     unit->m_pos = pos;
3382     unit->m_level = level;
3383     unit->m_curmaxval = curmaxval;
3384 }
3385 
3386 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3387 
Limiter_Dtor(Limiter * unit)3388 void Limiter_Dtor(Limiter* unit) { RTFree(unit->mWorld, unit->m_table); }
3389 
Limiter_Ctor(Limiter * unit)3390 void Limiter_Ctor(Limiter* unit) {
3391     // printf("Limiter_Reset\n");
3392     SETCALC(Limiter_next);
3393 
3394     float dur = ZIN0(2);
3395     unit->m_bufsize = (long)(ceil(dur * SAMPLERATE));
3396     long allocsize = unit->m_bufsize * 3;
3397     allocsize = NEXTPOWEROFTWO(allocsize);
3398 
3399     unit->m_table = (float*)RTAlloc(unit->mWorld, allocsize * sizeof(float));
3400 
3401     unit->m_flips = 0;
3402     unit->m_pos = 0;
3403     unit->m_slope = 0.f;
3404     unit->m_level = 1.f;
3405     unit->m_prevmaxval = 0.0;
3406     unit->m_curmaxval = 0.0;
3407     unit->m_slopefactor = 1.f / unit->m_bufsize;
3408 
3409     unit->m_xinbuf = unit->m_table - ZOFF;
3410     unit->m_xmidbuf = unit->m_xinbuf + unit->m_bufsize;
3411     unit->m_xoutbuf = unit->m_xmidbuf + unit->m_bufsize;
3412     Limiter_next(unit, 1);
3413 }
3414 
3415 
Limiter_next(Limiter * unit,int inNumSamples)3416 void Limiter_next(Limiter* unit, int inNumSamples) {
3417     float* out = ZOUT(0);
3418     float* in = ZIN(0);
3419     float amp = ZIN0(1);
3420 
3421     long pos = unit->m_pos;
3422     float slope = unit->m_slope;
3423     float level = unit->m_level;
3424     float curmaxval = unit->m_curmaxval;
3425     float val;
3426 
3427     long bufsize = unit->m_bufsize;
3428     long buf_remain = bufsize - pos;
3429 
3430     long remain = inNumSamples;
3431     while (remain > 0) {
3432         long nsmps = sc_min(remain, buf_remain);
3433         float* xinbuf = unit->m_xinbuf + pos;
3434         float* xoutbuf = unit->m_xoutbuf + pos;
3435         if (unit->m_flips >= 2) {
3436             LOOP(nsmps, ZXP(xinbuf) = val = ZXP(in); ZXP(out) = level * ZXP(xoutbuf); level += slope;
3437                  val = std::abs(val); if (val > curmaxval) curmaxval = val;);
3438         } else {
3439             LOOP(nsmps, ZXP(xinbuf) = val = ZXP(in); ZXP(out) = 0.f; level += slope; val = std::abs(val);
3440                  if (val > curmaxval) curmaxval = val;);
3441         }
3442         pos += nsmps;
3443         if (pos >= bufsize) {
3444             pos = 0;
3445             buf_remain = bufsize;
3446 
3447             float maxval2 = sc_max(unit->m_prevmaxval, curmaxval);
3448             unit->m_prevmaxval = curmaxval;
3449             unit->m_curmaxval = curmaxval = 0.f;
3450 
3451             float next_level;
3452             if (maxval2 > amp)
3453                 next_level = amp / maxval2;
3454             else
3455                 next_level = 1.0;
3456 
3457             slope = unit->m_slope = (next_level - level) * unit->m_slopefactor;
3458 
3459             float* temp = unit->m_xoutbuf;
3460             unit->m_xoutbuf = unit->m_xmidbuf;
3461             unit->m_xmidbuf = unit->m_xinbuf;
3462             unit->m_xinbuf = temp;
3463 
3464             unit->m_flips++;
3465         }
3466         remain -= nsmps;
3467     }
3468 
3469     unit->m_pos = pos;
3470     unit->m_level = level;
3471     unit->m_curmaxval = curmaxval;
3472 }
3473 
3474 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3475 
Amplitude_Ctor(Amplitude * unit)3476 void Amplitude_Ctor(Amplitude* unit) {
3477     if (INRATE(1) != calc_ScalarRate || INRATE(2) != calc_ScalarRate) {
3478         if (INRATE(0) == calc_FullRate && unit->mCalcRate == calc_BufRate) {
3479             SETCALC(Amplitude_next_atok_kk);
3480         } else {
3481             SETCALC(Amplitude_next_kk);
3482         }
3483 
3484     } else {
3485         if (INRATE(0) == calc_FullRate && unit->mCalcRate == calc_BufRate) {
3486             SETCALC(Amplitude_next_atok);
3487         } else {
3488             SETCALC(Amplitude_next);
3489         }
3490     }
3491 
3492     float clamp = ZIN0(1);
3493     unit->m_clampcoef = clamp == 0.0 ? 0.0 : exp(log1 / (clamp * SAMPLERATE));
3494 
3495     float relax = ZIN0(2);
3496     unit->m_relaxcoef = relax == 0.0 ? 0.0 : exp(log1 / (relax * SAMPLERATE));
3497 
3498     unit->m_previn = std::abs(ZIN0(0));
3499     Amplitude_next(unit, 1);
3500 }
3501 
Amplitude_next(Amplitude * unit,int inNumSamples)3502 void Amplitude_next(Amplitude* unit, int inNumSamples) {
3503     float* out = ZOUT(0);
3504     float* in = ZIN(0);
3505 
3506     float relaxcoef = unit->m_relaxcoef;
3507     float clampcoef = unit->m_clampcoef;
3508     float previn = unit->m_previn;
3509 
3510     float val;
3511     LOOP1(
3512         inNumSamples, val = std::abs(ZXP(in)); if (val < previn) { val = val + (previn - val) * relaxcoef; } else {
3513             val = val + (previn - val) * clampcoef;
3514         } ZXP(out) = previn = val;);
3515 
3516     unit->m_previn = previn;
3517 }
3518 
Amplitude_next_atok(Amplitude * unit,int inNumSamples)3519 void Amplitude_next_atok(Amplitude* unit, int inNumSamples) {
3520     float* in = ZIN(0);
3521 
3522     float relaxcoef = unit->m_relaxcoef;
3523     float clampcoef = unit->m_clampcoef;
3524     float previn = unit->m_previn;
3525 
3526     float val;
3527     LOOP1(
3528         FULLBUFLENGTH, val = std::abs(ZXP(in)); if (val < previn) { val = val + (previn - val) * relaxcoef; } else {
3529             val = val + (previn - val) * clampcoef;
3530         } previn = val;);
3531     ZOUT0(0) = val;
3532 
3533     unit->m_previn = previn;
3534 }
3535 
Amplitude_next_kk(Amplitude * unit,int inNumSamples)3536 void Amplitude_next_kk(Amplitude* unit, int inNumSamples) {
3537     float* out = ZOUT(0);
3538     float* in = ZIN(0);
3539     float relaxcoef, clampcoef;
3540 
3541     if (ZIN0(1) != unit->m_clamp_in) {
3542         clampcoef = unit->m_clampcoef = exp(log1 / (ZIN0(1) * SAMPLERATE));
3543         unit->m_clamp_in = ZIN0(1);
3544     } else {
3545         clampcoef = unit->m_clampcoef;
3546     }
3547 
3548     if (ZIN0(2) != unit->m_relax_in) {
3549         relaxcoef = unit->m_relaxcoef = exp(log1 / (ZIN0(2) * SAMPLERATE));
3550         unit->m_relax_in = ZIN0(2);
3551     } else {
3552         relaxcoef = unit->m_relaxcoef;
3553     }
3554 
3555     float previn = unit->m_previn;
3556 
3557     float val;
3558     LOOP1(
3559         inNumSamples, val = std::abs(ZXP(in)); if (val < previn) { val = val + (previn - val) * relaxcoef; } else {
3560             val = val + (previn - val) * clampcoef;
3561         } ZXP(out) = previn = val;);
3562 
3563     unit->m_previn = previn;
3564 }
3565 
Amplitude_next_atok_kk(Amplitude * unit,int inNumSamples)3566 void Amplitude_next_atok_kk(Amplitude* unit, int inNumSamples) {
3567     float* in = ZIN(0);
3568     float relaxcoef, clampcoef;
3569 
3570     if (ZIN0(1) != unit->m_clamp_in) {
3571         clampcoef = unit->m_clampcoef = exp(log1 / (ZIN0(1) * SAMPLERATE));
3572         unit->m_clamp_in = ZIN0(1);
3573     } else {
3574         clampcoef = unit->m_clampcoef;
3575     }
3576 
3577     if (ZIN0(2) != unit->m_relax_in) {
3578         relaxcoef = unit->m_relaxcoef = exp(log1 / (ZIN0(2) * SAMPLERATE));
3579         unit->m_relax_in = ZIN0(2);
3580     } else {
3581         relaxcoef = unit->m_relaxcoef;
3582     }
3583 
3584     float previn = unit->m_previn;
3585 
3586     float val;
3587     LOOP1(
3588         FULLBUFLENGTH, val = std::abs(ZXP(in)); if (val < previn) { val = val + (previn - val) * relaxcoef; } else {
3589             val = val + (previn - val) * clampcoef;
3590         } previn = val;);
3591     ZOUT0(0) = val;
3592 
3593     unit->m_previn = previn;
3594 }
3595 
3596 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3597 
DetectSilence_Ctor(DetectSilence * unit)3598 void DetectSilence_Ctor(DetectSilence* unit) {
3599     // printf("DetectSilence_Reset\n");
3600     if (INRATE(1) == calc_ScalarRate && INRATE(2) == calc_ScalarRate) {
3601         unit->mThresh = ZIN0(1);
3602         unit->mEndCounter = (int32)(SAMPLERATE * ZIN0(2));
3603         SETCALC(DetectSilence_next);
3604     } else {
3605         SETCALC(DetectSilence_next_k);
3606     }
3607     unit->mCounter = -1;
3608     ClearUnitOutputs(unit, 1);
3609 }
3610 
3611 
DetectSilence_next(DetectSilence * unit,int inNumSamples)3612 void DetectSilence_next(DetectSilence* unit, int inNumSamples) {
3613     float thresh = unit->mThresh;
3614     int counter = unit->mCounter;
3615     float val;
3616 
3617     float* in = IN(0);
3618     float* out = OUT(0);
3619     for (int i = 0; i < inNumSamples; ++i) {
3620         val = std::abs(*in++);
3621         if (val > thresh) {
3622             counter = 0;
3623             *out++ = 0.f;
3624         } else if (counter >= 0) {
3625             if (++counter >= unit->mEndCounter) {
3626                 DoneAction((int)ZIN0(3), unit);
3627                 *out++ = 1.f;
3628             } else {
3629                 *out++ = 0.f;
3630             }
3631         } else
3632             *out++ = 0.f;
3633     }
3634     unit->mCounter = counter;
3635 }
3636 
DetectSilence_next_k(DetectSilence * unit,int inNumSamples)3637 void DetectSilence_next_k(DetectSilence* unit, int inNumSamples) {
3638     float thresh = ZIN0(1);
3639     int endCounter = (int32)(SAMPLERATE * ZIN0(2));
3640     int counter = unit->mCounter;
3641     float val;
3642     float* in = IN(0);
3643     float* out = OUT(0);
3644 
3645     for (int i = 0; i < inNumSamples; ++i) {
3646         val = std::abs(*in++);
3647         if (val > thresh) {
3648             counter = 0;
3649             *out++ = 0.f;
3650         } else if (counter >= 0) {
3651             if (++counter >= endCounter) {
3652                 DoneAction((int)ZIN0(3), unit);
3653                 *out++ = 1.f;
3654             } else {
3655                 *out++ = 0.f;
3656             }
3657         } else
3658             *out++ = 0.f;
3659     }
3660     unit->mCounter = counter;
3661 }
3662 
3663 
3664 ///////////////////////////////////////////////////////////////////////////////////////////////////////
3665 // Based on HilbertIIR from SC2
3666 // a 12 pole (6 per side) Hilbert IIR filter
3667 // based on Sean Costello and Bernie Hutchins
3668 // created by jl anderson - 7 jan 2001
3669 // UGen created by Josh Parmenter
3670 
3671 #define HILBERT_FILTER                                                                                                 \
3672     y0_1 = thisin - (coefs[0]) * y1[0];                                                                                \
3673     ay1 = coefs[0] * y0_1 + 1 * y1[0];                                                                                 \
3674     y1[0] = y0_1;                                                                                                      \
3675     y0_2 = ay1 - (coefs[1]) * y1[1];                                                                                   \
3676     ay2 = coefs[1] * y0_2 + 1 * y1[1];                                                                                 \
3677     y1[1] = y0_2;                                                                                                      \
3678     y0_3 = ay2 - (coefs[2]) * y1[2];                                                                                   \
3679     ay3 = coefs[2] * y0_3 + 1 * y1[2];                                                                                 \
3680     y1[2] = y0_3;                                                                                                      \
3681     y0_4 = ay3 - (coefs[3]) * y1[3];                                                                                   \
3682     ay4 = coefs[3] * y0_4 + 1 * y1[3];                                                                                 \
3683     y1[3] = y0_4;                                                                                                      \
3684     y0_5 = ay4 - (coefs[4]) * y1[4];                                                                                   \
3685     ay5 = coefs[4] * y0_5 + 1 * y1[4];                                                                                 \
3686     y1[4] = y0_5;                                                                                                      \
3687     y0_6 = ay5 - (coefs[5]) * y1[5];                                                                                   \
3688     ay6 = coefs[5] * y0_6 + 1 * y1[5];                                                                                 \
3689     y1[5] = y0_6;                                                                                                      \
3690                                                                                                                        \
3691     y0_7 = thisin - (coefs[6]) * y1[6];                                                                                \
3692     ay7 = coefs[6] * y0_7 + 1 * y1[6];                                                                                 \
3693     y1[6] = y0_7;                                                                                                      \
3694     y0_8 = ay7 - (coefs[7]) * y1[7];                                                                                   \
3695     ay8 = coefs[7] * y0_8 + 1 * y1[7];                                                                                 \
3696     y1[7] = y0_8;                                                                                                      \
3697     y0_9 = ay8 - (coefs[8]) * y1[8];                                                                                   \
3698     ay9 = coefs[8] * y0_9 + 1 * y1[8];                                                                                 \
3699     y1[8] = y0_9;                                                                                                      \
3700     y0_10 = ay9 - (coefs[9]) * y1[9];                                                                                  \
3701     ay10 = coefs[9] * y0_10 + 1 * y1[9];                                                                               \
3702     y1[9] = y0_10;                                                                                                     \
3703     y0_11 = ay10 - (coefs[10]) * y1[10];                                                                               \
3704     ay11 = coefs[10] * y0_11 + 1 * y1[10];                                                                             \
3705     y1[10] = y0_11;                                                                                                    \
3706     y0_12 = ay11 - (coefs[11]) * y1[11];                                                                               \
3707     ay12 = coefs[11] * y0_12 + 1 * y1[11];                                                                             \
3708     y1[11] = y0_12;
3709 
3710 
Hilbert_Ctor(Hilbert * unit)3711 void Hilbert_Ctor(Hilbert* unit) {
3712     // calculate coefs based on SampleRate, store in the struct
3713     SETCALC(Hilbert_next);
3714 
3715     double gamconst = (15.0f * pi_f) / SAMPLERATE;
3716     double gamma01 = gamconst * 0.3609f;
3717     double gamma02 = gamconst * 2.7412f;
3718     double gamma03 = gamconst * 11.1573f;
3719     double gamma04 = gamconst * 44.7581f;
3720     double gamma05 = gamconst * 179.6242f;
3721     double gamma06 = gamconst * 798.4578f;
3722     double gamma07 = gamconst * 1.2524f;
3723     double gamma08 = gamconst * 5.5671f;
3724     double gamma09 = gamconst * 22.3423f;
3725     double gamma10 = gamconst * 89.6271f;
3726     double gamma11 = gamconst * 364.7914f;
3727     double gamma12 = gamconst * 2770.1114f;
3728     unit->m_coefs[0] = (gamma01 - 1.f) / (gamma01 + 1.f);
3729     unit->m_coefs[1] = (gamma02 - 1.f) / (gamma02 + 1.f);
3730     unit->m_coefs[2] = (gamma03 - 1.f) / (gamma03 + 1.f);
3731     unit->m_coefs[3] = (gamma04 - 1.f) / (gamma04 + 1.f);
3732     unit->m_coefs[4] = (gamma05 - 1.f) / (gamma05 + 1.f);
3733     unit->m_coefs[5] = (gamma06 - 1.f) / (gamma06 + 1.f);
3734     unit->m_coefs[6] = (gamma07 - 1.f) / (gamma07 + 1.f);
3735     unit->m_coefs[7] = (gamma08 - 1.f) / (gamma08 + 1.f);
3736     unit->m_coefs[8] = (gamma09 - 1.f) / (gamma09 + 1.f);
3737     unit->m_coefs[9] = (gamma10 - 1.f) / (gamma10 + 1.f);
3738     unit->m_coefs[10] = (gamma11 - 1.f) / (gamma11 + 1.f);
3739     unit->m_coefs[11] = (gamma12 - 1.f) / (gamma12 + 1.f);
3740     Clear(12, unit->m_y1);
3741     Hilbert_next(unit, 1);
3742 }
3743 
Hilbert_next(Hilbert * unit,int inNumSamples)3744 void Hilbert_next(Hilbert* unit, int inNumSamples) {
3745     float* in = ZIN(0);
3746     float* outcos = ZOUT(0);
3747     float* outsin = ZOUT(1);
3748     float y1[12];
3749     float coefs[12];
3750 
3751     // each filter's last sample
3752     for (int i = 0; i < 12; ++i) {
3753         y1[i] = unit->m_y1[i];
3754         coefs[i] = unit->m_coefs[i];
3755     }
3756 
3757     double ay1, ay2, ay3, ay4, ay5, ay6;
3758     double ay7, ay8, ay9, ay10, ay11, ay12;
3759 
3760     double y0_1, y0_2, y0_3, y0_4, y0_5, y0_6;
3761     double y0_7, y0_8, y0_9, y0_10, y0_11, y0_12;
3762 
3763     LOOP1(inNumSamples, float thisin = ZXP(in);
3764 
3765           HILBERT_FILTER ZXP(outcos) = ay6; ZXP(outsin) = ay12;)
3766 
3767     for (int i = 0; i < 12; ++i)
3768         unit->m_y1[i] = zapgremlins(y1[i]);
3769 }
3770 
3771 
3772 /* Hilbert based SSB FreqShifter */
3773 
FreqShift_Ctor(FreqShift * unit)3774 void FreqShift_Ctor(FreqShift* unit) {
3775     // calculate coefs based on SampleRate, store in the struct
3776     //    SETCALC(FreqShift_next_kk);
3777     unit->m_phase = 0;
3778     if (INRATE(1) == calc_FullRate) {
3779         if (INRATE(2) == calc_FullRate)
3780             SETCALC(FreqShift_next_aa);
3781         else
3782             SETCALC(FreqShift_next_ak);
3783     } else {
3784         if (INRATE(2) == calc_FullRate)
3785             SETCALC(FreqShift_next_ka);
3786         else {
3787             SETCALC(FreqShift_next_kk);
3788             unit->m_phase = (int32)(unit->m_radtoinc * IN0(2));
3789         }
3790     }
3791     // set up for the oscil for the modualtion
3792 
3793     int tableSizeSin = ft->mSineSize;
3794     unit->m_lomask = (tableSizeSin - 1) << 3;
3795     unit->m_radtoinc = tableSizeSin * (rtwopi * 65536.);
3796     unit->m_cpstoinc = tableSizeSin * SAMPLEDUR * 65536.;
3797     unit->m_phasein = IN0(2);
3798 
3799     double gamconst = (15.0 * pi) / SAMPLERATE;
3800     double gamma01 = gamconst * 0.3609f;
3801     double gamma02 = gamconst * 2.7412f;
3802     double gamma03 = gamconst * 11.1573f;
3803     double gamma04 = gamconst * 44.7581f;
3804     double gamma05 = gamconst * 179.6242f;
3805     double gamma06 = gamconst * 798.4578f;
3806     double gamma07 = gamconst * 1.2524f;
3807     double gamma08 = gamconst * 5.5671f;
3808     double gamma09 = gamconst * 22.3423f;
3809     double gamma10 = gamconst * 89.6271f;
3810     double gamma11 = gamconst * 364.7914f;
3811     double gamma12 = gamconst * 2770.1114f;
3812     unit->m_coefs[0] = (gamma01 - 1.f) / (gamma01 + 1.f);
3813     unit->m_coefs[1] = (gamma02 - 1.f) / (gamma02 + 1.f);
3814     unit->m_coefs[2] = (gamma03 - 1.f) / (gamma03 + 1.f);
3815     unit->m_coefs[3] = (gamma04 - 1.f) / (gamma04 + 1.f);
3816     unit->m_coefs[4] = (gamma05 - 1.f) / (gamma05 + 1.f);
3817     unit->m_coefs[5] = (gamma06 - 1.f) / (gamma06 + 1.f);
3818     unit->m_coefs[6] = (gamma07 - 1.f) / (gamma07 + 1.f);
3819     unit->m_coefs[7] = (gamma08 - 1.f) / (gamma08 + 1.f);
3820     unit->m_coefs[8] = (gamma09 - 1.f) / (gamma09 + 1.f);
3821     unit->m_coefs[9] = (gamma10 - 1.f) / (gamma10 + 1.f);
3822     unit->m_coefs[10] = (gamma11 - 1.f) / (gamma11 + 1.f);
3823     unit->m_coefs[11] = (gamma12 - 1.f) / (gamma12 + 1.f);
3824 
3825     Clear(12, unit->m_y1);
3826     FreqShift_next_kk(unit, 1);
3827 }
3828 
FreqShift_next_kk(FreqShift * unit,int inNumSamples)3829 void FreqShift_next_kk(FreqShift* unit, int inNumSamples) {
3830     float* in = ZIN(0);
3831     float* out = ZOUT(0);
3832     float phasein = ZIN0(2);
3833     float freqin = ZIN0(1);
3834     float outcos, outsin; // the sample by sample output of the Hilbert
3835     float outsinosc, outsinoscHalfPi; // the samples from the oscil.
3836     int32 halfPi = (int32)(unit->m_radtoinc * (0.5 * pi));
3837     double y1[12];
3838     double coefs[12];
3839 
3840     float* table0 = ft->mSineWavetable;
3841     float* table1 = table0 + 1;
3842     int32 lomask = unit->m_lomask;
3843     int32 phase = unit->m_phase;
3844 
3845     int32 freq = (int32)(unit->m_cpstoinc * freqin);
3846     int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
3847     unit->m_phasein = phasein;
3848 
3849     // each filter's last sample
3850     for (int i = 0; i < 12; ++i) {
3851         y1[i] = unit->m_y1[i];
3852         coefs[i] = unit->m_coefs[i];
3853     }
3854 
3855     double ay1, ay2, ay3, ay4, ay5, ay6;
3856     double ay7, ay8, ay9, ay10, ay11, ay12;
3857 
3858     double y0_1, y0_2, y0_3, y0_4, y0_5, y0_6;
3859     double y0_7, y0_8, y0_9, y0_10, y0_11, y0_12;
3860 
3861     LOOP1(inNumSamples, float thisin = ZXP(in);
3862 
3863           HILBERT_FILTER
3864 
3865               outcos = ay6;
3866           outsin = ay12;
3867 
3868           outsinosc = lookupi1(table0, table1, phase, lomask);
3869           outsinoscHalfPi = lookupi1(table0, table1, phase + halfPi, lomask);
3870 
3871           ZXP(out) = (outcos * outsinoscHalfPi) + (outsinosc * outsin);
3872 
3873           phase += phaseinc;)
3874 
3875     unit->m_phase = phase;
3876 
3877     for (int i = 0; i < 12; ++i)
3878         unit->m_y1[i] = zapgremlins(y1[i]);
3879 }
3880 
FreqShift_next_aa(FreqShift * unit,int inNumSamples)3881 void FreqShift_next_aa(FreqShift* unit, int inNumSamples) {
3882     float* in = ZIN(0);
3883     float* out = ZOUT(0);
3884     float* phasein = ZIN(2);
3885     float* freqin = ZIN(1);
3886     float outcos, outsin; // the sample by sample output of the Hilbert
3887     float outsinosc, outsinoscHalfPi; // the samples from the oscil.
3888     int32 halfPi = (int32)(unit->m_radtoinc * (0.5 * pi));
3889     double y1[12];
3890     double coefs[12];
3891 
3892     float* table0 = ft->mSineWavetable;
3893     float* table1 = table0 + 1;
3894     int32 lomask = unit->m_lomask;
3895     int32 phase = unit->m_phase;
3896 
3897     // each filter's last sample
3898     for (int i = 0; i < 12; ++i) {
3899         y1[i] = unit->m_y1[i];
3900         coefs[i] = unit->m_coefs[i];
3901     }
3902 
3903     double ay1, ay2, ay3, ay4, ay5, ay6;
3904     double ay7, ay8, ay9, ay10, ay11, ay12;
3905 
3906     double y0_1, y0_2, y0_3, y0_4, y0_5, y0_6;
3907     double y0_7, y0_8, y0_9, y0_10, y0_11, y0_12;
3908 
3909     LOOP1(inNumSamples, float thisin = ZXP(in);
3910 
3911           HILBERT_FILTER
3912 
3913               outcos = ay6;
3914           outsin = ay12;
3915 
3916           int32 phaseoffset = phase + (int32)(unit->m_radtoinc * ZXP(phasein));
3917           outsinosc = lookupi1(table0, table1, phaseoffset, lomask);
3918           outsinoscHalfPi = lookupi1(table0, table1, phaseoffset + halfPi, lomask);
3919           phase += (int32)(unit->m_cpstoinc * ZXP(freqin));
3920 
3921           ZXP(out) = (outcos * outsinoscHalfPi) + (outsinosc * outsin);)
3922 
3923     unit->m_phase = phase;
3924 
3925     for (int i = 0; i < 12; ++i)
3926         unit->m_y1[i] = zapgremlins(y1[i]);
3927 }
3928 
FreqShift_next_ak(FreqShift * unit,int inNumSamples)3929 void FreqShift_next_ak(FreqShift* unit, int inNumSamples) {
3930     float* in = ZIN(0);
3931     float* out = ZOUT(0);
3932     float phasein = ZIN0(2);
3933     float* freqin = ZIN(1);
3934     float outcos, outsin; // the sample by sample output of the Hilbert
3935     float outsinosc, outsinoscHalfPi; // the samples from the oscil.
3936     int32 halfPi = (int32)(unit->m_radtoinc * (0.5 * pi));
3937     double y1[12];
3938     double coefs[12];
3939 
3940     float* table0 = ft->mSineWavetable;
3941     float* table1 = table0 + 1;
3942     int32 lomask = unit->m_lomask;
3943     int32 phase = unit->m_phase;
3944     float phasemod = unit->m_phasein;
3945     float phaseslope = CALCSLOPE(phasein, phasemod);
3946 
3947     // each filter's last sample
3948     for (int i = 0; i < 12; ++i) {
3949         y1[i] = unit->m_y1[i];
3950         coefs[i] = unit->m_coefs[i];
3951     }
3952 
3953     double ay1, ay2, ay3, ay4, ay5, ay6;
3954     double ay7, ay8, ay9, ay10, ay11, ay12;
3955 
3956     double y0_1, y0_2, y0_3, y0_4, y0_5, y0_6;
3957     double y0_7, y0_8, y0_9, y0_10, y0_11, y0_12;
3958 
3959     LOOP1(inNumSamples, float thisin = ZXP(in);
3960 
3961           HILBERT_FILTER
3962 
3963               outcos = ay6;
3964           outsin = ay12;
3965 
3966           int32 pphase = phase + (int32)(unit->m_radtoinc * phasemod); phasemod += phaseslope;
3967           outsinosc = lookupi1(table0, table1, pphase, lomask);
3968           outsinoscHalfPi = lookupi1(table0, table1, pphase + halfPi, lomask);
3969           phase += (int32)(unit->m_cpstoinc * ZXP(freqin));
3970 
3971           ZXP(out) = (outcos * outsinoscHalfPi) + (outsinosc * outsin);)
3972 
3973     unit->m_phase = phase;
3974     unit->m_phasein = phasein;
3975 
3976     for (int i = 0; i < 12; ++i)
3977         unit->m_y1[i] = zapgremlins(y1[i]);
3978 }
3979 
FreqShift_next_ka(FreqShift * unit,int inNumSamples)3980 void FreqShift_next_ka(FreqShift* unit, int inNumSamples) {
3981     float* in = ZIN(0);
3982     float* out = ZOUT(0);
3983     float* phasein = ZIN(2);
3984     float freqin = ZIN0(1);
3985     float outcos, outsin; // the sample by sample output of the Hilbert
3986     float outsinosc, outsinoscHalfPi; // the samples from the oscil.
3987     int32 halfPi = (int32)(unit->m_radtoinc * (0.5 * pi));
3988     double y1[12];
3989     double coefs[12];
3990 
3991     float* table0 = ft->mSineWavetable;
3992     float* table1 = table0 + 1;
3993     int32 lomask = unit->m_lomask;
3994     int32 phase = unit->m_phase;
3995 
3996     int32 freq = (int32)(unit->m_cpstoinc * freqin);
3997 
3998     // each filter's last sample
3999     for (int i = 0; i < 12; ++i) {
4000         y1[i] = unit->m_y1[i];
4001         coefs[i] = unit->m_coefs[i];
4002     }
4003 
4004     double ay1, ay2, ay3, ay4, ay5, ay6;
4005     double ay7, ay8, ay9, ay10, ay11, ay12;
4006 
4007     double y0_1, y0_2, y0_3, y0_4, y0_5, y0_6;
4008     double y0_7, y0_8, y0_9, y0_10, y0_11, y0_12;
4009 
4010     LOOP1(inNumSamples, float thisin = ZXP(in);
4011 
4012           HILBERT_FILTER
4013 
4014               outcos = ay6;
4015           outsin = ay12;
4016 
4017           int32 phaseoffset = phase + (int32)(unit->m_radtoinc * ZXP(phasein));
4018           outsinosc = lookupi1(table0, table1, phaseoffset, lomask);
4019           outsinoscHalfPi = lookupi1(table0, table1, phaseoffset + halfPi, lomask); phase += freq;
4020 
4021           ZXP(out) = (outcos * outsinoscHalfPi) + (outsinosc * outsin);)
4022 
4023     unit->m_phase = phase;
4024 
4025     for (int i = 0; i < 12; ++i)
4026         unit->m_y1[i] = zapgremlins(y1[i]);
4027 }
4028 
4029 
4030 /**
4031 "MoogFF" - Moog VCF digital implementation.
4032 As described in the paper entitled
4033 "Preserving the Digital Structure of the Moog VCF"
4034 by Federico Fontana
4035 appeared in the Proc. ICMC07, Copenhagen, 25-31 August 2007
4036 
4037 Original Java code Copyright F. Fontana - August 2007
4038 federico.fontana@univr.it
4039 
4040 Ported to C++ for SuperCollider by Dan Stowell - August 2007
4041 http://www.mcld.co.uk/
4042 
4043     This program is free software; you can redistribute it and/or modify
4044     it under the terms of the GNU General Public License as published by
4045     the Free Software Foundation; either version 2 of the License, or
4046     (at your option) any later version.
4047 
4048     This program is distributed in the hope that it will be useful,
4049     but WITHOUT ANY WARRANTY; without even the implied warranty of
4050     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4051     GNU General Public License for more details.
4052 
4053     You should have received a copy of the GNU General Public License
4054     along with this program; if not, write to the Free Software
4055     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
4056 */
4057 //////////////////////////////////////////////////////////////////
4058 
MoogFF_Ctor(MoogFF * unit)4059 void MoogFF_Ctor(MoogFF* unit) {
4060     SETCALC(MoogFF_next);
4061 
4062     // initialize the unit generator state variables.
4063     unit->m_freq = uninitializedControl;
4064     unit->m_k = IN0(2);
4065     unit->m_s1 = 0.f;
4066     unit->m_s2 = 0.f;
4067     unit->m_s3 = 0.f;
4068     unit->m_s4 = 0.f;
4069 
4070     // calculate one sample of output.
4071     MoogFF_next(unit, 1);
4072 }
4073 
MoogFF_next(MoogFF * unit,int inNumSamples)4074 void MoogFF_next(MoogFF* unit, int inNumSamples) {
4075     float k = IN0(2);
4076     k = sc_clip(k, 0.f, 4.f);
4077 
4078     // Load state from the struct
4079     double s1 = unit->m_s1;
4080     double s2 = unit->m_s2;
4081     double s3 = unit->m_s3;
4082     double s4 = unit->m_s4;
4083 
4084     // Reset filter state if requested
4085     if (IN0(3) > 0)
4086         s1 = s2 = s3 = s4 = 0.f;
4087 
4088     double a1 = unit->m_a1, b0 = unit->m_b0; // Filter coefficient parameters
4089 
4090     // Update filter coefficients, but only if freq changes since it involves some expensive operations
4091 
4092     float freqIn = IN0(1);
4093     if (unit->m_freq != freqIn) {
4094         // Print("Updated freq to %g\n", freq);
4095         double T = SAMPLEDUR;
4096         double wcD = 2.0 * tan(T * PI * freqIn) * SAMPLERATE;
4097         if (wcD < 0)
4098             wcD = 0; // Protect against negative cutoff freq
4099         double TwcD = T * wcD;
4100         b0 = TwcD / (TwcD + 2.);
4101         a1 = (TwcD - 2.) / (TwcD + 2.);
4102         unit->m_freq = freqIn;
4103         unit->m_b0 = b0;
4104         unit->m_a1 = a1;
4105     }
4106 
4107     float* out = ZOUT(0);
4108     float* in = ZIN(0);
4109 
4110     if (unit->m_k == k) {
4111         LOOP1(inNumSamples,
4112               // compute loop values
4113               double o = s4 + b0 * (s3 + b0 * (s2 + b0 * s1));
4114               double ins = ZXP(in);
4115               double outs = (b0 * b0 * b0 * b0 * ins + o) * sc_reciprocal(1.0 + b0 * b0 * b0 * b0 * k); ZXP(out) = outs;
4116               double u = ins - k * outs;
4117 
4118               // update 1st order filter states
4119               double past = u;
4120               double future = b0 * past + s1; s1 = b0 * past - a1 * future;
4121 
4122               past = future; future = b0 * past + s2; s2 = b0 * past - a1 * future;
4123 
4124               past = future; future = b0 * past + s3; s3 = b0 * past - a1 * future;
4125 
4126               s4 = b0 * future - a1 * outs;)
4127     } else {
4128         float new_k = k;
4129         float old_k = unit->m_k;
4130         float slope_k = CALCSLOPE(new_k, old_k);
4131         k = old_k;
4132 
4133         LOOP1(inNumSamples,
4134               // compute loop values
4135               double o = s4 + b0 * (s3 + b0 * (s2 + b0 * s1));
4136               double ins = ZXP(in);
4137               double outs = (b0 * b0 * b0 * b0 * ins + o) * sc_reciprocal(1.0 + b0 * b0 * b0 * b0 * k); ZXP(out) = outs;
4138               double u = ins - k * outs;
4139 
4140               // update 1st order filter states
4141               double past = u;
4142               double future = b0 * past + s1; s1 = b0 * past - a1 * future;
4143 
4144               past = future; future = b0 * past + s2; s2 = b0 * past - a1 * future;
4145 
4146               past = future; future = b0 * past + s3; s3 = b0 * past - a1 * future;
4147 
4148               s4 = b0 * future - a1 * outs; k += slope_k;);
4149         unit->m_k = new_k;
4150     }
4151 
4152     // Store state
4153     unit->m_s1 = zapgremlins(s1);
4154     unit->m_s2 = zapgremlins(s2);
4155     unit->m_s3 = zapgremlins(s3);
4156     unit->m_s4 = zapgremlins(s4);
4157 }
4158 
4159 /* BEQSuite */
4160 
4161 
BPerformFilterLoop(const Unit * unit,float * out,const float * in,double & y1,double & y2,double a0,double a1,double a2,double b1,double b2)4162 static void inline BPerformFilterLoop(const Unit* unit, float* out, const float* in, double& y1, double& y2, double a0,
4163                                       double a1, double a2, double b1, double b2) {
4164     LOOP(unit->mRate->mFilterLoops, double in0 = ZXP(in); double in1 = ZXP(in); double in2 = ZXP(in);
4165 
4166          double y0 = in0 + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
4167 
4168          y2 = in1 + b1 * y0 + b2 * y1; ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
4169 
4170          y1 = in2 + b1 * y2 + b2 * y0; ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;);
4171     LOOP(unit->mRate->mFilterRemain, double y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
4172          y2 = y1; y1 = y0;);
4173 }
4174 
BPerformFilterLoop(const Unit * unit,float * out,const float * in,double & y1,double & y2,double a0,double a1,double a2,double b1,double b2,double a0slope,double a1slope,double a2slope,double b1slope,double b2slope)4175 static void inline BPerformFilterLoop(const Unit* unit, float* out, const float* in, double& y1, double& y2, double a0,
4176                                       double a1, double a2, double b1, double b2, double a0slope, double a1slope,
4177                                       double a2slope, double b1slope, double b2slope) {
4178     LOOP(unit->mRate->mFilterLoops, double in0 = ZXP(in); double in1 = ZXP(in); double in2 = ZXP(in);
4179 
4180          double y0 = in0 + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
4181 
4182          y2 = in1 + b1 * y0 + b2 * y1; ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
4183 
4184          y1 = in2 + b1 * y2 + b2 * y0; ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
4185 
4186          a0 += a0slope; a1 += a1slope; a2 += a2slope; b1 += b1slope; b2 += b2slope;);
4187     LOOP(unit->mRate->mFilterRemain, double y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
4188          y2 = y1; y1 = y0;);
4189 }
4190 
4191 struct BLowPass : public Unit {
4192     double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
4193     float m_freq, m_rq;
4194 };
4195 
4196 extern "C" {
4197 void BLowPass_next_ii(BLowPass* unit, int inNumSamples);
4198 void BLowPass_next_kk(BLowPass* unit, int inNumSamples);
4199 void BLowPass_next_aa(BLowPass* unit, int inNumSamples);
4200 void BLowPass_Ctor(BLowPass* unit);
4201 }
4202 
4203 
BLowPass_Ctor(BLowPass * unit)4204 void BLowPass_Ctor(BLowPass* unit) {
4205     if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate))
4206         SETCALC(BLowPass_next_aa);
4207     else {
4208         if ((INRATE(1) == calc_ScalarRate) && (INRATE(2) == calc_ScalarRate))
4209             SETCALC(BLowPass_next_ii);
4210         else
4211             SETCALC(BLowPass_next_kk);
4212     }
4213     float rq = unit->m_rq = ZIN0(2);
4214     float freq = unit->m_freq = ZIN0(1);
4215     double w0 = twopi * (double)freq * SAMPLEDUR;
4216     double cosw0 = cos(w0);
4217     double i = 1. - cosw0;
4218     double alpha = sin(w0) * 0.5 * (double)rq;
4219     double b0rz = 1. / (1. + alpha);
4220     double a0 = unit->m_a0 = i * 0.5 * b0rz;
4221     unit->m_a1 = i * b0rz;
4222     unit->m_a2 = a0;
4223     unit->m_b1 = cosw0 * 2. * b0rz;
4224     unit->m_b2 = (1. - alpha) * -b0rz;
4225     unit->m_y1 = 0.;
4226 
4227     unit->m_y2 = 0.;
4228     PUSH_LOOPVALS
4229     BLowPass_next_kk(unit, 1);
4230     POP_LOOPVALS
4231 }
4232 
BLowPass_next_aa(BLowPass * unit,int inNumSamples)4233 void BLowPass_next_aa(BLowPass* unit, int inNumSamples) {
4234     float* out = ZOUT(0);
4235     float* in = ZIN(0);
4236     float* freq = ZIN(1);
4237     float* rq = ZIN(2);
4238 
4239     double a0, a1, a2, b1, b2, w0, cosw0, i, alpha, b0rz;
4240     double y0, y1, y2;
4241     float nextfreq, nextrq;
4242 
4243     y1 = unit->m_y1;
4244     y2 = unit->m_y2;
4245 
4246     a0 = unit->m_a0;
4247     a1 = unit->m_a1;
4248     a2 = unit->m_a2;
4249     b1 = unit->m_b1;
4250     b2 = unit->m_b2;
4251 
4252     LOOP(
4253         unit->mRate->mFilterLoops, nextfreq = ZXP(freq); nextrq = ZXP(rq);
4254         if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq)) {
4255             w0 = twopi * (double)nextfreq * SAMPLEDUR;
4256             cosw0 = cos(w0);
4257             i = 1. - cosw0;
4258             alpha = sin(w0) * 0.5 * (double)nextrq;
4259             b0rz = 1. / (1. + alpha);
4260             a0 = i * 0.5 * b0rz;
4261             a1 = i * b0rz;
4262             a2 = a0;
4263             b1 = cosw0 * 2. * b0rz;
4264             b2 = (1. - alpha) * -b0rz;
4265             unit->m_freq = nextfreq;
4266             unit->m_rq = nextrq;
4267         } y0 = ZXP(in) + b1 * y1 + b2 * y2;
4268         ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
4269 
4270         y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
4271 
4272         y1 = ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;);
4273 
4274     LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2; y2 = y1;
4275          y1 = y0; unit->m_freq = ZXP(freq); unit->m_rq = ZXP(rq););
4276 
4277     unit->m_a0 = a0;
4278     unit->m_a1 = a1;
4279     unit->m_a2 = a2;
4280     unit->m_b1 = b1;
4281     unit->m_b2 = b2;
4282     unit->m_y1 = zapgremlins(y1);
4283     unit->m_y2 = zapgremlins(y2);
4284 }
4285 
4286 
BLowPass_next_kk(BLowPass * unit,int inNumSamples)4287 void BLowPass_next_kk(BLowPass* unit, int inNumSamples) {
4288     float* out = ZOUT(0);
4289     float* in = ZIN(0);
4290     float nextfreq = ZIN0(1);
4291     float nextrq = ZIN0(2);
4292 
4293     double a0, a1, a2, b1, b2;
4294     double y1 = unit->m_y1;
4295     double y2 = unit->m_y2;
4296 
4297     a0 = unit->m_a0;
4298     a1 = unit->m_a1;
4299     a2 = unit->m_a2;
4300     b1 = unit->m_b1;
4301     b2 = unit->m_b2;
4302 
4303     if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq)) {
4304         double a0slope, a1slope, a2slope, b1slope, b2slope;
4305         double next_a0, next_a1, next_a2, next_b1, next_b2, next_w0, next_cosw0, next_i, next_alpha, next_b0rz;
4306 
4307         next_w0 = twopi * (double)nextfreq * SAMPLEDUR;
4308         next_cosw0 = cos(next_w0);
4309         next_i = 1. - next_cosw0;
4310         next_alpha = sin(next_w0) * 0.5 * (double)nextrq;
4311         next_b0rz = 1. / (1. + next_alpha);
4312         next_a0 = next_i * 0.5 * next_b0rz;
4313         next_a1 = next_i * next_b0rz;
4314         next_a2 = next_a0;
4315         next_b1 = next_cosw0 * 2. * next_b0rz;
4316         next_b2 = ((1. - next_alpha) * -next_b0rz);
4317         a0slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
4318         a1slope = (next_a1 - a1) * unit->mRate->mFilterSlope;
4319         a2slope = (next_a2 - a2) * unit->mRate->mFilterSlope;
4320         b1slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
4321         b2slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
4322         unit->m_freq = nextfreq;
4323         unit->m_rq = nextrq;
4324         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2, a0slope, a1slope, a2slope, b1slope, b2slope);
4325 
4326         unit->m_a0 = next_a0;
4327         unit->m_a1 = next_a1;
4328         unit->m_a2 = next_a2;
4329         unit->m_b1 = next_b1;
4330         unit->m_b2 = next_b2;
4331     } else
4332         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2);
4333 
4334     unit->m_y1 = zapgremlins(y1);
4335     unit->m_y2 = zapgremlins(y2);
4336 }
4337 
BLowPass_next_ii(BLowPass * unit,int inNumSamples)4338 void BLowPass_next_ii(BLowPass* unit, int inNumSamples) {
4339     float* out = ZOUT(0);
4340     float* in = ZIN(0);
4341 
4342     double a0, a1, a2, b1, b2;
4343     double y1 = unit->m_y1;
4344     double y2 = unit->m_y2;
4345 
4346     a0 = unit->m_a0;
4347     a1 = unit->m_a1;
4348     a2 = unit->m_a2;
4349     b1 = unit->m_b1;
4350     b2 = unit->m_b2;
4351 
4352     BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2);
4353 
4354     unit->m_y1 = zapgremlins(y1);
4355     unit->m_y2 = zapgremlins(y2);
4356 }
4357 
4358 
4359 struct BHiPass : public Unit {
4360     double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
4361     float m_freq, m_rq;
4362 };
4363 
4364 
4365 extern "C" {
4366 void BHiPass_next_ii(BHiPass* unit, int inNumSamples);
4367 void BHiPass_next_kk(BHiPass* unit, int inNumSamples);
4368 void BHiPass_next_aa(BHiPass* unit, int inNumSamples);
4369 void BHiPass_Ctor(BHiPass* unit);
4370 }
4371 
BHiPass_Ctor(BHiPass * unit)4372 void BHiPass_Ctor(BHiPass* unit) {
4373     if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate))
4374         SETCALC(BHiPass_next_aa);
4375     else {
4376         if ((INRATE(1) == calc_ScalarRate) && (INRATE(2) == calc_ScalarRate))
4377             SETCALC(BHiPass_next_ii);
4378         else
4379             SETCALC(BHiPass_next_kk);
4380     }
4381     float freq = unit->m_freq = ZIN0(1);
4382     float rq = unit->m_rq = ZIN0(2);
4383     double w0 = twopi * (double)freq * SAMPLEDUR;
4384     double cosw0 = cos(w0);
4385     double i = 1. + cosw0;
4386     double alpha = sin(w0) * 0.5 * (double)rq;
4387     double b0rz = 1. / (1. + alpha);
4388     double a0 = unit->m_a0 = i * 0.5 * b0rz;
4389     unit->m_a1 = -i * b0rz;
4390     unit->m_a2 = a0;
4391     unit->m_b1 = cosw0 * 2. * b0rz;
4392     unit->m_b2 = (1. - alpha) * -b0rz;
4393     unit->m_y1 = 0.;
4394 
4395     unit->m_y2 = 0.;
4396     PUSH_LOOPVALS
4397     BHiPass_next_kk(unit, 1);
4398     POP_LOOPVALS
4399 }
4400 
BHiPass_next_aa(BHiPass * unit,int inNumSamples)4401 void BHiPass_next_aa(BHiPass* unit, int inNumSamples) {
4402     float* out = ZOUT(0);
4403     float* in = ZIN(0);
4404     float* freq = ZIN(1);
4405     float* rq = ZIN(2);
4406 
4407     double a0, a1, a2, b1, b2, w0, cosw0, i, alpha, b0rz;
4408     double y0, y1, y2;
4409     float nextfreq, nextrq;
4410 
4411     y1 = unit->m_y1;
4412     y2 = unit->m_y2;
4413 
4414     a0 = unit->m_a0;
4415     a1 = unit->m_a1;
4416     a2 = unit->m_a2;
4417     b1 = unit->m_b1;
4418     b2 = unit->m_b2;
4419 
4420     LOOP(
4421         unit->mRate->mFilterLoops, nextfreq = ZXP(freq); nextrq = ZXP(rq);
4422         if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq)) {
4423             w0 = twopi * (double)nextfreq * SAMPLEDUR;
4424             cosw0 = cos(w0);
4425             i = 1. + cosw0;
4426             alpha = sin(w0) * 0.5 * (double)nextrq;
4427             b0rz = 1. / (1. + alpha);
4428             a0 = i * 0.5 * b0rz;
4429             a1 = -i * b0rz;
4430             a2 = a0;
4431             b1 = cosw0 * 2. * b0rz;
4432             b2 = (1. - alpha) * -b0rz;
4433             unit->m_freq = nextfreq;
4434             unit->m_rq = nextrq;
4435         } y0 = ZXP(in) + b1 * y1 + b2 * y2;
4436         ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
4437 
4438         y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
4439 
4440         y1 = ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;);
4441     LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2; y2 = y1;
4442          y1 = y0;);
4443 
4444     unit->m_a0 = a0;
4445     unit->m_a1 = a1;
4446     unit->m_a2 = a2;
4447     unit->m_b1 = b1;
4448     unit->m_b2 = b2;
4449     unit->m_y1 = zapgremlins(y1);
4450     unit->m_y2 = zapgremlins(y2);
4451 }
4452 
4453 
BHiPass_next_kk(BHiPass * unit,int inNumSamples)4454 void BHiPass_next_kk(BHiPass* unit, int inNumSamples) {
4455     float* out = ZOUT(0);
4456     float* in = ZIN(0);
4457     float nextfreq = ZIN0(1);
4458     float nextrq = ZIN0(2);
4459 
4460     double a0, a1, a2, b1, b2;
4461     double y1 = unit->m_y1;
4462     double y2 = unit->m_y2;
4463 
4464     a0 = unit->m_a0;
4465     a1 = unit->m_a1;
4466     a2 = unit->m_a2;
4467     b1 = unit->m_b1;
4468     b2 = unit->m_b2;
4469 
4470     if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq)) {
4471         double a0slope, a1slope, a2slope, b1slope, b2slope;
4472         double next_a0, next_a1, next_a2, next_b1, next_b2, next_w0, next_cosw0, next_i, next_alpha, next_b0rz;
4473 
4474         next_w0 = twopi * (double)nextfreq * SAMPLEDUR;
4475         next_cosw0 = cos(next_w0);
4476         next_i = 1. + next_cosw0;
4477         next_alpha = sin(next_w0) * 0.5 * (double)nextrq;
4478         next_b0rz = 1. / (1. + next_alpha);
4479         next_a0 = next_i * 0.5 * next_b0rz;
4480         next_a1 = -next_i * next_b0rz;
4481         next_a2 = next_a0;
4482         next_b1 = next_cosw0 * 2. * next_b0rz;
4483         next_b2 = (1. - next_alpha) * -next_b0rz;
4484         a0slope = (next_a0 - a0) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta0, a0);
4485         a1slope = (next_a1 - a1) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta1, a1);
4486         a2slope = (next_a2 - a2) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta2, a2);
4487         b1slope = (next_b1 - b1) * unit->mRate->mFilterSlope; // CALCSLOPE(nextb1, b1);
4488         b2slope = (next_b2 - b2) * unit->mRate->mFilterSlope; // CALCSLOPE(nextb2, b2);
4489         unit->m_freq = nextfreq;
4490         unit->m_rq = nextrq;
4491 
4492         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2, a0slope, a1slope, a2slope, b1slope, b2slope);
4493 
4494         unit->m_a0 = next_a0;
4495         unit->m_a1 = next_a1;
4496         unit->m_a2 = next_a2;
4497         unit->m_b1 = next_b1;
4498         unit->m_b2 = next_b2;
4499     } else
4500         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2);
4501 
4502     unit->m_y1 = zapgremlins(y1);
4503     unit->m_y2 = zapgremlins(y2);
4504 }
4505 
BHiPass_next_ii(BHiPass * unit,int inNumSamples)4506 void BHiPass_next_ii(BHiPass* unit, int inNumSamples) {
4507     float* out = ZOUT(0);
4508     float* in = ZIN(0);
4509 
4510     double a0, a1, a2, b1, b2;
4511     double y1 = unit->m_y1;
4512     double y2 = unit->m_y2;
4513 
4514     a0 = unit->m_a0;
4515     a1 = unit->m_a1;
4516     a2 = unit->m_a2;
4517     b1 = unit->m_b1;
4518     b2 = unit->m_b2;
4519 
4520     BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2);
4521 
4522     unit->m_y1 = zapgremlins(y1);
4523     unit->m_y2 = zapgremlins(y2);
4524 }
4525 
4526 
4527 /* BBandPass */
4528 
4529 struct BBandPass : public Unit {
4530     double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
4531     float m_freq, m_bw;
4532 };
4533 
4534 extern "C" {
4535 void BBandPass_next_kk(BBandPass* unit, int inNumSamples);
4536 void BBandPass_next_aa(BBandPass* unit, int inNumSamples);
4537 void BBandPass_Ctor(BBandPass* unit);
4538 }
4539 
BBandPass_Ctor(BBandPass * unit)4540 void BBandPass_Ctor(BBandPass* unit) {
4541     if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate))
4542         SETCALC(BBandPass_next_aa);
4543     else
4544         SETCALC(BBandPass_next_kk);
4545     float freq = unit->m_freq = ZIN0(1);
4546     float bw = unit->m_bw = ZIN0(2);
4547 
4548     double w0 = twopi * (double)freq * SAMPLEDUR;
4549     double sinw0 = sin(w0);
4550     double alpha = sinw0 * (sinh((0.34657359027997 * (double)bw * w0) / sinw0));
4551     double b0rz = 1. / (1. + alpha);
4552     double a0 = unit->m_a0 = alpha * b0rz;
4553     unit->m_a1 = 0.0f;
4554     unit->m_a2 = -a0;
4555     unit->m_b1 = cos(w0) * 2. * b0rz;
4556     unit->m_b2 = (1. - alpha) * -b0rz;
4557 
4558     unit->m_y1 = 0.;
4559 
4560     unit->m_y2 = 0.;
4561     PUSH_LOOPVALS
4562     BBandPass_next_kk(unit, 1);
4563     POP_LOOPVALS
4564 }
4565 
BBandPass_next_aa(BBandPass * unit,int inNumSamples)4566 void BBandPass_next_aa(BBandPass* unit, int inNumSamples) {
4567     float* out = ZOUT(0);
4568     float* in = ZIN(0);
4569     float* freq = ZIN(1);
4570     float* bw = ZIN(2);
4571 
4572     double a0, a1, a2, b1, b2, w0, sinw0, alpha, b0rz;
4573     double y0, y1, y2;
4574     float nextfreq, nextbw;
4575 
4576     y1 = unit->m_y1;
4577     y2 = unit->m_y2;
4578 
4579     a0 = unit->m_a0;
4580     a1 = unit->m_a1;
4581     a2 = unit->m_a2;
4582     b1 = unit->m_b1;
4583     b2 = unit->m_b2;
4584 
4585     LOOP(
4586         unit->mRate->mFilterLoops, nextfreq = ZXP(freq); nextbw = ZXP(bw);
4587         if ((unit->m_freq != nextfreq) || (unit->m_bw != nextbw)) {
4588             w0 = twopi * (double)nextfreq * SAMPLEDUR;
4589             sinw0 = sin(w0);
4590             alpha = sinw0 * (sinh((0.34657359027997 * (double)nextbw * w0) / sinw0));
4591             b0rz = 1. / (1. + alpha);
4592             a0 = alpha * b0rz;
4593             a1 = 0.0f;
4594             a2 = -a0;
4595             b1 = cos(w0) * 2. * b0rz;
4596             b2 = (1. - alpha) * -b0rz;
4597             unit->m_freq = ZXP(freq);
4598             unit->m_bw = ZXP(bw);
4599         } y0 = ZXP(in) + b1 * y1 + b2 * y2;
4600         ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
4601 
4602         y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
4603 
4604         y1 = ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;);
4605     LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2; y2 = y1;
4606          y1 = y0; unit->m_freq = nextfreq; unit->m_bw = nextbw;);
4607 
4608     unit->m_a0 = a0;
4609     unit->m_a1 = a1;
4610     unit->m_a2 = a2;
4611     unit->m_b1 = b1;
4612     unit->m_b2 = b2;
4613     unit->m_y1 = zapgremlins(y1);
4614     unit->m_y2 = zapgremlins(y2);
4615 }
4616 
4617 
BBandPass_next_kk(BBandPass * unit,int inNumSamples)4618 void BBandPass_next_kk(BBandPass* unit, int inNumSamples) {
4619     float* out = ZOUT(0);
4620     float* in = ZIN(0);
4621     float nextfreq = ZIN0(1);
4622     float nextbw = ZIN0(2);
4623 
4624     double a0, a1, a2, b1, b2;
4625     double y1 = unit->m_y1;
4626     double y2 = unit->m_y2;
4627 
4628     a0 = unit->m_a0;
4629     a1 = unit->m_a1;
4630     a2 = unit->m_a2;
4631     b1 = unit->m_b1;
4632     b2 = unit->m_b2;
4633     if ((unit->m_freq != nextfreq) || (unit->m_bw != nextbw)) {
4634         double a0slope, a1slope, a2slope, b1slope, b2slope;
4635         double next_a0, next_a1, next_a2, next_b1, next_b2, next_w0, next_alpha, next_b0rz;
4636 
4637         next_w0 = twopi * (double)nextfreq * SAMPLEDUR;
4638         next_alpha = sin(next_w0) * (sinh((0.34657359027997 * (double)nextbw * next_w0) / sin(next_w0)));
4639         next_b0rz = 1. / (1. + next_alpha);
4640         next_a0 = next_alpha * next_b0rz;
4641         next_a1 = 0.;
4642         next_a2 = -next_a0;
4643         next_b1 = cos(next_w0) * 2. * next_b0rz;
4644         next_b2 = ((1. - next_alpha) * -next_b0rz);
4645         a0slope = (next_a0 - a0) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta0, a0);
4646         a1slope = (next_a1 - a1) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta1, a1);
4647         a2slope = (next_a2 - a2) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta2, a2);
4648         b1slope = (next_b1 - b1) * unit->mRate->mFilterSlope; // CALCSLOPE(nextb1, b1);
4649         b2slope = (next_b2 - b2) * unit->mRate->mFilterSlope; // CALCSLOPE(nextb2, b2);
4650         unit->m_freq = nextfreq;
4651         unit->m_bw = nextbw;
4652 
4653         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2, a0slope, a1slope, a2slope, b1slope, b2slope);
4654 
4655         unit->m_a0 = next_a0;
4656         unit->m_a1 = next_a1;
4657         unit->m_a2 = next_a2;
4658         unit->m_b1 = next_b1;
4659         unit->m_b2 = next_b2;
4660     } else
4661         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2);
4662 
4663     unit->m_y1 = zapgremlins(y1);
4664     unit->m_y2 = zapgremlins(y2);
4665 }
4666 
4667 /* BBandStop */
4668 struct BBandStop : public Unit {
4669     double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
4670     float m_freq, m_bw;
4671 };
4672 
4673 extern "C" {
4674 void BBandStop_next_kk(BBandStop* unit, int inNumSamples);
4675 void BBandStop_next_aa(BBandStop* unit, int inNumSamples);
4676 void BBandStop_Ctor(BBandStop* unit);
4677 }
4678 
BBandStop_Ctor(BBandStop * unit)4679 void BBandStop_Ctor(BBandStop* unit) {
4680     if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate))
4681         SETCALC(BBandStop_next_aa);
4682     else
4683         SETCALC(BBandStop_next_kk);
4684     float freq = unit->m_freq = ZIN0(1);
4685     float bw = unit->m_bw = ZIN0(2);
4686 
4687     double w0 = twopi * (double)freq * SAMPLEDUR;
4688     double sinw0 = sin(w0);
4689     double alpha = sinw0 * (sinh((0.34657359027997 * (double)bw * w0) / sinw0));
4690     double b0rz = 1. / (1. + alpha);
4691     double b1 = unit->m_b1 = 2. * b0rz * cos(w0);
4692     unit->m_a0 = b0rz;
4693     unit->m_a1 = -b1;
4694     unit->m_a2 = b0rz;
4695     unit->m_b2 = (1. - alpha) * -b0rz;
4696 
4697     unit->m_y1 = 0.;
4698 
4699     unit->m_y2 = 0.;
4700     PUSH_LOOPVALS
4701     BBandStop_next_kk(unit, 1);
4702     POP_LOOPVALS
4703 }
4704 
BBandStop_next_aa(BBandStop * unit,int inNumSamples)4705 void BBandStop_next_aa(BBandStop* unit, int inNumSamples) {
4706     float* out = ZOUT(0);
4707     float* in = ZIN(0);
4708     float* freq = ZIN(1);
4709     float* bw = ZIN(2);
4710 
4711     double a0, a1, a2, b1, b2, w0, sinw0, alpha, b0rz;
4712     double y0, y1, y2;
4713     float nextfreq, nextbw;
4714 
4715     y1 = unit->m_y1;
4716     y2 = unit->m_y2;
4717 
4718     a0 = unit->m_a0;
4719     a1 = unit->m_a1;
4720     a2 = unit->m_a2;
4721     b1 = unit->m_b1;
4722     b2 = unit->m_b2;
4723 
4724     LOOP(
4725         unit->mRate->mFilterLoops, nextfreq = ZXP(freq); nextbw = ZXP(bw);
4726         if ((unit->m_freq != nextfreq) || (unit->m_bw != nextbw)) {
4727             w0 = twopi * (double)nextfreq * SAMPLEDUR;
4728             sinw0 = sin(w0);
4729             alpha = sinw0 * (sinh((0.34657359027997 * (double)nextbw * w0) / sinw0));
4730             b0rz = 1. / (1. + alpha);
4731             b1 = 2. * b0rz * cos(w0);
4732             a0 = b0rz;
4733             a1 = -b1;
4734             a2 = b0rz;
4735             b2 = (1. - alpha) * -b0rz;
4736             unit->m_freq = ZXP(freq);
4737             unit->m_bw = ZXP(bw);
4738         } y0 = ZXP(in) + b1 * y1 + b2 * y2;
4739         ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
4740 
4741         y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
4742 
4743         y1 = ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;);
4744     LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2; y2 = y1;
4745          y1 = y0; unit->m_freq = nextfreq; unit->m_bw = nextbw;);
4746 
4747     unit->m_a0 = a0;
4748     unit->m_a1 = a1;
4749     unit->m_a2 = a2;
4750     unit->m_b1 = b1;
4751     unit->m_b2 = b2;
4752     unit->m_y1 = zapgremlins(y1);
4753     unit->m_y2 = zapgremlins(y2);
4754 }
4755 
4756 
BBandStop_next_kk(BBandStop * unit,int inNumSamples)4757 void BBandStop_next_kk(BBandStop* unit, int inNumSamples) {
4758     float* out = ZOUT(0);
4759     float* in = ZIN(0);
4760     float nextfreq = ZIN0(1);
4761     float nextbw = ZIN0(2);
4762 
4763     double a0, a1, a2, b1, b2;
4764     double y1 = unit->m_y1;
4765     double y2 = unit->m_y2;
4766 
4767     a0 = unit->m_a0;
4768     a1 = unit->m_a1;
4769     a2 = unit->m_a2;
4770     b1 = unit->m_b1;
4771     b2 = unit->m_b2;
4772 
4773     if ((unit->m_freq != nextfreq) || (unit->m_bw != nextbw)) {
4774         double a0slope, a1slope, a2slope, b1slope, b2slope;
4775         double next_a0, next_a1, next_a2, next_b1, next_b2, next_w0, next_alpha, next_b0rz;
4776 
4777         next_w0 = twopi * (double)nextfreq * SAMPLEDUR;
4778         next_alpha = sin(next_w0) * (sinh((0.34657359027997 * (double)nextbw * next_w0) / sin(next_w0)));
4779         next_b0rz = 1. / (1. + next_alpha);
4780         next_b1 = cos(next_w0) * 2. * next_b0rz;
4781         next_a0 = next_b0rz;
4782         next_a1 = -next_b1;
4783         next_a2 = next_b0rz;
4784         next_b2 = ((1. - next_alpha) * -next_b0rz);
4785         a0slope = (next_a0 - a0) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta0, a0);
4786         a1slope = (next_a1 - a1) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta1, a1);
4787         a2slope = (next_a2 - a2) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta2, a2);
4788         b1slope = (next_b1 - b1) * unit->mRate->mFilterSlope; // CALCSLOPE(nextb1, b1);
4789         b2slope = (next_b2 - b2) * unit->mRate->mFilterSlope; // CALCSLOPE(nextb2, b2);
4790         unit->m_freq = nextfreq;
4791         unit->m_bw = nextbw;
4792 
4793         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2, a0slope, a1slope, a2slope, b1slope, b2slope);
4794 
4795         unit->m_a0 = next_a0;
4796         unit->m_a1 = next_a1;
4797         unit->m_a2 = next_a2;
4798         unit->m_b1 = next_b1;
4799         unit->m_b2 = next_b2;
4800     } else
4801         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2);
4802 
4803     unit->m_y1 = zapgremlins(y1);
4804     unit->m_y2 = zapgremlins(y2);
4805 }
4806 
4807 /* BPeakEQ */
4808 struct BPeakEQ : public Unit {
4809     double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
4810     float m_freq, m_rq, m_db;
4811 };
4812 
4813 extern "C" {
4814 void BPeakEQ_next_kkk(BPeakEQ* unit, int inNumSamples);
4815 void BPeakEQ_next_aaa(BPeakEQ* unit, int inNumSamples);
4816 void BPeakEQ_Ctor(BPeakEQ* unit);
4817 }
4818 
BPeakEQ_Ctor(BPeakEQ * unit)4819 void BPeakEQ_Ctor(BPeakEQ* unit) {
4820     if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate) && (INRATE(3) == calc_FullRate))
4821         SETCALC(BPeakEQ_next_aaa);
4822     else
4823         SETCALC(BPeakEQ_next_kkk);
4824 
4825     float freq = unit->m_freq = ZIN0(1);
4826     float rq = unit->m_rq = ZIN0(2);
4827     float db = unit->m_db = ZIN0(3);
4828     double a = pow(10., (double)db * 0.025);
4829     double w0 = twopi * (double)freq * SAMPLEDUR;
4830     double alpha = sin(w0) * 0.5 * (double)rq;
4831     double b0rz = 1. / (1. + (alpha / a));
4832     double b1 = unit->m_b1 = 2. * b0rz * cos(w0);
4833     unit->m_a0 = (1. + (alpha * a)) * b0rz;
4834     unit->m_a1 = -b1;
4835     unit->m_a2 = (1. - (alpha * a)) * b0rz;
4836     unit->m_b2 = (1. - (alpha / a)) * -b0rz;
4837 
4838     unit->m_y1 = 0.;
4839     unit->m_y2 = 0.;
4840     PUSH_LOOPVALS
4841     BPeakEQ_next_kkk(unit, 1);
4842     POP_LOOPVALS
4843 }
4844 
BPeakEQ_next_aaa(BPeakEQ * unit,int inNumSamples)4845 void BPeakEQ_next_aaa(BPeakEQ* unit, int inNumSamples) {
4846     float* out = ZOUT(0);
4847     float* in = ZIN(0);
4848     float* freq = ZIN(1);
4849     float* rq = ZIN(2);
4850     float* db = ZIN(3);
4851 
4852     double a0, a1, a2, b1, b2, a, w0, alpha, b0rz;
4853     double y0, y1, y2;
4854     float nextfreq, nextrq, nextdb;
4855 
4856     y1 = unit->m_y1;
4857     y2 = unit->m_y2;
4858 
4859     a0 = unit->m_a0;
4860     a1 = unit->m_a1;
4861     a2 = unit->m_a2;
4862     b1 = unit->m_b1;
4863     b2 = unit->m_b2;
4864 
4865 
4866     LOOP(
4867         unit->mRate->mFilterLoops, nextfreq = ZXP(freq); nextrq = ZXP(rq); nextdb = ZXP(db);
4868         if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq) || (unit->m_db != nextdb)) {
4869             a = pow(10., (double)nextdb * 0.025);
4870             w0 = twopi * (double)nextfreq * SAMPLEDUR;
4871             alpha = sin(w0) * 0.5 * (double)nextrq;
4872             b0rz = 1. / (1. + (alpha / a));
4873             b1 = 2. * b0rz * cos(w0);
4874             a0 = (1. + (alpha * a)) * b0rz;
4875             a1 = -b1;
4876             a2 = (1. - (alpha * a)) * b0rz;
4877             b2 = (1. - (alpha / a)) * -b0rz;
4878             unit->m_freq = nextfreq;
4879             unit->m_rq = nextrq;
4880             unit->m_db = nextdb;
4881         } y0 = ZXP(in) + b1 * y1 + b2 * y2;
4882         ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
4883 
4884         y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
4885 
4886         y1 = ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;);
4887     LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2; y2 = y1;
4888          y1 = y0; unit->m_freq = ZXP(freq); unit->m_rq = ZXP(rq); unit->m_db = ZXP(db););
4889 
4890     unit->m_a0 = a0;
4891     unit->m_a1 = a1;
4892     unit->m_a2 = a2;
4893     unit->m_b1 = b1;
4894     unit->m_b2 = b2;
4895     unit->m_y1 = zapgremlins(y1);
4896     unit->m_y2 = zapgremlins(y2);
4897 }
4898 
4899 
BPeakEQ_next_kkk(BPeakEQ * unit,int inNumSamples)4900 void BPeakEQ_next_kkk(BPeakEQ* unit, int inNumSamples) {
4901     float* out = ZOUT(0);
4902     float* in = ZIN(0);
4903     float nextfreq = ZIN0(1);
4904     float nextrq = ZIN0(2);
4905     float nextdb = ZIN0(3);
4906 
4907     double a0, a1, a2, b1, b2, a, w0, alpha, b0rz;
4908     double y1, y2;
4909 
4910     y1 = unit->m_y1;
4911     y2 = unit->m_y2;
4912 
4913     a0 = unit->m_a0;
4914     a1 = unit->m_a1;
4915     a2 = unit->m_a2;
4916     b1 = unit->m_b1;
4917     b2 = unit->m_b2;
4918 
4919     if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq) || (unit->m_db != nextdb)) {
4920         double a0slope, a1slope, a2slope, b1slope, b2slope;
4921         double next_a0, next_a1, next_a2, next_b1, next_b2;
4922 
4923         a = pow(10., (double)nextdb * 0.025);
4924         w0 = twopi * (double)nextfreq * SAMPLEDUR;
4925         alpha = sin(w0) * 0.5 * (double)nextrq;
4926         b0rz = 1. / (1. + (alpha / a));
4927         next_b1 = 2. * b0rz * cos(w0);
4928         next_a0 = (1. + (alpha * a)) * b0rz;
4929         next_a1 = -next_b1;
4930         next_a2 = (1. - (alpha * a)) * b0rz;
4931         next_b2 = (1. - (alpha / a)) * -b0rz;
4932         a0slope = (next_a0 - a0) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta0, a0);
4933         a1slope = (next_a1 - a1) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta1, a1);
4934         a2slope = (next_a2 - a2) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta2, a2);
4935         b1slope = (next_b1 - b1) * unit->mRate->mFilterSlope; // CALCSLOPE(nextb1, b1);
4936         b2slope = (next_b2 - b2) * unit->mRate->mFilterSlope; // CALCSLOPE(nextb2, b2);
4937         unit->m_freq = nextfreq;
4938         unit->m_db = nextdb;
4939         unit->m_rq = nextrq;
4940 
4941         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2, a0slope, a1slope, a2slope, b1slope, b2slope);
4942 
4943         unit->m_a0 = next_a0;
4944         unit->m_a1 = next_a1;
4945         unit->m_a2 = next_a2;
4946         unit->m_b1 = next_b1;
4947         unit->m_b2 = next_b2;
4948     } else
4949         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2);
4950 
4951     unit->m_y1 = zapgremlins(y1);
4952     unit->m_y2 = zapgremlins(y2);
4953 }
4954 
4955 /* BAllPass */
4956 struct BAllPass : public Unit {
4957     double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
4958     float m_freq, m_rq;
4959 };
4960 
4961 extern "C" {
4962 void BAllPass_next_kk(BAllPass* unit, int inNumSamples);
4963 void BAllPass_next_aa(BAllPass* unit, int inNumSamples);
4964 void BAllPass_Ctor(BAllPass* unit);
4965 }
4966 
BAllPass_Ctor(BAllPass * unit)4967 void BAllPass_Ctor(BAllPass* unit) {
4968     if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate))
4969         SETCALC(BAllPass_next_aa);
4970     else
4971         SETCALC(BAllPass_next_kk);
4972 
4973     float freq = unit->m_freq = ZIN0(1);
4974     float rq = unit->m_rq = ZIN0(2);
4975 
4976     double w0 = twopi * (double)freq * SAMPLEDUR;
4977     double alpha = sin(w0) * 0.5 * (double)rq;
4978     double b0rz = 1. / (1. + alpha);
4979     double a0 = unit->m_a0 = (1. - alpha) * b0rz;
4980     double b1 = unit->m_b1 = 2. * b0rz * cos(w0);
4981     unit->m_a1 = -b1;
4982     unit->m_a2 = 1.;
4983     unit->m_b2 = -a0;
4984 
4985     unit->m_y1 = 0.;
4986 
4987     unit->m_y2 = 0.;
4988     PUSH_LOOPVALS
4989     BAllPass_next_kk(unit, 1);
4990     POP_LOOPVALS
4991 }
4992 
BAllPass_next_aa(BAllPass * unit,int inNumSamples)4993 void BAllPass_next_aa(BAllPass* unit, int inNumSamples) {
4994     float* out = ZOUT(0);
4995     float* in = ZIN(0);
4996     float* freq = ZIN(1);
4997     float* rq = ZIN(2);
4998 
4999     double a0, a1, a2, b1, b2, w0, alpha, b0rz;
5000     double y0, y1, y2;
5001     float nextfreq, nextrq;
5002 
5003     y1 = unit->m_y1;
5004     y2 = unit->m_y2;
5005 
5006     a0 = unit->m_a0;
5007     a1 = unit->m_a1;
5008     a2 = unit->m_a2;
5009     b1 = unit->m_b1;
5010     b2 = unit->m_b2;
5011 
5012     LOOP(
5013         unit->mRate->mFilterLoops, nextfreq = ZXP(freq); nextrq = ZXP(rq);
5014         if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq)) {
5015             w0 = twopi * (double)nextfreq * SAMPLEDUR;
5016             alpha = sin(w0) * (double)nextrq * 0.5;
5017             b0rz = 1. / (1. + alpha);
5018             b1 = 2. * b0rz * cos(w0);
5019             a0 = (1. - alpha) * b0rz;
5020             a1 = -b1;
5021             a2 = 1.;
5022             b2 = -a0;
5023             unit->m_freq = nextfreq;
5024             unit->m_rq = nextrq;
5025         } y0 = ZXP(in) + b1 * y1 + b2 * y2;
5026         ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5027 
5028         y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5029 
5030         y1 = ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;);
5031     LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2; y2 = y1;
5032          y1 = y0; unit->m_freq = ZXP(freq); unit->m_rq = ZXP(rq););
5033 
5034     unit->m_a0 = a0;
5035     unit->m_a1 = a1;
5036     unit->m_a2 = a2;
5037     unit->m_b1 = b1;
5038     unit->m_b2 = b2;
5039     unit->m_y1 = zapgremlins(y1);
5040     unit->m_y2 = zapgremlins(y2);
5041 }
5042 
5043 
BAllPass_next_kk(BAllPass * unit,int inNumSamples)5044 void BAllPass_next_kk(BAllPass* unit, int inNumSamples) {
5045     float* out = ZOUT(0);
5046     float* in = ZIN(0);
5047     float nextfreq = ZIN0(1);
5048     float nextrq = ZIN0(2);
5049 
5050     double a0, a1, a2, b1, b2;
5051     double y1 = unit->m_y1;
5052     double y2 = unit->m_y2;
5053 
5054     a0 = unit->m_a0;
5055     a1 = unit->m_a1;
5056     a2 = unit->m_a2;
5057     b1 = unit->m_b1;
5058     b2 = unit->m_b2;
5059 
5060     if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq)) {
5061         double a0slope, a1slope, a2slope, b1slope, b2slope;
5062         double next_a0, next_a1, next_a2, next_b1, next_b2, next_w0, next_alpha, next_b0rz;
5063         next_w0 = twopi * (double)nextfreq * SAMPLEDUR;
5064         next_alpha = sin(next_w0) * 0.5 * (double)nextrq;
5065         next_b0rz = 1. / (1. + next_alpha);
5066         next_b1 = cos(next_w0) * 2. * next_b0rz;
5067         next_a0 = (1. - next_alpha) * next_b0rz;
5068         next_a1 = -next_b1;
5069         next_a2 = 1.;
5070         next_b2 = -next_a0;
5071         a0slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
5072         a1slope = (next_a1 - a1) * unit->mRate->mFilterSlope;
5073         a2slope = (next_a2 - a2) * unit->mRate->mFilterSlope;
5074         b1slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
5075         b2slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
5076         unit->m_freq = nextfreq;
5077         unit->m_rq = nextrq;
5078 
5079         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2, a0slope, a1slope, a2slope, b1slope, b2slope);
5080 
5081         unit->m_a0 = next_a0;
5082         unit->m_a1 = next_a1;
5083         unit->m_a2 = next_a2;
5084         unit->m_b1 = next_b1;
5085         unit->m_b2 = next_b2;
5086 
5087     } else
5088         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2);
5089 
5090     unit->m_y1 = zapgremlins(y1);
5091     unit->m_y2 = zapgremlins(y2);
5092 }
5093 
5094 
5095 /* BLowShelf */
5096 struct BLowShelf : public Unit {
5097     double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
5098     float m_freq, m_rs, m_db;
5099 };
5100 
5101 extern "C" {
5102 void BLowShelf_next_kkk(BLowShelf* unit, int inNumSamples);
5103 void BLowShelf_next_aaa(BLowShelf* unit, int inNumSamples);
5104 void BLowShelf_Ctor(BLowShelf* unit);
5105 }
5106 
BLowShelf_Ctor(BLowShelf * unit)5107 void BLowShelf_Ctor(BLowShelf* unit) {
5108     if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate) && (INRATE(3) == calc_FullRate))
5109         SETCALC(BLowShelf_next_aaa);
5110     else
5111         SETCALC(BLowShelf_next_kkk);
5112 
5113     float freq = unit->m_freq = ZIN0(1);
5114     float rs = unit->m_rs = ZIN0(2);
5115     float db = unit->m_db = ZIN0(3);
5116     double a = pow(10., (double)db * 0.025);
5117     double w0 = twopi * (double)freq * SAMPLEDUR;
5118     double cosw0 = cos(w0);
5119     double sinw0 = sin(w0);
5120     double alpha = sinw0 * 0.5 * sqrt((a + (1. / a)) * ((double)rs - 1.) + 2.);
5121     double i = (a + 1.) * cosw0;
5122     double j = (a - 1.) * cosw0;
5123     double k = 2. * sqrt(a) * alpha;
5124     double b0rz = 1. / ((a + 1.) + j + k);
5125     unit->m_a0 = a * ((a + 1.) - j + k) * b0rz;
5126     unit->m_a1 = 2. * a * ((a - 1.) - i) * b0rz;
5127     unit->m_a2 = a * ((a + 1.) - j - k) * b0rz;
5128     unit->m_b1 = 2. * ((a - 1.) + i) * b0rz;
5129     unit->m_b2 = ((a + 1.) + j - k) * -b0rz;
5130 
5131     unit->m_y1 = 0.;
5132 
5133     unit->m_y2 = 0.;
5134     PUSH_LOOPVALS
5135     BLowShelf_next_kkk(unit, 1);
5136     POP_LOOPVALS
5137 }
5138 
BLowShelf_next_aaa(BLowShelf * unit,int inNumSamples)5139 void BLowShelf_next_aaa(BLowShelf* unit, int inNumSamples) {
5140     float* out = ZOUT(0);
5141     float* in = ZIN(0);
5142     float* freq = ZIN(1);
5143     float* rs = ZIN(2);
5144     float* db = ZIN(3);
5145 
5146     double a0, a1, a2, b1, b2, a, w0, cosw0, sinw0, alpha, i, j, k, b0rz;
5147     double y0, y1, y2;
5148     float nextfreq, nextrs, nextdb;
5149 
5150     y1 = unit->m_y1;
5151     y2 = unit->m_y2;
5152 
5153     a0 = unit->m_a0;
5154     a1 = unit->m_a1;
5155     a2 = unit->m_a2;
5156     b1 = unit->m_b1;
5157     b2 = unit->m_b2;
5158 
5159     LOOP(
5160         unit->mRate->mFilterLoops, nextfreq = ZXP(freq); nextrs = ZXP(rs); nextdb = ZXP(db);
5161         if ((unit->m_freq != nextfreq) || (unit->m_rs != nextrs) || (unit->m_db != nextdb)) {
5162             a = pow(10., (double)nextdb * 0.025);
5163             w0 = twopi * (double)nextfreq * SAMPLEDUR;
5164             sinw0 = sin(w0);
5165             cosw0 = cos(w0);
5166             alpha = sinw0 * 0.5 * sqrt((a + (1. / a)) * ((double)nextrs - 1.) + 2.);
5167             i = (a + 1.) * cosw0;
5168             j = (a - 1.) * cosw0;
5169             k = 2. * sqrt(a) * alpha;
5170             b0rz = 1. / ((a + 1.) + j + k);
5171             a0 = a * ((a + 1.) - j + k) * b0rz;
5172             a1 = 2. * a * ((a - 1.) - i) * b0rz;
5173             a2 = a * ((a + 1.) - j - k) * b0rz;
5174             b1 = 2. * ((a - 1.) + i) * b0rz;
5175             b2 = ((a + 1.) + j - k) * -b0rz;
5176             unit->m_freq = nextfreq;
5177             unit->m_rs = nextrs;
5178             unit->m_db = nextdb;
5179         } y0 = ZXP(in) + b1 * y1 + b2 * y2;
5180         ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5181 
5182         y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5183 
5184         y1 = ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;);
5185     LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2; y2 = y1;
5186          y1 = y0; unit->m_freq = ZXP(freq); unit->m_rs = ZXP(rs); unit->m_db = ZXP(db););
5187 
5188     unit->m_a0 = a0;
5189     unit->m_a1 = a1;
5190     unit->m_a2 = a2;
5191     unit->m_b1 = b1;
5192     unit->m_b2 = b2;
5193     unit->m_y1 = zapgremlins(y1);
5194     unit->m_y2 = zapgremlins(y2);
5195 }
5196 
BLowShelf_next_kkk(BLowShelf * unit,int inNumSamples)5197 void BLowShelf_next_kkk(BLowShelf* unit, int inNumSamples) {
5198     float* out = ZOUT(0);
5199     float* in = ZIN(0);
5200     float nextfreq = ZIN0(1);
5201     float nextrs = ZIN0(2);
5202     float nextdb = ZIN0(3);
5203 
5204     double a0, a1, a2, b1, b2, a, w0, cosw0, sinw0, alpha, i, j, k, b0rz;
5205     double y1, y2;
5206 
5207     y1 = unit->m_y1;
5208     y2 = unit->m_y2;
5209 
5210     a0 = unit->m_a0;
5211     a1 = unit->m_a1;
5212     a2 = unit->m_a2;
5213     b1 = unit->m_b1;
5214     b2 = unit->m_b2;
5215 
5216     if ((unit->m_freq != nextfreq) || (unit->m_rs != nextrs) || (unit->m_db != nextdb)) {
5217         double a0slope, a1slope, a2slope, b1slope, b2slope, nexta0, nexta1, nexta2, nextb1, nextb2;
5218         a = pow(10., (double)nextdb * 0.025);
5219         w0 = twopi * (double)nextfreq * SAMPLEDUR;
5220         sinw0 = sin(w0);
5221         cosw0 = cos(w0);
5222         alpha = sinw0 * 0.5 * sqrt((a + (1. / a)) * ((double)nextrs - 1.) + 2.);
5223         i = (a + 1.) * cosw0;
5224         j = (a - 1.) * cosw0;
5225         k = 2. * sqrt(a) * alpha;
5226         b0rz = 1. / ((a + 1.) + j + k);
5227         nexta0 = a * ((a + 1.) - j + k) * b0rz;
5228         nexta1 = 2. * a * ((a - 1.) - i) * b0rz;
5229         nexta2 = a * ((a + 1.) - j - k) * b0rz;
5230         nextb1 = 2. * ((a - 1.) + i) * b0rz;
5231         nextb2 = ((a + 1.) + j - k) * -b0rz;
5232         a0slope = (nexta0 - a0) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta0, a0);
5233         a1slope = (nexta1 - a1) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta1, a1);
5234         a2slope = (nexta2 - a2) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta2, a2);
5235         b1slope = (nextb1 - b1) * unit->mRate->mFilterSlope; // CALCSLOPE(nextb1, b1);
5236         b2slope = (nextb2 - b2) * unit->mRate->mFilterSlope; // CALCSLOPE(nextb2, b2);
5237         unit->m_freq = nextfreq;
5238         unit->m_db = nextdb;
5239         unit->m_rs = nextrs;
5240 
5241         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2, a0slope, a1slope, a2slope, b1slope, b2slope);
5242 
5243         unit->m_a0 = nexta0;
5244         unit->m_a1 = nexta1;
5245         unit->m_a2 = nexta2;
5246         unit->m_b1 = nextb1;
5247         unit->m_b2 = nextb2;
5248     } else
5249         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2);
5250 
5251     unit->m_y1 = zapgremlins(y1);
5252     unit->m_y2 = zapgremlins(y2);
5253 }
5254 
5255 /* BHiShelf */
5256 struct BHiShelf : public Unit {
5257     double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
5258     float m_freq, m_rs, m_db;
5259 };
5260 
5261 extern "C" {
5262 void BHiShelf_next_kkk(BHiShelf* unit, int inNumSamples);
5263 void BHiShelf_next_aaa(BHiShelf* unit, int inNumSamples);
5264 void BHiShelf_Ctor(BHiShelf* unit);
5265 }
5266 
BHiShelf_Ctor(BHiShelf * unit)5267 void BHiShelf_Ctor(BHiShelf* unit) {
5268     if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate) && (INRATE(3) == calc_FullRate))
5269         SETCALC(BHiShelf_next_aaa);
5270     else
5271         SETCALC(BHiShelf_next_kkk);
5272 
5273     float freq = unit->m_freq = ZIN0(1);
5274     float rs = unit->m_rs = ZIN0(2);
5275     float db = unit->m_db = ZIN0(3);
5276     double a = pow(10., (double)db * 0.025);
5277     double w0 = twopi * (double)freq * SAMPLEDUR;
5278     double cosw0 = cos(w0);
5279     double sinw0 = sin(w0);
5280     double alpha = sinw0 * 0.5 * sqrt((a + (1. / a)) * ((double)rs - 1.) + 2.);
5281     double i = (a + 1.) * cosw0;
5282     double j = (a - 1.) * cosw0;
5283     double k = 2. * sqrt(a) * alpha;
5284 
5285     double b0rz = 1. / ((a + 1.) - j + k);
5286     unit->m_a0 = a * ((a + 1.) + j + k) * b0rz;
5287     unit->m_a1 = -2. * a * ((a - 1.) + i) * b0rz;
5288     unit->m_a2 = a * ((a + 1.) + j - k) * b0rz;
5289     unit->m_b1 = -2. * ((a - 1.) - i) * b0rz;
5290     unit->m_b2 = ((a + 1.) - j - k) * -b0rz;
5291 
5292     unit->m_y1 = 0.;
5293 
5294     unit->m_y2 = 0.;
5295     PUSH_LOOPVALS
5296     BHiShelf_next_kkk(unit, 1);
5297     POP_LOOPVALS
5298 }
5299 
BHiShelf_next_aaa(BHiShelf * unit,int inNumSamples)5300 void BHiShelf_next_aaa(BHiShelf* unit, int inNumSamples) {
5301     float* out = ZOUT(0);
5302     float* in = ZIN(0);
5303     float* freq = ZIN(1);
5304     float* rs = ZIN(2);
5305     float* db = ZIN(3);
5306 
5307     double a0, a1, a2, b1, b2, a, w0, cosw0, sinw0, alpha, i, j, k, b0rz;
5308     double y0, y1, y2;
5309     float nextfreq, nextrs, nextdb;
5310 
5311     y1 = unit->m_y1;
5312     y2 = unit->m_y2;
5313 
5314     a0 = unit->m_a0;
5315     a1 = unit->m_a1;
5316     a2 = unit->m_a2;
5317     b1 = unit->m_b1;
5318     b2 = unit->m_b2;
5319 
5320     LOOP(
5321         unit->mRate->mFilterLoops, nextfreq = ZXP(freq); nextrs = ZXP(rs); nextdb = ZXP(db);
5322         if ((unit->m_freq != nextfreq) || (unit->m_rs != nextrs) || (unit->m_db != nextdb)) {
5323             a = pow(10., (double)nextdb * 0.025);
5324             w0 = twopi * (double)nextfreq * SAMPLEDUR;
5325             sinw0 = sin(w0);
5326             cosw0 = cos(w0);
5327             alpha = sinw0 * 0.5 * sqrt((a + (1. / a)) * ((double)nextrs - 1.) + 2.);
5328             i = (a + 1.) * cosw0;
5329             j = (a - 1.) * cosw0;
5330             k = 2. * sqrt(a) * alpha;
5331             b0rz = 1. / ((a + 1.) - j + k);
5332             a0 = a * ((a + 1.) + j + k) * b0rz;
5333             a1 = -2. * a * ((a - 1.) + i) * b0rz;
5334             a2 = a * ((a + 1.) + j - k) * b0rz;
5335             b1 = -2. * ((a - 1.) - i) * b0rz;
5336             b2 = ((a + 1.) - j - k) * -b0rz;
5337             unit->m_freq = ZXP(freq);
5338             unit->m_rs = ZXP(rs);
5339             unit->m_db = ZXP(db);
5340         } y0 = ZXP(in) + b1 * y1 + b2 * y2;
5341         ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5342 
5343         y2 = ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5344 
5345         y1 = ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;);
5346     LOOP(unit->mRate->mFilterRemain, y0 = ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2; y2 = y1;
5347          y1 = y0; unit->m_freq = ZXP(freq); unit->m_rs = ZXP(rs); unit->m_db = ZXP(db););
5348 
5349     unit->m_a0 = a0;
5350     unit->m_a1 = a1;
5351     unit->m_a2 = a2;
5352     unit->m_b1 = b1;
5353     unit->m_b2 = b2;
5354     unit->m_y1 = zapgremlins(y1);
5355     unit->m_y2 = zapgremlins(y2);
5356 }
5357 
BHiShelf_next_kkk(BHiShelf * unit,int inNumSamples)5358 void BHiShelf_next_kkk(BHiShelf* unit, int inNumSamples) {
5359     float* out = ZOUT(0);
5360     float* in = ZIN(0);
5361     float nextfreq = ZIN0(1);
5362     float nextrs = ZIN0(2);
5363     float nextdb = ZIN0(3);
5364 
5365     double a0, a1, a2, b1, b2, a, w0, cosw0, sinw0, alpha, i, j, k, b0rz;
5366     double y1, y2;
5367 
5368     y1 = unit->m_y1;
5369     y2 = unit->m_y2;
5370 
5371     a0 = unit->m_a0;
5372     a1 = unit->m_a1;
5373     a2 = unit->m_a2;
5374     b1 = unit->m_b1;
5375     b2 = unit->m_b2;
5376 
5377     if ((unit->m_freq != nextfreq) || (unit->m_rs != nextrs) || (unit->m_db != nextdb)) {
5378         double a0slope, a1slope, a2slope, b1slope, b2slope;
5379         double next_a0, next_a1, next_a2, next_b1, next_b2;
5380         a = pow(10., (double)nextdb * 0.025);
5381         w0 = twopi * (double)nextfreq * SAMPLEDUR;
5382         sinw0 = sin(w0);
5383         cosw0 = cos(w0);
5384         alpha = sinw0 * 0.5 * sqrt((a + (1. / a)) * ((double)nextrs - 1.) + 2.);
5385         i = (a + 1.) * cosw0;
5386         j = (a - 1.) * cosw0;
5387         k = 2. * sqrt(a) * alpha;
5388         b0rz = 1. / ((a + 1.) - j + k);
5389         next_a0 = a * ((a + 1.) + j + k) * b0rz;
5390         next_a1 = -2. * a * ((a - 1.) + i) * b0rz;
5391         next_a2 = a * ((a + 1.) + j - k) * b0rz;
5392         next_b1 = -2. * ((a - 1.) - i) * b0rz;
5393         next_b2 = ((a + 1.) - j - k) * -b0rz;
5394         a0slope = (next_a0 - a0) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta0, a0);
5395         a1slope = (next_a1 - a1) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta1, a1);
5396         a2slope = (next_a2 - a2) * unit->mRate->mFilterSlope; // CALCSLOPE(nexta2, a2);
5397         b1slope = (next_b1 - b1) * unit->mRate->mFilterSlope; // CALCSLOPE(nextb1, b1);
5398         b2slope = (next_b2 - b2) * unit->mRate->mFilterSlope; // CALCSLOPE(nextb2, b2);
5399         unit->m_freq = nextfreq;
5400         unit->m_db = nextdb;
5401         unit->m_rs = nextrs;
5402 
5403         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2, a0slope, a1slope, a2slope, b1slope, b2slope);
5404 
5405         unit->m_a0 = next_a0;
5406         unit->m_a1 = next_a1;
5407         unit->m_a2 = next_a2;
5408         unit->m_b1 = next_b1;
5409         unit->m_b2 = next_b2;
5410 
5411     } else
5412         BPerformFilterLoop(unit, out, in, y1, y2, a0, a1, a2, b1, b2);
5413 
5414     unit->m_y1 = zapgremlins(y1);
5415     unit->m_y2 = zapgremlins(y2);
5416 }
5417 
5418 ////////////////////////////////////////////////////////////////////////////////////////////////////////
PluginLoad(Filter)5419 PluginLoad(Filter) {
5420     ft = inTable;
5421 
5422     DefineSimpleUnit(Ramp);
5423     DefineSimpleUnit(Lag);
5424     DefineSimpleUnit(Lag2);
5425     DefineSimpleUnit(Lag3);
5426     DefineSimpleUnit(LagUD);
5427     DefineSimpleUnit(Lag2UD);
5428     DefineSimpleUnit(Lag3UD);
5429     DefineSimpleUnit(VarLag);
5430     DefineSimpleUnit(OnePole);
5431     DefineSimpleUnit(OneZero);
5432     DefineSimpleUnit(TwoPole);
5433     DefineSimpleUnit(TwoZero);
5434     DefineSimpleUnit(Decay);
5435     DefineSimpleUnit(Decay2);
5436     DefineSimpleUnit(Flip);
5437     DefineSimpleUnit(Delay1);
5438     DefineSimpleUnit(Delay2);
5439     DefineSimpleUnit(Integrator);
5440     DefineSimpleUnit(LeakDC);
5441     DefineSimpleUnit(LPZ1);
5442     DefineSimpleUnit(HPZ1);
5443     DefineSimpleUnit(LPZ2);
5444     DefineSimpleUnit(HPZ2);
5445     DefineSimpleUnit(BPZ2);
5446     DefineSimpleUnit(BRZ2);
5447     DefineSimpleUnit(APF);
5448     DefineSimpleUnit(LPF);
5449     DefineSimpleUnit(HPF);
5450     DefineSimpleUnit(BPF);
5451     DefineSimpleUnit(BRF);
5452     DefineSimpleUnit(RLPF);
5453     DefineSimpleUnit(RHPF);
5454 
5455     DefineSimpleUnit(Slew);
5456     DefineSimpleUnit(Slope);
5457 
5458     DefineSimpleUnit(MidEQ);
5459     DefineSimpleUnit(Median);
5460 
5461     DefineSimpleUnit(Resonz);
5462     DefineSimpleUnit(Ringz);
5463     DefineSimpleUnit(Formlet);
5464 
5465     DefineSimpleUnit(FOS);
5466     DefineSimpleUnit(SOS);
5467 
5468     DefineSimpleUnit(Compander);
5469     DefineDtorUnit(Limiter);
5470     DefineDtorUnit(Normalizer);
5471 
5472     DefineSimpleUnit(Amplitude);
5473     DefineSimpleUnit(DetectSilence);
5474 
5475     DefineSimpleUnit(Hilbert);
5476     DefineSimpleUnit(FreqShift);
5477     DefineSimpleUnit(MoogFF);
5478 
5479     /* BEQSuite UGens */
5480     DefineSimpleUnit(BLowPass);
5481     DefineSimpleUnit(BHiPass);
5482     DefineSimpleUnit(BBandPass);
5483     DefineSimpleUnit(BBandStop);
5484     DefineSimpleUnit(BPeakEQ);
5485     DefineSimpleUnit(BAllPass);
5486     DefineSimpleUnit(BLowShelf);
5487     DefineSimpleUnit(BHiShelf);
5488 }
5489 
5490 //////////////////////////////////////////////////////////////////////////////////////////////////
5491