From 7858a550881a32079882899e7f046fd2e83a6438 Mon Sep 17 00:00:00 2001 From: faiface Date: Thu, 6 Jul 2017 22:34:22 +0200 Subject: [PATCH] actually fix the race condition --- audio/speaker/speaker.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/audio/speaker/speaker.go b/audio/speaker/speaker.go index e98092d..d541b6a 100644 --- a/audio/speaker/speaker.go +++ b/audio/speaker/speaker.go @@ -64,8 +64,8 @@ func Play(s audio.Streamer) { // 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 { - // pull data from the streamer, if any streamerMu.Lock() + // pull data from the streamer, if any n := 0 if streamer != nil { var ok bool @@ -74,9 +74,6 @@ func Update() error { streamer = nil } } - streamerMu.Unlock() - - playerMu.Lock() // convert samples to bytes for i := range samples[:n] { for c := range samples[i] { @@ -98,7 +95,10 @@ func Update() error { for i := n * 4; i < len(buf); i++ { buf[i] = 0 } + streamerMu.Unlock() + // send data to speaker + playerMu.Lock() player.Write(buf) playerMu.Unlock()