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.1.tar.gz (160.3 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.1-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py_speech_gen-0.1.1.tar.gz
  • Upload date:
  • Size: 160.3 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.1.tar.gz
Algorithm Hash digest
SHA256 364ef828adb194f8fca15f021946a7506df498e168249831ad02737f5fd2a6c1
MD5 33fb4df655a206ddefa727274e69f8b1
BLAKE2b-256 a2d52e82c98d9e4b0d4988315fe4914cf839cc1331bd63091f26da281ddfd624

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_speech_gen-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 24.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 66c3fcdd5576e9a8cc1d9716ab512bc5849b83a6cde30cb0a271d50b3cee7241
MD5 adba4f51229ee8728ac5458dbda8fbda
BLAKE2b-256 fc1a8f317d368a3156a679137f04817a6f2a44abe8348aa5de0ad59127fc4f86

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