Documentation
¶
Overview ¶
Package mpa is an MPEG-1 Audio library. It's currently decoding-only.
A trivial example which reads the MPEG-1 coded bitstream from stdin and writes the decoded PCM stream to stdout:
package main import ( "io" "os" "github.com/korandiz/mpa" ) func main() { io.Copy(os.Stdout, &mpa.Reader{Decoder: &mpa.Decoder{Input: os.Stdin}}) }
On Linux, for example, if you have alsa-utils installed, you can play an mp3 using the above code with something like this:
go run decode.go < nyan.mp3 | aplay -fcd
Index ¶
- Constants
- type Decoder
- func (d *Decoder) Bitrate() int
- func (d *Decoder) Copyrighted() bool
- func (d *Decoder) DecodeFrame() error
- func (d *Decoder) Emphasis() int
- func (d *Decoder) Layer() int
- func (d *Decoder) Mode() int
- func (d *Decoder) NChannels() int
- func (d *Decoder) NSamples() int
- func (d *Decoder) Original() bool
- func (d *Decoder) ReadSamples(ch int, dst []float32)
- func (d *Decoder) SamplingFrequency() int
- type MalformedStream
- type Reader
Constants ¶
const ( S16_LE = iota // Signed, 16-bit, little endian S16_BE // Signed, 16-bit, big endian U16_LE // Unsigned, 16-bit, little endian U16_BE // Unsigned, 16-bit, big endian S24_LE // Signed, 24-bit, little endian S24_BE // Signed, 24-bit, big endian U24_LE // Unsigned, 24-bit, little endian U24_BE // Unsigned, 24-bit, big endian S24_3LE // Signed, packed 24-bit, little endian S24_3BE // Signed, packed 24-bit, big endian U24_3LE // Unsigned, packed 24-bit, little endian U24_3BE // Unsigned, packed 24-bit, big endian S32_LE // Signed, 32-bit, little endian S32_BE // Signed, 32-bit, big endian U32_LE // Unsigned, 32-bit, little endian, U32_BE // Unsigned, 32-bit, big endian S8 // Signed, 8-bit U8 // Unsigned, 8-bit S20_3LE // Signed, packed 20-bit, little endian S20_3BE // Signed, packed 20-bit, big endian U20_3LE // Unsigned, packed 20-bit, little endian U20_3BE // Unsigned, packed 20-bit, big endian S18_3LE // Signed, packed 18-bit, little endian S18_3BE // Signed, packed 18-bit, big endian U18_3LE // Unsigned, packed 18-bit, little endian U18_3BE // Unsigned, packed 18-bit, big endian )
Named constants for Reader.Format.
const ( EmphNone = 0 // No emphasis Emph5015 = 1 // 50/15 µs emphasis EmphUnknown = 2 // "reserved" EmphCCITT = 3 // CCITT J.17 emphasis )
Named constants for the 'emphasis' header field.
const ( ModeStereo = 0 ModeJointStereo = 1 ModeDualChannel = 2 ModeMono = 3 )
Named constants for the 'mode' header field.
const ( ChLeft = 0 ChRight = 1 )
Indexes of the left and right channels.
const FreeFormat = 0
Indicates a free format bitstream, i.e. one not encoded at one of the predefined bitrates.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Decoder ¶
A Decoder reads and decodes MPEG-1 Audio data from an input stream. It has its own internal buffer, so wrapping the input with a bufio.Reader or similar object is unnecessary.
func (*Decoder) Bitrate ¶
Bitrate returns the bitrate of the last decoded frame in bps. For free format streams, it returns FreeFormat. If DecodeFrame hasn't been called yet, or if the last call failed, the return value is undefined.
func (*Decoder) Copyrighted ¶
Copyrighted returns true if and only if the bitstream is copyright-protected. This information comes directly from the frame header, and it doesn't affect the decoding process in any way. If DecodeFrame hasn't been called yet, or if the last call failed, the return value is undefined.
func (*Decoder) DecodeFrame ¶
DecodeFrame synchronizes the decoder with the bitstream and decodes the next frame found. The resulting PCM samples (and other information about the frame) can then be queried.
If Input has changed since the last call, it automatically resets the decoder.
DecodeFrame returns an error in the following cases:
- If it reaches the EOF before finding a syncword, it returns io.EOF.
- If it reaches the EOF in the middle of a frame, it returns io.ErrUnexpectedEOF.
- If several consecutive attempts at reading the input return no data and no error either, it returns io.ErrNoProgress.
- Any other I/O error is passed on verbatim.
- If it encounters a condition where it's absolutely impossible or absolutely pointless to continue (such as an invalid value in the 'layer' header field), DecodeFrame returns a MalformedStream, most likely leaving the decoder in an out-of-sync state.
func (*Decoder) Emphasis ¶
Emphasis returns the type of de-emphasis that shall be used for the last decoded frame. The return value is one of EmphNone, Emph5015, EmphUnknown, and EmphCCITT. This information comes directly from the frame header, and it doesn't affect the decoding process in any way. If DecodeFrame hasn't been called yet, or if the last call failed, the return value is undefined.
func (*Decoder) Layer ¶
Layer returns the layer (1, 2, or 3) used for the last decoded frame. If DecodeFrame hasn't been called yet, or if the last call failed, the return value is undefined.
func (*Decoder) Mode ¶
Mode returns the mode used for the last decoded frame. The return value is one of ModeStereo, ModeJointStereo, ModeDualChannel, and ModeMono. If DecodeFrame hasn't been called yet, or if the last call failed, the return value is undefined.
func (*Decoder) NChannels ¶
NChannels returns the number of channels (1 or 2) of the last decoded frame. If DecodeFrame hasn't been called yet, or if the last call failed, the return value is undefined.
func (*Decoder) NSamples ¶
NSamples returns the number of samples per channel (384 or 1152) in the last decoded frame. If DecodeFrame hasn't been called yet, or if the last call failed, the return value is undefined.
func (*Decoder) Original ¶
Original returns true, if the bitstream is an original, and false, if it's a copy. This information comes directly from the frame header, and it doesn't affect the decoding process in any way. If DecodeFrame hasn't been called yet, or if the last call failed, the return value is undefined.
func (*Decoder) ReadSamples ¶
ReadSamples reads the samples of channel ch of the last decoded frame into dst. If len(dst) is greater than the number of samples per channel, the remaining elements are left intact. If dst is shorter than the number of samples, then only the first len(dst) samples are read.
If ch is invalid (ch < 0 or ch >= NChannels), the data for the 0th (left) channel is read. This means it's safe to query "both" channels of a mono frame.
If DecodeFrame hasn't been called yet, or if the last call failed, the samples are still written into dst, but their value is undefined.
The samples are guaranteed to be in the interval [-1, 1], even after an error.
func (*Decoder) SamplingFrequency ¶
SamplingFrequency returns the sampling freqency (32k, 44.1k, or 48k) of the last decoded frame in Hz. If DecodeFrame hasn't been called yet, or if the last call failed, the return value is undefined.
type MalformedStream ¶
type MalformedStream string
A MalformedStream is returned when the decoder encounters a syntax or semantic error it's unable to conceal. Such errors usually leave the decoder in an out-of-sync state.
func (MalformedStream) Error ¶
func (err MalformedStream) Error() string
Error returns the error as a string.