Files
clipper-fm1/tools/gen_vector.py
T
isod89andClaude Opus 5.5 2217b8ec47 Clipper 0.8: JUNO and its chorus, VECTOR, MOD SEQ, SPICE / DICE
- JUNO engine (21): DCO saw / pulse (PWM) / both, sub, noise, the 106's four
  HPF positions, the 24 dB ladder, and the stereo BBD chorus I / II / I+II
  (fx.c juno_chorus, after the INSERT and FILTER); 16 sounds
- the JUNO chorus on any sound: INSERT types JUNO1, JUNO2, JUN12
  (WIDTH, TONE, NOISE)
- VECTOR engine (22): four band-limited waves at the corners of a square
  (16 own waves, tools/gen_vector.py), X / Y, MOVE paths (CIRC, 8, SWEEP,
  LOOP, RND, slow / fast); 16 sounds
- both new engines split across the two cores (bit for bit)
- MOD SEQ: 4 lanes x 16 steps per sound, RATE LEN SMTH START (SYNC / KEY),
  matrix sources SEQ1..SEQ4; MOD SEQ / MOD SEQ 2 pages with SHAPE fills;
  kept in UNDO, COMPARE, SWAP, user presets (note / flags + the bank's side
  record) and projects (free lock slots of tracks 3 and 4)
- SPICE / DICE: deterministic variations (rests, octaves, velocity, gate)
  of the arp's notes and the sequencer's steps
- editor protocol v13: MSEQ_GET / MSEQ_PUT; the editor's MOD SEQ card, the
  new pages, JUNO and VECTOR in the mock
- fix: UP_PUT from the editor wrote SLOOP's layout over a Clipper record
  (and past it)
- tests: synth8, mseq_ui; docs, version 0.8

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RDUenQzjQ4Kk2jQVG85bwq
2026-10-17 00:01:27 -04:00

