Skip to main content

AudioMentations Examples in Python.

AudioMentations: How To Examples in Python

AudioMentations is a Python library that provides a wide range of audio data augmentations for machine learning tasks. In this tutorial, we will cover several examples of how to use AudioMentations to augment audio data.

To get started, make sure you have AudioMentations installed. You can install it using pip:

pip install audiomentations

Once you have AudioMentations installed, you can import it into your Python script:

import audiomentations as AA

Example 1: Add Background Noise

You can use AudioMentations to add background noise to your audio data. Here's an example:

augmenter = AA.AddBackgroundNoise(
sounds_path="path/to/background/noise/folder",
min_snr_in_db=10,
max_snr_in_db=20,
p=0.5
)

# Load audio data
audio, sample_rate = load_audio_data("path/to/audio/file.wav")

# Apply augmentation
augmented_audio = augmenter(audio, sample_rate=sample_rate)

# Listen to the augmented audio
play_audio(augmented_audio)

In this example, we create an instance of the AddBackgroundNoise augmentation. We specify the path to a folder containing background noise sounds, the minimum and maximum signal-to-noise ratio (SNR) in decibels, and the probability of applying the augmentation. We then load an audio file, apply the augmentation, and listen to the augmented audio.

Example 2: Add Impulse Response

You can use AudioMentations to add an impulse response to your audio data. Here's an example:

augmenter = AA.AddImpulseResponse(
impulse_response_path="path/to/impulse/response/file.wav",
p=0.5
)

# Load audio data
audio, sample_rate = load_audio_data("path/to/audio/file.wav")

# Apply augmentation
augmented_audio = augmenter(audio, sample_rate=sample_rate)

# Listen to the augmented audio
play_audio(augmented_audio)

In this example, we create an instance of the AddImpulseResponse augmentation. We specify the path to an impulse response file and the probability of applying the augmentation. We then load an audio file, apply the augmentation, and listen to the augmented audio.

Example 3: Change Pitch

You can use AudioMentations to change the pitch of your audio data. Here's an example:

augmenter = AA.PitchShift(
min_semitones=-4,
max_semitones=4,
p=0.5
)

# Load audio data
audio, sample_rate = load_audio_data("path/to/audio/file.wav")

# Apply augmentation
augmented_audio = augmenter(audio, sample_rate=sample_rate)

# Listen to the augmented audio
play_audio(augmented_audio)

In this example, we create an instance of the PitchShift augmentation. We specify the minimum and maximum pitch shift in semitones and the probability of applying the augmentation. We then load an audio file, apply the augmentation, and listen to the augmented audio.

Example 4: Change Speed

You can use AudioMentations to change the speed of your audio data. Here's an example:

augmenter = AA.TimeStretch(
min_rate=0.8,
max_rate=1.2,
p=0.5
)

# Load audio data
audio, sample_rate = load_audio_data("path/to/audio/file.wav")

# Apply augmentation
augmented_audio = augmenter(audio, sample_rate=sample_rate)

# Listen to the augmented audio
play_audio(augmented_audio)

In this example, we create an instance of the TimeStretch augmentation. We specify the minimum and maximum speed change rate and the probability of applying the augmentation. We then load an audio file, apply the augmentation, and listen to the augmented audio.

Example 5: Change Volume

You can use AudioMentations to change the volume of your audio data. Here's an example:

augmenter = AA.Gain(
min_gain_in_db=-10,
max_gain_in_db=10,
p=0.5
)

# Load audio data
audio, sample_rate = load_audio_data("path/to/audio/file.wav")

# Apply augmentation
augmented_audio = augmenter(audio, sample_rate=sample_rate)

# Listen to the augmented audio
play_audio(augmented_audio)

In this example, we create an instance of the Gain augmentation. We specify the minimum and maximum gain in decibels and the probability of applying the augmentation. We then load an audio file, apply the augmentation, and listen to the augmented audio.

Example 6: Apply Distortion

You can use AudioMentations to apply distortion to your audio data. Here's an example:

augmenter = AA.ClippingDistortion(
p=0.5
)

# Load audio data
audio, sample_rate = load_audio_data("path/to/audio/file.wav")

# Apply augmentation
augmented_audio = augmenter(audio, sample_rate=sample_rate)

# Listen to the augmented audio
play_audio(augmented_audio)

In this example, we create an instance of the ClippingDistortion augmentation. We specify the probability of applying the augmentation. We then load an audio file, apply the augmentation, and listen to the augmented audio.

Example 7: Apply Low Pass Filter

You can use AudioMentations to apply a low pass filter to your audio data. Here's an example:

augmenter = AA.LowPassFilter(
min_cutoff_freq=500,
max_cutoff_freq=2000,
p=0.5
)

# Load audio data
audio, sample_rate = load_audio_data("path/to/audio/file.wav")

# Apply augmentation
augmented_audio = augmenter(audio, sample_rate=sample_rate)

# Listen to the augmented audio
play_audio(augmented_audio)

In this example, we create an instance of the LowPassFilter augmentation. We specify the minimum and maximum cutoff frequency in Hertz and the probability of applying the augmentation. We then load an audio file, apply the augmentation, and listen to the augmented audio.

Example 8: Apply High Pass Filter

You can use AudioMentations to apply a high pass filter to your audio data. Here's an example:

augmenter = AA.HighPassFilter(
min_cutoff_freq=500,
max_cutoff_freq=2000,
p=0.5
)

# Load audio data
audio, sample_rate = load_audio_data("path/to/audio/file.wav")

# Apply augmentation
augmented_audio = augmenter(audio, sample_rate=sample_rate)

# Listen to the augmented audio
play_audio(augmented_audio)

In this example, we create an instance of the HighPassFilter augmentation. We specify the minimum and maximum cutoff frequency in Hertz and the probability of applying the augmentation. We then load an audio file, apply the augmentation, and listen to the augmented audio.

Example 9: Apply Band Pass Filter

You can use AudioMentations to apply a band pass filter to your audio data. Here's an example:

augmenter = AA.BandPassFilter(
min_center_freq=1000,
max_center_freq=4000,
min_band_width=100,
max_band_width=1000,
p=0.5
)

# Load audio data
audio, sample_rate = load_audio_data("path/to/audio/file.wav")

# Apply augmentation
augmented_audio = augmenter(audio, sample_rate=sample_rate)

# Listen to the augmented audio
play_audio(augmented_audio)

In this example, we create an instance of the BandPassFilter augmentation. We specify the minimum and maximum center frequency, the minimum and maximum band width, and the probability of applying the augmentation. We then load an audio file, apply the augmentation, and listen to the augmented audio.

Example 10: Apply Time Masking

You can use AudioMentations to apply time masking to your audio data. Here's an example:

augmenter = AA.TimeMasking(
time_mask_factor=0.2,
num_masks=1,
p=0.5
)

# Load audio data
audio, sample_rate = load_audio_data("path/to/audio/file.wav")

# Apply augmentation
augmented_audio = augmenter(audio, sample_rate=sample_rate)

# Listen to the augmented audio
play_audio(augmented_audio)

In this example, we create an instance of the TimeMasking augmentation. We specify the time mask factor, the number of time masks to apply, and the probability of applying the augmentation. We then load an audio file, apply the augmentation, and listen to the augmented audio.

These are just a few examples of the many audio data augmentations that can be performed using AudioMentations. You can explore the documentation and experiment with different augmentations to find the ones that work best for your specific use case.

Happy audio data augmentation!