TTS.generate()
The .generate()
method is used to interact with the TTS model to produce an audio response. 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 Generation (ElevenLabs)
import { ElevenLabsTTS } from "@mastra/speech-elevenlabs";
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.generate({
text: "What is AI?",
voice: voiceId,
});
await writeFile(
path.join(process.cwd(), "/test-outputs/generate-output.mp3"),
audioBuffer,
);
Basic Audio Generation (OpenAI)
import { OpenAITTS } from "@mastra/speech-openai";
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.generate({
text: "What is AI?",
voice: voiceId,
});
const outputPath = path.join(
process.cwd(),
"test-outputs/open-aigenerate-test.mp3",
);
writeFileSync(outputPath, audioResult);
Basic Audio Generation (PlayAI)
import { PlayAITTS } from "@mastra/speech-playai";
const tts = new PlayAITTS({
model: {
name: "PlayDialog",
apiKey: process.env.PLAYAI_API_KEY!,
},
userId: process.env.PLAYAI_USER_ID!,
});
const voices = await tts.voices();
const voiceId = voices?.[0]?.voice_id!;
const { audioResult } = await tts.generate({
text: "What is AI?",
voice: voiceId,
});
const outputPath = path.join(
process.cwd(),
"test-outputs/open-aigenerate-test.mp3",
);
writeFileSync(outputPath, audioResult);
Azure Generation
import { AzureTTS } from "@mastra/speech-azure";
const tts = new AzureTTS({
model: {
name: "en-US-JennyNeural",
apiKey: process.env.AZURE_API_KEY,
region: process.env.AZURE_REGION,
},
});
const { audioResult } = await tts.generate({ text: "What is AI?" });
await writeFile(
path.join(process.cwd(), "/test-outputs/azure-output.mp3"),
audioResult,
);
Deepgram Generation
import { DeepgramTTS } from "@mastra/speech-deepgram";
const tts = new DeepgramTTS({
model: {
name: "aura",
voice: "asteria-en",
apiKey: process.env.DEEPGRAM_API_KEY,
},
});
const { audioResult } = await tts.generate({ text: "What is AI?" });
await writeFile(
path.join(process.cwd(), "/test-outputs/deepgram-output.mp3"),
audioResult,
);
Google Generation
import { GoogleTTS } from "@mastra/speech-google";
const tts = new GoogleTTS({
model: {
name: "en-US-Standard-A",
credentials: process.env.GOOGLE_CREDENTIALS,
},
});
const { audioResult } = await tts.generate({ text: "What is AI?" });
await writeFile(
path.join(process.cwd(), "/test-outputs/google-output.mp3"),
audioResult,
);
IBM Generation
import { IbmTTS } from "@mastra/speech-ibm";
const tts = new IbmTTS({
model: {
voice: "en-US_AllisonV3Voice",
apiKey: process.env.IBM_API_KEY,
},
});
const { audioResult } = await tts.generate({ text: "What is AI?" });
await writeFile(
path.join(process.cwd(), "/test-outputs/ibm-output.mp3"),
audioResult,
);
Murf Generation
import { MurfTTS } from "@mastra/speech-murf";
const tts = new MurfTTS({
model: {
name: "GEN2",
voice: "en-US-natalie",
apiKey: process.env.MURF_API_KEY,
},
});
const { audioResult } = await tts.generate({ text: "What is AI?" });
await writeFile(
path.join(process.cwd(), "/test-outputs/murf-output.mp3"),
audioResult,
);
Related Methods
For streaming audio responses, see the stream()
method documentation.