Skip to content

Commit

Permalink
darwin: Bug fix: Pause the audio when input does not come
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Nov 10, 2019
1 parent cdf15e4 commit 6298480
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions driver_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,25 +159,35 @@ func oto_render(inUserData unsafe.Pointer, inAQ C.AudioQueueRef, inBuffer C.Audi
d := getDriver()

var buf []byte
loop:
for len(buf) < queueBufferSize {
// Set the timer. When the application is in background or being switched, the driver's buffer is not
// updated and it is needed to fill the buffer with zeros.
s := time.Second * time.Duration(queueBufferSize) / time.Duration(d.sampleRate*d.audioInfo.channelNum*d.audioInfo.bitDepthInBytes)
t := time.NewTicker(s)
defer t.Stop()

// Set the timer. When the application is in background or being switched, the driver's buffer is not
// updated and it is needed to fill the buffer with zeros.
s := time.Second * time.Duration(queueBufferSize) / time.Duration(d.sampleRate*d.audioInfo.channelNum*d.audioInfo.bitDepthInBytes)
t := time.NewTicker(s)
defer t.Stop()
ch := t.C

paused := false

for len(buf) < queueBufferSize {
select {
case dbuf := <-d.chWrite:
if paused {
C.AudioQueueStart(inAQ, nil)
paused = false
}
n := queueBufferSize - len(buf)
if n > len(dbuf) {
n = len(dbuf)
}
buf = append(buf, dbuf[:n]...)
d.chWritten <- n
case <-t.C:
buf = append(buf, make([]byte, queueBufferSize-len(buf))...)
break loop
case <-ch:
if !paused {
C.AudioQueuePause(inAQ)
paused = true
}
ch = nil
}
}

Expand Down

0 comments on commit 6298480

Please sign in to comment.