Files
JIANT/tools/gen_tables.py
T
Claude caa8b2557b ctl64 (0.9.9): the control block 64 samples, notes at their sample inside the block
core.h CTL 32 -> 64 (tools/gen_tables.py; the half buffer 128 frames: two mix_block passes a half, not
four), so the fixed cost of a block (events_block, mc_split, the core-1 handshake, buffers cleared and
summed, sends, buses, master) is paid half as often. Everything that counted blocks of 32 counts in CTL:
envelope / LFO tables, glides, fades, the steal fade, the leveler's (its peak still read over 32) and
DUCK's followers, DRUM-X FLAM / PUNCH / pan slew / group fade, the FX gate, DJF crossfade, SPRING / RESO
glides, WORDS' phoneme timing, the FX layer's LPF / HPF sweep (perform.c PF_CSTEP), the mod matrix's
NOISE and STEP eases.

The sequencer did not get coarser: a note inside the block carries its sample (core.h ev_off; voice.c
voice_begin / voice_off_at / track_render render the sounding voice up to it and the new one from it;
every engine but FM6, which renders whole blocks and keeps the nearer edge). tests/ctl64_timing_test.c:
on a MICRO-shifted pattern every rendered onset is within 2 samples of the sample-at-a-time sequencer
(ANALOG POLY worst 2, ANALOG MONO cuts worst 2, DRUM kit hits worst 1); the 32-sample build placed them
-22 .. +5 samples off.

Host CPU (tests/cpu_vg.py, exact instructions / sample, 32 -> 64): mix/idle 516 -> 426 (-17.5 %),
seq/mono4 1514 -> 1188 (-21.5 %), eng/DRUM 1477 -> 1156 (-21.7 %), eng/FM6 1710 -> 1507 (-11.9 %),
eng/VOICE 1500 -> 1337 (-10.9 %), VOICE.EN 1783 -> 1619 (-9.2 %), ANALOG / PHASE / LOFI / NOISE / WHEEL
-6 .. -9 %, reverbs and delays -7 .. -8 %, mix/full 2671 -> 2505 (-6.2 %), seq/plain 2381 -> 2158
(-9.4 %), seq/heavy 2783 -> 2485 (-10.7 %), song/cputest 3016 -> 2742 (-9.1 %), peak/VOICE.EN6
3304 -> 2937 (-11.1 %). Firmware: image 567896 B, .data+.bss 88276 of 98304 (+9 KB: the CTL-long
buffers), RAM code 21848 of 24576; the render loops' target cost as before (tests/target_budget.txt).
docs/CPU3_PERFORMANCE.md, README (the dual-core section), CPU5_PERFORMANCE.md.

Goldens: tests/golden.txt, cpu_baseline_vg.txt, target_budget.txt regenerated (GOLDEN_UPDATE /
BUDGET_UPDATE: the sound changes through the buses' one-block lag, now 1.45 ms, and the leveler's
reading); djf_test / micro_test reference hashes of the 0.9.0 renders redone at CTL 64. Host tests that
counted blocks count in CTL (reverb DUCK 3 / 20 ms, voice glide 163 blocks of 32 and 1/8 st a block,
click_test, drumx_oct_test); drumx_test's HANDS and PUNCH checks reset the kit's noise generator before
each compared strike (they compared different noise realizations: up to x5 strike to strike on the
32-sample build too, measured). Full host suite: every section passes but gen_nav_map's stale
docs/navigation-map.svg, which the base reports as well.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
2026-10-23 17:07:04 +00:00

120 lines
7.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 (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)]
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)])
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])