Files
rosetta/tools/fixtures/tta.py
T
4DBug 085e385674 ws-audio2: TTA: codecs/audio/Tta (written from the TTA1 format description; no libtta / ffmpeg code) + audio/tta
TTA1 format 1, 8/16/24 bits, any channel count, header / seek table / frame CRC-32s, seeking, ID3v2 skipped, APEv2
tags; password-protected files -> UNSUPPORTED. 20 fixtures (ttaenc 3.4.1, the FATE inside.tta sample) bit-exact
against ttaenc -d. Branch-free filter steps (LuaJIT: no register-coalescing aborts, 7 -> 40 MB/s).
WavPack: the unary / gamma counts a byte at a time (LuaJIT traces). APE: the predictor rotations as single
assignments. docs/guide/audio.md: the lossless section (+ tools/fixtures/docs_lossless.py inputs) and the Formats
table.
2026-10-31 15:08:47 -05:00

94 lines
4.3 KiB
Python

# TTA fixtures (tools/fixtures.py): audio/tta, codecs/audio/Tta.
# tests/gen/tta_cases.luau synthetic signals encoded by the reference encoder (ttaenc 3.4.1: 8 / 16 / 24 bits,
# 1 / 2 / 3 / 6 channels, odd rates, short and odd lengths, several frames, silence, noise,
# full scale) and the ffmpeg FATE sample inside.tta (downloaded at fixture time, cut after 2
# frames, not committed); each with the CRC-32 of `ttaenc -d`'s samples (i32 LE)
import importlib.util, os, struct, tempfile, urllib.request, zlib
_dir = os.path.dirname(os.path.abspath(__file__))
_spec = importlib.util.spec_from_file_location("_wv", os.path.join(_dir, "wavpack.py"))
WV = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(WV)
_spec = importlib.util.spec_from_file_location("_ape", os.path.join(_dir, "ape.py"))
APE = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(APE)
CTX = None
TMP = None
CASES = []
FATE = "https://fate-suite.ffmpeg.org/lossless-audio/"
def emit(name, tta, note, partial=False, keep=None):
out = tta + ".out.wav"
try:
CTX.nix("ttaenc", ["ttaenc", "-d", tta, "-o", out])
except Exception:
if not partial:
raise
rate, ch, bits, fl, s = APE.wav_samples(out)
if keep is not None: # a cut file: ttaenc also writes the broken frame; keep the whole ones
s = s[:keep * ch]
CASES.append({"name": name, "note": note, "data": open(tta, "rb").read(), "rate": rate, "channels": ch,
"bits": bits, "float": False, "frames": len(s) // ch,
"crc": zlib.crc32(b"".join(struct.pack("<i", v) for v in s)), "partial": partial})
def plain_wav(path, vals, ch, rate, bits):
"""a plain PCM WAV (ttaenc reads WAVE_FORMAT_PCM)"""
w = bits // 8
top = (1 << (bits - 1)) - 1
data = bytearray()
for v in vals:
x = max(-top - 1, min(top, int(round(v * top))))
data += bytes([(x + 128) & 255]) if w == 1 else x.to_bytes(w, "little", signed=True)
fmt = struct.pack("<HHIIHH", 1, ch, rate, rate * ch * w, ch * w, bits)
body = b"WAVE" + b"fmt " + struct.pack("<I", len(fmt)) + fmt + b"data" + struct.pack("<I", len(data)) + bytes(data)
open(path, "wb").write(b"RIFF" + struct.pack("<I", len(body)) + body)
def case(name, secs=0.3, rate=44100, ch=2, bits=16, kind="music", seed=1, frames=None, note=None):
wav = os.path.join(TMP, name + ".wav")
tta = os.path.join(TMP, name + ".tta")
plain_wav(wav, WV.signal(secs, rate, ch, kind, seed, frames), ch, rate, bits)
CTX.nix("ttaenc", ["ttaenc", "-e", wav, "-o", tta])
emit(name, tta, note or "ttaenc -e, %d-bit, %d ch, %d Hz" % (bits, ch, rate))
def generate(ctx):
global CTX, TMP
CTX = ctx
APE.CTX = ctx
TMP = tempfile.mkdtemp()
CASES.clear()
case("s16")
case("s16_long", secs=3.0, seed=2, note="3 s, 3 frames (bench + seek)")
case("mono", ch=1)
case("u8", bits=8, ch=1, rate=8000, kind="mixed")
case("u8_stereo", bits=8, rate=11025)
case("s24", bits=24, rate=48000, secs=0.3)
case("s24_mixed", bits=24, rate=96000, secs=0.2, kind="mixed")
case("s24_mono", bits=24, ch=1, rate=48000)
case("ch3", ch=3, secs=0.2)
case("ch6", ch=6, rate=48000, secs=0.2)
case("noise", kind="noise", secs=0.2)
case("noise24", bits=24, kind="noise", secs=0.1)
case("silence", kind="silence")
case("mixed", kind="mixed", secs=0.4)
case("one_frame", frames=1)
case("seven_frames", frames=7, ch=1)
case("odd_len", frames=100001, rate=48000, seed=7, note="100001 frames: 3 TTA frames, a short last one")
case("rate37000", rate=37000, secs=0.2)
case("rate8000", rate=8000, secs=1.5, ch=1, note="8 kHz: 8359-sample frames")
path = os.path.join(TMP, "inside.tta")
try:
with urllib.request.urlopen(FATE + "inside.tta", timeout=60) as r:
data = r.read()
frames = (struct.unpack_from("<I", data, 18)[0] + 46079) // 46080
sizes = struct.unpack_from("<%dI" % frames, data, 22)
open(path, "wb").write(data[:22 + frames * 4 + 4 + sizes[0] + sizes[1] + 100])
emit("fate_inside", path, "ffmpeg FATE sample inside.tta (cut after 2 frames)", partial=True, keep=2 * 46080)
except OSError as e: # offline: skip the FATE sample
print("tta: skipping inside.tta (%s)" % e)
ctx.emit("tta_cases", CASES)