#!/usr/bin/env python3 """The onboarding comedy's chat sounds, written to `resources/audio/sounds/onboarding/` as 48 kHz mono 16-bit WAVs. Standard library only, so it runs anywhere: python3 scripts/sounds/chat.py - `chat_incoming`: a message arriving, two quick bright pips (G5 -> B5). - `chat_sent`: a message leaving, a breathy upward swish (500 Hz -> 2.4 kHz). - `chat_read`: the deadpan `"Read"`, one dry low pip (about 360 Hz). - `chat_rewind`: the story winding back, a soft downward swish that swells and stops short, like tape pulled back (2.6 kHz -> 420 Hz). - `chat_tapback`: a tapback landing, one tiny bright tick (about 1.9 kHz). - `slow_down`: time slowing, the clicks smearing down like a tape stop (a band of noise 1.8 kHz -> 160 Hz over a falling tone). - `slam`: a comic title hitting the stage, a short crack over a saturated bass drop (120 -> 36 Hz). - `confetti`: a party popper, a crack and a shower of tiny bright ticks that thin out as the paper falls. - `fireworks`: a quiet whistle rising (900 Hz -> 2.2 kHz), a soft boom and the crackle after it. - `word_1` to `word_3`: a word shooting out of the HUD, a tiny bright tick on a short airy flick upward, each a little different so a burst of them never repeats. - `card_burn`: a card struck and dissolving, a dry crack over a soft low knock, then a falling hiss with embers crackling out. The heartbeat and the triumph candidates come from `comedy.py`. Peaks are set to sit with the other onboarding cues (about -14 dBFS).""" import math import random import struct import wave from pathlib import Path SR = 48_000 OUT = Path(__file__).resolve().parents[2] / "resources/audio/sounds/onboarding" rng = random.Random(7) def silence(secs): return [0.0] * int(secs * SR) def add(into, at, x): i = int(at * SR) for j, v in enumerate(x): if i + j < len(into): into[i + j] += v def pip(hz, secs, tau, attack=0.002, h2=0.0): out = [] for i in range(int(secs * SR)): t = i / SR env = min(1.0, t / attack) * math.exp(-t / tau) out.append(env * (math.sin(2 * math.pi * hz * t) + h2 * math.sin(4 * math.pi * hz * t))) return out def swish(secs, lo, hi, backwards=False): """White noise through a resonant band-pass sweeping `lo` -> `hi`. `backwards` swells toward the end instead and stops short.""" n = int(secs * SR) low = band = 0.0 out = [] for i in range(n): u = i / n f = lo * (hi / lo) ** u g = 2 * math.sin(math.pi * f / SR) q = 0.9 x = rng.uniform(-1.0, 1.0) low += g * band high = x - low - q * band band += g * high # Swells in quickly and trails off, like breath. env = math.sin(math.pi * (1 - u if backwards else u) ** 0.7) ** 1.5 out.append(env * band) return out def thump(secs, hi, lo, tau, h2=0.25, attack=0.004): """A falling sine thump, `hi` -> `lo` Hz, decaying over `tau`.""" out = [] phase = 0.0 for i in range(int(secs * SR)): t = i / SR f = lo + (hi - lo) * math.exp(-t / (tau * 0.6)) phase += 2 * math.pi * f / SR env = min(1.0, t / attack) * math.exp(-t / tau) out.append(env * (math.sin(phase) + h2 * math.sin(2 * phase))) return out def noise_burst(secs, tau, hp=0.85): """A short crack: white noise, high-passed by a one-pole difference.""" out = [] prev = 0.0 for i in range(int(secs * SR)): t = i / SR x = rng.uniform(-1.0, 1.0) out.append((x - hp * prev) * math.exp(-t / tau)) prev = x return out def finish(x, peak_dbfs, fade=0.005): n = int(fade * SR) for i in range(min(n, len(x))): x[-1 - i] *= i / n top = max(abs(v) for v in x) or 1.0 gain = 10 ** (peak_dbfs / 20) / top return [v * gain for v in x] def write(name, x): with wave.open(str(OUT / f"{name}.wav"), "wb") as w: w.setnchannels(1) w.setsampwidth(2) w.setframerate(SR) w.writeframes(b"".join(struct.pack("