ws-audio: audio/vorbis decoder: stb_vorbis 1.22 port (setup, codebooks, floor 1, residue 0/1/2, coupling) on containers/Ogg + codecs/Mdct, libvorbis-exact trimming, granule-exact seek, packetDecoder for other containers; tests vs oggdec (counts, 1 LSB) + ffmpeg (SNR 138 dB), seeks, fuzz; bench line

This commit is contained in:
4DBug committed 2026-10-31 08:53:10 -05:00
1 parent bc061743da
commit a74033e171
13 files changed
+2210 -1

No files matched your search

+10
View File
@@ -84,3 +84,13 @@ for _, c in { { "celt 128k", OpusCelt }, { "silk wb", OpusSilk }, { "hybrid", Op
Opus.decode(Bytes.reader(b))
end)
end
-- vorbis (ws-audio): 10 s of 44.1 kHz stereo music at oggenc -q 5 (tools/fixtures/vorbis.py)
do
local Vorbis = require("@formats/audio/vorbis")
local VB = require("@tests/gen/vorbis_bench")
local b = buffer.fromstring(VB.data)
local dt = bench("vorbis decode q5 (10 s st)", 3, VB.frames * VB.channels * 4, function()
Vorbis.decode(Bytes.reader(b))
end)
print(string.format("%-8s %-30s %8.0fx real time", runtime, "", VB.frames / VB.rate / dt))
end
+1 -1
View File
@@ -3,7 +3,7 @@
cd "$(dirname "$0")/.." || exit 1
LUAU=${LUAU:-$(command -v luau || nix shell nixpkgs#luau -c sh -c 'command -v luau')}
LUAJIT=${LUAJIT:-$(command -v luajit || nix shell nixpkgs#luajit -c sh -c 'command -v luajit')}
python3 -I tools/fixtures.py deflate png mp3 opus > /dev/null || exit 1
python3 -I tools/fixtures.py deflate png mp3 opus vorbis > /dev/null || exit 1
python3 -I tools/build.py luajit > /dev/null || exit 1
"$LUAU" --codegen -O2 tools/bench.luau
"$LUAJIT" -e 'package.path = "build/luajit/?.lua;build/luajit/?/init.lua;" .. package.path' build/luajit/tools/bench.lua
+57
View File
@@ -0,0 +1,57 @@
# Ogg Vorbis fixtures (tools/fixtures.py): files made by oggenc (libvorbis, vorbis-tools) and ffmpeg (libvorbis) from
# the test signals in tools/fixtures/_audio.py: qualities -1 .. 10, managed bitrate, 8 .. 96 kHz, mono / stereo / 5.1,
# transients (block switching), silence, tags, a stream shorter than one block. References: oggdec (libvorbisfile:
# the authority on start / end trimming) as 16-bit samples, and ffmpeg's f32 decode (more precise, but its Ogg demuxer
# trims some streams differently: the test uses it only when its frame count agrees with oggdec).
# tests/gen/vorbis_cases.luau { name, data, rate, channels, frames (oggdec), ref16 (s16le, oggdec), ffFrames,
# ffRef (f32le, ffmpeg), inputFrames }
import importlib.util, os
_spec = importlib.util.spec_from_file_location("_audio", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_audio.py"))
A = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(A)
CASES = [
# name, signal, rate, channels, seconds, tool, args
("q5-stereo-44k-music", "music", 44100, 2, 1.5, "oggenc", ["-q", "5"]),
("q-1-mono-8k-music", "music", 8000, 1, 1.0, "oggenc", ["-q", "-1"]),
("q10-stereo-48k-sweep", "sweep", 48000, 2, 1.0, "oggenc", ["-q", "10"]),
("q3-mono-22k-noise", "noise", 22050, 1, 0.7, "oggenc", ["-q", "3"]),
("q0-stereo-44k-square", "square", 44100, 2, 1.0, "oggenc", ["-q", "0"]),
("q2-stereo-32k-tags", "sine", 32000, 2, 0.6, "oggenc", ["-q", "2", "-t", "A Title", "-a", "An Artist", "-l", "The Album", "-N", "7", "-c", "comment=hello vorbis"]),
("managed-64k-stereo", "music", 44100, 2, 1.0, "oggenc", ["--managed", "-b", "64"]),
("silence-stereo", "silence", 44100, 2, 0.5, "oggenc", ["-q", "4"]),
("short-100", "sine", 44100, 2, 100 / 44100, "oggenc", ["-q", "4"]),
("short-3000-mono", "music", 44100, 1, 3000 / 44100, "oggenc", ["-q", "4"]),
("q6-stereo-96k", "music", 96000, 2, 0.4, "oggenc", ["-q", "6"]),
("ffmpeg-5.1-48k", "music", 48000, 6, 0.6, "ffmpeg", ["-c:a", "libvorbis", "-q:a", "4"]),
("ffmpeg-mono-16k", "sweep", 16000, 1, 0.8, "ffmpeg", ["-c:a", "libvorbis", "-q:a", "1"]),
("loud-stereo", "loud", 44100, 2, 0.5, "oggenc", ["-q", "8"]),
]
INPUTS = []
def generate(ctx):
cases = []
bench = None
for name, kind, rate, ch, secs, tool, args in CASES:
frames = max(1, int(round(secs * rate)))
sig = A.signal(kind, rate, ch, frames, seed=len(name))
wav = A.wav(sig, rate, ch, float_=True) if tool == "ffmpeg" else A.wav(sig, rate, ch, 16)
if tool == "oggenc":
data = A.tool(ctx, "vorbis-tools", ["oggenc", "-Q", "--serial", "1234"] + args + ["-o", "{out}", "{in}"], wav, "wav", "ogg")
else:
data = A.tool(ctx, "ffmpeg", ["ffmpeg", "-v", "error", "-i", "{in}"] + args + ["-f", "ogg", "-y", "{out}"], wav, "wav", "ogg")
rrate, rch, ref = A.ffdecode(ctx, data, ext="ogg")
raw = A.tool(ctx, "vorbis-tools", ["oggdec", "-Q", "-R", "-o", "{out}", "{in}"], data, "ogg", "raw")
case = {"name": name, "data": data, "rate": rrate, "channels": rch, "frames": len(raw) // (2 * rch), "ref16": raw,
"ffFrames": len(ref) // rch, "inputFrames": frames}
if len(ref) // rch == len(raw) // (2 * rch):
case["ffRef"] = A.f32le(ref)
cases.append(case)
ctx.emit("vorbis_cases", cases)
# bench: 10 s of stereo music at q5
sig = A.signal("music", 44100, 2, 441000, seed=9)
data = A.tool(ctx, "vorbis-tools", ["oggenc", "-Q", "-q", "5", "-o", "{out}", "{in}"], A.wav(sig, 44100, 2, 16), "wav", "ogg")
ctx.emit("vorbis_bench", {"data": data, "frames": 441000, "rate": 44100, "channels": 2})