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 {
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?

View File

@ -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()
}

View File

@ -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
}

View File

@ -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
}