speaker: remove Update, add Lock and Unlock
This commit is contained in:
parent
0777dc3bc1
commit
6c527c3b89
|
@ -11,29 +11,25 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
streamerMu sync.Mutex
|
mu sync.Mutex
|
||||||
streamer audio.Streamer
|
streamer audio.Streamer
|
||||||
samples [][2]float64
|
samples [][2]float64
|
||||||
buf []byte
|
buf []byte
|
||||||
|
|
||||||
playerMu sync.Mutex
|
|
||||||
player *oto.Player
|
player *oto.Player
|
||||||
)
|
)
|
||||||
|
|
||||||
// Init initializes audio playback through speaker. Must be called before using this package. The
|
// Init initializes audio playback through speaker. Must be called before using this package. The
|
||||||
// value of audio.SampleRate must be set (or left to the default) before calling this function.
|
// value of audio.SampleRate must be set (or left to the default) before calling this function.
|
||||||
//
|
//
|
||||||
// The bufferSize argument specifies the length of the speaker's buffer. On calling Update, speaker
|
// The bufferSize argument specifies the length of the speaker's buffer. Bigger bufferSize means
|
||||||
// pulls this amount of data from the playing Streamers and starts playing this data. Bigger
|
// lower CPU usage and more reliable playback. Lower bufferSize means better responsiveness and less
|
||||||
// bufferSize means lower CPU usage and more reliable playback. Lower bufferSize means better
|
// delay.
|
||||||
// responsiveness and less delay.
|
|
||||||
func Init(bufferSize time.Duration) error {
|
func Init(bufferSize time.Duration) error {
|
||||||
playerMu.Lock()
|
mu.Lock()
|
||||||
defer playerMu.Unlock()
|
defer mu.Unlock()
|
||||||
|
|
||||||
if player != nil {
|
if player != nil {
|
||||||
player.Close()
|
panic("already called Init")
|
||||||
player = nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
numSamples := int(math.Ceil(bufferSize.Seconds() * audio.SampleRate))
|
numSamples := int(math.Ceil(bufferSize.Seconds() * audio.SampleRate))
|
||||||
|
@ -48,28 +44,43 @@ func Init(bufferSize time.Duration) error {
|
||||||
samples = make([][2]float64, numSamples)
|
samples = make([][2]float64, numSamples)
|
||||||
buf = make([]byte, numBytes)
|
buf = make([]byte, numBytes)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
update()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Lock locks the speaker. While locked, speaker won't pull new data from the playing Stramers. Lock
|
||||||
|
// if you want to modify any currently playing Streamers to avoid race conditions.
|
||||||
|
func Lock() {
|
||||||
|
mu.Lock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unlock unlocks the speaker. Call after modifying any currently playing Streamer.
|
||||||
|
func Unlock() {
|
||||||
|
mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
// Play starts playing the provided Streamer through the speaker.
|
// Play starts playing the provided Streamer through the speaker.
|
||||||
func Play(s audio.Streamer) {
|
func Play(s audio.Streamer) {
|
||||||
streamerMu.Lock()
|
mu.Lock()
|
||||||
streamer = s
|
streamer = s
|
||||||
streamerMu.Unlock()
|
mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update pulls new data from the playing Streamers and sends it to the speaker. Blocks until the
|
// update pulls new data from the playing Streamers and sends it to the speaker. Blocks until the
|
||||||
// data is sent and started playing.
|
// data is sent and started playing.
|
||||||
//
|
func update() {
|
||||||
// This function should be called at least once the duration of bufferSize given in Init, but it's
|
|
||||||
// recommended to call it more frequently to avoid glitches.
|
|
||||||
func Update() error {
|
|
||||||
streamerMu.Lock()
|
|
||||||
// pull data from the streamer, if any
|
// pull data from the streamer, if any
|
||||||
n := 0
|
n := 0
|
||||||
if streamer != nil {
|
if streamer != nil {
|
||||||
var ok bool
|
var ok bool
|
||||||
|
mu.Lock()
|
||||||
n, ok = streamer.Stream(samples)
|
n, ok = streamer.Stream(samples)
|
||||||
|
mu.Unlock()
|
||||||
if !ok {
|
if !ok {
|
||||||
streamer = nil
|
streamer = nil
|
||||||
}
|
}
|
||||||
|
@ -95,12 +106,7 @@ func Update() error {
|
||||||
for i := n * 4; i < len(buf); i++ {
|
for i := n * 4; i < len(buf); i++ {
|
||||||
buf[i] = 0
|
buf[i] = 0
|
||||||
}
|
}
|
||||||
streamerMu.Unlock()
|
|
||||||
|
|
||||||
// send data to speaker
|
// send data to speaker
|
||||||
playerMu.Lock()
|
|
||||||
player.Write(buf)
|
player.Write(buf)
|
||||||
playerMu.Unlock()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue