Package: rubberband
Introductionrubberbandis a simple Python3 wrapper around the well-knownlibrubberbandsound 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.InstallationThe module is available only for MacOS and Linux.  The code may compile on Windows, but it has not been tested. Dependencies are:Python 3 (preferably 3.6 or greater)librubberband(> 1.8)libsndfile(> 1.0)Assuming these dependencies are met, then installation is simplicity itself:pip3 install rubberbandThe 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.APIAudio stream formattingThe module exposes a single functionrubberband.stretchwhich applies thelibrubberbandalgorithm 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:FormatDescriptionConstantPCM_U8unsigned 8-bitrubberband.uint8PCM_S8signed 8-bitrubberband.int8PCM_16signed 16-bitrubberband.int16PCM_32signed 32-bitrubberband.int32FLOATnormalised32-bit floatrubberband.float32Note that floating point data is assumed to be normalised, so all samples lie in the range [-1,1).Audio data can be passed torubberband.stretchin any of three ways:Typed arrayA 1-dimensionalNUMPYtypedarrayobject, whosedtypeis one ofnumpy.uint8,numpy.int8,numpy.int16,numpy.int32ornumpy.float32.  The type of the audio
data is deduced from this, using the strangely convenient fact that ifTis one ofuint8,int8,int16,int32,float32thennumpy.dtype(numpy.T).num==rubberband.TListA simple Pythonlist, all of whose elements are of a type implicitly convertible tofloat.
In this case, the audio format cannot be deduced, so it must be specified using theformatargument
torubberband.stretch(see below).Raw bytestreamA Pythonbytesobject, 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 theformatargument
torubberband.stretch(see below).In all cases, the output fromrubberband.stretchhas the same PCM format, and is stored in the same
kind of object, as the input.  So, for example, given abytesobject representing aPCM_16audio channel,rubberband.stretchreturnsbytesobject representing aPCM_16stretched audio channel.Method signaturerubberband.stretch(input,format=rubberband.float32,rate=48000,ratio=1,crispness=5,formants=False,precise=True)ArgumentsinputThe 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.formatThe PCM format of the data, specified using one of the constants set out above.  This
value isignoredifinputis a NUMPY typed array, in which case the format is deduced
from itsdtype.rateThe frame rate of the input audio stream (so bit rate divided by sample size).ratioThe ratio of output length to input length (in seconds / number of samples).crispnessInteger 0 - 6, default 5: measure of performance - see therubberband-cli documentationfor more details.formantsBoolean, defaultFalse: whether or not to preserve formants - see therubberband-cli documentationfor more details.preciseBoolean, defaultTrue: whether or not to use the precise stretching algorithm -
see therubberband-cli documentationfor more details.Return valueAn object containing the stretched audio data, represented using the same PCM encoding as theinput. Samples are normalised to lie in the expected range for the format.Exampleimportrubberbandimportsoundfiledata,rate=soundfile.read('infile.wav',dtype='int16')bitrate=rate*16nFrames=len(data)print(f'Raw input type is :{type(data)}')oldDuration=nFrames/ratenewDuration=6ratio=newDuration/oldDurationout=rubberband.stretch(data,rate=rate,ratio=ratio,crispness=5,formants=False,precise=True)soundfile.write('outfile.wav',out,rate,'PCM_16')