blob: 6a74a0beb7dc937b54493827ab0727ecc010b763 [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
149\begin{funcdesc}{reverse}{fragment\, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000150Reverse the samples in a fragment and returns the modified fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000151\end{funcdesc}
152
Guido van Rossum470be141995-03-17 16:07:09 +0000153\begin{funcdesc}{rms}{fragment\, width}
154Return the root-mean-square of the fragment, i.e.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000155\iftexi
156the square root of the quotient of the sum of all squared sample value,
157divided by the sumber of samples.
158\else
159% in eqn: sqrt { sum S sub i sup 2 over n }
160\begin{displaymath}
161\catcode`_=8
162\sqrt{\frac{\sum{{S_{i}}^{2}}}{n}}
163\end{displaymath}
164\fi
165This is a measure of the power in an audio signal.
166\end{funcdesc}
167
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000168\begin{funcdesc}{tomono}{fragment\, width\, lfactor\, rfactor}
Guido van Rossum470be141995-03-17 16:07:09 +0000169Convert a stereo fragment to a mono fragment. The left channel is
170multiplied by \var{lfactor} and the right channel by \var{rfactor}
171before adding the two channels to give a mono signal.
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000172\end{funcdesc}
173
174\begin{funcdesc}{tostereo}{fragment\, width\, lfactor\, rfactor}
Guido van Rossum470be141995-03-17 16:07:09 +0000175Generate a stereo fragment from a mono fragment. Each pair of samples
176in the stereo fragment are computed from the mono sample, whereby left
177channel samples are multiplied by \var{lfactor} and right channel
178samples by \var{rfactor}.
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000179\end{funcdesc}
180
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000181\begin{funcdesc}{ulaw2lin}{fragment\, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000182Convert sound fragments in ULAW encoding to linearly encoded sound
183fragments. ULAW encoding always uses 8 bits samples, so \var{width}
184refers only to the sample width of the output fragment here.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000185\end{funcdesc}
186
187Note that operations such as \code{mul} or \code{max} make no
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000188distinction between mono and stereo fragments, i.e.\ all samples are
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000189treated equal. If this is a problem the stereo fragment should be split
190into two mono fragments first and recombined later. Here is an example
191of how to do that:
192\bcode\begin{verbatim}
193def mul_stereo(sample, width, lfactor, rfactor):
194 lsample = audioop.tomono(sample, width, 1, 0)
195 rsample = audioop.tomono(sample, width, 0, 1)
196 lsample = audioop.mul(sample, width, lfactor)
197 rsample = audioop.mul(sample, width, rfactor)
198 lsample = audioop.tostereo(lsample, width, 1, 0)
199 rsample = audioop.tostereo(rsample, width, 0, 1)
200 return audioop.add(lsample, rsample, width)
201\end{verbatim}\ecode
202
203If you use the ADPCM coder to build network packets and you want your
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000204protocol to be stateless (i.e.\ to be able to tolerate packet loss)
Guido van Rossum470be141995-03-17 16:07:09 +0000205you should not only transmit the data but also the state. Note that
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000206you should send the \var{initial} state (the one you passed to
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000207\code{lin2adpcm}) along to the decoder, not the final state (as returned by
Guido van Rossum470be141995-03-17 16:07:09 +0000208the coder). If you want to use \code{struct} to store the state in
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000209binary you can code the first element (the predicted value) in 16 bits
210and the second (the delta index) in 8.
211
212The ADPCM coders have never been tried against other ADPCM coders,
Guido van Rossum470be141995-03-17 16:07:09 +0000213only against themselves. It could well be that I misinterpreted the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000214standards in which case they will not be interoperable with the
215respective standards.
216
217The \code{find...} routines might look a bit funny at first sight.
Guido van Rossum470be141995-03-17 16:07:09 +0000218They are primarily meant to do echo cancellation. A reasonably
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000219fast way to do this is to pick the most energetic piece of the output
220sample, locate that in the input sample and subtract the whole output
221sample from the input sample:
222\bcode\begin{verbatim}
223def echocancel(outputdata, inputdata):
224 pos = audioop.findmax(outputdata, 800) # one tenth second
225 out_test = outputdata[pos*2:]
226 in_test = inputdata[pos*2:]
227 ipos, factor = audioop.findfit(in_test, out_test)
228 # Optional (for better cancellation):
229 # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],
230 # out_test)
231 prefill = '\0'*(pos+ipos)*2
232 postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata))
233 outputdata = prefill + audioop.mul(outputdata,2,-factor) + postfill
234 return audioop.add(inputdata, outputdata, 2)
235\end{verbatim}\ecode