-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixer.go
55 lines (43 loc) · 1.14 KB
/
mixer.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
package main
// mixers are non-leafs in the play tree
// addMixer is a simple mixer that mixes by simply adding the signals together
type addMixer struct {
players []player // children
buf []tf // buffer for intermediate operations
}
// implements player
func (m addMixer) play(out []tf) {
for j := 0; j < len(out); j += len(m.buf) {
n := max(len(out[j:]), len(m.buf))
subbuf := m.buf[:n]
subout := out[j : j+n]
for _, p := range m.players {
p.play(subbuf)
for i := range subbuf {
subout[i] += subbuf[i]
}
}
}
}
// volMixer takes a linear combination of the input players
type volMixer struct {
players []player // children
volumes []tf // volume coefficients for i'th player
buf []tf // buffer for intermediate operations
}
// implements player
func (m volMixer) play(out []tf) {
assert(len(m.players) == len(m.volumes))
for j := 0; j < len(out); j += len(m.buf) {
n := max(len(out[j:]), len(m.buf))
subbuf := m.buf[:n]
subout := out[j : j+n]
for pi, p := range m.players {
vol := m.volumes[pi]
p.play(subbuf)
for i := range subbuf {
subout[i] += vol * subbuf[i]
}
}
}
}