1 /*-----------------------------------------------------------------------------
3 Kunz Patrick 30.04.2007
5 A one pole low pass filter.
7 -----------------------------------------------------------------------------*/
9 #if !defined(__HighShelf_h)
29 float a
, w0
, q
, s
, alpha
;
30 float cosValue
, sqrtValue
;
31 float vsa
; // Very small amount (Denormal Fix)
36 HighShelf(float sampleRate
, int filterDecibel
)
38 this->sampleRate
= sampleRate
;
39 this->filterDecibel
= filterDecibel
;
47 q
= dBgain
= freq
= 0.0f
;
50 vsa
= (1.0f
/4294967295.0f
); // Very small amount (Denormal Fix)
53 // gain [0..1], q[0..1], freq[0..44100]
54 inline void tick(float *inSample
, float freq
, float q
, float gain
)
56 gain
= filterDecibel
- (1.0f
- gain
) * filterDecibel
* 2.0f
;
57 calcCoefficients(freq
, q
, gain
);
59 outSample
= b0
*x0
+ b1
*x1
+ b2
*x2
- a1
*y1
- a2
*y2
;
60 outSample
= b0
*x0
+ b1
*x1
+ b2
*x2
- a1
*y1
- a2
*y2
;
61 updateHistory(*inSample
, outSample
);
62 *inSample
= outSample
;
65 inline void calcCoefficients(float freq
, float q
, float dBgain
)
67 if (this->q
!= q
|| this->dBgain
!= dBgain
|| this->freq
!= freq
)
69 this->dBgain
= dBgain
;
72 w0
= 2.0f
* 3.141592653589793f
* freq
/ sampleRate
;
73 a
= sqrtf(pow(10, dBgain
/20.0f
));
74 alpha
= sinf(w0
)/(2.0f
*q
);
75 q
= 1.0f
/ sqrt((a
+ 1.0f
/a
)*(1.0f
/s
- 1.0f
) + 2.0f
);
80 b0
= a
* ((a
+ 1.0f
) + (a
- 1.0f
) * cosValue
+ 2.0f
* sqrtValue
* alpha
);
81 b1
= -2.0f
* a
* ((a
- 1.0f
) + (a
+ 1.0f
) * cosValue
);
82 b2
= a
* ((a
+ 1.0f
) + (a
- 1.0f
) * cosValue
- 2.0f
* sqrtValue
* alpha
);
83 a0
= (a
+ 1.0f
) - (a
- 1.0f
) * cosValue
+ 2.0f
* sqrtValue
* alpha
;
84 a1
= 2.0f
* ((a
- 1.0f
) - (a
+ 1.0f
) * cosValue
);
85 a2
= (a
+ 1.0f
) - (a
- 1.0f
) * cosValue
- 2.0f
* sqrtValue
* alpha
;
96 inline void updateHistory(float inSample
, float outSample
)
98 x0
= saturate(x1
, -0.05f
);
99 x1
= saturate(x2
, 0.04f
);
106 inline float saturate(float x
, float variation
)
108 return x
- 0.002f
* (x
+ variation
) * x
* x
;