audio: Bug fix: NewInfiniteLoop(WithIntro) should work with incomplete values

Closes #1503
This commit is contained in:
Hajime Hoshi 2021-02-22 02:14:55 +09:00
parent a13930650d
commit b8cdcdb847
2 changed files with 24 additions and 2 deletions

View File

@ -37,8 +37,8 @@ func NewInfiniteLoop(src io.ReadSeeker, length int64) *InfiniteLoop {
func NewInfiniteLoopWithIntro(src io.ReadSeeker, introLength int64, loopLength int64) *InfiniteLoop {
return &InfiniteLoop{
src: src,
lstart: introLength,
llength: loopLength,
lstart: introLength / bytesPerSample * bytesPerSample,
llength: loopLength / bytesPerSample * bytesPerSample,
pos: -1,
}
}

View File

@ -148,3 +148,25 @@ func TestInfiniteLoopWithIntro(t *testing.T) {
t.Errorf("got: %v, want: %v", err, nil)
}
}
func TestInfiniteLoopWithIncompleteSize(t *testing.T) {
// s1 should work as if 4092 is given.
s1 := NewInfiniteLoop(bytes.NewReader(make([]byte, 4096)), 4095)
n1, err := s1.Seek(4093, io.SeekStart)
if err != nil {
t.Error(err)
}
if got, want := n1, int64(4093-4092); got != want {
t.Errorf("got: %d, want: %d", got, want)
}
// s2 should work as if 2044 and 2044 are given.
s2 := NewInfiniteLoopWithIntro(bytes.NewReader(make([]byte, 4096)), 2047, 2046)
n2, err := s2.Seek(4093, io.SeekStart)
if err != nil {
t.Error(err)
}
if got, want := n2, int64(2044+(4093-(2044+2044))); got != want {
t.Errorf("got: %d, want: %d", got, want)
}
}