audio: Refactoring: Remove unneeded for loop

This commit is contained in:
Hajime Hoshi 2016-03-11 02:09:12 +09:00
parent 7aaa599f6b
commit 1313644611

View File

@ -81,33 +81,26 @@ func (p *player) proceed() error {
p.positionInSamples = c p.positionInSamples = c
} }
b := make([]byte, bufferSize) b := make([]byte, bufferSize)
for 0 < len(b) { n, err := p.src.Read(b)
n, err := p.src.Read(b) if 0 < n {
if 0 < n { const channelNum = 2
const channelNum = 2 const bytesPerSample = channelNum * 16 / 8
const bytesPerSample = channelNum * 16 / 8 buf := p.context.Call("createBuffer", channelNum, n/bytesPerSample, p.sampleRate)
buf := p.context.Call("createBuffer", channelNum, n/bytesPerSample, p.sampleRate) l := buf.Call("getChannelData", 0)
l := buf.Call("getChannelData", 0) r := buf.Call("getChannelData", 1)
r := buf.Call("getChannelData", 1) il, ir := toLR(b[:n])
il, ir := toLR(b[:n]) const max = 1 << 15
const max = 1 << 15 for i := 0; i < len(il); i++ {
for i := 0; i < len(il); i++ { l.SetIndex(i, float64(il[i])/max)
l.SetIndex(i, float64(il[i])/max) r.SetIndex(i, float64(ir[i])/max)
r.SetIndex(i, float64(ir[i])/max)
}
p.bufferSource = p.context.Call("createBufferSource")
p.bufferSource.Set("buffer", buf)
p.bufferSource.Call("connect", p.context.Get("destination"))
p.bufferSource.Call("start", float64(max64(p.positionInSamples, c))/float64(p.sampleRate))
p.positionInSamples += int64(len(il))
b = b[n:]
} }
if err != nil { p.bufferSource = p.context.Call("createBufferSource")
return err p.bufferSource.Set("buffer", buf)
} p.bufferSource.Call("connect", p.context.Get("destination"))
runtime.Gosched() p.bufferSource.Call("start", float64(p.positionInSamples)/float64(p.sampleRate))
p.positionInSamples += int64(len(il))
} }
return nil return err
} }
func (p *player) start() error { func (p *player) start() error {