soundmentations.OneOf

class soundmentations.OneOf(transforms, p=1.0)[source]

Bases: BaseCompose

Apply one transform from a list of transforms, chosen at random.

This class allows you to randomly select and apply one transform from a provided list. Each transform has an equal chance of being selected unless specified otherwise.

Parameters:
  • transforms (list) – List of transform objects to choose from. Each transform must implement __call__(samples, sample_rate).

  • p (float, optional) – Probability of applying one of the transforms, by default 1.0. If not applied, the input audio is returned unchanged.

Examples

Create a random augmentation pipeline:

>>> import soundmentations as S
>>>
>>> # Define individual transforms
>>> pipeline = S.OneOf([
...     S.Gain(gain=6.0),
...     S.FadeIn(duration=0.5),
...     S.FadeOut(duration=0.5)
... ], p=0.9)
>>>
>>> # Apply to audio
>>> augmented = pipeline(audio_samples, sample_rate=44100)

Randomized preprocessing pipeline:

>>> # ML training data preparation with randomness
>>> ml_pipeline = S.OneOf([
...     S.RandomTrim(duration=(1.0, 3.0)),
...     S.Pad(pad_length=44100),
...     S.Gain(gain=3.0)
... ], p=0.8)
>>>
>>> # Process batch of audio files
>>> for audio in audio_batch:
...     processed = ml_pipeline(audio, sample_rate=16000)

Audio enhancement with random effects:

>>> # Clean up audio recordings with variability
>>> enhance_pipeline = S.OneOf([
...     S.StartTrim(start_time=0.5),
...     S.EndTrim(end_time=10.0),
...     S.FadeIn(duration=0.2),
...     S.FadeOut(duration=0.2)
... ], p=0.7)
>>>
>>> enhanced = enhance_pipeline(noisy_audio, sample_rate=44100)

Notes

  • Only one transform is applied per call, chosen at random

  • Probability parameter (p) controls likelihood of applying any transform - If not applied, input audio is returned unchanged