librosa.effects.time_stretch#

librosa.effects.time_stretch(y, *, rate, n_fft=2048, hop_length=None, win_length=None, window='hann', center=True, pad_mode='constant')[source]#

Time-stretch an audio series by a fixed rate.

Parameters:
ynp.ndarray [shape=(…, n)]

audio time series. Multi-channel is supported.

ratefloat > 0 [scalar]

Stretch factor. If rate > 1, then the signal is sped up. If rate < 1, then the signal is slowed down.

n_fftint > 0 [scalar]

Length of the windowed signal after padding with zeros. The number of rows in the STFT matrix is (1 + n_fft/2).

hop_lengthint or None

Number of audio samples between adjacent STFT columns. If unspecified, defaults to win_length // 4.

win_lengthint or None

Each frame of audio is windowed by window of length win_length and then padded with zeros to match n_fft. If unspecified, defaults to win_length = n_fft.

windowstr, tuple, number, function, or np.ndarray [shape=(n_fft,)]

Window specification. See scipy.signal.get_window for supported values.

centerbool

If True, the signal is padded so that frame t is centered at y[t * hop_length].

pad_modestr

Padding mode used when center=True. See numpy.pad for available modes.

Returns:
y_stretchnp.ndarray [shape=(…, round(n/rate))]

audio time series stretched by the specified rate

See also

pitch_shift

Pitch shifting

librosa.phase_vocoder

Spectrogram phase vocoder

pyrubberband.pyrb.time_stretch

High-quality time stretching using RubberBand

Examples

Compress to be twice as fast

>>> y, sr = librosa.loadx('choice')
>>> y_fast = librosa.effects.time_stretch(y, rate=2.0)

Or half the original speed

>>> y_slow = librosa.effects.time_stretch(y, rate=0.5)