audio/internal/convert: bug fix: Read never returned io.EOF when seeked

This commit is contained in:
Hajime Hoshi 2024-07-13 22:34:13 +09:00
parent c03ece2671
commit 91cdaa9f11
2 changed files with 46 additions and 25 deletions

View File

@ -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) {

View File

@ -66,6 +66,13 @@ func TestFloat32(t *testing.T) {
for _, c := range cases {
c := c
t.Run(c.Name, func(t *testing.T) {
for _, seek := range []bool{false, true} {
seek := seek
name := "nonseek"
if seek {
name = "seek"
}
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 {
@ -76,7 +83,7 @@ func TestFloat32(t *testing.T) {
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))
r := convert.NewFloat32BytesReaderFromInt16BytesReader(bytes.NewReader(in)).(io.ReadSeeker)
var got []byte
for {
var buf [97]byte
@ -88,6 +95,14 @@ func TestFloat32(t *testing.T) {
}
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) {
@ -95,4 +110,6 @@ func TestFloat32(t *testing.T) {
}
})
}
})
}
}