示例#1
0
    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
示例#2
0
    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
示例#3
0
    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
示例#4
0
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
示例#5
0
    def test_add_short_noises(self):
        sample_rate = 16000
        samples = np.sin(np.linspace(0, 440 * 2 * np.pi,
                                     9 * sample_rate)).astype(np.float32)
        rms_before = calculate_rms(samples)
        augmenter = Compose([
            AddShortNoises(
                sounds_path=os.path.join(DEMO_DIR, "short_noises"),
                min_time_between_sounds=2.0,
                max_time_between_sounds=8.0,
                p=1.0,
            )
        ])
        samples_out = augmenter(samples=samples, sample_rate=sample_rate)
        self.assertEqual(samples_out.dtype, np.float32)
        self.assertEqual(samples_out.shape, samples.shape)

        rms_after = calculate_rms(samples_out)
        self.assertGreater(rms_after, rms_before)
示例#6
0
    def test_input_shorter_than_noise(self):
        """
        Verify correct behavior when the input sound is shorter than the added noise sounds.
        """
        sample_rate = 16000
        samples = np.sin(np.linspace(0, 440 * 2 * np.pi,
                                     500)).astype(np.float32)
        rms_before = calculate_rms(samples)
        augmenter = Compose([
            AddShortNoises(
                sounds_path=os.path.join(DEMO_DIR, "short_noises"),
                min_time_between_sounds=0.00001,
                max_time_between_sounds=0.00002,
                p=1.0,
            )
        ])
        samples_out = augmenter(samples=samples, sample_rate=sample_rate)
        self.assertEqual(samples_out.dtype, np.float32)
        self.assertEqual(samples_out.shape, samples.shape)

        rms_after = calculate_rms(samples_out)
        self.assertGreater(rms_after, rms_before)
示例#7
0
    def test_too_long_fade_time(self):
        """Check that a too long fade time does not result in an exception."""
        sample_rate = 16000
        samples = np.sin(np.linspace(0, 440 * 2 * np.pi,
                                     9 * sample_rate)).astype(np.float32)
        rms_before = calculate_rms(samples)
        augmenter = Compose([
            AddShortNoises(
                sounds_path=os.path.join(DEMO_DIR, "short_noises"),
                min_time_between_sounds=2.0,
                max_time_between_sounds=8.0,
                min_fade_in_time=0.9,
                max_fade_in_time=0.99,
                min_fade_out_time=0.9,
                max_fade_out_time=0.99,
                p=1.0,
            )
        ])
        samples_out = augmenter(samples=samples, sample_rate=sample_rate)
        self.assertEqual(samples_out.dtype, np.float32)
        self.assertEqual(samples_out.shape, samples.shape)

        rms_after = calculate_rms(samples_out)
        self.assertGreater(rms_after, rms_before)