Registry / ai-ml / torch-pitch-shift

torch-pitch-shift

JSON →
library1.2.5pypypi✓ verified 25d ago

torch-pitch-shift is a Python library that enables rapid pitch-shifting of audio clips using PyTorch, with full CUDA support. It also provides utilities for calculating efficient pitch-shift targets, which is particularly useful for augmentation scenarios where speed is prioritized over precise pitch-shifts. The library is currently at version 1.2.5 and maintains an active development and release cadence.

pip install torch-pitch-shift
INSTALL
IMPORT
SIG · TORCH-PITCH-SHIFT
T
torch-pitch-shift
ai-mlpythonv1.2.5
Install
65.8s avg
Import
5813ms
Disk
4736MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.2.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 74.8s
py 3.11
✕ build_error
✓ 69.7s
py 3.12
✕ build_error
✓ 61.7s
py 3.13
✕ build_error
✓ 57.1s
py 3.9
✕ build_error
✕ timeout
4736MB installed
● package 4736MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pitch_shift
from torch_pitch_shift import pitch_shift
get_fast_shifts
from torch_pitch_shift import get_fast_shifts
functional.pitch_shift
from torch_pitch_shift import pitch_shift
from torchaudio.functional import pitch_shift
The `torch-pitch-shift` library provides its own `pitch_shift` function, which has different arguments and internal implementation from `torchaudio.functional.pitch_shift`. Ensure you are importing from `torch_pitch_shift` if you intend to use this library's specific functionality.

This quickstart demonstrates how to generate a dummy audio waveform, apply a pitch shift of 3 semitones using the `pitch_shift` function, and print the resulting tensor shape. In a practical application, `waveform` would be loaded from an audio file (e.g., using `torchaudio.load`).

import torch from torch_pitch_shift import pitch_shift # Create a dummy mono audio waveform (batch_size, channels, samples) sample_rate = 44100 duration_seconds = 2 num_samples = sample_rate * duration_seconds waveform = torch.randn(1, 1, num_samples, dtype=torch.float32) # Define the pitch shift amount in semitones shift_semitones = 3.0 # Shift up by 3 semitones # Perform the pitch shift pitch_shifted_waveform = pitch_shift(waveform, shift_semitones, sample_rate) print(f"Original waveform shape: {waveform.shape}") print(f"Pitch-shifted waveform shape: {pitch_shifted_waveform.shape}") # In a real scenario, you would now save or play 'pitch_shifted_waveform'
Debug
Known issues
breakingOlder versions of `torch-pitch-shift` had specific compatibility requirements with `torchaudio` versions. For instance, v1.2.2 and v1.2.1 introduced backwards compatibility and explicit support for `torchaudio<=0.11.0` and `torchaudio v0.11` respectively. Users experiencing issues should check their `torchaudio` version and update `torch-pitch-shift` accordingly.
fix
Upgrade to `torch-pitch-shift>=1.2.2` and ensure `torchaudio` is within a compatible range, or explicitly manage `torchaudio` version based on `torch-pitch-shift` release notes.
affects: <1.2.2
gotchaFor improved audio quality and to reduce distortion during pitch-shifting, ensure you are using `torch-pitch-shift` version 1.2.0 or newer. This version introduced the `hop_length` argument, which significantly impacts output quality.
fix
Upgrade to `torch-pitch-shift>=1.2.0` and consider utilizing the `hop_length` argument for better results.
affects: <1.2.0
gotchaThe `get_fast_shifts` utility function may fail to compute valid pitch-shift ratios for certain sample rates or transpose ranges, raising an error. This typically occurs when no efficient fractional shifts can be found for the given parameters.
fix
Review the `sample_rate` and the `validate_fn` (transpose range) arguments passed to `get_fast_shifts`. Adjust the range or accept that not all desired shifts may be 'fast' for specific audio characteristics.
affects: All
gotchaThe library has been flagged for using dynamic code execution (e.g., `eval()`) by security analysis tools like Socket.dev. While not necessarily a vulnerability, this practice can pose security risks and may prevent the code from running in environments with strict security policies.
fix
Be aware of this characteristic if deploying in highly secure or constrained environments. Review the source code for specific `eval()` usages if concerns arise.
affects: All
gotchaWhile `torch-pitch-shift` is designed for speed with PyTorch/CUDA, performing pitch shifts on CPU can be significantly slower. Related libraries have also noted performance bottlenecks on CPU for similar operations, which might apply here.
fix
Prioritize GPU usage for performance-critical applications. If CPU usage is unavoidable, profile your code and consider batching operations to mitigate slowdowns.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'torch_pitch_shift'
The `torch-pitch-shift` library is not installed in the current Python environment.
fix
Install the library using pip: `pip install torch-pitch-shift`
RuntimeError: Expected object of device type CUDA but got device type CPU for argument #1 'self'
There is a mismatch between the device of the input tensors (audio, shift_val) and the device on which the `torch_pitch_shift` operation is attempting to execute.
fix
Ensure all input tensors are explicitly moved to the same device (e.g., `'cuda'` or `'cpu'`) as the `torch_pitch_shift` operation, using `.to('cuda')` or `.to(device)`.
RuntimeError: Expected a floating point tensor, got a Long tensor for argument #2 'shift_val'
The `shift_val` argument, which specifies the pitch shift, was provided as an integer (Long) tensor instead of a floating-point tensor.
fix
Cast the `shift_val` tensor to a floating-point type, for example, by adding `.float()` or ensuring its initial creation uses float values: `torch.tensor([2.0])`.
RuntimeError: Given groups=1, weight of size [3, 1, 100], expected input[1, 1, 65536] to have 1 channels, but got 65536 channels instead
The input audio tensor has an incorrect shape for the operation. The library expects mono audio as `(batch_size, 1, time_samples)` but received a tensor (e.g., `(batch_size, time_samples)`) where the time dimension was implicitly interpreted as the channel dimension.
fix
Reshape the input audio tensor to explicitly include a channel dimension, typically `(batch_size, 1, time_samples)` for mono audio, using `tensor.unsqueeze(1)` if your `tensor` is `(batch_size, time_samples)`.
Upgrade
Version history
1.2.5latest on PyPI · released Sep 25, 2024
Audit
Dependencies
torchrequiredCore deep learning framework for tensor operations and GPU acceleration.
torchaudiorequiredAudio I/O and transformations, frequently updated with specific version compatibility requirements.
packagingrequiredUsed for version handling and compatibility checks within the library.
primepyrequiredA utility for prime number calculations, likely used in shift ratio computations.
Agent activity
15 hits · last 30 days
node
12
OpenAI (training)
1
Resources
torch-pitch-shift — pip install torch-pitch-shift · libregistry