Skip to navigation

Set the output audio format

View as Markdown

output_audio_codec sets the format of the generated audio. It affects file size, quality and which players or telephony stacks can read the output.

Parameteroutput_audio_codec
Typestring
Valueswav, mp3, aac, flac, linear16, mulaw, alaw, opus
Modelsbulbul:v3, bulbul:v4-flash

The default depends on the API:

APIDefaultResponse shape
REST (POST /text-to-speech)wavJSON: {"request_id": "...", "audios": ["<base64>"]}
HTTP streaming (POST /text-to-speech/stream)mp3Raw binary body, Content-Type: audio/mpeg
WebSocket (/text-to-speech/ws)mp3JSON messages: {"type": "audio", "data": {"request_id", "content_type", "audio": "<base64>"}}
CodecContent typeTypical use
wavaudio/wavUncompressed, easy to inspect
mp3audio/mpegBroad compatibility, small files
aacaudio/aacCompressed, mobile playback
flacaudio/flacLossless
linear16audio/pcmRaw 16-bit PCM, no header
mulawaudio/mulawTelephony, typically at 8000 Hz
alawaudio/alawTelephony, typically at 8000 Hz
opusaudio/opusOgg-wrapped Opus, see the sample rate rule below

opus only works at sample rates 8000, 16000, 24000 (and 48000 on REST). At the default 22050 Hz the request fails, so set speech_sample_rate when you use it.

Example

import os
from sarvamai import SarvamAI
from sarvamai.play import save
client = SarvamAI(api_subscription_key=os.environ["SARVAM_API_KEY"])
response = client.text_to_speech.convert(
text="Welcome to Sarvam AI!",
language_code="en-IN",
model="bulbul:v3",
speaker="shubh",
output_audio_codec="aac",
)
save(response, "output.aac")

For HTTP streaming, send the same field to /text-to-speech/stream and write the response body directly (no base64 decoding). For WebSocket, put it in the config message; see the WebSocket TTS page.

What changes in the output

  • The file contents and content_type follow the codec. Use a matching file extension when saving.
  • mulaw and alaw are typically used with "speech_sample_rate": 8000 for telephony.
  • On HTTP streaming and WebSocket, output_audio_bitrate sets the bitrate, see bitrate.

Common errors

StatusMessageFix
400output_audio_codec: Input should be 'mp3', 'linear16', 'mulaw', 'alaw', 'opus', 'flac', 'aac' or 'wav'Use one of the listed values (ogg is not a codec name).
400OPUS codec requires one of these sample rates: 8000, 12000, 16000, 24000, 48000 Hz. Current sample rate: 22050 Hz.Set speech_sample_rate to 8000, 16000 or 24000. On the WebSocket the same message arrives as an in-band error message.