Files
banban/talkingq-url/services/target_audio_cache.py
2026-04-16 21:13:06 +08:00

35 lines
1.2 KiB
Python

import asyncio
from typing import Dict, List, Optional
class TargetAudioCache:
def __init__(self):
self.audio_data_cache: Dict[str, List[bytes]] = {}
self.lock = asyncio.Lock()
async def add_audio_data(self, device_id: str, audio_data: bytes):
async with self.lock:
if device_id not in self.audio_data_cache:
self.audio_data_cache[device_id] = []
self.audio_data_cache[device_id].append(audio_data)
async def get_audio_data(self, device_id: str) -> Optional[bytes]:
async with self.lock:
if device_id not in self.audio_data_cache:
return None
# 合并所有音频数据
combined_audio = b''.join(self.audio_data_cache[device_id])
return combined_audio
async def clear_audio_data(self, device_id: str):
async with self.lock:
if device_id in self.audio_data_cache:
del self.audio_data_cache[device_id]
async def has_cached_audio(self, device_id: str) -> bool:
async with self.lock:
return device_id in self.audio_data_cache and len(self.audio_data_cache[device_id]) > 0
# 单例实例
target_audio_cache = TargetAudioCache()