A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date. Images, audio, models and binaries go through Git LFS (.gitattributes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1003 lines
37 KiB
Python
1003 lines
37 KiB
Python
"""WRAP PARTY procedural audio generator.
|
|
|
|
Generates every SFX / loop / music track listed in Docs/DESIGN.md into Assets/Audio/.
|
|
Run: python Tools/audio/synth.py [name ...]
|
|
"""
|
|
import os
|
|
import sys
|
|
import wave
|
|
|
|
import numpy as np
|
|
from scipy import signal
|
|
|
|
SR = 44100
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
OUT = os.path.join(ROOT, "Assets", "Audio")
|
|
|
|
|
|
# ----------------------------------------------------------------------------- basics
|
|
|
|
def t_axis(dur):
|
|
return np.arange(int(dur * SR)) / SR
|
|
|
|
|
|
def rng(seed):
|
|
return np.random.default_rng(seed)
|
|
|
|
|
|
def db(x):
|
|
return 10 ** (x / 20.0)
|
|
|
|
|
|
def sos_band(lo, hi, order=2):
|
|
hi = min(hi, SR * 0.45)
|
|
return signal.butter(order, [lo, hi], btype="band", fs=SR, output="sos")
|
|
|
|
|
|
def bandpass(x, lo, hi, order=2):
|
|
return signal.sosfilt(sos_band(lo, hi, order), x)
|
|
|
|
|
|
def lowpass(x, f, order=2):
|
|
return signal.sosfilt(signal.butter(order, min(f, SR * 0.45), btype="low", fs=SR, output="sos"), x)
|
|
|
|
|
|
def highpass(x, f, order=2):
|
|
return signal.sosfilt(signal.butter(order, f, btype="high", fs=SR, output="sos"), x)
|
|
|
|
|
|
def peak(x, f, q):
|
|
b, a = signal.iirpeak(f, q, fs=SR)
|
|
return signal.lfilter(b, a, x)
|
|
|
|
|
|
def svf_sweep(x, freqs, q=1.0, mode="band"):
|
|
"""Chamberlin state-variable filter with per-sample cutoff (time-varying sweeps)."""
|
|
y = np.zeros_like(x)
|
|
low = band = 0.0
|
|
damp = 1.0 / q
|
|
for i in range(len(x)):
|
|
f = 2.0 * np.sin(np.pi * min(freqs[i], SR / 6.5) / SR)
|
|
high = x[i] - low - damp * band
|
|
band += f * high
|
|
low += f * band
|
|
y[i] = band if mode == "band" else (low if mode == "low" else high)
|
|
return y
|
|
|
|
|
|
def exp_env(n, tau, attack=0.0005):
|
|
t = np.arange(n) / SR
|
|
env = np.exp(-t / tau)
|
|
a = int(attack * SR)
|
|
if a > 0:
|
|
env[:a] *= np.linspace(0, 1, a)
|
|
return env
|
|
|
|
|
|
def adsr(n, a, d, s, r, hold=None):
|
|
"""Linear-ish ADSR with exponential decay; hold = sustain length in seconds."""
|
|
env = np.zeros(n)
|
|
ai, di, ri = int(a * SR), int(d * SR), int(r * SR)
|
|
hi = n - ai - di - ri if hold is None else int(hold * SR)
|
|
hi = max(hi, 0)
|
|
idx = 0
|
|
seg = min(ai, n - idx); env[idx:idx + seg] = np.linspace(0, 1, max(ai, 1))[:seg]; idx += seg
|
|
seg = min(di, n - idx); env[idx:idx + seg] = s + (1 - s) * np.exp(-np.linspace(0, 5, max(di, 1)))[:seg]; idx += seg
|
|
seg = min(hi, n - idx); env[idx:idx + seg] = s; idx += seg
|
|
seg = min(ri, n - idx); env[idx:idx + seg] = s * np.linspace(1, 0, max(ri, 1))[:seg]; idx += seg
|
|
return env
|
|
|
|
|
|
def sweep_sine(f_start, f_end, dur, curve=4.0, phase=0.0):
|
|
n = int(dur * SR)
|
|
t = np.linspace(0, 1, n)
|
|
f = f_end + (f_start - f_end) * np.exp(-curve * t)
|
|
return np.sin(phase + 2 * np.pi * np.cumsum(f) / SR), f
|
|
|
|
|
|
def place(buf, x, at):
|
|
i = max(0, int(at * SR))
|
|
if i >= len(buf):
|
|
return buf
|
|
n = min(len(x), len(buf) - i)
|
|
buf[i:i + n] += x[:n]
|
|
return buf
|
|
|
|
|
|
def fade(x, fin=0.002, fout=0.01):
|
|
x = x.copy()
|
|
a, b = int(fin * SR), int(fout * SR)
|
|
if a > 0:
|
|
x[:a] *= np.linspace(0, 1, a) ** 2
|
|
if b > 0:
|
|
x[-b:] *= np.linspace(1, 0, b) ** 2
|
|
return x
|
|
|
|
|
|
def reverb_ir(dur=1.6, decay=0.45, seed=7, bright=6000, predelay=0.012):
|
|
r = rng(seed)
|
|
n = int(dur * SR)
|
|
ir = r.standard_normal(n) * np.exp(-np.arange(n) / SR / decay)
|
|
ir = lowpass(ir, bright)
|
|
ir = np.concatenate([np.zeros(int(predelay * SR)), ir])
|
|
ir[0] = 0
|
|
return ir / np.sqrt(np.sum(ir ** 2))
|
|
|
|
|
|
def reverb(x, wet=0.25, **kw):
|
|
ir = reverb_ir(**kw)
|
|
y = signal.fftconvolve(x, ir)[: len(x) + len(ir)]
|
|
out = np.zeros(len(y))
|
|
out[: len(x)] += x
|
|
return out + wet * y
|
|
|
|
|
|
def stereo_reverb(x, wet=0.25, **kw):
|
|
"""x mono -> stereo with decorrelated reverbs; x stereo (n,2) -> per channel."""
|
|
if x.ndim == 1:
|
|
x = np.stack([x, x], axis=1)
|
|
seed = kw.pop("seed", 7)
|
|
l = reverb(x[:, 0], wet, seed=seed, **kw)
|
|
r = reverb(x[:, 1], wet, seed=seed + 101, **kw)
|
|
return np.stack([l, r], axis=1)
|
|
|
|
|
|
def normalize(x, peak_db=-3.0):
|
|
m = np.max(np.abs(x))
|
|
return x if m < 1e-9 else x * (db(peak_db) / m)
|
|
|
|
|
|
def soft_clip(x, drive=1.0):
|
|
return np.tanh(x * drive) / np.tanh(drive)
|
|
|
|
|
|
def trim_tail(x, thresh_db=-70):
|
|
a = np.abs(x if x.ndim == 1 else x.max(axis=1))
|
|
idx = np.where(a > db(thresh_db) * a.max())[0]
|
|
end = idx[-1] + int(0.01 * SR) if len(idx) else len(x)
|
|
return x[:end]
|
|
|
|
|
|
def write(name, x, peak_db=-3.0, stereo=False, trim=True, fades=True):
|
|
x = np.asarray(x, dtype=np.float64)
|
|
if trim:
|
|
x = trim_tail(x)
|
|
if fades:
|
|
if x.ndim == 1:
|
|
x = fade(x, 0.0005, 0.008)
|
|
else:
|
|
x = np.stack([fade(x[:, 0], 0.0005, 0.008), fade(x[:, 1], 0.0005, 0.008)], axis=1)
|
|
x = normalize(x, peak_db)
|
|
if stereo and x.ndim == 1:
|
|
x = np.stack([x, x], axis=1)
|
|
data = np.clip(x * 32767, -32768, 32767).astype(np.int16)
|
|
ch = 1 if data.ndim == 1 else 2
|
|
path = os.path.join(OUT, name + ".wav")
|
|
with wave.open(path, "wb") as w:
|
|
w.setnchannels(ch)
|
|
w.setsampwidth(2)
|
|
w.setframerate(SR)
|
|
w.writeframes(data.tobytes())
|
|
print(f" {name}.wav {len(data) / SR:6.2f}s {'stereo' if ch == 2 else 'mono'}")
|
|
return x
|
|
|
|
|
|
def write_loop(name, x, peak_db=-3.0, xfade=0.25):
|
|
"""Make x loop seamlessly by crossfading its tail into its head, then write without fades."""
|
|
n = int(xfade * SR)
|
|
body = x[:-n].copy()
|
|
tail = x[-n:]
|
|
w = np.linspace(0, 1, n)
|
|
if x.ndim == 2:
|
|
w = w[:, None]
|
|
body[:n] = body[:n] * np.sqrt(w) + tail * np.sqrt(1 - w)
|
|
return write(name, body, peak_db, trim=False, fades=False)
|
|
|
|
|
|
def wrap_tail(x, loop_len):
|
|
"""Render longer than the loop and fold everything past loop_len back to the start."""
|
|
n = int(loop_len * SR)
|
|
out = x[:n].copy()
|
|
rest = x[n:]
|
|
while len(rest):
|
|
k = min(len(rest), n)
|
|
out[:k] += rest[:k]
|
|
rest = rest[k:]
|
|
return out
|
|
|
|
|
|
# ----------------------------------------------------------------------------- SFX
|
|
|
|
def bubble_pop(seed, f0=1500, size=1.0, dur=0.22):
|
|
"""Bubble-wrap pop: crack + resonant puff with pitch drop + low thump + air + crinkle."""
|
|
r = rng(seed)
|
|
n = int(dur * SR)
|
|
out = np.zeros(n)
|
|
# 1) initial crack: very short broadband click
|
|
cn = int(0.0025 * SR)
|
|
crack = r.standard_normal(cn) * np.exp(-np.arange(cn) / (0.0006 * SR))
|
|
crack = highpass(crack, 1800)
|
|
out[:cn] += crack * 1.0
|
|
# 2) resonant body: band noise around f0 with fast decay
|
|
body_n = int(0.09 * SR)
|
|
noise = r.standard_normal(body_n)
|
|
fsweep = f0 * (0.75 + 0.9 * np.exp(-np.arange(body_n) / (0.006 * SR)))
|
|
body = svf_sweep(noise, fsweep, q=4.0 * size, mode="band")
|
|
body *= exp_env(body_n, 0.010 * size, attack=0.0003)
|
|
out[:body_n] += body * 0.9
|
|
# 3) tonal "pok": sine with downward pitch
|
|
tone, _ = sweep_sine(f0 * 1.45, f0 * 0.62, 0.06, curve=7)
|
|
tone *= exp_env(len(tone), 0.012 * size, attack=0.0004)
|
|
out[: len(tone)] += tone * 0.55
|
|
# 4) low thump (air pushing out)
|
|
th, _ = sweep_sine(240 * size, 110, 0.06, curve=6)
|
|
th *= exp_env(len(th), 0.014 * size, attack=0.001)
|
|
out[: len(th)] += th * 0.35 * size
|
|
# 5) airy tail
|
|
an = int(0.09 * SR)
|
|
air = highpass(r.standard_normal(an), 4000) * exp_env(an, 0.02, attack=0.002)
|
|
out[int(0.002 * SR): int(0.002 * SR) + an] += air[: n - int(0.002 * SR)] * 0.10
|
|
# 6) plastic crinkle after-ticks
|
|
for _ in range(r.integers(2, 6)):
|
|
at = 0.012 + r.random() * 0.07
|
|
k = int(0.0015 * SR)
|
|
tick = highpass(r.standard_normal(k), 3000) * np.exp(-np.arange(k) / (0.0003 * SR))
|
|
place(out, tick * (0.05 + 0.12 * r.random()), at)
|
|
out = lowpass(out, 14000)
|
|
return out
|
|
|
|
|
|
def sfx_pops():
|
|
freqs = [1350, 1650, 1180, 1900, 1500, 2150, 1050, 1750, 1420, 2000]
|
|
for i, f in enumerate(freqs):
|
|
x = bubble_pop(100 + i, f0=f, size=0.9 + 0.25 * ((i * 7) % 5) / 4)
|
|
write(f"pop_{i + 1:02d}", x, -3)
|
|
# big pop: deeper, punchier, small room
|
|
a = bubble_pop(220, f0=720, size=1.8, dur=0.4) * 0.9
|
|
b = bubble_pop(221, f0=980, size=1.4, dur=0.4) * 0.4
|
|
th, _ = sweep_sine(170, 55, 0.25, curve=5)
|
|
th *= exp_env(len(th), 0.07, attack=0.001)
|
|
x = np.zeros(int(0.5 * SR))
|
|
place(x, a, 0)
|
|
place(x, b, 0.004)
|
|
place(x, th * 0.9, 0)
|
|
x = soft_clip(x * 1.6, 1.3)
|
|
x = reverb(x, 0.10, dur=0.6, decay=0.12, bright=5000)
|
|
write("pop_big", x, -2)
|
|
# golden pop: pop + sparkly chime arpeggio
|
|
x = np.zeros(int(1.4 * SR))
|
|
place(x, bubble_pop(230, f0=1600, size=1.2) * 0.9, 0)
|
|
for k, (f, at) in enumerate([(1568, 0.02), (2093, 0.07), (2637, 0.12), (3136, 0.17)]):
|
|
place(x, bell(f, 0.9, seed=240 + k) * (0.32 - 0.04 * k), at)
|
|
x = reverb(x, 0.22, dur=1.2, decay=0.35, bright=9000)
|
|
write("pop_golden", x, -3)
|
|
|
|
|
|
def bell(f, dur=1.0, seed=0, bright=1.0):
|
|
"""Glassy bell / chime: inharmonic partials with individual decays."""
|
|
t = t_axis(dur)
|
|
ratios = [1.0, 2.0, 2.76, 3.9, 5.4, 6.8]
|
|
amps = [1.0, 0.45, 0.35 * bright, 0.18 * bright, 0.12 * bright, 0.06 * bright]
|
|
decs = [0.55, 0.35, 0.22, 0.14, 0.09, 0.06]
|
|
r = rng(seed)
|
|
x = np.zeros_like(t)
|
|
for ra, am, de in zip(ratios, amps, decs):
|
|
ph = r.random() * 2 * np.pi
|
|
x += am * np.sin(2 * np.pi * f * ra * t + ph) * np.exp(-t / (de * dur))
|
|
att = int(0.0015 * SR)
|
|
x[:att] *= np.linspace(0, 1, att)
|
|
return x
|
|
|
|
|
|
def marimba(f, dur=0.8, seed=0):
|
|
t = t_axis(dur)
|
|
x = np.sin(2 * np.pi * f * t) * np.exp(-t / 0.22)
|
|
x += 0.35 * np.sin(2 * np.pi * f * 4.0 * t) * np.exp(-t / 0.05)
|
|
x += 0.12 * np.sin(2 * np.pi * f * 9.9 * t) * np.exp(-t / 0.015)
|
|
att = int(0.002 * SR)
|
|
x[:att] *= np.linspace(0, 1, att)
|
|
return x
|
|
|
|
|
|
def sfx_tape_rip():
|
|
r = rng(300)
|
|
dur = 1.15
|
|
n = int(dur * SR)
|
|
t = np.arange(n) / SR
|
|
# stick-slip crackle: impulse train with accelerating density
|
|
rate = 90 + 520 * (t / dur) ** 1.3
|
|
phase = np.cumsum(rate / SR)
|
|
imp = np.zeros(n)
|
|
edges = np.where(np.diff(np.floor(phase)) > 0)[0]
|
|
imp[edges] = 0.4 + r.random(len(edges)) * 0.9
|
|
# extra random micro crackles
|
|
extra = r.random(n) < 0.012
|
|
imp[extra] += r.random(extra.sum()) * 0.5
|
|
grains = signal.lfilter([1.0], [1.0, -0.6], imp)
|
|
noise = r.standard_normal(n)
|
|
grain_env = signal.lfilter([1.0], [1.0, -0.985], imp) # each grain gates a little noise
|
|
grain_env /= grain_env.max()
|
|
sweep = 900 + 3800 * (t / dur) ** 1.2
|
|
body = svf_sweep(noise * (0.25 + grain_env), sweep, q=1.6)
|
|
crackle = highpass(grains + 0.3 * noise * grain_env, 1500)
|
|
env = np.minimum(1, t / 0.03) * np.where(t > dur - 0.12, np.clip((dur - t) / 0.12, 0, 1), 1)
|
|
env *= 0.75 + 0.25 * np.sin(2 * np.pi * 3.2 * t) ** 2
|
|
x = (body * 0.8 + crackle * 0.55) * env
|
|
# final "thwip" as the tape leaves the box
|
|
th = svf_sweep(r.standard_normal(int(0.12 * SR)), np.linspace(2500, 7000, int(0.12 * SR)), q=2.0)
|
|
th *= exp_env(len(th), 0.03, attack=0.004)
|
|
x = np.concatenate([x, np.zeros(int(0.2 * SR))])
|
|
place(x, th * 0.9, dur - 0.07)
|
|
write("tape_rip", x, -3)
|
|
|
|
|
|
def cardboard_hit(seed, f_body=160, dur=0.35, weight=1.0):
|
|
r = rng(seed)
|
|
n = int(dur * SR)
|
|
x = np.zeros(n)
|
|
th, _ = sweep_sine(f_body * 1.8, f_body * 0.7, dur, curve=9)
|
|
th *= exp_env(n, 0.045 * weight, attack=0.0015)
|
|
x += th * 0.9
|
|
nz = r.standard_normal(n)
|
|
box = peak(nz, 260 + r.random() * 120, 3) * 0.6 + bandpass(nz, 300, 1800) * 0.5
|
|
x += box * exp_env(n, 0.03 * weight, attack=0.0008)
|
|
k = int(0.004 * SR)
|
|
x[:k] += highpass(r.standard_normal(k), 1000) * np.exp(-np.arange(k) / (0.001 * SR)) * 0.4
|
|
# paper flutter
|
|
fl = bandpass(r.standard_normal(n), 1200, 5000) * exp_env(n, 0.06, attack=0.005) * 0.12
|
|
x += fl
|
|
return lowpass(x, 7000)
|
|
|
|
|
|
def sfx_cardboard():
|
|
for i, (f, w) in enumerate([(150, 1.0), (125, 1.25), (180, 0.85)]):
|
|
write(f"cardboard_thud_{i + 1}", cardboard_hit(400 + i, f, 0.4, w), -4)
|
|
|
|
|
|
def sfx_box_burst():
|
|
r = rng(500)
|
|
dur = 1.0
|
|
x = np.zeros(int(dur * SR))
|
|
place(x, cardboard_hit(501, 120, 0.4, 1.2) * 0.8, 0)
|
|
# flaps flapping open (four quick papery thwacks)
|
|
for k in range(4):
|
|
n = int(0.09 * SR)
|
|
f = bandpass(r.standard_normal(n), 500 + 300 * k, 3200) * exp_env(n, 0.018, attack=0.002)
|
|
place(x, f * (0.6 - 0.08 * k), 0.015 + 0.045 * k + r.random() * 0.01)
|
|
# whoosh puff of air and peanuts
|
|
n = int(0.6 * SR)
|
|
sw = svf_sweep(r.standard_normal(n), 400 + 2600 * np.sin(np.linspace(0, np.pi, n)) ** 1.5, q=0.9)
|
|
sw *= np.sin(np.linspace(0, np.pi, n)) ** 2
|
|
place(x, sw * 0.6, 0.03)
|
|
# packing peanut rattles: tiny styrofoam squeaky ticks
|
|
for _ in range(26):
|
|
at = 0.08 + r.random() * 0.75
|
|
m = int(0.012 * SR)
|
|
tick = bandpass(r.standard_normal(m), 2000, 9000) * exp_env(m, 0.002)
|
|
place(x, tick * (0.08 + 0.15 * r.random()) * (1 - at), at)
|
|
write("box_burst", reverb(x, 0.10, dur=0.5, decay=0.12), -3)
|
|
|
|
|
|
def crinkle(seed, dur, density=900, lo=2500, hi=11000, env=None):
|
|
r = rng(seed)
|
|
n = int(dur * SR)
|
|
imp = (r.random(n) < density / SR) * r.random(n) ** 2
|
|
imp = signal.lfilter([1.0], [1.0, -0.7], imp)
|
|
x = bandpass(imp + 0.03 * r.standard_normal(n), lo, hi)
|
|
if env is None:
|
|
env = np.sin(np.linspace(0, np.pi, n)) ** 0.8
|
|
return x * env
|
|
|
|
|
|
def sfx_plastic_grab():
|
|
n = int(0.3 * SR)
|
|
env = np.minimum(1, np.arange(n) / (0.01 * SR)) * np.exp(-np.arange(n) / (0.09 * SR))
|
|
x = crinkle(600, 0.3, 1400, env=env)
|
|
x += lowpass(rng(601).standard_normal(n), 900) * env * 0.08
|
|
write("plastic_grab", x, -6)
|
|
|
|
|
|
def sfx_whoosh():
|
|
r = rng(700)
|
|
n = int(0.45 * SR)
|
|
shape = np.sin(np.linspace(0, np.pi, n)) ** 2.2
|
|
f = 350 + 1500 * np.sin(np.linspace(0, np.pi, n)) ** 1.4
|
|
x = svf_sweep(r.standard_normal(n), f, q=1.3) * shape
|
|
write("throw_whoosh", x, -5)
|
|
|
|
|
|
def sfx_recycler():
|
|
r = rng(800)
|
|
dur = 1.7
|
|
n = int(dur * SR)
|
|
t = np.arange(n) / SR
|
|
env = np.minimum(1, t / 0.06) * np.clip((dur - t) / 0.5, 0, 1)
|
|
motor = signal.sawtooth(2 * np.pi * 58 * t) + 0.5 * signal.sawtooth(2 * np.pi * 116.5 * t)
|
|
motor = lowpass(motor, 600) * 0.35
|
|
rot = 0.55 + 0.45 * np.sin(2 * np.pi * 14 * t) ** 2
|
|
grind = bandpass(r.standard_normal(n), 300, 3500) * rot * 0.6
|
|
crunch = crinkle(801, dur, 700, 1500, 8000, env=np.ones(n)) * 1.4
|
|
pops = np.zeros(n)
|
|
for k in range(9):
|
|
p = bubble_pop(810 + k, f0=900 + r.random() * 900, size=1.0)
|
|
place(pops, p * (0.25 + 0.3 * r.random()), 0.1 + r.random() * 1.1)
|
|
x = (motor + grind + crunch) * env + pops
|
|
x = soft_clip(x, 1.4)
|
|
write("recycler_grind", x, -4)
|
|
# accept: chunk + cheerful two-note marimba ding
|
|
y = np.zeros(int(1.0 * SR))
|
|
place(y, cardboard_hit(820, 200, 0.2, 0.6) * 0.5, 0)
|
|
place(y, marimba(784, 0.8), 0.05)
|
|
place(y, marimba(1175, 0.8) * 0.9, 0.13)
|
|
place(y, bell(2349, 0.6, 821) * 0.15, 0.13)
|
|
write("recycler_accept", reverb(y, 0.18, dur=0.8, decay=0.2), -4)
|
|
|
|
|
|
def sfx_cash_register():
|
|
r = rng(900)
|
|
x = np.zeros(int(1.6 * SR))
|
|
# mechanical drawer clunk + key clack
|
|
for at, f in [(0.0, 3200), (0.035, 2500)]:
|
|
m = int(0.03 * SR)
|
|
clk = bandpass(r.standard_normal(m), f * 0.6, f * 1.6) * exp_env(m, 0.005)
|
|
place(x, clk * 0.6, at)
|
|
clunk = cardboard_hit(901, 110, 0.25, 0.8)
|
|
clunk = peak(clunk, 900, 4) * 0.5 + clunk * 0.5
|
|
place(x, clunk * 0.7, 0.06)
|
|
rattle = bandpass(r.standard_normal(int(0.15 * SR)), 2500, 7000) * exp_env(int(0.15 * SR), 0.03)
|
|
place(x, rattle * 0.25, 0.08)
|
|
# ka-CHING: two bright bell strikes
|
|
place(x, bell(2093, 1.0, 902, 1.2) * 0.45, 0.12)
|
|
ching = bell(2637, 1.3, 903, 1.4) + 0.6 * bell(3322, 1.3, 904, 1.2)
|
|
tt = t_axis(1.3)
|
|
ching *= 1 + 0.15 * np.sin(2 * np.pi * 9 * tt)
|
|
place(x, ching * 0.55, 0.2)
|
|
write("cash_register", reverb(x, 0.15, dur=0.9, decay=0.25, bright=10000), -3)
|
|
|
|
|
|
def sfx_coin_tick():
|
|
t = t_axis(0.08)
|
|
x = (np.sin(2 * np.pi * 3300 * t) + 0.6 * np.sin(2 * np.pi * 4950 * t) + 0.3 * np.sin(2 * np.pi * 7400 * t))
|
|
x *= np.exp(-t / 0.014)
|
|
k = int(0.001 * SR)
|
|
x[:k] += highpass(rng(950).standard_normal(k), 5000)
|
|
write("coin_tick", x, -8)
|
|
|
|
|
|
def blip(f0, f1, dur, tau, harm=0.25, curve=10):
|
|
x, f = sweep_sine(f0, f1, dur, curve=curve)
|
|
t = t_axis(dur)
|
|
x2 = np.sin(2 * np.pi * np.cumsum(2 * f) / SR)
|
|
y = (x + harm * x2) * np.exp(-t / tau)
|
|
a = int(0.003 * SR)
|
|
y[:a] *= np.linspace(0, 1, a) ** 2
|
|
return y
|
|
|
|
|
|
def sfx_ui():
|
|
# combo up: bubbly rising blip (pitch is scaled in-game)
|
|
x = blip(520, 1250, 0.2, 0.06, harm=0.3, curve=-0.0001)
|
|
x, f = sweep_sine(560, 1180, 0.16, curve=0.001)
|
|
t = t_axis(0.16)
|
|
fr = 560 * (1180 / 560) ** (t / 0.16)
|
|
x = np.sin(2 * np.pi * np.cumsum(fr) / SR) + 0.3 * np.sin(4 * np.pi * np.cumsum(fr) / SR)
|
|
x *= np.minimum(1, t / 0.006) * np.exp(-t / 0.07)
|
|
write("combo_up", reverb(x, 0.12, dur=0.4, decay=0.08), -6)
|
|
# combo break: descending wobbly bwomp
|
|
t = t_axis(0.45)
|
|
fr = 480 * (0.4 ** (t / 0.45)) * (1 + 0.04 * np.sin(2 * np.pi * 18 * t))
|
|
x = signal.sawtooth(2 * np.pi * np.cumsum(fr) / SR, 0.5)
|
|
x = lowpass(x, 1400) * np.minimum(1, t / 0.01) * np.exp(-t / 0.18)
|
|
write("combo_break", x, -7)
|
|
# hover: tiny soft blip
|
|
t = t_axis(0.06)
|
|
x = np.sin(2 * np.pi * (1300 + 300 * t / 0.06) * t) * np.minimum(1, t / 0.004) * np.exp(-t / 0.015)
|
|
write("ui_hover", x, -14)
|
|
# click: round "bloop"
|
|
t = t_axis(0.12)
|
|
fr = 620 + 600 * (1 - np.exp(-t / 0.012))
|
|
x = np.sin(2 * np.pi * np.cumsum(fr) / SR) * np.minimum(1, t / 0.002) * np.exp(-t / 0.03)
|
|
x += 0.3 * bubble_pop(960, 1900, 0.6, 0.12)[: len(x)]
|
|
write("ui_click", x, -7)
|
|
# open / close: two-note bloops with swish
|
|
def two(f1, f2, seed):
|
|
y = np.zeros(int(0.45 * SR))
|
|
place(y, blip(f1 * 0.9, f1, 0.18, 0.05, 0.2, 30), 0)
|
|
place(y, blip(f2 * 0.9, f2, 0.22, 0.07, 0.2, 30), 0.07)
|
|
n = int(0.2 * SR)
|
|
sw = svf_sweep(rng(seed).standard_normal(n), np.linspace(1500, 5000, n) if f2 > f1 else np.linspace(5000, 1500, n), q=1.2)
|
|
place(y, sw * np.sin(np.linspace(0, np.pi, n)) ** 2 * 0.25, 0)
|
|
return reverb(y, 0.12, dur=0.5, decay=0.1)
|
|
write("ui_open", two(660, 990, 970), -8)
|
|
write("ui_close", two(880, 587, 971), -8)
|
|
# purchase: coins + sparkle arpeggio
|
|
y = np.zeros(int(1.4 * SR))
|
|
for k in range(5):
|
|
tt = t_axis(0.12)
|
|
c = (np.sin(2 * np.pi * (3000 + 400 * k) * tt) + 0.5 * np.sin(2 * np.pi * (4600 + 300 * k) * tt)) * np.exp(-tt / 0.02)
|
|
place(y, c * 0.25, 0.03 * k)
|
|
for k, f in enumerate([1047, 1319, 1568, 2093]):
|
|
place(y, marimba(f, 0.7) * 0.55, 0.12 + 0.07 * k)
|
|
place(y, bell(f * 2, 0.6, 980 + k) * 0.12, 0.12 + 0.07 * k)
|
|
write("purchase", reverb(y, 0.2, dur=1.0, decay=0.3), -3)
|
|
# error: soft low double buzz
|
|
y = np.zeros(int(0.45 * SR))
|
|
for k in range(2):
|
|
tt = t_axis(0.13)
|
|
b = signal.square(2 * np.pi * 165 * tt) * 0.6 + np.sin(2 * np.pi * 165 * tt)
|
|
b = lowpass(b, 900) * np.minimum(1, tt / 0.005) * np.exp(-tt / 0.08)
|
|
place(y, b, 0.16 * k)
|
|
write("error", y, -8)
|
|
|
|
|
|
def saw_voice(f, dur, detune=0.006, n_voices=3, cutoff=2500):
|
|
t = t_axis(dur)
|
|
x = np.zeros_like(t)
|
|
for k in range(n_voices):
|
|
d = 1 + detune * (k - (n_voices - 1) / 2)
|
|
x += signal.sawtooth(2 * np.pi * f * d * t + k)
|
|
return lowpass(x / n_voices, cutoff)
|
|
|
|
|
|
def sfx_horn():
|
|
dur = 1.6
|
|
t = t_axis(dur)
|
|
vib = 1 + 0.006 * np.sin(2 * np.pi * 5.5 * t)
|
|
x = np.zeros_like(t)
|
|
for f in (311.1, 370.0, 466.2):
|
|
x += signal.sawtooth(2 * np.pi * np.cumsum(f * vib) / SR) * 0.33
|
|
x = peak(x, 900, 1.5) * 0.6 + lowpass(x, 1800) * 0.6
|
|
env = np.ones_like(t)
|
|
env[: int(0.03 * SR)] = np.linspace(0, 1, int(0.03 * SR))
|
|
gap = (t > 0.38) & (t < 0.46)
|
|
env[gap] = 0.05
|
|
env *= np.clip((dur - t) / 0.25, 0, 1)
|
|
env = signal.lfilter([0.02], [1, -0.98], env)
|
|
x = soft_clip(x * env * 1.5, 1.5)
|
|
write("shift_start_horn", reverb(x, 0.3, dur=1.6, decay=0.5, bright=4000, predelay=0.04), -3)
|
|
|
|
|
|
def sfx_fanfare():
|
|
x = np.zeros(int(3.2 * SR))
|
|
notes = [(523.25, 0.0), (659.25, 0.11), (783.99, 0.22), (1046.5, 0.33)]
|
|
for f, at in notes:
|
|
place(x, marimba(f, 0.9) * 0.7, at)
|
|
place(x, saw_voice(f, 0.25, cutoff=3000) * adsr(int(0.25 * SR), 0.01, 0.1, 0.5, 0.08) * 0.25, at)
|
|
# final chord stab (C major add9) with brassy saws
|
|
chord = [261.63, 329.63, 392.0, 523.25, 587.33, 659.25]
|
|
n = int(1.9 * SR)
|
|
env = adsr(n, 0.02, 0.3, 0.55, 0.9)
|
|
stab = np.zeros(n)
|
|
for f in chord:
|
|
stab += saw_voice(f, 1.9, cutoff=2800) * 0.22
|
|
place(x, stab * env, 0.48)
|
|
for f in [1046.5, 1318.5, 1568.0]:
|
|
place(x, bell(f, 1.4, int(f)) * 0.12, 0.48)
|
|
t = t_axis(0.5)
|
|
sn = bandpass(rng(990).standard_normal(len(t)), 300, 6000) * np.exp(-t / 0.15)
|
|
place(x, sn * 0.3, 0.48)
|
|
xs = stereo_reverb(x, 0.25, dur=1.8, decay=0.45, bright=8000)
|
|
write("shift_complete", xs, -2)
|
|
|
|
|
|
def sfx_stars():
|
|
for i, f in enumerate([1318.5, 1568.0, 2093.0]):
|
|
y = np.zeros(int(1.3 * SR))
|
|
place(y, marimba(f / 2, 0.6) * 0.5, 0)
|
|
place(y, bell(f, 1.1, 1100 + i, 1.3) * 0.6, 0)
|
|
for k in range(5):
|
|
place(y, bell(f * (1.5 + 0.25 * k), 0.4, 1110 + k) * 0.09, 0.04 + 0.035 * k)
|
|
write(f"star_{i + 1}", reverb(y, 0.25, dur=1.0, decay=0.3, bright=10000), -3)
|
|
|
|
|
|
def sfx_confetti():
|
|
r = rng(1200)
|
|
x = np.zeros(int(1.4 * SR))
|
|
n = int(0.12 * SR)
|
|
bang = lowpass(r.standard_normal(n), 3000) * exp_env(n, 0.02, attack=0.0005)
|
|
th, _ = sweep_sine(200, 60, 0.15, curve=6)
|
|
place(x, bang * 1.0, 0)
|
|
place(x, th * exp_env(len(th), 0.04) * 0.7, 0)
|
|
m = int(1.2 * SR)
|
|
paper = crinkle(1201, 1.2, 400, 1500, 9000, env=np.exp(-np.arange(m) / (0.4 * SR)) * np.minimum(1, np.arange(m) / (0.05 * SR)))
|
|
place(x, paper * 1.2, 0.03)
|
|
write("confetti", reverb(x, 0.12, dur=0.6, decay=0.15), -3)
|
|
|
|
|
|
def sfx_steps():
|
|
r = rng(1300)
|
|
for i in range(4):
|
|
n = int(0.16 * SR)
|
|
x = lowpass(r.standard_normal(n), 700 + 150 * i) * exp_env(n, 0.018, attack=0.002) * 1.0
|
|
th, _ = sweep_sine(130 + 15 * i, 70, 0.08, curve=5)
|
|
x[: len(th)] += th * exp_env(len(th), 0.015) * 0.6
|
|
scuff = bandpass(r.standard_normal(n), 1500, 6000) * exp_env(n, 0.03, attack=0.01) * 0.06
|
|
x += scuff
|
|
write(f"footstep_{i + 1}", x, -9)
|
|
# jump: short upward whoosh + cloth
|
|
n = int(0.25 * SR)
|
|
x = svf_sweep(r.standard_normal(n), np.linspace(500, 2200, n), q=1.0) * np.sin(np.linspace(0, np.pi, n)) ** 2
|
|
x += crinkle(1310, 0.25, 300, 800, 4000) * 0.3
|
|
write("jump", x, -9)
|
|
# land: heavier thump
|
|
n = int(0.3 * SR)
|
|
x = lowpass(r.standard_normal(n), 500) * exp_env(n, 0.03, attack=0.001)
|
|
th, _ = sweep_sine(150, 50, 0.2, curve=5)
|
|
x[: len(th)] += th * exp_env(len(th), 0.04) * 0.9
|
|
write("land", x, -6)
|
|
|
|
|
|
def sfx_unroll():
|
|
r = rng(1400)
|
|
dur = 1.8
|
|
n = int(dur * SR)
|
|
t = np.arange(n) / SR
|
|
speed = np.clip(1.4 - t / dur, 0.1, 1.0) # starts fast, slows down
|
|
rumble = lowpass(r.standard_normal(n), 220) * (0.5 + 0.5 * np.sin(2 * np.pi * np.cumsum(9 * speed) / SR) ** 2)
|
|
x = rumble * 0.9 * speed
|
|
x += crinkle(1401, dur, 1200, 2000, 9000, env=speed ** 1.5) * 0.8
|
|
# flapping of the sheet slapping the floor rhythmically
|
|
ph = np.cumsum(7 * speed) / SR
|
|
hits = np.where(np.diff(np.floor(ph)) > 0)[0]
|
|
for h in hits:
|
|
m = int(0.06 * SR)
|
|
s = bandpass(r.standard_normal(m), 400, 3000) * exp_env(m, 0.012)
|
|
place(x, s * 0.35 * speed[h], h / SR)
|
|
for k in range(4):
|
|
place(x, bubble_pop(1410 + k, 1200 + 300 * k) * 0.3, 0.2 + 0.3 * k + r.random() * 0.1)
|
|
x *= np.minimum(1, t / 0.03)
|
|
x = np.concatenate([x, np.zeros(int(0.1 * SR))])
|
|
write("roll_unroll", x, -4)
|
|
|
|
|
|
def sfx_mallet():
|
|
r = rng(1500)
|
|
n = int(0.5 * SR)
|
|
th, _ = sweep_sine(170, 48, 0.5, curve=7)
|
|
x = th * exp_env(n, 0.07, attack=0.0008) * 1.0
|
|
x += lowpass(r.standard_normal(n), 900) * exp_env(n, 0.02, attack=0.0005) * 0.7
|
|
knock = peak(r.standard_normal(n), 620, 8) * exp_env(n, 0.03) * 0.5
|
|
x += knock
|
|
x = soft_clip(x * 1.8, 1.5)
|
|
write("mallet_thump", reverb(x, 0.08, dur=0.5, decay=0.12), -2)
|
|
|
|
|
|
# ----------------------------------------------------------------------------- loops
|
|
|
|
def loop_engine():
|
|
"""Cute diesel putter. Loop length = integer number of firing cycles -> seamless."""
|
|
r = rng(1600)
|
|
rpm_hz = 21.0
|
|
cycles = 42
|
|
dur = cycles / rpm_hz
|
|
n = int(round(dur * SR))
|
|
period = n / cycles
|
|
x = np.zeros(n + int(0.2 * SR))
|
|
for c in range(cycles):
|
|
at = c * period
|
|
m = int(0.07 * SR)
|
|
amp = 0.8 + 0.3 * r.random() if c % 2 == 0 else 0.55 + 0.2 * r.random()
|
|
p = lowpass(r.standard_normal(m), 450) * exp_env(m, 0.012, attack=0.001)
|
|
th = np.sin(2 * np.pi * 62 * t_axis(0.07)) * exp_env(m, 0.018)
|
|
i = int(at)
|
|
x[i:i + m] += (p * 0.8 + th * 0.7) * amp
|
|
x = wrap_tail(x, n / SR)
|
|
t = np.arange(n) / SR
|
|
x += lowpass(signal.sawtooth(2 * np.pi * rpm_hz * 2 * t), 300) * 0.15
|
|
x += bandpass(r.standard_normal(n), 1500, 4000) * 0.02 # mechanical hiss
|
|
x = soft_clip(x * 1.3, 1.3)
|
|
write("roller_engine_loop", x, -6, trim=False, fades=False)
|
|
|
|
|
|
def loop_conveyor():
|
|
r = rng(1700)
|
|
dur = 2.0
|
|
n = int(dur * SR)
|
|
t = np.arange(n) / SR
|
|
x = lowpass(signal.sawtooth(2 * np.pi * 100 * t), 400) * 0.08
|
|
x += lowpass(signal.sawtooth(2 * np.pi * 50 * t), 200) * 0.08
|
|
x += bandpass(r.standard_normal(n), 800, 3000) * 0.05 * (1 + 0.3 * np.sin(2 * np.pi * 2 * t))
|
|
clicks = np.zeros(n + int(0.1 * SR))
|
|
for k in range(16): # roller clacks, 8 Hz, regular -> seamless
|
|
m = int(0.03 * SR)
|
|
c = bandpass(r.standard_normal(m), 900, 4000) * exp_env(m, 0.005)
|
|
i = int(k * n / 16 + r.random() * 30)
|
|
clicks[i:i + m] += c * (0.35 + 0.15 * r.random())
|
|
x += wrap_tail(clicks, dur)
|
|
write("conveyor_loop", x, -10, trim=False, fades=False)
|
|
|
|
|
|
def loop_ambience():
|
|
r = rng(1800)
|
|
dur = 24.0
|
|
n = int((dur + 1.0) * SR)
|
|
t = np.arange(n) / SR
|
|
brown = np.cumsum(r.standard_normal(n))
|
|
brown = highpass(brown, 30)
|
|
brown = lowpass(brown, 350)
|
|
brown /= np.max(np.abs(brown))
|
|
x = brown * 0.5
|
|
x += np.sin(2 * np.pi * 60 * t) * 0.025 + np.sin(2 * np.pi * 120 * t) * 0.012 # light hum
|
|
x += lowpass(highpass(r.standard_normal(n), 2000), 6000) * 0.012 # air
|
|
# distant chirpy birds through the windows
|
|
for k in range(9):
|
|
at = 1.0 + r.random() * (dur - 2)
|
|
bf = 2800 + r.random() * 1600
|
|
for j in range(r.integers(2, 5)):
|
|
tt = t_axis(0.08)
|
|
ch = np.sin(2 * np.pi * np.cumsum(bf * (1 + 0.25 * np.sin(np.pi * tt / 0.08))) / SR) * np.sin(np.pi * tt / 0.08) ** 2
|
|
place(x, lowpass(ch, 5000) * 0.05, at + j * 0.11)
|
|
x = reverb(x, 0.2, dur=1.5, decay=0.5, bright=3000)[:n]
|
|
write_loop("ambience_loop", x, -14, xfade=1.0)
|
|
|
|
|
|
# ----------------------------------------------------------------------------- music
|
|
|
|
NOTE = {"C": 0, "C#": 1, "Db": 1, "D": 2, "D#": 3, "Eb": 3, "E": 4, "F": 5, "F#": 6, "Gb": 6,
|
|
"G": 7, "G#": 8, "Ab": 8, "A": 9, "A#": 10, "Bb": 10, "B": 11}
|
|
|
|
|
|
def mf(midi):
|
|
return 440.0 * 2 ** ((midi - 69) / 12)
|
|
|
|
|
|
def chord_notes(root, quality, octave=4):
|
|
r = 12 * (octave + 1) + NOTE[root]
|
|
iv = {"maj7": [0, 4, 7, 11], "m7": [0, 3, 7, 10], "7": [0, 4, 7, 10], "m9": [0, 3, 7, 10, 14],
|
|
"maj9": [0, 4, 7, 11, 14], "6": [0, 4, 7, 9], "m6": [0, 3, 7, 9], "9": [0, 4, 7, 10, 14],
|
|
"sus": [0, 5, 7, 10]}[quality]
|
|
return r, [r + i for i in iv]
|
|
|
|
|
|
def ep_note(f, dur, vel=1.0, bright=1.0):
|
|
"""FM electric piano: 1:1 FM body + high tine, soft decay."""
|
|
rel = 0.25
|
|
t = t_axis(dur + rel)
|
|
idx = (1.6 * bright) * np.exp(-t / 0.35) + 0.15
|
|
body = np.sin(2 * np.pi * f * t + idx * np.sin(2 * np.pi * f * t))
|
|
tine = np.sin(2 * np.pi * f * 14.0 * t) * np.exp(-t / 0.03) * 0.25 * bright
|
|
bark = np.sin(2 * np.pi * f * 2.0 * t + 0.8 * np.sin(2 * np.pi * f * t)) * np.exp(-t / 0.12) * 0.2
|
|
env = np.exp(-t / 1.6) * np.minimum(1, t / 0.004)
|
|
off = int(dur * SR)
|
|
env[off:] *= np.linspace(1, 0, len(env) - off) ** 2
|
|
return (body + tine + bark) * env * vel
|
|
|
|
|
|
def bass_note(f, dur, pluck=0.5):
|
|
t = t_axis(dur + 0.06)
|
|
x = np.sin(2 * np.pi * f * t) + 0.25 * np.sin(4 * np.pi * f * t) * np.exp(-t / 0.2)
|
|
x += pluck * lowpass(signal.sawtooth(2 * np.pi * f * t), 900) * np.exp(-t / 0.08)
|
|
env = np.minimum(1, t / 0.006) * np.exp(-t / 0.9)
|
|
off = int(dur * SR)
|
|
env[off:] *= np.linspace(1, 0, len(env) - off)
|
|
return x * env
|
|
|
|
|
|
def kick(soft=1.0):
|
|
t = t_axis(0.4)
|
|
f = 48 + 90 * np.exp(-t / 0.03)
|
|
x = np.sin(2 * np.pi * np.cumsum(f) / SR) * np.exp(-t / (0.16 * soft))
|
|
x[:int(0.002 * SR)] += rng(1).standard_normal(int(0.002 * SR)) * 0.2
|
|
return x
|
|
|
|
|
|
def snare(seed, soft=1.0):
|
|
t = t_axis(0.3)
|
|
r = rng(seed)
|
|
nz = bandpass(r.standard_normal(len(t)), 1200, 7000) * np.exp(-t / (0.09 * soft))
|
|
tone = np.sin(2 * np.pi * 190 * t) * np.exp(-t / 0.05)
|
|
return nz * 0.8 + tone * 0.4
|
|
|
|
|
|
def rim(seed):
|
|
t = t_axis(0.3)
|
|
r = rng(seed)
|
|
return (peak(r.standard_normal(len(t)), 1800, 6) * 0.4 + np.sin(2 * np.pi * 820 * t) * 0.5) * np.exp(-t / 0.02)
|
|
|
|
|
|
def hat(seed, open_=False):
|
|
t = t_axis(0.25 if open_ else 0.06)
|
|
r = rng(seed)
|
|
x = highpass(r.standard_normal(len(t)), 7000) * np.exp(-t / (0.08 if open_ else 0.014))
|
|
return x
|
|
|
|
|
|
def shaker(seed):
|
|
t = t_axis(0.09)
|
|
r = rng(seed)
|
|
return bandpass(r.standard_normal(len(t)), 4000, 12000) * np.sin(np.pi * t / 0.09) ** 2
|
|
|
|
|
|
def pan(x, p):
|
|
"""Equal power pan, p in [-1, 1]."""
|
|
a = (p + 1) * np.pi / 4
|
|
return np.stack([x * np.cos(a), x * np.sin(a)], axis=1)
|
|
|
|
|
|
def place_st(buf, x, at):
|
|
i = max(0, int(at * SR))
|
|
if i >= len(buf):
|
|
return
|
|
n = min(len(x), len(buf) - i)
|
|
buf[i:i + n] += x[:n]
|
|
|
|
|
|
def render_song(bpm, prog, bars, swing, style, seed):
|
|
r = rng(seed)
|
|
beat = 60.0 / bpm
|
|
bar = beat * 4
|
|
loop_len = bars * bar
|
|
total = loop_len + 4.0
|
|
n = int(total * SR)
|
|
ep = np.zeros((n, 2))
|
|
bass = np.zeros(n)
|
|
drums = np.zeros((n, 2))
|
|
lead = np.zeros((n, 2))
|
|
|
|
def sw(pos16):
|
|
"""time of a 16th-note position with swing applied to odd 16ths"""
|
|
base = pos16 * beat / 4
|
|
if pos16 % 2 == 1:
|
|
base += swing * beat / 4
|
|
return base
|
|
|
|
# --- chords (EP comping)
|
|
for b in range(bars):
|
|
root, qual = prog[b % len(prog)]
|
|
rmidi, notes = chord_notes(root, qual, 4)
|
|
# voice-lead into a comfortable register around E4..E5
|
|
voiced = []
|
|
for m in notes[1:] if len(notes) > 4 else notes:
|
|
while m < 57:
|
|
m += 12
|
|
while m > 74:
|
|
m -= 12
|
|
voiced.append(m)
|
|
voiced = sorted(voiced)
|
|
t0 = b * bar
|
|
if style == "menu":
|
|
hits = [(0, 2.6, 0.75), (2.5, 1.4, 0.45)] if b % 2 == 0 else [(0, 1.6, 0.7), (1.75, 2.2, 0.5)]
|
|
else:
|
|
hits = [(0, 0.9, 0.75), (1.5, 0.4, 0.5), (2.5, 0.7, 0.6), (3.5, 0.4, 0.45)]
|
|
for st, ln, vel in hits:
|
|
for k, m in enumerate(voiced):
|
|
strum = k * 0.012 if style == "menu" else k * 0.006
|
|
at = t0 + st * beat + strum + (r.random() - 0.5) * 0.008
|
|
x = ep_note(mf(m), ln * beat, vel * (0.9 + 0.1 * r.random()) * 0.22, bright=0.8 if style == "menu" else 1.0)
|
|
place_st(ep, pan(x, -0.35 + 0.7 * k / max(1, len(voiced) - 1)), at)
|
|
# --- bass
|
|
rf = mf(rmidi - 12) if rmidi - 12 >= 36 else mf(rmidi)
|
|
fifth = rf * 1.5
|
|
octv = rf * 2
|
|
if style == "menu":
|
|
pattern = [(0, rf, 1.5), (2.0, rf, 0.9), (3.0, fifth, 0.8)]
|
|
else:
|
|
pattern = [(0, rf, 0.7), (0.75, rf, 0.25), (1.5, octv, 0.35), (2.0, rf, 0.5), (2.75, fifth, 0.25),
|
|
(3.25, rf, 0.3), (3.5, octv * 0.75 if b % 2 else fifth, 0.4)]
|
|
for st, f, ln in pattern:
|
|
at = t0 + sw(int(round(st * 4))) + (st * 4 - round(st * 4)) * beat / 4
|
|
place(bass, bass_note(f, ln * beat, 0.3 if style == "menu" else 0.6) * 0.5, at)
|
|
# --- drums
|
|
for s16 in range(16):
|
|
at = t0 + sw(s16)
|
|
beat_i = s16 / 4
|
|
if style == "menu":
|
|
if s16 in (0, 10) or (b % 2 == 1 and s16 == 7):
|
|
place_st(drums, pan(kick(0.9) * 0.9, 0), at)
|
|
if s16 in (4, 12):
|
|
place_st(drums, pan(rim(2000 + s16) * 0.45 + snare(2100 + b, 0.7) * 0.2, 0.1), at)
|
|
if s16 % 2 == 0:
|
|
place_st(drums, pan(hat(2200 + s16 + b) * (0.16 if s16 % 4 else 0.22), 0.3), at)
|
|
if s16 % 2 == 1:
|
|
place_st(drums, pan(shaker(2300 + s16) * 0.07, -0.3), at)
|
|
else:
|
|
if s16 in (0, 6, 10) or (b % 4 == 3 and s16 == 14):
|
|
place_st(drums, pan(kick() * 1.0, 0), at)
|
|
if s16 in (4, 12):
|
|
place_st(drums, pan(snare(2400 + b * 3 + s16) * 0.55, 0.05), at)
|
|
if s16 in (7, 15) and r.random() < 0.35:
|
|
place_st(drums, pan(snare(2500 + b + s16, 0.6) * 0.12, 0.05), at) # ghost note
|
|
vel = 0.2 if s16 % 2 == 0 else 0.11
|
|
if s16 % 4 == 2:
|
|
vel = 0.26
|
|
place_st(drums, pan(hat(2600 + s16 + 16 * b, open_=(s16 == 14 and b % 2 == 1)) * vel, 0.35), at)
|
|
if s16 % 4 == 2:
|
|
place_st(drums, pan(shaker(2700 + s16) * 0.06, -0.4), at)
|
|
|
|
# --- lead motif (pentatonic marimba / bell), call & response in second half
|
|
scale = [0, 2, 4, 7, 9]
|
|
key_root = 72 + NOTE[prog[0][0]] - 12 if style == "menu" else 72 + NOTE["C"]
|
|
motif = []
|
|
for k in range(8):
|
|
deg = r.integers(0, 5)
|
|
octv = r.integers(0, 2)
|
|
dur16 = [2, 2, 3, 1, 4, 2][r.integers(0, 6)]
|
|
motif.append((key_root + scale[deg] + 12 * octv - 12, dur16))
|
|
for b in range(bars // 2, bars):
|
|
if b % 4 not in (0, 1):
|
|
continue
|
|
pos = 0
|
|
for m, d16 in motif[(b % 2) * 4:(b % 2) * 4 + 4]:
|
|
at = b * bar + sw(pos) + (2 * beat if b % 2 == 0 else 0)
|
|
if pos < 16:
|
|
if style == "menu":
|
|
x = bell(mf(m), 1.2, int(m) + b, 0.6) * 0.07
|
|
else:
|
|
x = marimba(mf(m), 0.6) * 0.16
|
|
place_st(lead, pan(x, 0.25), at)
|
|
pos += d16
|
|
|
|
# --- mixing
|
|
ep_wet = stereo_reverb(ep, 0.35, dur=2.4, decay=0.7, bright=5000, seed=3000)[:n]
|
|
# gentle chorus on EP
|
|
t = np.arange(n) / SR
|
|
d = (0.004 + 0.0015 * np.sin(2 * np.pi * 0.6 * t)) * SR
|
|
idx = np.clip(np.arange(n) - d, 0, n - 1).astype(int)
|
|
ep_ch = ep_wet.copy()
|
|
ep_ch[:, 1] = 0.7 * ep_wet[:, 1] + 0.3 * ep_wet[idx, 0]
|
|
lead_w = stereo_reverb(lead, 0.4, dur=2.0, decay=0.6, seed=3100)[:n]
|
|
bass_f = lowpass(bass, 1200 if style == "game" else 700)
|
|
# sidechain-ish ducking from kick positions: simple envelope follower on drums low band
|
|
dlow = lowpass(np.abs(drums[:, 0]), 15)
|
|
duck = 1 - 0.25 * np.clip(dlow / (dlow.max() + 1e-9) * 3, 0, 1)
|
|
mix = ep_ch * duck[:, None] * 1.0 + lead_w + np.stack([bass_f, bass_f], axis=1) * 0.9 + drums * 0.8
|
|
if style == "menu":
|
|
# lo-fi vinyl crackle + slight tape wobble lowpass
|
|
cr = (r.random(n) < 0.0009) * r.standard_normal(n)
|
|
cr = highpass(cr, 2000) * 0.25 + highpass(r.standard_normal(n), 3000) * 0.004
|
|
mix += np.stack([cr, cr * 0.8], axis=1)
|
|
mix = np.stack([lowpass(mix[:, 0], 8000), lowpass(mix[:, 1], 8000)], axis=1)
|
|
mix = soft_clip(mix * 1.2, 1.1)
|
|
# fold the tail beyond the loop point back into the head -> seamless loop
|
|
out = np.zeros((int(loop_len * SR), 2))
|
|
out[:, 0] = wrap_tail(mix[:, 0], loop_len)
|
|
out[:, 1] = wrap_tail(mix[:, 1], loop_len)
|
|
# loudness: target RMS ~ -18 dBFS, peak ceiling -3 dBFS
|
|
rms = np.sqrt(np.mean(out ** 2))
|
|
out *= db(-18) / rms
|
|
pk = np.max(np.abs(out))
|
|
if pk > db(-3):
|
|
out = out * (db(-3) / pk)
|
|
return out
|
|
|
|
|
|
def music():
|
|
menu_prog = [("F", "maj7"), ("E", "m7"), ("D", "m9"), ("C", "maj7"),
|
|
("Bb", "maj7"), ("A", "m7"), ("G", "m7"), ("C", "sus")]
|
|
game_prog = [("C", "maj9"), ("A", "m7"), ("D", "m9"), ("G", "7"),
|
|
("E", "m7"), ("A", "7"), ("D", "m7"), ("G", "9")]
|
|
m = render_song(84, menu_prog, 24, 0.33, "menu", 4000)
|
|
_write_raw("music_menu_loop", m)
|
|
g = render_song(104, game_prog, 32, 0.28, "game", 5000)
|
|
_write_raw("music_game_loop", g)
|
|
|
|
|
|
def _write_raw(name, x):
|
|
data = np.clip(x * 32767, -32768, 32767).astype(np.int16)
|
|
with wave.open(os.path.join(OUT, name + ".wav"), "wb") as w:
|
|
w.setnchannels(2)
|
|
w.setsampwidth(2)
|
|
w.setframerate(SR)
|
|
w.writeframes(data.tobytes())
|
|
rms = 20 * np.log10(np.sqrt(np.mean(x ** 2)))
|
|
print(f" {name}.wav {len(x) / SR:6.2f}s stereo rms {rms:.1f} dBFS peak {20 * np.log10(np.abs(x).max()):.1f}")
|
|
|
|
|
|
# ----------------------------------------------------------------------------- main
|
|
|
|
GROUPS = {
|
|
"pops": sfx_pops, "tape": sfx_tape_rip, "cardboard": sfx_cardboard, "box": sfx_box_burst,
|
|
"grab": sfx_plastic_grab, "whoosh": sfx_whoosh, "recycler": sfx_recycler,
|
|
"cash": sfx_cash_register, "coin": sfx_coin_tick, "ui": sfx_ui, "horn": sfx_horn,
|
|
"fanfare": sfx_fanfare, "stars": sfx_stars, "confetti": sfx_confetti, "steps": sfx_steps,
|
|
"unroll": sfx_unroll, "mallet": sfx_mallet, "engine": loop_engine, "conveyor": loop_conveyor,
|
|
"ambience": loop_ambience, "music": music,
|
|
}
|
|
|
|
if __name__ == "__main__":
|
|
os.makedirs(OUT, exist_ok=True)
|
|
names = sys.argv[1:] or list(GROUPS)
|
|
for g in names:
|
|
print(f"[{g}]")
|
|
GROUPS[g]()
|