pythonbeginner

OpenAI Text-to-Speech Synthesis

Convert text to natural-sounding speech using the OpenAI TTS API with voice selection and streaming.

python
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.