diff --git a/audio/audio.go b/audio/audio.go index 0b1e3df95..84a82987c 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -70,8 +70,7 @@ func (p *players) Read(b []byte) (int, error) { closed := []*Player{} l := len(b) for p := range p.players { - err := p.readToBuffer(l) - if err == io.EOF { + if err := p.readToBuffer(l); err == io.EOF { closed = append(closed, p) } else if err != nil { return 0, err diff --git a/audio/wav/decode.go b/audio/wav/decode.go index a16704ef4..33a7b5c70 100644 --- a/audio/wav/decode.go +++ b/audio/wav/decode.go @@ -28,19 +28,30 @@ type Stream struct { src audio.ReadSeekCloser headerSize int64 dataSize int64 + remaining int64 } // Read is implementation of io.Reader's Read. func (s *Stream) Read(p []byte) (int, error) { - return s.src.Read(p) + if s.remaining <= 0 { + return 0, io.EOF + } + if s.remaining < int64(len(p)) { + p = p[0:s.remaining] + } + n, err := s.src.Read(p) + s.remaining -= int64(n) + return n, err } // Seek is implementation of io.Seeker's Seek. func (s *Stream) Seek(offset int64, whence int) (int64, error) { - if whence == 0 { + if whence == io.SeekStart { offset += s.headerSize } - return s.src.Seek(offset, whence) + n, err := s.src.Seek(offset, whence) + s.remaining = s.dataSize - (n - s.headerSize) + return n, err } // Read is implementation of io.Closer's Close. @@ -139,6 +150,7 @@ chunks: src: src, headerSize: headerSize, dataSize: dataSize, + remaining: dataSize, } return s, nil }