Skip to navigation

How to set the sample rate (audio quality)

View as Markdown

The speech_sample_rate parameter sets the output sample rate in Hz. It is optional.

Values the API accepts

SurfaceAccepted speech_sample_rate (Hz)
REST (POST /text-to-speech)8000, 16000, 22050, 24000, 32000, 44100, 48000
HTTP streaming (POST /text-to-speech/stream)8000, 16000, 22050, 24000
WebSocket streaming8000, 16000, 22050, 24000

If you omit speech_sample_rate, the API returns audio at 22050 Hz.

Values above 24000 (32000, 44100, 48000) are accepted on REST only. Streaming endpoints reject them with 400 Bad Request.

Example Code

from sarvamai import SarvamAI
from sarvamai.play import save
# Initialize the REST client
client = SarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY")
# Generate speech using REST
audio = client.text_to_speech.convert(
text="Welcome to Sarvam AI!",
model="bulbul:v4-flash",
language_code="en-IN",
speech_sample_rate=24000
)
save(audio, "output1.wav")