audio: Remove Stream.Len and add Stream.Size

This commit is contained in:
Hajime Hoshi 2016-03-29 02:55:30 +09:00
parent d2ccbdbe23
commit 4d7045c161
4 changed files with 15 additions and 18 deletions

View File

@ -252,7 +252,9 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) 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() { go func() {
s, err := wav.Decode(audioContext, wavF) s, err := wav.Decode(audioContext, wavF)
if err != nil { if err != nil {
@ -275,7 +277,7 @@ func main() {
} }
musicCh <- &Player{ musicCh <- &Player{
audioPlayer: p, audioPlayer: p,
total: s.Len(), total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate,
} }
close(musicCh) close(musicCh)
// TODO: Is this goroutine-safe? // TODO: Is this goroutine-safe?

View File

@ -16,12 +16,10 @@ package vorbis
import ( import (
"bytes" "bytes"
"time"
) )
type Stream struct { type Stream struct {
buf *bytes.Reader buf *bytes.Reader
sampleRate int
} }
func (s *Stream) Read(p []byte) (int, error) { func (s *Stream) Read(p []byte) (int, error) {
@ -37,7 +35,6 @@ func (s *Stream) Close() error {
return nil return nil
} }
func (s *Stream) Len() time.Duration { func (s *Stream) Size() int64 {
const bytesPerSample = 4 return s.buf.Size()
return time.Duration(s.buf.Len()/bytesPerSample) * time.Second / time.Duration(s.sampleRate)
} }

View File

@ -32,6 +32,7 @@ func Decode(context *audio.Context, src io.Reader) (*Stream, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
// TODO: Remove this magic number
if channels != 2 { if channels != 2 {
return nil, errors.New("vorbis: number of channels must be 2") return nil, errors.New("vorbis: number of channels must be 2")
} }
@ -46,7 +47,6 @@ func Decode(context *audio.Context, src io.Reader) (*Stream, error) {
} }
s := &Stream{ s := &Stream{
buf: bytes.NewReader(b), buf: bytes.NewReader(b),
sampleRate: sampleRate,
} }
return s, nil return s, nil
} }

View File

@ -19,7 +19,6 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"time"
"github.com/hajimehoshi/ebiten/exp/audio" "github.com/hajimehoshi/ebiten/exp/audio"
) )
@ -32,7 +31,6 @@ const (
type Stream struct { type Stream struct {
buf *bytes.Reader buf *bytes.Reader
sampleRate int
} }
func (s *Stream) Read(p []byte) (int, error) { func (s *Stream) Read(p []byte) (int, error) {
@ -48,9 +46,8 @@ func (s *Stream) Close() error {
return nil return nil
} }
func (s *Stream) Len() time.Duration { func (s *Stream) Size() int64 {
const bytesPerSample = 4 return s.buf.Size()
return time.Duration(s.buf.Len()/bytesPerSample) * time.Second / time.Duration(s.sampleRate)
} }
func Decode(context *audio.Context, src io.Reader) (*Stream, error) { 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") return nil, fmt.Errorf("wav: invalid header: WAVE not found")
} }
channels, depth := buf[22], buf[34] channels, depth := buf[22], buf[34]
// TODO: Remove this magic number
if channels != 2 { if channels != 2 {
return nil, fmt.Errorf("wav: invalid header: channel num must be 2") return nil, fmt.Errorf("wav: invalid header: channel num must be 2")
} }
// TODO: Remove this magic number
if depth != 16 { if depth != 16 {
return nil, fmt.Errorf("wav: invalid header: depth must be 16") return nil, fmt.Errorf("wav: invalid header: depth must be 16")
} }
@ -85,7 +84,6 @@ func Decode(context *audio.Context, src io.Reader) (*Stream, error) {
} }
s := &Stream{ s := &Stream{
buf: bytes.NewReader(b), buf: bytes.NewReader(b),
sampleRate: sampleRate,
} }
return s, nil return s, nil
} }