LMMS
Loading...
Searching...
No Matches
EqFilter.h
Go to the documentation of this file.
1/*
2 * eqfilter.cpp - defination of EqFilterclass.
3 *
4 * Copyright (c) 2014 David French <dave/dot/french3/at/googlemail/dot/com>
5 *
6 * This file is part of LMMS - https://lmms.io
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program (see COPYING); if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301 USA.
22 *
23 */
24
25#ifndef EQFILTER_H
26#define EQFILTER_H
27
28#include <numbers>
29
30#include "BasicFilters.h"
31#include "lmms_math.h"
32
33namespace lmms
34{
35
44{
45public:
47 m_sampleRate(0),
48 m_freq(0),
49 m_res(0),
50 m_gain(0),
51 m_bw(0)
52 {
53
54 }
55
56
57
58
59 virtual inline void setSampleRate( int sampleRate )
60 {
61 if( sampleRate != m_sampleRate )
62 {
63 m_sampleRate = sampleRate;
65 }
66 }
67
68
69
70
71 virtual inline void setFrequency( float freq ){
72 if ( freq != m_freq )
73 {
74 m_freq = freq;
76 }
77 }
78
79
80
81
82 virtual inline void setQ( float res )
83 {
84 if ( res != m_res )
85 {
86 m_res = res;
88 }
89 }
90
91
92
93
94 virtual inline void setGain( float gain )
95 {
96 if ( gain != m_gain )
97 {
98 m_gain = gain;
100 }
101 }
102
103
104
105 virtual inline void setParameters( float sampleRate, float freq, float res, float gain )
106 {
107 bool hasChanged = ( sampleRate != m_sampleRate ||
108 freq != m_freq ||
109 res != m_res ||
110 gain != m_gain );
111 if ( hasChanged )
112 {
113 m_sampleRate = sampleRate;
114 m_freq = freq;
115 m_res = res;
116 m_gain = gain;
118 }
119 }
120
121
131 inline float update( float in, ch_cnt_t ch, float frameProgress)
132 {
133 float initailF = m_biQuadFrameInitial.update( in, ch );
134 float targetF = m_biQuadFrameTarget.update( in, ch );
135
136 if(frameProgress > 0.99999 )
137 {
139 }
140
141 return (1.0f-frameProgress) * initailF + frameProgress * targetF;
142
143 }
144
145
146protected:
151 virtual void calcCoefficents(){
152 setCoeffs( 0, 0, 0, 0, 0 );
153
154 }
155
156 inline void setCoeffs( float a1, float a2, float b0, float b1, float b2 )
157 {
158 m_biQuadFrameTarget.setCoeffs( a1, a2, b0, b1, b2 );
159 }
160
161
162
163
164
165
167 float m_freq;
168 float m_res;
169 float m_gain;
170 float m_bw;
173};
174
175
176
177
182class EqHp12Filter : public EqFilter
183{
184public :
185 void calcCoefficents() override
186 {
187
188 // calc intermediate
189 float w0 = 2 * std::numbers::pi_v<float> * m_freq / m_sampleRate;
190 float c = std::cos(w0);
191 float s = std::sin(w0);
192 float alpha = s / ( 2 * m_res );
193
194 //calc coefficents
195 float b0 = (1 + c) * 0.5;
196 float b1 = (-(1 + c));
197 float b2 = (1 + c) * 0.5;
198 float a0 = 1 + alpha;
199 float a1 = (-2 * c);
200 float a2 = 1 - alpha;
201
202 //normalise
203 b0 /= a0;
204 b1 /= a0;
205 b2 /= a0;
206 a1 /= a0;
207 a2 /= a0;
208
209 a0 = 1;
210
211 setCoeffs( a1, a2, b0, b1, b2 );
212
213
214 }
215};
216
217
218
219
225class EqLp12Filter : public EqFilter
226{
227public :
228 void calcCoefficents() override
229 {
230
231 // calc intermediate
232 float w0 = 2 * std::numbers::pi_v<float> * m_freq / m_sampleRate;
233 float c = std::cos(w0);
234 float s = std::sin(w0);
235 float alpha = s / ( 2 * m_res );
236
237 //calc coefficents
238 float b0 = (1 - c) * 0.5;
239 float b1 = 1 - c;
240 float b2 = (1 - c) * 0.5;
241 float a0 = 1 + alpha;
242 float a1 = -2 * c;
243 float a2 = 1 - alpha;
244
245 //normalise
246 b0 /= a0;
247 b1 /= a0;
248 b2 /= a0;
249 a1 /= a0;
250 a2 /= a0;
251
252 a0 = 1;
253
254 setCoeffs( a1, a2, b0, b1, b2 );
255 }
256};
257
258
259
265class EqPeakFilter : public EqFilter
266{
267public:
268
269
270 void calcCoefficents() override
271 {
272 using namespace std::numbers;
273 // calc intermediate
274 float w0 = 2 * pi_v<float> * m_freq / m_sampleRate;
275 float c = std::cos(w0);
276 float s = std::sin(w0);
277 float A = fastPow10f(m_gain * 0.025);
278 float alpha = s * std::sinh(ln2 / 2 * m_bw * w0 / std::sin(w0));
279
280 //calc coefficents
281 float b0 = 1 + alpha * A;
282 float b1 = -2 * c;
283 float b2 = 1 - alpha * A;
284 float a0 = 1 + alpha / A;
285 float a1 = -2 * c;
286 float a2 = 1 - alpha / A;
287
288 //normalise
289 b0 /= a0;
290 b1 /= a0;
291 b2 /= a0;
292 a1 /= a0;
293 a2 /= a0;
294 a0 = 1;
295
296 setCoeffs( a1, a2, b0, b1, b2 );
297 }
298
299 inline void setParameters( float sampleRate, float freq, float bw, float gain ) override
300 {
301 bool hasChanged = false;
302 if( sampleRate != m_sampleRate )
303 {
304 m_sampleRate = sampleRate;
305 hasChanged = true;
306 }
307 if ( freq != m_freq )
308 {
309 m_freq = freq;
310 hasChanged = true;
311 }
312 if ( bw != m_bw )
313 {
314 m_bw = bw;
315 hasChanged = true;
316 }
317 if ( gain != m_gain )
318 {
319 m_gain = gain;
320 hasChanged = true;
321 }
322
323 if ( hasChanged ) { calcCoefficents(); }
324 }
325};
326
327
328
329
331{
332public :
333 void calcCoefficents() override
334 {
335
336 // calc intermediate
337 float w0 = 2 * std::numbers::pi_v<float> * m_freq / m_sampleRate;
338 float c = std::cos(w0);
339 float s = std::sin(w0);
340 float A = fastPow10f(m_gain * 0.025);
341 // float alpha = s / (2 * m_res);
342 float beta = std::sqrt(A) / m_res;
343
344 //calc coefficents
345 float b0 = A * ((A + 1) - (A - 1) * c + beta * s);
346 float b1 = 2 * A * ((A - 1) - (A + 1) * c);
347 float b2 = A * ((A + 1) - (A - 1) * c - beta * s);
348 float a0 = (A + 1) + (A - 1) * c + beta * s;
349 float a1 = -2 * ((A - 1) + (A + 1) * c);
350 float a2 = (A + 1) + (A - 1) * c - beta * s;
351
352 //normalise
353 b0 /= a0;
354 b1 /= a0;
355 b2 /= a0;
356 a1 /= a0;
357 a2 /= a0;
358
359 a0 = 1;
360
361 setCoeffs( a1, a2, b0, b1, b2 );
362
363
364 }
365};
366
368{
369public :
370 void calcCoefficents() override
371 {
372
373 // calc intermediate
374 float w0 = 2 * std::numbers::pi_v<float> * m_freq / m_sampleRate;
375 float c = std::cos(w0);
376 float s = std::sin(w0);
377 float A = fastPow10f(m_gain * 0.025);
378 float beta = std::sqrt(A) / m_res;
379
380 //calc coefficents
381 float b0 = A * ((A + 1) + (A - 1) * c + beta * s);
382 float b1 = -2 * A * ((A - 1) + (A + 1) * c);
383 float b2 = A * ((A + 1) + (A - 1) * c - beta * s);
384 float a0 = (A + 1) - (A - 1) * c + beta * s;
385 float a1 = 2 * ((A - 1) - (A + 1) * c);
386 float a2 = (A + 1) - (A - 1) * c - beta * s;
387
388 //normalise
389 b0 /= a0;
390 b1 /= a0;
391 b2 /= a0;
392 a1 /= a0;
393 a2 /= a0;
394 a0 = 1;
395
396 setCoeffs( a1, a2, b0, b1, b2 );
397 }
398};
399
400
401
402
404{
405public:
407 StereoLinkwitzRiley( 44100),
408 m_freq(0 ),
409 m_sr( 1 )
410 {
411 }
412
413 virtual inline void setSR( int sampleRate )
414 {
415 if( sampleRate != m_sr )
416 {
417 m_sr = sampleRate;
418 setSampleRate( sampleRate );
420 }
421 }
422
423
424
425
426 virtual inline void setFrequency( float freq ){
427 if ( freq != m_freq )
428 {
429 m_freq = freq;
431 }
432 }
433
434
435
436
437 virtual void processBuffer( SampleFrame* buf, const f_cnt_t frames )
438 {
439 for ( f_cnt_t f = 0 ; f < frames ; ++f)
440 {
441 buf[f][0] = update( buf[f][0] , 0);
442 buf[f][1] = update( buf[f][1] , 1);
443 }
444 }
445protected:
446
447 float m_freq;
448 int m_sr;
449
450
451};
452
453
454} // namespace lmms
455
456
457#endif // EQFILTER_H
float m_freq
Definition EqFilter.h:167
void setCoeffs(float a1, float a2, float b0, float b1, float b2)
Definition EqFilter.h:156
virtual void calcCoefficents()
calcCoefficents Override this in child classes to provide the coefficents, based on Freq,...
Definition EqFilter.h:151
virtual void setParameters(float sampleRate, float freq, float res, float gain)
Definition EqFilter.h:105
float m_res
Definition EqFilter.h:168
StereoBiQuad m_biQuadFrameInitial
Definition EqFilter.h:171
float m_gain
Definition EqFilter.h:169
StereoBiQuad m_biQuadFrameTarget
Definition EqFilter.h:172
virtual void setSampleRate(int sampleRate)
Definition EqFilter.h:59
virtual void setQ(float res)
Definition EqFilter.h:82
float m_sampleRate
Definition EqFilter.h:166
float update(float in, ch_cnt_t ch, float frameProgress)
update filters using two BiQuads, then crossfades, depending on on percentage of period processes
Definition EqFilter.h:131
virtual void setFrequency(float freq)
Definition EqFilter.h:71
float m_bw
Definition EqFilter.h:170
EqFilter()
Definition EqFilter.h:46
virtual void setGain(float gain)
Definition EqFilter.h:94
Definition EqFilter.h:368
void calcCoefficents() override
calcCoefficents Override this in child classes to provide the coefficents, based on Freq,...
Definition EqFilter.h:370
The EqHp12Filter class A 2 pole High Pass Filter Coefficent calculations from http://www....
Definition EqFilter.h:183
void calcCoefficents() override
calcCoefficents Override this in child classes to provide the coefficents, based on Freq,...
Definition EqFilter.h:185
virtual void processBuffer(SampleFrame *buf, const f_cnt_t frames)
Definition EqFilter.h:437
EqLinkwitzRiley()
Definition EqFilter.h:406
int m_sr
Definition EqFilter.h:448
virtual void setSR(int sampleRate)
Definition EqFilter.h:413
virtual void setFrequency(float freq)
Definition EqFilter.h:426
float m_freq
Definition EqFilter.h:447
Definition EqFilter.h:331
void calcCoefficents() override
calcCoefficents Override this in child classes to provide the coefficents, based on Freq,...
Definition EqFilter.h:333
The EqLp12Filter class. A 2 pole low pass filter Coefficent calculations from http://www....
Definition EqFilter.h:226
void calcCoefficents() override
calcCoefficents Override this in child classes to provide the coefficents, based on Freq,...
Definition EqFilter.h:228
The EqPeakFilter class A Peak Filter Coefficent calculations from http://www.musicdsp....
Definition EqFilter.h:266
void setParameters(float sampleRate, float freq, float bw, float gain) override
Definition EqFilter.h:299
void calcCoefficents() override
calcCoefficents Override this in child classes to provide the coefficents, based on Freq,...
Definition EqFilter.h:270
void setSampleRate(float sampleRate)
Definition BasicFilters.h:67
void setLowpass(float freq)
Definition BasicFilters.h:100
float update(float in, ch_cnt_t ch)
Definition BasicFilters.h:116
Definition SampleFrame.h:41
unsigned s
Definition inflate.c:1555
unsigned f
Definition inflate.c:1572
JHUFF_TBL long freq[]
Definition jchuff.h:50
#define A(x)
Definition lice_arc.cpp:13
float in
Definition lilv_test.c:1460
Definition AudioAlsa.cpp:35
std::uint16_t ch_cnt_t
Definition LmmsTypes.h:44
std::uint64_t f_cnt_t
Definition LmmsTypes.h:43
LinkwitzRiley< 2 > StereoLinkwitzRiley
Definition BasicFilters.h:140
BiQuad< 2 > StereoBiQuad
Definition BasicFilters.h:187
auto fastPow10f(T x)
Definition lmms_math.h:273
return c
Definition crypt.c:175