audio: Inifite stream in JS

This commit is contained in:
Hajime Hoshi 2016-02-13 22:36:08 +09:00
parent 797bd04b1f
commit 5dbdb23b46

View File

@ -18,7 +18,7 @@ package audio
import (
"io"
"io/ioutil"
"time"
"github.com/gopherjs/gopherjs/js"
)
@ -75,26 +75,16 @@ func toLR(data []byte) ([]int16, []int16) {
return l, r
}
func (p *player) play() error {
// TODO: Reading all data at once is temporary implemntation. Treat this as stream.
buf, err := ioutil.ReadAll(p.src)
if err != nil {
return err
}
if len(buf) == 0 {
return nil
}
// TODO: p.position should be updated
if p.bufferSource != nil {
p.bufferSource.Call("start", p.position)
return nil
}
func (p *player) proceed() error {
buf := make([]byte, 4096)
n, err := p.src.Read(buf)
if 0 < n {
const channelNum = 2
const bytesPerSample = channelNum * 16 / 8
b := context.Call("createBuffer", channelNum, len(buf)/bytesPerSample, p.sampleRate)
b := context.Call("createBuffer", channelNum, n/bytesPerSample, p.sampleRate)
l := b.Call("getChannelData", 0)
r := b.Call("getChannelData", 1)
il, ir := toLR(buf)
il, ir := toLR(buf[:n])
const max = 1 << 15
for i := 0; i < len(il); i++ {
l.SetIndex(i, float64(il[i])/max)
@ -105,11 +95,35 @@ func (p *player) play() error {
p.bufferSource.Call("connect", context.Get("destination"))
p.bufferSource.Call("start", p.position)
p.position += b.Get("duration").Float()
}
return err
}
func (p *player) play() error {
// TODO: What if play is already called?
go func() {
defer p.close()
for {
err := p.proceed()
if err == io.EOF {
break
}
if err != nil {
// TODO: Record the last error
panic(err)
}
time.Sleep(1)
}
}()
return nil
}
func (p *player) close() error {
if p.bufferSource == nil {
return nil
}
p.bufferSource.Call("stop")
p.bufferSource.Call("disconnect")
p.bufferSource = nil
return nil
}