Files
sloop-fm1/tools/gen_tables.py
T
2026-10-04 11:08:49 -04:00

68 lines
3.2 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments
"""Fixed-point lookup tables for FELUCCA (no float on the target).
FS = 44100 Hz, control rate = every CTL (32) samples. All curves map a
0..127 parameter value to the unit the DSP needs, plus a display table.
"""
import math
import sys
from pathlib import Path
FS = 44100
CTL = 32
def arr(name, ctype, vals, per=12):
out = [f"static const {ctype} {name}[{len(vals)}] = {{"]
for i in range(0, len(vals), per):
out.append(" " + ", ".join(str(v) for v in vals[i:i + per]) + ",")
out.append("};")
return out
def main(path):
L = ["/* generated by tools/gen_tables.py */", "#pragma once", "#include <stdint.h>",
f"#define FS {FS}", f"#define CTL {CTL}", ""]
L += arr("SINE", "int16_t", [int(round(32767 * math.sin(2 * math.pi * i / 1024))) for i in range(1024)])
# phase increment for pitch in 1/16 semitone, MIDI 0..127
inc = []
for p in range(128 * 16):
f = 440.0 * 2 ** ((p / 16 - 69) / 12)
inc.append(min(int(f / FS * 2 ** 32), 0x7FFFFFFF))
L += arr("PITCH_INC", "uint32_t", inc, 8)
# times 1 ms .. 10 s (exponential), env in Q24 (1.0 = 1<<24)
ms = [1.0 * (10000 ** (v / 127)) for v in range(128)]
L += arr("TIME_MS_X10", "uint32_t", [int(round(m * 10)) for m in ms], 10)
L += arr("ENV_LIN", "uint32_t", [max(1, int((1 << 24) * CTL / (m * FS / 1000))) for m in ms], 8)
exp_k = []
for m in ms:
tau = m / 1000 / 4.6 # ~99 % of the way after "m" ms
exp_k.append(max(1, min(65535, int(65536 * (1 - math.exp(-CTL / (tau * FS)))))))
L += arr("ENV_EXP", "uint16_t", exp_k)
# LFO 0.05 .. 40 Hz, phase increment per control tick
hz = [0.05 * (800 ** (v / 127)) for v in range(128)]
L += arr("LFO_HZ_X100", "uint32_t", [int(round(h * 100)) for h in hz], 10)
L += arr("LFO_INC", "uint32_t", [int(h * CTL / FS * 2 ** 32) for h in hz], 8)
# filter cutoff 30 Hz .. 16 kHz
fc = [30.0 * ((16000 / 30) ** (v / 127)) for v in range(128)]
L += arr("CUTOFF_HZ", "uint16_t", [int(round(f)) for f in fc])
# trapezoidal (Simper) SVF: g = tan(pi fc / FS), Q12; stable at any cutoff/resonance
L += arr("SVF_G", "uint16_t", [int(4096 * math.tan(math.pi * min(f, 0.45 * FS) / FS)) for f in fc])
# level: 0 = off, else dB = (v - 112) / 2 (112 = 0 dB, 127 = +7.5 dB)
db = [None] + [(v - 112) / 2 for v in range(1, 128)]
L += arr("LEVEL_Q12", "uint16_t", [0] + [int(round(4096 * 10 ** (d / 20))) for d in db[1:]])
L += arr("LEVEL_DB_X10", "int16_t", [-999] + [int(round(d * 10)) for d in db[1:]])
# per-sample exponential decay for drum voices: 5 ms .. 4 s to -60 dB, Q16
dms = [5.0 * (800 ** (v / 127)) for v in range(128)]
L += arr("DECAY_K", "uint16_t", [min(65535, int(65536 * math.exp(-6.9 / (m / 1000 * FS)))) for m in dms])
# soft clip curve for |x| in 0..2 (Q12 in, Q15 out): tanh
L += arr("TANH_Q15", "int16_t", [int(32767 * math.tanh(i / 256 * 2)) for i in range(257)])
Path(path).write_text("\n".join(L) + "\n")
print(f"tables: {path}")
if __name__ == "__main__":
main(sys.argv[1])