Documentation
¶
Overview ¶
Package mp3 implements the MPEG-1/2/2.5 Layer III decoder core. References to minimp3.h identify lines in the pinned CC0-1.0 implementation linked from codec/mp3/UPSTREAM.md.
Package mp3 decodes MPEG-1, MPEG-2 and MPEG-2.5 Layer III streams.
Index ¶
- Constants
- Variables
- func FrameFormat(header []byte) (frameBytes, sampleRate int)
- func ID3v2Size(header []byte) (size int64, ok bool)
- func Probe(source io.ReaderAt, size, offset int64) (sampleRate int, err error)
- func ReadTags(source io.ReaderAt, size int64) (tagdata.Result, error)
- type Decoder
- type FrameInfo
- type StreamDecoder
- func (d *StreamDecoder) BufferedInput() int
- func (d *StreamDecoder) Close() error
- func (d *StreamDecoder) Decode(consume func([][]int32)) error
- func (d *StreamDecoder) Format() (channels, bitsPerSample int)
- func (d *StreamDecoder) FrameCount() (int64, bool)
- func (d *StreamDecoder) NextBlock() (audio.Buffer[float32], error)
- func (d *StreamDecoder) PCMFormat() audio.Format
Constants ¶
const (
// HeaderSize is the size of a Layer III frame header in bytes.
HeaderSize = 4
)
Constants mirroring minimp3.h (HDR_SIZE, MAX_FRAME_SYNC_MATCHES, MAX_FREE_FORMAT_FRAME_SIZE).
const ID3HeaderSize = 10
ID3HeaderSize is the length of an ID3v2 header.
const ID3Signature = "ID3"
ID3Signature identifies a leading ID3v2 tag.
const MaxSamplesPerChannel = 1152
MaxSamplesPerChannel is the largest number of samples per channel that one DecodeFrame call can return.
Variables ¶
var ( // ErrCorruptStream means a complete MP3 frame could not be decoded. ErrCorruptStream = errors.New("corrupt MP3 stream") // ErrUnsupportedLayer means the stream uses MPEG Audio Layer I or II. ErrUnsupportedLayer = errors.New("MPEG audio layer is unsupported") // ErrFrameLimit means decoding reached the compressed frame limit. ErrFrameLimit = errors.New("MP3 stream exceeds the compressed frame limit") )
Functions ¶
func FrameFormat ¶
FrameFormat reports a fixed-rate Layer III frame's byte length and sample rate. A free-format header needs subsequent frames to establish its size.
func ID3v2Size ¶
ID3v2Size reports the bytes occupied by an ID3v2 tag, including its header and any ID3v2.4 footer.
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder mirrors upstream mp3dec_t (minimp3.h:18-23): the persistent decoder state carried from one frame to the next. mdctOverlap is the hybrid-IMDCT overlap-add memory (mdct_overlap[2][9*32]), qmfState the synthesis filterbank memory (qmf_state[15*2*32]), res the bit reservoir (reserv + reserv_buf[511]), header the cached header for the fast-path (header[4]), and freeFormatBytes the sticky free-format frame size (free_format_bytes). scratch is the working set (see mp3Scratch), which upstream keeps on the stack rather than in mp3dec_t.
func NewDecoder ¶
func NewDecoder() *Decoder
NewDecoder returns a Decoder with no stream state. Its zero value is equivalent to the upstream oracle's zero-initialized mp3dec_t.
func (*Decoder) DecodeFrame ¶
func (d *Decoder) DecodeFrame(mp3 []byte, pcm []float32, info *FrameInfo, isTrailer func([]byte) bool) int
DecodeFrame mirrors upstream mp3dec_decode_frame (minimp3.h:1713-1806). It decodes one MP3 frame from mp3 into interleaved float32 PCM in pcm and fills info, returning the number of samples per channel (0 when no frame decoded). mp3 must begin at or before a frame; the return and info follow upstream semantics for audio, so a caller advances by info.FrameBytes. isTrailer reports a recognized metadata boundary where a following audio header would otherwise be required for synchronization.
The fast path reuses the cached header when it matches the next frame, avoiding a full resync; a miss zeroes all persistent state (reset) and resyncs via findFrame. Only Layer III is decoded (the library's scope); a non-Layer-III frame is recognized and sized but produces no samples.
func (*Decoder) NextFrameSize ¶
NextFrameSize reports the size of a complete frame at the expected offset if it matches the header from the previous decode.
func (*Decoder) Reset ¶
func (d *Decoder) Reset()
Reset clears all persistent decoder state (overlap memory, filterbank state, bit reservoir, and cached header), so the next DecodeFrame call resyncs from scratch. The stream adapter calls Reset after a metadata frame so that metadata cannot contribute to the audio state.
type FrameInfo ¶
type FrameInfo struct {
FrameBytes, FrameOffset, Channels, SampleRateHz, Layer, BitrateKbps int
}
FrameInfo mirrors upstream mp3dec_frame_info_t (minimp3.h:13-16). DecodeFrame fills it with the same semantics as mp3dec_decode_frame: FrameBytes is the offset to the frame plus the frame size (i + frame_size upstream, so it already includes FrameOffset), FrameOffset is the number of bytes skipped before the frame's header, and the rest describe the decoded frame's format.
type StreamDecoder ¶
type StreamDecoder struct {
// contains filtered or unexported fields
}
StreamDecoder reads one pass of normalized, signed 32-bit PCM. Use one goroutine per decoder. The decoder borrows the source and its caller owns closing it.
func Open ¶
Open reads the first playable block to determine the channel count. It reads from the source's current position and keeps at most one compressed search window buffered.
func (*StreamDecoder) BufferedInput ¶
func (d *StreamDecoder) BufferedInput() int
BufferedInput reports compressed bytes read ahead of the last decoded frame.
func (*StreamDecoder) Close ¶
func (d *StreamDecoder) Close() error
Close releases the decoder and leaves the input open.
func (*StreamDecoder) Decode ¶
func (d *StreamDecoder) Decode(consume func([][]int32)) error
Decode passes each signed 32-bit block to consume synchronously. The channel slices are read-only and valid for the duration of the call.
func (*StreamDecoder) Format ¶
func (d *StreamDecoder) Format() (channels, bitsPerSample int)
Format reports the number of channels and the output PCM depth.
func (*StreamDecoder) FrameCount ¶
func (d *StreamDecoder) FrameCount() (int64, bool)
FrameCount reports the sample count when the Xing or VBRI header supplies one, with known encoder delay and padding removed.
func (*StreamDecoder) NextBlock ¶
func (d *StreamDecoder) NextBlock() (audio.Buffer[float32], error)
NextBlock returns a borrowed planar block at native float32 precision.
func (*StreamDecoder) PCMFormat ¶
func (d *StreamDecoder) PCMFormat() audio.Format
PCMFormat reports the native floating-point sample rate and layout.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package bits provides an MSB-first bit reader matching the bit-position and overrun semantics of minimp3's bs_t/get_bits (see minimp3.h, CC0-1.0), which every decode unit in this package reads its bitstream through.
|
Package bits provides an MSB-first bit reader matching the bit-position and overrun semantics of minimp3's bs_t/get_bits (see minimp3.h, CC0-1.0), which every decode unit in this package reads its bitstream through. |