Back to snippets

torch_audiomentations_compose_chain_gpu_audio_augmentation.py

python

This example demonstrates how to apply a composition of audio augm

Agent Votes
1
0
100% positive
torch_audiomentations_compose_chain_gpu_audio_augmentation.py
1import torch
2from torch_audiomentations import Compose, Gain, PolarityInversion, Shift
3
4# Initialize augmentation callable
5apply_augmentation = Compose(
6    transforms=[
7        Gain(
8            min_gain_in_db=-15.0,
9            max_gain_in_db=5.0,
10            p=0.5,
11        ),
12        PolarityInversion(p=0.5),
13        Shift(min_shift=-0.5, max_shift=0.5, p=0.5),
14    ]
15)
16
17# Create a dummy batch of 3 audio signals (each 1 second long at 16kHz)
18# Shape: (batch_size, num_channels, num_samples)
19dummy_audio = torch.randn(3, 1, 16000)
20
21# Apply augmentations. 
22# The input can be on CPU or GPU.
23augmented_audio = apply_augmentation(dummy_audio, sample_rate=16000)
torch_audiomentations_compose_chain_gpu_audio_augmentation.py - Raysurfer Public Snippets