Sub topic 7
Choosing voices and output formats
This chapter assumes you can already send a text-to-speech request, as covered in the previous chapter. It also assumes you have OPENAI_API_KEY set in your environment. Here you will choose a voice, an output format, and a speaking speed, and learn which choices cause problems in practice.
The three options that shape the output
A speech request has three main options beyond the model and the input text.
| Option | What it controls | Typical values |
|---|---|---|
| `voice` | The timbre and character of the speaker | `alloy`, `echo`, `fable`, `onyx`, `nova`, `shimmer` |
| `response_format` | The container and codec of the returned audio | `mp3`, `opus`, `aac`, `flac`, `wav`, `pcm` |
| `speed` | Speaking rate as a multiplier | `0.25` to `4.0`, default `1.0` |
Check the provider's current documentation for the exact lists. Voice names, supported formats, and model names change over time.
Choosing a voice
A voice is a fixed preset. You cannot adjust its pitch or accent through the API. You pick one by listening.
A practical way to choose is to render the same sentence with every voice and compare them. Use a sentence from your real app, not a generic greeting. Numbers, names, and questions show the differences best.
from openai import OpenAI
client = OpenAI()
SAMPLE = "Your appointment is confirmed for March 4th at 2:30 PM. Shall I send a reminder?"
VOICES = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"]
for voice in VOICES:
with client.audio.speech.with_streaming_response.create(
model="tts-1",
voice=voice,
input=SAMPLE,
response_format="mp3",
) as response:
response.stream_to_file(f"sample_{voice}.mp3")
print("wrote", f"sample_{voice}.mp3")Once you pick a voice, keep it consistent within a product. A voice that changes between turns confuses users.
Choosing an output format
The right format depends on where the audio goes.
- mp3 is the default. Nearly every player and browser supports it. Use it when you save files or serve them to unknown clients.
- opus is compact and suits internet streaming and low-bandwidth links.
- aac is common on mobile devices and in video containers.
- flac is lossless and compressed. Use it for archives, not for conversation.
- wav is uncompressed with a header. It is simple to process and large to store.
- pcm is raw samples with no header. Use it when you feed audio directly to a speaker buffer or a telephony system. Check the documentation for its sample rate and sample format before you play it. A player that does not know them will produce noise.
Compressed formats trade quality for size. For spoken replies, mp3 or opus at the provider's default settings is almost always good enough. Lossless formats mainly increase upload and download time.
Changing the speed
The speed parameter scales the speaking rate. A value of 1.0 is normal. Values from 1.1 to 1.25 suit users who want brisk answers. Values below 0.9 suit slow, careful instructions.
from openai import OpenAI
client = OpenAI()
with client.audio.speech.with_streaming_response.create(
model="tts-1",
voice="nova",
input="Step one. Open the settings menu.",
response_format="opus",
speed=0.9,
) as response:
response.stream_to_file("step_one.opus")Extreme values degrade clarity. Listen to your own content at the speed you plan to ship.
Common mistakes
- Mismatched extension. If you request
opusbut save the file asreply.mp3, some players fail. Name the file after the format you requested. - Testing with short text only. A voice that sounds fine on one sentence may sound flat over a paragraph. Test with realistic length.
- Ignoring the pipeline. If you build a phone system, choose the format your telephony layer expects. Converting after the fact adds latency and can lower quality.
- Forgetting disclosure. A synthetic voice must be presented as synthetic. A later chapter covers this in detail.
Exercise
Your app plays replies through a custom audio buffer that expects raw samples, and you saved a test reply as reply.pcm. Your media player refuses to open it. Why, and what are two ways to check the audio?
Show answer
PCM has no header, so a player cannot tell the sample rate, sample format, or channel count. You can wrap the data in a WAV header with the correct parameters, or open it in a tool where you enter those parameters manually. Alternatively, request wav for testing and keep pcm for the production buffer.