forked from clandry94/agraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelay_filter.go
72 lines (60 loc) · 1.23 KB
/
delay_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
package agraph
/*
Changes volume amount
*/
type Delay struct {
source chan []uint16
sink chan []uint16
name string
Delay int // something such as 1.2, 0.3, etc
Decay float32
i int
prevSamples []uint16
delayBuffer []float32
}
func newDelay(name string, delay int, decay float32) (Node, error) {
return &Delay{
source: make(chan []uint16, SOURCE_SIZE),
sink: nil,
name: name,
Delay: delay,
i: 0,
delayBuffer: make([]float32, delay),
Decay: decay,
}, nil
}
func (n *Delay) SetSink(c chan []uint16) {
n.sink = c
}
func (n *Delay) Source() chan []uint16 {
return n.source
}
func (n *Delay) Sink() chan []uint16 {
return n.sink
}
func (n *Delay) Process() error {
for {
select {
case data := <-n.source:
sample := data[0]
if n.i < 0 {
n.i += n.Delay
}
delayedSample := n.delayBuffer[n.i]
filteredData := uint16(delayedSample)
n.delayBuffer[n.i] = (delayedSample * n.Decay) + float32(sample)
n.i++
if n.i >= n.Delay {
n.i -= n.Delay
}
data[0] = filteredData
n.sink <- data
}
}
return nil
}
func (n *Delay) do(data []uint16) ([]uint16, error) {
sample := data[0]
data[0] = sample
return data, nil
}