soundmentations.Gain

class soundmentations.Gain(gain: float = 1.0, clip: bool = True, p: float = 1.0)[source]

Bases: BaseGain

Apply a fixed gain (in dB) to audio samples.

This transform multiplies the audio samples by a gain factor derived from the specified gain in decibels. Optionally clips the output to prevent values from exceeding the [-1, 1] range.

Parameters:
  • gain (float, optional) – Gain in decibels, by default 1.0. Positive values increase volume, negative values decrease volume.

  • clip (bool, optional) – Whether to clip the output to [-1, 1] range, by default True. Prevents audio distortion from excessive gain.

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

Examples

Apply a fixed gain to audio samples:

>>> import numpy as np
>>> from soundmentations.transforms.amplitude import Gain
>>>
>>> # Create audio samples
>>> samples = np.array([0.1, 0.2, -0.1, 0.3])
>>>
>>> # Apply +6dB gain
>>> gain_transform = Gain(gain=6.0)
>>> amplified = gain_transform(samples)
>>>
>>> # Apply -12dB gain with 50% probability
>>> quiet_transform = Gain(gain=-12.0, p=0.5)
>>> result = quiet_transform(samples)

Use in a pipeline with other transforms:

>>> import soundmentations as S
>>>
>>> # Create augmentation pipeline
>>> augment = S.Compose([
...     S.RandomTrim(duration=(1.0, 3.0), p=0.8),
...     S.Gain(gain=6.0, clip=True, p=0.7),
...     S.PadToLength(pad_length=44100, p=0.5)
... ])
>>>
>>> # Apply pipeline to audio
>>> audio_samples = np.random.randn(22050)  # 0.5 seconds at 44.1kHz
>>> augmented = augment(samples=audio_samples, sample_rate=44100)

Different gain scenarios:

>>> # Boost quiet audio
>>> boost = Gain(gain=12.0, clip=True)
>>>
>>> # Attenuate loud audio
>>> attenuate = Gain(gain=-6.0, clip=False)
>>>
>>> # Random volume variation
>>> random_volume = Gain(gain=np.random.uniform(-10, 10), p=0.6)