Skip to main content

A Python library for generating synthetic speech datasets using TTS providers.

Project description

py-speech-gen

PyPI version Python versions License: MIT

A Python library for generating synthetic speech datasets using TTS providers. Supports ElevenLabs and Piper TTS out of the box, with an extensible provider system for adding custom backends.

Features

  • Multi-provider support — ElevenLabs, Piper TTS, or your own custom provider
  • Text preprocessing — cleaning, normalization, number-to-words, sentence segmentation
  • Parameter randomization — per-sample variation for voice diversity
  • Background noise injection — 8 synthetic noise types (white, pink, brown, traffic, cafe, home, crowd, mic)
  • Flexible output formats — WAV, MP3, FLAC at configurable sample rates
  • Reproducible generation — export/load configs for deterministic datasets
  • Export options — JSON, CSV, pandas DataFrame

Installation

Basic install (core features only)

pip install py-speech-gen

The base installation includes text processing, dataset management, randomization, and noise mixing.

Install with TTS providers

# Piper TTS (local, offline)
pip install "py-speech-gen[piper]"

# ElevenLabs (cloud API)
pip install "py-speech-gen[elevenlabs]"

# All providers
pip install "py-speech-gen[all]"

Requirements

  • Python 3.11+
  • For Piper TTS: ONNX Runtime (GPU or CPU variant)
  • For ElevenLabs: valid API key

Quick Start

import os
from pathlib import Path
from py_speech_gen import (
    ElevenLabsProvider,
    PiperProvider,
    DatasetGenerator,
    TextProcessor,
)

# Setup providers
elevenlabs = ElevenLabsProvider(
    api_key=os.getenv("ELEVENLABS_API_KEY"),
    voice_ids=["voice_id_1", "voice_id_2"],
    model="eleven_flash_v2_5",
)

piper = PiperProvider(
    models_path="./models",
    voice_ids=["en_US-lessac-medium"],
)

# Generate dataset
generator = DatasetGenerator(
    providers=[elevenlabs, piper],
    output_dir="./output",
)

dataset = generator.generate_dataset(
    texts=["Hello world.", "This is a test."],
    dataset_name="my_speech_dataset",
    process_texts=True,
    texts_lang="en",
)

# Export results
dataset.save()        # JSON
dataset.save_csv()    # CSV
print(f"Generated {len(dataset)} samples")

Components

Component Description
Providers TTS backends (ElevenLabsProvider, PiperProvider) implementing BaseProvider
DatasetGenerator Orchestrator that manages generation across multiple providers
Dataset Data model with save/load/export (JSON, CSV, pandas)
TextProcessor Text cleaning, normalization, number-to-words, sentence segmentation
Randomizer Per-sample parameter randomization for voice diversity
NoiseMixer Background noise injection for realistic conditions

Randomizer

Adds random variation to provider parameters per sample for dataset diversity.

from py_speech_gen import Randomizer

# Use a preset: "subtle", "moderate", "extreme"
randomizer = Randomizer.preset("moderate", seed=42)

generator = DatasetGenerator(
    providers=[piper, elevenlabs],
    output_dir="./output",
    randomizer=randomizer,
)

Custom Randomizer Config

randomizer = Randomizer(
    config={
        "global": {"speed": (0.8, 1.2)},
        "elevenlabs": {
            "stability": (0.3, 0.7),
            "similarity_boost": (0.5, 0.9),
            "style": (0.0, 0.3),
            "use_speaker_boost": [True, False],
        },
        "piper": {
            "length_scale": (0.8, 1.3),
            "noise_scale": (0.5, 0.8),
            "noise_w_scale": (0.6, 1.0),
        },
    },
    seed=42,
)

# Export/load for reproducibility
randomizer.export("randomizer_config.json")
r = Randomizer.load("randomizer_config.json")

NoiseMixer

Adds background noise to generated audio for realistic conditions.

from py_speech_gen import NoiseMixer

mixer = NoiseMixer(
    noise_types=["white", "traffic", "cafe"],  # or "all", "synthetic"
    snr_db=20,
    random_snr=True,
    snr_range=(15, 30),
    skip_prob=0.2,  # 20% of samples get no noise
    seed=42,
)

generator = DatasetGenerator(
    providers=[piper, elevenlabs],
    output_dir="./output",
    noise_mixer=mixer,
)

Noise Types

Type Source Description
white Generated White noise
pink Generated Pink noise (1/f)
brown Generated Brown noise (deep bass)
traffic Generated Filtered noise with amplitude modulation
cafe Generated Bandpass noise with random bursts
home Generated 50/60Hz hum + random clicks
crowd Generated Bandpass noise with rhythmic modulation
mic Generated High-pass hiss + occasional pops

Configuration

Copy .env.example to .env and set your API keys:

cp .env.example .env

Full Pipeline Example

from py_speech_gen import (
    DatasetGenerator, Randomizer, NoiseMixer,
)

generator = DatasetGenerator(
    providers=[piper, elevenlabs],
    output_dir="./output",
    randomizer=Randomizer.preset("moderate", seed=42),
    noise_mixer=NoiseMixer(
        noise_types=["white", "traffic", "cafe"],
        snr_range=(15, 25),
        skip_prob=0.2,
        seed=42,
    ),
)

dataset = generator.generate_dataset(
    texts=["Sentence 1.", "Sentence 2."],
    dataset_name="demo",
    process_texts=True,
    texts_lang="en",
)

# Export full generation config for reproducibility
generator.export_config("./output/generation_config.json")

# Load config to reproduce
generator2 = DatasetGenerator.load_config(
    "./output/generation_config.json",
    providers=[piper, elevenlabs],
)

Custom Providers

You can create your own TTS provider by extending BaseProvider. See Custom Provider Tutorial.

Running Tests

pip install "py-speech-gen[dev]"
pytest tests/ -v

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_speech_gen-0.1.0.tar.gz (156.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_speech_gen-0.1.0-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file py_speech_gen-0.1.0.tar.gz.

File metadata

  • Download URL: py_speech_gen-0.1.0.tar.gz
  • Upload date:
  • Size: 156.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for py_speech_gen-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d0e86963db75c7f8c4926e8daa9d2c6e9f864988df8e170ca687b0e0a7d00d3a
MD5 2068cfb075569a67a28a3bffc6f3b6b0
BLAKE2b-256 5f086c41d70a7e5de7f21407f42e89fde9e4f05656d8f2eee34eaf1253e564ee

See more details on using hashes here.

File details

Details for the file py_speech_gen-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: py_speech_gen-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for py_speech_gen-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3842ebb49ecfab3ed53ca0d10b16d86e96f6db5633e25475dff881a3539f933
MD5 8b6146376115ecceb6065f71f622c571
BLAKE2b-256 1ce6c91dd28cb1712e1fe2fda990ead44cbe49a6da6044a3afaf80e7e7dc3ed8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page