mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-12 12:08:58 +01:00
audio: Inifite stream in JS
This commit is contained in:
parent
797bd04b1f
commit
5dbdb23b46
@ -18,7 +18,7 @@ package audio
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"time"
|
||||||
|
|
||||||
"github.com/gopherjs/gopherjs/js"
|
"github.com/gopherjs/gopherjs/js"
|
||||||
)
|
)
|
||||||
@ -75,26 +75,16 @@ func toLR(data []byte) ([]int16, []int16) {
|
|||||||
return l, r
|
return l, r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *player) play() error {
|
func (p *player) proceed() error {
|
||||||
// TODO: Reading all data at once is temporary implemntation. Treat this as stream.
|
buf := make([]byte, 4096)
|
||||||
buf, err := ioutil.ReadAll(p.src)
|
n, err := p.src.Read(buf)
|
||||||
if err != nil {
|
if 0 < n {
|
||||||
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
|
|
||||||
}
|
|
||||||
const channelNum = 2
|
const channelNum = 2
|
||||||
const bytesPerSample = channelNum * 16 / 8
|
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)
|
l := b.Call("getChannelData", 0)
|
||||||
r := b.Call("getChannelData", 1)
|
r := b.Call("getChannelData", 1)
|
||||||
il, ir := toLR(buf)
|
il, ir := toLR(buf[: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)
|
||||||
@ -105,11 +95,35 @@ func (p *player) play() error {
|
|||||||
p.bufferSource.Call("connect", context.Get("destination"))
|
p.bufferSource.Call("connect", context.Get("destination"))
|
||||||
p.bufferSource.Call("start", p.position)
|
p.bufferSource.Call("start", p.position)
|
||||||
p.position += b.Get("duration").Float()
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *player) close() error {
|
func (p *player) close() error {
|
||||||
p.bufferSource.Call("stop")
|
if p.bufferSource == nil {
|
||||||
p.bufferSource.Call("disconnect")
|
return nil
|
||||||
|
}
|
||||||
|
p.bufferSource.Call("stop")
|
||||||
|
p.bufferSource.Call("disconnect")
|
||||||
|
p.bufferSource = nil
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user