From 45329ab32ff8ef1d599beda9c87ae6f1d0f5efb6 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 8 Feb 2016 04:27:21 +0900 Subject: [PATCH] audio: Bug fix: always fill zero values (#147) --- internal/audio/audio.go | 2 -- internal/audio/audio_js.go | 13 +++++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/audio/audio.go b/internal/audio/audio.go index 3304ed83d..47678521d 100644 --- a/internal/audio/audio.go +++ b/internal/audio/audio.go @@ -119,12 +119,10 @@ func loadChannelBuffer(channel int, bufferSize int) []byte { if !audioEnabled { return } - ch := channels[channel] length := min(len(ch.buffer), bufferSize) input := ch.buffer[:length] ch.buffer = ch.buffer[length:] - r = input }) return r diff --git a/internal/audio/audio_js.go b/internal/audio/audio_js.go index 4e9a4a94b..33d27dce9 100644 --- a/internal/audio/audio_js.go +++ b/internal/audio/audio_js.go @@ -44,16 +44,21 @@ func toLR(data []byte) ([]int16, []int16) { } func (a *audioProcessor) Process(e *js.Object) { - // Can't use 'go' here. Probably it may cause race conditions. + // Can't use goroutines here. Probably it may cause race conditions. b := e.Get("outputBuffer") l := b.Call("getChannelData", 0) r := b.Call("getChannelData", 1) inputL, inputR := toLR(loadChannelBuffer(a.channel, bufferSize*4)) const max = 1 << 15 - for i := 0; i < len(inputL); i++ { + for i := 0; i < bufferSize; i++ { // TODO: Use copyToChannel? - l.SetIndex(i, float64(inputL[i])/max) - r.SetIndex(i, float64(inputR[i])/max) + if i < len(inputL) { + l.SetIndex(i, float64(inputL[i])/max) + r.SetIndex(i, float64(inputR[i])/max) + } else { + l.SetIndex(i, 0) + r.SetIndex(i, 0) + } } }