Built-in Module audioop

audioop

The audioop module contains some useful operations on sound fragments. It operates on sound fragments consisting of signed integer samples 8, 16 or 32 bits wide, stored in Python strings. This is the same format as used by the al and sunaudiodev modules. All scalar items are integers, unless specified otherwise.

A few of the more complicated operations only take 16-bit samples, otherwise the sample size (in bytes) is always a parameter of the operation.

The module defines the following variables and functions:


\begin{excdesc}{error}
This exception is raised on all errors, such as unknown number of bytes
per sample, etc.
\end{excdesc}


\begin{funcdesc}{add}{fragment1\, fragment2\, width}
Return a fragment which is ...
...\code{2} or \code{4}. Both fragments should have the same
length.
\end{funcdesc}


\begin{funcdesc}{adpcm2lin}{adpcmfragment\, width\, state}
Decode an Intel/DVI A...
...state})} where the sample
has the width specified in \var{width}.
\end{funcdesc}


\begin{funcdesc}{adpcm32lin}{adpcmfragment\, width\, state}
Decode an alternative 3-bit ADPCM code. See \code{lin2adpcm3} for
details.
\end{funcdesc}


\begin{funcdesc}{avg}{fragment\, width}
Return the average over all samples in the fragment.
\end{funcdesc}


\begin{funcdesc}{avgpp}{fragment\, width}
Return the average peak-peak value ove...
...ering is done, so the usefulness of this routine is
questionable.
\end{funcdesc}


\begin{funcdesc}{bias}{fragment\, width\, bias}
Return a fragment that is the original fragment with a bias added to
each sample.
\end{funcdesc}


\begin{funcdesc}{cross}{fragment\, width}
Return the number of zero crossings in the fragment passed as an
argument.
\end{funcdesc}


\begin{funcdesc}{findfactor}{fragment\, reference}
Return a factor \var{F} such ...
...e taken by this routine is proportional to \code{len(fragment)}.
\end{funcdesc}


\begin{funcdesc}{findfit}{fragment\, reference}
This routine (which only accepts...
...{factor} is the (floating-point) factor as per
\code{findfactor}.
\end{funcdesc}


\begin{funcdesc}{findmax}{fragment\, length}
Search \var{fragment} for a slice o...
...\par
The routine takes time proportional to \code{len(fragment)}.
\end{funcdesc}


\begin{funcdesc}{getsample}{fragment\, width\, index}
Return the value of sample \var{index} from the fragment.
\end{funcdesc}


\begin{funcdesc}{lin2lin}{fragment\, width\, newwidth}
Convert samples between 1-, 2- and 4-byte formats.
\end{funcdesc}


\begin{funcdesc}{lin2adpcm}{fragment\, width\, state}
Convert samples to 4 bit I...
...frag}
is the ADPCM coded fragment packed 2 4-bit values per byte.
\end{funcdesc}


\begin{funcdesc}{lin2adpcm3}{fragment\, width\, state}
This is an alternative AD...
...e to laziness on the side of the author). Its use is
discouraged.
\end{funcdesc}


\begin{funcdesc}{lin2ulaw}{fragment\, width}
Convert samples in the audio fragme...
... bit samples. It
is used by the Sun audio hardware, among others.
\end{funcdesc}


\begin{funcdesc}{minmax}{fragment\, width}
Return a tuple consisting of the minimum and maximum values of all
samples in the sound fragment.
\end{funcdesc}


\begin{funcdesc}{max}{fragment\, width}
Return the maximum of the {\em absolute value} of all samples in a
fragment.
\end{funcdesc}


\begin{funcdesc}{maxpp}{fragment\, width}
Return the maximum peak-peak value in the sound fragment.
\end{funcdesc}


\begin{funcdesc}{mul}{fragment\, width\, factor}
Return a fragment that has all ...
... floating-point value \var{factor}. Overflow is
silently ignored.
\end{funcdesc}


\begin{funcdesc}{reverse}{fragment\, width}
Reverse the samples in a fragment and returns the modified fragment.
\end{funcdesc}


\begin{funcdesc}{rms}{fragment\, width}
Return the root-mean-square of the fragm...
...isplaymath}\fi
This is a measure of the power in an audio signal.
\end{funcdesc}


\begin{funcdesc}{tomono}{fragment\, width\, lfactor\, rfactor}
Convert a stereo...
...ar{rfactor}
before adding the two channels to give a mono signal.
\end{funcdesc}


\begin{funcdesc}{tostereo}{fragment\, width\, lfactor\, rfactor}
Generate a ster...
...lied by \var{lfactor} and right channel
samples by \var{rfactor}.
\end{funcdesc}


\begin{funcdesc}{ulaw2lin}{fragment\, width}
Convert sound fragments in ULAW enc...
...dth}
refers only to the sample width of the output fragment here.
\end{funcdesc}

Note that operations such as mul or max make no distinction between mono and stereo fragments, i.e. all samples are treated equal. If this is a problem the stereo fragment should be split into two mono fragments first and recombined later. Here is an example of how to do that:

def mul_stereo(sample, width, lfactor, rfactor):
    lsample = audioop.tomono(sample, width, 1, 0)
    rsample = audioop.tomono(sample, width, 0, 1)
    lsample = audioop.mul(sample, width, lfactor)
    rsample = audioop.mul(sample, width, rfactor)
    lsample = audioop.tostereo(lsample, width, 1, 0)
    rsample = audioop.tostereo(rsample, width, 0, 1)
    return audioop.add(lsample, rsample, width)

If you use the ADPCM coder to build network packets and you want your protocol to be stateless (i.e. to be able to tolerate packet loss) you should not only transmit the data but also the state. Note that you should send the initial state (the one you passed to lin2adpcm) along to the decoder, not the final state (as returned by the coder). If you want to use struct to store the state in binary you can code the first element (the predicted value) in 16 bits and the second (the delta index) in 8.

The ADPCM coders have never been tried against other ADPCM coders, only against themselves. It could well be that I misinterpreted the standards in which case they will not be interoperable with the respective standards.

The find... routines might look a bit funny at first sight. They are primarily meant to do echo cancellation. A reasonably fast way to do this is to pick the most energetic piece of the output sample, locate that in the input sample and subtract the whole output sample from the input sample:

def echocancel(outputdata, inputdata):
    pos = audioop.findmax(outputdata, 800)    # one tenth second
    out_test = outputdata[pos*2:]
    in_test = inputdata[pos*2:]
    ipos, factor = audioop.findfit(in_test, out_test)
    # Optional (for better cancellation):
    # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)], 
    #              out_test)
    prefill = '\0'*(pos+ipos)*2
    postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata))
    outputdata = prefill + audioop.mul(outputdata,2,-factor) + postfill
    return audioop.add(inputdata, outputdata, 2)