v4: 改用本地IndexTTS2 + 新增飞书推送
- TTS从小爱音箱云API改为本地IndexTTS2模型 - 新增飞书群Webhook推送 - 新增notify_all统一通知(企业微信+飞书) - 新增local_tts.py本地TTS封装模块 - 新增run.bat一键启动脚本 - 新增README.md项目说明 - 新增.gitignore排除敏感文件 - 更新CLAUDE.md文档
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
"""
|
||||
本地TTS语音播报模块 - 基于IndexTTS2
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import tempfile
|
||||
import winsound
|
||||
|
||||
# IndexTTS2 路径配置
|
||||
_INDEXTTS_HOME = r"E:\2025Code\python\IndexTT\index-tts"
|
||||
_INDEXTTS_MODEL_DIR = os.path.join(_INDEXTTS_HOME, "checkpoints")
|
||||
_INDEXTTS_CFG = os.path.join(_INDEXTTS_MODEL_DIR, "config.yaml")
|
||||
_INDEXTTS_VENV_PYTHON = os.path.join(_INDEXTTS_HOME, ".venv", "Scripts", "python.exe")
|
||||
|
||||
# 默认语音样本
|
||||
_DEFAULT_VOICE_PROMPT = os.path.join(_INDEXTTS_HOME, "examples", "voice_01.wav")
|
||||
|
||||
# 全局 TTS 实例(延迟初始化)
|
||||
_tts_instance = None
|
||||
|
||||
|
||||
def _get_tts():
|
||||
"""延迟初始化 IndexTTS2 实例"""
|
||||
global _tts_instance
|
||||
if _tts_instance is None:
|
||||
sys.path.insert(0, _INDEXTTS_HOME)
|
||||
from indextts.infer_v2 import IndexTTS2
|
||||
print("[TTS] 正在加载 IndexTTS2 模型(首次加载较慢,请耐心等待)...")
|
||||
_tts_instance = IndexTTS2(
|
||||
cfg_path=_INDEXTTS_CFG,
|
||||
model_dir=_INDEXTTS_MODEL_DIR,
|
||||
use_fp16=True,
|
||||
use_cuda_kernel=False,
|
||||
use_deepspeed=False
|
||||
)
|
||||
print("[TTS] IndexTTS2 模型加载完成")
|
||||
return _tts_instance
|
||||
|
||||
|
||||
def speak(text, voice_prompt=None):
|
||||
"""使用本地 IndexTTS2 合成语音并播放
|
||||
|
||||
Args:
|
||||
text: 要播报的文本
|
||||
voice_prompt: 语音样本路径,默认使用 voice_01.wav
|
||||
"""
|
||||
if voice_prompt is None:
|
||||
voice_prompt = _DEFAULT_VOICE_PROMPT
|
||||
|
||||
tts = _get_tts()
|
||||
output_path = os.path.join(tempfile.gettempdir(), f"fx_tts_{int(time.time() * 1000)}.wav")
|
||||
|
||||
tts.infer(
|
||||
spk_audio_prompt=voice_prompt,
|
||||
text=text,
|
||||
output_path=output_path,
|
||||
verbose=False
|
||||
)
|
||||
|
||||
# 播放生成的音频
|
||||
winsound.PlaySound(output_path, winsound.SND_FILENAME)
|
||||
return output_path
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
speak("本地语音播报测试成功")
|
||||
Reference in New Issue
Block a user