TableTower/tools/theme_sounds/make_theme_sounds.py
RobinFiredrake 61a548902f Theme tiers 1-2: theme folders, mascot recolour, background scene and tile, frame sets, picture threads, sounds; nine new built-in themes
- ThemeAssets loads a theme's files; FrameSet + GradientMap (3-stop) generalise the card frame;
  ThreadStrip draws threads from a picture; BoardBackdrop draws scene and tile; MascotArt
  recolours Morgan by role (logo SVG now uses the Keep File importer); SoundBoard plays
  roll / card turn / thread / click / ambient with View > Sound levels.
- Vaporwave, Synthwave, Cozy Autumn, Rustic, Woodsy, Sky Blue, Frutiger Aero, Gothic, Terminal:
  generated art (tools/theme_art), sounds (tools/theme_sounds), settings (tools/theme_defs).
- Ten OFL fonts; --theme=<id> flag; docs/THEMES.md format spec; test_theme_assets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:05:40 -06:00

90 lines
3.4 KiB
Python

"""Render each theme's stand-in sounds to themes/<id>/sounds/*.ogg.
Run from anywhere: python3 -I tools/theme_sounds/make_theme_sounds.py
The sounds are ports of the WebAudio recipes in the theme mockup. They are
placeholders until real recordings replace them.
"""
import pathlib
import subprocess
import sys
import zlib
import numpy
TOOL_FOLDER = pathlib.Path(__file__).resolve().parent
sys.path.insert(0, str(TOOL_FOLDER)) # `-I` leaves the script folder off the path
from recipes import RECIPES # noqa: E402
from synth import SAMPLE_RATE, Mix # noqa: E402
REPO_ROOT = TOOL_FOLDER.parent.parent
# Theme id -> recipe for each sound slot. Slot names are the output file names.
THEME_SOUNDS = {
"tarot": {"roll": "harp", "card_turn": "swish"},
"vaporwave": {"roll": "dreamy", "card_turn": "tapestop"},
"synthwave": {"roll": "synth", "card_turn": "cassette"},
"autumn": {"roll": "windchime", "card_turn": "rustle"},
"rustic": {"roll": "clack", "card_turn": "creak"},
"woodsy": {"roll": "drops", "card_turn": "leafswish"},
"sky": {"roll": "airchime", "card_turn": "whoosh"},
"aero": {"roll": "bubbles", "card_turn": "tink"},
"gothic": {"roll": "bell", "card_turn": "scrape"},
"terminal": {"roll": "beeps", "card_turn": "relay"},
}
PEAK_DBFS = -3.0
FADE_IN_SECONDS = 0.005
FADE_OUT_SECONDS = 0.03
SILENCE_DBFS = -60.0 # quieter than this, relative to full scale, counts as silence
MAX_SECONDS = 2.5
MAX_SECONDS_BY_RECIPE = {"bell": 3.0}
def render(recipe_name):
"""Run one recipe with a seed taken from its name, so re-runs match."""
mix = Mix(seed=zlib.crc32(recipe_name.encode()))
RECIPES[recipe_name](mix)
return mix.samples
def finish(samples, max_seconds):
"""Normalise the peak, trim the silent tail, cap the length, add fades."""
samples = samples * (10.0 ** (PEAK_DBFS / 20.0) / numpy.max(numpy.abs(samples)))
loud_enough = numpy.nonzero(numpy.abs(samples) > 10.0 ** (SILENCE_DBFS / 20.0))[0]
end_index = min(loud_enough[-1] + 1, int(max_seconds * SAMPLE_RATE))
samples = samples[:end_index].copy()
fade_in_length = int(FADE_IN_SECONDS * SAMPLE_RATE)
samples[:fade_in_length] *= numpy.linspace(0.0, 1.0, fade_in_length)
fade_out_length = min(int(FADE_OUT_SECONDS * SAMPLE_RATE), len(samples))
samples[-fade_out_length:] *= numpy.linspace(1.0, 0.0, fade_out_length)
return samples
def write_ogg(samples, output_path):
output_path.parent.mkdir(parents=True, exist_ok=True)
command = [
"ffmpeg", "-y", "-loglevel", "error",
"-f", "f32le", "-ar", str(SAMPLE_RATE), "-ac", "1", "-i", "pipe:0",
"-c:a", "libvorbis", "-q:a", "4", "-map_metadata", "-1",
"-fflags", "+bitexact", "-flags:a", "+bitexact",
str(output_path),
]
subprocess.run(command, input=samples.astype("<f4").tobytes(), check=True)
def main():
for theme_id, slots in THEME_SOUNDS.items():
for slot_name, recipe_name in slots.items():
max_seconds = MAX_SECONDS_BY_RECIPE.get(recipe_name, MAX_SECONDS)
samples = finish(render(recipe_name), max_seconds)
output_path = REPO_ROOT / "themes" / theme_id / "sounds" / (slot_name + ".ogg")
write_ogg(samples, output_path)
seconds = len(samples) / SAMPLE_RATE
print(f"{output_path.relative_to(REPO_ROOT)} {recipe_name:<10} {seconds:.2f} s")
if __name__ == "__main__":
main()