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