2021-01-06 19:20:36 +01:00
|
|
|
// Copyright 2021 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
package readerdriver
|
2021-01-06 19:20:36 +01:00
|
|
|
|
|
|
|
import (
|
2021-02-20 17:47:39 +01:00
|
|
|
"errors"
|
2021-01-07 03:52:34 +01:00
|
|
|
"io"
|
2021-02-20 17:47:39 +01:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2021-01-06 19:20:36 +01:00
|
|
|
"syscall/js"
|
2021-02-20 17:47:39 +01:00
|
|
|
"unsafe"
|
2021-01-07 03:52:34 +01:00
|
|
|
|
2021-01-09 10:21:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio/internal/go2cpp"
|
2021-03-21 18:16:46 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/jsutil"
|
2021-01-06 19:20:36 +01:00
|
|
|
)
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
func IsAvailable() bool {
|
2021-02-20 17:47:39 +01:00
|
|
|
return true
|
2021-01-06 19:20:36 +01:00
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
type contextImpl struct {
|
2021-02-20 17:47:39 +01:00
|
|
|
audioContext js.Value
|
|
|
|
ready bool
|
|
|
|
callbacks map[string]js.Func
|
2021-03-28 11:07:38 +02:00
|
|
|
|
|
|
|
sampleRate int
|
|
|
|
channelNum int
|
|
|
|
bitDepthInBytes int
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
2021-04-19 16:55:26 +02:00
|
|
|
func NewContext(sampleRate int, channelNum int, bitDepthInBytes int, onReady func()) (Context, error) {
|
2021-02-20 17:47:39 +01:00
|
|
|
if js.Global().Get("go2cpp").Truthy() {
|
2021-04-19 16:59:28 +02:00
|
|
|
defer onReady()
|
2021-03-28 11:07:38 +02:00
|
|
|
return &go2cppDriverWrapper{go2cpp.NewContext(sampleRate, channelNum, bitDepthInBytes)}, nil
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class := js.Global().Get("AudioContext")
|
|
|
|
if !class.Truthy() {
|
|
|
|
class = js.Global().Get("webkitAudioContext")
|
|
|
|
}
|
|
|
|
if !class.Truthy() {
|
2021-03-28 11:07:38 +02:00
|
|
|
return nil, errors.New("readerdriver: AudioContext or webkitAudioContext was not found")
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
options := js.Global().Get("Object").New()
|
2021-03-28 11:07:38 +02:00
|
|
|
options.Set("sampleRate", sampleRate)
|
2021-02-20 17:47:39 +01:00
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
d := &contextImpl{
|
|
|
|
audioContext: class.New(options),
|
|
|
|
sampleRate: sampleRate,
|
|
|
|
channelNum: channelNum,
|
|
|
|
bitDepthInBytes: bitDepthInBytes,
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setCallback := func(event string) js.Func {
|
|
|
|
var f js.Func
|
|
|
|
f = js.FuncOf(func(this js.Value, arguments []js.Value) interface{} {
|
|
|
|
if !d.ready {
|
|
|
|
d.audioContext.Call("resume")
|
|
|
|
d.ready = true
|
2021-04-19 16:55:26 +02:00
|
|
|
onReady()
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
js.Global().Get("document").Call("removeEventListener", event, f)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
js.Global().Get("document").Call("addEventListener", event, f)
|
|
|
|
d.callbacks[event] = f
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// Browsers require user interaction to start the audio.
|
|
|
|
// https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#webaudio
|
|
|
|
d.callbacks = map[string]js.Func{}
|
|
|
|
setCallback("touchend")
|
|
|
|
setCallback("keyup")
|
|
|
|
setCallback("mouseup")
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
type playerImpl struct {
|
|
|
|
context *contextImpl
|
|
|
|
src io.Reader
|
|
|
|
eof bool
|
2021-03-28 16:27:47 +02:00
|
|
|
state playerState
|
2021-03-28 11:07:38 +02:00
|
|
|
gain js.Value
|
|
|
|
err error
|
2021-04-19 17:57:27 +02:00
|
|
|
buf []byte
|
2021-02-20 17:47:39 +01:00
|
|
|
|
|
|
|
nextPos float64
|
|
|
|
bufferSourceNodes []js.Value
|
|
|
|
appendBufferFunc js.Func
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
func (c *contextImpl) NewPlayer(src io.Reader) Player {
|
|
|
|
p := &playerImpl{
|
|
|
|
context: c,
|
|
|
|
src: src,
|
|
|
|
gain: c.audioContext.Call("createGain"),
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
p.appendBufferFunc = js.FuncOf(p.appendBuffer)
|
2021-03-28 11:07:38 +02:00
|
|
|
p.gain.Call("connect", c.audioContext.Get("destination"))
|
|
|
|
runtime.SetFinalizer(p, (*playerImpl).Close)
|
2021-02-20 17:47:39 +01:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
func (c *contextImpl) Close() error {
|
2021-02-20 17:47:39 +01:00
|
|
|
// TODO: Implement this
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
// TODO: The term 'buffer' is confusing. Name each buffer with good terms.
|
|
|
|
|
|
|
|
// oneBufferSize returns the size of one buffer in the player implementation.
|
|
|
|
func (c *contextImpl) oneBufferSize() int {
|
2021-04-19 19:39:33 +02:00
|
|
|
bytesPerSample := c.channelNum * c.bitDepthInBytes
|
|
|
|
s := c.sampleRate * bytesPerSample / 4
|
|
|
|
|
|
|
|
// Align s in multiples of bytes per sample, or a buffer could have extra bytes.
|
|
|
|
return s / bytesPerSample * bytesPerSample
|
2021-03-28 11:07:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// maxBufferSize returns the maximum size of the buffer for the audio source.
|
|
|
|
// This buffer is used when unreading on pausing the player.
|
|
|
|
func (c *contextImpl) MaxBufferSize() int {
|
|
|
|
// The number of underlying buffers should be 2.
|
|
|
|
return c.oneBufferSize() * 2
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) Pause() {
|
2021-03-28 16:27:47 +02:00
|
|
|
if p.state != playerPlay {
|
2021-02-20 17:47:39 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the state first. appendBuffer is called as an 'ended' callback.
|
|
|
|
for _, n := range p.bufferSourceNodes {
|
|
|
|
n.Set("onended", nil)
|
|
|
|
n.Call("stop")
|
|
|
|
n.Call("disconnect")
|
|
|
|
}
|
2021-03-28 16:27:47 +02:00
|
|
|
p.state = playerPaused
|
2021-02-20 17:47:39 +01:00
|
|
|
p.bufferSourceNodes = p.bufferSourceNodes[:0]
|
|
|
|
p.nextPos = 0
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
func (p *playerImpl) appendBuffer(this js.Value, args []js.Value) interface{} {
|
2021-02-20 17:47:39 +01:00
|
|
|
// appendBuffer is called as the 'ended' callback of a buffer.
|
|
|
|
// 'this' is an AudioBufferSourceNode that already finishes its playing.
|
|
|
|
for i, n := range p.bufferSourceNodes {
|
2021-03-21 18:16:46 +01:00
|
|
|
if jsutil.Equal(n, this) {
|
2021-02-20 17:47:39 +01:00
|
|
|
p.bufferSourceNodes = append(p.bufferSourceNodes[:i], p.bufferSourceNodes[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 16:27:47 +02:00
|
|
|
if p.state != playerPlay {
|
2021-02-20 17:47:39 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.eof {
|
|
|
|
if len(p.bufferSourceNodes) == 0 {
|
|
|
|
p.Pause()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
c := p.context.audioContext.Get("currentTime").Float()
|
2021-02-20 17:47:39 +01:00
|
|
|
if p.nextPos < c {
|
|
|
|
// The exact current time might be too early. Add some delay on purpose to avoid buffer overlapping.
|
|
|
|
p.nextPos = c + 1.0/60.0
|
|
|
|
}
|
|
|
|
|
2021-04-19 17:57:27 +02:00
|
|
|
tmp := make([]byte, 4096)
|
2021-04-19 20:20:29 +02:00
|
|
|
bs := make([]byte, 0, p.context.oneBufferSize())
|
|
|
|
for cap(bs)-len(bs) > 0 {
|
2021-04-19 17:57:27 +02:00
|
|
|
if len(p.buf) > 0 {
|
|
|
|
n := len(p.buf)
|
2021-04-19 20:20:29 +02:00
|
|
|
if need := cap(bs) - len(bs); n > need {
|
2021-04-19 17:57:27 +02:00
|
|
|
n = need
|
|
|
|
}
|
|
|
|
bs = append(bs, p.buf[:n]...)
|
|
|
|
p.buf = p.buf[n:]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
n, err := p.src.Read(tmp)
|
|
|
|
if err != nil && err != io.EOF {
|
2021-03-28 11:07:38 +02:00
|
|
|
p.err = err
|
|
|
|
p.Pause()
|
2021-02-20 17:47:39 +01:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-19 20:20:29 +02:00
|
|
|
if need := cap(bs) - len(bs); n > need {
|
2021-04-19 17:57:27 +02:00
|
|
|
p.buf = append(p.buf, tmp[need:]...)
|
|
|
|
n = need
|
|
|
|
}
|
|
|
|
bs = append(bs, tmp[:n]...)
|
|
|
|
if err == io.EOF {
|
|
|
|
p.eof = true
|
|
|
|
break
|
|
|
|
}
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
2021-04-19 17:57:27 +02:00
|
|
|
|
2021-02-20 17:47:39 +01:00
|
|
|
l, r := toLR(bs)
|
|
|
|
tl, tr := float32SliceToTypedArray(l), float32SliceToTypedArray(r)
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
buf := p.context.audioContext.Call("createBuffer", p.context.channelNum, len(bs)/p.context.channelNum/p.context.bitDepthInBytes, p.context.sampleRate)
|
2021-02-20 17:47:39 +01:00
|
|
|
if buf.Get("copyToChannel").Truthy() {
|
|
|
|
buf.Call("copyToChannel", tl, 0, 0)
|
|
|
|
buf.Call("copyToChannel", tr, 1, 0)
|
|
|
|
} else {
|
|
|
|
// copyToChannel is not defined on Safari 11.
|
|
|
|
buf.Call("getChannelData", 0).Call("set", tl)
|
|
|
|
buf.Call("getChannelData", 1).Call("set", tr)
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
s := p.context.audioContext.Call("createBufferSource")
|
2021-02-20 17:47:39 +01:00
|
|
|
s.Set("buffer", buf)
|
|
|
|
s.Set("onended", p.appendBufferFunc)
|
|
|
|
s.Call("connect", p.gain)
|
|
|
|
s.Call("start", p.nextPos)
|
|
|
|
p.nextPos += buf.Get("duration").Float()
|
|
|
|
p.bufferSourceNodes = append(p.bufferSourceNodes, s)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
func (p *playerImpl) Play() {
|
2021-03-28 16:27:47 +02:00
|
|
|
if p.state != playerPaused {
|
2021-02-20 17:47:39 +01:00
|
|
|
return
|
|
|
|
}
|
2021-03-28 16:27:47 +02:00
|
|
|
p.state = playerPlay
|
2021-02-20 17:47:39 +01:00
|
|
|
p.appendBuffer(js.Undefined(), nil)
|
|
|
|
p.appendBuffer(js.Undefined(), nil)
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
func (p *playerImpl) IsPlaying() bool {
|
2021-03-28 16:27:47 +02:00
|
|
|
return p.state == playerPlay
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
func (p *playerImpl) Reset() {
|
2021-03-28 16:27:47 +02:00
|
|
|
if p.state == playerClosed {
|
2021-02-20 17:47:39 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Pause()
|
|
|
|
p.eof = false
|
2021-04-19 17:57:27 +02:00
|
|
|
p.buf = p.buf[:0]
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
func (p *playerImpl) Volume() float64 {
|
2021-02-20 17:47:39 +01:00
|
|
|
return p.gain.Get("gain").Get("value").Float()
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
func (p *playerImpl) SetVolume(volume float64) {
|
2021-02-20 17:47:39 +01:00
|
|
|
p.gain.Get("gain").Set("value", volume)
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
func (p *playerImpl) UnplayedBufferSize() int64 {
|
2021-02-20 17:47:39 +01:00
|
|
|
// This is not an accurate buffer size as part of the buffers might already be consumed.
|
|
|
|
var sec float64
|
|
|
|
for _, n := range p.bufferSourceNodes {
|
|
|
|
sec += n.Get("buffer").Get("duration").Float()
|
|
|
|
}
|
2021-03-28 11:07:38 +02:00
|
|
|
return int64(sec * float64(p.context.sampleRate*p.context.channelNum*p.context.bitDepthInBytes))
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
2021-03-28 14:57:39 +02:00
|
|
|
func (p *playerImpl) Err() error {
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
func (p *playerImpl) Close() error {
|
2021-02-20 17:47:39 +01:00
|
|
|
runtime.SetFinalizer(p, nil)
|
|
|
|
p.Reset()
|
2021-03-28 16:27:47 +02:00
|
|
|
p.state = playerClosed
|
2021-02-20 17:47:39 +01:00
|
|
|
p.appendBufferFunc.Release()
|
2021-03-28 14:57:39 +02:00
|
|
|
return nil
|
2021-01-07 03:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type go2cppDriverWrapper struct {
|
|
|
|
c *go2cpp.Context
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
func (w *go2cppDriverWrapper) NewPlayer(r io.Reader) Player {
|
2021-01-07 03:52:34 +01:00
|
|
|
return w.c.NewPlayer(r)
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
func (w *go2cppDriverWrapper) MaxBufferSize() int {
|
|
|
|
return w.c.MaxBufferSize()
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:52:34 +01:00
|
|
|
func (w *go2cppDriverWrapper) Close() error {
|
|
|
|
return w.c.Close()
|
2021-01-06 19:20:36 +01:00
|
|
|
}
|
2021-02-20 17:47:39 +01:00
|
|
|
|
|
|
|
func toLR(data []byte) ([]float32, []float32) {
|
|
|
|
const max = 1 << 15
|
|
|
|
|
|
|
|
l := make([]float32, len(data)/4)
|
|
|
|
r := make([]float32, len(data)/4)
|
|
|
|
for i := 0; i < len(data)/4; i++ {
|
|
|
|
l[i] = float32(int16(data[4*i])|int16(data[4*i+1])<<8) / max
|
|
|
|
r[i] = float32(int16(data[4*i+2])|int16(data[4*i+3])<<8) / max
|
|
|
|
}
|
|
|
|
return l, r
|
|
|
|
}
|
|
|
|
|
|
|
|
func float32SliceToTypedArray(s []float32) js.Value {
|
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
|
|
|
h.Len *= 4
|
|
|
|
h.Cap *= 4
|
|
|
|
bs := *(*[]byte)(unsafe.Pointer(h))
|
|
|
|
|
|
|
|
a := js.Global().Get("Uint8Array").New(len(bs))
|
|
|
|
js.CopyBytesToJS(a, bs)
|
|
|
|
runtime.KeepAlive(s)
|
|
|
|
buf := a.Get("buffer")
|
|
|
|
return js.Global().Get("Float32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4)
|
|
|
|
}
|