pyttsx3 Documentation

repository·master·Indexed 25 days ago

https://github.com/nateshmbhat/pyttsx3

An offline text-to-speech (TTS) conversion library for Python 3 that works without internet connection. It supports multiple TTS engines including Sapi5 (Windows), nsss (macOS), and espeak (Linux). The library provides a high-level speak() function for simple usage and an Engine class for granular control over speech rate, volume, voice selection, and event callbacks.

Tokens
3.5K
Snippets
8
Records
29
Agent score
81%

What's inside pyttsx3

  1. Manage event loops with startLoop() and endLoop()

    master

    The engine requires an event loop to process commands and fire callbacks.

    1. Driver Loop (useDriverLoop=True): The engine uses the driver's internal loop. This is the default.
    2. External Loop (useDriverLoop=False): The caller is responsible for providing the loop. If you use an external loop, you must manually call engine.iterate() within that loop to pump events.

    To stop a running loop, use engine.endLoop(). To stop the current utterance and clear the queue, use engine.stop().

  2. Supported text-to-speech synthesizers by platform

    master

    The pyttsx3 library provides drivers for different text-to-speech (TTS) engines depending on your operating system. While these are the tested and known working combinations, drivers may work on other systems:

    • Windows: Uses SAPI5 (tested on Windows XP, Vista, 8, 8.1, and 10).
    • macOS: Uses NSSpeechSynthesizer (tested on Mac OS X 10.5 Leopard and 10.6 Snow Leopard).
    • Linux: Uses espeak (tested on Ubuntu Desktop 8.10, 9.04, and 9.10).

    You can select a specific synthesizer by name during initialization using pyttsx3.init().

  3. Configure Linux system requirements

    master

    On Linux, if voice output is not working, you must install espeak-ng and libespeak1 using the following command:

    sudo apt update && sudo apt install espeak-ng libespeak1
    sudo apt update && sudo apt install espeak-ng libespeak1
  4. How to implement a new driver for pyttsx3

    master

    To extend pyttsx3 with a custom driver, you must create a new Python module and implement a specific factory function and a delegate class. The pyttsx3.Engine uses the factory function to instantiate your driver, passing in a DriverProxy instance which your driver uses to communicate with the engine (e.g., sending notifications or managing the command queue).

    Implementation Steps:

    1. Create a Python module named after your new driver.
    2. Implement a buildDriver(proxy) factory function in that module.
    3. Implement a DriverDelegate class that adheres to the required interface.
    4. Use the provided DriverProxy instance within your delegate to control the event queue and notify the application of events.
  5. Install pyttsx3 system-wide

    master

    You can install pyttsx3 globally using pip. Note that platform-specific requirements apply:

    • Windows: You must first install the pywin32-extensions package using its Windows installer before installing pyttsx3 via pip.
    • OSX or Linux: Use sudo pip to install system-wide.

    Supported Synthesizers by Platform:

    • Windows: SAPI5 (tested on Windows XP, Vista, and 7).
    • Mac OS X: NSSpeechSynthesizer (tested on 10.5 through 10.8).
    • Linux: espeak (tested on 32-bit Ubuntu 8.10, 9.04, 9.10, and 12.04).
  6. Install pyttsx3 in a virtualenv

    master

    To install pyttsx3 within a virtual environment, follow the platform-specific instructions for dependency access:

    • Windows: Install pywin32-extensions system-wide first. Create the virtual environment with access to system site-packages using the --system-site-packages flag.
    • OSX: Create the virtual environment with access to system site-packages using the --system-site-packages flag to avoid lengthy pyobjc compilation.
    • Linux: No special system-site-packages access is required. You can use a standard virtual environment.
    # Windows
    $ virtualenv --system-site-packages myproj
    $ myproj\Scripts\activate
    (myproj)$ pip install pyttsx3
    
    # OSX
    $ virtualenv --system-site-packages myproj
    $ . myproj/bin/activate
    (myproj)$ pip install pyttsx3
    
    # Linux
    $ virtualenv --no-site-packages myproj
    $ . myproj/bin/activate
    (myproj)$ pip install pyttsx3
  7. Basic usage of pyttsx3

    master

    You can use the library in two ways: by initializing an engine object for fine-grained control, or by using the speak() convenience function for a single line of code.

    import pyttsx3
    
    # Method 1: Using the engine object (recommended for control)
    engine = pyttsx3.init()
    engine.say("I will speak this text")
    engine.runAndWait()
    
    # Method 2: Single line usage with default options
    pyttsx3.speak("I will speak this text")
  8. Speak text and save to file

    master

    Queue text for speech using say(text, name). The name parameter is an identifier used to track the utterance in callbacks. To process the queue, you must call runAndWait().

    You can also save the spoken text to an audio file using save_to_file(text, filename).

    import pyttsx3
    engine = pyttsx3.init()
    engine.say('Hello World', 'greeting')
    engine.save_to_file('Hello World', 'test.mp3')
    engine.runAndWait()
  9. The DriverDelegate interface requirements

    master

    All drivers must implement a class following the DriverDelegate interface. While DriverDelegate is not a base class you can inherit from directly, your implementation must provide these methods:

    MethodDescription
    __init__(proxy, *args, **kwargs)Constructor. Must store the proxy reference provided by buildDriver.
    destroy()Optional. Called by DriverProxy during destruction to allow cleaning up synthesizer resources.
    endLoop()Immediately ends the running driver event loop.
    getProperty(name)Returns the value of the named property. Must support properties listed in pyttsx3.Engine.getProperty documentation.
    say(text, name)Speaks an utterance. Must call proxy.setBusy(True) before returning to stall the queue. Must trigger started-utterance, started-word (for each word), and finished-utterance notifications.
    setProperty(name, value)Sets a property. Must call proxy.setBusy(False) after setting to pump the command queue.
    startLoop()Starts the event loop responsible for sending notifications and pumping the command queue via the proxy.
    stop()Stops current output. If output was ongoing, must trigger finished-utterance. Must call proxy.setBusy(False) after stopping.