Coze Text-to-Speech (TTS)
Convert text to natural-sounding speech using Coze API.
Setup
1. Get your API Key:
Get a key from Coze Platform
2. Set it in your environment:
CODEBLOCK0
Supported Output Formats
- - MP3 - Default format, widely compatible
- OGG_OPUS - Optimized for streaming and messaging
- WAV - Uncompressed audio
- PCM - Raw audio data
Usage
Basic TTS
Convert text to speech with default settings:
CODEBLOCK1
Save to Specific File
CODEBLOCK2
Use Different Voice
CODEBLOCK3
Change Output Format
CODEBLOCK4
Full Options
CODEBLOCK5
Parameters:
- -
text (required): Text to convert to speech - INLINECODE1 (optional): Output file path (default: auto-generated)
- INLINECODE2 (optional): Voice ID (default: 1)
- INLINECODE3 (optional): Output format - mp3/ogg_opus/wav/pcm (default: mp3)
Output
The script saves the audio file and outputs:
- - File path
- File size
- Audio duration (if ffprobe is available)
Example output:
CODEBLOCK6
Workflow Examples
Generate Notification Audio
CODEBLOCK7
Create Voice Greeting
CODEBLOCK8
Generate OGG for Messaging
CODEBLOCK9
Batch Generate
CODEBLOCK10
Integration with Other Skills
Combine with coze-asr for voice conversation:
CODEBLOCK11
Troubleshooting
Authentication Error:
- - Check COZEAPIKEY is set correctly
- Verify API key has TTS permissions
Invalid Voice ID:
- - Voice ID should be a number (int64 format)
- Try voice_id: 1 as default
File Not Created:
- - Check write permissions in output directory
- Ensure sufficient disk space
Limitations
- - Text length limits apply (check Coze documentation)
- Rate limits may apply based on your plan
- Some voices may not support all output formats
API Reference
- - Endpoint: INLINECODE5
- Authentication: Bearer token (COZEAPIKEY)
- Content-Type: application/json
Required Environment Variables
| Variable | Description | Required |
|---|
| INLINECODE6 | Coze API authentication key | Yes |
Required Tools
| Tool | Purpose | Required |
|---|
| INLINECODE7 | JSON processing | Yes |
| INLINECODE8 |
Audio duration detection | Optional |
License
MIT
Coze 文本转语音 (TTS)
使用 Coze API 将文本转换为自然语音。
设置
1. 获取您的 API 密钥:
从 Coze 平台 获取密钥
2. 在环境中设置:
bash
export COZEAPIKEY=your-key-here
支持的输出格式
- - MP3 - 默认格式,广泛兼容
- OGG_OPUS - 针对流媒体和消息传递优化
- WAV - 未压缩音频
- PCM - 原始音频数据
使用方法
基本 TTS
使用默认设置将文本转换为语音:
bash
bash scripts/texttospeech.sh 你好,这是测试语音
保存到特定文件
bash
bash scripts/texttospeech.sh 你好世界 -o output.mp3
使用不同音色
bash
bash scripts/texttospeech.sh 你好 -v 2
更改输出格式
bash
bash scripts/texttospeech.sh 你好 -f ogg_opus
完整选项
bash
bash scripts/texttospeech.sh 要转换的文本 -o output.mp3 -v 1 -f mp3
参数:
- - text(必需):要转换为语音的文本
- -o, --output(可选):输出文件路径(默认:自动生成)
- -v, --voice(可选):音色 ID(默认:1)
- -f, --format(可选):输出格式 - mp3/ogg_opus/wav/pcm(默认:mp3)
输出
脚本保存音频文件并输出:
- - 文件路径
- 文件大小
- 音频时长(如果 ffprobe 可用)
示例输出:
✓ 音频已保存:cozetts20260324235030a1b2c3d4.mp3
大小:25.3 KB
时长:约 3 秒
工作流示例
生成通知音频
bash
bash scripts/texttospeech.sh 您有一条新消息 -o notification.mp3
创建语音问候
bash
bash scripts/texttospeech.sh 欢迎使用 Coze 语音服务 -v 2 -o greeting.mp3
为消息传递生成 OGG
bash
bash scripts/texttospeech.sh 你好 -f ogg_opus -o message.ogg
批量生成
bash
for text in 你好 谢谢 再见; do
bash scripts/texttospeech.sh $text -o ${text}.mp3
done
与其他技能集成
与 coze-asr 结合实现语音对话:
bash
1. 用户说话 -> ASR 转换为文本
bash coze-asr/scripts/speech
totext.sh input.ogg
2. 使用 AI 处理文本...
3. AI 响应 -> TTS 转换为语音
bash coze-tts/scripts/text
tospeech.sh AI的回复 -o response.mp3
故障排除
身份验证错误:
- - 检查 COZEAPIKEY 是否正确设置
- 验证 API 密钥是否具有 TTS 权限
无效的音色 ID:
- - 音色 ID 应为数字(int64 格式)
- 尝试使用默认音色 ID:1
文件未创建:
限制
- - 文本长度有限制(请查阅 Coze 文档)
- 根据您的套餐可能存在速率限制
- 某些音色可能不支持所有输出格式
API 参考
- - 端点: POST https://api.coze.cn/v1/audio/speech
- 身份验证: Bearer 令牌(COZEAPIKEY)
- 内容类型: application/json
必需的环境变量
| 变量 | 描述 | 必需 |
|---|
| COZEAPIKEY | Coze API 身份验证密钥 | 是 |
必需的工具
音频时长检测 | 可选 |
许可证
MIT