mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
fix: 阿里云百炼流式tts语音卡顿感优化
This commit is contained in:
parent
22262be7cd
commit
83ff7bb356
1 changed files with 30 additions and 12 deletions
|
|
@ -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
|
||||||
):
|
):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue