This are the python bindings for the library libsox, which is an audio manipulation library used by sox.
The most important classes are CSoxStream, CEffectsChain and CEffect. Using these classes all internal effects provided by libsox can be applied to audio files.
Using CNullFile, the effects requiring dummy input (such as synth) can also be used.
Please check out the examples provided with the source, they will set you up fast and easy.
import pysox
def mktestfile():
#open the nullfile to provide signal parameters: 48000kHz, 32bit on 2 channels
#the nullfile produces an infinite amount of silence. So only to be used with
# trim effect or synth, which has a length parameter
nullfile = pysox.CNullFile()
signal = nullfile.get_signal()
#open an output file using the nullfiles signal parameters
out = pysox.CSoxStream('test.wav', 'w', signal)
#create the effect chain with input and output streams
chain = pysox.CEffectsChain(nullfile, out)
#add the synth effect to the chain, we use 3 seconds of sine sweep
effect = pysox.CEffect("synth",[b'3', b'sine', b'300-3000'])
chain.add_effect(effect)
#process the effects chain, this applies all effecte on the input, producing the output
chain.flow_effects()
#cleanup
out.close()
#libsox internal cleanup takes place if we delete our chain or the script exits. So we are lazy here.
mktestfile()
Representation of struct sox_encodinginfo_t
Constructor to create an encodinginfo
The optional positional arguments encoding, bits_per_sample can be given, in order to generate a static encodinginfo suitable to provide configuration data for streams.
Return a dictionary describing the encoding of the stream.
Alter the given parameters of this encodinginfo.
ecode is the numeric encoding, see sox.h, enum sox_encoding_t for encodings. CEncodingInfo defines symbols for encoding as class attributes:
CSoxEncoding.UNKNOWN = 0 #,
CSoxEncoding.SIGN2 = 1 #, /* signed linear 2's comp: Mac */
CSoxEncoding.UNSIGNED = 2 #, /* unsigned linear: Sound Blaster */
CSoxEncoding.FLOAT = 3 #, /* floating point (binary format) */
CSoxEncoding.FLOAT_TEXT = 4 #, /* floating point (text format) */
CSoxEncoding.FLAC = 5 #, /* FLAC compression */
CSoxEncoding.HCOM = 6 #, /* */
CSoxEncoding.WAVPACK = 7 #, /* */
CSoxEncoding.WAVPACKF = 8 #, /* */
CSoxEncoding.ULAW = 9 #, /* u-law signed logs: US telephony, SPARC */
CSoxEncoding.ALAW = 10 #, /* A-law signed logs: non-US telephony, Psion */
CSoxEncoding.G721 = 11 #, /* G.721 4-bit ADPCM */
CSoxEncoding.G723 = 12 #, /* G.723 3 or 5 bit ADPCM */
CSoxEncoding.CL_ADPCM = 13 #, /* Creative Labs 8 --> 2,3,4 bit Compressed PCM */
CSoxEncoding.CL_ADPCM16 = 14#, /* Creative Labs 16 --> 4 bit Compressed PCM */
CSoxEncoding.MS_ADPCM = 15 #, /* Microsoft Compressed PCM */
CSoxEncoding.IMA_ADPCM = 16#, /* IMA Compressed PCM */
CSoxEncoding.OKI_ADPCM = 17#, /* Dialogic/OKI Compressed PCM */
CSoxEncoding.DPCM = 18 #, /* */
CSoxEncoding.DWVW = 19 #, /* */
CSoxEncoding.DWVWN = 20 #, /* */
CSoxEncoding.GSM = 21 #, /* GSM 6.10 33byte frame lossy compression */
CSoxEncoding.MP3 = 22 #, /* MP3 compression */
CSoxEncoding.VORBIS = 23 #, /* Vorbis compression */
CSoxEncoding.AMR_WB = 24 #, /* AMR-WB compression */
CSoxEncoding.AMR_NB = 25 #, /* AMR-NB compression */
CSoxEncoding.CVSD = 26 #, /* */
CSoxEncoding.LPC10 = 27 #, /* */
bps is the sample rate in bits per sample
Representation of struct sox_format_t
This class also has the capability of opening audio files for reading and writing and to retrieve their stream parameters such as encodinginfo and signalinfo.
Create a sox stream (sox_format_t)
Constructor
param String path: | |
---|---|
The filename to read or write | |
param String mode: | |
optional file mode ‘r’ or ‘w’, defaults to ‘r’ | |
param CSignalInfo signalInfo: | |
required only for write mode to define the signal characteristics using the CSignalInfo class. | |
param CEncodingInfo encodingInfo: | |
optional output encoding for write mode using the CEncodingInfo class. | |
param String fileType: | |
optional, e.g. “wav” | |
raises IOError: | if the file cannot be opened |
raises TypeError: | |
if write mode is requested and the signalInfo is not given |
Close the stream
retrieve the CEncodingInfo object representation of this streams encoding
Changes to the encoding’s parameters will change the stream’s encoding, too
retrieve the CSignalInfo object representation of this streams signal
Changes to the signal’s parameter will change the stream’s parameters, too
open an audio file for reading
Open this stream for writing
path
signalInfo
encodingInfo
fileType
TypeError if signalInfo is missing
IOError if the file cannot be opened
Representation of struct sox_signalinfo_t
create a signalinfo descriptor
If the parameters are given, the signalinfo is a static structure usable to provide signal data for streams.
Retrieve the details of the signal
Returns:
dict containing rate, channels, precision and length
alter the parameters of this signalinfo. Either of the kwargs can be given to set appropriate aspect.
Kwargs: rate: int
channels: int
precision: int
length: int
Python representation of a sox buffer memory area
Supports the PEP-3118 buffer interface and can be used within a memoryview object.
Additionally, __getitem__ and __setitem__ and the iterator interface are supported in order to access the buffer data using index operations
replace our buffer with the contents of the bytearray
replace our buffer with the contents of the array of integers
return the size of this buffer in bytes. len(obj) defaults to this unless set_datalen has been called. In that case, len(obj) returns the number of valid bytes, whereas this function still returns the size of our buffer
return a bytearray object of the buffer.
The bytearray is a copy. Modifications will not reflect into the sox buffer
Return a list of this buffers items.
The list is a copy, changes will not reflect into the sox buffer
fill our buffer with the contents of the bytearray
fill our buffer with the contents of the array of integers
The effectschain is the primary instrument of processing audio data. CEffectsChain is basically a wrapper around sox_effects_chain_t and provides methods to add effects, to start the processing (flow) and to set parameters on input and output.
Create an effect chain
istream: CSoxStream input stream object
ostream: CSoxStream output stream object
Add an effect to the chain
Flow samples through the effects processing chain until EOF is reached.
Input stream analog to CSoxStream, but reading from a python:processes Pipe
Close the stream
CEffect(“trim”, [ b‘2’, b‘4’])
Basic sox effect wraps all builtin effects. It is also used to derive custom effects by inheriting and overloading the create method.
name: string of the effects name
arguments: array of effect arguments containing byte strings
Create a sox internal effect
optional parameters to initialize the effect: @param <string> name of the effect @param <[]> array of effect arguments @see create
retrieve the CEncodingInfo object representation of this streams in_encoding
retrieve the CSignalInfo object representation of this streams in_signal
Retrieve the effect’s name
retrieve the CEncodingInfo object representation of this streams in_encoding
retrieve the CSignalInfo object representation of this streams out_signal
SocketOutput(“output”,[connection])
Output the chain data to a multiprocess socket. Used as output effect at the end of a chain.
Example:
parent_conn, child_conn = Pipe()
output = SocketOutput("output", [child_conn])
chain = pysox.CEffectsChain()
chain.add_effect(output)
...
Example custom effect usable as audio data input into a chain. CCustomEffect must be overloaded and the method callback is then used to provide audio input data.
Create a sox internal effect
optional parameters to initialize the effect: @param <string> name of the effect @param <[]> array of effect arguments @see create
virtual function, must be overloaded
virtual function, must be overloaded
Called when chain shuts down, after stop
Used to do final cleanups
Called when chain starts flowing
Used to do last setups
Called when chain shuts down
PowerMixFiles(“input”, [“test1.wav”, “test2.wav”])
Input effect provider for CEffectChain in order to mix multiple input files. This effect replaces the sox internal input effect.
Volumes are mixed down by 1/sqrt(n) where n is the number of inputs.
Clipping might occur here, but in most cases distortions are not susceptible.
ConcatenateFiles(“input”, [“test1.wav”, “test2.wav”])
Input effect provider for CEffectChain in order to concatenate multiple input files. This effect replaces the sox internal input effect.
All audio data must be the same format (bitrate, channels, precision) The output’s length is the total length of all combined signals.
MixFiles(“input”, [“test1.wav”, “test2.wav”])
Input effect provider for CEffectChain in order to mix multiple input files. This effect replaces the sox internal input effect.
Volumes are mixed down by 1/n where n is the number of inputs.
Clipping probably will not occur with this method, but the signal appears to be less loud than the originals.
The input files signals must be equal, meaning bitrate and channels must be equal. The signal’s length can be different, the output will get the length of the longest signal.
nullstream = CNullFile()
The null input file, which produces an infinite amount of silence
path: string
mode: string
fileType: string
Create the null input file, which produces silence of infinite length.
from pysox import CSoxApp
sapp = CSoxApp(input1, input2, output, nullparams=(44100,2,32) )
sapp.flow([ ('trim', [b'0',b'2']), ] )
or:
sapp = CSoxApp(input1, input2, input3, nullparams=(44100,2,32), effectparams=[('trim', [b'0',b'2']), ], output='output0' )
sapp.flow()
nullparams: set(rate,channels,precision)
If any inputfile is the string ‘null’, then CNullFile is used to generate silence with the nullparams as signal parameters.
flow the chain created in setup