Skip to content

Commit 008f6c8

Browse files
committed
Chorus tuning.
1 parent b8dcab1 commit 008f6c8

7 files changed

Lines changed: 736 additions & 297 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
3+
namespace EddiSpeechService.SpeechEffects
4+
{
5+
public class AllPassFilter
6+
{
7+
private readonly float[] _buf;
8+
private int _idx;
9+
private float _g;
10+
11+
public AllPassFilter ( int samples, float g = 0.5f )
12+
{
13+
_buf = new float[ Math.Max( 2, samples ) ];
14+
_idx = 0;
15+
_g = Clamp( g, -0.7f, 0.7f );
16+
}
17+
18+
public void SetGain ( float g ) => _g = Clamp( g, -0.7f, 0.7f );
19+
20+
public float Process ( float x )
21+
{
22+
var y = ( -_g * x ) + _buf[ _idx ];
23+
_buf[ _idx ] = x + ( _g * y );
24+
if ( ++_idx >= _buf.Length ) _idx = 0;
25+
return y;
26+
}
27+
28+
private static float Clamp ( float value, float min, float max )
29+
{
30+
if ( value < min )
31+
{
32+
return min;
33+
}
34+
35+
return value > max ? max : value;
36+
}
37+
}
38+
}

SpeechService/SpeechEffects/BiquadHighShelfFilter.cs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

SpeechService/SpeechEffects/BiquadLowShelfFilter.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,40 @@ namespace EddiSpeechService.SpeechEffects
44
{
55
public class BiquadLowShelf
66
{
7-
private readonly float _a0, _a1, _a2, _b1, _b2;
8-
private float _z1, _z2;
7+
private readonly float a0, a1, a2, b1, b2;
8+
private float x1, x2, y1, y2;
99

1010
public BiquadLowShelf ( float cutoffHz, float gainDb, float q, int sampleRate )
1111
{
1212
var A = (float)Math.Pow( 10, gainDb / 40.0 );
13-
var w0 = 2.0f * (float)Math.PI * cutoffHz / sampleRate;
13+
var w0 = 2f * (float)Math.PI * cutoffHz / sampleRate;
1414
var cosw0 = (float)Math.Cos( w0 );
1515
var sinw0 = (float)Math.Sin( w0 );
16-
var alpha = sinw0 / ( 2.0f * q );
16+
var alpha = sinw0 / ( 2f * q );
1717
var sqrtA = (float)Math.Sqrt( A );
1818

19-
// Low-shelf coefficients
2019
var b0 = A * ( A + 1f - ( ( A - 1f ) * cosw0 ) + ( 2f * sqrtA * alpha ) );
21-
var b1 = 2f * A * ( A - 1f - ( ( A + 1f ) * cosw0 ) );
22-
var b2 = A * ( A + 1f - ( ( A - 1f ) * cosw0 ) - ( 2f * sqrtA * alpha ) );
23-
var a0 = A + 1f + ( ( A - 1f ) * cosw0 ) + ( 2f * sqrtA * alpha );
24-
var a1 = -2f * ( A - 1f + ( ( A + 1f ) * cosw0 ) );
25-
var a2 = A + 1f + ( ( A - 1f ) * cosw0 ) - ( 2f * sqrtA * alpha );
20+
var b1n = 2f * A * ( A - 1f - ( ( A + 1f ) * cosw0 ) );
21+
var b2n = A * ( A + 1f - ( ( A - 1f ) * cosw0 ) - ( 2f * sqrtA * alpha ) );
22+
var a0n = A + 1f + ( ( A - 1f ) * cosw0 ) + ( 2f * sqrtA * alpha );
23+
var a1n = -2f * ( A - 1f + ( ( A + 1f ) * cosw0 ) );
24+
var a2n = A + 1f + ( ( A - 1f ) * cosw0 ) - ( 2f * sqrtA * alpha );
2625

27-
_a0 = b0 / a0;
28-
_a1 = b1 / a0;
29-
_a2 = b2 / a0;
30-
_b1 = a1 / a0;
31-
_b2 = a2 / a0;
26+
a0 = b0 / a0n;
27+
a1 = b1n / a0n;
28+
a2 = b2n / a0n;
29+
b1 = a1n / a0n;
30+
b2 = a2n / a0n;
3231
}
3332

34-
public float Process ( float input )
33+
public float Process ( float x )
3534
{
36-
var output = ( _a0 * input ) + ( _a1 * _z1 ) + ( _a2 * _z2 ) - ( _b1 * _z1 ) - ( _b2 * _z2 );
37-
_z2 = _z1;
38-
_z1 = output;
39-
return output;
35+
var y = ( a0 * x ) + ( a1 * x1 ) + ( a2 * x2 ) - ( b1 * y1 ) - ( b2 * y2 );
36+
x2 = x1;
37+
x1 = x;
38+
y2 = y1;
39+
y1 = y;
40+
return y;
4041
}
4142
}
4243
}

0 commit comments

Comments
 (0)