voice.listen()
The listen() method is a core function available in all Mastra voice providers that converts speech to text. It takes an audio stream as input and returns the transcribed text.
ParametersParameters的直接链接
audioStream:
NodeJS.ReadableStream | unknown
Audio to transcribe. This is usually a file stream or a microphone stream; some realtime providers accept other audio input types.
options?:
object
Provider-specific options for speech recognition
Return valueReturn value的直接链接
Returns one of the following:
Promise<string>: A promise that resolves to the transcribed textPromise<NodeJS.ReadableStream>: A promise that resolves to a stream of transcribed text (for streaming transcription)Promise<void>: For real-time providers that emit 'writing' events instead of returning text directly
Provider-specific optionsProvider-specific options的直接链接
Each voice provider may support additional options specific to their implementation. Here are some examples:
OpenAIOpenAI的直接链接
options?:
Options
Configuration options.
Options
filetype?:
string
Audio file format (e.g., 'mp3', 'wav', 'm4a')
prompt?:
string
Text to guide the model's transcription
language?:
string
Language code (e.g., 'en', 'fr', 'de')
GoogleGoogle的直接链接
options?:
Options
Configuration options.
Options
stream?:
boolean
Whether to use streaming recognition
config?:
object
Recognition configuration from Google Cloud Speech-to-Text API
DeepgramDeepgram的直接链接
options?:
Options
Configuration options.
Options
model?:
string
Deepgram model to use for transcription
language?:
string
Language code for transcription
Usage exampleUsage example的直接链接
import { OpenAIVoice } from '@mastra/voice-openai'
import { getMicrophoneStream } from '@mastra/node-audio'
import { createReadStream } from 'fs'
import path from 'path'
// Initialize a voice provider
const voice = new OpenAIVoice({
listeningModel: {
name: 'whisper-1',
apiKey: process.env.OPENAI_API_KEY,
},
})
// Basic usage with a file stream
const audioFilePath = path.join(process.cwd(), 'audio.mp3')
const audioStream = createReadStream(audioFilePath)
const transcript = await voice.listen(audioStream, {
filetype: 'mp3',
})
console.log('Transcribed text:', transcript)
// Using a microphone stream
const microphoneStream = getMicrophoneStream() // Assume this function gets audio input
const transcription = await voice.listen(microphoneStream)
// With provider-specific options
const transcriptWithOptions = await voice.listen(audioStream, {
language: 'en',
prompt: 'This is a conversation about artificial intelligence.',
})
Using with CompositeVoiceusing-with-compositevoice的直接链接
When using CompositeVoice, the listen() method delegates to the configured listening provider:
import { CompositeVoice } from '@mastra/core/voice'
import { OpenAIVoice } from '@mastra/voice-openai'
import { PlayAIVoice } from '@mastra/voice-playai'
const voice = new CompositeVoice({
input: new OpenAIVoice(),
output: new PlayAIVoice(),
})
// This will use the OpenAIVoice provider
const transcript = await voice.listen(audioStream)
Using AI SDK Model ProvidersUsing AI SDK Model Providers的直接链接
You can also use AI SDK transcription models directly with CompositeVoice:
import { CompositeVoice } from '@mastra/core/voice'
import { openai } from '@ai-sdk/openai'
import { groq } from '@ai-sdk/groq'
// Use AI SDK transcription models
const voice = new CompositeVoice({
input: openai.transcription('whisper-1'), // AI SDK model
output: new PlayAIVoice(), // Mastra provider
})
// Works the same way
const transcript = await voice.listen(audioStream)
// Provider-specific options can be passed through
const transcriptWithOptions = await voice.listen(audioStream, {
providerOptions: {
openai: {
language: 'en',
prompt: 'This is about AI',
},
},
})
See the CompositeVoice reference for more details on AI SDK integration.
Realtime voice providersRealtime voice providers的直接链接
When using realtime voice providers like OpenAIRealtimeVoice, the listen() method behaves differently:
- Instead of returning transcribed text, it emits 'writing' events with the transcribed text
- You need to register an event listener to receive the transcription
import { OpenAIRealtimeVoice } from '@mastra/voice-openai-realtime'
import { getMicrophoneStream } from '@mastra/node-audio'
const voice = new OpenAIRealtimeVoice()
await voice.connect()
// Register event listener for transcription
voice.on('writing', ({ text, role }) => {
console.log(`${role}: ${text}`)
})
// This will emit 'writing' events instead of returning text
const microphoneStream = getMicrophoneStream()
await voice.listen(microphoneStream)
NotesNotes的直接链接
- Not all voice providers support speech-to-text functionality (e.g., PlayAI, Speechify)
- The behavior of
listen()may vary slightly between providers, but all implementations follow the same basic interface - When using a realtime voice provider, the method might not return text directly but instead emit a 'writing' event
- The audio format supported depends on the provider. Common formats include MP3, WAV, and M4A
- Some providers support streaming transcription, where text is returned as it's transcribed
- For best performance, consider closing or ending the audio stream when you're done with it
Related methodsRelated methods的直接链接
- voice.speak(): Converts text to speech
- voice.send(): Sends audio data to the voice provider in real-time
- voice.on(): Registers an event listener for voice events