audio: Refactoring again (OpenAL's buffer requires fixed-size data)

This commit is contained in:
Hajime Hoshi 2015-02-14 20:11:40 +09:00
parent e13e153cb4
commit d5a2bdd7ac
2 changed files with 9 additions and 4 deletions

View File

@ -148,8 +148,8 @@ func loadChannelBuffer(channel int, bufferSize int) (l, r []int16) {
ch := channels[channel]
length := min(len(ch.l), bufferSize)
inputL := make([]int16, bufferSize)
inputR := make([]int16, bufferSize)
inputL := make([]int16, length)
inputR := make([]int16, length)
copy(inputL, ch.l[:length])
copy(inputR, ch.r[:length])
ch.l = ch.l[length:]

View File

@ -26,7 +26,7 @@ import (
"time"
)
func toBytes(l, r []int16) []byte {
func toBytesWithPadding(l, r []int16, size int) []byte {
if len(l) != len(r) {
panic("len(l) must equal to len(r)")
}
@ -36,6 +36,11 @@ func toBytes(l, r []int16) []byte {
panic(err)
}
}
if 0 < size-len(l) {
if err := binary.Write(b, binary.LittleEndian, make([]int16, (size-len(l))*2)); err != nil {
panic(err)
}
}
return b.Bytes()
}
@ -91,7 +96,7 @@ func initialize() {
source.UnqueueBuffers(buffers)
for _, buffer := range buffers {
l, r := loadChannelBuffer(channel, bufferSize)
b := toBytes(l, r)
b := toBytesWithPadding(l, r, bufferSize)
buffer.SetData(openal.FormatStereo16, b, SampleRate)
source.QueueBuffer(buffer)
}