-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlabel2textgrid.py
55 lines (43 loc) · 1.73 KB
/
label2textgrid.py
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
import argparse
import os
import sys
from lib.textgrid import *
__author__ = 'yossiadi'
def create_text_grid(label_path, output_text_grid, length, start_extract):
# defines
msc_2_sec = 0.001
# validation
if not os.path.exists(label_path):
print >> sys.stderr, "label file does not exits"
return
# read the label file and parse it
fid = open(label_path)
lines = fid.readlines()
values = lines[0].split()
fid.close()
# create the TextGrid file and save it
if len(values) == 2:
onset = values[0]
offset = values[1]
text_grid = TextGrid()
vot_tier = IntervalTier(name='word', xmin=0.0, xmax=float(length))
vot_tier.append(Interval(0, (float(onset) * msc_2_sec + start_extract) * 10, ""))
vot_tier.append(
Interval((float(onset) * msc_2_sec + start_extract) * 10, (float(offset) * msc_2_sec + start_extract) * 10,
"word"))
vot_tier.append(Interval((float(offset) * msc_2_sec + start_extract) * 10, float(length), ""))
text_grid.append(vot_tier)
text_grid.write(output_text_grid)
if __name__ == "__main__":
# the first argument is the label file path
# the second argument is the wav file path
# the third argument is the output path
# -------------MENU-------------- #
# command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("label_filename", help="The label file")
parser.add_argument("wav_filename", help="The wav file")
parser.add_argument("output_text_grid", help="The output TextGrid file")
args = parser.parse_args()
# main function
create_text_grid(args.label_filename, args.wav_filename, args.output_text_grid, 0.05)