delete 测试代码

This commit is contained in:
Ken 2025-03-11 14:24:33 +08:00
parent a2abacc634
commit 9fc1cefffe
4 changed files with 5 additions and 45 deletions

View file

@ -88,18 +88,6 @@ class ConnectionHandler:
self.auth_code_gen = AuthCodeGenerator.get_instance() self.auth_code_gen = AuthCodeGenerator.get_instance()
self.is_device_verified = False # 添加设备验证状态标志 self.is_device_verified = False # 添加设备验证状态标志
self.music_handler = _music self.music_handler = _music
# 确保路径指向core目录下的assets文件夹
# 我打个盹
self.default_audio_path_0 = os.path.join(
os.path.dirname(__file__), # 当前文件所在目录(core/)
"assets/network_timeout_0.wav"
)
#网络超时
self.default_audio_path_1 = os.path.join(
os.path.dirname(__file__), # 当前文件所在目录(core/)
"assets/network_timeout_1.wav"
)
async def handle_connection(self, ws): async def handle_connection(self, ws):
try: try:
@ -306,14 +294,6 @@ class ConnectionHandler:
try: try:
self.logger.bind(tag=TAG).debug("正在处理TTS任务...") self.logger.bind(tag=TAG).debug("正在处理TTS任务...")
tts_file, text, text_index = future.result(timeout=10) tts_file, text, text_index = future.result(timeout=10)
# 新增重试逻辑
retry_count = 0
max_retries = 3 # 最大重试次数
while (text is None or len(text) <= 0 or tts_file is None) and retry_count < max_retries:
retry_count += 1
self.logger.bind(tag=TAG).warning(f"TTS生成失败,第{retry_count}次重试...")
tts_file, text, text_index = self.speak_and_play(text, text_index)
if text is None or len(text) <= 0: if text is None or len(text) <= 0:
self.logger.bind(tag=TAG).error(f"TTS出错:{text_index}: tts text is empty") self.logger.bind(tag=TAG).error(f"TTS出错:{text_index}: tts text is empty")
elif tts_file is None: elif tts_file is None:
@ -326,12 +306,8 @@ class ConnectionHandler:
self.logger.bind(tag=TAG).error(f"TTS出错:文件不存在{tts_file}") self.logger.bind(tag=TAG).error(f"TTS出错:文件不存在{tts_file}")
except TimeoutError: except TimeoutError:
self.logger.bind(tag=TAG).error("TTS超时") self.logger.bind(tag=TAG).error("TTS超时")
if os.path.exists(self.default_audio_path_0):
opus_datas, _ = self.tts.wav_to_opus_data(self.default_audio_path)
except Exception as e: except Exception as e:
self.logger.bind(tag=TAG).error(f"TTS出错: {e}") self.logger.bind(tag=TAG).error(f"TTS出错: {e}")
if os.path.exists(self.default_audio_path_0):
opus_datas, _ = self.tts.wav_to_opus_data(self.default_audio_path)
if not self.client_abort: if not self.client_abort:
# 如果没有中途打断就发送语音 # 如果没有中途打断就发送语音
self.audio_play_queue.put((opus_datas, text, text_index)) self.audio_play_queue.put((opus_datas, text, text_index))
@ -339,8 +315,6 @@ class ConnectionHandler:
os.remove(tts_file) os.remove(tts_file)
except Exception as e: except Exception as e:
self.logger.bind(tag=TAG).error(f"TTS任务处理错误: {e}") self.logger.bind(tag=TAG).error(f"TTS任务处理错误: {e}")
if os.path.exists(self.default_audio_path_1):
opus_datas, _ = self.tts.wav_to_opus_data(self.default_audio_path)
self.clearSpeakStatus() self.clearSpeakStatus()
asyncio.run_coroutine_threadsafe( asyncio.run_coroutine_threadsafe(
self.websocket.send(json.dumps({"type": "tts", "state": "stop", "session_id": self.session_id})), self.websocket.send(json.dumps({"type": "tts", "state": "stop", "session_id": self.session_id})),

View file

@ -18,38 +18,24 @@ class TTSProviderBase(ABC):
@abstractmethod @abstractmethod
def generate_filename(self): def generate_filename(self):
pass pass
def _filter_special_symbols(self, text):
"""过滤AI会朗读的特殊符号"""
import re
# 保留中文、英文、数字、基本标点
# 特别处理URL中的斜杠(替换为空格避免连读)
text = re.sub(r'(http[s]?://[^\s]+)', lambda m: m.group(0).replace('/', ' '), text)
# 移除其他特殊符号
return re.sub(r'[<>\[\](){}♫♪★☆▽※↑↓←→\\|*#_~`^€£¥¢§©®™]', '', text)
def to_tts(self, text): def to_tts(self, text):
tmp_file = self.generate_filename() tmp_file = self.generate_filename()
# 我打个盹
self.default_audio_path_0 = os.path.join(
os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../core/assets")),
"network_timeout_0.mp3"
)
try: try:
max_repeat_time = 5 max_repeat_time = 5
while not os.path.exists(tmp_file) and max_repeat_time > 0: while not os.path.exists(tmp_file) and max_repeat_time > 0:
# 新增过滤处理 asyncio.run(self.text_to_speak(text, tmp_file))
filtered_text = self._filter_special_symbols(text)
asyncio.run(self.text_to_speak(filtered_text, tmp_file))
if not os.path.exists(tmp_file): if not os.path.exists(tmp_file):
max_repeat_time = max_repeat_time - 1 max_repeat_time = max_repeat_time - 1
logger.bind(tag=TAG).error(f"语音生成失败: {filtered_text}:{tmp_file},再试{max_repeat_time}次") logger.bind(tag=TAG).error(f"语音生成失败: {text}:{tmp_file},再试{max_repeat_time}次")
if max_repeat_time > 0: if max_repeat_time > 0:
logger.bind(tag=TAG).info(f"语音生成成功: {filtered_text}:{tmp_file},重试{5 - max_repeat_time}次") logger.bind(tag=TAG).info(f"语音生成成功: {text}:{tmp_file},重试{5 - max_repeat_time}次")
return tmp_file return tmp_file
except Exception as e: except Exception as e:
logger.bind(tag=TAG).info(f"Failed to generate TTS file: {e}") logger.bind(tag=TAG).info(f"Failed to generate TTS file: {e}")
return self.default_audio_path_0 return None
@abstractmethod @abstractmethod
async def text_to_speak(self, text, output_file): async def text_to_speak(self, text, output_file):