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.
This commit is contained in:
4DBug committed 2026-10-31 15:08:47 -05:00
1 parent ae8ff9d6bf
commit 085e385674
12 files changed
+857 -20

No files matched your search

+6
View File
@@ -1100,4 +1100,10 @@ do
lossless("ape " .. c.name, ApeF, c.data)
end
end
local TtaF = require("@formats/audio/tta")
for _, c in require("@tests/gen/tta_cases") do
if c.name == "s16_long" or c.name == "s24" or c.name == "ch6" then
lossless("tta " .. c.name, TtaF, c.data)
end
end
end
+40
View File
@@ -0,0 +1,40 @@
# Docs inputs for the guide's lossless audio section (tools/fixtures.py; tools/docs_check.py reads them): the docs
# stereo tone (tools/fixtures/docs_inputs.py) encoded by the reference encoders, with tags: WavPack lossless, WavPack
# hybrid (.wv + its .wvc correction file), Monkey's Audio (-c2000) and TTA.
# tests/gen/docs_lossless.luau { files = { [name] = bytes }, vars = { wvBytes = "tone.wv", ... } }
# tests/gen/docs_files/assets/<name> the same files, for the snippets that load by path
import importlib.util, os, tempfile
INPUTS = ["tools/docs_check.py", "tools/fixtures/docs_inputs.py"]
def load(ctx, name):
spec = importlib.util.spec_from_file_location("docs_lossless_" + name.replace("/", "_"),
os.path.join(ctx.root, "tools", name + ".py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
def generate(ctx):
dc = load(ctx, "docs_check")
di = load(ctx, "fixtures/docs_inputs")
files = {}
with tempfile.TemporaryDirectory() as d:
wav = os.path.join(d, "tone.wav")
with open(wav, "wb") as f:
f.write(di.tone(44100, 1.0))
tags = ["-w", "Title=Docs Tone", "-w", "Artist=formats-luau"]
ctx.nix("wavpack", ["wavpack", "-q", "-y", "-h"] + tags + [wav, "-o", os.path.join(d, "tone.wv")])
ctx.nix("wavpack", ["wavpack", "-q", "-y", "-b3", "-c"] + tags + [wav, "-o", os.path.join(d, "tone_hybrid.wv")])
ctx.nix("monkeys-audio", ["mac", wav, os.path.join(d, "tone.ape"), "-c2000", "-t", "Title=Docs Tone|Artist=formats-luau"])
ctx.nix("ttaenc", ["ttaenc", "-e", wav, "-o", os.path.join(d, "tone.tta")])
for name in ("tone.wv", "tone_hybrid.wv", "tone_hybrid.wvc", "tone.ape", "tone.tta"):
files[name] = open(os.path.join(d, name), "rb").read()
dc.emit_inputs(ctx, files, {
"wvBytes": "tone.wv",
"hybridWvBytes": "tone_hybrid.wv",
"hybridWvcBytes": "tone_hybrid.wvc",
"apeBytes": "tone.ape",
"ttaBytes": "tone.tta",
})
+93
View File
@@ -0,0 +1,93 @@
# 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)