Commit 08996e0a950f0fdcea74bfdb62edae33882dffe0

Authored by quillotm
1 parent 233b7d451b
Exists in master

Little modifications on filter_file

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

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