alac

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package alac decodes Apple Lossless audio in progressive MP4 files.

Index

Constants

View Source
const (
	// MaxCoefs is the maximum predictor order (5-bit field).
	MaxCoefs = 32

	// NumActiveDelta triggers first-order delta decode mode.
	NumActiveDelta = 31

	// UnusedHeaderBits is the unused header field size in SCE/CPE (spec-defined).
	UnusedHeaderBits = 12
)
View Source
const MaxPacketSize = mp4.MaxPacketSize

MaxPacketSize is the maximum compressed packet size accepted for waveform decoding.

View Source
const Padding = 16

Padding is the zeroed tail a caller appends after the compressed packet.

Variables

View Source
var (
	// ErrConfig indicates an invalid or unsupported ALAC configuration
	// (bad magic cookie, unsupported version, unsupported bit depth).
	ErrConfig = errors.New("invalid configuration")

	// ErrNoTrack indicates no usable ALAC track was found in the container
	// (missing track, missing or malformed MP4 boxes).
	ErrNoTrack = errors.New("no track found")

	// ErrDecode indicates a failure during packet decoding
	// (bitstream overrun, invalid headers, unsupported elements).
	ErrDecode = errors.New("decode failed")
)

Public sentinel errors for consumer error matching.

View Source
var (
	ErrInvalidCookie      = errors.New("alac: invalid magic cookie")
	ErrUnsupportedVersion = errors.New("alac: unsupported compatible version")
	ErrUnsupportedElement = errors.New("alac: unsupported element type (CCE/PCE)")
	ErrInvalidHeader      = errors.New("alac: invalid frame header")
	ErrInvalidShift       = errors.New("alac: invalid bytesShifted value")
	ErrBitstreamOverrun   = errors.New("alac: bitstream overrun")
	ErrSampleOverrun      = errors.New("alac: sample count exceeds buffer")
	ErrBitDepth           = errors.New("alac: unsupported bit depth")
	ErrChannels           = errors.New("alac: channel count out of range")
	ErrFrameLength        = errors.New("alac: frame length out of range")
	ErrSampleRate         = errors.New("alac: sample rate is zero")
	ErrMissingChannels    = errors.New("alac: fewer channels decoded than declared")
	ErrElementLayout      = errors.New("alac: channel pair element past the channel layout")
)

ALAC decoder error sentinels.

View Source
var ErrSeekRequired = errors.New("ALAC in MP4 requires a seekable reader")

ErrSeekRequired means the source cannot seek to MP4 metadata and packets.

Functions

func DynDecomp

func DynDecomp(params *AGParams, bitBuf *BitBuffer, predCoefs []int32, numSamples, maxSize int) error

DynDecomp performs adaptive Golomb-Rice entropy decoding of a sample block. Writes decoded prediction residuals into predCoefs. Every code must end within the packet: one that would end in the padding returns ErrBitstreamOverrun.

func ReadVerbatim

func ReadVerbatim(bits *BitBuffer, left, right []int32, depth uint32)

ReadVerbatim reads one or two channels of uncompressed signed samples. The caller verifies that the complete element fits in the packet. The padded tail permits a full word load at the last sample.

func RestoreMono

func RestoreMono(samples []int32, depth uint8, low []uint16, bytesShifted int)

RestoreMono reattaches the low bits and keeps the native signed PCM width.

func RestoreStereo

func RestoreStereo(left, right []int32, depth uint8, mixBits, mixRes int32, low []uint16, bytesShifted int)

RestoreStereo inverts the ALAC stereo matrix in place.

func SetAGParams

func SetAGParams(params *AGParams, meanBase, partBound, kBase, frameWin, sampleWin, maxrun uint32)

SetAGParams initialises the adaptive Golomb-Rice parameters.

func UnpcBlock

func UnpcBlock(pc1, out []int32, num int, coefs []int16, numActive int32, chanBits, denShift uint32)

UnpcBlock reverses the linear prediction encoding. pc1 contains the entropy-decoded prediction residuals (input). out receives the reconstructed sample values (output). pc1 and out may be the same slice for in-place operation (numActive==31 mode).

Types

type AGParams

