Python bindings for sox.
Project description
SoxBindings
Python bindings for SoX. An attempt to bind a subset of the capabilities of the SoX command line utility but in Python via bindings for speed. This avoids costly exec calls when using augmentations in SoX. This is a work in progress! Help welcome.
soxbindings only supports Unix systems (Linux and OSX), due to how one builds sox. A related library (torchaudio) has similar problems: https://github.com/pytorch/audio/issues/425.
Install from pip
If on MacOS or Linux, just do:
pip install soxbindings
If on Windows, it's not supported but you could install sox from source,
and then link libsox and get everything working possibly. If you do and figure
out an automated way to do it using cibuildwheel
, please put in a PR adding
Windows support!
Installation from source
On Unix (Linux, OS X) using Anaconda
- clone this repository
- Make a conda environment
conda install -c conda-forge sox
- If on Linux:
- Option 1:
conda install gcc_linux-64 gxx_linux-64
- Option 2:
sudo apt-get install sox libsox-dev
- Option 3: build and install sox from source (e.g. as in
.github/workflows/build_install_sox_centos.sh
).
- Option 1:
pip install -e .
Run the tests to make sure everything works:
pip install -r extra_requirements.txt
python -m pytest .
The tests run a large variety of commands, all pulled from the pysox test cases. SoxBindings output is then compared with pysox output.
Usage
SoxBindings is built to be a drop-in replacement for the sox command
line tool, avoiding a costly exec call. Specifically, the way it works
is to provide an alternative backend to the excellent library that wraps
the command line tool pysox. SoxBindings
simply re-implements the build
function in pysox Transformer
classes.
Note that Combiner
classes in pysox are NOT supported.
If you have a script that works with pysox, like so:
import sox
# create transformer
tfm = sox.Transformer()
# trim the audio between 5 and 10.5 seconds.
tfm.trim(5, 10.5)
# apply compression
tfm.compand()
# apply a fade in and fade out
tfm.fade(fade_in_len=1.0, fade_out_len=0.5)
# create an output file.
tfm.build_file('path/to/input_audio.wav', 'path/to/output/audio.aiff')
# or equivalently using the legacy API
tfm.build('path/to/input_audio.wav', 'path/to/output/audio.aiff')
# get the output in-memory as a numpy array
# by default the sample rate will be the same as the input file
array_out = tfm.build_array(input_filepath='path/to/input_audio.wav')
# see the applied effects
tfm.effects_log
> ['trim', 'compand', 'fade']
Then, all you have to do is change the import:
import soxbindings as sox
and everything should work, but be faster because of the direct bindings to libsox!
Multithreading
SoxBindings requires some special care when being used in a multi-threaded program
(i.e. a TensorFlow data loader). This issue
has more discussion. To use SoxBindings in a multi-threaded program, you must use
the SoxBindings context manager: soxbindings.sox_context
.
Note below that anything related to SoxBindings is called in the context
block with sox.sox_context():
.
import numpy as np
import soxbindings as sox
y1 = np.zeros((4000, 1))
y2 = np.zeros((3000, 1))
def do_transform(y):
tfm = sox.Transformer()
tfm.vol(0.5)
y_out = tfm.build_array(input_array=y, sample_rate_in=1000)
return y_out
# multithread
pool = ThreadPool(2)
with sox.sox_context():
multi_thread = pool.map(do_transform, [y1, y2])
for a1, a2 in zip(single_thread, multi_thread):
assert np.allclose(a1, a2)
The other option is to wrap your program into a function, and then decorate the function:
import numpy as np
import soxbindings as sox
@sox.sox_context()
def run():
y1 = np.zeros((4000, 1))
y2 = np.zeros((3000, 1))
def do_transform(y):
tfm = sox.Transformer()
tfm.vol(0.5)
y_out = tfm.build_array(input_array=y, sample_rate_in=1000)
return y_out
# multithread
pool = ThreadPool(2)
multi_thread = pool.map(do_transform, [y1, y2])
for a1, a2 in zip(single_thread, multi_thread):
assert np.allclose(a1, a2)
If your program is single-threaded, no changes are needed. SoxBindings checks to see if SoX has been initialized already before initializing again.
Deploying to PyPI
The Github action workflow "Build wheels" gets run every time there is a commit to master. When it's done, the wheels for OSX and Linux are created and place in an artifact. For example:
https://github.com/pseeth/soxbindings/actions/runs/169544837
Download the artifact zip, then do the following steps from the root of the soxbindings repo:
unzip [/path/to/artifact.zip]
# clear out dist
rm -rf dist/
# create source distribution
python setup.py sdist
cp -r [/path/to/artifact]/* dist/
The dist folder should look something like:
dist
├── soxbindings-0.0.1-cp35-cp35m-macosx_10_9_x86_64.whl
├── soxbindings-0.0.1-cp35-cp35m-manylinux2010_i686.whl
├── soxbindings-0.0.1-cp35-cp35m-manylinux2010_x86_64.whl
├── soxbindings-0.0.1-cp36-cp36m-macosx_10_9_x86_64.whl
├── soxbindings-0.0.1-cp36-cp36m-manylinux2010_i686.whl
├── soxbindings-0.0.1-cp36-cp36m-manylinux2010_x86_64.whl
├── soxbindings-0.0.1-cp37-cp37m-macosx_10_9_x86_64.whl
├── soxbindings-0.0.1-cp37-cp37m-manylinux2010_i686.whl
├── soxbindings-0.0.1-cp37-cp37m-manylinux2010_x86_64.whl
├── soxbindings-0.0.1-cp38-cp38-macosx_10_9_x86_64.whl
├── soxbindings-0.0.1-cp38-cp38-manylinux2010_i686.whl
├── soxbindings-0.0.1-cp38-cp38-manylinux2010_x86_64.whl
├── soxbindings-0.0.1-pp27-pypy_73-macosx_10_9_x86_64.whl
├── soxbindings-0.0.1-pp27-pypy_73-manylinux2010_x86_64.whl
├── soxbindings-0.0.1-pp36-pypy36_pp73-macosx_10_9_x86_64.whl
├── soxbindings-0.0.1-pp36-pypy36_pp73-manylinux2010_x86_64.whl
└── soxbindings-0.0.1.tar.gz
Upload it to the test server first (requires a version bump):
twine upload --repository testpypi dist/*
Make sure you can pip install it on both Linux and OSX:
pip install -U --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple -U soxbindings
Use the demo script included in this repo to try it out. Finally, upload it to the regular PyPi server:
twine upload dist/*
License
soxbindings is under an MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for soxbindings-1.2.3-pp37-pypy37_pp73-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b80d70baa4ec76d36fe1a311b15a7273eec4524b99c1987ea37ff41cf679ead2 |
|
MD5 | 9f4e348411d2a0aef04efe73038f0bc7 |
|
BLAKE2b-256 | e1ea3fe7a4c336e38f6afe13f27464c84d8632d944c2aa7759ee1a2131942695 |
Hashes for soxbindings-1.2.3-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5413cc79942ff601992bccb7330046191f09a135a79d522f9f3f8cfa0e7530d0 |
|
MD5 | 67665db1716241a8d5cbe65ad5ba5fc7 |
|
BLAKE2b-256 | c4d6c3532974800e1a4ddcbd825debffe4e32d155a1a137b00f98503bd40b7e0 |
Hashes for soxbindings-1.2.3-pp36-pypy36_pp73-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d4423c57ae196638ffa85bd4a2a10b2a09b1bfdff5de37e3ea443aa44b746dd |
|
MD5 | 26f61236a50b7e98fb9dd271ecbc2c19 |
|
BLAKE2b-256 | 6111f3201a856eb1fd1926907dbe6b5945e4a7ff029645c6445dba88f67d6227 |
Hashes for soxbindings-1.2.3-pp36-pypy36_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9b682f121d12f8bd363531e73da06ac0d8269e854b116dc4f3ec8cf866692a5 |
|
MD5 | 029f39d9e17a28bb052d059329e1adf5 |
|
BLAKE2b-256 | 81e97d370425156e4c7b78e3012c952811431a92d053b53d125423987f352a00 |
Hashes for soxbindings-1.2.3-pp27-pypy_73-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eeac77dd77ecbfd7085545a9187e0b7d483f07e33744ad7aa1e684a1866b3cbb |
|
MD5 | 77eb1850756c3779681f70e550b820f6 |
|
BLAKE2b-256 | 77e5d3258e39699ebcd287a17683dde9f9f09d6bd4db32c7a8795d5af18bbd34 |
Hashes for soxbindings-1.2.3-pp27-pypy_73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a43077c29e74ab3cc953f976af4e0b165489d9a179d32d0c1b831cef8674b7b |
|
MD5 | 0e2025fd39252a373542a02f54e02240 |
|
BLAKE2b-256 | 7ef58f62f5f34ced14f33b0ec42a944b32c12d574c4aa8b3f146fc85a18e8788 |
Hashes for soxbindings-1.2.3-cp39-cp39-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20212a6a9125a6b2b40341b0881daf620269ce94625d1c8c003979684813986f |
|
MD5 | d78c0e727011baa03a2a3efb959b9ca3 |
|
BLAKE2b-256 | 76812c285b87ce995ffc1cf89a9051e71656ea70163e90c7b2b3031c90739db6 |
Hashes for soxbindings-1.2.3-cp39-cp39-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0926e7a045c4577e7b318bcbef001d2ed3bf18fa2f9cc891dd36b2e89e6e0106 |
|
MD5 | 353dff00edd54ec3de2c5f8cb7c23b40 |
|
BLAKE2b-256 | 9cacdca0398c075eec878e55ca2d8d93084556e26121910144c4b77871402da1 |
Hashes for soxbindings-1.2.3-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 529ecc8872a250b3398d0598c45b6df99baf1a2dbb31914414cd2d3790f53a13 |
|
MD5 | 44bb6b94e39f3ddf4368eb28b6240111 |
|
BLAKE2b-256 | 18209e68a87db01e3b3050dd495b334ff54d38da669bb928331a979c7b6d4bc0 |
Hashes for soxbindings-1.2.3-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2cd77e8051943d856b923d811a2ceb67c692a3cc17711a479fae8b2aee0f7ec2 |
|
MD5 | e49f08fe9e4475dd203d9e69852106f4 |
|
BLAKE2b-256 | c41e776068401df57dcb6745285e9160dbc0557f3e9ade7c5dabc7ed1b450dec |
Hashes for soxbindings-1.2.3-cp38-cp38-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f10926e4572d8e259fa664a4929b8d4fcbdff6fd76379662917a1ee937e756de |
|
MD5 | df22fd3192d534e176b02f2594263a82 |
|
BLAKE2b-256 | 9bc0016b5264865d3030378206b915a2d26754bf2c3f427e005074317694e76a |
Hashes for soxbindings-1.2.3-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4cf4ec726fac912cc6aa74b6c276b0ee76f80044113a5599bb068329d7e6bba |
|
MD5 | 7457b6b4348932c338d96a0ab21f1c2c |
|
BLAKE2b-256 | 540d420088262e1155899056804346b21b34c235a797f2c7862d7ee2db8cbf93 |
Hashes for soxbindings-1.2.3-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7aa84f29d42e28de575618ef40bb54d0ac79d09c59c563baa2fdbd9c59d0687d |
|
MD5 | 813bf0678cb32399348420859ed41944 |
|
BLAKE2b-256 | 0857571daf4671e5e3e4f0d6a6ad8a45e66f92f0a3bfe8d0417d24eefa6b136f |
Hashes for soxbindings-1.2.3-cp37-cp37m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45de0dc019a9621c17299f2589e2da51604f90253cf12068268d6d4a3f21e70b |
|
MD5 | accee8c70341b55b1eeac4b5568b505a |
|
BLAKE2b-256 | 629141101c78e29dfe95c870f57c01cbc29aec0295b3e007376ab53328aaa907 |
Hashes for soxbindings-1.2.3-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1704f035ca918c5596e9a9a6d501aab67dea461f7f7ce945462801fa8103b426 |
|
MD5 | fcd8106ed3b0e256cb0d63063531b6e7 |
|
BLAKE2b-256 | 8a5320eb4978cff2ef50b9664b942af725a5e2eac0a76044ee7b40f0a276e1db |
Hashes for soxbindings-1.2.3-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b878bb7157207c8a81f422ce13fc8cc2f5a93513e36476aed78ea0ef7eab878 |
|
MD5 | 6577c5c1bf531455c5d492163ad0443b |
|
BLAKE2b-256 | 036730b1427d9570f27803ab2aaeb6bc5b01b5def7946234b3b9a64f7911f1cf |
Hashes for soxbindings-1.2.3-cp36-cp36m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1cfbedf35ddb114d41dd070f07b75e8530f79b752ebb5848388ee6e85ffceed9 |
|
MD5 | ec8685bd9167558aea6bb8c8bba42f1a |
|
BLAKE2b-256 | 596453a9e026015b0ae20fea1f216cb6d69d898f599f4bfdb64eb545fe10cdff |
Hashes for soxbindings-1.2.3-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a67a9c7fb68266d603a980a3a5359c1039f8ea21feed376d6e6add7f5a69123a |
|
MD5 | 26da7113bbe105b80233d99ad8440789 |
|
BLAKE2b-256 | bb748f06a43f2bc45bdca0f06cc28b83df80a3dc58fab71cabcdabbc0bffe577 |
Hashes for soxbindings-1.2.3-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ec4b74a22a79616c06d60d374f70d609ed213ff2b2907771f2ae28cf11b2bb1 |
|
MD5 | 8db1cb2b7bcad47164bb0bce21be3308 |
|
BLAKE2b-256 | b66e01f0d85ad4498a72d5ba7784793ae29c29e9958148c11d6c95f8ff6720c2 |
Hashes for soxbindings-1.2.3-cp35-cp35m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0efeaec8b562a80e4229a897c5c98a4f7b1ef12b1cd77f7bab65b2e440ba301 |
|
MD5 | 36f0ee7104ea17e400597aa7ebadd2b0 |
|
BLAKE2b-256 | 3015bba8b7f55f603fcb968423af9684c53acbbe0922d0ecabccd31c8e398e24 |
Hashes for soxbindings-1.2.3-cp35-cp35m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6310b2836b52f58abdff0a317c4a55b3ec48e9a0f716210cbb5624af6b59a53e |
|
MD5 | fefc2152ef9fa9d6919bc85c4ce6bf97 |
|
BLAKE2b-256 | bdbf5aee4b30b02342b51384b55f15fc1fc38d48d0f9d394874c9625a80226cc |