Audio Playback Interface
The Humanoid SDK provides a comprehensive set of audio playback interfaces, supporting playback of audio files, text-to-speech, and other functions. This document describes in detail how to use these interfaces.
Usage Instructions
Before using these interfaces, you need to perform the following operations on the host computer (these operations must be performed regardless of the host computer model):
In the <kuavo_ros_application>/src/kuavo_speech_synthesis directory, execute the following:
sudo su && ./install.sh- Because the offline model is large, the download process may take 10–20 minutes, so please wait patiently. After installation and deployment are complete, a green success message will appear.
Interface Description
RobotAudio
The RobotAudio class provides interfaces for controlling robot audio playback.
Method Interfaces
| Function Name | play_audio |
|---|---|
| Function Prototype | bool play_audio(str file_name, int volume = 100, float speed = 1.0) |
| Function Overview | Plays the specified audio file. |
| Parameters | file_name: Audio file name volume: Volume level, range [0, 100], default 100 speed: Playback speed, default 1.0 |
| Return Value | true: Playback request sent successfully false: Failed to send playback request |
| Remarks | 1. The audio file must be placed in the robot system in advance 2. The volume is automatically limited to the valid range |
| Function Name | stop_music |
|---|---|
| Function Prototype | bool stop_music() |
| Function Overview | Stops the audio currently being played. |
| Parameters | None |
| Return Value | true: Stop request sent successfully false: Failed to send stop request |
| Remarks | None |
| Function Name | text_to_speech |
|---|---|
| Function Prototype | bool text_to_speech(str text, float volume = 0.5) |
| Function Overview | Converts text to speech and plays it. |
| Parameters | text: The text content to be played volume: Volume level, range [0.0, 1.0], default 0.5 |
| Return Value | true: Playback request sent successfully false: Failed to send playback request |
| Remarks | 1. Supports Chinese text-to-speech 2. The volume is automatically limited to the valid range |
Example Code
Refer to the SDK example code:
Audio playback: <kuavo-ros-opensource>/src/kuavo_humanoid_websocket_sdk/examples/atomic_skills/audio_play.py
Demonstrates how to control the robot via the SDK to play audio files and perform text-to-speech.
The example code demonstrates the following main functions:
-
Play an audio file:
kuavo_robot = KuavoRobotAudio() kuavo_robot.play_audio("2_抱拳.wav") # 播放指定音频文件 time.sleep(3) # 等待播放完成 -
Text-to-speech:
kuavo_robot.text_to_speech("你好,世界") # 播放文本转语音 time.sleep(3) # 等待播放完成 -
Stop playback:
kuavo_robot.stop_music() # 停止当前播放
Notes:
- Make sure the audio file exists in the robot system before playing it
- You can adjust the volume via the volume parameter
- When playing text-to-speech, it is recommended to wait a while to ensure playback completes
- Use stop_music() to stop the current playback at any time