File size: 1,947 Bytes
98115b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
from viphoneme import vi2IPA_split
import tqdm, glob
from pydub import AudioSegment

def process_text():
     f = open("DATA/train.txt", "r", encoding="utf-8")
     lines = f.read().splitlines()
     f.close()

     norm_lines = []
     for line in tqdm.tqdm(lines):
          file, script = line.split(" ",1)
          if not file.endswith(".wav"):
               file = file + ".wav"
          phoneme = vi2IPA_split(script.lower(), "/")
          if len(phoneme.split(" ")) < 4:
               continue
          norm_lines.append(file+"|"+file.replace("/wavs", "/embedding").replace(".wav",".npy")+"|"+phoneme)
     with open("DATA/train.txt", "w", encoding="utf-8") as file:
          for item in norm_lines:
               file.write(item + "\n")

     f = open("DATA/val.txt", "r", encoding="utf-8")
     lines = f.read().splitlines()
     f.close()

     norm_lines = []
     for line in tqdm.tqdm(lines):
          file, script = line.split(" ",1)
          if not file.endswith(".wav"):
               file = file + ".wav"
          phoneme = vi2IPA_split(script.lower(), "/")
          if len(phoneme.split(" ")) < 4:
               continue
          norm_lines.append(file+"|"+file.replace("/wavs", "/embedding").replace(".wav",".npy")+"|"+phoneme)
     with open("DATA/val.txt", "w", encoding="utf-8") as file:
          for item in norm_lines:
               file.write(item + "\n")

def process_speech():

     wavs = glob.glob("DATA/wavs/*.wav")
     for wav_file in tqdm.tqdm(wavs):
          audio = AudioSegment.from_file(wav_file)

          if audio.channels == 2:
               # Convert stereo audio to mono
               audio = audio.set_channels(1)

          if audio.frame_rate != 22050:
               # Convert the audio to 22050 Hz sample rate
               audio = audio.set_frame_rate(22050)

          audio.export(wav_file, format="wav")

if __name__ == "__main__":
     process_text()
     process_speech()