Skip to main content

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 by 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

Audio stream formatting

The module exposes a single function rubberband.stretch which applies the librubberband algorithm to a mono audio stream, encoded in one of the following formats, each of which has a corresponding constant, as set out in the third column:

Format

Description

Constant

PCM_U8

unsigned 8-bit

rubberband.uint8

PCM_S8

signed 8-bit

rubberband.int8

PCM_16

signed 16-bit

rubberband.int16

PCM_32

signed 32-bit

rubberband.int32

FLOAT

normalised 32-bit float

rubberband.float32

Note that floating point data is assumed to be normalised, so all samples lie in the range [-1,1).

Audio data can be passed to rubberband.stretch in any of three ways:

Typed array

A 1-dimensional NUMPY typed array object, whose dtype is one of numpy.uint8, numpy.int8, numpy.int16, numpy.int32 or numpy.float32. The type of the audio data is deduced from this, using the strangely convenient fact that if T is one of uint8, int8, int16, int32, float32 then

numpy.dtype(numpy.T).num == rubberband.T
List

A simple Python list, all of whose elements are of a type implicitly convertible to float. In this case, the audio format cannot be deduced, so it must be specified using the format argument to rubberband.stretch (see below).

Raw bytestream

A Python bytes object, whose content is the raw PCM byte stream (note: audio file metadata, e.g. WAV file headers, must be stripped, so only PCM data remains). Again, in this case, the audio format cannot be deduced, so it must be specified using the format argument to rubberband.stretch (see below).

In all cases, the output from rubberband.stretch has the same PCM format, and is stored in the same kind of object, as the input. So, for example, given a bytes object representing a PCM_16 audio channel, rubberband.stretch returns bytes object representing a PCM_16 stretched audio channel.

Method signature

rubberband.stretch (input, format = rubberband.float32, rate = 48000 , ratio = 1 , crispness = 5 , formants = False, precise = True )

Arguments

input

The input is assumed to represent a single channel of PCM audio data, encoded with one of the schemes listed above. It can be any of the types set out above.

format

The PCM format of the data, specified using one of the constants set out above. This value is ignored if input is a NUMPY typed array, in which case the format is deduced from its dtype.

rate

The frame rate of the input audio stream (so bit rate divided by sample size).

ratio

The ratio 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.

precise

Boolean, default True : whether or not to use the precise stretching algorithm - see the rubberband-cli documentation for more details.

Return value

An object containing the stretched audio data, represented using the same PCM encoding as the input. Samples are normalised to lie in the expected range for the format.

Example

import rubberband
import soundfile

data,rate=soundfile.read('infile.wav',dtype='int16')
bitrate=rate*16
nFrames=len(data)
print(f'Raw input type is : {type(data)}')

oldDuration=nFrames/rate
newDuration=6
ratio=newDuration/oldDuration

out=rubberband.stretch(data,rate=rate,ratio=ratio,crispness=5,formants=False,precise=True)
soundfile.write('outfile.wav',out,rate,'PCM_16')

Release files for rubberband 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rubberband 1.0.2
File Size Uploaded
rubberband-1.0.2.tar.gz 99.4 kB Details

Release files / rubberband-1.0.2.tar.gz

Download URL rubberband-1.0.2.tar.gz
Size 99.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c569c1017be16f712a26cce45914ee6da3832f062fedd2aa08e2377580a93e12
BLAKE2b-256 checksum
How to use checksums
2840955257a03c46146fe2fc523323bbf9f0a7dbc84812dc4f0161c622b0a951
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

1.0.2 This release

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page