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:
1 parent
31e7472fd1
commit
f164697cf6
19 files changed
+2084
No files matched your search
@@ -0,0 +1,107 @@
|
||||
"""Creature voices: a horse's whinny and a witch's cackle.
|
||||
|
||||
Each call with the same seed gives the same call. echo adds a short, ghostly
|
||||
double echo after it, for a skeleton or a haunted place.
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
|
||||
from .synth import Biquad, breath_shape, buzz_wave, formant_filter, glide, rng_from, sampleRate
|
||||
|
||||
# A whinny lasts this long, before its echo, which repeats it this many seconds later, twice.
|
||||
whinnySeconds = 1.7
|
||||
whinnyEcho = 0.16
|
||||
|
||||
# A cackle is runs of "heh" syllables, each run starting high again; the last, full run falls all the way.
|
||||
cackleRuns = (3, 3, 11)
|
||||
cackleRate = 7.5
|
||||
cackleHighPitch = 620.0
|
||||
cackleLowPitch = 430.0
|
||||
# A short breath before each run starts over.
|
||||
cacklePause = 0.03
|
||||
cackleEcho = 0.14
|
||||
|
||||
|
||||
def _finish(sound, echoSeconds, echoLevels):
|
||||
"""Normalise to full scale, after adding the echoes if there are any."""
|
||||
if echoLevels:
|
||||
delay = int(echoSeconds * sampleRate)
|
||||
echoed = np.concatenate((sound, np.zeros(len(echoLevels) * delay)))
|
||||
for index, level in enumerate(echoLevels, start=1):
|
||||
echoed[index * delay:index * delay + len(sound)] += level * sound
|
||||
sound = echoed
|
||||
return sound / np.max(np.abs(sound))
|
||||
|
||||
|
||||
def horse_whinny(seed=0, echo=False):
|
||||
"""A horse's whinny: a squeal that falls away in a shaking trill, then a fluttering snort.
|
||||
|
||||
Horses sound two pitches at once, so a low voice sits under the squeal.
|
||||
"""
|
||||
rng = rng_from(seed)
|
||||
times = np.arange(int(whinnySeconds * sampleRate)) / sampleRate
|
||||
lift = rng.uniform(0.93, 1.07)
|
||||
pitch = glide(times, [(0.0, 700), (0.07, 1150), (0.25, 1050), (0.75, 720), (1.05, 480), (1.25, 330)]) * lift
|
||||
trillRate = glide(times, [(0.0, 17), (0.6, 15), (1.2, 9)]) * rng.uniform(0.92, 1.08)
|
||||
trill = np.sin(2.0 * math.pi * np.cumsum(trillRate / sampleRate))
|
||||
shake = 1.0 - 0.55 * (0.5 + 0.5 * trill) * glide(times, [(0.0, 0.2), (0.3, 1.0), (1.25, 1.0)])
|
||||
neighLevel = glide(times, [(0.0, 0.0), (0.03, 1.0), (0.9, 0.85), (1.2, 0.5), (1.3, 0.0)])
|
||||
high = buzz_wave(pitch * (1.0 + 0.07 * trill))
|
||||
low = buzz_wave(glide(times, [(0.0, 300), (1.25, 262)]) * (1.0 + 0.04 * trill))
|
||||
source = (high + 0.5 * low) * shake * neighLevel
|
||||
nasal = (Biquad("bandpass").process(source, 950.0, 2.5)
|
||||
+ 0.8 * Biquad("bandpass").process(source, 1800.0, 3.5)
|
||||
+ 0.4 * Biquad("bandpass").process(source, 3000.0, 4.0))
|
||||
breath = Biquad("highpass").process(rng.standard_normal(len(times)), 2500.0, 0.7) * 0.06 * neighLevel
|
||||
snortLevel = glide(times, [(0.0, 0.0), (1.28, 0.0), (1.33, 1.0), (1.55, 0.4), (1.7, 0.0)])
|
||||
flutter = 0.5 + 0.5 * np.sin(2.0 * math.pi * 30.0 * times)
|
||||
snort = Biquad("bandpass").process(rng.standard_normal(len(times)), 1200.0, 1.0) * flutter * snortLevel * 0.6
|
||||
sound = nasal + breath + snort
|
||||
return _finish(sound, whinnyEcho, (0.3, 0.12) if echo else ())
|
||||
|
||||
|
||||
def witch_cackle(seed=0, echo=False):
|
||||
"""A witch's cackle: two false starts of "heh heh heh", then a full run falling in pitch.
|
||||
|
||||
Each "heh" starts with a breathy "h", breath through the same mouth shape
|
||||
as the vowel, which keeps it from sounding like "ch". The voice is old and
|
||||
shaky, with a creaky rasp.
|
||||
"""
|
||||
rng = rng_from(seed)
|
||||
syllableGap = 1.0 / cackleRate
|
||||
longestRun = max(cackleRuns)
|
||||
starts = []
|
||||
clock = 0.0
|
||||
for runIndex, count in enumerate(cackleRuns):
|
||||
if runIndex > 0:
|
||||
clock += cacklePause
|
||||
for index in range(count):
|
||||
starts.append((clock, index))
|
||||
clock += syllableGap
|
||||
times = np.arange(int((clock + 0.15) * sampleRate)) / sampleRate
|
||||
pitch = np.full(len(times), cackleLowPitch)
|
||||
voiced = np.zeros(len(times))
|
||||
breathy = np.zeros(len(times))
|
||||
for syllableStart, index in starts:
|
||||
start = syllableStart + rng.uniform(-0.008, 0.008)
|
||||
base = cackleHighPitch + (cackleLowPitch - cackleHighPitch) * index / (longestRun - 1)
|
||||
base *= rng.uniform(0.97, 1.03)
|
||||
length = syllableGap * rng.uniform(0.55, 0.7)
|
||||
inside = (times >= start) & (times < start + length)
|
||||
local = (times[inside] - start) / length
|
||||
# Each "heh" jumps up, then sags.
|
||||
pitch[inside] = base * (1.08 - 0.15 * local)
|
||||
voiced[inside] = np.minimum(1.0, local / 0.15) * np.clip((1.0 - local) / 0.3, 0.0, 1.0)
|
||||
onset = (times >= start - 0.05) & (times < start + 0.03)
|
||||
breathy[onset] = np.maximum(breathy[onset], breath_shape(onset.sum()))
|
||||
jitter = 1.0 + 0.012 * Biquad("lowpass").process(rng.standard_normal(len(times)), 60.0, 0.7)
|
||||
tremble = 1.0 + 0.03 * np.sin(2.0 * math.pi * 11.0 * times)
|
||||
source = buzz_wave(pitch * jitter * tremble)
|
||||
# The rasp: every other pulse of the voice weaker.
|
||||
halfPhase = np.cumsum(pitch / sampleRate / 2.0) % 1.0
|
||||
source *= (1.0 - 0.35 * (halfPhase < 0.5)) * voiced
|
||||
noise = Biquad("lowpass").process(rng.standard_normal(len(times)), 3500.0, 0.7)
|
||||
sound = formant_filter(source + noise * (0.35 * breathy + 0.04 * voiced), "pinched_eh")
|
||||
return _finish(sound, cackleEcho, (0.25, 0.1) if echo else ())
|
||||
Reference in new issue
Block a user