Documentation ¶
Overview ¶
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 github.com/badgerodon/mp3, and has also drawn from Konrad Windszus' excellent article on mp3 frame parsing http://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header
TODO CRC isn't currently checked.
Index ¶
- Variables
- func MakeSilence() io.ReadCloser
- type Decoder
- type Frame
- func (f *Frame) Bytes() []byte
- func (f *Frame) CRC() (uint16, error)
- func (f *Frame) Duration() time.Duration
- func (f *Frame) Header() FrameHeader
- func (f *Frame) Reader() io.Reader
- func (f *Frame) Samples() int
- func (f *Frame) SideInfo() FrameSideInfo
- func (f *Frame) SideInfoLength() (int, error)
- func (f *Frame) Size() int
- func (f *Frame) String() string
- type FrameBitRate
- type FrameChannelMode
- type FrameEmphasis
- type FrameHeader
- func (h FrameHeader) BitRate() FrameBitRate
- func (h FrameHeader) ChannelMode() FrameChannelMode
- func (h FrameHeader) CopyRight() bool
- func (h FrameHeader) Emphasis() FrameEmphasis
- func (h FrameHeader) Layer() FrameLayer
- func (h FrameHeader) Original() bool
- func (h FrameHeader) Pad() bool
- func (h FrameHeader) Private() bool
- func (h FrameHeader) Protection() bool
- func (h FrameHeader) SampleRate() FrameSampleRate
- func (h FrameHeader) String() string
- func (h FrameHeader) Version() FrameVersion
- type FrameLayer
- type FrameSampleRate
- type FrameSideInfo
- type FrameVersion
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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") )
Functions ¶
func MakeSilence ¶
func MakeSilence() io.ReadCloser
MakeSilence provides a constant stream of silenct frames.
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder translates a io.Reader into a series of frames
func NewDecoder ¶
NewDecoder returns a decoder that will process the provided reader.
func (*Decoder) Decode ¶
Decode reads the next complete discovered frame into the provided Frame struct. A count of skipped bytes will be written to skipped.
Example ¶
skipped := 0 r, err := os.Open("file.mp3") if err != nil { fmt.Println(err) return } d := NewDecoder(r) var f Frame for { if err := d.Decode(&f, &skipped); err != nil { fmt.Println(err) return } fmt.Println(&f) }
Output:
type Frame ¶
type Frame struct {
// contains filtered or unexported fields
}
Frame represents one individual mp3 frame
func (*Frame) 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) Samples ¶
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 ¶
SideInfoLength retursn the expected side info length for this mp3 frame
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 JointStereo DualChannel SingleChannel ChannelModeMax )
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 Emph5015 EmphReserved EmphCCITJ17 EmphMax )
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 Layer3 Layer2 Layer1 LayerMax )
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 MPEGReserved MPEG2 MPEG1 VERSIONMAX )
func (FrameVersion) String ¶
func (i FrameVersion) String() string