soundmentations.CenterTrim

class soundmentations.CenterTrim(duration: float, p: float = 1.0)[source]

Bases: BaseTrim

Trim audio to keep only the center portion of specified duration.

This extracts a segment from the middle of the audio, useful for focusing on the main content while removing silence at the beginning and end.

Parameters:
  • duration (float) – Duration of the center portion to keep in seconds. Must be positive.

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

Examples

Extract center content:

>>> import numpy as np
>>> from soundmentations.transforms.time import CenterTrim
>>>
>>> # Keep 3 seconds from center
>>> trim_transform = CenterTrim(duration=3.0)
>>> trimmed = trim_transform(audio, sample_rate=44100)
>>> print(len(trimmed) / 44100)  # 3.0 seconds

Use for focusing on main content:

>>> import soundmentations as S
>>>
>>> # Extract center and enhance
>>> focus_pipeline = S.Compose([
...     S.CenterTrim(duration=4.0, p=1.0),
...     S.Gain(gain=6.0, p=0.6),
...     S.PadToLength(pad_length=176400, p=1.0)  # 4 seconds
... ])
>>>
>>> focused = focus_pipeline(noisy_audio, sample_rate=44100)