{"id":2812,"library":"torch-pitch-shift","title":"Torch Pitch Shift","description":"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.","status":"active","version":"1.2.5","language":"en","source_language":"en","source_url":"https://github.com/KentoNishi/torch-pitch-shift","tags":["pytorch","audio","pitch-shift","sound-processing","augmentation","gpu","torchaudio"],"install":[{"cmd":"pip install torch-pitch-shift","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core deep learning framework for tensor operations and GPU acceleration.","package":"torch"},{"reason":"Audio I/O and transformations, frequently updated with specific version compatibility requirements.","package":"torchaudio"},{"reason":"Used for version handling and compatibility checks within the library.","package":"packaging"},{"reason":"A utility for prime number calculations, likely used in shift ratio computations.","package":"primepy"}],"imports":[{"symbol":"pitch_shift","correct":"from torch_pitch_shift import pitch_shift"},{"symbol":"get_fast_shifts","correct":"from torch_pitch_shift import get_fast_shifts"},{"note":"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.","wrong":"from torchaudio.functional import pitch_shift","symbol":"functional.pitch_shift","correct":"from torch_pitch_shift import pitch_shift"}],"quickstart":{"code":"import torch\nfrom torch_pitch_shift import pitch_shift\n\n# Create a dummy mono audio waveform (batch_size, channels, samples)\nsample_rate = 44100\nduration_seconds = 2\nnum_samples = sample_rate * duration_seconds\nwaveform = torch.randn(1, 1, num_samples, dtype=torch.float32)\n\n# Define the pitch shift amount in semitones\nshift_semitones = 3.0 # Shift up by 3 semitones\n\n# Perform the pitch shift\npitch_shifted_waveform = pitch_shift(waveform, shift_semitones, sample_rate)\n\nprint(f\"Original waveform shape: {waveform.shape}\")\nprint(f\"Pitch-shifted waveform shape: {pitch_shifted_waveform.shape}\")\n# In a real scenario, you would now save or play 'pitch_shifted_waveform'","lang":"python","description":"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`)."},"warnings":[{"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.","message":"Older 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.","severity":"breaking","affected_versions":"<1.2.2"},{"fix":"Upgrade to `torch-pitch-shift>=1.2.0` and consider utilizing the `hop_length` argument for better results.","message":"For 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.","severity":"gotcha","affected_versions":"<1.2.0"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"All"},{"fix":"Prioritize GPU usage for performance-critical applications. If CPU usage is unavoidable, profile your code and consider batching operations to mitigate slowdowns.","message":"While `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.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}