1 |
|
# Orca |
2 |
|
# |
3 |
|
# Copyright 2005-2007 Sun Microsystems Inc. |
4 |
|
# |
5 |
|
# This library is free software; you can redistribute it and/or |
6 |
|
# modify it under the terms of the GNU Library General Public |
7 |
|
# License as published by the Free Software Foundation; either |
8 |
|
# version 2 of the License, or (at your option) any later version. |
9 |
|
# |
10 |
|
# This library is distributed in the hope that it will be useful, |
11 |
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 |
|
# Library General Public License for more details. |
14 |
|
# |
15 |
|
# You should have received a copy of the GNU Library General Public |
16 |
|
# License along with this library; if not, write to the |
17 |
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 |
|
# Boston, MA 02111-1307, USA. |
19 |
|
|
20 |
|
"""Provides an abtract class for working with speech servers. |
21 |
|
|
22 |
|
A speech server (class SpeechServer) provides the ability to tell the |
23 |
|
machine to speak. Each speech server provides a set of known |
24 |
|
voices (identified by name) which can be combined with various |
25 |
1 |
attributes to create aural style sheets.""" |
26 |
|
|
27 |
1 |
__id__ = "$Id: speechserver.py 2548 2007-07-21 21:46:43Z wwalker $" |
28 |
1 |
__version__ = "$Revision: 2548 $" |
29 |
1 |
__date__ = "$Date: 2007-07-21 17:46:43 -0400 (Sat, 21 Jul 2007) $" |
30 |
1 |
__copyright__ = "Copyright (c) 2005-2007 Sun Microsystems Inc." |
31 |
1 |
__license__ = "LGPL" |
32 |
|
|
33 |
1 |
import logging |
34 |
1 |
import keynames |
35 |
1 |
import settings |
36 |
1 |
import orca |
37 |
1 |
log = logging.getLogger("speech") |
38 |
|
|
39 |
1 |
import debug |
40 |
|
|
41 |
1 |
from orca_i18n import _ # for gettext support |
42 |
|
|
43 |
2 |
class VoiceFamily(dict): |
44 |
1 |
"""Holds the family description for a voice.""" |
45 |
|
|
46 |
1 |
NAME = "name" |
47 |
1 |
GENDER = "gender" |
48 |
1 |
LOCALE = "locale" |
49 |
|
|
50 |
1 |
MALE = "male" |
51 |
1 |
FEMALE = "female" |
52 |
|
|
53 |
1 |
settings = { |
54 |
1 |
NAME : None, |
55 |
1 |
GENDER : None, |
56 |
1 |
LOCALE : None |
57 |
|
} |
58 |
|
|
59 |
1 |
def __init__(self, props): |
60 |
|
"""Create and initialize VoiceFamily.""" |
61 |
0 |
self.update(VoiceFamily.settings) |
62 |
0 |
if props: |
63 |
0 |
self.update(props) |
64 |
|
|
65 |
2 |
class SayAllContext: |
66 |
|
|
67 |
1 |
PROGRESS = 0 |
68 |
1 |
INTERRUPTED = 1 |
69 |
1 |
COMPLETED = 2 |
70 |
|
|
71 |
1 |
def __init__(self, obj, utterance, startOffset=-1, endOffset=-1): |
72 |
|
"""Creates a new SayAllContext that will be passed to the |
73 |
|
SayAll callback handler for progress updates on speech. |
74 |
|
If the object does not have an accessible text specialization, |
75 |
|
then startOffset and endOffset parameters are meaningless. |
76 |
|
If the object does have an accessible text specialization, |
77 |
|
then values >= 0 for startOffset and endOffset indicate |
78 |
|
where in the text the utterance has come from. |
79 |
|
|
80 |
|
Arguments: |
81 |
|
-obj: the Accessible being spoken |
82 |
|
-utterance: the actual utterance being spoken |
83 |
|
-startOffset: the start offset of the Accessible's text |
84 |
|
-endOffset: the end offset of the Accessible's text |
85 |
|
""" |
86 |
3 |
self.obj = obj |
87 |
3 |
self.utterance = utterance |
88 |
3 |
self.startOffset = startOffset |
89 |
3 |
self.currentOffset = startOffset |
90 |
3 |
self.endOffset = endOffset |
91 |
|
|
92 |
|
|
93 |
2 |
class SpeechServer(object): |
94 |
|
|
95 |
1 |
"""Provides speech server abstraction.""" |
96 |
|
|
97 |
1 |
def getFactoryName(): |
98 |
|
"""Returns a localized name describing this factory.""" |
99 |
0 |
pass |
100 |
|
|
101 |
1 |
getFactoryName = staticmethod(getFactoryName) |
102 |
|
|
103 |
1 |
def getSpeechServers(): |
104 |
|
"""Gets available speech servers as a list. The caller |
105 |
|
is responsible for calling the shutdown() method of each |
106 |
|
speech server returned. |
107 |
|
""" |
108 |
0 |
pass |
109 |
|
|
110 |
1 |
getSpeechServers = staticmethod(getSpeechServers) |
111 |
|
|
112 |
1 |
def getSpeechServer(info): |
113 |
|
"""Gets a given SpeechServer based upon the info. |
114 |
|
See SpeechServer.getInfo() for more info. |
115 |
|
""" |
116 |
0 |
pass |
117 |
|
|
118 |
1 |
getSpeechServer = staticmethod(getSpeechServer) |
119 |
|
|
120 |
1 |
def shutdownActiveServers(): |
121 |
|
"""Cleans up and shuts down this factory. |
122 |
|
""" |
123 |
0 |
pass |
124 |
|
|
125 |
1 |
shutdownActiveServers = staticmethod(shutdownActiveServers) |
126 |
|
|
127 |
1 |
def __init__(self): |
128 |
0 |
pass |
129 |
|
|
130 |
1 |
def getInfo(self): |
131 |
|
"""Returns [name, id] |
132 |
|
""" |
133 |
0 |
pass |
134 |
|
|
135 |
1 |
def getVoiceFamilies(self): |
136 |
|
"""Returns a list of VoiceFamily instances representing all |
137 |
|
voice families known by the speech server.""" |
138 |
0 |
pass |
139 |
|
|
140 |
1 |
def queueText(self, text="", acss=None): |
141 |
|
"""Adds the text to the queue. |
142 |
|
|
143 |
|
Arguments: |
144 |
|
- text: text to be spoken |
145 |
|
- acss: acss.ACSS instance; if None, |
146 |
|
the default voice settings will be used. |
147 |
|
Otherwise, the acss settings will be |
148 |
|
used to augment/override the default |
149 |
|
voice settings. |
150 |
|
|
151 |
|
Output is produced by the next call to speak. |
152 |
|
""" |
153 |
0 |
pass |
154 |
|
|
155 |
1 |
def queueTone(self, pitch=440, duration=50): |
156 |
|
"""Adds a tone to the queue. |
157 |
|
|
158 |
|
Output is produced by the next call to speak. |
159 |
|
""" |
160 |
0 |
pass |
161 |
|
|
162 |
1 |
def queueSilence(self, duration=50): |
163 |
|
"""Adds silence to the queue. |
164 |
|
|
165 |
|
Output is produced by the next call to speak. |
166 |
|
""" |
167 |
0 |
pass |
168 |
|
|
169 |
1 |
def speakCharacter(self, character, acss=None): |
170 |
|
"""Speaks a single character immediately. |
171 |
|
|
172 |
|
Arguments: |
173 |
|
- character: text to be spoken |
174 |
|
- acss: acss.ACSS instance; if None, |
175 |
|
the default voice settings will be used. |
176 |
|
Otherwise, the acss settings will be |
177 |
|
used to augment/override the default |
178 |
|
voice settings. |
179 |
|
""" |
180 |
0 |
pass |
181 |
|
|
182 |
1 |
def speakKeyEvent(self, event_string, type): |
183 |
|
"""Speaks a key event immediately. |
184 |
|
|
185 |
|
Arguments: |
186 |
|
- event_string: string representing the key event as defined by |
187 |
|
input_event.KeyboardEvent. |
188 |
|
- type: key event type as one of orca.KeyEventType constants. |
189 |
|
|
190 |
|
""" |
191 |
0 |
if type == orca.KeyEventType.PRINTABLE and \ |
192 |
0 |
event_string.decode("UTF-8").isupper(): |
193 |
0 |
voice = settings.voices[settings.UPPERCASE_VOICE] |
194 |
|
else: |
195 |
0 |
voice = settings.voices[settings.DEFAULT_VOICE] |
196 |
|
|
197 |
|
# Check to see if there are localized words to be spoken for |
198 |
|
# this key event. |
199 |
|
# |
200 |
0 |
event_string = keynames.getKeyName(event_string) |
201 |
|
|
202 |
0 |
if type == orca.KeyEventType.LOCKING_LOCKED: |
203 |
|
# Translators: this represents the state of a locking modifier |
204 |
|
# key (e.g., Caps Lock) |
205 |
|
# |
206 |
0 |
event_string += " " + _("on") |
207 |
0 |
elif type == orca.KeyEventType.LOCKING_UNLOCKED: |
208 |
|
# Translators: this represents the state of a locking modifier |
209 |
|
# key (e.g., Caps Lock) |
210 |
|
# |
211 |
0 |
event_string += " " + _("off") |
212 |
|
|
213 |
0 |
debug.println(debug.LEVEL_INFO, "SPEECH OUTPUT: '" + event_string +"'") |
214 |
0 |
log.info("speakKeyEvent utterance='%s'" % event_string) |
215 |
|
|
216 |
0 |
self.speak(event_string, acss=voice) |
217 |
|
|
218 |
1 |
def speakUtterances(self, list, acss=None, interrupt=True): |
219 |
|
"""Speaks the given list of utterances immediately. |
220 |
|
|
221 |
|
Arguments: |
222 |
|
- list: list of strings to be spoken |
223 |
|
- acss: acss.ACSS instance; if None, |
224 |
|
the default voice settings will be used. |
225 |
|
Otherwise, the acss settings will be |
226 |
|
used to augment/override the default |
227 |
|
voice settings. |
228 |
|
- interrupt: if True, stop any speech currently in progress. |
229 |
|
""" |
230 |
0 |
pass |
231 |
|
|
232 |
1 |
def speak(self, text=None, acss=None, interrupt=True): |
233 |
|
"""Speaks all queued text immediately. If text is not None, |
234 |
|
it is added to the queue before speaking. |
235 |
|
|
236 |
|
Arguments: |
237 |
|
- text: optional text to add to the queue before speaking |
238 |
|
- acss: acss.ACSS instance; if None, |
239 |
|
the default voice settings will be used. |
240 |
|
Otherwise, the acss settings will be |
241 |
|
used to augment/override the default |
242 |
|
voice settings. |
243 |
|
- interrupt: if True, stops any speech in progress before |
244 |
|
speaking the text |
245 |
|
""" |
246 |
0 |
pass |
247 |
|
|
248 |
1 |
def isSpeaking(self): |
249 |
|
""""Returns True if the system is currently speaking.""" |
250 |
0 |
return False |
251 |
|
|
252 |
1 |
def sayAll(self, utteranceIterator, progressCallback): |
253 |
|
"""Iterates through the given utteranceIterator, speaking |
254 |
|
each utterance one at a time. Subclasses may postpone |
255 |
|
getting a new element until the current element has been |
256 |
|
spoken. |
257 |
|
|
258 |
|
Arguments: |
259 |
|
- utteranceIterator: iterator/generator whose next() function |
260 |
|
returns a [SayAllContext, acss] tuple |
261 |
|
- progressCallback: called as speech progress is made - has a |
262 |
|
signature of (SayAllContext, type), where |
263 |
|
type is one of PROGRESS, INTERRUPTED, or |
264 |
|
COMPLETED. |
265 |
|
""" |
266 |
0 |
for [context, acss] in utteranceIterator: |
267 |
0 |
debug.println(debug.LEVEL_INFO, |
268 |
0 |
"SPEECH OUTPUT: '" + context.utterance + "'") |
269 |
0 |
log.info("sayAll utterance='%s'" % context.utterance) |
270 |
0 |
self.speak(context.utterance, acss) |
271 |
|
|
272 |
1 |
def increaseSpeechRate(self, step=5): |
273 |
|
"""Increases the speech rate. |
274 |
|
""" |
275 |
0 |
pass |
276 |
|
|
277 |
1 |
def decreaseSpeechRate(self, step=5): |
278 |
|
"""Decreases the speech rate. |
279 |
|
""" |
280 |
0 |
pass |
281 |
|
|
282 |
1 |
def increaseSpeechPitch(self, step=0.5): |
283 |
|
"""Increases the speech pitch. |
284 |
|
""" |
285 |
0 |
pass |
286 |
|
|
287 |
1 |
def decreaseSpeechPitch(self, step=0.5): |
288 |
|
"""Decreases the speech pitch. |
289 |
|
""" |
290 |
0 |
pass |
291 |
|
|
292 |
1 |
def stop(self): |
293 |
|
"""Stops ongoing speech and flushes the queue.""" |
294 |
0 |
pass |
295 |
|
|
296 |
1 |
def shutdown(self): |
297 |
|
"""Shuts down the speech engine.""" |
298 |
0 |
pass |
299 |
|
|
300 |
1 |
def reset(self, text=None, acss=None): |
301 |
|
"""Resets the speech engine.""" |
302 |
0 |
pass |