-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtts_engine.py
172 lines (132 loc) · 5.55 KB
/
tts_engine.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import getopt
import json
import subprocess
import sys
import tempfile
import os
from datetime import datetime
path = (os.getcwd() + "/configuration.json")
with open(path) as json_file1:
# global configuration
configuration = json.load(json_file1)
def list_voices():
print("Available voices installed in /usr/share/festival/voices, "
"this option only works if festival is installed ok\n")
langs = os.listdir("/usr/share/festival/voices")
for n in range(0, len(langs)):
print(langs[n] + ": " + str(os.listdir("/usr/share/festival/voices/" + langs[n])))
def speech_lang(text, lang, gender):
speech(text, configuration["voice"][lang][gender])
def export_lang(text, lang, gender):
export(text, configuration["voice"][lang][gender])
def speech(text, voice):
iconv = '/usr/bin/iconv'
txt2wave = '/usr/bin/text2wave'
with tempfile.NamedTemporaryFile() as text_file, \
tempfile.NamedTemporaryFile() as encoded_file, \
tempfile.NamedTemporaryFile() as wave_file:
f = open(text_file.name, 'w', encoding='utf8')
f.write(text)
f.close()
cmd = '{0} -f utf8 -t ISO-8859-15//TRANSLIT {1} > {2}'. \
format(iconv, text_file.name, encoded_file.name)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
p.wait()
cmd = "{0} -o {1} {2} -eval '(voice_" + voice + ")'"
cmd = cmd.format(txt2wave, wave_file.name, encoded_file.name)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
p.wait()
os.system("aplay %s" % wave_file.name)
return
def export(text, voice):
iconv = '/usr/bin/iconv'
txt2wave = '/usr/bin/text2wave'
now = datetime.now()
date_time = now.strftime("%m-%d-%Y_%H-%M-%S")
with tempfile.NamedTemporaryFile() as text_file, \
tempfile.NamedTemporaryFile() as encoded_file, \
open('/tmp/' + date_time + '.wav', 'w') as wave_file:
f = open(text_file.name, 'w', encoding='utf8')
f.write(text)
f.close()
cmd = '{0} -f utf8 -t ISO-8859-15//TRANSLIT {1} > {2}'. \
format(iconv, text_file.name, encoded_file.name)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
p.wait()
cmd = "{0} -o {1} {2} -eval '(voice_" + voice + ")'"
cmd = cmd.format(txt2wave, wave_file.name, encoded_file.name)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
p.wait()
print(wave_file.name)
return
def main(argv):
with open('configuration.json') as json_file:
global configuration
configuration = json.load(json_file)
text = ''
lang = ''
gender = ''
voice = ''
is_export = False
try:
opts, args = getopt.getopt(argv, "svhl:g:x:t:e:", ["help", "list-voices", "version", "lang=", "gender=", "voice=", "text=", "export="])
except getopt.GetoptError:
print('-l <lang> -g <gender> -t <text> /OR/ -x <voice> -t <text>')
print('--lang <lang> --gender <gender> --text <text> /OR/ --voice <voice> --text <text>')
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
print(" #### USAGE ####")
print('-l <lang> -g <gender> -t <text> /OR/ -x <voice> -t <text>')
print('--lang <lang> --gender <gender> --text <text> /OR/ --voice <voice> --text <text>')
print('--lang <lang> --gender <gender> --text <text> /OR/ --voice <voice> --text <text>\n')
print(" #### UTILS ####")
print("list voices : -s or --list-voices")
print("version : -v or --version")
sys.exit()
elif opt in ("-v", "--version"):
print(" **** festival-python: 1.0 ****")
os.system("festival --version")
os.system("python3 --version")
sys.exit()
elif opt in ("-l", "--lang"):
lang = arg
elif opt in ("-g", "--gender"):
gender = arg
elif opt in ("-x", "--voice"):
voice = arg
elif opt in ("-t", "--text"):
text = arg
elif opt in ("-e", "--export"):
is_export = True
elif opt in ("-s", "--list-voices"):
list_voices()
sys.exit()
if is_export:
if (lang == '' or gender == '') and (voice != '' and text != ''):
export(text, voice)
elif lang != '' and text != '' and gender != '':
export_lang(text, lang, gender)
else:
# if (lang == '' or text == '' or gender == '') and voice == '':
print('## Error, missing parameters')
print('-l <lang> -g <gender> -t <text> /OR/ -x <voice> -t <text>')
print('--lang <lang> --gender <gender> --text <text> /OR/ --voice <voice> --text <text>')
sys.exit()
else:
if (lang == '' or gender == '') and (voice != '' and text != ''):
speech(text, voice)
elif lang != '' and text != '' and gender != '':
speech_lang(text, lang, gender)
else:
# if (lang == '' or text == '' or gender == '') and voice == '':
print('## Error, missing parameters')
print('-l <lang> -g <gender> -t <text> /OR/ -x <voice> -t <text>')
print('--lang <lang> --gender <gender> --text <text> /OR/ --voice <voice> --text <text>')
sys.exit()
print('Lang is ', lang)
print('Gender is ', gender)
print('Text is ', text)
# speech(text, lang, gender)
if __name__ == "__main__":
main(sys.argv[1:])