librosa.phase_vocoder#

librosa.phase_vocoder(D, *, rate=None, t_out=None, kind='linear')[source]#

Phase vocoder. Given an STFT matrix D, speed up by a factor of rate

Based on the implementation provided by [1].

This is a simplified implementation, intended primarily for reference and pedagogical purposes. It makes no attempt to handle transients, and is likely to produce many audible artifacts. For a higher quality implementation, we recommend the RubberBand library [2] and its Python wrapper pyrubberband.

Parameters:
Dnp.ndarray [shape=(…, n_bins, n_frames), dtype=complex]

STFT matrix

ratefloat > 0 [scalar]

Speed-up factor: rate > 1 is faster, rate < 1 is slower. Use this for constant-rate time-stretching.

t_outnp.ndarray [shape=(n_output,)], optional

Optional array of fractional input frame indices specifying the input time corresponding to each output frame. Values must lie in [0, n_frames). This can be used for variable-rate time-stretching. If given, rate must be None.

kindstr

Interpolation kind for magnitude interpolation, e.g., ‘linear’ or ‘nearest’. See scipy.interpolate.interp1d for a full list of supported options.

Returns:
D_stretchednp.ndarray [shape=(…, n_bins, t / rate), dtype=complex]

time-stretched STFT. For variable-rate stretching, the output will have len(t_out) frames.

See also

pyrubberband

Examples

>>> # Play at double speed
>>> y, sr = librosa.loadx("trumpet")
>>> D = librosa.stft(y, n_fft=2048, hop_length=512)
>>> D_fast = librosa.phase_vocoder(D, rate=2.0, hop_length=512)
>>> y_fast = librosa.istft(D_fast, hop_length=512)
>>> # Or play at 1/3 speed
>>> y, sr = librosa.loadx("trumpet")
>>> D = librosa.stft(y, n_fft=2048, hop_length=512)
>>> D_slow = librosa.phase_vocoder(D, rate=1.0 / 3, hop_length=512)
>>> y_slow = librosa.istft(D_slow, hop_length=512)