Files
Storm DragonandClaude Opus 5.5 962f695226 Generated footsteps on grass, leaves, gravel, snow, wood and concrete
movement.footstep(surface, seed) makes one footstep, and each seed gives a
slightly different step, so a game can register several and play them at
random. Every surface except grass is the heel landing and then the toe:

- leaves: bright crisp crackles that spread out, over a soft rustle.
- gravel: dozens of small stones knocking under the heel and toe, over a
  low grind.
- snow: a muffled crunch that gets denser as the foot packs it down.
- wood: a hollow knock with the boards ringing, and on some steps a creak.
- concrete: a hard heel and a scuff of grit on the toe.

Grass is one swish instead, with blades flicking off the shoe, as a foot
swings through. The loose surfaces are built from many tiny filtered
bursts of noise, and how many there are, how bright, and how they spread
over the step is what tells stones from snow or leaves. Each surface was
tuned by ear against how real footsteps sound.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 22:00:22 -05:00

218 lines
10 KiB
Python

"""Movement sounds: jumping, and footsteps on different ground."""
import math
import numpy as np
from .synth import Biquad, normalize, rng_from, sampleRate, sweep_filtered
def jump(seconds=0.24, low=300.0, high=1000.0):
"""A triangle tone springing straight up in pitch from low to high as it fades out.
The tone holds full volume for its first 0.04 seconds, then fades out over
the rest along a half sine.
"""
times = np.arange(int(sampleRate * seconds)) / sampleRate
frequency = low + (high - low) * times / seconds
# Start halfway through the cycle, at the triangle's lowest point.
phases = np.cumsum(frequency) / sampleRate + 0.5
triangle = 2.0 * np.abs(2.0 * (phases % 1.0) - 1.0) - 1.0
fadeSeconds = min(0.2, seconds)
fade = np.clip((times - (seconds - fadeSeconds)) / fadeSeconds, 0.0, 1.0)
envelope = 0.5 * (1.0 + np.cos(np.pi * fade))
return 0.3 * triangle * envelope
footstepSurfaces = ("grass", "leaves", "gravel", "snow", "wood", "concrete")
def footstep(surface, seed=0):
"""One footstep on surface: "grass", "leaves", "gravel", "snow", "wood" or "concrete".
A step is the heel landing, then the toe a moment later, each with the
ground's own sound. Each seed gives a slightly different step, so a game
can make several and play them at random:
sfx.add_sounds(sounds, {f"step_gravel{number}": lambda number=number: movement.footstep("gravel", number)
for number in range(1, 7)})
sg.play_random(sounds, "step_gravel")
"""
makers = {"grass": _grass_step, "leaves": _leaves_step, "gravel": _gravel_step, "snow": _snow_step,
"wood": _wood_step, "concrete": _concrete_step}
if surface not in makers:
raise ValueError(f"No footstep for {surface!r}; the surfaces are {', '.join(footstepSurfaces)}.")
rng = rng_from(seed)
sound = makers[surface](rng)
fadeLength = int(sampleRate * 0.01)
sound[-fadeLength:] *= np.linspace(1.0, 0.0, fadeLength)
return normalize(sound, 0.45 * rng.uniform(0.82, 1.0))
def _times(seconds):
return np.arange(int(sampleRate * seconds)) / sampleRate
def _hit(times, start, attackSeconds, decaySeconds):
"""An envelope that starts at start, rises over attackSeconds and dies away over decaySeconds."""
since = times - start
after = np.maximum(since, 0.0)
return np.where(since >= 0.0, np.minimum(1.0, after / attackSeconds) * np.exp(-after / decaySeconds), 0.0)
def _swell(times, start, riseSeconds, fallSeconds):
"""An envelope that swells up from start over riseSeconds, then fades over fallSeconds."""
since = times - start
rising = np.clip(since / riseSeconds, 0.0, 1.0) ** 2
falling = np.exp(-np.maximum(since - riseSeconds, 0.0) / fallSeconds)
return np.where(since >= 0.0, rising * falling, 0.0)
def _noise(rng, times, kind, cutoff, quality=0.707):
return Biquad(kind).process(rng.standard_normal(len(times)), cutoff, quality)
def _thump(times, start, pitch, decaySeconds):
"""The dull low thud of a foot's weight, dropping a little in pitch."""
since = np.maximum(times - start, 0.0)
frequency = pitch * (1.0 + 0.6 * np.exp(-since / 0.01))
phase = np.cumsum(frequency) / sampleRate
return np.sin(2.0 * math.pi * phase) * _hit(times, start, 0.002, decaySeconds)
def _grains(rng, length, startTimes, low, high, quality, grainSeconds, sharpness=1.0):
"""Tiny filtered bursts of noise: stones knocking, snow crystals breaking, leaves cracking.
Each grain starts at one of startTimes, rings at its own pitch between low
and high, and lasts about grainSeconds. Higher sharpness makes a few grains
much louder than the rest, as in crisp, crackly things.
"""
output = np.zeros(length)
for start in startTimes:
offset = int(start * sampleRate)
if offset >= length:
continue
grainLength = max(8, int(sampleRate * grainSeconds * rng.uniform(0.5, 1.6)))
grainLength = min(grainLength, length - offset)
grainTimes = np.arange(grainLength) / sampleRate
burst = rng.standard_normal(grainLength) * np.exp(-grainTimes / (grainSeconds * 0.35))
centre = math.exp(rng.uniform(math.log(low), math.log(high)))
grain = Biquad("bandpass").process(burst, centre, quality * rng.uniform(0.7, 1.4))
level = rng.lognormal(0.0, 0.5 * sharpness)
output[offset:offset + grainLength] += level * grain
return output
def _cluster(rng, count, start, spreadSeconds):
"""Grain start times bunched just after start, thinning out over about spreadSeconds."""
return start + rng.exponential(spreadSeconds, count)
def _concrete_step(rng):
"""A hard shoe on concrete: a dry heel click, a scuff of grit, and the toe."""
times = _times(0.32)
toe = rng.uniform(0.075, 0.1)
sound = np.zeros(len(times))
for start, weight in ((0.0, 1.0), (toe, 0.55)):
click = _noise(rng, times, "bandpass", 3500.0, 0.8) * _hit(times, start, 0.0004, 0.004)
body = _noise(rng, times, "bandpass", rng.uniform(260.0, 340.0), 1.2) * _hit(times, start, 0.001, 0.016)
sound += weight * (0.35 * click + 1.0 * body + 0.4 * _thump(times, start, 95.0, 0.018))
scuff = _noise(rng, times, "bandpass", rng.uniform(2200.0, 3200.0), 0.8) * _swell(times, toe - 0.02, 0.02, 0.025)
grit = _grains(rng, len(times), _cluster(rng, 10, toe, 0.02), 3000.0, 8000.0, 2.0, 0.002)
return sound + 0.12 * scuff + 0.05 * grit
def _wood_step(rng):
"""A shoe on wooden boards: a hollow knock that rings in the boards, and now and then a creak."""
times = _times(0.42)
toe = rng.uniform(0.08, 0.11)
sound = np.zeros(len(times))
modes = [(rng.uniform(100.0, 125.0), 1.0, 0.07), (rng.uniform(180.0, 210.0), 0.7, 0.05),
(rng.uniform(300.0, 340.0), 0.5, 0.035), (rng.uniform(500.0, 560.0), 0.3, 0.02)]
for start, weight in ((0.0, 1.0), (toe, 0.5)):
ring = sum(level * np.sin(2.0 * math.pi * frequency * times + rng.uniform(0.0, 2.0 * math.pi))
* _hit(times, start, 0.001, decay) for frequency, level, decay in modes)
knock = _noise(rng, times, "bandpass", rng.uniform(1000.0, 1400.0), 1.5) * _hit(times, start, 0.0005, 0.007)
click = _noise(rng, times, "highpass", 2500.0) * _hit(times, start, 0.0003, 0.002)
sound += weight * (0.25 * ring + 1.0 * knock + 0.3 * click)
if rng.random() < 0.3:
sound += 0.25 * _creak(rng, times, toe + 0.02)
return sound
def _creak(rng, times, start):
"""A board creaking under weight: rough friction ticks, too fast to hear apart, through a narrow filter."""
seconds = rng.uniform(0.12, 0.2)
ticks = np.zeros(len(times))
tickTime = start
rate = rng.uniform(50.0, 90.0)
while tickTime < start + seconds:
index = int(tickTime * sampleRate)
if index >= len(ticks):
break
ticks[index] = rng.uniform(0.5, 1.0)
tickTime += rng.uniform(0.85, 1.15) / rate
shape = _swell(times, start, seconds * 0.4, seconds * 0.35)
tone = Biquad("bandpass").process(ticks, rng.uniform(700.0, 1100.0), 6.0)
return 8.0 * tone * shape
def _gravel_step(rng):
"""Stepping on gravel: dozens of small stones knocking and grinding under the heel, then the toe."""
times = _times(0.42)
toe = rng.uniform(0.09, 0.12)
length = len(times)
starts = np.concatenate((_cluster(rng, 70, 0.0, 0.035), _cluster(rng, 50, toe, 0.04)))
stones = _grains(rng, length, starts, 1200.0, 6000.0, 3.0, 0.006)
grindShape = _hit(times, 0.0, 0.004, 0.05) + 0.6 * _hit(times, toe, 0.004, 0.05)
grind = _noise(rng, times, "bandpass", 1800.0, 0.7) * grindShape
thud = _noise(rng, times, "lowpass", 350.0) * _hit(times, 0.0, 0.003, 0.03)
return 0.6 * stones + 0.25 * grind + 0.5 * thud
def _snow_step(rng):
"""Stepping in snow: a muffled crunch that packs down under the heel, and a smaller one under the toe."""
times = _times(0.48)
toe = rng.uniform(0.12, 0.16)
length = len(times)
# Snow crystals break faster and faster as the foot packs them down, then stop.
heel = 0.13 * rng.beta(2.0, 1.6, 260)
starts = np.concatenate((heel, toe + 0.09 * rng.beta(2.0, 1.6, 140)))
crunch = _grains(rng, length, starts, 700.0, 3500.0, 1.4, 0.004)
crunch = Biquad("lowpass").process(crunch, 4500.0)
packingShape = _swell(times, 0.0, 0.08, 0.06) + 0.5 * _swell(times, toe, 0.06, 0.05)
packing = _noise(rng, times, "lowpass", 1400.0) * packingShape
return 0.55 * crunch + 0.3 * packing + 0.3 * _thump(times, 0.0, 70.0, 0.04)
def _leaves_step(rng):
"""Stepping on dry leaves: bright, crisp crackles that spread out longer, over a soft rustle."""
times = _times(0.5)
toe = rng.uniform(0.1, 0.14)
length = len(times)
starts = np.concatenate((_cluster(rng, 90, 0.0, 0.07), _cluster(rng, 60, toe, 0.07)))
crackles = _grains(rng, length, starts, 2200.0, 9000.0, 1.0, 0.003, sharpness=2.2)
rustleShape = _swell(times, 0.0, 0.02, 0.09) + 0.6 * _swell(times, toe, 0.02, 0.08)
rustle = _noise(rng, times, "highpass", 3000.0) * rustleShape
thud = _noise(rng, times, "lowpass", 300.0) * _hit(times, 0.0, 0.004, 0.035)
return 0.5 * crackles + 0.12 * rustle + 0.4 * thud
def _grass_step(rng):
"""A foot swinging through grass: one soft swish swelling in and out, with blades flicking off the shoe."""
seconds = rng.uniform(0.22, 0.28)
times = _times(seconds)
length = len(times)
# One smooth swell, quicker in than out, as the foot sweeps through and the grass springs back.
peak = rng.uniform(0.35, 0.45)
position = times / seconds
shape = np.where(position < peak, np.sin(0.5 * math.pi * position / peak) ** 2,
np.cos(0.5 * math.pi * (position - peak) / (1.0 - peak)) ** 2)
# The swish falls a little in pitch as the foot passes.
blockCount = (length + 511) // 512
centres = np.linspace(rng.uniform(3100.0, 3800.0), rng.uniform(2000.0, 2500.0), blockCount)
swish = sweep_filtered(rng.standard_normal(length), "bandpass", centres, 0.7)
swish = Biquad("lowpass").process(swish, 7000.0) * shape
blades = _grains(rng, length, rng.uniform(0.15, 0.85, 14) * seconds, 2500.0, 7000.0, 2.0, 0.002) * shape
return swish + 0.2 * blades