Skip to content

Commit b91efeb

Browse files
committed
Upgrade to Oto v2 & let Oto pull samples instead of pushing them to Oto
1 parent d230727 commit b91efeb

File tree

3 files changed

+67
-56
lines changed

3 files changed

+67
-56
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.14
55
require (
66
github.com/gdamore/tcell v1.3.0
77
github.com/hajimehoshi/go-mp3 v0.3.0
8-
github.com/hajimehoshi/oto v0.7.1
8+
github.com/hajimehoshi/oto/v2 v2.4.0-alpha.4
99
github.com/jfreymuth/oggvorbis v1.0.1
1010
github.com/mewkiz/flac v1.0.7
1111
github.com/pkg/errors v0.9.1

go.sum

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
github.com/DATA-DOG/go-sqlmock v1.3.3 h1:CWUqKXe0s8A2z6qCgkP4Kru7wC11YoAnoupUKFDnH08=
22
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
33
github.com/d4l3k/messagediff v1.2.2-0.20190829033028-7e0a312ae40b/go.mod h1:Oozbb1TVXFac9FtSIxHBMnBCq2qeH/2KkEQxENCrlLo=
4+
github.com/ebitengine/purego v0.0.0-20220907032450-cf3e27c364c7 h1:tmSauY5l3s/Cp5n+cEiG1epUR2AejmdHeMJMycMFxb0=
5+
github.com/ebitengine/purego v0.0.0-20220907032450-cf3e27c364c7/go.mod h1:Eh8I3yvknDYZeCuXH9kRNaPuHEwvXDCk378o9xszmHg=
46
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
57
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
68
github.com/gdamore/tcell v1.3.0 h1:r35w0JBADPZCVQijYebl6YMWWtHRqVEGt7kL2eBADRM=
@@ -12,10 +14,8 @@ github.com/hajimehoshi/go-mp3 v0.3.0 h1:fTM5DXjp/DL2G74HHAs/aBGiS9Tg7wnp+jkU38bH
1214
github.com/hajimehoshi/go-mp3 v0.3.0/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
1315
github.com/hajimehoshi/oto v0.6.1 h1:7cJz/zRQV4aJvMSSRqzN2TImoVVMpE0BCY4nrNJaDOM=
1416
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
15-
github.com/hajimehoshi/oto v0.6.3 h1:NfrHdINv+7J8JhfkbHBROlWCzFSWc9PaHm2lS90KNzY=
16-
github.com/hajimehoshi/oto v0.6.3/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
17-
github.com/hajimehoshi/oto v0.7.1 h1:I7maFPz5MBCwiutOrz++DLdbr4rTzBsbBuV2VpgU9kk=
18-
github.com/hajimehoshi/oto v0.7.1/go.mod h1:wovJ8WWMfFKvP587mhHgot/MBr4DnNy9m6EepeVGnos=
17+
github.com/hajimehoshi/oto/v2 v2.4.0-alpha.4 h1:m29xzbn3Pv5MgvgjMPs7m28uhUgVt3B3AIGjQLgkqUI=
18+
github.com/hajimehoshi/oto/v2 v2.4.0-alpha.4/go.mod h1:OdGUICBjy7upAjvqqacbB63XIuYR3fqXZ7kYtlVYJgQ=
1919
github.com/icza/bitio v1.0.0 h1:squ/m1SHyFeCA6+6Gyol1AxV9nmPPlJFT8c2vKdj3U8=
2020
github.com/icza/bitio v1.0.0/go.mod h1:0jGnlLAx8MKMr9VGnn/4YrvZiprkvBelsVIbA9Jjr9A=
2121
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6 h1:8UsGZ2rr2ksmEru6lToqnXgA8Mz1DP11X4zSJ159C3k=
@@ -32,22 +32,18 @@ github.com/mewkiz/flac v1.0.7 h1:uIXEjnuXqdRaZttmSFM5v5Ukp4U6orrZsnYGGR3yow8=
3232
github.com/mewkiz/flac v1.0.7/go.mod h1:yU74UH277dBUpqxPouHSQIar3G1X/QIclVbFahSd1pU=
3333
github.com/mewkiz/pkg v0.0.0-20190919212034-518ade7978e2 h1:EyTNMdePWaoWsRSGQnXiSoQu0r6RS1eA557AwJhlzHU=
3434
github.com/mewkiz/pkg v0.0.0-20190919212034-518ade7978e2/go.mod h1:3E2FUC/qYUfM8+r9zAwpeHJzqRVVMIYnpzD/clwWxyA=
35-
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
3635
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
3736
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
3837
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
39-
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8 h1:idBdZTd9UioThJp8KpM/rTSinK/ChZFBE43/WtIy8zg=
4038
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
4139
golang.org/x/image v0.0.0-20190220214146-31aff87c08e9/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
42-
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067 h1:KYGJGHOQy8oSi1fDlSpcZF0+juKwk/hEMv5SiwHogR0=
4340
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
44-
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6 h1:vyLBGJPIl9ZYbcQFM2USFmJBK6KI+t+z6jL0lbwjrnc=
4541
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
4642
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
4743
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
48-
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872 h1:cGjJzUd8RgBw428LXP65YXni0aiGNA4Bl+ls8SmLOm8=
4944
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
50-
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756 h1:9nuHUbU8dRnRRfj9KjWUVrJeoexdbeMjttk6Oh1rD10=
5145
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
46+
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e h1:NHvCuwuS43lGnYhten69ZWqi2QOj/CiDNcKbVqwVoew=
47+
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5248
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
5349
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

speaker/speaker.go

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22
package speaker
33

44
import (
5-
"sync"
6-
75
"github.com/faiface/beep"
8-
"github.com/hajimehoshi/oto"
6+
"github.com/hajimehoshi/oto/v2"
97
"github.com/pkg/errors"
8+
"io"
9+
"sync"
1010
)
1111

12+
const channelCount = 2
13+
const bitDepthInBytes = 2
14+
const bytesPerSample = bitDepthInBytes * channelCount
15+
1216
var (
1317
mu sync.Mutex
1418
mixer beep.Mixer
15-
samples [][2]float64
16-
buf []byte
1719
context *oto.Context
18-
player *oto.Player
19-
done chan struct{}
20+
player oto.Player
2021
)
2122

2223
// Init initializes audio playback through speaker. Must be called before using this package.
@@ -25,36 +26,23 @@ var (
2526
// bufferSize means lower CPU usage and more reliable playback. Lower bufferSize means better
2627
// responsiveness and less delay.
2728
func Init(sampleRate beep.SampleRate, bufferSize int) error {
28-
mu.Lock()
29-
defer mu.Unlock()
30-
31-
Close()
29+
if context != nil {
30+
return errors.New("speaker cannot be initialized more than once")
31+
}
3232

3333
mixer = beep.Mixer{}
3434

35-
numBytes := bufferSize * 4
36-
samples = make([][2]float64, bufferSize)
37-
buf = make([]byte, numBytes)
38-
3935
var err error
40-
context, err = oto.NewContext(int(sampleRate), 2, 2, numBytes)
36+
var readyChan chan struct{}
37+
context, readyChan, err = oto.NewContext(int(sampleRate), channelCount, bitDepthInBytes)
4138
if err != nil {
4239
return errors.Wrap(err, "failed to initialize speaker")
4340
}
44-
player = context.NewPlayer()
45-
46-
done = make(chan struct{})
41+
<-readyChan
4742

48-
go func() {
49-
for {
50-
select {
51-
default:
52-
update()
53-
case <-done:
54-
return
55-
}
56-
}
57-
}()
43+
player = context.NewPlayer(newReaderFromStreamer(&mixer))
44+
player.(oto.BufferSizeSetter).SetBufferSize(bufferSize * bytesPerSample)
45+
player.Play()
5846

5947
return nil
6048
}
@@ -63,19 +51,16 @@ func Init(sampleRate beep.SampleRate, bufferSize int) error {
6351
// even when the program doesn't play anymore, because in properly set systems, the default mixer
6452
// handles multiple concurrent processes. It's only when the default device is not a virtual but hardware
6553
// device, that you'll probably want to manually manage the device from your application.
54+
//
55+
// TODO: investigate what happens now that oto.Context doesn't have a Close method.
6656
func Close() {
6757
if player != nil {
68-
if done != nil {
69-
done <- struct{}{}
70-
done = nil
71-
}
7258
player.Close()
73-
context.Close()
7459
player = nil
7560
}
7661
}
7762

78-
// Lock locks the speaker. While locked, speaker won't pull new data from the playing Stramers. Lock
63+
// Lock locks the speaker. While locked, speaker won't pull new data from the playing Streamers. Lock
7964
// if you want to modify any currently playing Streamers to avoid race conditions.
8065
//
8166
// Always lock speaker for as little time as possible, to avoid playback glitches.
@@ -102,16 +87,46 @@ func Clear() {
10287
mu.Unlock()
10388
}
10489

105-
// update pulls new data from the playing Streamers and sends it to the speaker. Blocks until the
106-
// data is sent and started playing.
107-
func update() {
90+
// sampleReader is a wrapper for beep.Streamer to implement io.Reader.
91+
type sampleReader struct {
92+
s beep.Streamer
93+
buf [][2]float64
94+
}
95+
96+
func newReaderFromStreamer(s beep.Streamer) *sampleReader {
97+
return &sampleReader{
98+
s: s,
99+
}
100+
}
101+
102+
// Read pulls samples from the reader and fills buf with the encoded
103+
// samples. Read expects the size of buf be divisible by the length
104+
// of a sample (= channel count * bit depth in bytes).
105+
func (s *sampleReader) Read(buf []byte) (n int, err error) {
106+
// Read samples from streamer
107+
if len(buf)%bytesPerSample != 0 {
108+
return 0, errors.New("requested number of bytes do not align with the samples")
109+
}
110+
ns := len(buf) / bytesPerSample
111+
if len(s.buf) < ns {
112+
s.buf = make([][2]float64, ns)
113+
}
108114
mu.Lock()
109-
mixer.Stream(samples)
115+
ns, ok := s.s.Stream(s.buf[:ns])
110116
mu.Unlock()
117+
if !ok {
118+
if s.s.Err() != nil {
119+
return 0, errors.Wrap(s.s.Err(), "streamer returned error when requesting samples")
120+
}
121+
if ns == 0 {
122+
return 0, io.EOF
123+
}
124+
}
111125

112-
for i := range samples {
113-
for c := range samples[i] {
114-
val := samples[i][c]
126+
// Convert samples to bytes
127+
for i := range s.buf[:ns] {
128+
for c := range s.buf[i] {
129+
val := s.buf[i][c]
115130
if val < -1 {
116131
val = -1
117132
}
@@ -121,10 +136,10 @@ func update() {
121136
valInt16 := int16(val * (1<<15 - 1))
122137
low := byte(valInt16)
123138
high := byte(valInt16 >> 8)
124-
buf[i*4+c*2+0] = low
125-
buf[i*4+c*2+1] = high
139+
buf[i*bytesPerSample+c*bitDepthInBytes+0] = low
140+
buf[i*bytesPerSample+c*bitDepthInBytes+1] = high
126141
}
127142
}
128143

129-
player.Write(buf)
144+
return ns * bytesPerSample, nil
130145
}

0 commit comments

Comments
 (0)