Files
GladiatorIdle/tools/make_levelup.py

58 lines
2.2 KiB
Python

"""Stufen-Ton (sfx_levelup) ohne Generator: gezupfte Leier (Karplus-Strong) spielt aufsteigend einen
Dur-Dreiklang, dazu ein leiser tiefer Gong und Raumhall. Warm und kurz statt greller Fanfare.
py -3.12 tools/make_levelup.py → Assets/Audio/sfx_levelup.mp3
"""
import os, subprocess, tempfile
import numpy as np
import soundfile as sf
SR = 44100
OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "Assets", "Audio", "sfx_levelup.mp3")
rng = np.random.default_rng(7)
def pluck(freq, dur, bright=0.5):
n = int(SR * dur); p = int(SR / freq)
buf = rng.uniform(-1, 1, p) * np.hanning(p)
out = np.zeros(n)
for i in range(n):
out[i] = buf[i % p]
buf[i % p] = 0.5 * (buf[i % p] + buf[(i + 1) % p]) * (0.996 + 0.003 * bright)
return out * np.exp(-np.arange(n) / (SR * dur * 0.45))
def gong(freq, dur):
t = np.arange(int(SR * dur)) / SR
parts = [(1, 1.0), (2.76, 0.4), (5.4, 0.18), (1.5, 0.25)]
s = sum(a * np.sin(2 * np.pi * freq * m * t + rng.uniform(0, 6)) for m, a in parts)
return s * np.exp(-t / 0.7) * (1 - np.exp(-t / 0.004))
def reverb(x, mix=0.25):
y = np.concatenate([x, np.zeros(int(0.9 * SR))]); wet = np.zeros_like(y)
for d, g in ((1557, 0.62), (1617, 0.6), (1491, 0.64), (1422, 0.66)):
buf = np.zeros_like(y)
for i in range(len(y)):
buf[i] = y[i] + (buf[i - d] * g if i >= d else 0)
wet += buf * 0.25
return y * (1 - mix) + wet * mix
notes = [293.66, 369.99, 440.0, 587.33] # D-Dur: D4 Fis4 A4 D5
x = np.zeros(int(SR * 1.6))
for k, f in enumerate(notes):
s = pluck(f, 1.2 - 0.15 * k, bright=0.3 + 0.2 * k) * (0.8 + 0.1 * k)
at = int(SR * 0.075 * k)
x[at:at + len(s)] += s[:len(x) - at]
g = gong(146.83, 1.6) * 0.35
x[:len(g)] += g[:len(x)]
x = reverb(x / np.abs(x).max())
x = x / np.abs(x).max() * 0.8
x[-int(0.3 * SR):] *= np.linspace(1, 0, int(0.3 * SR))
st = np.stack([x, np.concatenate([np.zeros(300), x[:-300]])], axis=1) # leicht breit
with tempfile.TemporaryDirectory() as t:
w = os.path.join(t, "l.wav"); sf.write(w, st.astype(np.float32), SR)
subprocess.run(["ffmpeg", "-y", "-loglevel", "error", "-i", w, "-af", "loudnorm=I=-18:TP=-2", "-ar", "44100", "-b:a", "192k", OUT], check=True)
print("ok", OUT)