From 4df5d02c36b9d92230fdf075c58d298934228dd7 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 11 Feb 2025 14:44:47 +0900 Subject: [PATCH] audio/vorbis: bug fix: an int16Stream with io.Reader didn't work Closes #3192 --- audio/vorbis/vorbis.go | 15 ++------------- audio/vorbis/vorbis_test.go | 8 +++++++- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/audio/vorbis/vorbis.go b/audio/vorbis/vorbis.go index 960091a26..077fbb703 100644 --- a/audio/vorbis/vorbis.go +++ b/audio/vorbis/vorbis.go @@ -113,14 +113,7 @@ func (s *i16Stream) Read(b []byte) (int, error) { s.i16Reader = newInt16BytesReaderFromFloat32Reader(s.vorbisReader) } - l := s.totalBytes() - s.posInBytes - if l > int64(len(b)) { - l = int64(len(b)) - } - if l < 0 { - return 0, io.EOF - } - + l := int64(len(b)) retry: n, err := s.i16Reader.Read(b[:l]) if err != nil && err != io.EOF { @@ -130,12 +123,8 @@ retry: // When l is too small, decoder's Read might return 0 for a while. Let's retry. goto retry } - s.posInBytes += int64(n) - if s.posInBytes == s.totalBytes() || err == io.EOF { - return n, io.EOF - } - return n, nil + return n, err } func (s *i16Stream) Seek(offset int64, whence int) (int64, error) { diff --git a/audio/vorbis/vorbis_test.go b/audio/vorbis/vorbis_test.go index 76e395e13..a557f7422 100644 --- a/audio/vorbis/vorbis_test.go +++ b/audio/vorbis/vorbis_test.go @@ -138,7 +138,13 @@ func TestNonSeeker(t *testing.T) { t.Errorf("s.SampleRate(): got: %d, want: %d", got, want) } - // TODO: Check the result of io.ReadAll (#3192). + buf, err := io.ReadAll(s) + if err != nil { + t.Errorf("io.ReadAll: %v", err) + } + if len(buf) == 0 { + t.Errorf("len(buf): got: %d, want: > 0", len(buf)) + } } func TestNonSeekerF32(t *testing.T) {