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>
127 lines
6.4 KiB
Python
127 lines
6.4 KiB
Python
"""Materials: snaps, clanks, rattles, knocks, wet squelches and splashes, and a wooden launch."""
|
|
|
|
import math
|
|
|
|
import numpy as np
|
|
|
|
from .synth import Biquad, blockSize, rng_from, sampleRate, sweep_filtered
|
|
|
|
|
|
def finger_snap(pitch=1.0, seed=0):
|
|
"""A finger snap: a sharp bright crack with a short hollow knock under it."""
|
|
rng = rng_from(seed)
|
|
times = np.arange(int(sampleRate * 0.09)) / sampleRate
|
|
crack = Biquad("bandpass").process(rng.standard_normal(len(times)), 2600.0 * pitch, 1.4) * np.exp(-times / 0.012)
|
|
knock = np.sin(2.0 * math.pi * 900.0 * pitch * times) * np.exp(-times / 0.008)
|
|
return 0.5 * (crack + 0.4 * knock) * np.minimum(1.0, times / 0.0005)
|
|
|
|
|
|
def metal_clank(pitch=820.0, seed=0):
|
|
"""A metal tool dropped in a metal box: a hard clank with a short, clangy ring."""
|
|
rng = rng_from(seed)
|
|
times = np.arange(int(sampleRate * 0.16)) / sampleRate
|
|
ring = np.zeros_like(times)
|
|
for partial, level, decay in ((1.0, 1.0, 0.05), (1.58, 0.7, 0.04), (2.31, 0.5, 0.03), (3.93, 0.3, 0.02)):
|
|
ring += level * np.sin(2.0 * math.pi * pitch * partial * times) * np.exp(-times / decay)
|
|
hit = Biquad("bandpass").process(rng.standard_normal(len(times)), 3000.0, 1.0) * np.exp(-times / 0.004)
|
|
return 0.3 * (ring + 0.8 * hit) * np.minimum(1.0, times / 0.0005)
|
|
|
|
|
|
def rattle(seconds=0.07, seed=0):
|
|
"""One shake of seeds in a gourd: a quick dry hiss of tiny clicks."""
|
|
rng = rng_from(seed)
|
|
length = int(sampleRate * seconds)
|
|
times = np.arange(length) / sampleRate
|
|
grains = rng.standard_normal(length) * (rng.random(length) < 0.25)
|
|
hiss = Biquad("highpass").process(grains, 3500.0, 0.8)
|
|
body = Biquad("bandpass").process(rng.standard_normal(length), 700.0, 2.0)
|
|
envelope = np.sin(math.pi * times / times[-1]) ** 0.7
|
|
return 0.19 * (hiss + 0.25 * body) * envelope
|
|
|
|
|
|
def hollow_knock(frequency):
|
|
"""A hollow bone or wooden bar struck once, like one note of a xylophone."""
|
|
length = int(sampleRate * 0.18)
|
|
times = np.arange(length) / sampleRate
|
|
knock = (np.sin(2.0 * math.pi * frequency * times)
|
|
+ 0.4 * np.sin(2.0 * math.pi * frequency * 3.9 * times) * np.exp(-times / 0.01))
|
|
return 0.5 * knock * np.exp(-times / 0.04)
|
|
|
|
|
|
def squelch(seconds, low, high, quality=4.0, seed=0):
|
|
"""A wet squelch: noise through a narrow filter that swoops up from low to high and back down."""
|
|
rng = rng_from(seed)
|
|
length = int(sampleRate * seconds)
|
|
times = np.arange(length) / sampleRate
|
|
blockCount = (length + blockSize - 1) // blockSize
|
|
shape = np.sin(math.pi * np.linspace(0.0, 1.0, blockCount)) ** 0.7
|
|
squelchSound = sweep_filtered(rng.standard_normal(length), "bandpass", low + (high - low) * shape, quality)
|
|
envelope = np.minimum(1.0, times / 0.012) * (1.0 - times / times[-1]) ** 1.3
|
|
return squelchSound * envelope
|
|
|
|
|
|
def wet_splat(seed=0):
|
|
"""Landing in something soft and wet: a big squelch with a slap, over a low thump."""
|
|
rng = rng_from(seed)
|
|
squelchSound = 1.8 * squelch(0.45, 200.0, 1100.0, quality=3.0, seed=rng)
|
|
length = len(squelchSound)
|
|
times = np.arange(length) / sampleRate
|
|
slap = Biquad("lowpass").process(rng.standard_normal(length), 2500.0) * np.exp(-times / 0.02)
|
|
thump = np.sin(2.0 * math.pi * np.cumsum(70.0 * (1.0 + np.exp(-times / 0.02))) / sampleRate) * np.exp(-times / 0.08)
|
|
return 0.6 * squelchSound + 0.4 * slap + 0.5 * thump
|
|
|
|
|
|
def thick_splash(seed=0):
|
|
"""Driving or stepping through a thick liquid: a heavy wet smack, a short sheet of spray,
|
|
then slow drops pattering back down."""
|
|
rng = rng_from(seed)
|
|
length = int(sampleRate * 0.65)
|
|
times = np.arange(length) / sampleRate
|
|
# The smack of hitting the puddle: a dull wet body and a low thump.
|
|
body = Biquad("bandpass").process(rng.standard_normal(length), 450.0, 1.2) * np.exp(-times / 0.035)
|
|
thump = np.sin(2.0 * math.pi * np.cumsum(75.0 * (1.0 + np.exp(-times / 0.01))) / sampleRate) * np.exp(-times / 0.05)
|
|
# The sheet thrown up: noise whose band sweeps up and back down over a fifth of a second.
|
|
sheetLength = int(sampleRate * 0.2)
|
|
blockCount = (sheetLength + blockSize - 1) // blockSize
|
|
shape = np.sin(math.pi * np.linspace(0.0, 1.0, blockCount))
|
|
sheet = sweep_filtered(rng.standard_normal(sheetLength), "bandpass", 500.0 + 1700.0 * shape, 1.8)
|
|
sheetTimes = np.arange(sheetLength) / sampleRate
|
|
sheet *= np.minimum(1.0, sheetTimes / 0.01) * (1.0 - sheetTimes / sheetTimes[-1]) ** 1.5
|
|
# Drops falling back: each a short plop that rises in pitch, thinning out as the splash settles.
|
|
drops = np.zeros(length)
|
|
for _ in range(22):
|
|
start = int(sampleRate * (0.05 + 0.5 * rng.random() ** 1.8))
|
|
dropLength = int(sampleRate * rng.uniform(0.015, 0.035))
|
|
dropTimes = np.arange(dropLength) / sampleRate
|
|
pitch = rng.uniform(450.0, 1300.0) * (1.0 + 1.2 * dropTimes / dropTimes[-1])
|
|
drop = np.sin(2.0 * math.pi * np.cumsum(pitch) / sampleRate) * np.sin(math.pi * dropTimes / dropTimes[-1])
|
|
end = min(length, start + dropLength)
|
|
# Later drops are smaller.
|
|
drops[start:end] += rng.uniform(0.3, 1.0) * (1.0 - start / length) * drop[:end - start]
|
|
output = 0.55 * body + 0.45 * thump + 0.25 * drops
|
|
output[:sheetLength] += 0.5 * sheet
|
|
return 1.1 * output
|
|
|
|
|
|
def wooden_launch(seed=0):
|
|
"""A wooden throwing arm flinging something: a heavy thunk, a creak, and a rush of air rising away."""
|
|
rng = rng_from(seed)
|
|
length = int(sampleRate * 1.3)
|
|
times = np.arange(length) / sampleRate
|
|
thunkPitch = 70.0 + 60.0 * np.exp(-times / 0.05)
|
|
thunk = np.sin(2.0 * math.pi * np.cumsum(thunkPitch) / sampleRate) * np.exp(-times / 0.15)
|
|
crack = rng.standard_normal(length) * np.exp(-times / 0.01)
|
|
# The arm creaks as it swings: a rough low buzz that wavers.
|
|
creakPitch = 140.0 + 25.0 * np.sin(2.0 * math.pi * 7.0 * times)
|
|
creakPhases = (np.cumsum(creakPitch) / sampleRate) % 1.0
|
|
creak = (2.0 * creakPhases - 1.0) * np.exp(-((times - 0.12) / 0.08) ** 2)
|
|
rushFilter = Biquad("bandpass")
|
|
noise = rng.standard_normal(length)
|
|
rush = np.zeros(length)
|
|
for blockStart in range(0, length, blockSize):
|
|
blockEnd = min(blockStart + blockSize, length)
|
|
progress = blockStart / length
|
|
rush[blockStart:blockEnd] = rushFilter.process(noise[blockStart:blockEnd], 400.0 * 8.0 ** progress, 1.0)
|
|
rush *= np.sin(math.pi * np.minimum(times / 1.3, 1.0)) ** 2
|
|
return np.tanh(1.2 * (0.9 * thunk + 0.25 * crack + 0.3 * creak + 0.7 * rush)) * 0.8
|