audio: Change position from float64 to int64

This commit is contained in:
Hajime Hoshi 2016-03-11 01:51:13 +09:00
parent 76a2dbad26
commit 7aaa599f6b

View File

@ -26,7 +26,7 @@ import (
type player struct { type player struct {
src io.Reader src io.Reader
sampleRate int sampleRate int
position float64 positionInSamples int64
context *js.Object context *js.Object
bufferSource *js.Object bufferSource *js.Object
} }
@ -50,7 +50,7 @@ func startPlaying(src io.Reader, sampleRate int) (*player, error) {
bufferSource: nil, bufferSource: nil,
context: class.New(), context: class.New(),
} }
p.position = p.context.Get("currentTime").Float() p.positionInSamples = int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
if err := p.start(); err != nil { if err := p.start(); err != nil {
return nil, err return nil, err
} }
@ -67,7 +67,7 @@ func toLR(data []byte) ([]int16, []int16) {
return l, r return l, r
} }
func maxF(a, b float64) float64 { func max64(a, b int64) int64 {
if a > b { if a > b {
return a return a
} }
@ -76,12 +76,9 @@ func maxF(a, b float64) float64 {
func (p *player) proceed() error { func (p *player) proceed() error {
const bufferSize = 4096 const bufferSize = 4096
c := p.context.Get("currentTime").Float() c := int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
if c < p.position-bufferSize*0.5/float64(p.sampleRate) { if p.positionInSamples < c {
return nil p.positionInSamples = c
}
if p.position < c {
p.position = c
} }
b := make([]byte, bufferSize) b := make([]byte, bufferSize)
for 0 < len(b) { for 0 < len(b) {
@ -101,8 +98,8 @@ func (p *player) proceed() error {
p.bufferSource = p.context.Call("createBufferSource") p.bufferSource = p.context.Call("createBufferSource")
p.bufferSource.Set("buffer", buf) p.bufferSource.Set("buffer", buf)
p.bufferSource.Call("connect", p.context.Get("destination")) p.bufferSource.Call("connect", p.context.Get("destination"))
p.bufferSource.Call("start", maxF(p.position, c)) p.bufferSource.Call("start", float64(max64(p.positionInSamples, c))/float64(p.sampleRate))
p.position += buf.Get("duration").Float() p.positionInSamples += int64(len(il))
b = b[n:] b = b[n:]
} }
if err != nil { if err != nil {