Files
JIANT/tools/gen_tables.py
Claude da1b1dd2c1 Copyright headers: name Juan Jiant on JIANT's own files, add him on the modified Felucca ones
Comment-only change (the built image is byte-identical: felucca.bin 583936 B, felucca.fwsc 684736 B, same SHA-256).

- 37 files written new for JIANT FM after the fork (no Felucca text in them: checked against ac6bfcbd by literal
  code lines, runs of consecutive lines, same-named functions and moves in their creating commit) carried Leo
  Kuroshita's copyright line copied from a template: it is now Juan Jiant's (SPDX GPL-3.0-only kept). Among them
  sing.c, eng_animal.c, eng_input.c, eng_string.c, pfx.c, seq_live.c, ui_engvis.c, ui_lens.c, ui_lz*.c, ui_being.c,
  ui_organic.c, ui_tech.c, ui_dx.c, ui_live.c, tools/lyrics/*.txt, gen_drum_maps.py, gen_px_font.py and 14 tests.
- 140 files that hold Felucca's code (Felucca 1.1.5.1 files changed since, imports of Felucca 1.5.x, the SLOOP-based
  song_main/song_play, and new files that carry copied Felucca helpers: drumx_voice.c dv_exp2/dv_kb, gen_lyrics.py's
  Huffman, test harness helpers) keep Leo's notice and add "Portions Copyright (C) 2026 Juan Ignacio Antonucci
  (Juan Jiant)". Files unchanged since the fork are left as they were.
- LICENSING.md and README.md name both copyright holders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
2026-11-11 18:04:53 +00:00

127 lines
7.7 KiB
Python
Executable File

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments
# Portions Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant)
"""Fixed-point lookup tables for FELUCCA (no float on the target).
FS = 44100 Hz, control rate = every CTL (64) samples (0.9.9 ctl64: was 32). 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 = 64
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}", ""]
# (1025 points: SINE[i + 1] needs no wrap: FM6's operators; the others mask)
L += arr("SINE", "int16_t", [int(round(32767 * math.sin(2 * math.pi * i / 1024))) for i in range(1025)])
# 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))
# stored as its top octave (1856..2047) shifted down: floor(floor(x) / 2^k) = floor(x / 2^k), so
# pitch_inc(p) equals the 2048-entry table bit for bit (checked here); 7.4 KB less flash
oct_ = inc[1856:2048]
assert all(inc[q] == oct_[(q + 64) % 192] >> (10 - (q + 64) // 192) for q in range(2048))
assert all((q + 64) * 21846 >> 22 == (q + 64) // 192 for q in range(2048))
L += arr("PITCH_OCT", "uint32_t", oct_, 8)
L += ["/* phase increment of pitch p in 1/16 semitone, 0..2047 (MIDI 0..127): 440 Hz * 2^((p / 16 - 69) / 12) */",
"static inline uint32_t pitch_inc(uint32_t p)",
"{",
" uint32_t i = p + 64u, o = (i * 21846u) >> 22; /* i / 192: the octave above PITCH_OCT's - 10 */",
" return PITCH_OCT[i - o * 192u] >> (10u - o);",
"}"]
# times 1 ms .. 10 s (exponential), env in Q24 (1.0 = 1<<24)
ms = [1.0 * (10000 ** (v / 127)) for v in range(128)]
t10 = [int(round(m * 10)) for m in ms]
assert all((t >= 65536) == (v >= 122) for v, t in enumerate(t10))
L += arr("TIME_MS_X10", "uint16_t", [t & 0xFFFF for t in t10], 10)
L += ["/* TIME_MS_X10[v]: 1 ms .. 10 s in 0.1 ms; above 65535 from v 122 on (checked) */",
"#define TIME_MS_X10_AT(v) ((uint32_t)TIME_MS_X10[(v) & 127] + ((((v) & 127) >= 122) ? 65536u : 0u))"]
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", "uint16_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:]])
assert all(int(round(d * 10)) == (v - 112) * 5 for v, d in enumerate(db) if v)
L += ["#define LEVEL_DB_X10_AT(v) ((v) ? ((int32_t)(v) - 112) * 5 : -999) /* 0.1 dB; 0 = off */"]
# 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)])
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 / 512) - 1))) for i in range(513)], 8) # (V10.6k: 513
# points, half the flash; the lines between them ~-110 dB off)
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])