#!/usr/bin/env python3 # Published copy, nothing is changed except this line. The sha256 of the original is in sha256.txt. """Cut the 1 h test file into pieces <= LIMIT s, only between utterances (same rule as build_audio.py, shorter limit). usage: make_chunks_short.py audio/ 120 chunks120 -> audio/chunks120/chunk-NN.wav + chunks120.json""" import json, sys, wave from pathlib import Path a = Path(sys.argv[1]); limit = float(sys.argv[2]); name = sys.argv[3] m = json.loads((a / "manifest.json").read_text()) src = wave.open(str(a / "librispeech-test-clean-1h.wav")) sr = src.getframerate(); assert sr == 16000 and src.getnchannels() == 1 and src.getsampwidth() == 2 out = a / name; out.mkdir(exist_ok=True) chunks, cur = [], [] for u in m["utterances"]: if cur and (u["end_s"] - cur[0]["start_s"]) > limit: chunks.append(cur); cur = [] cur.append(u) if cur: chunks.append(cur) info = [] for i, ch in enumerate(chunks): s0, s1 = ch[0]["start_s"], ch[-1]["end_s"] src.setpos(int(round(s0 * sr))) frames = src.readframes(int(round((s1 - s0) * sr))) p = out / f"chunk-{i:02d}.wav" with wave.open(str(p), "wb") as w: w.setnchannels(1); w.setsampwidth(2); w.setframerate(sr); w.writeframes(frames) info.append({"file": p.name, "start_s": s0, "end_s": s1, "dur_s": round(s1 - s0, 3), "n_utts": len(ch)}) (a / f"{name}.json").write_text(json.dumps(info, indent=1)) print(len(chunks), "pieces, max", max(c["dur_s"] for c in info), "s, total", round(sum(c["dur_s"] for c in info), 2), "s")