Here's a Python script that applies a noise audio file to a clean audio file:
import numpy as np
import soundfile as sf
def apply_noise(clean_file, noise_file, output_file, snr_db):
# Read clean audio file
clean_audio, clean_sample_rate = sf.read(clean_file)
# Read noise audio file
noise_audio, noise_sample_rate = sf.read(noise_file)
# Ensure the sample rates match
assert clean_sample_rate == noise_sample_rate, "Sample rates of clean and noise audio files must match."
# Ensure the noise audio is at least as long as the clean audio
if len(noise_audio) < len(clean_audio):
raise ValueError("Noise audio file must be at least as long as the clean audio file.")
# Truncate the noise audio to the same length as the clean audio
noise_audio = noise_audio[:len(clean_audio)]
# Calculate the power of the clean audio and noise audio
clean_power = np.mean(clean_audio ** 2)
noise_power = np.mean(noise_audio ** 2)
# Calculate the scaling factor for the noise audio based on the desired SNR
snr = 10 ** (snr_db / 10)
scale_factor = np.sqrt(clean_power / (snr * noise_power))
# Scale the noise audio
scaled_noise_audio = scale_factor * noise_audio
# Add the scaled noise audio to the clean audio
noisy_audio = clean_audio + scaled_noise_audio
# Write the noisy audio to the output file
sf.write(output_file, noisy_audio, clean_sample_rate)
# Example usage
clean_file = "clean_audio.wav"
noise_file = "noise_audio.wav"
output_file = "noisy_audio.wav"
snr_db = 10 # Desired Signal-to-Noise Ratio (SNR) in decibels
apply_noise(clean_file, noise_file, output_file, snr_db)
This script does the following:
-
It imports the necessary libraries: numpy for numerical operations and soundfile for reading and writing audio files.
-
The apply_noise function takes four parameters:
clean_file: The path to the clean audio file.
noise_file: The path to the noise audio file.
output_file: The path to save the noisy audio file.
snr_db: The desired Signal-to-Noise Ratio (SNR) in decibels.
-
It reads the clean audio file and the noise audio file using soundfile.read().
-
It ensures that the sample rates of both audio files match and that the noise audio is at least as long as the clean audio.
-
It truncates the noise audio to the same length as the clean audio.
-
It calculates the power of the clean audio and the noise audio using np.mean().
-
It calculates the scaling factor for the noise audio based on the desired SNR using the formula: scale_factor = sqrt(clean_power / (snr * noise_power)).
-
It scales the noise audio by multiplying it with the scaling factor.
-
It adds the scaled noise audio to the clean audio to obtain the noisy audio.
-
Finally, it writes the noisy audio to the output file using soundfile.write().
You can customize the clean_file, noise_file, output_file, and snr_db variables according to your specific audio files and desired SNR.
Note: Make sure you have the numpy and soundfile libraries installed before running the script. You can install them using pip install numpy soundfile.