Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e28db25801
Fetching contributors…

Cannot retrieve contributors at this time

6631 lines (5597 sloc) 155.268 kb
/*
SuperCollider real time audio synthesis system
Copyright (c) 2002 James McCartney. All rights reserved.
http://www.audiosynth.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "SC_PlugIn.h"
#define PI 3.1415926535898f
#define PUSH_LOOPVALS \
int tmp_floops = unit->mRate->mFilterLoops; \
int tmp_fremain = unit->mRate->mFilterRemain; \
unit->mRate->mFilterLoops = 0; \
unit->mRate->mFilterRemain = 1;
#define POP_LOOPVALS \
unit->mRate->mFilterLoops = tmp_floops; \
unit->mRate->mFilterRemain = tmp_fremain;
using namespace std; // for math functions
static InterfaceTable *ft;
struct Ramp : public Unit
{
double m_level, m_slope;
int m_counter;
};
struct Lag : public Unit
{
float m_lag;
double m_b1, m_y1;
};
struct Lag2 : public Unit
{
float m_lag;
double m_b1, m_y1a, m_y1b;
};
struct Lag3 : public Unit
{
float m_lag;
double m_b1, m_y1a, m_y1b, m_y1c;
};
struct LagUD : public Unit
{
float m_lagu, m_lagd;
double m_b1u, m_b1d, m_y1;
};
struct Lag2UD : public Unit
{
float m_lagu, m_lagd;
double m_b1u, m_b1d, m_y1a, m_y1b;
};
struct Lag3UD : public Unit
{
float m_lagu, m_lagd;
double m_b1u, m_b1d, m_y1a, m_y1b, m_y1c;
};
struct VarLag : public Unit
{
double m_level, m_slope;
int m_counter;
float m_in, m_lagTime;
};
struct OnePole : public Unit
{
double m_b1, m_y1;
};
struct OneZero : public Unit
{
double m_b1, m_x1;
};
struct Integrator : public Unit
{
double m_b1, m_y1;
};
struct Decay : public Unit
{
float m_decayTime;
double m_y1, m_b1;
};
struct Decay2 : public Unit
{
float m_attackTime;
double m_y1a, m_b1a;
float m_decayTime;
double m_y1b, m_b1b;
};
struct LeakDC : public Unit
{
double m_b1, m_x1, m_y1;
};
struct TwoPole : public Unit
{
float m_freq, m_reson;
double m_y1, m_y2, m_b1, m_b2;
};
struct APF : public Unit
{
float m_freq, m_reson;
double m_y1, m_y2, m_x1, m_x2, m_b1, m_b2;
};
struct TwoZero : public Unit
{
float m_freq, m_reson;
double m_x1, m_x2, m_b1, m_b2;
};
struct LPZ1 : public Unit
{
double m_x1;
};
struct HPZ1 : public Unit
{
double m_x1;
};
struct HPZ2 : public Unit
{
double m_x1, m_x2;
};
struct BPZ2 : public Unit
{
double m_x1, m_x2;
};
struct BRZ2 : public Unit
{
double m_x1, m_x2;
};
struct LPZ2 : public Unit
{
double m_x1, m_x2;
};
struct Flip : public Unit
{
};
struct Delay1 : public Unit
{
float m_x1;
};
struct Delay2 : public Unit
{
float m_x1, m_x2;
};
struct Slope : public Unit
{
double m_x1;
};
struct Slew : public Unit
{
double mLevel;
};
struct RLPF : public Unit
{
float m_freq, m_reson;
double m_y1, m_y2, m_a0, m_b1, m_b2;
};
struct RHPF : public Unit
{
float m_freq, m_reson;
double m_y1, m_y2, m_a0, m_b1, m_b2;
};
struct LPF : public Unit
{
float m_freq;
double m_y1, m_y2, m_a0, m_b1, m_b2;
};
struct HPF : public Unit
{
float m_freq;
double m_y1, m_y2, m_a0, m_b1, m_b2;
};
struct BPF : public Unit
{
float m_freq, m_bw;
double m_y1, m_y2, m_a0, m_b1, m_b2;
};
struct BRF : public Unit
{
float m_freq, m_bw;
double m_y1, m_y2, m_a0, m_a1, m_b2;
};
struct MidEQ : public Unit
{
float m_freq, m_bw, m_db;
double m_y1, m_y2, m_a0, m_b1, m_b2;
};
struct Resonz : public Unit
{
float m_freq, m_rq;
double m_y1, m_y2, m_a0, m_b1, m_b2;
};
struct Ringz : public Unit
{
float m_freq, m_decayTime;
double m_y1, m_y2, m_b1, m_b2;
};
struct FOS : public Unit
{
double m_y1, m_a0, m_a1, m_b1;
};
struct SOS : public Unit
{
double m_y1, m_y2, m_a0, m_a1, m_a2, m_b1, m_b2;
};
struct Formlet : public Unit
{
float m_freq, m_decayTime, m_attackTime;
double m_y01, m_y02, m_b01, m_b02;
double m_y11, m_y12, m_b11, m_b12;
};
const int kMAXMEDIANSIZE = 32;
struct Median : public Unit
{
float m_medianValue[kMAXMEDIANSIZE];
long m_medianAge[kMAXMEDIANSIZE];
long m_medianSize, m_medianIndex;
};
struct Compander : public Unit
{
float m_prevmaxval, m_gain, m_clamp, m_clampcoef, m_relax, m_relaxcoef;
};
struct Normalizer : public Unit
{
float *m_table, *m_xinbuf, *m_xoutbuf, *m_xmidbuf;
long m_flips, m_pos, m_bufsize;
float m_slope, m_level, m_curmaxval, m_prevmaxval, m_slopefactor;
};
struct Limiter : public Unit
{
float *m_table, *m_xinbuf, *m_xoutbuf, *m_xmidbuf;
long m_flips, m_pos, m_bufsize;
float m_slope, m_level, m_curmaxval, m_prevmaxval, m_slopefactor;
};
struct Amplitude : public Unit
{
float m_previn, m_clampcoef, m_relaxcoef, m_clamp_in, m_relax_in;
};
struct DetectSilence : public Unit
{
float mThresh;
int32 mCounter, mEndCounter;
};
struct Hilbert : public Unit
{
double m_coefs[12];
double m_y1[12];
};
struct FreqShift : public Unit
{
float m_coefs[12];
float m_y1[12];
int32 m_phase;
int32 m_phaseoffset, m_lomask;
double m_cpstoinc, m_radtoinc, m_phasein;
};
struct MoogFF : public Unit
{
float m_freq, m_k;
double m_b0, m_a1; // Resonant freq and corresponding vals; stored because we need to compare against prev vals
double m_wcD;
double m_s1, m_s2, m_s3, m_s4; // 1st order filter states
};
//////////////////////////////////////////////////////////////////////////////////////////////////
extern "C"
{
void Ramp_next(Ramp *unit, int inNumSamples);
void Ramp_next_1(Ramp *unit, int inNumSamples);
void Ramp_Ctor(Ramp* unit);
void Lag_next(Lag *unit, int inNumSamples);
void Lag_Ctor(Lag* unit);
void Lag2_Ctor(Lag2* unit);
void Lag3_next(Lag3 *unit, int inNumSamples);
void Lag3_Ctor(Lag3* unit);
void LagUD_next(LagUD *unit, int inNumSamples);
void LagUD_Ctor(LagUD* unit);
void Lag2UD_next(Lag2UD *unit, int inNumSamples);
void Lag2UD_Ctor(Lag2UD* unit);
void Lag3UD_next(Lag3UD *unit, int inNumSamples);
void Lag3UD_Ctor(Lag3UD* unit);
void VarLag_next(VarLag *unit, int inNumSamples);
void VarLag_Ctor(VarLag* unit);
void OnePole_next_a(OnePole *unit, int inNumSamples);
void OnePole_next_k(OnePole *unit, int inNumSamples);
void OnePole_Ctor(OnePole* unit);
void OneZero_next(OneZero* unit, int inNumSamples);
void OneZero_Ctor(OneZero* unit);
void Integrator_next(Integrator *unit, int inNumSamples);
void Integrator_Ctor(Integrator* unit);
void Decay_next(Decay *unit, int inNumSamples);
void Decay_Ctor(Decay* unit);
void Decay2_next(Decay2 *unit, int inNumSamples);
void Decay2_Ctor(Decay2* unit);
void LeakDC_next(LeakDC *unit, int inNumSamples);
void LeakDC_next_1(LeakDC* unit, int inNumSamples);
void LeakDC_Ctor(LeakDC* unit);
void TwoPole_next(TwoPole *unit, int inNumSamples);
void TwoPole_Ctor(TwoPole* unit);
void TwoZero_next(TwoZero *unit, int inNumSamples);
void TwoZero_Ctor(TwoZero* unit);
void APF_next(APF *unit, int inNumSamples);
void APF_Ctor(APF* unit);
void LPZ1_next(LPZ1 *unit, int inNumSamples);
void LPZ1_Ctor(LPZ1* unit);
void HPZ1_next(HPZ1 *unit, int inNumSamples);
void HPZ1_Ctor(HPZ1* unit);
void Slope_next(Slope *unit, int inNumSamples);
void Slope_Ctor(Slope* unit);
void Delay1_next(Delay1 *unit, int inNumSamples);
void Delay1_Ctor(Delay1* unit);
void Flip_Ctor(Flip* unit);
void Flip_next_even(Flip *unit, int inNumSamples);
void Flip_next_odd(Flip *unit, int inNumSamples);
void Delay2_next(Delay2 *unit, int inNumSamples);
void Delay2_Ctor(Delay2* unit);
void LPZ2_next(LPZ2 *unit, int inNumSamples);
void LPZ2_Ctor(LPZ2* unit);
void HPZ2_next(HPZ2 *unit, int inNumSamples);
void HPZ2_Ctor(HPZ2* unit);
void BPZ2_next(BPZ2 *unit, int inNumSamples);
void BPZ2_Ctor(BPZ2* unit);
void BRZ2_next(BRZ2 *unit, int inNumSamples);
void BRZ2_Ctor(BRZ2* unit);
void Slew_next(Slew *unit, int inNumSamples);
void Slew_Ctor(Slew* unit);
void RLPF_next(RLPF *unit, int inNumSamples);
void RLPF_next_1(RLPF *unit, int inNumSamples);
void RLPF_Ctor(RLPF* unit);
void RHPF_next(RHPF *unit, int inNumSamples);
void RHPF_next_1(RHPF *unit, int inNumSamples);
void RHPF_Ctor(RHPF* unit);
void LPF_next(LPF *unit, int inNumSamples);
void LPF_next_1(LPF *unit, int inNumSamples);
void LPF_Ctor(LPF* unit);
void HPF_next(HPF *unit, int inNumSamples);
void HPF_next_1(HPF *unit, int inNumSamples);
void HPF_Ctor(HPF* unit);
void BPF_next(BPF *unit, int inNumSamples);
void BPF_next_1(BPF *unit, int inNumSamples);
void BPF_Ctor(BPF* unit);
void BRF_next(BRF *unit, int inNumSamples);
void BRF_next_1(BRF *unit, int inNumSamples);
void BRF_Ctor(BRF* unit);
void Median_next(Median *unit, int inNumSamples);
void Median_Ctor(Median* unit);
void MidEQ_next(MidEQ *unit, int inNumSamples);
void MidEQ_Ctor(MidEQ* unit);
void Resonz_next(Resonz *unit, int inNumSamples);
void Resonz_Ctor(Resonz* unit);
void Ringz_next(Ringz *unit, int inNumSamples);
void Ringz_Ctor(Ringz* unit);
void Formlet_next(Formlet *unit, int inNumSamples);
void Formlet_next_1(Formlet *unit, int inNumSamples);
void Formlet_Ctor(Formlet* unit);
void FOS_next_k(FOS *unit, int inNumSamples);
void FOS_next_a(FOS *unit, int inNumSamples);
void FOS_next_1(FOS *unit, int inNumSamples);
void FOS_Ctor(FOS* unit);
void SOS_next_k(SOS *unit, int inNumSamples);
void SOS_next_a(SOS *unit, int inNumSamples);
void SOS_next_1(SOS *unit, int inNumSamples);
void SOS_Ctor(SOS* unit);
void Normalizer_next(Normalizer *unit, int inNumSamples);
void Normalizer_Ctor(Normalizer* unit);
void Normalizer_Dtor(Normalizer* unit);
void Limiter_next(Limiter *unit, int inNumSamples);
void Limiter_Ctor(Limiter* unit);
void Limiter_Dtor(Limiter* unit);
void Compander_next(Compander *unit, int inNumSamples);
void Compander_Ctor(Compander* unit);
void Amplitude_next(Amplitude *unit, int inNumSamples);
void Amplitude_next_kk(Amplitude *unit, int inNumSamples);
void Amplitude_next_atok(Amplitude *unit, int inNumSamples);
void Amplitude_next_atok_kk(Amplitude *unit, int inNumSamples);
void Amplitude_Ctor(Amplitude* unit);
void DetectSilence_next(DetectSilence *unit, int inNumSamples);
void DetectSilence_done(DetectSilence *unit, int inNumSamples);
void DetectSilence_Ctor(DetectSilence* unit);
void Hilbert_Ctor(Hilbert* unit);
void Hilbert_next(Hilbert *unit, int inNumSamples);
void FreqShift_Ctor(FreqShift* unit);
void FreqShift_next_kk(FreqShift *unit, int inNumSamples);
void FreqShift_next_aa(FreqShift *unit, int inNumSamples);
void FreqShift_next_ak(FreqShift *unit, int inNumSamples);
void FreqShift_next_ka(FreqShift *unit, int inNumSamples);
void MoogFF_next(MoogFF *unit, int inNumSamples);
void MoogFF_Ctor(MoogFF* unit);
/*
void Lag_next(Lag *unit, int inNumSamples);
void Lag_Ctor(Lag* unit);
void Lag_next(Lag *unit, int inNumSamples);
void Lag_Ctor(Lag* unit);
*/
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void Ramp_next(Ramp *unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = IN(0);
float period = ZIN0(1);
double slope = unit->m_slope;
double level = unit->m_level;
int counter = unit->m_counter;
int remain = inNumSamples;
while (remain) {
int nsmps = sc_min(remain, counter);
LOOP(nsmps,
ZXP(out) = level;
level += slope;
);
in += nsmps;
counter -= nsmps;
remain -= nsmps;
if (counter <= 0) {
counter = (int)(period * SAMPLERATE);
counter = sc_max(1, counter);
slope = (*in - level) / counter;
}
}
unit->m_level = level;
unit->m_slope = slope;
unit->m_counter = counter;
}
void Ramp_next_1(Ramp *unit, int inNumSamples)
{
float *out = OUT(0);
*out = unit->m_level;
unit->m_level += unit->m_slope;
if (--unit->m_counter <= 0) {
float in = ZIN0(0);
float period = ZIN0(1);
int counter = (int)(period * SAMPLERATE);
unit->m_counter = counter = sc_max(1, counter);
unit->m_slope = (in - unit->m_level) / counter;
}
}
void Ramp_Ctor(Ramp* unit)
{
if (BUFLENGTH == 1) {
SETCALC(Ramp_next_1);
} else {
SETCALC(Ramp_next);
}
unit->m_counter = 1;
unit->m_level = ZIN0(0);
unit->m_slope = 0.f;
ZOUT0(0) = unit->m_level;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void Lag_next(Lag *unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
float lag = ZIN0(1);
double y1 = unit->m_y1;
double b1 = unit->m_b1;
if (lag == unit->m_lag) {
LOOP1(inNumSamples,
double y0 = ZXP(in);
ZXP(out) = y1 = y0 + b1 * (y1 - y0);
);
} else {
unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
double b1_slope = CALCSLOPE(unit->m_b1, b1);
unit->m_lag = lag;
LOOP1(inNumSamples,
b1 += b1_slope;
double y0 = ZXP(in);
ZXP(out) = y1 = y0 + b1 * (y1 - y0);
);
}
unit->m_y1 = zapgremlins(y1);
}
void Lag_next_1(Lag *unit, int inNumSamples)
{
float *out = OUT(0);
float *in = IN(0);
float lag = IN0(1);
double y1 = unit->m_y1;
double b1 = unit->m_b1;
if (lag == unit->m_lag) {
double y0 = *in;
*out = y1 = y0 + b1 * (y1 - y0);
} else {
unit->m_b1 = b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
unit->m_lag = lag;
double y0 = *in;
*out = y1 = y0 + b1 * (y1 - y0);
}
unit->m_y1 = zapgremlins(y1);
}
void Lag_Ctor(Lag* unit)
{
if (BUFLENGTH == 1)
SETCALC(Lag_next_1);
else
SETCALC(Lag_next);
unit->m_lag = 0.f;
unit->m_b1 = 0.f;
unit->m_y1 = ZIN0(0);
Lag_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void LagUD_next(LagUD *unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
float lagu = ZIN0(1);
float lagd = ZIN0(2);
double y1 = unit->m_y1;
double b1u = unit->m_b1u;
double b1d = unit->m_b1d;
if ( (lagu == unit->m_lagu) && (lagd == unit->m_lagd) ) {
LOOP1(inNumSamples,
double y0 = ZXP(in);
if ( y0 > y1 )
ZXP(out) = y1 = y0 + b1u * (y1 - y0);
else
ZXP(out) = y1 = y0 + b1d * (y1 - y0);
);
} else {
unit->m_b1u = lagu == 0.f ? 0.f : exp(log001 / (lagu * unit->mRate->mSampleRate));
double b1u_slope = CALCSLOPE(unit->m_b1u, b1u);
unit->m_lagu = lagu;
unit->m_b1d = lagd == 0.f ? 0.f : exp(log001 / (lagd * unit->mRate->mSampleRate));
double b1d_slope = CALCSLOPE(unit->m_b1d, b1d);
unit->m_lagd = lagd;
LOOP1(inNumSamples,
b1u += b1u_slope;
b1d += b1d_slope;
double y0 = ZXP(in);
if ( y0 > y1 )
ZXP(out) = y1 = y0 + b1u * (y1 - y0);
else
ZXP(out) = y1 = y0 + b1d * (y1 - y0);
);
}
unit->m_y1 = zapgremlins(y1);
}
void LagUD_Ctor(LagUD* unit)
{
SETCALC(LagUD_next);
unit->m_lagu = 0.f;
unit->m_lagd = 0.f;
unit->m_b1u = 0.f;
unit->m_b1d = 0.f;
unit->m_y1 = ZIN0(0);
LagUD_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
static void Lag2_next_k(Lag2 *unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
float lag = ZIN0(1);
double y1a = unit->m_y1a;
double y1b = unit->m_y1b;
double b1 = unit->m_b1;
if (lag == unit->m_lag) {
LOOP1(inNumSamples,
double y0a = ZXP(in);
y1a = y0a + b1 * (y1a - y0a);
y1b = y1a + b1 * (y1b - y1a);
ZXP(out) = y1b;
);
} else {
unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
double b1_slope = CALCSLOPE(unit->m_b1, b1);
unit->m_lag = lag;
LOOP1(inNumSamples,
b1 += b1_slope;
double y0a = ZXP(in);
y1a = y0a + b1 * (y1a - y0a);
y1b = y1a + b1 * (y1b - y1a);
ZXP(out) = y1b;
);
}
unit->m_y1a = zapgremlins(y1a);
unit->m_y1b = zapgremlins(y1b);
}
static void Lag2_next_i(Lag2 *unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
double y1a = unit->m_y1a;
double y1b = unit->m_y1b;
double b1 = unit->m_b1;
LOOP1(inNumSamples,
double y0a = ZXP(in);
y1a = y0a + b1 * (y1a - y0a);
y1b = y1a + b1 * (y1b - y1a);
ZXP(out) = y1b;
);
unit->m_y1a = zapgremlins(y1a);
unit->m_y1b = zapgremlins(y1b);
}
static void Lag2_next_1_i(Lag2 *unit, int inNumSamples)
{
double y1a = unit->m_y1a;
double y1b = unit->m_y1b;
double b1 = unit->m_b1;
float y0a = ZIN0(0);
y1a = y0a + b1 * (y1a - y0a);
y1b = y1a + b1 * (y1b - y1a);
ZOUT0(0) = y1b;
unit->m_y1a = zapgremlins(y1a);
unit->m_y1b = zapgremlins(y1b);
}
void Lag2_Ctor(Lag2* unit)
{
switch (INRATE(1)) {
case calc_FullRate:
case calc_BufRate:
SETCALC(Lag2_next_k);
break;
default:
if (BUFLENGTH == 1)
SETCALC(Lag2_next_1_i);
else
SETCALC(Lag2_next_i);
break;
}
unit->m_lag = 0.f;
unit->m_b1 = 0.f;
unit->m_y1a = unit->m_y1b = ZIN0(0);
Lag2_next_k(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void Lag2UD_next(Lag2UD *unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
float lagu = ZIN0(1);
float lagd = ZIN0(2);
double y1a = unit->m_y1a;
double y1b = unit->m_y1b;
double b1u = unit->m_b1u;
double b1d = unit->m_b1d;
if ( (lagu == unit->m_lagu) && (lagd == unit->m_lagd) ) {
LOOP1(inNumSamples,
double y0a = ZXP(in);
if ( y0a > y1a ) {
y1a = y0a + b1u * (y1a - y0a);
} else {
y1a = y0a + b1d * (y1a - y0a);
}
if ( y1a > y1b )
y1b = y1a + b1u * (y1b - y1a);
else
y1b = y1a + b1d * (y1b - y1a);
ZXP(out) = y1b;
);
} else {
unit->m_b1u = lagu == 0.f ? 0.f : exp(log001 / (lagu * unit->mRate->mSampleRate));
double b1u_slope = CALCSLOPE(unit->m_b1u, b1u);
unit->m_lagu = lagu;
unit->m_b1d = lagd == 0.f ? 0.f : exp(log001 / (lagd * unit->mRate->mSampleRate));
double b1d_slope = CALCSLOPE(unit->m_b1d, b1d);
unit->m_lagd = lagd;
LOOP1(inNumSamples,
b1u += b1u_slope;
b1d += b1d_slope;
double y0a = ZXP(in);
if ( y0a > y1a ) {
y1a = y0a + b1u * (y1a - y0a);
} else {
y1a = y0a + b1d * (y1a - y0a);
}
if ( y1a > y1b )
y1b = y1a + b1u * (y1b - y1a);
else
y1b = y1a + b1d * (y1b - y1a);
ZXP(out) = y1b;
);
}
unit->m_y1a = zapgremlins(y1a);
unit->m_y1b = zapgremlins(y1b);
}
void Lag2UD_Ctor(Lag2UD* unit)
{
SETCALC(Lag2UD_next);
unit->m_lagu = 0.f;
unit->m_lagd = 0.f;
unit->m_b1u = 0.f;
unit->m_b1d = 0.f;
unit->m_y1a = unit->m_y1b = ZIN0(0);
Lag2UD_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void Lag3_next(Lag3 *unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
float lag = ZIN0(1);
double y1a = unit->m_y1a;
double y1b = unit->m_y1b;
double y1c = unit->m_y1c;
double b1 = unit->m_b1;
if (lag == unit->m_lag) {
LOOP1(inNumSamples,
double y0a = ZXP(in);
y1a = y0a + b1 * (y1a - y0a);
y1b = y1a + b1 * (y1b - y1a);
y1c = y1b + b1 * (y1c - y1b);
ZXP(out) = y1c;
);
} else {
unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
double b1_slope = CALCSLOPE(unit->m_b1, b1);
unit->m_lag = lag;
LOOP1(inNumSamples,
b1 += b1_slope;
double y0a = ZXP(in);
y1a = y0a + b1 * (y1a - y0a);
y1b = y1a + b1 * (y1b - y1a);
y1c = y1b + b1 * (y1c - y1b);
ZXP(out) = y1c;
);
}
unit->m_y1a = zapgremlins(y1a);
unit->m_y1b = zapgremlins(y1b);
unit->m_y1c = zapgremlins(y1c);
}
void Lag3_Ctor(Lag3* unit)
{
SETCALC(Lag3_next);
unit->m_lag = 0.f;
unit->m_b1 = 0.f;
unit->m_y1a = unit->m_y1b = unit->m_y1c = ZIN0(0);
Lag3_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void Lag3UD_next(Lag3UD *unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
float lagu = ZIN0(1);
float lagd = ZIN0(2);
double y1a = unit->m_y1a;
double y1b = unit->m_y1b;
double y1c = unit->m_y1c;
double b1u = unit->m_b1u;
double b1d = unit->m_b1d;
if ( (lagu == unit->m_lagu) && (lagd == unit->m_lagd) ) {
LOOP1(inNumSamples,
double y0a = ZXP(in);
if ( y0a > y1a ) {
y1a = y0a + b1u * (y1a - y0a);
} else {
y1a = y0a + b1d * (y1a - y0a);
}
if ( y1a > y1b ) {
y1b = y1a + b1u * (y1b - y1a);
} else {
y1b = y1a + b1d * (y1b - y1a);
}
if ( y1a > y1b ) {
y1c = y1b + b1u * (y1c - y1b);
} else {
y1c = y1b + b1d * (y1c - y1b);
}
ZXP(out) = y1c;
);
} else {
unit->m_b1u = lagu == 0.f ? 0.f : exp(log001 / (lagu * unit->mRate->mSampleRate));
double b1u_slope = CALCSLOPE(unit->m_b1u, b1u);
unit->m_lagu = lagu;
unit->m_b1d = lagd == 0.f ? 0.f : exp(log001 / (lagd * unit->mRate->mSampleRate));
double b1d_slope = CALCSLOPE(unit->m_b1d, b1d);
unit->m_lagd = lagd;
LOOP1(inNumSamples,
b1u += b1u_slope;
b1d += b1d_slope;
double y0a = ZXP(in);
if ( y0a > y1a ) {
y1a = y0a + b1u * (y1a - y0a);
} else {
y1a = y0a + b1d * (y1a - y0a);
}
if ( y1a > y1b ) {
y1b = y1a + b1u * (y1b - y1a);
} else {
y1b = y1a + b1d * (y1b - y1a);
}
if ( y1a > y1b ) {
y1c = y1b + b1u * (y1c - y1b);
} else {
y1c = y1b + b1d * (y1c - y1b);
}
ZXP(out) = y1c;
);
}
unit->m_y1a = zapgremlins(y1a);
unit->m_y1b = zapgremlins(y1b);
unit->m_y1c = zapgremlins(y1c);
}
void Lag3UD_Ctor(Lag3UD* unit)
{
SETCALC(Lag3UD_next);
unit->m_lagu = 0.f;
unit->m_lagd = 0.f;
unit->m_b1u = 0.f;
unit->m_b1d = 0.f;
unit->m_y1a = unit->m_y1b = unit->m_y1c = ZIN0(0);
Lag3UD_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void VarLag_next(VarLag *unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = IN(0);
float lagTime = ZIN0(1);
double slope = unit->m_slope;
double level = unit->m_level;
int counter = unit->m_counter;
int remain = inNumSamples;
if ( *in != unit->m_in) {
counter = (int)(lagTime * SAMPLERATE);
counter = unit->m_counter = sc_max(1, counter);
slope = unit->m_slope = ( *in - unit->m_level) / counter;
unit->m_in = *in;
unit->m_lagTime = lagTime;
} else {
if (lagTime != unit->m_lagTime) {
float scaleFactor = lagTime/unit->m_lagTime;
counter = (int) (unit->m_counter * scaleFactor);
counter = unit->m_counter = sc_max(1, counter);
slope = unit->m_slope / scaleFactor;
unit->m_lagTime = lagTime;
}
}
if(counter >0) {
LOOP(remain,
ZXP(out) = level;
if( counter > 0) {
level += slope; --counter;
} else {
level = unit->m_in;
};
)
} else {
LOOP(remain, ZXP(out) = level );
}
unit->m_level = level;
unit->m_slope = slope;
unit->m_counter = counter;
}
void VarLag_next_1(VarLag *unit, int inNumSamples)
{
float *out = OUT(0);
float in = *IN(0);
float lagTime = ZIN0(1);
int counter = unit->m_counter;
if ( in != unit->m_in) {
counter = (int)(lagTime * SAMPLERATE);
unit->m_counter = counter = sc_max(1, counter);
unit->m_slope = ( in - unit->m_level) / counter;
unit->m_in = in;
unit->m_lagTime = lagTime;
} {
if (lagTime != unit->m_lagTime) {
if (counter != 0) {
double scaleFactor = lagTime/unit->m_lagTime;
counter = (int) (unit->m_counter * scaleFactor);
unit->m_counter = counter = sc_max(1, counter);
unit->m_slope = unit->m_slope / scaleFactor; }
unit->m_lagTime = lagTime; }
}
*out = unit->m_level;
if (unit->m_counter > 0) {
unit->m_level += unit->m_slope;
--unit->m_counter;
} else {
unit->m_level = unit->m_in;
}
}
void VarLag_Ctor(VarLag* unit)
{
if (BUFLENGTH == 1) {
SETCALC(VarLag_next_1);
} else {
SETCALC(VarLag_next);
}
float in = *IN(0);
float lagTime = ZIN0(1);
unit->m_level = ZIN0(2);
int counter = (int)(lagTime * SAMPLERATE);
unit->m_counter = counter = sc_max(1, counter);
unit->m_slope = ( in - unit->m_level) / counter;
unit->m_in = in;
unit->m_lagTime = lagTime;
ZOUT0(0) = unit->m_level;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void OnePole_next_a(OnePole *unit, int inNumSamples)
{
//printf("OnePole_next_a\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float *b1p = ZIN(1);
double y1 = unit->m_y1;
LOOP1(inNumSamples,
double y0 = ZXP(in);
double b1 = ZXP(b1p);
ZXP(out) = y1 = y0 + b1 * (y1 - y0);
);
unit->m_y1 = zapgremlins(y1);
}
void OnePole_next_k(OnePole *unit, int inNumSamples)
{
//printf("OnePole_next_a\n");
float *out = ZOUT(0);
float *in = ZIN(0);
double b1 = unit->m_b1;
unit->m_b1 = ZIN0(1);
double y1 = unit->m_y1;
if (b1 == unit->m_b1) {
if (b1 >= 0.f) {
LOOP1(inNumSamples,
double y0 = ZXP(in);
ZXP(out) = y1 = y0 + b1 * (y1 - y0);
);
} else {
LOOP1(inNumSamples,
double y0 = ZXP(in);
ZXP(out) = y1 = y0 + b1 * (y1 + y0);
);
}
} else {
double b1_slope = CALCSLOPE(unit->m_b1, b1);
if (b1 >= 0.f && unit->m_b1 >= 0) {
LOOP1(inNumSamples,
double y0 = ZXP(in);
ZXP(out) = y1 = y0 + b1 * (y1 - y0);
b1 += b1_slope;
);
} else if (b1 <= 0.f && unit->m_b1 <= 0) {
LOOP1(inNumSamples,
double y0 = ZXP(in);
ZXP(out) = y1 = y0 + b1 * (y1 + y0);
b1 += b1_slope;
);
} else {
LOOP1(inNumSamples,
double y0 = ZXP(in);
ZXP(out) = y1 = (1.f - std::abs(b1)) * y0 + b1 * y1;
b1 += b1_slope;
);
}
}
unit->m_y1 = zapgremlins(y1);
}
void OnePole_Ctor(OnePole* unit)
{
if (INRATE(1) == calc_FullRate) {
SETCALC(OnePole_next_a);
} else {
SETCALC(OnePole_next_k);
}
unit->m_b1 = 0.f;
unit->m_y1 = 0.f;
OnePole_next_a(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void OneZero_Ctor(OneZero* unit)
{
SETCALC(OneZero_next);
unit->m_b1 = 0.f;
unit->m_x1 = ZIN0(0);
OneZero_next(unit, 1);
}
void OneZero_next(OneZero* unit, int inNumSamples)
{
//printf("OneZero::next\n");
float *out = ZOUT(0);
float *in = ZIN(0);
double b1 = unit->m_b1;
unit->m_b1 = ZIN0(1);
double x1 = unit->m_x1;
if (b1 == unit->m_b1) {
if (b1 >= 0.f) {
LOOP1(inNumSamples,
double x0 = ZXP(in);
ZXP(out) = x0 + b1 * (x1 - x0);
x1 = x0;
);
} else {
LOOP1(inNumSamples,
double x0 = ZXP(in);
ZXP(out) = x0 + b1 * (x1 + x0);
x1 = x0;
);
}
} else {
double b1_slope = CALCSLOPE(unit->m_b1, b1);
if (b1 >= 0.f && unit->m_b1 >= 0) {
LOOP1(inNumSamples,
double x0 = ZXP(in);
ZXP(out) = x0 + b1 * (x1 - x0);
x1 = x0;
b1 += b1_slope;
);
} else if (b1 <= 0.f && unit->m_b1 <= 0) {
LOOP1(inNumSamples,
double x0 = ZXP(in);
ZXP(out) = x0 + b1 * (x1 + x0);
x1 = x0;
b1 += b1_slope;
);
} else {
LOOP1(inNumSamples,
double x0 = ZXP(in);
ZXP(out) = (1.f - std::abs(b1)) * x0 + b1 * x1;
x1 = x0;
b1 += b1_slope;
);
}
}
unit->m_x1 = x1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Integrator_Ctor(Integrator* unit)
{
//printf("Integrator_Reset\n");
SETCALC(Integrator_next);
unit->m_b1 = 0.f;
unit->m_y1 = 0.f;
Integrator_next(unit, 1);
}
void Integrator_next(Integrator* unit, int inNumSamples)
{
//printf("Integrator_next_a\n");
float *out = ZOUT(0);
float *in = ZIN(0);
double b1 = unit->m_b1;
unit->m_b1 = ZIN0(1);
double y1 = unit->m_y1;
if (b1 == unit->m_b1) {
if (b1 == 1.f) {
LOOP1(inNumSamples,
double y0 = ZXP(in);
ZXP(out) = y1 = y0 + y1;
);
} else if (b1 == 0.f) {
LOOP1(inNumSamples,
double y0 = ZXP(in);
ZXP(out) = y1 = y0;
);
} else {
LOOP1(inNumSamples,
double y0 = ZXP(in);
ZXP(out) = y1 = y0 + b1 * y1;
);
}
} else {
double b1_slope = CALCSLOPE(unit->m_b1, b1);
LOOP1(inNumSamples,
double y0 = ZXP(in);
ZXP(out) = y1 = y0 + b1 * y1;
b1 += b1_slope;
);
}
unit->m_y1 = zapgremlins(y1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Decay_Ctor(Decay* unit)
{
SETCALC(Decay_next);
unit->m_decayTime = 0.f;
unit->m_b1 = 0.f;
unit->m_y1 = 0.f;
Decay_next(unit, 1);
}
void Decay_next(Decay* unit, int inNumSamples)
{
//printf("Decay_next_a\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float decayTime = ZIN0(1);
double y1 = unit->m_y1;
double b1 = unit->m_b1;
if (decayTime == unit->m_decayTime) {
if (b1 == 0.f) {
LOOP1(inNumSamples,
double y0 = ZXP(in);
ZXP(out) = y1 = y0;
);
} else {
LOOP1(inNumSamples,
double y0 = ZXP(in);
ZXP(out) = y1 = y0 + b1 * y1;
);
}
} else {
unit->m_b1 = decayTime == 0.f ? 0.f : exp(log001 / (decayTime * SAMPLERATE));
unit->m_decayTime = decayTime;
double b1_slope = CALCSLOPE(unit->m_b1, b1);
//printf("decayTime %g %g %g\n", unit->m_decayTime, next_b1, b1);
LOOP1(inNumSamples,
double y0 = ZXP(in);
ZXP(out) = y1 = y0 + b1 * y1;
b1 += b1_slope;
);
}
unit->m_y1 = zapgremlins(y1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Decay2_Ctor(Decay2 *unit)
{
SETCALC(Decay2_next);
float attackTime = ZIN0(1);
float decayTime = ZIN0(2);
unit->m_b1a = decayTime == 0.f ? 0.f : exp(log001 / (decayTime * SAMPLERATE));
unit->m_b1b = attackTime == 0.f ? 0.f : exp(log001 / (attackTime * SAMPLERATE));
unit->m_decayTime = decayTime;
unit->m_attackTime = attackTime;
float y0 = ZIN0(0);
unit->m_y1a = y0;
unit->m_y1b = y0;
ZOUT0(0) = 0.f;
}
void Decay2_next(Decay2* unit, int inNumSamples)
{
//printf("Decay2_next_a\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float attackTime = ZIN0(1);
float decayTime = ZIN0(2);
double y1a = unit->m_y1a;
double y1b = unit->m_y1b;
double b1a = unit->m_b1a;
double b1b = unit->m_b1b;
if (decayTime == unit->m_decayTime && attackTime == unit->m_attackTime) {
LOOP1(inNumSamples,
double y0 = ZXP(in);
y1a = y0 + b1a * y1a;
y1b = y0 + b1b * y1b;
ZXP(out) = y1a - y1b;
);
} else {
unit->m_decayTime = decayTime;
unit->m_attackTime = attackTime;
double next_b1a = decayTime == 0.f ? 0.f : exp(log001 / (decayTime * SAMPLERATE));
double next_b1b = attackTime == 0.f ? 0.f : exp(log001 / (attackTime * SAMPLERATE));
unit->m_decayTime = decayTime;
double b1a_slope = CALCSLOPE(next_b1a, b1a);
double b1b_slope = CALCSLOPE(next_b1b, b1b);
unit->m_b1a = next_b1a;
unit->m_b1b = next_b1b;
LOOP1(inNumSamples,
double y0 = ZXP(in);
y1a = y0 + b1a * y1a;
y1b = y0 + b1b * y1b;
ZXP(out) = y1a - y1b;
b1a += b1a_slope;
b1b += b1b_slope;
);
}
unit->m_y1a = y1a;
unit->m_y1b = y1b;
unit->m_b1a = b1a;
unit->m_b1b = b1b;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void LeakDC_next_i_4(LeakDC* unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
double b1 = unit->m_b1;
double y1 = unit->m_y1;
double x1 = unit->m_x1;
LOOP1(inNumSamples/4,
double x00 = ZXP(in);
double x01 = ZXP(in);
double x02 = ZXP(in);
double x03 = ZXP(in);
float out0 = y1 = x00 - x1 + b1 * y1;
float out1 = y1 = x01 - x00 + b1 * y1;
float out2 = y1 = x02 - x01 + b1 * y1;
float out3 = y1 = x03 - x02 + b1 * y1;
ZXP(out) = out0;
ZXP(out) = out1;
ZXP(out) = out2;
ZXP(out) = out3;
x1 = x03;
);
unit->m_x1 = x1;
unit->m_y1 = zapgremlins(y1);
}
void LeakDC_next_i(LeakDC* unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
double b1 = unit->m_b1;
double y1 = unit->m_y1;
double x1 = unit->m_x1;
LOOP1(inNumSamples,
double x0 = ZXP(in);
ZXP(out) = y1 = x0 - x1 + b1 * y1;
x1 = x0;
);
unit->m_x1 = x1;
unit->m_y1 = zapgremlins(y1);
}
void LeakDC_next(LeakDC* unit, int inNumSamples)
{
if (ZIN0(1) == unit->m_b1) {
if ((inNumSamples & 3) == 0)
LeakDC_next_i_4(unit, inNumSamples);
else
LeakDC_next_i(unit, inNumSamples);
} else {
float *out = ZOUT(0);
float *in = ZIN(0);
double b1 = unit->m_b1;
unit->m_b1 = ZIN0(1);
double y1 = unit->m_y1;
double x1 = unit->m_x1;
double b1_slope = CALCSLOPE(unit->m_b1, b1);
LOOP1(inNumSamples,
double x0 = ZXP(in);
ZXP(out) = y1 = x0 - x1 + b1 * y1;
x1 = x0;
b1 += b1_slope;
);
unit->m_x1 = x1;
unit->m_y1 = zapgremlins(y1);
}
}
void LeakDC_next_1(LeakDC* unit, int inNumSamples)
{
double b1 = unit->m_b1 = ZIN0(1);
double y1 = unit->m_y1;
double x1 = unit->m_x1;
double x0 = ZIN0(0);
ZOUT0(0) = y1 = x0 - x1 + b1 * y1;
x1 = x0;
unit->m_x1 = x1;
unit->m_y1 = zapgremlins(y1);
}
void LeakDC_Ctor(LeakDC *unit)
{
//printf("LeakDC_Ctor\n");
if (BUFLENGTH == 1)
SETCALC(LeakDC_next_1);
else {
if (INRATE(1) == calc_ScalarRate) {
if ((BUFLENGTH & 3) == 0)
SETCALC(LeakDC_next_i_4);
else
SETCALC(LeakDC_next_i);
} else
SETCALC(LeakDC_next);
}
unit->m_b1 = 0.0;
unit->m_x1 = ZIN0(0);
unit->m_y1 = 0.0;
LeakDC_next_1(unit, 1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void TwoPole_Ctor(TwoPole *unit)
{
//printf("TwoPole_Reset\n");
SETCALC(TwoPole_next);
unit->m_b1 = 0.f;
unit->m_b2 = 0.f;
unit->m_y1 = 0.f;
unit->m_y2 = 0.f;
unit->m_freq = 0.f;
unit->m_reson = 0.f;
PUSH_LOOPVALS
TwoPole_next(unit, 1);
POP_LOOPVALS
}
void TwoPole_next(TwoPole* unit, int inNumSamples)
{
//printf("TwoPole_next_a\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float freq = ZIN0(1);
float reson = ZIN0(2);
double y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
if (freq != unit->m_freq || reson != unit->m_reson) {
double b1 = unit->m_b1;
double b2 = unit->m_b2;
double b1_next = 2 * reson * cos(freq * unit->mRate->mRadiansPerSample);
double b2_next = -(reson * reson);
double b1_slope = (b1_next - b1) * unit->mRate->mFilterSlope;
double b2_slope = (b2_next - b2) * unit->mRate->mFilterSlope;
LOOP(unit->mRate->mFilterLoops,
ZXP(out) = y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = y2 = ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = y1 = ZXP(in) + b1 * y2 + b2 * y0;
b1 += b1_slope;
b2 += b2_slope;
);
LOOP(unit->mRate->mFilterRemain,
ZXP(out) = y0 = ZXP(in) + b1 * y1 + b2 * y2;
y2 = y1;
y1 = y0;
);
unit->m_freq = freq;
unit->m_reson = reson;
unit->m_b1 = b1;
unit->m_b2 = b2;
} else {
double b1 = unit->m_b1;
double b2 = unit->m_b2;
LOOP(unit->mRate->mFilterLoops,
ZXP(out) = y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = y2 = ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = y1 = ZXP(in) + b1 * y2 + b2 * y0;
);
LOOP(unit->mRate->mFilterRemain,
ZXP(out) = y0 = ZXP(in) + b1 * y1 + b2 * y2;
y2 = y1;
y1 = y0;
);
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void TwoZero_Ctor(TwoZero* unit)
{
//printf("TwoZero_Reset\n");
SETCALC(TwoZero_next);
unit->m_b1 = 0.f;
unit->m_b2 = 0.f;
unit->m_x1 = 0.f;
unit->m_x2 = 0.f;
unit->m_freq = 0.f;
unit->m_reson = 0.f;
PUSH_LOOPVALS
TwoZero_next(unit, 1);
POP_LOOPVALS
}
void TwoZero_next(TwoZero* unit, int inNumSamples)
{
//printf("TwoZero_next\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float freq = ZIN0(1);
float reson = ZIN0(2);
double x0;
double x1 = unit->m_x1;
double x2 = unit->m_x2;
if (freq != unit->m_freq || reson != unit->m_reson) {
double b1 = unit->m_b1;
double b2 = unit->m_b2;
double b1_next = -2 * reson * cos(freq * unit->mRate->mRadiansPerSample);
double b2_next = (reson * reson);
double b1_slope = (b1_next - b1) * unit->mRate->mFilterSlope;
double b2_slope = (b2_next - b2) * unit->mRate->mFilterSlope;
LOOP(unit->mRate->mFilterLoops,
x0 = ZXP(in);
ZXP(out) = x0 + b1 * x1 + b2 * x2;
x2 = ZXP(in);
ZXP(out) = x2 + b1 * x0 + b2 * x1;
x1 = ZXP(in);
ZXP(out) = x1 + b1 * x2 + b2 * x0;
b1 += b1_slope;
b2 += b2_slope;
);
LOOP(unit->mRate->mFilterRemain,
x0 = ZXP(in);
ZXP(out) = x0 + b1 * x1 + b2 * x2;
x2 = x1;
x1 = x0;
);
unit->m_freq = freq;
unit->m_reson = reson;
unit->m_b1 = b1;
unit->m_b2 = b2;
} else {
double b1 = unit->m_b1;
double b2 = unit->m_b2;
LOOP(unit->mRate->mFilterLoops,
x0 = ZXP(in);
ZXP(out) = x0 + b1 * x1 + b2 * x2;
x2 = ZXP(in);
ZXP(out) = x2 + b1 * x0 + b2 * x1;
x1 = ZXP(in);
ZXP(out) = x1 + b1 * x2 + b2 * x0;
);
LOOP(unit->mRate->mFilterRemain,
x0 = ZXP(in);
ZXP(out) = x0 + b1 * x1 + b2 * x2;
x2 = x1;
x1 = x0;
);
}
unit->m_x1 = x1;
unit->m_x2 = x2;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void APF_Ctor(APF* unit)
{
//printf("APF_Reset\n");
SETCALC(APF_next);
unit->m_b1 = 0.f;
unit->m_b2 = 0.f;
unit->m_y1 = 0.f;
unit->m_y2 = 0.f;
unit->m_x1 = 0.f;
unit->m_x2 = 0.f;
unit->m_freq = 0.f;
unit->m_reson = 0.f;
PUSH_LOOPVALS
APF_next(unit, 1);
POP_LOOPVALS
}
void APF_next(APF* unit, int inNumSamples)
{
//printf("APF_next_a\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float freq = ZIN0(1);
float reson = ZIN0(2);
double x0, y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double x1 = unit->m_x1;
double x2 = unit->m_x2;
if (freq != unit->m_freq || reson != unit->m_reson) {
double b1 = unit->m_b1;
double b2 = unit->m_b2;
double b1_next = 2.f * reson * cos(freq * unit->mRate->mRadiansPerSample);
double b2_next = -(reson * reson);
double b1_slope = (b1_next - b1) * unit->mRate->mFilterSlope;
double b2_slope = (b2_next - b2) * unit->mRate->mFilterSlope;
LOOP(unit->mRate->mFilterLoops,
x0 = ZXP(in);
ZXP(out) = y0 = x0 + b1 * (y1 - x1) + b2 * (y2 - x2);
x2 = ZXP(in);
ZXP(out) = y2 = x2 + b1 * (y0 - x0) + b2 * (y2 - x1);
x1 = ZXP(in);
ZXP(out) = y1 = x1 + b1 * (y2 - x2) + b2 * (y2 - x0);
b1 += b1_slope;
b2 += b2_slope;
);
LOOP(unit->mRate->mFilterRemain,
x0 = ZXP(in);
ZXP(out) = y0 = x0 + b1 * (y1 - x1) + b2 * (y2 - x2);
y2 = y1;
y1 = y0;
x2 = x1;
x1 = x0;
);
unit->m_freq = freq;
unit->m_reson = reson;
unit->m_b1 = b1;
unit->m_b2 = b2;
} else {
double b1 = unit->m_b1;
double b2 = unit->m_b2;
LOOP(unit->mRate->mFilterLoops,
float in0 = ZXP(in);
float in1 = ZXP(in);
float in2 = ZXP(in);
x0 = in0;
float out0 = y0 = x0 + b1 * (y1 - x1) + b2 * (y2 - x2);
x2 = in1;
float out1 = y2 = x2 + b1 * (y0 - x0) + b2 * (y2 - x1);
x1 = in2;
float out2 = y1 = x1 + b1 * (y2 - x2) + b2 * (y2 - x0);
ZXP(out) = out0;
ZXP(out) = out1;
ZXP(out) = out2;
);
LOOP(unit->mRate->mFilterRemain,
x0 = ZXP(in);
ZXP(out) = y0 = x0 + b1 * (y1 - x1) + b2 * (y2 - x2);
y2 = y1;
y1 = y0;
x2 = x1;
x1 = x0;
);
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
unit->m_x1 = x1;
unit->m_x2 = x2;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void LPZ1_Ctor(LPZ1* unit)
{
//printf("LPZ1_Reset\n");
SETCALC(LPZ1_next);
unit->m_x1 = ZIN0(0);
LPZ1_next(unit, 1);
}
void LPZ1_next(LPZ1* unit, int inNumSamples)
{
//printf("LPZ1_next_a\n");
float *out = ZOUT(0);
float *in = ZIN(0);
double x0;
double x1 = unit->m_x1;
LOOP(inNumSamples >> 2,
x0 = ZXP(in);
float out0 = 0.5 * (x0 + x1);
x1 = ZXP(in);
float out1 = 0.5 * (x1 + x0);
x0 = ZXP(in);
float out2 = 0.5 * (x0 + x1);
x1 = ZXP(in);
float out3 = 0.5 * (x1 + x0);
ZXP(out) = out0;
ZXP(out) = out1;
ZXP(out) = out2;
ZXP(out) = out3;
);
LOOP(inNumSamples & 3,
x0 = ZXP(in);
ZXP(out) = 0.5 * (x0 + x1);
x1 = x0;
);
unit->m_x1 = x1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void HPZ1_Ctor(HPZ1* unit)
{
//printf("HPZ1_Reset\n");
SETCALC(HPZ1_next);
unit->m_x1 = ZIN0(0);
HPZ1_next(unit, 1);
}
void HPZ1_next(HPZ1* unit, int inNumSamples)
{
//printf("HPZ1_next\n");
float *out = ZOUT(0);
float *in = ZIN(0);
double x0;
double x1 = unit->m_x1;
LOOP(inNumSamples >> 2,
x0 = ZXP(in);
float out0 = 0.5 * (x0 - x1);
x1 = ZXP(in);
float out1 = 0.5 * (x1 - x0);
x0 = ZXP(in);
float out2 = 0.5 * (x0 - x1);
x1 = ZXP(in);
float out3 = 0.5 * (x1 - x0);
ZXP(out) = out0;
ZXP(out) = out1;
ZXP(out) = out2;
ZXP(out) = out3;
);
LOOP(inNumSamples & 3,
x0 = ZXP(in);
//printf("%d %d %g %g\n", this, inNumSamples, x0, x1);
ZXP(out) = 0.5f * (x0 - x1);
x1 = x0;
);
unit->m_x1 = x1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Slope_Ctor(Slope* unit)
{
//printf("Slope_Reset\n");
SETCALC(Slope_next);
unit->m_x1 = ZIN0(0);
//printf("Slope_Reset %g\n", unit->m_x1);
Slope_next(unit, 1);
}
void Slope_next(Slope* unit, int inNumSamples)
{
//printf("Slope_next_a %g\n", unit->m_x1);
float *out = ZOUT(0);
float *in = ZIN(0);
double x1 = unit->m_x1;
double sr = SAMPLERATE;
LOOP1(inNumSamples,
double x0 = ZXP(in);
ZXP(out) = sr * (x0 - x1);
x1 = x0;
);
unit->m_x1 = x1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Delay1_Ctor(Delay1* unit)
{
//printf("Delay1_Reset\n");
SETCALC(Delay1_next);
unit->m_x1 = ZIN0(0);
Delay1_next(unit, 1);
}
void Delay1_next(Delay1* unit, int inNumSamples)
{
//printf("Delay1_next_a\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float x0;
float x1 = unit->m_x1;
LOOP(inNumSamples >> 2,
x0 = ZXP(in);
ZXP(out) = x1;
x1 = ZXP(in);
ZXP(out) = x0;
x0 = ZXP(in);
ZXP(out) = x1;
x1 = ZXP(in);
ZXP(out) = x0;
);
LOOP(inNumSamples & 3,
x0 = ZXP(in);
ZXP(out) = x1;
x1 = x0;
);
unit->m_x1 = x1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Flip_Ctor(Flip* unit)
{
if (unit->mBufLength & 1) {
SETCALC(Flip_next_odd);
Flip_next_odd(unit, 1);
} else {
SETCALC(Flip_next_even);
ZOUT0(0) = ZIN0(0);
}
}
void Flip_next_even(Flip* unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
LOOP(inNumSamples >> 1,
ZXP(out) = -ZXP(in);
ZXP(out) = ZXP(in);
);
}
void Flip_next_odd(Flip* unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
if (unit->mWorld->mBufCounter & 1) {
ZXP(out) = ZXP(in);
LOOP(inNumSamples >> 1,
ZXP(out) = -ZXP(in);
ZXP(out) = ZXP(in);
);
} else {
LOOP(inNumSamples >> 1,
ZXP(out) = -ZXP(in);
ZXP(out) = ZXP(in);
);
ZXP(out) = -ZXP(in);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Delay2_Ctor(Delay2* unit)
{
SETCALC(Delay2_next);
unit->m_x1 = ZIN0(0);
ZOUT0(0) = 0.f;
}
void Delay2_next(Delay2* unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
double x0;
double x1 = unit->m_x1;
double x2 = unit->m_x2;
LOOP(unit->mRate->mFilterLoops,
x0 = ZXP(in);
ZXP(out) = x2;
x2 = ZXP(in);
ZXP(out) = x1;
x1 = ZXP(in);
ZXP(out) = x0;
);
LOOP(unit->mRate->mFilterRemain,
x0 = ZXP(in);
ZXP(out) = x2;
x2 = x1;
x1 = x0;
);
unit->m_x1 = x1;
unit->m_x2 = x2;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void LPZ2_Ctor(LPZ2* unit)
{
//printf("LPZ2_Reset\n");
SETCALC(LPZ2_next);
unit->m_x1 = unit->m_x2 = ZIN0(0);
PUSH_LOOPVALS
LPZ2_next(unit, 1);
POP_LOOPVALS
}
void LPZ2_next(LPZ2* unit, int inNumSamples)
{
//printf("LPZ2_next_a\n");
float *out = ZOUT(0);
float *in = ZIN(0);
double x0;
double x1 = unit->m_x1;
double x2 = unit->m_x2;
LOOP(unit->mRate->mFilterLoops,
x0 = ZXP(in);
float out0 = (x0 + 2.0 * x1 + x2) * 0.25;
x2 = ZXP(in);
float out1 = (x2 + 2.0 * x0 + x1) * 0.25;
x1 = ZXP(in);
float out2 = (x1 + 2.0 * x2 + x0) * 0.25;
ZXP(out) = out0;
ZXP(out) = out1;
ZXP(out) = out2;
);
LOOP(unit->mRate->mFilterRemain,
x0 = ZXP(in);
ZXP(out) = (x0 + 2. * x1 + x2) * 0.25;
x2 = x1;
x1 = x0;
);
unit->m_x1 = x1;
unit->m_x2 = x2;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void HPZ2_Ctor(HPZ2* unit)
{
//printf("HPZ2_Reset\n");
SETCALC(HPZ2_next);
unit->m_x1 = unit->m_x2 = ZIN0(0);
PUSH_LOOPVALS
HPZ2_next(unit, 1);
POP_LOOPVALS
}
void HPZ2_next(HPZ2* unit, int inNumSamples)
{
//printf("HPZ2_next_a\n");
float *out = ZOUT(0);
float *in = ZIN(0);
double x0;
double x1 = unit->m_x1;
double x2 = unit->m_x2;
LOOP(unit->mRate->mFilterLoops,
x0 = ZXP(in);
float out0 = (x0 - 2.0 * x1 + x2) * 0.25;
x2 = ZXP(in);
float out1 = (x2 - 2.0 * x0 + x1) * 0.25;
x1 = ZXP(in);
float out2 = (x1 - 2.0 * x2 + x0) * 0.25;
ZXP(out) = out0;
ZXP(out) = out1;
ZXP(out) = out2;
);
LOOP(unit->mRate->mFilterRemain,
x0 = ZXP(in);
ZXP(out) = (x0 - 2.0 * x1 + x2) * 0.25;
x2 = x1;
x1 = x0;
);
unit->m_x1 = x1;
unit->m_x2 = x2;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void BPZ2_Ctor(BPZ2* unit)
{
//printf("BPZ2_Reset\n");
SETCALC(BPZ2_next);
unit->m_x1 = unit->m_x2 = ZIN0(0);
PUSH_LOOPVALS
BPZ2_next(unit, 1);
POP_LOOPVALS
}
void BPZ2_next(BPZ2* unit, int inNumSamples)
{
//printf("BPZ2_next_a\n");
float *out = ZOUT(0);
float *in = ZIN(0);
double x0;
double x1 = unit->m_x1;
double x2 = unit->m_x2;
LOOP(unit->mRate->mFilterLoops,
x0 = ZXP(in);
ZXP(out) = (x0 - x2) * 0.5;
x2 = ZXP(in);
ZXP(out) = (x2 - x1) * 0.5;
x1 = ZXP(in);
ZXP(out) = (x1 - x0) * 0.5;
);
LOOP(unit->mRate->mFilterRemain,
x0 = ZXP(in);
ZXP(out) = (x0 - x2) * 0.5;
x2 = x1;
x1 = x0;
);
unit->m_x1 = x1;
unit->m_x2 = x2;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void BRZ2_Ctor(BRZ2* unit)
{
//printf("BRZ2_Reset\n");
SETCALC(BRZ2_next);
unit->m_x1 = unit->m_x2 = ZIN0(0);
PUSH_LOOPVALS
BRZ2_next(unit, 1);
POP_LOOPVALS
}
void BRZ2_next(BRZ2* unit, int inNumSamples)
{
//printf("BRZ2_next_a\n");
float *out = ZOUT(0);
float *in = ZIN(0);
double x0;
double x1 = unit->m_x1;
double x2 = unit->m_x2;
LOOP(unit->mRate->mFilterLoops,
x0 = ZXP(in);
ZXP(out) = (x0 + x2) * 0.5;
x2 = ZXP(in);
ZXP(out) = (x2 + x1) * 0.5;
x1 = ZXP(in);
ZXP(out) = (x1 + x0) * 0.5;
);
LOOP(unit->mRate->mFilterRemain,
x0 = ZXP(in);
ZXP(out) = (x0 + x2) * 0.5;
x2 = x1;
x1 = x0;
);
unit->m_x1 = x1;
unit->m_x2 = x2;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Slew_Ctor(Slew* unit)
{
//printf("Slew_Reset\n");
SETCALC(Slew_next);
unit->mLevel = ZIN0(0);
Slew_next(unit, 1);
}
void Slew_next(Slew* unit, int inNumSamples)
{
//printf("Slew_next_a\n");
float sampleDur = unit->mRate->mSampleDur;
float *out = ZOUT(0);
float *in = ZIN(0);
float upf = ZIN0(1) * sampleDur;
float dnf = 0.f - ZIN0(2) * sampleDur;
double level = unit->mLevel;
LOOP1(inNumSamples,
double slope = ZXP(in) - level;
level += sc_clip(slope, dnf, upf);
ZXP(out) = level;
);
unit->mLevel = level;
}
/*void Slew_next_1(Slew* unit, int inNumSamples)
{
//printf("Slew_next_a\n");
float *out = ZOUT(0);
float in = ZIN0(0);
float upf = ZIN0(1);
float dnf = ZIN0(2);
float level = unit->mLevel;
float slope = ZXP(in) - level;
if (slope > upf) slope = upf;
else if (slope < dnf) slope = dnf;
level += slope;
ZXP(out) = level;
unit->mLevel = level;
}*/
////////////////////////////////////////////////////////////////////////////////////////////////////////
void RLPF_Ctor(RLPF* unit)
{
//printf("RLPF_Reset\n");
if (unit->mBufLength == 1) {
SETCALC(RLPF_next_1);
} else {
SETCALC(RLPF_next);
}
unit->m_a0 = 0.f;
unit->m_b1 = 0.f;
unit->m_b2 = 0.f;
unit->m_y1 = 0.f;
unit->m_y2 = 0.f;
unit->m_freq = 0.f;
unit->m_reson = 0.f;
RLPF_next_1(unit, 1);
}
void RLPF_next(RLPF* unit, int inNumSamples)
{
//printf("RLPF_next\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float freq = ZIN0(1);
float reson = ZIN0(2);
double y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = unit->m_a0;
double b1 = unit->m_b1;
double b2 = unit->m_b2;
if (freq != unit->m_freq || reson != unit->m_reson) {
double qres = sc_max(0.001, reson);
double pfreq = freq * unit->mRate->mRadiansPerSample;
double D = tan(pfreq * qres * 0.5);
double C = ((1.0-D)/(1.0+D));
double cosf = cos(pfreq);
double next_b1 = (1.0 + C) * cosf;
double next_b2 = -C;
double next_a0 = (1.0 + C - next_b1) * .25;
//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);
double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
LOOP(unit->mRate->mFilterLoops,
y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = y0 + 2.0 * y1 + y2;
y2 = a0 * ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = y2 + 2.0 * y0 + y1;
y1 = a0 * ZXP(in) + b1 * y2 + b2 * y0;
ZXP(out) = y1 + 2.0 * y2 + y0;
a0 += a0_slope;
b1 += b1_slope;
b2 += b2_slope;
);
LOOP(unit->mRate->mFilterRemain,
y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = y0 + 2.0 * y1 + y2;
y2 = y1;
y1 = y0;
);
unit->m_freq = freq;
unit->m_reson = reson;
unit->m_a0 = a0;
unit->m_b1 = b1;
unit->m_b2 = b2;
} else {
LOOP(unit->mRate->mFilterLoops,
y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = y0 + 2.0 * y1 + y2;
y2 = a0 * ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = y2 + 2.0 * y0 + y1;
y1 = a0 * ZXP(in) + b1 * y2 + b2 * y0;
ZXP(out) = y1 + 2.0 * y2 + y0;
);
LOOP(unit->mRate->mFilterRemain,
y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = y0 + 2.0 * y1 + y2;
y2 = y1;
y1 = y0;
);
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
void RLPF_next_1(RLPF* unit, int inNumSamples)
{
//printf("RLPF_next_1\n");
float in = ZIN0(0);
float freq = ZIN0(1);
float reson = ZIN0(2);
double y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = unit->m_a0;
double b1 = unit->m_b1;
double b2 = unit->m_b2;
if (freq != unit->m_freq || reson != unit->m_reson) {
double qres = sc_max(0.001f, reson);
double pfreq = freq * unit->mRate->mRadiansPerSample;
double D = tan(pfreq * qres * 0.5f);
double C = ((1.f-D)/(1.f+D));
double cosf = cos(pfreq);
b1 = (1.0 + C) * cosf;
b2 = -C;
a0 = (1.0 + C - b1) * .25;
y0 = a0 * in + b1 * y1 + b2 * y2;
ZOUT0(0) = y0 + 2.0 * y1 + y2;
y2 = y1;
y1 = y0;
unit->m_freq = freq;
unit->m_reson = reson;
unit->m_a0 = a0;
unit->m_b1 = b1;
unit->m_b2 = b2;
} else {
y0 = a0 * in + b1 * y1 + b2 * y2;
ZOUT0(0) = y0 + 2.0 * y1 + y2;
y2 = y1;
y1 = y0;
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void RHPF_Ctor(RHPF* unit)
{
if (unit->mBufLength == 1)
SETCALC(RHPF_next_1);
else
SETCALC(RHPF_next);
unit->m_a0 = 0.;
unit->m_b1 = 0.;
unit->m_b2 = 0.;
unit->m_y1 = 0.;
unit->m_y2 = 0.;
unit->m_freq = 0.f;
unit->m_reson = 0.f;
RHPF_next_1(unit, 1);
}
void RHPF_next(RHPF* unit, int inNumSamples)
{
//printf("RHPFs_next\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float freq = ZIN0(1);
float reson = ZIN0(2);
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = unit->m_a0;
double b1 = unit->m_b1;
double b2 = unit->m_b2;
if (freq != unit->m_freq || reson != unit->m_reson) {
double qres = sc_max(0.001f, reson);
double pfreq = freq * unit->mRate->mRadiansPerSample;
double D = tan(pfreq * qres * 0.5f);
double C = ((1.0-D)/(1.0+D));
double cosf = cos(pfreq);
double next_b1 = (1.0 + C) * cosf;
double next_b2 = -C;
double next_a0 = (1.0 + C + next_b1) * .25;
//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);
double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
LOOP(unit->mRate->mFilterLoops,
double y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = y0 - 2.0 * y1 + y2;
y2 = a0 * ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = y2 - 2.0 * y0 + y1;
y1 = a0 * ZXP(in) + b1 * y2 + b2 * y0;
ZXP(out) = y1 - 2.0 * y2 + y0;
a0 += a0_slope;
b1 += b1_slope;
b2 += b2_slope;
);
LOOP(unit->mRate->mFilterRemain,
double y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = y0 - 2.0 * y1 + y2;
y2 = y1;
y1 = y0;
);
unit->m_freq = freq;
unit->m_reson = reson;
unit->m_a0 = a0;
unit->m_b1 = b1;
unit->m_b2 = b2;
} else {
LOOP(unit->mRate->mFilterLoops,
double y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = y0 - 2.0 * y1 + y2;
y2 = a0 * ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = y2 - 2.0 * y0 + y1;
y1 = a0 * ZXP(in) + b1 * y2 + b2 * y0;
ZXP(out) = y1 - 2.0 * y2 + y0;
);
LOOP(unit->mRate->mFilterRemain,
double y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = y0 - 2.0 * y1 + y2;
y2 = y1;
y1 = y0;
);
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
void RHPF_next_1(RHPF* unit, int inNumSamples)
{
//printf("RHPFs_next_1\n");
float in = ZIN0(0);
float freq = ZIN0(1);
float reson = ZIN0(2);
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = unit->m_a0;
double b1 = unit->m_b1;
double b2 = unit->m_b2;
if (freq != unit->m_freq || reson != unit->m_reson) {
double qres = sc_max(0.001f, reson);
double pfreq = freq * unit->mRate->mRadiansPerSample;
double D = tan(pfreq * qres * 0.5f);
double C = ((1.-D)/(1.+D));
double cosf = cos(pfreq);
b1 = (1. + C) * cosf;
b2 = -C;
a0 = (1. + C + b1) * .25;
double y0 = a0 * in + b1 * y1 + b2 * y2;
ZOUT0(0) = y0 - 2.0 * y1 + y2;
y2 = y1;
y1 = y0;
unit->m_freq = freq;
unit->m_reson = reson;
unit->m_a0 = a0;
unit->m_b1 = b1;
unit->m_b2 = b2;
} else {
double y0 = a0 * in + b1 * y1 + b2 * y2;
ZOUT0(0) = y0 - 2.0 * y1 + y2;
y2 = y1;
y1 = y0;
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void LPF_Ctor(LPF* unit)
{
if (unit->mBufLength == 1)
SETCALC(LPF_next_1);
else
SETCALC(LPF_next);
unit->m_a0 = 0.f;
unit->m_b1 = 0.f;
unit->m_b2 = 0.f;
unit->m_y1 = 0.f;
unit->m_y2 = 0.f;
unit->m_freq = 0.f;
LPF_next_1(unit, 1);
}
void LPF_next(LPF* unit, int inNumSamples)
{
//printf("LPF_next\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float freq = ZIN0(1);
double y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = unit->m_a0;
double b1 = unit->m_b1;
double b2 = unit->m_b2;
if (freq != unit->m_freq) {
double pfreq = freq * unit->mRate->mRadiansPerSample * 0.5;
double C = 1.f / tan(pfreq);
double C2 = C * C;
double sqrt2C = C * sqrt2_f;
double next_a0 = 1.f / (1.f + sqrt2C + C2);
double next_b1 = -2.f * (1.f - C2) * next_a0 ;
double next_b2 = -(1.f - sqrt2C + C2) * next_a0;
//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);
double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
LOOP(unit->mRate->mFilterLoops,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 + 2. * y1 + y2);
y2 = ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = a0 * (y2 + 2. * y0 + y1);
y1 = ZXP(in) + b1 * y2 + b2 * y0;
ZXP(out) = a0 * (y1 + 2.f * y2 + y0);
a0 += a0_slope;
b1 += b1_slope;
b2 += b2_slope;
);
LOOP(unit->mRate->mFilterRemain,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 + 2. * y1 + y2);
y2 = y1;
y1 = y0;
);
unit->m_freq = freq;
unit->m_a0 = a0;
unit->m_b1 = b1;
unit->m_b2 = b2;
} else {
LOOP(unit->mRate->mFilterLoops,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
float out0 = a0 * (y0 + 2. * y1 + y2);
y2 = ZXP(in) + b1 * y0 + b2 * y1;
float out1 = a0 * (y2 + 2. * y0 + y1);
y1 = ZXP(in) + b1 * y2 + b2 * y0;
float out2 = a0 * (y1 + 2. * y2 + y0);
ZXP(out) = out0;
ZXP(out) = out1;
ZXP(out) = out2;
);
LOOP(unit->mRate->mFilterRemain,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 + 2. * y1 + y2);
y2 = y1;
y1 = y0;
);
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
void LPF_next_1(LPF* unit, int inNumSamples)
{
//printf("LPF_next\n");
float in = ZIN0(0);
float freq = ZIN0(1);
double y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = unit->m_a0;
double b1 = unit->m_b1;
double b2 = unit->m_b2;
if (freq != unit->m_freq) {
double pfreq = freq * unit->mRate->mRadiansPerSample * 0.5;
double C = 1.f / tan(pfreq);
double C2 = C * C;
double sqrt2C = C * sqrt2_f;
a0 = 1.f / (1.f + sqrt2C + C2);
b1 = -2.f * (1.f - C2) * a0 ;
b2 = -(1.f - sqrt2C + C2) * a0;
y0 = in + b1 * y1 + b2 * y2;
ZOUT0(0) = a0 * (y0 + 2. * y1 + y2);
y2 = y1;
y1 = y0;
unit->m_freq = freq;
unit->m_a0 = a0;
unit->m_b1 = b1;
unit->m_b2 = b2;
} else {
y0 = in + b1 * y1 + b2 * y2;
ZOUT0(0) = a0 * (y0 + 2. * y1 + y2);
y2 = y1;
y1 = y0;
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void HPF_Ctor(HPF* unit)
{
if (unit->mBufLength == 1)
SETCALC(HPF_next_1);
else
SETCALC(HPF_next);
unit->m_a0 = 0.;
unit->m_b1 = 0.;
unit->m_b2 = 0.;
unit->m_y1 = 0.;
unit->m_y2 = 0.;
unit->m_freq = -1e6f;
HPF_next_1(unit, 1);
}
void HPF_next(HPF* unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
float freq = ZIN0(1);
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = unit->m_a0;
double b1 = unit->m_b1;
double b2 = unit->m_b2;
if (freq != unit->m_freq) {
double pfreq = freq * unit->mRate->mRadiansPerSample * 0.5;
double C = tan(pfreq);
double C2 = C * C;
double sqrt2C = C * sqrt2_f;
double next_a0 = 1. / (1. + sqrt2C + C2);
double next_b1 = 2. * (1. - C2) * next_a0 ;
double next_b2 = -(1. - sqrt2C + C2) * next_a0;
//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);
double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
LOOP(unit->mRate->mFilterLoops,
double y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - 2. * y1 + y2);
y2 = ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = a0 * (y2 - 2. * y0 + y1);
y1 = ZXP(in) + b1 * y2 + b2 * y0;
ZXP(out) = a0 * (y1 - 2. * y2 + y0);
a0 += a0_slope;
b1 += b1_slope;
b2 += b2_slope;
);
LOOP(unit->mRate->mFilterRemain,
double y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - 2. * y1 + y2);
y2 = y1;
y1 = y0;
);
unit->m_freq = freq;
unit->m_a0 = a0;
unit->m_b1 = b1;
unit->m_b2 = b2;
} else {
LOOP(unit->mRate->mFilterLoops,
double y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - 2. * y1 + y2);
y2 = ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = a0 * (y2 - 2. * y0 + y1);
y1 = ZXP(in) + b1 * y2 + b2 * y0;
ZXP(out) = a0 * (y1 - 2. * y2 + y0);
);
LOOP(unit->mRate->mFilterRemain,
double y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - 2. * y1 + y2);
y2 = y1;
y1 = y0;
);
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
void HPF_next_1(HPF* unit, int inNumSamples)
{
double in = ZIN0(0);
double freq = ZIN0(1);
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = unit->m_a0;
double b1 = unit->m_b1;
double b2 = unit->m_b2;
if (freq != unit->m_freq) {
double pfreq = freq * unit->mRate->mRadiansPerSample * 0.5f;
double C = tan(pfreq);
double C2 = C * C;
double sqrt2C = C * sqrt2_f;
a0 = 1. / (1. + sqrt2C + C2);
b1 = 2. * (1. - C2) * a0 ;
b2 = -(1. - sqrt2C + C2) * a0;
double y0 = in + b1 * y1 + b2 * y2;
ZOUT0(0) = a0 * (y0 - 2. * y1 + y2);
y2 = y1;
y1 = y0;
unit->m_freq = freq;
unit->m_a0 = a0;
unit->m_b1 = b1;
unit->m_b2 = b2;
} else {
double y0 = in + b1 * y1 + b2 * y2;
ZOUT0(0) = a0 * (y0 - 2. * y1 + y2);
y2 = y1;
y1 = y0;
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void BPF_Ctor(BPF* unit)
{
//printf("BPF_Reset\n");
if (unit->mBufLength == 1) {
SETCALC(BPF_next_1);
} else {
SETCALC(BPF_next);
};
unit->m_a0 = 0.f;
unit->m_b1 = 0.f;
unit->m_b2 = 0.f;
unit->m_y1 = 0.f;
unit->m_y2 = 0.f;
unit->m_freq = 0.f;
unit->m_bw = 0.f;
BPF_next_1(unit, 1);
}
void BPF_next(BPF* unit, int inNumSamples)
{
//printf("BPF_next\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float freq = ZIN0(1);
float bw = ZIN0(2);
double y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = unit->m_a0;
double b1 = unit->m_b1;
double b2 = unit->m_b2;
if (freq != unit->m_freq || bw != unit->m_bw) {
double pfreq = freq * unit->mRate->mRadiansPerSample;
double pbw = bw * pfreq * 0.5f;
double C = 1.f / tan(pbw);
double D = 2.f * cos(pfreq);
double next_a0 = 1.f / (1.f + C);
double next_b1 = C * D * next_a0 ;
double next_b2 = (1.f - C) * next_a0;
double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
LOOP(unit->mRate->mFilterLoops,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - y2);
y2 = ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = a0 * (y2 - y1);
y1 = ZXP(in) + b1 * y2 + b2 * y0;
ZXP(out) = a0 * (y1 - y0);
a0 += a0_slope;
b1 += b1_slope;
b2 += b2_slope;
);
LOOP(unit->mRate->mFilterRemain,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - y2);
y2 = y1;
y1 = y0;
);
unit->m_freq = freq;
unit->m_bw = bw;
unit->m_a0 = a0;
unit->m_b1 = b1;
unit->m_b2 = b2;
} else {
LOOP(unit->mRate->mFilterLoops,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - y2);
y2 = ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = a0 * (y2 - y1);
y1 = ZXP(in) + b1 * y2 + b2 * y0;
ZXP(out) = a0 * (y1 - y0);
);
LOOP(unit->mRate->mFilterRemain,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - y2);
y2 = y1;
y1 = y0;
);
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
void BPF_next_1(BPF* unit, int inNumSamples)
{
//printf("BPF_next_1\n");
float in = ZIN0(0);
float freq = ZIN0(1);
float bw = ZIN0(2);
double y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = unit->m_a0;
double b1 = unit->m_b1;
double b2 = unit->m_b2;
if (freq != unit->m_freq || bw != unit->m_bw) {
double pfreq = freq * unit->mRate->mRadiansPerSample;
double pbw = bw * pfreq * 0.5;
double C = 1.f / tan(pbw);
double D = 2.f * cos(pfreq);
double a0 = 1.f / (1.f + C);
double b1 = C * D * a0 ;
double b2 = (1.f - C) * a0;
y0 = in + b1 * y1 + b2 * y2;
ZOUT0(0) = a0 * (y0 - y2);
y2 = y1;
y1 = y0;
unit->m_freq = freq;
unit->m_bw = bw;
unit->m_a0 = a0;
unit->m_b1 = b1;
unit->m_b2 = b2;
} else {
y0 = in + b1 * y1 + b2 * y2;
ZOUT0(0) = a0 * (y0 - y2);
y2 = y1;
y1 = y0;
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void BRF_Ctor(BRF* unit)
{
//printf("BRF_Reset\n");
if (unit->mBufLength == 1) {
SETCALC(BRF_next_1);
} else {
SETCALC(BRF_next);
};
unit->m_a0 = 0.f;
unit->m_a1 = 0.f;
unit->m_b2 = 0.f;
unit->m_y1 = 0.f;
unit->m_y2 = 0.f;
unit->m_freq = 0.f;
unit->m_bw = 0.f;
BRF_next_1(unit, 1);
}
void BRF_next(BRF* unit, int inNumSamples)
{
//printf("BRF_next\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float freq = ZIN0(1);
float bw = ZIN0(2);
double ay;
double y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = unit->m_a0;
double a1 = unit->m_a1;
double b2 = unit->m_b2;
if (freq != unit->m_freq || bw != unit->m_bw) {
double pfreq = freq * unit->mRate->mRadiansPerSample;
double pbw = bw * pfreq * 0.5f;
double C = tan(pbw);
double D = 2.f * cos(pfreq);
double next_a0 = 1.f / (1.f + C);
double next_a1 = -D * next_a0;
double next_b2 = (1.f - C) * next_a0;
double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
double a1_slope = (next_a1 - a1) * unit->mRate->mFilterSlope;
double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
LOOP(unit->mRate->mFilterLoops,
ay = a1 * y1;
y0 = ZXP(in) - ay - b2 * y2;
ZXP(out) = a0 * (y0 + y2) + ay;
ay = a1 * y0;
y2 = ZXP(in) - ay - b2 * y1;
ZXP(out) = a0 * (y2 + y1) + ay;
ay = a1 * y2;
y1 = ZXP(in) - ay - b2 * y0;
ZXP(out) = a0 * (y1 + y0) + ay;
a0 += a0_slope;
a1 += a1_slope;
b2 += b2_slope;
);
LOOP(unit->mRate->mFilterRemain,
ay = a1 * y1;
y0 = ZXP(in) - ay - b2 * y2;
ZXP(out) = a0 * (y0 + y2) + ay;
y2 = y1;
y1 = y0;
);
unit->m_freq = freq;
unit->m_bw = bw;
unit->m_a0 = a0;
unit->m_a1 = a1;
unit->m_b2 = b2;
} else {
LOOP(unit->mRate->mFilterLoops,
ay = a1 * y1;
y0 = ZXP(in) - ay - b2 * y2;
ZXP(out) = a0 * (y0 + y2) + ay;
ay = a1 * y0;
y2 = ZXP(in) - ay - b2 * y1;
ZXP(out) = a0 * (y2 + y1) + ay;
ay = a1 * y2;
y1 = ZXP(in) - ay - b2 * y0;
ZXP(out) = a0 * (y1 + y0) + ay;
);
LOOP(unit->mRate->mFilterRemain,
ay = a1 * y1;
y0 = ZXP(in) - ay - b2 * y2;
ZXP(out) = a0 * (y0 + y2) + ay;
y2 = y1;
y1 = y0;
);
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
void BRF_next_1(BRF* unit, int inNumSamples)
{
//printf("BRF_next_1\n");
float in = ZIN0(0);
float freq = ZIN0(1);
float bw = ZIN0(2);
double ay;
double y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = unit->m_a0;
double a1 = unit->m_a1;
double b2 = unit->m_b2;
if (freq != unit->m_freq || bw != unit->m_bw) {
double pfreq = freq * unit->mRate->mRadiansPerSample;
double pbw = bw * pfreq * 0.5f;
double C = tan(pbw);
double D = 2.f * cos(pfreq);
double a0 = 1.f / (1.f + C);
double a1 = -D * a0;
double b2 = (1.f - C) * a0;
ay = a1 * y1;
y0 = in - ay - b2 * y2;
ZOUT0(0) = a0 * (y0 + y2) + ay;
y2 = y1;
y1 = y0;
unit->m_freq = freq;
unit->m_bw = bw;
unit->m_a0 = a0;
unit->m_a1 = a1;
unit->m_b2 = b2;
} else {
ay = a1 * y1;
y0 = in - ay - b2 * y2;
ZOUT0(0) = a0 * (y0 + y2) + ay;
y2 = y1;
y1 = y0;
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void MidEQ_Ctor(MidEQ* unit)
{
//printf("MidEQ_Reset\n");
SETCALC(MidEQ_next);
unit->m_a0 = 0.f;
unit->m_b1 = 0.f;
unit->m_b2 = 0.f;
unit->m_y1 = 0.f;
unit->m_y2 = 0.f;
unit->m_freq = 0.f;
unit->m_bw = 0.f;
unit->m_db = 0.f;
PUSH_LOOPVALS
MidEQ_next(unit, 1);
POP_LOOPVALS
}
void MidEQ_next(MidEQ* unit, int inNumSamples)
{
//printf("MidEQ_next\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float freq = ZIN0(1);
float bw = ZIN0(2);
float db = ZIN0(3);
double y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = unit->m_a0;
double b1 = unit->m_b1;
double b2 = unit->m_b2;
if (freq != unit->m_freq || bw != unit->m_bw || db != unit->m_db) {
double amp = sc_dbamp(db) - 1.0f;
double pfreq = freq * unit->mRate->mRadiansPerSample;
double pbw = bw * pfreq * 0.5f;
double C = 1.f / tan(pbw);
double D = 2.f * cos(pfreq);
double next_a0 = 1.f / (1.f + C);
double next_b1 = C * D * next_a0 ;
double next_b2 = (1.f - C) * next_a0;
next_a0 *= amp;
double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
double zin;
LOOP(unit->mRate->mFilterLoops,
zin = ZXP(in);
y0 = zin + b1 * y1 + b2 * y2;
ZXP(out) = zin + a0 * (y0 - y2);
zin = ZXP(in);
y2 = zin + b1 * y0 + b2 * y1;
ZXP(out) = zin + a0 * (y2 - y1);
zin = ZXP(in);
y1 = zin + b1 * y2 + b2 * y0;
ZXP(out) = zin + a0 * (y1 - y0);
a0 += a0_slope;
b1 += b1_slope;
b2 += b2_slope;
);
LOOP(unit->mRate->mFilterRemain,
zin = ZXP(in);
y0 = zin + b1 * y1 + b2 * y2;
ZXP(out) = zin + a0 * (y0 - y2);
y2 = y1;
y1 = y0;
);
unit->m_freq = freq;
unit->m_bw = bw;
unit->m_db = db;
unit->m_a0 = a0;
unit->m_b1 = b1;
unit->m_b2 = b2;
} else {
double zin;
LOOP(unit->mRate->mFilterLoops,
zin = ZXP(in);
y0 = zin + b1 * y1 + b2 * y2;
ZXP(out) = zin + a0 * (y0 - y2);
zin = ZXP(in);
y2 = zin + b1 * y0 + b2 * y1;
ZXP(out) = zin + a0 * (y2 - y1);
zin = ZXP(in);
y1 = zin + b1 * y2 + b2 * y0;
ZXP(out) = zin + a0 * (y1 - y0);
);
LOOP(unit->mRate->mFilterRemain,
zin = ZXP(in);
y0 = zin + b1 * y1 + b2 * y2;
ZXP(out) = zin + a0 * (y0 - y2);
y2 = y1;
y1 = y0;
);
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
static void Median_InitMedian(Median* unit, long size, float value);
static float Median_InsertMedian(Median* unit, float value);
void Median_Ctor(Median* unit)
{
//printf("Median_Reset\n");
SETCALC(Median_next);
float in = ZIN0(1);
unit->m_medianSize = sc_clip((int)ZIN0(0), 0, kMAXMEDIANSIZE);
Median_InitMedian(unit, unit->m_medianSize, in);
ZOUT0(0) = Median_InsertMedian(unit, in);
}
float Median_InsertMedian(Median* unit, float value)
{
long pos=-1;
// keeps a sorted list of the previous n=size values
// the oldest is removed and the newest is inserted.
// values between the oldest and the newest are shifted over by one.
// values and ages are both arrays that are 'size' long.
// the median value is always values[size>>1]
long last = unit->m_medianSize - 1;
// find oldest bin and age the other bins.
for (int i=0; i<unit->m_medianSize; ++i) {
if (unit->m_medianAge[i] == last) { // is it the oldest bin ?
pos = i;
} else {
unit->m_medianAge[i]++; // age the bin
}
}
// move values to fill in place of the oldest and make a space for the newest
// search lower if value is too small for the open space
while (pos != 0 && value < unit->m_medianValue[pos-1]) {
unit->m_medianValue[pos] = unit->m_medianValue[pos-1];
unit->m_medianAge[pos] = unit->m_medianAge[pos-1];
pos--;
}
// search higher if value is too big for the open space
while (pos != last && value > unit->m_medianValue[pos+1]) {
unit->m_medianValue[pos] = unit->m_medianValue[pos+1];
unit->m_medianAge[pos] = unit->m_medianAge[pos+1];
pos++;
}
unit->m_medianValue[pos] = value;
unit->m_medianAge[pos] = 0; // this is the newest bin, age = 0
return unit->m_medianValue[unit->m_medianSize>>1];
}
void Median_InitMedian(Median* unit, long size, float value)
{
// initialize the arrays with the first value
unit->m_medianSize = size;
for (int i=0; i<size; ++i) {
unit->m_medianValue[i] = value;
unit->m_medianAge[i] = i;
}
}
void Median_next(Median* unit, int inNumSamples)
{
//printf("Median_next_a\n");
float *out = ZOUT(0);
float *in = ZIN(1);
LOOP1(inNumSamples,
ZXP(out) = Median_InsertMedian(unit, ZXP(in));
);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Resonz_Ctor(Resonz* unit)
{
//printf("Resonz_Reset\n");
SETCALC(Resonz_next);
unit->m_a0 = 0.f;
unit->m_b1 = 0.f;
unit->m_b2 = 0.f;
unit->m_y1 = 0.f;
unit->m_y2 = 0.f;
unit->m_freq = 0.f;
unit->m_rq = 0.f;
PUSH_LOOPVALS
Resonz_next(unit, 1);
POP_LOOPVALS
}
void Resonz_next(Resonz* unit, int inNumSamples)
{
//printf("Resonz_next\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float freq = ZIN0(1);
float rq = ZIN0(2);
double y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = unit->m_a0;
double b1 = unit->m_b1;
double b2 = unit->m_b2;
if (freq != unit->m_freq || rq != unit->m_rq) {
double ffreq = freq * unit->mRate->mRadiansPerSample;
double B = ffreq * rq;
double R = 1.f - B * 0.5f;
double twoR = 2.f * R;
double R2 = R * R;
double cost = (twoR * cos(ffreq)) / (1.f + R2);
double b1_next = twoR * cost;
double b2_next = -R2;
double a0_next = (1.f - R2) * 0.5f;
double a0_slope = (a0_next - a0) * unit->mRate->mFilterSlope;
double b1_slope = (b1_next - b1) * unit->mRate->mFilterSlope;
double b2_slope = (b2_next - b2) * unit->mRate->mFilterSlope;
LOOP(unit->mRate->mFilterLoops,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - y2);
y2 = ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = a0 * (y2 - y1);
y1 = ZXP(in) + b1 * y2 + b2 * y0;
ZXP(out) = a0 * (y1 - y0);
a0 += a0_slope;
b1 += b1_slope;
b2 += b2_slope;
);
LOOP(unit->mRate->mFilterRemain,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - y2);
y2 = y1;
y1 = y0;
);
unit->m_freq = freq;
unit->m_rq = rq;
unit->m_a0 = a0_next;
unit->m_b1 = b1_next;
unit->m_b2 = b2_next;
} else {
LOOP(unit->mRate->mFilterLoops,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - y2);
y2 = ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = a0 * (y2 - y1);
y1 = ZXP(in) + b1 * y2 + b2 * y0;
ZXP(out) = a0 * (y1 - y0);
);
LOOP(unit->mRate->mFilterRemain,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - y2);
y2 = y1;
y1 = y0;
);
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Ringz_Ctor(Ringz* unit)
{
//printf("Ringz_ctor\n");
SETCALC(Ringz_next);
unit->m_b1 = 0.f;
unit->m_b2 = 0.f;
unit->m_y1 = 0.f;
unit->m_y2 = 0.f;
unit->m_freq = 0.f;
unit->m_decayTime = 0.f;
PUSH_LOOPVALS
Ringz_next(unit, 1);
POP_LOOPVALS
}
void Ringz_next(Ringz* unit, int inNumSamples)
{
//printf("Ringz_next\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float freq = ZIN0(1);
float decayTime = ZIN0(2);
double y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = 0.5f;
double b1 = unit->m_b1;
double b2 = unit->m_b2;
if (freq != unit->m_freq || decayTime != unit->m_decayTime) {
double ffreq = freq * unit->mRate->mRadiansPerSample;
double R = decayTime == 0.f ? 0.f : exp(log001/(decayTime * SAMPLERATE));
double twoR = 2.f * R;
double R2 = R * R;
double cost = (twoR * cos(ffreq)) / (1.f + R2);
double b1_next = twoR * cost;
double b2_next = -R2;
double b1_slope = (b1_next - b1) * unit->mRate->mFilterSlope;
double b2_slope = (b2_next - b2) * unit->mRate->mFilterSlope;
LOOP(unit->mRate->mFilterLoops,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - y2);
y2 = ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = a0 * (y2 - y1);
y1 = ZXP(in) + b1 * y2 + b2 * y0;
ZXP(out) = a0 * (y1 - y0);
b1 += b1_slope;
b2 += b2_slope;
);
LOOP(unit->mRate->mFilterRemain,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - y2);
y2 = y1;
y1 = y0;
);
unit->m_freq = freq;
unit->m_decayTime = decayTime;
unit->m_b1 = b1_next;
unit->m_b2 = b2_next;
} else {
LOOP(unit->mRate->mFilterLoops,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - y2);
y2 = ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = a0 * (y2 - y1);
y1 = ZXP(in) + b1 * y2 + b2 * y0;
ZXP(out) = a0 * (y1 - y0);
);
LOOP(unit->mRate->mFilterRemain,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * (y0 - y2);
y2 = y1;
y1 = y0;
);
}
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Formlet_Ctor(Formlet* unit)
{
//printf("Formlet_Reset\n");
if (unit->mBufLength == 1) {
SETCALC(Formlet_next_1);
} else {
SETCALC(Formlet_next);
};
unit->m_b01 = 0.f;
unit->m_b02 = 0.f;
unit->m_y01 = 0.f;
unit->m_y02 = 0.f;
unit->m_b11 = 0.f;
unit->m_b12 = 0.f;
unit->m_y11 = 0.f;
unit->m_y12 = 0.f;
unit->m_freq = 0.f;
unit->m_attackTime = 0.f;
unit->m_decayTime = 0.f;
Formlet_next_1(unit, 1);
}
void Formlet_next(Formlet* unit, int inNumSamples)
{
//printf("Formlet_next\n");
float *out = ZOUT(0);
float *in = ZIN(0);
float freq = ZIN0(1);
float attackTime = ZIN0(2);
float decayTime = ZIN0(3);
double y00;
double y10;
double y01 = unit->m_y01;
double y11 = unit->m_y11;
double y02 = unit->m_y02;
double y12 = unit->m_y12;
double b01 = unit->m_b01;
double b11 = unit->m_b11;
double b02 = unit->m_b02;
double b12 = unit->m_b12;
double ain;
if (freq != unit->m_freq || decayTime != unit->m_decayTime || attackTime != unit->m_attackTime) {
double ffreq = freq * unit->mRate->mRadiansPerSample;
double R = decayTime == 0.f ? 0.f : exp(log001/(decayTime * SAMPLERATE));
double twoR = 2.f * R;
double R2 = R * R;
double cost = (twoR * cos(ffreq)) / (1.f + R2);
double b01_next = twoR * cost;
double b02_next = -R2;
double b01_slope = (b01_next - b01) * unit->mRate->mFilterSlope;
double b02_slope = (b02_next - b02) * unit->mRate->mFilterSlope;
R = attackTime == 0.f ? 0.f : exp(log001/(attackTime * SAMPLERATE));
twoR = 2.f * R;
R2 = R * R;
cost = (twoR * cos(ffreq)) / (1.f + R2);
double b11_next = twoR * cost;
double b12_next = -R2;
double b11_slope = (b11_next - b11) * unit->mRate->mFilterSlope;
double b12_slope = (b12_next - b12) * unit->mRate->mFilterSlope;
LOOP(unit->mRate->mFilterLoops,
ain = ZXP(in);
y00 = ain + b01 * y01 + b02 * y02;
y10 = ain + b11 * y11 + b12 * y12;
ZXP(out) = 0.25 * ((y00 - y02) - (y10 - y12));
ain = ZXP(in);
y02 = ain + b01 * y00 + b02 * y01;
y12 = ain + b11 * y10 + b12 * y11;
ZXP(out) = 0.25 * ((y02 - y01) - (y12 - y11));
ain = ZXP(in);
y01 = ain + b01 * y02 + b02 * y00;
y11 = ain + b11 * y12 + b12 * y10;
ZXP(out) = 0.25 * ((y01 - y00) - (y11 - y10));
b01 += b01_slope;
b02 += b02_slope;
b11 += b11_slope;
b12 += b12_slope;
);
LOOP(unit->mRate->mFilterRemain,
ain = ZXP(in);
y00 = ain + b01 * y01 + b02 * y02;
y10 = ain + b11 * y11 + b12 * y12;
ZXP(out) = 0.25 * ((y00 - y02) - (y10 - y12));
y02 = y01;
y01 = y00;
y12 = y11;
y11 = y10;
);
unit->m_freq = freq;
unit->m_attackTime = attackTime;
unit->m_decayTime = decayTime;
unit->m_b01 = b01_next;
unit->m_b02 = b02_next;
unit->m_b11 = b11_next;
unit->m_b12 = b12_next;
} else {
LOOP(unit->mRate->mFilterLoops,
ain = ZXP(in);
y00 = ain + b01 * y01 + b02 * y02;
y10 = ain + b11 * y11 + b12 * y12;
ZXP(out) = 0.25 * ((y00 - y02) - (y10 - y12));
ain = ZXP(in);
y02 = ain + b01 * y00 + b02 * y01;
y12 = ain + b11 * y10 + b12 * y11;
ZXP(out) = 0.25 * ((y02 - y01) - (y12 - y11));
ain = ZXP(in);
y01 = ain + b01 * y02 + b02 * y00;
y11 = ain + b11 * y12 + b12 * y10;
ZXP(out) = 0.25 * ((y01 - y00) - (y11 - y10));
);
LOOP(unit->mRate->mFilterRemain,
ain = ZXP(in);
y00 = ain + b01 * y01 + b02 * y02;
y10 = ain + b11 * y11 + b12 * y12;
ZXP(out) = 0.25 * ((y00 - y02) - (y10 - y12));
y02 = y01;
y01 = y00;
y12 = y11;
y11 = y10;
);
}
unit->m_y01 = y01;
unit->m_y02 = y02;
unit->m_y11 = y11;
unit->m_y12 = y12;
}
void Formlet_next_1(Formlet* unit, int inNumSamples)
{
//printf("Formlet_next\n");
float in = ZIN0(0);
float freq = ZIN0(1);
float attackTime = ZIN0(2);
float decayTime = ZIN0(3);
double y00;
double y10;
double y01 = unit->m_y01;
double y11 = unit->m_y11;
double y02 = unit->m_y02;
double y12 = unit->m_y12;
double b01 = unit->m_b01;
double b11 = unit->m_b11;
double b02 = unit->m_b02;
double b12 = unit->m_b12;
double ain;
if (freq != unit->m_freq || decayTime != unit->m_decayTime || attackTime != unit->m_attackTime) {
double ffreq = freq * unit->mRate->mRadiansPerSample;
double R = decayTime == 0.f ? 0.f : exp(log001/(decayTime * SAMPLERATE));
double twoR = 2.f * R;
double R2 = R * R;
double cost = (twoR * cos(ffreq)) / (1.f + R2);
b01 = twoR * cost;
b02 = -R2;
R = attackTime == 0.f ? 0.f : exp(log001/(attackTime * SAMPLERATE));
twoR = 2.f * R;
R2 = R * R;
cost = (twoR * cos(ffreq)) / (1.f + R2);
b11 = twoR * cost;
b12 = -R2;
ain = in;
y00 = ain + b01 * y01 + b02 * y02;
y10 = ain + b11 * y11 + b12 * y12;
ZOUT0(0) = 0.25f * ((y00 - y02) - (y10 - y12));
y02 = y01;
y01 = y00;
y12 = y11;
y11 = y10;
unit->m_freq = freq;
unit->m_attackTime = attackTime;
unit->m_decayTime = decayTime;
unit->m_b01 = b01;
unit->m_b02 = b02;
unit->m_b11 = b11;
unit->m_b12 = b12;
} else {
ain = in;
y00 = ain + b01 * y01 + b02 * y02;
y10 = ain + b11 * y11 + b12 * y12;
ZOUT0(0) = 0.25f * ((y00 - y02) - (y10 - y12));
y02 = y01;
y01 = y00;
y12 = y11;
y11 = y10;
}
unit->m_y01 = y01;
unit->m_y02 = y02;
unit->m_y11 = y11;
unit->m_y12 = y12;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void FOS_Ctor(FOS* unit)
{
//printf("FOS_Reset\n");
if (unit->mBufLength == 1) {
SETCALC(FOS_next_1);
} else {
if (INRATE(1) == calc_FullRate
&& INRATE(2) == calc_FullRate
&& INRATE(3) == calc_FullRate) {
SETCALC(FOS_next_a);
} else {
SETCALC(FOS_next_k);
}
};
unit->m_y1 = 0.f;
unit->m_a0 = 0.f;
unit->m_a1 = 0.f;
unit->m_b1 = 0.f;
FOS_next_1(unit, 1);
}
void FOS_next_a(FOS* unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
float *a0 = ZIN(1);
float *a1 = ZIN(2);
float *b1 = ZIN(3);
double y1 = unit->m_y1;
LOOP1(inNumSamples,
double y0 = ZXP(in) + ZXP(b1) * y1;
ZXP(out) = ZXP(a0) * y0 + ZXP(a1) * y1;
y1 = y0;
);
unit->m_y1 = zapgremlins(y1);
}
void FOS_next_1(FOS* unit, int inNumSamples)
{
float in = ZIN0(0);
float a0 = ZIN0(1);
float a1 = ZIN0(2);
float b1 = ZIN0(3);
double y1 = unit->m_y1;
double y0 = in + b1 * y1;
ZOUT0(0) = a0 * y0 + a1 * y1;
y1 = y0;
unit->m_y1 = zapgremlins(y1);
}
void FOS_next_k(FOS* unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
float next_a0 = ZIN0(1);
float next_a1 = ZIN0(2);
float next_b1 = ZIN0(3);
double y1 = unit->m_y1;
double a0 = unit->m_a0;
double a1 = unit->m_a1;
double b1 = unit->m_b1;
double a0_slope = CALCSLOPE(next_a0, a0);
double a1_slope = CALCSLOPE(next_a1, a1);
double b1_slope = CALCSLOPE(next_b1, b1);
LOOP1(inNumSamples,
double y0 = ZXP(in) + b1 * y1;
ZXP(out) = a0 * y0 + a1 * y1;
y1 = y0;
a0 += a0_slope;
a1 += a1_slope;
b1 += b1_slope;
);
unit->m_y1 = zapgremlins(y1);
unit->m_a0 = next_a0;
unit->m_a1 = next_a1;
unit->m_b1 = next_b1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void SOS_Ctor(SOS* unit)
{
// printf("SOS_Reset\n");
if (unit->mBufLength != 1) {
if (INRATE(1) == calc_FullRate
&& INRATE(2) == calc_FullRate
&& INRATE(3) == calc_FullRate
&& INRATE(4) == calc_FullRate
&& INRATE(5) == calc_FullRate) {
SETCALC(SOS_next_a);
// printf("SOS_next_a\n");
} else {
SETCALC(SOS_next_k);
// printf("SOS_next_k\n");
}
} else {
SETCALC(SOS_next_1);
// printf("SOS_next_1\n");
}
unit->m_y1 = 0.f;
unit->m_y2 = 0.f;
unit->m_a0 = 0.f;
unit->m_a1 = 0.f;
unit->m_a2 = 0.f;
unit->m_b1 = 0.f;
unit->m_b2 = 0.f;
SOS_next_1(unit, 1);
}
void SOS_next_a(SOS *unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
float *a0 = ZIN(1);
float *a1 = ZIN(2);
float *a2 = ZIN(3);
float *b1 = ZIN(4);
float *b2 = ZIN(5);
double y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
LOOP(unit->mRate->mFilterLoops,
y0 = ZXP(in) + ZXP(b1) * y1 + ZXP(b2) * y2;
ZXP(out) = ZXP(a0) * y0 + ZXP(a1) * y1 + ZXP(a2) * y2;
y2 = ZXP(in) + ZXP(b1) * y0 + ZXP(b2) * y1;
ZXP(out) = ZXP(a0) * y2 + ZXP(a1) * y0 + ZXP(a2) * y1;
y1 = ZXP(in) + ZXP(b1) * y2 + ZXP(b2) * y0;
ZXP(out) = ZXP(a0) * y1 + ZXP(a1) * y2 + ZXP(a2) * y0;
);
LOOP(unit->mRate->mFilterRemain,
y0 = ZXP(in) + ZXP(b1) * y1 + ZXP(b2) * y2;
ZXP(out) = ZXP(a0) * y0 + ZXP(a1) * y1 + ZXP(a2) * y2;
y2 = y1;
y1 = y0;
);
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
void SOS_next_k(SOS *unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
float next_a0 = ZIN0(1);
float next_a1 = ZIN0(2);
float next_a2 = ZIN0(3);
float next_b1 = ZIN0(4);
float next_b2 = ZIN0(5);
double y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
double a0 = unit->m_a0;
double a1 = unit->m_a1;
double a2 = unit->m_a2;
double b1 = unit->m_b1;
double b2 = unit->m_b2;
double a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope;
double a1_slope = (next_a1 - a1) * unit->mRate->mFilterSlope;
double a2_slope = (next_a2 - a2) * unit->mRate->mFilterSlope;
double b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope;
double b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope;
LOOP(unit->mRate->mFilterLoops,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
y2 = ZXP(in) + b1 * y0 + b2 * y1;
ZXP(out) = a0 * y2 + a1 * y0 + a2 * y1;
y1 = ZXP(in) + b1 * y2 + b2 * y0;
ZXP(out) = a0 * y1 + a1 * y2 + a2 * y0;
a0 += a0_slope;
a1 += a1_slope;
a2 += a2_slope;
b1 += b1_slope;
b2 += b2_slope;
);
LOOP(unit->mRate->mFilterRemain,
y0 = ZXP(in) + b1 * y1 + b2 * y2;
ZXP(out) = a0 * y0 + a1 * y1 + a2 * y2;
y2 = y1;
y1 = y0;
);
unit->m_a0 = a0;
unit->m_a1 = a1;
unit->m_a2 = a2;
unit->m_b1 = b1;
unit->m_b2 = b2;
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
void SOS_next_1(SOS *unit, int inNumSamples) // optimized for SOS.kr
{
float in = ZIN0(0);
float a0 = ZIN0(1);
float a1 = ZIN0(2);
float a2 = ZIN0(3);
float b1 = ZIN0(4);
float b2 = ZIN0(5);
double y0;
double y1 = unit->m_y1;
double y2 = unit->m_y2;
y0 = in + b1 * y1 + b2 * y2;
ZOUT0(0) = a0 * y0 + a1 * y1 + a2 * y2;
y2 = y1;
y1 = y0;
unit->m_y1 = zapgremlins(y1);
unit->m_y2 = zapgremlins(y2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Compander_Ctor(Compander* unit)
{
//printf("Compander_Reset\n");
SETCALC(Compander_next);
unit->m_clamp = 0.f;
unit->m_clampcoef = 0.f;
unit->m_relax = 0.f;
unit->m_relaxcoef = 0.f;
unit->m_prevmaxval = 0.f;
unit->m_gain = 0.f;
Compander_next(unit, 1);
}
void Compander_next(Compander* unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);