2
Dummy driver that produces no output but gives all expected callbacks. Useful
3
for testing and as a model for real drivers.
5
Copyright (c) 2009, 2013 Peter Parente
7
Permission to use, copy, modify, and distribute this software for any
8
purpose with or without fee is hereby granted, provided that the above
9
copyright notice and this permission notice appear in all copies.
11
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
from ..voice import Voice
22
def buildDriver(proxy):
24
Builds a new instance of a driver and returns it for use by the driver
27
@param proxy: Proxy creating the driver
28
@type proxy: L{driver.DriverProxy}
30
return DummyDriver(proxy)
32
class DummyDriver(object):
34
Dummy speech engine implementation. Documents the interface, notifications,
35
properties, and sequencing responsibilities of a driver implementation.
37
@ivar _proxy: Driver proxy that manages this instance
38
@type _proxy: L{driver.DriverProxy}
39
@ivar _config: Dummy configuration
41
@ivar _looping: True when in the dummy event loop, False when not
44
def __init__(self, proxy):
46
Constructs the driver.
48
@param proxy: Proxy creating the driver
49
@type proxy: L{driver.DriverProxy}
53
# hold config values as if we had a real tts implementation that
56
Voice('dummy.voice1', 'John Doe', ['en-US', 'en-GB'], 'male', 'adult'),
57
Voice('dummy.voice2', 'Jane Doe', ['en-US', 'en-GB'], 'female', 'adult'),
58
Voice('dummy.voice3', 'Jimmy Doe', ['en-US', 'en-GB'], 'male', 10)
69
Optional method that will be called when the driver proxy is being
70
destroyed. Can cleanup any resources to make sure the engine terminates
77
Starts a blocking run loop in which driver callbacks are properly
80
@precondition: There was no previous successful call to L{startLoop}
81
without an intervening call to L{stopLoop}.
87
self._proxy.setBusy(False)
93
Stops a previously started run loop.
95
@precondition: A previous call to L{startLoop} suceeded and there was
96
no intervening call to L{endLoop}.
102
Iterates from within an external run loop.
104
self._proxy.setBusy(False)
109
Speaks the given text. Generates the following notifications during
112
started-utterance: When speech output has started
113
started-word: When a word is about to be spoken. Includes the character
114
"location" of the start of the word in the original utterance text
115
and the "length" of the word in characters.
116
finished-utterance: When speech output has finished. Includes a flag
117
indicating if the entire utterance was "completed" or not.
119
The proxy automatically adds any "name" associated with the utterance
120
to the notifications on behalf of the driver.
122
When starting to output an utterance, the driver must inform its proxy
123
that it is busy by invoking L{driver.DriverProxy.setBusy} with a flag
124
of True. When the utterance completes or is interrupted, the driver
125
inform the proxy that it is no longer busy by invoking
126
L{driver.DriverProxy.setBusy} with a flag of False.
128
@param text: Unicode text to speak
131
self._proxy.setBusy(True)
132
self._proxy.notify('started-utterance')
134
for word in text.split(' '):
135
self._proxy.notify('started-word', location=i, length=len(word))
137
i = text.index(' ', i+1)+1
140
self._proxy.notify('finished-utterance', completed=True)
141
self._proxy.setBusy(False)
145
Stops any current output. If an utterance was being spoken, the driver
146
is still responsible for sending the closing finished-utterance
147
notification documented above and resetting the busy state of the
152
def getProperty(self, name):
154
Gets a property value of the speech engine. The suppoted properties
155
and their values are:
157
voices: List of L{voice.Voice} objects supported by the driver
158
voice: String ID of the current voice
159
rate: Integer speech rate in words per minute
160
volume: Floating point volume of speech in the range [0.0, 1.0]
162
@param name: Property name
164
@raise KeyError: When the property name is unknown
167
return self._config[name]
169
raise KeyError('unknown property %s' % name)
171
def setProperty(self, name, value):
173
Sets one of the supported property values of the speech engine listed
174
above. If a value is invalid, attempts to clip it / coerce so it is
175
valid before giving up and firing an exception.
177
@param name: Property name
179
@param value: Property value
181
@raise KeyError: When the property name is unknown
182
@raise ValueError: When the value cannot be coerced to fit the property
185
v = filter(lambda v: v.id == value, self._config['voices'])
186
self._config['voice'] = v[0]
188
self._config['rate'] = value
189
elif name == 'volume':
190
self._config['volume'] = value
192
raise KeyError('unknown property %s' % name)
b'\\ No newline at end of file'