From 0bf6eee605329846e31ff264190063a61e9d7b68 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 4 Jan 2021 21:51:26 +0900 Subject: [PATCH] audio: Reduce making a slice at a player's read --- audio/audio.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/audio/audio.go b/audio/audio.go index c3b0bc2f4..aad4d9e5a 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -229,9 +229,10 @@ type playerImpl struct { closedExplicitly bool isLoopActive bool - buf []byte - pos int64 - volume float64 + buf []byte + readbuf []byte + pos int64 + volume float64 m sync.Mutex } @@ -412,8 +413,10 @@ func (p *playerImpl) read() ([]byte, bool) { <-p.context.semaphore }() - newBuf := make([]byte, bufSize-len(p.buf)) - n, err := p.src.Read(newBuf) + if p.readbuf == nil { + p.readbuf = make([]byte, bufSize) + } + n, err := p.src.Read(p.readbuf[:bufSize-len(p.buf)]) if err != nil { if err != io.EOF { p.context.setError(err) @@ -423,7 +426,7 @@ func (p *playerImpl) read() ([]byte, bool) { return nil, false } } - buf := append(p.buf, newBuf[:n]...) + buf := append(p.buf, p.readbuf[:n]...) n2 := len(buf) - len(buf)%bytesPerSample buf, p.buf = buf[:n2], buf[n2:]