Voice & Speech (TTS/STT)
Relevant source files
The following files were used as context for generating this wiki page:- voice/azure/architecture-llm.md
- templates/template-pdf-to-audio/README.md
- voice/sarvam/src/index.test.ts
- DEVELOPMENT.md
- voice/openai/package.json
- voice/google/package.json
- voice/sarvam/package.json
- voice/azure/README.md
- voice/cloudflare/package.json
- voice/speechify/package.json
- templates/template-pdf-to-audio/package.json
The Mastra framework provides a modular system for integrating voice capabilities, including Text-to-Speech (TTS) and Speech-to-Text (STT). This system is built around a core abstraction with multiple pluggable providers, allowing developers to easily add voice synthesis and recognition to their AI agents and workflows. The primary goal is to offer a standardized interface for voice interactions, regardless of the underlying service provider.
The voice components are located in the voice/ directory of the monorepo. Each sub-directory represents a specific provider integration, such as Azure, OpenAI, Google, and others. These packages can be built individually or all at once using the pnpm build:speech command.
Sources: DEVELOPMENT.md, voice/azure/architecture-llm.md
Core Architecture
The voice system is designed around a central abstract class, MastraVoice, which is part of the @mastra/core package. Every voice provider must extend this class and implement its abstract methods, ensuring a consistent API across all integrations.
Sources: voice/azure/architecture-llm.md
This diagram shows the inheritance structure, where specific provider classes like AzureVoice and OpenAIVoice extend the base MastraVoice class.
MastraVoice Interface
The MastraVoice class defines the contract that all voice providers must adhere to.
| Method | Description | Return Type |
|---|---|---|
speak(input, options) | Converts text to speech (TTS). The input can be a string or a ReadableStream. | Promise<NodeJS.ReadableStream> |
listen(audioStream) | Transcribes an audio stream to text (STT). | Promise<string> |
getSpeakers() | Returns a list of available voice speakers for TTS. | Promise<Array<Speaker>> |
getListener() | Checks if STT capabilities are enabled for the provider. | Promise<{ enabled: boolean }> |
Sources: voice/azure/architecture-llm.md
Available Voice Providers
Mastra supports a variety of third-party voice service providers. Each provider is a separate package within the voice/ directory.
| Provider | Package Name | Description |
|---|---|---|
| Azure | @mastra/voice-azure | Integration with Microsoft Azure's Cognitive Services Speech SDK. |
| Cloudflare | @mastra/voice-cloudflare | Integration with Cloudflare's AI voice services. |
@mastra/voice-google | Integration with Google Cloud Text-to-Speech and Speech-to-Text. | |
| OpenAI | @mastra/voice-openai | Integration with OpenAI's TTS and STT (Whisper) models. |
| Sarvam | @mastra/voice-sarvam | Integration with Sarvam AI for Indian languages. |
| Speechify | @mastra/voice-speechify | Integration with the Speechify API. |
Sources: DEVELOPMENT.md, voice/azure/README.md, voice/cloudflare/package.json, voice/google/package.json, voice/openai/package.json, voice/sarvam/package.json, voice/speechify/package.json
Provider Implementation Example: AzureVoice
The @mastra/voice-azure package is a representative example of how a voice provider is implemented. It wraps the microsoft-cognitiveservices-speech-sdk.
Sources: voice/azure/architecture-llm.md
Configuration
The AzureVoice class is initialized with a configuration object for TTS (speechModel) and STT (listeningModel). It falls back to environment variables AZURE_API_KEY and AZURE_REGION if credentials are not provided directly.
| Parameter | Type | Description | Environment Variable |
|---|---|---|---|
speechModel.apiKey | string | Azure Speech Services API key for TTS. | AZURE_API_KEY |
speechModel.region | string | Azure region for TTS (e.g., 'eastus'). | AZURE_REGION |
speechModel.voiceName | string | Default voice for TTS (e.g., 'en-US-AriaNeural'). | N/A |
listeningModel.apiKey | string | Azure Speech Services API key for STT. | AZURE_API_KEY |
listeningModel.region | string | Azure region for STT. | AZURE_REGION |
listeningModel.language | string | Recognition language for STT (e.g., 'en-US'). | N/A |
Sources: voice/azure/architecture-llm.md, voice/azure/README.md
speak() Method Flow
The speak method converts a text string or stream into a WAV audio stream. It creates a new SpeechSynthesizer instance for each request.
This diagram illustrates the data flow for the speak method in the Azure provider.
Sources: voice/azure/architecture-llm.md
listen() Method Flow
The listen method transcribes a WAV audio stream into text. It buffers the entire stream into memory before sending it to the Azure SDK for recognition.
This diagram illustrates the data flow for the listen method in the Azure provider.
Sources: voice/azure/architecture-llm.md
Usage in Applications
Voice capabilities are typically integrated into Mastra applications through tools and workflows. The template-pdf-to-audio project provides a clear example of this pattern.
Sources: templates/template-pdf-to-audio/README.md
PDF to Audio Workflow
This template uses a workflow to orchestrate the process of converting a PDF to an audio file. It uses the @mastra/voice-openai package for voice synthesis.
The following diagram shows the steps in the pdfToAudioWorkflow.
OpenAI Voice Provider
The template-pdf-to-audio uses the OpenAI provider, which offers several distinct voices.
- Available Voices:
alloy,echo,fable,onyx,nova,shimmer - Configurable Speed: Speaking speed can be adjusted from 0.1x to 4.0x.
The textToSpeechTool in this template is a wrapper around the OpenAIVoice.speak() method, demonstrating how the core voice abstraction can be used within a higher-level tool.
Sources: templates/template-pdf-to-audio/README.md, templates/template-pdf-to-audio/package.json
Example Instantiation
The following test code shows how a voice provider (SarvamVoice) is instantiated with a configuration object and used to perform TTS and STT operations. This pattern is consistent across all voice providers.
// From voice/sarvam/src/index.test.ts
const voice = new SarvamVoice({
speechModel: {
model: 'bulbul:v1',
apiKey: process.env.SARVAM_API_KEY!,
language: 'en-IN',
},
});
// ...
it('should test speak method', async () => {
const audioStream = await voice.speak('Hello from Sarvam AI!', {
speaker: voiceId,
});
// ...
}, 30000);
it('should transcribe audio from fixture file', async () => {
const fixturePath = path.join(process.cwd(), '__fixtures__', 'voice-test.m4a');
const audioStream = createReadStream(fixturePath);
const text = await voice.listen(audioStream);
// ...
}, 15000);
Sources: voice/sarvam/src/index.test.ts:10-17, 49-55, 107-112
Conclusion
Mastra's voice and speech system provides a powerful and flexible way to integrate TTS and STT capabilities into AI applications. By using a common MastraVoice interface and a pluggable provider architecture, developers can choose the best voice service for their needs without altering their core application logic. The separation of concerns into distinct packages for each provider ensures modularity and maintainability.