-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtts_model_debugger.py
More file actions
64 lines (53 loc) · 2.4 KB
/
Copy pathtts_model_debugger.py
File metadata and controls
64 lines (53 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import asyncio
import importlib
from pathlib import Path
from src.plugins.base_tts_model import BaseTTSModel
from typing import List
import soundfile as sf
import numpy as np
class TTSModelDebugger:
def __init__(self, config_path: str):
self.config_path = config_path
self.tts_list: List[BaseTTSModel] = []
def import_module(self):
"""动态导入TTS适配"""
from src.config import Config
config = Config(self.config_path)
for tts in config.enabled_plugin.enabled:
module_name = f"src.plugins.{tts}"
try:
module = importlib.import_module(module_name)
tts_class: BaseTTSModel = module.TTSModel()
self.tts_list.append(tts_class)
except ImportError as e:
print(f"Error importing {module_name}: {e}")
except AttributeError as e:
print(f"Error accessing TTSModel in {module_name}: {e}")
except Exception as e:
print(f"Unexpected error importing {module_name}: {e}")
async def test_tts(self, text: str, platform: str):
"""测试TTS模型"""
if not self.tts_list:
print("没有启用任何TTS模型")
return
for tts_class in self.tts_list:
print(f"测试模型: {tts_class.__class__.__name__}")
try:
audio_data = await tts_class.tts(text=text, platform=platform)
audio_np = np.frombuffer(audio_data, dtype=np.int16)
print(f"模型 {tts_class.__class__.__name__} 生成了音频数据,长度: {len(audio_data)} bytes")
# 将音频数据写入WAV文件
# output_file = f"{tts_class.__class__.__name__}_output.wav"
# sf.write(output_file, audio_np, samplerate=48000, format='WAV')
# print(f"音频已保存到 {output_file}")
except Exception as e:
print(f"模型 {tts_class.__class__.__name__} 处理失败: {e}")
if __name__ == "__main__":
config_path = Path(__file__).parent / "configs" / "base.toml"
debugger = TTSModelDebugger(str(config_path))
debugger.import_module()
text_to_test = "你好,这是一段测试文本。"
platform_to_test = "qq"
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(debugger.test_tts(text_to_test, platform_to_test))