文本转语音 (TTS)
将文本转换为听起来自然的语音
发布
/api/v1/tts/generate/
使用 AI 语音合成技术将文本转换为语音,并返回 MP3 音频文件。
身份验证
在 Authorization 标头中包含您的 API 密钥。
请求体
| 参数 | 必填 | 类型 | 描述 |
|---|---|---|---|
| text | 必需的 | 字符串 | 待转换文本(最多 1500 字符) |
| output_format | 可选的 | 字符串 | 所需的音频输出格式。格式为 codec_sampleRate_bitrate(例如 mp3_22050_32)。默认值为 mp3_44100_128。完整列表请参阅下方的“输出格式”部分。 |
| voice_id | 必需的 | 整数 | 来自……的语音识别 语音列表. |
| prerecording | 可选的 | 整数 | 1(基础增强)或 2(AI 增强) |
| voice_settings | 可选的 | 字符串 (JSON) | 语音自定义选项 |
支持的输出格式
output_format 参数可以是以下任一值:
- MP3: mp3_22050_32, mp3_24000_48, mp3_44100_32, mp3_44100_64, mp3_44100_96, mp3_44100_128, mp3_44100_192
- PCM: pcm_8000, pcm_16000, pcm_22050, pcm_24000, pcm_32000, pcm_44100, pcm_48000
- 作品: opus_48000_32, opus_48000_64, opus_48000_96, opus_48000_128, opus_48000_192
本部分仅供参考,方便用户选择合适的格式,同时避免使主请求表格显得杂乱。
语音设置对象
| 参数 | 类型 | 范围 | 默认 | 描述 |
|---|---|---|---|---|
| temperature | 整数 | 0 – 100 | 25 | 控制语音的变异性与表现力。较低的数值会生成更稳定、可预测的语音,而较高的数值则会增加变化、自发性以及富有创意的表达。 |
| voice_similarity | 整数 | 0 – 100 | 100 | 决定生成的语音与所选语音特征的匹配程度。数值越高,越能保留原始语音的特质;数值越低,则允许在音色和发音上出现更多偏差。 |
| speed | 漂浮 | 0.7 – 1.2 | 1.0 | 调节语速。低于 1.0 的数值会放慢语速以提高清晰度,而高于 1.0 的数值则会加快语速。 |
| enable_emotions | 布尔 | false / true | false | 支持生成富有情感表现力的语音。启用后,模型会根据情感语境动态控制语速、语调和重音。 |
| fast_mode | 整数 | 0, 1, or 2 | 0 |
以牺牲音频质量为代价,降低合成延迟。
|
语音设置规则与限制
-
当 enable_emotions 设置为 true 时,voice_settings 中不得包含以下字段:
voice_similarityspeedfast_mode
- 如果在启用情感功能的情况下,请求中包含这些字段中的任何一个,该请求将被拒绝,并返回 400 INVALID_REQUEST 错误。
示例
curl -X POST "https://moknah.io/api/v1/tts/generate/" \
-H "Authorization: "Bearer your_api_key"
-d '{
"text": "مرحبا بك في منصة مكنة",
"voice_id": 1,
"prerecording": 1,
"voice_settings": {
"speed": 1.0,
"temperature": 25
}
}' \
--output speech.mp3
import requests
response = requests.post(
"https://moknah.io/api/v1/tts/generate/",
headers={
"Authorization": "Bearer your_api_key",
"Content-Type": "application/json"
},
json={
"text": "مرحبا بك في منصة مكنة",
"voice_id": 1,
"prerecording": 1,
"voice_settings": {
"speed": 1.0,
"temperature": 25
}
}
)
with open("speech.mp3", "wb") as f:
f.write(response.content)
const response = await fetch(
'https://moknah.io/api/v1/tts/generate/',
{
method: 'POST',
headers: {
'Authorization': 'Bearer your_api_key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: 'مرحبا بك في منصة مكنة',
voice_id: 1,
prerecording: 1,
voice_settings: {
speed: 1.0,
temperature: 25
}
})
}
);
const blob = await response.blob();
// Save or play the audio
响应
返回包含以下标头的二进制 MP3 音频数据:
| 页眉 | 描述 |
|---|---|
Content-Type |
audio/mpeg |
错误
| 状态 | 代码 | 描述 |
|---|---|---|
400 |
INVALID_REQUEST | 无效参数 |
401 |
UNAUTHORIZED | 无效的 API 密钥 |
402 |
INSUFFICIENT_CREDITS | 积分不足 |
429 |
RATE_LIMIT_EXCEEDED | 请求过多 |
API 支持
如有关于 API 的问题或疑问,请联系我们: api@moknah.io.