audio: Refactoring

This commit is contained in:
Hajime Hoshi 2016-04-05 02:50:08 +09:00
parent 7bdda4e9ca
commit ff8f15a212

View File

@ -28,6 +28,9 @@ type mixingStream struct {
writtenBytes int writtenBytes int
frames int frames int
players map[*Player]struct{} players map[*Player]struct{}
// Note that Read (and other methods) need to be concurrent safe
// because Read is called from another groutine (see NewContext).
sync.RWMutex sync.RWMutex
} }
@ -47,6 +50,12 @@ const (
mask = ^(channelNum*bytesPerSample - 1) mask = ^(channelNum*bytesPerSample - 1)
) )
func (s *mixingStream) SampleRate() int {
s.RLock()
defer s.RUnlock()
return s.sampleRate
}
func (s *mixingStream) Read(b []byte) (int, error) { func (s *mixingStream) Read(b []byte) (int, error) {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
@ -67,7 +76,7 @@ func (s *mixingStream) Read(b []byte) (int, error) {
closed := []*Player{} closed := []*Player{}
l := len(b) l := len(b)
for p := range s.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)
} else if err != nil { } else if err != nil {
@ -104,10 +113,11 @@ func (s *mixingStream) Read(b []byte) (int, error) {
return l, nil return l, nil
} }
func (s *mixingStream) update() { func (s *mixingStream) update() error {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
s.frames++ s.frames++
return nil
} }
func (s *mixingStream) newPlayer(src ReadSeekCloser) (*Player, error) { func (s *mixingStream) newPlayer(src ReadSeekCloser) (*Player, error) {
@ -221,14 +231,13 @@ func (c *Context) Update() error {
return err return err
default: default:
} }
c.stream.update() return c.stream.update()
return nil
} }
// SampleRate returns the sample rate. // SampleRate returns the sample rate.
// All audio source must have the same sample rate. // All audio source must have the same sample rate.
func (c *Context) SampleRate() int { func (c *Context) SampleRate() int {
return c.stream.sampleRate return c.stream.SampleRate()
} }
// ReadSeekCloser is an io.ReadSeeker and io.Closer. // ReadSeekCloser is an io.ReadSeeker and io.Closer.
@ -260,13 +269,13 @@ func (p *Player) Close() error {
return p.stream.closePlayer(p) return p.stream.closePlayer(p)
} }
func (p *Player) readToBuffer(length int) (int, error) { func (p *Player) readToBuffer(length int) error {
bb := make([]byte, length) bb := make([]byte, length)
n, err := p.src.Read(bb) n, err := p.src.Read(bb)
if 0 < n { if 0 < n {
p.buf = append(p.buf, bb[:n]...) p.buf = append(p.buf, bb[:n]...)
} }
return n, err return err
} }
func (p *Player) bufferToInt16(lengthInBytes int) []int16 { func (p *Player) bufferToInt16(lengthInBytes int) []int16 {