audio: Add NewInfiniteLoopWithIntro (#652)

This commit is contained in:
Hajime Hoshi 2018-07-24 03:35:21 +09:00
parent b9265d5120
commit 59bcbca379
2 changed files with 140 additions and 20 deletions

View File

@ -19,56 +19,107 @@ import (
"io"
)
// InfiniteLoop represents a loop which never ends.
// InfiniteLoop represents a looped stream which never ends.
type InfiniteLoop struct {
src ReadSeekCloser
size int64
src ReadSeekCloser
lstart int64
llength int64
pos int64
}
// NewInfiniteLoop creates a new infinite loop stream with a source stream and size in bytes.
func NewInfiniteLoop(src ReadSeekCloser, size int64) *InfiniteLoop {
// NewInfiniteLoop creates a new infinite loop stream with a source stream and length in bytes.
func NewInfiniteLoop(src ReadSeekCloser, length int64) *InfiniteLoop {
return NewInfiniteLoopWithIntro(src, 0, length)
}
// NewInfiniteLoopWithIntro creates a new infinite loop stream with an intro part.
// NewInfiniteLoopWithIntro accepts a source stream src, introLength in bytes and loopLength in bytes.
func NewInfiniteLoopWithIntro(src ReadSeekCloser, introLength int64, loopLength int64) *InfiniteLoop {
return &InfiniteLoop{
src: src,
size: size,
src: src,
lstart: introLength,
llength: loopLength,
pos: -1,
}
}
func (i *InfiniteLoop) length() int64 {
return i.lstart + i.llength
}
func (i *InfiniteLoop) ensurePos() error {
if i.pos >= 0 {
return nil
}
pos, err := i.src.Seek(0, io.SeekCurrent)
if err != nil {
return err
}
if pos >= i.length() {
return fmt.Errorf("audio: stream position must be less than the specified length")
}
i.pos = pos
return nil
}
// Read is implementation of ReadSeekCloser's Read.
func (i *InfiniteLoop) Read(b []byte) (int, error) {
if err := i.ensurePos(); err != nil {
return 0, err
}
if i.pos+int64(len(b)) > i.length() {
b = b[:i.length()-i.pos]
}
n, err := i.src.Read(b)
if err == io.EOF {
if _, err := i.Seek(0, io.SeekStart); err != nil {
i.pos += int64(n)
if i.pos > i.length() {
panic("not reached")
}
if err != nil && err != io.EOF {
return 0, err
}
if err == io.EOF || i.pos == i.length() {
pos, err := i.Seek(i.lstart, io.SeekStart)
if err != nil {
return 0, err
}
err = nil
i.pos = pos
}
return n, err
return n, nil
}
// Seek is implementation of ReadSeekCloser's Seek.
func (i *InfiniteLoop) Seek(offset int64, whence int) (int64, error) {
if err := i.ensurePos(); err != nil {
return 0, err
}
next := int64(0)
switch whence {
case io.SeekStart:
next = offset
case io.SeekCurrent:
current, err := i.src.Seek(0, io.SeekCurrent)
if err != nil {
return 0, err
}
next = current + offset
next = i.pos + offset
case io.SeekEnd:
return 0, fmt.Errorf("audio: whence must be io.SeekStart or io.SeekCurrent for InfiniteLoop")
}
if next < 0 {
return 0, fmt.Errorf("audio: position must >= 0")
}
next %= i.size
pos, err := i.src.Seek(next, io.SeekStart)
if err != nil {
if next >= i.lstart {
next = ((next - i.lstart) % i.llength) + i.lstart
}
// Ignore the new position returned by Seek since the source position might not be match with the position
// managed by this.
if _, err := i.src.Seek(next, io.SeekStart); err != nil {
return 0, err
}
return pos, nil
i.pos = next
return i.pos, nil
}
// Close is implementation of ReadSeekCloser's Close.

View File

@ -78,3 +78,72 @@ func TestInfiniteLoop(t *testing.T) {
t.Errorf("got: %v, want: %v", err, nil)
}
}
func TestInfiniteLoopWithIntro(t *testing.T) {
const (
srcLength = 17 * 4
introLength = 19 * 4
loopLength = 23 * 4
)
indexToByte := func(index int) byte {
return byte(math.Sin(float64(index)) * 256)
}
src := make([]byte, srcLength)
for i := range src {
src[i] = indexToByte(i)
}
srcInf := NewInfiniteLoop(BytesReadSeekCloser(src), srcLength)
l := NewInfiniteLoopWithIntro(srcInf, introLength, loopLength)
buf := make([]byte, srcLength*4)
if _, err := io.ReadFull(l, buf); err != nil {
t.Error(err)
}
for i, b := range buf {
got := b
want := byte(0)
if i < introLength {
want = indexToByte(i % srcLength)
} else {
want = indexToByte(((i-introLength)%loopLength + introLength) % srcLength)
}
if got != want {
t.Errorf("index: %d, got: %v, want: %v", i, got, want)
}
}
n, err := l.Seek(srcLength*5+128, io.SeekStart)
if err != nil {
t.Error(err)
}
if want := int64((srcLength*5+128-introLength)%loopLength + introLength); n != want {
t.Errorf("got: %v, want: %v", n, want)
}
n2, err := l.Seek(srcLength*6+64, io.SeekCurrent)
if err != nil {
t.Error(err)
}
if want := int64(((srcLength*11+192)-introLength)%loopLength + introLength); n2 != want {
t.Errorf("got: %v, want: %v", n, want)
}
buf2 := make([]byte, srcLength*7)
if _, err := io.ReadFull(l, buf2); err != nil {
t.Error(err)
}
for i, b := range buf2 {
got := b
idx := ((int(n2+int64(i))-introLength)%loopLength + introLength) % srcLength
want := indexToByte(idx)
if got != want {
t.Errorf("index: %d, got: %v, want: %v", i, got, want)
}
}
// Seek to negative position is an error.
if _, err := l.Seek(-1, io.SeekStart); err == nil {
t.Errorf("got: %v, want: %v", err, nil)
}
}