blob: 40ef0f4c4d67f3a1491e7ba9042adb7a64c3e42f [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Built-in Module \sectcode{audioop}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00002\bimodindex{audioop}
3
Guido van Rossum6bb1adc1995-03-13 10:03:32 +00004The \code{audioop} module contains some useful operations on sound fragments.
5It operates on sound fragments consisting of signed integer samples
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000068, 16 or 32 bits wide, stored in Python strings. This is the same
7format as used by the \code{al} and \code{sunaudiodev} modules. All
8scalar items are integers, unless specified otherwise.
9
10A few of the more complicated operations only take 16-bit samples,
11otherwise the sample size (in bytes) is always a parameter of the operation.
12
13The module defines the following variables and functions:
14
15\renewcommand{\indexsubitem}{(in module audioop)}
16\begin{excdesc}{error}
17This exception is raised on all errors, such as unknown number of bytes
18per sample, etc.
19\end{excdesc}
20
21\begin{funcdesc}{add}{fragment1\, fragment2\, width}
Guido van Rossum470be141995-03-17 16:07:09 +000022Return a fragment which is the addition of the two samples passed as
23parameters. \var{width} is the sample width in bytes, either
24\code{1}, \code{2} or \code{4}. Both fragments should have the same
25length.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000026\end{funcdesc}
27
28\begin{funcdesc}{adpcm2lin}{adpcmfragment\, width\, state}
Guido van Rossum470be141995-03-17 16:07:09 +000029Decode an Intel/DVI ADPCM coded fragment to a linear fragment. See
30the description of \code{lin2adpcm} for details on ADPCM coding.
31Return a tuple \code{(\var{sample}, \var{newstate})} where the sample
32has the width specified in \var{width}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000033\end{funcdesc}
34
35\begin{funcdesc}{adpcm32lin}{adpcmfragment\, width\, state}
Guido van Rossum470be141995-03-17 16:07:09 +000036Decode an alternative 3-bit ADPCM code. See \code{lin2adpcm3} for
37details.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000038\end{funcdesc}
39
40\begin{funcdesc}{avg}{fragment\, width}
Guido van Rossum470be141995-03-17 16:07:09 +000041Return the average over all samples in the fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000042\end{funcdesc}
43
44\begin{funcdesc}{avgpp}{fragment\, width}
Guido van Rossum470be141995-03-17 16:07:09 +000045Return the average peak-peak value over all samples in the fragment.
46No filtering is done, so the usefulness of this routine is
47questionable.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000048\end{funcdesc}
49
50\begin{funcdesc}{bias}{fragment\, width\, bias}
Guido van Rossum470be141995-03-17 16:07:09 +000051Return a fragment that is the original fragment with a bias added to
52each sample.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000053\end{funcdesc}
54
55\begin{funcdesc}{cross}{fragment\, width}
Guido van Rossum470be141995-03-17 16:07:09 +000056Return the number of zero crossings in the fragment passed as an
57argument.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000058\end{funcdesc}
59
60\begin{funcdesc}{findfactor}{fragment\, reference}
Guido van Rossum470be141995-03-17 16:07:09 +000061Return a factor \var{F} such that
62\code{rms(add(fragment, mul(reference, -F)))} is minimal, i.e.,
63return the factor with which you should multiply \var{reference} to
64make it match as well as possible to \var{fragment}. The fragments
65should both contain 2-byte samples.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000066
67The time taken by this routine is proportional to \code{len(fragment)}.
68\end{funcdesc}
69
70\begin{funcdesc}{findfit}{fragment\, reference}
Guido van Rossum470be141995-03-17 16:07:09 +000071This routine (which only accepts 2-byte sample fragments)
72
73Try to match \var{reference} as well as possible to a portion of
74\var{fragment} (which should be the longer fragment). This is
75(conceptually) done by taking slices out of \var{fragment}, using
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000076\code{findfactor} to compute the best match, and minimizing the
Guido van Rossum470be141995-03-17 16:07:09 +000077result. The fragments should both contain 2-byte samples. Return a
78tuple \code{(\var{offset}, \var{factor})} where \var{offset} is the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079(integer) offset into \var{fragment} where the optimal match started
Guido van Rossum470be141995-03-17 16:07:09 +000080and \var{factor} is the (floating-point) factor as per
81\code{findfactor}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000082\end{funcdesc}
83
84\begin{funcdesc}{findmax}{fragment\, length}
Guido van Rossum470be141995-03-17 16:07:09 +000085Search \var{fragment} for a slice of length \var{length} samples (not
86bytes!)\ with maximum energy, i.e., return \var{i} for which
87\code{rms(fragment[i*2:(i+length)*2])} is maximal. The fragments
88should both contain 2-byte samples.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000089
90The routine takes time proportional to \code{len(fragment)}.
91\end{funcdesc}
92
93\begin{funcdesc}{getsample}{fragment\, width\, index}
Guido van Rossum470be141995-03-17 16:07:09 +000094Return the value of sample \var{index} from the fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000095\end{funcdesc}
96
97\begin{funcdesc}{lin2lin}{fragment\, width\, newwidth}
Guido van Rossum470be141995-03-17 16:07:09 +000098Convert samples between 1-, 2- and 4-byte formats.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000099\end{funcdesc}
100
101\begin{funcdesc}{lin2adpcm}{fragment\, width\, state}
Guido van Rossum470be141995-03-17 16:07:09 +0000102Convert samples to 4 bit Intel/DVI ADPCM encoding. ADPCM coding is an
103adaptive coding scheme, whereby each 4 bit number is the difference
104between one sample and the next, divided by a (varying) step. The
105Intel/DVI ADPCM algorithm has been selected for use by the IMA, so it
106may well become a standard.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000107
Guido van Rossum470be141995-03-17 16:07:09 +0000108\code{State} is a tuple containing the state of the coder. The coder
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000109returns a tuple \code{(\var{adpcmfrag}, \var{newstate})}, and the
110\var{newstate} should be passed to the next call of lin2adpcm. In the
Guido van Rossum470be141995-03-17 16:07:09 +0000111initial call \code{None} can be passed as the state. \var{adpcmfrag}
112is the ADPCM coded fragment packed 2 4-bit values per byte.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000113\end{funcdesc}
114
115\begin{funcdesc}{lin2adpcm3}{fragment\, width\, state}
116This is an alternative ADPCM coder that uses only 3 bits per sample.
117It is not compatible with the Intel/DVI ADPCM coder and its output is
Guido van Rossum470be141995-03-17 16:07:09 +0000118not packed (due to laziness on the side of the author). Its use is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000119discouraged.
120\end{funcdesc}
121
122\begin{funcdesc}{lin2ulaw}{fragment\, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000123Convert samples in the audio fragment to U-LAW encoding and return
124this as a Python string. U-LAW is an audio encoding format whereby
125you get a dynamic range of about 14 bits using only 8 bit samples. It
126is used by the Sun audio hardware, among others.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000127\end{funcdesc}
128
129\begin{funcdesc}{minmax}{fragment\, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000130Return a tuple consisting of the minimum and maximum values of all
131samples in the sound fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000132\end{funcdesc}
133
134\begin{funcdesc}{max}{fragment\, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000135Return the maximum of the {\em absolute value} of all samples in a
136fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000137\end{funcdesc}
138
139\begin{funcdesc}{maxpp}{fragment\, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000140Return the maximum peak-peak value in the sound fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000141\end{funcdesc}
142
143\begin{funcdesc}{mul}{fragment\, width\, factor}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000144Return a fragment that has all samples in the original framgent
Guido van Rossum470be141995-03-17 16:07:09 +0000145multiplied by the floating-point value \var{factor}. Overflow is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000146silently ignored.
147\end{funcdesc}
148
Guido van Rossum6fb6f101997-02-14 15:59:49 +0000149\begin{funcdesc}{ratecv}{fragment\, width\, nchannels\, inrate\, outrate\, state\optional{\, weightA\, weightB}}
150Convert the frame rate of the input fragment.
151
152\code{State} is a tuple containing the state of the converter. The
153converter returns a tupl \code{(\var{newfragment}, \var{newstate})},
154and \var{newstate} should be passed to the next call of ratecv.
155
Guido van Rossum3ff73171997-03-03 16:02:32 +0000156The \code{weightA} and \code{weightB} arguments are parameters for a
Guido van Rossum6fb6f101997-02-14 15:59:49 +0000157simple digital filter and default to 1 and 0 respectively.
158\end{funcdesc}
159
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000160\begin{funcdesc}{reverse}{fragment\, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000161Reverse the samples in a fragment and returns the modified fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000162\end{funcdesc}
163
Guido van Rossum470be141995-03-17 16:07:09 +0000164\begin{funcdesc}{rms}{fragment\, width}
165Return the root-mean-square of the fragment, i.e.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000166\iftexi
167the square root of the quotient of the sum of all squared sample value,
168divided by the sumber of samples.
169\else
170% in eqn: sqrt { sum S sub i sup 2 over n }
171\begin{displaymath}
172\catcode`_=8
173\sqrt{\frac{\sum{{S_{i}}^{2}}}{n}}
174\end{displaymath}
175\fi
176This is a measure of the power in an audio signal.
177\end{funcdesc}
178
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000179\begin{funcdesc}{tomono}{fragment\, width\, lfactor\, rfactor}
Guido van Rossum470be141995-03-17 16:07:09 +0000180Convert a stereo fragment to a mono fragment. The left channel is
181multiplied by \var{lfactor} and the right channel by \var{rfactor}
182before adding the two channels to give a mono signal.
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000183\end{funcdesc}
184
185\begin{funcdesc}{tostereo}{fragment\, width\, lfactor\, rfactor}
Guido van Rossum470be141995-03-17 16:07:09 +0000186Generate a stereo fragment from a mono fragment. Each pair of samples
187in the stereo fragment are computed from the mono sample, whereby left
188channel samples are multiplied by \var{lfactor} and right channel
189samples by \var{rfactor}.
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000190\end{funcdesc}
191
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000192\begin{funcdesc}{ulaw2lin}{fragment\, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000193Convert sound fragments in ULAW encoding to linearly encoded sound
194fragments. ULAW encoding always uses 8 bits samples, so \var{width}
195refers only to the sample width of the output fragment here.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000196\end{funcdesc}
197
198Note that operations such as \code{mul} or \code{max} make no
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000199distinction between mono and stereo fragments, i.e.\ all samples are
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000200treated equal. If this is a problem the stereo fragment should be split
201into two mono fragments first and recombined later. Here is an example
202of how to do that:
203\bcode\begin{verbatim}
204def mul_stereo(sample, width, lfactor, rfactor):
205 lsample = audioop.tomono(sample, width, 1, 0)
206 rsample = audioop.tomono(sample, width, 0, 1)
207 lsample = audioop.mul(sample, width, lfactor)
208 rsample = audioop.mul(sample, width, rfactor)
209 lsample = audioop.tostereo(lsample, width, 1, 0)
210 rsample = audioop.tostereo(rsample, width, 0, 1)
211 return audioop.add(lsample, rsample, width)
212\end{verbatim}\ecode
213
214If you use the ADPCM coder to build network packets and you want your
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000215protocol to be stateless (i.e.\ to be able to tolerate packet loss)
Guido van Rossum470be141995-03-17 16:07:09 +0000216you should not only transmit the data but also the state. Note that
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000217you should send the \var{initial} state (the one you passed to
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000218\code{lin2adpcm}) along to the decoder, not the final state (as returned by
Guido van Rossum470be141995-03-17 16:07:09 +0000219the coder). If you want to use \code{struct} to store the state in
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000220binary you can code the first element (the predicted value) in 16 bits
221and the second (the delta index) in 8.
222
223The ADPCM coders have never been tried against other ADPCM coders,
Guido van Rossum470be141995-03-17 16:07:09 +0000224only against themselves. It could well be that I misinterpreted the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000225standards in which case they will not be interoperable with the
226respective standards.
227
228The \code{find...} routines might look a bit funny at first sight.
Guido van Rossum470be141995-03-17 16:07:09 +0000229They are primarily meant to do echo cancellation. A reasonably
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000230fast way to do this is to pick the most energetic piece of the output
231sample, locate that in the input sample and subtract the whole output
232sample from the input sample:
233\bcode\begin{verbatim}
234def echocancel(outputdata, inputdata):
235 pos = audioop.findmax(outputdata, 800) # one tenth second
236 out_test = outputdata[pos*2:]
237 in_test = inputdata[pos*2:]
238 ipos, factor = audioop.findfit(in_test, out_test)
239 # Optional (for better cancellation):
240 # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],
241 # out_test)
242 prefill = '\0'*(pos+ipos)*2
243 postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata))
244 outputdata = prefill + audioop.mul(outputdata,2,-factor) + postfill
245 return audioop.add(inputdata, outputdata, 2)
246\end{verbatim}\ecode