export_live.py
← Gallery · core.py · scenes.py · render.py · fidelity.py · export_live.py
"""export_live.py DEST id=audio[,title] ... : features for the in-browser renderer.
Per track: env.png (Zvon key envelopes), envs.png (Sand envelopes), spec.png (whitened log-f, 3 bins/semitone),
spec12.png (12 bins/semitone, short tracks only), feat.bin (flux, loud, chroma x12 as u8; ridge as u16), audio.m4a, and tracks.json.
Image layout: width PAGE (time), height pages*rows; row = page*rows + r. A value at frame i of row r is at (i % PAGE, (i // PAGE)*rows + r)."""
import sys, os, json, subprocess, numpy as np
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src"))
import core, scenes
from PIL import Image
PAGE = 4096
def paged(a): # a: rows x T float 0..1 -> u8 image
rows, T = a.shape; pages = -(-T // PAGE)
img = np.zeros((pages * rows, PAGE), np.uint8)
q = np.clip(np.round(a * 255), 0, 255).astype(np.uint8)
for p in range(pages):
seg = q[:, p * PAGE:(p + 1) * PAGE]; img[p * rows:(p + 1) * rows, :seg.shape[1]] = seg
return Image.fromarray(img, "L"), pages
dest = sys.argv[1]; tracks = []
meta_path = os.path.join(dest, "tracks.json")
old = {t["id"]: t for t in json.load(open(meta_path))} if os.path.exists(meta_path) else {}
for spec in sys.argv[2:]:
tid, rest = spec.split("=", 1); path, *extra = rest.split("|")
d = os.path.join(dest, tid); os.makedirs(d, exist_ok=True)
x = core.load(path); A = core.Analysis(x)
im, pages = paged(scenes.key_env(A, thr=0.3, release=0.35, top=7, dyn=0.75)); im.save(os.path.join(d, "env.png"), optimize=True)
paged(scenes.key_env(A, thr=0.3, smooth=10, top=5))[0].save(os.path.join(d, "envs.png"), optimize=True)
paged(np.clip(A.W / 3.6, 0, 1))[0].save(os.path.join(d, "spec.png"), optimize=True)
has12 = A.T <= 3 * PAGE
if has12:
_, L12 = core.logspec(x, bps=12); W12 = core.whiten(L12, fmed=97)
paged(np.clip(W12 / 2.2, 0, 1.3) / 1.3)[0].save(os.path.join(d, "spec12.png"), optimize=True)
r, _ = core.ridge(A.W, 36 * core.BPS, 75 * core.BPS, max_jump=4, jump_cost=1.2)
from scipy import ndimage
rs = ndimage.gaussian_filter1d(r.astype(float), 2)
with open(os.path.join(d, "feat.bin"), "wb") as f:
f.write(np.clip(A.flux * 128, 0, 255).astype(np.uint8).tobytes())
f.write(np.clip(A.loud * 255, 0, 255).astype(np.uint8).tobytes())
f.write(np.clip(A.chroma_h * 255, 0, 255).astype(np.uint8).tobytes()) # 12 x T, row-major
f.write(np.clip(rs * 64, 0, 65535).astype("<u2").tobytes())
subprocess.run(["ffmpeg", "-v", "error", "-y", "-i", path, "-ac", "1", "-ar", "44100", "-c:a", "aac", "-b:a", "72k", "-movflags", "+faststart", os.path.join(d, "audio.m4a")], check=True)
t = {"id": tid, "title": extra[0] if extra else tid, "rec": extra[1] if len(extra) > 1 else "", "T": int(A.T), "fps": core.FPS_A, "pages": pages, "page": PAGE, "dur": round(A.dur, 2), "spec12": bool(has12)}
old[tid] = t; print(tid, A.T, pages, {f: os.path.getsize(os.path.join(d, f)) // 1024 for f in os.listdir(d)}, flush=True)
json.dump(list(old.values()), open(meta_path, "w"), indent=1, ensure_ascii=False)