utils/music_theory/music_theory/sfx_presets.py
Krzysztof kuhy Rudnicki 26c55675dd music_theory: sfx module -- noise, sweeps, biquads and 22 spellcode presets
Code-generated sound effects for spellcode (no AI audio, no sfxr port):
seeded white/pink noise and grain gates (noise), a phase-accumulating
oscillator over glide/vibrato/arpeggio/jump/retrigger pitch curves plus
ring mod (sweep), exponential and punch envelopes (envelopes), resonant
RBJ low/high/band-pass with a moving cutoff (biquad), and an sfxr-style
layered parameter set rendered at 44.1 kHz by default (sfx).

Presets: cast/hit/impact for arcane, fire, water, lightning, earth and
wind, plus dodge, parry_normal, parry_perfect and fizzle, following R1's
per-element recipes (arcane's is new). `python -m music_theory sfx --out
DIR` writes 16-bit mono WAVs and a SHA-256 manifest; manifest.entry now
takes any Recordable (Recipe or Sfx). analysis gains band_energy and
spectral_slope, which the tests use as the oracle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016o4CqWFZwpfiDj8csAPgLa
2026-10-19 11:27:49 +02:00

108 lines
3.2 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki
"""SFX presets: every sound spellcode needs, by name.
Six elements (spellcode's v1 set: arcane, fire, water, lightning, earth,
wind) times three beats (cast, hit, impact), plus the moves: dodge, a
normal and a perfect parry, and the fizzle of a spell that failed.
The moves follow sfxr's rule of thumb -- a down-slide is failure or
impact, an up-slide is reward: the perfect parry rings higher and longer
than the normal one and climbs a fifth, the fizzle slides down into a
crushed sputter.
"""
from __future__ import annotations
from dataclasses import replace
from music_theory.envelopes import Shape
from music_theory.sfx import Filter, Grit, Layer, Sfx, Tone
from music_theory.sfx_layers import chime, crackle, make, transient, whoosh
from music_theory.sfx_presets_a import ARCANE, FIRE, WATER
from music_theory.sfx_presets_b import EARTH, LIGHTNING, WIND
from music_theory.sweep import Pitch
ELEMENTS: tuple[str, ...] = ("arcane", "fire", "water", "lightning", "earth", "wind")
MOVE_PEAK = 0.6
## Inharmonic partials of a struck bar, the "chime" in R1's ice recipe.
_BAR_PARTIALS = (1.0, 2.76, 5.40)
_PERFECT_HZ = 1600.0
MOVES: tuple[Sfx, ...] = (
make(
"dodge",
replace(
whoosh((500.0, 2500.0, 700.0), Shape(attack=0.04, hold=0.04, decay=0.1)),
tone=Tone("pink"),
),
peak=MOVE_PEAK,
),
make(
"parry_normal",
transient(0.01, 3000.0, 0.7),
Layer(
Tone("triangle", pitch=Pitch(hz=(900.0,))),
Shape(attack=0.001, hold=0.01, punch=0.5, decay=0.22, curve=6.0),
grit=Grit(ring_hz=370.0),
),
Layer(
Tone("square", duty=0.3, pitch=Pitch(hz=(1400.0,))),
Shape(attack=0.001, hold=0.0, decay=0.12),
grit=Grit(ring_hz=530.0),
gain=0.3,
),
peak=MOVE_PEAK,
),
make(
"parry_perfect",
transient(0.01, 4000.0, 0.6),
*(
chime(_PERFECT_HZ * ratio, 0.5, 0.6 / (index + 1))
for index, ratio in enumerate(_BAR_PARTIALS)
),
chime(_PERFECT_HZ * 1.5, 0.45, 0.4, delay=0.06),
Layer(
Tone("square", duty=0.25, pitch=Pitch(hz=(2000.0, 4000.0))),
Shape(attack=0.01, hold=0.04, decay=0.08),
Filter("lowpass", (6000.0,)),
gain=0.15,
),
peak=0.8,
),
make(
"fizzle",
Layer(
Tone("saw", pitch=Pitch(hz=(500.0, 90.0))),
Shape(attack=0.01, hold=0.1, decay=0.25),
Filter("lowpass", (2500.0, 400.0)),
Grit(crush_bits=4.0),
),
crackle(
30.0,
Shape(attack=0.0, hold=0.15, decay=0.15),
Filter("highpass", (1000.0,)),
0.4,
),
peak=MOVE_PEAK,
),
)
PRESETS: dict[str, Sfx] = {
sound.name: sound
for group in (ARCANE, FIRE, WATER, LIGHTNING, EARTH, WIND, MOVES)
for sound in group
}
NAMES: tuple[str, ...] = tuple(PRESETS)
def preset(name: str) -> Sfx:
"""The preset called ``name``."""
if name not in PRESETS:
msg = f"unknown preset {name!r}; choose one of {NAMES}"
raise KeyError(msg)
return PRESETS[name]