audio/vorbis: Enable to decode parallelly in JavaScript

This commit is contained in:
Hajime Hoshi 2016-03-20 04:33:49 +09:00
parent 3acb3e08ce
commit 82788c45b0

View File

@ -20,6 +20,7 @@ import (
"bytes" "bytes"
"io" "io"
"io/ioutil" "io/ioutil"
"runtime"
"github.com/gopherjs/gopherjs/js" "github.com/gopherjs/gopherjs/js"
"github.com/hajimehoshi/ebiten/exp/audio" "github.com/hajimehoshi/ebiten/exp/audio"
@ -41,6 +42,7 @@ func Decode(context *audio.Context, src io.Reader) (Stream, error) {
// TODO: 1 is a correct second argument? // TODO: 1 is a correct second argument?
oc := js.Global.Get("OfflineAudioContext").New(2, 1, context.SampleRate()) oc := js.Global.Get("OfflineAudioContext").New(2, 1, context.SampleRate())
oc.Call("decodeAudioData", js.NewArrayBuffer(b), func(buf *js.Object) { oc.Call("decodeAudioData", js.NewArrayBuffer(b), func(buf *js.Object) {
go func() {
defer close(ch) defer close(ch)
il := buf.Call("getChannelData", 0).Interface().([]float32) il := buf.Call("getChannelData", 0).Interface().([]float32)
ir := buf.Call("getChannelData", 1).Interface().([]float32) ir := buf.Call("getChannelData", 1).Interface().([]float32)
@ -52,8 +54,12 @@ func Decode(context *audio.Context, src io.Reader) (Stream, error) {
b[4*i+1] = uint8(l >> 8) b[4*i+1] = uint8(l >> 8)
b[4*i+2] = uint8(r) b[4*i+2] = uint8(r)
b[4*i+3] = uint8(r >> 8) b[4*i+3] = uint8(r >> 8)
if i%16384 == 0 {
runtime.Gosched()
}
} }
s.buf = bytes.NewReader(b) s.buf = bytes.NewReader(b)
}()
}) })
<-ch <-ch
return s, nil return s, nil