~ubuntu-branches/debian/jessie/openteacher/jessie

« back to all changes in this revision

Viewing changes to openteacher/pyttsx/drivers/dummy.py

  • Committer: Package Import Robot
  • Author(s): Charlie Smotherman, Charlie Smotherman, Jakub Wilk
  • Date: 2013-08-02 18:05:22 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130802180522-nzeghz70d1gu7y0f
Tags: 3.2-1
[ Charlie Smotherman ]
* New upstream release.
* Update packaging to use pybuild.
* Update manpage
* Added debian/clean to exclude courtousy copies of code, and extra license
  files.
* debian/rules
  - added rules to install into usr/lib now that OT builds extensions.
* Update debian/copyright to include new content.
* Removed debian/docs and debian/doc-base not needed any more.
* debian/control
  - added B-D on python-dev and dh-python.
  - changed Architecture from all to any.
  - added ${shlibs:Depends}, libjs-jquery-mobile, ibus-qt4 and espeak to
    Depends.
* Added symlinks for static pics now in /usr/share updated debian/links.
* Added debian/patches/04desktop to correct Version field typo.
* Added debian/patches/05setup_py to split out pic files and place them
  into /usr/share.

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'''
 
2
Dummy driver that produces no output but gives all expected callbacks. Useful
 
3
for testing and as a model for real drivers.
 
4
 
 
5
Copyright (c) 2009, 2013 Peter Parente
 
6
 
 
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.
 
10
 
 
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.
 
18
'''
 
19
from ..voice import Voice
 
20
import time
 
21
 
 
22
def buildDriver(proxy):
 
23
    '''
 
24
    Builds a new instance of a driver and returns it for use by the driver
 
25
    proxy.
 
26
 
 
27
    @param proxy: Proxy creating the driver
 
28
    @type proxy: L{driver.DriverProxy}
 
29
    '''
 
30
    return DummyDriver(proxy)
 
31
 
 
32
class DummyDriver(object):
 
33
    '''
 
34
    Dummy speech engine implementation. Documents the interface, notifications,
 
35
    properties, and sequencing responsibilities of a driver implementation.
 
36
 
 
37
    @ivar _proxy: Driver proxy that manages this instance
 
38
    @type _proxy: L{driver.DriverProxy}
 
39
    @ivar _config: Dummy configuration
 
40
    @type _config: dict
 
41
    @ivar _looping: True when in the dummy event loop, False when not
 
42
    @ivar _looping: bool
 
43
    '''
 
44
    def __init__(self, proxy):
 
45
        '''
 
46
        Constructs the driver.
 
47
 
 
48
        @param proxy: Proxy creating the driver
 
49
        @type proxy: L{driver.DriverProxy}
 
50
        '''
 
51
        self._proxy = proxy
 
52
        self._looping = False
 
53
        # hold config values as if we had a real tts implementation that
 
54
        # supported them
 
55
        voices = [
 
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)
 
59
        ]
 
60
        self._config = {
 
61
            'rate' : 200,
 
62
            'volume' : 1.0,
 
63
            'voice' : voices[0],
 
64
            'voices' : voices
 
65
        }
 
66
 
 
67
    def destroy(self):
 
68
        '''
 
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
 
71
        properly.
 
72
        '''
 
73
        pass
 
74
 
 
75
    def startLoop(self):
 
76
        '''
 
77
        Starts a blocking run loop in which driver callbacks are properly
 
78
        invoked.
 
79
 
 
80
        @precondition: There was no previous successful call to L{startLoop}
 
81
            without an intervening call to L{stopLoop}.
 
82
        '''
 
83
        first = True
 
84
        self._looping = True
 
85
        while self._looping:
 
86
            if first:
 
87
                self._proxy.setBusy(False)
 
88
                first = False
 
89
            time.sleep(0.5)
 
90
 
 
91
    def endLoop(self):
 
92
        '''
 
93
        Stops a previously started run loop.
 
94
 
 
95
        @precondition: A previous call to L{startLoop} suceeded and there was
 
96
            no intervening call to L{endLoop}.
 
97
        '''
 
98
        self._looping = False
 
99
 
 
100
    def iterate(self):
 
101
        '''
 
102
        Iterates from within an external run loop.
 
103
        '''
 
104
        self._proxy.setBusy(False)
 
105
        yield
 
106
 
 
107
    def say(self, text):
 
108
        '''
 
109
        Speaks the given text. Generates the following notifications during
 
110
        output:
 
111
 
 
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.
 
118
 
 
119
        The proxy automatically adds any "name" associated with the utterance
 
120
        to the notifications on behalf of the driver.
 
121
 
 
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.
 
127
 
 
128
        @param text: Unicode text to speak
 
129
        @type text: unicode
 
130
        '''
 
131
        self._proxy.setBusy(True)
 
132
        self._proxy.notify('started-utterance')
 
133
        i = 0
 
134
        for word in text.split(' '):
 
135
            self._proxy.notify('started-word', location=i, length=len(word))
 
136
            try:
 
137
                i = text.index(' ', i+1)+1
 
138
            except Exception:
 
139
                pass
 
140
        self._proxy.notify('finished-utterance', completed=True)
 
141
        self._proxy.setBusy(False)
 
142
 
 
143
    def stop(self):
 
144
        '''
 
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
 
148
        proxy.
 
149
        '''
 
150
        pass
 
151
 
 
152
    def getProperty(self, name):
 
153
        '''
 
154
        Gets a property value of the speech engine. The suppoted properties
 
155
        and their values are:
 
156
 
 
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]
 
161
 
 
162
        @param name: Property name
 
163
        @type name: str
 
164
        @raise KeyError: When the property name is unknown
 
165
        '''
 
166
        try:
 
167
            return self._config[name]
 
168
        except KeyError:
 
169
            raise KeyError('unknown property %s' % name)
 
170
 
 
171
    def setProperty(self, name, value):
 
172
        '''
 
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.
 
176
 
 
177
        @param name: Property name
 
178
        @type name: str
 
179
        @param value: Property value
 
180
        @type value: object
 
181
        @raise KeyError: When the property name is unknown
 
182
        @raise ValueError: When the value cannot be coerced to fit the property
 
183
        '''
 
184
        if name == 'voice':
 
185
            v = filter(lambda v: v.id == value, self._config['voices'])
 
186
            self._config['voice'] = v[0]
 
187
        elif name == 'rate':
 
188
            self._config['rate'] = value
 
189
        elif name == 'volume':
 
190
            self._config['volume'] = value
 
191
        else:
 
192
            raise KeyError('unknown property %s' % name)
 
 
b'\\ No newline at end of file'