forked from sugarlabs/read-activity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech_dispatcher.py
124 lines (101 loc) · 3.58 KB
/
speech_dispatcher.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
# Copyright (C) 2008 James D. Simmons
#
# 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
from gi.repository import Gdk
import time
import threading
import speechd
import logging
import speech
_logger = logging.getLogger('read-etexts-activity')
done = True
def voices():
try:
client = speechd.SSIPClient('readetextstest')
voices = client.list_synthesis_voices()
client.close()
return voices
except Exception, e:
_logger.warning('speech dispatcher not started: %s' % e)
return []
def say(words):
try:
client = speechd.SSIPClient('readetextstest')
client.set_rate(int(speech.rate))
client.set_pitch(int(speech.pitch))
client.set_language(speech.voice[1])
client.speak(words)
client.close()
except Exception, e:
_logger.warning('speech dispatcher not running: %s' % e)
def is_stopped():
return done
def pause():
pass
def stop():
global done
done = True
def play(words):
global thread
thread = EspeakThread(words)
thread.start()
class EspeakThread(threading.Thread):
def __init__(self, words):
threading.Thread.__init__(self)
self.words = words
def run(self):
"This is the code that is executed when the start() method is called"
self.client = None
try:
self.client = speechd.SSIPClient('readetexts')
self.client._conn.send_command('SET', speechd.Scope.SELF,
'SSML_MODE', "ON")
if speech.voice:
self.client.set_language(speech.voice[1])
self.client.set_rate(speech.rate)
self.client.set_pitch(speech.pitch)
self.client.speak(self.words, self.next_word_cb,
(speechd.CallbackType.INDEX_MARK,
speechd.CallbackType.END))
global done
done = False
while not done:
time.sleep(0.1)
self.cancel()
self.client.close()
except Exception, e:
_logger.warning('speech-dispatcher client not created: %s' % e)
def cancel(self):
if self.client:
try:
self.client.cancel()
except Exception, e:
_logger.warning('speech dispatcher cancel failed: %s' % e)
def next_word_cb(self, type, **kargs):
if type == speechd.CallbackType.INDEX_MARK:
mark = kargs['index_mark']
word_count = int(mark)
Gdk.threads_enter()
speech.highlight_cb(word_count)
Gdk.threads_leave()
elif type == speechd.CallbackType.END:
Gdk.threads_enter()
if speech.reset_cb is not None:
speech.reset_cb()
if speech.reset_buttons_cb is not None:
speech.reset_buttons_cb()
Gdk.threads_leave()
global done
done = True