实现异步类

This commit is contained in:
haotian 2025-09-01 11:05:28 +08:00
parent 241ac482e4
commit 2f93e9a313

View File

@ -0,0 +1,42 @@
import asyncio
import concurrent.futures
import json
from unitree_sdk2py.g1.audio.g1_audio_client import AudioClient
# ROBOT_API_ID_AUDIO_TTS = 1001
# ROBOT_API_ID_AUDIO_ASR = 1002
# ROBOT_API_ID_AUDIO_START_PLAY = 1003
# ROBOT_API_ID_AUDIO_STOP_PLAY = 1004
# ROBOT_API_ID_AUDIO_GET_VOLUME = 1005
# ROBOT_API_ID_AUDIO_SET_VOLUME = 1006
# ROBOT_API_ID_AUDIO_SET_RGB_LED = 1010
class AsyncAudioClient(AudioClient):
def __init__(self):
super().__init__()
self.executor = concurrent.futures.ThreadPoolExecutor()
async def async_tts_maker(self, text: str, speaker_id: int):
loop = asyncio.get_event_loop()
return await loop.run_in_executor(
self.executor,
self.TtsMaker,
text,
speaker_id
)
async def main():
async_audio_client = AsyncAudioClient()
async_audio_client.Init()
# 并发执行多个TTS任务
tasks = [
async_audio_client.async_tts_maker("Hello", 0),
async_audio_client.async_tts_maker("World", 0)
]
results = await asyncio.gather(*tasks)