-
Notifications
You must be signed in to change notification settings - Fork 9
/
lab2audacity.py
46 lines (42 loc) · 1.71 KB
/
lab2audacity.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
import sys
import os
import traceback
floc = sys.argv[-1]
base, ext = os.path.splitext(floc)
def groups(iterable, num):
for i in range(0, len(iterable) - num + 1):
res = []
for j in range(0, num):
res.append(iterable[i + j])
yield tuple(res)
try:
if ext == '.lab':
lab = open(floc).readlines()
with open(base + '.txt', 'w') as f:
for i in lab:
time = i.strip().split()
if '.' in time[0]:
f.write('\t'.join(time) + '\n')
else:
f.write(f'{float(time[0]) / (10 ** 7):.6f}\t{float(time[1]) / (10 ** 7):.6f}\t{time[2]}\n')
elif ext == '.txt':
aud = open(floc).readlines()
for i in range(len(aud) - 1, -1, -1):
if aud[i].startswith('\\'):
del aud[i]
with open(base + '.lab', 'w') as f:
for i, j in groups(aud, 2):
time1 = i.strip().split()
time2 = j.strip().split()
f.write(f'{int(float(time1[0]) * (10 ** 7))} {int(float(time2[0]) * (10 ** 7))} {time1[2]}\n')
if not aud[-1].strip().endswith('-'):
time = aud[-1].strip().split()
f.write(f'{int(float(time[0]) * (10 ** 7))} {int(float(time[1]) * (10 ** 7))} {time[2]}\n')
except Exception as e:
for err in traceback.format_exception(e.__class__, e, e.__traceback__):
print(err, end='')
if e.__class__.__name__ == 'IndexError':
print('Probably a label without a phoneme in it? Here:\n')
print(i)
print('Search it in the audacity or HTS mono label you put in.')
os.system('pause')