-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
66 lines (57 loc) · 1.41 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
package module
import (
"buddin.us/eolian/dsp"
"github.com/mitchellh/mapstructure"
)
func init() {
Register("Filter", func(c Config) (Patcher, error) {
var config struct {
Poles int
}
if err := mapstructure.Decode(c, &config); err != nil {
return nil, err
}
if config.Poles == 0 {
config.Poles = 4
}
return newSVFilter(config.Poles)
})
}
type svFilter struct {
multiOutIO
in, cutoff, resonance *In
filter *dsp.SVFilter
lp, bp, hp dsp.Frame
}
func newSVFilter(poles int) (*svFilter, error) {
m := &svFilter{
in: NewIn("input", dsp.Float64(0)),
cutoff: NewInBuffer("cutoff", dsp.Frequency(1000)),
resonance: NewInBuffer("resonance", dsp.Float64(1)),
filter: &dsp.SVFilter{Poles: poles},
lp: dsp.NewFrame(),
bp: dsp.NewFrame(),
hp: dsp.NewFrame(),
}
return m, m.Expose(
"Filter",
[]*In{m.in, m.cutoff, m.resonance},
[]*Out{
{Name: "lowpass", Provider: provideCopyOut(m, &m.lp)},
{Name: "highpass", Provider: provideCopyOut(m, &m.hp)},
{Name: "bandpass", Provider: provideCopyOut(m, &m.bp)},
},
)
}
func (f *svFilter) Process(out dsp.Frame) {
f.incrRead(func() {
f.in.Process(out)
cutoff := f.cutoff.ProcessFrame()
resonance := f.resonance.ProcessFrame()
for i := range out {
f.filter.Cutoff = cutoff[i]
f.filter.Resonance = resonance[i]
f.lp[i], f.bp[i], f.hp[i] = f.filter.Tick(out[i])
}
})
}