Fix audio playback stutter

This commit is contained in:
Nonoo 2020-10-20 16:09:59 +02:00
parent e484a51c31
commit 9a08b66994

View file

@ -24,30 +24,32 @@ type audioStruct struct {
var audio audioStruct
func (a *audioStruct) playLoop() {
frameBuf := make([]byte, 1920)
for {
<-a.canPlay
// Trying to read the whole buffer.
d := make([]byte, a.playBuf.Len())
bytesToWrite, err := a.playBuf.Read(d)
if err != nil {
log.Error(err)
continue
}
for {
written, err := a.source.Write(d)
for a.playBuf.Len() > 0 {
bytesToWrite, err := a.playBuf.Read(frameBuf)
if err != nil {
if _, ok := err.(*os.PathError); !ok {
log.Error(err)
log.Error(err)
continue
}
for {
written, err := a.source.Write(frameBuf[:bytesToWrite])
if err != nil {
if _, ok := err.(*os.PathError); !ok {
log.Error(err)
}
return
}
return
bytesToWrite -= written
if bytesToWrite == 0 {
break
}
frameBuf = frameBuf[written:]
}
bytesToWrite -= written
if bytesToWrite == 0 {
break
}
d = d[written:]
}
}
}