mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
audio/vorbis: Enable to decode parallelly in JavaScript
This commit is contained in:
parent
3acb3e08ce
commit
82788c45b0
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user