修改设备端对接
This commit is contained in:
35
talkingq-url/services/target_audio_cache.py
Normal file
35
talkingq-url/services/target_audio_cache.py
Normal file
@@ -0,0 +1,35 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user