ws-audio: Vorbis encoder (audio/vorbis/Encode: long blocks, floor 1 fit to the envelope, square-polar coupling, residue 2 with 6 classes + cascades, fixed Laplacian-Huffman codebooks, exact granules, Ogg via containers/Ogg Mux) wired into audio/vorbis encode; fixtures: ogginfo clean, oggdec exact lengths + SNR floors, ffmpeg; bench line
This commit is contained in:
1 parent
929b5d6d53
commit
cb05744367
7 files changed
+1068
-7
No files matched your search
@@ -160,3 +160,17 @@ do
|
||||
print(string.format("%-8s %-30s %8.1fx real time", runtime, "", 10 / dt))
|
||||
end
|
||||
end
|
||||
-- vorbis encode (ws-audio): 10 s of 44.1 kHz stereo tones at quality 3
|
||||
do
|
||||
local Vb = require("@formats/audio/vorbis")
|
||||
local n = 441000
|
||||
local a = Types.Audio.new(44100, 2, n)
|
||||
for i = 0, n - 1 do
|
||||
buffer.writef32(a.samples, i * 8, math.sin(i * 0.031) * 0.4 + math.sin(i * 0.21) * 0.1)
|
||||
buffer.writef32(a.samples, i * 8 + 4, math.sin(i * 0.017) * 0.3)
|
||||
end
|
||||
local dt = bench("vorbis encode q3 (10 s st)", 2, n * 8, function()
|
||||
Vb.encode(a, { quality = 3 })
|
||||
end)
|
||||
print(string.format("%-8s %-30s %8.1fx real time", runtime, "", 10 / dt))
|
||||
end
|
||||
@@ -0,0 +1,74 @@
|
||||
# Vorbis encoder fixtures (tools/fixtures.py): our encoder runs in the fixture step (tools/fixtures/_audio.py
|
||||
# encode_luau_many) on test signals (qualities -1 .. 10, mono / stereo / 5.1, 8 .. 96 kHz, odd lengths, silence);
|
||||
# oggdec (libvorbisfile) and ffmpeg decode every file, ogginfo checks the stream. Recorded per file: ogginfo's warnings
|
||||
# and errors, oggdec's frame count, the SNR of oggdec's and ffmpeg's decodes against the input (no delay: the
|
||||
# granules are exact).
|
||||
# tests/gen/vorbis_enc.luau { name, sig, quality, rate, channels, frames, ogginfo, oggdecFrames, oggdecSnr,
|
||||
# ffFrames, ffSnr, ffErrors, kbps, data }
|
||||
# tests/gen/vorbis_enc_sigs.luau { [sig] = { rate, channels, frames, data (f32le) } }
|
||||
import importlib.util, os, re
|
||||
|
||||
_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)
|
||||
|
||||
INPUTS = ["src/Formats/audio/vorbis", "src/Formats/codecs", "src/Formats/containers/Ogg"]
|
||||
|
||||
|
||||
WAVE_FROM_VORBIS6 = [0, 2, 1, 4, 5, 3] # vorbis channel k is WAVE channel WAVE_FROM_VORBIS6[k]
|
||||
|
||||
|
||||
def reorder(x):
|
||||
out = [0.0] * len(x)
|
||||
for i in range(0, len(x) - 5, 6):
|
||||
for k in range(6):
|
||||
out[i + k] = x[i + WAVE_FROM_VORBIS6[k]]
|
||||
return out
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
sigs = {}
|
||||
|
||||
def sig(kind, ch, frames, rate):
|
||||
name = "%s_%d_%d_%d" % (kind, ch, frames, rate)
|
||||
if name not in sigs:
|
||||
sigs[name] = (A.from_f32le(A.f32le(A.signal(kind, rate, ch, frames, seed=frames % 1000 + ch))), rate, ch)
|
||||
return name
|
||||
|
||||
cases = []
|
||||
for q in (-1, 0, 3, 6, 10):
|
||||
cases.append(("music-st-44k-q%d" % q, sig("music", 2, 44100, 44100), q))
|
||||
cases.append(("sine-st-44k-q3", sig("sine", 2, 30000, 44100), 3))
|
||||
cases.append(("sweep-mono-48k-q3", sig("sweep", 1, 40000, 48000), 3))
|
||||
cases.append(("noise-mono-22k-q5", sig("noise", 1, 15000, 22050), 5))
|
||||
cases.append(("music-mono-8k-q2", sig("music", 1, 9000, 8000), 2))
|
||||
cases.append(("music-st-96k-q4", sig("music", 2, 30000, 96000), 4))
|
||||
cases.append(("music-6ch-48k-q3", sig("music", 6, 20000, 48000), 3))
|
||||
cases.append(("square-st-32k-q5", sig("square", 2, 20000, 32000), 5))
|
||||
cases.append(("loud-st-44k-q8", sig("loud", 2, 20000, 44100), 8))
|
||||
cases.append(("silence-st-44k", sig("silence", 2, 10000, 44100), 3))
|
||||
for n in (1, 100, 1024, 1025, 3000):
|
||||
cases.append(("len-%d" % n, sig("music", 2, n, 44100), 3))
|
||||
ctx.emit("vorbis_enc_sigs", {n: {"rate": r, "channels": c, "frames": len(x) // c, "data": A.f32le(x)} for n, (x, r, c) in sigs.items()})
|
||||
outs = A.encode_luau_many(ctx, "audio/vorbis", [(c[1], "{ quality = %d }" % c[2]) for c in cases], "vorbis_enc_sigs")
|
||||
emitted = []
|
||||
for (name, sigmod, q), data in zip(cases, outs):
|
||||
samples, rate, ch = sigs[sigmod]
|
||||
frames = len(samples) // ch
|
||||
info = A.tool(ctx, "vorbis-tools", ["sh", "-c", "ogginfo {in} 2>&1; true"], data, "ogg").decode(errors="replace")
|
||||
problems = "\n".join(l for l in info.split("\n") if re.search(r"(?i)warning|error", l))
|
||||
raw = A.tool(ctx, "vorbis-tools", ["oggdec", "-Q", "-R", "-o", "{out}", "{in}"], data, "ogg", "raw")
|
||||
od = [int.from_bytes(raw[i:i + 2], "little", signed=True) / 32768 for i in range(0, len(raw) - 1, 2)]
|
||||
osnr, _ = A.snr(samples, od, ch) # (oggdec -R keeps the Vorbis channel order)
|
||||
ffe = A.tool(ctx, "ffmpeg", ["ffmpeg", "-v", "error", "-i", "{in}", "-f", "null", "-"], data, "ogg").decode(errors="replace")
|
||||
_, fch, ff = A.ffdecode(ctx, data, ext="ogg")
|
||||
if ch == 6: # ffmpeg reorders to WAVE order (FL FR FC LFE RL RR); Vorbis order is FL FC FR RL RR LFE
|
||||
ff = reorder(ff)
|
||||
fsnr, _ = A.snr(samples, ff, ch) if fch == ch else (-99.0, 0)
|
||||
e = {"name": name, "sig": sigmod, "quality": q, "rate": rate, "channels": ch, "frames": frames, "ogginfo": problems,
|
||||
"oggdecFrames": len(od) // ch, "oggdecSnr": osnr, "ffFrames": len(ff) // max(1, fch), "ffSnr": fsnr,
|
||||
"ffErrors": ffe, "kbps": len(data) * 8 / (frames / rate) / 1000, "data": data}
|
||||
emitted.append(e)
|
||||
print(" vorbis_enc: %-22s %7.1f kbps oggdec %d/%d frames %.1f dB ffmpeg %.1f dB%s" % (name, e["kbps"], e["oggdecFrames"], frames,
|
||||
osnr, fsnr, (" OGGINFO " + problems[:80]) if problems else ""))
|
||||
ctx.emit("vorbis_enc", emitted)
|
||||
Reference in new issue
Block a user