This commit is contained in:
dbsd11 2026-04-13 10:23:17 +08:00 committed by GitHub
commit c8b37622e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -69,6 +69,8 @@ class TTSProvider(TTSProviderBase):
"X-DashScope-DataInspection": "enable", "X-DashScope-DataInspection": "enable",
} }
self.audio_cache_queue = queue.Queue()
async def _ensure_connection(self): async def _ensure_connection(self):
"""确保WebSocket连接可用,支持60秒内连接复用""" """确保WebSocket连接可用,支持60秒内连接复用"""
try: try:
@ -347,18 +349,22 @@ class TTSProvider(TTSProviderBase):
logger.bind(tag=TAG).debug("TTS任务启动成功~") logger.bind(tag=TAG).debug("TTS任务启动成功~")
self.tts_audio_queue.put((SentenceType.FIRST, [], None)) self.tts_audio_queue.put((SentenceType.FIRST, [], None))
elif event == "result-generated": elif event == "result-generated":
# 发送缓存的数据 output_type = data.get("payload", {}).get("output", {}).get("type")
if self.conn.tts_MessageText: if output_type == "sentence-end":
logger.bind(tag=TAG).info( self.flush_audio_cache_queue()
f"句子语音生成成功: {self.conn.tts_MessageText}" original_text = data.get("payload", {}).get("output", {}).get("original_text", "")
) if original_text:
self.tts_audio_queue.put( logger.bind(tag=TAG).info(
(SentenceType.FIRST, [], self.conn.tts_MessageText) f"句子语音生成成功: {original_text}"
) )
self.conn.tts_MessageText = None self.tts_audio_queue.put(
(SentenceType.FIRST, [], original_text)
)
elif event == "task-finished": elif event == "task-finished":
logger.bind(tag=TAG).debug("TTS任务完成~") logger.bind(tag=TAG).debug("TTS任务完成~")
self.flush_audio_cache_queue()
self._process_before_stop_play_files() self._process_before_stop_play_files()
self.conn.tts_MessageText = None
session_finished = True session_finished = True
break break
elif event == "task-failed": elif event == "task-failed":
@ -367,13 +373,12 @@ class TTSProvider(TTSProviderBase):
logger.bind(tag=TAG).error( logger.bind(tag=TAG).error(
f"TTS任务失败: {error_code} - {error_message}" f"TTS任务失败: {error_code} - {error_message}"
) )
self.conn.tts_MessageText = None
break break
except json.JSONDecodeError: except json.JSONDecodeError:
logger.bind(tag=TAG).warning("收到无效的JSON消息") logger.bind(tag=TAG).warning("收到无效的JSON消息")
elif isinstance(msg, (bytes, bytearray)): elif isinstance(msg, (bytes, bytearray)):
self.opus_encoder.encode_pcm_to_opus_stream( self.audio_cache_queue.put(msg)
msg, False, callback=self.handle_opus
)
except websockets.ConnectionClosed: except websockets.ConnectionClosed:
logger.bind(tag=TAG).warning("WebSocket连接已关闭") logger.bind(tag=TAG).warning("WebSocket连接已关闭")
break break
@ -394,6 +399,19 @@ class TTSProvider(TTSProviderBase):
finally: finally:
self._monitor_task = None self._monitor_task = None
def flush_audio_cache_queue(self):
combined_pcm_data = b""
while not self.audio_cache_queue.empty():
try:
pcm_data = self.audio_cache_queue.get_nowait()
combined_pcm_data += pcm_data
except queue.Empty:
break
if combined_pcm_data:
self.opus_encoder.encode_pcm_to_opus_stream(
combined_pcm_data, False, callback=self.handle_opus
)
def audio_to_opus_data_stream( def audio_to_opus_data_stream(
self, audio_file_path, callback: Callable[[Any], Any] = None self, audio_file_path, callback: Callable[[Any], Any] = None
): ):