> ## Documentation Index
> Fetch the complete documentation index at: https://docs.geekai.co/llms.txt
> Use this file to discover all available pages before exploring further.

# 文本转语音

### 快速入门

目前支持文本转语音的模型有 `tts-1`、`tts-1-hd`、`gpt-4o-mini-tts`、`doubao-tts`、`seed-tts-1.1`，你可以通过如下方式调用：

```bash theme={null}
curl --location 'https://geekai.co/api/v1/audio/speech' \
--header 'Content-Type: application/json' \
--header 'Authorization: {YOUR_GEEKAI_API_KEY}' \
--data '{
    "input":"Hello! Nice to meet you!",
    "model":"gpt-4o-mini-tts"
}'
```

豆包语音合成模型仅支持中英文，其他语种建议使用 OpenAI 语音模型。

### 声音角色

OpenAI 支持的声音角色（通过 `voice` 指定）有：

* alloy
* ash
* ballad
* coral
* echo
* fable
* onyx
* nova
* sage
* shimmer

选择任意值填充到 `voice` 参数即可。

豆包平台支持的声音角色太多，这里不一一列举，参考火山引擎的[大模型语音合成音色列表](https://www.volcengine.com/docs/6561/1257544)，将其中的 `voice_type` 值填充到 `voice` 参数即可。

### 音频格式

OpenAI 支持的音频输出格式（通过 `response_format` 参数指定）如下：

* `mp3`
* `opus`
* `wav`
* `flac`
* `pcm`
* `aac`

不指定的话默认输出格式为 `mp3`，豆包平台目前仅支持生成 `mp3`/`wav`/`pcm`/`ogg_opus` 格式的音频。

### 流式输出

文本转语音默认输出的是二进制音频文件流，如果你想要实现边输出边播放的效果，可以设置 `stream_format` 请求字段为 `sse` 开启 SSE 流式输出，目前仅 `gpt-4o-mini-tts`、`doubao-tts`、`seed-tts-1.1` 支持流式输出：

```bash theme={null}
curl --location 'https://geekai.co/api/v1/audio/speech' \
--header 'Content-Type: application/json' \
--header 'Authorization: {YOUR_GEEKAI_API_KEY}' \
--data '{
    "input":"你好，很高兴见到你！",
    "model":"doubao-tts",
    "response_format":"pcm",
    "stream_format":"sse"
}'
```

<Note>
  流式输出推荐使用 `pcm` 音频格式，延迟最低，适合边输出边播放，豆包平台的 `wav` 格式不支持流式输出，使用时请注意规避。
</Note>

返回结果会以 SSE 流的形式分块返回经过 base64 编码的音频数据，你可以通过监听 EventStream 来获取每一块数据，直到接收到 `[DONE]` 表示流式输出结束，极客智坊语音合成流式输出格式兼容 OpenAI 流式语音合成输出格式。

`type` 字段为 `speech.audio.delta` 表示这是音频数据块，`audio` 字段为经过 base64 编码的音频数据片段，你可以将这些片段解码后拼接成完整的音频文件，或者边解码边播放：

```json theme={null}
{
    "id": "24f44c9a-e6f9-4a80-812a-d17e6621e107",
    "type": "speech.audio.delta",
    "audio": "{base64音频数据片段} ",
}
```

`type` 字段为 `speech.audio.done` 表示流式输出结束，接下来可以关闭服务端 EventStream 了：

```json theme={null}
{
    "id": "24f44c9a-e6f9-4a80-812a-d17e6621e107",
    "type": "speech.audio.done",
    "usage": {
        "input_tokens": 3,
        "output_tokens": 73,
        "total_tokens": 76
    }
}
```

更多语音合成参数请参考[API文档](https://docs.geekai.co/cn/api/audio/texttospeech)。
