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.
|
|
|
|
|
2015-01-23 15:04:56 +01:00
|
|
|
package audio
|
2015-01-10 17:23:43 +01:00
|
|
|
|
|
|
|
import (
|
2016-02-10 18:04:23 +01:00
|
|
|
"io"
|
2016-03-28 17:06:37 +02:00
|
|
|
"runtime"
|
2016-03-03 03:57:25 +01:00
|
|
|
"sync"
|
2016-03-06 10:55:20 +01:00
|
|
|
"time"
|
2016-03-12 20:48:13 +01:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
2015-01-10 17:23:43 +01:00
|
|
|
)
|
|
|
|
|
2016-04-04 17:40:16 +02:00
|
|
|
type mixingStream struct {
|
2016-04-04 18:04:14 +02:00
|
|
|
sampleRate int
|
2016-03-10 16:01:00 +01:00
|
|
|
writtenBytes int
|
2016-04-04 18:04:14 +02:00
|
|
|
frames int
|
|
|
|
players map[*Player]struct{}
|
2016-04-04 19:50:08 +02:00
|
|
|
|
|
|
|
// Note that Read (and other methods) need to be concurrent safe
|
|
|
|
// because Read is called from another groutine (see NewContext).
|
2016-04-04 19:24:54 +02:00
|
|
|
sync.RWMutex
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2016-03-12 19:33:02 +01:00
|
|
|
const (
|
|
|
|
channelNum = 2
|
|
|
|
bytesPerSample = 2
|
|
|
|
bitsPerSample = bytesPerSample * 8
|
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-04 19:50:08 +02:00
|
|
|
func (s *mixingStream) SampleRate() int {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
return s.sampleRate
|
|
|
|
}
|
|
|
|
|
2016-04-04 17:40:16 +02:00
|
|
|
func (s *mixingStream) Read(b []byte) (int, error) {
|
2016-04-04 18:04:14 +02:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2016-03-03 03:57:25 +01:00
|
|
|
|
2016-04-04 18:04:14 +02:00
|
|
|
bytesPerFrame := s.sampleRate * bytesPerSample * channelNum / ebiten.FPS
|
|
|
|
x := s.frames*bytesPerFrame + len(b)
|
2016-03-10 16:01:00 +01:00
|
|
|
if x <= s.writtenBytes {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2016-04-04 18:04:14 +02:00
|
|
|
if len(s.players) == 0 {
|
2016-03-10 16:01:00 +01:00
|
|
|
l := min(len(b), x-s.writtenBytes)
|
2016-03-15 19:02:54 +01:00
|
|
|
l &= mask
|
2016-03-10 16:01:00 +01:00
|
|
|
copy(b, make([]byte, l))
|
|
|
|
s.writtenBytes += l
|
|
|
|
return l, nil
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
|
|
|
closed := []*Player{}
|
2016-03-15 18:57:54 +01:00
|
|
|
l := len(b)
|
2016-04-04 18:04:14 +02:00
|
|
|
for p := range s.players {
|
2016-04-04 19:50:08 +02:00
|
|
|
err := p.readToBuffer(l)
|
2016-03-03 03:57:25 +01:00
|
|
|
if err == io.EOF {
|
|
|
|
closed = append(closed, p)
|
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
|
|
|
}
|
2016-03-15 19:02:54 +01:00
|
|
|
l = min(p.bufferLength(), 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{}
|
2016-04-04 18:04:14 +02:00
|
|
|
for p := range s.players {
|
2016-03-15 18:57:54 +01:00
|
|
|
b16s = append(b16s, p.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)
|
|
|
|
}
|
2016-04-04 18:04:14 +02:00
|
|
|
for p := range s.players {
|
2016-03-15 18:57:54 +01:00
|
|
|
p.proceed(l)
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
|
|
|
for _, p := range closed {
|
2016-04-04 18:04:14 +02:00
|
|
|
delete(s.players, p)
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
2016-03-15 18:57:54 +01:00
|
|
|
s.writtenBytes += l
|
|
|
|
return l, nil
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 19:50:08 +02:00
|
|
|
func (s *mixingStream) update() error {
|
2016-04-04 18:04:14 +02:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
s.frames++
|
2016-04-04 19:50:08 +02:00
|
|
|
return nil
|
2016-04-04 18:04:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mixingStream) newPlayer(src ReadSeekCloser) (*Player, error) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
p := &Player{
|
|
|
|
stream: s,
|
|
|
|
src: src,
|
|
|
|
buf: []byte{},
|
|
|
|
volume: 1,
|
|
|
|
}
|
|
|
|
// Get the current position of the source.
|
|
|
|
pos, err := p.src.Seek(0, 1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.pos = pos
|
|
|
|
runtime.SetFinalizer(p, (*Player).Close)
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mixingStream) closePlayer(player *Player) error {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
runtime.SetFinalizer(player, nil)
|
|
|
|
return player.src.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mixingStream) addPlayer(player *Player) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
s.players[player] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mixingStream) removePlayer(player *Player) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
delete(s.players, player)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mixingStream) hasPlayer(player *Player) bool {
|
2016-04-04 19:24:54 +02:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
2016-04-04 18:04:14 +02:00
|
|
|
_, ok := s.players[player]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mixingStream) seekPlayer(player *Player, offset time.Duration) error {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
o := int64(offset) * bytesPerSample * channelNum * int64(s.sampleRate) / int64(time.Second)
|
|
|
|
o &= mask
|
|
|
|
return player.seek(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mixingStream) playerCurrent(player *Player) time.Duration {
|
2016-04-04 19:24:54 +02:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
2016-04-04 18:04:14 +02:00
|
|
|
sample := player.pos / bytesPerSample / channelNum
|
|
|
|
return time.Duration(sample) * time.Second / time.Duration(s.sampleRate)
|
|
|
|
}
|
|
|
|
|
2016-03-02 16:48:59 +01:00
|
|
|
// TODO: Enable to specify the format like Mono8?
|
|
|
|
|
|
|
|
type Context struct {
|
2016-04-04 19:37:29 +02:00
|
|
|
stream *mixingStream
|
|
|
|
errorCh chan error
|
2016-03-02 16:48:59 +01:00
|
|
|
}
|
|
|
|
|
2016-04-02 19:59:44 +02:00
|
|
|
func NewContext(sampleRate int) (*Context, error) {
|
2016-03-03 03:57:25 +01:00
|
|
|
// TODO: Panic if one context exists.
|
|
|
|
c := &Context{
|
2016-04-04 19:37:29 +02:00
|
|
|
errorCh: make(chan error),
|
2016-03-02 16:48:59 +01:00
|
|
|
}
|
2016-04-04 18:04:14 +02:00
|
|
|
c.stream = &mixingStream{
|
|
|
|
sampleRate: sampleRate,
|
|
|
|
players: map[*Player]struct{}{},
|
2016-03-10 16:01:00 +01:00
|
|
|
}
|
2016-04-04 19:37:29 +02:00
|
|
|
// TODO: Rename this other than player
|
|
|
|
p, err := newPlayer(c.stream, sampleRate)
|
2016-04-04 16:42:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
2016-04-04 16:42:44 +02:00
|
|
|
go func() {
|
|
|
|
// TODO: Is it OK to close asap?
|
|
|
|
defer p.close()
|
|
|
|
for {
|
|
|
|
err := p.proceed()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-04-04 17:09:00 +02:00
|
|
|
c.errorCh <- err
|
|
|
|
return
|
2016-04-04 16:42:44 +02:00
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}()
|
2016-04-02 19:59:44 +02:00
|
|
|
return c, nil
|
2016-03-02 16:48:59 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 16:01:00 +01:00
|
|
|
// Update proceeds the inner (logical) time of the context by 1/60 second.
|
|
|
|
// This is expected to be called in the game's updating function (sync mode)
|
|
|
|
// or an independent goroutine with timers (unsync mode).
|
|
|
|
// In sync mode, the game logical time syncs the audio logical time and
|
|
|
|
// you will find audio stops when the game stops e.g. when the window is deactivated.
|
|
|
|
// In unsync mode, the audio never stops even when the game stops.
|
2016-04-04 17:09:00 +02:00
|
|
|
func (c *Context) Update() error {
|
|
|
|
select {
|
|
|
|
case err := <-c.errorCh:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
}
|
2016-04-04 19:50:08 +02:00
|
|
|
return c.stream.update()
|
2016-03-10 16:01:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 19:00:05 +01:00
|
|
|
// SampleRate returns the sample rate.
|
2016-04-04 18:04:14 +02:00
|
|
|
// All audio source must have the same sample rate.
|
2016-03-12 19:00:05 +01:00
|
|
|
func (c *Context) SampleRate() int {
|
2016-04-04 19:50:08 +02:00
|
|
|
return c.stream.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
|
|
|
|
}
|
|
|
|
|
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-04 18:04:14 +02:00
|
|
|
stream *mixingStream
|
|
|
|
src ReadSeekCloser
|
|
|
|
buf []byte
|
|
|
|
pos int64
|
|
|
|
volume float64
|
2016-02-10 18:18:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPlayer creates a new player with the given data to the given channel.
|
2016-02-07 16:51:25 +01:00
|
|
|
// The given data is queued to the end of the buffer.
|
|
|
|
// This may not be played immediately when data already exists in the buffer.
|
2015-01-24 07:48:48 +01:00
|
|
|
//
|
2016-02-10 18:04:23 +01:00
|
|
|
// src's format must be linear PCM (16bits, 2 channel stereo, little endian)
|
2016-02-07 16:51:25 +01:00
|
|
|
// without a header (e.g. RIFF header).
|
2016-03-28 17:06:37 +02:00
|
|
|
func (c *Context) NewPlayer(src ReadSeekCloser) (*Player, error) {
|
2016-04-04 18:04:14 +02:00
|
|
|
return c.stream.newPlayer(src)
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
2016-02-12 13:39:48 +01:00
|
|
|
|
2016-03-28 17:06:37 +02:00
|
|
|
func (p *Player) Close() error {
|
2016-04-04 18:04:14 +02:00
|
|
|
return p.stream.closePlayer(p)
|
2016-03-28 17:06:37 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 19:50:08 +02:00
|
|
|
func (p *Player) readToBuffer(length int) error {
|
2016-03-06 14:03:11 +01:00
|
|
|
bb := make([]byte, length)
|
|
|
|
n, err := p.src.Read(bb)
|
|
|
|
if 0 < n {
|
|
|
|
p.buf = append(p.buf, bb[:n]...)
|
|
|
|
}
|
2016-04-04 19:50:08 +02:00
|
|
|
return err
|
2016-03-06 14:03:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) bufferToInt16(lengthInBytes int) []int16 {
|
|
|
|
r := make([]int16, lengthInBytes/2)
|
|
|
|
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
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) proceed(length int) {
|
|
|
|
p.buf = p.buf[length:]
|
|
|
|
p.pos += int64(length)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) bufferLength() int {
|
|
|
|
return len(p.buf)
|
|
|
|
}
|
|
|
|
|
2016-02-10 18:18:39 +01:00
|
|
|
func (p *Player) Play() error {
|
2016-04-04 18:04:14 +02:00
|
|
|
p.stream.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-03-06 10:55:20 +01:00
|
|
|
func (p *Player) IsPlaying() bool {
|
2016-04-04 18:04:14 +02:00
|
|
|
return p.stream.hasPlayer(p)
|
2016-03-06 10:55:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) Rewind() error {
|
|
|
|
return p.Seek(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) Seek(offset time.Duration) error {
|
2016-04-04 18:04:14 +02:00
|
|
|
return p.stream.seekPlayer(p, offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) seek(offset int64) error {
|
2016-03-06 10:55:20 +01:00
|
|
|
p.buf = []byte{}
|
2016-04-04 18:04:14 +02:00
|
|
|
pos, err := p.src.Seek(offset, 0)
|
2016-03-06 10:55:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.pos = pos
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-03 03:57:25 +01:00
|
|
|
|
2016-03-04 17:01:57 +01:00
|
|
|
func (p *Player) Pause() error {
|
2016-04-04 18:04:14 +02:00
|
|
|
p.stream.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
|
|
|
|
|
|
|
func (p *Player) Current() time.Duration {
|
2016-04-04 18:04:14 +02:00
|
|
|
return p.stream.playerCurrent(p)
|
2016-03-06 10:55:20 +01:00
|
|
|
}
|
|
|
|
|
2016-03-28 04:06:17 +02:00
|
|
|
func (p *Player) Volume() float64 {
|
|
|
|
return p.volume
|
|
|
|
}
|
|
|
|
|
2016-04-03 19:16:26 +02:00
|
|
|
// SetVolume sets the volume.
|
|
|
|
// volume must be in between 0 and 1. This function panics otherwise.
|
2016-03-28 04:06:17 +02:00
|
|
|
func (p *Player) 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
|
|
|
}
|
|
|
|
p.volume = volume
|
|
|
|
}
|
|
|
|
|
2016-03-06 10:55:20 +01:00
|
|
|
// TODO: Panning
|