-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5_1_process_textgrid_mid_as_sp.py
41 lines (37 loc) · 1.49 KB
/
5_1_process_textgrid_mid_as_sp.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
import os
import sys
import numpy as np
from pypinyin import pinyin, lazy_pinyin, Style
import re
import textgrid
base_dir = "./"
# root_dir = "./train/"
root_dir = "./my_aligned_textgrid/"
pause_time_dir = "./out_textgrid_mid_as_sp/"
pattern = re.compile(r'(.*)\.TextGrid$')
for root, dir, files in os.walk(root_dir):
for filename in files:
out_file_ptn = pattern.match(filename)
if out_file_ptn is not None:
tg = textgrid.TextGrid()
tg.read(root_dir+filename) # 'file.TextGrid' 是文件名
pause_time_start = 0.0
count = 0
current_character = "start"
out_file = pause_time_dir + out_file_ptn.group(1) + ".sp"
# 处理每一个文件
with open(out_file, mode="w") as out:
for index, i in enumerate(tg.tiers[0]):
if i.mark != "":
# print("asdasd", i.mark)
# 当前字的pause_time
mid = (i.maxTime + i.minTime) / 2.0
pause_time = mid - pause_time_start
print(current_character, "pause_time:", pause_time)
if current_character != "start":
out.write("{}".format(pause_time))
out.write("\n")
# 更新当前字
current_character = i.mark
pause_time_start = mid
count += 1