Files
grass-cutters/Tools/director/finish_audio.py
NiemandKG d1c3761e58 Grass Cutters Together: the project as of moon-sweepers f3666f0
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>
2026-11-06 18:44:40 +06:00

88 lines
3.7 KiB
Python

"""Raw ElevenLabs takes -> game clips in Assets/Audio: the director's lines (a touch of phone colour, trimmed, levelled)
and the intro/call/critter sounds (trimmed, levelled, faded). Run after eleven.py; the raw folder is git-ignored."""
import os, wave
import numpy as np
from scipy import signal
HERE = os.path.dirname(os.path.abspath(__file__))
RAW = os.path.join(HERE, "..", "audio", "eleven_raw")
OUT = os.path.join(HERE, "..", "..", "Assets", "Audio")
def load(name):
w = wave.open(os.path.join(RAW, name))
ch, r = w.getnchannels(), w.getframerate()
a = np.frombuffer(w.readframes(w.getnframes()), np.int16).astype(np.float64) / 32768
if ch == 2: a = a.reshape(-1, 2).mean(1)
return a, r
def save(name, a, r):
a = np.clip(a, -1, 1)
with wave.open(os.path.join(OUT, name + ".wav"), "wb") as w:
w.setnchannels(1); w.setsampwidth(2); w.setframerate(r)
w.writeframes((a * 32767).astype(np.int16).tobytes())
print(name, f"{len(a) / r:.2f}s")
def trim(a, r, floor=0.006, pad=0.06):
win = max(1, int(r * 0.02))
e = np.sqrt(np.convolve(a * a, np.ones(win) / win, "same"))
idx = np.where(e > floor)[0]
if not len(idx): return a
s, t = max(0, idx[0] - int(pad * r)), min(len(a), idx[-1] + int(pad * r * 3))
return a[s:t]
def fade(a, r, fin=0.005, fout=0.08):
n1, n2 = int(fin * r), int(fout * r)
a = a.copy()
if n1: a[:n1] *= np.linspace(0, 1, n1)
if n2: a[-n2:] *= np.linspace(1, 0, n2)
return a
def level(a, peak=0.92):
return a * (peak / max(1e-6, np.abs(a).max()))
VOICE_DB = -15.0
def speech(a, r):
"""Every line, the Director's and the assistant's, at one loudness: the RMS of its voiced 50 ms frames brought down
to VOICE_DB (never up, so nothing clips). The game plays them as they are, under the Voices slider."""
hop = r // 20
k = len(a) // hop
if k == 0: return a
e = np.sqrt((a[:k * hop].reshape(k, hop) ** 2).mean(1))
e = e[e > 10 ** (-50 / 20)]
if not len(e): return a
db = 20 * np.log10(np.sqrt((e ** 2).mean()))
return a * min(1.0, 10 ** ((VOICE_DB - db) / 20))
VOICE = {"dir_intro": "dir_intro_1.wav"}
for f in sorted(os.listdir(RAW)):
if not f.startswith("dir_") or not f.endswith("_0.wav"): continue
name = f[:-6]
if name == "dir_intro": continue
VOICE.setdefault(name, f)
for name, f in VOICE.items():
a, r = load(f)
# the call comes through a phone: roll off the boom and the air, not a tin-can band
b, c = signal.butter(2, [160 / (r / 2), 7000 / (r / 2)], "band")
a = signal.filtfilt(b, c, a)
# gentle squash so the shouting doesn't clip and the mutters still carry
a = np.tanh(a * 1.6) / np.tanh(1.6)
save(name, speech(fade(level(trim(a, r)), r, 0.01, 0.12), r), r)
# the assistant, in your ear over the headset: a little narrower than the Director's phone, and kept even
for f in sorted(os.listdir(RAW)):
if not f.startswith("asst_") or not f.endswith("_0.wav"): continue
a, r = load(f)
b, c = signal.butter(2, [220 / (r / 2), 6500 / (r / 2)], "band")
a = signal.filtfilt(b, c, a)
a = np.tanh(a * 2.0) / np.tanh(2.0)
save(f[:-6], speech(fade(level(trim(a, r), 0.85), r, 0.01, 0.1), r), r)
SFX = {"phone_ring": ("phone_ring_0.wav", 0.8), "call_connect": ("call_connect_0.wav", 0.7), "call_hangup": ("call_hangup_0.wav", 0.7),
"intro_echo": ("intro_echo_0.wav", 0.85), "cut_whoosh_1": ("cut_whoosh_0.wav", 0.9), "cut_whoosh_2": ("cut_whoosh_1.wav", 0.9),
"cut_return": ("cut_return_0.wav", 0.95), "space_rumble": ("space_rumble_0.wav", 0.85),
"bug_scatter_1": ("bug_scatter_0.wav", 0.6), "bug_scatter_2": ("bug_scatter_1.wav", 0.6)}
for name, (f, pk) in SFX.items():
a, r = load(f)
save(name, fade(level(trim(a, r, 0.003), pk), r, 0.004, 0.25), r)