Text-to-Speech (TTS)
Convert text into lifelike spoken audio using state-of-the-art AI models. Explore commercial APIs like OpenAI and ElevenLabs, or run open-source models like MeloTTS and Bark locally.
TTS Landscape (2024/2025)
Modern TTS has moved far beyond robotic, concatenated synthesis. Neural TTS models now generate speech that is often indistinguishable from human speakers, capturing emotion, prosody, and breath.
Commercial APIs
- OpenAI Audio: Natural, affordable, easy integration.
- ElevenLabs: Industry leader for voice cloning and emotion.
- Google/AWS/Azure: Enterprise-grade, massive language support.
Open Source / Local
- MeloTTS: High-speed, multi-lingual, CPU-friendly.
- Bark (Suno): Generates non-speech sounds (laughs, sighs).
- Coqui TTS / XTTS: Voice cloning and fine-tuning capability.
OpenAI TTS API
Best for general-purpose applications needing high quality at a reasonable usage cost.
from pathlib import Path
from openai import OpenAI
client = OpenAI()
speech_file_path = Path(__file__).parent / "speech.mp3"
response = client.audio.speech.create(
model="tts-1", # or "tts-1-hd" for higher quality
voice="alloy", # Options: alloy, echo, fable, onyx, nova, shimmer
input="The quick brown fox jumps over the lazy dog."
)
response.stream_to_file(speech_file_path)
print(f"Saved to {speech_file_path}")
ElevenLabs (Voice Cloning)
The standard for voice cloning and emotional range. Requires an API key.
import os
from elevenlabs.client import ElevenLabs
from elevenlabs import save
client = ElevenLabs(
api_key="YOUR_API_KEY",
)
audio = client.generate(
text="Hello! This is a realistic AI voice generated by ElevenLabs.",
voice="Rachel",
model="eleven_multilingual_v2"
)
save(audio, "elevenlabs_output.mp3")
π‘ Pricing: ElevenLabs has a generous free tier, but character limits apply.
Micro-Service: MeloTTS (Open Source)
Run high-quality TTS locally or on your own server. MeloTTS is fast enough for CPU inference.
1. Installation
pip install melotts # Requires mecab for some languages python -m unidic download
2. Python Usage
from melo.api import TTS
# Speed is adjustable
speed = 1.0
device = 'cpu' # or 'cuda:0'
model = TTS(language='EN', device=device)
speaker_ids = model.hps.data.spk2id
# Use American English accent
output_path = 'melo_en.wav'
model.tts_to_file("Hello, this is running locally on your machine.", speaker_ids['EN-US'], output_path, speed=speed)
Real-time Streaming Pattern
For voice assistants, waiting for the full audio file is too slow. Use streaming to play audio chunks as they arrive.
# Example using OpenAI Streaming
response = client.audio.speech.create(
model="tts-1",
voice="alloy",
input="This text is being streamed in real-time.",
)
# Stream to a file or audio output
response.stream_to_file("output.mp3")
# Ideally, pipe 'response.content' chunks to an audio player like PyAudio
# for true low-latency playback.
Comparison Matrix
| Tool | Type | Quality | Latency | Cost |
|---|---|---|---|---|
| OpenAI TTS | API | High | Low (tts-1) | $15 / 1M chars |
| ElevenLabs | API | Very High | Medium (Turbo available) | High ($$$) |
| MeloTTS | Local | Good | Very Low | Free (Compute only) |
| Bark | Local | Creative (FX included) | High (Slow) | Free |