lmms
Cross-platform music production software
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
130 inline float update( float in, ch_cnt_t ch, float frameProgress)
131 {
132 float initailF = m_biQuadFrameInitial.update( in, ch );
133 float targetF = m_biQuadFrameTarget.update( in, ch );
134
135 if(frameProgress > 0.99999 )
136 {
138 }
139
140 return (1.0f-frameProgress) * initailF + frameProgress * targetF;
141
142 }
143
144
145protected:
150 virtual void calcCoefficents(){
151 setCoeffs( 0, 0, 0, 0, 0 );
152
153 }
154
155 inline void setCoeffs( float a1, float a2, float b0, float b1, float b2 )
156 {
157 m_biQuadFrameTarget.setCoeffs( a1, a2, b0, b1, b2 );
158 }
159
160
161
162
163
164
166 float m_freq;
167 float m_res;
168 float m_gain;
169 float m_bw;
172};
173
174
175
176
181class EqHp12Filter : public EqFilter
182{
183public :
184 void calcCoefficents() override
185 {
186
187 // calc intermediate
188 float w0 = 2 * std::numbers::pi_v<float> * m_freq / m_sampleRate;
189 float c = std::cos(w0);
190 float s = std::sin(w0);
191 float alpha = s / ( 2 * m_res );
192
193 //calc coefficents
194 float b0 = (1 + c) * 0.5;
195 float b1 = (-(1 + c));
196 float b2 = (1 + c) * 0.5;
197 float a0 = 1 + alpha;
198 float a1 = (-2 * c);
199 float a2 = 1 - alpha;
200
201 //normalise
202 b0 /= a0;
203 b1 /= a0;
204 b2 /= a0;
205 a1 /= a0;
206 a2 /= a0;
207
208 a0 = 1;
209
210 setCoeffs( a1, a2, b0, b1, b2 );
211
212
213 }
214};
215
216
217
218
224class EqLp12Filter : public EqFilter
225{
226public :
227 void calcCoefficents() override
228 {
229
230 // calc intermediate
231 float w0 = 2 * std::numbers::pi_v<float> * m_freq / m_sampleRate;
232 float c = std::cos(w0);
233 float s = std::sin(w0);
234 float alpha = s / ( 2 * m_res );
235
236 //calc coefficents
237 float b0 = (1 - c) * 0.5;
238 float b1 = 1 - c;
239 float b2 = (1 - c) * 0.5;
240 float a0 = 1 + alpha;
241 float a1 = -2 * c;
242 float a2 = 1 - alpha;
243
244 //normalise
245 b0 /= a0;
246 b1 /= a0;
247 b2 /= a0;
248 a1 /= a0;
249 a2 /= a0;
250
251 a0 = 1;
252
253 setCoeffs( a1, a2, b0, b1, b2 );
254 }
255};
256
257
258
264class EqPeakFilter : public EqFilter
265{
266public:
267
268
269 void calcCoefficents() override
270 {
271 using namespace std::numbers;
272 // calc intermediate
273 float w0 = 2 * pi_v<float> * m_freq / m_sampleRate;
274 float c = std::cos(w0);
275 float s = std::sin(w0);
276 float A = fastPow10f(m_gain * 0.025);
277 float alpha = s * std::sinh(ln2 / 2 * m_bw * w0 / std::sin(w0));
278
279 //calc coefficents
280 float b0 = 1 + alpha * A;
281 float b1 = -2 * c;
282 float b2 = 1 - alpha * A;
283 float a0 = 1 + alpha / A;
284 float a1 = -2 * c;
285 float a2 = 1 - alpha / A;
286
287 //normalise
288 b0 /= a0;
289 b1 /= a0;
290 b2 /= a0;
291 a1 /= a0;
292 a2 /= a0;
293 a0 = 1;
294
295 setCoeffs( a1, a2, b0, b1, b2 );
296 }
297
298 inline void setParameters( float sampleRate, float freq, float bw, float gain ) override
299 {
300 bool hasChanged = false;
301 if( sampleRate != m_sampleRate )
302 {
303 m_sampleRate = sampleRate;
304 hasChanged = true;
305 }
306 if ( freq != m_freq )
307 {
308 m_freq = freq;
309 hasChanged = true;
310 }
311 if ( bw != m_bw )
312 {
313 m_bw = bw;
314 hasChanged = true;
315 }
316 if ( gain != m_gain )
317 {
318 m_gain = gain;
319 hasChanged = true;
320 }
321
322 if ( hasChanged ) { calcCoefficents(); }
323 }
324};
325
326
327
328
330{
331public :
332 void calcCoefficents() override
333 {
334
335 // calc intermediate
336 float w0 = 2 * std::numbers::pi_v<float> * m_freq / m_sampleRate;
337 float c = std::cos(w0);
338 float s = std::sin(w0);
339 float A = fastPow10f(m_gain * 0.025);
340 // float alpha = s / (2 * m_res);
341 float beta = std::sqrt(A) / m_res;
342
343 //calc coefficents
344 float b0 = A * ((A + 1) - (A - 1) * c + beta * s);
345 float b1 = 2 * A * ((A - 1) - (A + 1) * c);
346 float b2 = A * ((A + 1) - (A - 1) * c - beta * s);
347 float a0 = (A + 1) + (A - 1) * c + beta * s;
348 float a1 = -2 * ((A - 1) + (A + 1) * c);
349 float a2 = (A + 1) + (A - 1) * c - beta * s;
350
351 //normalise
352 b0 /= a0;
353 b1 /= a0;
354 b2 /= a0;
355 a1 /= a0;
356 a2 /= a0;
357
358 a0 = 1;
359
360 setCoeffs( a1, a2, b0, b1, b2 );
361
362
363 }
364};
365
367{
368public :
369 void calcCoefficents() override
370 {
371
372 // calc intermediate
373 float w0 = 2 * std::numbers::pi_v<float> * m_freq / m_sampleRate;
374 float c = std::cos(w0);
375 float s = std::sin(w0);
376 float A = fastPow10f(m_gain * 0.025);
377 float beta = std::sqrt(A) / m_res;
378
379 //calc coefficents
380 float b0 = A * ((A + 1) + (A - 1) * c + beta * s);
381 float b1 = -2 * A * ((A - 1) + (A + 1) * c);
382 float b2 = A * ((A + 1) + (A - 1) * c - beta * s);
383 float a0 = (A + 1) - (A - 1) * c + beta * s;
384 float a1 = 2 * ((A - 1) - (A + 1) * c);
385 float a2 = (A + 1) - (A - 1) * c - beta * s;
386
387 //normalise
388 b0 /= a0;
389 b1 /= a0;
390 b2 /= a0;
391 a1 /= a0;
392 a2 /= a0;
393 a0 = 1;
394
395 setCoeffs( a1, a2, b0, b1, b2 );
396 }
397};
398
399
400
401
403{
404public:
406 StereoLinkwitzRiley( 44100),
407 m_freq(0 ),
408 m_sr( 1 )
409 {
410 }
411
412 virtual inline void setSR( int sampleRate )
413 {
414 if( sampleRate != m_sr )
415 {
416 m_sr = sampleRate;
417 setSampleRate( sampleRate );
419 }
420 }
421
422
423
424
425 virtual inline void setFrequency( float freq ){
426 if ( freq != m_freq )
427 {
428 m_freq = freq;
430 }
431 }
432
433
434
435
436 virtual void processBuffer( SampleFrame* buf, const f_cnt_t frames )
437 {
438 for ( f_cnt_t f = 0 ; f < frames ; ++f)
439 {
440 buf[f][0] = update( buf[f][0] , 0);
441 buf[f][1] = update( buf[f][1] , 1);
442 }
443 }
444protected:
445
446 float m_freq;
447 int m_sr;
448
449
450};
451
452
453} // namespace lmms
454
455
456#endif // EQFILTER_H
float update(float in, ch_cnt_t ch)
Definition BasicFilters.h:173
void setCoeffs(float a1, float a2, float b0, float b1, float b2)
Definition BasicFilters.h:157
float m_freq
Definition EqFilter.h:166
void setCoeffs(float a1, float a2, float b0, float b1, float b2)
Definition EqFilter.h:155
virtual void calcCoefficents()
calcCoefficents Override this in child classes to provide the coefficents, based on Freq,...
Definition EqFilter.h:150
virtual void setParameters(float sampleRate, float freq, float res, float gain)
Definition EqFilter.h:105
float m_res
Definition EqFilter.h:167
StereoBiQuad m_biQuadFrameInitial
Definition EqFilter.h:170
float m_gain
Definition EqFilter.h:168
StereoBiQuad m_biQuadFrameTarget
Definition EqFilter.h:171
virtual void setSampleRate(int sampleRate)
Definition EqFilter.h:59
virtual void setQ(float res)
Definition EqFilter.h:82
float m_sampleRate
Definition EqFilter.h:165
float update(float in, ch_cnt_t ch, float frameProgress)
Update filters using two BiQuads, then crossfade.
Definition EqFilter.h:130
virtual void setFrequency(float freq)
Definition EqFilter.h:71
float m_bw
Definition EqFilter.h:169
EqFilter()
Definition EqFilter.h:46
virtual void setGain(float gain)
Definition EqFilter.h:94
Definition EqFilter.h:367
void calcCoefficents() override
calcCoefficents Override this in child classes to provide the coefficents, based on Freq,...
Definition EqFilter.h:369
The EqHp12Filter class A 2 pole High Pass Filter Coefficent calculations from http://www....
Definition EqFilter.h:182
void calcCoefficents() override
calcCoefficents Override this in child classes to provide the coefficents, based on Freq,...
Definition EqFilter.h:184
virtual void processBuffer(SampleFrame *buf, const f_cnt_t frames)
Definition EqFilter.h:436
EqLinkwitzRiley()
Definition EqFilter.h:405
int m_sr
Definition EqFilter.h:447
virtual void setSR(int sampleRate)
Definition EqFilter.h:412
virtual void setFrequency(float freq)
Definition EqFilter.h:425
float m_freq
Definition EqFilter.h:446
Definition EqFilter.h:330
void calcCoefficents() override
calcCoefficents Override this in child classes to provide the coefficents, based on Freq,...
Definition EqFilter.h:332
The EqLp12Filter class. A 2 pole low pass filter Coefficent calculations from http://www....
Definition EqFilter.h:225
void calcCoefficents() override
calcCoefficents Override this in child classes to provide the coefficents, based on Freq,...
Definition EqFilter.h:227
The EqPeakFilter class A Peak Filter Coefficent calculations from http://www.musicdsp....
Definition EqFilter.h:265
void setParameters(float sampleRate, float freq, float bw, float gain) override
Definition EqFilter.h:298
void calcCoefficents() override
calcCoefficents Override this in child classes to provide the coefficents, based on Freq,...
Definition EqFilter.h:269
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
Definition RemotePluginBase.cpp:34
auto fastPow10f(T x)
Definition lmms_math.h:275
std::uint16_t ch_cnt_t
Definition LmmsTypes.h:44
@ A
Definition Note.h:58
std::uint64_t f_cnt_t
Definition LmmsTypes.h:43
LinkwitzRiley< 2 > StereoLinkwitzRiley
Definition BasicFilters.h:140
BiQuad< 2 > StereoBiQuad
Definition BasicFilters.h:187