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 return r
} }
func (r *Resampling) Size() int64 { func (r *Resampling) Length() int64 {
s := int64(float64(r.size) * float64(r.to) / float64(r.from)) s := int64(float64(r.size) * float64(r.to) / float64(r.from))
return s / 4 * 4 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) { func (r *Resampling) Read(b []uint8) (int, error) {
if r.pos == r.Size() { if r.pos == r.Length() {
return 0, io.EOF return 0, io.EOF
} }
n := len(b) / 4 * 4 n := len(b) / 4 * 4
if r.Size()-r.pos <= int64(n) { if r.Length()-r.pos <= int64(n) {
n = int(r.Size() - r.pos) n = int(r.Length() - r.pos)
} }
for i := 0; i < n/4; i++ { for i := 0; i < n/4; i++ {
l, r, err := r.at(r.pos/4 + int64(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: case io.SeekCurrent:
r.pos += offset r.pos += offset
case io.SeekEnd: case io.SeekEnd:
r.pos += r.Size() + offset r.pos += r.Length() + offset
} }
if r.pos < 0 { if r.pos < 0 {
r.pos = 0 r.pos = 0
} }
if r.Size() <= r.pos { if r.Length() <= r.pos {
r.pos = r.Size() r.pos = r.Length()
} }
return r.pos, nil return r.pos, nil
} }

View File

@ -83,7 +83,7 @@ func (s *Stream) Seek(offset int64, whence int) (int64, error) {
case io.SeekCurrent: case io.SeekCurrent:
next = int64(s.posInBytes) + offset next = int64(s.posInBytes) + offset
case io.SeekEnd: case io.SeekEnd:
next = s.Size() + offset next = s.Length() + offset
} }
s.posInBytes = int(next) s.posInBytes = int(next)
return next, nil return next, nil
@ -93,10 +93,15 @@ func (s *Stream) Close() error {
return nil return nil
} }
func (s *Stream) Size() int64 { func (s *Stream) Length() int64 {
return int64(len(s.leftData) * 4) 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 seeks the next frame and returns the new buffer with the new position.
// seekNextFrame also returns true when seeking is successful, or false otherwise. // 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() return s.orig.Close()
} }
// Size returns the size of decoded stream in bytes. // Length returns the size of decoded stream in bytes.
func (s *Stream) Size() int64 { func (s *Stream) Length() int64 {
if s.resampling != nil { if s.resampling != nil {
return s.resampling.Size() return s.resampling.Length()
} }
return s.orig.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 decodes MP3 source and returns a decoded stream.
// //
// Decode returns error when decoding fails or IO error happens. // 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() return s.decoded.Close()
} }
// Size returns the size of decoded stream in bytes. // Length returns the size of decoded stream in bytes.
func (s *Stream) Size() int64 { func (s *Stream) Length() int64 {
return s.size 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 { type decoded struct {
data []float32 data []float32
totalBytes int totalBytes int
@ -141,7 +146,7 @@ func (d *decoded) Close() error {
return nil return nil
} }
func (d *decoded) Size() int64 { func (d *decoded) Length() int64 {
return int64(d.totalBytes) 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) return nil, fmt.Errorf("vorbis: number of channels must be 1 or 2 but was %d", channelNum)
} }
var s audio.ReadSeekCloser = decoded var s audio.ReadSeekCloser = decoded
size := decoded.Size() size := decoded.Length()
if channelNum == 1 { if channelNum == 1 {
s = convert.NewStereo16(s, true, false) s = convert.NewStereo16(s, true, false)
size *= 2 size *= 2
@ -190,7 +195,7 @@ func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
if sampleRate != context.SampleRate() { if sampleRate != context.SampleRate() {
r := convert.NewResampling(s, size, sampleRate, context.SampleRate()) r := convert.NewResampling(s, size, sampleRate, context.SampleRate())
s = r s = r
size = r.Size() size = r.Length()
} }
return &Stream{decoded: s, size: size}, nil return &Stream{decoded: s, size: size}, nil
} }

View File

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

View File

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

View File

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