1998-08-10 16:42:37 -03:00
|
|
|
\section{\module{audioop} ---
|
1999-04-22 18:23:22 -03:00
|
|
|
Manipulate raw audio data}
|
1998-07-23 14:59:49 -03:00
|
|
|
|
1999-04-22 18:23:22 -03:00
|
|
|
\declaremodule{builtin}{audioop}
|
1998-07-23 14:59:49 -03:00
|
|
|
\modulesynopsis{Manipulate raw audio data.}
|
|
|
|
|
1994-01-01 21:22:07 -04:00
|
|
|
|
1998-04-04 03:15:02 -04:00
|
|
|
The \module{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
|
1999-04-22 18:23:22 -03:00
|
|
|
is the same format as used by the \refmodule{al} and \refmodule{sunaudiodev}
|
1998-04-04 03:15:02 -04:00
|
|
|
modules. All scalar items are integers, unless specified otherwise.
|
|
|
|
|
|
|
|
% This para is mostly here to provide an excuse for the index entries...
|
2006-04-21 07:40:58 -03:00
|
|
|
This module provides support for a-LAW, u-LAW and Intel/DVI ADPCM encodings.
|
1998-04-04 03:15:02 -04:00
|
|
|
\index{Intel/DVI ADPCM}
|
|
|
|
\index{ADPCM, Intel/DVI}
|
2006-04-21 07:40:58 -03:00
|
|
|
\index{a-LAW}
|
1998-04-04 03:15:02 -04:00
|
|
|
\index{u-LAW}
|
1994-01-01 21:22:07 -04:00
|
|
|
|
|
|
|
A few of the more complicated operations only take 16-bit samples,
|
1998-04-04 03:15:02 -04:00
|
|
|
otherwise the sample size (in bytes) is always a parameter of the
|
|
|
|
operation.
|
1994-01-01 21:22:07 -04:00
|
|
|
|
|
|
|
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}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{add}{fragment1, fragment2, width}
|
1995-03-17 12:07:09 -04:00
|
|
|
Return a fragment which is the addition of the two samples passed as
|
|
|
|
parameters. \var{width} is the sample width in bytes, either
|
|
|
|
\code{1}, \code{2} or \code{4}. Both fragments should have the same
|
|
|
|
length.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{adpcm2lin}{adpcmfragment, width, state}
|
1995-03-17 12:07:09 -04:00
|
|
|
Decode an Intel/DVI ADPCM coded fragment to a linear fragment. See
|
1998-04-04 03:15:02 -04:00
|
|
|
the description of \function{lin2adpcm()} for details on ADPCM coding.
|
1995-03-17 12:07:09 -04:00
|
|
|
Return a tuple \code{(\var{sample}, \var{newstate})} where the sample
|
|
|
|
has the width specified in \var{width}.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
\begin{funcdesc}{alaw2lin}{fragment, width}
|
|
|
|
Convert sound fragments in a-LAW encoding to linearly encoded sound
|
|
|
|
fragments. a-LAW encoding always uses 8 bits samples, so \var{width}
|
|
|
|
refers only to the sample width of the output fragment here.
|
|
|
|
\versionadded{2.5}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{avg}{fragment, width}
|
1995-03-17 12:07:09 -04:00
|
|
|
Return the average over all samples in the fragment.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{avgpp}{fragment, width}
|
1995-03-17 12:07:09 -04:00
|
|
|
Return the average peak-peak value over all samples in the fragment.
|
|
|
|
No filtering is done, so the usefulness of this routine is
|
|
|
|
questionable.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{bias}{fragment, width, bias}
|
1995-03-17 12:07:09 -04:00
|
|
|
Return a fragment that is the original fragment with a bias added to
|
|
|
|
each sample.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{cross}{fragment, width}
|
1995-03-17 12:07:09 -04:00
|
|
|
Return the number of zero crossings in the fragment passed as an
|
|
|
|
argument.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{findfactor}{fragment, reference}
|
1995-03-17 12:07:09 -04:00
|
|
|
Return a factor \var{F} such that
|
1998-04-04 03:15:02 -04:00
|
|
|
\code{rms(add(\var{fragment}, mul(\var{reference}, -\var{F})))} is
|
|
|
|
minimal, i.e., return the factor with which you should multiply
|
|
|
|
\var{reference} to make it match as well as possible to
|
|
|
|
\var{fragment}. The fragments should both contain 2-byte samples.
|
1994-01-01 21:22:07 -04:00
|
|
|
|
1998-04-04 03:15:02 -04:00
|
|
|
The time taken by this routine is proportional to
|
|
|
|
\code{len(\var{fragment})}.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{findfit}{fragment, reference}
|
1995-03-17 12:07:09 -04:00
|
|
|
Try to match \var{reference} as well as possible to a portion of
|
|
|
|
\var{fragment} (which should be the longer fragment). This is
|
|
|
|
(conceptually) done by taking slices out of \var{fragment}, using
|
1998-04-04 03:15:02 -04:00
|
|
|
\function{findfactor()} to compute the best match, and minimizing the
|
1995-03-17 12:07:09 -04:00
|
|
|
result. The fragments should both contain 2-byte samples. Return a
|
|
|
|
tuple \code{(\var{offset}, \var{factor})} where \var{offset} is the
|
1994-01-01 21:22:07 -04:00
|
|
|
(integer) offset into \var{fragment} where the optimal match started
|
1995-03-17 12:07:09 -04:00
|
|
|
and \var{factor} is the (floating-point) factor as per
|
1998-04-04 03:15:02 -04:00
|
|
|
\function{findfactor()}.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{findmax}{fragment, length}
|
1995-03-17 12:07:09 -04:00
|
|
|
Search \var{fragment} for a slice of length \var{length} samples (not
|
|
|
|
bytes!)\ with maximum energy, i.e., return \var{i} for which
|
|
|
|
\code{rms(fragment[i*2:(i+length)*2])} is maximal. The fragments
|
|
|
|
should both contain 2-byte samples.
|
1994-01-01 21:22:07 -04:00
|
|
|
|
1998-04-04 03:15:02 -04:00
|
|
|
The routine takes time proportional to \code{len(\var{fragment})}.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{getsample}{fragment, width, index}
|
1995-03-17 12:07:09 -04:00
|
|
|
Return the value of sample \var{index} from the fragment.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{lin2adpcm}{fragment, width, state}
|
1995-03-17 12:07:09 -04:00
|
|
|
Convert samples to 4 bit Intel/DVI ADPCM encoding. ADPCM coding is an
|
|
|
|
adaptive coding scheme, whereby each 4 bit number is the difference
|
|
|
|
between one sample and the next, divided by a (varying) step. The
|
|
|
|
Intel/DVI ADPCM algorithm has been selected for use by the IMA, so it
|
|
|
|
may well become a standard.
|
1994-01-01 21:22:07 -04:00
|
|
|
|
1998-04-04 03:15:02 -04:00
|
|
|
\var{state} is a tuple containing the state of the coder. The coder
|
1994-01-01 21:22:07 -04:00
|
|
|
returns a tuple \code{(\var{adpcmfrag}, \var{newstate})}, and the
|
1998-04-04 03:15:02 -04:00
|
|
|
\var{newstate} should be passed to the next call of
|
|
|
|
\function{lin2adpcm()}. In the initial call, \code{None} can be
|
|
|
|
passed as the state. \var{adpcmfrag} is the ADPCM coded fragment
|
|
|
|
packed 2 4-bit values per byte.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
\begin{funcdesc}{lin2alaw}{fragment, width}
|
|
|
|
Convert samples in the audio fragment to a-LAW encoding and return
|
|
|
|
this as a Python string. a-LAW is an audio encoding format whereby
|
|
|
|
you get a dynamic range of about 13 bits using only 8 bit samples. It
|
|
|
|
is used by the Sun audio hardware, among others.
|
|
|
|
\versionadded{2.5}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{lin2lin}{fragment, width, newwidth}
|
|
|
|
Convert samples between 1-, 2- and 4-byte formats.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{lin2ulaw}{fragment, width}
|
1998-04-04 03:15:02 -04:00
|
|
|
Convert samples in the audio fragment to u-LAW encoding and return
|
|
|
|
this as a Python string. u-LAW is an audio encoding format whereby
|
1995-03-17 12:07:09 -04:00
|
|
|
you get a dynamic range of about 14 bits using only 8 bit samples. It
|
|
|
|
is used by the Sun audio hardware, among others.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{minmax}{fragment, width}
|
1995-03-17 12:07:09 -04:00
|
|
|
Return a tuple consisting of the minimum and maximum values of all
|
|
|
|
samples in the sound fragment.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{max}{fragment, width}
|
1998-01-14 10:51:31 -04:00
|
|
|
Return the maximum of the \emph{absolute value} of all samples in a
|
1995-03-17 12:07:09 -04:00
|
|
|
fragment.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{maxpp}{fragment, width}
|
1995-03-17 12:07:09 -04:00
|
|
|
Return the maximum peak-peak value in the sound fragment.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{mul}{fragment, width, factor}
|
2000-07-16 16:01:10 -03:00
|
|
|
Return a fragment that has all samples in the original fragment
|
1995-03-17 12:07:09 -04:00
|
|
|
multiplied by the floating-point value \var{factor}. Overflow is
|
1994-01-01 21:22:07 -04:00
|
|
|
silently ignored.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-04-04 03:15:02 -04:00
|
|
|
\begin{funcdesc}{ratecv}{fragment, width, nchannels, inrate, outrate,
|
|
|
|
state\optional{, weightA\optional{, weightB}}}
|
1997-02-14 11:59:49 -04:00
|
|
|
Convert the frame rate of the input fragment.
|
|
|
|
|
1998-04-04 03:15:02 -04:00
|
|
|
\var{state} is a tuple containing the state of the converter. The
|
2000-07-16 16:01:10 -03:00
|
|
|
converter returns a tuple \code{(\var{newfragment}, \var{newstate})},
|
1998-04-04 03:15:02 -04:00
|
|
|
and \var{newstate} should be passed to the next call of
|
2001-12-06 19:16:09 -04:00
|
|
|
\function{ratecv()}. The initial call should pass \code{None}
|
|
|
|
as the state.
|
1997-02-14 11:59:49 -04:00
|
|
|
|
1998-04-04 03:15:02 -04:00
|
|
|
The \var{weightA} and \var{weightB} arguments are parameters for a
|
1999-04-23 14:30:40 -03:00
|
|
|
simple digital filter and default to \code{1} and \code{0} respectively.
|
1997-02-14 11:59:49 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{reverse}{fragment, width}
|
1995-03-17 12:07:09 -04:00
|
|
|
Reverse the samples in a fragment and returns the modified fragment.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{rms}{fragment, width}
|
1995-03-17 12:07:09 -04:00
|
|
|
Return the root-mean-square of the fragment, i.e.
|
1994-01-01 21:22:07 -04:00
|
|
|
\begin{displaymath}
|
|
|
|
\catcode`_=8
|
|
|
|
\sqrt{\frac{\sum{{S_{i}}^{2}}}{n}}
|
|
|
|
\end{displaymath}
|
|
|
|
This is a measure of the power in an audio signal.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{tomono}{fragment, width, lfactor, rfactor}
|
1995-03-17 12:07:09 -04:00
|
|
|
Convert a stereo fragment to a mono fragment. The left channel is
|
|
|
|
multiplied by \var{lfactor} and the right channel by \var{rfactor}
|
|
|
|
before adding the two channels to give a mono signal.
|
1995-03-13 06:03:32 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{tostereo}{fragment, width, lfactor, rfactor}
|
1995-03-17 12:07:09 -04:00
|
|
|
Generate a stereo fragment from a mono fragment. Each pair of samples
|
|
|
|
in the stereo fragment are computed from the mono sample, whereby left
|
|
|
|
channel samples are multiplied by \var{lfactor} and right channel
|
|
|
|
samples by \var{rfactor}.
|
1995-03-13 06:03:32 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{ulaw2lin}{fragment, width}
|
1998-04-04 03:15:02 -04:00
|
|
|
Convert sound fragments in u-LAW encoding to linearly encoded sound
|
|
|
|
fragments. u-LAW encoding always uses 8 bits samples, so \var{width}
|
1995-03-17 12:07:09 -04:00
|
|
|
refers only to the sample width of the output fragment here.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-04-04 03:15:02 -04:00
|
|
|
Note that operations such as \function{mul()} or \function{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:
|
|
|
|
|
1998-02-13 02:58:54 -04:00
|
|
|
\begin{verbatim}
|
1994-01-01 21:22:07 -04:00
|
|
|
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)
|
1998-02-13 02:58:54 -04:00
|
|
|
\end{verbatim}
|
1998-04-04 03:15:02 -04:00
|
|
|
|
1994-01-01 21:22:07 -04:00
|
|
|
If you use the ADPCM coder to build network packets and you want your
|
1995-03-13 06:03:32 -04:00
|
|
|
protocol to be stateless (i.e.\ to be able to tolerate packet loss)
|
1995-03-17 12:07:09 -04:00
|
|
|
you should not only transmit the data but also the state. Note that
|
1994-01-01 21:22:07 -04:00
|
|
|
you should send the \var{initial} state (the one you passed to
|
1998-04-04 03:15:02 -04:00
|
|
|
\function{lin2adpcm()}) along to the decoder, not the final state (as
|
|
|
|
returned by the coder). If you want to use \function{struct.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.
|
1994-01-01 21:22:07 -04:00
|
|
|
|
|
|
|
The ADPCM coders have never been tried against other ADPCM coders,
|
1995-03-17 12:07:09 -04:00
|
|
|
only against themselves. It could well be that I misinterpreted the
|
1994-01-01 21:22:07 -04:00
|
|
|
standards in which case they will not be interoperable with the
|
|
|
|
respective standards.
|
|
|
|
|
1998-04-04 03:15:02 -04:00
|
|
|
The \function{find*()} routines might look a bit funny at first sight.
|
1995-03-17 12:07:09 -04:00
|
|
|
They are primarily meant to do echo cancellation. A reasonably
|
1994-01-01 21:22:07 -04:00
|
|
|
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:
|
1998-04-04 03:15:02 -04:00
|
|
|
|
1998-02-13 02:58:54 -04:00
|
|
|
\begin{verbatim}
|
1994-01-01 21:22:07 -04:00
|
|
|
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)
|
1998-02-13 02:58:54 -04:00
|
|
|
\end{verbatim}
|