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.
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// Package audio provides audio players. This can be used with or without ebiten package.
|
|
|
|
//
|
|
|
|
// The stream format must be 16-bit little endian and 2 channels.
|
|
|
|
//
|
|
|
|
// An audio context has a sample rate you can set and all streams you want to play must have the same
|
2017-01-15 10:02:21 +01:00
|
|
|
// sample rate. However, decoders like audio/vorbis and audio/wav adjust sample rate,
|
|
|
|
// and you don't have to care about it as long as you use those decoders.
|
2016-04-10 10:07:58 +02:00
|
|
|
//
|
|
|
|
// An audio context can generate 'players' (instances of audio.Player),
|
|
|
|
// 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
|
|
|
//
|
|
|
|
// Ebiten's game progress always synchronizes with audio progress.
|
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"
|
2016-06-26 19:02:01 +02:00
|
|
|
"errors"
|
2016-02-10 18:04:23 +01:00
|
|
|
"io"
|
2016-03-28 17:06:37 +02:00
|
|
|
"runtime"
|
2017-06-03 20:42:26 +02:00
|
|
|
"sync"
|
2016-03-06 10:55:20 +01:00
|
|
|
"time"
|
2016-03-12 20:48:13 +01:00
|
|
|
|
2017-05-04 14:09:02 +02:00
|
|
|
"github.com/hajimehoshi/oto"
|
2017-07-13 17:28:28 +02:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/clock"
|
2017-07-13 18:38:22 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/loop"
|
2015-01-10 17:23:43 +01:00
|
|
|
)
|
|
|
|
|
2016-04-15 17:52:07 +02:00
|
|
|
type players struct {
|
2017-06-04 09:46:02 +02:00
|
|
|
players map[*Player]struct{}
|
2016-04-04 19:24:54 +02:00
|
|
|
sync.RWMutex
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 19:33:02 +01:00
|
|
|
const (
|
|
|
|
channelNum = 2
|
|
|
|
bytesPerSample = 2
|
2016-03-19 17:40:10 +01:00
|
|
|
|
|
|
|
// TODO: This assumes that channelNum is a power of 2.
|
|
|
|
mask = ^(channelNum*bytesPerSample - 1)
|
2016-03-12 19:33:02 +01:00
|
|
|
)
|
|
|
|
|
2016-04-14 18:37:48 +02:00
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2017-06-04 09:05:22 +02:00
|
|
|
func (p *players) Read(b []uint8) (int, error) {
|
2016-04-15 17:52:07 +02:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
2016-03-03 03:57:25 +01:00
|
|
|
|
2017-06-03 21:05:30 +02:00
|
|
|
players := []*Player{}
|
|
|
|
for player := range p.players {
|
|
|
|
players = append(players, player)
|
|
|
|
}
|
|
|
|
if len(players) == 0 {
|
2016-04-14 18:37:48 +02:00
|
|
|
l := len(b)
|
2016-03-15 19:02:54 +01:00
|
|
|
l &= mask
|
2017-06-04 09:05:22 +02:00
|
|
|
copy(b, make([]uint8, l))
|
2016-03-10 16:01:00 +01:00
|
|
|
return l, nil
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
|
|
|
closed := []*Player{}
|
2016-03-15 18:57:54 +01:00
|
|
|
l := len(b)
|
2017-06-03 21:05:30 +02:00
|
|
|
for _, player := range players {
|
2017-06-04 09:46:02 +02:00
|
|
|
n, err := player.readToBuffer(l)
|
|
|
|
if err == io.EOF {
|
2016-11-28 19:36:16 +01:00
|
|
|
closed = append(closed, player)
|
2016-03-05 14:42:16 +01:00
|
|
|
} else if err != nil {
|
2016-03-05 08:49:35 +01:00
|
|
|
return 0, err
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
2017-06-04 09:46:02 +02:00
|
|
|
l = min(n, l)
|
2016-03-06 14:03:11 +01:00
|
|
|
}
|
2016-03-15 19:02:54 +01:00
|
|
|
l &= mask
|
2016-03-06 14:03:11 +01:00
|
|
|
b16s := [][]int16{}
|
2017-06-03 21:05:30 +02:00
|
|
|
for _, player := range players {
|
2016-11-28 19:36:16 +01:00
|
|
|
b16s = append(b16s, player.bufferToInt16(l))
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
2016-03-15 18:57:54 +01:00
|
|
|
for i := 0; i < l/2; i++ {
|
2016-03-03 04:22:10 +01:00
|
|
|
x := 0
|
2016-03-06 14:03:11 +01:00
|
|
|
for _, b16 := range b16s {
|
|
|
|
x += int(b16[i])
|
2016-03-03 04:22:10 +01:00
|
|
|
}
|
|
|
|
if x > (1<<15)-1 {
|
|
|
|
x = (1 << 15) - 1
|
|
|
|
}
|
|
|
|
if x < -(1 << 15) {
|
|
|
|
x = -(1 << 15)
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
|
|
|
b[2*i] = byte(x)
|
|
|
|
b[2*i+1] = byte(x >> 8)
|
|
|
|
}
|
2017-06-03 21:05:30 +02:00
|
|
|
for _, player := range players {
|
2016-11-28 19:36:16 +01:00
|
|
|
player.proceed(l)
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
2016-04-15 17:52:07 +02:00
|
|
|
for _, pl := range closed {
|
|
|
|
delete(p.players, pl)
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
2016-03-15 18:57:54 +01:00
|
|
|
return l, nil
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
|
|
|
|
2016-04-15 17:52:07 +02:00
|
|
|
func (p *players) addPlayer(player *Player) {
|
|
|
|
p.Lock()
|
|
|
|
p.players[player] = struct{}{}
|
2017-06-03 20:42:26 +02:00
|
|
|
p.Unlock()
|
2016-04-04 18:04:14 +02:00
|
|
|
}
|
|
|
|
|
2016-04-15 17:52:07 +02:00
|
|
|
func (p *players) removePlayer(player *Player) {
|
|
|
|
p.Lock()
|
|
|
|
delete(p.players, player)
|
2017-06-03 20:42:26 +02:00
|
|
|
p.Unlock()
|
2016-04-04 18:04:14 +02:00
|
|
|
}
|
|
|
|
|
2016-04-15 17:52:07 +02:00
|
|
|
func (p *players) hasPlayer(player *Player) bool {
|
|
|
|
p.RLock()
|
|
|
|
_, ok := p.players[player]
|
2017-06-03 20:42:26 +02:00
|
|
|
p.RUnlock()
|
2016-04-04 18:04:14 +02:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:02:01 +02:00
|
|
|
func (p *players) hasSource(src ReadSeekCloser) bool {
|
|
|
|
p.RLock()
|
|
|
|
defer p.RUnlock()
|
|
|
|
for player := range p.players {
|
|
|
|
if player.src == src {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// A Context is a current state of audio.
|
|
|
|
//
|
2016-09-26 16:08:34 +02:00
|
|
|
// There should be at most one Context object.
|
|
|
|
// This means only one constant sample rate is valid in your one application.
|
|
|
|
//
|
2016-04-10 10:07:58 +02:00
|
|
|
// The typical usage with ebiten package is:
|
|
|
|
//
|
|
|
|
// var audioContext *audio.Context
|
|
|
|
//
|
|
|
|
// func update(screen *ebiten.Image) error {
|
|
|
|
// // Update updates the audio stream by 1/60 [sec].
|
|
|
|
// if err := audioContext.Update(); err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// // ...
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func main() {
|
|
|
|
// audioContext, err = audio.NewContext(sampleRate)
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// ebiten.Run(run, update, 320, 240, 2, "Audio test")
|
|
|
|
// }
|
2016-03-02 16:48:59 +01:00
|
|
|
type Context struct {
|
2017-07-10 17:20:47 +02:00
|
|
|
players *players
|
|
|
|
errCh chan error
|
2017-07-11 19:24:54 +02:00
|
|
|
initCh chan struct{}
|
|
|
|
initedCh chan struct{}
|
2017-07-10 19:38:18 +02:00
|
|
|
pingCount int
|
2017-07-10 17:20:47 +02:00
|
|
|
sampleRate int
|
|
|
|
frames int64
|
|
|
|
framesReadOnly int64
|
|
|
|
writtenBytes int64
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// NewContext creates a new audio context with the given sample rate (e.g. 44100).
|
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
|
|
|
}
|
2016-04-15 17:52:07 +02:00
|
|
|
c := &Context{
|
2016-04-04 18:04:14 +02:00
|
|
|
sampleRate: sampleRate,
|
2017-07-10 17:20:47 +02:00
|
|
|
errCh: make(chan error, 1),
|
2017-07-11 19:24:54 +02:00
|
|
|
initCh: make(chan struct{}),
|
|
|
|
initedCh: make(chan struct{}),
|
2016-04-15 17:52:07 +02:00
|
|
|
}
|
2016-10-02 15:18:44 +02:00
|
|
|
theContext = c
|
2016-04-15 17:52:07 +02:00
|
|
|
c.players = &players{
|
2017-06-04 09:46:02 +02:00
|
|
|
players: map[*Player]struct{}{},
|
2016-03-10 16:01:00 +01:00
|
|
|
}
|
2017-07-10 17:20:47 +02:00
|
|
|
|
|
|
|
go c.loop()
|
|
|
|
|
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-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-07-13 18:38:22 +02:00
|
|
|
func (c *Context) ping() {
|
2017-07-11 19:24:54 +02:00
|
|
|
if c.initCh != nil {
|
|
|
|
close(c.initCh)
|
|
|
|
c.initCh = nil
|
|
|
|
<-c.initedCh
|
|
|
|
}
|
2017-07-10 19:38:18 +02:00
|
|
|
c.m.Lock()
|
|
|
|
c.pingCount = 5
|
|
|
|
c.m.Unlock()
|
2017-07-10 17:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) loop() {
|
2017-07-13 18:38:22 +02:00
|
|
|
loop.RegisterPing(c.ping)
|
|
|
|
|
2017-06-28 20:36:19 +02:00
|
|
|
// Initialize oto.Player lazily to enable calling NewContext in an 'init' function.
|
|
|
|
// Accessing oto.Player functions requires the environment to be already initialized,
|
2016-05-26 16:33:12 +02:00
|
|
|
// but if Ebiten is used for a shared library, the timing when init functions are called
|
|
|
|
// is unexpectable.
|
2016-05-24 20:47:59 +02:00
|
|
|
// e.g. a variable for JVM on Android might not be set.
|
2017-07-11 19:24:54 +02:00
|
|
|
<-c.initCh
|
2017-07-10 17:20:47 +02:00
|
|
|
|
2017-07-11 20:15:01 +02:00
|
|
|
p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, c.bufferSize())
|
2017-07-10 17:20:47 +02:00
|
|
|
if err != nil {
|
|
|
|
c.errCh <- err
|
2017-07-11 19:24:54 +02:00
|
|
|
return
|
2017-07-10 17:20:47 +02:00
|
|
|
}
|
|
|
|
defer p.Close()
|
|
|
|
|
2017-07-11 19:24:54 +02:00
|
|
|
close(c.initedCh)
|
|
|
|
c.initedCh = nil
|
|
|
|
|
2017-07-10 17:20:47 +02:00
|
|
|
for {
|
|
|
|
c.m.Lock()
|
2017-07-10 19:38:18 +02:00
|
|
|
if c.pingCount == 0 {
|
|
|
|
c.m.Unlock()
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
continue
|
2017-07-10 17:20:47 +02:00
|
|
|
}
|
2017-07-10 19:38:18 +02:00
|
|
|
c.pingCount--
|
|
|
|
c.m.Unlock()
|
2017-07-10 17:20:47 +02:00
|
|
|
c.frames++
|
2017-07-13 17:28:28 +02:00
|
|
|
clock.Inc()
|
2017-07-13 20:19:50 +02:00
|
|
|
bytesPerFrame := c.sampleRate * bytesPerSample * channelNum / clock.FPS
|
2017-07-10 17:20:47 +02:00
|
|
|
l := (c.frames * int64(bytesPerFrame)) - c.writtenBytes
|
|
|
|
l &= mask
|
|
|
|
c.writtenBytes += l
|
|
|
|
buf := make([]uint8, l)
|
|
|
|
if _, err := io.ReadFull(c.players, buf); err != nil {
|
|
|
|
c.errCh <- err
|
|
|
|
}
|
|
|
|
if _, err = p.Write(buf); err != nil {
|
|
|
|
c.errCh <- err
|
2016-05-24 20:47:59 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-10 17:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update returns an error if some errors happen.
|
2017-07-13 18:45:36 +02:00
|
|
|
//
|
|
|
|
// As of 1.6.0-alpha, this just returns the error if an error happens internally,
|
|
|
|
// and do nothing related to updating the state.
|
|
|
|
// Then, the audio is available without Update,
|
|
|
|
// but it is recommended to call Update every frame.
|
2017-07-10 17:20:47 +02:00
|
|
|
func (c *Context) Update() error {
|
2017-06-28 20:36:19 +02:00
|
|
|
select {
|
2017-07-10 17:20:47 +02:00
|
|
|
case err := <-c.errCh:
|
2017-06-28 20:36:19 +02:00
|
|
|
return err
|
|
|
|
default:
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bytesReadSeekCloser) Read(buf []uint8) (int, error) {
|
|
|
|
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-06-03 18:03:01 +02:00
|
|
|
//
|
|
|
|
// A returned stream is concurrent safe.
|
2017-01-15 18:36:18 +01:00
|
|
|
func BytesReadSeekCloser(b []uint8) ReadSeekCloser {
|
2017-06-03 18:03:01 +02:00
|
|
|
return &bytesReadSeekCloser{reader: bytes.NewReader(b)}
|
2017-01-15 18:21:21 +01:00
|
|
|
}
|
|
|
|
|
2017-06-11 12:13:45 +02:00
|
|
|
type readingResult struct {
|
|
|
|
data []uint8
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2016-04-04 19:24:54 +02:00
|
|
|
// Player is an audio player which has one stream.
|
2016-02-10 18:18:39 +01:00
|
|
|
type Player struct {
|
2016-04-15 17:52:07 +02:00
|
|
|
players *players
|
|
|
|
src ReadSeekCloser
|
|
|
|
sampleRate int
|
2017-06-03 19:45:38 +02:00
|
|
|
|
2017-06-04 09:05:22 +02:00
|
|
|
buf []uint8
|
2017-06-03 19:45:38 +02:00
|
|
|
pos int64
|
|
|
|
volume float64
|
|
|
|
|
2017-06-03 20:14:48 +02:00
|
|
|
srcM sync.Mutex
|
|
|
|
m sync.RWMutex
|
2016-02-10 18:18:39 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
//
|
2016-06-26 19:02:01 +02:00
|
|
|
// Note that the given src can't be shared with other Players.
|
|
|
|
//
|
2017-04-17 17:59:10 +02:00
|
|
|
// NewPlayer tries to rewind src by calling Seek to get the current position.
|
|
|
|
// NewPlayer returns error when the Seek returns error.
|
2016-04-19 16:43:54 +02:00
|
|
|
func NewPlayer(context *Context, src ReadSeekCloser) (*Player, error) {
|
2016-06-26 19:02:01 +02:00
|
|
|
if context.players.hasSource(src) {
|
|
|
|
return nil, errors.New("audio: src cannot be shared with another Player")
|
|
|
|
}
|
2016-04-15 17:52:07 +02:00
|
|
|
p := &Player{
|
2016-04-19 16:43:54 +02:00
|
|
|
players: context.players,
|
2016-04-15 17:52:07 +02:00
|
|
|
src: src,
|
2016-04-19 16:43:54 +02:00
|
|
|
sampleRate: context.sampleRate,
|
2017-06-04 09:05:22 +02:00
|
|
|
buf: []uint8{},
|
2016-04-15 17:52:07 +02:00
|
|
|
volume: 1,
|
|
|
|
}
|
|
|
|
// Get the current position of the source.
|
2016-11-28 19:36:16 +01:00
|
|
|
pos, err := p.src.Seek(0, io.SeekCurrent)
|
2016-04-15 17:52:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.pos = pos
|
|
|
|
runtime.SetFinalizer(p, (*Player).Close)
|
|
|
|
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-06-04 09:05:22 +02:00
|
|
|
func NewPlayerFromBytes(context *Context, src []uint8) (*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.
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return p, nil
|
2016-06-26 19:22:44 +02:00
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// Close closes the stream. Ths source stream passed by NewPlayer will also be closed.
|
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.
|
2016-06-26 19:02:01 +02:00
|
|
|
//
|
2017-06-03 19:52:11 +02:00
|
|
|
// Close is concurrent safe.
|
|
|
|
//
|
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
|
|
|
p.players.removePlayer(p)
|
|
|
|
runtime.SetFinalizer(p, nil)
|
2017-06-03 20:14:48 +02:00
|
|
|
p.srcM.Lock()
|
2017-06-03 19:52:11 +02:00
|
|
|
err := p.src.Close()
|
2017-06-03 20:14:48 +02:00
|
|
|
p.srcM.Unlock()
|
2017-06-03 19:52:11 +02:00
|
|
|
return err
|
2016-03-28 17:06:37 +02:00
|
|
|
}
|
|
|
|
|
2017-06-04 09:46:02 +02:00
|
|
|
func (p *Player) readToBuffer(length int) (int, error) {
|
2017-07-14 20:03:40 +02:00
|
|
|
b := make([]uint8, length)
|
|
|
|
p.srcM.Lock()
|
|
|
|
n, err := p.src.Read(b)
|
|
|
|
p.srcM.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2016-03-06 14:03:11 +01:00
|
|
|
}
|
2017-07-14 20:03:40 +02:00
|
|
|
p.buf = append(p.buf, b[:n]...)
|
2017-07-02 09:28:02 +02:00
|
|
|
return len(p.buf), nil
|
2016-03-06 14:03:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) bufferToInt16(lengthInBytes int) []int16 {
|
|
|
|
r := make([]int16, lengthInBytes/2)
|
2017-06-04 09:46:02 +02:00
|
|
|
// This function must be called on the same goruotine of readToBuffer.
|
2017-06-03 21:37:59 +02:00
|
|
|
p.m.RLock()
|
2016-03-06 14:03:11 +01:00
|
|
|
for i := 0; i < lengthInBytes/2; i++ {
|
|
|
|
r[i] = int16(p.buf[2*i]) | (int16(p.buf[2*i+1]) << 8)
|
2016-03-28 04:06:17 +02:00
|
|
|
r[i] = int16(float64(r[i]) * p.volume)
|
2016-03-06 14:03:11 +01:00
|
|
|
}
|
2017-06-03 20:14:48 +02:00
|
|
|
p.m.RUnlock()
|
2016-03-06 14:03:11 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) proceed(length int) {
|
2017-06-04 09:46:02 +02:00
|
|
|
// This function must be called on the same goruotine of readToBuffer.
|
2016-03-06 14:03:11 +01:00
|
|
|
p.buf = p.buf[length:]
|
|
|
|
p.pos += int64(length)
|
|
|
|
}
|
|
|
|
|
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.
|
2017-06-03 19:52:11 +02:00
|
|
|
//
|
|
|
|
// Play is concurrent safe.
|
2016-02-10 18:18:39 +01:00
|
|
|
func (p *Player) Play() error {
|
2016-04-15 17:52:07 +02:00
|
|
|
p.players.addPlayer(p)
|
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
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// IsPlaying returns boolean indicating whether the player is playing.
|
2017-06-03 19:52:11 +02:00
|
|
|
//
|
|
|
|
// IsPlaying is concurrent safe.
|
2016-03-06 10:55:20 +01:00
|
|
|
func (p *Player) IsPlaying() bool {
|
2016-04-15 17:52:07 +02:00
|
|
|
return p.players.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
|
|
|
//
|
2017-06-03 19:52:11 +02:00
|
|
|
// Rewind is concurrent safe.
|
|
|
|
//
|
2017-04-04 18:12:02 +02:00
|
|
|
// Rewind returns error when seeking the source returns error.
|
2016-03-06 10:55:20 +01:00
|
|
|
func (p *Player) Rewind() error {
|
|
|
|
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
|
|
|
//
|
2017-06-03 19:52:11 +02:00
|
|
|
// Seek is concurrent safe.
|
|
|
|
//
|
2017-04-04 18:12:02 +02:00
|
|
|
// Seek returns error when seeking the source returns error.
|
2016-03-06 10:55:20 +01:00
|
|
|
func (p *Player) Seek(offset time.Duration) error {
|
2016-04-15 17:52:07 +02:00
|
|
|
o := int64(offset) * bytesPerSample * channelNum * int64(p.sampleRate) / int64(time.Second)
|
|
|
|
o &= mask
|
2017-06-03 20:14:48 +02:00
|
|
|
p.srcM.Lock()
|
2016-11-28 19:01:52 +01:00
|
|
|
pos, err := p.src.Seek(o, io.SeekStart)
|
2017-06-03 20:14:48 +02:00
|
|
|
p.srcM.Unlock()
|
2016-03-06 10:55:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-14 20:03:40 +02:00
|
|
|
p.buf = []uint8{}
|
|
|
|
p.pos = pos
|
2016-03-06 10:55:20 +01:00
|
|
|
return nil
|
|
|
|
}
|
2016-03-03 03:57:25 +01:00
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// Pause pauses the playing.
|
2016-04-15 17:52:07 +02:00
|
|
|
//
|
2017-06-03 19:52:11 +02:00
|
|
|
// Pause is concurrent safe.
|
|
|
|
//
|
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 {
|
2016-04-15 17:52:07 +02:00
|
|
|
p.players.removePlayer(p)
|
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
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// Current returns the current position.
|
2017-06-03 19:52:11 +02:00
|
|
|
//
|
|
|
|
// Current is concurrent safe.
|
2016-03-06 10:55:20 +01:00
|
|
|
func (p *Player) Current() time.Duration {
|
2017-06-03 20:14:48 +02:00
|
|
|
p.m.RLock()
|
2016-11-28 19:01:52 +01:00
|
|
|
sample := p.pos / bytesPerSample / channelNum
|
2017-06-03 20:14:48 +02:00
|
|
|
t := time.Duration(sample) * time.Second / time.Duration(p.sampleRate)
|
|
|
|
p.m.RUnlock()
|
|
|
|
return t
|
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].
|
2017-06-03 19:52:11 +02:00
|
|
|
//
|
|
|
|
// Volume is concurrent safe.
|
2016-03-28 04:06:17 +02:00
|
|
|
func (p *Player) Volume() float64 {
|
2017-06-03 20:14:48 +02:00
|
|
|
p.m.RLock()
|
|
|
|
v := p.volume
|
|
|
|
p.m.RUnlock()
|
|
|
|
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.
|
2016-04-03 19:16:26 +02:00
|
|
|
// volume must be in between 0 and 1. This function panics otherwise.
|
2017-06-03 19:52:11 +02:00
|
|
|
//
|
|
|
|
// SetVolume is concurrent safe.
|
2016-03-28 04:06:17 +02:00
|
|
|
func (p *Player) SetVolume(volume float64) {
|
2017-06-03 20:14:48 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
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
|
|
|
}
|
|
|
|
p.volume = volume
|
|
|
|
}
|