three takes for weak lines, keep the best
Browse files238 unmodified lines239240241242243244245246247248249250251252253254255256238 unmodified lines# right duration filled with near-silence (peak 0.13 against 0.68# for every other line). One retake with a different seed is cheap;# keep whichever take is longer and louder.ref_peak = float(np.median(peaks)) if len(peaks) >= 2 else 0.0weak = float(np.abs(y).max()) < (0.45 * ref_peak if ref_peak else 0.20)short = y.size / SR < 0.09 * len(line["text"])if short or weak:why = "short" if short else "near-silent"log(f"{tag} line {line['index']} {why} ({y.size/SR:.2f}s peak {np.abs(y).max():.2f}), retaking")try:y2 = gen(1)if (y2.size, np.abs(y2).max()) > (y.size, np.abs(y).max()):y = y2except Exception:log(f"{tag} line {line['index']} retake failed (keeping the first take)")peaks.append(float(np.abs(y).max()))wall = time.time() - t0p = OUT / "lines" / f"{tag}_{line['index']:02d}.wav"238 unmodified lines239240241242243244245246247248249250251252253254255256257258259260261262263264265266267238 unmodified lines# right duration filled with near-silence (peak 0.13 against 0.68# for every other line). One retake with a different seed is cheap;# keep whichever take is longer and louder.def usable(a: np.ndarray) -> bool:ref_peak = float(np.median(peaks)) if len(peaks) >= 2 else 0.0floor = 0.45 * ref_peak if ref_peak else 0.20return (a.size / SR >= 0.09 * len(line["text"])and float(np.abs(a).max()) >= max(floor, 0.20))if not usable(y):log(f"{tag} line {line['index']} weak ({y.size/SR:.2f}s peak {np.abs(y).max():.2f})")# Whisper takes are stochastic: two of six in one run came out# as 0.4 s or as near-silence at the right length. Three tries,# keep the best by (valid, peak, duration).for attempt in (1, 2, 3):try:cand = gen(attempt)except Exception:continueif usable(cand):y = candlog(f"{tag} line {line['index']} take {attempt} accepted "f"({y.size/SR:.2f}s peak {np.abs(y).max():.2f})")breakif (cand.size, float(np.abs(cand).max())) > (y.size, float(np.abs(y).max())):y = candpeaks.append(float(np.abs(y).max()))wall = time.time() - t0p = OUT / "lines" / f"{tag}_{line['index']:02d}.wav"
kaggle/radio_demo.py+23-12