audio/internal/readerdriver: Refactoring

This commit is contained in:
Hajime Hoshi 2021-04-20 03:20:29 +09:00
parent 7f8c3a13b3
commit 08c3025560

View File

@ -182,17 +182,15 @@ func (p *playerImpl) appendBuffer(this js.Value, args []js.Value) interface{} {
}
tmp := make([]byte, 4096)
need := p.context.oneBufferSize()
bs := make([]byte, 0, need)
for need > 0 {
bs := make([]byte, 0, p.context.oneBufferSize())
for cap(bs)-len(bs) > 0 {
if len(p.buf) > 0 {
n := len(p.buf)
if n > need {
if need := cap(bs) - len(bs); n > need {
n = need
}
bs = append(bs, p.buf[:n]...)
p.buf = p.buf[n:]
need -= n
continue
}
n, err := p.src.Read(tmp)
@ -201,12 +199,11 @@ func (p *playerImpl) appendBuffer(this js.Value, args []js.Value) interface{} {
p.Pause()
return nil
}
if n > need {
if need := cap(bs) - len(bs); n > need {
p.buf = append(p.buf, tmp[need:]...)
n = need
}
bs = append(bs, tmp[:n]...)
need -= n
if err == io.EOF {
p.eof = true
break