1 /*-----------------------------------------------------------------------------
3 Kunz Patrick 30.04.2007
5 A one pole low pass filter.
7 -----------------------------------------------------------------------------*/
9 #if !defined(__LowShelf_h)
29 float a
, w0
, q
, s
, alpha
;
30 float cosValue
, sqrtValue
;
31 float vsa
; // Very small amount (Denormal Fix)
37 LowShelf(float sampleRate
, int filterDecibel
)
39 this->sampleRate
= sampleRate
;
40 this->filterDecibel
= filterDecibel
;
48 q
= dBgain
= freq
= 0.0f
;
51 vsa
= (1.0f
/4294967295.0f
); // Very small amount (Denormal Fix)
54 // gain [0..1], q[0..1], freq[0..44100]
55 inline void tick(float *inSample
, float freq
, float q
, float gain
)
57 gain
= filterDecibel
- (1.0f
- gain
) * filterDecibel
* 2.0f
;
58 calcCoefficients(freq
, q
, gain
);
60 outSample
= b0
*x0
+ b1
*x1
+ b2
*x2
- a1
*y1
- a2
*y2
;
61 outSample
= b0
*x0
+ b1
*x1
+ b2
*x2
- a1
*y1
- a2
*y2
;
62 updateHistory(*inSample
, outSample
);
63 *inSample
= outSample
;
66 inline void calcCoefficients(float freq
, float q
, float dBgain
)
68 if (this->q
!= q
|| this->dBgain
!= dBgain
|| this->freq
!= freq
)
70 this->dBgain
= dBgain
;
73 w0
= 2.0f
* 3.141592653589793f
* freq
/ sampleRate
;
75 a
= sqrtf(pow(10, dBgain
/20.0f
));
76 alpha
= sinf(w0
)/(2.0f
*q
);
77 q
= 1.0f
/ sqrt((a
+ 1.0f
/a
)*(1.0f
/s
- 1.0f
) + 2.0f
);
82 b0
= a
* ((a
+ 1.0f
) - (a
- 1.0f
) * cosValue
+ 2.0f
* sqrtValue
* alpha
);
83 b1
= 2.0f
* a
* ((a
- 1.0f
) - (a
+ 1.0f
) * cosValue
);
84 b2
= a
* ((a
+ 1.0f
) - (a
- 1.0f
) * cosValue
- 2.0f
* sqrtValue
* alpha
);
85 a0
= (a
+ 1.0f
) + (a
- 1.0f
) * cosValue
+ 2.0f
* sqrtValue
* alpha
;
86 a1
= - 2.0f
* ((a
- 1.0f
) + (a
+ 1.0f
) * cosValue
);
87 a2
= (a
+ 1.0f
) + (a
- 1.0f
) * cosValue
- 2.0f
* sqrtValue
* alpha
;
98 inline void updateHistory(float inSample
, float outSample
)
100 x0
= saturate(x1
, -0.08f
);
101 x1
= saturate(x2
, 0.05f
);
102 x2
= saturate(inSample
, 0.01f
);
108 inline float saturate(float x
, float variation
)
110 return x
- 0.002f
* (x
+ variation
) * x
* x
;