From 028bee3f5946b0bc1a994c5bcaa82194217b41d6 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Mon, 2 Nov 2026 00:40:25 -0600 Subject: [PATCH] docs/guide/audio: HE-AAC section and opts.sbr; audio/caf: SBR only at the described rate --- docs/guide/audio.md | 16 +++++++++++++++- src/Rosetta/audio/caf/init.luau | 4 ++++ tools/fixtures/docs_heaac.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tools/fixtures/docs_heaac.py diff --git a/docs/guide/audio.md b/docs/guide/audio.md index 80389ec..787f085 100644 --- a/docs/guide/audio.md +++ b/docs/guide/audio.md @@ -102,6 +102,7 @@ the guide's [README](README.md#inputs)). Options: | `maxFrames` | wav, flac, mp3, aiff, au, voc, aac, mpc, ac3 | `Err.LIMIT` past this many frames | | `drc` | ac3 | dynamic range control: 0 = off (default), 1 = as the stream says | | `stereo` | opus | downmix surround streams to stereo | +| `sbr` | aac, m4a, the AAC tracks of mp4 / mkv / ts | `false`: decode only the AAC-LC core of HE-AAC (half the rate, mono for HE-AACv2, about 4x faster) | ```luau local tags = assert(F.audio.open(mp3Bytes)).tags -- opening reads the tags; nothing is decoded yet @@ -112,6 +113,19 @@ local bad, err = F.audio.load("not audio at all") assert(bad == nil and err.code == F.Err.UNKNOWN_FORMAT) ``` +### HE-AAC + +HE-AAC (SBR) and HE-AACv2 (SBR + parametric stereo) decode at the full output rate in every AAC path: ADTS, M4A, +and the audio tracks of MP4, Matroska and MPEG-TS (HLS radio). ADTS cannot signal SBR, so the decoder looks for SBR +and PS data in the first frames, as ffmpeg does; `sampleRate`, `channels` and `frames` report the output. + +```luau +local dec = assert(F.audio.open(heAacBytes)) -- HE-AACv2 in ADTS: a 24 kHz mono core +assert(dec.sampleRate == 48000 and dec.channels == 2) -- SBR doubles the rate, PS makes it stereo +local core = assert(F.audio.open(heAacBytes, { sbr = false })) -- the AAC-LC core only, for slow devices +assert(core.sampleRate == 24000 and core.channels == 1 and core.frames * 2 == dec.frames) +``` + ## Editing: `F.ops.audio` Every function returns a new Audio, or `nil, err` for bad arguments. Times are in seconds. Rates and channel counts @@ -192,7 +206,7 @@ Tags are written from `audio.tags`, or from the Decoder's `tags`, wherever the f | Read and write | Read only | |---|---| -| wav (+ RF64), flac (+ Ogg FLAC), mp3 / mp2, vorbis (.ogg), opus, aiff / aifc, au, voc, caf, m4a (ALAC), qoa, adx, vag, adpcm (raw IMA / MS) | aac (ADTS), m4a / caf AAC, mpc (Musepack SV7 / SV8), ac3 (Dolby Digital), brstm / bcstm / bfstm, KVAG, wav / aifc / caf IMA and MS ADPCM, wavpack (.wv + .wvc), ape, tta | +| wav (+ RF64), flac (+ Ogg FLAC), mp3 / mp2, vorbis (.ogg), opus, aiff / aifc, au, voc, caf, m4a (ALAC), qoa, adx, vag, adpcm (raw IMA / MS) | aac (ADTS; AAC-LC, HE-AAC, HE-AACv2), m4a / caf AAC, mpc (Musepack SV7 / SV8), ac3 (Dolby Digital), brstm / bcstm / bfstm, KVAG, wav / aifc / caf IMA and MS ADPCM, wavpack (.wv + .wvc), ape, tta | Each format's capabilities are listed in [FORMATS.md](../FORMATS.md). The audio tracks of video files open as Decoders too: see `v.audio` in [video.md](video.md). Music files (MOD, MIDI, ...) render to a Decoder: see diff --git a/src/Rosetta/audio/caf/init.luau b/src/Rosetta/audio/caf/init.luau index b9bd8ea..9795fd5 100644 --- a/src/Rosetta/audio/caf/init.luau +++ b/src/Rosetta/audio/caf/init.luau @@ -398,6 +398,10 @@ local function source(r: Bytes.Reader, p: Parsed): (Source?, Err.Err?) if not dec then return nil, err end + -- HE-AAC: SBR output only when the description is at the SBR rate (else the AAC-LC core) + if dec.sbrOutput and dec.sampleRate ~= d.rate then + dec = Aac.decoder(asc :: buffer, { sbr = false }) + end local ad = dec :: Aac.Decoder return { channels = ad.channels, diff --git a/tools/fixtures/docs_heaac.py b/tools/fixtures/docs_heaac.py new file mode 100644 index 0000000..52df2b1 --- /dev/null +++ b/tools/fixtures/docs_heaac.py @@ -0,0 +1,29 @@ +# Docs input for the HE-AAC section of docs/guide/audio.md (tools/fixtures.py; tools/docs_check.py reads it): half a +# second of HE-AACv2 (SBR + PS) in ADTS from fdkaac, 48 kHz output from a 24 kHz mono core. +# tests/gen/docs_heaac.luau { files = { ["radio.aac"] = bytes }, vars = { heAacBytes = "radio.aac" } } +import importlib.util, math, os, struct, tempfile + +INPUTS = ["tools/docs_check.py"] + + +def generate(ctx): + spec = importlib.util.spec_from_file_location("docs_heaac_check", os.path.join(ctx.root, "tools", "docs_check.py")) + dc = importlib.util.module_from_spec(spec) + spec.loader.exec_module(dc) + rate, n = 48000, 24000 + ints = [] + for i in range(n): + t = i / rate + for c in range(2): + v = 0.3 * math.sin(2 * math.pi * (330 + 110 * c) * t) + 0.1 * math.sin(2 * math.pi * 7000 * t + c) + ints.append(int(v * 32767)) + data = struct.pack("<%dh" % len(ints), *ints) + fmt = struct.pack("