audio: Reduce making a slice at a player's read

This commit is contained in:
Hajime Hoshi 2021-01-04 21:51:26 +09:00
parent 3fb304e5a8
commit 0bf6eee605

View File

@ -229,9 +229,10 @@ type playerImpl struct {
closedExplicitly bool closedExplicitly bool
isLoopActive bool isLoopActive bool
buf []byte buf []byte
pos int64 readbuf []byte
volume float64 pos int64
volume float64
m sync.Mutex m sync.Mutex
} }
@ -412,8 +413,10 @@ func (p *playerImpl) read() ([]byte, bool) {
<-p.context.semaphore <-p.context.semaphore
}() }()
newBuf := make([]byte, bufSize-len(p.buf)) if p.readbuf == nil {
n, err := p.src.Read(newBuf) p.readbuf = make([]byte, bufSize)
}
n, err := p.src.Read(p.readbuf[:bufSize-len(p.buf)])
if err != nil { if err != nil {
if err != io.EOF { if err != io.EOF {
p.context.setError(err) p.context.setError(err)
@ -423,7 +426,7 @@ func (p *playerImpl) read() ([]byte, bool) {
return nil, false return nil, false
} }
} }
buf := append(p.buf, newBuf[:n]...) buf := append(p.buf, p.readbuf[:n]...)
n2 := len(buf) - len(buf)%bytesPerSample n2 := len(buf) - len(buf)%bytesPerSample
buf, p.buf = buf[:n2], buf[n2:] buf, p.buf = buf[:n2], buf[n2:]