-
Notifications
You must be signed in to change notification settings - Fork 152
/
ctrl.go
52 lines (49 loc) · 1.22 KB
/
ctrl.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
package beep
// Ctrl allows for pausing a Streamer.
//
// Wrap a Streamer in a Ctrl.
//
// ctrl := &beep.Ctrl{Streamer: s}
//
// Then, we can pause the streaming (this will cause Ctrl to stream silence).
//
// ctrl.Paused = true
//
// To completely stop a Ctrl before the wrapped Streamer is drained, just set the wrapped Streamer
// to nil.
//
// ctrl.Streamer = nil
//
// If you're playing a Streamer wrapped in a Ctrl through the speaker, you need to lock and unlock
// the speaker when modifying the Ctrl to avoid race conditions.
//
// speaker.Play(ctrl)
// // ...
// speaker.Lock()
// ctrl.Paused = true
// speaker.Unlock()
type Ctrl struct {
Streamer Streamer
Paused bool
}
// Stream streams the wrapped Streamer, if not nil. If the Streamer is nil, Ctrl acts as drained.
// When paused, Ctrl streams silence.
func (c *Ctrl) Stream(samples [][2]float64) (n int, ok bool) {
if c.Streamer == nil {
return 0, false
}
if c.Paused {
for i := range samples {
samples[i] = [2]float64{}
}
return len(samples), true
}
return c.Streamer.Stream(samples)
}
// Err returns the error of the wrapped Streamer, if not nil.
func (c *Ctrl) Err() error {
if c.Streamer == nil {
return nil
}
return c.Streamer.Err()
}