voice.getSpeakers()
getSpeakers() 方法从语音提供商处获取可用的语音选项(发声人)列表。这允许应用向用户展示语音选择,或在不同的场景中以编程方式选择最合适的语音。
🌐 The getSpeakers() method retrieves a list of available voice options (speakers) from the voice provider. This allows applications to present users with voice choices or programmatically select the most appropriate voice for different contexts.
使用示例Direct link to 使用示例
🌐 Usage Example
import { OpenAIVoice } from "@mastra/voice-openai";
import { ElevenLabsVoice } from "@mastra/voice-elevenlabs";
// Initialize voice providers
const openaiVoice = new OpenAIVoice();
const elevenLabsVoice = new ElevenLabsVoice({
apiKey: process.env.ELEVENLABS_API_KEY,
});
// Get available speakers from OpenAI
const openaiSpeakers = await openaiVoice.getSpeakers();
console.log("OpenAI voices:", openaiSpeakers);
// Example output: [{ voiceId: "alloy" }, { voiceId: "echo" }, { voiceId: "fable" }, ...]
// Get available speakers from ElevenLabs
const elevenLabsSpeakers = await elevenLabsVoice.getSpeakers();
console.log("ElevenLabs voices:", elevenLabsSpeakers);
// Example output: [{ voiceId: "21m00Tcm4TlvDq8ikWAM", name: "Rachel" }, ...]
// Use a specific voice for speech
const text = "Hello, this is a test of different voices.";
await openaiVoice.speak(text, { speaker: openaiSpeakers[2].voiceId });
await elevenLabsVoice.speak(text, { speaker: elevenLabsSpeakers[0].voiceId });
参数Direct link to 参数
🌐 Parameters
此方法不接受任何参数。
🌐 This method does not accept any parameters.
返回值Direct link to 返回值
🌐 Return Value
Promise<Array<{ voiceId: string } & TSpeakerMetadata>>:
Promise
A promise that resolves to an array of voice options, where each option contains at least a voiceId property and may include additional provider-specific metadata.
特定于提供商的元数据Direct link to 特定于提供商的元数据
🌐 Provider-Specific Metadata
不同的语音提供商会为他们的语音返回不同的元数据:
🌐 Different voice providers return different metadata for their voices:
- OpenAI
- OpenAI Realtime
- Deepgram
- ElevenLabs
- Azure
- Murf
- PlayAI
- Speechify
- Sarvam
voiceId:
string
Unique identifier for the voice (e.g., 'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer')
voiceId:
string
Unique identifier for the voice (e.g., 'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer')
voiceId:
string
Unique identifier for the voice
language:
string
Language code embedded in the voice ID (e.g., 'en')
voiceId:
string
Unique identifier for the voice
name:
string
Human-readable name of the voice
category:
string
Category of the voice (e.g., 'premade', 'cloned')
voiceId:
string
Unique identifier for the voice
languageCodes:
string[]
Array of language codes supported by the voice (e.g., ['en-US'])
voiceId:
string
Unique identifier for the voice
language:
string
Language code extracted from the voice ID (e.g., 'en')
region:
string
Region code extracted from the voice ID (e.g., 'US')
voiceId:
string
Unique identifier for the voice
name:
string
Name of the voice (same as voiceId)
language:
string
Language code extracted from the voice ID (e.g., 'en')
gender:
string
Gender of the voice (always 'neutral' in current implementation)
voiceId:
string
Unique identifier for the voice (S3 URL to manifest.json)
name:
string
Human-readable name of the voice (e.g., 'Angelo', 'Arsenio')
accent:
string
Accent of the voice (e.g., 'US', 'Irish', 'US African American')
gender:
string
Gender of the voice ('M' or 'F')
age:
string
Age category of the voice (e.g., 'Young', 'Middle')
style:
string
Speaking style of the voice (e.g., 'Conversational')
voiceId:
string
Unique identifier for the voice
name:
string
Human-readable name of the voice
language:
string
Language code of the voice (e.g., 'en-US')
voiceId:
string
Unique identifier for the voice
name:
string
Human-readable name of the voice
language:
string
Language of the voice (e.g., 'english', 'hindi')
gender:
string
Gender of the voice ('male' or 'female')
注意Direct link to 注意
🌐 Notes
- 不同提供商之间可用的语音差别很大
- 某些提供商可能需要身份验证才能获取完整的语音列表
- 如果提供程序不支持此方法,默认实现将返回一个空数组
- 出于性能考虑,如果需要频繁显示列表,建议缓存结果
voiceId属性在所有提供商中都是保证存在的,但其他元数据可能有所不同