Files
JIANT/tools/gen_uac_fir.py
T
Claude 329aff53fa 0.9.12 draft: 12 voices with the second core, sound types + PRESETS by type, MIDI LEARN (MENU > MIDI), FM6 bank sector guard, copyright lines
- voice.c voice_cap: VOICES_2CORE 12 while the second core takes parts (fx.c mc_two: CORES 2, not REC at
  power-on, not given up); one core 8, LOAD ECO 6; a track still NVOICE 8 (no RAM). After Skiff by illuminus5.
  cpu_vg: mix/v12 (both cores 3924 instr/sample), mix/v12.c0 (core 0 1949), mix/v12.one (one core, 8 voices 3241);
  regress.c chk_budget_2core; the dual-core equivalence test at one budget (mc_host_v12).
- Sound types (PT_BASS..PT_FX) on all factory presets in preset_t.mono bits 1..4 (no format change); PRESETS LIST
  ALL / FAV / BASS LEAD PAD KEYS PLUCK BELL VOX DRUM FX across engines, saved in favorites.factory[15][18];
  gen.c keeps PT_BASS sounds off the other roles (was a name match). After Skiff by illuminus5.
- MIDI LEARN ported from Felucca 1.5 (midi_learn.c): 10 CCs, 3 bytes each in DIGITAL's favourites row, reserved
  CCs and the standard map refused, any channel, USB and TRS; MENU > MIDI tab (MIDI OUT, LEARN, LEARN CLEAR);
  state in the pool.
- fm6_vbank.c: 0xE7000 holding foreign data (Skiff's pattern bank) reads as no bank and is never erased at
  power-on; only a bank the user sends replaces it. fm6bank_test foreign_test.
- Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) in JIANT's files ("Portions" in mostly-Felucca ones),
  README / LICENSING.md / MENU > ABOUT credits (Felucca, Skiff).
- Docs: MANUAL.md / MANUAL.es.md, README v0.9.12 draft, EDITOR_PROTOCOL.md (tabs), cheat sheets, nav map.

Flash 576192 -> 577712 B (+1520), .data+.bss unchanged, pool +8 B (spare 10528 B).

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

108 lines
5.2 KiB
Python

