But first, why this is so cool.
Imagine you have an audio file and you want to do something with it. Maybe you want to add some background noise or adjust the volume. With Pythons built-in multimedia services (which include the audioop module), you can easily manipulate raw audio data without having to learn a whole new programming language just for sound processing.
So, let’s get started! Before anything else installing the library. If you haven’t already done so, open up your terminal or command prompt and type:
# Import the audioop module
import audioop
# Install the audioop library using pip
# Note: This is not a python code, it is a command to be executed in the terminal or command prompt
# Annotation: This command installs the audioop library, which allows for manipulation of raw audio data in python
# Create a variable to store the audio data
audio_data = b'\x00\x00\x00\x00\x00\x00\x00\x00'
# Use the audioop module to get the maximum value of the audio data
max_value = audioop.max(audio_data, 2)
# Annotation: This function returns the maximum value of the audio data, with a sample width of 2 bytes
# Print the maximum value
print(max_value)
# Annotation: This prints the maximum value of the audio data to the console
Now that we have our module installed, lets import it into our Python script or Jupyter notebook:
# Import the audioop module
import audioop
# The audioop module provides a collection of functions for manipulating audio data.
# It is used to perform various operations on audio data, such as converting between different audio formats,
# adjusting volume levels, and detecting silence.
# The import statement allows us to access the functions and classes within the audioop module.
# Now we can use the functions within the audioop module to manipulate audio data in our script or notebook.
Alright, now you’re ready to start manipulating some raw audio data. Let’s say you want to add a little background noise to your favorite song. Here’s how you can do that using the `audioop.add()` function:
# Import necessary libraries
import audioop # Importing the audioop library for audio manipulation
from scipy.io import wavfile # Importing the wavfile module from the scipy library
# Load in our original audio file (in this case, "music.wav")
samplerate, data = wavfile.read("music.wav") # Using the wavfile.read() function to read the audio file and store the sample rate and data in variables
# Generate some background noise using Python's random module
import random # Importing the random module for generating random numbers
noise_data = [random.uniform(-1000, 1000) for _ in range(len(data))] # Using a list comprehension to generate a list of random numbers with the same length as the audio data
# Add the background noise to our original audio file (using a mixer)
new_data = [] # Creating an empty list to store the modified audio data
for i in range(len(data)): # Looping through the length of the audio data
new_data.append((audioop.add(data[i], noise_data[i]) / 2 ** 15)) # Using the audioop.add() function to add the original audio data and the background noise, and then dividing by 2 ** 15 to scale the data to the appropriate range
# Save the modified audio file as "music_with_background_noise.wav"
wavfile.write("music_with_background_noise.wav", samplerate, new_data) # Using the wavfile.write() function to write the modified audio data to a new file with the specified sample rate
And that’s it! You now have a brand-new version of your favorite song with some background noise added in.
But wait what if you want to adjust the volume instead? No problem! The `audioop.mul()` function lets us do just that:
# Import necessary libraries
import audioop # Import the audioop library for audio operations
from scipy.io import wavfile # Import the wavfile module from the scipy library for reading and writing wav files
# Load in our original audio file (in this case, "music.wav")
samplerate, data = wavfile.read("music.wav") # Use the wavfile.read() function to read the wav file and store the sample rate and data in variables
# Adjust the volume by multiplying each sample by a factor of 2
new_data = [] # Create an empty list to store the modified data
for i in range(len(data)): # Loop through each sample in the data
new_data.append((audioop.mul(data[i], 2) / 2 ** 15)) # Use the audioop.mul() function to multiply each sample by 2 and divide by 2^15 to adjust the volume, then append the result to the new_data list
# Save the modified audio file as "music_with_adjusted_volume.wav"
wavfile.write("music_with_adjusted_volume.wav", samplerate, new_data) # Use the wavfile.write() function to write the modified data to a new wav file with the specified sample rate and file name
And that’s it! You now have a brand-new version of your favorite song with the volume adjusted to twice its original level.
With just a few lines of code, you can manipulate raw audio data like a boss and create some truly amazing soundscapes.