diff --git a/examples/audio/main.go b/examples/audio/main.go index a698e0cdf..e63aff688 100644 --- a/examples/audio/main.go +++ b/examples/audio/main.go @@ -252,7 +252,9 @@ func main() { if err != nil { log.Fatal(err) } - audioContext = audio.NewContext(22050) + const sampleRate = 22050 + const bytesPerSample = 4 // TODO: This should be defined in audio package + audioContext = audio.NewContext(sampleRate) go func() { s, err := wav.Decode(audioContext, wavF) if err != nil { @@ -275,7 +277,7 @@ func main() { } musicCh <- &Player{ audioPlayer: p, - total: s.Len(), + total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate, } close(musicCh) // TODO: Is this goroutine-safe? diff --git a/exp/audio/vorbis/stream.go b/exp/audio/vorbis/stream.go index 983bef6b1..ff3abd3e1 100644 --- a/exp/audio/vorbis/stream.go +++ b/exp/audio/vorbis/stream.go @@ -16,12 +16,10 @@ package vorbis import ( "bytes" - "time" ) type Stream struct { - buf *bytes.Reader - sampleRate int + buf *bytes.Reader } func (s *Stream) Read(p []byte) (int, error) { @@ -37,7 +35,6 @@ func (s *Stream) Close() error { return nil } -func (s *Stream) Len() time.Duration { - const bytesPerSample = 4 - return time.Duration(s.buf.Len()/bytesPerSample) * time.Second / time.Duration(s.sampleRate) +func (s *Stream) Size() int64 { + return s.buf.Size() } diff --git a/exp/audio/vorbis/vorbis.go b/exp/audio/vorbis/vorbis.go index f41d95422..0cb651a95 100644 --- a/exp/audio/vorbis/vorbis.go +++ b/exp/audio/vorbis/vorbis.go @@ -32,6 +32,7 @@ func Decode(context *audio.Context, src io.Reader) (*Stream, error) { if err != nil { return nil, err } + // TODO: Remove this magic number if channels != 2 { return nil, errors.New("vorbis: number of channels must be 2") } @@ -45,8 +46,7 @@ func Decode(context *audio.Context, src io.Reader) (*Stream, error) { return nil, err } s := &Stream{ - buf: bytes.NewReader(b), - sampleRate: sampleRate, + buf: bytes.NewReader(b), } return s, nil } diff --git a/exp/audio/wav/decode.go b/exp/audio/wav/decode.go index 3de898ee1..70cb05f07 100644 --- a/exp/audio/wav/decode.go +++ b/exp/audio/wav/decode.go @@ -19,7 +19,6 @@ import ( "fmt" "io" "io/ioutil" - "time" "github.com/hajimehoshi/ebiten/exp/audio" ) @@ -31,8 +30,7 @@ const ( ) type Stream struct { - buf *bytes.Reader - sampleRate int + buf *bytes.Reader } func (s *Stream) Read(p []byte) (int, error) { @@ -48,9 +46,8 @@ func (s *Stream) Close() error { return nil } -func (s *Stream) Len() time.Duration { - const bytesPerSample = 4 - return time.Duration(s.buf.Len()/bytesPerSample) * time.Second / time.Duration(s.sampleRate) +func (s *Stream) Size() int64 { + return s.buf.Size() } func Decode(context *audio.Context, src io.Reader) (*Stream, error) { @@ -69,9 +66,11 @@ func Decode(context *audio.Context, src io.Reader) (*Stream, error) { return nil, fmt.Errorf("wav: invalid header: WAVE not found") } channels, depth := buf[22], buf[34] + // TODO: Remove this magic number if channels != 2 { return nil, fmt.Errorf("wav: invalid header: channel num must be 2") } + // TODO: Remove this magic number if depth != 16 { return nil, fmt.Errorf("wav: invalid header: depth must be 16") } @@ -84,8 +83,7 @@ func Decode(context *audio.Context, src io.Reader) (*Stream, error) { return nil, err } s := &Stream{ - buf: bytes.NewReader(b), - sampleRate: sampleRate, + buf: bytes.NewReader(b), } return s, nil }