absorb: merge ws/absorb-mp3

This commit is contained in:
4DBug committed 2026-10-31 08:35:04 -05:00
commit d5372992e7
45 files changed
+3608 -2

No files matched your search

+18 -1
View File
@@ -1,11 +1,13 @@
-- Throughput (bash tools/bench.sh): codec MB/s under both runtimes. MB/s are of decompressed output, best of n runs.
-- Fixtures: tools/fixtures/deflate.py (a 6 MB corpus) and tools/fixtures/png.py (1024 x 768 images).
-- Fixtures: tools/fixtures/deflate.py (a 6 MB corpus), tools/fixtures/png.py (1024 x 768 images), tools/fixtures/mp3.py.
local Deflate = require("@formats/codecs/Deflate")
local Crc32 = require("@formats/core/Crc32")
local Bytes = require("@formats/core/Bytes")
local Png = require("@formats/image/png")
local B = require("@tests/gen/deflate_bench")
local PB = require("@tests/gen/png_bench")
local Mp3 = require("@formats/audio/mp3")
local MC = require("@tests/gen/mp3_cases")
local runtime = if (_G :: any).jit ~= nil then "luajit" else "luau"
@@ -18,6 +20,7 @@ local function bench(label: string, n: number, bytes: number, f: () -> ())
dt = math.min(dt, os.clock() - t0)
end
print(string.format("%-8s %-30s %8.2f ms %7.1f MB/s", runtime, label, dt * 1000, bytes / dt / 1e6))
return dt
end
local z = buffer.fromstring(B.zlib)
@@ -56,3 +59,17 @@ do
Png.encode(img)
end)
end
for _, name in { "l3_cbr128_js", "l3_vbr_v2_48k", "l2_192_st" } do
for _, c in MC do
if c.name == name then
local b = buffer.fromstring(c.data)
local dec = assert(Mp3.open(Bytes.reader(b)))
local secs = dec.duration
local dt = bench("mp3 decode " .. name, 5, dec.frames * dec.channels * 4, function()
local d = assert(Mp3.open(Bytes.reader(b)))
d:readAll()
end)
print(string.format("%-8s %-30s %8.0fx real time", runtime, "", secs / dt))
end
end
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 > /dev/null || exit 1
python3 -I tools/fixtures.py deflate png mp3 > /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
+75
View File
@@ -0,0 +1,75 @@
# MP3 fixtures (tools/fixtures.py). Ported from mp3-luau tools/make_fixtures.sh + make_refs.py.
# tests/gen/mp3_cases.luau the committed streams in tests/fixtures/mp3/ (made by ffmpeg's libmp3lame / mp2 encoders
# from a synthetic test signal: tones, a chirp, noise, clicks) with their minimp3 reference
# decodes (*.ref, from the minimp3 C harness: see the format below), plus streams lame
# encodes here; each with ffmpeg's decode of three windows and its length.
# .ref layout (little-endian): "MP3REF1\0", u32 hz, channels, frames, window frames W, window count N, then
# N x (u32 start frame, W x channels f32), then u32 block size B, u32 block count, f64 sum of squares per block.
import math, os, random, struct, tempfile
INPUTS = ["tests/fixtures/mp3"]
DIR = os.path.join("tests", "fixtures", "mp3")
def ffmpeg_windows(ctx, data, ch, starts, W):
with tempfile.TemporaryDirectory() as tmp: # a file, not a pipe: ffmpeg applies the LAME gapless info only when it can seek
path = os.path.join(tmp, "in.mp3")
with open(path, "wb") as f:
f.write(data)
out = ctx.nix("ffmpeg", ["ffmpeg", "-v", "error", "-i", path, "-f", "f32le", "-ac", str(ch), "pipe:1"])
frames = len(out) // (4 * ch)
wins = []
for s in starts:
s = max(0, min(frames - W, s))
wins.append({"start": s, "pcm": out[s * ch * 4:(s + W) * ch * 4]})
return frames, wins
def ref_windows(ref):
hz, ch, frames, W, N = struct.unpack_from("<5I", ref, 8)
p, starts = 28, []
for _ in range(N):
starts.append(struct.unpack_from("<I", ref, p)[0])
p += 4 + W * ch * 4
return ch, W, starts
def wav(rate, ch, secs, seed):
rnd = random.Random(seed)
fr = bytearray()
for i in range(int(secs * rate)):
t = i / rate
for c in range(ch):
v = 0.25 * math.sin(2 * math.pi * (330 * (c + 1) + 200 * t) * t) + 0.04 * (rnd.random() - 0.5)
if (i // 2000) % 7 == 3:
v += 0.3 * math.sin(2 * math.pi * 4000 * t) * (1 - (i % 2000) / 2000)
fr += struct.pack("<h", int(max(-1, min(1, v)) * 32767))
hdr = b"RIFF" + struct.pack("<I", 36 + len(fr)) + b"WAVEfmt " + struct.pack("<IHHIIHH", 16, 1, ch, rate, rate * ch * 2, ch * 2, 16)
return hdr + b"data" + struct.pack("<I", len(fr)) + bytes(fr)
def generate(ctx):
cases = []
for fn in sorted(os.listdir(os.path.join(ctx.root, DIR))):
if not fn.endswith((".mp3", ".mp2")):
continue
name = fn[:-4]
data = ctx.read(os.path.join(DIR, fn))
ref = ctx.read(os.path.join(DIR, name + ".ref"))
ch, W, starts = ref_windows(ref)
frames, wins = ffmpeg_windows(ctx, data, ch, starts, W)
cases.append({"name": name, "data": data, "ref": ref, "ffFrames": frames, "ff": wins})
# streams the lame CLI writes here (no minimp3 reference: ffmpeg only)
for name, rate, ch, args in (("lame_v5_st", 44100, 2, ["-V", "5"]), ("lame_cbr64_mono_22k", 22050, 1, ["-b", "64", "-m", "m"]),
("lame_abr96_48k", 48000, 2, ["--abr", "96"])):
src = wav(rate, ch, 1.2, len(name))
with tempfile.TemporaryDirectory() as tmp: # a file: lame writes the LAME (gapless) tag only when it can seek
path = os.path.join(tmp, "out.mp3")
ctx.nix("lame", ["lame", "--quiet", "--resample", str(rate / 1000)] + args + ["-", path], input=src)
with open(path, "rb") as f:
data = f.read()
W = 1152
frames, wins = ffmpeg_windows(ctx, data, ch, [0, 20000, 10 ** 9], W)
cases.append({"name": name, "data": data, "ffFrames": frames, "ff": wins, "channels": ch, "rate": rate,
"srcFrames": int(1.2 * rate)})
ctx.emit("mp3_cases", cases)