audio: Refactoring: mixingStream has the global lock

This commit is contained in:
Hajime Hoshi 2016-04-05 01:04:14 +09:00
parent da6ffdf2b9
commit 79985535c3

View File

@ -24,8 +24,11 @@ import (
) )
type mixingStream struct { type mixingStream struct {
context *Context sampleRate int
writtenBytes int writtenBytes int
frames int
players map[*Player]struct{}
sync.Mutex
} }
func min(a, b int) int { func min(a, b int) int {
@ -45,16 +48,16 @@ const (
) )
func (s *mixingStream) Read(b []byte) (int, error) { func (s *mixingStream) Read(b []byte) (int, error) {
s.context.Lock() s.Lock()
defer s.context.Unlock() defer s.Unlock()
bytesPerFrame := s.context.sampleRate * bytesPerSample * channelNum / ebiten.FPS bytesPerFrame := s.sampleRate * bytesPerSample * channelNum / ebiten.FPS
x := s.context.frames*bytesPerFrame + len(b) x := s.frames*bytesPerFrame + len(b)
if x <= s.writtenBytes { if x <= s.writtenBytes {
return 0, nil return 0, nil
} }
if len(s.context.players) == 0 { if len(s.players) == 0 {
l := min(len(b), x-s.writtenBytes) l := min(len(b), x-s.writtenBytes)
l &= mask l &= mask
copy(b, make([]byte, l)) copy(b, make([]byte, l))
@ -63,7 +66,7 @@ func (s *mixingStream) Read(b []byte) (int, error) {
} }
closed := []*Player{} closed := []*Player{}
l := len(b) l := len(b)
for p := range s.context.players { for p := range s.players {
_, err := p.readToBuffer(l) _, err := p.readToBuffer(l)
if err == io.EOF { if err == io.EOF {
closed = append(closed, p) closed = append(closed, p)
@ -74,7 +77,7 @@ func (s *mixingStream) Read(b []byte) (int, error) {
} }
l &= mask l &= mask
b16s := [][]int16{} b16s := [][]int16{}
for p := range s.context.players { for p := range s.players {
b16s = append(b16s, p.bufferToInt16(l)) b16s = append(b16s, p.bufferToInt16(l))
} }
for i := 0; i < l/2; i++ { for i := 0; i < l/2; i++ {
@ -91,37 +94,101 @@ func (s *mixingStream) Read(b []byte) (int, error) {
b[2*i] = byte(x) b[2*i] = byte(x)
b[2*i+1] = byte(x >> 8) b[2*i+1] = byte(x >> 8)
} }
for p := range s.context.players { for p := range s.players {
p.proceed(l) p.proceed(l)
} }
for _, p := range closed { for _, p := range closed {
delete(s.context.players, p) delete(s.players, p)
} }
s.writtenBytes += l s.writtenBytes += l
return l, nil return l, nil
} }
func (s *mixingStream) update() {
s.Lock()
defer s.Unlock()
s.frames++
}
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 {
s.Lock()
defer s.Unlock()
_, 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 {
s.Lock()
defer s.Unlock()
sample := player.pos / bytesPerSample / channelNum
return time.Duration(sample) * time.Second / time.Duration(s.sampleRate)
}
// TODO: Enable to specify the format like Mono8? // TODO: Enable to specify the format like Mono8?
type Context struct { type Context struct {
sampleRate int sampleRate int
players map[*Player]struct{} stream *mixingStream
frames int
errorCh chan error errorCh chan error
sync.Mutex
} }
func NewContext(sampleRate int) (*Context, error) { func NewContext(sampleRate int) (*Context, error) {
// TODO: Panic if one context exists. // TODO: Panic if one context exists.
c := &Context{ c := &Context{
sampleRate: sampleRate, sampleRate: sampleRate,
players: map[*Player]struct{}{},
errorCh: make(chan error), errorCh: make(chan error),
} }
s := &mixingStream{ c.stream = &mixingStream{
context: c, sampleRate: sampleRate,
players: map[*Player]struct{}{},
} }
p, err := newPlayer(s, c.sampleRate) p, err := newPlayer(c.stream, c.sampleRate)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -150,21 +217,18 @@ func NewContext(sampleRate int) (*Context, error) {
// you will find audio stops when the game stops e.g. when the window is deactivated. // 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. // In unsync mode, the audio never stops even when the game stops.
func (c *Context) Update() error { func (c *Context) Update() error {
c.Lock()
defer c.Unlock()
select { select {
case err := <-c.errorCh: case err := <-c.errorCh:
return err return err
default: default:
} }
c.frames++ c.stream.update()
return nil return nil
} }
// SampleRate returns the sample rate. // SampleRate returns the sample rate.
// All audio source must have the same sample rate.
func (c *Context) SampleRate() int { func (c *Context) SampleRate() int {
c.Lock()
defer c.Unlock()
return c.sampleRate return c.sampleRate
} }
@ -174,7 +238,7 @@ type ReadSeekCloser interface {
} }
type Player struct { type Player struct {
context *Context stream *mixingStream
src ReadSeekCloser src ReadSeekCloser
buf []byte buf []byte
pos int64 pos int64
@ -188,27 +252,11 @@ type Player struct {
// src's format must be linear PCM (16bits, 2 channel stereo, little endian) // src's format must be linear PCM (16bits, 2 channel stereo, little endian)
// without a header (e.g. RIFF header). // without a header (e.g. RIFF header).
func (c *Context) NewPlayer(src ReadSeekCloser) (*Player, error) { func (c *Context) NewPlayer(src ReadSeekCloser) (*Player, error) {
c.Lock() return c.stream.newPlayer(src)
defer c.Unlock()
p := &Player{
context: c,
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 (p *Player) Close() error { func (p *Player) Close() error {
runtime.SetFinalizer(p, nil) return p.stream.closePlayer(p)
return p.src.Close()
} }
func (p *Player) readToBuffer(length int) (int, error) { func (p *Player) readToBuffer(length int) (int, error) {
@ -239,16 +287,12 @@ func (p *Player) bufferLength() int {
} }
func (p *Player) Play() error { func (p *Player) Play() error {
p.context.Lock() p.stream.addPlayer(p)
defer p.context.Unlock()
p.context.players[p] = struct{}{}
return nil return nil
} }
func (p *Player) IsPlaying() bool { func (p *Player) IsPlaying() bool {
_, ok := p.context.players[p] return p.stream.hasPlayer(p)
return ok
} }
func (p *Player) Rewind() error { func (p *Player) Rewind() error {
@ -256,10 +300,12 @@ func (p *Player) Rewind() error {
} }
func (p *Player) Seek(offset time.Duration) error { func (p *Player) Seek(offset time.Duration) error {
return p.stream.seekPlayer(p, offset)
}
func (p *Player) seek(offset int64) error {
p.buf = []byte{} p.buf = []byte{}
o := int64(offset) * bytesPerSample * channelNum * int64(p.context.sampleRate) / int64(time.Second) pos, err := p.src.Seek(offset, 0)
o &= mask
pos, err := p.src.Seek(o, 0)
if err != nil { if err != nil {
return err return err
} }
@ -268,15 +314,12 @@ func (p *Player) Seek(offset time.Duration) error {
} }
func (p *Player) Pause() error { func (p *Player) Pause() error {
p.context.Lock() p.stream.removePlayer(p)
defer p.context.Unlock()
delete(p.context.players, p)
return nil return nil
} }
func (p *Player) Current() time.Duration { func (p *Player) Current() time.Duration {
return time.Duration(p.pos/bytesPerSample/channelNum) * time.Second / time.Duration(p.context.sampleRate) return p.stream.playerCurrent(p)
} }
func (p *Player) Volume() float64 { func (p *Player) Volume() float64 {