Files
First-Person-Maze/tools/generate_audio.py
2026-10-27 19:02:27 +01:00

39 lines
1.3 KiB
Python

"""Generate original mono PCM effects using only the Python standard library.
Python is a development convenience; it is never needed to play the game.
"""
from pathlib import Path
import math
import struct
import wave
ROOT = Path(__file__).resolve().parents[1] / "assets" / "audio"
CUES = {
"pellet": ([880, 1320], 0.035),
"power": ([330, 440, 660, 880], 0.09),
"frightened": ([165, 220, 165], 0.10),
"defeated": ([660, 880, 1320], 0.07),
"death": ([660, 550, 440, 330, 165], 0.12),
"victory": ([440, 554.37, 659.25, 880, 659.25, 880], 0.16),
"game_over": ([330, 293.66, 261.63, 164.81], 0.22),
}
def main():
ROOT.mkdir(parents=True, exist_ok=True)
rate = 22050
for name, (notes, duration) in CUES.items():
samples = bytearray()
for frequency in notes:
count = int(rate * duration)
for i in range(count):
envelope = min(i / (rate * 0.005), 1) * (1 - i / count) ** 1.5
tone = math.sin(2 * math.pi * frequency * i / rate)
samples.extend(struct.pack("<h", int(tone * envelope * 6500)))
with wave.open(str(ROOT / f"{name}.wav"), "wb") as output:
output.setnchannels(1)
output.setsampwidth(2)
output.setframerate(rate)
output.writeframes(samples)
if __name__ == "__main__":
main()