mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-25 16:43:55 +08:00
update: 增加唤醒时声纹处理 ,1秒内发送至大模型 【需优化唤醒锁机制,中途会遭受打断(偶发),考虑忽略检测】
This commit is contained in:
@@ -1,26 +1,21 @@
|
||||
import time
|
||||
import json
|
||||
import asyncio
|
||||
from core.utils.util import audio_to_data_stream
|
||||
from core.handle.abortHandle import handleAbortMessage
|
||||
from core.handle.intentHandler import handle_user_intent
|
||||
from core.utils.output_counter import check_device_output_limit
|
||||
from core.utils.util import play_audio_frames, play_audio_response
|
||||
from core.handle.sendAudioHandle import send_stt_message, SentenceType
|
||||
|
||||
TAG = __name__
|
||||
|
||||
|
||||
async def handleAudioMessage(conn, audio):
|
||||
# 检查是否在唤醒处理锁定期内
|
||||
if getattr(conn, 'wakeup_processing_lock', 0) > time.monotonic():
|
||||
return
|
||||
|
||||
# 当前片段是否有人说话
|
||||
have_voice = conn.vad.is_vad(conn, audio)
|
||||
# 如果设备刚刚被唤醒,短暂忽略VAD检测
|
||||
if have_voice and hasattr(conn, "just_woken_up") and conn.just_woken_up:
|
||||
have_voice = False
|
||||
# 设置一个短暂延迟后恢复VAD检测
|
||||
conn.asr_audio.clear()
|
||||
if not hasattr(conn, "vad_resume_task") or conn.vad_resume_task.done():
|
||||
conn.vad_resume_task = asyncio.create_task(resume_vad_detection(conn))
|
||||
return
|
||||
if have_voice:
|
||||
if conn.client_is_speaking:
|
||||
await handleAbortMessage(conn)
|
||||
@@ -29,13 +24,6 @@ async def handleAudioMessage(conn, audio):
|
||||
# 接收音频
|
||||
await conn.asr.receive_audio(conn, audio, have_voice)
|
||||
|
||||
|
||||
async def resume_vad_detection(conn):
|
||||
# 等待2秒后恢复VAD检测
|
||||
await asyncio.sleep(1)
|
||||
conn.just_woken_up = False
|
||||
|
||||
|
||||
async def startToChat(conn, text):
|
||||
# 检查输入是否是JSON格式(包含说话人信息)
|
||||
speaker_name = None
|
||||
@@ -119,9 +107,7 @@ async def max_out_size(conn):
|
||||
text = "不好意思,我现在有点事情要忙,明天这个时候我们再聊,约好了哦!明天不见不散,拜拜!"
|
||||
await send_stt_message(conn, text)
|
||||
file_path = "config/assets/max_output_size.wav"
|
||||
conn.tts.tts_audio_queue.put((SentenceType.FIRST, [], text))
|
||||
play_audio_frames(conn, file_path)
|
||||
conn.tts.tts_audio_queue.put((SentenceType.LAST, [], None))
|
||||
play_audio_response(conn, {"text": text, "file_path": file_path})
|
||||
conn.close_after_chat = True
|
||||
|
||||
|
||||
@@ -156,17 +142,4 @@ async def check_bind_device(conn):
|
||||
text = f"没有找到该设备的版本信息,请正确配置 OTA地址,然后重新编译固件。"
|
||||
await send_stt_message(conn, text)
|
||||
music_path = "config/assets/bind_not_found.wav"
|
||||
conn.tts.tts_audio_queue.put((SentenceType.FIRST, [], text))
|
||||
play_audio_frames(conn, music_path)
|
||||
conn.tts.tts_audio_queue.put((SentenceType.LAST, [], None))
|
||||
|
||||
def play_audio_frames(conn, file_path):
|
||||
"""播放音频文件并处理发送帧数据"""
|
||||
def handle_audio_frame(frame_data):
|
||||
conn.tts.tts_audio_queue.put((SentenceType.MIDDLE, frame_data, None))
|
||||
|
||||
audio_to_data_stream(
|
||||
file_path,
|
||||
is_opus=True,
|
||||
callback=handle_audio_frame
|
||||
)
|
||||
play_audio_response(conn, {"text": text, "file_path": music_path})
|
||||
|
||||
Reference in New Issue
Block a user