chenlh
2026-03-10 0f65a1a9267b8a7ab4678ef20b07532e4c8377ca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
 
#include "Biquad.h"
#define _USE_MATH_DEFINES
#include "math.h"
 
namespace ReverbHallRoom
{
    Biquad::Biquad()
    {
        ClearBuffers();
    }
 
    Biquad::Biquad(FilterType filterType, float fs)
    {
        Type = filterType;
        SetSamplerate(fs);
 
        SetGainDb(0.0f);
        Frequency = (float)(fs * 0.25f);
        SetQ(0.5f);
        ClearBuffers();
    }
 
    Biquad::~Biquad()
    {
 
    }
 
 
    float Biquad::GetSamplerate()
    {
        return fs;
    }
 
    void Biquad::SetSamplerate(float fs)
    {
        this->fs = fs;
        fsInv = 1.0f / fs;
        Update();
    }
 
    float Biquad::GetGainDb()
    {
        return gainDB;
    }
 
    float Biquad::GetGain()
    {
        return gain;
    }
 
    void Biquad::SetGainDb (float value)
    {
            // Clamp value between -60 and 60
            if (value < -60)
                value = -60;
            else if (value > 60)
                value = 60;
 
            gainDB = value;
            gain = powf (10.0f, value / 20.0f);
    }
 
    void Biquad::SetGain (float value)
    {
        if (value < 0.001f)
            value = 0.001f; // -60dB
        else if (value > 1000.0f)
            value = 1000.0f; // 60dB
 
        gain = value;
        gainDB = log10f (gain) * 20;
    }
 
    float Biquad::GetQ()
    {
        return q;
    }
 
    void Biquad::SetQ(float value)
    {
        if (value < 0.001f)
            value = 0.001f;
        q = value;
    }
 
    // this is the newer set of formulas from http://www.earlevel.com/main/2011/01/02/biquad-formulas/
    // Note that for shelf and peak filters, I had to invert the if/else statements for boost and cut, as
    // I was getting the inverse desired effect, very odd...
    void Biquad::Update()
    {
        auto Fc = Frequency;
        //auto Fs = fs;
 
        auto V = powf(10, fabsf(gainDB) / 20.0f);
        //auto K = tanf(M_PI * Fc / Fs);
        auto K = tanf(M_PI * Fc * fsInv);
        auto Q = q;
        double norm = 1.0;
 
        switch (Type)
        {
        case FilterType::LowPass6db:
            a1 = -expf(-2.0 * M_PI * (Fc * fsInv));
            b0 = 1.0 + a1;
            b1 = b2 = a2 = 0;
            break;
        case FilterType::HighPass6db:
            a1 = -expf(-2.0 * M_PI * (Fc * fsInv));
            b0 = a1;
            b1 = -a1;
            b2 = a2 = 0;
            break;
        case FilterType::LowPass:
            norm = 1 / (1 + K / Q + K * K);
            b0 = K * K * norm;
            b1 = 2 * b0;
            b2 = b0;
            a1 = 2 * (K * K - 1) * norm;
            a2 = (1 - K / Q + K * K) * norm;
            break;
        case FilterType::HighPass:
            norm = 1 / (1 + K / Q + K * K);
            b0 = 1 * norm;
            b1 = -2 * b0;
            b2 = b0;
            a1 = 2 * (K * K - 1) * norm;
            a2 = (1 - K / Q + K * K) * norm;
            break;
        case FilterType::BandPass:
            norm = 1 / (1 + K / Q + K * K);
            b0 = K / Q * norm;
            b1 = 0;
            b2 = -b0;
            a1 = 2 * (K * K - 1) * norm;
            a2 = (1 - K / Q + K * K) * norm;
            break;
        case FilterType::Notch:
            norm = 1 / (1 + K / Q + K * K);
            b0 = (1 + K * K) * norm;
            b1 = 2 * (K * K - 1) * norm;
            b2 = b0;
            a1 = b1;
            a2 = (1 - K / Q + K * K) * norm;
            break;
        case FilterType::Peak:
            if (gainDB >= 0)
            {
                norm = 1 / (1 + 1 / Q * K + K * K);
                b0 = (1 + V / Q * K + K * K) * norm;
                b1 = 2 * (K * K - 1) * norm;
                b2 = (1 - V / Q * K + K * K) * norm;
                a1 = b1;
                a2 = (1 - 1 / Q * K + K * K) * norm;
            }
            else
            {
                norm = 1 / (1 + V / Q * K + K * K);
                b0 = (1 + 1 / Q * K + K * K) * norm;
                b1 = 2 * (K * K - 1) * norm;
                b2 = (1 - 1 / Q * K + K * K) * norm;
                a1 = b1;
                a2 = (1 - V / Q * K + K * K) * norm;
            }
            break;
        case FilterType::LowShelf:
            if (gainDB >= 0)
            {
                norm = 1 / (1 + sqrtf(2) * K + K * K);
                b0 = (1 + sqrtf(2 * V) * K + V * K * K) * norm;
                b1 = 2 * (V * K * K - 1) * norm;
                b2 = (1 - sqrtf(2 * V) * K + V * K * K) * norm;
                a1 = 2 * (K * K - 1) * norm;
                a2 = (1 - sqrtf(2) * K + K * K) * norm;
            }
            else
            {
                norm = 1 / (1 + sqrtf(2 * V) * K + V * K * K);
                b0 = (1 + sqrtf(2) * K + K * K) * norm;
                b1 = 2 * (K * K - 1) * norm;
                b2 = (1 - sqrtf(2) * K + K * K) * norm;
                a1 = 2 * (V * K * K - 1) * norm;
                a2 = (1 - sqrtf(2 * V) * K + V * K * K) * norm;
            }
            break;
        case FilterType::HighShelf:
            if (gainDB >= 0)
            {
                norm = 1 / (1 + sqrtf(2) * K + K * K);
                b0 = (V + sqrtf(2 * V) * K + K * K) * norm;
                b1 = 2 * (K * K - V) * norm;
                b2 = (V - sqrtf(2 * V) * K + K * K) * norm;
                a1 = 2 * (K * K - 1) * norm;
                a2 = (1 - sqrtf(2) * K + K * K) * norm;
            }
            else
            {
                norm = 1 / (V + sqrtf(2 * V) * K + K * K);
                b0 = (1 + sqrtf(2) * K + K * K) * norm;
                b1 = 2 * (K * K - 1) * norm;
                b2 = (1 - sqrtf(2) * K + K * K) * norm;
                a1 = 2 * (K * K - V) * norm;
                a2 = (V - sqrtf(2 * V) * K + K * K) * norm;
            }
            break;
        }
    }
 
    double Biquad::GetResponse(float freq) const
    {
        double phi = powf((sinf(2 * M_PI * freq / (2.0 * fs))), 2);
        double y = ((powf(b0 + b1 + b2, 2.0) - 4.0 * (b0 * b1 + 4.0 * b0 * b2 + b1 * b2) * phi + 16.0 * b0 * b2 * phi * phi) / (powf(1.0 + a1 + a2, 2.0) - 4.0 * (a1 + 4.0 * a2 + a1 * a2) * phi + 16.0 * a2 * phi * phi));
        // y gives you power gain, not voltage gain, and this a 10 * log_10(g) formula instead of 20 * log_10(g)
        // by taking the sqrt we get a value that's more suitable for signal processing, i.e. the voltage gain
        return sqrtf(y);
    }
 
    void Biquad::ClearBuffers()
    {
        y = 0;
        x2 = 0;
        y2 = 0;
        x1 = 0;
        y1 = 0;
    }
}