67 lines
2.9 KiB
Python
67 lines
2.9 KiB
Python
|
|
"""Neue Musik (z. B. aus Gemini) fürs Spiel aufbereiten: Stille an den Rändern weg, Lautheit
|
|||
|
|
angleichen, Schleifen-Stücke nahtlos machen (Ende blendet in den Anfang über).
|
|||
|
|
|
|||
|
|
py -3.12 tools/prep_music.py [ordner] Standard: Assets/Audio/music
|
|||
|
|
|
|||
|
|
Liest <ordner>/music_*.mp3, schreibt Assets/Audio/music_*.mp3, verschiebt die Vorlagen nach
|
|||
|
|
_Archiv/Musik-Vorlagen/ (nicht ausgeliefert).
|
|||
|
|
"""
|
|||
|
|
import os, shutil, subprocess, sys, tempfile
|
|||
|
|
import numpy as np
|
|||
|
|
import soundfile as sf
|
|||
|
|
|
|||
|
|
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
|
|||
|
|
SRC = sys.argv[1] if len(sys.argv) > 1 else os.path.join(ROOT, "Assets", "Audio", "music")
|
|||
|
|
DST = os.path.join(ROOT, "Assets", "Audio")
|
|||
|
|
ARCHIVE = os.path.join(ROOT, "_Archiv", "Musik-Vorlagen")
|
|||
|
|
LOOPS = {"music_menu", "music_ludus", "music_arena", "music_champion"}
|
|||
|
|
# Ziel-Lautheit (LUFS): ruhige Stücke leiser, Kampf etwas lauter – passend zu Stimmen und Effekten
|
|||
|
|
TARGET = {"music_ludus": -19, "music_menu": -18, "music_oath": -20, "music_arena": -17, "music_champion": -16.5}
|
|||
|
|
XFADE = 3.0
|
|||
|
|
|
|||
|
|
|
|||
|
|
def decode(p):
|
|||
|
|
with tempfile.TemporaryDirectory() as t:
|
|||
|
|
w = os.path.join(t, "a.wav")
|
|||
|
|
subprocess.run(["ffmpeg", "-y", "-loglevel", "error", "-i", p, "-ar", "44100", "-ac", "2", w], check=True)
|
|||
|
|
return sf.read(w, dtype="float32")
|
|||
|
|
|
|||
|
|
|
|||
|
|
def trim(x, sr, thr=10 ** (-45 / 20)):
|
|||
|
|
loud = np.where(np.abs(x).max(axis=1) > thr)[0]
|
|||
|
|
return x[max(0, loud[0] - int(0.02 * sr)):loud[-1] + 1] if len(loud) else x
|
|||
|
|
|
|||
|
|
|
|||
|
|
def loop(x, sr):
|
|||
|
|
"""Ende weich in den Anfang blenden: das Stück endet genau dort, wo es wieder beginnt."""
|
|||
|
|
n = int(XFADE * sr)
|
|||
|
|
head, tail = x[:n], x[-n:]
|
|||
|
|
k = np.linspace(0, 1, n, dtype=np.float32)[:, None]
|
|||
|
|
fade = np.sqrt(1 - k) * tail + np.sqrt(k) * head # gleiche Leistung
|
|||
|
|
return np.concatenate([fade, x[n:-n]])
|
|||
|
|
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
os.makedirs(ARCHIVE, exist_ok=True)
|
|||
|
|
for f in sorted(os.listdir(SRC)):
|
|||
|
|
if not (f.startswith("music_") and f.endswith(".mp3")): continue
|
|||
|
|
name = f[:-4]
|
|||
|
|
x, sr = decode(os.path.join(SRC, f))
|
|||
|
|
x = trim(x, sr)
|
|||
|
|
if name in LOOPS: x = loop(x, sr)
|
|||
|
|
else: # einmalige Stücke: kurz ein- und ausblenden
|
|||
|
|
n = int(0.05 * sr); x[:n] *= np.linspace(0, 1, n)[:, None]
|
|||
|
|
m = int(1.5 * sr); x[-m:] *= np.linspace(1, 0, m)[:, None]
|
|||
|
|
with tempfile.TemporaryDirectory() as t:
|
|||
|
|
w = os.path.join(t, "b.wav"); sf.write(w, x, sr)
|
|||
|
|
tgt = TARGET.get(name, -17.5)
|
|||
|
|
subprocess.run(["ffmpeg", "-y", "-loglevel", "error", "-i", w, "-af", f"loudnorm=I={tgt}:TP=-1.5:LRA=11",
|
|||
|
|
"-ar", "44100", "-b:a", "192k", os.path.join(DST, f)], check=True)
|
|||
|
|
shutil.move(os.path.join(SRC, f), os.path.join(ARCHIVE, f))
|
|||
|
|
print(f"{name}: {len(x) / sr:.1f} s, {'Schleife' if name in LOOPS else 'einmal'}, {TARGET.get(name, -17.5)} LUFS")
|
|||
|
|
if not os.listdir(SRC): os.rmdir(SRC)
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|