stream()
The stream()
method is used to interact with the TTS model to produce an audio response stream. This method accepts text
and voice
as parameters.
Parameters
text:
string
The messages to be processed by TTS.
voice:
string
Voice ID to be used with generation.
Returns
audioResult?:
Readable
The generated audio stream
Examples
Basic Audio Stream (ElevenLabs)
import { ElevenLabsTTS } from '@mastra/tts'
const tts = new ElevenLabsTTS({
model: {
name: 'eleven_multilingual_v2',
apiKey: process.env.ELEVENLABS_API_KEY!,
},
});
const voices = await tts.voices();
const voiceId = voices?.[0]?.voice_id!;
const { audioResult } = await tts.stream({ text: "What is AI?", voice: voiceId });
// Create a write stream to simulate real-time playback
const outputPath = path.join(process.cwd(), '/test-outputs/streaming-output.mp3');
const writeStream = createWriteStream(outputPath);
let firstChunkTime: number | null = null;
let lastChunkTime: number | null = null;
let totalChunks = 0;
// Process chunks as they arrive
for await (const chunk of audioResult) {
if (!firstChunkTime) {
firstChunkTime = Date.now();
}
lastChunkTime = Date.now();
totalChunks++;
// Write chunk immediately as it arrives
writeStream.write(chunk);
// Log timing of chunk arrival
console.log(`Received chunk ${totalChunks} at ${lastChunkTime - firstChunkTime!}ms`);
}
writeStream.end()
Basic Audio Stream (OpenAI)
import { OpenAITTS } from '@mastra/tts'
const tts = new OpenAITTS({
model: {
name: 'tts-1',
apiKey: process.env.OPENAI_API_KEY!,
},
});
const voices = await tts.voices();
const voiceId = voices?.[0]?.voice_id!;
const { audioResult } = await tts.stream({ text: "What is AI?", voice: voiceId });
// Create a write stream to simulate real-time playback
const outputPath = path.join(process.cwd(), '/test-outputs/streaming-output.mp3');
const writeStream = createWriteStream(outputPath);
let firstChunkTime: number | null = null;
let lastChunkTime: number | null = null;
let totalChunks = 0;
// Process chunks as they arrive
for await (const chunk of audioResult) {
if (!firstChunkTime) {
firstChunkTime = Date.now();
}
lastChunkTime = Date.now();
totalChunks++;
// Write chunk immediately as it arrives
writeStream.write(chunk);
// Log timing of chunk arrival
console.log(`Received chunk ${totalChunks} at ${lastChunkTime - firstChunkTime!}ms`);
}
writeStream.end()