utils/music_theory/music_theory/sfx_render.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

38 lines
1.3 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki
"""Render SFX presets to a directory of 16-bit mono WAVs plus a manifest.
One ``<name>.wav`` per preset and a ``MANIFEST.json`` holding each file's
full parameter set, seed, package version and SHA-256 -- so a game can
commit the WAVs and anyone can prove a re-render reproduced them.
"""
from __future__ import annotations
from dataclasses import replace
from typing import TYPE_CHECKING
from music_theory import manifest, sfx, sfx_presets, wav
if TYPE_CHECKING:
from collections.abc import Sequence
from pathlib import Path
MANIFEST_NAME = "MANIFEST.json"
def render_all(
out_dir: Path,
sample_rate: int = sfx.SFX_SAMPLE_RATE,
names: Sequence[str] | None = None,
) -> dict[str, manifest.Entry]:
"""Render ``names`` (default: every preset) and merge them into the manifest."""
manifest_path = out_dir / MANIFEST_NAME
entries = manifest.read(manifest_path) if manifest_path.exists() else {}
for name in sfx_presets.NAMES if names is None else names:
sound = replace(sfx_presets.preset(name), sample_rate=sample_rate)
signal = sfx.render(sound)
path = wav.write(out_dir / f"{name}.wav", signal, sample_rate)
entries[path.name] = manifest.entry(sound, path, signal)
manifest.write(manifest_path, entries)
return entries