CompositeVoice
The CompositeVoice class allows you to combine different voice providers for text-to-speech and speech-to-text operations. This is particularly useful when you want to use the best provider for each operation - for example, using OpenAI for speech-to-text and PlayAI for text-to-speech.
CompositeVoice supports both Mastra voice providers and AI SDK model providers
Constructor parametersDirect link to Constructor parameters
input?:
output?:
realtime?:
MethodsDirect link to Methods
speak()Direct link to speak
Converts text to speech using the configured speaking provider.
input:
options?:
Notes:
- If a realtime provider is configured, it handles the call; otherwise the speaking provider is used
- Throws a
MastraErroronly when neither a realtime nor a speaking provider is configured - Options are passed through to the provider that handles the call
- Returns a stream of audio data, or
voidwhen a realtime provider handles the call
listen()Direct link to listen
Converts speech to text using the configured listening provider.
audioStream:
options?:
Notes:
- If a realtime provider is configured, it handles the call; otherwise the listening provider is used
- Throws a
MastraErroronly when neither a realtime nor a listening provider is configured - Options are passed through to the provider that handles the call
- Returns a string or a stream of transcribed text, depending on the provider, or
voidwhen a realtime provider emits results through events
getSpeakers()Direct link to getspeakers
Returns a list of available voices from the realtime provider, or from the speaking provider when no realtime provider is configured, where each node contains:
voiceId:
key?:
Notes:
- Returns voices from the realtime provider when one is configured, otherwise from the speaking provider
- Throws a
MastraErrorwhen neither a realtime nor a speaking provider is configured - Each voice object will have at least a voiceId property
- Additional voice properties depend on the provider
Usage examplesDirect link to Usage examples
Using Mastra Voice ProvidersDirect link to Using Mastra Voice Providers
import { CompositeVoice } from '@mastra/core/voice'
import { OpenAIVoice } from '@mastra/voice-openai'
import { PlayAIVoice } from '@mastra/voice-playai'
// Create voice providers
const openai = new OpenAIVoice()
const playai = new PlayAIVoice()
// Use OpenAI for listening (speech-to-text) and PlayAI for speaking (text-to-speech)
const voice = new CompositeVoice({
input: openai,
output: playai,
})
// Convert speech to text using OpenAI
const text = await voice.listen(audioStream)
// Convert text to speech using PlayAI
const audio = await voice.speak('Hello, world!')
Using AI SDK Model ProvidersDirect link to Using AI SDK Model Providers
You can pass AI SDK transcription and speech models directly to CompositeVoice:
import { CompositeVoice } from '@mastra/core/voice'
import { openai } from '@ai-sdk/openai'
import { elevenlabs } from '@ai-sdk/elevenlabs'
// Use AI SDK models directly - they will be auto-wrapped
const voice = new CompositeVoice({
input: openai.transcription('whisper-1'), // AI SDK transcription
output: elevenlabs.speech('eleven_turbo_v2'), // AI SDK speech
})
// Works the same way as with Mastra providers
const text = await voice.listen(audioStream)
const audio = await voice.speak('Hello from AI SDK!')
Mix and MatchDirect link to Mix and Match
You can combine Mastra providers with AI SDK models:
import { CompositeVoice } from '@mastra/core/voice'
import { PlayAIVoice } from '@mastra/voice-playai'
import { groq } from '@ai-sdk/groq'
const voice = new CompositeVoice({
input: groq.transcription('whisper-large-v3'), // AI SDK for STT
output: new PlayAIVoice(), // Mastra for TTS
})