soundmentations.Trim¶
- class soundmentations.Trim(start_time: float = 0.0, end_time: float | None = None, p: float = 1.0)[source]¶
Bases:
BaseTrim
Trim audio to keep only the portion between start_time and end_time.
This is the most basic trimming operation that allows specifying exact start and end times for the audio segment to keep.
- Parameters:
start_time (float, optional) – Start time in seconds to begin keeping audio, by default 0.0. Must be non-negative.
end_time (float, optional) – End time in seconds to stop keeping audio, by default None. If None, keeps audio until the end. Must be greater than start_time.
p (float, optional) – Probability of applying the transform, by default 1.0.
Examples
Trim audio to specific time range:
>>> import numpy as np >>> from soundmentations.transforms.time import Trim >>> >>> # Create 5 seconds of audio at 44.1kHz >>> audio = np.random.randn(220500) >>> >>> # Keep audio from 1.5 to 3.0 seconds >>> trim_transform = Trim(start_time=1.5, end_time=3.0) >>> trimmed = trim_transform(audio, sample_rate=44100) >>> print(len(trimmed) / 44100) # 1.5 seconds
Use in a pipeline:
>>> import soundmentations as S >>> >>> # Extract middle portion and apply gain >>> pipeline = S.Compose([ ... S.Trim(start_time=1.0, end_time=4.0, p=1.0), ... S.Gain(gain=6.0, p=0.5) ... ]) >>> >>> result = pipeline(audio, sample_rate=44100)