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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
# -*- coding: utf-8-*-
"""
A Speaker handles audio output from Jasper to the user
Speaker methods:
say - output 'phrase' as speech
play - play the audio in 'filename'
is_available - returns True if the platform supports this implementation
"""
import os
import platform
import re
import sys
import tempfile
import subprocess
import pipes
import logging
from abc import ABCMeta, abstractmethod
from distutils.spawn import find_executable
import yaml
import argparse
import pyaudio
import wave
try:
import mad
import gtts
except ImportError:
pass
class AbstractSpeaker(object):
"""
Generic parent class for all speakers
"""
__metaclass__ = ABCMeta
@classmethod
@abstractmethod
def is_available(cls):
return True
def __init__(self):
self._logger = logging.getLogger(__name__)
@abstractmethod
def say(self, phrase, *args):
pass
def play(self, filename):
# FIXME: Use platform-independent audio-output here
# See issue jasperproject/jasper-client#188
cmd = ['aplay', str(filename)]
self._logger.debug('Executing %s', ' '.join([pipes.quote(arg) for arg in cmd]))
with tempfile.TemporaryFile() as f:
subprocess.call(cmd, stdout=f, stderr=f)
f.seek(0)
output = f.read()
if output:
self._logger.debug("Output was: '%s'", output)
class AbstractMp3Speaker(AbstractSpeaker):
"""
Generic class that implements the 'play' method for mp3 files
"""
@classmethod
def is_available(cls):
return (super(AbstractMp3Speaker, cls).is_available() and 'mad' in sys.modules.keys())
def play_mp3(self, filename):
mf = mad.MadFile(filename)
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
wav = wave.open(f, mode='wb')
wav.setframerate(mf.samplerate())
wav.setnchannels(1 if mf.mode() == mad.MODE_SINGLE_CHANNEL else 2)
wav.setsampwidth(pyaudio.get_sample_size(pyaudio.paInt32))
frame = mf.read()
while frame is not None:
wav.writeframes(frame)
frame = mf.read()
wav.close()
self.play(f.name)
class eSpeakSpeaker(AbstractSpeaker):
"""
Uses the eSpeak speech synthesizer included in the Jasper disk image
Requires espeak to be available
"""
SLUG = "espeak-tts"
@classmethod
def is_available(cls):
return (super(eSpeakSpeaker, cls).is_available() and find_executable('espeak') is not None)
def say(self, phrase, voice='default+m3', pitch_adjustment=40, words_per_minute=160):
self._logger.debug("Saying '%s' with '%s'", phrase, self.SLUG)
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
fname = f.name
cmd = ['espeak', '-v', voice,
'-p', pitch_adjustment,
'-s', words_per_minute,
'-w', fname,
phrase]
cmd = [str(x) for x in cmd]
self._logger.debug('Executing %s', ' '.join([pipes.quote(arg) for arg in cmd]))
with tempfile.TemporaryFile() as f:
subprocess.call(cmd, stdout=f, stderr=f)
f.seek(0)
output = f.read()
if output:
self._logger.debug("Output was: '%s'", output)
self.play(fname)
os.remove(fname)
class saySpeaker(AbstractSpeaker):
"""
Uses the OS X built-in 'say' command
"""
SLUG = "osx-tts"
@classmethod
def is_available(cls):
return (platform.system() == 'darwin' and find_executable('say') is not None and find_executable('afplay') is not None)
def say(self, phrase):
self._logger.debug("Saying '%s' with '%s'", phrase, self.SLUG)
cmd = ['say', str(phrase)]
self._logger.debug('Executing %s', ' '.join([pipes.quote(arg) for arg in cmd]))
with tempfile.TemporaryFile() as f:
subprocess.call(cmd, stdout=f, stderr=f)
f.seek(0)
output = f.read()
if output:
self._logger.debug("Output was: '%s'", output)
def play(self, filename):
cmd = ['afplay', str(filename)]
self._logger.debug('Executing %s', ' '.join([pipes.quote(arg) for arg in cmd]))
with tempfile.TemporaryFile() as f:
subprocess.call(cmd, stdout=f, stderr=f)
f.seek(0)
output = f.read()
if output:
self._logger.debug("Output was: '%s'", output)
class picoSpeaker(AbstractSpeaker):
"""
Uses the svox-pico-tts speech synthesizer
Requires pico2wave to be available
"""
SLUG = "pico-tts"
@classmethod
def is_available(cls):
return (super(picoSpeaker, cls).is_available() and find_executable('pico2wave') is not None)
@property
def languages(self):
cmd = ['pico2wave', '-l', 'NULL',
'-w', os.devnull,
'NULL']
with tempfile.SpooledTemporaryFile() as f:
subprocess.call(cmd, stderr=f)
f.seek(0)
output = f.read()
pattern = re.compile(r'Unknown language: NULL\nValid languages:\n((?:[a-z]{2}-[A-Z]{2}\n)+)')
matchobj = pattern.match(output)
if not matchobj:
raise RuntimeError("pico2wave: valid languages not detected")
langs = matchobj.group(1).split()
return langs
def say(self, phrase, language="en-US"):
self._logger.debug("Saying '%s' with '%s'", phrase, self.SLUG)
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
fname = f.name
cmd = ['pico2wave', '--wave', fname]
if language:
if language not in self.languages:
raise ValueError("Language '%s' not supported by '%s'", language, self.SLUG)
cmd.extend(['-l',language])
cmd.append(phrase)
self._logger.debug('Executing %s', ' '.join([pipes.quote(arg) for arg in cmd]))
with tempfile.TemporaryFile() as f:
subprocess.call(cmd, stdout=f, stderr=f)
f.seek(0)
output = f.read()
if output:
self._logger.debug("Output was: '%s'", output)
self.play(fname)
os.remove(fname)
class googleSpeaker(AbstractMp3Speaker):
"""
Uses the Google TTS online translator
Requires pymad and gTTS to be available
"""
SLUG = "google-tts"
@classmethod
def is_available(cls):
return (super(googleSpeaker, cls).is_available() and 'gtts' in sys.modules.keys())
@property
def languages(self):
langs = ['af', 'sq', 'ar', 'hy', 'ca', 'zh-CN', 'zh-TW', 'hr', 'cs', 'da', 'nl', 'en', 'eo', 'fi', 'fr', 'de',
'el', 'ht', 'hi', 'hu', 'is', 'id', 'it', 'ja', 'ko', 'la', 'lv', 'mk', 'no', 'pl', 'pt', 'ro', 'ru',
'sr', 'sk', 'es', 'sw', 'sv', 'ta', 'th', 'tr', 'vi', 'cy']
return langs
def say(self, phrase, language='en'):
self._logger.debug("Saying '%s' with '%s'", phrase, self.SLUG)
if language not in self.languages:
raise ValueError("Language '%s' not supported by '%s'", language, self.SLUG)
tts = gtts.gTTS(text=phrase, lang=language)
with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as f:
tmpfile = f.name
tts.save(tmpfile)
self.play_mp3(tmpfile)
os.remove(tmpfile)
TTS_ENGINES = [googleSpeaker, picoSpeaker, eSpeakSpeaker, saySpeaker]
def newSpeaker():
"""
Returns:
A speaker implementation available on the current platform
Raises:
ValueError if no speaker implementation is supported on this platform
"""
tts_engine = None
# Try to get tts_engine from config
if os.path.exists('profile.yml'):
with open('profile.yml', 'r') as f:
profile = yaml.safe_load(f)
if 'tts_engine' in profile:
tts_engine = profile['tts_engine']
# Default values if config file does not exist or option not set
if not tts_engine:
if platform.system() == 'darwin':
tts_engine = 'osx-tts'
else:
tts_engine = 'espeak-tts'
selected_engines = filter(lambda engine: hasattr(engine, "SLUG") and engine.SLUG == tts_engine, TTS_ENGINES)
if len(selected_engines) == 0:
raise ValueError("No TTS engine found for slug '%s'" % tts_engine)
else:
if len(selected_engines) > 1:
print("WARNING: Multiple TTS engines found for slug '%s'. This is most certainly a bug." % tts_engine)
engine = selected_engines[0]
if not engine.is_available():
raise ValueError("TTS engine '%s' is not available (due to missing dependencies, missing dependencies, etc.)" % tts_engine)
return engine()
def get_engines():
def get_subclasses(cls):
subclasses = set()
for subclass in cls.__subclasses__():
subclasses.add(subclass)
subclasses.update(get_subclasses(subclass))
return subclasses
return [tts_engine for tts_engine in list(get_subclasses(AbstractSpeaker)) if hasattr(tts_engine, 'SLUG') and tts_engine.SLUG]
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Jasper TTS module')
parser.add_argument('--debug', action='store_true', help='Show debug messages')
args = parser.parse_args()
logging.basicConfig()
if args.debug:
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
engines = get_engines()
available_engines = []
for engine in get_engines():
if engine.is_available():
available_engines.append(engine)
print("Available TTS engines:")
for i, engine in enumerate(available_engines, start=1):
print("%d. %s" % (i, engine.SLUG))
print("")
print("Disabled TTS engines:")
for i, engine in enumerate(list(set(engines).difference(set(available_engines))), start=1):
print("%d. %s" % (i, engine.SLUG))
print("")
for i, engine in enumerate(available_engines, start=1):
print("%d. Testing engine '%s'..." % (i, engine.SLUG))
engine().say("This is a test.")
print("Done.")
|