diff --git a/audio/internal/convert/float32.go b/audio/internal/convert/float32.go index bf75b079d..97e56632d 100644 --- a/audio/internal/convert/float32.go +++ b/audio/internal/convert/float32.go @@ -68,7 +68,11 @@ func (r *float32BytesReader) Read(buf []byte) (int, error) { copy(r.i16Buf, r.i16Buf[samplesToFill*2:]) r.i16Buf = r.i16Buf[:len(r.i16Buf)-samplesToFill*2] - return samplesToFill * 4, nil + n := samplesToFill * 4 + if r.eof { + return n, io.EOF + } + return n, nil } func (r *float32BytesReader) Seek(offset int64, whence int) (int64, error) { diff --git a/audio/internal/convert/float32_test.go b/audio/internal/convert/float32_test.go index 0eb6441b3..e6325f17e 100644 --- a/audio/internal/convert/float32_test.go +++ b/audio/internal/convert/float32_test.go @@ -66,32 +66,49 @@ func TestFloat32(t *testing.T) { for _, c := range cases { c := c t.Run(c.Name, func(t *testing.T) { - // Note that unsafe.SliceData is available as of Go 1.20. - var in, out []byte - if len(c.In) > 0 { - outF32 := make([]float32, len(c.In)) - for i := range c.In { - outF32[i] = float32(c.In[i]) / (1 << 15) + for _, seek := range []bool{false, true} { + seek := seek + name := "nonseek" + if seek { + name = "seek" } - in = unsafe.Slice((*byte)(unsafe.Pointer(&c.In[0])), len(c.In)*2) - out = unsafe.Slice((*byte)(unsafe.Pointer(&outF32[0])), len(outF32)*4) - } - r := convert.NewFloat32BytesReaderFromInt16BytesReader(bytes.NewReader(in)) - var got []byte - for { - var buf [97]byte - n, err := r.Read(buf[:]) - got = append(got, buf[:n]...) - if err != nil { - if err != io.EOF { - t.Fatal(err) + t.Run(name, func(t *testing.T) { + // Note that unsafe.SliceData is available as of Go 1.20. + var in, out []byte + if len(c.In) > 0 { + outF32 := make([]float32, len(c.In)) + for i := range c.In { + outF32[i] = float32(c.In[i]) / (1 << 15) + } + in = unsafe.Slice((*byte)(unsafe.Pointer(&c.In[0])), len(c.In)*2) + out = unsafe.Slice((*byte)(unsafe.Pointer(&outF32[0])), len(outF32)*4) } - break - } - } - want := out - if !bytes.Equal(got, want) { - t.Errorf("got: %v, want: %v", got, want) + r := convert.NewFloat32BytesReaderFromInt16BytesReader(bytes.NewReader(in)).(io.ReadSeeker) + var got []byte + for { + var buf [97]byte + n, err := r.Read(buf[:]) + got = append(got, buf[:n]...) + if err != nil { + if err != io.EOF { + t.Fatal(err) + } + break + } + if seek { + if _, err := r.Seek(0, io.SeekCurrent); err != nil { + if err != io.EOF { + t.Fatal(err) + } + break + } + } + } + want := out + if !bytes.Equal(got, want) { + t.Errorf("got: %v, want: %v", got, want) + } + }) } }) }