three takes for weak lines, keep the best
Browse files- kaggle/radio_demo.py +23 -12
kaggle/radio_demo.py
CHANGED
|
@@ -239,18 +239,29 @@ def main() -> None:
|
|
| 239 |
# right duration filled with near-silence (peak 0.13 against 0.68
|
| 240 |
# for every other line). One retake with a different seed is cheap;
|
| 241 |
# keep whichever take is longer and louder.
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
peaks.append(float(np.abs(y).max()))
|
| 255 |
wall = time.time() - t0
|
| 256 |
p = OUT / "lines" / f"{tag}_{line['index']:02d}.wav"
|
|
|
|
| 239 |
# right duration filled with near-silence (peak 0.13 against 0.68
|
| 240 |
# for every other line). One retake with a different seed is cheap;
|
| 241 |
# keep whichever take is longer and louder.
|
| 242 |
+
def usable(a: np.ndarray) -> bool:
|
| 243 |
+
ref_peak = float(np.median(peaks)) if len(peaks) >= 2 else 0.0
|
| 244 |
+
floor = 0.45 * ref_peak if ref_peak else 0.20
|
| 245 |
+
return (a.size / SR >= 0.09 * len(line["text"])
|
| 246 |
+
and float(np.abs(a).max()) >= max(floor, 0.20))
|
| 247 |
+
|
| 248 |
+
if not usable(y):
|
| 249 |
+
log(f"{tag} line {line['index']} weak ({y.size/SR:.2f}s peak {np.abs(y).max():.2f})")
|
| 250 |
+
# Whisper takes are stochastic: two of six in one run came out
|
| 251 |
+
# as 0.4 s or as near-silence at the right length. Three tries,
|
| 252 |
+
# keep the best by (valid, peak, duration).
|
| 253 |
+
for attempt in (1, 2, 3):
|
| 254 |
+
try:
|
| 255 |
+
cand = gen(attempt)
|
| 256 |
+
except Exception:
|
| 257 |
+
continue
|
| 258 |
+
if usable(cand):
|
| 259 |
+
y = cand
|
| 260 |
+
log(f"{tag} line {line['index']} take {attempt} accepted "
|
| 261 |
+
f"({y.size/SR:.2f}s peak {np.abs(y).max():.2f})")
|
| 262 |
+
break
|
| 263 |
+
if (cand.size, float(np.abs(cand).max())) > (y.size, float(np.abs(y).max())):
|
| 264 |
+
y = cand
|
| 265 |
peaks.append(float(np.abs(y).max()))
|
| 266 |
wall = time.time() - t0
|
| 267 |
p = OUT / "lines" / f"{tag}_{line['index']:02d}.wav"
|