class library: SynthDef - replaceUGen fixes
[supercollider.git] / server / plugins / FilterUGens.cpp
blob9ba5383531425e8b218fc699e4baa1e082ecff16
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
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.
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.
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
22 #include "SC_PlugIn.h"
24 #define PI 3.1415926535898f
26 #define PUSH_LOOPVALS \
27 int tmp_floops = unit->mRate->mFilterLoops; \
28 int tmp_fremain = unit->mRate->mFilterRemain; \
29 unit->mRate->mFilterLoops = 0; \
30 unit->mRate->mFilterRemain = 1;
32 #define POP_LOOPVALS \
33 unit->mRate->mFilterLoops = tmp_floops; \
34 unit->mRate->mFilterRemain = tmp_fremain;
36 using namespace std; // for math functions
38 static InterfaceTable *ft;
40 struct Ramp : public Unit
42 double m_level, m_slope;
43 int m_counter;
46 struct Lag : public Unit
48 float m_lag, m_b1, m_y1;
51 struct Lag2 : public Unit
53 float m_lag, m_b1, m_y1a, m_y1b;
56 struct Lag3 : public Unit
58 float m_lag, m_b1, m_y1a, m_y1b, m_y1c;
61 struct LagUD : public Unit
63 float m_lagu, m_lagd, m_b1u, m_b1d, m_y1;
66 struct Lag2UD : public Unit
68 float m_lagu, m_lagd, m_b1u, m_b1d, m_y1a, m_y1b;
71 struct Lag3UD : public Unit
73 float m_lagu, m_lagd, m_b1u, m_b1d, m_y1a, m_y1b, m_y1c;
76 struct VarLag : public Unit
78 double m_level, m_slope;
79 int m_counter;
80 float m_in, m_lagTime;
83 struct OnePole : public Unit
85 float m_b1, m_y1;
88 struct OneZero : public Unit
90 float m_b1, m_x1;
93 struct Integrator : public Unit
95 float m_b1, m_y1;
98 struct Decay : public Unit
100 float m_decayTime, m_y1, m_b1;
103 struct Decay2 : public Unit
105 float m_attackTime, m_y1a, m_b1a;
106 float m_decayTime, m_y1b, m_b1b;
109 struct LeakDC : public Unit
111 double m_b1, m_x1, m_y1;
114 struct TwoPole : public Unit
116 float m_y1, m_y2, m_b1, m_b2, m_freq, m_reson;
119 struct APF : public Unit
121 float m_y1, m_y2, m_x1, m_x2, m_b1, m_b2, m_freq, m_reson;
124 struct TwoZero : public Unit
126 float m_x1, m_x2, m_b1, m_b2, m_freq, m_reson;
129 struct LPZ1 : public Unit
131 float m_x1;
134 struct HPZ1 : public Unit
136 float m_x1;
139 struct HPZ2 : public Unit
141 float m_x1, m_x2;
144 struct BPZ2 : public Unit
146 float m_x1, m_x2;
149 struct BRZ2 : public Unit
151 float m_x1, m_x2;
154 struct LPZ2 : public Unit
156 float m_x1, m_x2;
159 struct Flip : public Unit
163 struct Delay1 : public Unit
165 float m_x1;
168 struct Delay2 : public Unit
170 float m_x1, m_x2;
173 struct Slope : public Unit
175 float m_x1;
178 struct Slew : public Unit
180 float mLevel;
183 struct RLPF : public Unit
185 float m_y1, m_y2, m_a0, m_b1, m_b2, m_freq, m_reson;
188 struct RHPF : public Unit
190 double m_y1, m_y2, m_a0, m_b1, m_b2;
191 float m_freq, m_reson;
194 struct LPF : public Unit
196 float m_y1, m_y2, m_a0, m_b1, m_b2, m_freq;
199 struct HPF : public Unit
201 double m_y1, m_y2, m_a0, m_b1, m_b2;
202 float m_freq;
205 struct BPF : public Unit
207 float m_y1, m_y2, m_a0, m_b1, m_b2, m_freq, m_bw;
210 struct BRF : public Unit
212 float m_y1, m_y2, m_a0, m_a1, m_b2, m_freq, m_bw;
215 struct MidEQ : public Unit
217 float m_y1, m_y2, m_a0, m_b1, m_b2, m_freq, m_bw, m_db;
220 struct Resonz : public Unit
222 float m_y1, m_y2, m_a0, m_b1, m_b2, m_freq, m_rq;
225 struct Ringz : public Unit
227 float m_y1, m_y2, m_b1, m_b2, m_freq, m_decayTime;
230 struct FOS : public Unit
232 float m_y1, m_a0, m_a1, m_b1;
235 struct SOS : public Unit
237 float m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
240 struct Formlet : public Unit
242 float m_freq, m_decayTime, m_attackTime;
243 float m_y01, m_y02, m_b01, m_b02;
244 float m_y11, m_y12, m_b11, m_b12;
247 const int kMAXMEDIANSIZE = 32;
249 struct Median : public Unit
251 float m_medianValue[kMAXMEDIANSIZE];
252 long m_medianAge[kMAXMEDIANSIZE];
253 long m_medianSize, m_medianIndex;
256 struct Compander : public Unit
258 float m_prevmaxval, m_gain, m_clamp, m_clampcoef, m_relax, m_relaxcoef;
261 struct Normalizer : public Unit
263 float *m_table, *m_xinbuf, *m_xoutbuf, *m_xmidbuf;
264 long m_flips, m_pos, m_bufsize;
265 float m_slope, m_level, m_curmaxval, m_prevmaxval, m_slopefactor;
268 struct Limiter : public Unit
270 float *m_table, *m_xinbuf, *m_xoutbuf, *m_xmidbuf;
271 long m_flips, m_pos, m_bufsize;
272 float m_slope, m_level, m_curmaxval, m_prevmaxval, m_slopefactor;
276 struct Amplitude : public Unit
278 float m_previn, m_clampcoef, m_relaxcoef, m_clamp_in, m_relax_in;
281 struct DetectSilence : public Unit
283 float mThresh;
284 int32 mCounter, mEndCounter;
287 struct Hilbert : public Unit
289 float m_coefs[12];
290 float m_y1[12];
293 struct FreqShift : public Unit
295 float m_coefs[12];
296 float m_y1[12];
297 int32 m_phase;
298 int32 m_phaseoffset, m_lomask;
299 double m_cpstoinc, m_radtoinc, m_phasein;
303 struct MoogFF : public Unit
305 float m_freq, m_b0, m_a1; // Resonant freq and corresponding vals; stored because we need to compare against prev vals
306 double m_wcD;
308 double m_T; // 1/SAMPLEFREQ
309 float m_s1, m_s2, m_s3, m_s4; // 1st order filter states
312 //////////////////////////////////////////////////////////////////////////////////////////////////
315 extern "C"
318 void Ramp_next(Ramp *unit, int inNumSamples);
319 void Ramp_next_1(Ramp *unit, int inNumSamples);
320 void Ramp_Ctor(Ramp* unit);
322 void Lag_next(Lag *unit, int inNumSamples);
323 void Lag_Ctor(Lag* unit);
325 void Lag2_next(Lag2 *unit, int inNumSamples);
326 void Lag2_Ctor(Lag2* unit);
328 void Lag3_next(Lag3 *unit, int inNumSamples);
329 void Lag3_Ctor(Lag3* unit);
331 void LagUD_next(LagUD *unit, int inNumSamples);
332 void LagUD_Ctor(LagUD* unit);
334 void Lag2UD_next(Lag2UD *unit, int inNumSamples);
335 void Lag2UD_Ctor(Lag2UD* unit);
337 void Lag3UD_next(Lag3UD *unit, int inNumSamples);
338 void Lag3UD_Ctor(Lag3UD* unit);
340 void VarLag_next(VarLag *unit, int inNumSamples);
341 void VarLag_Ctor(VarLag* unit);
343 void OnePole_next_a(OnePole *unit, int inNumSamples);
344 void OnePole_next_k(OnePole *unit, int inNumSamples);
345 void OnePole_Ctor(OnePole* unit);
347 void OneZero_next(OneZero* unit, int inNumSamples);
348 void OneZero_Ctor(OneZero* unit);
350 void Integrator_next(Integrator *unit, int inNumSamples);
351 void Integrator_Ctor(Integrator* unit);
353 void Decay_next(Decay *unit, int inNumSamples);
354 void Decay_Ctor(Decay* unit);
356 void Decay2_next(Decay2 *unit, int inNumSamples);
357 void Decay2_Ctor(Decay2* unit);
359 void LeakDC_next(LeakDC *unit, int inNumSamples);
360 void LeakDC_next_1(LeakDC* unit, int inNumSamples);
361 void LeakDC_Ctor(LeakDC* unit);
363 void TwoPole_next(TwoPole *unit, int inNumSamples);
364 void TwoPole_Ctor(TwoPole* unit);
366 void TwoZero_next(TwoZero *unit, int inNumSamples);
367 void TwoZero_Ctor(TwoZero* unit);
369 void APF_next(APF *unit, int inNumSamples);
370 void APF_Ctor(APF* unit);
372 void LPZ1_next(LPZ1 *unit, int inNumSamples);
373 void LPZ1_Ctor(LPZ1* unit);
375 void HPZ1_next(HPZ1 *unit, int inNumSamples);
376 void HPZ1_Ctor(HPZ1* unit);
378 void Slope_next(Slope *unit, int inNumSamples);
379 void Slope_Ctor(Slope* unit);
381 void Delay1_next(Delay1 *unit, int inNumSamples);
382 void Delay1_Ctor(Delay1* unit);
384 void Flip_Ctor(Flip* unit);
385 void Flip_next_even(Flip *unit, int inNumSamples);
386 void Flip_next_odd(Flip *unit, int inNumSamples);
388 void Delay2_next(Delay2 *unit, int inNumSamples);
389 void Delay2_Ctor(Delay2* unit);
391 void LPZ2_next(LPZ2 *unit, int inNumSamples);
392 void LPZ2_Ctor(LPZ2* unit);
394 void HPZ2_next(HPZ2 *unit, int inNumSamples);
395 void HPZ2_Ctor(HPZ2* unit);
397 void BPZ2_next(BPZ2 *unit, int inNumSamples);
398 void BPZ2_Ctor(BPZ2* unit);
400 void BRZ2_next(BRZ2 *unit, int inNumSamples);
401 void BRZ2_Ctor(BRZ2* unit);
403 void Slew_next(Slew *unit, int inNumSamples);
404 void Slew_Ctor(Slew* unit);
406 void RLPF_next(RLPF *unit, int inNumSamples);
407 void RLPF_next_1(RLPF *unit, int inNumSamples);
408 void RLPF_Ctor(RLPF* unit);
410 void RHPF_next(RHPF *unit, int inNumSamples);
411 void RHPF_next_1(RHPF *unit, int inNumSamples);
412 void RHPF_Ctor(RHPF* unit);
414 void LPF_next(LPF *unit, int inNumSamples);
415 void LPF_next_1(LPF *unit, int inNumSamples);
416 void LPF_Ctor(LPF* unit);
418 void HPF_next(HPF *unit, int inNumSamples);
419 void HPF_next_1(HPF *unit, int inNumSamples);
420 void HPF_Ctor(HPF* unit);
422 void BPF_next(BPF *unit, int inNumSamples);
423 void BPF_next_1(BPF *unit, int inNumSamples);
424 void BPF_Ctor(BPF* unit);
426 void BRF_next(BRF *unit, int inNumSamples);
427 void BRF_next_1(BRF *unit, int inNumSamples);
428 void BRF_Ctor(BRF* unit);
430 void Median_next(Median *unit, int inNumSamples);
431 void Median_Ctor(Median* unit);
433 void MidEQ_next(MidEQ *unit, int inNumSamples);
434 void MidEQ_Ctor(MidEQ* unit);
436 void Resonz_next(Resonz *unit, int inNumSamples);
437 void Resonz_Ctor(Resonz* unit);
439 void Ringz_next(Ringz *unit, int inNumSamples);
440 void Ringz_Ctor(Ringz* unit);
442 void Formlet_next(Formlet *unit, int inNumSamples);
443 void Formlet_next_1(Formlet *unit, int inNumSamples);
444 void Formlet_Ctor(Formlet* unit);
446 void FOS_next_k(FOS *unit, int inNumSamples);
447 void FOS_next_a(FOS *unit, int inNumSamples);
448 void FOS_next_1(FOS *unit, int inNumSamples);
449 void FOS_Ctor(FOS* unit);
451 void SOS_next_k(SOS *unit, int inNumSamples);
452 void SOS_next_a(SOS *unit, int inNumSamples);
453 void SOS_next_1(SOS *unit, int inNumSamples);
454 void SOS_Ctor(SOS* unit);
456 void Normalizer_next(Normalizer *unit, int inNumSamples);
457 void Normalizer_Ctor(Normalizer* unit);
458 void Normalizer_Dtor(Normalizer* unit);
460 void Limiter_next(Limiter *unit, int inNumSamples);
461 void Limiter_Ctor(Limiter* unit);
462 void Limiter_Dtor(Limiter* unit);
464 void Compander_next(Compander *unit, int inNumSamples);
465 void Compander_Ctor(Compander* unit);
467 void Amplitude_next(Amplitude *unit, int inNumSamples);
468 void Amplitude_next_kk(Amplitude *unit, int inNumSamples);
469 void Amplitude_next_atok(Amplitude *unit, int inNumSamples);
470 void Amplitude_next_atok_kk(Amplitude *unit, int inNumSamples);
471 void Amplitude_Ctor(Amplitude* unit);
473 void DetectSilence_next(DetectSilence *unit, int inNumSamples);
474 void DetectSilence_done(DetectSilence *unit, int inNumSamples);
475 void DetectSilence_Ctor(DetectSilence* unit);
477 void Hilbert_Ctor(Hilbert* unit);
478 void Hilbert_next(Hilbert *unit, int inNumSamples);
480 void FreqShift_Ctor(FreqShift* unit);
481 void FreqShift_next_kk(FreqShift *unit, int inNumSamples);
482 void FreqShift_next_aa(FreqShift *unit, int inNumSamples);
483 void FreqShift_next_ak(FreqShift *unit, int inNumSamples);
484 void FreqShift_next_ka(FreqShift *unit, int inNumSamples);
486 void MoogFF_next(MoogFF *unit, int inNumSamples);
487 void MoogFF_Ctor(MoogFF* unit);
490 void Lag_next(Lag *unit, int inNumSamples);
491 void Lag_Ctor(Lag* unit);
493 void Lag_next(Lag *unit, int inNumSamples);
494 void Lag_Ctor(Lag* unit);
498 //////////////////////////////////////////////////////////////////////////////////////////////////
500 void Ramp_next(Ramp *unit, int inNumSamples)
502 float *out = ZOUT(0);
503 float *in = IN(0);
504 float period = ZIN0(1);
506 double slope = unit->m_slope;
507 double level = unit->m_level;
508 int counter = unit->m_counter;
509 int remain = inNumSamples;
510 while (remain) {
511 int nsmps = sc_min(remain, counter);
512 LOOP(nsmps,
513 ZXP(out) = level;
514 level += slope;
516 in += nsmps;
517 counter -= nsmps;
518 remain -= nsmps;
519 if (counter <= 0) {
520 counter = (int)(period * SAMPLERATE);
521 counter = sc_max(1, counter);
522 slope = (*in - level) / counter;
525 unit->m_level = level;
526 unit->m_slope = slope;
527 unit->m_counter = counter;
530 void Ramp_next_1(Ramp *unit, int inNumSamples)
532 float *out = OUT(0);
534 *out = unit->m_level;
535 unit->m_level += unit->m_slope;
536 if (--unit->m_counter <= 0) {
537 float in = ZIN0(0);
538 float period = ZIN0(1);
539 int counter = (int)(period * SAMPLERATE);
540 unit->m_counter = counter = sc_max(1, counter);
541 unit->m_slope = (in - unit->m_level) / counter;
545 void Ramp_Ctor(Ramp* unit)
547 if (BUFLENGTH == 1) {
548 SETCALC(Ramp_next_1);
549 } else {
550 SETCALC(Ramp_next);
553 unit->m_counter = 1;
554 unit->m_level = ZIN0(0);
555 unit->m_slope = 0.f;
556 ZOUT0(0) = unit->m_level;
559 //////////////////////////////////////////////////////////////////////////////////////////////////
561 void Lag_next(Lag *unit, int inNumSamples)
563 float *out = ZOUT(0);
564 float *in = ZIN(0);
565 float lag = ZIN0(1);
568 float y1 = unit->m_y1;
569 float b1 = unit->m_b1;
571 if (lag == unit->m_lag) {
572 LOOP1(inNumSamples,
573 float y0 = ZXP(in);
574 ZXP(out) = y1 = y0 + b1 * (y1 - y0);
576 } else {
577 unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
578 float b1_slope = CALCSLOPE(unit->m_b1, b1);
579 unit->m_lag = lag;
580 LOOP1(inNumSamples,
581 b1 += b1_slope;
582 float y0 = ZXP(in);
583 ZXP(out) = y1 = y0 + b1 * (y1 - y0);
586 unit->m_y1 = zapgremlins(y1);
589 void Lag_next_1(Lag *unit, int inNumSamples)
591 float *out = OUT(0);
592 float *in = IN(0);
593 float lag = IN0(1);
595 float y1 = unit->m_y1;
596 float b1 = unit->m_b1;
598 if (lag == unit->m_lag) {
599 float y0 = *in;
600 *out = y1 = y0 + b1 * (y1 - y0);
601 } else {
602 unit->m_b1 = b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
603 unit->m_lag = lag;
604 float y0 = *in;
605 *out = y1 = y0 + b1 * (y1 - y0);
607 unit->m_y1 = zapgremlins(y1);
610 void Lag_Ctor(Lag* unit)
612 if (BUFLENGTH == 1)
613 SETCALC(Lag_next_1);
614 else
615 SETCALC(Lag_next);
617 unit->m_lag = 0.f;
618 unit->m_b1 = 0.f;
619 unit->m_y1 = ZIN0(0);
620 Lag_next(unit, 1);
623 //////////////////////////////////////////////////////////////////////////////////////////////////
624 void LagUD_next(LagUD *unit, int inNumSamples)
626 float *out = ZOUT(0);
627 float *in = ZIN(0);
628 float lagu = ZIN0(1);
629 float lagd = ZIN0(2);
631 float y1 = unit->m_y1;
632 float b1u = unit->m_b1u;
633 float b1d = unit->m_b1d;
635 if ( (lagu == unit->m_lagu) && (lagd == unit->m_lagd) ) {
636 LOOP1(inNumSamples,
637 float y0 = ZXP(in);
638 if ( y0 > y1 )
639 ZXP(out) = y1 = y0 + b1u * (y1 - y0);
640 else
641 ZXP(out) = y1 = y0 + b1d * (y1 - y0);
643 } else {
644 unit->m_b1u = lagu == 0.f ? 0.f : exp(log001 / (lagu * unit->mRate->mSampleRate));
645 float b1u_slope = CALCSLOPE(unit->m_b1u, b1u);
646 unit->m_lagu = lagu;
647 unit->m_b1d = lagd == 0.f ? 0.f : exp(log001 / (lagd * unit->mRate->mSampleRate));
648 float b1d_slope = CALCSLOPE(unit->m_b1d, b1d);
649 unit->m_lagd = lagd;
650 LOOP1(inNumSamples,
651 b1u += b1u_slope;
652 b1d += b1d_slope;
653 float y0 = ZXP(in);
654 if ( y0 > y1 )
655 ZXP(out) = y1 = y0 + b1u * (y1 - y0);
656 else
657 ZXP(out) = y1 = y0 + b1d * (y1 - y0);
660 unit->m_y1 = zapgremlins(y1);
663 void LagUD_Ctor(LagUD* unit)
665 SETCALC(LagUD_next);
667 unit->m_lagu = 0.f;
668 unit->m_lagd = 0.f;
669 unit->m_b1u = 0.f;
670 unit->m_b1d = 0.f;
671 unit->m_y1 = ZIN0(0);
672 LagUD_next(unit, 1);
675 //////////////////////////////////////////////////////////////////////////////////////////////////
677 void Lag2_next(Lag2 *unit, int inNumSamples)
679 float *out = ZOUT(0);
680 float *in = ZIN(0);
681 float lag = ZIN0(1);
683 float y1a = unit->m_y1a;
684 float y1b = unit->m_y1b;
685 float b1 = unit->m_b1;
687 if (lag == unit->m_lag) {
688 LOOP1(inNumSamples,
689 float y0a = ZXP(in);
690 y1a = y0a + b1 * (y1a - y0a);
691 y1b = y1a + b1 * (y1b - y1a);
692 ZXP(out) = y1b;
694 } else {
695 unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
696 float b1_slope = CALCSLOPE(unit->m_b1, b1);
697 unit->m_lag = lag;
698 LOOP1(inNumSamples,
699 b1 += b1_slope;
700 float y0a = ZXP(in);
701 y1a = y0a + b1 * (y1a - y0a);
702 y1b = y1a + b1 * (y1b - y1a);
703 ZXP(out) = y1b;
706 unit->m_y1a = zapgremlins(y1a);
707 unit->m_y1b = zapgremlins(y1b);
710 void Lag2_Ctor(Lag2* unit)
712 SETCALC(Lag2_next);
714 unit->m_lag = 0.f;
715 unit->m_b1 = 0.f;
716 unit->m_y1a = unit->m_y1b = ZIN0(0);
717 Lag2_next(unit, 1);
720 //////////////////////////////////////////////////////////////////////////////////////////////////
722 void Lag2UD_next(Lag2UD *unit, int inNumSamples)
724 float *out = ZOUT(0);
725 float *in = ZIN(0);
726 float lagu = ZIN0(1);
727 float lagd = ZIN0(2);
729 float y1a = unit->m_y1a;
730 float y1b = unit->m_y1b;
731 float b1u = unit->m_b1u;
732 float b1d = unit->m_b1d;
734 if ( (lagu == unit->m_lagu) && (lagd == unit->m_lagd) ) {
735 LOOP1(inNumSamples,
736 float y0a = ZXP(in);
737 if ( y0a > y1a ) {
738 y1a = y0a + b1u * (y1a - y0a);
739 } else {
740 y1a = y0a + b1d * (y1a - y0a);
742 if ( y1a > y1b )
743 y1b = y1a + b1u * (y1b - y1a);
744 else
745 y1b = y1a + b1d * (y1b - y1a);
746 ZXP(out) = y1b;
748 } else {
749 unit->m_b1u = lagu == 0.f ? 0.f : exp(log001 / (lagu * unit->mRate->mSampleRate));
750 float b1u_slope = CALCSLOPE(unit->m_b1u, b1u);
751 unit->m_lagu = lagu;
752 unit->m_b1d = lagd == 0.f ? 0.f : exp(log001 / (lagd * unit->mRate->mSampleRate));
753 float b1d_slope = CALCSLOPE(unit->m_b1d, b1d);
754 unit->m_lagd = lagd;
755 LOOP1(inNumSamples,
756 b1u += b1u_slope;
757 b1d += b1d_slope;
758 float y0a = ZXP(in);
759 if ( y0a > y1a ) {
760 y1a = y0a + b1u * (y1a - y0a);
761 } else {
762 y1a = y0a + b1d * (y1a - y0a);
764 if ( y1a > y1b )
765 y1b = y1a + b1u * (y1b - y1a);
766 else
767 y1b = y1a + b1d * (y1b - y1a);
768 ZXP(out) = y1b;
771 unit->m_y1a = zapgremlins(y1a);
772 unit->m_y1b = zapgremlins(y1b);
775 void Lag2UD_Ctor(Lag2UD* unit)
777 SETCALC(Lag2UD_next);
779 unit->m_lagu = 0.f;
780 unit->m_lagd = 0.f;
781 unit->m_b1u = 0.f;
782 unit->m_b1d = 0.f;
783 unit->m_y1a = unit->m_y1b = ZIN0(0);
784 Lag2UD_next(unit, 1);
787 //////////////////////////////////////////////////////////////////////////////////////////////////
789 void Lag3_next(Lag3 *unit, int inNumSamples)
791 float *out = ZOUT(0);
792 float *in = ZIN(0);
793 float lag = ZIN0(1);
795 float y1a = unit->m_y1a;
796 float y1b = unit->m_y1b;
797 float y1c = unit->m_y1c;
798 float b1 = unit->m_b1;
800 if (lag == unit->m_lag) {
801 LOOP1(inNumSamples,
802 float y0a = ZXP(in);
803 y1a = y0a + b1 * (y1a - y0a);
804 y1b = y1a + b1 * (y1b - y1a);
805 y1c = y1b + b1 * (y1c - y1b);
806 ZXP(out) = y1c;
808 } else {
809 unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
810 float b1_slope = CALCSLOPE(unit->m_b1, b1);
811 unit->m_lag = lag;
812 LOOP1(inNumSamples,
813 b1 += b1_slope;
814 float y0a = ZXP(in);
815 y1a = y0a + b1 * (y1a - y0a);
816 y1b = y1a + b1 * (y1b - y1a);
817 y1c = y1b + b1 * (y1c - y1b);
818 ZXP(out) = y1c;
821 unit->m_y1a = zapgremlins(y1a);
822 unit->m_y1b = zapgremlins(y1b);
823 unit->m_y1c = zapgremlins(y1c);
826 void Lag3_Ctor(Lag3* unit)
828 SETCALC(Lag3_next);
830 unit->m_lag = 0.f;
831 unit->m_b1 = 0.f;
832 unit->m_y1a = unit->m_y1b = unit->m_y1c = ZIN0(0);
833 Lag3_next(unit, 1);
836 //////////////////////////////////////////////////////////////////////////////////////////////////
838 void Lag3UD_next(Lag3UD *unit, int inNumSamples)
840 float *out = ZOUT(0);
841 float *in = ZIN(0);
842 float lagu = ZIN0(1);
843 float lagd = ZIN0(2);
845 float y1a = unit->m_y1a;
846 float y1b = unit->m_y1b;
847 float y1c = unit->m_y1c;
848 float b1u = unit->m_b1u;
849 float b1d = unit->m_b1d;
851 if ( (lagu == unit->m_lagu) && (lagd == unit->m_lagd) ) {
852 LOOP1(inNumSamples,
853 float y0a = ZXP(in);
854 if ( y0a > y1a ) {
855 y1a = y0a + b1u * (y1a - y0a);
856 } else {
857 y1a = y0a + b1d * (y1a - y0a);
859 if ( y1a > y1b ) {
860 y1b = y1a + b1u * (y1b - y1a);
861 } else {
862 y1b = y1a + b1d * (y1b - y1a);
864 if ( y1a > y1b ) {
865 y1c = y1b + b1u * (y1c - y1b);
866 } else {
867 y1c = y1b + b1d * (y1c - y1b);
869 ZXP(out) = y1c;
871 } else {
872 unit->m_b1u = lagu == 0.f ? 0.f : exp(log001 / (lagu * unit->mRate->mSampleRate));
873 float b1u_slope = CALCSLOPE(unit->m_b1u, b1u);
874 unit->m_lagu = lagu;
875 unit->m_b1d = lagd == 0.f ? 0.f : exp(log001 / (lagd * unit->mRate->mSampleRate));
876 float b1d_slope = CALCSLOPE(unit->m_b1d, b1d);
877 unit->m_lagd = lagd;
878 LOOP1(inNumSamples,
879 b1u += b1u_slope;
880 b1d += b1d_slope;
881 float y0a = ZXP(in);
882 if ( y0a > y1a ) {
883 y1a = y0a + b1u * (y1a - y0a);
884 } else {
885 y1a = y0a + b1d * (y1a - y0a);
887 if ( y1a > y1b ) {
888 y1b = y1a + b1u * (y1b - y1a);
889 } else {
890 y1b = y1a + b1d * (y1b - y1a);
892 if ( y1a > y1b ) {
893 y1c = y1b + b1u * (y1c - y1b);
894 } else {
895 y1c = y1b + b1d * (y1c - y1b);
897 ZXP(out) = y1c;
900 unit->m_y1a = zapgremlins(y1a);
901 unit->m_y1b = zapgremlins(y1b);
902 unit->m_y1c = zapgremlins(y1c);
905 void Lag3UD_Ctor(Lag3UD* unit)
907 SETCALC(Lag3UD_next);
909 unit->m_lagu = 0.f;
910 unit->m_lagd = 0.f;
911 unit->m_b1u = 0.f;
912 unit->m_b1d = 0.f;
914 unit->m_y1a = unit->m_y1b = unit->m_y1c = ZIN0(0);
915 Lag3UD_next(unit, 1);
918 //////////////////////////////////////////////////////////////////////////////////////////////////
920 void VarLag_next(VarLag *unit, int inNumSamples)
922 float *out = ZOUT(0);
923 float *in = IN(0);
924 float lagTime = ZIN0(1);
925 double slope = unit->m_slope;
926 double level = unit->m_level;
927 int counter = unit->m_counter;
928 int remain = inNumSamples;
930 if ( *in != unit->m_in) {
931 counter = (int)(lagTime * SAMPLERATE);
932 counter = unit->m_counter = sc_max(1, counter);
933 slope = unit->m_slope = ( *in - unit->m_level) / counter;
934 unit->m_in = *in;
935 unit->m_lagTime = lagTime;
936 } else {
937 if (lagTime != unit->m_lagTime) {
938 float scaleFactor = lagTime/unit->m_lagTime;
939 counter = (int) (unit->m_counter * scaleFactor);
940 counter = unit->m_counter = sc_max(1, counter);
941 slope = unit->m_slope / scaleFactor;
942 unit->m_lagTime = lagTime;
945 if(counter >0) {
946 LOOP(remain,
947 ZXP(out) = level;
948 if( counter > 0) {
949 level += slope; --counter;
950 } else {
951 level = unit->m_in;
955 LOOP(remain, ZXP(out) = level );
958 unit->m_level = level;
959 unit->m_slope = slope;
960 unit->m_counter = counter;
964 void VarLag_next_1(VarLag *unit, int inNumSamples)
966 float *out = OUT(0);
967 float in = *IN(0);
968 float lagTime = ZIN0(1);
969 int counter = unit->m_counter;
970 if ( in != unit->m_in) {
971 counter = (int)(lagTime * SAMPLERATE);
972 unit->m_counter = counter = sc_max(1, counter);
973 unit->m_slope = ( in - unit->m_level) / counter;
974 unit->m_in = in;
975 unit->m_lagTime = lagTime;
977 if (lagTime != unit->m_lagTime) {
978 if (counter != 0) {
979 double scaleFactor = lagTime/unit->m_lagTime;
980 counter = (int) (unit->m_counter * scaleFactor);
981 unit->m_counter = counter = sc_max(1, counter);
982 unit->m_slope = unit->m_slope / scaleFactor; }
983 unit->m_lagTime = lagTime; }
985 *out = unit->m_level;
987 if (unit->m_counter > 0) {
988 unit->m_level += unit->m_slope;
989 --unit->m_counter;
990 } else {
991 unit->m_level = unit->m_in;
996 void VarLag_Ctor(VarLag* unit)
998 if (BUFLENGTH == 1) {
999 SETCALC(VarLag_next_1);
1000 } else {
1001 SETCALC(VarLag_next);
1003 float in = *IN(0);
1004 float lagTime = ZIN0(1);
1005 unit->m_level = ZIN0(2);
1006 int counter = (int)(lagTime * SAMPLERATE);
1007 unit->m_counter = counter = sc_max(1, counter);
1008 unit->m_slope = ( in - unit->m_level) / counter;
1009 unit->m_in = in;
1010 unit->m_lagTime = lagTime;
1011 ZOUT0(0) = unit->m_level;
1015 //////////////////////////////////////////////////////////////////////////////////////////////////
1017 void OnePole_next_a(OnePole *unit, int inNumSamples)
1019 //printf("OnePole_next_a\n");
1021 float *out = ZOUT(0);
1022 float *in = ZIN(0);
1023 float *b1p = ZIN(1);
1025 float y1 = unit->m_y1;
1027 LOOP1(inNumSamples,
1028 float y0 = ZXP(in);
1029 float b1 = ZXP(b1p);
1030 ZXP(out) = y1 = y0 + b1 * (y1 - y0);
1032 unit->m_y1 = zapgremlins(y1);
1035 void OnePole_next_k(OnePole *unit, int inNumSamples)
1037 //printf("OnePole_next_a\n");
1039 float *out = ZOUT(0);
1040 float *in = ZIN(0);
1041 float b1 = unit->m_b1;
1042 unit->m_b1 = ZIN0(1);
1044 float y1 = unit->m_y1;
1046 if (b1 == unit->m_b1) {
1047 if (b1 >= 0.f) {
1048 LOOP1(inNumSamples,
1049 float y0 = ZXP(in);
1050 ZXP(out) = y1 = y0 + b1 * (y1 - y0);
1052 } else {
1053 LOOP1(inNumSamples,
1054 float y0 = ZXP(in);
1055 ZXP(out) = y1 = y0 + b1 * (y1 + y0);
1058 } else {
1059 float b1_slope = CALCSLOPE(unit->m_b1, b1);
1060 if (b1 >= 0.f && unit->m_b1 >= 0) {
1061 LOOP1(inNumSamples,
1062 float y0 = ZXP(in);
1063 ZXP(out) = y1 = y0 + b1 * (y1 - y0);
1064 b1 += b1_slope;
1066 } else if (b1 <= 0.f && unit->m_b1 <= 0) {
1067 LOOP1(inNumSamples,
1068 float y0 = ZXP(in);
1069 ZXP(out) = y1 = y0 + b1 * (y1 + y0);
1070 b1 += b1_slope;
1072 } else {
1073 LOOP1(inNumSamples,
1074 float y0 = ZXP(in);
1075 ZXP(out) = y1 = (1.f - std::abs(b1)) * y0 + b1 * y1;
1076 b1 += b1_slope;
1080 unit->m_y1 = zapgremlins(y1);
1083 void OnePole_Ctor(OnePole* unit)
1085 if (INRATE(1) == calc_FullRate) {
1086 SETCALC(OnePole_next_a);
1087 } else {
1088 SETCALC(OnePole_next_k);
1090 unit->m_b1 = 0.f;
1091 unit->m_y1 = 0.f;
1092 OnePole_next_a(unit, 1);
1095 //////////////////////////////////////////////////////////////////////////////////////////////////
1097 void OneZero_Ctor(OneZero* unit)
1099 SETCALC(OneZero_next);
1100 unit->m_b1 = 0.f;
1101 unit->m_x1 = ZIN0(0);
1102 OneZero_next(unit, 1);
1106 void OneZero_next(OneZero* unit, int inNumSamples)
1108 //printf("OneZero::next\n");
1110 float *out = ZOUT(0);
1111 float *in = ZIN(0);
1112 float b1 = unit->m_b1;
1113 unit->m_b1 = ZIN0(1);
1115 float x1 = unit->m_x1;
1116 if (b1 == unit->m_b1) {
1117 if (b1 >= 0.f) {
1118 LOOP1(inNumSamples,
1119 float x0 = ZXP(in);
1120 ZXP(out) = x0 + b1 * (x1 - x0);
1121 x1 = x0;
1123 } else {
1124 LOOP1(inNumSamples,
1125 float x0 = ZXP(in);
1126 ZXP(out) = x0 + b1 * (x1 + x0);
1127 x1 = x0;
1130 } else {
1131 float b1_slope = CALCSLOPE(unit->m_b1, b1);
1132 if (b1 >= 0.f && unit->m_b1 >= 0) {
1133 LOOP1(inNumSamples,
1134 float x0 = ZXP(in);
1135 ZXP(out) = x0 + b1 * (x1 - x0);
1136 x1 = x0;
1137 b1 += b1_slope;
1139 } else if (b1 <= 0.f && unit->m_b1 <= 0) {
1140 LOOP1(inNumSamples,
1141 float x0 = ZXP(in);
1142 ZXP(out) = x0 + b1 * (x1 + x0);
1143 x1 = x0;
1144 b1 += b1_slope;
1146 } else {
1147 LOOP1(inNumSamples,
1148 float x0 = ZXP(in);
1149 ZXP(out) = (1.f - std::abs(b1)) * x0 + b1 * x1;
1150 x1 = x0;
1151 b1 += b1_slope;
1155 unit->m_x1 = x1;
1158 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1160 void Integrator_Ctor(Integrator* unit)
1162 //printf("Integrator_Reset\n");
1163 SETCALC(Integrator_next);
1164 unit->m_b1 = 0.f;
1165 unit->m_y1 = 0.f;
1166 Integrator_next(unit, 1);
1169 void Integrator_next(Integrator* unit, int inNumSamples)
1171 //printf("Integrator_next_a\n");
1173 float *out = ZOUT(0);
1174 float *in = ZIN(0);
1175 float b1 = unit->m_b1;
1176 unit->m_b1 = ZIN0(1);
1178 float y1 = unit->m_y1;
1180 if (b1 == unit->m_b1) {
1181 if (b1 == 1.f) {
1182 LOOP1(inNumSamples,
1183 float y0 = ZXP(in);
1184 ZXP(out) = y1 = y0 + y1;
1186 } else if (b1 == 0.f) {
1187 LOOP1(inNumSamples,
1188 float y0 = ZXP(in);
1189 ZXP(out) = y1 = y0;
1191 } else {
1192 LOOP1(inNumSamples,
1193 float y0 = ZXP(in);
1194 ZXP(out) = y1 = y0 + b1 * y1;
1197 } else {
1198 float b1_slope = CALCSLOPE(unit->m_b1, b1);
1199 LOOP1(inNumSamples,
1200 float y0 = ZXP(in);
1201 ZXP(out) = y1 = y0 + b1 * y1;
1202 b1 += b1_slope;
1205 unit->m_y1 = zapgremlins(y1);
1208 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1210 void Decay_Ctor(Decay* unit)
1212 SETCALC(Decay_next);
1213 unit->m_decayTime = 0.f;
1214 unit->m_b1 = 0.f;
1215 unit->m_y1 = 0.f;
1216 Decay_next(unit, 1);
1219 void Decay_next(Decay* unit, int inNumSamples)
1221 //printf("Decay_next_a\n");
1223 float *out = ZOUT(0);
1224 float *in = ZIN(0);
1225 float decayTime = ZIN0(1);
1227 float y1 = unit->m_y1;
1228 float b1 = unit->m_b1;
1229 if (decayTime == unit->m_decayTime) {
1230 if (b1 == 0.f) {
1231 LOOP1(inNumSamples,
1232 float y0 = ZXP(in);
1233 ZXP(out) = y1 = y0;
1235 } else {
1236 LOOP1(inNumSamples,
1237 float y0 = ZXP(in);
1238 ZXP(out) = y1 = y0 + b1 * y1;
1241 } else {
1242 unit->m_b1 = decayTime == 0.f ? 0.f : exp(log001 / (decayTime * SAMPLERATE));
1243 unit->m_decayTime = decayTime;
1244 float b1_slope = CALCSLOPE(unit->m_b1, b1);
1245 //printf("decayTime %g %g %g\n", unit->m_decayTime, next_b1, b1);
1246 LOOP1(inNumSamples,
1247 float y0 = ZXP(in);
1248 ZXP(out) = y1 = y0 + b1 * y1;
1249 b1 += b1_slope;
1252 unit->m_y1 = zapgremlins(y1);
1255 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1258 void Decay2_Ctor(Decay2 *unit)
1260 SETCALC(Decay2_next);
1262 float attackTime = ZIN0(1);
1263 float decayTime = ZIN0(2);
1264 unit->m_b1a = decayTime == 0.f ? 0.f : exp(log001 / (decayTime * SAMPLERATE));
1265 unit->m_b1b = attackTime == 0.f ? 0.f : exp(log001 / (attackTime * SAMPLERATE));
1266 unit->m_decayTime = decayTime;
1267 unit->m_attackTime = attackTime;
1269 float y0 = ZIN0(0);
1270 unit->m_y1a = y0;
1271 unit->m_y1b = y0;
1272 ZOUT0(0) = 0.f;
1275 void Decay2_next(Decay2* unit, int inNumSamples)
1277 //printf("Decay2_next_a\n");
1279 float *out = ZOUT(0);
1280 float *in = ZIN(0);
1281 float attackTime = ZIN0(1);
1282 float decayTime = ZIN0(2);
1284 float y1a = unit->m_y1a;
1285 float y1b = unit->m_y1b;
1286 float b1a = unit->m_b1a;
1287 float b1b = unit->m_b1b;
1288 if (decayTime == unit->m_decayTime && attackTime == unit->m_attackTime) {
1289 LOOP1(inNumSamples,
1290 float y0 = ZXP(in);
1291 y1a = y0 + b1a * y1a;
1292 y1b = y0 + b1b * y1b;
1293 ZXP(out) = y1a - y1b;
1295 } else {
1296 unit->m_decayTime = decayTime;
1297 unit->m_attackTime = attackTime;
1298 float next_b1a = decayTime == 0.f ? 0.f : exp(log001 / (decayTime * SAMPLERATE));
1299 float next_b1b = attackTime == 0.f ? 0.f : exp(log001 / (attackTime * SAMPLERATE));
1300 unit->m_decayTime = decayTime;
1301 float b1a_slope = CALCSLOPE(next_b1a, b1a);
1302 float b1b_slope = CALCSLOPE(next_b1b, b1b);
1303 unit->m_b1a = next_b1a;
1304 unit->m_b1b = next_b1b;
1305 LOOP1(inNumSamples,
1306 float y0 = ZXP(in);
1307 y1a = y0 + b1a * y1a;
1308 y1b = y0 + b1b * y1b;
1309 ZXP(out) = y1a - y1b;
1310 b1a += b1a_slope;
1311 b1b += b1b_slope;
1314 unit->m_y1a = y1a;
1315 unit->m_y1b = y1b;
1316 unit->m_b1a = b1a;
1317 unit->m_b1b = b1b;
1320 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1322 void LeakDC_next_i_4(LeakDC* unit, int inNumSamples)
1324 float *out = ZOUT(0);
1325 float *in = ZIN(0);
1326 double b1 = unit->m_b1;
1327 double y1 = unit->m_y1;
1328 double x1 = unit->m_x1;
1330 LOOP1(inNumSamples/4,
1331 double x00 = ZXP(in);
1332 double x01 = ZXP(in);
1333 double x02 = ZXP(in);
1334 double x03 = ZXP(in);
1335 ZXP(out) = y1 = x00 - x1 + b1 * y1;
1336 ZXP(out) = y1 = x01 - x00 + b1 * y1;
1337 ZXP(out) = y1 = x02 - x01 + b1 * y1;
1338 ZXP(out) = y1 = x03 - x02 + b1 * y1;
1340 x1 = x03;
1342 unit->m_x1 = x1;
1343 unit->m_y1 = zapgremlins(y1);
1346 void LeakDC_next_i(LeakDC* unit, int inNumSamples)
1348 float *out = ZOUT(0);
1349 float *in = ZIN(0);
1350 double b1 = unit->m_b1;
1351 double y1 = unit->m_y1;
1352 double x1 = unit->m_x1;
1354 LOOP1(inNumSamples,
1355 double x0 = ZXP(in);
1356 ZXP(out) = y1 = x0 - x1 + b1 * y1;
1357 x1 = x0;
1359 unit->m_x1 = x1;
1360 unit->m_y1 = zapgremlins(y1);
1363 void LeakDC_next(LeakDC* unit, int inNumSamples)
1365 if (ZIN0(1) == unit->m_b1) {
1366 if ((inNumSamples & 3) == 0)
1367 LeakDC_next_i_4(unit, inNumSamples);
1368 else
1369 LeakDC_next_i(unit, inNumSamples);
1370 } else {
1371 float *out = ZOUT(0);
1372 float *in = ZIN(0);
1373 double b1 = unit->m_b1;
1374 unit->m_b1 = ZIN0(1);
1376 double y1 = unit->m_y1;
1377 double x1 = unit->m_x1;
1379 double b1_slope = CALCSLOPE(unit->m_b1, b1);
1380 LOOP1(inNumSamples,
1381 double x0 = ZXP(in);
1382 ZXP(out) = y1 = x0 - x1 + b1 * y1;
1383 x1 = x0;
1384 b1 += b1_slope;
1386 unit->m_x1 = x1;
1387 unit->m_y1 = zapgremlins(y1);
1392 void LeakDC_next_1(LeakDC* unit, int inNumSamples)
1394 double b1 = unit->m_b1 = ZIN0(1);
1396 double y1 = unit->m_y1;
1397 double x1 = unit->m_x1;
1399 double x0 = ZIN0(0);
1400 ZOUT0(0) = y1 = x0 - x1 + b1 * y1;
1401 x1 = x0;
1403 unit->m_x1 = x1;
1404 unit->m_y1 = zapgremlins(y1);
1407 void LeakDC_Ctor(LeakDC *unit)
1409 //printf("LeakDC_Ctor\n");
1410 if (BUFLENGTH == 1)
1411 SETCALC(LeakDC_next_1);
1412 else {
1413 if (INRATE(1) == calc_ScalarRate) {
1414 if ((BUFLENGTH & 3) == 0)
1415 SETCALC(LeakDC_next_i_4);
1416 else
1417 SETCALC(LeakDC_next_i);
1418 } else
1419 SETCALC(LeakDC_next);
1421 unit->m_b1 = 0.0;
1422 unit->m_x1 = ZIN0(0);
1423 unit->m_y1 = 0.0;
1424 LeakDC_next_1(unit, 1);
1428 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1430 void TwoPole_Ctor(TwoPole *unit)
1432 //printf("TwoPole_Reset\n");
1433 SETCALC(TwoPole_next);
1434 unit->m_b1 = 0.f;
1435 unit->m_b2 = 0.f;
1436 unit->m_y1 = 0.f;
1437 unit->m_y2 = 0.f;
1438 unit->m_freq = 0.f;
1439 unit->m_reson = 0.f;
1440 PUSH_LOOPVALS
1441 TwoPole_next(unit, 1);
1442 POP_LOOPVALS
1446 void TwoPole_next(TwoPole* unit, int inNumSamples)
1448 //printf("TwoPole_next_a\n");
1450 float *out = ZOUT(0);
1451 float *in = ZIN(0);
1452 float freq = ZIN0(1);
1453 float reson = ZIN0(2);
1455 float y0;
1456 float y1 = unit->m_y1;
1457 float y2 = unit->m_y2;
1459 if (freq != unit->m_freq || reson != unit->m_reson) {
1460 float b1 = unit->m_b1;
1461 float b2 = unit->m_b2;
1462 float b1_next = 2.f * reson * cos(freq * unit->mRate->mRadiansPerSample);
1463 float b2_next = -(reson * reson);
1464 float b1_slope = (b1_next - b1) * unit->mRate->mFilterSlope;
1465 float b2_slope = (b2_next - b2) * unit->mRate->mFilterSlope;
1466 LOOP(unit->mRate->mFilterLoops,
1467 ZXP(out) = y0 = ZXP(in) + b1 * y1 + b2 * y2;
1468 ZXP(out) = y2 = ZXP(in) + b1 * y0 + b2 * y1;
1469 ZXP(out) = y1 = ZXP(in) + b1 * y2 + b2 * y0;
1471 b1 += b1_slope;
1472 b2 += b2_slope;
1474 LOOP(unit->mRate->mFilterRemain,
1475 ZXP(out) = y0 = ZXP(in) + b1 * y1 + b2 * y2;
1476 y2 = y1;
1477 y1 = y0;
1480 unit->m_freq = freq;
1481 unit->m_reson = reson;
1482 unit->m_b1 = b1;
1483 unit->m_b2 = b2;
1484 } else {
1485 float b1 = unit->m_b1;
1486 float b2 = unit->m_b2;
1487 LOOP(unit->mRate->mFilterLoops,
1488 ZXP(out) = y0 = ZXP(in) + b1 * y1 + b2 * y2;
1489 ZXP(out) = y2 = ZXP(in) + b1 * y0 + b2 * y1;
1490 ZXP(out) = y1 = ZXP(in) + b1 * y2 + b2 * y0;
1492 LOOP(unit->mRate->mFilterRemain,
1493 ZXP(out) = y0 = ZXP(in) + b1 * y1 + b2 * y2;
1494 y2 = y1;
1495 y1 = y0;
1498 unit->m_y1 = zapgremlins(y1);
1499 unit->m_y2 = zapgremlins(y2);
1503 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1506 void TwoZero_Ctor(TwoZero* unit)
1508 //printf("TwoZero_Reset\n");
1509 SETCALC(TwoZero_next);
1510 unit->m_b1 = 0.f;
1511 unit->m_b2 = 0.f;
1512 unit->m_x1 = 0.f;
1513 unit->m_x2 = 0.f;
1514 unit->m_freq = 0.f;
1515 unit->m_reson = 0.f;
1516 PUSH_LOOPVALS
1517 TwoZero_next(unit, 1);
1518 POP_LOOPVALS
1521 void TwoZero_next(TwoZero* unit, int inNumSamples)
1523 //printf("TwoZero_next\n");
1525 float *out = ZOUT(0);
1526 float *in = ZIN(0);
1527 float freq = ZIN0(1);
1528 float reson = ZIN0(2);
1530 float x0;
1531 float x1 = unit->m_x1;
1532 float x2 = unit->m_x2;
1534 if (freq != unit->m_freq || reson != unit->m_reson) {
1535 float b1 = unit->m_b1;
1536 float b2 = unit->m_b2;
1537 float b1_next = -2.f * reson * cos(freq * unit->mRate->mRadiansPerSample);
1538 float b2_next = (reson * reson);
1539 float b1_slope = (b1_next - b1) * unit->mRate->mFilterSlope;
1540 float b2_slope = (b2_next - b2) * unit->mRate->mFilterSlope;
1541 LOOP(unit->mRate->mFilterLoops,
1542 x0 = ZXP(in);
1543 ZXP(out) = x0 + b1 * x1 + b2 * x2;
1544 x2 = ZXP(in);
1545 ZXP(out) = x2 + b1 * x0 + b2 * x1;
1546 x1 = ZXP(in);
1547 ZXP(out) = x1 + b1 * x2 + b2 * x0;
1549 b1 += b1_slope;
1550 b2 += b2_slope;
1552 LOOP(unit->mRate->mFilterRemain,
1553 x0 = ZXP(in);
1554 ZXP(out) = x0 + b1 * x1 + b2 * x2;
1555 x2 = x1;
1556 x1 = x0;
1559 unit->m_freq = freq;
1560 unit->m_reson = reson;
1561 unit->m_b1 = b1;
1562 unit->m_b2 = b2;
1563 } else {
1564 float b1 = unit->m_b1;
1565 float b2 = unit->m_b2;
1566 LOOP(unit->mRate->mFilterLoops,
1567 x0 = ZXP(in);
1568 ZXP(out) = x0 + b1 * x1 + b2 * x2;
1569 x2 = ZXP(in);
1570 ZXP(out) = x2 + b1 * x0 + b2 * x1;
1571 x1 = ZXP(in);
1572 ZXP(out) = x1 + b1 * x2 + b2 * x0;
1574 LOOP(unit->mRate->mFilterRemain,
1575 x0 = ZXP(in);
1576 ZXP(out) = x0 + b1 * x1 + b2 * x2;
1577 x2 = x1;
1578 x1 = x0;
1581 unit->m_x1 = x1;
1582 unit->m_x2 = x2;
1586 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1589 void APF_Ctor(APF* unit)
1591 //printf("APF_Reset\n");
1592 SETCALC(APF_next);
1593 unit->m_b1 = 0.f;
1594 unit->m_b2 = 0.f;
1595 unit->m_y1 = 0.f;
1596 unit->m_y2 = 0.f;
1597 unit->m_x1 = 0.f;
1598 unit->m_x2 = 0.f;
1599 unit->m_freq = 0.f;
1600 unit->m_reson = 0.f;
1601 PUSH_LOOPVALS
1602 APF_next(unit, 1);
1603 POP_LOOPVALS
1607 void APF_next(APF* unit, int inNumSamples)
1609 //printf("APF_next_a\n");
1611 float *out = ZOUT(0);
1612 float *in = ZIN(0);
1613 float freq = ZIN0(1);
1614 float reson = ZIN0(2);
1616 float x0, y0;
1617 float y1 = unit->m_y1;
1618 float y2 = unit->m_y2;
1619 float x1 = unit->m_x1;
1620 float x2 = unit->m_x2;
1622 if (freq != unit->m_freq || reson != unit->m_reson) {
1623 float b1 = unit->m_b1;
1624 float b2 = unit->m_b2;
1625 float b1_next = 2.f * reson * cos(freq * unit->mRate->mRadiansPerSample);
1626 float b2_next = -(reson * reson);
1627 float b1_slope = (b1_next - b1) * unit->mRate->mFilterSlope;
1628 float b2_slope = (b2_next - b2) * unit->mRate->mFilterSlope;
1629 LOOP(unit->mRate->mFilterLoops,
1630 x0 = ZXP(in);
1631 ZXP(out) = y0 = x0 + b1 * (y1 - x1) + b2 * (y2 - x2);
1632 x2 = ZXP(in);
1633 ZXP(out) = y2 = x2 + b1 * (y0 - x0) + b2 * (y2 - x1);
1634 x1 = ZXP(in);
1635 ZXP(out) = y1 = x1 + b1 * (y2 - x2) + b2 * (y2 - x0);
1637 b1 += b1_slope;
1638 b2 += b2_slope;
1640 LOOP(unit->mRate->mFilterRemain,
1641 x0 = ZXP(in);
1642 ZXP(out) = y0 = x0 + b1 * (y1 - x1) + b2 * (y2 - x2);
1643 y2 = y1;
1644 y1 = y0;
1645 x2 = x1;
1646 x1 = x0;
1649 unit->m_freq = freq;
1650 unit->m_reson = reson;
1651 unit->m_b1 = b1;
1652 unit->m_b2 = b2;
1653 } else {
1654 float b1 = unit->m_b1;
1655 float b2 = unit->m_b2;
1656 LOOP(unit->mRate->mFilterLoops,
1657 x0 = ZXP(in);
1658 ZXP(out) = y0 = x0 + b1 * (y1 - x1) + b2 * (y2 - x2);
1659 x2 = ZXP(in);
1660 ZXP(out) = y2 = x2 + b1 * (y0 - x0) + b2 * (y2 - x1);
1661 x1 = ZXP(in);
1662 ZXP(out) = y1 = x1 + b1 * (y2 - x2) + b2 * (y2 - x0);
1664 LOOP(unit->mRate->mFilterRemain,
1665 x0 = ZXP(in);
1666 ZXP(out) = y0 = x0 + b1 * (y1 - x1) + b2 * (y2 - x2);
1667 y2 = y1;
1668 y1 = y0;
1669 x2 = x1;
1670 x1 = x0;
1673 unit->m_y1 = zapgremlins(y1);
1674 unit->m_y2 = zapgremlins(y2);
1675 unit->m_x1 = x1;
1676 unit->m_x2 = x2;
1679 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1681 void LPZ1_Ctor(LPZ1* unit)
1683 //printf("LPZ1_Reset\n");
1684 SETCALC(LPZ1_next);
1685 unit->m_x1 = ZIN0(0);
1686 LPZ1_next(unit, 1);
1690 void LPZ1_next(LPZ1* unit, int inNumSamples)
1692 //printf("LPZ1_next_a\n");
1694 float *out = ZOUT(0);
1695 float *in = ZIN(0);
1697 float x0;
1698 float x1 = unit->m_x1;
1700 LOOP(inNumSamples >> 2,
1701 x0 = ZXP(in);
1702 ZXP(out) = 0.5f * (x0 + x1);
1703 x1 = ZXP(in);
1704 ZXP(out) = 0.5f * (x1 + x0);
1705 x0 = ZXP(in);
1706 ZXP(out) = 0.5f * (x0 + x1);
1707 x1 = ZXP(in);
1708 ZXP(out) = 0.5f * (x1 + x0);
1710 LOOP(inNumSamples & 3,
1711 x0 = ZXP(in);
1712 ZXP(out) = 0.5f * (x0 + x1);
1713 x1 = x0;
1716 unit->m_x1 = x1;
1719 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1722 void HPZ1_Ctor(HPZ1* unit)
1724 //printf("HPZ1_Reset\n");
1725 SETCALC(HPZ1_next);
1726 unit->m_x1 = ZIN0(0);
1727 HPZ1_next(unit, 1);
1731 void HPZ1_next(HPZ1* unit, int inNumSamples)
1733 //printf("HPZ1_next\n");
1735 float *out = ZOUT(0);
1736 float *in = ZIN(0);
1738 float x0;
1739 float x1 = unit->m_x1;
1741 LOOP(inNumSamples >> 2,
1742 x0 = ZXP(in);
1743 ZXP(out) = 0.5f * (x0 - x1);
1744 x1 = ZXP(in);
1745 ZXP(out) = 0.5f * (x1 - x0);
1746 x0 = ZXP(in);
1747 ZXP(out) = 0.5f * (x0 - x1);
1748 x1 = ZXP(in);
1749 ZXP(out) = 0.5f * (x1 - x0);
1751 LOOP(inNumSamples & 3,
1752 x0 = ZXP(in);
1753 //printf("%d %d %g %g\n", this, inNumSamples, x0, x1);
1754 ZXP(out) = 0.5f * (x0 - x1);
1755 x1 = x0;
1758 unit->m_x1 = x1;
1761 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1763 void Slope_Ctor(Slope* unit)
1765 //printf("Slope_Reset\n");
1766 SETCALC(Slope_next);
1767 unit->m_x1 = ZIN0(0);
1768 //printf("Slope_Reset %g\n", unit->m_x1);
1769 Slope_next(unit, 1);
1773 void Slope_next(Slope* unit, int inNumSamples)
1775 //printf("Slope_next_a %g\n", unit->m_x1);
1777 float *out = ZOUT(0);
1778 float *in = ZIN(0);
1780 float x1 = unit->m_x1;
1782 float sr = SAMPLERATE;
1783 LOOP1(inNumSamples,
1784 float x0 = ZXP(in);
1785 ZXP(out) = sr * (x0 - x1);
1786 x1 = x0;
1789 unit->m_x1 = x1;
1792 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1794 void Delay1_Ctor(Delay1* unit)
1796 //printf("Delay1_Reset\n");
1797 SETCALC(Delay1_next);
1798 unit->m_x1 = ZIN0(0);
1799 Delay1_next(unit, 1);
1803 void Delay1_next(Delay1* unit, int inNumSamples)
1805 //printf("Delay1_next_a\n");
1807 float *out = ZOUT(0);
1808 float *in = ZIN(0);
1810 float x0;
1811 float x1 = unit->m_x1;
1813 LOOP(inNumSamples >> 2,
1814 x0 = ZXP(in);
1815 ZXP(out) = x1;
1816 x1 = ZXP(in);
1817 ZXP(out) = x0;
1818 x0 = ZXP(in);
1819 ZXP(out) = x1;
1820 x1 = ZXP(in);
1821 ZXP(out) = x0;
1823 LOOP(inNumSamples & 3,
1824 x0 = ZXP(in);
1825 ZXP(out) = x1;
1826 x1 = x0;
1829 unit->m_x1 = x1;
1832 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1834 void Flip_Ctor(Flip* unit)
1836 if (unit->mBufLength & 1) {
1837 SETCALC(Flip_next_odd);
1838 Flip_next_odd(unit, 1);
1839 } else {
1840 SETCALC(Flip_next_even);
1841 ZOUT0(0) = ZIN0(0);
1846 void Flip_next_even(Flip* unit, int inNumSamples)
1848 float *out = ZOUT(0);
1849 float *in = ZIN(0);
1851 LOOP(inNumSamples >> 1,
1852 ZXP(out) = -ZXP(in);
1853 ZXP(out) = ZXP(in);
1857 void Flip_next_odd(Flip* unit, int inNumSamples)
1859 float *out = ZOUT(0);
1860 float *in = ZIN(0);
1862 if (unit->mWorld->mBufCounter & 1) {
1863 ZXP(out) = ZXP(in);
1864 LOOP(inNumSamples >> 1,
1865 ZXP(out) = -ZXP(in);
1866 ZXP(out) = ZXP(in);
1868 } else {
1869 LOOP(inNumSamples >> 1,
1870 ZXP(out) = -ZXP(in);
1871 ZXP(out) = ZXP(in);
1873 ZXP(out) = -ZXP(in);
1877 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1879 void Delay2_Ctor(Delay2* unit)
1881 SETCALC(Delay2_next);
1882 unit->m_x1 = ZIN0(0);
1883 ZOUT0(0) = 0.f;
1887 void Delay2_next(Delay2* unit, int inNumSamples)
1889 float *out = ZOUT(0);
1890 float *in = ZIN(0);
1892 float x0;
1893 float x1 = unit->m_x1;
1894 float x2 = unit->m_x2;
1896 LOOP(unit->mRate->mFilterLoops,
1897 x0 = ZXP(in);
1898 ZXP(out) = x2;
1899 x2 = ZXP(in);
1900 ZXP(out) = x1;
1901 x1 = ZXP(in);
1902 ZXP(out) = x0;
1904 LOOP(unit->mRate->mFilterRemain,
1905 x0 = ZXP(in);
1906 ZXP(out) = x2;
1907 x2 = x1;
1908 x1 = x0;
1911 unit->m_x1 = x1;
1912 unit->m_x2 = x2;
1915 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1918 void LPZ2_Ctor(LPZ2* unit)
1920 //printf("LPZ2_Reset\n");
1921 SETCALC(LPZ2_next);
1922 unit->m_x1 = unit->m_x2 = ZIN0(0);
1923 PUSH_LOOPVALS
1924 LPZ2_next(unit, 1);
1925 POP_LOOPVALS
1929 void LPZ2_next(LPZ2* unit, int inNumSamples)
1931 //printf("LPZ2_next_a\n");
1933 float *out = ZOUT(0);
1934 float *in = ZIN(0);
1936 float x0;
1937 float x1 = unit->m_x1;
1938 float x2 = unit->m_x2;
1940 LOOP(unit->mRate->mFilterLoops,
1941 x0 = ZXP(in);
1942 ZXP(out) = (x0 + 2.f * x1 + x2) * 0.25f;
1943 x2 = ZXP(in);
1944 ZXP(out) = (x2 + 2.f * x0 + x1) * 0.25f;
1945 x1 = ZXP(in);
1946 ZXP(out) = (x1 + 2.f * x2 + x0) * 0.25f;
1948 LOOP(unit->mRate->mFilterRemain,
1949 x0 = ZXP(in);
1950 ZXP(out) = (x0 + 2.f * x1 + x2) * 0.25f;
1951 x2 = x1;
1952 x1 = x0;
1955 unit->m_x1 = x1;
1956 unit->m_x2 = x2;
1959 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1961 void HPZ2_Ctor(HPZ2* unit)
1963 //printf("HPZ2_Reset\n");
1964 SETCALC(HPZ2_next);
1965 unit->m_x1 = unit->m_x2 = ZIN0(0);
1966 PUSH_LOOPVALS
1967 HPZ2_next(unit, 1);
1968 POP_LOOPVALS
1972 void HPZ2_next(HPZ2* unit, int inNumSamples)
1974 //printf("HPZ2_next_a\n");
1976 float *out = ZOUT(0);
1977 float *in = ZIN(0);
1979 float x0;
1980 float x1 = unit->m_x1;
1981 float x2 = unit->m_x2;
1983 LOOP(unit->mRate->mFilterLoops,
1984 x0 = ZXP(in);
1985 ZXP(out) = (x0 - 2.f * x1 + x2) * 0.25f;
1986 x2 = ZXP(in);
1987 ZXP(out) = (x2 - 2.f * x0 + x1) * 0.25f;
1988 x1 = ZXP(in);
1989 ZXP(out) = (x1 - 2.f * x2 + x0) * 0.25f;
1991 LOOP(unit->mRate->mFilterRemain,
1992 x0 = ZXP(in);
1993 ZXP(out) = (x0 - 2.f * x1 + x2) * 0.25f;
1994 x2 = x1;
1995 x1 = x0;
1998 unit->m_x1 = x1;
1999 unit->m_x2 = x2;
2002 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2004 void BPZ2_Ctor(BPZ2* unit)
2006 //printf("BPZ2_Reset\n");
2007 SETCALC(BPZ2_next);
2008 unit->m_x1 = unit->m_x2 = ZIN0(0);
2009 PUSH_LOOPVALS
2010 BPZ2_next(unit, 1);
2011 POP_LOOPVALS
2015 void BPZ2_next(BPZ2* unit, int inNumSamples)
2017 //printf("BPZ2_next_a\n");
2019 float *out = ZOUT(0);
2020 float *in = ZIN(0);
2022 float x0;
2023 float x1 = unit->m_x1;
2024 float x2 = unit->m_x2;
2026 LOOP(unit->mRate->mFilterLoops,
2027 x0 = ZXP(in);
2028 ZXP(out) = (x0 - x2) * 0.5f;
2029 x2 = ZXP(in);
2030 ZXP(out) = (x2 - x1) * 0.5f;
2031 x1 = ZXP(in);
2032 ZXP(out) = (x1 - x0) * 0.5f;
2034 LOOP(unit->mRate->mFilterRemain,
2035 x0 = ZXP(in);
2036 ZXP(out) = (x0 - x2) * 0.5f;
2037 x2 = x1;
2038 x1 = x0;
2041 unit->m_x1 = x1;
2042 unit->m_x2 = x2;
2045 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2047 void BRZ2_Ctor(BRZ2* unit)
2049 //printf("BRZ2_Reset\n");
2050 SETCALC(BRZ2_next);
2051 unit->m_x1 = unit->m_x2 = ZIN0(0);
2052 PUSH_LOOPVALS
2053 BRZ2_next(unit, 1);
2054 POP_LOOPVALS
2058 void BRZ2_next(BRZ2* unit, int inNumSamples)
2060 //printf("BRZ2_next_a\n");
2062 float *out = ZOUT(0);
2063 float *in = ZIN(0);
2065 float x0;
2066 float x1 = unit->m_x1;
2067 float x2 = unit->m_x2;
2069 LOOP(unit->mRate->mFilterLoops,
2070 x0 = ZXP(in);
2071 ZXP(out) = (x0 + x2) * 0.5f;
2072 x2 = ZXP(in);
2073 ZXP(out) = (x2 + x1) * 0.5f;
2074 x1 = ZXP(in);
2075 ZXP(out) = (x1 + x0) * 0.5f;
2077 LOOP(unit->mRate->mFilterRemain,
2078 x0 = ZXP(in);
2079 ZXP(out) = (x0 + x2) * 0.5f;
2080 x2 = x1;
2081 x1 = x0;
2084 unit->m_x1 = x1;
2085 unit->m_x2 = x2;
2088 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2090 void Slew_Ctor(Slew* unit)
2092 //printf("Slew_Reset\n");
2093 SETCALC(Slew_next);
2094 unit->mLevel = ZIN0(0);
2095 Slew_next(unit, 1);
2099 void Slew_next(Slew* unit, int inNumSamples)
2101 //printf("Slew_next_a\n");
2103 float *out = ZOUT(0);
2104 float *in = ZIN(0);
2105 float upf = ZIN0(1) * unit->mRate->mSampleDur;
2106 float dnf = 0.f - ZIN0(2) * unit->mRate->mSampleDur;
2107 float level = unit->mLevel;
2108 LOOP1(inNumSamples,
2109 float slope = ZXP(in) - level;
2110 level += sc_clip(slope,dnf,upf);
2111 ZXP(out) = level;
2113 unit->mLevel = level;
2116 /*void Slew_next_1(Slew* unit, int inNumSamples)
2118 //printf("Slew_next_a\n");
2120 float *out = ZOUT(0);
2121 float in = ZIN0(0);
2122 float upf = ZIN0(1);
2123 float dnf = ZIN0(2);
2124 float level = unit->mLevel;
2125 float slope = ZXP(in) - level;
2126 if (slope > upf) slope = upf;
2127 else if (slope < dnf) slope = dnf;
2128 level += slope;
2129 ZXP(out) = level;
2130 unit->mLevel = level;
2134 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2136 void RLPF_Ctor(RLPF* unit)
2138 //printf("RLPF_Reset\n");
2139 if (unit->mBufLength == 1) {
2140 SETCALC(RLPF_next_1);
2141 } else {
2142 SETCALC(RLPF_next);
2145 unit->m_a0 = 0.f;
2146 unit->m_b1 = 0.f;
2147 unit->m_b2 = 0.f;
2148 unit->m_y1 = 0.f;
2149 unit->m_y2 = 0.f;
2150 unit->m_freq = 0.f;
2151 unit->m_reson = 0.f;
2152 RLPF_next_1(unit, 1);
2156 void RLPF_next(RLPF* unit, int inNumSamples)
2158 //printf("RLPF_next\n");
2160 float *out = ZOUT(0);
2161 float *in = ZIN(0);
2162 float freq = ZIN0(1);
2163 float reson = ZIN0(2);
2165 float y0;
2166 float y1 = unit->m_y1;
2167 float y2 = unit->m_y2;
2168 float a0 = unit->m_a0;
2169 float b1 = unit->m_b1;
2170 float b2 = unit->m_b2;
2172 if (freq != unit->m_freq || reson != unit->m_reson) {
2174 float qres = sc_max(0.001, reson);
2175 float pfreq = freq * unit->mRate->mRadiansPerSample;
2177 float D = tan(pfreq * qres * 0.5);
2178 float C = ((1.f-D)/(1.f+D));
2179 float cosf = cos(pfreq);
2181 float next_b1 = (1.f + C) * cosf;
2182 float next_b2 = -C;
2183 float next_a0 = (1.f + C - next_b1) * .25;
2184 //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, y2);
2186 float a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
2187 float b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
2188 float b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
2189 LOOP(unit->mRate->mFilterLoops,
2190 y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
2191 ZXP(out) = y0 + 2.f * y1 + y2;
2193 y2 = a0 * ZXP(in) + b1 * y0 + b2 * y1;
2194 ZXP(out) = y2 + 2.f * y0 + y1;
2196 y1 = a0 * ZXP(in) + b1 * y2 + b2 * y0;
2197 ZXP(out) = y1 + 2.f * y2 + y0;
2199 a0 += a0_slope;
2200 b1 += b1_slope;
2201 b2 += b2_slope;
2203 LOOP(unit->mRate->mFilterRemain,
2204 y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
2205 ZXP(out) = y0 + 2.f * y1 + y2;
2206 y2 = y1;
2207 y1 = y0;
2210 unit->m_freq = freq;
2211 unit->m_reson = reson;
2212 unit->m_a0 = a0;
2213 unit->m_b1 = b1;
2214 unit->m_b2 = b2;
2215 } else {
2216 LOOP(unit->mRate->mFilterLoops,
2217 y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
2218 ZXP(out) = y0 + 2.f * y1 + y2;
2220 y2 = a0 * ZXP(in) + b1 * y0 + b2 * y1;
2221 ZXP(out) = y2 + 2.f * y0 + y1;
2223 y1 = a0 * ZXP(in) + b1 * y2 + b2 * y0;
2224 ZXP(out) = y1 + 2.f * y2 + y0;
2226 LOOP(unit->mRate->mFilterRemain,
2227 y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
2228 ZXP(out) = y0 + 2.f * y1 + y2;
2229 y2 = y1;
2230 y1 = y0;
2233 unit->m_y1 = zapgremlins(y1);
2234 unit->m_y2 = zapgremlins(y2);
2238 void RLPF_next_1(RLPF* unit, int inNumSamples)
2240 //printf("RLPF_next_1\n");
2242 float in = ZIN0(0);
2243 float freq = ZIN0(1);
2244 float reson = ZIN0(2);
2246 float y0;
2247 float y1 = unit->m_y1;
2248 float y2 = unit->m_y2;
2249 float a0 = unit->m_a0;
2250 float b1 = unit->m_b1;
2251 float b2 = unit->m_b2;
2253 if (freq != unit->m_freq || reson != unit->m_reson) {
2254 float qres = sc_max(0.001f, reson);
2255 float pfreq = freq * unit->mRate->mRadiansPerSample;
2257 float D = tan(pfreq * qres * 0.5f);
2258 float C = ((1.f-D)/(1.f+D));
2259 float cosf = cos(pfreq);
2261 b1 = (1.f + C) * cosf;
2262 b2 = -C;
2263 a0 = (1.f + C - b1) * .25f;
2265 y0 = a0 * in + b1 * y1 + b2 * y2;
2266 ZOUT0(0) = y0 + 2.f * y1 + y2;
2267 y2 = y1;
2268 y1 = y0;
2270 unit->m_freq = freq;
2271 unit->m_reson = reson;
2272 unit->m_a0 = a0;
2273 unit->m_b1 = b1;
2274 unit->m_b2 = b2;
2275 } else {
2276 y0 = a0 * in + b1 * y1 + b2 * y2;
2277 ZOUT0(0) = y0 + 2.f * y1 + y2;
2278 y2 = y1;
2279 y1 = y0;
2281 unit->m_y1 = zapgremlins(y1);
2282 unit->m_y2 = zapgremlins(y2);
2287 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2289 void RHPF_Ctor(RHPF* unit)
2291 if (unit->mBufLength == 1)
2292 SETCALC(RHPF_next_1);
2293 else
2294 SETCALC(RHPF_next);
2296 unit->m_a0 = 0.;
2297 unit->m_b1 = 0.;
2298 unit->m_b2 = 0.;
2299 unit->m_y1 = 0.;
2300 unit->m_y2 = 0.;
2301 unit->m_freq = 0.f;
2302 unit->m_reson = 0.f;
2303 RHPF_next_1(unit, 1);
2307 void RHPF_next(RHPF* unit, int inNumSamples)
2309 //printf("RHPFs_next\n");
2311 float *out = ZOUT(0);
2312 float *in = ZIN(0);
2313 float freq = ZIN0(1);
2314 float reson = ZIN0(2);
2316 double y1 = unit->m_y1;
2317 double y2 = unit->m_y2;
2318 double a0 = unit->m_a0;
2319 double b1 = unit->m_b1;
2320 double b2 = unit->m_b2;
2322 if (freq != unit->m_freq || reson != unit->m_reson) {
2323 float qres = sc_max(0.001f, reson);
2324 float pfreq = freq * unit->mRate->mRadiansPerSample;
2326 double D = tan(pfreq * qres * 0.5f);
2327 double C = ((1.-D)/(1.+D));
2328 double cosf = cos(pfreq);
2330 double next_b1 = (1. + C) * cosf;
2331 double next_b2 = -C;
2332 double next_a0 = (1. + C + next_b1) * .25;
2334 //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, y2);
2336 double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
2337 double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
2338 double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
2339 LOOP(unit->mRate->mFilterLoops,
2340 double y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
2341 ZXP(out) = y0 - 2.f * y1 + y2;
2343 y2 = a0 * ZXP(in) + b1 * y0 + b2 * y1;
2344 ZXP(out) = y2 - 2.f * y0 + y1;
2346 y1 = a0 * ZXP(in) + b1 * y2 + b2 * y0;
2347 ZXP(out) = y1 - 2.f * y2 + y0;
2349 a0 += a0_slope;
2350 b1 += b1_slope;
2351 b2 += b2_slope;
2353 LOOP(unit->mRate->mFilterRemain,
2354 double y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
2355 ZXP(out) = y0 - 2.f * y1 + y2;
2356 y2 = y1;
2357 y1 = y0;
2360 unit->m_freq = freq;
2361 unit->m_reson = reson;
2362 unit->m_a0 = a0;
2363 unit->m_b1 = b1;
2364 unit->m_b2 = b2;
2365 } else {
2366 LOOP(unit->mRate->mFilterLoops,
2367 double y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
2368 ZXP(out) = y0 - 2.f * y1 + y2;
2370 y2 = a0 * ZXP(in) + b1 * y0 + b2 * y1;
2371 ZXP(out) = y2 - 2.f * y0 + y1;
2373 y1 = a0 * ZXP(in) + b1 * y2 + b2 * y0;
2374 ZXP(out) = y1 - 2.f * y2 + y0;
2376 LOOP(unit->mRate->mFilterRemain,
2377 double y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
2378 ZXP(out) = y0 - 2.f * y1 + y2;
2379 y2 = y1;
2380 y1 = y0;
2383 unit->m_y1 = zapgremlins(y1);
2384 unit->m_y2 = zapgremlins(y2);
2387 void RHPF_next_1(RHPF* unit, int inNumSamples)
2389 //printf("RHPFs_next_1\n");
2391 float in = ZIN0(0);
2392 float freq = ZIN0(1);
2393 float reson = ZIN0(2);
2395 double y1 = unit->m_y1;
2396 double y2 = unit->m_y2;
2397 double a0 = unit->m_a0;
2398 double b1 = unit->m_b1;
2399 double b2 = unit->m_b2;
2401 if (freq != unit->m_freq || reson != unit->m_reson) {
2402 float qres = sc_max(0.001f, reson);
2403 float pfreq = freq * unit->mRate->mRadiansPerSample;
2405 double D = tan(pfreq * qres * 0.5f);
2406 double C = ((1.-D)/(1.+D));
2407 double cosf = cos(pfreq);
2409 b1 = (1. + C) * cosf;
2410 b2 = -C;
2411 a0 = (1. + C + b1) * .25;
2413 double y0 = a0 * in + b1 * y1 + b2 * y2;
2414 ZOUT0(0) = y0 - 2.f * y1 + y2;
2415 y2 = y1;
2416 y1 = y0;
2418 unit->m_freq = freq;
2419 unit->m_reson = reson;
2420 unit->m_a0 = a0;
2421 unit->m_b1 = b1;
2422 unit->m_b2 = b2;
2423 } else {
2424 double y0 = a0 * in + b1 * y1 + b2 * y2;
2425 ZOUT0(0) = y0 - 2.f * y1 + y2;
2426 y2 = y1;
2427 y1 = y0;
2429 unit->m_y1 = zapgremlins(y1);
2430 unit->m_y2 = zapgremlins(y2);
2434 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2436 void LPF_Ctor(LPF* unit)
2438 if (unit->mBufLength == 1)
2439 SETCALC(LPF_next_1);
2440 else
2441 SETCALC(LPF_next);
2443 unit->m_a0 = 0.f;
2444 unit->m_b1 = 0.f;
2445 unit->m_b2 = 0.f;
2446 unit->m_y1 = 0.f;
2447 unit->m_y2 = 0.f;
2448 unit->m_freq = 0.f;
2449 LPF_next_1(unit, 1);
2453 void LPF_next(LPF* unit, int inNumSamples)
2455 //printf("LPF_next\n");
2457 float *out = ZOUT(0);
2458 float *in = ZIN(0);
2459 float freq = ZIN0(1);
2461 float y0;
2462 float y1 = unit->m_y1;
2463 float y2 = unit->m_y2;
2464 float a0 = unit->m_a0;
2465 float b1 = unit->m_b1;
2466 float b2 = unit->m_b2;
2468 if (freq != unit->m_freq) {
2470 float pfreq = freq * unit->mRate->mRadiansPerSample * 0.5;
2472 float C = 1.f / tan(pfreq);
2473 float C2 = C * C;
2474 float sqrt2C = C * sqrt2_f;
2475 float next_a0 = 1.f / (1.f + sqrt2C + C2);
2476 float next_b1 = -2.f * (1.f - C2) * next_a0 ;
2477 float next_b2 = -(1.f - sqrt2C + C2) * next_a0;
2479 //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, y2);
2481 float a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
2482 float b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
2483 float b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
2484 LOOP(unit->mRate->mFilterLoops,
2485 y0 = ZXP(in) + b1 * y1 + b2 * y2;
2486 ZXP(out) = a0 * (y0 + 2.f * y1 + y2);
2488 y2 = ZXP(in) + b1 * y0 + b2 * y1;
2489 ZXP(out) = a0 * (y2 + 2.f * y0 + y1);
2491 y1 = ZXP(in) + b1 * y2 + b2 * y0;
2492 ZXP(out) = a0 * (y1 + 2.f * y2 + y0);
2494 a0 += a0_slope;
2495 b1 += b1_slope;
2496 b2 += b2_slope;
2498 LOOP(unit->mRate->mFilterRemain,
2499 y0 = ZXP(in) + b1 * y1 + b2 * y2;
2500 ZXP(out) = a0 * (y0 + 2.f * y1 + y2);
2501 y2 = y1;
2502 y1 = y0;
2505 unit->m_freq = freq;
2506 unit->m_a0 = a0;
2507 unit->m_b1 = b1;
2508 unit->m_b2 = b2;
2509 } else {
2510 LOOP(unit->mRate->mFilterLoops,
2511 y0 = ZXP(in) + b1 * y1 + b2 * y2;
2512 ZXP(out) = a0 * (y0 + 2.f * y1 + y2);
2514 y2 = ZXP(in) + b1 * y0 + b2 * y1;
2515 ZXP(out) = a0 * (y2 + 2.f * y0 + y1);
2517 y1 = ZXP(in) + b1 * y2 + b2 * y0;
2518 ZXP(out) = a0 * (y1 + 2.f * y2 + y0);
2520 LOOP(unit->mRate->mFilterRemain,
2521 y0 = ZXP(in) + b1 * y1 + b2 * y2;
2522 ZXP(out) = a0 * (y0 + 2.f * y1 + y2);
2523 y2 = y1;
2524 y1 = y0;
2527 unit->m_y1 = zapgremlins(y1);
2528 unit->m_y2 = zapgremlins(y2);
2531 void LPF_next_1(LPF* unit, int inNumSamples)
2533 //printf("LPF_next\n");
2534 float in = ZIN0(0);
2535 float freq = ZIN0(1);
2537 float y0;
2538 float y1 = unit->m_y1;
2539 float y2 = unit->m_y2;
2540 float a0 = unit->m_a0;
2541 float b1 = unit->m_b1;
2542 float b2 = unit->m_b2;
2544 if (freq != unit->m_freq) {
2546 float pfreq = freq * unit->mRate->mRadiansPerSample * 0.5;
2548 float C = 1.f / tan(pfreq);
2549 float C2 = C * C;
2550 float sqrt2C = C * sqrt2_f;
2551 a0 = 1.f / (1.f + sqrt2C + C2);
2552 b1 = -2.f * (1.f - C2) * a0 ;
2553 b2 = -(1.f - sqrt2C + C2) * a0;
2555 y0 = in + b1 * y1 + b2 * y2;
2556 ZOUT0(0) = a0 * (y0 + 2.f * y1 + y2);
2557 y2 = y1;
2558 y1 = y0;
2560 unit->m_freq = freq;
2561 unit->m_a0 = a0;
2562 unit->m_b1 = b1;
2563 unit->m_b2 = b2;
2564 } else {
2566 y0 = in + b1 * y1 + b2 * y2;
2567 ZOUT0(0) = a0 * (y0 + 2.f * y1 + y2);
2568 y2 = y1;
2569 y1 = y0;
2572 unit->m_y1 = zapgremlins(y1);
2573 unit->m_y2 = zapgremlins(y2);
2577 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2579 void HPF_Ctor(HPF* unit)
2581 if (unit->mBufLength == 1)
2582 SETCALC(HPF_next_1);
2583 else
2584 SETCALC(HPF_next);
2585 unit->m_a0 = 0.;
2586 unit->m_b1 = 0.;
2587 unit->m_b2 = 0.;
2588 unit->m_y1 = 0.;
2589 unit->m_y2 = 0.;
2590 unit->m_freq = -1e6f;
2592 HPF_next_1(unit, 1);
2596 void HPF_next(HPF* unit, int inNumSamples)
2598 float *out = ZOUT(0);
2599 float *in = ZIN(0);
2600 float freq = ZIN0(1);
2602 double y1 = unit->m_y1;
2603 double y2 = unit->m_y2;
2604 double a0 = unit->m_a0;
2605 double b1 = unit->m_b1;
2606 double b2 = unit->m_b2;
2608 if (freq != unit->m_freq) {
2609 float pfreq = freq * unit->mRate->mRadiansPerSample * 0.5;
2611 double C = tan(pfreq);
2612 double C2 = C * C;
2613 double sqrt2C = C * sqrt2_f;
2614 double next_a0 = 1. / (1. + sqrt2C + C2);
2615 double next_b1 = 2. * (1. - C2) * next_a0 ;
2616 double next_b2 = -(1. - sqrt2C + C2) * next_a0;
2618 //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, y2);
2620 double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
2621 double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
2622 double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
2623 LOOP(unit->mRate->mFilterLoops,
2624 double y0 = ZXP(in) + b1 * y1 + b2 * y2;
2625 ZXP(out) = a0 * (y0 - 2. * y1 + y2);
2627 y2 = ZXP(in) + b1 * y0 + b2 * y1;
2628 ZXP(out) = a0 * (y2 - 2. * y0 + y1);
2630 y1 = ZXP(in) + b1 * y2 + b2 * y0;
2631 ZXP(out) = a0 * (y1 - 2. * y2 + y0);
2633 a0 += a0_slope;
2634 b1 += b1_slope;
2635 b2 += b2_slope;
2637 LOOP(unit->mRate->mFilterRemain,
2638 double y0 = ZXP(in) + b1 * y1 + b2 * y2;
2639 ZXP(out) = a0 * (y0 - 2. * y1 + y2);
2640 y2 = y1;
2641 y1 = y0;
2644 unit->m_freq = freq;
2645 unit->m_a0 = a0;
2646 unit->m_b1 = b1;
2647 unit->m_b2 = b2;
2648 } else {
2649 LOOP(unit->mRate->mFilterLoops,
2650 double y0 = ZXP(in) + b1 * y1 + b2 * y2;
2651 ZXP(out) = a0 * (y0 - 2. * y1 + y2);
2653 y2 = ZXP(in) + b1 * y0 + b2 * y1;
2654 ZXP(out) = a0 * (y2 - 2. * y0 + y1);
2656 y1 = ZXP(in) + b1 * y2 + b2 * y0;
2657 ZXP(out) = a0 * (y1 - 2. * y2 + y0);
2659 LOOP(unit->mRate->mFilterRemain,
2660 double y0 = ZXP(in) + b1 * y1 + b2 * y2;
2661 ZXP(out) = a0 * (y0 - 2. * y1 + y2);
2662 y2 = y1;
2663 y1 = y0;
2666 unit->m_y1 = zapgremlins(y1);
2667 unit->m_y2 = zapgremlins(y2);
2670 void HPF_next_1(HPF* unit, int inNumSamples)
2672 double in = ZIN0(0);
2673 double freq = ZIN0(1);
2675 double y1 = unit->m_y1;
2676 double y2 = unit->m_y2;
2677 double a0 = unit->m_a0;
2678 double b1 = unit->m_b1;
2679 double b2 = unit->m_b2;
2681 if (freq != unit->m_freq) {
2682 float pfreq = freq * unit->mRate->mRadiansPerSample * 0.5f;
2684 double C = tan(pfreq);
2685 double C2 = C * C;
2686 double sqrt2C = C * sqrt2_f;
2687 a0 = 1. / (1. + sqrt2C + C2);
2688 b1 = 2. * (1. - C2) * a0 ;
2689 b2 = -(1. - sqrt2C + C2) * a0;
2691 double y0 = in + b1 * y1 + b2 * y2;
2692 ZOUT0(0) = a0 * (y0 - 2. * y1 + y2);
2693 y2 = y1;
2694 y1 = y0;
2696 unit->m_freq = freq;
2697 unit->m_a0 = a0;
2698 unit->m_b1 = b1;
2699 unit->m_b2 = b2;
2700 } else {
2701 double y0 = in + b1 * y1 + b2 * y2;
2702 ZOUT0(0) = a0 * (y0 - 2. * y1 + y2);
2703 y2 = y1;
2704 y1 = y0;
2707 unit->m_y1 = zapgremlins(y1);
2708 unit->m_y2 = zapgremlins(y2);
2711 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2713 void BPF_Ctor(BPF* unit)
2715 //printf("BPF_Reset\n");
2716 if (unit->mBufLength == 1) {
2717 SETCALC(BPF_next_1);
2718 } else {
2719 SETCALC(BPF_next);
2721 unit->m_a0 = 0.f;
2722 unit->m_b1 = 0.f;
2723 unit->m_b2 = 0.f;
2724 unit->m_y1 = 0.f;
2725 unit->m_y2 = 0.f;
2726 unit->m_freq = 0.f;
2727 unit->m_bw = 0.f;
2729 BPF_next_1(unit, 1);
2732 void BPF_next(BPF* unit, int inNumSamples)
2734 //printf("BPF_next\n");
2736 float *out = ZOUT(0);
2737 float *in = ZIN(0);
2738 float freq = ZIN0(1);
2739 float bw = ZIN0(2);
2741 float y0;
2742 float y1 = unit->m_y1;
2743 float y2 = unit->m_y2;
2744 float a0 = unit->m_a0;
2745 float b1 = unit->m_b1;
2746 float b2 = unit->m_b2;
2748 if (freq != unit->m_freq || bw != unit->m_bw) {
2750 float pfreq = freq * unit->mRate->mRadiansPerSample;
2751 float pbw = bw * pfreq * 0.5f;
2753 float C = 1.f / tan(pbw);
2754 float D = 2.f * cos(pfreq);
2756 float next_a0 = 1.f / (1.f + C);
2757 float next_b1 = C * D * next_a0 ;
2758 float next_b2 = (1.f - C) * next_a0;
2760 float a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
2761 float b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
2762 float b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
2763 LOOP(unit->mRate->mFilterLoops,
2764 y0 = ZXP(in) + b1 * y1 + b2 * y2;
2765 ZXP(out) = a0 * (y0 - y2);
2767 y2 = ZXP(in) + b1 * y0 + b2 * y1;
2768 ZXP(out) = a0 * (y2 - y1);
2770 y1 = ZXP(in) + b1 * y2 + b2 * y0;
2771 ZXP(out) = a0 * (y1 - y0);
2773 a0 += a0_slope;
2774 b1 += b1_slope;
2775 b2 += b2_slope;
2777 LOOP(unit->mRate->mFilterRemain,
2778 y0 = ZXP(in) + b1 * y1 + b2 * y2;
2779 ZXP(out) = a0 * (y0 - y2);
2780 y2 = y1;
2781 y1 = y0;
2784 unit->m_freq = freq;
2785 unit->m_bw = bw;
2786 unit->m_a0 = a0;
2787 unit->m_b1 = b1;
2788 unit->m_b2 = b2;
2789 } else {
2790 LOOP(unit->mRate->mFilterLoops,
2791 y0 = ZXP(in) + b1 * y1 + b2 * y2;
2792 ZXP(out) = a0 * (y0 - y2);
2794 y2 = ZXP(in) + b1 * y0 + b2 * y1;
2795 ZXP(out) = a0 * (y2 - y1);
2797 y1 = ZXP(in) + b1 * y2 + b2 * y0;
2798 ZXP(out) = a0 * (y1 - y0);
2800 LOOP(unit->mRate->mFilterRemain,
2801 y0 = ZXP(in) + b1 * y1 + b2 * y2;
2802 ZXP(out) = a0 * (y0 - y2);
2803 y2 = y1;
2804 y1 = y0;
2807 unit->m_y1 = zapgremlins(y1);
2808 unit->m_y2 = zapgremlins(y2);
2811 void BPF_next_1(BPF* unit, int inNumSamples)
2813 //printf("BPF_next_1\n");
2815 float in = ZIN0(0);
2816 float freq = ZIN0(1);
2817 float bw = ZIN0(2);
2819 float y0;
2820 float y1 = unit->m_y1;
2821 float y2 = unit->m_y2;
2822 float a0 = unit->m_a0;
2823 float b1 = unit->m_b1;
2824 float b2 = unit->m_b2;
2826 if (freq != unit->m_freq || bw != unit->m_bw) {
2828 float pfreq = freq * unit->mRate->mRadiansPerSample;
2829 float pbw = bw * pfreq * 0.5;
2831 float C = 1.f / tan(pbw);
2832 float D = 2.f * cos(pfreq);
2834 float a0 = 1.f / (1.f + C);
2835 float b1 = C * D * a0 ;
2836 float b2 = (1.f - C) * a0;
2838 y0 = in + b1 * y1 + b2 * y2;
2839 ZOUT0(0) = a0 * (y0 - y2);
2840 y2 = y1;
2841 y1 = y0;
2843 unit->m_freq = freq;
2844 unit->m_bw = bw;
2845 unit->m_a0 = a0;
2846 unit->m_b1 = b1;
2847 unit->m_b2 = b2;
2848 } else {
2849 y0 = in + b1 * y1 + b2 * y2;
2850 ZOUT0(0) = a0 * (y0 - y2);
2851 y2 = y1;
2852 y1 = y0;
2854 unit->m_y1 = zapgremlins(y1);
2855 unit->m_y2 = zapgremlins(y2);
2859 ////////////////////////////////////////////////////////////////////////////////////////////////////////
2861 void BRF_Ctor(BRF* unit)
2863 //printf("BRF_Reset\n");
2864 if (unit->mBufLength == 1) {
2865 SETCALC(BRF_next_1);
2866 } else {
2867 SETCALC(BRF_next);
2869 unit->m_a0 = 0.f;
2870 unit->m_a1 = 0.f;
2871 unit->m_b2 = 0.f;
2872 unit->m_y1 = 0.f;
2873 unit->m_y2 = 0.f;
2874 unit->m_freq = 0.f;
2875 unit->m_bw = 0.f;
2876 BRF_next_1(unit, 1);
2880 void BRF_next(BRF* unit, int inNumSamples)
2882 //printf("BRF_next\n");
2884 float *out = ZOUT(0);
2885 float *in = ZIN(0);
2886 float freq = ZIN0(1);
2887 float bw = ZIN0(2);
2889 float ay;
2890 float y0;
2891 float y1 = unit->m_y1;
2892 float y2 = unit->m_y2;
2893 float a0 = unit->m_a0;
2894 float a1 = unit->m_a1;
2895 float b2 = unit->m_b2;
2897 if (freq != unit->m_freq || bw != unit->m_bw) {
2898 float pfreq = freq * unit->mRate->mRadiansPerSample;
2899 float pbw = bw * pfreq * 0.5f;
2901 float C = tan(pbw);
2902 float D = 2.f * cos(pfreq);
2904 float next_a0 = 1.f / (1.f + C);
2905 float next_a1 = -D * next_a0;
2906 float next_b2 = (1.f - C) * next_a0;
2908 float a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
2909 float a1_slope = (next_a1 - a1) * unit->mRate->mFilterSlope;
2910 float b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
2912 LOOP(unit->mRate->mFilterLoops,
2913 ay = a1 * y1;
2914 y0 = ZXP(in) - ay - b2 * y2;
2915 ZXP(out) = a0 * (y0 + y2) + ay;
2917 ay = a1 * y0;
2918 y2 = ZXP(in) - ay - b2 * y1;
2919 ZXP(out) = a0 * (y2 + y1) + ay;
2921 ay = a1 * y2;
2922 y1 = ZXP(in) - ay - b2 * y0;
2923 ZXP(out) = a0 * (y1 + y0) + ay;
2925 a0 += a0_slope;
2926 a1 += a1_slope;
2927 b2 += b2_slope;
2929 LOOP(unit->mRate->mFilterRemain,
2930 ay = a1 * y1;
2931 y0 = ZXP(in) - ay - b2 * y2;
2932 ZXP(out) = a0 * (y0 + y2) + ay;
2933 y2 = y1;
2934 y1 = y0;
2937 unit->m_freq = freq;
2938 unit->m_bw = bw;
2939 unit->m_a0 = a0;
2940 unit->m_a1 = a1;
2941 unit->m_b2 = b2;
2942 } else {
2943 LOOP(unit->mRate->mFilterLoops,
2944 ay = a1 * y1;
2945 y0 = ZXP(in) - ay - b2 * y2;
2946 ZXP(out) = a0 * (y0 + y2) + ay;
2948 ay = a1 * y0;
2949 y2 = ZXP(in) - ay - b2 * y1;
2950 ZXP(out) = a0 * (y2 + y1) + ay;
2952 ay = a1 * y2;
2953 y1 = ZXP(in) - ay - b2 * y0;
2954 ZXP(out) = a0 * (y1 + y0) + ay;
2956 LOOP(unit->mRate->mFilterRemain,
2957 ay = a1 * y1;
2958 y0 = ZXP(in) - ay - b2 * y2;
2959 ZXP(out) = a0 * (y0 + y2) + ay;
2960 y2 = y1;
2961 y1 = y0;
2964 unit->m_y1 = zapgremlins(y1);
2965 unit->m_y2 = zapgremlins(y2);
2970 void BRF_next_1(BRF* unit, int inNumSamples)
2972 //printf("BRF_next_1\n");
2974 float in = ZIN0(0);
2975 float freq = ZIN0(1);
2976 float bw = ZIN0(2);
2978 float ay;
2979 float y0;
2980 float y1 = unit->m_y1;
2981 float y2 = unit->m_y2;
2982 float a0 = unit->m_a0;
2983 float a1 = unit->m_a1;
2984 float b2 = unit->m_b2;
2986 if (freq != unit->m_freq || bw != unit->m_bw) {
2987 float pfreq = freq * unit->mRate->mRadiansPerSample;
2988 float pbw = bw * pfreq * 0.5f;
2990 float C = tan(pbw);
2991 float D = 2.f * cos(pfreq);
2993 float a0 = 1.f / (1.f + C);
2994 float a1 = -D * a0;
2995 float b2 = (1.f - C) * a0;
2997 ay = a1 * y1;
2998 y0 = in - ay - b2 * y2;
2999 ZOUT0(0) = a0 * (y0 + y2) + ay;
3000 y2 = y1;
3001 y1 = y0;
3003 unit->m_freq = freq;
3004 unit->m_bw = bw;
3005 unit->m_a0 = a0;
3006 unit->m_a1 = a1;
3007 unit->m_b2 = b2;
3008 } else {
3010 ay = a1 * y1;
3011 y0 = in - ay - b2 * y2;
3012 ZOUT0(0) = a0 * (y0 + y2) + ay;
3013 y2 = y1;
3014 y1 = y0;
3017 unit->m_y1 = zapgremlins(y1);
3018 unit->m_y2 = zapgremlins(y2);
3022 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3024 void MidEQ_Ctor(MidEQ* unit)
3026 //printf("MidEQ_Reset\n");
3027 SETCALC(MidEQ_next);
3028 unit->m_a0 = 0.f;
3029 unit->m_b1 = 0.f;
3030 unit->m_b2 = 0.f;
3031 unit->m_y1 = 0.f;
3032 unit->m_y2 = 0.f;
3033 unit->m_freq = 0.f;
3034 unit->m_bw = 0.f;
3035 unit->m_db = 0.f;
3036 PUSH_LOOPVALS
3037 MidEQ_next(unit, 1);
3038 POP_LOOPVALS
3042 void MidEQ_next(MidEQ* unit, int inNumSamples)
3044 //printf("MidEQ_next\n");
3046 float *out = ZOUT(0);
3047 float *in = ZIN(0);
3048 float freq = ZIN0(1);
3049 float bw = ZIN0(2);
3050 float db = ZIN0(3);
3052 float y0;
3053 float y1 = unit->m_y1;
3054 float y2 = unit->m_y2;
3055 float a0 = unit->m_a0;
3056 float b1 = unit->m_b1;
3057 float b2 = unit->m_b2;
3058 if (freq != unit->m_freq || bw != unit->m_bw || db != unit->m_db) {
3060 float amp = sc_dbamp(db) - 1.0f;
3061 float pfreq = freq * unit->mRate->mRadiansPerSample;
3062 float pbw = bw * pfreq * 0.5f;
3064 float C = 1.f / tan(pbw);
3065 float D = 2.f * cos(pfreq);
3067 float next_a0 = 1.f / (1.f + C);
3068 float next_b1 = C * D * next_a0 ;
3069 float next_b2 = (1.f - C) * next_a0;
3070 next_a0 *= amp;
3071 float a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
3072 float b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
3073 float b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
3074 float zin;
3075 LOOP(unit->mRate->mFilterLoops,
3076 zin = ZXP(in);
3077 y0 = zin + b1 * y1 + b2 * y2;
3078 ZXP(out) = zin + a0 * (y0 - y2);
3080 zin = ZXP(in);
3081 y2 = zin + b1 * y0 + b2 * y1;
3082 ZXP(out) = zin + a0 * (y2 - y1);
3084 zin = ZXP(in);
3085 y1 = zin + b1 * y2 + b2 * y0;
3086 ZXP(out) = zin + a0 * (y1 - y0);
3088 a0 += a0_slope;
3089 b1 += b1_slope;
3090 b2 += b2_slope;
3092 LOOP(unit->mRate->mFilterRemain,
3093 zin = ZXP(in);
3094 y0 = zin + b1 * y1 + b2 * y2;
3095 ZXP(out) = zin + a0 * (y0 - y2);
3096 y2 = y1;
3097 y1 = y0;
3100 unit->m_freq = freq;
3101 unit->m_bw = bw;
3102 unit->m_db = db;
3103 unit->m_a0 = a0;
3104 unit->m_b1 = b1;
3105 unit->m_b2 = b2;
3106 } else {
3107 float zin;
3108 LOOP(unit->mRate->mFilterLoops,
3109 zin = ZXP(in);
3110 y0 = zin + b1 * y1 + b2 * y2;
3111 ZXP(out) = zin + a0 * (y0 - y2);
3113 zin = ZXP(in);
3114 y2 = zin + b1 * y0 + b2 * y1;
3115 ZXP(out) = zin + a0 * (y2 - y1);
3117 zin = ZXP(in);
3118 y1 = zin + b1 * y2 + b2 * y0;
3119 ZXP(out) = zin + a0 * (y1 - y0);
3121 LOOP(unit->mRate->mFilterRemain,
3122 zin = ZXP(in);
3123 y0 = zin + b1 * y1 + b2 * y2;
3124 ZXP(out) = zin + a0 * (y0 - y2);
3125 y2 = y1;
3126 y1 = y0;
3129 unit->m_y1 = zapgremlins(y1);
3130 unit->m_y2 = zapgremlins(y2);
3133 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3135 static void Median_InitMedian(Median* unit, long size, float value);
3136 static float Median_InsertMedian(Median* unit, float value);
3138 void Median_Ctor(Median* unit)
3140 //printf("Median_Reset\n");
3141 SETCALC(Median_next);
3142 float in = ZIN0(1);
3143 unit->m_medianSize = sc_clip((int)ZIN0(0), 0, kMAXMEDIANSIZE);
3144 Median_InitMedian(unit, unit->m_medianSize, in);
3145 ZOUT0(0) = Median_InsertMedian(unit, in);
3149 float Median_InsertMedian(Median* unit, float value)
3151 long pos=-1;
3153 // keeps a sorted list of the previous n=size values
3154 // the oldest is removed and the newest is inserted.
3155 // values between the oldest and the newest are shifted over by one.
3157 // values and ages are both arrays that are 'size' long.
3158 // the median value is always values[size>>1]
3160 long last = unit->m_medianSize - 1;
3161 // find oldest bin and age the other bins.
3162 for (int i=0; i<unit->m_medianSize; ++i) {
3163 if (unit->m_medianAge[i] == last) { // is it the oldest bin ?
3164 pos = i;
3165 } else {
3166 unit->m_medianAge[i]++; // age the bin
3169 // move values to fill in place of the oldest and make a space for the newest
3170 // search lower if value is too small for the open space
3171 while (pos != 0 && value < unit->m_medianValue[pos-1]) {
3172 unit->m_medianValue[pos] = unit->m_medianValue[pos-1];
3173 unit->m_medianAge[pos] = unit->m_medianAge[pos-1];
3174 pos--;
3176 // search higher if value is too big for the open space
3177 while (pos != last && value > unit->m_medianValue[pos+1]) {
3178 unit->m_medianValue[pos] = unit->m_medianValue[pos+1];
3179 unit->m_medianAge[pos] = unit->m_medianAge[pos+1];
3180 pos++;
3182 unit->m_medianValue[pos] = value;
3183 unit->m_medianAge[pos] = 0; // this is the newest bin, age = 0
3184 return unit->m_medianValue[unit->m_medianSize>>1];
3187 void Median_InitMedian(Median* unit, long size, float value)
3189 // initialize the arrays with the first value
3190 unit->m_medianSize = size;
3191 for (int i=0; i<size; ++i) {
3192 unit->m_medianValue[i] = value;
3193 unit->m_medianAge[i] = i;
3197 void Median_next(Median* unit, int inNumSamples)
3199 //printf("Median_next_a\n");
3201 float *out = ZOUT(0);
3202 float *in = ZIN(1);
3204 LOOP1(inNumSamples,
3205 ZXP(out) = Median_InsertMedian(unit, ZXP(in));
3210 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3212 void Resonz_Ctor(Resonz* unit)
3214 //printf("Resonz_Reset\n");
3215 SETCALC(Resonz_next);
3216 unit->m_a0 = 0.f;
3217 unit->m_b1 = 0.f;
3218 unit->m_b2 = 0.f;
3219 unit->m_y1 = 0.f;
3220 unit->m_y2 = 0.f;
3221 unit->m_freq = 0.f;
3222 unit->m_rq = 0.f;
3223 PUSH_LOOPVALS
3224 Resonz_next(unit, 1);
3225 POP_LOOPVALS
3229 void Resonz_next(Resonz* unit, int inNumSamples)
3231 //printf("Resonz_next\n");
3233 float *out = ZOUT(0);
3234 float *in = ZIN(0);
3235 float freq = ZIN0(1);
3236 float rq = ZIN0(2);
3238 float y0;
3239 float y1 = unit->m_y1;
3240 float y2 = unit->m_y2;
3241 float a0 = unit->m_a0;
3242 float b1 = unit->m_b1;
3243 float b2 = unit->m_b2;
3245 if (freq != unit->m_freq || rq != unit->m_rq) {
3246 float ffreq = freq * unit->mRate->mRadiansPerSample;
3247 float B = ffreq * rq;
3248 float R = 1.f - B * 0.5f;
3249 float twoR = 2.f * R;
3250 float R2 = R * R;
3251 float cost = (twoR * cos(ffreq)) / (1.f + R2);
3252 float b1_next = twoR * cost;
3253 float b2_next = -R2;
3254 float a0_next = (1.f - R2) * 0.5f;
3255 float a0_slope = (a0_next - a0) * unit->mRate->mFilterSlope;
3256 float b1_slope = (b1_next - b1) * unit->mRate->mFilterSlope;
3257 float b2_slope = (b2_next - b2) * unit->mRate->mFilterSlope;
3258 LOOP(unit->mRate->mFilterLoops,
3259 y0 = ZXP(in) + b1 * y1 + b2 * y2;
3260 ZXP(out) = a0 * (y0 - y2);
3262 y2 = ZXP(in) + b1 * y0 + b2 * y1;
3263 ZXP(out) = a0 * (y2 - y1);
3265 y1 = ZXP(in) + b1 * y2 + b2 * y0;
3266 ZXP(out) = a0 * (y1 - y0);
3268 a0 += a0_slope;
3269 b1 += b1_slope;
3270 b2 += b2_slope;
3272 LOOP(unit->mRate->mFilterRemain,
3273 y0 = ZXP(in) + b1 * y1 + b2 * y2;
3274 ZXP(out) = a0 * (y0 - y2);
3275 y2 = y1;
3276 y1 = y0;
3279 unit->m_freq = freq;
3280 unit->m_rq = rq;
3281 unit->m_a0 = a0_next;
3282 unit->m_b1 = b1_next;
3283 unit->m_b2 = b2_next;
3284 } else {
3285 LOOP(unit->mRate->mFilterLoops,
3286 y0 = ZXP(in) + b1 * y1 + b2 * y2;
3287 ZXP(out) = a0 * (y0 - y2);
3289 y2 = ZXP(in) + b1 * y0 + b2 * y1;
3290 ZXP(out) = a0 * (y2 - y1);
3292 y1 = ZXP(in) + b1 * y2 + b2 * y0;
3293 ZXP(out) = a0 * (y1 - y0);
3295 LOOP(unit->mRate->mFilterRemain,
3296 y0 = ZXP(in) + b1 * y1 + b2 * y2;
3297 ZXP(out) = a0 * (y0 - y2);
3298 y2 = y1;
3299 y1 = y0;
3302 unit->m_y1 = zapgremlins(y1);
3303 unit->m_y2 = zapgremlins(y2);
3307 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3309 void Ringz_Ctor(Ringz* unit)
3311 //printf("Ringz_ctor\n");
3312 SETCALC(Ringz_next);
3313 unit->m_b1 = 0.f;
3314 unit->m_b2 = 0.f;
3315 unit->m_y1 = 0.f;
3316 unit->m_y2 = 0.f;
3317 unit->m_freq = 0.f;
3318 unit->m_decayTime = 0.f;
3319 PUSH_LOOPVALS
3320 Ringz_next(unit, 1);
3321 POP_LOOPVALS
3325 void Ringz_next(Ringz* unit, int inNumSamples)
3327 //printf("Ringz_next\n");
3329 float *out = ZOUT(0);
3330 float *in = ZIN(0);
3331 float freq = ZIN0(1);
3332 float decayTime = ZIN0(2);
3334 float y0;
3335 float y1 = unit->m_y1;
3336 float y2 = unit->m_y2;
3337 float a0 = 0.5f;
3338 float b1 = unit->m_b1;
3339 float b2 = unit->m_b2;
3341 if (freq != unit->m_freq || decayTime != unit->m_decayTime) {
3342 float ffreq = freq * unit->mRate->mRadiansPerSample;
3343 float R = decayTime == 0.f ? 0.f : exp(log001/(decayTime * SAMPLERATE));
3344 float twoR = 2.f * R;
3345 float R2 = R * R;
3346 float cost = (twoR * cos(ffreq)) / (1.f + R2);
3347 float b1_next = twoR * cost;
3348 float b2_next = -R2;
3349 float b1_slope = (b1_next - b1) * unit->mRate->mFilterSlope;
3350 float b2_slope = (b2_next - b2) * unit->mRate->mFilterSlope;
3351 LOOP(unit->mRate->mFilterLoops,
3352 y0 = ZXP(in) + b1 * y1 + b2 * y2;
3353 ZXP(out) = a0 * (y0 - y2);
3355 y2 = ZXP(in) + b1 * y0 + b2 * y1;
3356 ZXP(out) = a0 * (y2 - y1);
3358 y1 = ZXP(in) + b1 * y2 + b2 * y0;
3359 ZXP(out) = a0 * (y1 - y0);
3361 b1 += b1_slope;
3362 b2 += b2_slope;
3364 LOOP(unit->mRate->mFilterRemain,
3365 y0 = ZXP(in) + b1 * y1 + b2 * y2;
3366 ZXP(out) = a0 * (y0 - y2);
3367 y2 = y1;
3368 y1 = y0;
3371 unit->m_freq = freq;
3372 unit->m_decayTime = decayTime;
3373 unit->m_b1 = b1_next;
3374 unit->m_b2 = b2_next;
3375 } else {
3376 LOOP(unit->mRate->mFilterLoops,
3377 y0 = ZXP(in) + b1 * y1 + b2 * y2;
3378 ZXP(out) = a0 * (y0 - y2);
3380 y2 = ZXP(in) + b1 * y0 + b2 * y1;
3381 ZXP(out) = a0 * (y2 - y1);
3383 y1 = ZXP(in) + b1 * y2 + b2 * y0;
3384 ZXP(out) = a0 * (y1 - y0);
3386 LOOP(unit->mRate->mFilterRemain,
3387 y0 = ZXP(in) + b1 * y1 + b2 * y2;
3388 ZXP(out) = a0 * (y0 - y2);
3389 y2 = y1;
3390 y1 = y0;
3393 unit->m_y1 = zapgremlins(y1);
3394 unit->m_y2 = zapgremlins(y2);
3398 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3400 void Formlet_Ctor(Formlet* unit)
3402 //printf("Formlet_Reset\n");
3403 if (unit->mBufLength == 1) {
3404 SETCALC(Formlet_next_1);
3405 } else {
3406 SETCALC(Formlet_next);
3408 unit->m_b01 = 0.f;
3409 unit->m_b02 = 0.f;
3410 unit->m_y01 = 0.f;
3411 unit->m_y02 = 0.f;
3412 unit->m_b11 = 0.f;
3413 unit->m_b12 = 0.f;
3414 unit->m_y11 = 0.f;
3415 unit->m_y12 = 0.f;
3416 unit->m_freq = 0.f;
3417 unit->m_attackTime = 0.f;
3418 unit->m_decayTime = 0.f;
3419 Formlet_next_1(unit, 1);
3422 void Formlet_next(Formlet* unit, int inNumSamples)
3424 //printf("Formlet_next\n");
3426 float *out = ZOUT(0);
3427 float *in = ZIN(0);
3428 float freq = ZIN0(1);
3429 float attackTime = ZIN0(2);
3430 float decayTime = ZIN0(3);
3432 float y00;
3433 float y10;
3434 float y01 = unit->m_y01;
3435 float y11 = unit->m_y11;
3436 float y02 = unit->m_y02;
3437 float y12 = unit->m_y12;
3439 float b01 = unit->m_b01;
3440 float b11 = unit->m_b11;
3441 float b02 = unit->m_b02;
3442 float b12 = unit->m_b12;
3443 float ain;
3445 if (freq != unit->m_freq || decayTime != unit->m_decayTime || attackTime != unit->m_attackTime) {
3446 float ffreq = freq * unit->mRate->mRadiansPerSample;
3448 float R = decayTime == 0.f ? 0.f : exp(log001/(decayTime * SAMPLERATE));
3449 float twoR = 2.f * R;
3450 float R2 = R * R;
3451 float cost = (twoR * cos(ffreq)) / (1.f + R2);
3452 float b01_next = twoR * cost;
3453 float b02_next = -R2;
3454 float b01_slope = (b01_next - b01) * unit->mRate->mFilterSlope;
3455 float b02_slope = (b02_next - b02) * unit->mRate->mFilterSlope;
3457 R = attackTime == 0.f ? 0.f : exp(log001/(attackTime * SAMPLERATE));
3458 twoR = 2.f * R;
3459 R2 = R * R;
3460 cost = (twoR * cos(ffreq)) / (1.f + R2);
3461 float b11_next = twoR * cost;
3462 float b12_next = -R2;
3463 float b11_slope = (b11_next - b11) * unit->mRate->mFilterSlope;
3464 float b12_slope = (b12_next - b12) * unit->mRate->mFilterSlope;
3466 LOOP(unit->mRate->mFilterLoops,
3467 ain = ZXP(in);
3468 y00 = ain + b01 * y01 + b02 * y02;
3469 y10 = ain + b11 * y11 + b12 * y12;
3470 ZXP(out) = 0.25f * ((y00 - y02) - (y10 - y12));
3472 ain = ZXP(in);
3473 y02 = ain + b01 * y00 + b02 * y01;
3474 y12 = ain + b11 * y10 + b12 * y11;
3475 ZXP(out) = 0.25f * ((y02 - y01) - (y12 - y11));
3477 ain = ZXP(in);
3478 y01 = ain + b01 * y02 + b02 * y00;
3479 y11 = ain + b11 * y12 + b12 * y10;
3480 ZXP(out) = 0.25f * ((y01 - y00) - (y11 - y10));
3482 b01 += b01_slope;
3483 b02 += b02_slope;
3484 b11 += b11_slope;
3485 b12 += b12_slope;
3487 LOOP(unit->mRate->mFilterRemain,
3488 ain = ZXP(in);
3489 y00 = ain + b01 * y01 + b02 * y02;
3490 y10 = ain + b11 * y11 + b12 * y12;
3491 ZXP(out) = 0.25f * ((y00 - y02) - (y10 - y12));
3492 y02 = y01;
3493 y01 = y00;
3494 y12 = y11;
3495 y11 = y10;
3498 unit->m_freq = freq;
3499 unit->m_attackTime = attackTime;
3500 unit->m_decayTime = decayTime;
3501 unit->m_b01 = b01_next;
3502 unit->m_b02 = b02_next;
3503 unit->m_b11 = b11_next;
3504 unit->m_b12 = b12_next;
3505 } else {
3506 LOOP(unit->mRate->mFilterLoops,
3507 ain = ZXP(in);
3508 y00 = ain + b01 * y01 + b02 * y02;
3509 y10 = ain + b11 * y11 + b12 * y12;
3510 ZXP(out) = 0.25f * ((y00 - y02) - (y10 - y12));
3512 ain = ZXP(in);
3513 y02 = ain + b01 * y00 + b02 * y01;
3514 y12 = ain + b11 * y10 + b12 * y11;
3515 ZXP(out) = 0.25f * ((y02 - y01) - (y12 - y11));
3517 ain = ZXP(in);
3518 y01 = ain + b01 * y02 + b02 * y00;
3519 y11 = ain + b11 * y12 + b12 * y10;
3520 ZXP(out) = 0.25f * ((y01 - y00) - (y11 - y10));
3522 LOOP(unit->mRate->mFilterRemain,
3523 ain = ZXP(in);
3524 y00 = ain + b01 * y01 + b02 * y02;
3525 y10 = ain + b11 * y11 + b12 * y12;
3526 ZXP(out) = 0.25f * ((y00 - y02) - (y10 - y12));
3527 y02 = y01;
3528 y01 = y00;
3529 y12 = y11;
3530 y11 = y10;
3533 unit->m_y01 = y01;
3534 unit->m_y02 = y02;
3535 unit->m_y11 = y11;
3536 unit->m_y12 = y12;
3539 void Formlet_next_1(Formlet* unit, int inNumSamples)
3541 //printf("Formlet_next\n");
3543 float in = ZIN0(0);
3544 float freq = ZIN0(1);
3545 float attackTime = ZIN0(2);
3546 float decayTime = ZIN0(3);
3548 float y00;
3549 float y10;
3550 float y01 = unit->m_y01;
3551 float y11 = unit->m_y11;
3552 float y02 = unit->m_y02;
3553 float y12 = unit->m_y12;
3555 float b01 = unit->m_b01;
3556 float b11 = unit->m_b11;
3557 float b02 = unit->m_b02;
3558 float b12 = unit->m_b12;
3559 float ain;
3561 if (freq != unit->m_freq || decayTime != unit->m_decayTime || attackTime != unit->m_attackTime) {
3562 float ffreq = freq * unit->mRate->mRadiansPerSample;
3564 float R = decayTime == 0.f ? 0.f : exp(log001/(decayTime * SAMPLERATE));
3565 float twoR = 2.f * R;
3566 float R2 = R * R;
3567 float cost = (twoR * cos(ffreq)) / (1.f + R2);
3568 b01 = twoR * cost;
3569 b02 = -R2;
3571 R = attackTime == 0.f ? 0.f : exp(log001/(attackTime * SAMPLERATE));
3572 twoR = 2.f * R;
3573 R2 = R * R;
3574 cost = (twoR * cos(ffreq)) / (1.f + R2);
3575 b11 = twoR * cost;
3576 b12 = -R2;
3578 ain = in;
3579 y00 = ain + b01 * y01 + b02 * y02;
3580 y10 = ain + b11 * y11 + b12 * y12;
3581 ZOUT0(0) = 0.25f * ((y00 - y02) - (y10 - y12));
3583 y02 = y01;
3584 y01 = y00;
3585 y12 = y11;
3586 y11 = y10;
3588 unit->m_freq = freq;
3589 unit->m_attackTime = attackTime;
3590 unit->m_decayTime = decayTime;
3591 unit->m_b01 = b01;
3592 unit->m_b02 = b02;
3593 unit->m_b11 = b11;
3594 unit->m_b12 = b12;
3595 } else {
3596 ain = in;
3597 y00 = ain + b01 * y01 + b02 * y02;
3598 y10 = ain + b11 * y11 + b12 * y12;
3599 ZOUT0(0) = 0.25f * ((y00 - y02) - (y10 - y12));
3601 y02 = y01;
3602 y01 = y00;
3603 y12 = y11;
3604 y11 = y10;
3606 unit->m_y01 = y01;
3607 unit->m_y02 = y02;
3608 unit->m_y11 = y11;
3609 unit->m_y12 = y12;
3613 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3615 void FOS_Ctor(FOS* unit)
3617 //printf("FOS_Reset\n");
3618 if (unit->mBufLength == 1) {
3619 SETCALC(FOS_next_1);
3620 } else {
3621 if (INRATE(1) == calc_FullRate
3622 && INRATE(2) == calc_FullRate
3623 && INRATE(3) == calc_FullRate) {
3624 SETCALC(FOS_next_a);
3625 } else {
3626 SETCALC(FOS_next_k);
3629 unit->m_y1 = 0.f;
3630 unit->m_a0 = 0.f;
3631 unit->m_a1 = 0.f;
3632 unit->m_b1 = 0.f;
3633 FOS_next_1(unit, 1);
3636 void FOS_next_a(FOS* unit, int inNumSamples)
3638 float *out = ZOUT(0);
3639 float *in = ZIN(0);
3640 float *a0 = ZIN(1);
3641 float *a1 = ZIN(2);
3642 float *b1 = ZIN(3);
3644 float y1 = unit->m_y1;
3645 LOOP1(inNumSamples,
3646 float y0 = ZXP(in) + ZXP(b1) * y1;
3647 ZXP(out) = ZXP(a0) * y0 + ZXP(a1) * y1;
3648 y1 = y0;
3650 unit->m_y1 = zapgremlins(y1);
3653 void FOS_next_1(FOS* unit, int inNumSamples)
3655 float in = ZIN0(0);
3656 float a0 = ZIN0(1);
3657 float a1 = ZIN0(2);
3658 float b1 = ZIN0(3);
3660 float y1 = unit->m_y1;
3662 float y0 = in + b1 * y1;
3663 ZOUT0(0) = a0 * y0 + a1 * y1;
3664 y1 = y0;
3666 unit->m_y1 = zapgremlins(y1);
3670 void FOS_next_k(FOS* unit, int inNumSamples)
3672 float *out = ZOUT(0);
3673 float *in = ZIN(0);
3674 float next_a0 = ZIN0(1);
3675 float next_a1 = ZIN0(2);
3676 float next_b1 = ZIN0(3);
3678 float y1 = unit->m_y1;
3679 float a0 = unit->m_a0;
3680 float a1 = unit->m_a1;
3681 float b1 = unit->m_b1;
3682 float a0_slope = CALCSLOPE(next_a0, a0);
3683 float a1_slope = CALCSLOPE(next_a1, a1);
3684 float b1_slope = CALCSLOPE(next_b1, b1);
3685 LOOP1(inNumSamples,
3686 float y0 = ZXP(in) + b1 * y1;
3687 ZXP(out) = a0 * y0 + a1 * y1;
3688 y1 = y0;
3690 a0 += a0_slope;
3691 a1 += a1_slope;
3692 b1 += b1_slope;
3694 unit->m_y1 = zapgremlins(y1);
3695 unit->m_a0 = next_a0;
3696 unit->m_a1 = next_a1;
3697 unit->m_b1 = next_b1;
3700 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3702 void SOS_Ctor(SOS* unit)
3704 // printf("SOS_Reset\n");
3705 if (unit->mBufLength != 1) {
3706 if (INRATE(1) == calc_FullRate
3707 && INRATE(2) == calc_FullRate
3708 && INRATE(3) == calc_FullRate
3709 && INRATE(4) == calc_FullRate
3710 && INRATE(5) == calc_FullRate) {
3711 SETCALC(SOS_next_a);
3712 // printf("SOS_next_a\n");
3713 } else {
3714 SETCALC(SOS_next_k);
3715 // printf("SOS_next_k\n");
3717 } else {
3718 SETCALC(SOS_next_1);
3719 // printf("SOS_next_1\n");
3721 unit->m_y1 = 0.f;
3722 unit->m_y2 = 0.f;
3723 unit->m_a0 = 0.f;
3724 unit->m_a1 = 0.f;
3725 unit->m_a2 = 0.f;
3726 unit->m_b1 = 0.f;
3727 unit->m_b2 = 0.f;
3728 SOS_next_1(unit, 1);
3731 void SOS_next_a(SOS *unit, int inNumSamples)
3733 float *out = ZOUT(0);
3734 float *in = ZIN(0);
3735 float *a0 = ZIN(1);
3736 float *a1 = ZIN(2);
3737 float *a2 = ZIN(3);
3738 float *b1 = ZIN(4);
3739 float *b2 = ZIN(5);
3741 float y0;
3742 float y1 = unit->m_y1;
3743 float y2 = unit->m_y2;
3744 LOOP(unit->mRate->mFilterLoops,
3745 y0 = ZXP(in) + ZXP(b1) * y1 + ZXP(b2) * y2;
3746 ZXP(out) = ZXP(a0) * y0 + ZXP(a1) * y1 + ZXP(a2) * y2;
3748 y2 = ZXP(in) + ZXP(b1) * y0 + ZXP(b2) * y1;
3749 ZXP(out) = ZXP(a0) * y2 + ZXP(a1) * y0 + ZXP(a2) * y1;
3751 y1 = ZXP(in) + ZXP(b1) * y2 + ZXP(b2) * y0;
3752 ZXP(out) = ZXP(a0) * y1 + ZXP(a1) * y2 + ZXP(a2) * y0;
3754 LOOP(unit->mRate->mFilterRemain,
3755 y0 = ZXP(in) + ZXP(b1) * y1 + ZXP(b2) * y2;
3756 ZXP(out) = ZXP(a0) * y0 + ZXP(a1) * y1 + ZXP(a2) * y2;
3757 y2 = y1;
3758 y1 = y0;
3761 unit->m_y1 = zapgremlins(y1);
3762 unit->m_y2 = zapgremlins(y2);
3765 void SOS_next_k(SOS *unit, int inNumSamples)
3767 float *out = ZOUT(0);
3768 float *in = ZIN(0);
3769 float next_a0 = ZIN0(1);
3770 float next_a1 = ZIN0(2);
3771 float next_a2 = ZIN0(3);
3772 float next_b1 = ZIN0(4);
3773 float next_b2 = ZIN0(5);
3775 float y0;
3776 float y1 = unit->m_y1;
3777 float y2 = unit->m_y2;
3778 float a0 = unit->m_a0;
3779 float a1 = unit->m_a1;
3780 float a2 = unit->m_a2;
3781 float b1 = unit->m_b1;
3782 float b2 = unit->m_b2;
3783 float a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
3784 float a1_slope = (next_a1 - a1) * unit->mRate->mFilterSlope;
3785 float a2_slope = (next_a2 - a2) * unit->mRate->mFilterSlope;
3786 float b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
3787 float b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
3788 LOOP(unit->mRate->mFilterLoops,
3789 y0 = ZXP(in) + b1 * y1 + b2 * y2;
3790 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
3792 y2 = ZXP(in) + b1 * y0 + b2 * y1;
3793 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
3795 y1 = ZXP(in) + b1 * y2 + b2 * y0;
3796 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
3798 a0 += a0_slope;
3799 a1 += a1_slope;
3800 a2 += a2_slope;
3801 b1 += b1_slope;
3802 b2 += b2_slope;
3804 LOOP(unit->mRate->mFilterRemain,
3805 y0 = ZXP(in) + b1 * y1 + b2 * y2;
3806 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
3807 y2 = y1;
3808 y1 = y0;
3811 unit->m_a0 = a0;
3812 unit->m_a1 = a1;
3813 unit->m_a2 = a2;
3814 unit->m_b1 = b1;
3815 unit->m_b2 = b2;
3816 unit->m_y1 = zapgremlins(y1);
3817 unit->m_y2 = zapgremlins(y2);
3820 void SOS_next_1(SOS *unit, int inNumSamples) // optimized for SOS.kr
3822 float in = ZIN0(0);
3823 float a0 = ZIN0(1);
3824 float a1 = ZIN0(2);
3825 float a2 = ZIN0(3);
3826 float b1 = ZIN0(4);
3827 float b2 = ZIN0(5);
3829 float y0;
3830 float y1 = unit->m_y1;
3831 float y2 = unit->m_y2;
3833 y0 = in + b1 * y1 + b2 * y2;
3834 ZOUT0(0) = a0 * y0 + a1 * y1 + a2 * y2;
3835 y2 = y1;
3836 y1 = y0;
3838 unit->m_y1 = zapgremlins(y1);
3839 unit->m_y2 = zapgremlins(y2);
3842 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3844 void Compander_Ctor(Compander* unit)
3846 //printf("Compander_Reset\n");
3847 SETCALC(Compander_next);
3848 unit->m_clamp = 0.f;
3849 unit->m_clampcoef = 0.f;
3850 unit->m_relax = 0.f;
3851 unit->m_relaxcoef = 0.f;
3852 unit->m_prevmaxval = 0.f;
3853 unit->m_gain = 0.f;
3854 Compander_next(unit, 1);
3857 void Compander_next(Compander* unit, int inNumSamples)
3859 float *out = ZOUT(0);
3860 float *in = ZIN(0);
3861 float *control = ZIN(1);
3862 float thresh = ZIN0(2);
3863 float slope_below = ZIN0(3);
3864 float slope_above = ZIN0(4);
3865 float clamp = ZIN0(5);
3866 float relax = ZIN0(6);
3868 if (clamp != unit->m_clamp) {
3869 unit->m_clampcoef = clamp == 0.0 ? 0.0 : exp(log1/(clamp * SAMPLERATE));
3870 unit->m_clamp = clamp;
3872 if (relax != unit->m_relax) {
3873 unit->m_relaxcoef = relax == 0.0 ? 0.0 : exp(log1/(relax * SAMPLERATE));
3874 unit->m_relax = relax;
3877 float gain = unit->m_gain;
3879 float relaxcoef = unit->m_relaxcoef;
3880 float clampcoef = unit->m_clampcoef;
3882 float prevmaxval = unit->m_prevmaxval;
3884 float val;
3885 LOOP1(inNumSamples,
3886 val = std::abs(ZXP(control));
3887 if (val < prevmaxval) {
3888 val = val + (prevmaxval - val) * relaxcoef;
3889 } else {
3890 val = val + (prevmaxval - val) * clampcoef;
3892 prevmaxval = val;
3895 unit->m_prevmaxval = prevmaxval;
3897 float next_gain;//,absx;
3898 if (prevmaxval < thresh) {
3899 if (slope_below == 1.f) {
3900 next_gain = 1.f;
3901 } else {
3902 next_gain = pow(prevmaxval / thresh, slope_below - 1.f);
3903 //blows up here
3904 float32 absx = std::abs(next_gain);
3905 //zap gremlins, but returns 0. if gain is too small and 1. if gain is too big
3906 next_gain =
3907 (absx < (float32)1e-15) ? (float32)0. :
3908 (absx > (float32)1e15) ? (float32)1. : next_gain;
3910 } else {
3911 if (slope_above == 1.f) {
3912 next_gain = 1.f;
3913 } else {
3914 next_gain = pow(prevmaxval / thresh, slope_above - 1.f);
3918 float gain_slope = CALCSLOPE(next_gain, gain);
3919 LOOP1(inNumSamples, ZXP(out) = ZXP(in) * gain; gain += gain_slope;);
3920 unit->m_gain = gain;
3923 ////////////////////////////////////////////////////////////////////////////////////////////////////////
3926 void Normalizer_Dtor(Normalizer* unit)
3928 RTFree(unit->mWorld, unit->m_table);
3931 void Normalizer_Ctor(Normalizer* unit)
3933 SETCALC(Normalizer_next);
3934 //printf("Normalizer_Reset\n");
3936 float dur = ZIN0(2);
3937 unit->m_bufsize = (long)(ceil(dur * SAMPLERATE));
3938 long allocsize = unit->m_bufsize * 3;
3939 //allocsize = NEXTPOWEROFTWO(allocsize);
3941 unit->m_table = (float*)RTAlloc(unit->mWorld, allocsize * sizeof(float));
3943 unit->m_pos = 0;
3944 unit->m_flips = 0;
3945 unit->m_level = 1.f;
3946 unit->m_slope = 0.f;
3947 unit->m_prevmaxval = 0.0;
3948 unit->m_curmaxval = 0.0;
3949 unit->m_slopefactor = 1.f / unit->m_bufsize;
3951 unit->m_xinbuf = unit->m_table - ZOFF;
3952 unit->m_xmidbuf = unit->m_xinbuf + unit->m_bufsize;
3953 unit->m_xoutbuf = unit->m_xmidbuf + unit->m_bufsize;
3954 Normalizer_next(unit, 1);
3958 void Normalizer_next(Normalizer* unit, int inNumSamples)
3960 float *out = ZOUT(0);
3961 float *in = ZIN(0);
3962 float amp = ZIN0(1);
3964 long pos = unit->m_pos;
3965 float slope = unit->m_slope;
3966 float level = unit->m_level;
3967 float curmaxval = unit->m_curmaxval;
3968 float val;
3970 long bufsize = unit->m_bufsize;
3971 long buf_remain = bufsize - pos;
3973 long remain = inNumSamples;
3974 while (remain > 0) {
3975 long nsmps = sc_min(remain, buf_remain);
3976 float* xinbuf = unit->m_xinbuf + pos;
3977 float* xoutbuf = unit->m_xoutbuf + pos;
3978 if (unit->m_flips >= 2) {
3979 LOOP(nsmps,
3980 ZXP(xinbuf) = val = ZXP(in);
3981 ZXP(out) = level * ZXP(xoutbuf);
3982 level += slope;
3983 val = std::abs(val);
3984 if (val > curmaxval) curmaxval = val;
3986 } else {
3987 LOOP(nsmps,
3988 ZXP(xinbuf) = val = ZXP(in);
3989 ZXP(out) = 0.f;
3990 level += slope;
3991 val = std::abs(val);
3992 if (val > curmaxval) curmaxval = val;
3995 pos += nsmps;
3996 if (pos >= bufsize) {
3997 pos = 0;
3998 buf_remain = bufsize;
4000 float maxval2 = sc_max(unit->m_prevmaxval, curmaxval);
4001 unit->m_prevmaxval = curmaxval;
4002 unit->m_curmaxval = curmaxval = 0.f;
4004 float next_level;
4005 if (maxval2 <= 0.00001f) next_level = 100000.f * amp;
4006 else next_level = amp / maxval2;
4008 slope = unit->m_slope = (next_level - level) * unit->m_slopefactor;
4010 float* temp = unit->m_xoutbuf;
4011 unit->m_xoutbuf = unit->m_xmidbuf;
4012 unit->m_xmidbuf = unit->m_xinbuf;
4013 unit->m_xinbuf = temp;
4015 unit->m_flips++;
4017 remain -= nsmps;
4020 unit->m_pos = pos;
4021 unit->m_level = level;
4022 unit->m_curmaxval = curmaxval;
4025 ////////////////////////////////////////////////////////////////////////////////////////////////////////
4027 void Limiter_Dtor(Limiter* unit)
4029 RTFree(unit->mWorld, unit->m_table);
4032 void Limiter_Ctor(Limiter* unit)
4034 //printf("Limiter_Reset\n");
4035 SETCALC(Limiter_next);
4037 float dur = ZIN0(2);
4038 unit->m_bufsize = (long)(ceil(dur * SAMPLERATE));
4039 long allocsize = unit->m_bufsize * 3;
4040 allocsize = NEXTPOWEROFTWO(allocsize);
4042 unit->m_table = (float*)RTAlloc(unit->mWorld, allocsize * sizeof(float));
4044 unit->m_flips = 0;
4045 unit->m_pos = 0;
4046 unit->m_slope = 0.f;
4047 unit->m_level = 1.f;
4048 unit->m_prevmaxval = 0.0;
4049 unit->m_curmaxval = 0.0;
4050 unit->m_slopefactor = 1.f / unit->m_bufsize;
4052 unit->m_xinbuf = unit->m_table - ZOFF;
4053 unit->m_xmidbuf = unit->m_xinbuf + unit->m_bufsize;
4054 unit->m_xoutbuf = unit->m_xmidbuf + unit->m_bufsize;
4055 Limiter_next(unit, 1);
4059 void Limiter_next(Limiter* unit, int inNumSamples)
4061 float *out = ZOUT(0);
4062 float *in = ZIN(0);
4063 float amp = ZIN0(1);
4065 long pos = unit->m_pos;
4066 float slope = unit->m_slope;
4067 float level = unit->m_level;
4068 float curmaxval = unit->m_curmaxval;
4069 float val;
4071 long bufsize = unit->m_bufsize;
4072 long buf_remain = bufsize - pos;
4074 long remain = inNumSamples;
4075 while (remain > 0) {
4076 long nsmps = sc_min(remain, buf_remain);
4077 float* xinbuf = unit->m_xinbuf + pos;
4078 float* xoutbuf = unit->m_xoutbuf + pos;
4079 if (unit->m_flips >= 2) {
4080 LOOP(nsmps,
4081 ZXP(xinbuf) = val = ZXP(in);
4082 ZXP(out) = level * ZXP(xoutbuf);
4083 level += slope;
4084 val = std::abs(val);
4085 if (val > curmaxval) curmaxval = val;
4087 } else {
4088 LOOP(nsmps,
4089 ZXP(xinbuf) = val = ZXP(in);
4090 ZXP(out) = 0.f;
4091 level += slope;
4092 val = std::abs(val);
4093 if (val > curmaxval) curmaxval = val;
4096 pos += nsmps;
4097 if (pos >= bufsize) {
4098 pos = 0;
4099 buf_remain = bufsize;
4101 float maxval2 = sc_max(unit->m_prevmaxval, curmaxval);
4102 unit->m_prevmaxval = curmaxval;
4103 unit->m_curmaxval = curmaxval = 0.f;
4105 float next_level;
4106 if (maxval2 > amp) next_level = amp / maxval2;
4107 else next_level = 1.0;
4109 slope = unit->m_slope = (next_level - level) * unit->m_slopefactor;
4111 float* temp = unit->m_xoutbuf;
4112 unit->m_xoutbuf = unit->m_xmidbuf;
4113 unit->m_xmidbuf = unit->m_xinbuf;
4114 unit->m_xinbuf = temp;
4116 unit->m_flips++;
4118 remain -= nsmps;
4121 unit->m_pos = pos;
4122 unit->m_level = level;
4123 unit->m_curmaxval = curmaxval;
4126 ////////////////////////////////////////////////////////////////////////////////////////////////////////
4128 void Amplitude_Ctor(Amplitude* unit)
4130 if(INRATE(1) != calc_ScalarRate || INRATE(2) != calc_ScalarRate) {
4131 if(INRATE(0) == calc_FullRate && unit->mCalcRate == calc_BufRate){
4132 SETCALC(Amplitude_next_atok_kk);
4133 } else {
4134 SETCALC(Amplitude_next_kk);
4137 } else {
4138 if(INRATE(0) == calc_FullRate && unit->mCalcRate == calc_BufRate){
4139 SETCALC(Amplitude_next_atok);
4140 } else {
4141 SETCALC(Amplitude_next);
4145 float clamp = ZIN0(1);
4146 unit->m_clampcoef = clamp == 0.0 ? 0.0 : exp(log1/(clamp * SAMPLERATE));
4148 float relax = ZIN0(2);
4149 unit->m_relaxcoef = relax == 0.0 ? 0.0 : exp(log1/(relax * SAMPLERATE));
4151 unit->m_previn = 0;
4152 Amplitude_next(unit, 1);
4155 void Amplitude_next(Amplitude* unit, int inNumSamples)
4157 float *out = ZOUT(0);
4158 float *in = ZIN(0);
4160 float relaxcoef = unit->m_relaxcoef;
4161 float clampcoef = unit->m_clampcoef;
4162 float previn = unit->m_previn;
4164 float val;
4165 LOOP1(inNumSamples,
4166 val = std::abs(ZXP(in));
4167 if (val < previn) {
4168 val = val + (previn - val) * relaxcoef;
4169 } else {
4170 val = val + (previn - val) * clampcoef;
4172 ZXP(out) = previn = val;
4175 unit->m_previn = previn;
4178 void Amplitude_next_atok(Amplitude* unit, int inNumSamples)
4180 float *in = ZIN(0);
4182 float relaxcoef = unit->m_relaxcoef;
4183 float clampcoef = unit->m_clampcoef;
4184 float previn = unit->m_previn;
4186 float val;
4187 LOOP1(FULLBUFLENGTH,
4188 val = std::abs(ZXP(in));
4189 if (val < previn) {
4190 val = val + (previn - val) * relaxcoef;
4191 } else {
4192 val = val + (previn - val) * clampcoef;
4194 previn = val;
4196 ZOUT0(0) = val;
4198 unit->m_previn = previn;
4201 void Amplitude_next_kk(Amplitude* unit, int inNumSamples)
4203 float *out = ZOUT(0);
4204 float *in = ZIN(0);
4205 float relaxcoef, clampcoef;
4207 if(ZIN0(1) != unit->m_clamp_in) {
4208 clampcoef = unit->m_clampcoef = exp(log1/(ZIN0(1) * SAMPLERATE));
4209 unit->m_clamp_in = ZIN0(1);
4210 } else {
4211 clampcoef = unit->m_clampcoef;
4214 if(ZIN0(2) != unit->m_relax_in) {
4215 relaxcoef = unit->m_relaxcoef = exp(log1/(ZIN0(2) * SAMPLERATE));
4216 unit->m_relax_in = ZIN0(2);
4217 } else {
4218 relaxcoef = unit->m_relaxcoef;
4221 float previn = unit->m_previn;
4223 float val;
4224 LOOP1(inNumSamples,
4225 val = std::abs(ZXP(in));
4226 if (val < previn) {
4227 val = val + (previn - val) * relaxcoef;
4228 } else {
4229 val = val + (previn - val) * clampcoef;
4231 ZXP(out) = previn = val;
4234 unit->m_previn = previn;
4237 void Amplitude_next_atok_kk(Amplitude* unit, int inNumSamples)
4239 float *in = ZIN(0);
4240 float relaxcoef, clampcoef;
4242 if(ZIN0(1) != unit->m_clamp_in) {
4243 clampcoef = unit->m_clampcoef = exp(log1/(ZIN0(1) * SAMPLERATE));
4244 unit->m_clamp_in = ZIN0(1);
4245 } else {
4246 clampcoef = unit->m_clampcoef;
4249 if(ZIN0(2) != unit->m_relax_in) {
4250 relaxcoef = unit->m_relaxcoef = exp(log1/(ZIN0(2) * SAMPLERATE));
4251 unit->m_relax_in = ZIN0(2);
4252 } else {
4253 relaxcoef = unit->m_relaxcoef;
4256 float previn = unit->m_previn;
4258 float val;
4259 LOOP1(FULLBUFLENGTH,
4260 val = std::abs(ZXP(in));
4261 if (val < previn) {
4262 val = val + (previn - val) * relaxcoef;
4263 } else {
4264 val = val + (previn - val) * clampcoef;
4266 previn = val;
4268 ZOUT0(0) = val;
4270 unit->m_previn = previn;
4273 ////////////////////////////////////////////////////////////////////////////////////////////////////////
4275 void DetectSilence_Ctor(DetectSilence* unit)
4277 //printf("DetectSilence_Reset\n");
4278 SETCALC(DetectSilence_next);
4280 unit->mThresh = ZIN0(1);
4281 unit->mEndCounter = (int32)(SAMPLERATE * ZIN0(2));
4282 unit->mCounter = -1;
4286 void DetectSilence_next(DetectSilence* unit, int inNumSamples)
4288 float thresh = unit->mThresh;
4289 int counter = unit->mCounter;
4290 float val;
4292 // I thought of a better way to do this...
4294 for (int i=0; i<inNumSamples; ++i) {
4295 float val = std::abs(ZXP(in));
4296 if (val >= thresh) counter = 0;
4297 else if (counter >= 0) {
4298 if (++counter >= unit->mEndCounter && doneAction) {
4299 int doneAction = (int)ZIN0(3);
4300 DoneAction(doneAction, unit);
4301 SETCALC(DetectSilence_done);
4304 ZXP(out) = 0.f;
4307 float *in = IN(0);
4308 float *out = OUT(0);
4309 for (int i=0; i<inNumSamples; ++i) {
4310 val = std::abs(*in++);
4311 if (val > thresh) {
4312 counter = 0;
4313 *out++ = 0.f;
4314 } else if (counter >= 0) {
4315 if (++counter >= unit->mEndCounter) {
4316 int doneAction = (int)ZIN0(3);
4317 DoneAction(doneAction, unit);
4318 *out++ = 1.f;
4319 // SETCALC(DetectSilence_done);
4320 } else {
4321 *out++ = 0.f;
4324 else
4325 *out++ = 0.f;
4327 unit->mCounter = counter;
4330 void DetectSilence_done(DetectSilence* unit, int inNumSamples)
4333 ///////////////////////////////////////////////////////////////////////////////////////////////////////
4334 // Based on HilbertIIR from SC2
4335 // a 12 pole (6 per side) Hilbert IIR filter
4336 // based on Sean Costello and Bernie Hutchins
4337 // created by jl anderson - 7 jan 2001
4338 // UGen created by Josh Parmenter
4340 #define HILBERT_FILTER \
4341 y0_1 = thisin - (coefs[0]) * y1[0]; \
4342 ay1 = coefs[0] * y0_1 + 1 * y1[0]; \
4343 y1[0] = y0_1; \
4344 y0_2 = ay1 - (coefs[1]) * y1[1]; \
4345 ay2 = coefs[1] * y0_2 + 1 * y1[1]; \
4346 y1[1] = y0_2; \
4347 y0_3 = ay2 - (coefs[2]) * y1[2]; \
4348 ay3 = coefs[2] * y0_3 + 1 * y1[2]; \
4349 y1[2] = y0_3; \
4350 y0_4 = ay3 - (coefs[3]) * y1[3]; \
4351 ay4 =coefs[3] * y0_4 + 1 * y1[3]; \
4352 y1[3] = y0_4; \
4353 y0_5 = ay4 - (coefs[4]) * y1[4]; \
4354 ay5 = coefs[4] * y0_5 + 1 * y1[4]; \
4355 y1[4] = y0_5; \
4356 y0_6 = ay5 - (coefs[5]) * y1[5]; \
4357 ay6 = coefs[5] * y0_6 + 1 * y1[5]; \
4358 y1[5] = y0_6; \
4360 y0_7 = thisin - (coefs[6]) * y1[6]; \
4361 ay7 = coefs[6] * y0_7 + 1 * y1[6]; \
4362 y1[6] = y0_7; \
4363 y0_8 = ay7 - (coefs[7]) * y1[7]; \
4364 ay8 = coefs[7] * y0_8 + 1 * y1[7]; \
4365 y1[7] = y0_8; \
4366 y0_9 = ay8 - (coefs[8]) * y1[8]; \
4367 ay9 = coefs[8] * y0_9 + 1 * y1[8]; \
4368 y1[8] = y0_9; \
4369 y0_10 = ay9 - (coefs[9]) * y1[9]; \
4370 ay10 = coefs[9] * y0_10 + 1 * y1[9]; \
4371 y1[9] = y0_10; \
4372 y0_11 = ay10 - (coefs[10]) * y1[10]; \
4373 ay11 = coefs[10] * y0_11 + 1 * y1[10]; \
4374 y1[10] = y0_11; \
4375 y0_12 = ay11 - (coefs[11]) * y1[11]; \
4376 ay12 = coefs[11] * y0_12 + 1 * y1[11]; \
4377 y1[11] = y0_12; \
4380 void Hilbert_Ctor(Hilbert *unit)
4382 // calculate coefs based on SampleRate, store in the struct
4383 SETCALC(Hilbert_next);
4385 float gamconst = (15.0f * pi_f) / SAMPLERATE;
4386 float gamma01 = gamconst * 0.3609f;
4387 float gamma02 = gamconst * 2.7412f;
4388 float gamma03 = gamconst * 11.1573f;
4389 float gamma04 = gamconst * 44.7581f;
4390 float gamma05 = gamconst * 179.6242f;
4391 float gamma06 = gamconst * 798.4578f;
4392 float gamma07 = gamconst * 1.2524f;
4393 float gamma08 = gamconst * 5.5671f;
4394 float gamma09 = gamconst * 22.3423f;
4395 float gamma10 = gamconst * 89.6271f;
4396 float gamma11 = gamconst * 364.7914f;
4397 float gamma12 = gamconst * 2770.1114f;
4398 unit->m_coefs[0] = (gamma01 - 1.f) / (gamma01 + 1.f);
4399 unit->m_coefs[1] = (gamma02 - 1.f) / (gamma02 + 1.f);
4400 unit->m_coefs[2] = (gamma03 - 1.f) / (gamma03 + 1.f);
4401 unit->m_coefs[3] = (gamma04 - 1.f) / (gamma04 + 1.f);
4402 unit->m_coefs[4] = (gamma05 - 1.f) / (gamma05 + 1.f);
4403 unit->m_coefs[5] = (gamma06 - 1.f) / (gamma06 + 1.f);
4404 unit->m_coefs[6] = (gamma07 - 1.f) / (gamma07 + 1.f);
4405 unit->m_coefs[7] = (gamma08 - 1.f) / (gamma08 + 1.f);
4406 unit->m_coefs[8] = (gamma09 - 1.f) / (gamma09 + 1.f);
4407 unit->m_coefs[9] = (gamma10 - 1.f) / (gamma10 + 1.f);
4408 unit->m_coefs[10] = (gamma11 - 1.f) / (gamma11 + 1.f);
4409 unit->m_coefs[11] = (gamma12 - 1.f) / (gamma12 + 1.f);
4410 Clear(12, unit->m_y1);
4411 Hilbert_next(unit, 1);
4414 void Hilbert_next(Hilbert *unit, int inNumSamples)
4416 float *in = ZIN(0);
4417 float *outcos = ZOUT(0);
4418 float *outsin = ZOUT(1);
4419 float y1[12];
4420 float coefs[12];
4422 // each filter's last sample
4423 for(int i = 0; i < 12; ++i){
4424 y1[i] = unit->m_y1[i];
4425 coefs[i] = unit->m_coefs[i];
4428 float ay1, ay2, ay3, ay4, ay5, ay6;
4429 float ay7, ay8, ay9, ay10, ay11, ay12;
4431 float y0_1, y0_2, y0_3, y0_4, y0_5, y0_6;
4432 float y0_7, y0_8, y0_9, y0_10, y0_11, y0_12;
4434 LOOP1(inNumSamples,
4435 float thisin = ZXP(in);
4437 HILBERT_FILTER
4438 ZXP(outcos) = ay6;
4439 ZXP(outsin) = ay12;
4442 for(int i = 0; i < 12; ++i)
4443 unit->m_y1[i] = zapgremlins(y1[i]);
4447 /* Hilbert based SSB FreqShifter */
4449 void FreqShift_Ctor(FreqShift *unit)
4451 // calculate coefs based on SampleRate, store in the struct
4452 // SETCALC(FreqShift_next_kk);
4453 unit->m_phase = 0;
4454 if(INRATE(1) == calc_FullRate) {
4455 if(INRATE(2) == calc_FullRate)
4456 SETCALC(FreqShift_next_aa);
4457 else
4458 SETCALC(FreqShift_next_ak);
4459 } else {
4460 if(INRATE(2) == calc_FullRate)
4461 SETCALC(FreqShift_next_ka);
4462 else {
4463 SETCALC(FreqShift_next_kk);
4464 unit->m_phase = (int32)(unit->m_radtoinc * IN0(2));
4467 // set up for the oscil for the modualtion
4469 int tableSizeSin = ft->mSineSize;
4470 unit->m_lomask = (tableSizeSin - 1) << 3;
4471 unit->m_radtoinc = tableSizeSin * (rtwopi * 65536.);
4472 unit->m_cpstoinc = tableSizeSin * SAMPLEDUR * 65536.;
4473 unit->m_phasein = IN0(2);
4475 float gamconst = (15.0 * pi) / SAMPLERATE;
4476 float gamma01 = gamconst * 0.3609f;
4477 float gamma02 = gamconst * 2.7412f;
4478 float gamma03 = gamconst * 11.1573f;
4479 float gamma04 = gamconst * 44.7581f;
4480 float gamma05 = gamconst * 179.6242f;
4481 float gamma06 = gamconst * 798.4578f;
4482 float gamma07 = gamconst * 1.2524f;
4483 float gamma08 = gamconst * 5.5671f;
4484 float gamma09 = gamconst * 22.3423f;
4485 float gamma10 = gamconst * 89.6271f;
4486 float gamma11 = gamconst * 364.7914f;
4487 float gamma12 = gamconst * 2770.1114f;
4488 unit->m_coefs[0] = (gamma01 - 1.f) / (gamma01 + 1.f);
4489 unit->m_coefs[1] = (gamma02 - 1.f) / (gamma02 + 1.f);
4490 unit->m_coefs[2] = (gamma03 - 1.f) / (gamma03 + 1.f);
4491 unit->m_coefs[3] = (gamma04 - 1.f) / (gamma04 + 1.f);
4492 unit->m_coefs[4] = (gamma05 - 1.f) / (gamma05 + 1.f);
4493 unit->m_coefs[5] = (gamma06 - 1.f) / (gamma06 + 1.f);
4494 unit->m_coefs[6] = (gamma07 - 1.f) / (gamma07 + 1.f);
4495 unit->m_coefs[7] = (gamma08 - 1.f) / (gamma08 + 1.f);
4496 unit->m_coefs[8] = (gamma09 - 1.f) / (gamma09 + 1.f);
4497 unit->m_coefs[9] = (gamma10 - 1.f) / (gamma10 + 1.f);
4498 unit->m_coefs[10] = (gamma11 - 1.f) / (gamma11 + 1.f);
4499 unit->m_coefs[11] = (gamma12 - 1.f) / (gamma12 + 1.f);
4501 Clear(12, unit->m_y1);
4502 FreqShift_next_kk(unit, 1);
4505 void FreqShift_next_kk(FreqShift *unit, int inNumSamples)
4507 float *in = ZIN(0);
4508 float *out = ZOUT(0);
4509 float phasein = ZIN0(2);
4510 float freqin = ZIN0(1);
4511 float outcos, outsin; // the sample by sample output of the Hilbert
4512 float outsinosc, outsinoscHalfPi; // the samples from the oscil.
4513 int32 halfPi = (int32)(unit->m_radtoinc * (0.5 * pi));
4514 float y1[12];
4515 float coefs[12];
4517 float *table0 = ft->mSineWavetable;
4518 float *table1 = table0 + 1;
4519 int32 lomask = unit->m_lomask;
4520 int32 phase = unit->m_phase;
4522 int32 freq = (int32)(unit->m_cpstoinc * freqin);
4523 int32 phaseinc = freq + (int32)(CALCSLOPE(phasein, unit->m_phasein) * unit->m_radtoinc);
4524 unit->m_phasein = phasein;
4526 // each filter's last sample
4527 for(int i = 0; i < 12; ++i) {
4528 y1[i] = unit->m_y1[i];
4529 coefs[i] = unit->m_coefs[i];
4532 float ay1, ay2, ay3, ay4, ay5, ay6;
4533 float ay7, ay8, ay9, ay10, ay11, ay12;
4535 float y0_1, y0_2, y0_3, y0_4, y0_5, y0_6;
4536 float y0_7, y0_8, y0_9, y0_10, y0_11, y0_12;
4538 LOOP1(inNumSamples,
4539 float thisin = ZXP(in);
4541 HILBERT_FILTER
4543 outcos = ay6;
4544 outsin = ay12;
4546 outsinosc = lookupi1(table0, table1, phase, lomask);
4547 outsinoscHalfPi = lookupi1(table0, table1, phase + halfPi, lomask);
4549 ZXP(out) = (outcos * outsinoscHalfPi) + (outsinosc * outsin);
4551 phase += phaseinc;
4554 unit->m_phase = phase;
4556 for(int i = 0; i < 12; ++i)
4557 unit->m_y1[i] = zapgremlins(y1[i]);
4560 void FreqShift_next_aa(FreqShift *unit, int inNumSamples)
4562 float *in = ZIN(0);
4563 float *out = ZOUT(0);
4564 float *phasein = ZIN(2);
4565 float *freqin = ZIN(1);
4566 float outcos, outsin; // the sample by sample output of the Hilbert
4567 float outsinosc, outsinoscHalfPi; // the samples from the oscil.
4568 int32 halfPi = (int32)(unit->m_radtoinc * (0.5 * pi));
4569 float y1[12];
4570 float coefs[12];
4572 float *table0 = ft->mSineWavetable;
4573 float *table1 = table0 + 1;
4574 int32 lomask = unit->m_lomask;
4575 int32 phase = unit->m_phase;
4577 // each filter's last sample
4578 for(int i = 0; i < 12; ++i){
4579 y1[i] = unit->m_y1[i];
4580 coefs[i] = unit->m_coefs[i];
4583 float ay1, ay2, ay3, ay4, ay5, ay6;
4584 float ay7, ay8, ay9, ay10, ay11, ay12;
4586 float y0_1, y0_2, y0_3, y0_4, y0_5, y0_6;
4587 float y0_7, y0_8, y0_9, y0_10, y0_11, y0_12;
4589 LOOP1(inNumSamples,
4590 float thisin = ZXP(in);
4592 HILBERT_FILTER
4594 outcos = ay6;
4595 outsin = ay12;
4597 int32 phaseoffset = phase + (int32)(unit->m_radtoinc * ZXP(phasein));
4598 outsinosc = lookupi1(table0, table1, phaseoffset, lomask);
4599 outsinoscHalfPi = lookupi1(table0, table1, phaseoffset + halfPi, lomask);
4600 phase += (int32)(unit->m_cpstoinc * ZXP(freqin));
4602 ZXP(out) = (outcos * outsinoscHalfPi) + (outsinosc * outsin);
4605 unit->m_phase = phase;
4607 for(int i = 0; i < 12; ++i)
4608 unit->m_y1[i] = zapgremlins(y1[i]);
4611 void FreqShift_next_ak(FreqShift *unit, int inNumSamples)
4613 float *in = ZIN(0);
4614 float *out = ZOUT(0);
4615 float phasein = ZIN0(2);
4616 float *freqin = ZIN(1);
4617 float outcos, outsin; // the sample by sample output of the Hilbert
4618 float outsinosc, outsinoscHalfPi; // the samples from the oscil.
4619 int32 halfPi = (int32)(unit->m_radtoinc * (0.5 * pi));
4620 float y1[12];
4621 float coefs[12];
4623 float *table0 = ft->mSineWavetable;
4624 float *table1 = table0 + 1;
4625 int32 lomask = unit->m_lomask;
4626 int32 phase = unit->m_phase;
4627 float phasemod = unit->m_phasein;
4628 float phaseslope = CALCSLOPE(phasein, phasemod);
4630 // each filter's last sample
4631 for(int i = 0; i < 12; ++i) {
4632 y1[i] = unit->m_y1[i];
4633 coefs[i] = unit->m_coefs[i];
4636 float ay1, ay2, ay3, ay4, ay5, ay6;
4637 float ay7, ay8, ay9, ay10, ay11, ay12;
4639 float y0_1, y0_2, y0_3, y0_4, y0_5, y0_6;
4640 float y0_7, y0_8, y0_9, y0_10, y0_11, y0_12;
4642 LOOP1(inNumSamples,
4643 float thisin = ZXP(in);
4645 HILBERT_FILTER
4647 outcos = ay6;
4648 outsin = ay12;
4650 int32 pphase = phase + (int32)(unit->m_radtoinc * phasemod);
4651 phasemod += phaseslope;
4652 outsinosc = lookupi1(table0, table1, pphase, lomask);
4653 outsinoscHalfPi = lookupi1(table0, table1, pphase + halfPi, lomask);
4654 phase += (int32)(unit->m_cpstoinc * ZXP(freqin));
4656 ZXP(out) = (outcos * outsinoscHalfPi) + (outsinosc * outsin);
4659 unit->m_phase = phase;
4660 unit->m_phasein = phasein;
4662 for(int i = 0; i < 12; ++i)
4663 unit->m_y1[i] = zapgremlins(y1[i]);
4666 void FreqShift_next_ka(FreqShift *unit, int inNumSamples)
4668 float *in = ZIN(0);
4669 float *out = ZOUT(0);
4670 float *phasein = ZIN(2);
4671 float freqin = ZIN0(1);
4672 float outcos, outsin; // the sample by sample output of the Hilbert
4673 float outsinosc, outsinoscHalfPi; // the samples from the oscil.
4674 int32 halfPi = (int32)(unit->m_radtoinc * (0.5 * pi));
4675 float y1[12];
4676 float coefs[12];
4678 float *table0 = ft->mSineWavetable;
4679 float *table1 = table0 + 1;
4680 int32 lomask = unit->m_lomask;
4681 int32 phase = unit->m_phase;
4682 float phasemod = unit->m_phasein;
4684 int32 freq = (int32)(unit->m_cpstoinc * freqin);
4686 // each filter's last sample
4687 for(int i = 0; i < 12; ++i) {
4688 y1[i] = unit->m_y1[i];
4689 coefs[i] = unit->m_coefs[i];
4692 float ay1, ay2, ay3, ay4, ay5, ay6;
4693 float ay7, ay8, ay9, ay10, ay11, ay12;
4695 float y0_1, y0_2, y0_3, y0_4, y0_5, y0_6;
4696 float y0_7, y0_8, y0_9, y0_10, y0_11, y0_12;
4698 LOOP1(inNumSamples,
4699 float thisin = ZXP(in);
4701 HILBERT_FILTER
4703 outcos = ay6;
4704 outsin = ay12;
4706 int32 phaseoffset = phase + (int32)(unit->m_radtoinc * ZXP(phasein));
4707 outsinosc = lookupi1(table0, table1, phaseoffset, lomask);
4708 outsinoscHalfPi = lookupi1(table0, table1, phaseoffset + halfPi, lomask);
4709 phase += freq;
4711 ZXP(out) = (outcos * outsinoscHalfPi) + (outsinosc * outsin);
4714 unit->m_phase = phase;
4716 for(int i = 0; i < 12; ++i)
4717 unit->m_y1[i] = zapgremlins(y1[i]);
4722 "MoogFF" - Moog VCF digital implementation.
4723 As described in the paper entitled
4724 "Preserving the Digital Structure of the Moog VCF"
4725 by Federico Fontana
4726 appeared in the Proc. ICMC07, Copenhagen, 25-31 August 2007
4728 Original Java code Copyright F. Fontana - August 2007
4729 federico.fontana@univr.it
4731 Ported to C++ for SuperCollider by Dan Stowell - August 2007
4732 http://www.mcld.co.uk/
4734 This program is free software; you can redistribute it and/or modify
4735 it under the terms of the GNU General Public License as published by
4736 the Free Software Foundation; either version 2 of the License, or
4737 (at your option) any later version.
4739 This program is distributed in the hope that it will be useful,
4740 but WITHOUT ANY WARRANTY; without even the implied warranty of
4741 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4742 GNU General Public License for more details.
4744 You should have received a copy of the GNU General Public License
4745 along with this program; if not, write to the Free Software
4746 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4748 //////////////////////////////////////////////////////////////////
4750 void MoogFF_Ctor(MoogFF* unit)
4752 SETCALC(MoogFF_next);
4754 // initialize the unit generator state variables.
4755 unit->m_freq = -10000.3f; // Force the freq to update on first run
4756 unit->m_T = 1.0 / SAMPLERATE;
4757 unit->m_s1 = 0.f;
4758 unit->m_s2 = 0.f;
4759 unit->m_s3 = 0.f;
4760 unit->m_s4 = 0.f;
4762 // calculate one sample of output.
4763 MoogFF_next(unit, 1);
4766 void MoogFF_next(MoogFF *unit, int inNumSamples)
4768 float *out = ZOUT(0);
4770 float *in = ZIN(0);
4771 float k = IN0(2);
4772 k = k > 4.f? 4.f : (k<0.f ? 0.f : k);
4774 // Load state from the struct
4775 float s1 = unit->m_s1;
4776 float s2 = unit->m_s2;
4777 float s3 = unit->m_s3;
4778 float s4 = unit->m_s4;
4779 float freq = unit->m_freq;///
4780 double T = unit->m_T;
4782 // Reset filter state if requested
4783 if(IN0(3)>0)
4784 s1 = s2 = s3 = s4 = 0.f;
4786 double wcD=unit->m_wcD;
4787 float a1 = unit->m_a1, b0 = unit->m_b0; // Filter coefficient parameters
4788 float o, u; // System's null response, loop input
4790 // Update filter coefficients, but only if freq changes since it involves some expensive operations
4791 if(freq != IN0(1)) {
4792 freq = IN0(1);
4793 //Print("Updated freq to %g\n", freq);
4794 wcD = 2.0 * tan(T * PI * freq) * SAMPLERATE;
4795 if(wcD<0)
4796 wcD = 0; // Protect against negative cutoff freq
4797 double TwcD = T*wcD;
4798 b0 = (float)(TwcD/(TwcD + 2.));
4799 a1 = (float)((TwcD - 2.)/(TwcD + 2.));
4800 unit->m_freq = freq;
4801 unit->m_b0 = b0;
4802 unit->m_a1 = a1;
4803 unit->m_wcD = wcD;
4806 LOOP1(inNumSamples,
4807 // compute loop values
4808 o = s4 + b0*(s3 + b0*(s2 + b0*s1));
4809 float ins = ZXP(in);
4810 float outs = (b0*b0*b0*b0*ins + o)/(1.f + b0*b0*b0*b0*k);
4811 ZXP(out) = outs;
4812 u = ins - k*outs;
4814 // update 1st order filter states
4815 float past = u;
4816 float future = b0*past + s1;
4817 s1 = b0*past - a1*future;
4819 past = future;
4820 future = b0*past + s2;
4821 s2 = b0*past - a1*future;
4823 past = future;
4824 future = b0*past + s3;
4825 s3 = b0*past - a1*future;
4827 s4 = b0*future - a1*outs;
4830 // Store state
4831 unit->m_s1 = s1;
4832 unit->m_s2 = s2;
4833 unit->m_s3 = s3;
4834 unit->m_s4 = s4;
4837 /* BEQSuite */
4840 struct BLowPass : public Unit
4842 double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
4843 float m_freq, m_rq;
4846 extern "C"
4848 void BLowPass_next_kk(BLowPass *unit, int inNumSamples);
4849 void BLowPass_next_aa(BLowPass *unit, int inNumSamples);
4850 void BLowPass_Ctor(BLowPass* unit);
4854 void BLowPass_Ctor(BLowPass* unit)
4856 if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate))
4857 SETCALC(BLowPass_next_aa);
4858 else
4859 SETCALC(BLowPass_next_kk);
4860 float rq = unit->m_rq = ZIN0(2);
4861 float freq = unit->m_freq = ZIN0(1);
4862 double w0 = twopi * (double)freq * SAMPLEDUR;
4863 double cosw0 = cos(w0);
4864 double i = 1. - cosw0;
4865 double alpha = sin(w0) * 0.5 * (double)rq;
4866 double b0rz = 1. / (1. + alpha);
4867 double a0 = unit->m_a0 = i * 0.5 * b0rz;
4868 unit->m_a1 = i * b0rz;
4869 unit->m_a2 = a0;
4870 unit->m_b1 = cosw0 * 2. * b0rz;
4871 unit->m_b2 = (1. - alpha) * -b0rz;
4872 unit->m_y1 = 0.;
4874 unit->m_y2 = 0.;
4875 PUSH_LOOPVALS
4876 BLowPass_next_kk(unit, 1);
4877 POP_LOOPVALS
4880 void BLowPass_next_aa(BLowPass *unit, int inNumSamples)
4882 float *out = ZOUT(0);
4883 float *in = ZIN(0);
4884 float *freq = ZIN(1);
4885 float *rq = ZIN(2);
4887 double a0, a1, a2, b1, b2, w0, cosw0, i, alpha, b0rz;
4888 double y0, y1, y2;
4889 float nextfreq, nextrq;
4891 y1 = unit->m_y1;
4892 y2 = unit->m_y2;
4894 a0 = unit->m_a0;
4895 a1 = unit->m_a1;
4896 a2 = unit->m_a2;
4897 b1 = unit->m_b1;
4898 b2 = unit->m_b2;
4900 LOOP(unit->mRate->mFilterLoops,
4901 nextfreq = ZXP(freq);
4902 nextrq = ZXP(rq);
4903 if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq)) {
4904 w0 = twopi * (double)nextfreq * SAMPLEDUR;
4905 cosw0 = cos(w0);
4906 i = 1. - cosw0;
4907 alpha = sin(w0) * 0.5 * (double)nextrq;
4908 b0rz = 1. / (1. + alpha);
4909 a0 = i * 0.5 * b0rz;
4910 a1 = i * b0rz;
4911 a2 = a0;
4912 b1 = cosw0 * 2. * b0rz;
4913 b2 = (1. - alpha) * -b0rz;
4914 unit->m_freq = nextfreq;
4915 unit->m_rq = nextrq;
4917 y0 = ZXP(in) + b1 * y1 + b2 * y2;
4918 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
4920 y2 = ZXP(in) + b1 * y0 + b2 * y1;
4921 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
4923 y1 = ZXP(in) + b1 * y2 + b2 * y0;
4924 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
4927 LOOP(unit->mRate->mFilterRemain,
4928 y0 = ZXP(in) + b1 * y1 + b2 * y2;
4929 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
4930 y2 = y1;
4931 y1 = y0;
4932 unit->m_freq = ZXP(freq);
4933 unit->m_rq = ZXP(rq);
4936 unit->m_a0 = a0;
4937 unit->m_a1 = a1;
4938 unit->m_a2 = a2;
4939 unit->m_b1 = b1;
4940 unit->m_b2 = b2;
4941 unit->m_y1 = zapgremlins(y1);
4942 unit->m_y2 = zapgremlins(y2);
4946 void BLowPass_next_kk(BLowPass *unit, int inNumSamples)
4948 float *out = ZOUT(0);
4949 float *in = ZIN(0);
4950 float nextfreq = ZIN0(1);
4951 float nextrq = ZIN0(2);
4953 double a0, a1, a2, b1, b2;
4954 double nexta0, nexta1, nexta2, nextb1, nextb2, nextw0, nextcosw0, nexti, nextalpha, nextb0rz;
4955 double y0;
4956 double y1 = unit->m_y1;
4957 double y2 = unit->m_y2;
4958 double a0slope, a1slope, a2slope, b1slope, b2slope;
4960 a0 = unit->m_a0;
4961 a1 = unit->m_a1;
4962 a2 = unit->m_a2;
4963 b1 = unit->m_b1;
4964 b2 = unit->m_b2;
4966 if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq)){
4967 nextw0 = twopi * (double)nextfreq * SAMPLEDUR;
4968 nextcosw0 = cos(nextw0);
4969 nexti = 1. - nextcosw0;
4970 nextalpha = sin(nextw0) * 0.5 * (double)nextrq;
4971 nextb0rz = 1. / (1. + nextalpha);
4972 nexta0 = nexti * 0.5 * nextb0rz;
4973 nexta1 = nexti * nextb0rz;
4974 nexta2 = nexta0;
4975 nextb1 = nextcosw0 * 2. * nextb0rz;
4976 nextb2 = ((1. - nextalpha) * -nextb0rz);
4977 a0slope = (nexta0 - a0) * unit->mRate->mFilterSlope;
4978 a1slope = (nexta1 - a1) * unit->mRate->mFilterSlope;
4979 a2slope = (nexta2 - a2) * unit->mRate->mFilterSlope;
4980 b1slope = (nextb1 - b1) * unit->mRate->mFilterSlope;
4981 b2slope = (nextb2 - b2) * unit->mRate->mFilterSlope;
4982 unit->m_freq = nextfreq;
4983 unit->m_rq = nextrq;
4984 LOOP(unit->mRate->mFilterLoops,
4985 y0 = ZXP(in) + b1 * y1 + b2 * y2;
4986 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
4988 y2 = ZXP(in) + b1 * y0 + b2 * y1;
4989 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
4991 y1 = ZXP(in) + b1 * y2 + b2 * y0;
4992 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
4993 a0 += a0slope;
4994 a1 += a1slope;
4995 a2 += a2slope;
4996 b1 += b1slope;
4997 b2 += b2slope;
4999 LOOP(unit->mRate->mFilterRemain,
5000 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5001 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5002 y2 = y1;
5003 y1 = y0;
5006 } else {
5007 LOOP(unit->mRate->mFilterLoops,
5008 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5009 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5011 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5012 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5014 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5015 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5017 LOOP(unit->mRate->mFilterRemain,
5018 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5019 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5020 y2 = y1;
5021 y1 = y0;
5024 unit->m_y1 = zapgremlins(y1);
5025 unit->m_y2 = zapgremlins(y2);
5026 unit->m_a0 = a0;
5027 unit->m_a1 = a1;
5028 unit->m_a2 = a2;
5029 unit->m_b1 = b1;
5030 unit->m_b2 = b2;
5033 struct BHiPass : public Unit
5035 double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
5036 float m_freq, m_rq;
5040 extern "C"
5042 void BHiPass_next_kk(BHiPass *unit, int inNumSamples);
5043 void BHiPass_next_aa(BHiPass *unit, int inNumSamples);
5044 void BHiPass_Ctor(BHiPass* unit);
5047 void BHiPass_Ctor(BHiPass* unit)
5049 if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate))
5050 SETCALC(BHiPass_next_aa);
5051 else
5052 SETCALC(BHiPass_next_kk);
5053 float freq = unit->m_freq = ZIN0(1);
5054 float rq = unit->m_rq = ZIN0(2);
5055 double w0 = twopi * (double)freq * SAMPLEDUR;
5056 double cosw0 = cos(w0);
5057 double i = 1. + cosw0;
5058 double alpha = sin(w0) * 0.5 * (double)rq;
5059 double b0rz = 1. / (1. + alpha);
5060 double a0 = unit->m_a0 = i * 0.5 * b0rz;
5061 unit->m_a1 = -i * b0rz;
5062 unit->m_a2 = a0;
5063 unit->m_b1 = cosw0 * 2. * b0rz;
5064 unit->m_b2 = (1. - alpha) * -b0rz;
5065 unit->m_y1 = 0.;
5067 unit->m_y2 = 0.;
5068 PUSH_LOOPVALS
5069 BHiPass_next_kk(unit, 1);
5070 POP_LOOPVALS
5073 void BHiPass_next_aa(BHiPass *unit, int inNumSamples)
5075 float *out = ZOUT(0);
5076 float *in = ZIN(0);
5077 float *freq = ZIN(1);
5078 float *rq = ZIN(2);
5080 double a0, a1, a2, b1, b2, w0, cosw0, i, alpha, b0rz;
5081 double y0, y1, y2;
5082 float nextfreq, nextrq;
5084 y1 = unit->m_y1;
5085 y2 = unit->m_y2;
5087 a0 = unit->m_a0;
5088 a1 = unit->m_a1;
5089 a2 = unit->m_a2;
5090 b1 = unit->m_b1;
5091 b2 = unit->m_b2;
5093 LOOP(unit->mRate->mFilterLoops,
5094 nextfreq = ZXP(freq);
5095 nextrq = ZXP(rq);
5096 if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq)) {
5097 w0 = twopi * (double)nextfreq * SAMPLEDUR;
5098 cosw0 = cos(w0);
5099 i = 1. + cosw0;
5100 alpha = sin(w0) * 0.5 * (double)nextrq;
5101 b0rz = 1. / (1. + alpha);
5102 a0 = i * 0.5 * b0rz;
5103 a1 = -i * b0rz;
5104 a2 = a0;
5105 b1 = cosw0 * 2. * b0rz;
5106 b2 = (1. - alpha) * -b0rz;
5107 unit->m_freq = nextfreq;
5108 unit->m_rq = nextrq;
5110 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5111 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5113 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5114 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5116 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5117 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5119 LOOP(unit->mRate->mFilterRemain,
5120 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5121 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5122 y2 = y1;
5123 y1 = y0;
5126 unit->m_a0 = a0;
5127 unit->m_a1 = a1;
5128 unit->m_a2 = a2;
5129 unit->m_b1 = b1;
5130 unit->m_b2 = b2;
5131 unit->m_y1 = zapgremlins(y1);
5132 unit->m_y2 = zapgremlins(y2);
5136 void BHiPass_next_kk(BHiPass *unit, int inNumSamples)
5138 float *out = ZOUT(0);
5139 float *in = ZIN(0);
5140 float nextfreq = ZIN0(1);
5141 float nextrq = ZIN0(2);
5143 double a0, a1, a2, b1, b2;
5144 double nexta0, nexta1, nexta2, nextb1, nextb2, nextw0, nextcosw0, nexti, nextalpha, nextb0rz;
5145 double y0;
5146 double y1 = unit->m_y1;
5147 double y2 = unit->m_y2;
5148 double a0slope, a1slope, a2slope, b1slope, b2slope;
5150 a0 = unit->m_a0;
5151 a1 = unit->m_a1;
5152 a2 = unit->m_a2;
5153 b1 = unit->m_b1;
5154 b2 = unit->m_b2;
5156 if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq)){
5157 nextw0 = twopi * (double)nextfreq * SAMPLEDUR;
5158 nextcosw0 = cos(nextw0);
5159 nexti = 1. + nextcosw0;
5160 nextalpha = sin(nextw0) * 0.5 * (double)nextrq;
5161 nextb0rz = 1. / (1. + nextalpha);
5162 nexta0 = nexti * 0.5 * nextb0rz;
5163 nexta1 = -nexti * nextb0rz;
5164 nexta2 = nexta0;
5165 nextb1 = nextcosw0 * 2. * nextb0rz;
5166 nextb2 = (1. - nextalpha) * -nextb0rz;
5167 a0slope = (nexta0 - a0) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta0, a0);
5168 a1slope = (nexta1 - a1) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta1, a1);
5169 a2slope = (nexta2 - a2) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta2, a2);
5170 b1slope = (nextb1 - b1) * unit->mRate->mFilterSlope; //CALCSLOPE(nextb1, b1);
5171 b2slope = (nextb2 - b2) * unit->mRate->mFilterSlope; //CALCSLOPE(nextb2, b2);
5173 LOOP(unit->mRate->mFilterLoops,
5174 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5175 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5177 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5178 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5180 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5181 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5182 a0 += a0slope;
5183 a1 += a1slope;
5184 a2 += a2slope;
5185 b1 += b1slope;
5186 b2 += b2slope;
5188 LOOP(unit->mRate->mFilterRemain,
5189 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5190 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5191 y2 = y1;
5192 y1 = y0;
5194 unit->m_freq = nextfreq;
5195 unit->m_rq = nextrq;
5196 unit->m_a0 = a0;
5197 unit->m_a1 = a1;
5198 unit->m_a2 = a2;
5199 unit->m_b1 = b1;
5200 unit->m_b2 = b2;
5202 } else {
5204 LOOP(unit->mRate->mFilterLoops,
5205 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5206 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5208 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5209 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5211 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5212 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5214 LOOP(unit->mRate->mFilterRemain,
5215 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5216 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5217 y2 = y1;
5218 y1 = y0;
5222 unit->m_y1 = zapgremlins(y1);
5223 unit->m_y2 = zapgremlins(y2);
5226 /* BBandPass */
5228 struct BBandPass : public Unit
5230 double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
5231 float m_freq, m_bw;
5234 extern "C"
5236 void BBandPass_next_kk(BBandPass *unit, int inNumSamples);
5237 void BBandPass_next_aa(BBandPass *unit, int inNumSamples);
5238 void BBandPass_Ctor(BBandPass* unit);
5241 void BBandPass_Ctor(BBandPass* unit)
5243 if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate))
5244 SETCALC(BBandPass_next_aa);
5245 else
5246 SETCALC(BBandPass_next_kk);
5247 float freq = unit->m_freq = ZIN0(1);
5248 float bw = unit->m_bw = ZIN0(2);
5250 double w0 = twopi * (double)freq * SAMPLEDUR;
5251 double sinw0 = sin(w0);
5252 double alpha = sinw0 * (sinh((0.34657359027997 * (double)bw * w0) / sinw0));
5253 double b0rz = 1. / (1. + alpha);
5254 double a0 = unit->m_a0 = alpha * b0rz;
5255 unit->m_a1 = 0.0f;
5256 unit->m_a2 = -a0;
5257 unit->m_b1 = cos(w0) * 2. * b0rz;
5258 unit->m_b2 = (1. - alpha) * -b0rz;
5260 unit->m_y1 = 0.;
5262 unit->m_y2 = 0.;
5263 PUSH_LOOPVALS
5264 BBandPass_next_kk(unit, 1);
5265 POP_LOOPVALS
5268 void BBandPass_next_aa(BBandPass *unit, int inNumSamples)
5270 float *out = ZOUT(0);
5271 float *in = ZIN(0);
5272 float *freq = ZIN(1);
5273 float *bw = ZIN(2);
5275 double a0, a1, a2, b1, b2, w0, sinw0, alpha, b0rz;
5276 double y0, y1, y2;
5277 float nextfreq, nextbw;
5279 y1 = unit->m_y1;
5280 y2 = unit->m_y2;
5282 a0 = unit->m_a0;
5283 a1 = unit->m_a1;
5284 a2 = unit->m_a2;
5285 b1 = unit->m_b1;
5286 b2 = unit->m_b2;
5288 LOOP(unit->mRate->mFilterLoops,
5289 nextfreq = ZXP(freq);
5290 nextbw = ZXP(bw);
5291 if ((unit->m_freq != nextfreq) || (unit->m_bw != nextbw)) {
5292 w0 = twopi * (double)nextfreq * SAMPLEDUR;
5293 sinw0 = sin(w0);
5294 alpha = sinw0 * (sinh((0.34657359027997 * (double)nextbw * w0) / sinw0));
5295 b0rz = 1. / (1. + alpha);
5296 a0 = alpha * b0rz;
5297 a1 = 0.0f;
5298 a2 = -a0;
5299 b1 = cos(w0) * 2. * b0rz;
5300 b2 = (1. - alpha) * -b0rz;
5301 unit->m_freq = ZXP(freq);
5302 unit->m_bw = ZXP(bw);
5304 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5305 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5307 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5308 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5310 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5311 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5313 LOOP(unit->mRate->mFilterRemain,
5314 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5315 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5316 y2 = y1;
5317 y1 = y0;
5318 unit->m_freq = nextfreq;
5319 unit->m_bw = nextbw;
5322 unit->m_a0 = a0;
5323 unit->m_a1 = a1;
5324 unit->m_a2 = a2;
5325 unit->m_b1 = b1;
5326 unit->m_b2 = b2;
5327 unit->m_y1 = zapgremlins(y1);
5328 unit->m_y2 = zapgremlins(y2);
5332 void BBandPass_next_kk(BBandPass *unit, int inNumSamples)
5334 float *out = ZOUT(0);
5335 float *in = ZIN(0);
5336 float nextfreq = ZIN0(1);
5337 float nextbw = ZIN0(2);
5339 double a0, a1, a2, b1, b2;
5340 double nexta0, nexta1, nexta2, nextb1, nextb2, nextw0, nextalpha, nextb0rz;
5341 double y0;
5342 double y1 = unit->m_y1;
5343 double y2 = unit->m_y2;
5344 double a0slope, a1slope, a2slope, b1slope, b2slope;
5346 a0 = unit->m_a0;
5347 a1 = unit->m_a1;
5348 a2 = unit->m_a2;
5349 b1 = unit->m_b1;
5350 b2 = unit->m_b2;
5351 if ((unit->m_freq != nextfreq) || (unit->m_bw != nextbw)){
5352 nextw0 = twopi * (double)nextfreq * SAMPLEDUR;
5353 nextalpha = sin(nextw0) * (sinh((0.34657359027997 * (double)nextbw * nextw0) / sin(nextw0)));
5354 nextb0rz = 1. / (1. + nextalpha);
5355 nexta0 = nextalpha * nextb0rz;
5356 nexta1 = 0.;
5357 nexta2 = -nexta0;
5358 nextb1 = cos(nextw0) * 2. * nextb0rz;
5359 nextb2 = ((1. - nextalpha) * -nextb0rz);
5360 a0slope = (nexta0 - a0) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta0, a0);
5361 a1slope = (nexta1 - a1) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta1, a1);
5362 a2slope = (nexta2 - a2) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta2, a2);
5363 b1slope = (nextb1 - b1) * unit->mRate->mFilterSlope; //CALCSLOPE(nextb1, b1);
5364 b2slope = (nextb2 - b2) * unit->mRate->mFilterSlope; //CALCSLOPE(nextb2, b2);
5365 unit->m_freq = nextfreq;
5366 unit->m_bw = nextbw;
5367 LOOP(unit->mRate->mFilterLoops,
5368 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5369 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5371 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5372 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5374 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5375 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5376 a0 += a0slope;
5377 a1 += a1slope;
5378 a2 += a2slope;
5379 b1 += b1slope;
5380 b2 += b2slope;
5382 LOOP(unit->mRate->mFilterRemain,
5383 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5384 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5385 y2 = y1;
5386 y1 = y0;
5388 } else {
5389 LOOP(unit->mRate->mFilterLoops,
5390 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5391 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5393 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5394 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5396 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5397 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5399 LOOP(unit->mRate->mFilterRemain,
5400 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5401 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5402 y2 = y1;
5403 y1 = y0;
5407 unit->m_a0 = a0;
5408 unit->m_a1 = a1;
5409 unit->m_a2 = a2;
5410 unit->m_b1 = b1;
5411 unit->m_b2 = b2;
5412 unit->m_y1 = zapgremlins(y1);
5413 unit->m_y2 = zapgremlins(y2);
5416 /* BBandStop */
5417 struct BBandStop : public Unit
5419 double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
5420 float m_freq, m_bw;
5423 extern "C"
5425 void BBandStop_next_kk(BBandStop *unit, int inNumSamples);
5426 void BBandStop_next_aa(BBandStop *unit, int inNumSamples);
5427 void BBandStop_Ctor(BBandStop* unit);
5430 void BBandStop_Ctor(BBandStop* unit)
5432 if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate))
5433 SETCALC(BBandStop_next_aa);
5434 else
5435 SETCALC(BBandStop_next_kk);
5436 float freq = unit->m_freq = ZIN0(1);
5437 float bw = unit->m_bw = ZIN0(2);
5439 double w0 = twopi * (double)freq * SAMPLEDUR;
5440 double sinw0 = sin(w0);
5441 double alpha = sinw0 * (sinh((0.34657359027997 * (double)bw * w0) / sinw0));
5442 double b0rz = 1. / (1. + alpha);
5443 double b1 = unit->m_b1 = 2. * b0rz * cos(w0);
5444 unit->m_a0 = b0rz;
5445 unit->m_a1 = -b1;
5446 unit->m_a2 = b0rz;
5447 unit->m_b2 = (1. - alpha) * -b0rz;
5449 unit->m_y1 = 0.;
5451 unit->m_y2 = 0.;
5452 PUSH_LOOPVALS
5453 BBandStop_next_kk(unit, 1);
5454 POP_LOOPVALS
5457 void BBandStop_next_aa(BBandStop *unit, int inNumSamples)
5459 float *out = ZOUT(0);
5460 float *in = ZIN(0);
5461 float *freq = ZIN(1);
5462 float *bw = ZIN(2);
5464 double a0, a1, a2, b1, b2, w0, sinw0, alpha, b0rz;
5465 double y0, y1, y2;
5466 float nextfreq, nextbw;
5468 y1 = unit->m_y1;
5469 y2 = unit->m_y2;
5471 a0 = unit->m_a0;
5472 a1 = unit->m_a1;
5473 a2 = unit->m_a2;
5474 b1 = unit->m_b1;
5475 b2 = unit->m_b2;
5477 LOOP(unit->mRate->mFilterLoops,
5478 nextfreq = ZXP(freq);
5479 nextbw = ZXP(bw);
5480 if ((unit->m_freq != nextfreq) || (unit->m_bw != nextbw)) {
5481 w0 = twopi * (double)nextfreq * SAMPLEDUR;
5482 sinw0 = sin(w0);
5483 alpha = sinw0 * (sinh((0.34657359027997 * (double)nextbw * w0) / sinw0));
5484 b0rz = 1. / (1. + alpha);
5485 b1 = 2. * b0rz * cos(w0);
5486 a0 = b0rz;
5487 a1 = -b1;
5488 a2 = b0rz;
5489 b2 = (1. - alpha) * -b0rz;
5490 unit->m_freq = ZXP(freq);
5491 unit->m_bw = ZXP(bw);
5493 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5494 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5496 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5497 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5499 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5500 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5502 LOOP(unit->mRate->mFilterRemain,
5503 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5504 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5505 y2 = y1;
5506 y1 = y0;
5507 unit->m_freq = nextfreq;
5508 unit->m_bw = nextbw;
5511 unit->m_a0 = a0;
5512 unit->m_a1 = a1;
5513 unit->m_a2 = a2;
5514 unit->m_b1 = b1;
5515 unit->m_b2 = b2;
5516 unit->m_y1 = zapgremlins(y1);
5517 unit->m_y2 = zapgremlins(y2);
5521 void BBandStop_next_kk(BBandStop *unit, int inNumSamples)
5523 float *out = ZOUT(0);
5524 float *in = ZIN(0);
5525 float nextfreq = ZIN0(1);
5526 float nextbw = ZIN0(2);
5528 double a0, a1, a2, b1, b2;
5529 double nexta0, nexta1, nexta2, nextb1, nextb2, nextw0, nextalpha, nextb0rz;
5530 double y0;
5531 double y1 = unit->m_y1;
5532 double y2 = unit->m_y2;
5533 double a0slope, a1slope, a2slope, b1slope, b2slope;
5535 a0 = unit->m_a0;
5536 a1 = unit->m_a1;
5537 a2 = unit->m_a2;
5538 b1 = unit->m_b1;
5539 b2 = unit->m_b2;
5541 if ((unit->m_freq != nextfreq) || (unit->m_bw != nextbw)){
5542 nextw0 = twopi * (double)nextfreq * SAMPLEDUR;
5543 nextalpha = sin(nextw0) * (sinh((0.34657359027997 * (double)nextbw * nextw0) / sin(nextw0)));
5544 nextb0rz = 1. / (1. + nextalpha);
5545 nextb1 = cos(nextw0) * 2. * nextb0rz;
5546 nexta0 = nextb0rz;
5547 nexta1 = -nextb1;
5548 nexta2 = nextb0rz;
5549 nextb2 = ((1. - nextalpha) * -nextb0rz);
5550 a0slope = (nexta0 - a0) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta0, a0);
5551 a1slope = (nexta1 - a1) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta1, a1);
5552 a2slope = (nexta2 - a2) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta2, a2);
5553 b1slope = (nextb1 - b1) * unit->mRate->mFilterSlope; //CALCSLOPE(nextb1, b1);
5554 b2slope = (nextb2 - b2) * unit->mRate->mFilterSlope; //CALCSLOPE(nextb2, b2);
5555 unit->m_freq = nextfreq;
5556 unit->m_bw = nextbw;
5557 LOOP(unit->mRate->mFilterLoops,
5558 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5559 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5561 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5562 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5564 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5565 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5566 a0 += a0slope;
5567 a1 += a1slope;
5568 a2 += a2slope;
5569 b1 += b1slope;
5570 b2 += b2slope;
5572 LOOP(unit->mRate->mFilterRemain,
5573 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5574 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5575 y2 = y1;
5576 y1 = y0;
5579 } else {
5580 LOOP(unit->mRate->mFilterLoops,
5581 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5582 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5584 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5585 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5587 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5588 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5590 LOOP(unit->mRate->mFilterRemain,
5591 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5592 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5593 y2 = y1;
5594 y1 = y0;
5598 unit->m_a0 = a0;
5599 unit->m_a1 = a1;
5600 unit->m_a2 = a2;
5601 unit->m_b1 = b1;
5602 unit->m_b2 = b2;
5603 unit->m_y1 = zapgremlins(y1);
5604 unit->m_y2 = zapgremlins(y2);
5607 /* BPeakEQ */
5608 struct BPeakEQ : public Unit
5610 double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
5611 float m_freq, m_rq, m_db;
5614 extern "C"
5616 void BPeakEQ_next_kkk(BPeakEQ *unit, int inNumSamples);
5617 void BPeakEQ_next_aaa(BPeakEQ *unit, int inNumSamples);
5618 void BPeakEQ_Ctor(BPeakEQ* unit);
5621 void BPeakEQ_Ctor(BPeakEQ* unit)
5623 if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate) && (INRATE(3) == calc_FullRate))
5624 SETCALC(BPeakEQ_next_aaa);
5625 else
5626 SETCALC(BPeakEQ_next_kkk);
5628 float freq = unit->m_freq = ZIN0(1);
5629 float rq = unit->m_rq = ZIN0(2);
5630 float db = unit->m_db = ZIN0(3);
5631 double a = pow(10., (double)db * 0.025);
5632 double w0 = twopi * (double)freq * SAMPLEDUR;
5633 double alpha = sin(w0) * 0.5 * (double)rq;
5634 double b0rz = 1. / (1. + (alpha / a));
5635 double b1 = unit->m_b1 = 2. * b0rz * cos(w0);
5636 unit->m_a0 = (1. + (alpha * a)) * b0rz;
5637 unit->m_a1 = -b1;
5638 unit->m_a2 = (1. - (alpha * a)) * b0rz;
5639 unit->m_b2 = (1. - (alpha / a)) * -b0rz;
5641 unit->m_y1 = 0.;
5642 unit->m_y2 = 0.;
5643 PUSH_LOOPVALS
5644 BPeakEQ_next_kkk(unit, 1);
5645 POP_LOOPVALS
5648 void BPeakEQ_next_aaa(BPeakEQ *unit, int inNumSamples)
5650 float *out = ZOUT(0);
5651 float *in = ZIN(0);
5652 float *freq = ZIN(1);
5653 float *rq = ZIN(2);
5654 float *db = ZIN(3);
5656 double a0, a1, a2, b1, b2, a, w0, alpha, b0rz;
5657 double y0, y1, y2;
5658 float nextfreq, nextrq, nextdb;
5660 y1 = unit->m_y1;
5661 y2 = unit->m_y2;
5663 a0 = unit->m_a0;
5664 a1 = unit->m_a1;
5665 a2 = unit->m_a2;
5666 b1 = unit->m_b1;
5667 b2 = unit->m_b2;
5670 LOOP(unit->mRate->mFilterLoops,
5671 nextfreq = ZXP(freq);
5672 nextrq = ZXP(rq);
5673 nextdb = ZXP(db);
5674 if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq) || (unit->m_db != nextdb)) {
5675 a = pow(10., (double)nextdb * 0.025);
5676 w0 = twopi * (double)nextfreq * SAMPLEDUR;
5677 alpha = sin(w0) * 0.5 * (double)nextrq;
5678 b0rz = 1. / (1. + (alpha / a));
5679 b1 = 2. * b0rz * cos(w0);
5680 a0 = (1. + (alpha * a)) * b0rz;
5681 a1 = -b1;
5682 a2 = (1. - (alpha * a)) * b0rz;
5683 b2 = (1. - (alpha / a)) * -b0rz;
5684 unit->m_freq = nextfreq;
5685 unit->m_rq = nextrq;
5686 unit->m_db = nextdb;
5688 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5689 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5691 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5692 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5694 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5695 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5697 LOOP(unit->mRate->mFilterRemain,
5698 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5699 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5700 y2 = y1;
5701 y1 = y0;
5702 unit->m_freq = ZXP(freq);
5703 unit->m_rq = ZXP(rq);
5704 unit->m_db = ZXP(db);
5707 unit->m_a0 = a0;
5708 unit->m_a1 = a1;
5709 unit->m_a2 = a2;
5710 unit->m_b1 = b1;
5711 unit->m_b2 = b2;
5712 unit->m_y1 = zapgremlins(y1);
5713 unit->m_y2 = zapgremlins(y2);
5717 void BPeakEQ_next_kkk(BPeakEQ *unit, int inNumSamples)
5719 float *out = ZOUT(0);
5720 float *in = ZIN(0);
5721 float nextfreq = ZIN0(1);
5722 float nextrq = ZIN0(2);
5723 float nextdb = ZIN0(3);
5725 double a0, a1, a2, b1, b2, a, w0, alpha, b0rz;
5726 double y0, y1, y2, a0slope, a1slope, a2slope, b1slope, b2slope, nexta0, nexta1, nexta2, nextb1, nextb2;
5728 y1 = unit->m_y1;
5729 y2 = unit->m_y2;
5731 a0 = unit->m_a0;
5732 a1 = unit->m_a1;
5733 a2 = unit->m_a2;
5734 b1 = unit->m_b1;
5735 b2 = unit->m_b2;
5737 if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq) || (unit->m_db != nextdb)) {
5738 a = pow(10., (double)nextdb * 0.025);
5739 w0 = twopi * (double)nextfreq * SAMPLEDUR;
5740 alpha = sin(w0) * 0.5 * (double)nextrq;
5741 b0rz = 1. / (1. + (alpha / a));
5742 nextb1 = 2. * b0rz * cos(w0);
5743 nexta0 = (1. + (alpha * a)) * b0rz;
5744 nexta1 = -nextb1;
5745 nexta2 = (1. - (alpha * a)) * b0rz;
5746 nextb2 = (1. - (alpha / a)) * -b0rz;
5747 a0slope = (nexta0 - a0) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta0, a0);
5748 a1slope = (nexta1 - a1) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta1, a1);
5749 a2slope = (nexta2 - a2) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta2, a2);
5750 b1slope = (nextb1 - b1) * unit->mRate->mFilterSlope; //CALCSLOPE(nextb1, b1);
5751 b2slope = (nextb2 - b2) * unit->mRate->mFilterSlope; //CALCSLOPE(nextb2, b2);
5752 unit->m_freq = nextfreq;
5753 unit->m_db = nextdb;
5754 unit->m_rq = nextrq;
5755 LOOP(unit->mRate->mFilterLoops,
5756 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5757 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5759 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5760 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5762 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5763 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5765 a0 += a0slope;
5766 a1 += a1slope;
5767 a2 += a2slope;
5768 b1 += b1slope;
5769 b2 += b2slope;
5772 LOOP(unit->mRate->mFilterRemain,
5773 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5774 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5775 y2 = y1;
5776 y1 = y0;
5779 } else {
5780 LOOP(unit->mRate->mFilterLoops,
5781 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5782 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5784 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5785 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5787 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5788 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5791 LOOP(unit->mRate->mFilterRemain,
5792 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5793 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5794 y2 = y1;
5795 y1 = y0;
5799 unit->m_a0 = a0;
5800 unit->m_a1 = a1;
5801 unit->m_a2 = a2;
5802 unit->m_b1 = b1;
5803 unit->m_b2 = b2;
5804 unit->m_y1 = zapgremlins(y1);
5805 unit->m_y2 = zapgremlins(y2);
5808 /* BAllPass */
5809 struct BAllPass : public Unit
5811 double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
5812 float m_freq, m_rq;
5815 extern "C"
5817 void BAllPass_next_kk(BAllPass *unit, int inNumSamples);
5818 void BAllPass_next_aa(BAllPass *unit, int inNumSamples);
5819 void BAllPass_Ctor(BAllPass* unit);
5822 void BAllPass_Ctor(BAllPass* unit)
5824 if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate))
5825 SETCALC(BAllPass_next_aa);
5826 else
5827 SETCALC(BAllPass_next_kk);
5829 float freq = unit->m_freq = ZIN0(1);
5830 float rq = unit->m_rq = ZIN0(2);
5832 double w0 = twopi * (double)freq * SAMPLEDUR;
5833 double alpha = sin(w0) * 0.5 * (double)rq;
5834 double b0rz = 1. / (1. + alpha);
5835 double a0 = unit->m_a0 = (1. - alpha) * b0rz;
5836 double b1 = unit->m_b1 = 2. * b0rz * cos(w0);
5837 unit->m_a1 = -b1;
5838 unit->m_a2 = 1.;
5839 unit->m_b2 = -a0;
5841 unit->m_y1 = 0.;
5843 unit->m_y2 = 0.;
5844 PUSH_LOOPVALS
5845 BAllPass_next_kk(unit, 1);
5846 POP_LOOPVALS
5849 void BAllPass_next_aa(BAllPass *unit, int inNumSamples)
5851 float *out = ZOUT(0);
5852 float *in = ZIN(0);
5853 float *freq = ZIN(1);
5854 float *rq = ZIN(2);
5856 double a0, a1, a2, b1, b2, w0, alpha, b0rz;
5857 double y0, y1, y2;
5858 float nextfreq, nextrq;
5860 y1 = unit->m_y1;
5861 y2 = unit->m_y2;
5863 a0 = unit->m_a0;
5864 a1 = unit->m_a1;
5865 a2 = unit->m_a2;
5866 b1 = unit->m_b1;
5867 b2 = unit->m_b2;
5869 LOOP(unit->mRate->mFilterLoops,
5870 nextfreq = ZXP(freq);
5871 nextrq = ZXP(rq);
5872 if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq)) {
5873 w0 = twopi * (double)nextfreq * SAMPLEDUR;
5874 alpha = sin(w0) * (double)nextrq * 0.5;
5875 b0rz = 1. / (1. + alpha);
5876 b1 = 2. * b0rz * cos(w0);
5877 a0 = (1. - alpha) * b0rz;
5878 a1 = -b1;
5879 a2 = 1.;
5880 b2 = -a0;
5881 unit->m_freq = nextfreq;
5882 unit->m_rq = nextrq;
5884 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5885 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5887 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5888 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5890 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5891 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5893 LOOP(unit->mRate->mFilterRemain,
5894 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5895 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5896 y2 = y1;
5897 y1 = y0;
5898 unit->m_freq = ZXP(freq);
5899 unit->m_rq = ZXP(rq);
5902 unit->m_a0 = a0;
5903 unit->m_a1 = a1;
5904 unit->m_a2 = a2;
5905 unit->m_b1 = b1;
5906 unit->m_b2 = b2;
5907 unit->m_y1 = zapgremlins(y1);
5908 unit->m_y2 = zapgremlins(y2);
5912 void BAllPass_next_kk(BAllPass *unit, int inNumSamples)
5914 float *out = ZOUT(0);
5915 float *in = ZIN(0);
5916 float nextfreq = ZIN0(1);
5917 float nextrq = ZIN0(2);
5919 double a0, a1, a2, b1, b2;
5920 double nexta0, nexta1, nexta2, nextb1, nextb2, nextw0, nextalpha, nextb0rz;
5921 double y0;
5922 double y1 = unit->m_y1;
5923 double y2 = unit->m_y2;
5924 double a0slope, a1slope, a2slope, b1slope, b2slope;
5926 a0 = unit->m_a0;
5927 a1 = unit->m_a1;
5928 a2 = unit->m_a2;
5929 b1 = unit->m_b1;
5930 b2 = unit->m_b2;
5932 if ((unit->m_freq != nextfreq) || (unit->m_rq != nextrq)) {
5933 nextw0 = twopi * (double)nextfreq * SAMPLEDUR;
5934 nextalpha = sin(nextw0) * 0.5 * (double)nextrq;
5935 nextb0rz = 1. / (1. + nextalpha);
5936 nextb1 = cos(nextw0) * 2. * nextb0rz;
5937 nexta0 = (1. - nextalpha) * nextb0rz;
5938 nexta1 = -nextb1;
5939 nexta2 = 1.;
5940 nextb2 = -nexta0;
5941 a0slope = (nexta0 - a0) * unit->mRate->mFilterSlope;
5942 a1slope = (nexta1 - a1) * unit->mRate->mFilterSlope;
5943 a2slope = (nexta2 - a2) * unit->mRate->mFilterSlope;
5944 b1slope = (nextb1 - b1) * unit->mRate->mFilterSlope;
5945 b2slope = (nextb2 - b2) * unit->mRate->mFilterSlope;
5946 unit->m_freq = nextfreq;
5947 unit->m_rq = nextrq;
5948 LOOP(unit->mRate->mFilterLoops,
5949 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5950 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5952 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5953 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5955 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5956 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5957 a0 += a0slope;
5958 a1 += a1slope;
5959 a2 += a2slope;
5960 b1 += b1slope;
5961 b2 += b2slope;
5963 LOOP(unit->mRate->mFilterRemain,
5964 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5965 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5966 y2 = y1;
5967 y1 = y0;
5970 } else {
5971 LOOP(unit->mRate->mFilterLoops,
5972 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5973 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5975 y2 = ZXP(in) + b1 * y0 + b2 * y1;
5976 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
5978 y1 = ZXP(in) + b1 * y2 + b2 * y0;
5979 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
5981 LOOP(unit->mRate->mFilterRemain,
5982 y0 = ZXP(in) + b1 * y1 + b2 * y2;
5983 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
5984 y2 = y1;
5985 y1 = y0;
5989 unit->m_a0 = a0;
5990 unit->m_a1 = a1;
5991 unit->m_a2 = a2;
5992 unit->m_b1 = b1;
5993 unit->m_b2 = b2;
5994 unit->m_y1 = zapgremlins(y1);
5995 unit->m_y2 = zapgremlins(y2);
5999 /* BLowShelf */
6000 struct BLowShelf : public Unit
6002 double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
6003 float m_freq, m_rs, m_db;
6006 extern "C"
6008 void BLowShelf_next_kkk(BLowShelf *unit, int inNumSamples);
6009 void BLowShelf_next_aaa(BLowShelf *unit, int inNumSamples);
6010 void BLowShelf_Ctor(BLowShelf* unit);
6013 void BLowShelf_Ctor(BLowShelf* unit)
6015 if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate) && (INRATE(3) == calc_FullRate))
6016 SETCALC(BLowShelf_next_aaa);
6017 else
6018 SETCALC(BLowShelf_next_kkk);
6020 float freq = unit->m_freq = ZIN0(1);
6021 float rs = unit->m_rs = ZIN0(2);
6022 float db = unit->m_db = ZIN0(3);
6023 double a = pow(10., (double)db * 0.025);
6024 double w0 = twopi * (double)freq * SAMPLEDUR;
6025 double cosw0 = cos(w0);
6026 double sinw0 = sin(w0);
6027 double alpha = sinw0 * 0.5 * sqrt((a + (1./a)) * ((double)rs - 1.) + 2.);
6028 double i = (a + 1.) * cosw0;
6029 double j = (a - 1.) * cosw0;
6030 double k = 2. * sqrt(a) * alpha;
6031 double b0rz = 1. / ((a + 1.) + j + k);
6032 unit->m_a0 = a * (( a + 1.) - j + k) * b0rz;
6033 unit->m_a1 = 2. * a * ((a - 1.) - i) * b0rz;
6034 unit->m_a2 = a * ((a + 1.) - j - k) * b0rz;
6035 unit->m_b1 = 2. * ((a - 1.) + i) * b0rz;
6036 unit->m_b2 = ((a + 1.) + j - k) * -b0rz;
6038 unit->m_y1 = 0.;
6040 unit->m_y2 = 0.;
6041 PUSH_LOOPVALS
6042 BLowShelf_next_kkk(unit, 1);
6043 POP_LOOPVALS
6046 void BLowShelf_next_aaa(BLowShelf *unit, int inNumSamples)
6048 float *out = ZOUT(0);
6049 float *in = ZIN(0);
6050 float *freq = ZIN(1);
6051 float *rs = ZIN(2);
6052 float *db = ZIN(3);
6054 double a0, a1, a2, b1, b2, a, w0, cosw0, sinw0, alpha, i, j, k, b0rz;
6055 double y0, y1, y2;
6056 float nextfreq, nextrs, nextdb;
6058 y1 = unit->m_y1;
6059 y2 = unit->m_y2;
6061 a0 = unit->m_a0;
6062 a1 = unit->m_a1;
6063 a2 = unit->m_a2;
6064 b1 = unit->m_b1;
6065 b2 = unit->m_b2;
6067 LOOP(unit->mRate->mFilterLoops,
6068 nextfreq = ZXP(freq);
6069 nextrs = ZXP(rs);
6070 nextdb = ZXP(db);
6071 if ((unit->m_freq != nextfreq) || (unit->m_rs != nextrs) || (unit->m_db != nextdb)) {
6072 a = pow(10., (double)nextdb * 0.025);
6073 w0 = twopi * (double)nextfreq * SAMPLEDUR;
6074 sinw0 = sin(w0);
6075 cosw0 = cos(w0);
6076 alpha = sinw0 * 0.5 * sqrt((a + (1./a)) * ((double)nextrs - 1.) + 2.);
6077 i = (a + 1.) * cosw0;
6078 j = (a - 1.) * cosw0;
6079 k = 2. * sqrt(a) * alpha;
6080 b0rz = 1. / ((a + 1.) + j + k);
6081 a0 = a * (( a + 1.) - j + k) * b0rz;
6082 a1 = 2. * a * ((a - 1.) - i) * b0rz;
6083 a2 = a * ((a + 1.) - j - k) * b0rz;
6084 b1 = 2. * ((a - 1.) + i) * b0rz;
6085 b2 = ((a + 1.) + j - k) * -b0rz;
6086 unit->m_freq = nextfreq;
6087 unit->m_rs = nextrs;
6088 unit->m_db = nextdb;
6090 y0 = ZXP(in) + b1 * y1 + b2 * y2;
6091 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
6093 y2 = ZXP(in) + b1 * y0 + b2 * y1;
6094 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
6096 y1 = ZXP(in) + b1 * y2 + b2 * y0;
6097 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
6099 LOOP(unit->mRate->mFilterRemain,
6100 y0 = ZXP(in) + b1 * y1 + b2 * y2;
6101 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
6102 y2 = y1;
6103 y1 = y0;
6104 unit->m_freq = ZXP(freq);
6105 unit->m_rs = ZXP(rs);
6106 unit->m_db = ZXP(db);
6109 unit->m_a0 = a0;
6110 unit->m_a1 = a1;
6111 unit->m_a2 = a2;
6112 unit->m_b1 = b1;
6113 unit->m_b2 = b2;
6114 unit->m_y1 = zapgremlins(y1);
6115 unit->m_y2 = zapgremlins(y2);
6118 void BLowShelf_next_kkk(BLowShelf *unit, int inNumSamples)
6120 float *out = ZOUT(0);
6121 float *in = ZIN(0);
6122 float nextfreq = ZIN0(1);
6123 float nextrs = ZIN0(2);
6124 float nextdb = ZIN0(3);
6126 double a0, a1, a2, b1, b2, a, w0, cosw0, sinw0, alpha, i, j, k, b0rz;
6127 double y0, y1, y2, a0slope, a1slope, a2slope, b1slope, b2slope, nexta0, nexta1, nexta2, nextb1, nextb2;
6129 y1 = unit->m_y1;
6130 y2 = unit->m_y2;
6132 a0 = unit->m_a0;
6133 a1 = unit->m_a1;
6134 a2 = unit->m_a2;
6135 b1 = unit->m_b1;
6136 b2 = unit->m_b2;
6138 if ((unit->m_freq != nextfreq) || (unit->m_rs != nextrs) || (unit->m_db != nextdb)) {
6139 a = pow(10., (double)nextdb * 0.025);
6140 w0 = twopi * (double)nextfreq * SAMPLEDUR;
6141 sinw0 = sin(w0);
6142 cosw0 = cos(w0);
6143 alpha = sinw0 * 0.5 * sqrt((a + (1./a)) * ((double)nextrs - 1.) + 2.);
6144 i = (a + 1.) * cosw0;
6145 j = (a - 1.) * cosw0;
6146 k = 2. * sqrt(a) * alpha;
6147 b0rz = 1. / ((a + 1.) + j + k);
6148 nexta0 = a * (( a + 1.) - j + k) * b0rz;
6149 nexta1 = 2. * a * ((a - 1.) - i) * b0rz;
6150 nexta2 = a * ((a + 1.) - j - k) * b0rz;
6151 nextb1 = 2. * ((a - 1.) + i) * b0rz;
6152 nextb2 = ((a + 1.) + j - k) * -b0rz;
6153 a0slope = (nexta0 - a0) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta0, a0);
6154 a1slope = (nexta1 - a1) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta1, a1);
6155 a2slope = (nexta2 - a2) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta2, a2);
6156 b1slope = (nextb1 - b1) * unit->mRate->mFilterSlope; //CALCSLOPE(nextb1, b1);
6157 b2slope = (nextb2 - b2) * unit->mRate->mFilterSlope; //CALCSLOPE(nextb2, b2);
6158 unit->m_freq = nextfreq;
6159 unit->m_db = nextdb;
6160 unit->m_rs = nextrs;
6161 LOOP(unit->mRate->mFilterLoops,
6162 y0 = ZXP(in) + b1 * y1 + b2 * y2;
6163 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
6165 y2 = ZXP(in) + b1 * y0 + b2 * y1;
6166 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
6168 y1 = ZXP(in) + b1 * y2 + b2 * y0;
6169 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
6171 a0 += a0slope;
6172 a1 += a1slope;
6173 a2 += a2slope;
6174 b1 += b1slope;
6175 b2 += b2slope;
6178 LOOP(unit->mRate->mFilterRemain,
6179 y0 = ZXP(in) + b1 * y1 + b2 * y2;
6180 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
6181 y2 = y1;
6182 y1 = y0;
6185 } else {
6186 LOOP(unit->mRate->mFilterLoops,
6187 y0 = ZXP(in) + b1 * y1 + b2 * y2;
6188 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
6190 y2 = ZXP(in) + b1 * y0 + b2 * y1;
6191 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
6193 y1 = ZXP(in) + b1 * y2 + b2 * y0;
6194 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
6197 LOOP(unit->mRate->mFilterRemain,
6198 y0 = ZXP(in) + b1 * y1 + b2 * y2;
6199 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
6200 y2 = y1;
6201 y1 = y0;
6205 unit->m_a0 = a0;
6206 unit->m_a1 = a1;
6207 unit->m_a2 = a2;
6208 unit->m_b1 = b1;
6209 unit->m_b2 = b2;
6210 unit->m_y1 = zapgremlins(y1);
6211 unit->m_y2 = zapgremlins(y2);
6214 /* BHiShelf */
6215 struct BHiShelf : public Unit
6217 double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
6218 float m_freq, m_rs, m_db;
6221 extern "C"
6223 void BHiShelf_next_kkk(BHiShelf *unit, int inNumSamples);
6224 void BHiShelf_next_aaa(BHiShelf *unit, int inNumSamples);
6225 void BHiShelf_Ctor(BHiShelf* unit);
6228 void BHiShelf_Ctor(BHiShelf* unit)
6230 if ((INRATE(1) == calc_FullRate) && (INRATE(2) == calc_FullRate) && (INRATE(3) == calc_FullRate))
6231 SETCALC(BHiShelf_next_aaa);
6232 else
6233 SETCALC(BHiShelf_next_kkk);
6235 float freq = unit->m_freq = ZIN0(1);
6236 float rs = unit->m_rs = ZIN0(2);
6237 float db = unit->m_db = ZIN0(3);
6238 double a = pow(10., (double)db * 0.025);
6239 double w0 = twopi * (double)freq * SAMPLEDUR;
6240 double cosw0 = cos(w0);
6241 double sinw0 = sin(w0);
6242 double alpha = sinw0 * 0.5 * sqrt((a + (1./a)) * ((double)rs - 1.) + 2.);
6243 double i = (a + 1.) * cosw0;
6244 double j = (a - 1.) * cosw0;
6245 double k = 2. * sqrt(a) * alpha;
6247 double b0rz = 1. / ((a + 1.) - j + k);
6248 unit->m_a0 = a * (( a + 1.) + j + k) * b0rz;
6249 unit->m_a1 = -2. * a * ((a - 1.) + i) * b0rz;
6250 unit->m_a2 = a * ((a + 1.) + j - k) * b0rz;
6251 unit->m_b1 = -2. * ((a - 1.) - i) * b0rz;
6252 unit->m_b2 = ((a + 1.) - j - k) * -b0rz;
6254 unit->m_y1 = 0.;
6256 unit->m_y2 = 0.;
6257 PUSH_LOOPVALS
6258 BHiShelf_next_kkk(unit, 1);
6259 POP_LOOPVALS
6262 void BHiShelf_next_aaa(BHiShelf *unit, int inNumSamples)
6264 float *out = ZOUT(0);
6265 float *in = ZIN(0);
6266 float *freq = ZIN(1);
6267 float *rs = ZIN(2);
6268 float *db = ZIN(3);
6270 double a0, a1, a2, b1, b2, a, w0, cosw0, sinw0, alpha, i, j, k, b0rz;
6271 double y0, y1, y2;
6272 float nextfreq, nextrs, nextdb;
6274 y1 = unit->m_y1;
6275 y2 = unit->m_y2;
6277 a0 = unit->m_a0;
6278 a1 = unit->m_a1;
6279 a2 = unit->m_a2;
6280 b1 = unit->m_b1;
6281 b2 = unit->m_b2;
6283 LOOP(unit->mRate->mFilterLoops,
6284 nextfreq = ZXP(freq);
6285 nextrs = ZXP(rs);
6286 nextdb = ZXP(db);
6287 if ((unit->m_freq != nextfreq) || (unit->m_rs != nextrs) || (unit->m_db != nextdb)) {
6288 a = pow(10., (double)nextdb * 0.025);
6289 w0 = twopi * (double)nextfreq * SAMPLEDUR;
6290 sinw0 = sin(w0);
6291 cosw0 = cos(w0);
6292 alpha = sinw0 * 0.5 * sqrt((a + (1./a)) * ((double)nextrs - 1.) + 2.);
6293 i = (a + 1.) * cosw0;
6294 j = (a - 1.) * cosw0;
6295 k = 2. * sqrt(a) * alpha;
6296 b0rz = 1. / ((a + 1.) - j + k);
6297 a0 = a * (( a + 1.) + j + k) * b0rz;
6298 a1 = -2. * a * ((a - 1.) + i) * b0rz;
6299 a2 = a * ((a + 1.) + j - k) * b0rz;
6300 b1 = -2. * ((a - 1.) - i) * b0rz;
6301 b2 = ((a + 1.) - j - k) * -b0rz;
6302 unit->m_freq = ZXP(freq);
6303 unit->m_rs = ZXP(rs);
6304 unit->m_db = ZXP(db);
6306 y0 = ZXP(in) + b1 * y1 + b2 * y2;
6307 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
6309 y2 = ZXP(in) + b1 * y0 + b2 * y1;
6310 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
6312 y1 = ZXP(in) + b1 * y2 + b2 * y0;
6313 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
6315 LOOP(unit->mRate->mFilterRemain,
6316 y0 = ZXP(in) + b1 * y1 + b2 * y2;
6317 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
6318 y2 = y1;
6319 y1 = y0;
6320 unit->m_freq = ZXP(freq);
6321 unit->m_rs = ZXP(rs);
6322 unit->m_db = ZXP(db);
6325 unit->m_a0 = a0;
6326 unit->m_a1 = a1;
6327 unit->m_a2 = a2;
6328 unit->m_b1 = b1;
6329 unit->m_b2 = b2;
6330 unit->m_y1 = zapgremlins(y1);
6331 unit->m_y2 = zapgremlins(y2);
6334 void BHiShelf_next_kkk(BHiShelf *unit, int inNumSamples)
6336 float *out = ZOUT(0);
6337 float *in = ZIN(0);
6338 float nextfreq = ZIN0(1);
6339 float nextrs = ZIN0(2);
6340 float nextdb = ZIN0(3);
6342 double a0, a1, a2, b1, b2, a, w0, cosw0, sinw0, alpha, i, j, k, b0rz;
6343 double y0, y1, y2, a0slope, a1slope, a2slope, b1slope, b2slope, nexta0, nexta1, nexta2, nextb1, nextb2;
6345 y1 = unit->m_y1;
6346 y2 = unit->m_y2;
6348 a0 = unit->m_a0;
6349 a1 = unit->m_a1;
6350 a2 = unit->m_a2;
6351 b1 = unit->m_b1;
6352 b2 = unit->m_b2;
6354 if ((unit->m_freq != nextfreq) || (unit->m_rs != nextrs) || (unit->m_db != nextdb)) {
6355 a = pow(10., (double)nextdb * 0.025);
6356 w0 = twopi * (double)nextfreq * SAMPLEDUR;
6357 sinw0 = sin(w0);
6358 cosw0 = cos(w0);
6359 alpha = sinw0 * 0.5 * sqrt((a + (1./a)) * ((double)nextrs - 1.) + 2.);
6360 i = (a + 1.) * cosw0;
6361 j = (a - 1.) * cosw0;
6362 k = 2. * sqrt(a) * alpha;
6363 b0rz = 1. / ((a + 1.) - j + k);
6364 nexta0 = a * (( a + 1.) + j + k) * b0rz;
6365 nexta1 = -2. * a * ((a - 1.) + i) * b0rz;
6366 nexta2 = a * ((a + 1.) + j - k) * b0rz;
6367 nextb1 = -2. * ((a - 1.) - i) * b0rz;
6368 nextb2 = ((a + 1.) - j - k) * -b0rz;
6369 a0slope = (nexta0 - a0) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta0, a0);
6370 a1slope = (nexta1 - a1) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta1, a1);
6371 a2slope = (nexta2 - a2) * unit->mRate->mFilterSlope; //CALCSLOPE(nexta2, a2);
6372 b1slope = (nextb1 - b1) * unit->mRate->mFilterSlope; //CALCSLOPE(nextb1, b1);
6373 b2slope = (nextb2 - b2) * unit->mRate->mFilterSlope; //CALCSLOPE(nextb2, b2);
6374 unit->m_freq = nextfreq;
6375 unit->m_db = nextdb;
6376 unit->m_rs = nextrs;
6377 LOOP(unit->mRate->mFilterLoops,
6378 y0 = ZXP(in) + b1 * y1 + b2 * y2;
6379 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
6381 y2 = ZXP(in) + b1 * y0 + b2 * y1;
6382 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
6384 y1 = ZXP(in) + b1 * y2 + b2 * y0;
6385 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
6387 a0 += a0slope;
6388 a1 += a1slope;
6389 a2 += a2slope;
6390 b1 += b1slope;
6391 b2 += b2slope;
6394 LOOP(unit->mRate->mFilterRemain,
6395 y0 = ZXP(in) + b1 * y1 + b2 * y2;
6396 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
6397 y2 = y1;
6398 y1 = y0;
6401 } else {
6402 LOOP(unit->mRate->mFilterLoops,
6403 y0 = ZXP(in) + b1 * y1 + b2 * y2;
6404 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
6406 y2 = ZXP(in) + b1 * y0 + b2 * y1;
6407 ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
6409 y1 = ZXP(in) + b1 * y2 + b2 * y0;
6410 ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
6413 LOOP(unit->mRate->mFilterRemain,
6414 y0 = ZXP(in) + b1 * y1 + b2 * y2;
6415 ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
6416 y2 = y1;
6417 y1 = y0;
6420 unit->m_a0 = a0;
6421 unit->m_a1 = a1;
6422 unit->m_a2 = a2;
6423 unit->m_b1 = b1;
6424 unit->m_b2 = b2;
6425 unit->m_y1 = zapgremlins(y1);
6426 unit->m_y2 = zapgremlins(y2);
6429 ////////////////////////////////////////////////////////////////////////////////////////////////////////
6430 PluginLoad(Filter)
6432 ft = inTable;
6434 DefineSimpleUnit(Ramp);
6435 DefineSimpleUnit(Lag);
6436 DefineSimpleUnit(Lag2);
6437 DefineSimpleUnit(Lag3);
6438 DefineSimpleUnit(LagUD);
6439 DefineSimpleUnit(Lag2UD);
6440 DefineSimpleUnit(Lag3UD);
6441 DefineSimpleUnit(VarLag);
6442 DefineSimpleUnit(OnePole);
6443 DefineSimpleUnit(OneZero);
6444 DefineSimpleUnit(TwoPole);
6445 DefineSimpleUnit(TwoZero);
6446 DefineSimpleUnit(Decay);
6447 DefineSimpleUnit(Decay2);
6448 DefineSimpleUnit(Flip);
6449 DefineSimpleUnit(Delay1);
6450 DefineSimpleUnit(Delay2);
6451 DefineSimpleUnit(Integrator);
6452 DefineSimpleUnit(LeakDC);
6453 DefineSimpleUnit(LPZ1);
6454 DefineSimpleUnit(HPZ1);
6455 DefineSimpleUnit(LPZ2);
6456 DefineSimpleUnit(HPZ2);
6457 DefineSimpleUnit(BPZ2);
6458 DefineSimpleUnit(BRZ2);
6459 DefineSimpleUnit(APF);
6460 DefineSimpleUnit(LPF);
6461 DefineSimpleUnit(HPF);
6462 DefineSimpleUnit(BPF);
6463 DefineSimpleUnit(BRF);
6464 DefineSimpleUnit(RLPF);
6465 DefineSimpleUnit(RHPF);
6467 DefineSimpleUnit(Slew);
6468 DefineSimpleUnit(Slope);
6470 DefineSimpleUnit(MidEQ);
6471 DefineSimpleUnit(Median);
6473 DefineSimpleUnit(Resonz);
6474 DefineSimpleUnit(Ringz);
6475 DefineSimpleUnit(Formlet);
6477 DefineSimpleUnit(FOS);
6478 DefineSimpleUnit(SOS);
6480 DefineSimpleUnit(Compander);
6481 DefineDtorUnit(Limiter);
6482 DefineDtorUnit(Normalizer);
6484 DefineSimpleUnit(Amplitude);
6485 DefineSimpleUnit(DetectSilence);
6487 DefineSimpleUnit(Hilbert);
6488 DefineSimpleUnit(FreqShift);
6489 DefineSimpleUnit(MoogFF);
6491 /* BEQSuite UGens */
6492 DefineSimpleUnit(BLowPass);
6493 DefineSimpleUnit(BHiPass);
6494 DefineSimpleUnit(BBandPass);
6495 DefineSimpleUnit(BBandStop);
6496 DefineSimpleUnit(BPeakEQ);
6497 DefineSimpleUnit(BAllPass);
6498 DefineSimpleUnit(BLowShelf);
6499 DefineSimpleUnit(BHiShelf);
6503 //////////////////////////////////////////////////////////////////////////////////////////////////