speaker: fix race condition

This commit is contained in:
faiface 2017-07-06 22:26:20 +02:00
parent 0737b86059
commit fdb6359fbb
1 changed files with 2 additions and 3 deletions

View File

@ -77,6 +77,7 @@ func Update() error {
} }
streamerMu.Unlock() streamerMu.Unlock()
playerMu.Lock()
// convert samples to bytes // convert samples to bytes
for i := range samples[:n] { for i := range samples[:n] {
for c := range samples[i] { for c := range samples[i] {
@ -94,13 +95,11 @@ func Update() error {
buf[i*4+c*2+1] = high buf[i*4+c*2+1] = high
} }
} }
// fill the rest with silence // fill the rest with silence
for i := n * 4; i < len(buf); i++ { for i := n * 4; i < len(buf); i++ {
buf[i] = 0 buf[i] = 0
} }
// send data to speaker
playerMu.Lock()
player.Write(buf) player.Write(buf)
playerMu.Unlock() playerMu.Unlock()