-
Notifications
You must be signed in to change notification settings - Fork 12
/
streamers.go
65 lines (62 loc) · 1.45 KB
/
streamers.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
package beep
// Silence returns a Streamer which streams num samples of silence. If num is negative, silence is
// streamed forever.
//
// Deprecated: beep.Silence has been moved to generators.Silence
func Silence(num int) Streamer {
return StreamerFunc(func(samples [][2]float64) (n int, ok bool) {
if num == 0 {
return 0, false
}
if 0 < num && num < len(samples) {
samples = samples[:num]
}
clear(samples)
if num > 0 {
num -= len(samples)
}
return len(samples), true
})
}
// Callback returns a Streamer, which does not stream any samples, but instead calls f the first
// time its Stream method is called. The speaker is locked while f is called.
func Callback(f func()) Streamer {
return StreamerFunc(func(samples [][2]float64) (n int, ok bool) {
if f != nil {
f()
f = nil
}
return 0, false
})
}
// Iterate returns a Streamer which successively streams Streamers obtains by calling the provided g
// function. The streaming stops when g returns nil.
//
// Iterate does not propagate errors from the generated Streamers.
func Iterate(g func() Streamer) Streamer {
var (
s Streamer
first = true
)
return StreamerFunc(func(samples [][2]float64) (n int, ok bool) {
if first {
s = g()
first = false
}
if s == nil {
return 0, false
}
for len(samples) > 0 {
if s == nil {
break
}
sn, sok := s.Stream(samples)
if !sok {
s = g()
}
samples = samples[sn:]
n += sn
}
return n, true
})
}