-
Notifications
You must be signed in to change notification settings - Fork 1
/
buffer.go
52 lines (44 loc) · 915 Bytes
/
buffer.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 tracks
import (
"sync"
"github.com/gopxl/beep"
)
type continuousBuffer struct {
mu sync.Mutex
cond sync.Cond
audio *beep.Buffer
}
func newContinuousBuffer(format beep.Format) *continuousBuffer {
b := &continuousBuffer{audio: beep.NewBuffer(format)}
b.cond.L = &b.mu
return b
}
func (b *continuousBuffer) Format() beep.Format {
return b.audio.Format()
}
func (b *continuousBuffer) Append(s beep.Streamer) {
b.mu.Lock()
defer b.mu.Unlock()
b.audio.Append(s)
b.cond.Broadcast()
}
func (b *continuousBuffer) Len() int {
b.mu.Lock()
defer b.mu.Unlock()
return b.audio.Len()
}
func (b *continuousBuffer) StreamerFrom(start int) beep.Streamer {
return beep.Iterate(func() beep.Streamer {
b.mu.Lock()
defer b.mu.Unlock()
for {
end := b.audio.Len()
if end > start {
stream := b.audio.Streamer(start, end)
start = end
return stream
}
b.cond.Wait()
}
})
}