audio: Refactoring

This commit is contained in:
Hajime Hoshi 2015-02-14 19:55:43 +09:00
parent beecf31937
commit dc54867401
2 changed files with 11 additions and 14 deletions

View File

@ -147,17 +147,13 @@ func loadChannelBuffer(channel int, bufferSize int) (l, r []int16) {
} }
ch := channels[channel] ch := channels[channel]
inputL := make([]int16, bufferSize)
inputR := make([]int16, bufferSize)
length := min(len(ch.l), bufferSize) length := min(len(ch.l), bufferSize)
for i := 0; i < length; i++ { inputL := make([]int16, length)
inputL[i] = ch.l[i] inputR := make([]int16, length)
inputR[i] = ch.r[i] copy(inputL, ch.l[:length])
} copy(inputR, ch.r[:length])
usedLen := min(bufferSize, len(ch.l)) ch.l = ch.l[length:]
ch.l = ch.l[usedLen:] ch.r = ch.r[length:]
ch.r = ch.r[usedLen:]
ch.nextInsertionPosition -= min(bufferSize, ch.nextInsertionPosition) ch.nextInsertionPosition -= min(bufferSize, ch.nextInsertionPosition)
return inputL, inputR return inputL, inputR
} }

View File

@ -33,12 +33,13 @@ func audioProcess(channel int) func(e js.Object) {
currentPosition += bufferSize currentPosition += bufferSize
}() }()
l := e.Get("outputBuffer").Call("getChannelData", 0) b := e.Get("outputBuffer")
r := e.Get("outputBuffer").Call("getChannelData", 1) l := b.Call("getChannelData", 0)
r := b.Call("getChannelData", 1)
inputL, inputR := loadChannelBuffer(channel, bufferSize) inputL, inputR := loadChannelBuffer(channel, bufferSize)
const max = 1 << 15 const max = 1 << 15
for i := 0; i < bufferSize; i++ { for i := 0; i < len(inputL); i++ {
// TODO: Use copyFromChannel? // TODO: Use copyToChannel?
if len(inputL) <= i { if len(inputL) <= i {
l.SetIndex(i, 0) l.SetIndex(i, 0)
r.SetIndex(i, 0) r.SetIndex(i, 0)