#!/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)
"""The polyphase FIR of the USB audio input's 44.1 -> 48 kHz resampler (src/usb.c, FELUCCA_UAC_48K).
48000 / 44100 = 160 / 147: up by L = 160, low-pass, down by M = 147. Output frame k sits at input time
k * 147 / 160: phase p = (k * 147) % 160, newest input frame floor(k * 147 / 160). Each phase is N taps of
the prototype (a Kaiser-windowed sinc of 160 * N taps at 7.056 MHz, cut-off FC):
y[k] = sum_j PROTO[p + 160 j] * x[base - j], j = 0 .. N-1
The table holds the taps oldest input first (UAC_FIR[p][i] = L * PROTO[p + 160 (N-1-i)]), Q30, and only the
phases 0 .. 79: the prototype is symmetric, so phase 159 - p is phase p read backwards. Each phase's taps sum
to exactly 1 << 30 (unity gain at DC, the rounding left on its largest tap).
Why these numbers (tests/uac_test.c measures them on the C code):
N = 24, FC = 22 kHz, beta 9: images that would fold below 20 kHz (input above 28.1 kHz at 7.056 MHz) are
down 90 dB, the passband is flat within 0.06 dB to 18 kHz (-1.2 dB at 20 kHz); with Q30 taps the 1 kHz SNR
sits at the 16-bit floor (~94 dB at -1 dBFS); Q15 taps would cap it near 87 dB.
tools/gen_uac_fir.py [OUT] (default firmware/src/uac_fir.h); numpy only. (0.8.9: written as differences, deltas)"""
import sys
from pathlib import Path
import numpy as np
L, M, N = 160, 147, 24
FC, BETA, QB = 22000.0, 9.0, 30
FS = 44100.0
def prototype():
n = L * N
t = np.arange(n) - (n - 1) / 2.0
fc = FC / (FS * L) # cycles per sample at the up-sampled rate
h = 2 * fc * np.sinc(2 * fc * t) * np.kaiser(n, BETA)
return h / h.sum() * L # DC gain L: unity after the zero stuffing
def table():
h = prototype()
tab = np.zeros((L, N), dtype=np.int64)
for p in range(L):
c = h[p::L][::-1] # oldest input first
q = np.round(c * (1 << QB)).astype(np.int64)
q[np.argmax(np.abs(c))] += (1 << QB) - q.sum()
tab[p] = q
for p in range(L // 2): # the symmetry the C code relies on
assert (tab[L - 1 - p] == tab[p][::-1]).all() or np.abs(tab[L - 1 - p] - tab[p][::-1]).max() <= 1
return tab
def deltas(tab):
"""(0.8.9) the table as it is stored: along the phases each tap is smooth, so its third differences fit 16 bits.
Per tap: x0 (phase 0), d1 and d2 (its first and second differences at phase 0), then the 77 third differences.
usb.c uac_rs_init sums them back, exactly: 3984 bytes instead of 7680"""
h = L // 2
x0, d10, d20, d3 = [], [], [], []
for j in range(N):
c = [int(tab[p][j]) for p in range(h)]
a = [c[p + 1] - c[p] for p in range(h - 1)]
b = [a[p + 1] - a[p] for p in range(h - 2)]
t = [b[p + 1] - b[p] for p in range(h - 3)]
assert max(abs(v) for v in t) < 32768
x0.append(c[0]); d10.append(a[0]); d20.append(b[0]); d3.append(t)
for j in range(N): # the decoder, as usb.c runs it
x, a, b = x0[j], d10[j], d20[j]
for p in range(h):
assert x == int(tab[p][j])
x += a
a += b
if p < h - 3:
b += d3[j][p]
return x0, d10, d20, d3
def main():
out = Path(sys.argv[1] if len(sys.argv) > 1 else Path(__file__).resolve().parents[1] / "firmware" / "src" / "uac_fir.h")
tab = table()
x0, d10, d20, d3 = deltas(tab)
chk = sum((p * N + j + 1) * int(tab[p][j]) for p in range(L // 2) for j in range(N)) % (1 << 64)
lines = [
"/* SPDX-License-Identifier: GPL-3.0-only",
" * Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */",
"/* generated by tools/gen_uac_fir.py: do not edit. The 44.1 -> 48 kHz polyphase FIR of the USB",
f" * audio input (usb.c): L {L}, M {M}, {N} taps per phase, Kaiser beta {BETA:g}, cut-off {FC:g} Hz, Q{QB};",
f" * phases 0..{L // 2 - 1} (phase {L - 1} - p = phase p backwards), taps oldest input first.",
" * (0.8.9) Stored by tap along the phases: phase 0's value, its first and second differences, then the third",
" * differences (16 bit); usb.c uac_rs_init sums them back into the RAM table, exactly (UAC_FIR_CHK). */",
f"#define UAC_FIR_L {L}u",
f"#define UAC_FIR_M {M}u",
f"#define UAC_FIR_N {N}u",
f"#define UAC_FIR_Q {QB}",
f"#define UAC_FIR_CHK 0x{chk:016X}ull /* sum of (p * N + j + 1) * tap[p][j], phases 0..{L // 2 - 1}, mod 2^64 */",
f"static const int32_t UAC_FIR0[3][{N}] = {{",
" {" + ", ".join(str(v) for v in x0) + "},",
" {" + ", ".join(str(v) for v in d10) + "},",
" {" + ", ".join(str(v) for v in d20) + "},",
"};",
f"static const int16_t UAC_FIR3[{N}][{L // 2 - 3}] = {{",
]
for j in range(N):
lines.append(" {" + ", ".join(str(v) for v in d3[j]) + "},")
lines.append("};")
out.write_text("\n".join(lines) + "\n")
print(f"wrote {out}: {L // 2} x {N} taps, {3 * N * 4 + N * (L // 2 - 3) * 2} B (as differences; {L // 2 * N * 4} B plain)")
if __name__ == "__main__":
main()