package module
Version: v0.0.0-...-e79c5a4 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2017 License: MIT Imports: 7 Imported by: 17



Stream orientated mp3 frame decoder




Package mp3 provides decoding of mp3 files into their underlying frames. It is primarily intended for streaming tasks with minimal internal buffering and no requirement to seek.

The implementation started as a reworking of, and has also drawn from Konrad Windszus' excellent article on mp3 frame parsing

TODO CRC isn't currently checked.




This section is empty.


View Source
var (

	// ErrInvalidSampleRate indicates that no samplerate could be found for the frame header provided
	ErrInvalidSampleRate = FrameSampleRate(-1)

	// ErrNoSyncBits implies we could not find a valid frame header sync bit before EOF
	ErrNoSyncBits = errors.New("EOF before sync bits found")

	// ErrPrematureEOF indicates that the filed ended before a complete frame could be read
	ErrPrematureEOF = errors.New("EOF mid stream")


func MakeSilence

func MakeSilence() io.ReadCloser

MakeSilence provides a constant stream of silenct frames.


type Decoder

type Decoder struct {
	// contains filtered or unexported fields

Decoder translates a io.Reader into a series of frames

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a decoder that will process the provided reader.

func (*Decoder) Decode

func (d *Decoder) Decode(v *Frame, skipped *int) (err error)

Decode reads the next complete discovered frame into the provided Frame struct. A count of skipped bytes will be written to skipped.

skipped := 0
r, err := os.Open("file.mp3")
if err != nil {

d := NewDecoder(r)
var f Frame
for {

	if err := d.Decode(&f, &skipped); err != nil {


type Frame

type Frame struct {
	// contains filtered or unexported fields

Frame represents one individual mp3 frame

var (
	// SilentFrame is the sound of Ripley screaming on the Nostromo, from the outside
	SilentFrame *Frame

	// SilentBytes is the raw raw data behind SilentFrame
	SilentBytes []byte

func (*Frame) CRC

func (f *Frame) CRC() (uint16, error)

CRC returns the CRC word stored in this frame

func (*Frame) Duration

func (f *Frame) Duration() time.Duration

Duration calculates the time duration of this frame based on the samplerate and number of samples

func (*Frame) Header

func (f *Frame) Header() FrameHeader

Header returns the header for this frame

func (*Frame) Reader

func (f *Frame) Reader() io.Reader

Reader returns an io.Reader that reads the individual bytes from the frame

func (*Frame) Samples

func (f *Frame) Samples() int

Samples determines the number of samples based on the MPEG version and Layer from the header

func (*Frame) SideInfo

func (f *Frame) SideInfo() FrameSideInfo

SideInfo returns the side info for this frame

func (*Frame) SideInfoLength

func (f *Frame) SideInfoLength() (int, error)

SideInfoLength retursn the expected side info length for this mp3 frame

func (*Frame) Size

func (f *Frame) Size() int

Size clculates the expected size of this frame in bytes based on the header information

func (*Frame) String

func (f *Frame) String() string

Frame returns a string describing this frame, header and side info

type FrameBitRate

type FrameBitRate int

FrameBitRate is the bit rate from the frame header

const (
	// ErrInvalidBitrate indicates that the header information did not contain a recognized bitrate
	ErrInvalidBitrate FrameBitRate = -1

type FrameChannelMode

type FrameChannelMode byte

FrameChannelMode is the Channel mode from the frame header

const (
	Stereo FrameChannelMode = iota

func (FrameChannelMode) String

func (i FrameChannelMode) String() string

type FrameEmphasis

type FrameEmphasis byte

FrameEmphasis is the Emphasis value from the frame header

const (
	EmphNone FrameEmphasis = iota

func (FrameEmphasis) String

func (i FrameEmphasis) String() string

type FrameHeader

type FrameHeader []byte

FrameHeader represents the entire header of a frame

func (FrameHeader) BitRate

func (h FrameHeader) BitRate() FrameBitRate

BitRate returns the calculated bit rate from the header

func (FrameHeader) ChannelMode

func (h FrameHeader) ChannelMode() FrameChannelMode

ChannelMode returns the channel mode from the header

func (FrameHeader) CopyRight

func (h FrameHeader) CopyRight() bool

CopyRight returns the CopyRight bit from the header

func (FrameHeader) Emphasis

func (h FrameHeader) Emphasis() FrameEmphasis

Emphasis returns the Emphasis from the header

func (FrameHeader) Layer

func (h FrameHeader) Layer() FrameLayer

Layer returns the MPEG layer from the header

func (FrameHeader) Original

func (h FrameHeader) Original() bool

Original returns the "original content" bit from the header

func (FrameHeader) Pad

func (h FrameHeader) Pad() bool

Pad returns the pad bit, indicating if there are extra samples in this frame to make up the correct bitrate

func (FrameHeader) Private

func (h FrameHeader) Private() bool

Private retrusn the Private bit from the header

func (FrameHeader) Protection

func (h FrameHeader) Protection() bool

Protection indicates if there is a CRC present after the header (before the side data)

func (FrameHeader) SampleRate

func (h FrameHeader) SampleRate() FrameSampleRate

SampleRate returns the samplerate from the header

func (FrameHeader) String

func (h FrameHeader) String() string

String dumps the frame header as a string for display purposes

func (FrameHeader) Version

func (h FrameHeader) Version() FrameVersion

Version returns the MPEG version from the header

type FrameLayer

type FrameLayer byte

FrameLayer is the MPEG layer given in the frame header

const (
	LayerReserved FrameLayer = iota

func (FrameLayer) String

func (i FrameLayer) String() string

type FrameSampleRate

type FrameSampleRate int

FrameSampleRate is the sample rate from teh frame header

type FrameSideInfo

type FrameSideInfo []byte

FrameSideInfo holds the SideInfo bytes from the frame

func (FrameSideInfo) NDataBegin

func (i FrameSideInfo) NDataBegin() uint16

NDataBegin is the number of bytes before the frame header at which the sample data begins 0 indicates that the data begins after the side channel information. This data is the data from the "bit reservoir" and can be up to 511 bytes

func (FrameSideInfo) String

func (i FrameSideInfo) String() string

String renders the side info as a string for display purposes

type FrameVersion

type FrameVersion byte

FrameVersion is the MPEG version given in the frame header

const (
	MPEG25 FrameVersion = iota

func (FrameVersion) String

func (i FrameVersion) String() string


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL