nativedec

package
v2.22.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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")
)
View Source
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 Open

func Open(path string, kind Kind) (*Decoder, error)

Open decodes the file at path.

func OpenBytes

func OpenBytes(data []byte, kind Kind) (*Decoder, error)

OpenBytes decodes data held in memory.

func (*Decoder) Channels

func (d *Decoder) Channels() int

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) Close

func (d *Decoder) Close() error

Close frees the decoder. It is safe to call more than once.

func (*Decoder) Err

func (d *Decoder) Err() error

Err returns the error that stopped the stream, if any.

func (*Decoder) Len

func (d *Decoder) Len() int

Len is the length in frames, or -1 when the file does not say.

func (*Decoder) Position

func (d *Decoder) Position() int

Position is the next frame Stream returns.

func (*Decoder) SampleRate

func (d *Decoder) SampleRate() int

SampleRate is the decoded audio's sample rate.

func (*Decoder) Seek

func (d *Decoder) Seek(p int) error

Seek moves to frame p.

func (*Decoder) Stream

func (d *Decoder) Stream(samples [][2]float64) (int, bool)

Stream fills samples with the next frames. It returns how many it wrote, and false once the audio has ended or failed.

type Kind

type Kind int

Kind is an encoded audio format a Decoder reads.

const (
	MP3    Kind = C.ND_MP3
	FLAC   Kind = C.ND_FLAC
	Vorbis Kind = C.ND_VORBIS
)
const AAC Kind = C.ND_AAC

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

func KindForExt(ext string) (Kind, bool)

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.

Jump to

Keyboard shortcuts

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