From 85b436b5bb76343e74682295e8f9a92d081aea81 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 5 Jun 2021 22:01:52 +0900 Subject: [PATCH] audio/internal/readerdriver: Reduce locked scopes (Linux) Updates #1549 --- audio/internal/readerdriver/driver_unix.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/audio/internal/readerdriver/driver_unix.go b/audio/internal/readerdriver/driver_unix.go index 9aea0b550..b359acd85 100644 --- a/audio/internal/readerdriver/driver_unix.go +++ b/audio/internal/readerdriver/driver_unix.go @@ -454,9 +454,9 @@ func (p *playerImpl) closeImpl() error { func (p *playerImpl) addBuffer(buf []float32) int { p.m.Lock() - defer p.m.Unlock() if p.state != playerPlay { + p.m.Unlock() return 0 } @@ -466,19 +466,22 @@ func (p *playerImpl) addBuffer(buf []float32) int { n = len(buf) } volume := float32(p.volume) + src := p.buf[:n*bitDepthInBytes] + p.buf = p.buf[n*bitDepthInBytes:] + p.m.Unlock() + for i := 0; i < n; i++ { var v float32 switch bitDepthInBytes { case 1: - v8 := p.buf[i] + v8 := src[i] v = float32(v8-(1<<7)) / (1 << 7) case 2: - v16 := int16(p.buf[2*i]) | (int16(p.buf[2*i+1]) << 8) + v16 := int16(src[2*i]) | (int16(src[2*i+1]) << 8) v = float32(v16) / (1 << 15) } buf[i] += v * volume } - p.buf = p.buf[n*bitDepthInBytes:] return n }