C2, the hierarchy (tools/move.py, every reference rewritten mechanically): - codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder; codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url; codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err Crc32 Adler32 Base64 Md5 Input Sniff. - LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md). - Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes. C3, generic code out of format folders: - containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first, trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64 through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it. - codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position, priming and end trimming) that aac, caf and m4a each carried (~70 lines each). - codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of its own (the AND mask / alpha rule stay ICO's). - codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model), Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the fixtures' bind shapes). - README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes: FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93. Co-Authored-By: Claude Opus 5.5 <[email protected]>
81 lines
5.4 KiB
Python
81 lines
5.4 KiB
Python
# MP3 / MP2 encoder fixtures (tools/fixtures.py): our layer III and layer II encoders run in the fixture step
|
|
# (tools/fixtures/_audio.py encode_luau_many) on test signals at every MPEG sample rate group, several bit rates and
|
|
# modes; ffmpeg (mp3float / mp2float) and LAME (`lame --decode`, layer III) decode the files. Recorded per file: ffmpeg's
|
|
# error output (must be empty), the decoded frame counts, and the SNR against the input after aligning the decoder delay.
|
|
# tests/gen/mp3_enc.luau { name, sig, layer, bitrate, mode, rate, channels, frames, ffErrors, ffFrames, ffSnr,
|
|
# ffLag, lameFrames?, lameSnr?, size, data }
|
|
# tests/gen/mp3_enc_sigs.luau { [sig] = { rate, channels, frames, data (f32le) } }
|
|
import importlib.util, io, os, wave
|
|
|
|
_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/mp3", "src/Formats/codecs/audio/Pcm"]
|
|
|
|
|
|
def generate(ctx):
|
|
sigs = {}
|
|
|
|
def sig(kind, ch, secs, rate):
|
|
name = "%s_%d_%d" % (kind, ch, rate)
|
|
if name not in sigs:
|
|
frames = int(secs * rate)
|
|
sigs[name] = (A.from_f32le(A.f32le(A.signal(kind, rate, ch, frames, seed=ch * 7 + rate % 97))), rate, ch)
|
|
return name
|
|
|
|
cases = [] # name, sig, layer, bitrate, mode
|
|
for kind in ("music", "sine", "sweep"):
|
|
for br in (64, 128, 192, 320):
|
|
cases.append(("l3-%s-44k-%d-joint" % (kind, br), sig(kind, 2, 1.0, 44100), 3, br, "joint"))
|
|
cases.append(("l3-music-44k-128-stereo", sig("music", 2, 1.0, 44100), 3, 128, "stereo"))
|
|
cases.append(("l3-music-48k-160-joint", sig("music", 2, 1.0, 48000), 3, 160, "joint"))
|
|
cases.append(("l3-music-32k-96-mono", sig("music", 1, 1.0, 32000), 3, 96, "mono"))
|
|
cases.append(("l3-noise-44k-128-mono", sig("noise", 1, 0.6, 44100), 3, 128, "mono"))
|
|
cases.append(("l3-music-22k-64-joint", sig("music", 2, 1.0, 22050), 3, 64, "joint"))
|
|
cases.append(("l3-music-24k-96-stereo", sig("music", 2, 1.0, 24000), 3, 96, "stereo"))
|
|
cases.append(("l3-sweep-16k-48-mono", sig("sweep", 1, 1.0, 16000), 3, 48, "mono"))
|
|
cases.append(("l3-music-11k-32-mono", sig("music", 1, 1.0, 11025), 3, 32, "mono"))
|
|
cases.append(("l3-music-8k-24-joint", sig("music", 2, 1.0, 8000), 3, 24, "joint"))
|
|
cases.append(("l3-music-12k-64-stereo", sig("music", 2, 1.0, 12000), 3, 64, "stereo"))
|
|
cases.append(("l3-silence-44k-128", sig("silence", 2, 0.3, 44100), 3, 128, "joint"))
|
|
cases.append(("l3-loud-44k-128", sig("loud", 2, 0.5, 44100), 3, 128, "joint"))
|
|
for kind in ("music", "sine", "sweep"):
|
|
for br in (128, 192, 256, 384):
|
|
cases.append(("l2-%s-44k-%d" % (kind, br), sig(kind, 2, 1.0, 44100), 2, br, "stereo"))
|
|
cases.append(("l2-music-48k-192", sig("music", 2, 1.0, 48000), 2, 192, "stereo"))
|
|
cases.append(("l2-music-32k-mono-96", sig("music", 1, 1.0, 32000), 2, 96, "mono"))
|
|
cases.append(("l2-music-44k-mono-64", sig("music", 1, 1.0, 44100), 2, 64, "mono"))
|
|
cases.append(("l2-music-24k-96", sig("music", 2, 1.0, 24000), 2, 96, "stereo"))
|
|
cases.append(("l2-sweep-16k-mono-48", sig("sweep", 1, 1.0, 16000), 2, 48, "mono"))
|
|
cases.append(("l2-loud-44k-192", sig("loud", 2, 0.5, 44100), 2, 192, "stereo"))
|
|
|
|
ctx.emit("mp3_enc_sigs", {n: {"rate": r, "channels": c, "frames": len(x) // c, "data": A.f32le(x)} for n, (x, r, c) in sigs.items()})
|
|
jobs = [(c[1], '{ layer = %d, bitrate = %d, mode = "%s" }' % (c[2], c[3], c[4])) for c in cases]
|
|
outs = A.encode_luau_many(ctx, "audio/mp3", jobs, "mp3_enc_sigs")
|
|
emitted = []
|
|
for (name, sigmod, layer, br, mode), data in zip(cases, outs):
|
|
samples, rate, ch = sigs[sigmod]
|
|
ext = "mp3" if layer == 3 else "mp2"
|
|
errs = A.tool(ctx, "ffmpeg", ["ffmpeg", "-v", "error", "-i", "{in}", "-f", "null", "-"], data, ext).decode(errors="replace")
|
|
frate, fch, dec = A.ffdecode(ctx, data, ext=ext)
|
|
dch = fch
|
|
ref = samples if dch == ch else [samples[i // dch] for i in range(len(samples) // ch * dch)]
|
|
snr, lag = A.snr(ref, dec, dch, max_lag=1600, skip=200, hints=[1057 if layer == 3 else 481]) if dch == ch else (-99.0, 0)
|
|
e = {"name": name, "sig": sigmod, "layer": layer, "bitrate": br, "mode": mode, "rate": rate, "channels": ch,
|
|
"frames": len(samples) // ch, "ffErrors": errs, "ffFrames": len(dec) // max(1, fch), "ffRate": frate,
|
|
"ffChannels": fch, "ffSnr": snr, "ffLag": lag, "size": len(data), "data": data}
|
|
if layer == 3:
|
|
wavb = A.tool(ctx, "lame", ["lame", "--quiet", "--decode", "{in}", "{out}"], data, "mp3", "wav")
|
|
with wave.open(io.BytesIO(wavb)) as wf:
|
|
lch, raw = wf.getnchannels(), wf.readframes(wf.getnframes())
|
|
lame = [int.from_bytes(raw[i:i + 2], "little", signed=True) / 32768 for i in range(0, len(raw), 2)]
|
|
if lch == ch:
|
|
ls, ll = A.snr(samples, lame, ch, max_lag=1600, skip=200, hints=[1057, 528, 529])
|
|
e["lameSnr"], e["lameLag"] = ls, ll
|
|
e["lameFrames"] = len(lame) // lch
|
|
emitted.append(e)
|
|
print(" mp3_enc: %-28s %5d bytes ffmpeg %.1f dB (lag %d)%s%s" % (name, len(data), snr, lag,
|
|
(" lame %.1f dB" % e["lameSnr"]) if "lameSnr" in e else "", (" ERR " + errs.strip()[:60]) if errs.strip() else ""))
|
|
ctx.emit("mp3_enc", emitted)
|