Generated sound effects, made from code instead of sound files

libstormgames.sfx makes sound effects with numpy and scipy, so a game can
use them without shipping sound files, and every game that asks for the
same sound with the same seed gets exactly the same samples. The sounds
come from Grave Racer and match it sample for sample.

The modules are synth (waves, filters, envelopes, loops), impacts, ui,
rewards, materials, creatures, babble, vehicles, ambience and alerts.
sfx.make_sound turns a sound into a pygame Sound for the mixer as it is
set up, turning down only a sound that would go past full scale.
sfx.add_sounds adds generated sounds by name, keeping any sound file of
the same name.

The menus' menu-move and menu-select sounds are generated when a game
has no file for them. sfx and scipy are only imported then, so a game
that ships both files, or never uses sfx, is not slowed down or made to
need scipy.

The tests check every sound against a stored fingerprint, so none can
change by accident; tools/update-sfx-fingerprints.py remakes them after
a deliberate change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Storm DragonandClaude Opus 5.5 committed 2026-11-28 15:03:48 -05:00
1 parent 31e7472fd1
commit f164697cf6
19 files changed
+2084

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
"""Interface sounds: beeps, menu moves, confirmations and on/off sweeps."""
import math
import numpy as np
from .synth import sampleRate, soften, square_wave
def beep(frequency, seconds):
"""A clean beep with a touch of its octave, clickless at both ends."""
times = np.arange(int(sampleRate * seconds)) / sampleRate
envelope = np.minimum(1.0, times / 0.005) * np.minimum(1.0, (seconds - times) / 0.03)
tone = np.sin(2.0 * math.pi * frequency * times) + 0.3 * np.sin(4.0 * math.pi * frequency * times)
return 0.25 * tone * envelope
def fanfare():
"""A quick rising C major arpeggio of beeps, holding the top note."""
notes = [523.3, 659.3, 784.0, 1046.5]
parts = [beep(frequency, 0.14) for frequency in notes[:-1]] + [beep(notes[-1], 0.5)]
return np.concatenate(parts)
def toggle_sweep(rising, seconds=0.14, low=500.0, high=1000.0):
"""A short triangle tone gliding up an octave for something turning on, or down for turning off."""
times = np.arange(int(sampleRate * seconds)) / sampleRate
progress = times / seconds
if not rising:
progress = 1.0 - progress
frequency = low * (high / low) ** progress
phases = np.cumsum(frequency) / sampleRate
triangle = 2.0 * np.abs(2.0 * (phases % 1.0) - 1.0) - 1.0
envelope = np.minimum(1.0, times / 0.005) * np.minimum(1.0, (seconds - times) / 0.03)
return 0.3 * triangle * envelope
def denied_click():
"""A dull click: nothing to use, or not allowed."""
times = np.arange(int(sampleRate * 0.06)) / sampleRate
return 0.3 * np.sin(2.0 * math.pi * 180.0 * times) * np.exp(-times / 0.012)
def menu_move():
"""Moving between menu items: a short hollow knock, like tapping a bone."""
times = np.arange(int(sampleRate * 0.08)) / sampleRate
tone = np.sin(2.0 * math.pi * 620.0 * times) + 0.4 * np.sin(2.0 * math.pi * 1710.0 * times)
return 0.2 * tone * np.exp(-times / 0.018) * np.minimum(1.0, times / 0.001)
def confirm_chime(step=0):
"""Choosing or confirming: a short low square blip, then a tone an octave up that fades slowly away.
step raises it by that many semitones, so a count can be heard, such as
one step higher for each item held.
"""
lift = 2.0 ** (step / 12.0)
blip = square_wave(500.0 * lift, 0.07)
tone = square_wave(1000.0 * lift, 0.8)
# The tail falls steeply at first, then lingers quietly.
tailProgress = np.arange(len(tone)) / len(tone)
tone *= (1.0 - tailProgress) ** 2.5
chime = np.concatenate((blip, tone))
# Soften the very start and the join, so neither clicks.
attack = int(sampleRate * 0.003)
chime[:attack] *= np.linspace(0.0, 1.0, attack)
return 0.22 * soften(chime)