"""Per-sentence measurement of generated speech (no listening needed). Usage (SesVer's own venv has faster-whisper, numpy, soundfile; run it with a clean environment because the agent's PYTHONPATH points at another Python's numpy): cd /sesver env -i HOME=$HOME PATH=/usr/bin:/bin HF_HUB_OFFLINE=1 .venv/bin/python \ /scripts/measure_speech.py a.wav b.wav ... Prints, per file: duration, peak dBFS, clipped samples; per Whisper segment: start-end, RMS dB, median pitch (Hz, autocorrelation, voiced 40 ms frames), text and per-word probability; then silent gaps >= 0.15 s (start, length). Compare the marked sentence against the same sentence in a neutral render and against the clip the user approved. Pitch ratio vs neutral is the main signal for shout/excited; a garbled first word shows up in the transcript. """ import sys import numpy as np import soundfile as sf from faster_whisper import WhisperModel def db(x): r = np.sqrt(np.mean(x ** 2)) if len(x) else 0 return 20 * np.log10(r + 1e-9) def pitch(x, sr): w = int(sr * 0.04) out = [] for i in range(0, len(x) - w, w): s = x[i:i + w] if db(s) < -35: continue s = s - s.mean() c = np.correlate(s, s, "full")[w - 1:] lo, hi = int(sr / 400), int(sr / 70) k = lo + np.argmax(c[lo:hi]) if c[k] > 0.3 * c[0]: out.append(sr / k) return float(np.median(out)) if out else None def main(paths, lang="tr"): model = WhisperModel("small", device="cpu", compute_type="int8") for path in paths: x, sr = sf.read(path) if x.ndim > 1: x = x.mean(1) peak = 20 * np.log10(np.max(np.abs(x)) + 1e-9) print(f"\n=== {path} {len(x) / sr:.2f} s, peak {peak:.1f} dBFS, clipped {int(np.sum(np.abs(x) >= 0.999))}") segs, _ = model.transcribe(path, language=lang, word_timestamps=True) for s in segs: a, b = int(s.start * sr), int(s.end * sr) p = pitch(x[a:b], sr) print(f"[{s.start:5.2f}-{s.end:5.2f}] {db(x[a:b]):6.1f} dB pitch {p and round(p)} Hz | {s.text.strip()}") print(" words:", " ".join(f"{w.word.strip()}({w.probability:.2f})" for w in s.words)) fr = int(sr * 0.02) energy = [db(x[i:i + fr]) for i in range(0, len(x) - fr, fr)] gaps, run = [], 0 for i, e in enumerate(energy): if e < -45: run += 1 else: if run * 0.02 >= 0.15: gaps.append((round((i - run) * 0.02, 2), round(run * 0.02, 2))) run = 0 print(" silent gaps (start s, length s):", gaps) if __name__ == "__main__": main(sys.argv[1:])