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
+142
@@ -0,0 +1,142 @@
|
||||
"""Live alert voices that follow the game: a proximity warning, a falling scream, a falling whistle and chattering.
|
||||
|
||||
Each makes sound a block at a time through render(frameCount); set its
|
||||
attributes between blocks. Without a live mixer, record one with
|
||||
synth.render_voice and a change function that moves its attributes.
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
from scipy.signal import lfilter
|
||||
|
||||
from .synth import Biquad, Oscillator, Ramp, rng_from, sampleRate, saw_wave
|
||||
|
||||
|
||||
class ProximityWarningVoice:
|
||||
"""Beeps for something closing in: faster and higher as it nears, then one solid tone once it locks on.
|
||||
|
||||
Set active to sound it, urgency from 0 (just noticed) to 1 (about to
|
||||
arrive), and locked for the solid tone. kind is "soft" for a round tone or
|
||||
"buzzy" for a harsher one, so two kinds of threat can be told apart.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.active = False
|
||||
self.urgency = 0.0
|
||||
self.locked = False
|
||||
self.kind = "soft"
|
||||
self.tone = Oscillator()
|
||||
self.beepPhase = 0.0
|
||||
self.levelRamp = Ramp(0.0)
|
||||
self.gateState = np.zeros(1)
|
||||
|
||||
def render(self, frameCount):
|
||||
urgency = min(max(self.urgency, 0.0), 1.0)
|
||||
if self.locked:
|
||||
frequency = 1500.0
|
||||
gate = np.ones(frameCount)
|
||||
else:
|
||||
frequency = 600.0 + 700.0 * urgency
|
||||
# From about 2.5 beeps a second to 12, each lasting 40 percent of its cycle.
|
||||
beepRate = 2.5 + 9.5 * urgency ** 1.5
|
||||
steps = self.beepPhase + np.cumsum(np.full(frameCount, beepRate / sampleRate))
|
||||
self.beepPhase = steps[-1] % 1.0
|
||||
gate = (steps % 1.0 < 0.4).astype(float)
|
||||
phases, _ = self.tone.advance(np.full(frameCount, frequency))
|
||||
wave = np.sin(2.0 * math.pi * phases)
|
||||
if self.kind == "buzzy":
|
||||
wave = np.tanh(4.0 * wave) * 0.7
|
||||
# A short ramp on the gate keeps each beep from clicking.
|
||||
level = self.levelRamp.to(1.0 if self.active else 0.0, frameCount)
|
||||
gate, self.gateState = lfilter([0.02], [1.0, -0.98], gate, zi=self.gateState)
|
||||
return 0.5 * level * wave * gate
|
||||
|
||||
|
||||
class FallingScreamVoice:
|
||||
"""Something falling from the sky, screaming.
|
||||
|
||||
The scream is a buzzing voice shaped by three formant filters into an
|
||||
"aah", with a ragged, wavering pitch. Set progress from 0 to 1 as it
|
||||
falls: the scream slides down and grows louder.
|
||||
"""
|
||||
|
||||
# Formants of a screamed "aah": centre, sharpness and level.
|
||||
formants = [(850.0, 6.0, 1.0), (1250.0, 7.0, 0.7), (2900.0, 8.0, 0.35)]
|
||||
|
||||
def __init__(self, seed=0):
|
||||
self.rng = rng_from(seed)
|
||||
self.voice = Oscillator()
|
||||
self.filters = [Biquad("bandpass") for _ in self.formants]
|
||||
self.breathFilter = Biquad("bandpass")
|
||||
self.startPitch = self.rng.uniform(820.0, 1050.0)
|
||||
self.vibratoRate = self.rng.uniform(5.0, 7.0)
|
||||
self.time = 0.0
|
||||
self.progress = 0.0
|
||||
|
||||
def render(self, frameCount):
|
||||
progress = min(max(self.progress, 0.0), 1.0)
|
||||
times = self.time + np.arange(frameCount) / sampleRate
|
||||
self.time += frameCount / sampleRate
|
||||
# The scream falls by about a fifth as it comes down, with a wild vibrato.
|
||||
vibrato = 1.0 + 0.04 * np.sin(2.0 * math.pi * self.vibratoRate * times) + 0.015 * self.rng.standard_normal()
|
||||
pitch = self.startPitch * (1.0 - 0.3 * progress) * vibrato
|
||||
phases, phaseStep = self.voice.advance(pitch)
|
||||
buzz = saw_wave(phases, phaseStep)
|
||||
# A rasp in the throat: the buzz is roughened, more so as it screams louder.
|
||||
buzz *= 1.0 + 0.3 * self.rng.standard_normal(frameCount) * (0.4 + 0.6 * progress)
|
||||
scream = np.zeros(frameCount)
|
||||
for (centre, quality, level), formantFilter in zip(self.formants, self.filters):
|
||||
# The mouth opens wider as it screams, lifting the first formants.
|
||||
scream += level * formantFilter.process(buzz, centre * (1.0 + 0.12 * progress), quality)
|
||||
breath = self.breathFilter.process(self.rng.standard_normal(frameCount), 1800.0, 1.0)
|
||||
swell = 0.35 + 0.65 * progress
|
||||
# A short fade in, so the scream does not start with a click.
|
||||
fadeIn = min(1.0, self.time / 0.08)
|
||||
return 0.75 * fadeIn * swell * (scream + 0.08 * breath)
|
||||
|
||||
|
||||
class WhistlingFallVoice:
|
||||
"""Something thrown or dropped, in flight: a falling whistle over rushing air.
|
||||
|
||||
pitchFactor bends the pitch, such as for Doppler: above 1 while it closes
|
||||
on you, below 1 as it moves away.
|
||||
"""
|
||||
|
||||
def __init__(self, seed=0):
|
||||
self.rng = rng_from(seed)
|
||||
self.pitchFactor = 1.0
|
||||
self.pitchRamp = Ramp(1.0)
|
||||
self.whistle = Oscillator()
|
||||
self.air = Biquad("bandpass")
|
||||
self.time = 0.0
|
||||
|
||||
def render(self, frameCount):
|
||||
times = self.time + np.arange(frameCount) / sampleRate
|
||||
self.time += frameCount / sampleRate
|
||||
pitch = self.pitchRamp.to(self.pitchFactor, frameCount)
|
||||
frequency = (1400.0 - 250.0 * np.minimum(times, 4.0)) * pitch
|
||||
phases, _ = self.whistle.advance(frequency)
|
||||
rush = self.air.process(self.rng.standard_normal(frameCount), 900.0, 0.8)
|
||||
return 0.25 * np.sin(2.0 * math.pi * phases) + 0.5 * rush
|
||||
|
||||
|
||||
class ChatteringVoice:
|
||||
"""Teeth chattering about twelve times a second, such as a skull flying at you.
|
||||
|
||||
pitchFactor (0.5 to 2) speeds and raises the chatter, such as for Doppler.
|
||||
"""
|
||||
|
||||
def __init__(self, seed=0):
|
||||
self.rng = rng_from(seed)
|
||||
self.pitchFactor = 1.0
|
||||
self.chatter = Oscillator()
|
||||
self.clickFilter = Biquad("bandpass")
|
||||
|
||||
def render(self, frameCount):
|
||||
pitch = min(max(self.pitchFactor, 0.5), 2.0)
|
||||
phases, _ = self.chatter.advance(np.full(frameCount, 12.0 * pitch))
|
||||
# Longer clicks, driven into soft clipping, carry much further than bare ticks without harsh peaks.
|
||||
envelope = np.exp(-phases * 12.0)
|
||||
clicks = self.clickFilter.process(self.rng.standard_normal(frameCount), 2200.0 * pitch, 3.0)
|
||||
return 0.9 * np.tanh(6.6 * clicks * envelope)
|
||||
Reference in new issue
Block a user