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

# 文本转语音

> 使用 Venice 的文本转语音模型、模型特定的音色以及 /audio/speech 端点，将文本合成为语音。

文本转语音可将书面文本合成为语音。选择一个 TTS 模型和该模型支持的音色，将文本发送到 `/audio/speech`，然后保存二进制的音频响应。

本指南适用于标准的语音生成。如果你希望通过自定义参考音频生成语音，请参阅 [语音克隆](/guides/media/voice-cloning)。

## 基本用法

<CodeGroup>
  ```python Python theme={"system"}
  import os
  from pathlib import Path

  import requests

  response = requests.post(
      "https://api.venice.ai/api/v1/audio/speech",
      headers={
          "Authorization": f"Bearer {os.environ['VENICE_API_KEY']}",
          "Content-Type": "application/json",
      },
      json={
          "model": "tts-kokoro",
          "voice": "af_sky",
          "input": "Hello, welcome to Venice Voice.",
      },
  )

  response.raise_for_status()
  Path("speech.mp3").write_bytes(response.content)
  ```

  ```javascript Node.js theme={"system"}
  import { writeFile } from "node:fs/promises";

  const response = await fetch("https://api.venice.ai/api/v1/audio/speech", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.VENICE_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      model: "tts-kokoro",
      voice: "af_sky",
      input: "Hello, welcome to Venice Voice.",
    }),
  });

  if (!response.ok) {
    throw new Error(await response.text());
  }

  await writeFile("speech.mp3", Buffer.from(await response.arrayBuffer()));
  ```

  ```bash cURL theme={"system"}
  curl https://api.venice.ai/api/v1/audio/speech \
    -H "Authorization: Bearer $VENICE_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "tts-kokoro",
      "voice": "af_sky",
      "input": "Hello, welcome to Venice Voice."
    }' \
    --output speech.mp3
  ```
</CodeGroup>

## 选择模型和音色

音色是模型专属的。`voice` 的值必须与所选的 `model` 匹配。

请在 [文本转语音模型](/models/text-to-speech) 页面浏览可用的模型和音色。音色选择器中列出了你在请求中需要传入的确切音色 ID。

<Note>
  音色 ID 区分大小写。切换 TTS 模型时，请同时更新 `voice` 的值。
</Note>

## 请求结构

| 参数      | 类型     | 是否必填 | 说明            |
| ------- | ------ | ---- | ------------- |
| `model` | string | 是    | 文本转语音模型 ID。   |
| `voice` | string | 是    | 所选模型支持的音色 ID。 |
| `input` | string | 是    | 需要合成的文本。      |

## 生产环境建议

* 当源文本和音色会被重复使用时，缓存生成的音频。
* 合成前对文本进行规范化和校对。标点符号会影响节奏和语调。
* 输出文件请使用与模型响应格式相符的文件扩展名。

## 相关资源

* [Audio Speech API](/api-reference/endpoint/audio/speech)
* [文本转语音模型](/models/text-to-speech)
* [语音克隆指南](/guides/media/voice-cloning)
