mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-25 16:43:55 +08:00
fix: 音频影响线程问题
This commit is contained in:
@@ -9,7 +9,6 @@ import asyncio
|
|||||||
import traceback
|
import traceback
|
||||||
import threading
|
import threading
|
||||||
import opuslib_next
|
import opuslib_next
|
||||||
import concurrent.futures
|
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from config.logger import setup_logging
|
from config.logger import setup_logging
|
||||||
from typing import Optional, Tuple, List
|
from typing import Optional, Tuple, List
|
||||||
@@ -94,66 +93,31 @@ class ASRProviderBase(ABC):
|
|||||||
wav_data = self._pcm_to_wav(combined_pcm_data)
|
wav_data = self._pcm_to_wav(combined_pcm_data)
|
||||||
|
|
||||||
# 定义ASR任务
|
# 定义ASR任务
|
||||||
def run_asr():
|
asr_task = self.speech_to_text(asr_audio_task, conn.session_id, conn.audio_format)
|
||||||
start_time = time.monotonic()
|
|
||||||
try:
|
|
||||||
loop = asyncio.new_event_loop()
|
|
||||||
asyncio.set_event_loop(loop)
|
|
||||||
try:
|
|
||||||
result = loop.run_until_complete(
|
|
||||||
self.speech_to_text(asr_audio_task, conn.session_id, conn.audio_format)
|
|
||||||
)
|
|
||||||
end_time = time.monotonic()
|
|
||||||
logger.bind(tag=TAG).debug(f"ASR耗时: {end_time - start_time:.3f}s")
|
|
||||||
return result
|
|
||||||
finally:
|
|
||||||
loop.close()
|
|
||||||
except Exception as e:
|
|
||||||
end_time = time.monotonic()
|
|
||||||
logger.bind(tag=TAG).error(f"ASR失败: {e}")
|
|
||||||
return ("", None)
|
|
||||||
|
|
||||||
# 定义声纹识别任务
|
if conn.voiceprint_provider and wav_data:
|
||||||
def run_voiceprint():
|
voiceprint_task = conn.voiceprint_provider.identify_speaker(wav_data, conn.session_id)
|
||||||
if not wav_data:
|
# 并发等待两个结果
|
||||||
return None
|
asr_result, voiceprint_result = await asyncio.gather(
|
||||||
try:
|
asr_task, voiceprint_task, return_exceptions=True
|
||||||
loop = asyncio.new_event_loop()
|
)
|
||||||
asyncio.set_event_loop(loop)
|
else:
|
||||||
try:
|
asr_result = await asr_task
|
||||||
# 使用连接的声纹识别提供者
|
voiceprint_result = None
|
||||||
result = loop.run_until_complete(
|
|
||||||
conn.voiceprint_provider.identify_speaker(wav_data, conn.session_id)
|
|
||||||
)
|
|
||||||
return result
|
|
||||||
finally:
|
|
||||||
loop.close()
|
|
||||||
except Exception as e:
|
|
||||||
logger.bind(tag=TAG).error(f"声纹识别失败: {e}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
# 使用线程池执行器并行运行
|
# 记录识别结果 - 检查是否为异常
|
||||||
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as thread_executor:
|
if isinstance(asr_result, Exception):
|
||||||
asr_future = thread_executor.submit(run_asr)
|
logger.bind(tag=TAG).error(f"ASR识别失败: {asr_result}")
|
||||||
|
raw_text = ""
|
||||||
|
else:
|
||||||
|
raw_text, _ = asr_result
|
||||||
|
|
||||||
if conn.voiceprint_provider and wav_data:
|
if isinstance(voiceprint_result, Exception):
|
||||||
voiceprint_future = thread_executor.submit(run_voiceprint)
|
logger.bind(tag=TAG).error(f"声纹识别失败: {voiceprint_result}")
|
||||||
|
speaker_name = ""
|
||||||
|
else:
|
||||||
|
speaker_name = voiceprint_result
|
||||||
|
|
||||||
# 等待两个线程都完成
|
|
||||||
asr_result = asr_future.result(timeout=15)
|
|
||||||
voiceprint_result = voiceprint_future.result(timeout=15)
|
|
||||||
|
|
||||||
results = {"asr": asr_result, "voiceprint": voiceprint_result}
|
|
||||||
else:
|
|
||||||
asr_result = asr_future.result(timeout=15)
|
|
||||||
results = {"asr": asr_result, "voiceprint": None}
|
|
||||||
|
|
||||||
|
|
||||||
# 处理结果
|
|
||||||
raw_text, _ = results.get("asr", ("", None))
|
|
||||||
speaker_name = results.get("voiceprint", None)
|
|
||||||
|
|
||||||
# 记录识别结果
|
|
||||||
if raw_text:
|
if raw_text:
|
||||||
logger.bind(tag=TAG).info(f"识别文本: {raw_text}")
|
logger.bind(tag=TAG).info(f"识别文本: {raw_text}")
|
||||||
if speaker_name:
|
if speaker_name:
|
||||||
|
|||||||
Reference in New Issue
Block a user