Files
rosetta/tools/fixtures/ape.py
T
4DBug ae8ff9d6bf ws-audio2: Monkey's Audio: codecs/audio/Ape (port of the MAC SDK 13.28 decoder, BSD-3) + audio/ape
File versions 3.95+ (3.99+ verified; the 3.95-3.98 range-coder model is ported without a reference fixture), every
level (fast .. insane), 8/16/24/32-bit int and 32-bit float, 1-32 channels, special frames, frame CRCs, seeking,
APEv2 tags, ID3v2 in front. 35 fixtures (mac 13.28, the FATE luckynight sample) bit-exact against mac -d.
tests/audio/_lossless.luau: the per-fixture checks shared by the lossless format tests. tools/bench.luau: the
ws-audio2 lossless block.
2026-10-31 14:52:08 -05:00

138 lines
6.2 KiB
Python

# Monkey's Audio fixtures (tools/fixtures.py): audio/ape, codecs/audio/Ape.
# tests/gen/ape_cases.luau synthetic signals encoded by the reference encoder (mac 13.28: -c1000 .. -c5000, 8 / 16 /
# 24 / 32-bit int and 32-bit float, 1 / 2 / 3 / 4 / 6 / 8 channels, odd rates, short and
# odd lengths, silence (special frames), mono-in-stereo (pseudo stereo), APEv2 tags), and
# the ffmpeg FATE samples luckynight-partial.ape (an older 3.99 encoder, cut) and
# NoLegacy-cut.ape (downloaded at fixture time, not committed); each with the CRC-32 of
# `mac -d`'s samples (integers as i32 LE, floats as their f32 bytes)
import importlib.util, os, struct, tempfile, urllib.request, zlib
_spec = importlib.util.spec_from_file_location("_wv", os.path.join(os.path.dirname(os.path.abspath(__file__)), "wavpack.py"))
WV = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(WV)
CTX = None
TMP = None
CASES = []
FATE = "https://fate-suite.ffmpeg.org/lossless-audio/"
def wav_samples(path):
"""(rate, channels, bits, float, ints-or-raw-bytes) of a WAV file (PCM / float, plain or extensible)"""
d = open(path, "rb").read()
i = 12
fmt = data = None
while i + 8 <= len(d):
cid, size = d[i:i + 4], struct.unpack_from("<I", d, i + 4)[0]
if cid == b"fmt ":
fmt = d[i + 8:i + 8 + size]
elif cid == b"data":
data = d[i + 8:i + 8 + size]
break
i += 8 + size + (size & 1)
tag, ch, rate = struct.unpack_from("<HHI", fmt, 0)
bits = struct.unpack_from("<H", fmt, 14)[0]
fl = tag == 3 or (tag == 0xFFFE and fmt[24:26] == b"\x03\x00")
w = bits // 8
if fl:
return rate, ch, bits, True, data
if w == 1:
ints = [b - 128 for b in data]
else:
ints = [int.from_bytes(data[k:k + w], "little", signed=True) for k in range(0, len(data) - w + 1, w)]
return rate, ch, bits, False, ints
def emit(name, ape, note, tags=None, partial=False):
out = ape + ".out.wav"
try:
CTX.nix("monkeys-audio", ["mac", ape, out, "-d"])
except Exception:
if not partial: # a cut file: mac writes the whole frames before the cut, then fails
raise
rate, ch, bits, fl, s = wav_samples(out)
if fl:
crc, n = zlib.crc32(s), len(s) // 4
else:
crc, n = zlib.crc32(b"".join(struct.pack("<i", v) for v in s)), len(s)
c = {"name": name, "note": note, "data": open(ape, "rb").read(), "rate": rate, "channels": ch, "bits": bits,
"float": fl, "frames": n // ch, "crc": crc, "partial": partial}
if tags:
c["tags"] = tags
CASES.append(c)
def case(name, level, secs=0.3, rate=44100, ch=2, bits=16, fl=False, kind="music", seed=1, frames=None, note=None,
tags=None, mono_stereo=False):
if fl:
bits = 32
wav = os.path.join(TMP, name + ".wav")
ape = os.path.join(TMP, name + ".ape")
vals = WV.signal(secs, rate, 1 if mono_stereo else ch, kind, seed, frames)
if mono_stereo:
vals = [v for v in vals for _ in range(ch)]
WV.write_wav(wav, vals, ch, rate, bits, fl)
args = ["mac", wav, ape, "-c%d" % level]
if tags:
args += ["-t", "|".join("%s=%s" % kv for kv in tags.items())]
CTX.nix("monkeys-audio", args)
emit(name, ape, note or "mac -c%d, %d-bit%s, %d ch" % (level, bits, " float" if fl else "", ch),
tags={({"year": "date"}).get(k.lower(), k.lower()): v for k, v in tags.items()} if tags else None)
def fate(name, file, cut=None):
path = os.path.join(TMP, file)
try:
with urllib.request.urlopen(FATE + file, timeout=60) as r:
data = r.read()
if cut: # keep `cut` whole frames and a little of the next one
desc = struct.unpack_from("<I", data, 8)[0]
head = struct.unpack_from("<I", data, 12)[0]
data = data[:struct.unpack_from("<I", data, desc + head + 4 * cut)[0] + 64]
open(path, "wb").write(data)
except Exception as e: # offline: skip the FATE samples
print("ape: skipping %s (%s)" % (file, e))
return
emit(name, path, "ffmpeg FATE sample " + file, partial=True)
def generate(ctx):
global CTX, TMP
CTX = ctx
WV.CTX = ctx
TMP = tempfile.mkdtemp()
CASES.clear()
for level in (1000, 2000, 3000, 4000, 5000):
case("s16_c%d" % level, level, seed=level // 1000)
case("s16_long", 2000, secs=3.0, seed=2, note="mac -c2000, 3 s (bench + seek)")
case("s16_long_c4000", 4000, secs=1.5, seed=3, note="mac -c4000, 1.5 s (bench)")
case("mono", 2000, ch=1)
case("mono_c5000", 5000, ch=1, rate=22050, seed=4)
case("u8", 2000, bits=8, ch=1, rate=8000, kind="mixed")
case("u8_stereo", 3000, bits=8, rate=11025)
case("s24", 2000, bits=24, rate=48000, secs=0.2)
case("s24_c4000", 4000, bits=24, rate=96000, secs=0.15, kind="mixed")
case("s24_c5000", 5000, bits=24, rate=48000, secs=0.15, seed=5)
case("s24_mono", 3000, bits=24, ch=1, rate=48000, secs=0.2)
case("s32", 2000, bits=32, rate=48000, secs=0.15)
case("s32_c4000", 4000, bits=32, rate=48000, secs=0.15, kind="mixed")
case("f32", 2000, fl=True, rate=48000, secs=0.15)
case("f32_c3000", 3000, fl=True, rate=44100, secs=0.15, kind="mixed")
case("ch3", 2000, ch=3, secs=0.15)
case("ch4", 3000, ch=4, secs=0.15)
case("ch6", 2000, ch=6, rate=48000, secs=0.15)
case("ch6_24", 3000, ch=6, bits=24, rate=48000, secs=0.1)
case("ch8", 1000, ch=8, rate=48000, secs=0.1)
case("noise", 2000, kind="noise", secs=0.2)
case("noise_c5000", 5000, kind="noise", secs=0.1, seed=6)
case("silence", 2000, kind="silence")
case("mixed", 3000, kind="mixed", secs=0.4)
case("pseudo_stereo", 2000, mono_stereo=True, note="the same signal in both channels (pseudo-stereo frames)")
case("one_frame", 2000, frames=1)
case("seven_frames", 3000, frames=7, ch=1)
case("odd_len", 2000, frames=300001, rate=48000, seed=7, note="300001 frames: two APE frames")
case("rate37000", 2000, rate=37000, secs=0.2)
case("tags", 2000, tags={"Title": "Lossless Tone", "Artist": "formats-luau", "Year": "2026", "Track": "3"})
fate("fate_lucky", "luckynight-partial.ape", cut=2)
ctx.emit("ape_cases", CASES)