audio: Refactoring

This commit is contained in:
Hajime Hoshi 2019-05-02 03:19:17 +09:00
parent 5dd12afd38
commit 2e35d9dc70

View File

@ -454,32 +454,27 @@ func (p *playerImpl) read() ([]byte, bool) {
const bufSize = 2048 const bufSize = 2048
var buf []byte if !p.context.playable() {
var err error
var proceed int64
if p.context.playable() {
newBuf := make([]byte, bufSize-len(p.buf))
n := 0
n, err = p.src.Read(newBuf)
buf = append(p.buf, newBuf[:n]...)
n2 := len(buf) - len(buf)%bytesPerSample
buf, p.buf = buf[:n2], buf[n2:]
proceed = int64(len(buf))
} else {
// Fill zero values, or the driver can block forever as trying to proceed. // Fill zero values, or the driver can block forever as trying to proceed.
buf = make([]byte, bufSize) buf := make([]byte, bufSize)
return buf, true
} }
if err == io.EOF && len(buf) == 0 { newBuf := make([]byte, bufSize-len(p.buf))
return nil, false n, err := p.src.Read(newBuf)
} if err != nil {
if err != io.EOF {
if err != nil && err != io.EOF {
p.context.setError(err) p.context.setError(err)
return nil, false return nil, false
} }
if n == 0 {
return nil, false
}
}
buf := append(p.buf, newBuf[:n]...)
n2 := len(buf) - len(buf)%bytesPerSample
buf, p.buf = buf[:n2], buf[n2:]
for i := 0; i < len(buf)/2; i++ { for i := 0; i < len(buf)/2; i++ {
v16 := int16(buf[2*i]) | (int16(buf[2*i+1]) << 8) v16 := int16(buf[2*i]) | (int16(buf[2*i+1]) << 8)
@ -487,7 +482,7 @@ func (p *playerImpl) read() ([]byte, bool) {
buf[2*i] = byte(v16) buf[2*i] = byte(v16)
buf[2*i+1] = byte(v16 >> 8) buf[2*i+1] = byte(v16 >> 8)
} }
p.pos += proceed p.pos += int64(len(buf))
p.context.setReady() p.context.setReady()
return buf, true return buf, true