Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Accessing Sox Audio Effects from Python via Pysox
import pysox
import librosa
import numpy as np
def apply_sox_effect(signal, sr, fxstr):
# This writes signal to a .wav file, processes it sox to another file, loads that and returns it.
#
# signal: a numpy list of numbers; the audio signal
# sr: the sample rate in Hz, must be an integer
# fxstr: a semicolon-separated string starting with the effect name followed by parameter values in order
# e.g., "lowpass;500" or "vol;18dB" or 'compand;0.3,0.8;-50', or if you're feeling ambitious,..
# fxstr='ladspa;/usr/lib/ladspa/mbeq_1197.so;mbeq;-2;-3;-3;-6;-9;-9;-10;-8;-6;-5;-4;-3;-1;0;0;0'
# See the sox docs for more on effects: http://sox.sourceforge.net/sox.html#EFFECTS
# (Why semicolon-separated? Because sox looks for both commas and colons!)
inpath, outpath = 'in.wav', 'out.wav'
librosa.output.write_wav(inpath, signal, sr) # write the input audio to a file
tmp = fxstr.split(';')
fxname = tmp[0]
fxvals = [str.encode(x) for x in tmp[1:]]
effectparams = [(fxname, fxvals),]
app = pysox.CSoxApp(inpath, outpath, effectparams=effectparams) # apply the sox effect & get new file
app.flow()
out_signal, sr = librosa.load(outpath, sr)
return out_signal
sr = 44100
noise = np.random.rand(4*sr) - 0.5 # 4 seconds of noise
new_noise = apply_sox_effect(noise, sr, 'lowpass;500')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment