Documentation
¶
Index ¶
- Variables
- type Decoder
- func (d *Decoder) BytesPerFrame() int64
- func (d *Decoder) Duration() time.Duration
- func (d *Decoder) GaplessInfo() *lameinfo.Info
- func (d *Decoder) Length() int64
- func (d *Decoder) Position() time.Duration
- func (d *Decoder) Progress() float64
- func (d *Decoder) RawLength() int64
- func (d *Decoder) Read(buf []byte) (int, error)
- func (d *Decoder) Remaining() time.Duration
- func (d *Decoder) SampleCount() int64
- func (d *Decoder) SamplePosition() int64
- func (d *Decoder) SampleRate() int
- func (d *Decoder) Seek(offset int64, whence int) (int64, error)
- func (d *Decoder) SeekToSample(sample int64) error
- func (d *Decoder) SeekToTime(t time.Duration) error
- func (d *Decoder) Skip(delta time.Duration) error
- type DecoderOptions
Constants ¶
This section is empty.
Variables ¶
var ErrNotSeekable = errors.New("mp3: source is not seekable")
ErrNotSeekable is returned by the seeking methods when the source is a plain io.Reader. Length and Duration can still be available in that case, when the file carries a Xing/Info header.
Functions ¶
This section is empty.
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder is a MP3-decoded stream.
Decoder decodes its underlying source on the fly.
A Decoder is not safe for concurrent use. If multiple goroutines need to access the same Decoder (e.g., one for playback and one for seeking), the caller must synchronize access with a mutex or similar mechanism.
func NewDecoder ¶
NewDecoder decodes the given io.Reader and returns a decoded stream.
The stream is always formatted as 16bit (little endian) 2 channels even if the source is single channel MP3. Thus, a sample always consists of 4 bytes.
By default, gapless playback is enabled. Use NewDecoderWithOptions to configure decoder behavior.
func NewDecoderWithOptions ¶ added in v1.2.0
func NewDecoderWithOptions(r io.Reader, opts DecoderOptions) (*Decoder, error)
NewDecoderWithOptions decodes the given io.Reader with the specified options.
The stream is always formatted as 16bit (little endian) 2 channels even if the source is single channel MP3. Thus, a sample always consists of 4 bytes.
func (*Decoder) BytesPerFrame ¶
BytesPerFrame returns the number of decoded bytes per MP3 frame. This is useful for calculating frame timing or positions.
func (*Decoder) Duration ¶
Duration returns the total duration of the audio stream. Returns -1 if the duration cannot be determined.
func (*Decoder) GaplessInfo ¶ added in v1.2.0
GaplessInfo returns the LAME/Xing gapless metadata if available. Returns nil if no gapless metadata was found or gapless mode is disabled.
func (*Decoder) Length ¶
Length returns the total size in bytes.
It is free when the file carries a Xing/Info header. Without one, the first call walks the file's frame headers to count them.
Length returns -1 when the total size cannot be determined, i.e. when the source is neither seekable nor carrying a Xing/Info header.
func (*Decoder) Progress ¶
Progress returns the playback progress as a value between 0.0 and 1.0. Returns -1 if progress cannot be determined.
func (*Decoder) RawLength ¶ added in v1.2.0
RawLength returns the total decoded bytes without gapless trimming. Returns -1 if the length cannot be determined. If gapless mode is not active, this returns the same value as Length().
func (*Decoder) Remaining ¶
Remaining returns the remaining duration from the current position. Returns -1 if duration cannot be determined.
func (*Decoder) SampleCount ¶
SampleCount returns the total number of samples (per channel). Returns -1 if the count cannot be determined.
func (*Decoder) SamplePosition ¶
SamplePosition returns the current position in samples (per channel). Each sample is 4 bytes (stereo 16-bit).
func (*Decoder) SampleRate ¶
SampleRate returns the sample rate like 44100.
Note that the sample rate is retrieved from the first frame.
func (*Decoder) Seek ¶
Seek is io.Seeker's Seek.
Seek returns ErrNotSeekable when the underlying source is not io.Seeker. The first seek on a file without a Xing header walks its frame headers to build an index; later seeks reuse it.
Note that seek uses a byte offset but samples are aligned to 4 bytes (2 channels, 2 bytes each). Be careful to seek to an offset that is divisible by 4 if you want to read at full sample boundaries.
func (*Decoder) SeekToSample ¶
SeekToSample seeks to the specified sample position. Returns an error if seeking is not supported. Negative positions are clamped to 0, positions beyond the end are clamped.
func (*Decoder) SeekToTime ¶
SeekToTime seeks to the specified absolute time position. Returns an error if seeking is not supported. Negative times are clamped to 0, times beyond duration are clamped to the end.
type DecoderOptions ¶ added in v1.2.0
type DecoderOptions struct {
// Gapless enables gapless playback by trimming encoder delay and padding.
// When true (default), the decoder reads LAME/Xing metadata and adjusts
// the audio stream to remove encoder-added silence.
Gapless bool
}
DecoderOptions configures the MP3 decoder behavior.
func DefaultDecoderOptions ¶ added in v1.2.0
func DefaultDecoderOptions() DecoderOptions
DefaultDecoderOptions returns options with gapless enabled.