# 调用meloTTS生成语音文件
## 安装 meloTTS
```bash
docker run -it -p 8888:8888 kisaragi29/melotts:latest
```
## 基本用法
通过接口调用:
```bash
curl 'http://localhost:8888/queue/join?' \
-H 'Accept: */*' \
-H 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6' \
-H 'Connection: keep-alive' \
-H 'Origin: http://localhost:8888' \
-H 'Referer: http://localhost:8888/' \
-H 'Sec-Fetch-Dest: empty' \
-H 'Sec-Fetch-Mode: cors' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0' \
-H 'content-type: application/json' \
-H 'sec-ch-ua: "Microsoft Edge";v="143", "Chromium";v="143", "Not A(Brand";v="24"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "Windows"' \
--data-raw '{"data":["ZH","\"你好,这是自动化调用",1,"ZH"],"event_data":null,"fn_index":1,"trigger_id":8,"session_hash":"ij0ms7exi1f"}'
```
如果没有错误,会立即返回:
```json
{"event_id":"8fb78ad972964dfbbdd0eb9705bac6e7"}
网页端可以通过sse监听进度:
```bash
curl 'http://localhost:8888/queue/data?session_hash=ij0ms7exi1f' \
-H 'Accept: text/event-stream' \
-H 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6' \
-H 'Connection: keep-alive' \
-H 'Content-Type: application/json' \
-H 'Referer: http://localhost:8888/' \
-H 'Sec-Fetch-Dest: empty' \
-H 'Sec-Fetch-Mode: cors' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0' \
-H 'sec-ch-ua: "Microsoft Edge";v="143", "Chromium";v="143", "Not A(Brand";v="24"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "Windows"'
```
响应值是这样的:
···text
message {"msg":"estimation","event_id":"8fb78ad972964dfbbdd0eb9705bac6e7","rank":0,"queue_size":1,"rank_eta":null}
message {"msg":"process_starts","event_id":"8fb78ad972964dfbbdd0eb9705bac6e7","eta":null}
message {"msg":"progress","event_id":"8fb78ad972964dfbbdd0eb9705bac6e7","progress_data":[{"index":0,"length":1,"unit":"steps","progress":null,"desc":null}]}
message {"msg":"heartbeat"}
message {"msg":"process_completed","event_id":"8fb78ad972964dfbbdd0eb9705bac6e7","output":{"data":[{"path":"/tmp/gradio/a22f0f35818f618ae40c78d1990da69e4319390e/audio","url":"http://localhost:8888/file=/tmp/gradio/a22f0f35818f618ae40c78d1990da69e4319390e/audio","size":null,"orig_name":"audio","mime_type":null,"is_stream":false,"meta":{"_type":"gradio.FileData"}}],"is_generating":false,"duration":28.17717671394348,"average_duration":28.17717671394348,"render_config":null,"changed_state_ids":[]},"success":true}
message {"msg":"close_stream"}
···
### 当文本内容非常多时,推荐做法(官方脚本已自带)
1. 按标点断句成 ≤ 100 字的小段(MeloTTS 仓库 split_sentence.py 一键完成)。
2. 批量生成小段 wav → 得到 chunk_001.wav … chunk_00n.wav 。
3. 用 sox / ffmpeg 无损拼接:
```bash
# 生成文件列表
ls chunk_*.wav | sort -V > list.txt
# 拼接
ffmpeg -f concat -safe 0 -i list.txt -c copy full_2000.wav
```