2021-01-07 03:52:34 +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.
|
|
|
|
|
|
|
|
package go2cpp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"syscall/js"
|
2021-08-21 17:36:28 +02:00
|
|
|
|
2021-08-21 18:54:02 +02:00
|
|
|
"github.com/hajimehoshi/oto/v2"
|
2021-01-07 03:52:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Context struct {
|
2021-01-16 20:48:11 +01:00
|
|
|
v js.Value
|
|
|
|
sampleRate int
|
|
|
|
channelNum int
|
|
|
|
bitDepthInBytes int
|
2021-01-07 03:52:34 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 19:28:47 +01:00
|
|
|
func NewContext(sampleRate int, channelNum, bitDepthInBytes int) *Context {
|
|
|
|
v := js.Global().Get("go2cpp").Call("createAudio", sampleRate, channelNum, bitDepthInBytes)
|
2021-01-07 03:52:34 +01:00
|
|
|
return &Context{
|
2021-01-16 20:48:11 +01:00
|
|
|
v: v,
|
|
|
|
sampleRate: sampleRate,
|
|
|
|
channelNum: channelNum,
|
|
|
|
bitDepthInBytes: bitDepthInBytes,
|
2021-01-07 03:52:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 18:54:02 +02:00
|
|
|
func (c *Context) NewPlayer(r io.Reader) oto.Player {
|
2021-01-07 03:52:34 +01:00
|
|
|
cond := sync.NewCond(&sync.Mutex{})
|
|
|
|
onwritten := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
|
|
|
cond.Signal()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
p := &Player{
|
2021-01-12 03:29:23 +01:00
|
|
|
context: c,
|
2021-01-07 03:52:34 +01:00
|
|
|
src: r,
|
2021-01-12 03:29:23 +01:00
|
|
|
volume: 1,
|
2021-01-07 03:52:34 +01:00
|
|
|
cond: cond,
|
|
|
|
onWritten: onwritten,
|
|
|
|
}
|
|
|
|
runtime.SetFinalizer(p, (*Player).Close)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-08-21 17:36:28 +02:00
|
|
|
func (c *Context) Suspend() error {
|
|
|
|
// Do nothing so far.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) Resume() error {
|
|
|
|
// Do nothing so far.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
func (c *Context) oneBufferSize() int {
|
|
|
|
// TODO: This must be audio.oneBufferSize(p.context.sampleRate). Avoid the duplication.
|
|
|
|
return c.sampleRate * c.channelNum * c.bitDepthInBytes / 4
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) MaxBufferSize() int {
|
|
|
|
// TODO: This must be audio.maxBufferSize(p.context.sampleRate). Avoid the duplication.
|
|
|
|
return c.oneBufferSize() * 2
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:52:34 +01:00
|
|
|
type playerState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
playerStatePaused playerState = iota
|
|
|
|
playerStatePlaying
|
|
|
|
playerStateClosed
|
|
|
|
)
|
|
|
|
|
|
|
|
type Player struct {
|
2021-01-12 03:29:23 +01:00
|
|
|
context *Context
|
|
|
|
src io.Reader
|
|
|
|
v js.Value
|
|
|
|
state playerState
|
|
|
|
volume float64
|
|
|
|
cond *sync.Cond
|
|
|
|
err error
|
2021-01-24 19:09:13 +01:00
|
|
|
buf []byte
|
2021-01-07 03:52:34 +01:00
|
|
|
|
|
|
|
onWritten js.Func
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) Pause() {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
|
|
|
if p.state == playerStateClosed {
|
|
|
|
return
|
|
|
|
}
|
2021-01-12 03:29:23 +01:00
|
|
|
if !p.v.Truthy() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:52:34 +01:00
|
|
|
p.v.Call("pause")
|
|
|
|
p.state = playerStatePaused
|
2021-01-11 16:43:07 +01:00
|
|
|
p.cond.Signal()
|
2021-01-07 03:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) Play() {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
|
|
|
if p.state == playerStateClosed {
|
|
|
|
return
|
|
|
|
}
|
2021-01-15 19:28:47 +01:00
|
|
|
|
|
|
|
var runloop bool
|
2021-01-12 03:29:23 +01:00
|
|
|
if !p.v.Truthy() {
|
|
|
|
p.v = p.context.v.Call("createPlayer", p.onWritten)
|
2021-01-15 19:28:47 +01:00
|
|
|
runloop = true
|
2021-01-12 03:29:23 +01:00
|
|
|
}
|
2021-01-16 20:48:11 +01:00
|
|
|
|
2021-03-31 13:54:48 +02:00
|
|
|
p.v.Set("volume", p.volume)
|
2021-01-07 03:52:34 +01:00
|
|
|
p.v.Call("play")
|
2021-01-16 20:48:11 +01:00
|
|
|
|
|
|
|
// Prepare the first data as soon as possible, or the audio can get stuck.
|
|
|
|
// TODO: Get the appropriate buffer size from the C++ side.
|
2021-01-24 19:09:13 +01:00
|
|
|
if p.buf == nil {
|
2021-03-28 11:07:38 +02:00
|
|
|
n := p.context.oneBufferSize()
|
2021-05-09 20:22:19 +02:00
|
|
|
if max := p.context.MaxBufferSize() - p.UnplayedBufferSize(); n > max {
|
2021-03-23 15:34:04 +01:00
|
|
|
n = max
|
|
|
|
}
|
|
|
|
p.buf = make([]byte, n)
|
2021-01-24 19:09:13 +01:00
|
|
|
}
|
|
|
|
n, err := p.src.Read(p.buf)
|
2021-01-16 20:48:11 +01:00
|
|
|
if err != nil && err != io.EOF {
|
2021-05-03 10:54:22 +02:00
|
|
|
p.setErrorImpl(err)
|
2021-01-16 20:48:11 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if n > 0 {
|
|
|
|
dst := js.Global().Get("Uint8Array").New(n)
|
2021-01-24 19:09:13 +01:00
|
|
|
p.writeImpl(dst, p.buf[:n])
|
2021-01-16 20:48:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 19:28:47 +01:00
|
|
|
if runloop {
|
|
|
|
go p.loop()
|
|
|
|
}
|
2021-01-07 03:52:34 +01:00
|
|
|
p.state = playerStatePlaying
|
|
|
|
p.cond.Signal()
|
|
|
|
}
|
|
|
|
|
2021-01-15 17:01:19 +01:00
|
|
|
func (p *Player) IsPlaying() bool {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
|
|
|
return p.state == playerStatePlaying
|
|
|
|
}
|
|
|
|
|
2021-01-11 15:54:43 +01:00
|
|
|
func (p *Player) Reset() {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
|
|
|
if p.state == playerStateClosed {
|
|
|
|
return
|
|
|
|
}
|
2021-02-21 15:54:43 +01:00
|
|
|
p.state = playerStatePaused
|
|
|
|
|
2021-01-12 03:29:23 +01:00
|
|
|
if !p.v.Truthy() {
|
|
|
|
return
|
|
|
|
}
|
2021-01-11 15:54:43 +01:00
|
|
|
|
2021-01-12 03:29:23 +01:00
|
|
|
p.v.Call("close", true)
|
|
|
|
p.v = js.Undefined()
|
2021-01-11 15:54:43 +01:00
|
|
|
p.cond.Signal()
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:52:34 +01:00
|
|
|
func (p *Player) Volume() float64 {
|
2021-03-28 15:40:36 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
2021-01-12 03:29:23 +01:00
|
|
|
if !p.v.Truthy() {
|
|
|
|
return p.volume
|
|
|
|
}
|
2021-01-07 03:52:34 +01:00
|
|
|
return p.v.Get("volume").Float()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) SetVolume(volume float64) {
|
2021-03-28 15:40:36 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
2021-03-31 13:54:48 +02:00
|
|
|
p.volume = volume
|
2021-01-12 03:29:23 +01:00
|
|
|
if !p.v.Truthy() {
|
|
|
|
return
|
|
|
|
}
|
2021-01-07 03:52:34 +01:00
|
|
|
p.v.Set("volume", volume)
|
|
|
|
}
|
|
|
|
|
2021-05-09 20:22:19 +02:00
|
|
|
func (p *Player) UnplayedBufferSize() int {
|
2021-03-23 15:34:04 +01:00
|
|
|
if !p.v.Truthy() {
|
|
|
|
return 0
|
|
|
|
}
|
2021-05-09 20:22:19 +02:00
|
|
|
return p.v.Get("unplayedBufferSize").Int()
|
2021-02-27 17:42:48 +01:00
|
|
|
}
|
|
|
|
|
2021-03-28 14:57:39 +02:00
|
|
|
func (p *Player) Err() error {
|
2021-03-28 15:40:36 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
2021-03-28 14:57:39 +02:00
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:52:34 +01:00
|
|
|
func (p *Player) Close() error {
|
|
|
|
runtime.SetFinalizer(p, nil)
|
2021-01-12 03:29:23 +01:00
|
|
|
return p.close(true)
|
|
|
|
}
|
2021-01-07 03:52:34 +01:00
|
|
|
|
2021-01-12 03:29:23 +01:00
|
|
|
func (p *Player) close(remove bool) error {
|
2021-01-07 03:52:34 +01:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
2021-05-09 21:00:58 +02:00
|
|
|
return p.closeImpl(remove)
|
|
|
|
}
|
2021-01-07 03:52:34 +01:00
|
|
|
|
2021-05-09 21:00:58 +02:00
|
|
|
func (p *Player) closeImpl(remove bool) error {
|
2021-01-12 03:29:23 +01:00
|
|
|
if p.state == playerStateClosed {
|
2021-01-07 03:52:34 +01:00
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
|
2021-01-15 17:01:19 +01:00
|
|
|
if p.v.Truthy() {
|
|
|
|
p.v.Call("close", false)
|
|
|
|
p.v = js.Undefined()
|
|
|
|
}
|
2021-01-12 03:29:23 +01:00
|
|
|
if remove {
|
|
|
|
p.state = playerStateClosed
|
|
|
|
p.onWritten.Release()
|
|
|
|
} else {
|
|
|
|
p.state = playerStatePaused
|
|
|
|
}
|
2021-01-07 03:52:34 +01:00
|
|
|
p.cond.Signal()
|
2021-01-12 03:29:23 +01:00
|
|
|
return p.err
|
2021-01-07 03:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) setError(err error) {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
2021-05-03 10:54:22 +02:00
|
|
|
p.setErrorImpl(err)
|
|
|
|
}
|
2021-01-07 03:52:34 +01:00
|
|
|
|
2021-05-03 10:54:22 +02:00
|
|
|
func (p *Player) setErrorImpl(err error) {
|
2021-01-12 03:29:23 +01:00
|
|
|
if p.state != playerStateClosed && p.v.Truthy() {
|
|
|
|
p.v.Call("close", true)
|
|
|
|
p.v = js.Undefined()
|
2021-01-07 03:52:34 +01:00
|
|
|
}
|
|
|
|
p.err = err
|
2021-01-11 07:25:53 +01:00
|
|
|
p.state = playerStateClosed
|
2021-01-07 03:52:34 +01:00
|
|
|
p.cond.Signal()
|
|
|
|
}
|
|
|
|
|
2021-03-23 15:34:04 +01:00
|
|
|
func (p *Player) shouldWait() bool {
|
|
|
|
if !p.v.Truthy() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch p.state {
|
|
|
|
case playerStatePaused:
|
|
|
|
return true
|
|
|
|
case playerStatePlaying:
|
2021-03-28 11:07:38 +02:00
|
|
|
return p.v.Get("unplayedBufferSize").Int() >= p.context.MaxBufferSize()
|
2021-03-23 15:34:04 +01:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:52:34 +01:00
|
|
|
func (p *Player) waitUntilUnpaused() bool {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
2021-03-23 15:34:04 +01:00
|
|
|
for p.shouldWait() {
|
2021-01-07 03:52:34 +01:00
|
|
|
p.cond.Wait()
|
|
|
|
}
|
2021-01-12 03:29:23 +01:00
|
|
|
return p.v.Truthy() && p.state == playerStatePlaying
|
2021-01-07 03:52:34 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 19:28:47 +01:00
|
|
|
func (p *Player) writeImpl(dst js.Value, src []byte) {
|
2021-01-12 18:06:40 +01:00
|
|
|
if p.state == playerStateClosed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !p.v.Truthy() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
js.CopyBytesToJS(dst, src)
|
|
|
|
p.v.Call("write", dst, len(src))
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:52:34 +01:00
|
|
|
func (p *Player) loop() {
|
2021-05-09 20:35:21 +02:00
|
|
|
const readChunkSize = 4096
|
|
|
|
|
2021-03-23 15:34:04 +01:00
|
|
|
buf := make([]byte, readChunkSize)
|
|
|
|
dst := js.Global().Get("Uint8Array").New(readChunkSize)
|
2021-01-07 03:52:34 +01:00
|
|
|
|
|
|
|
for {
|
|
|
|
if !p.waitUntilUnpaused() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-09 21:00:58 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
n, err := p.src.Read(buf)
|
2021-01-07 03:52:34 +01:00
|
|
|
if err != nil && err != io.EOF {
|
2021-05-09 21:00:58 +02:00
|
|
|
p.setErrorImpl(err)
|
|
|
|
p.cond.L.Unlock()
|
2021-01-07 03:52:34 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if n > 0 {
|
2021-05-09 21:00:58 +02:00
|
|
|
p.writeImpl(dst, buf[:n])
|
2021-01-07 03:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err == io.EOF {
|
2021-05-09 21:00:58 +02:00
|
|
|
p.closeImpl(false)
|
|
|
|
p.cond.L.Unlock()
|
2021-01-07 03:52:34 +01:00
|
|
|
return
|
|
|
|
}
|
2021-05-09 21:00:58 +02:00
|
|
|
p.cond.L.Unlock()
|
2021-01-07 03:52:34 +01:00
|
|
|
}
|
|
|
|
}
|