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/
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)
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
35 from tts_common
import *
39 from http
.client
import HTTPConnection
40 from urllib
.parse
import urlencode
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()
51 os
.rename(output
, filename
)
54 def generate(str, filename
):
58 str = " !" # this is so blank wav files never exist!
60 if "speak" in sys
.argv
:
61 if "sapi" in sys
.argv
:
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"
69 print("which speach engine?")
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
:
92 subprocess
.Popen(["ffmpeg", "-y", "-i", ttsmp3
, "-acodec", "pcm_s16le", ttsfilename
], stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
).wait()
96 print("which speach engine?")
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]
107 subprocess
.Popen(["sox", "--show-progress", "-v", maxvolume
, filename
, ttsfilename
], stdout
=subprocess
.PIPE
).communicate()[0]
109 subprocess
.Popen(["sox", "-twav", ttsfilename
, "-b1600", "-c1", "-e", "a-law", filename
], stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
).wait()
111 subprocess
.Popen(["sox", "-twav", ttsfilename
, "-b32000", "-c1", "-e", "a-law", filename
], stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
).wait()
114 subprocess
.Popen(["ffmpeg", "-y", "-i", ttsfilename
, "-acodec", defaultcodec
, "-ar", "16000", filename
], stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
).wait()
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
)
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
:
133 # tts.SetOutputFormat(16, 16, 1)
136 if "list" in sys
.argv
:
137 print(tts
.GetVoiceNames())
140 if "mulaw" in sys
.argv
:
141 defaultcodec
= "pcm_mulaw"
143 defaultcodec
= "pcm_alaw"
146 from tts_en
import systemSounds
, sounds
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"
164 tts
.SetVoiceByName("ScanSoftFiona_Full_22kHz")
165 voice
= "english-english"
166 elif "sapi2" in sys
.argv
:
167 if "scottish" in sys
.argv
:
169 voice
= "english-scottish"
170 elif "american" in sys
.argv
:
172 voice
= "english-american"
173 elif "australian" in sys
.argv
:
175 voice
= "english-australian"
176 elif "irish" in sys
.argv
:
178 voice
= "english-irish"
179 elif "french" in sys
.argv
:
181 voice
= "english-french"
182 elif "german" in sys
.argv
:
184 voice
= "english-german"
187 voice
= "english-english"
189 elif "espeak" in sys
.argv
:
190 espeakVoice
= "mb-us1"
193 elif "fr" in sys
.argv
:
194 from tts_fr
import systemSounds
, sounds
198 if "sapi" in sys
.argv
:
199 tts
.SetVoiceByName("ScanSoftVirginie_Full_22kHz")
200 elif "espeak" in sys
.argv
:
201 espeakVoice
= "mb-fr4+f4"
204 elif "it" in sys
.argv
:
205 from tts_it
import systemSounds
, sounds
209 if "sapi" in sys
.argv
:
210 tts
.SetVoiceByName("ScanSoftVirginie_Full_22kHz")
211 elif "espeak" in sys
.argv
:
212 espeakVoice
= "mb-it4"
215 elif "de" in sys
.argv
:
216 from tts_de
import systemSounds
, sounds
220 if "sapi" in sys
.argv
:
221 tts
.SetVoiceByName("ScanSoftVirginie_Full_22kHz")
222 elif "espeak" in sys
.argv
:
223 espeakVoice
= "mb-de4"
226 elif "pt" in sys
.argv
:
227 from tts_pt
import systemSounds
, sounds
231 if "sapi" in sys
.argv
:
232 tts
.SetVoiceByName("ScanSoftVirginie_Full_22kHz")
233 elif "espeak" in sys
.argv
:
234 espeakVoice
= "mb-pt1+f1"
237 elif "es" in sys
.argv
:
238 from tts_es
import systemSounds
, sounds
242 if "sapi" in sys
.argv
:
243 tts
.SetVoiceByName("ScanSoftVirginie_Full_22kHz")
244 elif "espeak" in sys
.argv
:
245 espeakVoice
= "mb-es1+f1"
248 elif "cz" in sys
.argv
:
249 from tts_cz
import systemSounds
, sounds
253 if "sapi" in sys
.argv
:
254 tts
.SetVoiceByName("ScanSoftZuzana_Full_22kHz")
255 elif "espeak" in sys
.argv
:
256 espeakVoice
= "mb-cz2"
260 print("which language?")
263 if "csv" in sys
.argv
:
264 with
open("%s-%s.csv" % (voice
, board
), "w") as csvFile
:
265 for s
, f
in systemSounds
:
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"))
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
:
286 if board
in ("sky9x", "taranis"):
287 zip.write(f
, "SOUNDS/%s/SYSTEM/" % directory
+ f
)
294 if board
in ("sky9x", "taranis"):
295 zip.write(f
, "SOUNDS/%s/" % directory
+ f
)