Add TAL-Reverb-II plugin to test
[juce-lv2.git] / tal-reverb-2-juce / src / Engine / HighShelf.h
blob2509e4fc6c145df1142dd16fffcd324618b641d2
1 /*-----------------------------------------------------------------------------
3 Kunz Patrick 30.04.2007
5 A one pole low pass filter.
7 -----------------------------------------------------------------------------*/
9 #if !defined(__HighShelf_h)
10 #define __HighShelf_h
12 #include "Math.h"
14 class HighShelf
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;
36 HighShelf(float sampleRate, int filterDecibel)
38 this->sampleRate = sampleRate;
39 this->filterDecibel = filterDecibel;
41 a0 = a1 = a2 = 0.0f;
42 b0 = b1 = b2 = 0.0f;
44 x0 = x1 = x2 = 0.0f;
45 y1 = y2 = 0.0f;
47 q = dBgain = freq = 0.0f;
49 s = 2.0f;
50 vsa= (1.0f/4294967295.0f); // Very small amount (Denormal Fix)
53 // gain [0..1], q[0..1], freq[0..44100]
54 inline void tick(float *inSample, float freq, float q, float gain)
56 gain = filterDecibel - (1.0f - gain) * filterDecibel * 2.0f;
57 calcCoefficients(freq, q, gain);
59 outSample = b0*x0 + b1*x1 + b2*x2 - a1*y1 - a2*y2;
60 outSample = b0*x0 + b1*x1 + b2*x2 - a1*y1 - a2*y2;
61 updateHistory(*inSample, outSample);
62 *inSample = outSample;
65 inline void calcCoefficients(float freq, float q, float dBgain)
67 if (this->q != q || this->dBgain != dBgain || this->freq != freq)
69 this->dBgain = dBgain;
70 this->q = q;
71 this->freq = freq;
72 w0 = 2.0f * 3.141592653589793f * freq / sampleRate;
73 a = sqrtf(pow(10, dBgain/20.0f));
74 alpha = sinf(w0)/(2.0f*q);
75 q = 1.0f / sqrt((a + 1.0f/a)*(1.0f/s - 1.0f) + 2.0f);
77 cosValue = cosf(w0);
78 sqrtValue = sqrtf(a);
80 b0 = a * ((a + 1.0f) + (a - 1.0f) * cosValue + 2.0f * sqrtValue * alpha );
81 b1 = -2.0f * a * ((a - 1.0f) + (a + 1.0f) * cosValue );
82 b2 = a * ((a + 1.0f) + (a - 1.0f) * cosValue - 2.0f * sqrtValue * alpha );
83 a0 = (a + 1.0f) - (a - 1.0f) * cosValue + 2.0f * sqrtValue * alpha;
84 a1 = 2.0f * ((a - 1.0f) - (a + 1.0f) * cosValue );
85 a2 = (a + 1.0f) - (a - 1.0f) * cosValue - 2.0f * sqrtValue * alpha;
87 a0 = 1.0f/a0;
89 b0 *= a0;
90 b1 *= a0;
91 b2 *= a0;
92 a1 *= a0;
93 a2 *= a0;
96 inline void updateHistory(float inSample, float outSample)
98 x0 = saturate(x1, -0.05f);
99 x1 = saturate(x2, 0.04f);
100 x2 = inSample;
102 y2 = y1;
103 y1 = outSample;
106 inline float saturate(float x, float variation)
108 return x - 0.002f * (x + variation) * x * x;
112 #endif