diff --git a/exp/audio/vorbis/stream.go b/exp/audio/vorbis/stream.go new file mode 100644 index 000000000..483818eb1 --- /dev/null +++ b/exp/audio/vorbis/stream.go @@ -0,0 +1,44 @@ +// Copyright 2016 Hajime Hoshi +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package vorbis + +import ( + "bytes" + "io" + "time" +) + +type Stream interface { + io.ReadSeeker + Len() time.Duration +} + +type stream struct { + buf *bytes.Reader + sampleRate int +} + +func (s *stream) Read(p []byte) (int, error) { + return s.buf.Read(p) +} + +func (s *stream) Seek(offset int64, whence int) (int64, error) { + return s.buf.Seek(offset, whence) +} + +func (s *stream) Len() time.Duration { + const bytesPerSample = 4 + return time.Duration(s.buf.Len() / bytesPerSample / s.sampleRate) +} diff --git a/exp/audio/vorbis/vorbis.go b/exp/audio/vorbis/vorbis.go index 67f65a703..b83ef77cf 100644 --- a/exp/audio/vorbis/vorbis.go +++ b/exp/audio/vorbis/vorbis.go @@ -27,11 +27,7 @@ import ( "github.com/hajimehoshi/go-vorbis" ) -type stream struct { - buf *bytes.Reader -} - -func Decode(context *audio.Context, src io.Reader) (io.ReadSeeker, error) { +func Decode(context *audio.Context, src io.Reader) (Stream, error) { decoded, channels, sampleRate, err := vorbis.Decode(src) if err != nil { return nil, err @@ -49,15 +45,8 @@ func Decode(context *audio.Context, src io.Reader) (io.ReadSeeker, error) { return nil, err } s := &stream{ - buf: bytes.NewReader(b), + buf: bytes.NewReader(b), + sampleRate: sampleRate, } return s, nil } - -func (s *stream) Read(p []byte) (int, error) { - return s.buf.Read(p) -} - -func (s *stream) Seek(offset int64, whence int) (int64, error) { - return s.buf.Seek(offset, whence) -} diff --git a/exp/audio/vorbis/vorbis_js.go b/exp/audio/vorbis/vorbis_js.go index bdf0052ba..d76bc3596 100644 --- a/exp/audio/vorbis/vorbis_js.go +++ b/exp/audio/vorbis/vorbis_js.go @@ -25,14 +25,10 @@ import ( "github.com/hajimehoshi/ebiten/exp/audio" ) -type stream struct { - buf *bytes.Reader -} - // TODO: This just uses decodeAudioData can treat audio files other than Ogg/Vorbis. // TODO: This doesn't work on iOS which doesn't have Ogg/Vorbis decoder. -func Decode(context *audio.Context, src io.Reader) (io.ReadSeeker, error) { +func Decode(context *audio.Context, src io.Reader) (Stream, error) { b, err := ioutil.ReadAll(src) if err != nil { return nil, err @@ -60,11 +56,3 @@ func Decode(context *audio.Context, src io.Reader) (io.ReadSeeker, error) { <-ch return s, nil } - -func (s *stream) Read(p []byte) (int, error) { - return s.buf.Read(p) -} - -func (s *stream) Seek(offset int64, whence int) (int64, error) { - return s.buf.Seek(offset, whence) -}