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

# 文本转语音(TTS)

> 使用在 Qualcomm NPU 上运行的设备端 TTS 模型将文本合成为语音。

TTS 服务使用完全在 Qualcomm NPU 上运行的设备端模型将文本转换为语音。音频以原始 PCM 格式通过 HTTP 分块传输编码返回。

## 前提条件

确保 Audio Analytics 容器已部署并正在运行。有关设置说明,请参见[运行 Audio Analytics 容器](/zh/ai-workflows/audio-analytics-overview)。

## 可用模型

查询模型端点以获取可用的 TTS 模型和语音列表:

```bash theme={null}
curl http://localhost:8085/audio-analytics/v1/api/tts/models
```

当前可用的模型有:

| 模型             | 语言   | 原生采样率    |
| -------------- | ---- | -------- |
| `melo-tts-en`  | 英语   | 44100 Hz |
| `melo-tts-es`  | 西班牙语 | 44100 Hz |
| `melo-tts-zh`  | 中文   | 44100 Hz |
| `piper-tts-en` | 英语   | 22050 Hz |
| `piper-tts-de` | 德语   | 22050 Hz |
| `piper-tts-it` | 意大利语 | 22050 Hz |

在合成请求中使用响应中的 `name` 值。

## 端点

| 方法     | 端点                | 描述              |
| ------ | ----------------- | --------------- |
| `GET`  | `/tts/models`     | 列出可用的 TTS 模型和语音 |
| `POST` | `/tts/synthesize` | 从文本合成语音         |
| `POST` | `/tts/cancel`     | 取消正在进行的合成       |
| `POST` | `/tts/close`      | 关闭当前 TTS 会话     |

## 合成语音

```bash theme={null}
curl -X POST http://localhost:8085/audio-analytics/v1/api/tts/synthesize \
  -H "Content-Type: application/json" \
  -d '{
    "text": "Hello, how are you today?",
    "model": "melo-tts-en",
    "language": "en",
    "voice": "default",
    "sample_rate": 44100
  }' \
  --output speech.pcm
```

**参数:**

* `text`(必需):要合成的文本
* `model`(必需):TTS 模型名称
* `language`(可选):语言代码(`en`、`es`、`zh`、`de`、`it`)
* `voice`(可选):语音名称(默认:`default`)
* `sample_rate`(可选):输出采样率 — `44100`、`22050` 或 `16000` Hz。如果与模型的原生采样率不同,音频将被重采样。
* `parameters.speaking_rate`(可选):语速倍率,例如 `"0.8"` 表示更慢,`"1.5"` 表示更快。

**响应:** 通过 HTTP 分块传输流式返回的原始 PCM 音频。

<Note>TTS 返回的是原始 PCM,而非 WAV。请在播放前将其转换为 WAV。</Note>

## 语速

如果模型支持,可以调整语速,例如 `"0.8"` 表示更慢,`"1.5"` 表示更快:

```bash theme={null}
curl -X POST http://localhost:8085/audio-analytics/v1/api/tts/synthesize \
  -H "Content-Type: application/json" \
  -d '{
    "text": "Hello, this is a speech rate test.",
    "model": "melo-tts-en",
    "language": "en",
    "sample_rate": 44100,
    "parameters": {"speaking_rate": "1.5"}
  }' \
  --output speech.pcm
```

## 取消合成

停止正在进行的合成:

```bash theme={null}
curl -X POST http://localhost:8085/audio-analytics/v1/api/tts/cancel
```

## 关闭

合成完成后释放 TTS 资源:

```bash theme={null}
curl -X POST http://localhost:8085/audio-analytics/v1/api/tts/close
```

## Python 示例

```python theme={null}
import requests

def synthesize(text, model="melo-tts-en", language="en", sample_rate=44100):
    url = "http://localhost:8085/audio-analytics/v1/api/tts/synthesize"
    response = requests.post(url, json={
        "text": text,
        "model": model,
        "language": language,
        "sample_rate": sample_rate
    }, timeout=30)
    response.raise_for_status()
    with open("speech.pcm", "wb") as f:
        f.write(response.content)
    print(f"Saved {len(response.content)} bytes to speech.pcm")

synthesize("Hello, how are you today?")
```

## 故障排除

**PCM 无法播放:** PCM 是原始音频。请在播放前将其转换为 WAV。

**音频质量差:** 尝试更高的采样率(44100 Hz),或检查输入文本中是否包含特殊字符。

## 后续步骤

<CardGroup cols={2}>
  <Card title="语音识别" icon="microphone" href="/zh/ai-workflows/asr-container">
    在设备端将音频转录为文本
  </Card>

  <Card title="语言翻译" icon="language" href="/zh/ai-workflows/language-translation">
    在设备端进行语言间文本翻译
  </Card>
</CardGroup>
