blob: 03e074d08d991a26bebfec4151df9f94c477a049 [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in module \sectcode{audioop}}
2\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 Rossum6bb1adc1995-03-13 10:03:32 +000022This function returns a fragment which is the addition of the two samples
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000023passed as parameters. \var{width} is the sample width in bytes, either
24\code{1}, \code{2} or \code{4}. Both fragments should have the same length.
25\end{funcdesc}
26
27\begin{funcdesc}{adpcm2lin}{adpcmfragment\, width\, state}
28This routine decodes an Intel/DVI ADPCM coded fragment to a linear
29fragment. See the description of \code{lin2adpcm} for details on ADPCM
30coding. The routine returns a tuple
31\code{(\var{sample}, \var{newstate})}
32where the sample has the width specified in \var{width}.
33\end{funcdesc}
34
35\begin{funcdesc}{adpcm32lin}{adpcmfragment\, width\, state}
36This routine decodes an alternative 3-bit ADPCM code. See
37\code{lin2adpcm3} for details.
38\end{funcdesc}
39
40\begin{funcdesc}{avg}{fragment\, width}
41This function returns the average over all samples in the fragment.
42\end{funcdesc}
43
44\begin{funcdesc}{avgpp}{fragment\, width}
45This function returns the average peak-peak value over all samples in
Guido van Rossum16d6e711994-08-08 12:30:22 +000046the fragment. No filtering is done, so the usefulness of this routine
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000047is questionable.
48\end{funcdesc}
49
50\begin{funcdesc}{bias}{fragment\, width\, bias}
51This function returns a fragment that is the original fragment with a
52bias added to each sample.
53\end{funcdesc}
54
55\begin{funcdesc}{cross}{fragment\, width}
56This function returns the number of zero crossings in the fragment
57passed as an argument.
58\end{funcdesc}
59
60\begin{funcdesc}{findfactor}{fragment\, reference}
61This routine (which only accepts 2-byte sample fragments) calculates a
62factor \var{F} such that \code{rms(add(fragment, mul(reference, -F)))}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000063is minimal, i.e.\ it calculates the factor with which you should
64multiply \var{reference} to make it match as well as possible to
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000065\var{fragment}. The fragments should be the same size.
66
67The time taken by this routine is proportional to \code{len(fragment)}.
68\end{funcdesc}
69
70\begin{funcdesc}{findfit}{fragment\, reference}
71This routine (which only accepts 2-byte sample fragments) tries to
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000072match \var{reference} as well as possible to a portion of
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000073\var{fragment} (which should be the longer fragment). It
74(conceptually) does this by taking slices out of \var{fragment}, using
75\code{findfactor} to compute the best match, and minimizing the
76result.
Guido van Rossum16d6e711994-08-08 12:30:22 +000077It returns a tuple \code{(\var{offset}, \var{factor})} with \var{offset} the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000078(integer) offset into \var{fragment} where the optimal match started
Guido van Rossum16d6e711994-08-08 12:30:22 +000079and \var{factor} the floating-point factor as per \code{findfactor}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000080\end{funcdesc}
81
82\begin{funcdesc}{findmax}{fragment\, length}
83This routine (which only accepts 2-byte sample fragments) searches
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000084\var{fragment} for a slice of length \var{length} samples (not bytes!)\
85with maximum energy, i.e.\ it returns \var{i} for which
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000086\code{rms(fragment[i*2:(i+length)*2])} is maximal.
87
88The routine takes time proportional to \code{len(fragment)}.
89\end{funcdesc}
90
91\begin{funcdesc}{getsample}{fragment\, width\, index}
92This function returns the value of sample \var{index} from the
93fragment.
94\end{funcdesc}
95
96\begin{funcdesc}{lin2lin}{fragment\, width\, newwidth}
97This function converts samples between 1-, 2- and 4-byte formats.
98\end{funcdesc}
99
100\begin{funcdesc}{lin2adpcm}{fragment\, width\, state}
101This function converts samples to 4 bit Intel/DVI ADPCM encoding.
102ADPCM coding is an adaptive coding scheme, whereby each 4 bit number
103is the difference between one sample and the next, divided by a
Guido van Rossum16d6e711994-08-08 12:30:22 +0000104(varying) step. The Intel/DVI ADPCM algorithm has been selected for
105use by the IMA, so it may well become a standard.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000106
107\code{State} is a tuple containing the state of the coder. The coder
108returns a tuple \code{(\var{adpcmfrag}, \var{newstate})}, and the
109\var{newstate} should be passed to the next call of lin2adpcm. In the
110initial call \code{None} can be passed as the state. \var{adpcmfrag} is
111the ADPCM coded fragment packed 2 4-bit values per byte.
112\end{funcdesc}
113
114\begin{funcdesc}{lin2adpcm3}{fragment\, width\, state}
115This is an alternative ADPCM coder that uses only 3 bits per sample.
116It is not compatible with the Intel/DVI ADPCM coder and its output is
117not packed (due to laziness on the side of the author). Its use is
118discouraged.
119\end{funcdesc}
120
121\begin{funcdesc}{lin2ulaw}{fragment\, width}
122This function converts samples in the audio fragment to U-LAW encoding
Guido van Rossum16d6e711994-08-08 12:30:22 +0000123and returns this as a Python string. U-LAW is an audio encoding format
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000124whereby you get a dynamic range of about 14 bits using only 8 bit
125samples. It is used by the Sun audio hardware, among others.
126\end{funcdesc}
127
128\begin{funcdesc}{minmax}{fragment\, width}
129This function returns a tuple consisting of the minimum and maximum
130values of all samples in the sound fragment.
131\end{funcdesc}
132
133\begin{funcdesc}{max}{fragment\, width}
134This function returns the maximum of the {\em absolute value} of all
135samples in a fragment.
136\end{funcdesc}
137
138\begin{funcdesc}{maxpp}{fragment\, width}
139This function returns the maximum peak-peak value in the sound fragment.
140\end{funcdesc}
141
142\begin{funcdesc}{mul}{fragment\, width\, factor}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000143Return a fragment that has all samples in the original framgent
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000144multiplied by the floating-point value \var{factor}. Overflow is
145silently ignored.
146\end{funcdesc}
147
148\begin{funcdesc}{reverse}{fragment\, width}
149This function reverses the samples in a fragment and returns the
150modified fragment.
151\end{funcdesc}
152
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000153\begin{funcdesc}{rms}{fragment\, width\, factor}
154Returns the root-mean-square of the fragment, i.e.
155\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}
169This function converts a stereo fragment to a mono fragment. The left
170channel is multiplied by \var{lfactor} and the right channel by
171\var{rfactor} before adding the two channels to give a mono signal.
172\end{funcdesc}
173
174\begin{funcdesc}{tostereo}{fragment\, width\, lfactor\, rfactor}
175This function generates a stereo fragment from a mono fragment. Each
176pair of samples in the stereo fragment are computed from the mono
177sample, whereby left channel samples are multiplied by \var{lfactor}
178and right channel samples by \var{rfactor}.
179\end{funcdesc}
180
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000181\begin{funcdesc}{ulaw2lin}{fragment\, width}
182This function converts sound fragments in ULAW encoding to linearly
183encoded sound fragments. ULAW encoding always uses 8 bits samples, so
184\var{width} refers only to the sample width of the output fragment here.
185\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 Rossum5fdeeea1994-01-02 01:22:07 +0000205you should not only transmit the data but also the state. Note that
206you 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 Rossum5fdeeea1994-01-02 01:22:07 +0000208the coder). If you want to use \code{struct} to store the state in
209binary 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,
213only against themselves. It could well be that I misinterpreted the
214standards 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.
218They are primarily meant for doing echo cancellation. A reasonably
219fast 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