blob: 774a92e1d22e84251b0f1be9c2b4c955b61ce191 [file] [log] [blame]
Fred Drakefc576191998-04-04 07:15:02 +00001\section{Built-in Module \module{audioop}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-audioop}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003\bimodindex{audioop}
4
Fred Drakefc576191998-04-04 07:15:02 +00005The \module{audioop} module contains some useful operations on sound
6fragments. It operates on sound fragments consisting of signed
7integer samples 8, 16 or 32 bits wide, stored in Python strings. This
8is the same format as used by the \module{al} and \module{sunaudiodev}
9modules. All scalar items are integers, unless specified otherwise.
10
11% This para is mostly here to provide an excuse for the index entries...
12This module provides support for u-LAW and Intel/DVI ADPCM encodings.
13\index{Intel/DVI ADPCM}
14\index{ADPCM, Intel/DVI}
15\index{u-LAW}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000016
17A few of the more complicated operations only take 16-bit samples,
Fred Drakefc576191998-04-04 07:15:02 +000018otherwise the sample size (in bytes) is always a parameter of the
19operation.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020
21The module defines the following variables and functions:
22
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000023\begin{excdesc}{error}
24This exception is raised on all errors, such as unknown number of bytes
25per sample, etc.
26\end{excdesc}
27
Fred Drakecce10901998-03-17 06:33:25 +000028\begin{funcdesc}{add}{fragment1, fragment2, width}
Guido van Rossum470be141995-03-17 16:07:09 +000029Return a fragment which is the addition of the two samples passed as
30parameters. \var{width} is the sample width in bytes, either
31\code{1}, \code{2} or \code{4}. Both fragments should have the same
32length.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000033\end{funcdesc}
34
Fred Drakecce10901998-03-17 06:33:25 +000035\begin{funcdesc}{adpcm2lin}{adpcmfragment, width, state}
Guido van Rossum470be141995-03-17 16:07:09 +000036Decode an Intel/DVI ADPCM coded fragment to a linear fragment. See
Fred Drakefc576191998-04-04 07:15:02 +000037the description of \function{lin2adpcm()} for details on ADPCM coding.
Guido van Rossum470be141995-03-17 16:07:09 +000038Return a tuple \code{(\var{sample}, \var{newstate})} where the sample
39has the width specified in \var{width}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000040\end{funcdesc}
41
Fred Drakecce10901998-03-17 06:33:25 +000042\begin{funcdesc}{adpcm32lin}{adpcmfragment, width, state}
Fred Drakefc576191998-04-04 07:15:02 +000043Decode an alternative 3-bit ADPCM code. See \function{lin2adpcm3()}
44for details.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000045\end{funcdesc}
46
Fred Drakecce10901998-03-17 06:33:25 +000047\begin{funcdesc}{avg}{fragment, width}
Guido van Rossum470be141995-03-17 16:07:09 +000048Return the average over all samples in the fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000049\end{funcdesc}
50
Fred Drakecce10901998-03-17 06:33:25 +000051\begin{funcdesc}{avgpp}{fragment, width}
Guido van Rossum470be141995-03-17 16:07:09 +000052Return the average peak-peak value over all samples in the fragment.
53No filtering is done, so the usefulness of this routine is
54questionable.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055\end{funcdesc}
56
Fred Drakecce10901998-03-17 06:33:25 +000057\begin{funcdesc}{bias}{fragment, width, bias}
Guido van Rossum470be141995-03-17 16:07:09 +000058Return a fragment that is the original fragment with a bias added to
59each sample.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000060\end{funcdesc}
61
Fred Drakecce10901998-03-17 06:33:25 +000062\begin{funcdesc}{cross}{fragment, width}
Guido van Rossum470be141995-03-17 16:07:09 +000063Return the number of zero crossings in the fragment passed as an
64argument.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000065\end{funcdesc}
66
Fred Drakecce10901998-03-17 06:33:25 +000067\begin{funcdesc}{findfactor}{fragment, reference}
Guido van Rossum470be141995-03-17 16:07:09 +000068Return a factor \var{F} such that
Fred Drakefc576191998-04-04 07:15:02 +000069\code{rms(add(\var{fragment}, mul(\var{reference}, -\var{F})))} is
70minimal, i.e., return the factor with which you should multiply
71\var{reference} to make it match as well as possible to
72\var{fragment}. The fragments should both contain 2-byte samples.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000073
Fred Drakefc576191998-04-04 07:15:02 +000074The time taken by this routine is proportional to
75\code{len(\var{fragment})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000076\end{funcdesc}
77
Fred Drakecce10901998-03-17 06:33:25 +000078\begin{funcdesc}{findfit}{fragment, reference}
Guido van Rossum470be141995-03-17 16:07:09 +000079Try to match \var{reference} as well as possible to a portion of
80\var{fragment} (which should be the longer fragment). This is
81(conceptually) done by taking slices out of \var{fragment}, using
Fred Drakefc576191998-04-04 07:15:02 +000082\function{findfactor()} to compute the best match, and minimizing the
Guido van Rossum470be141995-03-17 16:07:09 +000083result. The fragments should both contain 2-byte samples. Return a
84tuple \code{(\var{offset}, \var{factor})} where \var{offset} is the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000085(integer) offset into \var{fragment} where the optimal match started
Guido van Rossum470be141995-03-17 16:07:09 +000086and \var{factor} is the (floating-point) factor as per
Fred Drakefc576191998-04-04 07:15:02 +000087\function{findfactor()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000088\end{funcdesc}
89
Fred Drakecce10901998-03-17 06:33:25 +000090\begin{funcdesc}{findmax}{fragment, length}
Guido van Rossum470be141995-03-17 16:07:09 +000091Search \var{fragment} for a slice of length \var{length} samples (not
92bytes!)\ with maximum energy, i.e., return \var{i} for which
93\code{rms(fragment[i*2:(i+length)*2])} is maximal. The fragments
94should both contain 2-byte samples.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000095
Fred Drakefc576191998-04-04 07:15:02 +000096The routine takes time proportional to \code{len(\var{fragment})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000097\end{funcdesc}
98
Fred Drakecce10901998-03-17 06:33:25 +000099\begin{funcdesc}{getsample}{fragment, width, index}
Guido van Rossum470be141995-03-17 16:07:09 +0000100Return the value of sample \var{index} from the fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000101\end{funcdesc}
102
Fred Drakecce10901998-03-17 06:33:25 +0000103\begin{funcdesc}{lin2lin}{fragment, width, newwidth}
Guido van Rossum470be141995-03-17 16:07:09 +0000104Convert samples between 1-, 2- and 4-byte formats.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000105\end{funcdesc}
106
Fred Drakecce10901998-03-17 06:33:25 +0000107\begin{funcdesc}{lin2adpcm}{fragment, width, state}
Guido van Rossum470be141995-03-17 16:07:09 +0000108Convert samples to 4 bit Intel/DVI ADPCM encoding. ADPCM coding is an
109adaptive coding scheme, whereby each 4 bit number is the difference
110between one sample and the next, divided by a (varying) step. The
111Intel/DVI ADPCM algorithm has been selected for use by the IMA, so it
112may well become a standard.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000113
Fred Drakefc576191998-04-04 07:15:02 +0000114\var{state} is a tuple containing the state of the coder. The coder
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000115returns a tuple \code{(\var{adpcmfrag}, \var{newstate})}, and the
Fred Drakefc576191998-04-04 07:15:02 +0000116\var{newstate} should be passed to the next call of
117\function{lin2adpcm()}. In the initial call, \code{None} can be
118passed as the state. \var{adpcmfrag} is the ADPCM coded fragment
119packed 2 4-bit values per byte.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000120\end{funcdesc}
121
Fred Drakecce10901998-03-17 06:33:25 +0000122\begin{funcdesc}{lin2adpcm3}{fragment, width, state}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000123This is an alternative ADPCM coder that uses only 3 bits per sample.
124It is not compatible with the Intel/DVI ADPCM coder and its output is
Guido van Rossum470be141995-03-17 16:07:09 +0000125not packed (due to laziness on the side of the author). Its use is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000126discouraged.
127\end{funcdesc}
128
Fred Drakecce10901998-03-17 06:33:25 +0000129\begin{funcdesc}{lin2ulaw}{fragment, width}
Fred Drakefc576191998-04-04 07:15:02 +0000130Convert samples in the audio fragment to u-LAW encoding and return
131this as a Python string. u-LAW is an audio encoding format whereby
Guido van Rossum470be141995-03-17 16:07:09 +0000132you get a dynamic range of about 14 bits using only 8 bit samples. It
133is used by the Sun audio hardware, among others.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000134\end{funcdesc}
135
Fred Drakecce10901998-03-17 06:33:25 +0000136\begin{funcdesc}{minmax}{fragment, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000137Return a tuple consisting of the minimum and maximum values of all
138samples in the sound fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000139\end{funcdesc}
140
Fred Drakecce10901998-03-17 06:33:25 +0000141\begin{funcdesc}{max}{fragment, width}
Fred Drakeaf8a0151998-01-14 14:51:31 +0000142Return the maximum of the \emph{absolute value} of all samples in a
Guido van Rossum470be141995-03-17 16:07:09 +0000143fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000144\end{funcdesc}
145
Fred Drakecce10901998-03-17 06:33:25 +0000146\begin{funcdesc}{maxpp}{fragment, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000147Return the maximum peak-peak value in the sound fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000148\end{funcdesc}
149
Fred Drakecce10901998-03-17 06:33:25 +0000150\begin{funcdesc}{mul}{fragment, width, factor}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000151Return a fragment that has all samples in the original framgent
Guido van Rossum470be141995-03-17 16:07:09 +0000152multiplied by the floating-point value \var{factor}. Overflow is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000153silently ignored.
154\end{funcdesc}
155
Fred Drakefc576191998-04-04 07:15:02 +0000156\begin{funcdesc}{ratecv}{fragment, width, nchannels, inrate, outrate,
157 state\optional{, weightA\optional{, weightB}}}
Guido van Rossum6fb6f101997-02-14 15:59:49 +0000158Convert the frame rate of the input fragment.
159
Fred Drakefc576191998-04-04 07:15:02 +0000160\var{state} is a tuple containing the state of the converter. The
Guido van Rossum6fb6f101997-02-14 15:59:49 +0000161converter returns a tupl \code{(\var{newfragment}, \var{newstate})},
Fred Drakefc576191998-04-04 07:15:02 +0000162and \var{newstate} should be passed to the next call of
163\function{ratecv()}.
Guido van Rossum6fb6f101997-02-14 15:59:49 +0000164
Fred Drakefc576191998-04-04 07:15:02 +0000165The \var{weightA} and \var{weightB} arguments are parameters for a
166simple digital filter and default to \code{1} and \code{0}
167respectively.
Guido van Rossum6fb6f101997-02-14 15:59:49 +0000168\end{funcdesc}
169
Fred Drakecce10901998-03-17 06:33:25 +0000170\begin{funcdesc}{reverse}{fragment, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000171Reverse the samples in a fragment and returns the modified fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000172\end{funcdesc}
173
Fred Drakecce10901998-03-17 06:33:25 +0000174\begin{funcdesc}{rms}{fragment, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000175Return the root-mean-square of the fragment, i.e.
Fred Drakea4c640e1998-05-11 19:51:11 +0000176%begin{latexonly}
177\iftexi
178%end{latexonly}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000179the square root of the quotient of the sum of all squared sample value,
180divided by the sumber of samples.
Fred Drakefc576191998-04-04 07:15:02 +0000181%begin{latexonly}
Fred Drakea4c640e1998-05-11 19:51:11 +0000182\else
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000183% in eqn: sqrt { sum S sub i sup 2 over n }
184\begin{displaymath}
185\catcode`_=8
186\sqrt{\frac{\sum{{S_{i}}^{2}}}{n}}
187\end{displaymath}
Fred Drakea4c640e1998-05-11 19:51:11 +0000188\fi
Fred Drakefc576191998-04-04 07:15:02 +0000189%end{latexonly}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000190This is a measure of the power in an audio signal.
191\end{funcdesc}
192
Fred Drakecce10901998-03-17 06:33:25 +0000193\begin{funcdesc}{tomono}{fragment, width, lfactor, rfactor}
Guido van Rossum470be141995-03-17 16:07:09 +0000194Convert a stereo fragment to a mono fragment. The left channel is
195multiplied by \var{lfactor} and the right channel by \var{rfactor}
196before adding the two channels to give a mono signal.
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000197\end{funcdesc}
198
Fred Drakecce10901998-03-17 06:33:25 +0000199\begin{funcdesc}{tostereo}{fragment, width, lfactor, rfactor}
Guido van Rossum470be141995-03-17 16:07:09 +0000200Generate a stereo fragment from a mono fragment. Each pair of samples
201in the stereo fragment are computed from the mono sample, whereby left
202channel samples are multiplied by \var{lfactor} and right channel
203samples by \var{rfactor}.
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000204\end{funcdesc}
205
Fred Drakecce10901998-03-17 06:33:25 +0000206\begin{funcdesc}{ulaw2lin}{fragment, width}
Fred Drakefc576191998-04-04 07:15:02 +0000207Convert sound fragments in u-LAW encoding to linearly encoded sound
208fragments. u-LAW encoding always uses 8 bits samples, so \var{width}
Guido van Rossum470be141995-03-17 16:07:09 +0000209refers only to the sample width of the output fragment here.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000210\end{funcdesc}
211
Fred Drakefc576191998-04-04 07:15:02 +0000212Note that operations such as \function{mul()} or \function{max()} make
213no distinction between mono and stereo fragments, i.e.\ all samples
214are treated equal. If this is a problem the stereo fragment should be
215split into two mono fragments first and recombined later. Here is an
216example of how to do that:
217
Fred Drake19479911998-02-13 06:58:54 +0000218\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000219def mul_stereo(sample, width, lfactor, rfactor):
220 lsample = audioop.tomono(sample, width, 1, 0)
221 rsample = audioop.tomono(sample, width, 0, 1)
222 lsample = audioop.mul(sample, width, lfactor)
223 rsample = audioop.mul(sample, width, rfactor)
224 lsample = audioop.tostereo(lsample, width, 1, 0)
225 rsample = audioop.tostereo(rsample, width, 0, 1)
226 return audioop.add(lsample, rsample, width)
Fred Drake19479911998-02-13 06:58:54 +0000227\end{verbatim}
Fred Drakefc576191998-04-04 07:15:02 +0000228
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000229If you use the ADPCM coder to build network packets and you want your
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000230protocol to be stateless (i.e.\ to be able to tolerate packet loss)
Guido van Rossum470be141995-03-17 16:07:09 +0000231you should not only transmit the data but also the state. Note that
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000232you should send the \var{initial} state (the one you passed to
Fred Drakefc576191998-04-04 07:15:02 +0000233\function{lin2adpcm()}) along to the decoder, not the final state (as
234returned by the coder). If you want to use \function{struct.struct()}
235to store the state in binary you can code the first element (the
236predicted value) in 16 bits and the second (the delta index) in 8.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000237
238The ADPCM coders have never been tried against other ADPCM coders,
Guido van Rossum470be141995-03-17 16:07:09 +0000239only against themselves. It could well be that I misinterpreted the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000240standards in which case they will not be interoperable with the
241respective standards.
242
Fred Drakefc576191998-04-04 07:15:02 +0000243The \function{find*()} routines might look a bit funny at first sight.
Guido van Rossum470be141995-03-17 16:07:09 +0000244They are primarily meant to do echo cancellation. A reasonably
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000245fast way to do this is to pick the most energetic piece of the output
246sample, locate that in the input sample and subtract the whole output
247sample from the input sample:
Fred Drakefc576191998-04-04 07:15:02 +0000248
Fred Drake19479911998-02-13 06:58:54 +0000249\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000250def echocancel(outputdata, inputdata):
251 pos = audioop.findmax(outputdata, 800) # one tenth second
252 out_test = outputdata[pos*2:]
253 in_test = inputdata[pos*2:]
254 ipos, factor = audioop.findfit(in_test, out_test)
255 # Optional (for better cancellation):
256 # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],
257 # out_test)
258 prefill = '\0'*(pos+ipos)*2
259 postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata))
260 outputdata = prefill + audioop.mul(outputdata,2,-factor) + postfill
261 return audioop.add(inputdata, outputdata, 2)
Fred Drake19479911998-02-13 06:58:54 +0000262\end{verbatim}