Commit e72cf052678044c365d792cca2f2aa5db3034324
1 parent
1c1f0f29a7
Exists in
master
Few useless modifications
Showing 1 changed file with 0 additions and 9 deletions Inline Diff
volia/data.py
1 | import argparse | 1 | import argparse |
2 | import sys | ||
3 | |||
4 | from utils import SubCommandRunner | 2 | from utils import SubCommandRunner |
5 | |||
6 | import core.data | 3 | import core.data |
7 | 4 | ||
8 | 5 | ||
9 | |||
10 | |||
11 | def filter_file(file, filter, outfile): | 6 | def filter_file(file, filter, outfile): |
12 | file_path = file | 7 | file_path = file |
13 | filter_path = filter | 8 | filter_path = filter |
14 | file_data = core.data.read_id_values(file_path) | 9 | file_data = core.data.read_id_values(file_path) |
15 | filter_data = core.data.read_id_values(filter_path) | 10 | filter_data = core.data.read_id_values(filter_path) |
16 | 11 | ||
17 | with open(outfile, "w") as out: | 12 | with open(outfile, "w") as out: |
18 | for id_ in filter_data: | 13 | for id_ in filter_data: |
19 | core.data.write_line(id_, file_data[id_], out) | 14 | core.data.write_line(id_, file_data[id_], out) |
20 | |||
21 | |||
22 | 15 | ||
23 | |||
24 | 16 | ||
25 | def convert(file, type_from, type): | 17 | def convert(file, type_from, type): |
26 | |||
27 | pass | 18 | pass |
28 | 19 | ||
29 | def utt2dur(wavscp: str, outfile: str): | 20 | def utt2dur(wavscp: str, outfile: str): |
30 | 21 | ||
31 | import wave | 22 | import wave |
32 | import contextlib | 23 | import contextlib |
33 | 24 | ||
34 | with open(wavscp, "r") as f, open(outfile, "w") as of: | 25 | with open(wavscp, "r") as f, open(outfile, "w") as of: |
35 | for line in f: | 26 | for line in f: |
36 | splited = line.replace("\n", "").split(" ") | 27 | splited = line.replace("\n", "").split(" ") |
37 | id_ = splited[0] | 28 | id_ = splited[0] |
38 | wav_ = splited[1] | 29 | wav_ = splited[1] |
39 | duration = 0 | 30 | duration = 0 |
40 | 31 | ||
41 | with contextlib.closing(wave.open(wav_,'r')) as wav_f: | 32 | with contextlib.closing(wave.open(wav_,'r')) as wav_f: |
42 | frames = wav_f.getnframes() | 33 | frames = wav_f.getnframes() |
43 | rate = wav_f.getframerate() | 34 | rate = wav_f.getframerate() |
44 | duration = frames / float(rate) | 35 | duration = frames / float(rate) |
45 | 36 | ||
46 | of.write(f"{id_} {duration}\n") | 37 | of.write(f"{id_} {duration}\n") |
47 | 38 | ||
48 | 39 | ||
49 | if __name__ == "__main__": | 40 | if __name__ == "__main__": |
50 | # Main parser | 41 | # Main parser |
51 | parser = argparse.ArgumentParser(description="manage files") | 42 | parser = argparse.ArgumentParser(description="manage files") |
52 | subparsers = parser.add_subparsers(title="actions") | 43 | subparsers = parser.add_subparsers(title="actions") |
53 | 44 | ||
54 | # Filter ids | 45 | # Filter ids |
55 | parser_filter = subparsers.add_parser("filter", help="filter a file") | 46 | parser_filter = subparsers.add_parser("filter", help="filter a file") |
56 | parser_filter.add_argument("--file", type=str, help="path of the file to filter", required=True) | 47 | parser_filter.add_argument("--file", type=str, help="path of the file to filter", required=True) |
57 | parser_filter.add_argument("--filter", type=str, help="filter file [id_from] [id_to]", required=True) | 48 | parser_filter.add_argument("--filter", type=str, help="filter file [id_from] [id_to]", required=True) |
58 | parser_filter.add_argument("--outfile", type=str, help="output file", required=True) | 49 | parser_filter.add_argument("--outfile", type=str, help="output file", required=True) |
59 | parser_filter.set_defaults(which="filter") | 50 | parser_filter.set_defaults(which="filter") |
60 | 51 | ||
61 | # Convert | 52 | # Convert |
62 | parser_convert = subparsers.add_parser("convert", help="convert a file") | 53 | parser_convert = subparsers.add_parser("convert", help="convert a file") |
63 | parser_convert.add_argument("--file", type=str, help="...", required=True) | 54 | parser_convert.add_argument("--file", type=str, help="...", required=True) |
64 | parser_convert.add_argument("--type-from", type=str, choices=["old-masseffect", "new-masseffect"], help="...", required=True) | 55 | parser_convert.add_argument("--type-from", type=str, choices=["old-masseffect", "new-masseffect"], help="...", required=True) |
65 | parser_convert.add_argument("--type", choices=["old-masseffect", "new-masseffect"], required=True) | 56 | parser_convert.add_argument("--type", choices=["old-masseffect", "new-masseffect"], required=True) |
66 | parser_convert.set_defaults(which="convert") | 57 | parser_convert.set_defaults(which="convert") |
67 | 58 | ||
68 | # utt2dur | 59 | # utt2dur |
69 | parser_utt2dur = subparsers.add_parser("utt2dur", help="generate utt2dur file") | 60 | parser_utt2dur = subparsers.add_parser("utt2dur", help="generate utt2dur file") |
70 | parser_utt2dur.add_argument("--wavscp", type=str, help="wav file", required=True) | 61 | 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") | 62 | parser_utt2dur.add_argument("--outfile", type=str, default="utt2dur", help="output file") |
72 | parser_utt2dur.set_defaults(which="utt2dur") | 63 | parser_utt2dur.set_defaults(which="utt2dur") |
73 | 64 | ||
74 | # TODO: utt2label_to_label2utt | 65 | # TODO: utt2label_to_label2utt |
75 | 66 | ||
76 | # TODO: label2utt_to_utt2label | 67 | # TODO: label2utt_to_utt2label |
77 | 68 | ||
78 | # Parse | 69 | # Parse |
79 | args = parser.parse_args() | 70 | args = parser.parse_args() |
80 | 71 | ||
81 | # Run commands | 72 | # Run commands |
82 | runner = SubCommandRunner({ | 73 | runner = SubCommandRunner({ |
83 | "convert" : convert, | 74 | "convert" : convert, |
84 | "filter": filter_file, | 75 | "filter": filter_file, |
85 | "utt2dur": utt2dur | 76 | "utt2dur": utt2dur |
86 | }) | 77 | }) |
87 | 78 | ||
88 | runner.run(args.which, args.__dict__, remove="which") | 79 | runner.run(args.which, args.__dict__, remove="which") |
89 | 80 |