-
Notifications
You must be signed in to change notification settings - Fork 4
/
speechSynthesis.py
181 lines (135 loc) · 5.21 KB
/
speechSynthesis.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
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3
# adapted from https://github.com/MycroftAI/mimic3/blob/be72c18/mimic3_tts/__main__.py
import argparse
import queue
import signal
import shlex
import shutil
import subprocess
import tempfile
import threading
import time
import mimic3_tts
import yarp
import roboticslab_speech
PLAY_PROGRAMS = ['paplay', 'play -q', 'aplay -q']
class TextToSpeechResponder(roboticslab_speech.TextToSpeechIDL):
def __init__(self, engine):
super().__init__()
self.engine = engine
self.is_playing = False
self.p = None
self.result_queue = queue.Queue(maxsize=5)
self.result_thread = threading.Thread(target=self._process_result, daemon=True)
self.result_thread.start()
def setLanguage(self, language):
if language.startswith('#'):
# same voice, different speaker
self.engine.speaker = language[1:]
else:
# different voice
self.engine.voice = language
if self.engine.voice not in list(self.getSupportedLangs()):
print('Voice not available: %s' % self.engine.voice)
return False
else:
self.engine.preload_voice(self.engine.voice)
print('Loaded voice: %s (speaker: %s)' % (self.engine.voice, self.engine.speaker or 'default'))
return True
def setSpeed(self, speed):
self.engine.rate = float(speed) / 100
return True
def setPitch(self, pitch):
return super().setPitch(pitch)
def getSpeed(self):
return int(self.engine.rate * 100)
def getPitch(self):
return super().getPitch()
def getSupportedLangs(self):
all_voices = sorted(list(self.engine.get_voices()), key=lambda v: v.key)
local_voices = filter(lambda v: not v.location.startswith('http'), all_voices)
available_voices = [v.key for v in local_voices]
return yarp.SVector(available_voices)
def say(self, text):
self.engine.begin_utterance()
self.engine.speak_text(text)
for result in self.engine.end_utterance():
self.result_queue.put(result)
return True
def play(self):
return super().play()
def pause(self):
return super().pause()
def stop(self):
if self.p:
self.p.terminate()
return True
def checkSayDone(self):
return not self.is_playing
def _process_result(self):
while True:
result = self.result_queue.get()
if result is None:
break
wav_bytes = result.to_wav_bytes()
if not wav_bytes:
continue
with tempfile.NamedTemporaryFile(mode='wb+', suffix='.wav') as wav_file:
wav_file.write(wav_bytes)
wav_file.seek(0)
for play_program in reversed(PLAY_PROGRAMS):
play_cmd = shlex.split(play_program)
if not shutil.which(play_cmd[0]):
continue
play_cmd.append(wav_file.name)
self.is_playing = True
with subprocess.Popen(play_cmd) as self.p:
try:
self.p.wait()
except: # e.g. on keyboard interrupt
self.p.kill()
self.is_playing = False
break
parser = argparse.ArgumentParser(prog='speechSynthesis', description='TTS service running a Mimic 3 engine')
parser.add_argument('--voice', '-v', help='Name of voice (expected in <voices-dir>/<language>)', required=True)
parser.add_argument('--speaker', '-s', help='Name or number of speaker (default: first speaker)')
parser.add_argument('--noise-scale', type=float, help='Noise scale [0-1], default is 0.667')
parser.add_argument('--length-scale', type=float, help='Length scale (1.0 is default speed, 0.5 is 2x faster)')
parser.add_argument('--noise-w', type=float, help='Variation in cadence [0-1], default is 0.8')
parser.add_argument('--cuda', action='store_true', help='Use Onnx CUDA execution provider (requires onnxruntime-gpu)')
parser.add_argument('--port', '-p', default='/speechSynthesis', help='YARP port prefix')
args = parser.parse_args()
tts = mimic3_tts.Mimic3TextToSpeechSystem(
mimic3_tts.Mimic3Settings(
length_scale=args.length_scale,
noise_scale=args.noise_scale,
noise_w=args.noise_w,
use_cuda=args.cuda,
)
)
tts.voice = args.voice
tts.speaker = args.speaker
print('Preloading voice: %s' % args.voice)
tts.preload_voice(args.voice)
yarp.Network.init()
if not yarp.Network.checkNetwork():
print('YARP network not found')
raise SystemExit
rpc = yarp.RpcServer()
processor = TextToSpeechResponder(tts)
if not rpc.open(args.port + '/rpc:s'):
print('Cannot open port %s' % rpc.getName())
raise SystemExit
processor.yarp().attachAsServer(rpc)
quitRequested = False
def askToStop():
global quitRequested
quitRequested = True
signal.signal(signal.SIGINT, lambda signal, frame: askToStop())
signal.signal(signal.SIGTERM, lambda signal, frame: askToStop())
while not quitRequested:
time.sleep(0.1)
rpc.interrupt()
rpc.close()
processor.result_queue.put(None)
processor.result_thread.join()