audio/vorbis: Refactoring

This commit is contained in:
Hajime Hoshi 2016-03-31 22:47:16 +09:00
parent 07bc077f5b
commit 5c7d36e62e
2 changed files with 23 additions and 19 deletions

View File

@ -28,16 +28,16 @@ import (
// TODO: src should be ReadCloser? // TODO: src should be ReadCloser?
func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) { func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
decoded, channels, sampleRate, err := decode(src) decoded, err := decode(src)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// TODO: Remove this magic number // TODO: Remove this magic number
if channels != 2 { if decoded.Channels() != 2 {
return nil, errors.New("vorbis: number of channels must be 2") return nil, errors.New("vorbis: number of channels must be 2")
} }
if sampleRate != context.SampleRate() { if decoded.SampleRate() != context.SampleRate() {
return nil, fmt.Errorf("vorbis: sample rate must be %d but %d", context.SampleRate(), sampleRate) return nil, fmt.Errorf("vorbis: sample rate must be %d but %d", context.SampleRate(), decoded.SampleRate())
} }
// TODO: Read all data once so that Seek can be implemented easily. // TODO: Read all data once so that Seek can be implemented easily.
// We should look for a wiser way. // We should look for a wiser way.

View File

@ -5557,14 +5557,11 @@ type decoder struct {
const bufferSize = 4096 const bufferSize = 4096
// TODO: Move this to ebiten!
// TODO: Want Seek
func (d *decoder) Read(out []byte) (int, error) { func (d *decoder) Read(out []byte) (int, error) {
if !d.ineof { if !d.ineof {
tmp := make([]byte, bufferSize) b := make([]byte, bufferSize)
n, err := d.in.Read(tmp) n, err := d.in.Read(b)
d.inbuf = append(d.inbuf, tmp[:n]...) d.inbuf = append(d.inbuf, b[:n]...)
if err == io.EOF { if err == io.EOF {
d.ineof = true d.ineof = true
} }
@ -5622,10 +5619,17 @@ func (d *decoder) Close() error {
return nil return nil
} }
func (d *decoder) Channels() int {
return int(d.v.channels)
}
func (d *decoder) SampleRate() int {
return int(d.v.sample_rate)
}
// decode accepts an ogg stream and returns a decorded stream. // decode accepts an ogg stream and returns a decorded stream.
// This returns stream IO object, number of channels, sample rate and error if needed.
// The decorded format is 1 or 2-channel interleaved littleendian int16 values. // The decorded format is 1 or 2-channel interleaved littleendian int16 values.
func decode(in io.Reader) (io.ReadCloser, int, int, error) { func decode(in io.Reader) (*decoder, error) {
d := &decoder{ d := &decoder{
in: in, in: in,
inbuf: []byte{}, inbuf: []byte{},
@ -5633,10 +5637,10 @@ func decode(in io.Reader) (io.ReadCloser, int, int, error) {
} }
runtime.SetFinalizer(d, (*decoder).Close) runtime.SetFinalizer(d, (*decoder).Close)
for { for {
tmp := make([]byte, bufferSize) b := make([]byte, bufferSize)
n, err := in.Read(tmp) n, err := in.Read(b)
if 0 < n { if 0 < n {
d.inbuf = append(d.inbuf, tmp[:n]...) d.inbuf = append(d.inbuf, b[:n]...)
} }
if 0 < len(d.inbuf) { if 0 < len(d.inbuf) {
used := C.int(0) used := C.int(0)
@ -5649,17 +5653,17 @@ func decode(in io.Reader) (io.ReadCloser, int, int, error) {
if error == C.VORBIS_need_more_data { if error == C.VORBIS_need_more_data {
continue continue
} }
return nil, 0, 0, fmt.Errorf("go-vorbis: Error %d", error) return nil, fmt.Errorf("vorbis: decoding error %d", error)
} }
if err == io.EOF { if err == io.EOF {
break break
} }
if err != nil { if err != nil {
return nil, 0, 0, err return nil, err
} }
} }
if d.v == nil { if d.v == nil {
return nil, 0, 0, errors.New("go-vorbis: initializing failed") return nil, errors.New("vorbis: initializing failed")
} }
return d, int(d.v.channels), int(d.v.sample_rate), nil return d, nil
} }