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


Download files

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

Source Distribution

rubberband-0.2.0.tar.gz (93.2 kB view hashes)

Uploaded Source

Built Distribution

rubberband-0.2.0-cp37-cp37m-macosx_10_9_x86_64.whl (19.1 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page