- New strata_core/feedback.lua: sound sets (stone, dirt, wood, leaves) via strata.node_sounds; nodes without sounds get a set from their dig group after all mods load. Walls get dirt/wood/stone explicitly. - Server-side break dust textured from the dug node, spawned in front of the plane so every player sees it. - Walls, torches and doors play their place sound from their custom on_place (strata.play_place_sound). - Avatar footsteps on two walk frames; on damage the avatar flashes red (strata.flash) and other players hear player_damage / player_falling_damage at it. The engine plays those for the hurt player. - Slimes flash via damage_texture_modifier and squelch when hit or killed. - Sounds are synthesized by the new tools/gen_sounds.py (CC0, no samples), listed in the new CREDITS.md. The swing animation was already done in #15. - 9 selftest cases in tests/feedback.lua. Closes #16 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
260 lines
7.8 KiB
Python
Executable File
260 lines
7.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Generate Strata's placeholder sound effects.
|
|
|
|
Every sound is synthesized here from noise and simple oscillators, so the
|
|
results are original works, dedicated to the public domain (CC0 1.0) like
|
|
this script's output. See CREDITS.md. Real recorded or composed audio can
|
|
replace them later; keep the file names.
|
|
|
|
Needs only the Python standard library plus `oggenc` (vorbis-tools).
|
|
|
|
Usage: tools/gen_sounds.py
|
|
"""
|
|
|
|
import math
|
|
import os
|
|
import random
|
|
import struct
|
|
import subprocess
|
|
import tempfile
|
|
import wave
|
|
|
|
RATE = 44100
|
|
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
|
|
CORE = os.path.join(ROOT, "mods", "strata_core", "sounds")
|
|
MOBS = os.path.join(ROOT, "mods", "strata_mobs", "sounds")
|
|
OGG_QUALITY = "3"
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Building blocks. A sound is a list of floats in -1..1.
|
|
# --------------------------------------------------------------------------
|
|
|
|
def silence(sec):
|
|
return [0.0] * int(RATE * sec)
|
|
|
|
|
|
def noise(sec, rng):
|
|
return [rng.uniform(-1, 1) for _ in range(int(RATE * sec))]
|
|
|
|
|
|
def tone(sec, f0, f1=None, shape="sine"):
|
|
"""Oscillator sweeping exponentially from f0 to f1 Hz."""
|
|
f1 = f1 or f0
|
|
n = int(RATE * sec)
|
|
out, phase = [], 0.0
|
|
for i in range(n):
|
|
f = f0 * (f1 / f0) ** (i / max(n - 1, 1))
|
|
phase += 2 * math.pi * f / RATE
|
|
s = math.sin(phase)
|
|
if shape == "square":
|
|
s = 1.0 if s >= 0 else -1.0
|
|
elif shape == "tri":
|
|
s = 2 / math.pi * math.asin(s)
|
|
out.append(s)
|
|
return out
|
|
|
|
|
|
def lowpass(x, cutoff):
|
|
a = 1 - math.exp(-2 * math.pi * cutoff / RATE)
|
|
out, y = [], 0.0
|
|
for s in x:
|
|
y += a * (s - y)
|
|
out.append(y)
|
|
return out
|
|
|
|
|
|
def highpass(x, cutoff):
|
|
low = lowpass(x, cutoff)
|
|
return [s - l for s, l in zip(x, low)]
|
|
|
|
|
|
def bandpass(x, lo, hi):
|
|
return lowpass(highpass(x, lo), hi)
|
|
|
|
|
|
def env(x, attack=0.002, decay=0.1, curve=1.0):
|
|
"""Linear attack, then exponential decay with time constant `decay`."""
|
|
out = []
|
|
na = max(int(RATE * attack), 1)
|
|
for i, s in enumerate(x):
|
|
t = i / RATE
|
|
if i < na:
|
|
g = i / na
|
|
else:
|
|
g = math.exp(-(t - attack) / decay) ** curve
|
|
out.append(s * g)
|
|
return out
|
|
|
|
|
|
def mix(*parts):
|
|
"""Mix (offset_seconds, gain, samples) parts."""
|
|
n = max(int(RATE * off) + len(x) for off, _, x in parts)
|
|
out = [0.0] * n
|
|
for off, gain, x in parts:
|
|
o = int(RATE * off)
|
|
for i, s in enumerate(x):
|
|
out[o + i] += s * gain
|
|
return out
|
|
|
|
|
|
def normalize(x, peak=0.85):
|
|
m = max(abs(s) for s in x) or 1.0
|
|
out = [s * peak / m for s in x]
|
|
# 5 ms fade at the end so nothing clicks
|
|
fade = min(int(RATE * 0.005), len(out))
|
|
for i in range(fade):
|
|
out[-1 - i] *= i / fade
|
|
return out
|
|
|
|
|
|
def grains(rng, count, spread, make):
|
|
"""Scatter `count` short sounds from make(rng) over `spread` seconds."""
|
|
parts = []
|
|
for _ in range(count):
|
|
parts.append((rng.uniform(0, spread), rng.uniform(0.5, 1.0), make(rng)))
|
|
return mix(*parts)
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# The sounds
|
|
# --------------------------------------------------------------------------
|
|
|
|
def stone_tick(rng):
|
|
click = env(highpass(noise(0.06, rng), 1500), decay=0.008)
|
|
ping = env(tone(0.06, rng.uniform(1700, 2100)), decay=0.012)
|
|
return mix((0, 1.0, click), (0, 0.5, ping))
|
|
|
|
|
|
def dig_stone(rng):
|
|
return stone_tick(rng)
|
|
|
|
|
|
def dug_stone(rng):
|
|
crunch = grains(rng, 7, 0.09, lambda r: env(bandpass(noise(0.05, r), 600, 4000), decay=0.012))
|
|
thump = env(tone(0.15, 180, 90), decay=0.04)
|
|
return mix((0, 1.0, crunch), (0, 0.7, thump))
|
|
|
|
|
|
def dig_dirt(rng):
|
|
return env(lowpass(noise(0.09, rng), 900), attack=0.004, decay=0.025)
|
|
|
|
|
|
def dug_dirt(rng):
|
|
rumble = env(lowpass(noise(0.22, rng), 500), attack=0.006, decay=0.06)
|
|
bits = grains(rng, 5, 0.12, lambda r: env(lowpass(noise(0.04, r), 1400), decay=0.01))
|
|
return mix((0, 1.0, rumble), (0.01, 0.5, bits))
|
|
|
|
|
|
def wood_knock(rng, f):
|
|
body = env(mix((0, 1.0, tone(0.12, f)), (0, 0.5, tone(0.12, f * 2.3))), decay=0.03)
|
|
click = env(highpass(noise(0.02, rng), 2000), decay=0.004)
|
|
return mix((0, 1.0, body), (0, 0.4, click))
|
|
|
|
|
|
def dig_wood(rng):
|
|
return wood_knock(rng, rng.uniform(260, 320))
|
|
|
|
|
|
def dug_wood(rng):
|
|
crack = env(bandpass(noise(0.12, rng), 800, 5000), decay=0.025)
|
|
return mix((0, 1.0, wood_knock(rng, 220)), (0.03, 0.8, crack), (0.06, 0.6, wood_knock(rng, 170)))
|
|
|
|
|
|
def dig_leaves(rng):
|
|
return env(bandpass(noise(0.12, rng), 2500, 7000), attack=0.02, decay=0.04)
|
|
|
|
|
|
def dug_leaves(rng):
|
|
return grains(rng, 6, 0.18,
|
|
lambda r: env(bandpass(noise(0.08, r), 2000, 7000), attack=0.01, decay=0.03))
|
|
|
|
|
|
def hurt(rng):
|
|
voice = env(lowpass(tone(0.22, 420, 210, "square"), 2200), attack=0.004, decay=0.07)
|
|
hit = env(lowpass(noise(0.05, rng), 1500), decay=0.012)
|
|
return mix((0, 0.8, voice), (0, 0.7, hit))
|
|
|
|
|
|
def fall_hurt(rng):
|
|
thud = env(tone(0.2, 120, 50), decay=0.06)
|
|
dust = env(lowpass(noise(0.15, rng), 700), decay=0.04)
|
|
return mix((0, 1.0, thud), (0, 0.6, dust), (0.03, 0.6, hurt(rng)))
|
|
|
|
|
|
def pickup(rng):
|
|
a = env(tone(0.06, 880, shape="tri"), decay=0.03)
|
|
b = env(tone(0.09, 1320, shape="tri"), decay=0.04)
|
|
return mix((0, 0.8, a), (0.05, 1.0, b))
|
|
|
|
|
|
def wobble(sec, f0, f1, depth, rate):
|
|
n = int(RATE * sec)
|
|
out, phase = [], 0.0
|
|
for i in range(n):
|
|
t = i / RATE
|
|
f = f0 * (f1 / f0) ** (i / max(n - 1, 1)) * (1 + depth * math.sin(2 * math.pi * rate * t))
|
|
phase += 2 * math.pi * f / RATE
|
|
out.append(math.sin(phase))
|
|
return out
|
|
|
|
|
|
def slime_hit(rng):
|
|
squelch = env(wobble(0.16, 220, 110, 0.25, 30), attack=0.003, decay=0.05)
|
|
wet = env(lowpass(noise(0.1, rng), 1200), decay=0.025)
|
|
return mix((0, 1.0, squelch), (0, 0.6, wet))
|
|
|
|
|
|
def slime_die(rng):
|
|
squelch = env(wobble(0.35, 260, 70, 0.3, 22), attack=0.003, decay=0.12)
|
|
splat = grains(rng, 6, 0.2, lambda r: env(lowpass(noise(0.06, r), 1500), decay=0.015))
|
|
return mix((0, 1.0, squelch), (0.02, 0.7, splat))
|
|
|
|
|
|
SOUNDS = [
|
|
# (directory, name, function, variants)
|
|
(CORE, "strata_dig_stone", dig_stone, 2),
|
|
(CORE, "strata_dug_stone", dug_stone, 1),
|
|
(CORE, "strata_dig_dirt", dig_dirt, 2),
|
|
(CORE, "strata_dug_dirt", dug_dirt, 1),
|
|
(CORE, "strata_dig_wood", dig_wood, 2),
|
|
(CORE, "strata_dug_wood", dug_wood, 1),
|
|
(CORE, "strata_dig_leaves", dig_leaves, 2),
|
|
(CORE, "strata_dug_leaves", dug_leaves, 1),
|
|
(CORE, "strata_pickup", pickup, 1),
|
|
# played by the engine itself when the local player is hurt
|
|
(CORE, "player_damage", hurt, 1),
|
|
(CORE, "player_falling_damage", fall_hurt, 1),
|
|
(MOBS, "strata_slime_hit", slime_hit, 1),
|
|
(MOBS, "strata_slime_die", slime_die, 1),
|
|
]
|
|
|
|
|
|
def write_ogg(samples, path):
|
|
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
|
wav = tmp.name
|
|
try:
|
|
with wave.open(wav, "wb") as w:
|
|
w.setnchannels(1)
|
|
w.setsampwidth(2)
|
|
w.setframerate(RATE)
|
|
w.writeframes(b"".join(struct.pack("<h", int(max(-1, min(1, s)) * 32767)) for s in samples))
|
|
subprocess.run(["oggenc", "-Q", "-q", OGG_QUALITY, "-o", path, wav], check=True)
|
|
finally:
|
|
os.unlink(wav)
|
|
|
|
|
|
def main():
|
|
for directory, name, fn, variants in SOUNDS:
|
|
os.makedirs(directory, exist_ok=True)
|
|
for v in range(1, variants + 1):
|
|
rng = random.Random(f"{name}.{v}")
|
|
# Luanti picks a random variant among name.1.ogg, name.2.ogg, ...
|
|
fname = f"{name}.{v}.ogg" if variants > 1 else f"{name}.ogg"
|
|
write_ogg(normalize(fn(rng)), os.path.join(directory, fname))
|
|
print("wrote", os.path.relpath(os.path.join(directory, fname), ROOT))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|