audio: Refactoring

This commit is contained in:
Hajime Hoshi 2016-04-06 01:26:21 +09:00
parent 3cedae882f
commit 1ded7dc6b8

View File

@ -29,9 +29,6 @@ const (
maxBufferNum = 8 maxBufferNum = 8
) )
// TODO: This should be in player
var totalBufferNum = 0
type player struct { type player struct {
alSource al.Source alSource al.Source
alBuffers []al.Buffer alBuffers []al.Buffer
@ -56,22 +53,12 @@ func newPlayer(src io.Reader, sampleRate int) (*player, error) {
} }
runtime.SetFinalizer(p, (*player).close) runtime.SetFinalizer(p, (*player).close)
n := maxBufferNum - int(p.alSource.BuffersQueued()) - len(p.alBuffers) bs := al.GenBuffers(maxBufferNum)
if 0 < n {
p.alBuffers = append(p.alBuffers, al.GenBuffers(n)...)
totalBufferNum += n
if maxBufferNum < totalBufferNum {
panic("audio: not reach: too many buffers are created")
}
}
if 0 < len(p.alBuffers) {
emptyBytes := make([]byte, bufferSize) emptyBytes := make([]byte, bufferSize)
for _, buf := range p.alBuffers { for _, b := range bs {
// Note that the third argument of only the first buffer is used. // Note that the third argument of only the first buffer is used.
buf.BufferData(al.FormatStereo16, emptyBytes, int32(p.sampleRate)) b.BufferData(al.FormatStereo16, emptyBytes, int32(p.sampleRate))
p.alSource.QueueBuffers(buf) p.alSource.QueueBuffers(b)
}
p.alBuffers = []al.Buffer{}
} }
al.PlaySources(p.alSource) al.PlaySources(p.alSource)
return p, nil return p, nil