Skip to main content

Azure

Mastra 中的 AzureVoice 类使用 Microsoft Azure 认知服务提供文本转语音和语音转文本功能。

🌐 The AzureVoice class in Mastra provides text-to-speech and speech-to-text capabilities using Microsoft Azure Cognitive Services.

使用示例
Direct link to 使用示例

🌐 Usage Example

这需要 Azure 语音服务凭据,可以通过环境变量提供,也可以直接在配置中提供:

🌐 This requires Azure Speech Services credentials that can be provided through environment variables or directly in the configuration:

import { AzureVoice } from "@mastra/voice-azure";

// Initialize with configuration
const voice = new AzureVoice({
speechModel: {
apiKey: "your-azure-speech-api-key", // Or use AZURE_API_KEY env var
region: "eastus", // Or use AZURE_REGION env var
voiceName: "en-US-AriaNeural", // Optional: specific voice for TTS
},
listeningModel: {
apiKey: "your-azure-speech-api-key", // Or use AZURE_API_KEY env var
region: "eastus", // Or use AZURE_REGION env var
language: "en-US", // Optional: recognition language for STT
},
speaker: "en-US-JennyNeural", // Optional: default voice
});

// Convert text to speech
const audioStream = await voice.speak("Hello, how can I help you?", {
speaker: "en-US-GuyNeural", // Optional: override default voice
});

// Convert speech to text
const text = await voice.listen(audioStream);

配置
Direct link to 配置

🌐 Configuration

构造函数选项
Direct link to 构造函数选项

🌐 Constructor Options

speechModel?:

AzureSpeechConfig
Configuration for text-to-speech synthesis.

listeningModel?:

AzureSpeechConfig
Configuration for speech-to-text recognition.

speaker?:

string
Default voice ID for speech synthesis.

AzureSpeechConfig
Direct link to AzureSpeechConfig

用于语音合成(speechModel)和识别(listeningModel)的配置对象。

🌐 Configuration object for speech synthesis (speechModel) and recognition (listeningModel).

apiKey?:

string
Azure Speech Services API key (NOT Azure OpenAI key). Falls back to AZURE_API_KEY environment variable.

region?:

string
Azure region (e.g., 'eastus', 'westeurope'). Falls back to AZURE_REGION environment variable.

voiceName?:

string
Voice ID for speech synthesis (e.g., 'en-US-AriaNeural', 'en-US-JennyNeural'). Only used in speechModel. See voice list below.

language?:

string
Recognition language code (e.g., 'en-US', 'fr-FR'). Only used in listeningModel.

方法
Direct link to 方法

🌐 Methods

speak()
Direct link to speak()

使用 Azure 的神经文本转语音服务将文本转换为语音。

🌐 Converts text to speech using Azure's neural text-to-speech service.

input:

string | NodeJS.ReadableStream
Text or text stream to convert to speech.

options.speaker?:

string
= Constructor's speaker value
Voice ID to use for speech synthesis (e.g., 'en-US-JennyNeural'). Overrides the default voice.

返回:Promise<NodeJS.ReadableStream> - WAV 格式的音频流

🌐 Returns: Promise<NodeJS.ReadableStream> - Audio stream in WAV format

listen()
Direct link to listen()

使用 Azure 的语音转文本服务转录音频。

🌐 Transcribes audio using Azure's speech-to-text service.

audioStream:

NodeJS.ReadableStream
Audio stream to transcribe. Must be in WAV format.

返回值:Promise<string> - 从音频中识别出的文本

🌐 Returns: Promise<string> - The recognized text from the audio

注意: 语言和识别设置在初始化时通过 listeningModel 配置进行配置,而不是作为选项传递给此方法。

getSpeakers()
Direct link to getSpeakers()

返回一个可用语音选项的数组(200 多种语音),其中每个节点包含:

🌐 Returns an array of available voice options (200+ voices), where each node contains:

voiceId:

string
Unique identifier for the voice (e.g., 'en-US-JennyNeural', 'fr-FR-DeniseNeural')

language:

string
Language code extracted from voice ID (e.g., 'en', 'fr')

region:

string
Region code extracted from voice ID (e.g., 'US', 'GB', 'FR')

返回:Promise<Array<{ voiceId: string; language: string; region: string; }>>

重要提示
Direct link to 重要提示

🌐 Important Notes

Azure 语音服务 vs Azure OpenAI
Direct link to Azure 语音服务 vs Azure OpenAI

🌐 Azure Speech Services vs Azure OpenAI

⚠️ 重要: 此软件包使用 Azure 语音服务,与 Azure OpenAI 服务 不同。

  • 不要将你的 AZURE_OPENAI_API_KEY 用于此封装
  • 务必 使用 Azure 语音服务订阅密钥(可在 Azure 门户 “语音服务” 下获取)
  • 这些是具有不同 API 密钥和端点的独立 Azure 资源

环境变量
Direct link to 环境变量

🌐 Environment Variables

API 密钥和区域可以通过构造函数选项或环境变量提供:

🌐 API keys and regions can be provided via constructor options or environment variables:

  • AZURE_API_KEY - 你的 Azure 语音服务订阅密钥
  • AZURE_REGION - 你的 Azure 区域(例如 'eastus'、'westeurope')

语音功能
Direct link to 语音功能

🌐 Voice Capabilities

  • Azure 提供超过 200 种神经语音,覆盖 50 多种语言
  • 每个语音ID的格式如下:{language}-{region}-{name}Neural(例如,'en-US-JennyNeural')
  • 一些语音包括多语言支持或高清质量版本
  • 音频输出为 WAV 格式
  • 用于识别的音频输入必须是 WAV 格式

可用语音
Direct link to 可用语音

🌐 Available Voices

Azure 提供 200 多种神经语音,覆盖多种语言。一些常用的英文语音包括:

🌐 Azure provides 200+ neural voices across many languages. Some popular English voices include:

  • 美式英语:
    • en-US-AriaNeural(女性,默认)
    • en-US-JennyNeural(女)
    • en-US-GuyNeural(男)
    • en-US-DavisNeural(男)
    • en-US-AvaNeural(女)
    • en-US-AndrewNeural(男)
  • 英式英语:
    • en-GB-SoniaNeural(女)
    • en-GB-RyanNeural(男)
    • en-GB-LibbyNeural(女)
  • 澳大利亚英语:
    • en-AU-NatashaNeural(女)
    • en-AU-WilliamNeural(男)

要获取所有200多个语音的完整列表:

🌐 To get a complete list of all 200+ voices:

const voices = await voice.getSpeakers();
console.log(voices); // Array of { voiceId, language, region }

欲了解更多信息,请参阅 Azure 神经语音合成文档

🌐 For more information, see the Azure Neural TTS documentation.