mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-27 04:08:53 +01:00
mp3: Avoid calling Length() if possible (#463)
This commit is contained in:
parent
c636dec721
commit
31350dc497
@ -29,28 +29,40 @@ import (
|
|||||||
|
|
||||||
// Stream is a decoded stream.
|
// Stream is a decoded stream.
|
||||||
type Stream struct {
|
type Stream struct {
|
||||||
inner audio.ReadSeekCloser
|
orig *mp3.Decoder
|
||||||
size int64
|
resampling *convert.Resampling
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read is implementation of io.Reader's Read.
|
// Read is implementation of io.Reader's Read.
|
||||||
func (s *Stream) Read(buf []byte) (int, error) {
|
func (s *Stream) Read(buf []byte) (int, error) {
|
||||||
return s.inner.Read(buf)
|
if s.resampling != nil {
|
||||||
|
return s.resampling.Read(buf)
|
||||||
|
}
|
||||||
|
return s.orig.Read(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Seek is implementation of io.Seeker's Seek.
|
// Seek is implementation of io.Seeker's Seek.
|
||||||
func (s *Stream) Seek(offset int64, whence int) (int64, error) {
|
func (s *Stream) Seek(offset int64, whence int) (int64, error) {
|
||||||
return s.inner.Seek(offset, whence)
|
if s.resampling != nil {
|
||||||
|
return s.resampling.Seek(offset, whence)
|
||||||
|
}
|
||||||
|
return s.orig.Seek(offset, whence)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close is implementation of io.Closer's Close.
|
// Close is implementation of io.Closer's Close.
|
||||||
func (s *Stream) Close() error {
|
func (s *Stream) Close() error {
|
||||||
return s.inner.Close()
|
if s.resampling != nil {
|
||||||
|
return s.resampling.Close()
|
||||||
|
}
|
||||||
|
return s.orig.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size returns the size of decoded stream in bytes.
|
// Size returns the size of decoded stream in bytes.
|
||||||
func (s *Stream) Size() int64 {
|
func (s *Stream) Size() int64 {
|
||||||
return s.size
|
if s.resampling != nil {
|
||||||
|
return s.resampling.Size()
|
||||||
|
}
|
||||||
|
return s.orig.Length()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode decodes MP3 source and returns a decoded stream.
|
// Decode decodes MP3 source and returns a decoded stream.
|
||||||
@ -63,16 +75,12 @@ func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// TODO: Resampling
|
var r *convert.Resampling
|
||||||
var s audio.ReadSeekCloser = d
|
|
||||||
size := d.Length()
|
|
||||||
if d.SampleRate() != context.SampleRate() {
|
if d.SampleRate() != context.SampleRate() {
|
||||||
r := convert.NewResampling(s, d.Length(), d.SampleRate(), context.SampleRate())
|
r = convert.NewResampling(d, d.Length(), d.SampleRate(), context.SampleRate())
|
||||||
s = r
|
|
||||||
size = r.Size()
|
|
||||||
}
|
}
|
||||||
return &Stream{
|
return &Stream{
|
||||||
inner: s,
|
orig: d,
|
||||||
size: size,
|
resampling: r,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user