diff --git a/audio/mp3/decode_notjs.go b/audio/mp3/decode_notjs.go index 585287b0a..b0633663c 100644 --- a/audio/mp3/decode_notjs.go +++ b/audio/mp3/decode_notjs.go @@ -98,24 +98,33 @@ func (s *source) getFilepos() int { } type Decoder struct { - source *source - length int64 - buf []uint8 - frame *frame - eof bool + source *source + sampleRate int + length int64 + buf []uint8 + frame *frame + eof bool } +func (d *Decoder) read() error { + var err error + d.frame, err = d.source.readNextFrame(d.frame) + if err != nil { + if err == io.EOF { + d.eof = true + } + return err + } + d.buf = append(d.buf, d.frame.decodeL3()...) + return nil +} + +// Read is io.Reader's Read. func (d *Decoder) Read(buf []uint8) (int, error) { for len(d.buf) == 0 && !d.eof { - var err error - d.frame, err = d.source.readNextFrame(d.frame) - if err != nil { - if err == io.EOF { - d.eof = true - } + if err := d.read(); err != nil { return 0, err } - d.buf = append(d.buf, d.frame.decodeL3()...) } if d.eof { return 0, io.EOF @@ -125,6 +134,13 @@ func (d *Decoder) Read(buf []uint8) (int, error) { return n, nil } +// SampleRate returns the sample rate like 44100. +// +// Note that the sample rate is retrieved from the first frame. +func (d *Decoder) SampleRate() int { + return d.sampleRate +} + // Length returns the total size in bytes. // // Length returns -1 when the total size is not available @@ -160,5 +176,9 @@ func decode(r io.Reader) (*Decoder, error) { } d.length = l } + if err := d.read(); err != nil { + return nil, err + } + d.sampleRate = samplingFrequency[d.frame.header.sampling_frequency] return d, nil } diff --git a/audio/mp3/types.go b/audio/mp3/types.go index 75962257b..0171d98b0 100644 --- a/audio/mp3/types.go +++ b/audio/mp3/types.go @@ -110,7 +110,7 @@ var mpeg1Bitrates = map[mpeg1Layer][15]int{ }, } -var samplingFrequency = [3]int{44100, 48000, 32000} +var samplingFrequency = []int{44100, 48000, 32000} func (h *mpeg1FrameHeader) frameSize() int { return (144*mpeg1Bitrates[h.layer][h.bitrate_index])/