liumaolin
commited on
Commit
·
5ecb408
1
Parent(s):
9273b76
Add TTS generation error handling and `is_task_interrupted` helper function
Browse files- Log errors during TTS audio generation to improve debugging.
- Abstract task interruption check into `is_task_interrupted` method for better readability and reuse.
src/voice_dialogue/services/audio/generator.py
CHANGED
|
@@ -6,6 +6,7 @@ from voice_dialogue.core.base import BaseThread
|
|
| 6 |
from voice_dialogue.core.constants import dropped_audio_cache, user_still_speaking_event, voice_state_manager, \
|
| 7 |
session_manager
|
| 8 |
from voice_dialogue.models.voice_task import VoiceTask
|
|
|
|
| 9 |
from .generators import tts_manager, BaseTTSConfig
|
| 10 |
|
| 11 |
|
|
@@ -77,16 +78,35 @@ class TTSAudioGenerator(BaseThread):
|
|
| 77 |
if answer_id in dropped_audio_cache:
|
| 78 |
continue
|
| 79 |
|
| 80 |
-
if voice_task
|
| 81 |
continue
|
| 82 |
|
| 83 |
if voice_task.session_id != session_manager.current_id:
|
| 84 |
continue
|
| 85 |
|
| 86 |
voice_task.tts_start_time = time.time()
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
voice_task.tts_generated_sentence_audio = tts_generated_sentence_audio
|
| 89 |
voice_task.tts_end_time = time.time()
|
| 90 |
# print(f'生成音频:{voice_task.answer_sentence}')
|
| 91 |
|
| 92 |
self.audio_output_queue.put(voice_task)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from voice_dialogue.core.constants import dropped_audio_cache, user_still_speaking_event, voice_state_manager, \
|
| 7 |
session_manager
|
| 8 |
from voice_dialogue.models.voice_task import VoiceTask
|
| 9 |
+
from voice_dialogue.utils.logger import logger
|
| 10 |
from .generators import tts_manager, BaseTTSConfig
|
| 11 |
|
| 12 |
|
|
|
|
| 78 |
if answer_id in dropped_audio_cache:
|
| 79 |
continue
|
| 80 |
|
| 81 |
+
if self.is_task_interrupted(voice_task):
|
| 82 |
continue
|
| 83 |
|
| 84 |
if voice_task.session_id != session_manager.current_id:
|
| 85 |
continue
|
| 86 |
|
| 87 |
voice_task.tts_start_time = time.time()
|
| 88 |
+
try:
|
| 89 |
+
tts_generated_sentence_audio = self.tts_instance.synthesize(voice_task.answer_sentence)
|
| 90 |
+
except Exception as e:
|
| 91 |
+
logger.error(f"TTS 音频生成失败: {e}")
|
| 92 |
+
voice_state_manager.reset_task_id()
|
| 93 |
+
continue
|
| 94 |
+
|
| 95 |
voice_task.tts_generated_sentence_audio = tts_generated_sentence_audio
|
| 96 |
voice_task.tts_end_time = time.time()
|
| 97 |
# print(f'生成音频:{voice_task.answer_sentence}')
|
| 98 |
|
| 99 |
self.audio_output_queue.put(voice_task)
|
| 100 |
+
|
| 101 |
+
def is_task_interrupted(self, voice_task: VoiceTask) -> bool:
|
| 102 |
+
"""
|
| 103 |
+
检查语音任务是否被中断
|
| 104 |
+
|
| 105 |
+
Args:
|
| 106 |
+
voice_task: 当前处理的语音任务
|
| 107 |
+
|
| 108 |
+
Returns:
|
| 109 |
+
bool: 如果任务被中断返回True,否则返回False
|
| 110 |
+
"""
|
| 111 |
+
return (voice_state_manager.interrupt_task_id and
|
| 112 |
+
voice_task.id != voice_state_manager.interrupt_task_id)
|