Release / build (push) Successful in 4m32s
The card recording is very quiet with rumble all through it, and its landing turns into a harsh crack once brought up to the others' loudness. The slide is clean above 1 kHz, so the recording is now filtered below 900 Hz and the slide used instead, as chosen by ear: - a card played: the slide - the deal: three slides, each a little faster or slower - a trick gathered: the slide played slower and deeper Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
259 lines
10 KiB
Python
259 lines
10 KiB
Python
"""
|
|
Make the game's sounds from the recordings in audio_assets/, and synthesise the two that have no recording.
|
|
|
|
python tools/make_sounds.py (needs numpy, and ffmpeg on the PATH or in FFMPEG)
|
|
|
|
Each sound is cut from its recording and cleaned up:
|
|
- the card recording's rumble is filtered out, leaving the slide,
|
|
- the silence before the sound is trimmed, so it plays the moment it is asked for,
|
|
- the end fades out instead of being cut.
|
|
All of them are then set to the same loudness: the loudest 100 ms of each, measured the way the ear hears it
|
|
(K-weighted, as in ITU-R BS.1770), is brought to TARGET_LUFS, kept a little below what games usually use so it
|
|
never blares, with the peak held under PEAK_DBFS.
|
|
|
|
Writes shared/src/commonMain/composeResources/files/sounds/<sound>.wav, one per hr.bela.shared.Sound: 44.1 kHz
|
|
16-bit mono WAV, which every platform decodes without the silence MP3 adds at the start.
|
|
"""
|
|
|
|
import math
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import wave
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
SOURCES = ROOT / "audio_assets"
|
|
OUT = ROOT / "shared" / "src" / "commonMain" / "composeResources" / "files" / "sounds"
|
|
FFMPEG = os.environ.get("FFMPEG", "ffmpeg")
|
|
|
|
RATE = 44_100
|
|
TARGET_LUFS = -20.0 # loudest 100 ms of every sound; the old synthesised chimes were about -14
|
|
PEAK_DBFS = -4.0 # no sample above this
|
|
|
|
|
|
# ---------------------------------------------------------------- reading and cutting
|
|
|
|
def recording(prefix: str) -> np.ndarray:
|
|
"""The recording whose file name starts with [prefix], as mono float samples at RATE."""
|
|
matches = sorted(SOURCES.glob(prefix + "*"))
|
|
if not matches:
|
|
sys.exit(f"No recording {prefix}* in {SOURCES}")
|
|
raw = subprocess.run(
|
|
[FFMPEG, "-v", "error", "-i", str(matches[0]), "-ac", "1", "-ar", str(RATE), "-f", "f32le", "-"],
|
|
check=True, capture_output=True,
|
|
).stdout
|
|
return np.frombuffer(raw, dtype="<f4").astype(np.float64)
|
|
|
|
|
|
def cut(x: np.ndarray, start: float, end: float, fade_in: float = 0.002, fade_out: float = 0.05) -> np.ndarray:
|
|
"""[start, end) seconds of [x], faded in and out (a cut mid-sound would click)."""
|
|
y = x[int(start * RATE):int(end * RATE)].copy()
|
|
fade(y, fade_in, fade_out)
|
|
return y
|
|
|
|
|
|
def fade(y: np.ndarray, fade_in: float, fade_out: float) -> None:
|
|
n_in = min(len(y), int(fade_in * RATE))
|
|
n_out = min(len(y), int(fade_out * RATE))
|
|
if n_in:
|
|
y[:n_in] *= np.linspace(0, 1, n_in)
|
|
if n_out:
|
|
# A raised cosine sounds smoother than a straight line.
|
|
y[-n_out:] *= 0.5 + 0.5 * np.cos(np.linspace(0, math.pi, n_out))
|
|
|
|
|
|
def trim_start(y: np.ndarray, below_db: float = -40.0) -> np.ndarray:
|
|
"""Drops the near-silence before the sound starts, keeping a millisecond before the onset."""
|
|
threshold = np.abs(y).max() * 10 ** (below_db / 20)
|
|
onset = int(np.argmax(np.abs(y) > threshold))
|
|
return y[max(0, onset - RATE // 1000):]
|
|
|
|
|
|
def speed(y: np.ndarray, factor: float) -> np.ndarray:
|
|
"""[y] played faster (factor above 1) or slower, its pitch with it, as a tape would."""
|
|
n = int(len(y) / factor)
|
|
return np.interp(np.arange(n) * factor, np.arange(len(y)), y)
|
|
|
|
|
|
def layer(parts: list[tuple[float, np.ndarray]]) -> np.ndarray:
|
|
"""Sounds mixed together, each starting at its time in seconds."""
|
|
length = max(int(at * RATE) + len(p) for at, p in parts)
|
|
out = np.zeros(length)
|
|
for at, p in parts:
|
|
i = int(at * RATE)
|
|
out[i:i + len(p)] += p
|
|
return out
|
|
|
|
|
|
# ---------------------------------------------------------------- filters
|
|
|
|
def biquad(x: np.ndarray, b: tuple, a: tuple) -> np.ndarray:
|
|
b0, b1, b2 = (v / a[0] for v in b)
|
|
a1, a2 = a[1] / a[0], a[2] / a[0]
|
|
y = np.zeros_like(x)
|
|
x1 = x2 = y1 = y2 = 0.0
|
|
for i, v in enumerate(x):
|
|
out = b0 * v + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2
|
|
x2, x1, y2, y1 = x1, v, y1, out
|
|
y[i] = out
|
|
return y
|
|
|
|
|
|
def highpass(x: np.ndarray, hz: float, q: float = 0.7071) -> np.ndarray:
|
|
w0 = 2 * math.pi * hz / RATE
|
|
alpha = math.sin(w0) / (2 * q)
|
|
c = math.cos(w0)
|
|
return biquad(x, ((1 + c) / 2, -(1 + c), (1 + c) / 2), (1 + alpha, -2 * c, 1 - alpha))
|
|
|
|
|
|
def k_weighted(x: np.ndarray) -> np.ndarray:
|
|
"""The BS.1770 K-weighting: a high shelf (the head's effect) and a high pass (the ear's)."""
|
|
gain_db, q, hz = 3.99984385397, 0.7071752369554193, 1681.9744509555319
|
|
a = 10 ** (gain_db / 40)
|
|
w0 = 2 * math.pi * hz / RATE
|
|
alpha = math.sin(w0) / (2 * q)
|
|
c = math.cos(w0)
|
|
shelf = biquad(
|
|
x,
|
|
(a * ((a + 1) + (a - 1) * c + 2 * math.sqrt(a) * alpha),
|
|
-2 * a * ((a - 1) + (a + 1) * c),
|
|
a * ((a + 1) + (a - 1) * c - 2 * math.sqrt(a) * alpha)),
|
|
((a + 1) - (a - 1) * c + 2 * math.sqrt(a) * alpha,
|
|
2 * ((a - 1) - (a + 1) * c),
|
|
(a + 1) - (a - 1) * c - 2 * math.sqrt(a) * alpha),
|
|
)
|
|
return highpass(shelf, 38.13547087613982, 0.5003270373253953)
|
|
|
|
|
|
def loudness(x: np.ndarray) -> float:
|
|
"""The loudest 100 ms, in LUFS: short sounds and long ones on the same scale."""
|
|
k = k_weighted(np.concatenate([x, np.zeros(RATE // 10)]))
|
|
window = RATE // 10
|
|
power = np.convolve(k * k, np.ones(window) / window, mode="valid")
|
|
return -0.691 + 10 * math.log10(max(power.max(), 1e-12))
|
|
|
|
|
|
def peak_db(x: np.ndarray) -> float:
|
|
return 20 * math.log10(max(np.abs(x).max(), 1e-12))
|
|
|
|
|
|
# ---------------------------------------------------------------- synthesis (no recording for these yet)
|
|
|
|
def bell(at: float, hz: float, level: float, decay: float, length: float) -> np.ndarray:
|
|
"""A struck tone: each partial dies away faster the higher it is (as SoundSynth did)."""
|
|
t = np.arange(int(length * RATE)) / RATE
|
|
attack = np.minimum(t / 0.004, 1)
|
|
v = sum(amp * np.sin(2 * math.pi * hz * mult * t) * np.exp(-t * mult / decay)
|
|
for mult, amp in ((1.0, 1.0), (2.0, 0.35), (3.0, 0.12), (4.2, 0.06)))
|
|
return layer([(at, level * attack * v)])
|
|
|
|
|
|
def trump() -> np.ndarray:
|
|
"""Trump called: a strummed C major chord."""
|
|
notes = (523.25, 659.25, 783.99, 1046.5)
|
|
y = layer([(i * 0.04, bell(0, hz, 0.7, 0.6, 1.5)) for i, hz in enumerate(notes)])
|
|
fade(y, 0, 0.3)
|
|
return y
|
|
|
|
|
|
def hand() -> np.ndarray:
|
|
"""The hand is over: two bells, falling."""
|
|
y = layer([(0.0, bell(0, 1046.5, 0.9, 0.5, 1.3)), (0.12, bell(0, 783.99, 0.8, 0.6, 1.3))])
|
|
fade(y, 0, 0.3)
|
|
return y
|
|
|
|
|
|
# ---------------------------------------------------------------- the sounds
|
|
|
|
def make() -> dict[str, np.ndarray]:
|
|
nudge = trim_start(recording("a_simple_nudge_ring"))
|
|
ring = trim_start(recording("simple_ring_as_a_rem"))
|
|
clock = recording("a_clock_second")
|
|
# The card recording is very quiet, with rumble all through it. The card's slide (0.55 to 0.75 s) is all above
|
|
# 1 kHz, so filtering below 900 Hz leaves only the slide; the landing at 1.19 s is a harsh crack once turned
|
|
# up, so it is not used.
|
|
cards = highpass(highpass(recording("poker_card_sliding"), 900), 900)
|
|
happy = trim_start(recording("a_short_happy_melody"))
|
|
sad = trim_start(recording("a_short_sad_melody"))
|
|
|
|
turn = cut(nudge, 0, 0.30, fade_out=0.15)
|
|
slide = trim_start(cut(cards, 0.555, 0.745, fade_in=0.008, fade_out=0.05), -30)
|
|
return {
|
|
# Your turn: the soft nudge. To call trump: the nudge twice, so the two are told apart.
|
|
"turn": turn,
|
|
"bid": layer([(0.0, turn), (0.15, turn)]),
|
|
# 10 seconds left: the reminder ring, three strikes. The last 5 seconds: the clock's tick (without its tock).
|
|
"warning": cut(ring, 0, 1.25, fade_out=0.4),
|
|
"tick": trim_start(cut(clock, 0.072, 0.15, fade_in=0.003, fade_out=0.04)),
|
|
# The card's slide: a card played. The deal: three slides, each a little faster or slower so they do not
|
|
# sound like one sound repeated. A trick swept off the table: the slide played slower, deeper.
|
|
"card": slide,
|
|
"deal": layer([(0.0, speed(slide, 1.06) * 0.8), (0.085, slide), (0.17, speed(slide, 0.95) * 0.9)]),
|
|
"trick": speed(cut(cards, 0.555, 0.745, fade_in=0.02, fade_out=0.08), 0.75),
|
|
"trump": trump(),
|
|
"hand": hand(),
|
|
"win": cut(happy, 0, 2.5, fade_out=0.35),
|
|
"lose": cut(sad, 0, 2.6, fade_out=0.35),
|
|
# Someone sat down in your room: the ring's first two strikes, like a doorbell.
|
|
"join": cut(ring, 0, 0.22, fade_out=0.1),
|
|
}
|
|
|
|
|
|
def limit(y: np.ndarray, ceiling_db: float, lookahead: float = 0.0015, release: float = 0.015) -> np.ndarray:
|
|
"""
|
|
Holds the peaks under [ceiling_db] by turning the gain down just before them (looking ahead, so the first
|
|
edge of a click is caught too) and back up gently after: a click keeps its sound, only its spike is tamed.
|
|
"""
|
|
ceiling = 10 ** (ceiling_db / 20)
|
|
need = np.minimum(1.0, ceiling / np.maximum(np.abs(y), 1e-12))
|
|
ahead = int(lookahead * RATE)
|
|
padded = np.concatenate([need, np.ones(ahead)])
|
|
soonest = np.array([padded[i:i + ahead + 1].min() for i in range(len(y))])
|
|
back = math.exp(-1 / (release * RATE))
|
|
gain = np.empty_like(y)
|
|
g = 1.0
|
|
for i, want in enumerate(soonest):
|
|
g = want if want < g else want + (g - want) * back
|
|
gain[i] = g
|
|
return y * gain
|
|
|
|
|
|
def level(y: np.ndarray) -> tuple[np.ndarray, str]:
|
|
"""
|
|
[y] at TARGET_LUFS with no peak above PEAK_DBFS. A sharp click (the clock) would pass the peak before it is as
|
|
loud as the rest: its spike is limited, and it is turned up again for what the limiting took.
|
|
"""
|
|
note = ""
|
|
out = y
|
|
for _ in range(12):
|
|
out = out * 10 ** ((TARGET_LUFS - loudness(out)) / 20)
|
|
if peak_db(out) <= PEAK_DBFS + 0.01:
|
|
break
|
|
out = limit(out, PEAK_DBFS)
|
|
note = " (peaks limited)"
|
|
return out, note
|
|
|
|
|
|
def write(path: Path, y: np.ndarray) -> None:
|
|
pcm = np.clip(np.round(y * 32767), -32768, 32767).astype("<i2")
|
|
with wave.open(str(path), "wb") as w:
|
|
w.setnchannels(1)
|
|
w.setsampwidth(2)
|
|
w.setframerate(RATE)
|
|
w.writeframes(pcm.tobytes())
|
|
|
|
|
|
def main() -> None:
|
|
OUT.mkdir(parents=True, exist_ok=True)
|
|
for name, y in make().items():
|
|
y, note = level(y)
|
|
write(OUT / f"{name}.wav", y)
|
|
print(f"{name:8} {len(y) / RATE:5.2f} s {loudness(y):6.1f} LUFS peak {peak_db(y):5.1f} dBFS{note}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|