mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
audio: Remove Stream.Len and add Stream.Size
This commit is contained in:
parent
d2ccbdbe23
commit
4d7045c161
@ -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?
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user