Skip to main content

ElevenLabs

Mastra 中的 ElevenLabs 语音实现使用 ElevenLabs API 提供高质量的文本转语音(TTS)和语音转文本(STT)功能。

🌐 The ElevenLabs voice implementation in Mastra provides high-quality text-to-speech (TTS) and speech-to-text (STT) capabilities using the ElevenLabs API.

使用示例
Direct link to 使用示例

🌐 Usage Example

import { ElevenLabsVoice } from "@mastra/voice-elevenlabs";

// Initialize with default configuration (uses ELEVENLABS_API_KEY environment variable)
const voice = new ElevenLabsVoice();

// Initialize with custom configuration
const voice = new ElevenLabsVoice({
speechModel: {
name: "eleven_multilingual_v2",
apiKey: "your-api-key",
},
speaker: "custom-speaker-id",
});

// Text-to-Speech
const audioStream = await voice.speak("Hello, world!");

// Get available speakers
const speakers = await voice.getSpeakers();

构造函数参数
Direct link to 构造函数参数

🌐 Constructor Parameters

speechModel?:

ElevenLabsVoiceConfig
= { name: 'eleven_multilingual_v2' }
Configuration for text-to-speech functionality.

speaker?:

string
= '9BWtsMINqrJLrRacOk9x' (Aria voice)
ID of the speaker to use for text-to-speech

ElevenLabsVoiceConfig
Direct link to ElevenLabsVoiceConfig

name?:

ElevenLabsModel
= 'eleven_multilingual_v2'
The ElevenLabs model to use

apiKey?:

string
ElevenLabs API key. Falls back to ELEVENLABS_API_KEY environment variable

方法
Direct link to 方法

🌐 Methods

speak()
Direct link to speak()

使用配置的语音模型和声音将文本转换为语音。

🌐 Converts text to speech using the configured speech model and voice.

input:

string | NodeJS.ReadableStream
Text to convert to speech. If a stream is provided, it will be converted to text first.

options?:

object
Additional options for speech synthesis

options.speaker?:

string
Override the default speaker ID for this request

返回:Promise<NodeJS.ReadableStream>

🌐 Returns: Promise<NodeJS.ReadableStream>

getSpeakers()
Direct link to getSpeakers()

返回一个可用语音选项的数组,每个节点包含:

🌐 Returns an array of available voice options, where each node contains:

voiceId:

string
Unique identifier for the voice

name:

string
Display name of the voice

language:

string
Language code for the voice

gender:

string
Gender of the voice

listen()
Direct link to listen()

使用 ElevenLabs 语音转文本 API 将音频输入转换为文本。

🌐 Converts audio input to text using ElevenLabs Speech-to-Text API.

input:

NodeJS.ReadableStream
A readable stream containing the audio data to transcribe

options?:

object
Configuration options for the transcription

options 对象支持以下属性:

🌐 The options object supports the following properties:

language_code?:

string
ISO language code (e.g., 'en', 'fr', 'es')

tag_audio_events?:

boolean
Whether to tag audio events like [MUSIC], [LAUGHTER], etc.

num_speakers?:

number
Number of speakers to detect in the audio

filetype?:

string
Audio file format (e.g., 'mp3', 'wav', 'ogg')

timeoutInSeconds?:

number
Request timeout in seconds

maxRetries?:

number
Maximum number of retry attempts

abortSignal?:

AbortSignal
Signal to abort the request

返回:Promise<string> - 一个解析为转录文本的 Promise

🌐 Returns: Promise<string> - A Promise that resolves to the transcribed text

重要提示
Direct link to 重要提示

🌐 Important Notes

  1. 需要 ElevenLabs API 密钥。可以通过 ELEVENLABS_API_KEY 环境变量设置,或在构造函数中传入。
  2. 默认扬声器设置为 Aria(ID:'9BWtsMINqrJLrRacOk9x')。
  3. ElevenLabs 不支持语音转文字功能。
  4. 可以使用 getSpeakers() 方法检索可用的语音,该方法返回每个语音的详细信息,包括语言和性别。