type AGParams struct {
	MB, MB0 uint32
	PB      uint32
	KB      uint32
	WB      uint32
	QB      uint32
	FW, SW  uint32
	MaxRun  uint32
}

AGParams holds the adaptive Golomb-Rice codec state.

type BitBuffer

type BitBuffer struct {
	Buf    []byte
	Pos    int
	BitIdx uint32
	Size   int
}

BitBuffer reads bits from a packet followed by Padding zero bytes.

func (*BitBuffer) Advance

func (b *BitBuffer) Advance(numBits uint32)

Advance skips forward by numBits bits.

func (*BitBuffer) BitsLeft

func (b *BitBuffer) BitsLeft() int64

BitsLeft returns the number of data bits from the read position to the end, negative once the position has been advanced past the end.

func (*BitBuffer) ByteAlign

func (b *BitBuffer) ByteAlign()

ByteAlign advances to the next byte boundary (if not already aligned).

func (*BitBuffer) Copy

func (b *BitBuffer) Copy() BitBuffer

Copy returns a snapshot of the current BitBuffer state. The copy shares the underlying data but has independent position tracking.

func (*BitBuffer) PastEnd

func (b *BitBuffer) PastEnd() bool

PastEnd returns true if the read position is at or past the original data end.

func (*BitBuffer) Read

func (b *BitBuffer) Read(numBits uint8) uint32

Read reads up to 16 bits and returns them right-aligned. Equivalent to BitBufferRead in the Apple implementation.

A read at or past the end of the data (an overrun, which PastEnd reports) returns zero bits and does not advance: the 4-byte padding covers a read that starts inside the data and crosses its end, not one that starts beyond it.

func (*BitBuffer) ReadOne

func (b *BitBuffer) ReadOne() uint8

ReadOne reads a single bit. Past the end it behaves as Read does.

func (*BitBuffer) ReadSmall

func (b *BitBuffer) ReadSmall(numBits uint8) uint8

ReadSmall reads up to 8 bits. Equivalent to BitBufferReadSmall. Past the end it behaves as Read does.

func (*BitBuffer) Reset

func (b *BitBuffer) Reset(data []byte, size int)

Reset binds the reader to a packet and its padding for the duration of decode.

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder reads and decodes an ALAC track. Use it for one pass in one goroutine.

func Open

func Open(ctx context.Context, input io.Reader) (*Decoder, error)

Open locates the ALAC track and reads its sample table. The caller owns source and keeps it open for the duration of Decode. Source must implement io.ReadSeeker, since progressive MP4 stores metadata and packets separately.

func (*Decoder) Close

func (d *Decoder) Close() error

Close releases the decoder and leaves the input open.

func (*Decoder) Decode

func (d *Decoder) Decode(consume func([][]int32)) error

Decode delivers one block of signed, right-justified PCM at a time. The channel slices are decoder-owned and may only be read during consume.

func (*Decoder) Format

func (d *Decoder) Format() (channels, bitsPerSample int)

Format reports the channel count and native PCM depth.

func (*Decoder) NextBlock

func (d *Decoder) NextBlock() (audio.Buffer[int32], error)

NextBlock returns the next ALAC packet as borrowed signed PCM planes.

func (*Decoder) PCMFormat

func (d *Decoder) PCMFormat() audio.Format

PCMFormat reports the native sample rate, ordered channels and depth.

type PacketConfig

type PacketConfig struct {
	FrameLength   uint32
	BitDepth      uint8
	NumChannels   uint8
	PB            uint8
	MB            uint8
	KB            uint8
	MaxRun        uint16
	MaxFrameBytes uint32
	AvgBitRate    uint32
	SampleRate    uint32
}

PacketConfig holds ALAC decoder configuration parsed from the magic cookie.

func ParseMagicCookie

func ParseMagicCookie(cookie []byte) (PacketConfig, error)

ParseMagicCookie reads an ALACSpecificConfig from a magic cookie byte slice. Handles legacy wrappers ('frma' and 'alac' atoms).

Directories

Path Synopsis
Package mp4 extracts ALAC audio tracks and sample tables from MP4/M4A containers.
Package mp4 extracts ALAC audio tracks and sample tables from MP4/M4A containers.

Jump to

Keyboard shortcuts

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