1、从 GitHub 克隆代码
git clone https://github.com/2noise/ChatTTS.git
2、安装依赖:
pip install -r requirements.txt
3、快速开始
导入必要的库:
import ChatTTS
from IPython.display import Audio
import torchaudio
初始化 ChatTTS:
chat = ChatTTS.Chat()
chat.load_models(compile=False) # 设置为 True 以获得更快的性能
准备文本:
texts = [“Hello, welcome to ChatTTS!”]
生成语音:
wavs = chat.infer(texts, use_decoder=True)
torchaudio.save(“output.wav”, torch.from_numpy(wavs[0]), 24000)
Audio(wavs[0], rate=24000, autoplay=True)
进阶用法
随机采样说话人
rand_spk = chat.sample_random_speaker()
params_infer_code = {‘spk_emb’: rand_spk, ‘temperature’: 0.3, ‘top_P’: 0.7, ‘top_K’: 20}
wavs = chat.infer(texts, params_infer_code=params_infer_code)
手动控制句子级别的韵律
params_refine_text = {‘prompt’: ‘[oral_2][laugh_0][break_6]’}
wavs = chat.infer(texts, params_refine_text=params_refine_text, params_infer_code=params_infer_code)
完整示例代码
import torch
import ChatTTS
from IPython.display import Audio
import torchaudio# 初始化 ChatTTS
chat = ChatTTS.Chat()
chat.load_models()# 定义文本
texts = [“Hello, welcome to ChatTTS!”]# 生成语音
wavs = chat.infer(texts, use_decoder=True)# 保存生成的语音
torchaudio.save(“output.wav”, torch.from_numpy(wavs[0]), 24000)# 播放生成的语音
Audio(wavs[0], rate=24000, autoplay=True)