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>
100 lines
4.5 KiB
Python
100 lines
4.5 KiB
Python
"""Impacts and weather: thunder, explosions, booms, thuds and things smashing.
|
|
|
|
Each takes a seed: the same seed always gives the same sound, a different seed
|
|
a variant of it, and None a new variant every time.
|
|
"""
|
|
|
|
import math
|
|
|
|
import numpy as np
|
|
|
|
from .synth import Biquad, blockSize, rng_from, sampleRate, sweep_filtered
|
|
|
|
|
|
def thunder(seed=0):
|
|
"""A crack of thunder and a long rolling rumble, about three seconds."""
|
|
rng = rng_from(seed)
|
|
length = int(sampleRate * 3.0)
|
|
times = np.arange(length) / sampleRate
|
|
blockCount = (length + blockSize - 1) // blockSize
|
|
blockTimes = np.arange(blockCount) * blockSize / sampleRate
|
|
# The rumble rolls: its brightness rises and falls a few times as it dies away.
|
|
centres = 120.0 + 900.0 * np.exp(-blockTimes / 0.25) + 150.0 * (1.0 + np.sin(2.0 * math.pi * 1.7 * blockTimes))
|
|
rumble = sweep_filtered(rng.standard_normal(length), "lowpass", centres, 0.8)
|
|
roll = 0.6 + 0.4 * np.sin(2.0 * math.pi * 1.3 * times + 1.0) ** 2
|
|
rumble *= np.minimum(1.0, times / 0.02) * np.exp(-times / 1.0) * roll
|
|
crack = Biquad("highpass").process(rng.standard_normal(length), 800.0) * np.exp(-times / 0.05)
|
|
return np.tanh(1.5 * (0.9 * rumble + 0.4 * crack)) * 0.8
|
|
|
|
|
|
def explosion(seed=0):
|
|
"""A crack and a rumbling boom, a little under a second and a half."""
|
|
rng = rng_from(seed)
|
|
length = int(sampleRate * 1.4)
|
|
sampleIndex = np.arange(length)
|
|
times = sampleIndex / sampleRate
|
|
noise = rng.standard_normal(length)
|
|
boomFilter = Biquad("lowpass")
|
|
boom = np.zeros(length)
|
|
for blockStart in range(0, length, blockSize):
|
|
blockEnd = min(blockStart + blockSize, length)
|
|
cutoff = 3000.0 * math.exp(-blockStart / sampleRate * 4.0) + 150.0
|
|
boom[blockStart:blockEnd] = boomFilter.process(noise[blockStart:blockEnd], cutoff, 0.8)
|
|
boom *= np.exp(-times / 0.4)
|
|
thump = np.sin(2.0 * math.pi * 55.0 * times) * np.exp(-times / 0.15)
|
|
crack = noise * np.exp(-times / 0.01)
|
|
return 0.45 * boom + 0.4 * thump + 0.15 * crack
|
|
|
|
|
|
def heavy_boom(seed=0):
|
|
"""Something huge coming to rest: a deep thump that drops in pitch, then a long rolling rumble."""
|
|
rng = rng_from(seed)
|
|
length = int(sampleRate * 2.2)
|
|
times = np.arange(length) / sampleRate
|
|
noise = rng.standard_normal(length)
|
|
# The thump falls from 95 to 32 cycles a second, felt as much as heard.
|
|
thumpPitch = 32.0 + 63.0 * np.exp(-times / 0.08)
|
|
thump = np.sin(2.0 * math.pi * np.cumsum(thumpPitch) / sampleRate) * np.exp(-times / 0.35)
|
|
rumbleFilter = Biquad("lowpass")
|
|
rumble = np.zeros(length)
|
|
for blockStart in range(0, length, blockSize):
|
|
blockEnd = min(blockStart + blockSize, length)
|
|
cutoff = 1800.0 * math.exp(-blockStart / sampleRate * 3.0) + 90.0
|
|
rumble[blockStart:blockEnd] = rumbleFilter.process(noise[blockStart:blockEnd], cutoff, 0.9)
|
|
rumble *= np.exp(-times / 0.7)
|
|
crack = noise * np.exp(-times / 0.015)
|
|
attack = np.minimum(1.0, times / 0.002)
|
|
# Driving it into soft clipping gives the grit, and keeps it loud without harsh peaks.
|
|
boom = np.tanh(1.8 * (1.0 * thump + 0.8 * rumble + 0.25 * crack)) * attack
|
|
return 0.95 * boom
|
|
|
|
|
|
def thud(seed=0):
|
|
"""Two solid things bumping: a dull knock with a scrape."""
|
|
rng = rng_from(seed)
|
|
length = int(sampleRate * 0.25)
|
|
sampleIndex = np.arange(length)
|
|
knock = np.sin(2.0 * math.pi * 70.0 * sampleIndex / sampleRate) * np.exp(-sampleIndex / (sampleRate * 0.05))
|
|
scrape = Biquad("bandpass").process(rng.standard_normal(length), 1200.0, 1.0)
|
|
scrape *= np.exp(-sampleIndex / (sampleRate * 0.08))
|
|
return 0.6 * knock + 0.25 * scrape
|
|
|
|
|
|
def shatter(seed=0):
|
|
"""Something brittle hitting the ground: a hard crack and a thud, then pieces clattering away."""
|
|
rng = rng_from(seed)
|
|
length = int(sampleRate * 0.6)
|
|
times = np.arange(length) / sampleRate
|
|
crack = Biquad("highpass").process(rng.standard_normal(length), 1500.0) * np.exp(-times / 0.006)
|
|
thudPart = np.sin(2.0 * math.pi * np.cumsum(85.0 * (1.0 + 0.8 * np.exp(-times / 0.015))) / sampleRate)
|
|
thudPart *= np.exp(-times / 0.06)
|
|
clatter = np.zeros(length)
|
|
for _ in range(6):
|
|
start = int(sampleRate * rng.uniform(0.04, 0.4))
|
|
pieceLength = int(sampleRate * 0.05)
|
|
pieceTimes = np.arange(pieceLength) / sampleRate
|
|
piece = np.sin(2.0 * math.pi * rng.uniform(700.0, 1600.0) * pieceTimes) * np.exp(-pieceTimes / 0.008)
|
|
end = min(length, start + pieceLength)
|
|
clatter[start:end] += rng.uniform(0.2, 0.5) * piece[:end - start]
|
|
return 0.35 * crack + 0.4 * thudPart + 0.3 * clatter
|