Files
grass-cutters/Tools/audio/inspect_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

58 lines
1.7 KiB
Python

"""Render waveform + spectrogram PNGs for WAV files and print onset/peak/length stats.
Usage: python inspect_audio.py out.png file1.wav [file2.wav ...] [--max 4.0]
"""
import sys
import wave
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
def load(path):
with wave.open(path) as w:
ch, sr, n = w.getnchannels(), w.getframerate(), w.getnframes()
x = np.frombuffer(w.readframes(n), dtype=np.int16).astype(np.float64) / 32768
if ch == 2:
x = x.reshape(-1, 2).mean(axis=1)
return x, sr
def stats(x, sr):
a = np.abs(x)
pk = a.max() + 1e-12
onset = np.argmax(a > pk * 0.1) / sr
pk_t = np.argmax(a) / sr
rms = np.sqrt(np.mean(x ** 2)) + 1e-12
return f"len {len(x) / sr:.3f}s onset {onset * 1000:.1f}ms peak@{pk_t * 1000:.0f}ms peak {20 * np.log10(pk):.1f}dB rms {20 * np.log10(rms):.1f}dB"
def main():
args = sys.argv[1:]
mx = None
if "--max" in args:
i = args.index("--max")
mx = float(args[i + 1])
del args[i:i + 2]
out, files = args[0], args[1:]
fig, axes = plt.subplots(len(files), 2, figsize=(14, 2.2 * len(files)), squeeze=False)
for row, f in enumerate(files):
x, sr = load(f)
print(f.split("/")[-1].split("\\")[-1], stats(x, sr))
if mx:
x = x[: int(mx * sr)]
t = np.arange(len(x)) / sr
axes[row, 0].plot(t, x, lw=0.4)
axes[row, 0].set_ylim(-1, 1)
axes[row, 0].set_title(f.split("/")[-1].split("\\")[-1], fontsize=8)
axes[row, 1].specgram(x + 1e-9, Fs=sr, NFFT=512, noverlap=384, cmap="magma")
axes[row, 1].set_ylim(0, 16000)
plt.tight_layout()
plt.savefig(out, dpi=70)
if __name__ == "__main__":
main()