Fix ar9x compile/boot (#7102)
[opentx.git] / radio / util / tts.py
blobc72a4b7259ac96b340d0e5651e0b2c3d6d1a47df
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
5 # To use this script you need the following installed within python
6 # Python 2.5 http://www.python.org/download/releases/2.5.4/
7 # Python 2.7 http://www.python.org/download/releases/2.7.3/
8 # PyTTS http://pypi.python.org/pypi/pyTTS
9 # PyWin32 http://sourceforge.net/projects/pywin32/files/pywin32/
11 # Note
12 # At the moment, pyTTS is only available for Python 2.3, 2.4 and 2.5. To use it for later versions without having to
13 # recompile it, a quick and dirty solution is to:
14 # copy the entire pyTTS directory from Python25\Lib\site-packages to Python26 or Python27
15 # replace TTSFast.py with an empty file. This way the version-dependent pyd file isn't loaded.
17 # in addition you will need some tools.
18 # ffmpeg, sox, adconvertor ttscmd (2cnd speach centre)
19 # have fun!
20 # Sound pack maintainers (incomplete list) by language alphabetical order
21 # Czech : Martin Hotar
22 # French : Bertrand Songis & André Bernet
23 # English : Rob Thompson & Martin Hotar
24 # German : Romolo Manfredini (Some corrections by Peer)
25 # Italian : Romolo Manfredini
26 # Portuguese : Romolo Manfredini
27 # Spanish : Romolo Manfredini (With the help of Jose Moreno)
29 # from __future__ import print_function
31 import os
32 import sys
33 import subprocess
34 import zipfile
35 from tts_common import *
37 try:
38 # Python 3
39 from http.client import HTTPConnection
40 from urllib.parse import urlencode
41 except ImportError:
42 # Python 2
43 from httplib import HTTPConnection
44 from urllib import urlencode
47 def wavstrip(filename):
48 output = "_" + filename
49 subprocess.Popen(["sox", filename, output, "silence", "1", "0.1", "0.1%", "reverse", "silence", "1", "0.1", "0.1%", "reverse"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait()
50 os.remove(filename)
51 os.rename(output, filename)
54 def generate(str, filename):
55 print(filename, str)
57 if not str:
58 str = " !" # this is so blank wav files never exist!
60 if "speak" in sys.argv:
61 if "sapi" in sys.argv:
62 tts.Speak(str)
63 elif "espeak" in sys.argv:
64 subprocess.Popen(["espeak", "-v", espeakVoice, "-s", espeakspeed, "-z", str.encode("utf-8")], stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait()
65 elif "google" in sys.argv:
66 "speak not implemented with google tts engine"
67 exit()
68 else:
69 print("which speach engine?")
70 exit()
71 else:
72 if "sapi" in sys.argv:
73 ttsfilename = "ttsfile.wav"
74 tts.SpeakToWave(ttsfilename, str)
75 elif "sapi2" in sys.argv:
76 ttsfilename = "ttsfile.wav"
77 subprocess.Popen(["ttscmd", "/ttw", str.encode("utf-8"), ttsfilename, "-v", voiceID, "-b", "32", "-s", "\"-3\"", "-w", "32", "-f", "47"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait()
78 elif "espeak" in sys.argv:
79 ttsfilename = "ttsfile.wav"
80 subprocess.Popen(["espeak", "-v", espeakVoice, "-s", espeakspeed, "-z", "-w", ttsfilename, str.encode("utf-8")], stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait()
81 elif "google" in sys.argv:
82 ttsmp3 = "ttsfile.mp3"
83 ttsfilename = "ttsfile.wav"
84 conn = HTTPConnection("translate.google.com")
85 params = urlencode({'ie': "UTF-8", 'tl': directory, 'q': str.encode("utf-8")})
86 headers = {"User-Agent": "Mozilla"}
87 conn.request("GET", u"/translate_tts?%s" % params, headers=headers)
88 # conn.request("GET", "/translate_tts?ie=UTF-8&tl=%s&q=%s" % (directory, urllib.urlencode(str)), headers={"User-Agent": "Mozilla"})
89 resp = conn.getresponse()
90 with open(ttsmp3, "wb") as f:
91 f.write(resp.read())
92 subprocess.Popen(["ffmpeg", "-y", "-i", ttsmp3, "-acodec", "pcm_s16le", ttsfilename], stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait()
93 conn.close()
95 else:
96 print("which speach engine?")
97 exit()
99 wavstrip(ttsfilename)
101 if board in ('sky9x', 'taranis'):
102 if 'sox' in sys.argv:
103 maxvolume = subprocess.Popen(["sox", ttsfilename, "-n", "stat", "-v"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[1]
104 if "not sound" in maxvolume:
105 subprocess.Popen(["sox", "--show-progress", filename, ttsfilename], stdout=subprocess.PIPE).communicate()[0]
106 else:
107 subprocess.Popen(["sox", "--show-progress", "-v", maxvolume, filename, ttsfilename], stdout=subprocess.PIPE).communicate()[0]
108 if board == 'sky9x':
109 subprocess.Popen(["sox", "-twav", ttsfilename, "-b1600", "-c1", "-e", "a-law", filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait()
110 else:
111 subprocess.Popen(["sox", "-twav", ttsfilename, "-b32000", "-c1", "-e", "a-law", filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait()
112 else:
113 if board == 'sky9x':
114 subprocess.Popen(["ffmpeg", "-y", "-i", ttsfilename, "-acodec", defaultcodec, "-ar", "16000", filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait()
115 else:
116 subprocess.Popen(["ffmpeg", "-y", "-i", ttsfilename, "-acodec", defaultcodec, "-ar", "32000", filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait()
117 elif board == 'gruvin9x':
118 subprocess.Popen(["AD4CONVERTER", "-E4", ttsfilename], stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait()
119 os.rename(ttsfilename.replace(".wav", ".ad4"), filename)
120 else:
121 subprocess.Popen(["ffmpeg", "-y", "-i", ttsfilename, "-acodec", "pcm_u8", "-ar", "16000", filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait()
122 os.remove(ttsfilename)
124 ################################################################
126 if __name__ == "__main__":
130 if "sapi" in sys.argv:
131 import pyTTS
132 tts = pyTTS.Create()
133 # tts.SetOutputFormat(16, 16, 1)
134 # tts.Volume = 40
135 # tts.SetRate(1)
136 if "list" in sys.argv:
137 print(tts.GetVoiceNames())
138 exit()
140 if "mulaw" in sys.argv:
141 defaultcodec = "pcm_mulaw"
142 else:
143 defaultcodec = "pcm_alaw"
145 if "en" in sys.argv:
146 from tts_en import systemSounds, sounds
148 directory = "en"
149 voice = "english"
150 if "sapi" in sys.argv:
151 if "scottish" in sys.argv:
152 tts.SetVoiceByName("ScanSoftFiona_Full_22kHz")
153 voice = "english-scottish"
154 elif "american" in sys.argv:
155 tts.SetVoiceByName("ScanSoftJennifer_Full_22kHz")
156 voice = "english-american"
157 elif "australian" in sys.argv:
158 tts.SetVoiceByName("ScanSoftKaren_Full_22kHz")
159 voice = "english-australian"
160 elif "irish" in sys.argv:
161 tts.SetVoiceByName("ScanSoftMoira_Full_22kHz")
162 voice = "english-irish"
163 else:
164 tts.SetVoiceByName("ScanSoftFiona_Full_22kHz")
165 voice = "english-english"
166 elif "sapi2" in sys.argv:
167 if "scottish" in sys.argv:
168 voiceID = "17"
169 voice = "english-scottish"
170 elif "american" in sys.argv:
171 voiceID = "18"
172 voice = "english-american"
173 elif "australian" in sys.argv:
174 voiceID = "20"
175 voice = "english-australian"
176 elif "irish" in sys.argv:
177 voiceID = "21"
178 voice = "english-irish"
179 elif "french" in sys.argv:
180 voiceID = "19"
181 voice = "english-french"
182 elif "german" in sys.argv:
183 voiceID = "22"
184 voice = "english-german"
185 else:
186 voiceID = "17"
187 voice = "english-english"
189 elif "espeak" in sys.argv:
190 espeakVoice = "mb-us1"
191 espeakspeed = "150"
193 elif "fr" in sys.argv:
194 from tts_fr import systemSounds, sounds
196 directory = "fr"
197 voice = "french"
198 if "sapi" in sys.argv:
199 tts.SetVoiceByName("ScanSoftVirginie_Full_22kHz")
200 elif "espeak" in sys.argv:
201 espeakVoice = "mb-fr4+f4"
202 espeakspeed = "140"
204 elif "it" in sys.argv:
205 from tts_it import systemSounds, sounds
207 directory = "it"
208 voice = "italian"
209 if "sapi" in sys.argv:
210 tts.SetVoiceByName("ScanSoftVirginie_Full_22kHz")
211 elif "espeak" in sys.argv:
212 espeakVoice = "mb-it4"
213 espeakspeed = "160"
215 elif "de" in sys.argv:
216 from tts_de import systemSounds, sounds
218 directory = "de"
219 voice = "german"
220 if "sapi" in sys.argv:
221 tts.SetVoiceByName("ScanSoftVirginie_Full_22kHz")
222 elif "espeak" in sys.argv:
223 espeakVoice = "mb-de4"
224 espeakspeed = "160"
226 elif "pt" in sys.argv:
227 from tts_pt import systemSounds, sounds
229 directory = "pt"
230 voice = "portuguese"
231 if "sapi" in sys.argv:
232 tts.SetVoiceByName("ScanSoftVirginie_Full_22kHz")
233 elif "espeak" in sys.argv:
234 espeakVoice = "mb-pt1+f1"
235 espeakspeed = "160"
237 elif "es" in sys.argv:
238 from tts_es import systemSounds, sounds
240 directory = "es"
241 voice = "spanish"
242 if "sapi" in sys.argv:
243 tts.SetVoiceByName("ScanSoftVirginie_Full_22kHz")
244 elif "espeak" in sys.argv:
245 espeakVoice = "mb-es1+f1"
246 espeakspeed = "160"
248 elif "cz" in sys.argv:
249 from tts_cz import systemSounds, sounds
251 directory = "cz"
252 voice = "czech"
253 if "sapi" in sys.argv:
254 tts.SetVoiceByName("ScanSoftZuzana_Full_22kHz")
255 elif "espeak" in sys.argv:
256 espeakVoice = "mb-cz2"
257 espeakspeed = "150"
259 else:
260 print("which language?")
261 exit()
263 if "csv" in sys.argv:
264 with open("%s-%s.csv" % (voice, board), "w") as csvFile:
265 for s, f in systemSounds:
266 if s and f:
267 l = u""
268 if board in ("sky9x", "taranis"):
269 l += u"SOUNDS/%s/SYSTEM;" % directory
270 l += f + u";" + s + u"\n"
271 csvFile.write(l.encode("utf-8"))
272 for s, f in sounds:
273 if s and f:
274 l = u""
275 if board in ("sky9x", "taranis"):
276 l += u"SOUNDS/%s;" % directory
277 l += f + u";" + s + u"\n"
278 csvFile.write(l.encode("utf-8"))
280 if "zip" in sys.argv:
281 zip_name = "%s-%s.zip" % (voice, board)
282 with zipfile.ZipFile(zip_name, "w", zipfile.ZIP_DEFLATED) as zip:
283 for s, f in systemSounds:
284 if s and f:
285 generate(s, f)
286 if board in ("sky9x", "taranis"):
287 zip.write(f, "SOUNDS/%s/SYSTEM/" % directory + f)
288 else:
289 zip.write(f, f)
290 os.remove(f)
291 for s, f in sounds:
292 if s and f:
293 generate(s, f)
294 if board in ("sky9x", "taranis"):
295 zip.write(f, "SOUNDS/%s/" % directory + f)
296 else:
297 zip.write(f, f)
298 os.remove(f)