Views
No views yet
cotovia_0.5_amd64.deb and cotovia-lang-gl_0.5_all.deb, which can be installed using the following commands:1sudo dpkg -i cotovia_0.5_amd64.deb
2sudo dpkg -i cotovia-lang-gl_0.5_all.debpip install TTS1import argparse
2import random
3import re
4import string
5import subprocess
6from TTS.utils.synthesizer import Synthesizer
7
8PUNCLIST = [';', '?', '¿', ',', ':', '.', '!', '¡']
9
10def sanitize_filename(filename):
11 """Remove or replace any characters that are not allowed in file names."""
12 return ''.join(c for c in filename if c.isalnum() or c in (' ', '_', '-')).rstrip()
13
14def canBeNumber(n):
15 try:
16 int(n)
17 return True
18 except ValueError:
19 # Not a number
20 return False
21
22def is_number(index, text):
23 if index == 0:
24 return False
25 elif index == len(text) - 1:
26 return False
27 else:
28 return canBeNumber(text[index - 1]) and canBeNumber(text[index + 1])
29
30def split_punc(text):
31 segments = []
32 puncs = []
33 curr_seg = ""
34 previous_punc = False
35
36 for i, c in enumerate(text):
37 if c in PUNCLIST and not previous_punc and not is_number(i, text):
38 segments.append(curr_seg.strip())
39 puncs.append(c)
40 curr_seg = ""
41 previous_punc = True
42 elif c in PUNCLIST and previous_punc:
43 puncs[-1] += c
44 else:
45 curr_seg += c
46 previous_punc = False
47
48 segments.append(curr_seg.strip())
49
50 #Remove empty segments in the list
51 segments = filter(None, segments)
52
53 # store segments as a list
54 segments = list(segments)
55
56 return segments, puncs
57
58def remove_tra3_tags(phontrans):
59 s = re.sub(r'#(.+?)#', r'', phontrans)
60 s = re.sub(r'%(.+?)%', r'', s)
61 s = re.sub(' +',' ',s)
62 s = re.sub('-','',s)
63 return s.strip()
64
65def to_cotovia(text_segments):
66 # Input and output Cotovía files
67 res = ''.join(random.choices(string.ascii_lowercase + string.digits, k=5))
68 COTOVIA_IN_TXT_PATH = res + '.txt'
69 COTOVIA_IN_TXT_PATH_ISO = 'iso8859-1' + res + '.txt'
70 COTOVIA_OUT_PRE_PATH = 'iso8859-1' + res + '.tra'
71 COTOVIA_OUT_PRE_PATH_UTF8 = 'utf8' + res + '.tra'
72
73 with open(COTOVIA_IN_TXT_PATH, 'w') as f:
74 for seg in text_segments:
75 if seg:
76 f.write(seg + '\n')
77 else:
78 f.write(',' + '\n')
79
80 # utf-8 to iso8859-1
81 subprocess.run(["iconv", "-f", "utf-8", "-t", "iso8859-1", COTOVIA_IN_TXT_PATH, "-o", COTOVIA_IN_TXT_PATH_ISO], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
82 # call cotovia with -t3 option
83 subprocess.run(["cotovia", "-i", COTOVIA_IN_TXT_PATH_ISO, "-t3", "-n"], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
84 # iso8859-1 to utf-8
85 subprocess.run(["iconv", "-f", "iso8859-1", "-t", "utf-8", COTOVIA_OUT_PRE_PATH, "-o", COTOVIA_OUT_PRE_PATH_UTF8], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
86
87 segs = []
88 try:
89 with open(COTOVIA_OUT_PRE_PATH_UTF8, 'r') as f:
90 segs = [line.rstrip() for line in f]
91 segs = [remove_tra3_tags(line) for line in segs]
92 except:
93 print("ERROR: Couldn't read cotovia output")
94
95 subprocess.run(["rm", COTOVIA_IN_TXT_PATH, COTOVIA_IN_TXT_PATH_ISO, COTOVIA_OUT_PRE_PATH, COTOVIA_OUT_PRE_PATH_UTF8], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
96
97 return segs
98
99def merge_punc(text_segs, puncs):
100 merged_str = ""
101
102 for i, seg in enumerate(text_segs):
103 merged_str += seg + " "
104 if i < len(puncs):
105 merged_str += puncs[i] + " "
106
107 # remove spaces before , . ! ? ; : ) ] of the merged string
108 merged_str = re.sub(r"\s+([.,!?;:)\]])", r"\1", merged_str)
109
110 # remove spaces after ( [ ¡ ¿ of the merged string
111 merged_str = re.sub(r"([\(\[¡¿])\s+", r"\1", merged_str)
112
113 return merged_str.strip()
114
115
116def accent_convert(phontrans):
117 transcript = re.sub('a\^','á',phontrans)
118 transcript = re.sub('e\^','é',transcript)
119 transcript = re.sub('i\^','í',transcript)
120 transcript = re.sub('o\^','ó',transcript)
121 transcript = re.sub('u\^','ú',transcript)
122 transcript = re.sub('E\^','É',transcript)
123 transcript = re.sub('O\^','Ó',transcript)
124 return transcript
125
126
127def text_preprocess(text):
128 #Split from punc
129 text_segments, puncs = split_punc(text)
130 cotovia_phon_segs = to_cotovia(text_segments)
131 cotovia_phon_str = merge_punc(cotovia_phon_segs, puncs)
132 phon_str = accent_convert(cotovia_phon_str)
133 return phon_str
134
135
136def main():
137 parser = argparse.ArgumentParser(description='Cotovia phoneme transcription.')
138 parser.add_argument('text', type=str, help='Text to synthetize')
139 parser.add_argument('model_path', type=str, help='Absolute path to the model checkpoint.pth')
140 parser.add_argument('config_path', type=str, help='Absolute path to the model config.json')
141
142 args = parser.parse_args()
143
144 print("Text before preprocessing: ", args.text)
145 text = text_preprocess(args.text)
146 print("Text after preprocessing: ", text)
147 synthesizer = Synthesizer(
148 args.model_path, args.config_path, None, None, None, None,
149 )
150
151 # Step 1: Extract the first word from the text
152 first_word = args.text.split()[0] if args.text.split() else "audio"
153 first_word = sanitize_filename(first_word) # Sanitize to make it a valid filename
154
155 # Step 2: Use synthesizer's built-in function to synthesize and save the audio
156 wavs = synthesizer.tts(text)
157 filename = f"{first_word}.wav"
158 synthesizer.save_wav(wavs, filename)
159
160 print(f"Audio file saved as: {filename}")
161
162if __name__ == "__main__":
163 main()synthesize.py, avaliable in this repository. You can use this script to synthesise speech from an input text as follows:python synthesize.py text model_path config_path| Hyperparameter | Value |
|---|---|
| Model | vits |
| Batch Size | 48 |
| Eval Batch Size | 16 |
| Mixed Precision | true |
| Window Length | 1024 |
| Hop Length | 256 |
| FTT size | 1024 |
| Num Mels | 80 |
| Phonemizer | null |
| Phoneme Language | null |
| Text Cleaners | null |
| Formatter | nos_fonemas |
| Optimizer | adam |
| Adam betas | (0.8, 0.99) |
| Adam eps | 1e-09 |
| Adam weight decay | 0.01 |
| Learning Rate Gen | 0.0002 |
| Lr. scheduler Gen | ExponentialLR |
| Lr. scheduler Gamma Gen | 0.999875 |
| Learning Rate Disc | 0.0002 |
| Lr. scheduler Disc | ExponentialLR |
| Lr. scheduler Gamma Disc | 0.999875 |