Usage

Installation

You can install torch-pitch-shift from PyPI.

pip install torch-pitch-shift

To upgrade an existing installation of torch-pitch-shift, use the following command:

pip install --upgrade --no-cache-dir torch-pitch-shift

Importing

First, import torch-pitch-shift.

# import all functions
from torch_pitch_shift import *

# ... or import them manually
from torch_pitch_shift import get_fast_shifts, pitch_shift

What's included

torch-pitch-shift includes the following:

Type Name Description
Function pitch_shift Shift the pitch of a batch of waveforms by a given amount.
Function get_fast_shifts Utility function for calculating pitch-shifts that can be executed quickly.
Function semitones_to_ratio Utility function to convert semitonal shifts into ratios.
Function ratio_to_semitones Utility function to convert rational shifts to semitones.

Methods

pitch_shift

Shift the pitch of a batch of waveforms by a given amount.

Arguments

Argument Required Default Value Type Description
input Yes
torch.Tensor [
    shape=(
        batch_size,
        channels,
        samples
    )
]
Input audio clips of shape (batch_size, channels, samples)
shift Yes float or Fraction Inputs of type float indicate the amount to pitch-shift in # of bins (where 1 bin == 1 semitone if bins_per_octave == 12). Inputs of type Fraction indicate a pitch-shift ratio (usually an element in get_fast_shifts()).
sample_rate Yes int The sample rate of the input audio clips.
n_fft No sample_rate // 64 int Size of FFT. Default sample_rate // 64. Smaller is faster.
bins_per_octave No 12 int Number of bins per octave. Default is 12.

Return value

Type Description
torch.Tensor [
    shape=(
        batch_size,
        channels,
        samples
    )
]
The pitch-shifted batch of audio clips

get_fast_shifts

Search for pitch-shift targets that can be computed quickly for a given sample rate.

Argument Required Default Value Type Description
sample_rate Yes int The sample rate of an audio clip.
condition No
lambda x: (
    x >= 0.5 and x <= 2 and x != 1
)
Callable A function to validate fast shift ratios. Default value limits computed targets to values between -1 and +1 octaves.

Return value

Type Description
List[Fraction] A list of fast pitch-shift target ratios that satisfy the given conditions.

semitones_to_ratio

Convert semitonal shifts into ratios.

Argument Required Default Value Type Description
semitones Yes float The number of semitones for a desired shift.

Return value

Type Description
Fraction A Fraction indicating a pitch shift ratio

ratio_to_semitones

Convert rational shifts to semitones.

Argument Required Default Value Type Description
ratio Yes Fraction The ratio for a desired shift.

Return value

Type Description
float The magnitude of a pitch shift in semitones

Example

See example.py to see an example of torch-pitch-shift in action!