An application invokes the pyttsx.init() factory function to get a reference to a pyttsx.Engine instance. During construction, the engine initializes a pyttsx.driver.DriverProxy object responsible for loading a speech engine driver implementation from the pyttsx.drivers module. After construction, an application uses the engine object to register and unregister event callbacks; produce and stop speech; get and set speech engine properties; and start and stop event loops.
Gets a reference to an engine instance that will use the given driver. If the requested driver is already in use by another engine instance, that engine is returned. Otherwise, a new engine is created.
Parameters: |
|
---|---|
Raises: |
|
Provides application access to text-to-speech synthesis.
Registers a callback for notifications on the given topic.
Parameters: |
|
---|---|
Returns: | A token that the caller can use to unsubscribe the callback later. |
The following are the valid topics and their callback signatures.
Fired when the engine begins speaking an utterance. The associated callback must have the folowing signature.
Parameters: | name – Name associated with the utterance. |
---|
Fired when the engine begins speaking a word. The associated callback must have the folowing signature.
Parameters: | name – Name associated with the utterance. |
---|
Fired when the engine finishes speaking an utterance. The associated callback must have the folowing signature.
Parameters: |
|
---|
Fired when the engine encounters an error. The associated callback must have the folowing signature.
Parameters: |
|
---|
Unregisters a notification callback.
Parameters: | token – Token returned by connect() associated with the callback to be disconnected. |
---|
Ends a running event loop. If startLoop() was called with useDriverLoop set to True, this method stops processing of engine commands and immediately exits the event loop. If it was called with False, this method stops processing of engine commands, but it is up to the caller to end the external event loop it started.
Raises RuntimeError: | |
---|---|
When the loop is not running |
Gets the current value of an engine property.
Parameters: | name – Name of the property to query. |
---|---|
Returns: | Value of the property at the time of this invocation. |
The following property names are valid for all drivers.
Integer speech rate in words per minute. Defaults to 200 word per minute.
String identifier of the active voice.
List of pyttsx.voice.Voice descriptor objects.
Floating point volume in the range of 0.0 to 1.0 inclusive. Defaults to 1.0.
Gets if the engine is currently busy speaking an utterance or not.
Returns: | True if speaking, false if not. |
---|
Blocks while processing all currently queued commands. Invokes callbacks for engine notifications appropriately. Returns when all commands queued before this call are emptied from the queue.
Queues a command to speak an utterance. The speech is output according to the properties set before this command in the queue.
Parameters: |
|
---|
Queues a command to set an engine property. The new property value affects all utterances queued after this command.
Parameters: |
|
---|
The following property names are valid for all drivers.
Integer speech rate in words per minute.
String identifier of the active voice.
Floating point volume in the range of 0.0 to 1.0 inclusive.
Starts running an event loop during which queued commands are processed and notifications are fired.
Parameters: | useDriverLoop – True to use the loop provided by the selected driver. False to indicate the caller will enter its own loop after invoking this method. The caller’s loop must pump events for the driver in use so that pyttsx notifications are delivered properly (e.g., SAPI5 requires a COM message pump). Defaults to True. |
---|
Stops the current utterance and clears the command queue.
Contains information about a speech synthesizer voice.
Integer age of the voice in years. Defaults to None if unknown.
String gender of the voice: male, female, or neutral. Defaults to None if unknown.
String identifier of the voice. Used to set the active voice via pyttsx.engine.Engine.setPropertyValue(). This attribute is always defined.
List of string languages supported by this voice. Defaults to an empty list of unknown.
Human readable name of the voice. Defaults to None if unknown.
import pyttsx
engine = pyttsx.init()
engine.say('Sally sells seashells by the seashore.')
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
import pyttsx
def onStart(name):
print 'starting', name
def onWord(name, location, length):
print 'word', name, location, length
def onEnd(name, completed):
print 'finishing', name, completed
engine = pyttsx.init()
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
import pyttsx
def onWord(name, location, length):
print 'word', name, location, length
if location > 10:
engine.stop()
engine = pyttsx.init()
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
engine = pyttsx.init()
voices = engine.getProperty('voices')
for voice in voices:
engine.setProperty('voice', voice.id)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
engine = pyttsx.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', rate+50)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
engine = pyttsx.init()
volume = engine.getProperty('volume')
engine.setProperty('volume', volume-0.25)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
engine = pyttsx.init()
def onStart(name):
print 'starting', name
def onWord(name, location, length):
print 'word', name, location, length
def onEnd(name, completed):
print 'finishing', name, completed
if name == 'fox':
engine.say('What a lazy dog!', 'dog')
elif name == 'dog':
engine.endLoop()
engine = pyttsx.init()
engine.say('The quick brown fox jumped over the lazy dog.', 'fox')
engine.startLoop()
engine = pyttsx.init()
engine.say('The quick brown fox jumped over the lazy dog.', 'fox')
engine.startLoop(False)
# engine.iterate() must be called inside externalLoop()
externalLoop()
engine.endLoop()