Add TAL-Reverb-II plugin to test
[juce-lv2.git] / tal-reverb-2-juce / src / Engine / LowShelf.h
blob9bc65480e9045dd3310eb61a577f3dc46377281b
1 /*-----------------------------------------------------------------------------
3 Kunz Patrick 30.04.2007
5 A one pole low pass filter.
7 -----------------------------------------------------------------------------*/
9 #if !defined(__LowShelf_h)
10 #define __LowShelf_h
12 #include "Math.h"
14 class LowShelf
16 public:
17 int filterDecibel;
19 float sampleRate;
21 float a0, a1, a2;
22 float b0, b1, b2;
24 float x0, x1, x2;
25 float y1, y2;
27 float outSample;
29 float a, w0, q, s, alpha;
30 float cosValue, sqrtValue;
31 float vsa; // Very small amount (Denormal Fix)
33 float dBgain, freq;
35 float random;
37 LowShelf(float sampleRate, int filterDecibel)
39 this->sampleRate = sampleRate;
40 this->filterDecibel = filterDecibel;
42 a0 = a1 = a2 = 0.0f;
43 b0 = b1 = b2 = 0.0f;
45 x0 = x1 = x2 = 0.0f;
46 y1 = y2 = 0.0f;
48 q = dBgain = freq = 0.0f;
50 s = 1.0f;
51 vsa= (1.0f/4294967295.0f); // Very small amount (Denormal Fix)
54 // gain [0..1], q[0..1], freq[0..44100]
55 inline void tick(float *inSample, float freq, float q, float gain)
57 gain = filterDecibel - (1.0f - gain) * filterDecibel * 2.0f;
58 calcCoefficients(freq, q, gain);
60 outSample = b0*x0 + b1*x1 + b2*x2 - a1*y1 - a2*y2;
61 outSample = b0*x0 + b1*x1 + b2*x2 - a1*y1 - a2*y2;
62 updateHistory(*inSample, outSample);
63 *inSample = outSample;
66 inline void calcCoefficients(float freq, float q, float dBgain)
68 if (this->q != q || this->dBgain != dBgain || this->freq != freq)
70 this->dBgain = dBgain;
71 this->q = q;
72 this->freq = freq;
73 w0 = 2.0f * 3.141592653589793f * freq / sampleRate;
75 a = sqrtf(pow(10, dBgain/20.0f));
76 alpha = sinf(w0)/(2.0f*q);
77 q = 1.0f / sqrt((a + 1.0f/a)*(1.0f/s - 1.0f) + 2.0f);
79 cosValue = cosf(w0);
80 sqrtValue = sqrtf(a);
82 b0 = a * ((a + 1.0f) - (a - 1.0f) * cosValue + 2.0f * sqrtValue * alpha );
83 b1 = 2.0f * a * ((a - 1.0f) - (a + 1.0f) * cosValue );
84 b2 = a * ((a + 1.0f) - (a - 1.0f) * cosValue - 2.0f * sqrtValue * alpha );
85 a0 = (a + 1.0f) + (a - 1.0f) * cosValue + 2.0f * sqrtValue * alpha;
86 a1 = - 2.0f * ((a - 1.0f) + (a + 1.0f) * cosValue );
87 a2 = (a + 1.0f) + (a - 1.0f) * cosValue - 2.0f * sqrtValue * alpha;
89 a0 = 1.0f/a0;
91 b0 *= a0;
92 b1 *= a0;
93 b2 *= a0;
94 a1 *= a0;
95 a2 *= a0;
98 inline void updateHistory(float inSample, float outSample)
100 x0 = saturate(x1, -0.08f);
101 x1 = saturate(x2, 0.05f);
102 x2 = saturate(inSample, 0.01f);
104 y2 = y1;
105 y1 = outSample;
108 inline float saturate(float x, float variation)
110 return x - 0.002f * (x + variation) * x * x;
114 #endif