Skip to navigation

Set the sample rate

View as Markdown

speech_sample_rate sets the sample rate of the generated audio in Hz. Higher rates give more audio bandwidth and larger output.

Parameterspeech_sample_rate
Typeinteger (a string such as "16000" in the WebSocket config message)
Default22050
Modelsbulbul:v3, bulbul:v4-flash
APIAccepted values (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 (/text-to-speech/ws)8000, 16000, 22050, 24000

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",
speech_sample_rate=16000,
)
save(response, "output.wav")

For streaming, set speech_sample_rate in the HTTP stream request body or in the WebSocket config message (as a string, for example "speech_sample_rate": "16000"). See WebSocket TTS.

What changes in the output

  • The sample rate of the returned audio matches the value you send, in every codec that carries a sample rate (for example the WAV header).
  • Lower rates (8000) give smaller files and suit telephony. Higher rates (24000 and above) preserve more detail.
  • Omitting the field returns 22050 Hz.

Common errors

StatusMessageFix
400Speech sample rate can only be 8000, 16000, 22050, 24000 Hz32000, 44100 and 48000 were sent to a streaming endpoint. Use a streaming rate, or use REST.
400speech_sample_rate: Input should be 8000, 16000, 22050, 24000, 32000, 44100 or 48000The value is not a supported rate (for example 11025).
400OPUS codec requires one of these sample rates: 8000, 12000, 16000, 24000, 48000 Hzopus was requested at the default 22050. See audio codec.