From dc548674015f8d38fa571dd907954e79d8879755 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 14 Feb 2015 19:55:43 +0900 Subject: [PATCH] audio: Refactoring --- exp/audio/internal/audio.go | 16 ++++++---------- exp/audio/internal/audio_js.go | 9 +++++---- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/exp/audio/internal/audio.go b/exp/audio/internal/audio.go index edd64a8d8..749be5506 100644 --- a/exp/audio/internal/audio.go +++ b/exp/audio/internal/audio.go @@ -147,17 +147,13 @@ func loadChannelBuffer(channel int, bufferSize int) (l, r []int16) { } ch := channels[channel] - inputL := make([]int16, bufferSize) - inputR := make([]int16, bufferSize) length := min(len(ch.l), bufferSize) - for i := 0; i < length; i++ { - inputL[i] = ch.l[i] - inputR[i] = ch.r[i] - } - usedLen := min(bufferSize, len(ch.l)) - ch.l = ch.l[usedLen:] - ch.r = ch.r[usedLen:] - + inputL := make([]int16, length) + inputR := make([]int16, length) + copy(inputL, ch.l[:length]) + copy(inputR, ch.r[:length]) + ch.l = ch.l[length:] + ch.r = ch.r[length:] ch.nextInsertionPosition -= min(bufferSize, ch.nextInsertionPosition) return inputL, inputR } diff --git a/exp/audio/internal/audio_js.go b/exp/audio/internal/audio_js.go index f286b56bb..41fd81731 100644 --- a/exp/audio/internal/audio_js.go +++ b/exp/audio/internal/audio_js.go @@ -33,12 +33,13 @@ func audioProcess(channel int) func(e js.Object) { currentPosition += bufferSize }() - l := e.Get("outputBuffer").Call("getChannelData", 0) - r := e.Get("outputBuffer").Call("getChannelData", 1) + b := e.Get("outputBuffer") + l := b.Call("getChannelData", 0) + r := b.Call("getChannelData", 1) inputL, inputR := loadChannelBuffer(channel, bufferSize) const max = 1 << 15 - for i := 0; i < bufferSize; i++ { - // TODO: Use copyFromChannel? + for i := 0; i < len(inputL); i++ { + // TODO: Use copyToChannel? if len(inputL) <= i { l.SetIndex(i, 0) r.SetIndex(i, 0)