Convert .lab to textgrid
Because the lab is Praatland
!pip install praatio
def _read_lab(filename: str, skip_empty_labels: bool = True):
ret = []
with open(filename) as file:
for line in file.readlines():
if line.strip() == '':
continue
if line.startswith('#'):
continue
l = line.rstrip().split(' ')
if skip_empty_labels and len(l) != 3:
continue
tmp = {}
tmp['start'] = l[0]
tmp['end'] = l[1]
if len(l) == 3:
tmp['phone'] = l[2]
else:
tmp['phone'] = ''
ret.append(tmp)
return ret
%%writefile sample.lab
0 9700000 sil
9700000 10900000 i
10900000 12400000 mj
12400000 13100000 lj
13100000 15200000 au
15200000 16300000 r
16300000 18100000 sil
18100000 19100000 sil
19100000 23700000
ll = _read_lab('sample.lab', False)
from praatio import textgrid
from praatio.utilities.constants import Interval
out = []
for l in ll:
out.append(Interval(int(l['start'])/10000000.0, int(l['end'])/10000000.0, l['phone']))
tier_start = out[0][0]
tier_end = out[-1][1]
out
tg = textgrid.Textgrid()
phone_tier = textgrid.IntervalTier('phones', out, tier_start, tier_end)
tg.addTier(phone_tier)
tg.save('out.TextGrid', format="long_textgrid", includeBlankSpaces=False)
!cat out.TextGrid