audio/wav: Allow passing non-io.Seeker to Decode

Updates #1202
This commit is contained in:
Hajime Hoshi 2020-08-23 19:15:14 +09:00
parent 2a6241234d
commit 19cd69da55

View File

@ -39,6 +39,8 @@ func (s *Stream) Read(p []byte) (int, error) {
// Seek is implementation of io.Seeker's Seek.
//
// Note that Seek can take long since decoding is a relatively heavy task.
//
// If the underlying source is not io.Seeker, Seek panics.
func (s *Stream) Seek(offset int64, whence int) (int64, error) {
return s.inner.Seek(offset, whence)
}
@ -62,7 +64,7 @@ func (s *Stream) Size() int64 {
}
type stream struct {
src audio.ReadSeekCloser
src io.ReadCloser
headerSize int64
dataSize int64
remaining int64
@ -83,6 +85,11 @@ func (s *stream) Read(p []byte) (int, error) {
// Seek is implementation of io.Seeker's Seek.
func (s *stream) Seek(offset int64, whence int) (int64, error) {
seeker, ok := s.src.(io.Seeker)
if !ok {
panic("wav: the underlying source is not io.Seeker")
}
switch whence {
case io.SeekStart:
offset = offset + s.headerSize
@ -91,7 +98,7 @@ func (s *stream) Seek(offset int64, whence int) (int64, error) {
offset = s.headerSize + s.dataSize + offset
whence = io.SeekStart
}
n, err := s.src.Seek(offset, whence)
n, err := seeker.Seek(offset, whence)
if err != nil {
return 0, err
}
@ -128,7 +135,7 @@ func (s *stream) Length() int64 {
// Decode automatically resamples the stream to fit with the audio context if necessary.
//
// Decode takes the ownership of src, and Stream's Close function closes src.
func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
func Decode(context *audio.Context, src io.ReadCloser) (*Stream, error) {
buf := make([]byte, 12)
n, err := io.ReadFull(src, buf)
if n != len(buf) {