File size: 885 Bytes
12da6cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from argparse import ArgumentParser
from pathlib import Path

import numpy as np
from scipy.io import wavfile
from textgrid import TextGrid
from tqdm.auto import tqdm

from .config import FLAGS

parser = ArgumentParser()

parser.add_argument("-o", "--output-dir", type=Path, required=True)
args = parser.parse_args()

files = sorted(FLAGS.data_dir.glob("*.TextGrid"))
for fn in tqdm(files):
    tg = TextGrid.fromFile(str(fn.resolve()))
    wav_fn = FLAGS.data_dir / f"{fn.stem}.wav"
    sr, y = wavfile.read(wav_fn)
    y = np.copy(y)
    for phone in tg[1]:
        mark = phone.mark.strip().lower()
        if len(mark) == 0:
            mark = "sil"
        if mark in FLAGS.special_phonemes:
            l = int(phone.minTime * sr)
            r = int(phone.maxTime * sr)
            y[l:r] = 0
    out_file = args.output_dir / f"{fn.stem}.wav"
    wavfile.write(out_file, sr, y)