Skip to main content

High quality, one-dimensional sample-rate conversion library

Project description

Python-SoXR

GitHub PyPI conda-forge Packaging status Read the Docs

High quality, one-dimensional sample-rate conversion library for Python.

Keywords: Resampler, Audio resampling, Samplerate conversion, DSP(Digital Signal Processing)

Python-SoXR is a Python wrapper of libsoxr.

Installation

pip install soxr

If installation fails, upgrade pip with python -m pip install --upgrade pip and try again.

in Conda environment

conda install -c conda-forge soxr-python

Note: Conda packge name is soxr-python, not python-soxr.

Basic usage

import soxr

y = soxr.resample(
    x,          # input array – mono(1D) or multi-channel(2D of [frame, channel])
    48000,      # input samplerate
    16000       # target samplerate
)

If input is not numpy.ndarray, it will be converted to numpy.ndarray(dtype='float32').
dtype should be one of float32, float64, int16, int32.

Output is numpy.ndarray with same dimension and data type of input.

Streaming usage

Use ResampleStream for real-time processing or very long signal.

import soxr

rs = soxr.ResampleStream(
    44100,              # input samplerate
    16000,              # target samplerate
    1,                  # channel(s)
    dtype='float32'     # data type (default = 'float32')
)

eof = False
while not eof:
    # Get chunk
    ...

    y_chunk = rs.resample_chunk(
        x,              # input aray – mono(1D) or multi-channel(2D of [frame, channel])
        last=eof        # Set True at end of input
    )

Output frame count may not be consistent. This is normal operation.
(ex. [0, 0, 0, 186, 186, 166, 186, 186, 168, ...])

📝 More code examples

Benchmark

Sweep, impulse, speed compairsion with other resamplers for Python.

https://colab.research.google.com/drive/1_xYUs00VWYOAXShB85W1MFWaUjGHfO4K?usp=sharing

Speed comparison summary

Downsampling 10 sec of 48000 Hz to 44100 Hz.
Ran on Google Colab.

Library Time on CPU (ms)
soxr (HQ) 10.8
torchaudio 13.8
soxr (VHQ) 14.5
scipy.signal.resample 21.3
lilfilter 24.7
julius 31
resampy (kaiser_fast) 108
samplerate (sinc_medium) 223
resampy (kaiser_best) 310
samplerate (sinc_best) 794

Technical detail

For technical details behind resampler, see libsoxr docs.

Python-SoXR uses modified version of libsoxr. See changes here.
These changes does not apply to dynamic linked builds. (e.g. conda-forge build)
To check the version of libsoxr, use soxr.__libsoxr_version__.

Credit and License

Python-SoXR is LGPL v2.1+ licensed, following libsoxr's license.

OSS libraries used

libsoxr (LGPLv2.1+)

The SoX Resampler library
https://sourceforge.net/projects/soxr/

Python-SoXR is a Python wrapper of libsoxr.

PFFFT (BSD-like)

PFFFT: a pretty fast FFT.
https://bitbucket.org/jpommier/pffft/

libsoxr dependency.

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

soxr-0.5.0rc1.tar.gz (170.5 kB view hashes)

Uploaded Source

Built Distributions

soxr-0.5.0rc1-cp312-abi3-win_amd64.whl (163.9 kB view hashes)

Uploaded CPython 3.12+ Windows x86-64

soxr-0.5.0rc1-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (248.4 kB view hashes)

Uploaded CPython 3.12+ manylinux: glibc 2.17+ x86-64

soxr-0.5.0rc1-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (216.5 kB view hashes)

Uploaded CPython 3.12+ manylinux: glibc 2.17+ ARM64

soxr-0.5.0rc1-cp312-abi3-macosx_11_0_arm64.whl (156.3 kB view hashes)

Uploaded CPython 3.12+ macOS 11.0+ ARM64

soxr-0.5.0rc1-cp312-abi3-macosx_10_14_x86_64.whl (199.9 kB view hashes)

Uploaded CPython 3.12+ macOS 10.14+ x86-64

soxr-0.5.0rc1-cp311-cp311-win_amd64.whl (165.6 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

soxr-0.5.0rc1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (252.4 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

soxr-0.5.0rc1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (220.9 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

soxr-0.5.0rc1-cp311-cp311-macosx_11_0_arm64.whl (159.8 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

soxr-0.5.0rc1-cp311-cp311-macosx_10_14_x86_64.whl (203.6 kB view hashes)

Uploaded CPython 3.11 macOS 10.14+ x86-64

soxr-0.5.0rc1-cp310-cp310-win_amd64.whl (165.8 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

soxr-0.5.0rc1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (252.7 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

soxr-0.5.0rc1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (221.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

soxr-0.5.0rc1-cp310-cp310-macosx_11_0_arm64.whl (160.1 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

soxr-0.5.0rc1-cp310-cp310-macosx_10_14_x86_64.whl (203.8 kB view hashes)

Uploaded CPython 3.10 macOS 10.14+ x86-64

soxr-0.5.0rc1-cp39-cp39-win_amd64.whl (166.2 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

soxr-0.5.0rc1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (253.0 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

soxr-0.5.0rc1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (221.4 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

soxr-0.5.0rc1-cp39-cp39-macosx_11_0_arm64.whl (160.3 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

soxr-0.5.0rc1-cp39-cp39-macosx_10_14_x86_64.whl (204.0 kB view hashes)

Uploaded CPython 3.9 macOS 10.14+ 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