ws-audio2: WavPack: codecs/audio/WavPack (port of WavPack 5.9.0's decoder, BSD) + audio/wavpack
Lossless and hybrid (lossy, and lossless with the .wvc via opts.correction / opts.resolve), 8-32 bit int (int32 modes, wvx) and 32-bit float, 1-8+ channels, every decorrelation pass, joint stereo, cross decorrelation, noise shaping, block CRCs; APEv2 tags; sample-accurate seeking. 58 fixtures (wavpack -f..-hhx6, ffmpeg) bit-exact against wvunpack. codecs/audio/Stream (shared, backward compatible): Stream.packets takes optional spec.scale (read() writes pcm values times scale) and spec.ints (adds dec:readInt for bit-exact integer reads).
This commit is contained in:
1 parent
b7006dbbed
commit
967b91db91
11 files changed
+2435
-6
No files matched your search
@@ -0,0 +1,177 @@
|
||||
# WavPack fixtures (tools/fixtures.py): audio/wavpack, codecs/audio/WavPack.
|
||||
# tests/gen/wavpack_cases.luau synthetic signals encoded by the reference encoder (wavpack 5.9: -f / default / -h /
|
||||
# -hh / -x1..6, joint stereo on / off, 8 / 12 / 16 / 20 / 24 / 32-bit int and 32-bit
|
||||
# float, 1 / 2 / 3 / 6 / 8 channels, odd rates, short and odd lengths, small blocks,
|
||||
# hybrid lossy (-b, noise shaping) and hybrid lossless (+ the .wvc), APEv2 tags, MD5)
|
||||
# and by ffmpeg's encoder; each with the CRC-32 of wvunpack's raw output (integers as
|
||||
# i32 LE, floats as their f32 bytes)
|
||||
import math, os, random, struct, tempfile, zlib
|
||||
|
||||
CTX = None
|
||||
TMP = None
|
||||
CASES = []
|
||||
|
||||
|
||||
def signal(secs, rate, ch, kind="music", seed=1, frames=None):
|
||||
"""floats in -1..1, interleaved"""
|
||||
rnd = random.Random(seed)
|
||||
n = frames if frames is not None else int(secs * rate)
|
||||
out = []
|
||||
notes = [(rnd.uniform(80, 1500), rnd.uniform(0, max(n, 1)), rnd.uniform(0.1, 0.5) * rate) for _ in range(int(n / rate * 6) + 3)]
|
||||
for i in range(n):
|
||||
t = i / rate
|
||||
for c in range(ch):
|
||||
if kind == "noise":
|
||||
v = rnd.uniform(-0.95, 0.95)
|
||||
elif kind == "silence":
|
||||
v = 0.0
|
||||
elif kind == "mixed": # silence, a tone, noise bursts, full scale
|
||||
seg = (i * 8 // max(n, 1))
|
||||
if seg in (0, 4):
|
||||
v = 0.0
|
||||
elif seg == 5:
|
||||
v = rnd.uniform(-1, 1)
|
||||
elif seg == 6:
|
||||
v = 0.999 if (i // 37) % 2 else -1.0
|
||||
else:
|
||||
v = 0.7 * math.sin(2 * math.pi * (300 + 50 * c) * t)
|
||||
else:
|
||||
v = 0.0
|
||||
for f, st, dur in notes:
|
||||
if st <= i < st + dur:
|
||||
e = math.exp(-3 * (i - st) / dur) * min(1.0, (i - st) / 100)
|
||||
v += 0.25 * e * (math.sin(2 * math.pi * f * (1 + 0.002 * c) * t) + 0.3 * math.sin(6 * math.pi * f * t + c))
|
||||
v += 0.003 * (rnd.random() - 0.5) + 0.04 * math.sin(2 * math.pi * (60 + 7 * c) * t)
|
||||
out.append(max(-1.0, min(1.0, v)))
|
||||
return out
|
||||
|
||||
|
||||
def write_wav(path, vals, ch, rate, bits, fl=False):
|
||||
container = 32 if fl else (bits + 7) // 8 * 8
|
||||
data = bytearray()
|
||||
top = (1 << (bits - 1)) - 1
|
||||
for v in vals:
|
||||
if fl:
|
||||
data += struct.pack("<f", v)
|
||||
continue
|
||||
x = int(round(v * top)) if v < 1 else top
|
||||
x = max(-top - 1, min(top, x)) << (container - bits)
|
||||
if container == 8:
|
||||
data.append((x + 128) & 255)
|
||||
else:
|
||||
data += x.to_bytes(container // 8, "little", signed=True)
|
||||
sub = b"\x03\x00" if fl else b"\x01\x00"
|
||||
guid = sub + b"\x00\x00\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71"
|
||||
mask = {1: 4, 2: 3, 3: 7, 4: 0x33, 6: 0x3F, 8: 0x63F}.get(ch, 0)
|
||||
fmt = struct.pack("<HHIIHHHHI16s", 0xFFFE, ch, rate, rate * ch * container // 8, ch * container // 8, container,
|
||||
22, bits, mask, guid)
|
||||
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 reference(wv, container, fl, ignore_wvc=False):
|
||||
raw = wv + ".raw"
|
||||
CTX.nix("wavpack", ["wvunpack", "-q", "-y", "-r"] + (["-i"] if ignore_wvc else []) + [wv, "-o", raw])
|
||||
d = open(raw, "rb").read()
|
||||
if fl:
|
||||
return zlib.crc32(d), len(d) // 4
|
||||
w = container // 8
|
||||
if w == 1:
|
||||
ints = [b - 128 for b in d]
|
||||
else:
|
||||
ints = [int.from_bytes(d[i:i + w], "little", signed=True) for i in range(0, len(d), w)]
|
||||
return zlib.crc32(b"".join(struct.pack("<i", v) for v in ints)), len(ints)
|
||||
|
||||
|
||||
def case(name, args, secs=0.3, rate=44100, ch=2, bits=16, fl=False, kind="music", seed=1, frames=None, note=None,
|
||||
wvc=False, lossy=False, tags=None, ffmpeg=None):
|
||||
if fl:
|
||||
bits = 32
|
||||
wav = os.path.join(TMP, name + ".wav")
|
||||
wv = os.path.join(TMP, name + ".wv")
|
||||
write_wav(wav, signal(secs, rate, ch, kind, seed, frames), ch, rate, bits, fl)
|
||||
if ffmpeg is not None:
|
||||
CTX.nix("ffmpeg", ["ffmpeg", "-v", "error", "-y", "-i", wav] + ffmpeg + [wv])
|
||||
else:
|
||||
CTX.nix("wavpack", ["wavpack", "-q", "-y"] + args + [wav, "-o", wv])
|
||||
data = open(wv, "rb").read()
|
||||
k = 0
|
||||
while struct.unpack_from("<I", data, k + 20)[0] == 0: # the first audio block's flags give the container
|
||||
k += 8 + struct.unpack_from("<I", data, k + 4)[0]
|
||||
flags = struct.unpack_from("<I", data, k + 24)[0]
|
||||
container = ((flags & 3) + 1) * 8
|
||||
crc, count = reference(wv, container, fl, ignore_wvc=lossy)
|
||||
c = {"name": name, "note": note or ("ffmpeg " + " ".join(ffmpeg) if ffmpeg is not None else "wavpack " + " ".join(args)),
|
||||
"data": data, "rate": rate, "channels": ch, "bits": bits, "container": container,
|
||||
"float": fl, "frames": count // ch, "crc": crc, "lossy": lossy}
|
||||
if wvc:
|
||||
c["wvc"] = open(wv + "c", "rb").read()
|
||||
c["lossy_crc"] = reference(wv, container, fl, ignore_wvc=True)[0]
|
||||
if tags:
|
||||
c["tags"] = tags
|
||||
CASES.append(c)
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
global CTX, TMP
|
||||
CTX = ctx
|
||||
TMP = tempfile.mkdtemp()
|
||||
CASES.clear()
|
||||
for mode in ("-f", "-g", "-h", "-hh"):
|
||||
case("s16" + mode.replace("-", "_"), [mode])
|
||||
for x in range(1, 7):
|
||||
case("s16_x%d" % x, ["-x%d" % x], secs=0.2, seed=x)
|
||||
case("s16_hhx6", ["-hh", "-x6"], seed=7)
|
||||
case("s16_j0", ["-j0"], seed=8)
|
||||
case("s16_j1_hh", ["-j1", "-hh"], seed=9)
|
||||
case("s16_long", ["-h"], secs=3.0, seed=2, note="wavpack -h, 3 s (bench + seek)")
|
||||
case("mono16", ["-g"], ch=1)
|
||||
case("mono16_hh", ["-hh", "-x4"], ch=1, rate=22050)
|
||||
case("u8", ["-g"], bits=8, ch=1, rate=8000, kind="mixed")
|
||||
case("u8_stereo", ["-h"], bits=8, rate=11025)
|
||||
case("s12", ["-g"], bits=12, rate=32000)
|
||||
case("s20", ["-hh"], bits=20, rate=48000)
|
||||
case("s24", ["-g"], bits=24, rate=96000, secs=0.2)
|
||||
case("s24_hh", ["-hh", "-x3"], bits=24, rate=48000, kind="mixed")
|
||||
case("s32", ["-g"], bits=32, rate=48000, secs=0.2)
|
||||
case("s32_hh", ["-hh"], bits=32, rate=48000, secs=0.2, kind="mixed")
|
||||
case("s32_opt", ["--optimize-int32"], bits=32, rate=48000, secs=0.2)
|
||||
case("f32", ["-g"], fl=True, rate=48000, secs=0.2)
|
||||
case("f32_hh", ["-hh", "-x2"], fl=True, rate=44100, kind="mixed")
|
||||
case("f32_mono", ["-f"], fl=True, ch=1, rate=22050)
|
||||
case("ch3", ["-g"], ch=3, bits=24, secs=0.2)
|
||||
case("ch6", ["-h"], ch=6, rate=48000, secs=0.2)
|
||||
case("ch8", ["-f"], ch=8, rate=48000, secs=0.1)
|
||||
case("noise", ["-g"], kind="noise", secs=0.2)
|
||||
case("silence", ["-g"], kind="silence", secs=0.3)
|
||||
case("mixed", ["-hh"], kind="mixed", secs=0.4)
|
||||
case("one_frame", ["-g"], frames=1)
|
||||
case("seven_frames", ["-h"], frames=7, ch=1)
|
||||
case("odd_len", ["-g", "--blocksize=1001"], frames=12345, seed=3, note="block size 1001, 12345 frames")
|
||||
case("rate37000", ["-g"], rate=37000, secs=0.2, note="a rate without a header code")
|
||||
case("rate7", ["-g"], rate=7, frames=300, note="7 Hz")
|
||||
case("md5_tags", ["-g", "-m", "-w", "Title=Lossless Tone", "-w", "Artist=formats-luau", "-w", "Year=2026",
|
||||
"-w", "Track=3/9", "-w", "Album Artist=Some Band"],
|
||||
tags={"title": "Lossless Tone", "artist": "formats-luau", "date": "2026", "track": "3/9",
|
||||
"albumartist": "Some Band"})
|
||||
# hybrid: lossy (no correction file), and lossless with the .wvc
|
||||
case("hyb_b3", ["-b3"], lossy=True, note="hybrid lossy, 3 bits / sample")
|
||||
case("hyb_b2_mono", ["-b2.5", "-h"], ch=1, lossy=True)
|
||||
case("hyb_b4_s0", ["-b4", "-s0"], lossy=True, note="hybrid lossy, no noise shaping")
|
||||
case("hyb_b3_24", ["-b3.5", "-hh"], bits=24, rate=48000, lossy=True)
|
||||
case("hyb_c3", ["-b3", "-c"], wvc=True, note="hybrid lossless (wv + wvc)")
|
||||
case("hyb_c3_shape", ["-b3", "-c", "-s0.5"], wvc=True, seed=4, note="hybrid lossless, positive noise shaping")
|
||||
case("hyb_c3_neg", ["-b3", "-c", "-s-0.5"], wvc=True, seed=5, note="hybrid lossless, negative shaping (NEW_SHAPING)")
|
||||
case("hyb_cc", ["-b2.5", "-cc", "-h"], wvc=True, seed=6, note="hybrid lossless, max compression (cross decorr)")
|
||||
case("hyb_c_mono", ["-b3", "-c", "-hh"], ch=1, wvc=True)
|
||||
case("hyb_c_dns", ["-b3", "-c", "--use-dns"], wvc=True, seed=10, note="dynamic noise shaping")
|
||||
case("hyb_c_24", ["-b4", "-c", "-x3"], bits=24, rate=48000, wvc=True)
|
||||
case("hyb_c_f32", ["-b4", "-c"], fl=True, wvc=True, note="hybrid lossless float (wvx in the wvc)")
|
||||
case("hyb_c_ch6", ["-b3", "-c"], ch=6, rate=48000, secs=0.2, wvc=True)
|
||||
# ffmpeg's encoder
|
||||
case("ff_s16", [], ffmpeg=["-c:a", "wavpack"])
|
||||
case("ff_l8", [], ffmpeg=["-c:a", "wavpack", "-compression_level", "8"], seed=11)
|
||||
case("ff_s24", [], bits=24, ffmpeg=["-c:a", "wavpack", "-sample_fmt", "s32p"], seed=12)
|
||||
case("ff_f32", [], fl=True, ffmpeg=["-c:a", "wavpack"], seed=13)
|
||||
case("ff_mono", [], ch=1, ffmpeg=["-c:a", "wavpack", "-compression_level", "3"], seed=14)
|
||||
ctx.emit("wavpack_cases", CASES)
|
||||
Reference in new issue
Block a user