2019-01-26 19:29:41 +01:00
|
|
|
// Copyright 2019 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 audio
|
|
|
|
|
|
|
|
import (
|
2021-01-06 16:42:56 +01:00
|
|
|
"fmt"
|
2019-01-26 19:29:41 +01:00
|
|
|
"io"
|
|
|
|
"sync"
|
2021-01-06 16:42:56 +01:00
|
|
|
"time"
|
2019-01-26 19:29:41 +01:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
|
2019-01-26 19:29:41 +01:00
|
|
|
)
|
|
|
|
|
2021-01-06 18:06:09 +01:00
|
|
|
// writerDriver represents a driver using io.WriteClosers.
|
|
|
|
// The actual implementation is otoDriver.
|
|
|
|
type writerDriver interface {
|
2019-01-26 19:29:41 +01:00
|
|
|
NewPlayer() io.WriteCloser
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:06:09 +01:00
|
|
|
var writerDriverForTesting writerDriver
|
2019-01-26 19:29:41 +01:00
|
|
|
|
2021-01-06 18:06:09 +01:00
|
|
|
func newWriterDriver(sampleRate int) writerDriver {
|
|
|
|
if writerDriverForTesting != nil {
|
|
|
|
return writerDriverForTesting
|
2019-01-26 19:29:41 +01:00
|
|
|
}
|
2019-04-27 15:30:09 +02:00
|
|
|
|
|
|
|
ch := make(chan struct{})
|
|
|
|
var once sync.Once
|
|
|
|
hooks.AppendHookOnBeforeUpdate(func() error {
|
|
|
|
once.Do(func() {
|
|
|
|
close(ch)
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
2021-01-06 18:06:09 +01:00
|
|
|
return newOtoDriver(sampleRate, ch)
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
type writerPlayerFactory struct {
|
2021-01-06 18:06:09 +01:00
|
|
|
driver writerDriver
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
func newWriterPlayerFactory(sampleRate int) *writerPlayerFactory {
|
|
|
|
return &writerPlayerFactory{
|
2021-01-06 18:06:09 +01:00
|
|
|
driver: newWriterDriver(sampleRate),
|
|
|
|
}
|
2019-01-26 19:29:41 +01:00
|
|
|
}
|
2021-01-06 16:42:56 +01:00
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
type writerPlayer struct {
|
2021-01-06 16:42:56 +01:00
|
|
|
context *Context
|
2021-01-06 18:06:09 +01:00
|
|
|
driver writerDriver
|
2021-01-06 16:42:56 +01:00
|
|
|
src io.Reader
|
|
|
|
playing bool
|
|
|
|
closedExplicitly bool
|
|
|
|
isLoopActive bool
|
|
|
|
|
|
|
|
buf []byte
|
|
|
|
readbuf []byte
|
|
|
|
pos int64
|
|
|
|
volume float64
|
|
|
|
|
|
|
|
m sync.Mutex
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
func (c *writerPlayerFactory) newPlayerImpl(context *Context, src io.Reader) (playerImpl, error) {
|
|
|
|
p := &writerPlayer{
|
2021-01-06 18:06:09 +01:00
|
|
|
context: context,
|
|
|
|
driver: c.driver,
|
|
|
|
src: src,
|
|
|
|
volume: 1,
|
2021-01-06 16:59:13 +01:00
|
|
|
}
|
|
|
|
if seeker, ok := p.src.(io.Seeker); ok {
|
|
|
|
// Get the current position of the source.
|
|
|
|
pos, err := seeker.Seek(0, io.SeekCurrent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.pos = pos
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
func (p *writerPlayer) Close() error {
|
2021-01-06 16:42:56 +01:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
|
|
|
p.playing = false
|
|
|
|
if p.closedExplicitly {
|
|
|
|
return fmt.Errorf("audio: the player is already closed")
|
|
|
|
}
|
|
|
|
p.closedExplicitly = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
func (p *writerPlayer) Play() {
|
2021-01-06 16:42:56 +01:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
|
|
|
if p.closedExplicitly {
|
|
|
|
p.context.setError(fmt.Errorf("audio: the player is already closed"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.playing = true
|
|
|
|
if p.isLoopActive {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set p.isLoopActive to true here, not in the loop. This prevents duplicated active loops.
|
|
|
|
p.isLoopActive = true
|
|
|
|
p.context.addPlayer(p)
|
|
|
|
|
|
|
|
go p.loop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
func (p *writerPlayer) loop() {
|
2021-01-06 17:30:03 +01:00
|
|
|
p.context.waitUntilInited()
|
2021-01-06 16:42:56 +01:00
|
|
|
|
2021-01-06 18:06:09 +01:00
|
|
|
w := p.driver.NewPlayer()
|
2021-01-06 16:42:56 +01:00
|
|
|
wclosed := make(chan struct{})
|
|
|
|
defer func() {
|
|
|
|
<-wclosed
|
|
|
|
w.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
p.m.Lock()
|
|
|
|
p.playing = false
|
|
|
|
p.context.removePlayer(p)
|
|
|
|
p.isLoopActive = false
|
|
|
|
p.m.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
ch := make(chan []byte)
|
|
|
|
defer close(ch)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for buf := range ch {
|
|
|
|
if _, err := w.Write(buf); err != nil {
|
|
|
|
p.context.setError(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p.context.setReady()
|
|
|
|
}
|
|
|
|
close(wclosed)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
buf, ok := p.read()
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ch <- buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
func (p *writerPlayer) read() ([]byte, bool) {
|
2021-01-06 16:42:56 +01:00
|
|
|
if p.context.hasError() {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.closedExplicitly {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2021-01-23 15:07:16 +01:00
|
|
|
p.context.acquireSemaphore()
|
|
|
|
defer func() {
|
|
|
|
p.context.releaseSemaphore()
|
|
|
|
}()
|
|
|
|
|
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
2021-01-06 16:42:56 +01:00
|
|
|
// playing can be false when pausing.
|
|
|
|
if !p.playing {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
const bufSize = 2048
|
|
|
|
if p.readbuf == nil {
|
|
|
|
p.readbuf = make([]byte, bufSize)
|
|
|
|
}
|
|
|
|
n, err := p.src.Read(p.readbuf[:bufSize-len(p.buf)])
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
p.context.setError(err)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if n == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf := append(p.buf, p.readbuf[:n]...)
|
|
|
|
|
|
|
|
n2 := len(buf) - len(buf)%bytesPerSample
|
|
|
|
buf, p.buf = buf[:n2], buf[n2:]
|
|
|
|
|
|
|
|
for i := 0; i < len(buf)/2; i++ {
|
|
|
|
v16 := int16(buf[2*i]) | (int16(buf[2*i+1]) << 8)
|
|
|
|
v16 = int16(float64(v16) * p.volume)
|
|
|
|
buf[2*i] = byte(v16)
|
|
|
|
buf[2*i+1] = byte(v16 >> 8)
|
|
|
|
}
|
|
|
|
p.pos += int64(len(buf))
|
|
|
|
|
|
|
|
return buf, true
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
func (p *writerPlayer) IsPlaying() bool {
|
2021-01-06 16:42:56 +01:00
|
|
|
p.m.Lock()
|
|
|
|
r := p.playing
|
|
|
|
p.m.Unlock()
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
func (p *writerPlayer) Rewind() error {
|
2021-01-06 16:42:56 +01:00
|
|
|
if _, ok := p.src.(io.Seeker); !ok {
|
|
|
|
panic("audio: player to be rewound must be io.Seeker")
|
|
|
|
}
|
|
|
|
return p.Seek(0)
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
func (p *writerPlayer) Seek(offset time.Duration) error {
|
2021-01-06 16:42:56 +01:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
2021-01-06 17:30:03 +01:00
|
|
|
o := int64(offset) * bytesPerSample * int64(p.context.SampleRate()) / int64(time.Second)
|
2021-01-06 16:42:56 +01:00
|
|
|
o = o - (o % bytesPerSample)
|
|
|
|
|
|
|
|
seeker, ok := p.src.(io.Seeker)
|
|
|
|
if !ok {
|
|
|
|
panic("audio: the source must be io.Seeker when seeking")
|
|
|
|
}
|
|
|
|
pos, err := seeker.Seek(o, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.buf = nil
|
|
|
|
p.pos = pos
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
func (p *writerPlayer) Pause() {
|
2021-01-06 16:42:56 +01:00
|
|
|
p.m.Lock()
|
|
|
|
p.playing = false
|
|
|
|
p.m.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
func (p *writerPlayer) Current() time.Duration {
|
2021-01-06 16:42:56 +01:00
|
|
|
p.m.Lock()
|
|
|
|
sample := p.pos / bytesPerSample
|
|
|
|
p.m.Unlock()
|
2021-01-06 17:30:03 +01:00
|
|
|
return time.Duration(sample) * time.Second / time.Duration(p.context.SampleRate())
|
2021-01-06 16:42:56 +01:00
|
|
|
}
|
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
func (p *writerPlayer) Volume() float64 {
|
2021-01-06 16:42:56 +01:00
|
|
|
p.m.Lock()
|
|
|
|
v := p.volume
|
|
|
|
p.m.Unlock()
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
func (p *writerPlayer) SetVolume(volume float64) {
|
2021-01-06 16:42:56 +01:00
|
|
|
// The condition must be true when volume is NaN.
|
|
|
|
if !(0 <= volume && volume <= 1) {
|
|
|
|
panic("audio: volume must be in between 0 and 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
p.m.Lock()
|
|
|
|
p.volume = volume
|
|
|
|
p.m.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:24:03 +01:00
|
|
|
func (p *writerPlayer) source() io.Reader {
|
2021-01-06 16:42:56 +01:00
|
|
|
return p.src
|
|
|
|
}
|