How to use audiomentations - 10 common examples

To help you get started, we’ve selected a few audiomentations examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github iver56 / audiomentations / audiomentations / augmentations / transforms.py View on Github external
y = lfilter(b, a, data).astype(np.float32)
        return y

    def apply(self, samples, sample_rate):
        band_width = random.randint(
            self.min_frequency_band * sample_rate // 2,
            self.max_frequency_band * sample_rate // 2,
        )
        freq_start = random.randint(16, sample_rate / 2 - band_width)
        samples = self.__butter_bandstop_filter(
            samples, freq_start, freq_start + band_width, sample_rate, order=6
        )
        return samples


class TimeMask(BasicTransform):
    """Mask some time band on the spectrogram. Inspired by https://arxiv.org/pdf/1904.08779.pdf """

    def __init__(self, min_band_part=0.0, max_band_part=0.5, p=0.5):
        """
        :param min_band_part: Minimum length of the silent part as a fraction of the
            total sound length. Float.
        :param max_band_part: Maximum length of the silent part as a fraction of the
            total sound length. Float.
        :param p:
        """
        super().__init__(p)
        self.min_band_part = min_band_part
        self.max_band_part = max_band_part

    def apply(self, samples, sample_rate):
        new_samples = samples.copy()
github iver56 / audiomentations / tests / test_pitch_shift.py View on Github external
def test_dynamic_length(self):
        samples = np.zeros((512,), dtype=np.float32)
        sample_rate = 16000
        augmenter = Compose([PitchShift(min_semitones=-2, max_semitones=-1, p=1.0)])
        samples = augmenter(samples=samples, sample_rate=sample_rate)

        self.assertEqual(samples.dtype, np.float32)
        self.assertEqual(len(samples), 512)
github iver56 / audiomentations / tests / test_time_mask.py View on Github external
def test_dynamic_length(self):
        sample_len = 1024
        samples_in = np.random.normal(0, 1, size=sample_len).astype(np.float32)
        sample_rate = 16000
        augmenter = Compose([TimeMask(min_band_part=0.2, max_band_part=0.5, p=1.0)])

        samples_out = augmenter(samples=samples_in, sample_rate=sample_rate)
        self.assertEqual(samples_out.dtype, np.float32)
        self.assertEqual(len(samples_out), sample_len)

        std_in = np.mean(np.abs(samples_in))
        std_out = np.mean(np.abs(samples_out))
        self.assertLess(std_out, std_in)
github iver56 / audiomentations / tests / test_gaussian_noise.py View on Github external
def test_gaussian_noise(self):
        samples = np.zeros((20,), dtype=np.float32)
        sample_rate = 16000
        augmenter = Compose([AddGaussianNoise(p=1.0)])
        samples = augmenter(samples=samples, sample_rate=sample_rate)

        self.assertEqual(samples.dtype, np.float32)
        self.assertNotAlmostEqual(float(np.sum(np.abs(samples))), 0.0)
github iver56 / audiomentations / tests / test_impulse_response.py View on Github external
def test_dynamic_length(self):
        sample_len = 1024
        samples_in = np.random.normal(0, 1, size=sample_len).astype(np.float32)
        sample_rate = 16000
        augmenter = Compose(
            [AddImpulseResponse(ir_path=os.path.join(DEMO_DIR, "ir"), p=1.0)]
        )

        self.assertEqual(len(samples_in), sample_len)
        samples_out = augmenter(samples=samples_in, sample_rate=sample_rate)

        self.assertEqual(samples_out.dtype, np.float32)
        self.assertGreater(len(samples_out), len(samples_in))
github iver56 / audiomentations / tests / test_normalize.py View on Github external
def test_normalize_negative_peak(self):
        samples = np.array([0.5, 0.6, -0.8, 0.0], dtype=np.float32)
        sample_rate = 16000
        augmenter = Compose([Normalize(p=1.0)])
        samples = augmenter(samples=samples, sample_rate=sample_rate)

        self.assertEqual(np.amin(samples), -1.0)
        self.assertEqual(samples[-1], 0.0)
        self.assertEqual(samples.dtype, np.float32)
        self.assertEqual(len(samples), 4)
github iver56 / audiomentations / tests / test_resample.py View on Github external
def test_resample(self):
        samples = np.zeros((512,), dtype=np.float32)
        sample_rate = 16000
        augmenter = Compose(
            [Resample(min_sample_rate=8000, max_sample_rate=44100, p=1.0)]
        )
        samples = augmenter(samples=samples, sample_rate=sample_rate)

        self.assertEqual(samples.dtype, np.float32)
        self.assertLessEqual(
            len(samples), math.ceil(len(samples) * 44100 / sample_rate)
        )
        self.assertGreaterEqual(
            len(samples), math.ceil(len(samples) * 8000 / sample_rate)
        )
github iver56 / audiomentations / tests / test_clipping_distortion.py View on Github external
def test_distort(self):
        sample_len = 1024
        samples_in = np.random.normal(0, 1, size=sample_len).astype(np.float32)
        sample_rate = 16000
        augmenter = Compose(
            [
                ClippingDistortion(
                    min_percentile_threshold=0, max_percentile_threshold=40, p=1.0
                )
            ]
        )

        samples_out = augmenter(samples=samples_in, sample_rate=sample_rate)
        self.assertEqual(samples_out.dtype, np.float32)
        self.assertEqual(len(samples_out), sample_len)
        self.assertLessEqual(sum(abs(samples_out)), sum(abs(samples_in)))
github iver56 / audiomentations / tests / test_time_mask.py View on Github external
def test_dynamic_length_with_fade_short_signal(self):
        sample_len = 100
        samples_in = np.random.normal(0, 1, size=sample_len).astype(np.float32)
        sample_rate = 16000
        augmenter = Compose(
            [TimeMask(min_band_part=0.2, max_band_part=0.5, fade=True, p=1.0)]
        )

        samples_out = augmenter(samples=samples_in, sample_rate=sample_rate)
        self.assertEqual(samples_out.dtype, np.float32)
        self.assertEqual(len(samples_out), sample_len)

        std_in = np.mean(np.abs(samples_in))
        std_out = np.mean(np.abs(samples_out))
        self.assertLess(std_out, std_in)
github iver56 / audiomentations / tests / test_time_stretch.py View on Github external
def test_dynamic_length(self):
        samples = np.zeros((20,), dtype=np.float32)
        sample_rate = 16000
        augmenter = Compose(
            [
                TimeStretch(
                    min_rate=0.8, max_rate=0.9, leave_length_unchanged=False, p=1.0
                )
            ]
        )
        samples = augmenter(samples=samples, sample_rate=sample_rate)

        self.assertEqual(samples.dtype, np.float32)
        self.assertGreater(len(samples), 20)