sox(destination, source=None, format=None, nChannels=None, fs=None, transform=None, effects='', verbose=None)¶
An instance of this class connects to the standalone
soxexecutable, assuming that that
IsInstalled(). The instance can then be used to transform audio data in a wide variety of ways, and encode it to disk in a variety of formats (although typically fewer formats than
You may find the class method
sox.Process()more useful than an instance.
You can also chain processing from one
soxinstance to another, or between a
soxinstance and a
ffmpeginstance, by passing the other instance as this one’s
destinationinstead of specifying a filename.
Here is an example of chaining. Let’s say we want to use a sox effect and save the result as in mp3 format, but sox does not support the mp3 format. So instead we write out, on a pipe, to an
ffmpegprocess, which can save to mp3:
import audiomath as am s = am.TestSound('12') am.sox.Process( s, destination=am.ffmpeg( 'output.mp3', source=s ), effects='loudness -10', )
The sox binary is not included in the Python package by default. Installation is up to you. You can install it like any other application, somewhere on the system path. Alternatively, if you want to, you can put it directly inside the
audiomathpackage directory, or inside a subdirectory called
aux-bin—the class method
sox.Install()can help you do this. If you install it inside the
audiomathpackage, then this has two advantages: (a) audiomath will find it there when it attempts to launch it, and (b) it will be included in the output of
Manifest('pyinstaller')which you can use to help you freeze your application.
- destination (str, ffmpeg, sox) – output filename—be sure to include the file
extension so that ffmpeg can infer the desired
output format; can alternatively use another
soxas the destination (in that case,
fswill also be intelligently inferred from that instance if they are not otherwise specified);
- source (Sound, Recorder, Player, Stream) – optional instance that can intelligently specify
fsall in one go;
- format (str) – format of the raw data—should be either a valid
ffmpeg PCM format string (e.g.
'f32le') or a construction argument for
- nChannels (int) – number of channels to expect in each raw data packet;
- fs (int, float) – sampling frequency, in Hz;
- effects (str, list, tuple) – a string, or sequence of strings, specifying the effect names and effect options to be passed to sox on the command line;
- transform (None, function) – an optional callable that can receive each data
packet, along with the sample number and sampling
frequency, and return a transformed version of the
data packet to be send to
soxin place of the original;
- verbose (bool) – whether or not to print the standard output and standard error content produced by the binary.
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
Convert a format specifier (anything that is accepted as an input to
numpy.dtype()) into a string that specifies sample format in ffmpeg style (e.g.
Install(pathToDownloadedExecutable, deleteOriginal=False, preview=False)¶
Auxiliary command-line binaries (like
sox) can be large relative to the rest of
audiomath. Therefore, they are not included in the package by default. You should download what you need from the respective project websites (e.g. https://ffmpeg.org or http://sox.sourceforge.net )
If the binary managed by this class is already installed somewhere on the operating-system’s search path,
audiomathwill be able to find it, so then you probably do not need this helper function. However, if you have just downloaded, say, a statically-linked build of
ffmpeg.exe, are looking for somewhere to put it, and want to put it inside the
audiomathpackage directory itself, then this function will do it for you (it will actually put it inside an automatically-created sub-directory called
audiomathwill find it there, and it will also be included in the output of
Manifest('pyinstaller')which helps if you want to use
pyinstallerto freeze your application.
NB: if the utility consists of multiple files (e.g.
sox.exeand its many associated
dllfiles on Windows) then
pathToDownloadedExecutableshould be a path to the directory that contains them all.
Falsedepending on whether the auxiliary binary executable managed by this class (
sox) is accessible and executable, either inside the
Install()) or on the operating system’s search path.
Process(snd, destination=None, **kwargs)¶
Whereas instances of
AuxiliaryBinaryInterfacesubclasses are good for processing chunks of sound data sequentially, if you simply want to process a whole
Soundinstance in one go, it is easier to use this class method. Under the hood, it will create a temporary instance, using any specified
You can direct its output to the file specified by
destination. Alternatively you can leave
destination=Noneand thereby receive the output (actually written to and read back from a temporary uncompressed
wavfile) as a new
Soundinstance returned by this function.
Example, using the
import audiomath as am s = am.TestSound('12') s2 = am.sox.Process(s, effects='loudness -10') # or, to take that example one stage further, # the following will generally equalize the # perceived loudness (according to ISO 226) # across all channels of a Sound `s`: s = am.TestSound().AutoScale() s2 = am.Stack( am.sox.Process(eachChannel, effects='loudness -10') for eachChannel in s.SplitChannels() ).AutoScale() # finally rescale all channels together # according to their collective maximum
- destination (str, ffmpeg, sox) – output filename—be sure to include the file extension so that ffmpeg can infer the desired output format; can alternatively use another instance of