mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-29 18:23:59 +08:00
fix: 修复使用豆包流式ASR时,不能使用声纹识别功能的问题。
This commit is contained in:
@@ -57,6 +57,16 @@ class ASRProvider(ASRProviderBase):
|
|||||||
conn.asr_audio.append(audio)
|
conn.asr_audio.append(audio)
|
||||||
conn.asr_audio = conn.asr_audio[-10:]
|
conn.asr_audio = conn.asr_audio[-10:]
|
||||||
|
|
||||||
|
# 存储音频数据
|
||||||
|
if not hasattr(conn, 'asr_audio_for_voiceprint'):
|
||||||
|
conn.asr_audio_for_voiceprint = []
|
||||||
|
conn.asr_audio_for_voiceprint.append(audio)
|
||||||
|
|
||||||
|
# 当没有音频数据时处理完整语音片段
|
||||||
|
if not audio and len(conn.asr_audio_for_voiceprint) > 0:
|
||||||
|
await self.handle_voice_stop(conn, conn.asr_audio_for_voiceprint)
|
||||||
|
conn.asr_audio_for_voiceprint = []
|
||||||
|
|
||||||
# 如果本次有声音,且之前没有建立连接
|
# 如果本次有声音,且之前没有建立连接
|
||||||
if audio_have_voice and self.asr_ws is None and not self.is_processing:
|
if audio_have_voice and self.asr_ws is None and not self.is_processing:
|
||||||
try:
|
try:
|
||||||
@@ -148,6 +158,8 @@ class ASRProvider(ASRProviderBase):
|
|||||||
async def _forward_asr_results(self, conn):
|
async def _forward_asr_results(self, conn):
|
||||||
try:
|
try:
|
||||||
while self.asr_ws and not conn.stop_event.is_set():
|
while self.asr_ws and not conn.stop_event.is_set():
|
||||||
|
# 获取当前连接的音频数据
|
||||||
|
audio_data = getattr(conn, 'asr_audio_for_voiceprint', [])
|
||||||
try:
|
try:
|
||||||
response = await self.asr_ws.recv()
|
response = await self.asr_ws.recv()
|
||||||
result = self.parse_response(response)
|
result = self.parse_response(response)
|
||||||
@@ -171,7 +183,8 @@ class ASRProvider(ASRProviderBase):
|
|||||||
logger.bind(tag=TAG).error(f"识别文本:空")
|
logger.bind(tag=TAG).error(f"识别文本:空")
|
||||||
self.text = ""
|
self.text = ""
|
||||||
conn.reset_vad_states()
|
conn.reset_vad_states()
|
||||||
await self.handle_voice_stop(conn, None)
|
if len(audio_data) > 15: # 确保有足够音频数据
|
||||||
|
await self.handle_voice_stop(conn, audio_data)
|
||||||
break
|
break
|
||||||
|
|
||||||
for utterance in utterances:
|
for utterance in utterances:
|
||||||
@@ -181,7 +194,8 @@ class ASRProvider(ASRProviderBase):
|
|||||||
f"识别到文本: {self.text}"
|
f"识别到文本: {self.text}"
|
||||||
)
|
)
|
||||||
conn.reset_vad_states()
|
conn.reset_vad_states()
|
||||||
await self.handle_voice_stop(conn, None)
|
if len(audio_data) > 15: # 确保有足够音频数据
|
||||||
|
await self.handle_voice_stop(conn, audio_data)
|
||||||
break
|
break
|
||||||
elif "error" in payload:
|
elif "error" in payload:
|
||||||
error_msg = payload.get("error", "未知错误")
|
error_msg = payload.get("error", "未知错误")
|
||||||
@@ -208,6 +222,13 @@ class ASRProvider(ASRProviderBase):
|
|||||||
await self.asr_ws.close()
|
await self.asr_ws.close()
|
||||||
self.asr_ws = None
|
self.asr_ws = None
|
||||||
self.is_processing = False
|
self.is_processing = False
|
||||||
|
if conn:
|
||||||
|
if hasattr(conn, 'asr_audio_for_voiceprint'):
|
||||||
|
conn.asr_audio_for_voiceprint = []
|
||||||
|
if hasattr(conn, 'asr_audio'):
|
||||||
|
conn.asr_audio = []
|
||||||
|
if hasattr(conn, 'has_valid_voice'):
|
||||||
|
conn.has_valid_voice = False
|
||||||
|
|
||||||
def stop_ws_connection(self):
|
def stop_ws_connection(self):
|
||||||
if self.asr_ws:
|
if self.asr_ws:
|
||||||
@@ -349,3 +370,12 @@ class ASRProvider(ASRProviderBase):
|
|||||||
pass
|
pass
|
||||||
self.forward_task = None
|
self.forward_task = None
|
||||||
self.is_processing = False
|
self.is_processing = False
|
||||||
|
# 清理所有连接的音频缓冲区
|
||||||
|
if hasattr(self, '_connections'):
|
||||||
|
for conn in self._connections.values():
|
||||||
|
if hasattr(conn, 'asr_audio_for_voiceprint'):
|
||||||
|
conn.asr_audio_for_voiceprint = []
|
||||||
|
if hasattr(conn, 'asr_audio'):
|
||||||
|
conn.asr_audio = []
|
||||||
|
if hasattr(conn, 'has_valid_voice'):
|
||||||
|
conn.has_valid_voice = False
|
||||||
|
|||||||
Reference in New Issue
Block a user