Skip to main content

Simple wrapper for librubberband

Project description

Introduction

rubberband is a simple Python3 wrapper around the well-known librubberband sound stretching / pitch-shifting library. Unlike existing Python wrappers (e.g. pyrubberband) this is a true native extension.

The initial release provides a single function that will stretch a mono audio stream my multiplying its duration by a provided factor. Future versions may include pitch shifting, and more complex processing based on data maps.

Installation

The module is available only for MacOS and Linux. The code may compile on Windows, but it has not been tested. Dependencies are:

Assuming these dependencies are met, then installation is simplicity itself:

pip3 install rubberband

The install script does check for the required libraries, and will complain vociferously if they cannot be located. Information on obtaining them is available from the links above.

API

rubberband.stretch (inarray, rate , ratio , crispness = 5 , formants = False )

Arguments

inarray
input audio is assumed to be encoded as single-dimensional NUMPY arrays of type float32. librubberband assumes that input data is normalised to the range [-1,1]; rubberband.stretch automatically applies normalisation if it is required
rate
the frame rate of the input audio stream (so bit rate divided by sample size)
ratio
the ration of output length to input length (in seconds / number of samples)
crispness
integer 0 - 6, default 5: measure of performance - see the rubberband-cli documentation for more details
formants
Boolean, default False : whether or not to preserve formants - see the rubberband-cli documentation for more details
Return value
a one-dimensional NUMPY array of type float32 containing the stretched audio data

Example

import rubberband
from sound import wavfile

nFrames = None
rate = None
bitrate = None
stream = None

with wavfile("infile.wav",rw='r') as wav:
    rate=wav.rate
    bitrate=rate*16
    stream = wav.read()

nFrames=len(stream)
oldDuration=nFrames/rate

newDuration=4
ratio=newDuration/oldDuration
print(f'Ratio is {ratio}')

out=rubberband.stretch(stream,rate=bitrate,ratio=ratio,
                       crispness=5,formants=True)

with wavfile("outfilet.wav",rw='w',rate=rate) as f:
    f.write(out)

Project details


Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for rubberband, version 0.2.0
Filename, size File type Python version Upload date Hashes
Filename, size rubberband-0.2.0-cp37-cp37m-macosx_10_9_x86_64.whl (19.1 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size rubberband-0.2.0.tar.gz (93.2 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page