Skip to content

pawel-glomski/pylibrb

Repository files navigation

pylibrb

tests PyPI version

pylibrb (py-lib-rubberband) is a simple Python extension exposing Rubber Band Library using nanobind bindings.

Since this is not a wrapper around a command-line tool (like pyrubberband), both offline and real-time modes are available.

Currently this extenstion exposes only a single class: RubberBandStretcher, which implements all the functionalities of the underlying C++ class. The interface is nearly identical to the original library, with a few changes to make it a bit more Pythonic by:

  • using snake_case for functions, variables and properties, and SNAKE_CASE for constants
  • not using (magic) numbers to represent the state
    • use is_done() to see if all the data has been processed and returned from the stretcher, instead of available() == -1
    • use stretcher.formant_scale = pylibrb.AUTO_FORMANT_SCALE instead of stretcher.formant_scale = 0

Throughout the library, audio is accepted and returned in the form of NumPy ndarrays.

Example

from pylibrb import RubberBandStretcher, Option, create_audio_array

# create a stretcher
stretcher = RubberBandStretcher(sample_rate=16000,
                                channels=1,
                                options=Option.PROCESS_REALTIME | Option.ENGINE_FINER,
                                initial_time_ratio=0.5)
stretcher.set_max_process_size(1024)

# provide the audio to the stretcher, until some output is available
audio_in = create_audio_array(channels_num=1, samples_num=1024)
while not stretcher.available():
  audio_in[:] = 0  # get the next batch of samples, here we just use silence
  stretcher.process(audio_in)

# retrieve the available samples
audio_out = stretcher.retrieve_available()

For more instructions, read the docstings of the RubberBandStretcher class and the Option enum, or see the documentation of Rubber Band Library.