I have two audio files that I would like to mix in Python.
I'll refer to the original audio as "Audio A" and the audio to mix is "Audio B". I am able to add white noise at a specific SNR to an the Audio A signal as described here:
audio, sr = librosa.load(file_name, sr=None, res_type='kaiser_fast')
power = audio ** 2 # Calculate power
signalpower_db = 10 * np.log10(power) # convert power to dB
#snr_dB = 0 # add SNR of specified dB
signal_average_power = np.mean(power) # Calculate signal power)
signal_averagepower_dB = 10 * np.log10(signal_average_power) # convert signal power to dB
noise_dB = signal_averagepower_dB - snr_dB # Calculate noise
noise_watts = 10 ** (noise_dB / 10) # Convert noise from dB to watts
# Generate sample of white noise
mean_noise = 0
noise = np.random.normal(mean_noise, np.sqrt(noise_watts), len(audio))
noise_signal = (audio + noise) / 1.3 # To prevent clipping of signal
In the code given here, I have done a 0 dB SNR.
How do I now, instead of white noise, use "Audio B" as the noise source and obtain a SNR of 0dB. That is, how to replace np.random.normal
noise with Audio B as the noise source injected in the original signal "Audio A" at a SNR = 0?
Any help and guidance is sincerely appreciated!
You have to make sure that the two audios have the same duration and then you can calculate what is the gain that gives the desired SNR. If you simply add then you will change the energy of the input signal, so I adjust the signal both the signal energy and the noise energy so that the noisy signal has the same energy as the clean signal (assuming that noise is uncorrelated)
Here an example how you can use the function