-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathspeech.py
201 lines (153 loc) · 5.94 KB
/
speech.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Copyright (C) 2009, Aleksey Lim
# Copyright (C) 2019, Chihurumnaya Ibiam <ibiamchihurumnaya@sugarlabs.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import numpy
from gi.repository import Gst
from gi.repository import GLib
from gi.repository import GObject
import logging
logger = logging.getLogger('speak')
from sugar3.speech import GstSpeechPlayer
PITCH_MIN = 0
PITCH_MAX = 200
RATE_MIN = 0
RATE_MAX = 200
class Speech(GstSpeechPlayer):
__gsignals__ = {
'peak': (GObject.SIGNAL_RUN_FIRST, None, [GObject.TYPE_PYOBJECT]),
'wave': (GObject.SIGNAL_RUN_FIRST, None, [GObject.TYPE_PYOBJECT]),
'idle': (GObject.SIGNAL_RUN_FIRST, None, []),
}
def __init__(self):
GstSpeechPlayer.__init__(self)
self.pipeline = None
self._cb = {}
for cb in ['peak', 'wave', 'idle']:
self._cb[cb] = None
def disconnect_all(self):
for cb in ['peak', 'wave', 'idle']:
hid = self._cb[cb]
if hid is not None:
self.disconnect(hid)
self._cb[cb] = None
def connect_peak(self, cb):
self._cb['peak'] = self.connect('peak', cb)
def connect_wave(self, cb):
self._cb['wave'] = self.connect('wave', cb)
def connect_idle(self, cb):
self._cb['idle'] = self.connect('idle', cb)
def make_pipeline(self):
if self.pipeline is not None:
self.stop_sound_device()
del self.pipeline
# build a pipeline that makes speech
# and sends it to both the audio output
# and a fake one that we use to draw from
cmd = 'espeak name=espeak' \
' ! capsfilter name=caps' \
' ! tee name=me' \
' me.! queue ! autoaudiosink name=ears' \
' me.! queue ! fakesink name=sink'
self.pipeline = Gst.parse_launch(cmd)
# force a sample bit width to match our numpy code below
caps = self.pipeline.get_by_name('caps')
want = 'audio/x-raw,channels=(int)1,depth=(int)16'
caps.set_property('caps', Gst.caps_from_string(want))
# grab reference to the output element for scheduling mouth moves
ears = self.pipeline.get_by_name('ears')
def handoff(element, data, pad):
size = data.get_size()
if size == 0 or data.duration == 0:
return True # common
npc = 50000000 # nanoseconds per chunk
bpc = size * npc // data.duration # bytes per chunk
bpc = bpc // 2 * 2 # force alignment for int16
a = []
p = []
w = []
here = 0 # offset in bytes
when = data.pts
last = data.pts + data.duration
while True:
wave = numpy.fromstring(data.extract_dup(here, bpc), 'int16')
peak = numpy.core.max(wave)
a.append(wave)
p.append(peak)
w.append(when)
here += bpc
when += npc
if when < last:
continue
break
def poke(pts):
success, position = ears.query_position(Gst.Format.TIME)
if not success:
return False
if len(w) == 0:
return False
if position < w[0]:
return True
self.emit("wave", a[0])
self.emit("peak", p[0])
del a[0]
del w[0]
del p[0]
if len(w) > 0:
return True
return False
GLib.timeout_add(25, poke, data.pts)
return True
sink = self.pipeline.get_by_name('sink')
sink.props.signal_handoffs = True
sink.connect('handoff', handoff)
def gst_message_cb(bus, message):
self._was_message = True
if message.type == Gst.MessageType.WARNING:
def check_after_warnings():
if not self._was_message:
self.stop_sound_device()
return True
logger.debug(message.type)
self._was_message = False
GLib.timeout_add(500, check_after_warnings)
elif message.type in (Gst.MessageType.EOS, Gst.MessageType.ERROR):
logger.debug(message.type)
self.stop_sound_device()
return True
self._was_message = False
bus = self.pipeline.get_bus()
bus.add_signal_watch()
bus.connect('message', gst_message_cb)
def speak(self, status, text):
self.make_pipeline()
src = self.pipeline.get_by_name('espeak')
pitch = int(status.pitch) - 100
rate = int(status.rate) - 100
logger.debug('pitch=%d rate=%d voice=%s text=%s' % (pitch, rate,
status.voice.name,
text))
src.props.pitch = pitch
src.props.rate = rate
src.props.voice = status.voice.name
src.props.track = 1
src.props.text = text
self.restart_sound_device()
_speech = None
def get_speech():
global _speech
if _speech is None:
_speech = Speech()
return _speech