mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 11:48:55 +01:00
audio: Add audio/internal/readerdriver package
This commit is contained in:
parent
19702619ee
commit
b46cb324ed
@ -40,6 +40,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/audio/internal/readerdriver"
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
|
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -101,7 +102,7 @@ func NewContext(sampleRate int) *Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var np newPlayerImpler
|
var np newPlayerImpler
|
||||||
if isReaderContextAvailable() {
|
if readerdriver.IsAvailable() {
|
||||||
// 'Reader players' are players that implement io.Reader. This is the new way and
|
// 'Reader players' are players that implement io.Reader. This is the new way and
|
||||||
// not all the environments support reader players. Reader players can have enough
|
// not all the environments support reader players. Reader players can have enough
|
||||||
// buffers so that clicking noises can be avoided compared to writer players.
|
// buffers so that clicking noises can be avoided compared to writer players.
|
||||||
|
@ -18,18 +18,20 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/audio/internal/readerdriver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
dummyWriterPlayerDriver struct{}
|
dummyWriterContext struct{}
|
||||||
dummyWriterPlayer struct{}
|
dummyWriterPlayer struct{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (d *dummyWriterPlayerDriver) NewPlayer() io.WriteCloser {
|
func (c *dummyWriterContext) NewPlayer() io.WriteCloser {
|
||||||
return &dummyWriterPlayer{}
|
return &dummyWriterPlayer{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dummyWriterPlayerDriver) Close() error {
|
func (c *dummyWriterContext) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,11 +44,11 @@ func (p *dummyWriterPlayer) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
writerDriverForTesting = &dummyWriterPlayerDriver{}
|
writerDriverForTesting = &dummyWriterContext{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type (
|
type (
|
||||||
dummyReaderPlayerDriver struct{}
|
dummyReaderContext struct{}
|
||||||
dummyReaderPlayer struct {
|
dummyReaderPlayer struct {
|
||||||
r io.Reader
|
r io.Reader
|
||||||
playing bool
|
playing bool
|
||||||
@ -55,14 +57,18 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (d *dummyReaderPlayerDriver) NewPlayer(r io.Reader) readerDriverPlayer {
|
func (c *dummyReaderContext) NewPlayer(r io.Reader) readerdriver.Player {
|
||||||
return &dummyReaderPlayer{
|
return &dummyReaderPlayer{
|
||||||
r: r,
|
r: r,
|
||||||
volume: 1,
|
volume: 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dummyReaderPlayerDriver) Close() error {
|
func (c *dummyReaderContext) MaxBufferSize() int {
|
||||||
|
return 48000 * channelNum * bitDepthInBytes / 4
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *dummyReaderContext) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,7 +124,7 @@ func (p *dummyReaderPlayer) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
readerDriverForTesting = &dummyReaderPlayerDriver{}
|
readerDriverForTesting = &dummyReaderContext{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type dummyHook struct {
|
type dummyHook struct {
|
||||||
|
@ -63,6 +63,16 @@ func (c *Context) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
type playerState int
|
type playerState int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -100,16 +110,6 @@ func (p *Player) Pause() {
|
|||||||
p.cond.Signal()
|
p.cond.Signal()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) oneBufferSize() int {
|
|
||||||
// TODO: This must be audio.oneBufferSize(p.context.sampleRate). Avoid the duplication.
|
|
||||||
return p.context.sampleRate * p.context.channelNum * p.context.bitDepthInBytes / 4
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Player) maxBufferSize() int {
|
|
||||||
// TODO: This must be audio.maxBufferSize(p.context.sampleRate). Avoid the duplication.
|
|
||||||
return p.oneBufferSize() * 2
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Player) Play() {
|
func (p *Player) Play() {
|
||||||
p.cond.L.Lock()
|
p.cond.L.Lock()
|
||||||
defer p.cond.L.Unlock()
|
defer p.cond.L.Unlock()
|
||||||
@ -130,8 +130,8 @@ func (p *Player) Play() {
|
|||||||
// Prepare the first data as soon as possible, or the audio can get stuck.
|
// Prepare the first data as soon as possible, or the audio can get stuck.
|
||||||
// TODO: Get the appropriate buffer size from the C++ side.
|
// TODO: Get the appropriate buffer size from the C++ side.
|
||||||
if p.buf == nil {
|
if p.buf == nil {
|
||||||
n := p.oneBufferSize()
|
n := p.context.oneBufferSize()
|
||||||
if max := p.maxBufferSize() - int(p.UnplayedBufferSize()); n > max {
|
if max := p.context.MaxBufferSize() - int(p.UnplayedBufferSize()); n > max {
|
||||||
n = max
|
n = max
|
||||||
}
|
}
|
||||||
p.buf = make([]byte, n)
|
p.buf = make([]byte, n)
|
||||||
@ -248,7 +248,7 @@ func (p *Player) shouldWait() bool {
|
|||||||
case playerStatePaused:
|
case playerStatePaused:
|
||||||
return true
|
return true
|
||||||
case playerStatePlaying:
|
case playerStatePlaying:
|
||||||
return p.v.Get("unplayedBufferSize").Int() >= p.maxBufferSize()
|
return p.v.Get("unplayedBufferSize").Int() >= p.context.MaxBufferSize()
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -291,7 +291,7 @@ func (p *Player) loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
n := readChunkSize
|
n := readChunkSize
|
||||||
if max := p.maxBufferSize() - int(p.UnplayedBufferSize()); n > max {
|
if max := p.context.MaxBufferSize() - int(p.UnplayedBufferSize()); n > max {
|
||||||
n = max
|
n = max
|
||||||
}
|
}
|
||||||
n2, err := p.src.Read(buf[:n])
|
n2, err := p.src.Read(buf[:n])
|
||||||
|
36
audio/internal/readerdriver/driver.go
Normal file
36
audio/internal/readerdriver/driver.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
// 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 readerdriver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Context interface {
|
||||||
|
NewPlayer(io.Reader) Player
|
||||||
|
MaxBufferSize() int
|
||||||
|
io.Closer
|
||||||
|
}
|
||||||
|
|
||||||
|
type Player interface {
|
||||||
|
Pause()
|
||||||
|
Play()
|
||||||
|
IsPlaying() bool
|
||||||
|
Reset()
|
||||||
|
Volume() float64
|
||||||
|
SetVolume(volume float64)
|
||||||
|
UnplayedBufferSize() int64
|
||||||
|
io.Closer
|
||||||
|
}
|
@ -14,17 +14,17 @@
|
|||||||
|
|
||||||
// +build !js
|
// +build !js
|
||||||
|
|
||||||
package audio
|
package readerdriver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func isReaderContextAvailable() bool {
|
func IsAvailable() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func newReaderDriverImpl(context *Context) (readerDriver, error) {
|
func NewContext(sampleRate int, channelNum int, bitDepthInBytes int) (Context, error) {
|
||||||
panic(fmt.Sprintf("audio: newReaderDriver is not available on this environment: GOOS=%s", runtime.GOOS))
|
panic(fmt.Sprintf("readerdriver: NewContext is not available on this environment: GOOS=%s", runtime.GOOS))
|
||||||
}
|
}
|
@ -12,7 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package audio
|
package readerdriver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -26,20 +26,23 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/v2/internal/jsutil"
|
"github.com/hajimehoshi/ebiten/v2/internal/jsutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func isReaderContextAvailable() bool {
|
func IsAvailable() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
type readerDriverImpl struct {
|
type contextImpl struct {
|
||||||
context *Context
|
|
||||||
audioContext js.Value
|
audioContext js.Value
|
||||||
ready bool
|
ready bool
|
||||||
callbacks map[string]js.Func
|
callbacks map[string]js.Func
|
||||||
|
|
||||||
|
sampleRate int
|
||||||
|
channelNum int
|
||||||
|
bitDepthInBytes int
|
||||||
}
|
}
|
||||||
|
|
||||||
func newReaderDriverImpl(context *Context) (readerDriver, error) {
|
func NewContext(sampleRate int, channelNum int, bitDepthInBytes int) (Context, error) {
|
||||||
if js.Global().Get("go2cpp").Truthy() {
|
if js.Global().Get("go2cpp").Truthy() {
|
||||||
return &go2cppDriverWrapper{go2cpp.NewContext(context.sampleRate, channelNum, bitDepthInBytes)}, nil
|
return &go2cppDriverWrapper{go2cpp.NewContext(sampleRate, channelNum, bitDepthInBytes)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
class := js.Global().Get("AudioContext")
|
class := js.Global().Get("AudioContext")
|
||||||
@ -47,14 +50,16 @@ func newReaderDriverImpl(context *Context) (readerDriver, error) {
|
|||||||
class = js.Global().Get("webkitAudioContext")
|
class = js.Global().Get("webkitAudioContext")
|
||||||
}
|
}
|
||||||
if !class.Truthy() {
|
if !class.Truthy() {
|
||||||
return nil, errors.New("audio: AudioContext or webkitAudioContext was not found")
|
return nil, errors.New("readerdriver: AudioContext or webkitAudioContext was not found")
|
||||||
}
|
}
|
||||||
options := js.Global().Get("Object").New()
|
options := js.Global().Get("Object").New()
|
||||||
options.Set("sampleRate", context.sampleRate)
|
options.Set("sampleRate", sampleRate)
|
||||||
|
|
||||||
d := &readerDriverImpl{
|
d := &contextImpl{
|
||||||
context: context,
|
|
||||||
audioContext: class.New(options),
|
audioContext: class.New(options),
|
||||||
|
sampleRate: sampleRate,
|
||||||
|
channelNum: channelNum,
|
||||||
|
bitDepthInBytes: bitDepthInBytes,
|
||||||
}
|
}
|
||||||
|
|
||||||
setCallback := func(event string) js.Func {
|
setCallback := func(event string) js.Func {
|
||||||
@ -90,36 +95,51 @@ const (
|
|||||||
readerPlayerClosed
|
readerPlayerClosed
|
||||||
)
|
)
|
||||||
|
|
||||||
type readerDriverPlayerImpl struct {
|
type playerImpl struct {
|
||||||
driver *readerDriverImpl
|
context *contextImpl
|
||||||
src io.Reader
|
src io.Reader
|
||||||
eof bool
|
eof bool
|
||||||
state readerPlayerState
|
state readerPlayerState
|
||||||
gain js.Value
|
gain js.Value
|
||||||
|
err error
|
||||||
|
|
||||||
nextPos float64
|
nextPos float64
|
||||||
bufferSourceNodes []js.Value
|
bufferSourceNodes []js.Value
|
||||||
appendBufferFunc js.Func
|
appendBufferFunc js.Func
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *readerDriverImpl) NewPlayer(src io.Reader) readerDriverPlayer {
|
func (c *contextImpl) NewPlayer(src io.Reader) Player {
|
||||||
p := &readerDriverPlayerImpl{
|
p := &playerImpl{
|
||||||
driver: d,
|
context: c,
|
||||||
src: src,
|
src: src,
|
||||||
gain: d.audioContext.Call("createGain"),
|
gain: c.audioContext.Call("createGain"),
|
||||||
}
|
}
|
||||||
p.appendBufferFunc = js.FuncOf(p.appendBuffer)
|
p.appendBufferFunc = js.FuncOf(p.appendBuffer)
|
||||||
p.gain.Call("connect", d.audioContext.Get("destination"))
|
p.gain.Call("connect", c.audioContext.Get("destination"))
|
||||||
runtime.SetFinalizer(p, (*readerDriverPlayerImpl).Close)
|
runtime.SetFinalizer(p, (*playerImpl).Close)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *readerDriverImpl) Close() error {
|
func (c *contextImpl) Close() error {
|
||||||
// TODO: Implement this
|
// TODO: Implement this
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *readerDriverPlayerImpl) Pause() {
|
// 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 {
|
||||||
|
return c.sampleRate * c.channelNum * c.bitDepthInBytes / 4
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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() {
|
||||||
if p.state != readerPlayerPlay {
|
if p.state != readerPlayerPlay {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -135,7 +155,7 @@ func (p *readerDriverPlayerImpl) Pause() {
|
|||||||
p.nextPos = 0
|
p.nextPos = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *readerDriverPlayerImpl) appendBuffer(this js.Value, args []js.Value) interface{} {
|
func (p *playerImpl) appendBuffer(this js.Value, args []js.Value) interface{} {
|
||||||
// appendBuffer is called as the 'ended' callback of a buffer.
|
// appendBuffer is called as the 'ended' callback of a buffer.
|
||||||
// 'this' is an AudioBufferSourceNode that already finishes its playing.
|
// 'this' is an AudioBufferSourceNode that already finishes its playing.
|
||||||
for i, n := range p.bufferSourceNodes {
|
for i, n := range p.bufferSourceNodes {
|
||||||
@ -156,17 +176,18 @@ func (p *readerDriverPlayerImpl) appendBuffer(this js.Value, args []js.Value) in
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
c := p.driver.audioContext.Get("currentTime").Float()
|
c := p.context.audioContext.Get("currentTime").Float()
|
||||||
if p.nextPos < c {
|
if p.nextPos < c {
|
||||||
// The exact current time might be too early. Add some delay on purpose to avoid buffer overlapping.
|
// The exact current time might be too early. Add some delay on purpose to avoid buffer overlapping.
|
||||||
p.nextPos = c + 1.0/60.0
|
p.nextPos = c + 1.0/60.0
|
||||||
}
|
}
|
||||||
|
|
||||||
bs := make([]byte, oneBufferSize(p.driver.context.sampleRate))
|
bs := make([]byte, p.context.oneBufferSize())
|
||||||
n, err := io.ReadFull(p.src, bs)
|
n, err := io.ReadFull(p.src, bs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err != io.EOF && err != io.ErrUnexpectedEOF {
|
if err != io.EOF && err != io.ErrUnexpectedEOF {
|
||||||
p.driver.context.setError(err)
|
p.err = err
|
||||||
|
p.Pause()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
p.eof = true
|
p.eof = true
|
||||||
@ -178,7 +199,7 @@ func (p *readerDriverPlayerImpl) appendBuffer(this js.Value, args []js.Value) in
|
|||||||
l, r := toLR(bs)
|
l, r := toLR(bs)
|
||||||
tl, tr := float32SliceToTypedArray(l), float32SliceToTypedArray(r)
|
tl, tr := float32SliceToTypedArray(l), float32SliceToTypedArray(r)
|
||||||
|
|
||||||
buf := p.driver.audioContext.Call("createBuffer", channelNum, len(bs)/channelNum/bitDepthInBytes, p.driver.context.sampleRate)
|
buf := p.context.audioContext.Call("createBuffer", p.context.channelNum, len(bs)/p.context.channelNum/p.context.bitDepthInBytes, p.context.sampleRate)
|
||||||
if buf.Get("copyToChannel").Truthy() {
|
if buf.Get("copyToChannel").Truthy() {
|
||||||
buf.Call("copyToChannel", tl, 0, 0)
|
buf.Call("copyToChannel", tl, 0, 0)
|
||||||
buf.Call("copyToChannel", tr, 1, 0)
|
buf.Call("copyToChannel", tr, 1, 0)
|
||||||
@ -188,7 +209,7 @@ func (p *readerDriverPlayerImpl) appendBuffer(this js.Value, args []js.Value) in
|
|||||||
buf.Call("getChannelData", 1).Call("set", tr)
|
buf.Call("getChannelData", 1).Call("set", tr)
|
||||||
}
|
}
|
||||||
|
|
||||||
s := p.driver.audioContext.Call("createBufferSource")
|
s := p.context.audioContext.Call("createBufferSource")
|
||||||
s.Set("buffer", buf)
|
s.Set("buffer", buf)
|
||||||
s.Set("onended", p.appendBufferFunc)
|
s.Set("onended", p.appendBufferFunc)
|
||||||
s.Call("connect", p.gain)
|
s.Call("connect", p.gain)
|
||||||
@ -199,7 +220,7 @@ func (p *readerDriverPlayerImpl) appendBuffer(this js.Value, args []js.Value) in
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *readerDriverPlayerImpl) Play() {
|
func (p *playerImpl) Play() {
|
||||||
if p.state != readerPlayerPaused {
|
if p.state != readerPlayerPaused {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -208,11 +229,11 @@ func (p *readerDriverPlayerImpl) Play() {
|
|||||||
p.appendBuffer(js.Undefined(), nil)
|
p.appendBuffer(js.Undefined(), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *readerDriverPlayerImpl) IsPlaying() bool {
|
func (p *playerImpl) IsPlaying() bool {
|
||||||
return p.state == readerPlayerPlay
|
return p.state == readerPlayerPlay
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *readerDriverPlayerImpl) Reset() {
|
func (p *playerImpl) Reset() {
|
||||||
if p.state == readerPlayerClosed {
|
if p.state == readerPlayerClosed {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -221,39 +242,43 @@ func (p *readerDriverPlayerImpl) Reset() {
|
|||||||
p.eof = false
|
p.eof = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *readerDriverPlayerImpl) Volume() float64 {
|
func (p *playerImpl) Volume() float64 {
|
||||||
return p.gain.Get("gain").Get("value").Float()
|
return p.gain.Get("gain").Get("value").Float()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *readerDriverPlayerImpl) SetVolume(volume float64) {
|
func (p *playerImpl) SetVolume(volume float64) {
|
||||||
p.gain.Get("gain").Set("value", volume)
|
p.gain.Get("gain").Set("value", volume)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *readerDriverPlayerImpl) UnplayedBufferSize() int64 {
|
func (p *playerImpl) UnplayedBufferSize() int64 {
|
||||||
// This is not an accurate buffer size as part of the buffers might already be consumed.
|
// This is not an accurate buffer size as part of the buffers might already be consumed.
|
||||||
var sec float64
|
var sec float64
|
||||||
for _, n := range p.bufferSourceNodes {
|
for _, n := range p.bufferSourceNodes {
|
||||||
sec += n.Get("buffer").Get("duration").Float()
|
sec += n.Get("buffer").Get("duration").Float()
|
||||||
}
|
}
|
||||||
return int64(sec * float64(p.driver.context.sampleRate*channelNum*bitDepthInBytes))
|
return int64(sec * float64(p.context.sampleRate*p.context.channelNum*p.context.bitDepthInBytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *readerDriverPlayerImpl) Close() error {
|
func (p *playerImpl) Close() error {
|
||||||
runtime.SetFinalizer(p, nil)
|
runtime.SetFinalizer(p, nil)
|
||||||
p.Reset()
|
p.Reset()
|
||||||
p.state = readerPlayerClosed
|
p.state = readerPlayerClosed
|
||||||
p.appendBufferFunc.Release()
|
p.appendBufferFunc.Release()
|
||||||
return nil
|
return p.err
|
||||||
}
|
}
|
||||||
|
|
||||||
type go2cppDriverWrapper struct {
|
type go2cppDriverWrapper struct {
|
||||||
c *go2cpp.Context
|
c *go2cpp.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *go2cppDriverWrapper) NewPlayer(r io.Reader) readerDriverPlayer {
|
func (w *go2cppDriverWrapper) NewPlayer(r io.Reader) Player {
|
||||||
return w.c.NewPlayer(r)
|
return w.c.NewPlayer(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *go2cppDriverWrapper) MaxBufferSize() int {
|
||||||
|
return w.c.MaxBufferSize()
|
||||||
|
}
|
||||||
|
|
||||||
func (w *go2cppDriverWrapper) Close() error {
|
func (w *go2cppDriverWrapper) Close() error {
|
||||||
return w.c.Close()
|
return w.c.Close()
|
||||||
}
|
}
|
@ -20,52 +20,23 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/audio/internal/readerdriver"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: The term 'buffer' is confusing. Name each buffer with good terms.
|
|
||||||
|
|
||||||
// oneBufferSize returns the size of one buffer in the player implementation.
|
|
||||||
func oneBufferSize(sampleRate int) int {
|
|
||||||
return sampleRate * channelNum * bitDepthInBytes / 4
|
|
||||||
}
|
|
||||||
|
|
||||||
// maxBufferSize returns the maximum size of the buffer for the audio source.
|
|
||||||
// This buffer is used when unreading on pausing the player.
|
|
||||||
func maxBufferSize(sampleRate int) int {
|
|
||||||
// The number of underlying buffers should be 2.
|
|
||||||
return oneBufferSize(sampleRate) * 2
|
|
||||||
}
|
|
||||||
|
|
||||||
// readerDriver represents a driver using io.ReadClosers.
|
|
||||||
type readerDriver interface {
|
|
||||||
NewPlayer(io.Reader) readerDriverPlayer
|
|
||||||
io.Closer
|
|
||||||
}
|
|
||||||
|
|
||||||
type readerDriverPlayer interface {
|
|
||||||
Pause()
|
|
||||||
Play()
|
|
||||||
IsPlaying() bool
|
|
||||||
Reset()
|
|
||||||
Volume() float64
|
|
||||||
SetVolume(volume float64)
|
|
||||||
UnplayedBufferSize() int64
|
|
||||||
io.Closer
|
|
||||||
}
|
|
||||||
|
|
||||||
type readerPlayerFactory struct {
|
type readerPlayerFactory struct {
|
||||||
driver readerDriver
|
context readerdriver.Context
|
||||||
sampleRate int
|
sampleRate int
|
||||||
}
|
}
|
||||||
|
|
||||||
var readerDriverForTesting readerDriver
|
var readerDriverForTesting readerdriver.Context
|
||||||
|
|
||||||
func newReaderPlayerFactory(sampleRate int) *readerPlayerFactory {
|
func newReaderPlayerFactory(sampleRate int) *readerPlayerFactory {
|
||||||
f := &readerPlayerFactory{
|
f := &readerPlayerFactory{
|
||||||
sampleRate: sampleRate,
|
sampleRate: sampleRate,
|
||||||
}
|
}
|
||||||
if readerDriverForTesting != nil {
|
if readerDriverForTesting != nil {
|
||||||
f.driver = readerDriverForTesting
|
f.context = readerDriverForTesting
|
||||||
}
|
}
|
||||||
// TODO: Consider the hooks.
|
// TODO: Consider the hooks.
|
||||||
return f
|
return f
|
||||||
@ -73,22 +44,17 @@ func newReaderPlayerFactory(sampleRate int) *readerPlayerFactory {
|
|||||||
|
|
||||||
type readerPlayer struct {
|
type readerPlayer struct {
|
||||||
context *Context
|
context *Context
|
||||||
player readerDriverPlayer
|
player readerdriver.Player
|
||||||
|
src io.Reader
|
||||||
stream *timeStream
|
stream *timeStream
|
||||||
factory *readerPlayerFactory
|
factory *readerPlayerFactory
|
||||||
m sync.Mutex
|
m sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *readerPlayerFactory) newPlayerImpl(context *Context, src io.Reader) (playerImpl, error) {
|
func (f *readerPlayerFactory) newPlayerImpl(context *Context, src io.Reader) (playerImpl, error) {
|
||||||
sampleRate := context.SampleRate()
|
|
||||||
s, err := newTimeStream(src, sampleRate)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
p := &readerPlayer{
|
p := &readerPlayer{
|
||||||
|
src: src,
|
||||||
context: context,
|
context: context,
|
||||||
stream: s,
|
|
||||||
factory: f,
|
factory: f,
|
||||||
}
|
}
|
||||||
runtime.SetFinalizer(p, (*readerPlayer).Close)
|
runtime.SetFinalizer(p, (*readerPlayer).Close)
|
||||||
@ -101,15 +67,22 @@ func (p *readerPlayer) ensurePlayer() error {
|
|||||||
// but if Ebiten is used for a shared library, the timing when init functions are called
|
// but if Ebiten is used for a shared library, the timing when init functions are called
|
||||||
// is unexpectable.
|
// is unexpectable.
|
||||||
// e.g. a variable for JVM on Android might not be set.
|
// e.g. a variable for JVM on Android might not be set.
|
||||||
if p.factory.driver == nil {
|
if p.factory.context == nil {
|
||||||
d, err := newReaderDriverImpl(p.context)
|
c, err := readerdriver.NewContext(p.factory.sampleRate, channelNum, bitDepthInBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.factory.driver = d
|
p.factory.context = c
|
||||||
|
}
|
||||||
|
if p.stream == nil {
|
||||||
|
s, err := newTimeStream(p.src, p.factory.sampleRate, p.factory.context.MaxBufferSize())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
p.stream = s
|
||||||
}
|
}
|
||||||
if p.player == nil {
|
if p.player == nil {
|
||||||
p.player = p.factory.driver.NewPlayer(p.stream)
|
p.player = p.factory.context.NewPlayer(p.stream)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -213,19 +186,21 @@ func (p *readerPlayer) Seek(offset time.Duration) error {
|
|||||||
p.m.Lock()
|
p.m.Lock()
|
||||||
defer p.m.Unlock()
|
defer p.m.Unlock()
|
||||||
|
|
||||||
if p.player != nil {
|
if err := p.ensurePlayer(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if p.player.IsPlaying() {
|
if p.player.IsPlaying() {
|
||||||
defer func() {
|
defer func() {
|
||||||
p.player.Play()
|
p.player.Play()
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
p.player.Reset()
|
p.player.Reset()
|
||||||
}
|
|
||||||
return p.stream.Seek(offset)
|
return p.stream.Seek(offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *readerPlayer) source() io.Reader {
|
func (p *readerPlayer) source() io.Reader {
|
||||||
return p.stream.r
|
return p.src
|
||||||
}
|
}
|
||||||
|
|
||||||
type timeStream struct {
|
type timeStream struct {
|
||||||
@ -234,12 +209,14 @@ type timeStream struct {
|
|||||||
pos int64
|
pos int64
|
||||||
buf []byte
|
buf []byte
|
||||||
unread int
|
unread int
|
||||||
|
maxBufferSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTimeStream(r io.Reader, sampleRate int) (*timeStream, error) {
|
func newTimeStream(r io.Reader, sampleRate int, maxBufferSize int) (*timeStream, error) {
|
||||||
s := &timeStream{
|
s := &timeStream{
|
||||||
r: r,
|
r: r,
|
||||||
sampleRate: sampleRate,
|
sampleRate: sampleRate,
|
||||||
|
maxBufferSize: maxBufferSize,
|
||||||
}
|
}
|
||||||
if seeker, ok := s.r.(io.Seeker); ok {
|
if seeker, ok := s.r.(io.Seeker); ok {
|
||||||
// Get the current position of the source.
|
// Get the current position of the source.
|
||||||
@ -271,7 +248,7 @@ func (s *timeStream) Read(buf []byte) (int, error) {
|
|||||||
n, err := s.r.Read(buf)
|
n, err := s.r.Read(buf)
|
||||||
s.pos += int64(n)
|
s.pos += int64(n)
|
||||||
s.buf = append(s.buf, buf[:n]...)
|
s.buf = append(s.buf, buf[:n]...)
|
||||||
if m := maxBufferSize(s.sampleRate); len(s.buf) > m {
|
if m := s.maxBufferSize; len(s.buf) > m {
|
||||||
s.buf = s.buf[len(s.buf)-m:]
|
s.buf = s.buf[len(s.buf)-m:]
|
||||||
}
|
}
|
||||||
return n, err
|
return n, err
|
||||||
|
Loading…
Reference in New Issue
Block a user