From 51da7108605b61eb99eb29ab4a451dc6d34ab307 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 30 Nov 2016 12:00:53 +0900 Subject: [PATCH] audio/vorbis: Read some data before actual using (#297) --- audio/vorbis/vorbis.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/audio/vorbis/vorbis.go b/audio/vorbis/vorbis.go index 80f2f3961..5300b4fe7 100644 --- a/audio/vorbis/vorbis.go +++ b/audio/vorbis/vorbis.go @@ -33,8 +33,8 @@ type decoded struct { func (d *decoded) readUntil(posInBytes int) error { c := 0 + buffer := make([]float32, 8192) for len(d.data) < posInBytes/2 { - buffer := make([]float32, 8192) n, err := d.decoder.Read(buffer) if n > 0 { d.data = append(d.data, buffer[:n]...) @@ -56,7 +56,7 @@ func (d *decoded) readUntil(posInBytes int) error { return nil } -func (d *decoded) Read(b []byte) (int, error) { +func (d *decoded) Read(b []uint8) (int, error) { total := d.totalBytes l := total - d.posInBytes if l > len(b) { @@ -119,12 +119,18 @@ func decode(in audio.ReadSeekCloser) (*decoded, int, int, error) { return nil, 0, 0, err } d := &decoded{ - data: []float32{}, + data: make([]float32, 0, r.Length()), totalBytes: int(r.Length()) * 4, // TODO: What if length is 0? posInBytes: 0, source: in, decoder: r, } runtime.SetFinalizer(d, (*decoded).Close) + if _, err := d.Read(make([]uint8, 65536)); err != nil { + return nil, 0, 0, err + } + if _, err := d.Seek(0, io.SeekStart); err != nil { + return nil, 0, 0, err + } return d, r.Channels(), r.SampleRate(), nil }