From 5c261528d026cc1489b030c58406ad172c7affd1 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Sat, 13 Dec 2025 23:10:40 +0800 Subject: [PATCH] =?UTF-8?q?update:=E5=B8=B8=E7=94=A8=E9=9F=B3=E9=A2=91?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BC=93=E5=AD=98=EF=BC=8C=E6=8A=B5=E5=BE=A1?= =?UTF-8?q?=E9=AB=98=E5=B9=B6=E5=8F=91=E6=9C=AA=E6=8E=88=E6=9D=83=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xiaozhi-server/core/handle/helloHandle.py | 2 +- .../xiaozhi-server/core/utils/cache/config.py | 4 ++++ main/xiaozhi-server/core/utils/util.py | 24 +++++++++++++++++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/main/xiaozhi-server/core/handle/helloHandle.py b/main/xiaozhi-server/core/handle/helloHandle.py index efe66b59..a4220d5a 100644 --- a/main/xiaozhi-server/core/handle/helloHandle.py +++ b/main/xiaozhi-server/core/handle/helloHandle.py @@ -101,7 +101,7 @@ async def checkWakeupWords(conn, text): } # 获取音频数据 - opus_packets = await audio_to_data(response.get("file_path")) + opus_packets = await audio_to_data(response.get("file_path"), use_cache=False) # 播放唤醒词回复 conn.client_abort = False diff --git a/main/xiaozhi-server/core/utils/cache/config.py b/main/xiaozhi-server/core/utils/cache/config.py index 248c2af7..f85e40bb 100644 --- a/main/xiaozhi-server/core/utils/cache/config.py +++ b/main/xiaozhi-server/core/utils/cache/config.py @@ -19,6 +19,7 @@ class CacheType(Enum): CONFIG = "config" DEVICE_PROMPT = "device_prompt" VOICEPRINT_HEALTH = "voiceprint_health" # 声纹识别健康检查 + AUDIO_DATA = "audio_data" # 音频数据缓存 @dataclass @@ -58,5 +59,8 @@ class CacheConfig: CacheType.VOICEPRINT_HEALTH: cls( strategy=CacheStrategy.TTL, ttl=600, max_size=100 # 10分钟过期 ), + CacheType.AUDIO_DATA: cls( + strategy=CacheStrategy.TTL, ttl=600, max_size=100 # 10分钟过期 + ), } return configs.get(cache_type, cls()) diff --git a/main/xiaozhi-server/core/utils/util.py b/main/xiaozhi-server/core/utils/util.py index 2baedf68..4547ad8f 100644 --- a/main/xiaozhi-server/core/utils/util.py +++ b/main/xiaozhi-server/core/utils/util.py @@ -269,13 +269,27 @@ def audio_to_data_stream( pcm_to_data_stream(raw_data, is_opus, callback) -async def audio_to_data(audio_file_path: str, is_opus: bool = True) -> list[bytes]: +async def audio_to_data( + audio_file_path: str, is_opus: bool = True, use_cache: bool = True +) -> list[bytes]: """ 将音频文件转换为Opus/PCM编码的帧列表 Args: audio_file_path: 音频文件路径 is_opus: 是否进行Opus编码 + use_cache: 是否使用缓存 """ + from core.utils.cache.manager import cache_manager + from core.utils.cache.config import CacheType + + # 生成缓存键,包含文件路径和编码类型 + cache_key = f"{audio_file_path}:{is_opus}" + + # 尝试从缓存获取结果 + if use_cache: + cached_result = cache_manager.get(CacheType.AUDIO_DATA, cache_key) + if cached_result is not None: + return cached_result def _sync_audio_to_data(): # 获取文件后缀名 @@ -324,7 +338,13 @@ async def audio_to_data(audio_file_path: str, is_opus: bool = True) -> list[byte loop = asyncio.get_running_loop() # 在单独的线程中执行同步的音频处理操作 - return await loop.run_in_executor(None, _sync_audio_to_data) + result = await loop.run_in_executor(None, _sync_audio_to_data) + + # 将结果存入缓存,使用配置中定义的TTL(10分钟) + if use_cache: + cache_manager.set(CacheType.AUDIO_DATA, cache_key, result) + + return result def audio_bytes_to_data_stream(