How to choose audio formats

View as Markdown

Input formats

The ref_audio file accepts the following codecs:

CodecNotes
WAVRecommended. Uncompressed; fastest to decode and avoids lossy compression artifacts.
MP3Widely available; ensure bitrate ≥ 128 kbps for cleaner clones.
FLACLossless compression; equivalent quality to WAV with smaller file size.
OPUSModern codec with good speech quality at low bitrates.
AACCommon in mobile recordings.
LINEAR16Raw 16-bit PCM.
MULAW8-bit μ-law (telephony). Quality is limited by the codec.
ALAW8-bit A-law (telephony). Quality is limited by the codec.

For best cloning quality, use WAV or FLAC. Telephony codecs (MULAW, ALAW) work but cap the achievable fidelity at the source.

Output formats

Specify the output codec via output_audio_codec. The default is wav.

Codec valueDescription
wavUncompressed WAV. Default. Best quality for downstream processing.
mp3MP3-encoded audio. Smaller file size; suitable for web playback.
flacLossless compression.
opusLow-bitrate codec ideal for streaming.
aacCommon in mobile and broadcast.
linear16Raw 16-bit PCM.
mulaw8-bit μ-law. Use for IVR/telephony pipelines.
alaw8-bit A-law. Use for IVR/telephony pipelines.

OPUS restricts the sample rate. When output_audio_codec is opus and speech_sample_rate is explicitly set, it must be one of 8000, 16000, 24000, or 48000 Hz. Other rates fail with 400 Bad Request. When speech_sample_rate is omitted, opus output is returned at the native 24000 Hz.

Sample rate

Use speech_sample_rate to set the output sample rate in Hz:

8000, 16000, 22050, 24000, 32000, 44100, 48000

If omitted, the API returns audio at the model’s native rate (24,000 Hz). Set the sample rate to match your downstream pipeline:

PipelineRecommended sample rate
Telephony / IVR8000
Voice agents / VoIP16000
Web playback24000 (default)
Broadcast / studio44100 or 48000

Sample rates above 24000 Hz (32000, 44100, 48000) are supported for broadcast-quality output, matching the Bulbul v3 REST API.

File size guidance

Higher sample rates and uncompressed codecs produce larger output files. As a rough guide for a 10-second clip:

CodecSample rateApproximate size
wav24000~470 KB
wav48000~940 KB
mp3 (128 kbps)24000~160 KB
opus (32 kbps)24000~40 KB
mulaw8000~80 KB

For high-volume API usage, choose a compressed codec (mp3, opus, aac) unless your downstream pipeline specifically requires uncompressed audio.

Decoding the response

The audio field is a base64-encoded string of the raw bytes for the codec you requested. Decode it and write it to a file with the appropriate extension:

curl -s -X POST "https://api.sarvam.ai/voices/clone" \
-H "api-subscription-key: $SARVAM_API_KEY" \
-F "text=Hello world" \
-F "language_code=en-IN" \
-F "ref_audio=@reference.wav" \
-F "output_audio_codec=wav" \
-o response.json
# Decode audio into a file; use the extension matching your output_audio_codec
python3 -c "import base64, json; open('output.wav', 'wb').write(base64.b64decode(json.load(open('response.json'))['audio']))"
import fs from "fs";
const form = new FormData();
form.append("text", "Hello world");
form.append("language_code", "en-IN");
form.append("output_audio_codec", "wav");
form.append("ref_audio", new Blob([fs.readFileSync("reference.wav")]), "reference.wav");
const response = await fetch("https://api.sarvam.ai/voices/clone", {
method: "POST",
headers: { "api-subscription-key": process.env.SARVAM_API_KEY },
body: form,
});
const result = await response.json();
if (!response.ok) {
console.error(`Voice cloning failed (${response.status}):`, result.error?.message);
process.exit(1);
}
const audioBytes = Buffer.from(result.audio, "base64");
fs.writeFileSync("output.wav", audioBytes);

The API is multipart/form-data. From Node 18+, FormData and Blob are available globally, so no extra library is needed to build the request body.

Next steps