-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.go
68 lines (62 loc) · 1.46 KB
/
count.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
package module
import (
"buddin.us/eolian/dsp"
"github.com/mitchellh/mapstructure"
)
func init() {
Register("Count", func(c Config) (Patcher, error) {
var config struct {
Limit int
Step int
}
if err := mapstructure.Decode(c, &config); err != nil {
return nil, err
}
if config.Limit == 0 {
config.Limit = 1024
}
if config.Step == 0 {
config.Step = 1
}
return newCount(config.Limit, config.Step)
})
}
type count struct {
IO
count int
trigger, reset, limit, step *In
lastTrigger, lastReset dsp.Float64
}
func newCount(limit, step int) (*count, error) {
m := &count{
trigger: NewInBuffer("trigger", dsp.Float64(-1)),
reset: NewInBuffer("reset", dsp.Float64(-1)),
limit: NewInBuffer("limit", dsp.Float64(limit)),
step: NewInBuffer("step", dsp.Float64(step)),
lastTrigger: -1,
lastReset: -1,
}
return m, m.Expose(
"Count",
[]*In{m.trigger, m.reset, m.limit, m.step},
[]*Out{{Name: "output", Provider: dsp.Provide(m)}})
}
func (c *count) Process(out dsp.Frame) {
var (
trigger = c.trigger.ProcessFrame()
reset = c.reset.ProcessFrame()
limit = c.limit.ProcessFrame()
step = c.step.ProcessFrame()
)
for i := range out {
if c.lastReset < 0 && reset[i] > 0 {
c.count = 0
}
if c.lastTrigger < 0 && trigger[i] > 0 {
c.count = (c.count + int(step[i])) % int(limit[i])
}
out[i] = dsp.Float64(c.count)
c.lastReset = reset[i]
c.lastTrigger = trigger[i]
}
}