-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.go
244 lines (209 loc) · 4.23 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package audio
import "math"
type Filter struct {
a, b []FilterTap
buf []float64
i int
}
type FilterTap struct {
Delay int
Coef float64
}
func (f *Filter) Taps(a, b []FilterTap) *Filter {
n := 0
a0 := 0.0
for _, a := range a {
if a.Delay < 0 {
panic(a.Delay)
}
if n < a.Delay {
n = a.Delay
}
if a.Delay == 0 {
a0 = a.Coef
}
}
for _, b := range b {
if b.Delay < 0 {
panic(b.Delay)
}
if n < b.Delay {
n = b.Delay
}
}
if a0 == 0 {
panic(a0)
}
for i := range a {
a[i].Coef /= a0
}
for i := range b {
b[i].Coef /= a0
}
f.a = a
f.b = b
f.buf = make([]float64, n)
f.i = 0
return f
}
func (f *Filter) Filter(x float64) float64 {
y := 0.0
n := len(f.buf)
// Direct form II implementation:
// First, accumulate feedback into x.
for _, a := range f.a {
if a.Delay > 0 {
x -= a.Coef * f.buf[(f.i-a.Delay+n)%n]
}
}
// Then, accumulate feedforward into y.
for _, b := range f.b {
if b.Delay > 0 {
y += b.Coef * f.buf[(f.i-b.Delay+n)%n]
} else {
y += b.Coef * x
}
}
f.buf[f.i] = x
f.i = (f.i + 1) % n
return y
}
func AllPassFilterTaps(a []FilterTap) (_, _ []FilterTap) {
a_ := make([]FilterTap, len(a))
N := 0
for _, a := range a {
if N < a.Delay {
N = a.Delay
}
}
for i := range a {
a_[len(a)-i-1] = FilterTap{N - a[i].Delay, a[i].Coef}
}
return a, a_
}
func TapsFromRoots(r []complex128) []FilterTap {
p := poly{1}
for _, r := range r {
p = p.mul(poly{1, -2 * real(r), real(r) * real(r) + imag(r) * imag(r)})
}
t := make([]FilterTap, len(p))
for i := range p {
t[i] = FilterTap{i, p[i]}
}
return t
}
type poly []float64
func (p poly) mul(q poly) poly {
r := make(poly, len(p) + len(q) - 1)
for i := range p {
for j := range q {
r[i+j] += p[i] * q[j]
}
}
return r
}
type LowPass1 struct {
p Params
freq float64
a0, b1, y1 float64
}
func (f *LowPass1) Freq(freq float64) *LowPass1 {
f.freq = freq
f.b1 = math.Exp(-2 * math.Pi * freq / f.p.SampleRate)
f.a0 = 1 - f.b1
return f
}
func (f *LowPass1) InitAudio(p Params) {
f.p = p
f.Freq(f.freq)
}
func (f *LowPass1) Filter(x float64) float64 {
f.y1 = f.a0*x + f.b1*f.y1
return f.y1
}
type DCFilter struct {
a, x, y float64
}
func (f *DCFilter) InitAudio(p Params) {
rc := 1 / (2 * math.Pi * 10)
f.a = rc / (rc + 1/p.SampleRate)
}
func (f *DCFilter) Filter(x float64) float64 {
f.y = f.a * (f.y + x - f.x)
f.x = x
return f.y
}
type LinSmoother struct {
params Params
attackSpeed, releaseSpeed float64
up, down float64
x, y float64
}
func NewLinSmoother(attackSpeed, releaseSpeed, startValue float64) *LinSmoother {
return &LinSmoother{attackSpeed: attackSpeed, releaseSpeed: releaseSpeed, y: startValue}
}
func (e *LinSmoother) InitAudio(p Params) {
e.params = p
e.SetAttackSpeed(e.attackSpeed)
e.SetReleaseSpeed(e.releaseSpeed)
}
func (e *LinSmoother) Value() float64 {
return e.y
}
func (e *LinSmoother) SetValue(v float64) {
e.y = v
}
func (e *LinSmoother) SetAttackSpeed(s float64) {
e.attackSpeed = s
e.up = s / e.params.SampleRate
}
func (e *LinSmoother) SetReleaseSpeed(s float64) {
e.releaseSpeed = s
e.down = -s / e.params.SampleRate
}
func (e *LinSmoother) Smooth(x float64) float64 {
e.x = x
d := e.up
if e.x < e.y {
d = e.down
}
e.y += d
return e.y
}
func (e *LinSmoother) Done() bool {
return math.Abs(e.x-e.y) < .0001
}
type ExpSmoother struct {
params Params
attackTime, releaseTime float64
up, down float64
x, y float64
}
func NewExpSmoother(attackTime, releaseTime float64) *ExpSmoother {
return &ExpSmoother{attackTime: attackTime, releaseTime: releaseTime}
}
func (e *ExpSmoother) InitAudio(p Params) {
e.params = p
e.SetAttackTime(e.attackTime)
e.SetReleaseTime(e.releaseTime)
}
func (e *ExpSmoother) SetAttackTime(t float64) {
e.attackTime = t
e.up = 1 - math.Pow(.01, 1/(e.params.SampleRate*t))
}
func (e *ExpSmoother) SetReleaseTime(t float64) {
e.releaseTime = t
e.down = 1 - math.Pow(.01, 1/(e.params.SampleRate*t))
}
func (e *ExpSmoother) Smooth(x float64) float64 {
e.x = x
a := e.up
if e.x < e.y {
a = e.down
}
e.y += a * (e.x - e.y)
return e.y
}
func (e *ExpSmoother) Done() bool {
return math.Abs(e.x-e.y) < .0001
}