def apply(self, samples, sample_rate): noise_sound, _ = self._load_sound( self.parameters["noise_file_path"], sample_rate ) noise_sound = noise_sound[ self.parameters["noise_start_index"] : self.parameters["noise_end_index"] ] noise_rms = calculate_rms(noise_sound) if noise_rms < 1e-9: warnings.warn( "The file {} is too silent to be added as noise. Returning the input" " unchanged.".format(self.parameters["noise_file_path"]) ) return samples clean_rms = calculate_rms(samples) desired_noise_rms = calculate_desired_noise_rms( clean_rms, self.parameters["snr_in_db"] ) # Adjust the noise to match the desired noise RMS noise_sound = noise_sound * (desired_noise_rms / noise_rms) # Repeat the sound if it shorter than the input sound num_samples = len(samples) while len(noise_sound) < num_samples: noise_sound = np.concatenate((noise_sound, noise_sound)) if len(noise_sound) > num_samples: noise_sound = noise_sound[0:num_samples] # Return a mix of the input sound and the background noise sound return samples + noise_sound
def apply(self, samples, sample_rate): noise_sound, _ = self.__load_sound( self.parameters["noise_file_path"], sample_rate ) noise_sound = noise_sound[ self.parameters["noise_start_index"] : self.parameters["noise_end_index"] ] clean_rms = calculate_rms(samples) noise_rms = calculate_rms(noise_sound) desired_noise_rms = calculate_desired_noise_rms( clean_rms, self.parameters["snr_in_db"] ) # Adjust the noise to match the desired noise RMS noise_sound = noise_sound * (desired_noise_rms / noise_rms) # Repeat the sound if it shorter than the input sound num_samples = len(samples) while len(noise_sound) < num_samples: noise_sound = np.concatenate((noise_sound, noise_sound)) if len(noise_sound) > num_samples: noise_sound = noise_sound[0:num_samples] # Return a mix of the input sound and the background noise sound return samples + noise_sound
def apply(self, samples, sample_rate): num_samples = len(samples) noise_placeholder = np.zeros_like(samples) for sound_params in self.parameters["sounds"]: if sound_params["end"] < 0: # Skip a sound if it ended before the start of the input sound continue noise_samples, _ = self.__load_sound(sound_params["file_path"], sample_rate) # Apply fade in and fade out noise_gain = np.ones_like(noise_samples) fade_in_time_in_samples = int(sound_params["fade_in_time"] * sample_rate) fade_in_mask = np.linspace(0.0, 1.0, num=fade_in_time_in_samples) fade_out_time_in_samples = int(sound_params["fade_out_time"] * sample_rate) fade_out_mask = np.linspace(1.0, 0.0, num=fade_out_time_in_samples) noise_gain[:fade_in_mask.shape[0]] = fade_in_mask noise_gain[-fade_out_mask.shape[0]:] = np.minimum( noise_gain[-fade_out_mask.shape[0]:], fade_out_mask) noise_samples = noise_samples * noise_gain start_sample_index = int(sound_params["start"] * sample_rate) end_sample_index = start_sample_index + len(noise_samples) if start_sample_index < 0: # crop noise_samples: shave off a chunk in the beginning num_samples_to_shave_off = abs(start_sample_index) noise_samples = noise_samples[num_samples_to_shave_off:] start_sample_index = 0 if end_sample_index > num_samples: # crop noise_samples: shave off a chunk in the end num_samples_to_shave_off = end_sample_index - num_samples noise_samples = noise_samples[:len(noise_samples) - num_samples_to_shave_off] end_sample_index = num_samples clean_rms = calculate_rms( samples[start_sample_index:end_sample_index]) noise_rms = calculate_rms(noise_samples) if noise_rms > 0: desired_noise_rms = calculate_desired_noise_rms( clean_rms, sound_params["snr_in_db"]) # Adjust the noise to match the desired noise RMS noise_samples = noise_samples * (desired_noise_rms / (noise_rms)) noise_placeholder[ start_sample_index:end_sample_index] += noise_samples # Return a mix of the input sound and the added sounds return samples + noise_placeholder
def add_background_noise(aud, noise_aud, min_snr_in_db=3, max_snr_in_db=30): snr_in_db = random.uniform(min_snr_in_db, max_snr_in_db) # snr_in_db = np.exp(random.uniform(np.log(min_snr_in_db), np.log(max_snr_in_db))) clean_rms = calculate_rms(aud) noise_rms = calculate_rms(noise_aud) + 1e-6 desired_noise_rms = calculate_desired_noise_rms(clean_rms, snr_in_db) noise_aud = noise_aud * desired_noise_rms / noise_rms _aud = aud + noise_aud # normalize normalize_factor = max(1e-6, np.abs(_aud).max()) _aud /= normalize_factor return _aud
def test_calculate_desired_noise_rms(self): noise_rms = calculate_desired_noise_rms(clean_rms=0.5, snr=6) self.assertAlmostEqual(noise_rms, 0.2505936168136362)