Piper TTS

1.4.2 · active · verified Thu Apr 16

Piper TTS is a fast, local, and neural text-to-speech engine optimized for CPU performance, allowing speech synthesis directly on device. It is currently at version 1.4.2 and receives frequent minor updates with occasional major version changes that introduce new features and API adjustments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load a Piper TTS voice model and synthesize speech. Remember that model files (`.onnx` and `.json`) must be downloaded separately. The `synthesize` method yields audio chunks (bytes) that can then be processed, saved, or played back.

import piper
import os

# NOTE: Model files (.onnx and .json) are NOT included with the pip package.
# You MUST download them separately, e.g., from huggingface.co/rhasspy/piper-voices
# Example: 'en_US-lessac-medium.onnx' and 'en_US-lessac-medium.json'

# Provide paths to your downloaded model files
model_path = os.environ.get('PIPER_MODEL_PATH', 'path/to/your_downloaded_model.onnx')
config_path = os.environ.get('PIPER_CONFIG_PATH', 'path/to/your_downloaded_model.json')

if not os.path.exists(model_path) or not os.path.exists(config_path):
    print(f"Error: Model files not found. Please download '.onnx' and '.json' files.")
    print(f"  Expected model at: {model_path}")
    print(f"  Expected config at: {config_path}")
    print(f"  Download example: https://huggingface.co/rhasspy/piper-voices/tree/main")
else:
    try:
        # Load the voice model
        voice = piper.PiperVoice.load(model_path, config_path=config_path)

        # Get default synthesis configuration
        synthesis_config = voice.config.synthesis
        text = "Hello, this is a test from Piper TTS."

        print(f"Synthesizing: '{text}'")
        audio_chunks_generator = voice.synthesize(text, synthesis_config)

        num_chunks = 0
        for audio_bytes in audio_chunks_generator:
            num_chunks += 1
            # In a real application, you would save audio_bytes to a file
            # or stream it for real-time playback (e.g., using 'sounddevice').
            # Example: print(f"Received chunk of {len(audio_bytes)} bytes.")
            pass # Just iterate to demonstrate generation

        print(f"Successfully generated {num_chunks} audio chunks.")
        print("Audio can be saved to a WAV file using Python's 'wave' module or 'soundfile'.")

    except Exception as e:
        print(f"An unexpected error occurred: {e}")

view raw JSON →