From 2d99b9924afaad304c2969e509295d7afe82b886 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 14:48:22 -0500 Subject: [PATCH] ws-audio2: Musepack / AC-3 bench lines, docs/guide/audio.md section (+ tools/fixtures/docs_lossy.py inputs), AVI AC-3 track test --- docs/guide/audio.md | 26 ++++++++++++++++++++++-- tests/audio/ac3.luau | 2 +- tools/bench.luau | 28 ++++++++++++++++++++++++++ tools/fixtures/ac3.py | 5 +++-- tools/fixtures/docs_lossy.py | 39 ++++++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 tools/fixtures/docs_lossy.py diff --git a/docs/guide/audio.md b/docs/guide/audio.md index 94271f6..8af0240 100644 --- a/docs/guide/audio.md +++ b/docs/guide/audio.md @@ -99,7 +99,8 @@ the guide's [README](README.md#inputs)). Options: | Option | Formats | Meaning | |---|---|---| | `format` | all | decode as this format (`"mp3"`, `"wav"`, ...) and skip detection | -| `maxFrames` | wav, flac, mp3, aiff, au, voc, aac | `Err.LIMIT` past this many frames | +| `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 | ```luau @@ -189,7 +190,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) | aac (ADTS), m4a / caf AAC | +| wav (+ RF64), flac (+ Ogg FLAC), mp3 / mp2, vorbis (.ogg), opus, aiff / aifc, au, voc, caf, m4a (ALAC) | aac (ADTS), m4a / caf AAC, mpc (Musepack SV7 / SV8), ac3 (Dolby Digital) | 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 @@ -243,6 +244,27 @@ end Roblox has no API to play raw samples, so on Roblox the decoded samples are data only, for example for analysis or visualisers. +### Musepack and AC-3 + +Musepack (`.mpc`, stream versions 7 and 8) decodes gapless, with its APEv2 tags. AC-3 (`.ac3`, Dolby Digital) decodes +every channel layout up to 5.1 without downmixing, in WAVE order (FL FR FC LFE SL SR), with dynamic range control off +unless `drc` asks for it. The AC-3 tracks of MKV, MP4, MPEG-TS and AVI files open as `v.audio` the same way. Both +seek exactly (Musepack SV7 like libmpcdec: 32 frames of pre-roll). E-AC-3 (Dolby Digital Plus) is `Err.UNSUPPORTED`. + +```luau +local mpc = assert(F.audio.load(mpcBytes)) -- Musepack: tags, exact length +assert(F.detect(mpcBytes) == "mpc" and mpc.tags.title == "Docs Tone") +assert(mpc.frames == 44100 and mpc.meta.source.version == 8) +local dd = assert(F.audio.open(ac3Bytes)) -- AC-3 5.1: a Decoder like any other +assert(dd.channels == 6 and dd.sampleRate == 48000) +dd:seek(0.25) +local _, n = dd:read(256) +assert(n == 256) +local stereo = assert(F.ops.audio.downmix(F.audio.load(ac3Bytes))) -- 2 channels, ITU-R BS.775 +local night = assert(F.audio.load(ac3Bytes, { drc = 1 })) -- the stream's dynamic range gains applied +assert(stereo.channels == 2 and night.channels == 6) +``` + ## Gotchas - `F.ops.audio.downmix(a)` defaults to **2** channels, so a stereo input stays stereo. Use `downmix(a, 1)` for mono. diff --git a/tests/audio/ac3.luau b/tests/audio/ac3.luau index 6f00ebd..447c959 100644 --- a/tests/audio/ac3.luau +++ b/tests/audio/ac3.luau @@ -2,7 +2,7 @@ -- 32 / 44.1 / 48 kHz) and aften (block switching, 1+1 dual mono, dynamic range profiles, the alternate BSI) against -- ffmpeg's float decode, or liba52's for block-switched streams (SNR, max difference; dithflags cleared in the -- fixture step so both sides decode the same numbers); dynamic range on; the encoders' dithered originals; AC-3 tracks --- of MKV / MP4 / MPEG-TS through the api's audio hook; exact seeking; resync and truncation; error codes (E-AC-3, +-- of MKV / MP4 / MPEG-TS / AVI through the api's audio hook; exact seeking; resync and truncation; error codes (E-AC-3, -- CRC); the fuzz no-throw pass. Fixtures: tools/fixtures/ac3.py. local T = require("@tests/T") local F = require("@formats") diff --git a/tools/bench.luau b/tools/bench.luau index b32ee01..bacd38a 100644 --- a/tools/bench.luau +++ b/tools/bench.luau @@ -1078,3 +1078,31 @@ do jitTune(false) end end +-- ws-audio2: lossy +do + -- Musepack (codecs/audio/Musepack) SV8 / SV7 and AC-3 5.1 (codecs/audio/Ac3): whole-file decodes, MB/s of f32 output + -- (tools/fixtures/mpc.py: 1.2 s of 44.1 kHz stereo at quality 5; tools/fixtures/ac3.py: 0.16 s of 48 kHz 5.1 at + -- 448 kbps, decoded 4 times per run) + local Mpc = require("@formats/audio/mpc") + local MpcFx = require("@tests/gen/mpc_cases") + local Ac3F = require("@formats/audio/ac3") + local Ac3Fx = require("@tests/gen/ac3_cases") + for _, c in MpcFx do + if c.name == "sv8_q5_stereo" or c.name == "sv7_q5_stereo" then + local b = buffer.fromstring(c.data) + bench("mpc " .. c.name, 5, c.frames * c.channels * 4, function() + assert(Mpc.decode(Bytes.reader(b))) + end) + end + end + for _, c in Ac3Fx do + if c.name == "5.1_448" then + local b = buffer.fromstring(c.data) + bench("ac3 5.1 448 kbps", 5, c.frames * c.channels * 4 * 4, function() + for _ = 1, 4 do + assert(Ac3F.decode(Bytes.reader(b))) + end + end) + end + end +end diff --git a/tools/fixtures/ac3.py b/tools/fixtures/ac3.py index f7cba37..a0c394e 100644 --- a/tools/fixtures/ac3.py +++ b/tools/fixtures/ac3.py @@ -3,7 +3,7 @@ # References: ffmpeg's float decode with -drc_scale 0 (dynamic range off, our default), except for block-switched # streams: ffmpeg (as packaged) mis-decodes the first of the two short transforms, so those use liba52's a52dec # (-r -a: no dynamic range, no level adjustment; float for stereo, 16-bit wav6 for more channels), which agrees with -# the A/52 transform equations. AC-3 tracks in Matroska / MP4 / MPEG-TS (remuxed by ffmpeg) for the api's audio hook. +# the A/52 transform equations. AC-3 tracks in Matroska / MP4 / MPEG-TS / AVI (remuxed by ffmpeg) for the api's audio hook. # # Both encoders set dithflag, and decoders draw their own random dither for the zero-bit mantissas, so two correct # decoders differ by about the coding noise. The reference streams therefore have their dithflags cleared (the bit @@ -214,7 +214,8 @@ def generate(ctx): with open(os.path.join(d, "t.raw"), "wb") as f: f.write(raw(sig)) for name, video, layout in (("ac3.mkv", "-c:v mjpeg -pix_fmt yuvj420p", "5.1(side)"), - ("ac3.mp4", "-c:v mjpeg -pix_fmt yuvj420p", "stereo"), ("ac3.ts", "-c:v mpeg1video", "stereo")): + ("ac3.mp4", "-c:v mjpeg -pix_fmt yuvj420p", "stereo"), ("ac3.ts", "-c:v mpeg1video", "stereo"), + ("ac3.avi", "-c:v mjpeg -pix_fmt yuvj420p", "stereo")): run("ffmpeg -v error -y -f f32le -ar 48000 -ch_layout '5.1(side)' -i t.raw -af aformat=channel_layouts='%s' " "-c:a ac3 -b:a 192k t.ac3" % layout) with open(os.path.join(d, "t.ac3"), "rb") as f: diff --git a/tools/fixtures/docs_lossy.py b/tools/fixtures/docs_lossy.py new file mode 100644 index 0000000..42f6f50 --- /dev/null +++ b/tools/fixtures/docs_lossy.py @@ -0,0 +1,39 @@ +# Docs inputs for the Musepack / AC-3 section of docs/guide/audio.md (tools/docs_check.py): a tagged stereo tone as +# Musepack SV8 (mpcenc, built by tools/fixtures/mpc.py's nix expression) and a 5.1 tone as AC-3 (ffmpeg). +# tests/gen/docs_lossy.luau { files = { [name] = bytes }, vars = { mpcBytes = "tone.mpc", ac3Bytes = "tone.ac3" } } +import importlib.util, math, os, shutil, struct, subprocess, tempfile + +INPUTS = ["tools/docs_check.py", "tools/fixtures/mpc.py"] + + +def load(ctx, rel, name): + spec = importlib.util.spec_from_file_location(name, os.path.join(ctx.root, rel)) + m = importlib.util.module_from_spec(spec) + spec.loader.exec_module(m) + return m + + +def generate(ctx): + dc = load(ctx, "tools/docs_check.py", "docs_check") + mpc = load(ctx, "tools/fixtures/mpc.py", "docs_lossy_mpc") + d = tempfile.mkdtemp(prefix="fmt-docs-lossy-") + try: + rate, n = 44100, 44100 + st = [0.3 * math.sin(2 * math.pi * f * i / rate) for i in range(n) for f in (440, 660)] + with open(os.path.join(d, "tone.wav"), "wb") as f: + f.write(mpc.A.wav(st, rate, 2)) + subprocess.run([os.path.join(mpc.tools(), "mpcenc"), "--silent", "--overwrite", "--quality", "4", + "--tag", "Title=Docs Tone", "--artist", "formats-luau", os.path.join(d, "tone.wav"), + os.path.join(d, "tone.mpc")], capture_output=True, check=True) + six = [0.2 * math.sin(2 * math.pi * (200 + 100 * c) * i / 48000) for i in range(24000) for c in range(6)] + with open(os.path.join(d, "six.raw"), "wb") as f: + f.write(struct.pack("<%df" % len(six), *six)) + ctx.nix("ffmpeg", ["sh", "-c", "cd '%s' && ffmpeg -v error -y -f f32le -ar 48000 -ch_layout '5.1(side)' -i six.raw " + "-c:a ac3 -b:a 384k tone.ac3" % d]) + files = {} + for name in ("tone.mpc", "tone.ac3"): + with open(os.path.join(d, name), "rb") as f: + files[name] = f.read() + finally: + shutil.rmtree(d, ignore_errors=True) + dc.emit_inputs(ctx, files, {"mpcBytes": "tone.mpc", "ac3Bytes": "tone.ac3"})