mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 23:23:55 +08:00
77 lines
2.9 KiB
Python
77 lines
2.9 KiB
Python
from config.logger import setup_logging
|
|
import time
|
|
from core.utils.util import remove_punctuation_and_length
|
|
from core.handle.sendAudioHandle import send_stt_message
|
|
from core.handle.intentHandler import handle_user_intent
|
|
|
|
TAG = __name__
|
|
logger = setup_logging()
|
|
|
|
|
|
async def handleAudioMessage(conn, audio):
|
|
if not conn.asr_server_receive:
|
|
# logger.bind(tag=TAG).debug(f"前期数据处理中,暂停接收")
|
|
return
|
|
if conn.client_listen_mode == "auto":
|
|
have_voice = conn.vad.is_vad(conn, audio)
|
|
else:
|
|
have_voice = conn.client_have_voice
|
|
|
|
# 如果本次没有声音,本段也没声音,就把声音丢弃了
|
|
if have_voice == False and conn.client_have_voice == False:
|
|
await no_voice_close_connect(conn)
|
|
conn.asr_audio.append(audio)
|
|
conn.asr_audio = conn.asr_audio[-10:] # 保留最新的10帧音频内容,解决ASR句首丢字问题
|
|
return
|
|
conn.client_no_voice_last_time = 0.0
|
|
conn.asr_audio.append(audio)
|
|
# 如果本段有声音,且已经停止了
|
|
if conn.client_voice_stop:
|
|
conn.client_abort = False
|
|
conn.asr_server_receive = False
|
|
# 音频太短了,无法识别
|
|
if len(conn.asr_audio) < 15:
|
|
conn.asr_server_receive = True
|
|
else:
|
|
text, file_path = await conn.asr.speech_to_text(conn.asr_audio, conn.session_id)
|
|
logger.bind(tag=TAG).info(f"识别文本: {text}")
|
|
text_len, _ = remove_punctuation_and_length(text)
|
|
if text_len > 0:
|
|
await startToChat(conn, text)
|
|
else:
|
|
conn.asr_server_receive = True
|
|
conn.asr_audio.clear()
|
|
conn.reset_vad_states()
|
|
|
|
|
|
async def startToChat(conn, text):
|
|
# 首先进行意图分析
|
|
intent_handled = await handle_user_intent(conn, text)
|
|
|
|
if intent_handled:
|
|
# 如果意图已被处理,不再进行聊天
|
|
conn.asr_server_receive = True
|
|
return
|
|
|
|
# 意图未被处理,继续常规聊天流程
|
|
await send_stt_message(conn, text)
|
|
if conn.use_function_call_mode:
|
|
# 使用支持function calling的聊天方法
|
|
conn.executor.submit(conn.chat_with_function_calling, text)
|
|
else:
|
|
conn.executor.submit(conn.chat, text)
|
|
|
|
|
|
async def no_voice_close_connect(conn):
|
|
if conn.client_no_voice_last_time == 0.0:
|
|
conn.client_no_voice_last_time = time.time() * 1000
|
|
else:
|
|
no_voice_time = time.time() * 1000 - conn.client_no_voice_last_time
|
|
close_connection_no_voice_time = conn.config.get("close_connection_no_voice_time", 120)
|
|
if not conn.close_after_chat and no_voice_time > 1000 * close_connection_no_voice_time:
|
|
conn.close_after_chat = True
|
|
conn.client_abort = False
|
|
conn.asr_server_receive = False
|
|
prompt = "请你以“时间过得真快”未来头,用富有感情、依依不舍的话来结束这场对话吧。"
|
|
await startToChat(conn, prompt)
|