10 Play Audio Files
Play Audio Files
For security reasons, you don't have direct access to audio devices through JupyterLab (environmental limitations), and we don't have a code block for users to run.
The program here comes from the audio_ctrl.py of the main program of the product, and you can refer to the code here to understand how the main program of the product implements the audio file playback function.
There is a folder called sounds in the main folder of the product, and there are many subfolders in this folder: connected, others, recv_new_cmd, robot_started, searching_for_target, target_detected, target_locked.
In the default program we provide, there is only one audio file in connected and one in each robot_started.
When the main program of the robot is running, it will automatically play an audio file within a robot_started.
When a client uses a browser to connect to this WEB application, it will automatically randomly play an audio file within the connected.
You can put custom audio files in these folders as voice packs to customize your product.
import pygame # import pygame library for audio playback import random # import random library for random selection of audio files import yaml # import yaml library for reading configuration files import os # import os library for file operations import threading # Import the threading library for multithreading # Get the configuration file curpath = os.path.realpath(__file__) # Get the absolute path of the current script thisPath = os.path.dirname(curpath) # Get the directory where the current script is located with open(thisPath + '/config.yaml', 'r') as yaml_file: # Open the configuration file config = yaml.safe_load(yaml_file) # Load configuration files using yaml library # Initialize pygame.mixer and set the default volume for audio output pygame.mixer.init() pygame.mixer.music.set_volume(config['audio_config']['default_volume']) # Create an event object that controls audio playback play_audio_event = threading. Event() # Read the minimum playback interval from the config file min_time_bewteen_play = config['audio_config']['min_time_bewteen_play'] # Define the function to play audio def play_audio(input_audio_file): try: pygame.mixer.music.load(input_audio_file) # Load the audio file pygame.mixer.music.play() # Play audio except: play_audio_event.clear() # Clear the event flag when an error occurs return while pygame.mixer.music.get_busy(): # Wait for audio playback to complete pass time.sleep(min_time_bewteen_play) # Wait for minimum playback interval play_audio_event.clear() # Clear event # Define the function to play random audio def play_random_audio(input_dirname, force_flag): if play_audio_event.is_set() and not force_flag: return # Get all audio files in the specified directory audio_files = [f for f in os.listdir(current_ path + "/sounds/" + input_dirname) if f.endswith((".mp3", ".wav")))] # Randomly select an audio file from the list of audio files audio_file = random.choice(audio_files) play_audio_event.set() # Set up the event # Create a thread to play the audio audio_ thread = threading. Thread(target=play_audio, args=(current_path + "/sounds/" + input_dirname + "/" + audio_file,)) audio_thread.start() # Start the thread # Define the thread function to play audio def play_audio_thread(input_file): if play_audio_event.is_set(): # Return if the event has already been set return play_audio_event.set() # Set the event # Create a thread to play audio audio_thread = threading. Thread(target=play_audio, args=(input_file,)) audio_thread.start() # Start the thread # Define the function to play the specified file def play_file(audio_file): audio_file = current_path + "/sounds/" + audio_file # Build the full path of the audio file play_audio_thread(audio_file) # Play the audio in a new thread # Define the function to set the audio volume def set_audio_volume(input_volume): input_volume = float(input_volume) # Convert the input volume to floating-point number if input_volume > 1: # If the volume is greater than 1, set it to 1 input_volume = 1 elif input_volume < 0: # If the volume is less than 0, set to 0 input_volume = 0 pygame.mixer.music.set_volume(input_volume) # Set the volume # Define a function to set the minimum playback interval def set_min_time_between(input_time): global min_time_bewteen_play # Use the global variable min_time_bewteen_play = input_time # Set the minimum playback interval