Piper TTS:修订间差异

无编辑摘要
 
第38行: 第38行:
简单测试test.py:
简单测试test.py:
<syntaxhighlight lang="python" >
<syntaxhighlight lang="python" >
import os
import wave
import wave
from piper import PiperVoice
from pathlib import Path
import piper


# 相对路径或绝对路径
# 完全离线设置(防止任何网络尝试)
model_path = "/path/to/project/voices/zh_CN-chaowen-medium.onnx"
os.environ["TRANSFORMERS_OFFLINE"] = "1"
download_dir = "/path/to/project/"
os.environ["HF_HUB_OFFLINE"] = "1"


# 加载语音
model_path = Path("/path/to/project/voices/zh_CN-chaowen-medium.onnx")
voice = PiperVoice.load(model_path, download_dir=download_dir)
download_dir = Path("/path/to/project/")


voice = piper.PiperVoice.load(model_path=model_path, download_dir=download_dir)


with wave.open("test.wav", "wb") as wav_file:
# 运行一次自定义 phonemizer,指定bert-base-chinese位置。
     voice.synthesize_wav("你好,树莓派。", wav_file)
from piper.phonemize_chinese import ChinesePhonemizer
from g2pw import G2PWConverter
 
class OfflineChinesePhonemizer(ChinesePhonemizer):
    def __init__(self, model_dir: Path):
        super().__init__(model_dir)  # 先让它找到 g2pw.onnx
        # 重新初始化 g2p,使用本地 bert-base-chinese
        bert_path = download_dir / "bert-base-chinese"
        self.g2p = G2PWConverter(
            model_dir=str(model_dir),
            model_source=str(bert_path),  # 本地 bert 路径
            style="pinyin",
            enable_non_tradional_chinese=True
        )
 
# 替换 phonemizer
voice._chinese_phonemizer = OfflineChinesePhonemizer(download_dir / "g2pW")
 
# 合成语音
out_file = "test.wav"
text = "你好,树莓派。"
print("正在合成:", text)
 
with wave.open(out_file, "wb") as wav_file:
     voice.synthesize_wav(text, wav_file)
 
print(f"✅ 成功生成 test.wav, 地址:{Path(out_file).resolve()}")
</syntaxhighlight>
</syntaxhighlight>


==资源==
==资源==

2026年4月20日 (一) 01:46的最新版本

Piper TTS是一个快速、本地运行的神经网络文本转语音(TTS)引擎,基于 VITS 模型,使用 ONNX 格式运行。它支持多种语言(包括中文),模型体积小、速度快,即使在低端硬件(如 Raspberry Pi)上也能实时合成高质量、自然的语音,完全离线隐私安全。注意:原 rhasspy/piper 仓库已归档,开发迁移到 OHF-Voice/piper1-gpl(GPL 许可)。

简介

时间轴

安装

快速入门

中文

对于离线,可以先在Hugging Face下载需要的模型和安装需要的包:

  • piper-voices中文模型: https://huggingface.co/rhasspy/piper-voices/tree/main/zh/zh_CN
  • g2pW模型:默认路径download_dir / "g2pW",其中 download_dir 默认是 当前工作目录(即你运行 Python 脚本或 piper 命令时的目录)。
  • bert-base-chinese模型:G2PWConverter 在初始化时调用了 BertTokenizer.from_pretrained(self.model_source),而 model_source 默认是 "bert-base-chinese"。地址:https://huggingface.co/google-bert/bert-base-chinese/tree/main。
  • Sentence Stream包:安装 pip install sentence-stream。 这个模块是 OHF-Voice 自己维护的一个小型句子分割器(sentence splitter),专门用于把长文本按句子切分后再做 phonemization。

文件结构如下:

/path/to/project/
├── voices/    
   ├── zh_CN-chaowen-medium.onnx
   ├── zh_CN-chaowen-medium.onnx.json
     └── ... 其他模型
├── g2pW/                
   ├── g2pw.onnx         
   ├── config.json       
   └── ...                
├── bert-base-chinese/    
   ├── config.json
   ├── tokenizer_config.json   
   ├── vocab.txt
   ├── pytorch_model.bin        model.safetensors
   ├── tokenizer.json
   └── ... 其他必要文件
└── test.py

简单测试test.py:

import os
import wave
from pathlib import Path
import piper

# 完全离线设置(防止任何网络尝试)
os.environ["TRANSFORMERS_OFFLINE"] = "1"
os.environ["HF_HUB_OFFLINE"] = "1"

model_path = Path("/path/to/project/voices/zh_CN-chaowen-medium.onnx")
download_dir = Path("/path/to/project/")

voice = piper.PiperVoice.load(model_path=model_path, download_dir=download_dir)

# 运行一次自定义 phonemizer,指定bert-base-chinese位置。
from piper.phonemize_chinese import ChinesePhonemizer
from g2pw import G2PWConverter

class OfflineChinesePhonemizer(ChinesePhonemizer):
    def __init__(self, model_dir: Path):
        super().__init__(model_dir)  # 先让它找到 g2pw.onnx
        # 重新初始化 g2p,使用本地 bert-base-chinese
        bert_path = download_dir / "bert-base-chinese"
        self.g2p = G2PWConverter(
            model_dir=str(model_dir),
            model_source=str(bert_path),   # 本地 bert 路径
            style="pinyin",
            enable_non_tradional_chinese=True
        )

# 替换 phonemizer
voice._chinese_phonemizer = OfflineChinesePhonemizer(download_dir / "g2pW")

# 合成语音
out_file = "test.wav"
text = "你好,树莓派。"
print("正在合成:", text)

with wave.open(out_file, "wb") as wav_file:
    voice.synthesize_wav(text, wav_file)

print(f"✅ 成功生成 test.wav, 地址:{Path(out_file).resolve()}")

资源

官网

网站

文章