File size: 2,091 Bytes
9b6e0aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import argparse
import json
from pathlib import Path

import soundfile as sf


MHB_ROOT = Path(__file__).resolve().parent.parent


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("test_dir", type=Path)
    args = parser.parse_args()

    test_dir = args.test_dir.expanduser().resolve()
    assert test_dir.is_dir(), f"Not found: {test_dir}"

    count = 0
    with (MHB_ROOT / "meta_data.jsonl").open("r", encoding="utf-8") as f:
        for line in f:
            if not line.strip():
                continue

            record = json.loads(line)
            if record["task_name"] != "ShipsEar":
                continue

            name = record["meta_data"]["ori_file_path"]

            # Trawler 使用划分脚本生成的独立测试片段
            if name.startswith("28__"):
                name = Path(name).stem + "_test.wav"

            matches = list(test_dir.glob(f"*{name}"))
            assert len(matches) == 1, f"Expected one source for {name}, found {len(matches)}"

            length = record["input_ts"]["length"]
            sr = record["input_ts"]["fs"]
            index = record["meta_data"]["seg_index"]
            assert length == sr * 30

            # 按 30 秒切分
            with sf.SoundFile(matches[0]) as audio:
                assert audio.samplerate == sr and audio.channels == 1, f"{matches[0], audio.samplerate, audio.channels}"
                start = index * (sr * 15)
                assert start + length <= audio.frames, f"Short recording: {matches[0]}"
                audio.seek(start)
                segment = audio.read(length, dtype="float32")

            # 保存到 meta_data.jsonl 指定的路径
            dst = MHB_ROOT / record["input_ts"]["path"]
            assert dst.name == f"{record['id']}.wav"
            dst.parent.mkdir(parents=True, exist_ok=True)

            sf.write(dst, segment, sr, subtype="FLOAT")
            count += 1

    assert count == 84, f"Expected 84 segments, got {count}"
    print(f"ShipsEar: created {count} WAV files")


if __name__ == "__main__":
    main()