Files
rosetta/tools/fixtures/mp3.py
T

76 lines
3.8 KiB
Python

# 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)