voice.getSpeakers()
getSpeakers()
メソッドは、音声プロバイダーから利用可能な音声オプション(スピーカー)のリストを取得します。これにより、アプリケーションはユーザーに音声の選択肢を提示したり、異なるコンテキストに最も適した音声をプログラムで選択したりすることができます。
使用例
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 });
パラメータ
このメソッドはパラメータを受け付けません。
戻り値
Promise<Array<{ voiceId: string } & TSpeakerMetadata>>:
Promise
音声オプションの配列を解決するプロミスで、各オプションには少なくともvoiceIdプロパティが含まれ、プロバイダー固有のメタデータが追加される場合があります。
プロバイダー固有のメタデータ
異なる音声プロバイダーは、それぞれの音声に対して異なるメタデータを返します:
OpenAI
voiceId:
string
音声の一意識別子(例:'alloy'、'echo'、'fable'、'onyx'、'nova'、'shimmer')
注意事項
- 利用可能な音声は、プロバイダーによって大きく異なります
- 一部のプロバイダーでは、音声の完全なリストを取得するために認証が必要な場合があります
- プロバイダーがこのメソッドをサポートしていない場合、デフォルトの実装では空の配列が返されます
- パフォーマンス上の理由から、リストを頻繁に表示する必要がある場合は結果をキャッシュすることを検討してください
voiceId
プロパティはすべてのプロバイダーで確実に存在しますが、追加のメタデータは異なります