-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaveformStreamer.go
More file actions
33 lines (29 loc) · 927 Bytes
/
waveformStreamer.go
File metadata and controls
33 lines (29 loc) · 927 Bytes
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
package main
import "github.com/gopxl/beep/v2"
// TapStreamer wraps another streamer and captures its samples.
type TapStreamer struct {
s beep.Streamer // underlying streamer
}
// Err just passes through the error from the wrapped streamer.
func (t *TapStreamer) Err() error {
return t.s.Err()
}
func (t *TapStreamer) Stream(samples [][2]float64) (n int, ok bool) {
n, ok = t.s.Stream(samples)
// If no samples were produced or the streamer signals it's finished, don't update.
if n == 0 || !ok {
return n, ok
}
// Convert the float64 samples (in [-1,1]) to a PCM byte slice.
buf := make([]byte, n*4) // 4 bytes per sample (2 channels x 2 bytes)
for i := 0; i < n; i++ {
left := int16(samples[i][0] * 32767)
right := int16(samples[i][1] * 32767)
buf[i*4+0] = byte(left)
buf[i*4+1] = byte(left >> 8)
buf[i*4+2] = byte(right)
buf[i*4+3] = byte(right >> 8)
}
updateVisualization(buf)
return n, ok
}