89 lines
3.2 KiB
Python
89 lines
3.2 KiB
Python
|
|
"""Take generated audio into the game: ../Assets/Audio/<name>.mp3.
|
||
|
|
|
||
|
|
Per clip: newest roll from output/audio, trailing silence cut (Stable Audio
|
||
|
|
pads every short clip to its full length), soft fades on music so a loop seam
|
||
|
|
is a dip rather than a click, then one gain to a peak that depends on the kind
|
||
|
|
of sound - a button click must sit well under an event chime, and both under
|
||
|
|
the music never.
|
||
|
|
|
||
|
|
py -3.12 adopt_audio.py sfx_click one clip
|
||
|
|
py -3.12 adopt_audio.py --all every clip in the list
|
||
|
|
"""
|
||
|
|
import glob
|
||
|
|
import importlib
|
||
|
|
import os
|
||
|
|
import re
|
||
|
|
import subprocess
|
||
|
|
import sys
|
||
|
|
|
||
|
|
import _bootstrap # Projektordner (config.py, Listen) vor allen anderen Modulen
|
||
|
|
import config
|
||
|
|
|
||
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
||
|
|
SRC = os.path.join(config.OUTPUT_DIR, "audio")
|
||
|
|
DEST = os.path.join(config.ASSETS_DIR, "Audio")
|
||
|
|
|
||
|
|
QUIET = {"sfx_click": -14.0, "sfx_tick": -16.0, "sfx_pop": -10.0,
|
||
|
|
"sfx_tip": -10.0, "sfx_choice": -9.0}
|
||
|
|
MUSIC_PEAK = -4.0
|
||
|
|
SFX_PEAK = -4.0
|
||
|
|
|
||
|
|
|
||
|
|
def peak_of(path, filters=""):
|
||
|
|
args = ["ffmpeg", "-hide_banner", "-i", path]
|
||
|
|
af = (filters + "," if filters else "") + "volumedetect"
|
||
|
|
run = subprocess.run(args + ["-af", af, "-f", "null", "-"], capture_output=True, text=True)
|
||
|
|
hit = re.search(r"max_volume:\s*(-?[\d.]+) dB", run.stderr)
|
||
|
|
return float(hit.group(1)) if hit else None
|
||
|
|
|
||
|
|
|
||
|
|
def duration_of(path):
|
||
|
|
run = subprocess.run(["ffprobe", "-v", "error", "-show_entries", "format=duration",
|
||
|
|
"-of", "default=nw=1:nk=1", path], capture_output=True, text=True)
|
||
|
|
return float(run.stdout.strip() or 0)
|
||
|
|
|
||
|
|
|
||
|
|
def adopt(name):
|
||
|
|
rolls = sorted(glob.glob(os.path.join(SRC, f"{name}_*.mp3")), key=os.path.getmtime)
|
||
|
|
rolls = [r for r in rolls if not r.endswith("_levelled.mp3")]
|
||
|
|
if not rolls:
|
||
|
|
print(f" {name}: no roll in output/audio, skipped")
|
||
|
|
return False
|
||
|
|
src = rolls[-1]
|
||
|
|
|
||
|
|
if name.startswith("music_"):
|
||
|
|
length = duration_of(src)
|
||
|
|
shape = f"afade=t=in:d=1.5,afade=t=out:st={max(0.0, length - 2.5):.2f}:d=2.5"
|
||
|
|
target = MUSIC_PEAK
|
||
|
|
else:
|
||
|
|
# cut the silent tail: reverse, drop leading silence, reverse back
|
||
|
|
shape = ("areverse,silenceremove=start_periods=1:start_threshold=-50dB:start_silence=0.05,"
|
||
|
|
"areverse")
|
||
|
|
target = QUIET.get(name, SFX_PEAK)
|
||
|
|
|
||
|
|
top = peak_of(src, shape)
|
||
|
|
if top is None:
|
||
|
|
print(f" {name}: could not be measured, skipped")
|
||
|
|
return False
|
||
|
|
gain = target - top
|
||
|
|
|
||
|
|
os.makedirs(DEST, exist_ok=True)
|
||
|
|
dest = os.path.join(DEST, f"{name}.mp3")
|
||
|
|
subprocess.run(["ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-i", src,
|
||
|
|
"-af", f"{shape},volume={gain:.2f}dB", "-ar", "44100",
|
||
|
|
"-codec:a", "libmp3lame", "-q:a", "3", dest], check=True)
|
||
|
|
print(f" {name:16s} {os.path.basename(src)} peak {top:6.1f} -> {target:5.1f} dB"
|
||
|
|
f" {duration_of(dest):5.1f}s")
|
||
|
|
return True
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
args = sys.argv[1:]
|
||
|
|
if not args:
|
||
|
|
print(__doc__)
|
||
|
|
sys.exit(1)
|
||
|
|
if args == ["--all"]:
|
||
|
|
args = [c[0] for c in importlib.import_module(config.AUDIO_MODULE).CLIPS]
|
||
|
|
ok = sum(adopt(n) for n in args)
|
||
|
|
print(f"{ok}/{len(args)} adopted")
|