Generated stand-in art and sounds, ten more OFL fonts, PNG tiles in the theme generator; untrack __pycache__. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
99 lines
3.9 KiB
Python
99 lines
3.9 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"},
|
|
"noir": {"roll": "typewriter", "card_turn": "slap"},
|
|
"eldritch": {"roll": "drone", "card_turn": "wetpage"},
|
|
"comic": {"roll": "slide", "card_turn": "whack"},
|
|
"brass": {"roll": "ratchet", "card_turn": "hiss"},
|
|
"chrome": {"roll": "glitch", "card_turn": "blip"},
|
|
"starchart": {"roll": "sonar", "card_turn": "hud"},
|
|
"map": {"roll": "quill", "card_turn": "unfold"},
|
|
"story": {"roll": "xylo", "card_turn": "pageturn"},
|
|
"web1": {"roll": "chip", "card_turn": "mouse"},
|
|
}
|
|
|
|
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} # drone fits the 2.5 s default
|
|
|
|
|
|
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()
|