Files
soothe2-re/handoff/emit_tables.py
T

70 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""Emit dsp/tables_data.hpp: embed captured live .npy tables as C arrays.
Sourced from handoff/rt*.npy (runtime capture of the DSP registry, NOTES_CAPTURE).
Output is a single header so the port zero-copies the real tables the plugin used.
"""
import numpy as np
import os
HERE = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.join(HERE, "..", "dsp", "tables_data.hpp")
SPECS = [
("WIN_WINDOW", "rtwin_freq_44100.npy"), # 0x540658 FFT-conv window, 8193 f32
("WIN_FREQAXIS", "rtfreqaxis_48000_internal.npy"), # freq-axis 2048 f32 (internal SR 48000)
("WTA_WEIGHT", "rtwa_596.npy"), # [03] 0.596->0.126
("WTB_WEIGHT", "rtwb_404.npy"), # [04] 0.404->0.874
("WTC_WEIGHT", "rtwc_043.npy"), # [05]
("WTD_WEIGHT", "rtwd_956.npy"), # [06]
]
WINDOW_N = 8193
def fmt_floats(a):
s = []
for v in a:
r = ("%.9g" % float(v)).encode().decode("ascii")
s.append(r)
return s
def emit_header(out_path, blocks):
with open(out_path, "w") as f:
f.write("// AUTOGENERATED from runtime capture handoff/rt*.npy (NOTES_CAPTURE 2026-08-19).\n")
f.write("// Do not edit by hand; regenerate with handoff/emit_tables.py.\n")
f.write("#pragma once\n#include <cstddef>\n#include <cstdint>\n\n")
for name, npy_file, arr in blocks:
f.write(f"constexpr size_t {name}_COUNT = {arr.size};\n")
f.write(f"const float {name}[{arr.size}] = {{\n")
line = []
for s in fmt_floats(arr):
line.append(s)
if len(line) == 8:
f.write(" " + ", ".join(line) + ",\n")
line = []
if line:
f.write(" " + ", ".join(line) + ",\n")
f.write("};\n\n")
def main():
blocks = []
for name, filename in SPECS:
path = os.path.join(HERE, filename)
if not os.path.exists(path):
print(f"missing table {filename} — skipping")
continue
arr = np.load(path)
arr = arr.ravel().astype(np.float32)
if arr.shape[0] not in (WINDOW_N, 8193, 2048, 2049):
print(f"unexpected size for {filename}: {arr.shape}")
blocks.append((name, filename, arr))
print(f"loaded {filename}: {arr.shape} {arr.dtype}")
emit_header(OUT, blocks)
print(f"wrote {OUT} ({sum(b[2].size for b in blocks)} floats)")
if __name__ == "__main__":
main()