librosa.effects.pitch_shift#

librosa.effects.pitch_shift(y, *, sr, n_steps, bins_per_octave=12, res_type='soxr_hq', scale=False, n_fft=2048, hop_length=None, win_length=None, window='hann', center=True, pad_mode='constant')[source]#

Shift the pitch of a waveform by n_steps steps.

A step is equal to a semitone if bins_per_octave is set to 12.

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

audio time series. Multi-channel is supported.

srnumber > 0 [scalar]

audio sampling rate of y

n_stepsfloat [scalar]

how many (fractional) steps to shift y

bins_per_octaveint > 0 [scalar]

how many steps per octave

res_typestr

Resample type. By default, ‘soxr_hq’ is used.

See librosa.resample for more information.

scalebool

Scale the resampled signal so that y and y_hat have approximately equal total energy.

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_shiftnp.ndarray [shape=(…, n)]

The pitch-shifted audio time-series

See also

time_stretch

Time stretching

librosa.phase_vocoder

Spectrogram phase vocoder

pyrubberband.pyrb.pitch_shift

High-quality pitch shifting using RubberBand

Examples

Shift up by a major third (four steps if bins_per_octave is 12)

>>> y, sr = librosa.loadx('choice')
>>> y_third = librosa.effects.pitch_shift(y, sr=sr, n_steps=4)

Shift down by a tritone (six steps if bins_per_octave is 12)

>>> y_tritone = librosa.effects.pitch_shift(y, sr=sr, n_steps=-6)

Shift up by 3 quarter-tones

>>> y_three_qt = librosa.effects.pitch_shift(y, sr=sr, n_steps=3,
...                                          bins_per_octave=24)