Add TAL-Reverb-II plugin to test
[juce-lv2.git] / tal-reverb-2-juce / src / Engine / PeakEq.h
blobfe434bb4a9936e5039cf79187570445b3a236a3f
1 #if !defined(__PeakEq_h)
2 #define __PeakEq_h
4 #include <stdio.h>
5 #include "Math.h"
7 class PeakEq
9 public:
10 int filterDecibel;
12 float sampleRate;
14 float a0, a1, a2;
15 float b0, b1, b2;
17 float x0, x1, x2;
18 float y1, y2;
20 float outSample;
22 float a, w0, q, s, alpha;
23 float cosValue, sqrtValue;
24 float vsa; // Very small amount (Denormal Fix)
26 float dBgain, freq;
29 PeakEq(float sampleRate, int filterDecibel)
31 this->sampleRate = sampleRate;
32 this->filterDecibel = filterDecibel;
34 a0 = a1 = a2 = 0.0f;
35 b0 = b1 = b2 = 0.0f;
37 x0 = x1 = x2 = 0.0f;
38 y1 = y2 = 0.0f;
40 q = dBgain = freq = 0.0f;
42 s = 2.0f;
43 vsa= (1.0f/4294967295.0f); // Very small amount (Denormal Fix)
46 // gain [0..1], q[0..1], freq[0..44100]
47 inline void tick(float *inSample, float freq, float q, float gain)
49 gain = filterDecibel - (1.0f - gain) * filterDecibel * 2.0f;
50 calcCoefficients(freq, q, gain);
52 outSample = b0*x0 + b1*x1 + b2*x2 - a1*y1 - a2*y2;
53 outSample = b0*x0 + b1*x1 + b2*x2 - a1*y1 - a2*y2;
54 updateHistory(*inSample, outSample);
55 *inSample = outSample;
58 inline void calcCoefficients(float freq, float q, float dBgain)
60 if (this->q != q || this->dBgain != dBgain || this->freq != freq)
62 this->dBgain = dBgain;
63 this->q = q;
64 this->freq = freq;
65 w0 = 2.0f * 3.141592653589793f * freq / sampleRate;
66 a = sqrtf(pow(10, dBgain/20.0f));
67 alpha = sinf(w0)*sinhf( logf(2.0f)/2.0f * q * w0/sinf(w0));
68 // q = 1.0f / sqrt((a + 1.0f/a)*(1.0f/s - 1.0f) + 2.0f);
70 cosValue = cosf(w0);
71 sqrtValue = sqrtf(a);
74 b0 = 1.0f + alpha * a;
75 b1 = -2.0f * cosValue;
76 b2 = 1.0f - alpha * a;
77 a0 = 1.0f + alpha / a;
78 a1 = -2.0f * cosValue;
79 a2 = 1.0f - alpha / a;
81 a0 = 1.0f/a0;
83 b0 *= a0;
84 b1 *= a0;
85 b2 *= a0;
86 a1 *= a0;
87 a2 *= a0;
90 inline void updateHistory(float inSample, float outSample)
92 x0 = saturate(x1, -0.05f);
93 x1 = saturate(x2, 0.04f);
94 x2 = inSample;
96 y2 = y1;
97 y1 = outSample;
100 inline float saturate(float x, float variation)
102 return x - 0.002f * (x + variation) * x * x;
106 #endif