soundmentations.RandomGain

class soundmentations.RandomGain(min_gain: float, max_gain: float, clip: bool = True, p: float = 1.0)[source]

Bases: Gain

Apply a random gain to audio samples within a specified range.

This transform randomly selects a gain value from a uniform distribution between min_gain and max_gain, applying it to the audio samples.

Parameters:
  • min_gain (float) – Minimum gain in decibels.

  • max_gain (float) – Maximum gain in decibels.

  • clip (bool, optional) – Whether to clip the output to [-1, 1] range, by default True.

  • p (float, optional) – Probability of applying the random gain transform, by default 1.0.

Examples

>>> import numpy as np
>>> from soundmentations.transforms.amplitude import RandomGain
>>>
>>> # Create audio samples
>>> samples = np.array([0.1, 0.2, -0.1, 0.3])
>>>
>>> # Apply random gain between -6dB and +6dB
>>> random_gain_transform = RandomGain(min_gain=-6.0, max_gain=6.0)
>>> result = random_gain_transform(samples)