open-standards/wai/tools/wai_encodec_decode.py
Transaction Science d8a8cb6013
All checks were successful
standards workspaces build + test / jouleclaw-rs (push) Successful in 2m4s
standards workspaces build + test / aac-rs (push) Successful in 1m15s
standards workspaces build + test / joule-code-rs (push) Successful in 46s
standards workspaces build + test / joulecontract-rs (push) Successful in 1m30s
standards workspaces build + test / map-rs (push) Successful in 2m19s
standards workspaces build + test / mesh-rs (push) Successful in 48s
standards workspaces build + test / openpay (push) Successful in 2m30s
jouleclaw public surface must stay documented / Public-surface audit (jouleclaw-rs) (push) Successful in 58s
standards workspaces build + test / eoc-rs (push) Successful in 1m51s
standards workspaces build + test / jcp-rs (push) Successful in 1m18s
standards workspaces build + test / proof-rs (push) Successful in 1m12s
standards workspaces build + test / sandbox-rs (push) Successful in 2m9s
standards workspaces build + test / smart-byte-rs (push) Successful in 2m34s
standards workspaces build + test / arl-rs (push) Successful in 2m46s
standards workspaces build + test / joule-ui-rs (push) Successful in 3m18s
standards workspaces build + test / jouledesk-rs (push) Successful in 2m52s
standards workspaces build + test / joulehook-rs (push) Successful in 3m11s
standards workspaces build + test / sense-rs (push) Successful in 3m12s
standards workspaces build + test / macOS energy backend (jcp-energy) (push) Successful in 2m46s
chore(paths): follow the drive split off macos_4TB_external
The single macos_4TB_external drive no longer exists: the model library
and caches moved to macos_4TB_external_1, datasets to
macos_4TB_external_2. Point every hardcoded path at the drive that now
holds that folder, and fix the macos_4TB_external-1 typo.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-09-30 21:49:16 -04:00

91 lines
3.1 KiB
Python

"""WAI sink-side neural decoder for `wai.neural.encodec32`.
This is the receiving half of the demonstration: a sink that has the
EnCodec model installed locally resolves the `wai.neural.encodec32`
capability by loading its own model and reconstructing audio from the
shipped tokens. The WAI envelope did NOT carry any model bytes.
If the sink doesn't have EnCodec installed, the file is inert here
(unless the fallback capability is also wired up — out of scope for
this single-capability demo).
Usage:
python tools/wai_encodec_decode.py <input.wai> -o <output.wav>
"""
from __future__ import annotations
import argparse
import json
import struct
import sys
import wave
from pathlib import Path
import numpy as np
ENCODEC_DIR = (
"/Volumes/macos_4TB_external_1/neuronexus_ai/model_library/encodec-32khz"
)
def decode(wai_path: str, out_path: str) -> None:
raw = Path(wai_path).read_bytes()
if raw[:4] != b"WAI1":
sys.exit("not a WAI v1 file (magic mismatch)")
o = 4
(ml,) = struct.unpack_from("<I", raw, o); o += 4
manifest = json.loads(raw[o:o + ml]); o += ml
(pl,) = struct.unpack_from("<I", raw, o); o += 4
payload = raw[o:o + pl]
cap = manifest["model_requirement"]["capability"]
fb = manifest["model_requirement"].get("fallback")
kind = manifest["conditioning"]["kind"]
print(f"WAI envelope: {len(raw):,} B")
print(f" capability: {cap}")
print(f" fallback: {fb}")
print(f" conditioning.kind:{kind}")
if cap != "wai.neural.encodec32":
sys.exit(f"this decoder only resolves wai.neural.encodec32; got {cap}")
# Sink-side capability resolution: do we have EnCodec installed?
if not Path(ENCODEC_DIR).exists():
print(f" resolution: MISSING — EnCodec model not at {ENCODEC_DIR}")
if fb is None:
sys.exit(" → file is INERT at this sink (no fallback declared)")
sys.exit(f" → would fall back to {fb} (not implemented in this demo)")
print(f" resolution: ✓ EnCodec installed locally at sink")
# Decode via locally-installed EnCodec
import torch
from transformers import EncodecModel
model = EncodecModel.from_pretrained(ENCODEC_DIR).eval()
q, t, n = struct.unpack_from("<III", payload, 0)
codes = (np.frombuffer(payload[12:], np.uint16).reshape(q, t)
.astype(np.int64))
with torch.no_grad():
audio = model.decode(torch.from_numpy(codes)[None, None], [None])[0]
samples = audio.squeeze().numpy()[:n]
sr = manifest["target"]["sr"]
with wave.open(out_path, "wb") as w:
w.setnchannels(1)
w.setsampwidth(2)
w.setframerate(sr)
w.writeframes((samples.clip(-1, 1) * 32767).astype(np.int16).tobytes())
dur = len(samples) / sr
raw_bytes = len(samples) * 2
print(f" reconstructed: {dur:.2f} s mono @ {sr} Hz "
f"({raw_bytes:,} B as int16 PCM) → {out_path}")
if __name__ == "__main__":
p = argparse.ArgumentParser()
p.add_argument("input")
p.add_argument("-o", "--output", required=True)
a = p.parse_args()
decode(a.input, a.output)