Skip to main content

Cloudflare

Mastra 中的 CloudflareVoice 类使用 Cloudflare Workers AI 提供文本转语音功能。该服务提供商专注于高效、低延迟的语音合成,适用于边缘计算环境。

🌐 The CloudflareVoice class in Mastra provides text-to-speech capabilities using Cloudflare Workers AI. This provider specializes in efficient, low-latency speech synthesis suitable for edge computing environments.

使用示例
Direct link to 使用示例

🌐 Usage Example

import { CloudflareVoice } from "@mastra/voice-cloudflare";

// Initialize with configuration
const voice = new CloudflareVoice({
speechModel: {
name: "@cf/meta/m2m100-1.2b",
apiKey: "your-cloudflare-api-token",
accountId: "your-cloudflare-account-id",
},
speaker: "en-US-1", // Default voice
});

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

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

配置
Direct link to 配置

🌐 Configuration

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

🌐 Constructor Options

speechModel?:

CloudflareSpeechConfig
Configuration for text-to-speech synthesis.

speaker?:

string
= 'en-US-1'
Default voice ID for speech synthesis.

CloudflareSpeechConfig
Direct link to CloudflareSpeechConfig

name?:

string
= '@cf/meta/m2m100-1.2b'
Model name to use for TTS.

apiKey?:

string
Cloudflare API token with Workers AI access. Falls back to CLOUDFLARE_API_TOKEN environment variable.

accountId?:

string
Cloudflare account ID. Falls back to CLOUDFLARE_ACCOUNT_ID environment variable.

方法
Direct link to 方法

🌐 Methods

speak()
Direct link to speak()

使用 Cloudflare 的文本转语音服务将文本转换为语音。

🌐 Converts text to speech using Cloudflare's 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.

options.format?:

string
= 'mp3'
Output audio format.

返回: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 (e.g., 'en-US-1')

language:

string
Language code of the voice (e.g., 'en-US')

注意
Direct link to 注意

🌐 Notes

  • API 令牌可以通过构造函数选项或环境变量(CLOUDFLARE_API_TOKEN 和 CLOUDFLARE_ACCOUNT_ID)提供
  • Cloudflare Workers AI 针对低延迟的边缘计算进行了优化
  • 该提供商仅支持文本转语音(TTS)功能,不支持语音转文本(STT)
  • 该服务能够很好地与其他 Cloudflare Workers 产品集成
  • 用于生产环境,请确保你的 Cloudflare 账户拥有相应的 Workers AI 订阅
  • 与其他一些提供商相比,语音选项更有限,但边缘的性能非常出色

🌐 Related Providers

如果你除了需要文字转语音功能外,还需要语音转文字功能,可以考虑使用以下提供商之一:

🌐 If you need speech-to-text capabilities in addition to text-to-speech, consider using one of these providers:

  • OpenAI - 提供语音合成 (TTS) 和语音识别 (STT)
  • Google - 提供语音合成(TTS)和语音识别(STT)
  • Azure - 提供语音合成(TTS)和语音识别(STT)