mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 19:28:57 +01:00
audio/internal/readerdriver: Make the implementation thread safe (Unix)
Closes #1771
This commit is contained in:
parent
9f0e95f6a2
commit
c87e0df8a4
@ -24,6 +24,7 @@ import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
@ -37,9 +38,13 @@ type context struct {
|
||||
channelNum int
|
||||
bitDepthInBytes int
|
||||
|
||||
suspended bool
|
||||
|
||||
handle *C.snd_pcm_t
|
||||
supportsPause bool
|
||||
|
||||
cond *sync.Cond
|
||||
|
||||
players *players
|
||||
}
|
||||
|
||||
@ -57,6 +62,7 @@ func NewContext(sampleRate, channelNum, bitDepthInBytes int) (Context, chan stru
|
||||
sampleRate: sampleRate,
|
||||
channelNum: channelNum,
|
||||
bitDepthInBytes: bitDepthInBytes,
|
||||
cond: sync.NewCond(&sync.Mutex{}),
|
||||
players: newPlayers(),
|
||||
}
|
||||
theContext = c
|
||||
@ -124,6 +130,13 @@ func (c *context) alsaPcmHwParams(sampleRate, channelNum int, bufferSize, period
|
||||
}
|
||||
|
||||
func (c *context) readAndWrite(buf32 []float32) error {
|
||||
c.cond.L.Lock()
|
||||
defer c.cond.L.Unlock()
|
||||
|
||||
for c.suspended {
|
||||
c.cond.Wait()
|
||||
}
|
||||
|
||||
for i := range buf32 {
|
||||
buf32[i] = 0
|
||||
}
|
||||
@ -147,6 +160,10 @@ func (c *context) readAndWrite(buf32 []float32) error {
|
||||
}
|
||||
|
||||
func (c *context) Suspend() error {
|
||||
c.cond.L.Lock()
|
||||
defer c.cond.L.Unlock()
|
||||
c.suspended = true
|
||||
|
||||
if c.supportsPause {
|
||||
if err := C.snd_pcm_pause(c.handle, 1); err < 0 {
|
||||
return alsaError(err)
|
||||
@ -161,6 +178,13 @@ func (c *context) Suspend() error {
|
||||
}
|
||||
|
||||
func (c *context) Resume() error {
|
||||
c.cond.L.Lock()
|
||||
defer c.cond.L.Unlock()
|
||||
defer func() {
|
||||
c.suspended = false
|
||||
c.cond.Signal()
|
||||
}()
|
||||
|
||||
if c.supportsPause {
|
||||
if err := C.snd_pcm_pause(c.handle, 0); err < 0 {
|
||||
return alsaError(err)
|
||||
|
Loading…
Reference in New Issue
Block a user