Python API 适合在应用中集成 RapidTTS。核心流程是创建 RapidTTS,构造 SynthesisRequest,再保存 SynthesisResponse。
from rapidtts import RapidTTS, SynthesisRequest
tts = RapidTTS()
resp = tts.synthesize(
SynthesisRequest(text="我最近在学习machine learning,希望能够在未来的artificial intelligence领域有所建树。")
)
resp.save("result.wav")未指定模型时,RapidTTS 使用配置中的默认后端,目前是 kokoro_onnx。
from rapidtts import RapidTTS, SynthesisRequest, TTSModel
tts = RapidTTS(model=TTSModel.KOKORO_ONNX)
resp = tts.synthesize(SynthesisRequest(text="你好,RapidTTS"))
resp.save("kokoro.wav")使用 MeloTTS ONNX:
from rapidtts import RapidTTS, SynthesisRequest, TTSModel
tts = RapidTTS(model=TTSModel.MELO_ONNX)
resp = tts.synthesize(SynthesisRequest(text="你好,RapidTTS"))
resp.save("melo.wav")可选模型:
TTSModel.KOKORO_ONNX:默认后端,支持多音色TTSModel.MELO_ONNX:可选后端,当前只暴露zf_001音色
音色统一通过 SynthesisRequest.voice 指定,不放在 extras 中。
Kokoro ONNX 支持多音色:
from rapidtts import RapidTTS, SynthesisRequest, TTSModel
tts = RapidTTS(model=TTSModel.KOKORO_ONNX)
resp = tts.synthesize(
SynthesisRequest(
text="你好,RapidTTS",
voice="zm_009",
)
)
resp.save("kokoro_zm_009.wav")查询当前模型可用音色:
from rapidtts import RapidTTS, TTSModel
tts = RapidTTS(model=TTSModel.KOKORO_ONNX)
print(tts.get_voices())MeloTTS ONNX 当前只暴露 zf_001:
from rapidtts import RapidTTS, SynthesisRequest, TTSModel
tts = RapidTTS(model=TTSModel.MELO_ONNX)
resp = tts.synthesize(
SynthesisRequest(
text="你好,RapidTTS",
voice="zf_001",
)
)
resp.save("melo_zf_001.wav")from rapidtts import RapidTTS, SynthesisRequest, TTSLanguage
tts = RapidTTS()
resp = tts.synthesize(
SynthesisRequest(
text="hello world",
language=TTSLanguage.EN,
speed=1.1,
sample_rate=16000,
)
)
resp.save("en.wav")常用语言:
TTSLanguage.ZH:中文TTSLanguage.EN:英文TTSLanguage.ZH_MIX_EN:中英混合
from rapidtts import RapidTTS, TTSModel
tts = RapidTTS(
model=TTSModel.KOKORO_ONNX,
model_root_dir="/path/to/kokoro_onnx",
)如果不指定 model_root_dir,RapidTTS 会使用默认模型目录,并在模型缺失时自动下载。
from rapidtts import RapidTTS, TTSModel
tts = RapidTTS(model=TTSModel.KOKORO_ONNX)
capability = tts.get_capability()
print(capability.name)
print(capability.languages)
print(capability.default_language)
print(capability.default_voice)
print(capability.voices)get_capability() 返回 ModelCapability,包含模型名称、支持语言、默认语言、音色列表、默认音色和音色来源。
RapidTTS 默认使用 WeText 文本归一化。也可以显式指定归一化器:
from rapidtts import RapidTTS, TextNormalizerType, TTSModel
tts = RapidTTS(
model=TTSModel.KOKORO_ONNX,
text_normalizer_type=TextNormalizerType.WETEXT,
)可选值:
TextNormalizerType.WETEXTTextNormalizerType.LEGACYTextNormalizerType.NONE
SynthesisRequest 常用字段如下:
| 字段 | 说明 |
|---|---|
text |
待合成文本 |
language |
语言,可选 ZH、EN、ZH_MIX_EN |
voice |
音色名称,例如 zf_001、zm_009 |
speed |
语速,默认 1.0 |
sample_rate |
输出采样率 |
audio_format |
音频格式,默认 wav |
extras |
后端扩展参数,不用于传递 voice |