Skip to content

Repository files navigation

python-stretch: pitch shifting and time stretching

License: MITSupported PlatformsPip Action StatusPip Action StatusPyPI Downloads

A simple Python Wrapper of the Signalsmith Stretch C++ library for pitch and time stretching.

Features

  • Multichannel support: Works with mono and multichannel audio files.
  • Seamless integration: Works natively with NumPy arrays for compatibility with libraries such as librosa and many others used in audio processing pipelines.
  • Backend of Audiomentations: python-stretch is used as the core method for pitch shifting and time stretching in the popular audiomentations library since version 0.39.0.

Installation

python-stretch is available in PyPI (via Platform Wheels):

pip install python-stretch

Alternatevly, you can easly build it from source (You need a c++ compiler and cmake).

# Clone from github
git clone --recurse-submodules https://github.com/gregogiudici/python-stretch.git
# Install
pip install ./python-stretch

Examples

Quick Start

importnumpyasnpimportlibrosaimportpython_stretchasps# Load an audio example from librosa (e.g., 'trumpet', 'brahms',...)audio, sr=librosa.load(librosa.ex('trumpet'), sr=None)
# Assure that "audio" is a 2d arrayif (audio.ndim==1):
audio=audio[np.newaxis, :]
# Create a Stretch objectstretch=ps.Signalsmith.Stretch()
# Configure using a presetstretch.preset(audio.shape[0], sr) # numChannels, sampleRate# Shift up by one octavestretch.setTransposeSemitones(12)
# Stretch timestretch.timeFactor=0.75# Processaudio_processed=stretch.process(audio)
# Save and listenimportsoundfileassfsf.write("audio_original.wav", np.squeeze(audio), sr)
sf.write("audio_processed.wav", np.squeeze(audio_processed), sr)

Acknowledgements

  • Signalsmith Stretch: python-stretch is built on top of the Signalsmith Stretch C++ library, which provides the core algorithms for time stretching and pitch shifting.
  • nanobind: This project utilizes nanobind for easily binding the C++ code to Python.

About

Simple python library for pitch shifting and time stretching. Wrapper of Signalsmith Stretch C++ Library

Topics

Resources

Stars

16 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages