mpa

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2015 License: ISC, GPL-3.0 Imports: 2 Imported by: 0

README

Please see http://godoc.org/github.com/korandiz/mpa

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

View Source
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.

View Source
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.

View Source
const (
	ModeStereo      = 0
	ModeJointStereo = 1
	ModeDualChannel = 2
	ModeMono        = 3
)

Named constants for the 'mode' header field.

View Source
const (
	ChLeft  = 0
	ChRight = 1
)

Indexes of the left and right channels.

View Source
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

type Decoder struct {
	Input io.Reader
	// contains filtered or unexported fields
}

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

func (d *Decoder) Bitrate() int

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

func (d *Decoder) Copyrighted() bool

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

func (d *Decoder) DecodeFrame() error

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

func (d *Decoder) Emphasis() int

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

func (d *Decoder) Layer() int

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

func (d *Decoder) Mode() int

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

func (d *Decoder) NChannels() int

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

func (d *Decoder) NSamples() int

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

func (d *Decoder) Original() bool

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

func (d *Decoder) ReadSamples(ch int, dst []float32)

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

func (d *Decoder) SamplingFrequency() int

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.

type Reader

type Reader struct {
	Decoder *Decoder
	Format  int
	Mono    bool
	Swap    bool
	// contains filtered or unexported fields
}

A Reader wraps a Decoder and converts its output into a byte stream.

func (*Reader) Read

func (r *Reader) Read(dst []byte) (int, error)

Read reads up to len(dst) bytes of PCM data into dst and returns the number of bytes read.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL