@@ -4,25 +4,38 @@ namespace EddiSpeechService.SpeechEffects
44{
55 public class BiquadHighShelf
66 {
7- private readonly float _a0 , _a1 , _a2 , _b1 , _b2 ;
7+ private readonly float _cutoffHz ;
8+ private readonly float _q ;
9+ private readonly int _sampleRate ;
10+
11+ private float _gainDb ;
12+ private float _a0 , _a1 , _a2 , _b1 , _b2 ;
813 private float _z1 , _z2 ;
914
1015 public BiquadHighShelf ( float cutoffHz , float gainDb , float q , int sampleRate )
1116 {
12- var A = ( float ) Math . Pow ( 10 , gainDb / 40.0 ) ;
13- var w0 = 2.0f * ( float ) Math . PI * cutoffHz / sampleRate ;
14- var cosw0 = ( float ) Math . Cos ( w0 ) ;
15- var sinw0 = ( float ) Math . Sin ( w0 ) ;
16- var alpha = sinw0 / ( 2.0f * q ) ;
17- var sqrtA = ( float ) Math . Sqrt ( A ) ;
17+ _cutoffHz = cutoffHz ;
18+ _q = q ;
19+ _sampleRate = sampleRate ;
20+ SetGainDb ( gainDb ) ;
21+ }
22+
23+ public void SetGainDb ( float gainDb )
24+ {
25+ _gainDb = gainDb ;
26+ var A = ( float ) Math . Pow ( 10 , _gainDb / 40.0 ) ;
27+ var w0 = 2.0f * ( float ) Math . PI * _cutoffHz / _sampleRate ;
28+ var cosw0 = ( float ) Math . Cos ( w0 ) ;
29+ var sinw0 = ( float ) Math . Sin ( w0 ) ;
30+ var alpha = sinw0 / ( 2.0f * _q ) ;
31+ var sqrtA = ( float ) Math . Sqrt ( A ) ;
1832
19- // High-shelf coefficients
20- var b0 = A * ( A + 1f + ( ( A - 1f ) * cosw0 ) + ( 2 * sqrtA * alpha ) ) ;
21- var b1 = - 2f * A * ( A - 1f + ( ( A + 1f ) * cosw0 ) ) ;
22- var b2 = A * ( A + 1f + ( ( A - 1f ) * cosw0 ) - ( 2 * sqrtA * alpha ) ) ;
23- var a0 = A + 1f - ( ( A - 1f ) * cosw0 ) + ( 2 * sqrtA * alpha ) ;
24- var a1 = 2f * ( A - 1f - ( ( A + 1f ) * cosw0 ) ) ;
25- var a2 = A + 1f - ( ( A - 1f ) * cosw0 ) - ( 2f * sqrtA * alpha ) ;
33+ var b0 = A * ( A + 1f + ( ( A - 1f ) * cosw0 ) + ( 2f * sqrtA * alpha ) ) ;
34+ var b1 = - 2f * A * ( A - 1f + ( ( A + 1f ) * cosw0 ) ) ;
35+ var b2 = A * ( A + 1f + ( ( A - 1f ) * cosw0 ) - ( 2f * sqrtA * alpha ) ) ;
36+ var a0 = A + 1f - ( ( A - 1f ) * cosw0 ) + ( 2f * sqrtA * alpha ) ;
37+ var a1 = 2f * ( A - 1f - ( ( A + 1f ) * cosw0 ) ) ;
38+ var a2 = A + 1f - ( ( A - 1f ) * cosw0 ) - ( 2f * sqrtA * alpha ) ;
2639
2740 _a0 = b0 / a0 ;
2841 _a1 = b1 / a0 ;
@@ -33,9 +46,9 @@ public BiquadHighShelf ( float cutoffHz, float gainDb, float q, int sampleRate )
3346
3447 public float Process ( float input )
3548 {
36- var output = ( _a0 * input ) + ( _a1 * _z1 ) + ( _a2 * _z2 ) - ( _b1 * _z1 ) - ( _b2 * _z2 ) ;
37- _z2 = _z1 ;
38- _z1 = output ;
49+ float output = ( _a0 * input ) + _z1 ;
50+ _z1 = ( _a1 * input ) + _z2 - ( _b1 * output ) ;
51+ _z2 = ( _a2 * input ) - ( _b2 * output ) ;
3952 return output ;
4053 }
4154 }
0 commit comments