Commit 88260de938b023b156eb46502688c7531f257dae
1 parent
3b5a487de8
Exists in
master
add utt2dur generation function to the data module
Showing 1 changed file with 31 additions and 1 deletions Side-by-side Diff
volia/data.py
... | ... | @@ -8,7 +8,6 @@ |
8 | 8 | |
9 | 9 | |
10 | 10 | |
11 | - | |
12 | 11 | def filter_file(file, filter, outfile): |
13 | 12 | file_path = file |
14 | 13 | filter_path = filter |
15 | 14 | |
16 | 15 | |
... | ... | @@ -24,9 +23,29 @@ |
24 | 23 | |
25 | 24 | |
26 | 25 | def convert(file, type_from, type): |
26 | + | |
27 | 27 | pass |
28 | 28 | |
29 | +def utt2dur(wavscp: str, outfile: str): | |
30 | + | |
31 | + import wave | |
32 | + import contextlib | |
29 | 33 | |
34 | + with open(wavscp, "r") as f, open(outfile, "w") as of: | |
35 | + for line in f: | |
36 | + splited = line.replace("\n", "").split(" ") | |
37 | + id_ = splited[0] | |
38 | + wav_ = splited[1] | |
39 | + duration = 0 | |
40 | + | |
41 | + with contextlib.closing(wave.open(wav_,'r')) as wav_f: | |
42 | + frames = wav_f.getnframes() | |
43 | + rate = wav_f.getframerate() | |
44 | + duration = frames / float(rate) | |
45 | + | |
46 | + of.write(f"{id_} {duration}\n") | |
47 | + | |
48 | + | |
30 | 49 | if __name__ == "__main__": |
31 | 50 | # Main parser |
32 | 51 | parser = argparse.ArgumentParser(description="manage files") |
... | ... | @@ -46,6 +65,16 @@ |
46 | 65 | parser_convert.add_argument("--type", choices=["old-masseffect", "new-masseffect"], required=True) |
47 | 66 | parser_convert.set_defaults(which="convert") |
48 | 67 | |
68 | + # utt2dur | |
69 | + parser_utt2dur = subparsers.add_parser("utt2dur", help="generate utt2dur file") | |
70 | + parser_utt2dur.add_argument("--wavscp", type=str, help="wav file", required=True) | |
71 | + parser_utt2dur.add_argument("--outfile", type=str, default="utt2dur", help="output file") | |
72 | + parser_utt2dur.set_defaults(which="utt2dur") | |
73 | + | |
74 | + # TODO: utt2label_to_label2utt | |
75 | + | |
76 | + # TODO: label2utt_to_utt2label | |
77 | + | |
49 | 78 | # Parse |
50 | 79 | args = parser.parse_args() |
51 | 80 | |
... | ... | @@ -53,6 +82,7 @@ |
53 | 82 | runner = SubCommandRunner({ |
54 | 83 | "convert" : convert, |
55 | 84 | "filter": filter_file, |
85 | + "utt2dur": utt2dur | |
56 | 86 | }) |
57 | 87 | |
58 | 88 | runner.run(args.which, args.__dict__, remove="which") |