# ChatTTS 对话语音

ChatTTS 是一个用于对话场景（如大型语言模型助手、聊天机器人和交互式语音应用）的 3 亿参数生成语音模型。它能生成具有自然停顿、笑声、填充词和语调的自然语音——这些特征大多数 TTS 系统难以重现。该模型支持英语和中文，并以 24 kHz 生成音频。

**GitHub：** [2noise/ChatTTS](https://github.com/2noise/ChatTTS) （30K+ 星） **许可：** AGPLv3+（代码），CC BY-NC 4.0（模型权重——非商业）

## 主要特性

* **对话韵律** — 为对话调整的自然停顿、填充词和语调
* **精细控制标签** — `[oral_0-9]`, `[laugh_0-2]`, `[break_0-7]`, `[uv_break]`, `[lbreak]`
* **多说话人** — 随机采样说话人或重用说话人嵌入以保持一致性
* **温度 / top-P / top-K** — 控制生成多样性
* **批量推理** — 在一次调用中合成多条文本
* **轻量级** — 约 3 亿参数，4 GB 显存即可运行

## 要求

| 组件     | 最低                | 推荐                  |
| ------ | ----------------- | ------------------- |
| GPU    | RTX 3060（4 GB 空闲） | RTX 3090 / RTX 4090 |
| 显存     | 4 GB              | 8 GB 及以上            |
| 内存     | 8 GB              | 16 GB               |
| 磁盘     | 5 GB              | 10 GB               |
| Python | 3.9+              | 3.11                |
| CUDA   | 11.8+             | 12.1+               |

**Clore.ai 建议：** 一块 RTX 3060（~~（$0.15–0.30/天）可轻松运行 ChatTTS。对于批量生产或更低延迟，请选择 RTX 3090（~~（$0.30–1.00/天）。

## 安装

```bash
# 从 PyPI 安装
pip install ChatTTS torch torchaudio

# 或从源码安装以获取最新功能
git clone https://github.com/2noise/ChatTTS.git
cd ChatTTS
pip install -r requirements.txt

# 验证 GPU
python -c "import torch; print(torch.cuda.get_device_name(0))"
```

## 快速开始

```python
import ChatTTS
import torch
import torchaudio

# 初始化并加载模型（首次运行时会下载权重）
chat = ChatTTS.Chat()
chat.load(compile=False)  # 在预热后将 compile=True 以获得更快的推理

texts = [
    "嗨！你今天过得怎么样？",
    "我整个上午都在做这个项目。进展不错。",
]

wavs = chat.infer(texts)

for i, wav in enumerate(wavs):
    audio_tensor = torch.from_numpy(wav)
    if audio_tensor.dim() == 1:
        audio_tensor = audio_tensor.unsqueeze(0)
    torchaudio.save(f"output_{i}.wav", audio_tensor, 24000)
    print(f"已保存 output_{i}.wav")
```

## 使用示例

### 一致的说话人音色

采样一个随机的说话人嵌入并在多次生成中复用它以保持一致的声音：

```python
import ChatTTS
import torch
import torchaudio

chat = ChatTTS.Chat()
chat.load(compile=False)

# 采样一个说话人 —— 将此字符串保存以便以后重用
rand_spk = chat.sample_random_speaker()

params_infer_code = ChatTTS.Chat.InferCodeParams(
    spk_emb=rand_spk,
    temperature=0.3,
    top_P=0.7,
    top_K=20,
)

params_refine_text = ChatTTS.Chat.RefineTextParams(
    prompt='[oral_2][laugh_0][break_4]',
)

texts = ["欢迎收听今天的节目。让我告诉你一些令人兴奋的事情。"]

wavs = chat.infer(
    texts,
    params_refine_text=params_refine_text,
    params_infer_code=params_infer_code,
)

audio = torch.from_numpy(wavs[0])
if audio.dim() == 1:
    audio = audio.unsqueeze(0)
torchaudio.save("consistent_speaker.wav", audio, 24000)
```

### 词级控制标签

将控制标签直接插入文本以获得精确的韵律：

```python
import ChatTTS
import torch
import torchaudio

chat = ChatTTS.Chat()
chat.load(compile=False)

# 标签：[uv_break] = 短暂停，[laugh] = 笑声，[lbreak] = 长暂停
text = 'What is [uv_break]your favorite food?[laugh][lbreak]'

rand_spk = chat.sample_random_speaker()
params = ChatTTS.Chat.InferCodeParams(spk_emb=rand_spk, temperature=0.3)

# skip_refine_text=True 会保留你手动的控制标签
wavs = chat.infer(text, skip_refine_text=True, params_infer_code=params)

audio = torch.from_numpy(wavs[0])
if audio.dim() == 1:
    audio = audio.unsqueeze(0)
torchaudio.save("controlled_output.wav", audio, 24000)
```

### 带 WebUI 的批量处理

ChatTTS 附带用于交互使用的 Gradio 网页界面：

```bash
cd ChatTTS
python examples/web/webui.py --server_name 0.0.0.0 --server_port 7860
```

打开 `http_pub` 从你的 Clore.ai 订单仪表板获取的 URL 以访问界面。

## 给 Clore.ai 用户的提示

* **使用 `compile=True`** 在初始测试之后 — PyTorch 编译会增加启动时间，但会显著加速重复推理
* **端口映射** — 暴露端口 `7860/http` 在使用 WebUI 部署时
* **Docker 镜像** — 使用 `pytorch/pytorch:2.5.1-cuda12.4-cudnn9-runtime` 作为基础
* **说话人持久化** — 保存 `rand_spk` 字符串到文件，以便在会话之间重用声音而无需重新采样
* **批量请求** — `chat.infer()` 接受文本列表并一起处理，这比逐条调用更高效
* **非商业许可证** — 模型权重为 CC BY-NC 4.0；请根据你的使用场景检查许可要求

## # 使用固定种子以获得一致结果

| 问题                         | 解决方案                                                           |
| -------------------------- | -------------------------------------------------------------- |
| `CUDA 内存不足（out of memory）` | 减少批量大小或使用 ≥ 6 GB 显存的 GPU                                       |
| 模型下载缓慢                     | 提前从 HuggingFace 预下载： `huggingface-cli download 2Noise/ChatTTS` |
| 音频有静电/噪声                   | 这是开源模型中的刻意设计（反滥用措施）；使用 `compile=True` 以获得更清洁的输出                |
| `torchaudio.save` 维度错误     | 确保张量为 2D： `audio.unsqueeze(0)` 如有需要                            |
| 中文输出乱码                     | 确保输入文本为 UTF-8 编码；安装 `WeTextProcessing` 以获得更好的规范化               |
| 第一次推理慢                     | 正常 —— 模型编译和权重加载发生在首次调用；后续调用会更快                                 |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/yin-pin-yu-yu-yin/chattts.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
