So how do we use it? Let’s say you have a video with some background music and sound effects that need to be adjusted based on user input or other factors. You can create a function like this:
# Define a function to adjust audio levels based on user input or other factors
def adjust_audio(sequence):
# Define gain parameters for each audio element using ReconGainParamDefinition()
# Add a gain parameter for background music with an initial value of 0 decibels
sequence.add_recon_gain_param("background_music", 0)
# Add a gain parameter for sound effects with an initial value of -10 decibels
sequence.add_recon_gain_param("sound_effects", -10)
# Set up a listener to adjust the gain parameters based on user input or other factors
def update_audio(event):
# Check if the event is "play"
if event == "play":
# Increase volume for background music and sound effects by 5 decibels when play button is clicked
# Set the gain for background music to 5 decibels higher than the initial value of 0 decibels
sequence.set_recon_gain("background_music", 0, 5)
# Set the gain for sound effects to 5 decibels higher than the initial value of -10 decibels
sequence.set_recon_gain("sound_effects", -10, 5)
# Check if the event is "pause"
elif event == "pause":
# Decrease volume for background music and sound effects by 5 decibels when pause button is clicked
# Set the gain for background music to 5 decibels lower than the initial value of 0 decibels
sequence.set_recon_gain("background_music", 0, -5)
# Set the gain for sound effects to 5 decibels lower than the initial value of -10 decibels
sequence.set_recon_gain("sound_effects", -10, -5)
# Add the listener to the event loop
sequence.add_listener(update_audio)
In this example, we’ve defined two gain parameters: one for background music and another for sound effects. We set their initial values using add_recon_gain_param() with a name (which can be anything you want), followed by the current value of the parameter. The function then returns an object that represents the newly added parameter, which we don’t need to use in this case but could if we wanted to do more advanced things like setting limits or constraints on the gain values.
To adjust these parameters based on user input (or other factors), we create a listener using add_listener() and define a function that will be called whenever an event occurs. In our example, we’re listening for two events: “play” and “pause”. When either of those events is triggered, the update_audio() function is called with the name of the event as its argument.
Inside this function, we use set_recon_gain() to adjust the gain parameters based on the current value (which was defined using add_recon_gain_param()) and a delta value that represents how much we want to increase or decrease the volume by. In our example, we’ve chosen 5 decibels as the delta value for both events, but you could adjust this depending on your needs.
With just a few lines of code, you can create an interactive audiovisual sequence that responds to user input and allows you to control the volume of specific audio elements using gain parameters defined with ReconGainParamDefinition().