From 2f93e9a3134c6ab1030b2c3726ad18cf45e0c0e9 Mon Sep 17 00:00:00 2001 From: haotian <2421912570@qq.com> Date: Mon, 1 Sep 2025 11:05:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=BC=82=E6=AD=A5=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/g1/audio/async_audio.py | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 unitree_sdk2_python-master/example/g1/audio/async_audio.py diff --git a/unitree_sdk2_python-master/example/g1/audio/async_audio.py b/unitree_sdk2_python-master/example/g1/audio/async_audio.py new file mode 100644 index 0000000..50a5e64 --- /dev/null +++ b/unitree_sdk2_python-master/example/g1/audio/async_audio.py @@ -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) \ No newline at end of file