Simple examples

Reading an audio file

Lets open an audio file from our harddisk

>>> import pysox
>>> audio = pysox.CSoxStream('test.wav')
>>> repr(audio)
'CSoxStream(test.wav, wav)'
>>> print(audio)

We have opened an existing file for reading. Now it is easy to retrieve the audio signal information:

>>> print(audio.get_signal())

We see that the bitrate is 48kHz, 2 channels, 32 bits precision and a total of 288000 are in the file.

>>> print(audio.get_encoding())
CencodingInfo(1,32,"Signed PCM","Signed Integer PCM",inf)

The audio is a simple PCM file, which is quite common for wave files.

Finally, we close the file.

>>> audio.close()

Create a new audio file

To open audio for writing, we again use CSoxStream:

>>> outfile = pysox.CSoxStream('out.wav','w', pysox.CSignalInfo(48000,2,32))
>>> repr(outfile)
'CSoxStream(out.wav, wav)'

The tricky part is the third parameter, the outfile needs the specification of its basic format. This is defined using the CSignalInfo class.

Of course, we cannot do much with this file.

>>> outfile.close()
>>> repr(outfile)

Process audio with a simple effect

To process audio, we open an input file and create an output file with the same signal characteristicts.

>>> infile = pysox.CSoxStream('test.wav')
>>> outfile = pysox.CSoxStream('out.wav','w',infile.get_signal())

Then we define an effectschain which will do all the work.

>>> chain = pysox.CEffectsChain(infile, outfile)

An effect is added to the chain, see the sox manpage for available effects.

>>> effect = pysox.CEffect("vol", [ b'18dB' ])
>>> chain.add_effect(effect)

All is set up, now we flow the audio through the chain

>>> chain.flow_effects()
vol: vol clipped 264906 samples; decrease volume?
>>> outfile.close()

We now have a new file which sounds louder than the original one.

Simplify using SoxApp

The above volume example can achieved with the CSoxApp wrapper class.

>>> import pysox
>>> app = pysox.CSoxApp('test.wav', 'out.wav', effectparams=[ ("vol", [ b'18dB' ]), ])
>>> app.flow()
vol: vol clipped 264906 samples; decrease volume?

Now out.wav is on our file system.