File size: 3,375 Bytes
434f7a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
"""Build 100 overlapping-speech clips: each = 3 utterances from 3 distinct European
languages (3 distinct speakers), temporally overlapped. Saves clip_NNN.mp3 +
clip_NNN.json (ground-truth timestamps + identities) into clips/, plus a 24k wav
under asr_wav/ for the ASR benchmark."""
import os, json, random, subprocess, collections
import numpy as np, soundfile as sf

random.seed(2024)
BASE = "/mnt/nvme/uvp/overlap_testset"
CLIPS = os.path.join(BASE, "clips"); os.makedirs(CLIPS, exist_ok=True)
AWAV = os.path.join(BASE, "asr_wav"); os.makedirs(AWAV, exist_ok=True)
N_CLIPS = 100
SR = 16000

pool = json.load(open(os.path.join(BASE, "pool", "pool_meta.json")))
by_lang = collections.defaultdict(list)
for s in pool:
    by_lang[s["lang"]].append(s)
LANGS = sorted(by_lang)
for l in LANGS:
    random.shuffle(by_lang[l])
cursor = {l: 0 for l in LANGS}

def draw(lang):
    lst = by_lang[lang]
    s = lst[cursor[lang] % len(lst)]
    cursor[lang] += 1
    return s

def rms_norm(x, target=0.08):
    r = np.sqrt(np.mean(x**2)) + 1e-9
    return x * (target / r)

index = []
for ci in range(N_CLIPS):
    langs = random.sample(LANGS, 3)
    utts = []
    for li, lang in enumerate(langs):
        s = draw(lang)
        x = rms_norm(np.asarray(sf.read(s["wav"])[0], dtype=np.float32))
        utts.append({"x": x, "d": len(x)/SR, "lang": lang, "code": s["code"],
                     "src_speaker_id": s["speaker_id"], "transcript": s["transcript"]})
    # staggered overlapping start times
    d = [u["d"] for u in utts]
    s0 = 0.0
    s1 = random.uniform(1.5, max(2.0, d[0]*0.6))
    s2 = random.uniform(s1 + 1.5, s1 + max(2.0, d[1]*0.6))
    starts = [s0, s1, s2]
    clip_dur = max(starts[i] + d[i] for i in range(3))
    buf = np.zeros(int(np.ceil(clip_dur*SR)) + SR//10, dtype=np.float32)
    speakers = []
    for i, u in enumerate(utts):
        off = int(starts[i]*SR)
        buf[off:off+len(u["x"])] += u["x"]
        speakers.append({"id": f"S{i+1}", "language": u["lang"], "code": u["code"],
                         "src_speaker_id": u["src_speaker_id"],
                         "start_time": round(starts[i], 3),
                         "end_time": round(starts[i] + u["d"], 3),
                         "transcript": u["transcript"]})
    peak = np.max(np.abs(buf)) + 1e-9
    if peak > 0.89:
        buf = buf * (0.89/peak)
    cid = f"clip_{ci:03d}"
    # 24k wav for ASR
    awav = os.path.join(AWAV, f"{cid}.wav")
    sf.write(awav, buf, SR)
    # deliverable mp3 (128k mono) + ground-truth json
    mp3 = os.path.join(CLIPS, f"{cid}.mp3")
    subprocess.run(["ffmpeg","-y","-i",awav,"-ac","1","-ar","16000","-b:a","128k",mp3],
                   check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    gt = {"clip_id": cid, "duration": round(clip_dur, 3), "num_speakers": 3,
          "languages": [u["lang"] for u in utts], "speakers": speakers}
    json.dump(gt, open(os.path.join(CLIPS, f"{cid}.json"), "w"), ensure_ascii=False, indent=2)
    index.append({"clip_id": cid, "mp3": mp3, "wav": awav, "json": os.path.join(CLIPS, f"{cid}.json")})

json.dump(index, open(os.path.join(BASE, "index.json"), "w"), indent=2)
# language usage stats
from collections import Counter
c = Counter(l for it in index for l in json.load(open(it["json"]))["languages"])
print("clips:", len(index), "| language usage:", dict(c))