pythonbeginner
OpenAI Text-to-Speech Synthesis
Convert text to natural-sounding speech using the OpenAI TTS API with voice selection and streaming.
pythonPress ⌘/Ctrl + Shift + C to copy
from openai import OpenAI
from pathlib import Path
client = OpenAI()
# Generate speech to file
response = client.audio.speech.create(
model='tts-1-hd',
voice='nova', # Options: alloy, echo, fable, onyx, nova, shimmer
input='Python is a fantastic programming language for AI and data science applications.',
speed=1.0,
response_format='mp3',
)
response.stream_to_file('output.mp3')
print(f'Saved {Path("output.mp3").stat().st_size / 1024:.1f} KB')
# Voices comparison
voices = ['alloy', 'echo', 'fable']
for voice in voices:
resp = client.audio.speech.create(model='tts-1', voice=voice, input=f'Hello, I am the {voice} voice.', response_format='mp3')
Path(f'{voice}.mp3').write_bytes(resp.content)
print(f'Created {voice}.mp3')Use Cases
- text-to-speech
- voice applications
- accessibility
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptbeginner
OpenAI Text-to-Speech
Generate natural speech audio from text using OpenAI TTS API with multiple voice options and formats.
Best for: Audiobook generation
#tts#speech
pythonbeginner
Whisper Audio Transcription Pipeline
Transcribe audio files to text using OpenAI Whisper API with language detection and timestamps.
Best for: meeting transcription
#whisper#transcription
typescriptintermediate
OpenAI Chat Completion with Streaming
Stream GPT responses token-by-token using the OpenAI SDK with async iteration.
Best for: chatbot UI
#openai#streaming
typescriptbeginner
Generate Text Embeddings with OpenAI
Create vector embeddings for semantic search and similarity matching using text-embedding-3-small.
Best for: semantic search
#openai#embeddings