2021-05-01 09:15:36 +02: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 readerdriver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
2021-05-05 09:49:02 +02:00
|
|
|
"time"
|
2021-05-01 09:15:36 +02:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio/internal/oboe"
|
|
|
|
)
|
|
|
|
|
|
|
|
func IsAvailable() bool {
|
2021-05-29 18:44:49 +02:00
|
|
|
// TODO: Enable this after #1656 is fixed
|
|
|
|
return false
|
2021-05-01 09:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type context struct {
|
|
|
|
sampleRate int
|
|
|
|
channelNum int
|
|
|
|
bitDepthInBytes int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewContext(sampleRate int, channelNum int, bitDepthInBytes int) (Context, chan struct{}, error) {
|
|
|
|
ready := make(chan struct{})
|
|
|
|
close(ready)
|
|
|
|
|
|
|
|
c := &context{
|
|
|
|
sampleRate: sampleRate,
|
|
|
|
channelNum: channelNum,
|
|
|
|
bitDepthInBytes: bitDepthInBytes,
|
|
|
|
}
|
|
|
|
return c, ready, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) NewPlayer(src io.Reader) Player {
|
|
|
|
p := &player{
|
|
|
|
context: c,
|
|
|
|
src: src,
|
|
|
|
cond: sync.NewCond(&sync.Mutex{}),
|
|
|
|
volume: 1,
|
|
|
|
}
|
|
|
|
runtime.SetFinalizer(p, (*player).Close)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) Suspend() error {
|
|
|
|
return oboe.Suspend()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) Resume() error {
|
|
|
|
return oboe.Resume()
|
|
|
|
}
|
|
|
|
|
|
|
|
type player struct {
|
|
|
|
context *context
|
|
|
|
p *oboe.Player
|
|
|
|
src io.Reader
|
|
|
|
err error
|
|
|
|
cond *sync.Cond
|
2021-05-05 08:32:50 +02:00
|
|
|
closed bool
|
2021-05-01 09:15:36 +02:00
|
|
|
volume float64
|
2021-05-26 05:46:37 +02:00
|
|
|
eof bool
|
2021-05-01 09:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Pause() {
|
2021-05-05 14:46:19 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
|
|
|
if p.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.closed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := p.p.Pause(); err != nil {
|
|
|
|
p.setErrorImpl(err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-05 19:36:44 +02:00
|
|
|
p.cond.Signal()
|
2021-05-01 09:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Play() {
|
2021-05-16 13:10:29 +02:00
|
|
|
// Call Play asynchronously since Oboe's Play might take long.
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
close(ch)
|
|
|
|
p.playImpl()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait until the mutex is locked in the above goroutine.
|
|
|
|
<-ch
|
|
|
|
}
|
2021-05-01 09:15:36 +02:00
|
|
|
|
2021-05-16 13:10:29 +02:00
|
|
|
func (p *player) playImpl() {
|
2021-05-01 09:15:36 +02:00
|
|
|
if p.err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-05-05 08:32:50 +02:00
|
|
|
if p.p != nil && p.p.IsPlaying() {
|
2021-05-01 09:15:36 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer p.cond.Signal()
|
2021-05-04 19:14:56 +02:00
|
|
|
var runLoop bool
|
2021-05-01 09:15:36 +02:00
|
|
|
if p.p == nil {
|
|
|
|
p.p = oboe.NewPlayer(p.context.sampleRate, p.context.channelNum, p.context.bitDepthInBytes, p.volume, func() {
|
|
|
|
p.cond.Signal()
|
|
|
|
})
|
2021-05-04 19:14:56 +02:00
|
|
|
runLoop = true
|
2021-05-08 20:51:45 +02:00
|
|
|
}
|
2021-05-04 19:14:56 +02:00
|
|
|
|
2021-05-08 20:51:45 +02:00
|
|
|
buf := make([]byte, p.context.maxBufferSize())
|
2021-05-09 20:22:19 +02:00
|
|
|
for p.p.UnplayedBufferSize() < p.context.maxBufferSize() {
|
2021-05-04 19:14:56 +02:00
|
|
|
n, err := p.src.Read(buf)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
p.setErrorImpl(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.p.AppendBuffer(buf[:n])
|
2021-05-08 20:51:45 +02:00
|
|
|
if err == io.EOF {
|
2021-05-26 06:03:42 +02:00
|
|
|
p.eof = true
|
2021-05-08 20:51:45 +02:00
|
|
|
break
|
|
|
|
}
|
2021-05-01 09:15:36 +02:00
|
|
|
}
|
2021-05-08 20:51:45 +02:00
|
|
|
|
2021-05-01 09:15:36 +02:00
|
|
|
if err := p.p.Play(); err != nil {
|
|
|
|
p.setErrorImpl(err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-04 19:14:56 +02:00
|
|
|
if runLoop {
|
|
|
|
go p.loop()
|
|
|
|
}
|
2021-05-01 09:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) IsPlaying() bool {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
2021-05-05 08:32:50 +02:00
|
|
|
if p.p == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return p.p.IsPlaying()
|
2021-05-01 09:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Reset() {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
|
|
|
if p.err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-05-05 08:32:50 +02:00
|
|
|
if p.closed {
|
2021-05-01 09:15:36 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := p.p.Close(); err != nil {
|
|
|
|
p.setErrorImpl(err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-15 18:53:44 +02:00
|
|
|
p.p = nil
|
2021-05-26 05:46:37 +02:00
|
|
|
p.eof = false
|
2021-05-15 18:53:44 +02:00
|
|
|
p.cond.Signal()
|
2021-05-01 09:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Volume() float64 {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
return p.volume
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) SetVolume(volume float64) {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
p.volume = volume
|
|
|
|
if p.p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.p.SetVolume(volume)
|
|
|
|
}
|
|
|
|
|
2021-05-09 20:22:19 +02:00
|
|
|
func (p *player) UnplayedBufferSize() int {
|
2021-05-01 09:15:36 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
2021-05-26 05:46:37 +02:00
|
|
|
|
2021-05-01 09:15:36 +02:00
|
|
|
if p.p == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return p.p.UnplayedBufferSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Err() error {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Close() error {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
return p.closeImpl()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) closeImpl() error {
|
|
|
|
defer p.cond.Signal()
|
|
|
|
|
|
|
|
runtime.SetFinalizer(p, nil)
|
2021-05-05 08:32:50 +02:00
|
|
|
p.closed = true
|
2021-05-01 09:15:36 +02:00
|
|
|
if p.p == nil {
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
if err := p.p.Close(); err != nil && p.err == nil {
|
2021-05-04 20:58:47 +02:00
|
|
|
// Do not call setErrorImpl, or this can cause infinite recursive.
|
|
|
|
p.err = err
|
2021-05-01 09:15:36 +02:00
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
p.p = nil
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) setError(err error) {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
p.setErrorImpl(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) setErrorImpl(err error) {
|
|
|
|
p.err = err
|
|
|
|
p.closeImpl()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) shouldWait() bool {
|
2021-05-05 08:32:50 +02:00
|
|
|
if p.closed {
|
|
|
|
return false
|
|
|
|
}
|
2021-05-01 09:15:36 +02:00
|
|
|
if p.p == nil {
|
|
|
|
return false
|
|
|
|
}
|
2021-05-26 05:46:37 +02:00
|
|
|
|
|
|
|
// Wait when the player is paused.
|
|
|
|
if !p.p.IsPlaying() {
|
|
|
|
return true
|
2021-05-01 09:15:36 +02:00
|
|
|
}
|
2021-05-26 05:46:37 +02:00
|
|
|
|
|
|
|
// When the source reaches EOF, wait until all the data is consumed.
|
|
|
|
if p.eof {
|
|
|
|
return p.p.UnplayedBufferSize() > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.p.UnplayedBufferSize() >= p.context.maxBufferSize()
|
2021-05-01 09:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) wait() bool {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
|
|
|
for p.shouldWait() {
|
|
|
|
p.cond.Wait()
|
|
|
|
}
|
2021-05-05 08:32:50 +02:00
|
|
|
return p.p != nil && p.p.IsPlaying()
|
2021-05-01 09:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) loop() {
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
for {
|
|
|
|
if !p.wait() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := p.src.Read(buf)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
p.setError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-26 05:46:37 +02:00
|
|
|
p.cond.L.Lock()
|
2021-05-26 17:56:08 +02:00
|
|
|
p.p.AppendBuffer(buf[:n])
|
2021-05-26 05:46:37 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
p.eof = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now p.resetImpl() doesn't close the stream gracefully. Then buffer size check is necessary here.
|
2021-05-26 17:56:08 +02:00
|
|
|
if p.eof && p.p.UnplayedBufferSize() == 0 {
|
2021-05-26 05:46:37 +02:00
|
|
|
// Even when the unplayed buffer size is 0,
|
|
|
|
// the audio data in the hardware might not be played yet (#1632).
|
2021-05-05 09:49:02 +02:00
|
|
|
// Just wait for a while.
|
2021-05-26 05:46:37 +02:00
|
|
|
p.cond.L.Unlock()
|
2021-05-26 17:52:40 +02:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
p.Reset()
|
2021-05-01 09:15:36 +02:00
|
|
|
return
|
|
|
|
}
|
2021-05-26 05:46:37 +02:00
|
|
|
p.cond.L.Unlock()
|
2021-05-01 09:15:36 +02:00
|
|
|
}
|
|
|
|
}
|