Audio frontend
The audio frontend decodes integer PCM WAV and narrow progressive MP4 AAC-LC/AC-3 subsets to caller-owned S16 buffers. It includes explicit MP4 track selection, AC-3 mono/stereo downmix or one/two-channel extraction, native or downmixed multichannel AAC-LC, streaming channel conversion, rational polyphase resampling, integral edit/trim handling and an optional hashed checkpoint store. Exact amd64 SSE2/AVX2 and selected ARM64 NEON kernels cover measured numeric paths, with scalar references and a purego fallback. SIMD coverage and measurements list the implementation and remaining serial work. The decoder is pre-release; only the qualified cases below are supported.
Public contract
dec, err := audio.Open(ctx, readerAt, size, audio.Options{}) // 16 kHz mono S16
// Handle err before use.
defer dec.Close()
meta := dec.Metadata()
buf := make([]int16, 4096)
for {
n, span, err := dec.ReadPCM(ctx, buf)
// Consume buf[:n] BEFORE checking err. n counts scalar samples;
// span and meta count frames (one sample per channel).
_ = span
if err == io.EOF { break }
if err != nil { return err }
}
_ = meta
Probe(ctx, readerAt, size, pcm.Limits{}) validates metadata without decoding PCM and returns the edited source-rate extent. ProbeMetadata additionally returns the pre-trim decoded extent, priming, padding and leading silence; callers must not pass the edited Probe count back to mp4.Track.TimingPlan, whose argument is the pre-trim decoded count. Invalid or unsupported AAC/AC-3 payloads may still fail when read. Sources are immutable and caller-owned; Close never closes their underlying file. Reads, seeks and close are sequential. Seek(ctx, canonicalFrame) reproduces the resampler history from source samples, including near boundaries. AAC reads after seeks replay earlier access units as needed for exact PNS/overlap state, so random access can be linear in stream length. Cancellation is checked per replayed access unit; no fast-seek claim is made. Cancellation is checked at chunk/sample boundaries; arbitrary blocking ReaderAt implementations cannot be forcibly interrupted. Contexts must be non-nil. Open requires random access. OpenStream(ctx, reader, privateTempDir, opts) provides capped temporary spooling for non-seekable inputs; Close removes its owned spool without closing the caller's input. Spooling checks one extra byte beyond the cap without writing it and cleans up failed/cancelled copies. It is not a durable checkpoint.
The API is pre-release. Audio and video share the existing module version. No new module or dependency is introduced. go-pherence may consume this contract through an adapter; mel/Fbank features, inference, upload storage and job scheduling are outside this package.
| Input |
Behaviour |
Limits |
| RIFF/WAVE format 1 integer PCM |
Decodes 8/16/24/32-bit mono or stereo |
8–192 kHz |
| Float, compressed or extensible WAV; RF64/RIFX |
Returns a typed unsupported error |
Unsupported |
| MP4/M4A/MOV AAC-LC |
Decodes progressive AAC-LC channel configurations 1–7 (mono to 7.1) with 1024- or 128-sample windows |
Integral unit-rate edits; unsupported tools fail during reads; configuration 0/PCE and CCE fail |
| MP4/MOV AC-3 |
Decodes one exact trak; accepts bsid 0–8 and mono-to-5.1 input |
One complete syncframe per MP4 sample; mono/stereo downmix or one/two canonical source channels |
| E-AC-3, HE-AAC/SBR/PS, ALAC, DRM, fragmented MP4 |
Returns a typed unsupported error |
Unsupported |
The reader rejects duplicate fmt/data, data before format, invalid sizes/alignment, partial frames and trailing bytes outside RIFF. Default limits: 512 MiB, four hours and 65,536 chunks. Negative limits fail. PCM files above four hours fail even when their byte size fits. No whole-file or caller-sized scratch allocation is used.
PCM and timing
- Low-level
ReadFrames yields normalised interleaved float64 and returns frame counts. Public ReadPCM yields S16 and returns scalar sample counts.
- Output is mono/stereo, or the source channel count for multichannel AAC-LC. PCM/AAC stereo-to-mono is
(L+R)/2; mono-to-stereo duplicates samples. AC-3 accepts mono–5.1 input and either downmixes using coded centre/surround levels (excluding LFE) or extracts explicit canonical channels. For 5.1, channel indices are FL, FR, FC, LFE, SL, SR.
- Multichannel AAC-LC (
TargetChannels equal to the source count) is interleaved in WAVE/SMPTE order, as FFmpeg and AC-3 Layout use, not bitstream order (which starts with centre): FL FR FC, FL FR FC BC, FL FR FC BL BR, FL FR FC LFE BL BR and FL FR FC LFE BL BR FLC FRC for configurations 3–7; aac.Layout returns them. Configuration 7 uses the specification-defined 7.1 wide layout. FFmpeg's strict decoder mode preserves these positions; its default compatibility mode reinterprets them as conventional 7.1. TargetChannels 2 applies a fixed ITU-R BS.775 downmix: front and outside-front channels at unity, centre and surround at −3 dB (1/√2), LFE dropped, normalised by 1/max(1, sum of gains per output) (5.1: 1/(1+2/√2)), matching AC-3 with its default mix levels; configurations 1–7 carry no downmix metadata. TargetChannels 1 is (L+R)/2 of that downmix. Resampling and pass-through support up to eight channels; other channel conversions fail.
- Integer conversion clips, rounds ties away from zero and applies no dither. Non-finite PCM fails. Same-rate S16 WAV roundtrips exactly.
- Output frame count is
ceil(sourceFrames*outRate/inRate). Timestamps use checked integer ratios, never accumulated float time.
- Resampling uses an exact rational phase table, Blackman-windowed sinc and zero extension. Downsampling uses a 94% destination-Nyquist transition; upsampling uses input-Nyquist cutoff so integer phases reproduce source samples. It aligns output frame zero with source time zero and removes filter delay from the output timeline.
LookaheadFrames reports input lookahead; metadata delay is zero after compensation.
- Output rates 8–48 kHz, at most 2,048 rational phases. Common 8/16/22.05/44.1/48/96/192 kHz source rates are accepted. Unusual ratios above the phase cap fail.
- The common contiguous mono FIR uses checked SSE2 and AVX2 kernels on amd64. AVX2 dispatch requires CPUID AVX2, OSXSAVE and XCR0 XMM/YMM state. Products are accumulated in source order; no FMA, horizontal reduction or alternate accumulator changes rounding. Edges, ring wrap, stereo and
-tags=purego retain scalar paths. Odd tails, unaligned buffers and protected-page bounds are tested.
- Priming/padding fields are zero for WAV.
mp4.Track.TimingPlan(actualDecodedFrames) computes an exact integer trim plan for no edits, one unit-rate media edit, or an initial empty edit plus media edit. Fractional-sample edits, repeats and rate changes fail explicitly. The public MP4 path applies this plan while streaming, validates even fully trimmed trailing access units, and includes empty-edit silence in output. Metadata.Source.Frames is pre-trim coded-frame count; Output.Frames is the edited/resampled length. LeadingSilenceFrames uses source-rate units. Span.SourceStartFrame is the floored pre-trim PCM coordinate after subtracting leading silence and adding priming; spans starting in leading silence report -1 and SourcePadding=true. It describes only the span start, not every FIR contributing sample.
Package isolation
audio/pcm is the shared leaf. audio/ac3 is a strict stateful AC-3 syncframe decoder. E-AC-3 fails closed. Each successful call returns 1,536 frames. Cancellation and malformed frames leave decoder state and destination unchanged. audio.Options.TrackIndex selects an exact zero-based MP4 trak; nil retains AAC-first default selection (mono/stereo AAC-LC, then AC-3, then multichannel AAC-LC, so files that previously fell back to AC-3 keep that track). SourceChannels requests one or two canonical AC-3 channels; nil selects deterministic mono/stereo downmix. Decoder.TrackIndex() and SourceChannels() report the applied policy. audio/wav, audio/convert and audio/resample are independently importable. audio composes them. audio/aac parses progressive AAC-LC 1024/128 access units for channel configurations 1–7 and reconstructs spectra, PNS, M/S, intensity and TNS through the FFT filterbank. Each SCE/CPE/LFE must appear once in ISO/IEC 14496-3 Table 1.19 order (instance tags are not interpreted); missing, extra or reordered elements and short-window LFE are malformed. Elements are parsed, reconstructed and synthesised one at a time, so mono/stereo keep their stack footprint and decode stays allocation-free for every layout. Decode(ctx, packet, dst) returns 1,024 frames on success; errors leave output and decoder state unchanged. NewDecoderWithOptions selects native or 1/2-channel downmix output. Callers apply container timing and trimming. CCE, PCE (including channel configuration 0), prediction, gain control, SBR/PS and unknown fill extensions are explicitly unsupported. PNS noise vectors remain independent per channel even when a common-window M/S mask covers both noise bands; ordinary M/S transformation excludes noise codebooks. Five-frame per-band/all-band mask oracles match FFmpeg within 1.1e-10 maximum absolute PCM error. Synthetic FFmpeg 3.0/4.0/5.0/5.1/7.1 AAC-LC (configurations 3–7) with a distinct tone per channel, plus 5.1/7.1 PNS-noise and transient variants and 44.1 kHz cases, match FFmpeg's native decode per channel at about 138–141 dB SNR (maximum error below 1.7e-7) after the first access unit; tones are isolated by at least 71 dB, and float downmixes match the documented gains applied to FFmpeg's decode at about 139 dB. Public S16 stereo output reproduces every gain within 0.002 and contains no LFE; in-phase near-full-scale input does not reach the rails. For the first access unit only, the filterbank applies the current window shape where FFmpeg assumes a previous sine window; this predates multichannel support and is confined to encoder priming that edit lists trim. audio/mp4.Open selects the first accepted progressive AAC-LC track (for configurations 3–7 the ASC channel count is authoritative, as dac3 is for AC-3), validates self-contained data references, indexes stsc/stsz/stz2/stco/co64/stts/ctts and returns caller-buffer packets. Edits are copied as metadata; TimingPlan separately validates the supported integer subset. QuickTime versioned audio entries, description switching, fragmentation and protected tracks fail. Limits bound tables, tracks, packets, duration and descriptor nesting. There are no video, CLI, go-pherence, native-code, subprocess or third-party Go module runtime dependencies.
TestAudioImportBoundary checks all runtime Go imports, including inactive architecture files. Run focused tests with CGO_ENABLED=0 go test ./audio/...; these do not require video fixtures. .github/workflows/audio.yml runs default and purego tests, vet, the full race detector and synthetic FFmpeg oracle matrix on amd64, plus default, purego, vet and race tests on a native Linux ARM64 runner. Race builds skip two exact-bit tests because instrumentation changes Go floating-point code generation while assembly is uninstrumented; default native tests retain exact NEON parity and the pinned production synthesis digest. Cross-builds and QEMU remain functional checks, not native architecture evidence.
Canonical PCM checkpoints
audio/store is an optional single-writer store in a caller-owned private directory. Create it with a source SHA256, exact decoder/configuration identifier and PCM metadata; append interleaved S16 segments up to 1 MiB. Segment files are synced and installed with no-clobber links, then the manifest is replaced atomically and its directory synced. An uncertain final sync returns ErrUncertainDurability and makes that instance unusable until reopened.
Open validates contiguous counts and committed segment hashes before exposing Frames(). Resume by re-verifying source identity, reopening the same decoder configuration and seeking to that canonical frame. Orphan segments are not counted; identical orphans can be reused, conflicting bytes are not overwritten. There is no concurrent-writer/symlink sandbox guarantee, nor hardware power-loss certification. Tests inject cancellation/sync failures and verify WAV replay-to-checkpoint equivalence. Upload retention and admission/disk quotas belong to the application.
Audio-only command
CGO_ENABLED=0 go build ./cmd/decodeaudio builds an audio-only consumer with no video imports. Run decodeaudio -rate 16000 -channels 1 input.wav > output.s16le. The command owns the input file and honours interrupt cancellation; output is raw little-endian PCM, not a WAV container.
Licensing
The entire go-264 project, including these audio packages, is licensed under the root MIT License. Imported material retains its upstream copyright and MIT notices; see third-party notices. Referenced external recordings keep their separately documented dataset licences.
New dependencies must be MIT-licensed. The AC-3 bit-allocation, mantissa and transform implementation is adapted from MIT-licensed AC3Psy revision ebdd1d3d6cf80690d1c7e648231f7d603ca33f97, copyright (c) 2026 PCPX; its complete notice is retained in audio/ac3/LICENSE.ac3psy. E-AC-3, C ABI, concealment, telemetry and native C code are omitted. A non-MIT implementation can be replaced only through a genuine clean-room process; translating it to Go or assembly does not change its provenance. No FAAD2/LGPL/Apache implementation is included. The filterbank, bitreader and Huffman primitives are attributed MIT ports from OxideAV at 7dcb2f4a9e6f7ccfa6b199342aeb95861dc57885; their MIT notices are retained in each internal package. go generate ./audio/aac/internal/huffman regenerates 1,241 spectral codewords with decoded tuples and 121 scalefactor entries from checksum-pinned upstream sources. Set GO264_MIT_AAC_REFERENCE to a checkout of that revision when regenerating outside the development workspace. The FFT reduction is derived from the direct cosine equation and checked against that scalar oracle. internal/lc parser and reconstruction code retains MIT attribution. A corrected first-noise-delta rule avoids sign-extending a value whose bias was already applied; synthetic PCM regression tests verify the result. This narrow decoder does not establish general AAC conformance.
Fixtures and gates
Unit fixtures are synthetic, authored in the tests and distributed under the repository licence. They contain literal PCM extrema, tones, impulses, silence and malformed containers. No private recordings are used. WAV PCM requires exact results. AAC acceptance is limited to the declared AAC-LC subset and the oracle/public-speech evidence below; broader encoder and corpus coverage is optional qualification, not permission to accept unsupported tools. FFmpeg may generate offline oracle output, never serve runtime decoding.
A synthetic FFmpeg 48 kHz 5.1 AC-3 MP4 uses six distinct allocated tones to verify canonical FL, FR, FC, LFE, SL, SR extraction after the exact 256-frame edit offset. All channels achieve at least 77.1 dB SNR and 0.999999990 correlation against FFmpeg; deterministic dither is separately tested because A/52 permits implementation-defined reasonably random sequences. The retained 48 kHz 5.1 BBB track decodes all 19,817 syncframes (30,438,912 frames) in mono downmix, stereo downmix, centre, centre+LFE and surround extraction modes with pinned full-output hashes. Frame decode is allocation-free and measured at about 357–359 µs per 1,536-frame 5.1-to-stereo syncframe on the development i5-1340P. Packed overlap-add reduces its 256-sample kernel from about 154 ns to 68 ns on amd64, with exact scalar parity, protected-page bounds and ARM64/QEMU NEON parity. Eight synthetic FFmpeg M4A cases (mono/stereo, 44.1/48 kHz, front/tail moov) check every packet's bytes, offset, size and unedited timestamp against ffprobe. Eight additional source-rate public-API MP4 PCM tests check explicit priming/end trim, caller chunking, exact backward/forward/end-of-file seek and S16 error ≤0.501 LSB against FFmpeg's retained media interval. FFmpeg's raw output includes end padding beyond that interval; total lengths are compared to the declared edit plan rather than silently retaining it. Forty-eight separately decoded mono/stereo cases at 8/16/22.05/24/32/44.1/48/96 kHz across tone, transient and PNS fixtures require SNR ≥70 dB and maximum absolute error ≤1e-5 against FFmpeg float PCM. A second eight-case, 1.25-second matrix varies 16–96 kHz, mono/stereo, 24–192 kb/s and encoder PNS/M/S/intensity/TNS policies. All cases exceed 137 dB SNR, and maximum absolute error stays below 1.53e-7. Four handcrafted TNS cases cover both directions and coefficient signs, with maximum absolute error below 8e-11. Six intensity-stereo sign/M/S-mode cases and independent or common-window masked stereo PNS are checked against FFmpeg. The optional go-pherence backend now passes scoped paired public-speech checks: six MINDS PCM/AAC pairs have identical Tiny windows after the interpolation correction; one annotated 30-second sample has a diarisation error-rate delta of 0 percentage points for WAV and +0.08514 percentage points for AAC, within the 0.5-point limit. The DER run overlaps six seconds of another session's setup, so its timings are not isolated evidence. See testdata/public-fixtures.json for hashes, licences and limits. The fixture set does not establish broad codec, production speech-recognition or meeting quality. Filterbank tests compare FFT output to the direct equation across window sequences, shapes and coefficient scales.
Four-hour sparse-WAV and actual SIGKILL checkpoint stages pass, as do scalar/assembly, malformed-input and replay tests. The full audio/... race suite passes on linux/amd64 in Go 1.26.2. Native ARM64 race coverage executes every audio test except the two instrumentation-sensitive exact-bit checks described above. Synthetic two-second canonical decode measures about 138× realtime and isolated resampling about 750×; these exceed the provisional 50×/100× targets on those fixtures only. Generalisation to other encoders, tools, long workloads and physical power-loss behaviour has not been established. No tag or serving-default switch has been made.