Sarvam
Mastra 中的 SarvamVoice 类使用 Sarvam AI 模型提供文本转语音和语音转文本功能。
🌐 The SarvamVoice class in Mastra provides text-to-speech and speech-to-text capabilities using Sarvam AI models.
使用示例Direct link to 使用示例
🌐 Usage Example
import { SarvamVoice } from "@mastra/voice-sarvam";
// Initialize with default configuration using environment variables
const voice = new SarvamVoice();
// Or initialize with specific configuration
const voiceWithConfig = new SarvamVoice({
speechModel: {
model: "bulbul:v1",
apiKey: process.env.SARVAM_API_KEY!,
language: "en-IN",
properties: {
pitch: 0,
pace: 1.65,
loudness: 1.5,
speech_sample_rate: 8000,
enable_preprocessing: false,
eng_interpolation_wt: 123,
},
},
listeningModel: {
model: "saarika:v2",
apiKey: process.env.SARVAM_API_KEY!,
languageCode: "en-IN",
filetype?: 'wav';
},
speaker: "meera", // Default voice
});
// Convert text to speech
const audioStream = await voice.speak("Hello, how can I help you?");
// Convert speech to text
const text = await voice.listen(audioStream, {
filetype: "wav",
});
Sarvam API 文档 -Direct link to Sarvam API 文档 -
🌐 Sarvam API Docs -
https://docs.sarvam.ai/api-reference-docs/endpoints/text-to-speech
配置Direct link to 配置
🌐 Configuration
构造函数选项Direct link to 构造函数选项
🌐 Constructor Options
speechModel?:
SarvamVoiceConfig
= { model: 'bulbul:v1', language: 'en-IN' }
Configuration for text-to-speech synthesis.
speaker?:
SarvamVoiceId
= 'meera'
The speaker to be used for the output audio. If not provided, Meera will be used as default. AvailableOptions - meera, pavithra, maitreyi, arvind, amol, amartya, diya, neel, misha, vian, arjun, maya
listeningModel?:
SarvamListenOptions
= { model: 'saarika:v2', language_code: 'unknown' }
Configuration for speech-to-text recognition.
SarvamVoiceConfigDirect link to SarvamVoiceConfig
apiKey?:
string
Sarvam API key. Falls back to SARVAM_API_KEY environment variable.
model?:
SarvamTTSModel
= 'bulbul:v1'
Specifies the model to use for text-to-speech conversion.
language:
SarvamTTSLanguage
= 'en-IN'
Target language for speech synthesis. Available options: hi-IN, bn-IN, kn-IN, ml-IN, mr-IN, od-IN, pa-IN, ta-IN, te-IN, en-IN, gu-IN
properties?:
object
Additional voice properties for customization.
properties.pitch?:
number
Controls the pitch of the audio. Lower values result in a deeper voice, while higher values make it sharper. The suitable range is between -0.75 and 0.75.
properties.pace?:
number
Controls the speed of the audio. Lower values result in slower speech, while higher values make it faster. The suitable range is between 0.5 and 2.0. Default is 1.0. Required range: 0.3 <= x <= 3
properties.loudness?:
number
Controls the loudness of the audio. Lower values result in quieter audio, while higher values make it louder. The suitable range is between 0.3 and 3.0. Required range: 0 <= x <= 3
properties.speech_sample_rate?:
8000 | 16000 | 22050
Audio sample rate in Hz.
properties.enable_preprocessing?:
boolean
Controls whether normalization of English words and numeric entities (e.g., numbers, dates) is performed. Set to true for better handling of mixed-language text. Default is false.
properties.eng_interpolation_wt?:
number
Weight for interpolating with English speaker at encoder.
SarvamListenOptionsDirect link to SarvamListenOptions
apiKey?:
string
Sarvam API key. Falls back to SARVAM_API_KEY environment variable.
model?:
SarvamSTTModel
= 'saarika:v2'
Specifies the model to use for speech-to-text conversion. Note:- Default model is saarika:v2 . Available options: saarika:v1, saarika:v2, saarika:flash
languageCode?:
SarvamSTTLanguage
= 'unknown'
Specifies the language of the input audio. This parameter is required to ensure accurate transcription. For the saarika:v1 model, this parameter is mandatory. For the saarika:v2 model, it is optional. unknown: Use this when the language is not known; the API will detect it automatically. Note:- that the saarika:v1 model does not support unknown language code. Available options: unknown, hi-IN, bn-IN, kn-IN, ml-IN, mr-IN, od-IN, pa-IN, ta-IN, te-IN, en-IN, gu-IN
filetype?:
'mp3' | 'wav'
Audio format of the input stream.
方法Direct link to 方法
🌐 Methods
speak()Direct link to speak()
使用 Sarvam 的文本到语音模型将文本转换为语音。
🌐 Converts text to speech using Sarvam's text-to-speech models.
input:
string | NodeJS.ReadableStream
Text or text stream to convert to speech.
options.speaker?:
SarvamVoiceId
= Constructor's speaker value
Voice ID to use for speech synthesis.
返回:Promise<NodeJS.ReadableStream>
🌐 Returns: Promise<NodeJS.ReadableStream>
listen()Direct link to listen()
使用Sarvam的语音识别模型转录音频。
🌐 Transcribes audio using Sarvam's speech recognition models.
input:
NodeJS.ReadableStream
Audio stream to transcribe.
options?:
SarvamListenOptions
Configuration options for speech recognition.
返回:Promise<string>
🌐 Returns: Promise<string>
getSpeakers()Direct link to getSpeakers()
返回可用语音选项的数组。
🌐 Returns an array of available voice options.
返回:Promise<Array<{voiceId: SarvamVoiceId}>>
注意Direct link to 注意
🌐 Notes
- API 密钥可以通过构造函数选项或
SARVAM_API_KEY环境变量提供 - 如果未提供 API 密钥,构造函数将抛出错误
- 该服务通过
https://api.sarvam.ai与 Sarvam AI API 进行通信 - 音频以包含二进制音频数据的流形式返回
- 语音识别支持 mp3 和 wav 音频格式