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
+158
@@ -0,0 +1,158 @@
|
||||
"""Vehicle sounds: skids and tyre scrubs, and live voices for road surfaces and wind.
|
||||
|
||||
The live voices make sound a block at a time through render(frameCount), for a
|
||||
game's own live mixer; set their attributes, such as speed, between blocks.
|
||||
Without a live mixer, record one with synth.render_voice, and make it loop
|
||||
with synth.make_loop.
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
from scipy.signal import lfilter
|
||||
|
||||
from .synth import Biquad, Oscillator, Ramp, blockSize, rng_from, sampleRate, sweep_filtered
|
||||
|
||||
|
||||
def skid_spin(seed=0):
|
||||
"""Stereo spin-out: a swirling skid that circles your head and slows down."""
|
||||
rng = rng_from(seed)
|
||||
length = int(sampleRate * 1.8)
|
||||
times = np.arange(length) / sampleRate
|
||||
noise = rng.standard_normal(length)
|
||||
sweepFilter = Biquad("bandpass")
|
||||
skid = np.zeros(length)
|
||||
for blockStart in range(0, length, blockSize):
|
||||
blockEnd = min(blockStart + blockSize, length)
|
||||
progress = blockStart / length
|
||||
centre = 2600.0 * (1.0 - progress) + 400.0 * progress
|
||||
skid[blockStart:blockEnd] = sweepFilter.process(noise[blockStart:blockEnd], centre, 1.5)
|
||||
skid *= np.minimum(1.0, times / 0.03) * np.exp(-times / 0.8)
|
||||
# Turns slow from three a second to one as the spin dies away.
|
||||
turnPhase = 2.0 * math.pi * (3.0 * times - 0.55 * times ** 2)
|
||||
angles = (np.sin(turnPhase) + 1.0) * math.pi / 4.0
|
||||
return 0.9 * np.column_stack((skid * np.cos(angles), skid * np.sin(angles)))
|
||||
|
||||
|
||||
def whoosh(rising, seed=0):
|
||||
"""Air rushing past, as something passes you.
|
||||
|
||||
Falling, for passing something that drops behind you, or rising, for
|
||||
something overtaking you and pulling away ahead.
|
||||
"""
|
||||
rng = rng_from(seed)
|
||||
length = int(sampleRate * 0.45)
|
||||
times = np.arange(length) / sampleRate
|
||||
noise = rng.standard_normal(length)
|
||||
sweepFilter = Biquad("bandpass")
|
||||
output = np.zeros(length)
|
||||
for blockStart in range(0, length, blockSize):
|
||||
blockEnd = min(blockStart + blockSize, length)
|
||||
progress = blockStart / length
|
||||
if not rising:
|
||||
progress = 1.0 - progress
|
||||
centre = 500.0 * (6.0 ** progress)
|
||||
output[blockStart:blockEnd] = sweepFilter.process(noise[blockStart:blockEnd], centre, 1.2)
|
||||
envelope = np.sin(math.pi * times / times[-1]) ** 2
|
||||
return 0.8 * output * envelope
|
||||
|
||||
|
||||
def tyre_scrub_turn(seed=0):
|
||||
"""Turning round on the spot: tyres scrubbing as the sound swings from your left, past the front, to your right."""
|
||||
rng = rng_from(seed)
|
||||
length = int(sampleRate * 1.0)
|
||||
times = np.arange(length) / sampleRate
|
||||
blockCount = (length + blockSize - 1) // blockSize
|
||||
# The scrub judders, like a tyre gripping and letting go as it swings round.
|
||||
scrub = sweep_filtered(rng.standard_normal(length), "bandpass",
|
||||
700.0 + 300.0 * np.sin(np.linspace(0.0, math.pi, blockCount)), 2.0)
|
||||
judder = 0.6 + 0.4 * np.sin(2.0 * math.pi * 14.0 * times)
|
||||
scrub *= judder * np.minimum(1.0, times / 0.05) * np.minimum(1.0, (times[-1] - times) / 0.15)
|
||||
angles = times / times[-1] * math.pi / 2.0
|
||||
return np.column_stack((scrub * np.cos(angles), scrub * np.sin(angles)))
|
||||
|
||||
|
||||
class TyreSquealVoice:
|
||||
"""A wavering squeal for tyres at the limit of grip."""
|
||||
|
||||
def __init__(self, seed=0):
|
||||
self.rng = rng_from(seed)
|
||||
self.time = 0.0
|
||||
self.tone = Oscillator()
|
||||
self.hissFilter = Biquad("bandpass")
|
||||
|
||||
def render(self, frameCount):
|
||||
times = self.time + np.arange(frameCount) / sampleRate
|
||||
self.time += frameCount / sampleRate
|
||||
wobble = 120.0 * np.sin(2.0 * math.pi * 7.0 * times) + 60.0 * np.sin(2.0 * math.pi * 2.3 * times)
|
||||
phases, _ = self.tone.advance(1300.0 + wobble)
|
||||
hiss = self.hissFilter.process(self.rng.standard_normal(frameCount), 2500.0, 2.0)
|
||||
return 0.3 * np.sin(2.0 * math.pi * phases) + 0.5 * hiss
|
||||
|
||||
|
||||
class GravelVoice:
|
||||
"""Crunching stones under the wheels, busier the faster you go.
|
||||
|
||||
speed runs from 0 (stopped) to 1 (top speed); it is loudest from a quarter speed up.
|
||||
"""
|
||||
|
||||
def __init__(self, seed=0):
|
||||
self.speed = 0.0
|
||||
self.rng = rng_from(seed)
|
||||
self.grainState = np.zeros(1)
|
||||
self.crunchFilter = Biquad("bandpass")
|
||||
self.rumbleFilter = Biquad("lowpass")
|
||||
|
||||
def render(self, frameCount):
|
||||
speedNow = max(self.speed, 0.0)
|
||||
density = 40.0 + 400.0 * speedNow
|
||||
grains = (self.rng.random(frameCount) < density / sampleRate) * self.rng.uniform(0.3, 1.0, frameCount)
|
||||
grainEnvelope, self.grainState = lfilter([1.0], [1.0, -0.995], grains, zi=self.grainState)
|
||||
noise = self.rng.standard_normal(frameCount)
|
||||
crunch = self.crunchFilter.process(noise * grainEnvelope * 0.45, 1800.0, 0.7)
|
||||
rumble = self.rumbleFilter.process(self.rng.standard_normal(frameCount), 400.0, 0.7)
|
||||
return (crunch + 0.3 * rumble) * min(1.0, speedNow * 4.0)
|
||||
|
||||
|
||||
class RumbleStripVoice:
|
||||
"""A thump for every strip passed, which becomes a growl at speed.
|
||||
|
||||
topSpeedMetres is how far the vehicle goes in a second at top speed, and
|
||||
speed runs from 0 (stopped) to 1 (top speed). Strips are stripSpacing metres apart.
|
||||
"""
|
||||
|
||||
stripSpacing = 0.7
|
||||
|
||||
def __init__(self, topSpeedMetres, seed=0):
|
||||
self.topSpeedMetres = topSpeedMetres
|
||||
self.speed = 0.0
|
||||
self.rng = rng_from(seed)
|
||||
self.speedRamp = Ramp(0.0)
|
||||
self.strips = Oscillator()
|
||||
self.body = Oscillator()
|
||||
self.noiseFilter = Biquad("lowpass")
|
||||
|
||||
def render(self, frameCount):
|
||||
speedValues = self.speedRamp.to(max(self.speed, 0.0), frameCount)
|
||||
stripRate = speedValues * self.topSpeedMetres / self.stripSpacing
|
||||
stripPhases, _ = self.strips.advance(stripRate)
|
||||
# A very short attack keeps each thump punchy without a hard click.
|
||||
envelope = np.exp(-stripPhases * 6.0) * np.minimum(1.0, stripPhases * 40.0)
|
||||
bodyPhases, _ = self.body.advance(np.full(frameCount, 75.0))
|
||||
noise = self.noiseFilter.process(self.rng.standard_normal(frameCount), 500.0, 0.8)
|
||||
thump = 0.8 * np.sin(2.0 * math.pi * bodyPhases) + 0.6 * noise
|
||||
return thump * envelope * np.minimum(1.0, speedValues * 5.0)
|
||||
|
||||
|
||||
class WindRushVoice:
|
||||
"""Air rushing past: amount runs from 0 (a low rush) to 1 (a high roar)."""
|
||||
|
||||
def __init__(self, seed=0):
|
||||
self.rng = rng_from(seed)
|
||||
self.filter = Biquad("bandpass")
|
||||
self.amount = 0.0
|
||||
|
||||
def render(self, frameCount):
|
||||
amount = min(max(self.amount, 0.0), 1.0)
|
||||
noise = self.rng.standard_normal(frameCount)
|
||||
return 0.6 * self.filter.process(noise, 700.0 + 1800.0 * amount, 0.8)
|
||||
Reference in new issue
Block a user