audio: Deprecate Size() and add Length() (#466)

This commit is contained in:
Hajime Hoshi 2018-01-09 01:25:38 +09:00
parent ef282f7f4f
commit 5c55df87d7
7 changed files with 44 additions and 24 deletions

View File

@ -91,7 +91,7 @@ func NewResampling(source audio.ReadSeekCloser, size int64, from, to int) *Resam
return r
}
func (r *Resampling) Size() int64 {
func (r *Resampling) Length() int64 {
s := int64(float64(r.size) * float64(r.to) / float64(r.from))
return s / 4 * 4
}
@ -204,12 +204,12 @@ func (r *Resampling) at(t int64) (float64, float64, error) {
}
func (r *Resampling) Read(b []uint8) (int, error) {
if r.pos == r.Size() {
if r.pos == r.Length() {
return 0, io.EOF
}
n := len(b) / 4 * 4
if r.Size()-r.pos <= int64(n) {
n = int(r.Size() - r.pos)
if r.Length()-r.pos <= int64(n) {
n = int(r.Length() - r.pos)
}
for i := 0; i < n/4; i++ {
l, r, err := r.at(r.pos/4 + int64(i))
@ -234,13 +234,13 @@ func (r *Resampling) Seek(offset int64, whence int) (int64, error) {
case io.SeekCurrent:
r.pos += offset
case io.SeekEnd:
r.pos += r.Size() + offset
r.pos += r.Length() + offset
}
if r.pos < 0 {
r.pos = 0
}
if r.Size() <= r.pos {
r.pos = r.Size()
if r.Length() <= r.pos {
r.pos = r.Length()
}
return r.pos, nil
}

View File

@ -83,7 +83,7 @@ func (s *Stream) Seek(offset int64, whence int) (int64, error) {
case io.SeekCurrent:
next = int64(s.posInBytes) + offset
case io.SeekEnd:
next = s.Size() + offset
next = s.Length() + offset
}
s.posInBytes = int(next)
return next, nil
@ -93,10 +93,15 @@ func (s *Stream) Close() error {
return nil
}
func (s *Stream) Size() int64 {
func (s *Stream) Length() int64 {
return int64(len(s.leftData) * 4)
}
// Size is deprecated as of 1.6.0-alpha. Use Length instead.
func (s *Stream) Size() int64 {
return s.Length()
}
// seekNextFrame seeks the next frame and returns the new buffer with the new position.
// seekNextFrame also returns true when seeking is successful, or false otherwise.
//

View File

@ -57,14 +57,19 @@ func (s *Stream) Close() error {
return s.orig.Close()
}
// Size returns the size of decoded stream in bytes.
func (s *Stream) Size() int64 {
// Length returns the size of decoded stream in bytes.
func (s *Stream) Length() int64 {
if s.resampling != nil {
return s.resampling.Size()
return s.resampling.Length()
}
return s.orig.Length()
}
// Size is deprecated as of 1.6.0-alpha. Use Length instead.
func (s *Stream) Size() int64 {
return s.Length()
}
// Decode decodes MP3 source and returns a decoded stream.
//
// Decode returns error when decoding fails or IO error happens.

View File

@ -48,11 +48,16 @@ func (s *Stream) Close() error {
return s.decoded.Close()
}
// Size returns the size of decoded stream in bytes.
func (s *Stream) Size() int64 {
// Length returns the size of decoded stream in bytes.
func (s *Stream) Length() int64 {
return s.size
}
// Size is deprecated as of version 1.6.0-alpha. Use Length instead.
func (s *Stream) Size() int64 {
return s.Length()
}
type decoded struct {
data []float32
totalBytes int
@ -141,7 +146,7 @@ func (d *decoded) Close() error {
return nil
}
func (d *decoded) Size() int64 {
func (d *decoded) Length() int64 {
return int64(d.totalBytes)
}
@ -182,7 +187,7 @@ func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
return nil, fmt.Errorf("vorbis: number of channels must be 1 or 2 but was %d", channelNum)
}
var s audio.ReadSeekCloser = decoded
size := decoded.Size()
size := decoded.Length()
if channelNum == 1 {
s = convert.NewStereo16(s, true, false)
size *= 2
@ -190,7 +195,7 @@ func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
if sampleRate != context.SampleRate() {
r := convert.NewResampling(s, size, sampleRate, context.SampleRate())
s = r
size = r.Size()
size = r.Length()
}
return &Stream{decoded: s, size: size}, nil
}

View File

@ -47,11 +47,16 @@ func (s *Stream) Close() error {
return s.inner.Close()
}
// Size returns the size of decoded stream in bytes.
func (s *Stream) Size() int64 {
// Length returns the size of decoded stream in bytes.
func (s *Stream) Length() int64 {
return s.size
}
// Size is deprecated as of version 1.6.0-alpha. Use Length instead.
func (s *Stream) Size() int64 {
return s.Length()
}
type stream struct {
src audio.ReadSeekCloser
headerSize int64
@ -103,8 +108,8 @@ func (s *stream) Close() error {
return s.src.Close()
}
// Size returns the size of decoded stream in bytes.
func (s *stream) Size() int64 {
// Length returns the size of decoded stream in bytes.
func (s *stream) Length() int64 {
return s.dataSize
}
@ -221,7 +226,7 @@ chunks:
if sampleRateFrom != sampleRateTo {
r := convert.NewResampling(s, dataSize, sampleRateFrom, sampleRateTo)
s = r
dataSize = r.Size()
dataSize = r.Length()
}
return &Stream{inner: s, size: dataSize}, nil
}

View File

@ -126,7 +126,7 @@ func NewPlayer(audioContext *audio.Context) (*Player, error) {
},
audioContext: audioContext,
audioPlayer: p,
total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate,
total: time.Second * time.Duration(s.Length()) / bytesPerSample / sampleRate,
volume128: 128,
seCh: make(chan []uint8),
}

View File

@ -57,7 +57,7 @@ func update(screen *ebiten.Image) error {
return err
}
s := audio.NewInfiniteLoop(wavS, wavS.Size())
s := audio.NewInfiniteLoop(wavS, wavS.Length())
player, err = audio.NewPlayer(audioContext, s)
if err != nil {