absorb: merge ws/absorb-opus

This commit is contained in:
4DBug committed 2026-10-31 08:35:16 -05:00
commit ef95d6e51c
17 files changed
+6611 -2

No files matched your search

+12 -1
View File
@@ -1,5 +1,6 @@
-- 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), tools/fixtures/png.py (1024 x 768 images), tools/fixtures/mp3.py.
-- Fixtures: tools/fixtures/deflate.py (a 6 MB corpus), tools/fixtures/png.py (1024 x 768 images), tools/fixtures/mp3.py,
-- tools/fixtures/opus.py (3 s stereo files; MB/s of f32 output, 48 kHz stereo f32 is 0.384 MB/s of real time).
local Deflate = require("@formats/codecs/Deflate")
local Crc32 = require("@formats/core/Crc32")
local Bytes = require("@formats/core/Bytes")
@@ -8,6 +9,10 @@ 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 Opus = require("@formats/audio/opus")
local OpusCelt = require("@tests/gen/opus_celt_st_128")
local OpusSilk = require("@tests/gen/opus_silk_wb_st")
local OpusHybrid = require("@tests/gen/opus_hybrid_st")
local runtime = if (_G :: any).jit ~= nil then "luajit" else "luau"
@@ -73,3 +78,9 @@ for _, name in { "l3_cbr128_js", "l3_vbr_v2_48k", "l2_192_st" } do
end
end
end
for _, c in { { "celt 128k", OpusCelt }, { "silk wb", OpusSilk }, { "hybrid", OpusHybrid } } do
local b = buffer.fromstring(c[2].data)
bench("opus decode " .. c[1] .. " (3 s st)", 3, #c[2].ref, function()
Opus.decode(Bytes.reader(b))
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 mp3 > /dev/null || exit 1
python3 -I tools/fixtures.py deflate png mp3 opus > /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
+66
View File
@@ -0,0 +1,66 @@
# Opus fixtures (tools/fixtures.py): audio/opus. Ported from opus-luau tools/fixtures.sh.
# tests/gen/opus_<name>.luau { name, data = the .opus file, ref = opusdec --float output (f32 LE interleaved) }
# Sources: 48 kHz signals synthesised by ffmpeg (aevalsrc: tones, vibrato, chirps, noise bursts, clicks, a speech-like
# harmonic source with moving formants), encoded by opusenc (CELT-only frame sizes / bitrates, forced SILK NB / MB /
# WB, hybrid SWB / FB, a 5.1 mapping-family-1 stream, a long tagged file) and ffmpeg libopus (CBR, voip, lowdelay).
import os, tempfile
MUSIC = ("0.25*sin(2*PI*(220+30*sin(2*PI*5*t))*t)+0.15*sin(2*PI*(100*t+700*t*t))+0.3*exp(-25*mod(t,0.375))*(random(0)-0.5)"
"+0.4*lt(mod(t,0.5),0.0006)")
MUSIC2 = ("0.2*sin(2*PI*330*t)+0.1*sin(2*PI*1320*t)*lt(mod(t,0.25),0.125)+0.25*exp(-40*mod(t+0.1,0.375))*(random(1)-0.5)"
"+0.12*sin(2*PI*(3000-2500*t/3)*t)")
F0 = "(120+40*sin(2*PI*0.7*t))"
FM = "(600+300*sin(2*PI*1.3*t))"
SPEECH = ("(0.5+0.5*sin(2*PI*2.1*t))*(sin(2*PI*{f0}*t)*0.3+sin(2*PI*2*{f0}*t)*0.25+sin(2*PI*3*{f0}*t)*0.2*exp(-abs(3*{f0}-{fm})/400)"
"+sin(2*PI*5*{f0}*t)*0.15*exp(-abs(5*{f0}-{fm}*2)/400)+sin(2*PI*8*{f0}*t)*0.08+0.02*(random(2)-0.5))").format(f0=F0, fm=FM)
SOURCES = {
"music": (MUSIC + "|" + MUSIC2, 3),
"mono": (MUSIC + "+0.5*" + SPEECH, 3),
"speech": (SPEECH, 3),
"speechst": (SPEECH + "|0.7*" + SPEECH + "+0.1*sin(2*PI*440*t)", 3),
"mixst": (MUSIC + "+" + SPEECH + "|" + MUSIC2 + "+0.6*" + SPEECH, 3),
"surround": (MUSIC + "|" + MUSIC2 + "|" + SPEECH + "|0.3*sin(2*PI*55*t)|0.5*" + MUSIC2 + "|0.5*" + MUSIC, 2),
"long": (MUSIC + "+0.3*" + SPEECH + "|" + MUSIC2 + "+0.3*" + SPEECH, 12),
}
# name, source, encoder ("enc" = opusenc, "ff" = ffmpeg libopus), arguments
CASES = [
("celt_st_128", "music", "enc", ["--music", "--bitrate", "128"]),
("celt_st_32", "music", "enc", ["--music", "--bitrate", "32"]),
("celt_st_64", "mixst", "enc", ["--music", "--bitrate", "64"]),
("celt_st_256", "music", "enc", ["--music", "--bitrate", "256"]),
("celt_mono_64", "mono", "enc", ["--music", "--bitrate", "64"]),
("celt_fs2_5", "music", "enc", ["--music", "--bitrate", "96", "--framesize", "2.5"]),
("celt_fs5", "music", "enc", ["--music", "--bitrate", "96", "--framesize", "5"]),
("celt_fs10", "mixst", "enc", ["--music", "--bitrate", "96", "--framesize", "10"]),
("celt_fs40", "music", "enc", ["--music", "--bitrate", "96", "--framesize", "40"]),
("celt_fs60", "mixst", "enc", ["--music", "--bitrate", "160", "--framesize", "60"]),
("silk_nb", "speech", "enc", ["--speech", "--bitrate", "8", "--max-delay", "0"]),
("silk_mb", "speech", "enc", ["--speech", "--bitrate", "12", "--set-ctl-int", "4008=1102"]),
("silk_wb", "speech", "enc", ["--speech", "--bitrate", "20", "--framesize", "40", "--set-ctl-int", "4008=1103"]),
("silk_wb_st", "speechst", "enc", ["--speech", "--bitrate", "28", "--framesize", "60", "--set-ctl-int", "4008=1103"]),
("silk_nb_st", "speechst", "enc", ["--speech", "--bitrate", "12", "--framesize", "10", "--set-ctl-int", "4008=1101"]),
("hybrid_swb", "speech", "enc", ["--speech", "--bitrate", "24"]),
("hybrid_fb", "speech", "enc", ["--speech", "--bitrate", "36", "--framesize", "10"]),
("hybrid_st", "speechst", "enc", ["--speech", "--bitrate", "40"]),
("mix_st_24", "mixst", "enc", ["--bitrate", "24"]),
("surround51", "surround", "enc", ["--bitrate", "192"]),
("long_128", "long", "enc", ["--bitrate", "128", "--comment", "TITLE=Long Test", "--artist", "opus-luau"]),
("ff_cbr", "mixst", "ff", ["-b:a", "96k", "-vbr", "off"]),
("ff_voip", "speechst", "ff", ["-b:a", "32k", "-application", "voip"]),
("ff_lowdelay", "music", "ff", ["-b:a", "128k", "-application", "lowdelay", "-frame_duration", "5"]),
]
def generate(ctx):
tmp = tempfile.mkdtemp()
for name, (expr, secs) in SOURCES.items():
ctx.nix("ffmpeg", ["ffmpeg", "-loglevel", "error", "-y", "-f", "lavfi", "-i", "aevalsrc='%s':s=48000:d=%d" % (expr, secs),
"-c:a", "pcm_s16le", os.path.join(tmp, name + ".wav")])
for name, src, enc, args in CASES:
wav, opus, ref = (os.path.join(tmp, x) for x in (src + ".wav", name + ".opus", name + ".f32"))
if enc == "enc":
ctx.nix("opus-tools", ["opusenc", "--quiet"] + args + [wav, opus])
else:
ctx.nix("ffmpeg", ["ffmpeg", "-loglevel", "error", "-y", "-i", wav, "-c:a", "libopus"] + args + [opus])
ctx.nix("opus-tools", ["opusdec", "--quiet", "--float", opus, ref])
ctx.emit("opus_" + name, {"name": name, "data": open(opus, "rb").read(), "ref": open(ref, "rb").read()})