ebiten/exp/audio/audio.go

244 lines
5.2 KiB
Go
Raw Normal View History

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-03-03 03:57:25 +01:00
"fmt"
2016-02-10 18:04:23 +01:00
"io"
2016-03-03 03:57:25 +01:00
"sync"
2016-03-06 10:55:20 +01:00
"time"
2015-01-10 17:23:43 +01:00
)
2016-03-03 03:57:25 +01:00
type mixedPlayersStream struct {
2016-03-10 16:01:00 +01:00
context *Context
writtenBytes int
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-03 03:57:25 +01:00
func (s *mixedPlayersStream) Read(b []byte) (int, error) {
s.context.Lock()
defer s.context.Unlock()
2016-03-10 16:01:00 +01:00
// TODO: 60 (FPS) is a magic number
2016-03-12 19:33:02 +01:00
bytesPerFrame := s.context.sampleRate * bytesPerSample * channelNum / 60
2016-03-10 16:01:00 +01:00
x := s.context.frames*bytesPerFrame + len(b)
if x <= s.writtenBytes {
return 0, nil
}
l := len(b) / 4 * 4
2016-03-03 03:57:25 +01:00
if len(s.context.players) == 0 {
2016-03-10 16:01:00 +01:00
l := min(len(b), x-s.writtenBytes)
copy(b, make([]byte, l))
s.writtenBytes += l
return l, nil
2016-03-03 03:57:25 +01:00
}
closed := []*Player{}
2016-03-05 08:49:35 +01:00
ll := l
2016-03-03 03:57:25 +01:00
for p := range s.context.players {
2016-03-06 14:03:11 +01:00
_, err := p.readToBuffer(l)
2016-03-03 03:57:25 +01:00
if err == io.EOF {
closed = append(closed, p)
} 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-06 14:03:11 +01:00
ll = min(p.bufferLength()/4*4, ll)
}
b16s := [][]int16{}
for p := range s.context.players {
b16s = append(b16s, p.bufferToInt16(ll))
2016-03-03 03:57:25 +01:00
}
2016-03-05 08:49:35 +01:00
for i := 0; i < ll/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)
}
for p := range s.context.players {
2016-03-06 14:03:11 +01:00
p.proceed(ll)
2016-03-03 03:57:25 +01:00
}
for _, p := range closed {
delete(s.context.players, p)
}
2016-03-10 16:01:00 +01:00
s.writtenBytes += ll
2016-03-05 08:49:35 +01:00
return ll, nil
2016-03-03 03:57:25 +01:00
}
2016-03-02 16:48:59 +01:00
// TODO: Enable to specify the format like Mono8?
type Context struct {
sampleRate int
stream *mixedPlayersStream
players map[*Player]struct{}
innerPlayer *player
2016-03-10 16:01:00 +01:00
frames int
2016-03-03 03:57:25 +01:00
sync.Mutex
2016-03-02 16:48:59 +01:00
}
func NewContext(sampleRate int) *Context {
2016-03-03 03:57:25 +01:00
// TODO: Panic if one context exists.
c := &Context{
2016-03-02 16:48:59 +01:00
sampleRate: sampleRate,
2016-03-03 03:57:25 +01:00
players: map[*Player]struct{}{},
2016-03-02 16:48:59 +01:00
}
2016-03-10 16:01:00 +01:00
c.stream = &mixedPlayersStream{
context: c,
}
p, err := startPlaying(c.stream, c.sampleRate)
if err != nil {
2016-03-03 03:57:25 +01:00
panic(fmt.Sprintf("audio: NewContext error: %v", err))
}
c.innerPlayer = p
2016-03-03 03:57:25 +01:00
return c
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.
func (c *Context) Update() {
c.Lock()
defer c.Unlock()
c.frames++
}
// SampleRate returns the sample rate.
func (c *Context) SampleRate() int {
c.Lock()
defer c.Unlock()
return c.sampleRate
}
2016-02-10 18:18:39 +01:00
type Player struct {
2016-03-03 03:57:25 +01:00
context *Context
src io.ReadSeeker
buf []byte
2016-03-06 10:55:20 +01:00
pos int64
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-02 16:48:59 +01:00
func (c *Context) NewPlayer(src io.ReadSeeker) (*Player, error) {
2016-03-03 03:57:25 +01:00
c.Lock()
defer c.Unlock()
p := &Player{
context: c,
src: src,
buf: []byte{},
}
2016-03-06 10:55:20 +01:00
// Get the current position of the source.
pos, err := p.src.Seek(0, 1)
if err != nil {
return nil, err
}
p.pos = pos
2016-03-03 03:57:25 +01:00
return p, nil
}
2016-03-06 14:03:11 +01:00
func (p *Player) readToBuffer(length int) (int, error) {
bb := make([]byte, length)
n, err := p.src.Read(bb)
if 0 < n {
p.buf = append(p.buf, bb[:n]...)
}
return n, err
}
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)
}
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-03-03 03:57:25 +01:00
p.context.Lock()
defer p.context.Unlock()
p.context.players[p] = struct{}{}
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 {
_, ok := p.context.players[p]
return ok
}
func (p *Player) Rewind() error {
return p.Seek(0)
}
func (p *Player) Seek(offset time.Duration) error {
p.buf = []byte{}
o := int64(offset) * int64(p.context.sampleRate) / int64(time.Second)
pos, err := p.src.Seek(o, 0)
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-03-03 03:57:25 +01:00
p.context.Lock()
defer p.context.Unlock()
delete(p.context.players, p)
return nil
2016-02-11 11:55:59 +01:00
}
2016-03-06 10:55:20 +01:00
func (p *Player) Current() time.Duration {
return time.Duration(p.pos) * time.Second / time.Duration(p.context.sampleRate)
}
// TODO: Volume / SetVolume?
// TODO: Panning