Files
windroost/tools/create_temporary_sfx.py
T
wushenghua f894d5ed9f audio: add temporary slice SFX and make the mute setting real
The settings page had a mute flag that changed nothing and the game had no
audio at all. create_temporary_sfx.py generates eight deterministic 16-bit
WAV cues covering the priority feedback in PRODUCTION/08-audio.md: click,
hit, shield break, ultimate, victory, defeat, upgrade and UI. They are marked
temporary in their manifest; the 80-120 authored SFX and nine BGM in the
contract are still final production work.

SfxPlayer uses four players to respect the "at most 3-4 simultaneous" rule,
keeps high-frequency cues quieter, and treats mute as a hard gate that stops
active players before starting new ones. The same authoritative battle events
that select a VFX now select its audio cue, so feedback cannot disagree with
the simulation. GameRoot plays UI, upgrade, victory and defeat cues.

tools/run_p2_checks.sh now runs 16 gates; all pass. The headless audio test
verifies all eight streams load, mute suppresses playback, and boss events
produce click/hit/shield-break/ultimate cues.
2026-11-17 02:53:22 +08:00

140 lines
4.3 KiB
Python

#!/usr/bin/env python3
"""Generate a small deterministic temporary SFX set for the playable slice.
The audio contract (PRODUCTION/08-audio.md) calls for 80-120 authored SFX and
nine BGM. That is final production work, not something to fake here. These
eight short waveforms are *labelled temporary* in their manifest and exist so
the priority feedback (hit, shield break, ultimate, reward) is audible and the
mute setting has a real effect. Replace them with authored audio before ship.
All files are 22050Hz mono 16-bit WAV, generated with plain stdlib math.
"""
from __future__ import annotations
import hashlib
import json
import math
import random
import struct
import wave
from pathlib import Path
from typing import Callable, Dict, List
OUT_DIR = Path("PRODUCTION/source/pixel_pilot/audio")
RATE = 22050
def envelope(t: float, duration: float, attack: float = 0.01) -> float:
if t < attack:
return t / attack
remaining = duration - t
return max(0.0, min(1.0, remaining / (duration * 0.55)))
def render(duration: float, sample: Callable[[float, int], float]) -> bytes:
frames = bytearray()
count = int(duration * RATE)
for index in range(count):
t = index / RATE
value = max(-1.0, min(1.0, sample(t, index)))
frames.extend(struct.pack("<h", int(value * 32767)))
return bytes(frames)
def tone(freq: float, volume: float = 0.35):
return lambda t, i: math.sin(2 * math.pi * freq * t) * volume
def click(t: float, i: int) -> float:
return (math.sin(2 * math.pi * 880 * t) * 0.22 if (i // 90) % 2 == 0 else -0.12) * envelope(t, 0.07)
def hit(t: float, i: int) -> float:
noise = random.Random(i).uniform(-1, 1) * 0.18
return (math.sin(2 * math.pi * 180 * t) * 0.32 + noise) * envelope(t, 0.13)
def shield_break(t: float, i: int) -> float:
metallic = (
math.sin(2 * math.pi * 740 * t)
+ math.sin(2 * math.pi * 1117 * t) * 0.7
+ math.sin(2 * math.pi * 1609 * t) * 0.45
)
noise = random.Random(i + 7).uniform(-1, 1) * 0.12
return (metallic * 0.16 + noise) * envelope(t, 0.26)
def ultimate(t: float, i: int) -> float:
sweep = 220 + 900 * min(1.0, t / 0.35)
chord = math.sin(2 * math.pi * sweep * t) * 0.24
chord += math.sin(2 * math.pi * (sweep * 1.5) * t) * 0.12
return chord * envelope(t, 0.42)
def victory(t: float, i: int) -> float:
notes = [523.25, 659.25, 783.99, 1046.5]
note = notes[min(3, int(t / 0.12))]
return math.sin(2 * math.pi * note * t) * 0.22 * envelope(t, 0.5)
def defeat(t: float, i: int) -> float:
note = 440 - 180 * min(1.0, t / 0.4)
return math.sin(2 * math.pi * note * t) * 0.24 * envelope(t, 0.42)
def upgrade(t: float, i: int) -> float:
note = 600 if t < 0.1 else 900
return math.sin(2 * math.pi * note * t) * 0.22 * envelope(t, 0.2)
def ui(t: float, i: int) -> float:
return math.sin(2 * math.pi * 1200 * t) * 0.14 * envelope(t, 0.05)
SFX: Dict[str, tuple] = {
"click": (0.07, click),
"hit": (0.13, hit),
"shield_break": (0.26, shield_break),
"ultimate": (0.42, ultimate),
"victory": (0.50, victory),
"defeat": (0.42, defeat),
"upgrade": (0.20, upgrade),
"ui": (0.05, ui),
}
def main() -> int:
OUT_DIR.mkdir(parents=True, exist_ok=True)
manifest = {
"schema": 1,
"status": "temporary_placeholder",
"sample_rate": RATE,
"channels": 1,
"sample_width": 2,
"note": "Deterministic placeholder waveforms for slice feedback. Replace with authored SFX from PRODUCTION/08-audio.md before release.",
"effects": {},
}
for name, (duration, fn) in SFX.items():
path = OUT_DIR / f"{name}.wav"
data = render(duration, fn)
with wave.open(str(path), "wb") as handle:
handle.setnchannels(1)
handle.setsampwidth(2)
handle.setframerate(RATE)
handle.writeframes(data)
manifest["effects"][name] = {
"file": path.name,
"duration_seconds": duration,
"sha256": hashlib.sha256(path.read_bytes()).hexdigest(),
}
print(f"sfx {name}: {duration:.2f}s -> {path}")
(OUT_DIR / "sfx_manifest.json").write_text(
json.dumps(manifest, ensure_ascii=False, indent=2) + "\n", encoding="utf-8"
)
return 0
if __name__ == "__main__":
raise SystemExit(main())