137 lines
5.2 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (C) 2026 3dSam (Clipper)
"""VECTOR's waves (firmware/src/eng_vector.c, Clipper 0.8): 16 single cycles of Clipper's own, each in three
band-limited copies (the voice reads the one whose top harmonic stays under ~20 kHz at its pitch):
512 points, harmonics to the 96th (notes to ~208 Hz), 128 points to the 24th (~833 Hz), 32 points to the 6th.
Written to firmware/src/vector_tables.h (committed: no build step). Usage: python3 tools/gen_vector.py [out.h]
Each wave is a spectrum (harmonic -> amplitude, phase), made here from simple recipes (no samples of anyone's); the
three copies share one scale (the loudest copy's peak 0.9), so a note crossing a copy's range keeps its level."""
import math
import random
import sys
TAU = 2 * math.pi
LEVELS = [(512, 96), (128, 24), (32, 6)]
def soft(k, nh): # a gentle roll-off at the copy's top (no ringing edge)
return 0.5 + 0.5 * math.cos(math.pi * k / (nh + 1))
def spec_saw(k):
return 1.0 / k, 0.0
def spec_sqr(k):
return (1.0 / k if k % 2 else 0.0), 0.0
def spec_pulse(w):
return lambda k: (math.sin(math.pi * k * w) / k, -math.pi / 2)
def spec_tri(k):
return ((1.0 / (k * k)) * (1 if (k // 2) % 2 == 0 else -1) if k % 2 else 0.0), 0.0
def spec_sine(k):
return (1.0 if k == 1 else 0.0), 0.0
def spec_organ(k): # drawbars 16' 8' 4' 2 2/3' 2' (the fundamental here = 8')
bars = {1: 1.0, 2: 0.8, 3: 0.55, 4: 0.5, 6: 0.3, 8: 0.25}
return bars.get(k, 0.0), 0.0
def formant(centres):
def f(k):
a = 0.0
for c, bw, g in centres:
a += g * math.exp(-((k - c) / bw) ** 2)
return a / math.sqrt(k), 0.0
return f
def spec_bell(k): # harmonic stand-ins for a bell's partials (2, 3, 4.2 -> 4, 5.4 -> 5, 8)
return {1: 0.6, 2: 1.0, 3: 0.7, 4: 0.45, 5: 0.5, 8: 0.35, 11: 0.2, 16: 0.12}.get(k, 0.0), 0.0
def spec_hollow(k): # odd harmonics, slowly falling (a clarinet's idea)
return ((1.0 / k ** 0.7) if k % 2 else 0.04 / k), 0.0
def spec_bright(k): # a saw with its upper half lifted (a buzzy brass)
return (1.0 / k) * (1.0 + 1.5 * math.exp(-((k - 9) / 5.0) ** 2)), 0.0
def spec_reso(k): # a resonant peak at the 8th harmonic (a swept filter frozen)
return (1.0 / k) * (1.0 + 6.0 * math.exp(-((k - 8) / 1.5) ** 2)), 0.0
def spec_digi(): # a PPG-like digital spectrum: random levels and phases, fixed seed
rnd = random.Random(808)
lv = [rnd.random() ** 2 for _ in range(97)]
ph = [rnd.random() * TAU for _ in range(97)]
return lambda k: (lv[k] / math.sqrt(k), ph[k])
def spec_string(k): # a saw through a comb (the bowed string's notches)
return (1.0 / k) * abs(math.sin(math.pi * k * 0.18)) * 1.3, 0.0
def spec_metal(k): # sparse high partials (a struck metal)
return {1: 0.5, 3: 0.3, 7: 0.6, 9: 0.5, 13: 0.45, 17: 0.4, 19: 0.3, 23: 0.35, 29: 0.25, 31: 0.2}.get(k, 0.0), 0.0
def spec_glass(k): # a sine with a soft octave and twelfth (a glassy pad)
return {1: 1.0, 2: 0.35, 3: 0.25, 4: 0.1, 6: 0.15, 12: 0.06}.get(k, 0.0), 0.0
WAVES = [
("SINE", spec_sine), ("TRI", spec_tri), ("SAW", spec_saw), ("SQR", spec_sqr), ("PLS25", spec_pulse(0.25)),
("PLS10", spec_pulse(0.10)), ("ORGAN", spec_organ), ("VOX A", formant([(7, 2.0, 1.0), (13, 3.0, 0.6)])),
("VOX O", formant([(4, 1.5, 1.0), (8, 2.0, 0.4)])), ("BELL", spec_bell), ("HOLLW", spec_hollow),
("BRASS", spec_bright), ("RESO", spec_reso), ("DIGI", spec_digi()), ("STRNG", spec_string), ("METAL", spec_metal),
]
def render(spec, n, nh):
out = [0.0] * n
for k in range(1, nh + 1):
a, ph = spec(k)
if not a:
continue
a *= soft(k, nh) if nh > 6 else 1.0
for i in range(n):
out[i] += a * math.sin(TAU * k * i / n + ph)
return out
def main():
out = sys.argv[1] if len(sys.argv) > 1 else "firmware/src/vector_tables.h"
rows = []
for name, spec in WAVES:
copies = [render(spec, n, nh) for n, nh in LEVELS]
pk = max(max(abs(x) for x in c) for c in copies)
g = 0.9 * 32767 / pk
rows.append((name, [[int(round(x * g)) for x in c] for c in copies]))
lines = ["/* generated by tools/gen_vector.py: VECTOR's waves, 16 x (512 + 128 + 32) int16 (eng_vector.c) */",
"#pragma once",
"#define VEC_NW 16u",
"#define VEC_PTS (512u + 128u + 32u)",
"static const char *const N_VEC_WAVE[VEC_NW] = {%s};" % ", ".join('"%s"' % n for n, _ in rows),
"static const int16_t VEC_WAVE[VEC_NW][VEC_PTS] = {"]
for name, copies in rows:
lines.append(" { /* %s */" % name)
flat = copies[0] + copies[1] + copies[2]
for i in range(0, len(flat), 16):
lines.append(" " + ", ".join(str(x) for x in flat[i:i + 16]) + ",")
lines.append(" },")
lines.append("};")
open(out, "w").write("\n".join(lines) + "\n")
if __name__ == "__main__":
main()