Documentation
¶
Overview ¶
Package nativedec decodes MP3, FLAC and Ogg Vorbis with dr_mp3, dr_flac and stb_vorbis, single-file C decoders vendored in third_party, and AAC with libfaad (faad2) for network streams; see StreamDecoder. The pure Go decoders cost about half of a MiSTer CPU core to play one MP3, which the background music slot spent for as long as it played; these decode the same audio for a fraction of that. A Decoder is a beep StreamSeekCloser, so it slots in wherever the Go decoders were.
Index ¶
- Variables
- type Decoder
- type Kind
- type StreamDecoder
- func (d *StreamDecoder) Close() error
- func (d *StreamDecoder) Feed(p []byte) error
- func (d *StreamDecoder) Flush()
- func (d *StreamDecoder) Format() (rate, channels int, ok bool)
- func (d *StreamDecoder) Kind() Kind
- func (d *StreamDecoder) Read(out [][2]float64) (int, error)
- func (d *StreamDecoder) Reset()
- func (d *StreamDecoder) Space() int
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNeedMore is returned by StreamDecoder.Read when the buffered input // holds no further complete frame: Feed more and read again. ErrNeedMore = errors.New("nativedec: decoder needs more input") // ErrStreamFull is returned by StreamDecoder.Feed when the bytes do not // fit in the bounded input buffer. Space says how many would. ErrStreamFull = errors.New("nativedec: stream input buffer full") // ErrUndecodable is returned once a stream yields no audio for too long: // a long run of garbage or of frames that fail to decode. ErrUndecodable = errors.New("nativedec: stream cannot be decoded") // ErrUnsupportedCodec is returned for an Ogg stream that carries another // codec than Vorbis, such as Opus or FLAC. ErrUnsupportedCodec = errors.New("nativedec: unsupported codec in Ogg stream") )
var ErrClosed = errors.New("nativedec: decoder closed")
ErrClosed is returned by a Decoder used after Close.
Functions ¶
This section is empty.
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder streams one decoded file as stereo frames. It is not safe for concurrent use; the playback code drives it from one goroutine.
func (*Decoder) Channels ¶
Channels is how many channels the file has. Stream always produces two: mono is copied to both, and only the first two of more are kept.
func (*Decoder) SampleRate ¶
SampleRate is the decoded audio's sample rate.
type Kind ¶
type Kind int
Kind is an encoded audio format a Decoder reads.
AAC is ADTS-framed AAC (LC, HE-AAC v1 and v2). Only a StreamDecoder reads it; KindForExt does not map ".aac" to it.
func KindForExt ¶
KindForExt returns the Kind for a file extension (".mp3", ".flac", ".ogg"), case-insensitively.
type StreamDecoder ¶
type StreamDecoder struct {
// contains filtered or unexported fields
}
StreamDecoder decodes an endless, unseekable stream of MP3, Ogg Vorbis or ADTS AAC pushed to it in arbitrary chunks, as a network stream arrives. The input is untrusted: garbage is skipped while resyncing, memory is bounded, and a long run without audio is an error.
It is not safe for concurrent use.
func NewStreamDecoder ¶
func NewStreamDecoder(kind Kind) (*StreamDecoder, error)
NewStreamDecoder returns a decoder for kind: MP3, Vorbis or AAC.
func (*StreamDecoder) Close ¶
func (d *StreamDecoder) Close() error
Close frees the decoder. It is safe to call more than once.
func (*StreamDecoder) Feed ¶
func (d *StreamDecoder) Feed(p []byte) error
Feed appends encoded bytes. It fails with ErrStreamFull, taking none of them, when they do not fit; read first, or feed less.
func (*StreamDecoder) Flush ¶
func (d *StreamDecoder) Flush()
Flush marks the input finished: Read decodes the last buffered bytes, which it otherwise holds back until more data confirms the frame sync, and then returns io.EOF. Reset clears it.
func (*StreamDecoder) Format ¶
func (d *StreamDecoder) Format() (rate, channels int, ok bool)
Format is the sample rate and channel count of the frames the last Read returned, or of the stream's headers before any were read. ok is false until they are known. HE-AAC reports the SBR output rate.
func (*StreamDecoder) Kind ¶
func (d *StreamDecoder) Kind() Kind
Kind is the format the decoder reads.
func (*StreamDecoder) Read ¶
func (d *StreamDecoder) Read(out [][2]float64) (int, error)
Read decodes into out as stereo frames: mono is copied to both channels and only the first two of more are kept. It returns ErrNeedMore when the buffered input is used up, and every frame of one call has the format Format reports after it. A stream that changes rate mid-way does so between calls.
func (*StreamDecoder) Reset ¶
func (d *StreamDecoder) Reset()
Reset drops the buffered input, decoded audio and decoder state, as for a discontinuity in the stream; the decoder resyncs on what is fed next.
func (*StreamDecoder) Space ¶
func (d *StreamDecoder) Space() int
Space is how many more bytes Feed accepts before Read consumes some.