(Underdepth) Prepare for Pass 5

This commit is contained in:
AlexKucera committed 2026-08-13 18:26:55 +02:00
1 parent 6147ab3121
commit 05e340dec2
29 files changed
-4888

No files matched your search

-108
View File
@@ -1,108 +0,0 @@
"""Resilient generated-audio manager (plan.md Audio).
Every sound is synthesized in memory from a stdlib `array` of PCM samples — no
asset files. The mixer is initialized once, lazily and defensively: if the audio
driver is missing, `mixer.init` fails, or `Sound` creation throws, audio is
disabled and the game keeps running silently. No public method ever raises.
"""
from __future__ import annotations
import array
import math
import random
try:
import pygame
_HAS_PYGAME = True
except Exception: # pragma: no cover - pygame is a hard dep, but stay safe
pygame = None
_HAS_PYGAME = False
# name -> synthesis spec: freq (Hz), dur (s), decay (envelope rate),
# kind (sine | square | noise | sweep), optional freq2 (sweep target).
_SPECS: dict[str, dict] = {
"hit": dict(freq=330, dur=0.07, decay=9.0, kind="square"),
"miss": dict(freq=180, dur=0.06, decay=10.0, kind="sine"),
"damage": dict(freq=120, dur=0.18, decay=5.0, kind="noise"),
"pickup": dict(freq=660, dur=0.10, decay=8.0, kind="sweep", freq2=990),
"use": dict(freq=520, dur=0.12, decay=7.0, kind="sweep", freq2=780),
"stairs": dict(freq=400, dur=0.16, decay=5.0, kind="sweep", freq2=600),
"levelup": dict(freq=523, dur=0.30, decay=3.0, kind="sweep", freq2=1046),
"death": dict(freq=200, dur=0.45, decay=2.5, kind="sweep", freq2=80),
"victory": dict(freq=523, dur=0.50, decay=2.0, kind="sweep", freq2=1568),
}
class AudioManager:
"""Generates and plays short SFX; degrades to silence on any audio fault."""
def __init__(self) -> None:
self.muted: bool = False
self._ok: bool = False
self._sounds: dict = {}
self._init()
def play(self, name: str) -> None:
try:
if self.muted or not self._ok:
return
snd = self._sounds.get(name)
if snd is not None:
snd.play()
except Exception:
pass
def toggle_mute(self) -> None:
self.muted = not self.muted
# -- internal ------------------------------------------------------------- #
def _init(self) -> None:
if not _HAS_PYGAME:
return
try:
if not pygame.mixer.get_init():
pygame.mixer.init()
except Exception:
self._ok = False
return
self._ok = True
for name, spec in _SPECS.items():
try:
self._sounds[name] = self._make_sound(spec)
except Exception:
pass # that one effect just won't play; others may still work
def _make_sound(self, spec: dict):
info = pygame.mixer.get_init()
if info is None:
raise RuntimeError("mixer not initialized")
rate, size, channels = info
code = "h" if abs(size) == 16 else "b"
maxv = (1 << (abs(size) - 1)) - 1
n = max(1, int(rate * spec["dur"]))
freq = spec["freq"]
freq2 = spec.get("freq2")
decay = spec["decay"]
kind = spec["kind"]
vol = 0.45
rng = random.Random(0x5EED)
buf = array.array(code)
for i in range(n):
t = i / rate
frac = i / n
f = freq + (freq2 - freq) * frac if (kind == "sweep" and freq2) else freq
if kind == "noise":
wave = rng.uniform(-1.0, 1.0)
elif kind == "square":
wave = 1.0 if math.sin(2 * math.pi * f * t) >= 0 else -1.0
else:
wave = math.sin(2 * math.pi * f * t)
env = math.exp(-decay * t)
s = int(maxv * env * wave * vol)
if s > maxv:
s = maxv
elif s < -maxv:
s = -maxv
buf.extend([s] * channels)
return pygame.mixer.Sound(buffer=buf.tobytes())