forked from faiface/beep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompositors.go
141 lines (125 loc) · 2.77 KB
/
compositors.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
package beep
// Take returns a Streamer which streams at most num samples from s.
//
// The returned Streamer propagates s's errors throught Err.
func Take(num int, s Streamer) Streamer {
return &take{
s: s,
currSample: 0,
numSamples: num,
}
}
type take struct {
s Streamer
currSample int
numSamples int
}
func (t *take) Stream(samples [][2]float64) (n int, ok bool) {
if t.currSample >= t.numSamples {
return 0, false
}
toStream := t.numSamples - t.currSample
if len(samples) < toStream {
toStream = len(samples)
}
n, ok = t.s.Stream(samples[:toStream])
t.currSample += n
return n, ok
}
func (t *take) Err() error {
return t.s.Err()
}
// Loop takes a StreamSeeker and plays it count times. If count is negative, s is looped infinitely.
//
// The returned Streamer propagates s's errors.
func Loop(count int, s StreamSeeker) Streamer {
return &loop{
s: s,
remains: count,
}
}
type loop struct {
s StreamSeeker
remains int
}
func (l *loop) Stream(samples [][2]float64) (n int, ok bool) {
if l.remains == 0 || l.s.Err() != nil {
return 0, false
}
for len(samples) > 0 {
sn, sok := l.s.Stream(samples)
if !sok {
if l.remains == 0 {
break
}
err := l.s.Seek(0)
if err != nil {
return n, true
}
if l.remains > 0 {
l.remains--
}
continue
}
samples = samples[sn:]
n += sn
}
return n, true
}
func (l *loop) Err() error {
return l.s.Err()
}
// Seq takes zero or more Streamers and returns a Streamer which streams them one by one without pauses.
//
// Seq does not propagate errors from the Streamers.
func Seq(s ...Streamer) Streamer {
i := 0
return StreamerFunc(func(samples [][2]float64) (n int, ok bool) {
for i < len(s) && len(samples) > 0 {
sn, sok := s[i].Stream(samples)
samples = samples[sn:]
n, ok = n+sn, ok || sok
if !sok {
i++
}
}
return n, ok
})
}
// Mix takes zero or more Streamers and returns a Streamer which streames them mixed together.
//
// Mix does not propagate errors from the Streamers.
func Mix(s ...Streamer) Streamer {
return StreamerFunc(func(samples [][2]float64) (n int, ok bool) {
var tmp [512][2]float64
for len(samples) > 0 {
toStream := len(tmp)
if toStream > len(samples) {
toStream = len(samples)
}
// clear the samples
for i := range samples[:toStream] {
samples[i] = [2]float64{}
}
snMax := 0 // max number of streamed samples in this iteration
for _, st := range s {
// mix the stream
sn, sok := st.Stream(tmp[:toStream])
if sn > snMax {
snMax = sn
}
ok = ok || sok
for i := range tmp[:sn] {
samples[i][0] += tmp[i][0]
samples[i][1] += tmp[i][1]
}
}
n += snMax
if snMax < len(tmp) {
break
}
samples = samples[snMax:]
}
return n, ok
})
}