mirror of
https://github.com/ruppert/fmini-1.git
synced 2026-12-05 20:34:22 +01:00
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QUF6pQ1NkTwmBC8SR8AK94
106 lines
6.1 KiB
Python
Executable File
106 lines
6.1 KiB
Python
Executable File
#!/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
|
|
|
|
# the engines' own tables (SINE, PITCH_OCT / pitch_inc, SVF_G, TANH_Q15; FS, CTL) come from Felucca-Engines
|
|
# (firmware/engines/, a git submodule, MIT): one generator for both, so they cannot drift apart
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "firmware" / "engines" / "tools"))
|
|
import gen_engine_tables as fe # noqa: E402
|
|
|
|
FS = fe.FS
|
|
CTL = fe.CTL
|
|
|
|
|
|
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 += fe.sine() # (1025 points: SINE[i + 1] needs no wrap: FM6's operators; the others mask)
|
|
L += fe.pitch()
|
|
# times 1 ms .. 12 s (exponential), env in Q24 (1.0 = 1<<24)
|
|
ms = [1.0 * (12000 ** (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 / 2.8 # -24 dB after "m" ms: heard to its end (measured on the real sound: the filter closes with it)
|
|
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 = fe.cutoff_hz()
|
|
L += arr("CUTOFF_HZ", "uint16_t", [int(round(f)) for f in fc])
|
|
L += fe.svf_g() # the trapezoidal SVF's g = tan(pi fc / FS), Q12
|
|
# 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])
|
|
L += fe.tanh_q15() # soft clip curve (tanh), Q15
|
|
L += fm6_tables()
|
|
Path(path).write_text("\n".join(L) + "\n")
|
|
|
|
|
|
# FM6 (src/fm6_core.c, a C port of msfa, Apache-2.0): the tables msfa built with double math at start-up,
|
|
# precomputed here so the firmware needs no float. CTL-sample blocks at 44.1 kHz.
|
|
FM6_LFO_SOURCE = [ # msfa lfo.cc: the LFO rates 0..99 (Hz)
|
|
0.062541, 0.125031, 0.312393, 0.437120, 0.624610, 0.750694, 0.936330, 1.125302, 1.249609, 1.436782,
|
|
1.560915, 1.752081, 1.875117, 2.062494, 2.247191, 2.374451, 2.560492, 2.686728, 2.873976, 2.998950,
|
|
3.188013, 3.369840, 3.500175, 3.682224, 3.812065, 4.000800, 4.186202, 4.310716, 4.501260, 4.623209,
|
|
4.814636, 4.930480, 5.121901, 5.315191, 5.434783, 5.617346, 5.750431, 5.946717, 6.062811, 6.248438,
|
|
6.431695, 6.564264, 6.749460, 6.868132, 7.052186, 7.250580, 7.375719, 7.556294, 7.687577, 7.877738,
|
|
7.993605, 8.181967, 8.372405, 8.504848, 8.685079, 8.810573, 8.986341, 9.122423, 9.300595, 9.500285,
|
|
9.607994, 9.798158, 9.950249, 10.117361, 11.251125, 11.384335, 12.562814, 13.676149, 13.904338, 15.092062,
|
|
16.366612, 16.638935, 17.869907, 19.193858, 19.425019, 20.833333, 21.034918, 22.502250, 24.003841, 24.260068,
|
|
25.746653, 27.173913, 27.578599, 29.052876, 30.693677, 31.191516, 32.658393, 34.317090, 34.674064, 36.416606,
|
|
38.197097, 38.550501, 40.387722, 40.749796, 42.625746, 44.326241, 44.883303, 46.772685, 48.590865, 49.261084]
|
|
|
|
|
|
def fm6_tables():
|
|
L = ["/* FM6 (fm6_core.c): msfa's start-up tables, precomputed */",
|
|
f"#define FM6_LFO_UNIT {int(CTL * 25190424 / FS + 0.5)} /* msfa Lfo::unit_ */",
|
|
f"#define FM6_PENV_UNIT {int(CTL * (1 << 24) / (21.3 * FS) + 0.5)} /* msfa PitchEnv::unit_ */",
|
|
f"#define FM6_FREQ_R {int(round(2 ** 40 / FS))}u /* 2^40 / FS: the Q30 mantissa -> a Q24 phase increment (fm6_freq) */",
|
|
f"#define FM6_AMS_C0 {int(round(12.2 * math.log2(math.e) * 2 ** 24))} /* log2(e^12.2), Q24 (the AMS curve) */",
|
|
f"#define FM6_AMS_K {int(round(0.07 / 262144 * math.log2(math.e) * 2 ** 24 * 1024))} /* d/dsensamp of it, Q24 / 1024 */"]
|
|
# 2^(i / 1024) - 1, Q30: the mantissa of Exp2::lookup and Freqlut::lookup (linear between the points, as
|
|
# msfa's: within 6e-8 of 2^x; a Q16 table drifted the partials of a ratio against each other)
|
|
L += arr("FM6_EXP2", "uint32_t", [int(round((1 << 30) * (2 ** (i / 1024) - 1))) for i in range(1025)], 8)
|
|
lforatio = int(4437500000.0 * CTL / FS) # msfa Lfo::lforatio_ (uint32)
|
|
L += arr("FM6_LFO_DELTA", "uint32_t", [int(s * lforatio) for s in FM6_LFO_SOURCE], 8)
|
|
# msfa note osc_freq: the DETUNE step of note mn, Q24 log2 (0.0209 * exp(-0.396 * lf) / 7 * lf, lf in octaves)
|
|
base, step = 50857777, (1 << 24) // 12
|
|
det = []
|
|
for mn in range(128):
|
|
lf = base + step * mn
|
|
det.append(int(round(0.0209 * math.exp(-0.396 * (lf / (1 << 24))) / 7 * lf)))
|
|
L += arr("FM6_DETUNE", "uint16_t", det, 16)
|
|
# .. and its FINE: (1 << 24) / ln 2 * ln(1 + fine / 100), Q24 log2
|
|
L += arr("FM6_FINE", "int32_t", [int(math.floor(24204406.323123 * math.log(1 + 0.01 * f) + 0.5)) for f in range(100)], 10)
|
|
return L
|
|
print(f"tables: {path}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main(sys.argv[1])
|