Files
rosetta/tools/fixtures/vorbis_enc.py
T
4DBugandClaude Opus 5.5 fb2dd98db2 ws-cleanup: consistency (lower-case tags, WAVE channel order, loops = play count, Err messages, caps)
- Audio tags keys are lower case everywhere (Types.Tags): FLAC no longer returns TITLE / ARTIST (the raw fields stay
  in meta.source.comments; also for Vorbis and Opus); Vorbis / Opus / FLAC read and write tags through
  codecs/VorbisComment (TRACKNUMBER -> track, DISCNUMBER -> disc, DESCRIPTION -> comment); MP3 albumArtist ->
  albumartist and year -> date (ID3 TYER / TDRC, ID3v1); RIFF INFO / CAF / MP4 freeform names are lower-cased (MP4
  writes iTunSMPB / iTunNORM back in their usual spelling; m4a reads tags.itunsmpb).
- Channel order: Vorbis (and Opus mapping family 1) decode to WAVE order like FLAC / WAV / AAC (both used to return
  the Vorbis order); the Vorbis encoder takes WAVE order and writes Vorbis order. Pcm.WAVE_FROM_VORBIS / Pcm.reorder;
  Vorbis.packetDecoder gives pd.order. Tests reorder oggdec's / opusdec's raw (Vorbis-order) references.
- img.loops is the play count in GIF, APNG and WebP (they already agreed; Types / WebP docs said "loop count");
  tests/api round-trips loops 0 / 1 / 3 through all three.
- Err: messages without the format name (png / midi encoders said "png encode: ..."); codecs/Bzip2 and codecs/Zstd
  check arguments through Bytes.span.
- caps: mp3 encodeLayers = { 2, 3 }, opus encodeChannels = 2.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-31 11:01:22 -05:00

77 lines
4.5 KiB
Python

# 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 to_wave(x):
"""6-channel samples in Vorbis order -> WAVE order"""
out = [0.0] * len(x)
for i in range(0, len(x) - 5, 6):
for k in range(6):
out[i + WAVE_FROM_VORBIS6[k]] = x[i + 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)]
if ch == 6: # the input is in WAVE order (FL FR FC LFE RL RR); oggdec -R keeps the Vorbis order (FL FC FR RL RR LFE)
od = to_wave(od)
osnr, _ = A.snr(samples, od, ch)
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")
# (ffmpeg decodes to WAVE order, like our decoder)
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)