2015-01-10 17:23:43 +01:00
|
|
|
// Copyright 2015 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-10-01 10:24:30 +02:00
|
|
|
// Package audio provides audio players.
|
2016-04-10 10:07:58 +02:00
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// The stream format must be 16-bit little endian and 2 channels. The format is as follows:
|
|
|
|
// [data] = [sample 1] [sample 2] [sample 3] ...
|
|
|
|
// [sample *] = [channel 1] ...
|
|
|
|
// [channel *] = [byte 1] [byte 2] ...
|
2016-04-10 10:07:58 +02:00
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// An audio context (audio.Context object) has a sample rate you can specify and all streams you want to play must have the same
|
|
|
|
// sample rate. However, decoders in e.g. audio/mp3 package adjust sample rate automatically,
|
2017-01-15 10:02:21 +01:00
|
|
|
// and you don't have to care about it as long as you use those decoders.
|
2016-04-10 10:07:58 +02:00
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// An audio context can generate 'players' (audio.Player objects),
|
2016-04-10 10:07:58 +02:00
|
|
|
// and you can play sound by calling Play function of players.
|
|
|
|
// When multiple players play, mixing is automatically done.
|
|
|
|
// Note that too many players may cause distortion.
|
2017-07-13 19:26:41 +02:00
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// For the simplest example to play sound, see wav package in the examples.
|
2015-01-23 15:04:56 +01:00
|
|
|
package audio
|
2015-01-10 17:23:43 +01:00
|
|
|
|
|
|
|
import (
|
2016-06-26 19:22:44 +02:00
|
|
|
"bytes"
|
2017-12-23 10:29:43 +01:00
|
|
|
"fmt"
|
2016-02-10 18:04:23 +01:00
|
|
|
"io"
|
2016-03-28 17:06:37 +02:00
|
|
|
"runtime"
|
2018-05-09 05:14:17 +02:00
|
|
|
"sync"
|
2016-03-06 10:55:20 +01:00
|
|
|
"time"
|
2016-03-12 20:48:13 +01:00
|
|
|
|
2017-12-16 17:20:10 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/web"
|
2015-01-10 17:23:43 +01:00
|
|
|
)
|
|
|
|
|
2017-10-01 10:24:30 +02:00
|
|
|
// A Context represents a current state of audio.
|
2016-04-10 10:07:58 +02:00
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// At most one Context object can exist in one process.
|
2016-09-26 16:08:34 +02:00
|
|
|
// This means only one constant sample rate is valid in your one application.
|
|
|
|
//
|
2017-11-30 17:38:47 +01:00
|
|
|
// For a typical usage example, see examples/wav/main.go.
|
2016-03-02 16:48:59 +01:00
|
|
|
type Context struct {
|
2019-01-26 19:29:41 +01:00
|
|
|
c context
|
|
|
|
initCh chan struct{}
|
|
|
|
|
2019-01-09 13:55:42 +01:00
|
|
|
mux *mux
|
2017-12-23 16:23:57 +01:00
|
|
|
sampleRate int
|
2018-02-04 09:33:17 +01:00
|
|
|
err error
|
2018-10-13 15:41:37 +02:00
|
|
|
ready bool
|
2018-02-04 09:33:17 +01:00
|
|
|
|
|
|
|
m sync.Mutex
|
2016-03-02 16:48:59 +01:00
|
|
|
}
|
|
|
|
|
2016-10-02 15:18:44 +02:00
|
|
|
var (
|
|
|
|
theContext *Context
|
|
|
|
theContextLock sync.Mutex
|
|
|
|
)
|
|
|
|
|
2017-09-25 17:38:50 +02:00
|
|
|
// NewContext creates a new audio context with the given sample rate.
|
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// The sample rate is also used for decoding MP3 with audio/mp3 package
|
|
|
|
// or other formats as the target sample rate.
|
2017-09-25 17:38:50 +02:00
|
|
|
//
|
|
|
|
// sampleRate should be 44100 or 48000.
|
|
|
|
// Other values might not work.
|
|
|
|
// For example, 22050 causes error on Safari when decoding MP3.
|
2017-04-04 18:12:02 +02:00
|
|
|
//
|
|
|
|
// Error returned by NewContext is always nil as of 1.5.0-alpha.
|
|
|
|
//
|
|
|
|
// NewContext panics when an audio context is already created.
|
2016-04-02 19:59:44 +02:00
|
|
|
func NewContext(sampleRate int) (*Context, error) {
|
2016-10-02 15:18:44 +02:00
|
|
|
theContextLock.Lock()
|
|
|
|
defer theContextLock.Unlock()
|
|
|
|
if theContext != nil {
|
2017-04-04 18:12:02 +02:00
|
|
|
panic("audio: context is already created")
|
2016-10-02 15:18:44 +02:00
|
|
|
}
|
2019-01-26 19:29:41 +01:00
|
|
|
|
|
|
|
ch := make(chan struct{})
|
|
|
|
|
2016-04-15 17:52:07 +02:00
|
|
|
c := &Context{
|
2016-04-04 18:04:14 +02:00
|
|
|
sampleRate: sampleRate,
|
2019-03-01 20:42:19 +01:00
|
|
|
c: newContext(sampleRate, ch),
|
2019-01-26 19:29:41 +01:00
|
|
|
initCh: ch,
|
2016-04-15 17:52:07 +02:00
|
|
|
}
|
2016-10-02 15:18:44 +02:00
|
|
|
theContext = c
|
2019-01-09 13:55:42 +01:00
|
|
|
c.mux = newMux()
|
2017-07-10 17:20:47 +02:00
|
|
|
|
2017-08-17 04:15:04 +02:00
|
|
|
go c.loop()
|
2017-07-10 17:20:47 +02:00
|
|
|
|
2016-04-02 19:59:44 +02:00
|
|
|
return c, nil
|
2017-07-10 17:20:47 +02:00
|
|
|
}
|
2016-05-24 20:47:59 +02:00
|
|
|
|
2017-10-01 10:24:30 +02:00
|
|
|
// CurrentContext returns the current context or nil if there is no context.
|
2017-07-10 17:20:47 +02:00
|
|
|
func CurrentContext() *Context {
|
|
|
|
theContextLock.Lock()
|
|
|
|
c := theContext
|
|
|
|
theContextLock.Unlock()
|
|
|
|
return c
|
2016-03-02 16:48:59 +01:00
|
|
|
}
|
|
|
|
|
2017-08-17 04:15:04 +02:00
|
|
|
func (c *Context) loop() {
|
2018-05-26 11:04:20 +02:00
|
|
|
suspendCh := make(chan struct{}, 1)
|
|
|
|
resumeCh := make(chan struct{}, 1)
|
2019-04-28 12:35:59 +02:00
|
|
|
h := getHook()
|
|
|
|
h.OnSuspendAudio(func() {
|
2018-05-26 11:04:20 +02:00
|
|
|
suspendCh <- struct{}{}
|
|
|
|
})
|
2019-04-28 12:35:59 +02:00
|
|
|
h.OnResumeAudio(func() {
|
2018-05-26 11:04:20 +02:00
|
|
|
resumeCh <- struct{}{}
|
|
|
|
})
|
2019-01-09 16:44:53 +01:00
|
|
|
|
|
|
|
var once sync.Once
|
2019-04-28 12:35:59 +02:00
|
|
|
h.AppendHookOnBeforeUpdate(func() error {
|
2019-01-09 16:44:53 +01:00
|
|
|
once.Do(func() {
|
2019-01-26 19:29:41 +01:00
|
|
|
close(c.initCh)
|
2019-01-09 16:44:53 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
var err error
|
|
|
|
theContextLock.Lock()
|
|
|
|
if theContext != nil {
|
|
|
|
theContext.m.Lock()
|
|
|
|
err = theContext.err
|
|
|
|
theContext.m.Unlock()
|
|
|
|
}
|
|
|
|
theContextLock.Unlock()
|
|
|
|
return err
|
2018-05-26 11:04:20 +02:00
|
|
|
})
|
2017-07-13 18:38:22 +02:00
|
|
|
|
2019-01-26 19:29:41 +01:00
|
|
|
<-c.initCh
|
2017-07-10 17:20:47 +02:00
|
|
|
|
2019-01-26 19:29:41 +01:00
|
|
|
defer c.c.Close()
|
2019-01-26 16:31:55 +01:00
|
|
|
|
2019-01-26 19:29:41 +01:00
|
|
|
p := c.c.NewPlayer()
|
2017-07-10 17:20:47 +02:00
|
|
|
defer p.Close()
|
|
|
|
|
|
|
|
for {
|
2018-05-26 11:04:20 +02:00
|
|
|
select {
|
|
|
|
case <-suspendCh:
|
|
|
|
<-resumeCh
|
|
|
|
default:
|
2019-01-09 13:55:42 +01:00
|
|
|
if _, err := io.CopyN(p, c.mux, 2048); err != nil {
|
2019-04-28 12:39:18 +02:00
|
|
|
c.m.Lock()
|
2018-05-26 11:04:20 +02:00
|
|
|
c.err = err
|
2019-04-28 12:39:18 +02:00
|
|
|
c.m.Unlock()
|
2018-05-26 11:04:20 +02:00
|
|
|
return
|
|
|
|
}
|
2018-10-13 15:41:37 +02:00
|
|
|
c.m.Lock()
|
|
|
|
c.ready = true
|
|
|
|
c.m.Unlock()
|
2017-07-10 17:20:47 +02:00
|
|
|
}
|
2016-05-24 20:47:59 +02:00
|
|
|
}
|
2017-07-10 17:20:47 +02:00
|
|
|
}
|
|
|
|
|
2018-10-13 15:41:37 +02:00
|
|
|
// IsReady returns a boolean value indicating whether the audio is ready or not.
|
|
|
|
//
|
|
|
|
// On some browsers, user interaction like click or pressing keys is required to start audio.
|
|
|
|
func (c *Context) IsReady() bool {
|
|
|
|
c.m.Lock()
|
|
|
|
r := c.ready
|
|
|
|
c.m.Unlock()
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-11-30 16:26:28 +01:00
|
|
|
// Update is deprecated as of 1.6.0-alpha.
|
2017-07-13 18:45:36 +02:00
|
|
|
//
|
2017-11-30 16:26:28 +01:00
|
|
|
// As of 1.6.0-alpha, Update always returns nil and does nothing related to updating the state.
|
2018-07-14 18:04:46 +02:00
|
|
|
// You don't have to call Update any longer.
|
2017-11-30 16:26:28 +01:00
|
|
|
// The internal audio error is returned at ebiten.Run instead.
|
2017-07-10 17:20:47 +02:00
|
|
|
func (c *Context) Update() error {
|
2016-04-14 18:37:48 +02:00
|
|
|
return nil
|
2016-03-10 16:01:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 19:00:05 +01:00
|
|
|
// SampleRate returns the sample rate.
|
|
|
|
func (c *Context) SampleRate() int {
|
2016-04-15 17:52:07 +02:00
|
|
|
return c.sampleRate
|
2016-03-12 19:00:05 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 19:24:54 +02:00
|
|
|
// ReadSeekCloser is an io.ReadSeeker and io.Closer.
|
2016-03-28 17:06:37 +02:00
|
|
|
type ReadSeekCloser interface {
|
|
|
|
io.ReadSeeker
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
2017-01-15 18:36:18 +01:00
|
|
|
type bytesReadSeekCloser struct {
|
|
|
|
reader *bytes.Reader
|
|
|
|
}
|
|
|
|
|
2017-12-22 18:05:49 +01:00
|
|
|
func (b *bytesReadSeekCloser) Read(buf []byte) (int, error) {
|
2017-01-15 18:36:18 +01:00
|
|
|
return b.reader.Read(buf)
|
2017-01-15 18:21:21 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 18:36:18 +01:00
|
|
|
func (b *bytesReadSeekCloser) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
return b.reader.Seek(offset, whence)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bytesReadSeekCloser) Close() error {
|
|
|
|
b.reader = nil
|
2017-01-15 18:21:21 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-15 18:36:18 +01:00
|
|
|
// BytesReadSeekCloser creates ReadSeekCloser from bytes.
|
2017-12-22 18:05:49 +01:00
|
|
|
func BytesReadSeekCloser(b []byte) ReadSeekCloser {
|
2017-06-03 18:03:01 +02:00
|
|
|
return &bytesReadSeekCloser{reader: bytes.NewReader(b)}
|
2017-01-15 18:21:21 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 19:24:54 +02:00
|
|
|
// Player is an audio player which has one stream.
|
2018-12-18 15:07:19 +01:00
|
|
|
//
|
|
|
|
// Even when all references to a Player object is gone,
|
|
|
|
// the object is not GCed until the player finishes playing.
|
|
|
|
// This means that if a Player plays an infinite stream,
|
|
|
|
// the object is never GCed unless Close is called.
|
2016-02-10 18:18:39 +01:00
|
|
|
type Player struct {
|
2018-12-15 14:14:42 +01:00
|
|
|
p *playerImpl
|
|
|
|
}
|
|
|
|
|
|
|
|
type playerImpl struct {
|
2019-04-27 17:13:51 +02:00
|
|
|
mux *mux
|
|
|
|
src io.ReadCloser
|
|
|
|
srcEOF bool
|
|
|
|
sampleRate int
|
|
|
|
closedExplicitly bool
|
|
|
|
runningReadLoop bool
|
2017-06-03 19:45:38 +02:00
|
|
|
|
2017-12-22 18:05:49 +01:00
|
|
|
buf []byte
|
2017-06-03 19:45:38 +02:00
|
|
|
pos int64
|
|
|
|
volume float64
|
|
|
|
|
2019-04-27 17:13:51 +02:00
|
|
|
closeCh chan struct{}
|
|
|
|
closedCh chan struct{}
|
|
|
|
seekCh chan seekArgs
|
|
|
|
seekedCh chan error
|
|
|
|
proceedCh chan []int16
|
|
|
|
proceededCh chan proceededValues
|
|
|
|
syncCh chan func()
|
2019-04-27 17:53:46 +02:00
|
|
|
|
|
|
|
m sync.Mutex
|
2016-02-10 18:18:39 +01:00
|
|
|
}
|
|
|
|
|
2017-12-23 09:24:51 +01:00
|
|
|
type seekArgs struct {
|
|
|
|
offset int64
|
|
|
|
whence int
|
|
|
|
}
|
|
|
|
|
|
|
|
type proceededValues struct {
|
|
|
|
buf []int16
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// NewPlayer creates a new player with the given stream.
|
2015-01-24 07:48:48 +01:00
|
|
|
//
|
2016-04-10 10:07:58 +02:00
|
|
|
// src's format must be linear PCM (16bits little endian, 2 channel stereo)
|
2016-02-07 16:51:25 +01:00
|
|
|
// without a header (e.g. RIFF header).
|
2016-04-10 10:07:58 +02:00
|
|
|
// The sample rate must be same as that of the audio context.
|
2016-04-15 17:52:07 +02:00
|
|
|
//
|
2018-03-21 16:33:17 +01:00
|
|
|
// The player is seekable when src is io.Seeker.
|
|
|
|
// Attempt to seek the player that is not io.Seeker causes panic.
|
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// Note that the given src can't be shared with other Player objects.
|
2016-06-26 19:02:01 +02:00
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// NewPlayer tries to call Seek of src to get the current position.
|
2017-04-17 17:59:10 +02:00
|
|
|
// NewPlayer returns error when the Seek returns error.
|
2019-03-31 18:59:10 +02:00
|
|
|
//
|
|
|
|
// NewPlayer takes the ownership of src. Player's Close calls src's Close.
|
2018-03-21 16:33:17 +01:00
|
|
|
func NewPlayer(context *Context, src io.ReadCloser) (*Player, error) {
|
2016-04-15 17:52:07 +02:00
|
|
|
p := &Player{
|
2018-12-15 14:14:42 +01:00
|
|
|
&playerImpl{
|
2019-04-27 17:13:51 +02:00
|
|
|
mux: context.mux,
|
|
|
|
src: src,
|
|
|
|
sampleRate: context.sampleRate,
|
|
|
|
buf: nil,
|
|
|
|
volume: 1,
|
|
|
|
closeCh: make(chan struct{}),
|
|
|
|
closedCh: make(chan struct{}),
|
|
|
|
seekCh: make(chan seekArgs),
|
|
|
|
seekedCh: make(chan error),
|
|
|
|
proceedCh: make(chan []int16),
|
|
|
|
proceededCh: make(chan proceededValues),
|
2018-12-15 14:14:42 +01:00
|
|
|
},
|
2016-04-15 17:52:07 +02:00
|
|
|
}
|
2018-12-15 14:14:42 +01:00
|
|
|
if seeker, ok := p.p.src.(io.Seeker); ok {
|
2018-03-21 16:33:17 +01:00
|
|
|
// Get the current position of the source.
|
|
|
|
pos, err := seeker.Seek(0, io.SeekCurrent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-15 14:14:42 +01:00
|
|
|
p.p.pos = pos
|
2016-04-15 17:52:07 +02:00
|
|
|
}
|
2018-12-16 19:37:00 +01:00
|
|
|
runtime.SetFinalizer(p, (*Player).finalize)
|
2017-12-23 09:24:51 +01:00
|
|
|
|
2016-04-15 17:52:07 +02:00
|
|
|
return p, nil
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
2016-02-12 13:39:48 +01:00
|
|
|
|
2016-06-26 19:22:44 +02:00
|
|
|
// NewPlayerFromBytes creates a new player with the given bytes.
|
|
|
|
//
|
|
|
|
// As opposed to NewPlayer, you don't have to care if src is already used by another player or not.
|
|
|
|
// src can be shared by multiple players.
|
|
|
|
//
|
|
|
|
// The format of src should be same as noted at NewPlayer.
|
2017-04-17 17:59:10 +02:00
|
|
|
//
|
2017-06-02 18:12:58 +02:00
|
|
|
// NewPlayerFromBytes's error is always nil as of 1.5.0-alpha.
|
2017-12-22 18:05:49 +01:00
|
|
|
func NewPlayerFromBytes(context *Context, src []byte) (*Player, error) {
|
2017-01-15 18:36:18 +01:00
|
|
|
b := BytesReadSeekCloser(src)
|
2017-06-02 18:12:58 +02:00
|
|
|
p, err := NewPlayer(context, b)
|
|
|
|
if err != nil {
|
|
|
|
// Errors should never happen.
|
2019-02-07 09:19:24 +01:00
|
|
|
panic(fmt.Sprintf("audio: %v at NewPlayerFromBytes", err))
|
2017-06-02 18:12:58 +02:00
|
|
|
}
|
|
|
|
return p, nil
|
2016-06-26 19:22:44 +02:00
|
|
|
}
|
|
|
|
|
2018-12-16 19:37:00 +01:00
|
|
|
func (p *Player) finalize() {
|
|
|
|
runtime.SetFinalizer(p, nil)
|
|
|
|
// TODO: It is really hard to say concurrent safety.
|
|
|
|
// Refactor this package to reduce goroutines.
|
|
|
|
if !p.IsPlaying() {
|
|
|
|
p.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-01 10:24:30 +02:00
|
|
|
// Close closes the stream.
|
2016-04-15 17:52:07 +02:00
|
|
|
//
|
2016-06-27 19:50:13 +02:00
|
|
|
// When closing, the stream owned by the player will also be closed by calling its Close.
|
2017-10-01 10:24:30 +02:00
|
|
|
// This means that the source stream passed via NewPlayer will also be closed.
|
2017-06-03 19:52:11 +02:00
|
|
|
//
|
2017-04-04 18:12:02 +02:00
|
|
|
// Close returns error when closing the source returns error.
|
2016-03-28 17:06:37 +02:00
|
|
|
func (p *Player) Close() error {
|
2016-04-15 17:52:07 +02:00
|
|
|
runtime.SetFinalizer(p, nil)
|
2018-12-15 14:14:42 +01:00
|
|
|
return p.p.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) Close() error {
|
2019-01-09 13:55:42 +01:00
|
|
|
p.mux.removePlayer(p)
|
2019-04-27 19:21:56 +02:00
|
|
|
|
|
|
|
p.m.Lock()
|
|
|
|
c := p.closedExplicitly
|
|
|
|
p.m.Unlock()
|
|
|
|
if c {
|
|
|
|
return fmt.Errorf("audio: the player is already closed")
|
|
|
|
}
|
|
|
|
|
2019-04-27 17:13:51 +02:00
|
|
|
p.m.Lock()
|
|
|
|
p.closedExplicitly = true
|
|
|
|
p.m.Unlock()
|
|
|
|
// src.Close is called only when Player's Close is called.
|
|
|
|
if err := p.src.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-16 19:37:00 +01:00
|
|
|
return p.closeImpl()
|
|
|
|
}
|
2017-12-22 18:05:49 +01:00
|
|
|
|
2019-04-27 17:13:51 +02:00
|
|
|
func (p *playerImpl) ensureReadLoop() error {
|
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
if p.closedExplicitly {
|
2017-12-23 10:29:43 +01:00
|
|
|
return fmt.Errorf("audio: the player is already closed")
|
|
|
|
}
|
2019-04-27 17:13:51 +02:00
|
|
|
if p.runningReadLoop {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
go p.readLoop()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) closeImpl() error {
|
2019-04-27 19:21:56 +02:00
|
|
|
p.m.Lock()
|
|
|
|
r := p.runningReadLoop
|
|
|
|
p.m.Unlock()
|
|
|
|
if !r {
|
|
|
|
return nil
|
2019-04-27 17:13:51 +02:00
|
|
|
}
|
2019-04-27 19:21:56 +02:00
|
|
|
|
2019-04-27 17:13:51 +02:00
|
|
|
p.closeCh <- struct{}{}
|
|
|
|
<-p.closedCh
|
|
|
|
return nil
|
2016-03-06 14:03:11 +01:00
|
|
|
}
|
|
|
|
|
2018-12-15 14:14:42 +01:00
|
|
|
func (p *playerImpl) bufferToInt16(lengthInBytes int) ([]int16, error) {
|
2019-04-27 17:13:51 +02:00
|
|
|
if err := p.ensureReadLoop(); err != nil {
|
|
|
|
return nil, err
|
2017-12-23 10:29:43 +01:00
|
|
|
}
|
2019-04-27 17:13:51 +02:00
|
|
|
p.proceedCh <- make([]int16, lengthInBytes/2)
|
|
|
|
r := <-p.proceededCh
|
|
|
|
return r.buf, r.err
|
2016-03-06 14:03:11 +01:00
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// Play plays the stream.
|
2016-04-15 17:52:07 +02:00
|
|
|
//
|
2017-04-04 18:12:02 +02:00
|
|
|
// Play always returns nil.
|
2016-02-10 18:18:39 +01:00
|
|
|
func (p *Player) Play() error {
|
2018-12-15 14:14:42 +01:00
|
|
|
p.p.Play()
|
2016-03-03 03:57:25 +01:00
|
|
|
return nil
|
2015-01-22 19:02:23 +01:00
|
|
|
}
|
2016-02-11 11:55:59 +01:00
|
|
|
|
2018-12-15 14:14:42 +01:00
|
|
|
func (p *playerImpl) Play() {
|
2019-01-09 13:55:42 +01:00
|
|
|
p.mux.addPlayer(p)
|
2018-12-15 14:14:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) readLoop() {
|
2019-04-27 17:13:51 +02:00
|
|
|
p.m.Lock()
|
|
|
|
p.runningReadLoop = true
|
|
|
|
p.m.Unlock()
|
2017-12-23 10:29:43 +01:00
|
|
|
defer func() {
|
2019-04-27 17:13:51 +02:00
|
|
|
p.m.Lock()
|
|
|
|
p.runningReadLoop = false
|
|
|
|
p.m.Unlock()
|
2017-12-23 10:29:43 +01:00
|
|
|
}()
|
|
|
|
|
2018-03-04 11:23:38 +01:00
|
|
|
timer := time.NewTimer(0)
|
|
|
|
timerCh := timer.C
|
2017-12-23 09:24:51 +01:00
|
|
|
var readErr error
|
2017-12-22 18:05:49 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-p.closeCh:
|
2017-12-23 10:29:43 +01:00
|
|
|
p.closedCh <- struct{}{}
|
2017-12-22 20:12:12 +01:00
|
|
|
return
|
2017-12-23 09:24:51 +01:00
|
|
|
|
|
|
|
case s := <-p.seekCh:
|
2018-03-21 16:33:17 +01:00
|
|
|
seeker, ok := p.src.(io.Seeker)
|
|
|
|
if !ok {
|
2019-02-07 09:19:24 +01:00
|
|
|
panic("audio: the source must be io.Seeker when seeking")
|
2018-03-21 16:33:17 +01:00
|
|
|
}
|
|
|
|
pos, err := seeker.Seek(s.offset, s.whence)
|
2019-04-27 17:53:46 +02:00
|
|
|
|
|
|
|
p.m.Lock()
|
2017-12-23 09:24:51 +01:00
|
|
|
p.buf = nil
|
|
|
|
p.pos = pos
|
|
|
|
p.srcEOF = false
|
2019-04-27 17:53:46 +02:00
|
|
|
p.m.Unlock()
|
|
|
|
|
2017-12-23 09:24:51 +01:00
|
|
|
p.seekedCh <- err
|
2018-03-04 11:23:38 +01:00
|
|
|
if timer != nil {
|
|
|
|
timer.Stop()
|
|
|
|
}
|
|
|
|
timer = time.NewTimer(time.Millisecond)
|
|
|
|
timerCh = timer.C
|
2017-12-23 09:24:51 +01:00
|
|
|
|
2018-03-04 11:23:38 +01:00
|
|
|
case <-timerCh:
|
2017-12-23 14:52:01 +01:00
|
|
|
// If the buffer has 1 second, that's enough.
|
2019-04-27 17:53:46 +02:00
|
|
|
p.m.Lock()
|
2018-07-23 18:54:01 +02:00
|
|
|
if len(p.buf) >= p.sampleRate*bytesPerSample {
|
2018-03-04 11:23:38 +01:00
|
|
|
if timer != nil {
|
|
|
|
timer.Stop()
|
|
|
|
}
|
|
|
|
timer = time.NewTimer(100 * time.Millisecond)
|
|
|
|
timerCh = timer.C
|
2019-04-27 17:53:46 +02:00
|
|
|
p.m.Unlock()
|
2017-12-23 09:05:14 +01:00
|
|
|
break
|
|
|
|
}
|
2017-12-23 09:24:51 +01:00
|
|
|
|
2017-12-23 16:58:14 +01:00
|
|
|
// Try to read the buffer for 1/60[s].
|
2017-12-23 19:41:36 +01:00
|
|
|
s := 60
|
|
|
|
if web.IsAndroidChrome() {
|
|
|
|
s = 10
|
|
|
|
} else if web.IsBrowser() {
|
|
|
|
s = 20
|
|
|
|
}
|
2018-07-23 18:54:01 +02:00
|
|
|
l := p.sampleRate * bytesPerSample / s
|
2017-12-23 11:59:52 +01:00
|
|
|
l &= mask
|
|
|
|
buf := make([]byte, l)
|
2017-12-23 09:05:14 +01:00
|
|
|
n, err := p.src.Read(buf)
|
2017-12-23 09:24:51 +01:00
|
|
|
|
2017-12-23 09:05:14 +01:00
|
|
|
p.buf = append(p.buf, buf[:n]...)
|
|
|
|
if err == io.EOF {
|
|
|
|
p.srcEOF = true
|
|
|
|
}
|
|
|
|
if p.srcEOF && len(p.buf) == 0 {
|
2018-03-04 11:23:38 +01:00
|
|
|
if timer != nil {
|
|
|
|
timer.Stop()
|
|
|
|
}
|
|
|
|
timer = nil
|
|
|
|
timerCh = nil
|
2019-04-27 17:53:46 +02:00
|
|
|
p.m.Unlock()
|
2017-12-23 09:24:51 +01:00
|
|
|
break
|
2017-12-23 09:05:14 +01:00
|
|
|
}
|
2019-04-27 17:53:46 +02:00
|
|
|
p.m.Unlock()
|
|
|
|
|
2017-12-23 09:05:14 +01:00
|
|
|
if err != nil && err != io.EOF {
|
2017-12-23 09:24:51 +01:00
|
|
|
readErr = err
|
2018-03-04 11:23:38 +01:00
|
|
|
if timer != nil {
|
|
|
|
timer.Stop()
|
|
|
|
}
|
|
|
|
timer = nil
|
|
|
|
timerCh = nil
|
2017-12-23 09:24:51 +01:00
|
|
|
break
|
2017-12-22 18:05:49 +01:00
|
|
|
}
|
2018-03-04 11:23:38 +01:00
|
|
|
if timer != nil {
|
|
|
|
timer.Stop()
|
|
|
|
}
|
2017-12-23 19:41:36 +01:00
|
|
|
if web.IsBrowser() {
|
2018-03-04 11:23:38 +01:00
|
|
|
timer = time.NewTimer(10 * time.Millisecond)
|
2017-12-23 19:41:36 +01:00
|
|
|
} else {
|
2018-03-04 11:23:38 +01:00
|
|
|
timer = time.NewTimer(time.Millisecond)
|
2017-12-23 19:41:36 +01:00
|
|
|
}
|
2018-03-04 11:23:38 +01:00
|
|
|
timerCh = timer.C
|
2017-12-23 09:24:51 +01:00
|
|
|
|
|
|
|
case buf := <-p.proceedCh:
|
|
|
|
if readErr != nil {
|
|
|
|
p.proceededCh <- proceededValues{buf, readErr}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-27 17:53:46 +02:00
|
|
|
p.m.Lock()
|
2018-06-09 12:00:47 +02:00
|
|
|
if p.shouldSkipImpl() {
|
|
|
|
// Return zero values.
|
2018-06-09 12:28:20 +02:00
|
|
|
p.proceededCh <- proceededValues{buf, nil}
|
2019-04-27 17:53:46 +02:00
|
|
|
p.m.Unlock()
|
2018-06-09 12:28:20 +02:00
|
|
|
break
|
|
|
|
}
|
2018-06-09 12:00:47 +02:00
|
|
|
|
|
|
|
lengthInBytes := len(buf) * 2
|
|
|
|
l := lengthInBytes
|
|
|
|
|
2017-12-23 09:24:51 +01:00
|
|
|
if l > len(p.buf) {
|
|
|
|
l = len(p.buf)
|
|
|
|
}
|
|
|
|
for i := 0; i < l/2; i++ {
|
|
|
|
buf[i] = int16(p.buf[2*i]) | (int16(p.buf[2*i+1]) << 8)
|
|
|
|
buf[i] = int16(float64(buf[i]) * p.volume)
|
|
|
|
}
|
|
|
|
p.pos += int64(l)
|
|
|
|
p.buf = p.buf[l:]
|
2019-04-27 17:53:46 +02:00
|
|
|
p.m.Unlock()
|
2017-12-23 09:24:51 +01:00
|
|
|
|
2018-06-09 12:00:47 +02:00
|
|
|
p.proceededCh <- proceededValues{buf[:l/2], nil}
|
2017-12-22 18:05:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-15 14:14:42 +01:00
|
|
|
func (p *playerImpl) shouldSkip() bool {
|
2019-04-27 17:53:46 +02:00
|
|
|
p.m.Lock()
|
|
|
|
r := p.shouldSkipImpl()
|
|
|
|
p.m.Unlock()
|
2018-06-09 12:00:47 +02:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2018-12-15 14:14:42 +01:00
|
|
|
func (p *playerImpl) shouldSkipImpl() bool {
|
2018-06-09 12:00:47 +02:00
|
|
|
// When p.buf is nil, the player just starts playing or seeking.
|
|
|
|
// Note that this is different from len(p.buf) == 0 && p.buf != nil.
|
|
|
|
if p.buf == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if p.eofImpl() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-12-15 14:14:42 +01:00
|
|
|
func (p *playerImpl) bufferSizeInBytes() int {
|
2019-04-27 17:53:46 +02:00
|
|
|
p.m.Lock()
|
|
|
|
s := len(p.buf)
|
|
|
|
p.m.Unlock()
|
2018-06-09 12:00:47 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2018-12-15 14:14:42 +01:00
|
|
|
func (p *playerImpl) eof() bool {
|
2019-04-27 17:53:46 +02:00
|
|
|
p.m.Lock()
|
|
|
|
r := p.eofImpl()
|
|
|
|
p.m.Unlock()
|
2017-12-22 18:05:49 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2018-12-15 14:14:42 +01:00
|
|
|
func (p *playerImpl) eofImpl() bool {
|
2018-06-09 12:00:47 +02:00
|
|
|
return p.srcEOF && len(p.buf) == 0
|
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// IsPlaying returns boolean indicating whether the player is playing.
|
2016-03-06 10:55:20 +01:00
|
|
|
func (p *Player) IsPlaying() bool {
|
2018-12-15 14:14:42 +01:00
|
|
|
return p.p.IsPlaying()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) IsPlaying() bool {
|
2019-01-09 13:55:42 +01:00
|
|
|
return p.mux.hasPlayer(p)
|
2016-03-06 10:55:20 +01:00
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// Rewind rewinds the current position to the start.
|
2016-04-15 17:52:07 +02:00
|
|
|
//
|
2018-03-21 16:33:17 +01:00
|
|
|
// The passed source to NewPlayer must be io.Seeker, or Rewind panics.
|
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// Rewind returns error when seeking the source stream returns error.
|
2016-03-06 10:55:20 +01:00
|
|
|
func (p *Player) Rewind() error {
|
2018-12-15 14:14:42 +01:00
|
|
|
return p.p.Rewind()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) Rewind() error {
|
2018-03-21 16:33:17 +01:00
|
|
|
if _, ok := p.src.(io.Seeker); !ok {
|
2018-03-21 16:36:38 +01:00
|
|
|
panic("audio: player to be rewound must be io.Seeker")
|
2018-03-21 16:33:17 +01:00
|
|
|
}
|
2016-03-06 10:55:20 +01:00
|
|
|
return p.Seek(0)
|
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// Seek seeks the position with the given offset.
|
2016-04-15 17:52:07 +02:00
|
|
|
//
|
2018-03-21 16:33:17 +01:00
|
|
|
// The passed source to NewPlayer must be io.Seeker, or Seek panics.
|
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// Seek returns error when seeking the source stream returns error.
|
2016-03-06 10:55:20 +01:00
|
|
|
func (p *Player) Seek(offset time.Duration) error {
|
2018-12-15 14:14:42 +01:00
|
|
|
return p.p.Seek(offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) Seek(offset time.Duration) error {
|
2018-03-21 16:33:17 +01:00
|
|
|
if _, ok := p.src.(io.Seeker); !ok {
|
|
|
|
panic("audio: player to be sought must be io.Seeker")
|
|
|
|
}
|
2019-04-27 17:13:51 +02:00
|
|
|
if err := p.ensureReadLoop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-23 18:54:01 +02:00
|
|
|
o := int64(offset) * bytesPerSample * int64(p.sampleRate) / int64(time.Second)
|
2016-04-15 17:52:07 +02:00
|
|
|
o &= mask
|
2019-04-27 17:13:51 +02:00
|
|
|
p.seekCh <- seekArgs{o, io.SeekStart}
|
|
|
|
return <-p.seekedCh
|
2017-08-17 04:44:30 +02:00
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// Pause pauses the playing.
|
2016-04-15 17:52:07 +02:00
|
|
|
//
|
2017-04-04 18:12:02 +02:00
|
|
|
// Pause always returns nil.
|
2016-03-04 17:01:57 +01:00
|
|
|
func (p *Player) Pause() error {
|
2018-12-15 14:14:42 +01:00
|
|
|
p.p.Pause()
|
2016-03-03 03:57:25 +01:00
|
|
|
return nil
|
2016-02-11 11:55:59 +01:00
|
|
|
}
|
2016-03-06 10:55:20 +01:00
|
|
|
|
2018-12-15 14:14:42 +01:00
|
|
|
func (p *playerImpl) Pause() {
|
2019-01-09 13:55:42 +01:00
|
|
|
p.mux.removePlayer(p)
|
2018-12-15 14:14:42 +01:00
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// Current returns the current position.
|
2016-03-06 10:55:20 +01:00
|
|
|
func (p *Player) Current() time.Duration {
|
2018-12-15 14:14:42 +01:00
|
|
|
return p.p.Current()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) Current() time.Duration {
|
2019-04-27 17:53:46 +02:00
|
|
|
p.m.Lock()
|
|
|
|
sample := p.pos / bytesPerSample
|
|
|
|
p.m.Unlock()
|
2017-12-23 10:39:14 +01:00
|
|
|
return time.Duration(sample) * time.Second / time.Duration(p.sampleRate)
|
2016-03-06 10:55:20 +01:00
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// Volume returns the current volume of this player [0-1].
|
2016-03-28 04:06:17 +02:00
|
|
|
func (p *Player) Volume() float64 {
|
2018-12-15 14:14:42 +01:00
|
|
|
return p.p.Volume()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) Volume() float64 {
|
2019-04-27 17:53:46 +02:00
|
|
|
p.m.Lock()
|
|
|
|
v := p.volume
|
|
|
|
p.m.Unlock()
|
2017-06-03 20:14:48 +02:00
|
|
|
return v
|
2016-03-28 04:06:17 +02:00
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// SetVolume sets the volume of this player.
|
2018-07-14 18:04:46 +02:00
|
|
|
// volume must be in between 0 and 1. SetVolume panics otherwise.
|
2016-03-28 04:06:17 +02:00
|
|
|
func (p *Player) SetVolume(volume float64) {
|
2018-12-15 14:14:42 +01:00
|
|
|
p.p.SetVolume(volume)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) SetVolume(volume float64) {
|
2016-04-02 19:46:18 +02: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")
|
2016-03-28 04:06:17 +02:00
|
|
|
}
|
2017-12-23 10:39:14 +01:00
|
|
|
|
2019-04-27 17:53:46 +02:00
|
|
|
p.m.Lock()
|
|
|
|
p.volume = volume
|
|
|
|
p.m.Unlock()
|
2016-03-28 04:06:17 +02:00
|
|
|
}
|