Here's a Python script that applies a noise audio file to a clean audio file:
python
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 both audio files have the same sample rate
if clean_sample_rate != noise_sample_rate:
raise ValueError("Clean and noise audio files must have the same sample rate.")
# Ensure noise audio is at least as long as 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 noise audio to the same length as 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 = noise_audio * scale_factor
# 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 resulting 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 sf.read().
It checks if both audio files have the same sample rate and if the noise audio is at least as long as the clean audio. If not, it raises an appropriate error.
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 sf.write().
To use this script, you need to provide the paths to the clean audio file, noise audio file, and the desired output file. You also need to specify the desired SNR in decibels.
Note: Make sure you have the numpy and soundfile libraries installed. You can install them using pip install numpy soundfile.