mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-24 07:46:27 +01:00
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
78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki
|
|
"""SFX layers: the recurring building blocks of a spell sound, named.
|
|
|
|
A hit is a TRANSIENT (5-20 ms of noise that sells the contact), a BODY
|
|
(the element's identity), a TAIL (filtered noise dying away) and, for
|
|
heavy hits, a SUB (a low sine falling in pitch). These helpers build the
|
|
recurring layers so a preset reads as its recipe rather than as twenty
|
|
constructor arguments.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import zlib
|
|
|
|
from music_theory import synth
|
|
from music_theory.envelopes import Shape
|
|
from music_theory.sfx import Filter, Grit, Layer, Sfx, Tone
|
|
from music_theory.sweep import Pitch
|
|
|
|
|
|
def make(name: str, *layers: Layer, peak: float = synth.DEFAULT_PEAK) -> Sfx:
|
|
"""A named sound seeded from its name (crc32 is stable across runs)."""
|
|
return Sfx(name, layers, seed=zlib.crc32(name.encode()), peak=peak)
|
|
|
|
|
|
def transient(
|
|
seconds: float = 0.01, high_hz: float = 2500.0, gain: float = 0.8
|
|
) -> Layer:
|
|
"""A click of high-passed white noise: the moment of contact."""
|
|
return Layer(
|
|
Tone("noise"),
|
|
Shape(attack=0.0, hold=0.002, decay=seconds, curve=6.0),
|
|
Filter("highpass", (high_hz,)),
|
|
gain=gain,
|
|
)
|
|
|
|
|
|
def sub(start_hz: float, end_hz: float, seconds: float, gain: float = 0.9) -> Layer:
|
|
"""A sine dropping in pitch: weight you feel more than hear."""
|
|
return Layer(
|
|
Tone("sine", pitch=Pitch(hz=(start_hz, end_hz))),
|
|
Shape(attack=0.003, hold=0.0, decay=seconds, curve=3.0),
|
|
gain=gain,
|
|
)
|
|
|
|
|
|
def whoosh(
|
|
hz: tuple[float, ...], shape: Shape, q: float = 2.0, gain: float = 1.0
|
|
) -> Layer:
|
|
"""Noise through a band-pass whose centre glides through ``hz``."""
|
|
return Layer(Tone("noise"), shape, Filter("bandpass", hz, q), gain=gain)
|
|
|
|
|
|
def rumble(cutoff_hz: float, shape: Shape, gain: float = 0.7) -> Layer:
|
|
"""Low-passed white noise: a tail, thunder, the thud under a hit.
|
|
|
|
White rather than pink: pink's 1/f puts most of its energy below
|
|
20 Hz, which no speaker plays and every limiter still counts.
|
|
"""
|
|
return Layer(Tone("noise"), shape, Filter("lowpass", (cutoff_hz,)), gain=gain)
|
|
|
|
|
|
def crackle(
|
|
rate_hz: float, shape: Shape, filtering: Filter, gain: float = 0.6
|
|
) -> Layer:
|
|
"""Sparse 2 ms noise grains at ``rate_hz``: fire crackle, gravel rattle."""
|
|
return Layer(Tone("noise"), shape, filtering, Grit(grain_rate=rate_hz), gain=gain)
|
|
|
|
|
|
def chime(hz: float, seconds: float, gain: float = 0.6, delay: float = 0.0) -> Layer:
|
|
"""A struck sine with an exponential ring-out."""
|
|
return Layer(
|
|
Tone("sine", pitch=Pitch(hz=(hz,))),
|
|
Shape(attack=0.002, hold=0.0, decay=seconds, curve=4.0),
|
|
gain=gain,
|
|
delay=delay,
|
|
)
|