Files
xiaozhi-esp32-server/main/xiaozhi-server/core/handle/helloHandle.py
T

114 lines
3.8 KiB
Python
Raw Normal View History

2025-05-27 18:06:44 +08:00
import os
import time
2025-02-02 23:01:14 +08:00
import json
2025-05-27 18:06:44 +08:00
import random
2025-03-23 22:02:13 +08:00
import shutil
import asyncio
2025-05-27 18:06:44 +08:00
from core.handle.sendAudioHandle import send_stt_message
from core.utils.util import remove_punctuation_and_length
2025-05-27 18:51:08 +08:00
from core.providers.tts.dto.dto import ContentType, InterfaceType
2025-05-27 18:06:44 +08:00
2025-02-02 23:01:14 +08:00
2025-04-12 17:36:04 +08:00
TAG = __name__
2025-02-02 23:01:14 +08:00
2025-03-31 22:54:37 +08:00
WAKEUP_CONFIG = {
"dir": "config/assets/",
"file_name": "wakeup_words",
"create_time": time.time(),
"refresh_time": 10,
"words": ["你好小智", "你好啊小智", "小智你好", "小智"],
"text": "",
}
2025-03-23 22:02:13 +08:00
2025-02-02 23:01:14 +08:00
2025-05-06 14:57:29 +08:00
async def handleHelloMessage(conn, msg_json):
"""处理hello消息"""
audio_params = msg_json.get("audio_params")
if audio_params:
format = audio_params.get("format")
2025-05-08 11:31:12 +08:00
conn.logger.bind(tag=TAG).info(f"客户端音频格式: {format}")
2025-05-06 14:57:29 +08:00
conn.audio_format = format
2025-05-24 12:11:13 +08:00
if conn.asr is not None:
conn.asr.set_audio_format(format)
2025-05-08 11:11:28 +08:00
conn.welcome_msg["audio_params"] = audio_params
2025-05-06 14:57:29 +08:00
2025-02-02 23:01:14 +08:00
await conn.websocket.send(json.dumps(conn.welcome_msg))
2025-03-23 22:02:13 +08:00
async def checkWakeupWords(conn, text):
2025-03-31 22:54:37 +08:00
enable_wakeup_words_response_cache = conn.config[
"enable_wakeup_words_response_cache"
]
2025-05-27 18:51:08 +08:00
"""是否用的是非流式tts"""
if conn.tts and conn.tts.interface_type != InterfaceType.NON_STREAM:
return False
2025-03-23 22:02:13 +08:00
"""是否开启唤醒词加速"""
if not enable_wakeup_words_response_cache:
return False
"""检查是否是唤醒词"""
2025-05-19 15:18:01 +08:00
_, filtered_text = remove_punctuation_and_length(text)
if filtered_text in conn.config.get("wakeup_words"):
2025-03-23 22:02:13 +08:00
await send_stt_message(conn, text)
file = getWakeupWordFile(WAKEUP_CONFIG["file_name"])
2025-03-23 22:02:13 +08:00
if file is None:
asyncio.create_task(wakeupWordsResponse(conn))
return False
text_hello = WAKEUP_CONFIG["text"]
if not text_hello:
text_hello = text
2025-05-27 18:06:44 +08:00
conn.tts.tts_one_sentence(
conn, ContentType.FILE, content_file=file, content_detail=text_hello
)
2025-03-23 22:02:13 +08:00
if time.time() - WAKEUP_CONFIG["create_time"] > WAKEUP_CONFIG["refresh_time"]:
asyncio.create_task(wakeupWordsResponse(conn))
return True
return False
def getWakeupWordFile(file_name):
for file in os.listdir(WAKEUP_CONFIG["dir"]):
if file.startswith("my_" + file_name):
"""避免缓存文件是一个空文件"""
2025-03-31 16:48:56 +08:00
if os.stat(f"config/assets/{file}").st_size > (15 * 1024):
return f"config/assets/{file}"
2025-03-23 22:02:13 +08:00
"""查找config/assets/目录下名称为wakeup_words的文件"""
for file in os.listdir(WAKEUP_CONFIG["dir"]):
if file.startswith(file_name):
2025-03-23 22:02:13 +08:00
return f"config/assets/{file}"
return None
async def wakeupWordsResponse(conn):
2025-04-12 17:36:04 +08:00
wait_max_time = 5
while conn.llm is None or not conn.llm.response_no_stream:
await asyncio.sleep(1)
wait_max_time -= 1
if wait_max_time <= 0:
2025-05-07 18:06:13 +08:00
conn.logger.bind(tag=TAG).error("连接对象没有llm")
2025-04-12 17:36:04 +08:00
return
2025-03-23 22:02:13 +08:00
"""唤醒词响应"""
wakeup_word = random.choice(WAKEUP_CONFIG["words"])
2025-03-23 22:59:09 +08:00
result = conn.llm.response_no_stream(conn.config["prompt"], wakeup_word)
2025-04-16 16:44:44 +08:00
if result is None or result == "":
return
2025-05-26 02:20:38 +08:00
tts_file = await asyncio.to_thread(conn.tts.to_tts, result)
2025-03-23 22:02:13 +08:00
if tts_file is not None and os.path.exists(tts_file):
file_type = os.path.splitext(tts_file)[1]
if file_type:
2025-03-31 22:54:37 +08:00
file_type = file_type.lstrip(".")
old_file = getWakeupWordFile("my_" + WAKEUP_CONFIG["file_name"])
2025-03-23 22:02:13 +08:00
if old_file is not None:
os.remove(old_file)
"""将文件挪到"wakeup_words.mp3"""
2025-03-31 22:54:37 +08:00
shutil.move(
tts_file,
WAKEUP_CONFIG["dir"] + "my_" + WAKEUP_CONFIG["file_name"] + "." + file_type,
)
2025-03-23 22:02:13 +08:00
WAKEUP_CONFIG["create_time"] = time.time()
WAKEUP_CONFIG["text"] = result