"""The synth kit every sound module shares (tools/gen_sounds.py and tools/streak_anims/*_sounds.py). 22.05 kHz mono; a sound is a list of floats (buf), mixed into by the add_* helpers, then written normalised by write(). Standard library only, deterministic (seed every random source). """ import math import os import random import struct import wave SR = 22050 ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) OUT = os.path.join(ROOT, "shared", "sounds") TAU = 2 * math.pi def buf(dur): return [0.0] * int(SR * dur) def env(t, a, d): """Attack a seconds, then exponential decay with time constant d.""" return (t / a if t < a else math.exp(-(t - a) / d)) if a > 0 else math.exp(-t / d) def add_tone(b, start, dur, f0, f1=None, amp=0.5, a=0.005, d=0.15, shape="sine", vib=(0, 0)): """A tone gliding f0 -> f1 (exponentially), with optional vibrato (rate Hz, depth ratio).""" f1 = f1 or f0 ph = 0.0 n0 = int(start * SR) for k in range(int(dur * SR)): if n0 + k >= len(b): break t = k / SR f = f0 * (f1 / f0) ** (t / dur) if vib[0]: f *= 1 + vib[1] * math.sin(TAU * vib[0] * t) ph += TAU * f / SR if shape == "sine": v = math.sin(ph) elif shape == "tri": v = 2 / math.pi * math.asin(math.sin(ph)) elif shape == "square": v = 0.6 if math.sin(ph) >= 0 else -0.6 else: # saw v = ((ph / TAU) % 1.0) * 2 - 1 fade = min(1.0, (dur - t) / 0.01) # no click at the end b[n0 + k] += v * amp * env(t, a, d) * fade def add_noise(b, start, dur, amp=0.4, a=0.002, d=0.1, lp0=0.5, lp1=None, seed=1): """Noise through a one-pole lowpass whose coefficient sweeps lp0 -> lp1 (0..1, 1 = open).""" rng = random.Random(seed) lp1 = lp0 if lp1 is None else lp1 y = 0.0 n0 = int(start * SR) for k in range(int(dur * SR)): if n0 + k >= len(b): break t = k / SR c = lp0 + (lp1 - lp0) * (t / dur) y += c * (rng.uniform(-1, 1) - y) fade = min(1.0, (dur - t) / 0.01) b[n0 + k] += y * amp * env(t, a, d) * fade * 2 def add_pluck(b, start, freq, dur=0.6, amp=0.5, seed=3, damp=0.996): """Karplus-Strong plucked string.""" rng = random.Random(seed) n = max(2, int(SR / freq)) line = [rng.uniform(-1, 1) for _ in range(n)] n0 = int(start * SR) for k in range(int(dur * SR)): if n0 + k >= len(b): break v = line[k % n] line[k % n] = damp * 0.5 * (v + line[(k + 1) % n]) b[n0 + k] += v * amp def add_bell(b, start, f, amp=0.35, d=0.4): for ratio, a in ((1, 1.0), (2.76, 0.5), (5.4, 0.25), (8.93, 0.12)): add_tone(b, start, min(1.0 - start, d * 2.5), f * ratio, amp=amp * a, a=0.002, d=d / ratio ** 0.5) CALM_DUR = 2.0 def cbuf(): return buf(CALM_DUR) def pad(b, start, dur, f, amp=0.2, a=0.4, d=0.8, vib=(0, 0), shape="sine"): add_tone(b, start, dur, f, amp=amp, a=a, d=d, vib=vib, shape=shape) def soft_bell(b, start, f, amp=0.15, d=0.8): for ratio, a in ((1, 1.0), (2.0, 0.3), (3.01, 0.12)): add_tone(b, start, min(CALM_DUR - start, d * 3), f * ratio, amp=amp * a, a=0.01, d=d / ratio ** 0.5) def ease_off(b, secs=0.5): """Fade the last secs out, so a long, soft sound never stops dead.""" n = int(secs * SR) for k in range(n): b[len(b) - n + k] *= (1 - k / n) ** 2 return b def write(name, b, level=0.7, out=None): """b normalised to level (0..1 of full scale) as /.wav.""" peak = max(1e-6, max(abs(v) for v in b)) g = level / peak with wave.open(os.path.join(out or OUT, name + ".wav"), "wb") as w: w.setnchannels(1) w.setsampwidth(2) w.setframerate(SR) w.writeframes(b"".join(struct.pack("