(tg_fn, ph, mel, hparams)
| 272 | |
| 273 | |
| 274 | def get_mel2ph(tg_fn, ph, mel, hparams): |
| 275 | ph_list = ph.split(" ") |
| 276 | with open(tg_fn, "r") as f: |
| 277 | tg = f.readlines() |
| 278 | tg = remove_empty_lines(tg) |
| 279 | tg = TextGrid(tg) |
| 280 | tg = json.loads(tg.toJson()) |
| 281 | split = np.ones(len(ph_list) + 1, np.float) * -1 |
| 282 | tg_idx = 0 |
| 283 | ph_idx = 0 |
| 284 | tg_align = [x for x in tg['tiers'][-1]['items']] |
| 285 | tg_align_ = [] |
| 286 | for x in tg_align: |
| 287 | x['xmin'] = float(x['xmin']) |
| 288 | x['xmax'] = float(x['xmax']) |
| 289 | if x['text'] in ['sil', 'sp', '', 'SIL', 'PUNC']: |
| 290 | x['text'] = '' |
| 291 | if len(tg_align_) > 0 and tg_align_[-1]['text'] == '': |
| 292 | tg_align_[-1]['xmax'] = x['xmax'] |
| 293 | continue |
| 294 | tg_align_.append(x) |
| 295 | tg_align = tg_align_ |
| 296 | tg_len = len([x for x in tg_align if x['text'] != '']) |
| 297 | ph_len = len([x for x in ph_list if not is_sil_phoneme(x)]) |
| 298 | assert tg_len == ph_len, (tg_len, ph_len, tg_align, ph_list, tg_fn) |
| 299 | while tg_idx < len(tg_align) or ph_idx < len(ph_list): |
| 300 | if tg_idx == len(tg_align) and is_sil_phoneme(ph_list[ph_idx]): |
| 301 | split[ph_idx] = 1e8 |
| 302 | ph_idx += 1 |
| 303 | continue |
| 304 | x = tg_align[tg_idx] |
| 305 | if x['text'] == '' and ph_idx == len(ph_list): |
| 306 | tg_idx += 1 |
| 307 | continue |
| 308 | assert ph_idx < len(ph_list), (tg_len, ph_len, tg_align, ph_list, tg_fn) |
| 309 | ph = ph_list[ph_idx] |
| 310 | if x['text'] == '' and not is_sil_phoneme(ph): |
| 311 | assert False, (ph_list, tg_align) |
| 312 | if x['text'] != '' and is_sil_phoneme(ph): |
| 313 | ph_idx += 1 |
| 314 | else: |
| 315 | assert (x['text'] == '' and is_sil_phoneme(ph)) \ |
| 316 | or x['text'].lower() == ph.lower() \ |
| 317 | or x['text'].lower() == 'sil', (x['text'], ph) |
| 318 | split[ph_idx] = x['xmin'] |
| 319 | if ph_idx > 0 and split[ph_idx - 1] == -1 and is_sil_phoneme(ph_list[ph_idx - 1]): |
| 320 | split[ph_idx - 1] = split[ph_idx] |
| 321 | ph_idx += 1 |
| 322 | tg_idx += 1 |
| 323 | assert tg_idx == len(tg_align), (tg_idx, [x['text'] for x in tg_align]) |
| 324 | assert ph_idx >= len(ph_list) - 1, (ph_idx, ph_list, len(ph_list), [x['text'] for x in tg_align], tg_fn) |
| 325 | mel2ph = np.zeros([mel.shape[0]], np.int) |
| 326 | split[0] = 0 |
| 327 | split[-1] = 1e8 |
| 328 | for i in range(len(split) - 1): |
| 329 | assert split[i] != -1 and split[i] <= split[i + 1], (split[:-1],) |
| 330 | split = [int(s * hparams['audio_sample_rate'] / hparams['hop_size'] + 0.5) for s in split] |
| 331 | for ph_idx in range(len(ph_list)): |
no test coverage detected