def test_datatype():
    test_audio_paths = get_all_test_audio()
    snr_level = 30
    for audio_path in test_audio_paths:
        audio_data, sr = array_from_wave(audio_path)
        augmented_data = synthetic_gaussian_noise_inject(audio_data,
                                                         snr_range=(snr_level,
                                                                    snr_level))
        assert augmented_data.dtype == "int16"
示例#2
0
    def signal_augmentations(self, wave_file: str) -> tuple:
        """
        Performs all of the augmtations to the raw audio signal. The audio data is in pcm16 format.
        Arguments:
            wave_file - str: the path to the audio sample
        Returns:
            audio_data - np.ndarray: augmented np-array
            samp_rate - int: sample rate of the audio recording
        """
        if self.use_log:
            self.logger.info(f"preproc: audio_data read: {wave_file}")

        audio_data, samp_rate = array_from_wave(wave_file)

        # sox-based tempo, gain, pitch augmentations
        if self.tempo_gain_pitch_perturb and self.train_status:
            if np.random.binomial(1, self.tempo_gain_pitch_prob):
                audio_data, samp_rate = tempo_gain_pitch_perturb(
                    wave_file,
                    samp_rate,
                    self.tempo_range,
                    self.gain_range,
                    self.pitch_range,
                    self.augment_from_normal,
                    logger=self.logger)
                if self.use_log:
                    self.logger.info(f"preproc: tempo_gain_pitch applied")

        # synthetic gaussian noise
        if self.synthetic_gaussian_noise and self.train_status:
            if np.random.binomial(1, self.gauss_noise_prob):
                audio_data = synthetic_gaussian_noise_inject(
                    audio_data,
                    self.gauss_snr_db_range,
                    self.augment_from_normal,
                    logger=self.logger)
                if self.use_log:
                    self.logger.info(f"preproc: synth_gauss_noise applied")

        # noise injection
        if self.background_noise and self.train_status:
            if np.random.binomial(1, self.background_noise_prob):
                audio_data = inject_noise(audio_data, samp_rate,
                                          self.noise_dir,
                                          self.background_noise_range,
                                          self.augment_from_normal,
                                          self.logger)
                if self.use_log: self.logger.info(f"preproc: noise injected")

        return audio_data, samp_rate
def test_high_snr_value():
    test_audio_paths = get_all_test_audio()
    snr_level = 100
    # absolute tolerance is 1e-5 of the range of values in pcm16 format (2**16)
    atol = 2**16 * 1e-5
    for audio_path in test_audio_paths:
        audio_data, sr = array_from_wave(audio_path)
        augmented_data = synthetic_gaussian_noise_inject(audio_data,
                                                         snr_range=(snr_level,
                                                                    snr_level))
        np.testing.assert_allclose(audio_data,
                                   augmented_data,
                                   rtol=1e-03,
                                   atol=atol)
def test_regression_equal_pickle():
    """
    The pickle data is output from using the Speak-out.wav file with an snr_level = 30 and a random seed of zero
    """
    pickle_path = "../test_pickle/sythentic-gaussian-noise-inject_Speak-out_snr-30.pickle"
    with open(pickle_path, 'rb') as fid:
        pickle_data = pickle.load(fid)

    audio_path = "../test_audio/Speak-out.wav"
    snr_level = 30
    audio_data, sr = array_from_wave(audio_path)

    np.random.seed(0)
    augmented_data = synthetic_gaussian_noise_inject(audio_data,
                                                     snr_range=(snr_level,
                                                                snr_level))

    assert (augmented_data == pickle_data
            ).sum() == augmented_data.size, "regression test fails"
def test_float_input_failure():
    float_data = np.arange(100, dtype=np.float64)
    snr_level = 30
    with pytest.raises(AssertionError) as execinfo:
        synthetic_gaussian_noise_inject(float_data,
                                        snr_range=(snr_level, snr_level))
def test_zero_input():
    empty_data = np.empty((0, ), dtype="int16")
    snr_level = 30
    synthetic_gaussian_noise_inject(empty_data,
                                    snr_range=(snr_level, snr_level))