blob: 06b1c27616f5777ac44fe4044f5f9c74485ca157 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{audioop} ---
Fred Drakeffbe6871999-04-22 21:23:22 +00002 Manipulate raw audio data}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakeffbe6871999-04-22 21:23:22 +00004\declaremodule{builtin}{audioop}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Manipulate raw audio data.}
6
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007
Fred Drakefc576191998-04-04 07:15:02 +00008The \module{audioop} module contains some useful operations on sound
9fragments. It operates on sound fragments consisting of signed
10integer samples 8, 16 or 32 bits wide, stored in Python strings. This
Fred Drakeffbe6871999-04-22 21:23:22 +000011is the same format as used by the \refmodule{al} and \refmodule{sunaudiodev}
Fred Drakefc576191998-04-04 07:15:02 +000012modules. All scalar items are integers, unless specified otherwise.
13
14% This para is mostly here to provide an excuse for the index entries...
15This module provides support for u-LAW and Intel/DVI ADPCM encodings.
16\index{Intel/DVI ADPCM}
17\index{ADPCM, Intel/DVI}
18\index{u-LAW}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000019
20A few of the more complicated operations only take 16-bit samples,
Fred Drakefc576191998-04-04 07:15:02 +000021otherwise the sample size (in bytes) is always a parameter of the
22operation.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000023
24The module defines the following variables and functions:
25
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000026\begin{excdesc}{error}
27This exception is raised on all errors, such as unknown number of bytes
28per sample, etc.
29\end{excdesc}
30
Fred Drakecce10901998-03-17 06:33:25 +000031\begin{funcdesc}{add}{fragment1, fragment2, width}
Guido van Rossum470be141995-03-17 16:07:09 +000032Return a fragment which is the addition of the two samples passed as
33parameters. \var{width} is the sample width in bytes, either
34\code{1}, \code{2} or \code{4}. Both fragments should have the same
35length.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000036\end{funcdesc}
37
Fred Drakecce10901998-03-17 06:33:25 +000038\begin{funcdesc}{adpcm2lin}{adpcmfragment, width, state}
Guido van Rossum470be141995-03-17 16:07:09 +000039Decode an Intel/DVI ADPCM coded fragment to a linear fragment. See
Fred Drakefc576191998-04-04 07:15:02 +000040the description of \function{lin2adpcm()} for details on ADPCM coding.
Guido van Rossum470be141995-03-17 16:07:09 +000041Return a tuple \code{(\var{sample}, \var{newstate})} where the sample
42has the width specified in \var{width}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000043\end{funcdesc}
44
Fred Drakecce10901998-03-17 06:33:25 +000045\begin{funcdesc}{adpcm32lin}{adpcmfragment, width, state}
Fred Drakefc576191998-04-04 07:15:02 +000046Decode an alternative 3-bit ADPCM code. See \function{lin2adpcm3()}
47for details.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000048\end{funcdesc}
49
Fred Drakecce10901998-03-17 06:33:25 +000050\begin{funcdesc}{avg}{fragment, width}
Guido van Rossum470be141995-03-17 16:07:09 +000051Return the average over all samples in the fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000052\end{funcdesc}
53
Fred Drakecce10901998-03-17 06:33:25 +000054\begin{funcdesc}{avgpp}{fragment, width}
Guido van Rossum470be141995-03-17 16:07:09 +000055Return the average peak-peak value over all samples in the fragment.
56No filtering is done, so the usefulness of this routine is
57questionable.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000058\end{funcdesc}
59
Fred Drakecce10901998-03-17 06:33:25 +000060\begin{funcdesc}{bias}{fragment, width, bias}
Guido van Rossum470be141995-03-17 16:07:09 +000061Return a fragment that is the original fragment with a bias added to
62each sample.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000063\end{funcdesc}
64
Fred Drakecce10901998-03-17 06:33:25 +000065\begin{funcdesc}{cross}{fragment, width}
Guido van Rossum470be141995-03-17 16:07:09 +000066Return the number of zero crossings in the fragment passed as an
67argument.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000068\end{funcdesc}
69
Fred Drakecce10901998-03-17 06:33:25 +000070\begin{funcdesc}{findfactor}{fragment, reference}
Guido van Rossum470be141995-03-17 16:07:09 +000071Return a factor \var{F} such that
Fred Drakefc576191998-04-04 07:15:02 +000072\code{rms(add(\var{fragment}, mul(\var{reference}, -\var{F})))} is
73minimal, i.e., return the factor with which you should multiply
74\var{reference} to make it match as well as possible to
75\var{fragment}. The fragments should both contain 2-byte samples.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000076
Fred Drakefc576191998-04-04 07:15:02 +000077The time taken by this routine is proportional to
78\code{len(\var{fragment})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079\end{funcdesc}
80
Fred Drakecce10901998-03-17 06:33:25 +000081\begin{funcdesc}{findfit}{fragment, reference}
Guido van Rossum470be141995-03-17 16:07:09 +000082Try to match \var{reference} as well as possible to a portion of
83\var{fragment} (which should be the longer fragment). This is
84(conceptually) done by taking slices out of \var{fragment}, using
Fred Drakefc576191998-04-04 07:15:02 +000085\function{findfactor()} to compute the best match, and minimizing the
Guido van Rossum470be141995-03-17 16:07:09 +000086result. The fragments should both contain 2-byte samples. Return a
87tuple \code{(\var{offset}, \var{factor})} where \var{offset} is the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000088(integer) offset into \var{fragment} where the optimal match started
Guido van Rossum470be141995-03-17 16:07:09 +000089and \var{factor} is the (floating-point) factor as per
Fred Drakefc576191998-04-04 07:15:02 +000090\function{findfactor()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000091\end{funcdesc}
92
Fred Drakecce10901998-03-17 06:33:25 +000093\begin{funcdesc}{findmax}{fragment, length}
Guido van Rossum470be141995-03-17 16:07:09 +000094Search \var{fragment} for a slice of length \var{length} samples (not
95bytes!)\ with maximum energy, i.e., return \var{i} for which
96\code{rms(fragment[i*2:(i+length)*2])} is maximal. The fragments
97should both contain 2-byte samples.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000098
Fred Drakefc576191998-04-04 07:15:02 +000099The routine takes time proportional to \code{len(\var{fragment})}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000100\end{funcdesc}
101
Fred Drakecce10901998-03-17 06:33:25 +0000102\begin{funcdesc}{getsample}{fragment, width, index}
Guido van Rossum470be141995-03-17 16:07:09 +0000103Return the value of sample \var{index} from the fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000104\end{funcdesc}
105
Fred Drakecce10901998-03-17 06:33:25 +0000106\begin{funcdesc}{lin2lin}{fragment, width, newwidth}
Guido van Rossum470be141995-03-17 16:07:09 +0000107Convert samples between 1-, 2- and 4-byte formats.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000108\end{funcdesc}
109
Fred Drakecce10901998-03-17 06:33:25 +0000110\begin{funcdesc}{lin2adpcm}{fragment, width, state}
Guido van Rossum470be141995-03-17 16:07:09 +0000111Convert samples to 4 bit Intel/DVI ADPCM encoding. ADPCM coding is an
112adaptive coding scheme, whereby each 4 bit number is the difference
113between one sample and the next, divided by a (varying) step. The
114Intel/DVI ADPCM algorithm has been selected for use by the IMA, so it
115may well become a standard.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000116
Fred Drakefc576191998-04-04 07:15:02 +0000117\var{state} is a tuple containing the state of the coder. The coder
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000118returns a tuple \code{(\var{adpcmfrag}, \var{newstate})}, and the
Fred Drakefc576191998-04-04 07:15:02 +0000119\var{newstate} should be passed to the next call of
120\function{lin2adpcm()}. In the initial call, \code{None} can be
121passed as the state. \var{adpcmfrag} is the ADPCM coded fragment
122packed 2 4-bit values per byte.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000123\end{funcdesc}
124
Fred Drakecce10901998-03-17 06:33:25 +0000125\begin{funcdesc}{lin2adpcm3}{fragment, width, state}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000126This is an alternative ADPCM coder that uses only 3 bits per sample.
127It is not compatible with the Intel/DVI ADPCM coder and its output is
Guido van Rossum470be141995-03-17 16:07:09 +0000128not packed (due to laziness on the side of the author). Its use is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000129discouraged.
130\end{funcdesc}
131
Fred Drakecce10901998-03-17 06:33:25 +0000132\begin{funcdesc}{lin2ulaw}{fragment, width}
Fred Drakefc576191998-04-04 07:15:02 +0000133Convert samples in the audio fragment to u-LAW encoding and return
134this as a Python string. u-LAW is an audio encoding format whereby
Guido van Rossum470be141995-03-17 16:07:09 +0000135you get a dynamic range of about 14 bits using only 8 bit samples. It
136is used by the Sun audio hardware, among others.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000137\end{funcdesc}
138
Fred Drakecce10901998-03-17 06:33:25 +0000139\begin{funcdesc}{minmax}{fragment, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000140Return a tuple consisting of the minimum and maximum values of all
141samples in the sound fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000142\end{funcdesc}
143
Fred Drakecce10901998-03-17 06:33:25 +0000144\begin{funcdesc}{max}{fragment, width}
Fred Drakeaf8a0151998-01-14 14:51:31 +0000145Return the maximum of the \emph{absolute value} of all samples in a
Guido van Rossum470be141995-03-17 16:07:09 +0000146fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000147\end{funcdesc}
148
Fred Drakecce10901998-03-17 06:33:25 +0000149\begin{funcdesc}{maxpp}{fragment, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000150Return the maximum peak-peak value in the sound fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000151\end{funcdesc}
152
Fred Drakecce10901998-03-17 06:33:25 +0000153\begin{funcdesc}{mul}{fragment, width, factor}
Thomas Woutersf8316632000-07-16 19:01:10 +0000154Return a fragment that has all samples in the original fragment
Guido van Rossum470be141995-03-17 16:07:09 +0000155multiplied by the floating-point value \var{factor}. Overflow is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000156silently ignored.
157\end{funcdesc}
158
Fred Drakefc576191998-04-04 07:15:02 +0000159\begin{funcdesc}{ratecv}{fragment, width, nchannels, inrate, outrate,
160 state\optional{, weightA\optional{, weightB}}}
Guido van Rossum6fb6f101997-02-14 15:59:49 +0000161Convert the frame rate of the input fragment.
162
Fred Drakefc576191998-04-04 07:15:02 +0000163\var{state} is a tuple containing the state of the converter. The
Thomas Woutersf8316632000-07-16 19:01:10 +0000164converter returns a tuple \code{(\var{newfragment}, \var{newstate})},
Fred Drakefc576191998-04-04 07:15:02 +0000165and \var{newstate} should be passed to the next call of
Tim Petersc7cb6922001-12-06 23:16:09 +0000166\function{ratecv()}. The initial call should pass \code{None}
167as the state.
Guido van Rossum6fb6f101997-02-14 15:59:49 +0000168
Fred Drakefc576191998-04-04 07:15:02 +0000169The \var{weightA} and \var{weightB} arguments are parameters for a
Fred Drake4aa4f301999-04-23 17:30:40 +0000170simple digital filter and default to \code{1} and \code{0} respectively.
Guido van Rossum6fb6f101997-02-14 15:59:49 +0000171\end{funcdesc}
172
Fred Drakecce10901998-03-17 06:33:25 +0000173\begin{funcdesc}{reverse}{fragment, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000174Reverse the samples in a fragment and returns the modified fragment.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000175\end{funcdesc}
176
Fred Drakecce10901998-03-17 06:33:25 +0000177\begin{funcdesc}{rms}{fragment, width}
Guido van Rossum470be141995-03-17 16:07:09 +0000178Return the root-mean-square of the fragment, i.e.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000179\begin{displaymath}
180\catcode`_=8
181\sqrt{\frac{\sum{{S_{i}}^{2}}}{n}}
182\end{displaymath}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000183This is a measure of the power in an audio signal.
184\end{funcdesc}
185
Fred Drakecce10901998-03-17 06:33:25 +0000186\begin{funcdesc}{tomono}{fragment, width, lfactor, rfactor}
Guido van Rossum470be141995-03-17 16:07:09 +0000187Convert a stereo fragment to a mono fragment. The left channel is
188multiplied by \var{lfactor} and the right channel by \var{rfactor}
189before adding the two channels to give a mono signal.
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000190\end{funcdesc}
191
Fred Drakecce10901998-03-17 06:33:25 +0000192\begin{funcdesc}{tostereo}{fragment, width, lfactor, rfactor}
Guido van Rossum470be141995-03-17 16:07:09 +0000193Generate a stereo fragment from a mono fragment. Each pair of samples
194in the stereo fragment are computed from the mono sample, whereby left
195channel samples are multiplied by \var{lfactor} and right channel
196samples by \var{rfactor}.
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000197\end{funcdesc}
198
Fred Drakecce10901998-03-17 06:33:25 +0000199\begin{funcdesc}{ulaw2lin}{fragment, width}
Fred Drakefc576191998-04-04 07:15:02 +0000200Convert sound fragments in u-LAW encoding to linearly encoded sound
201fragments. u-LAW encoding always uses 8 bits samples, so \var{width}
Guido van Rossum470be141995-03-17 16:07:09 +0000202refers only to the sample width of the output fragment here.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000203\end{funcdesc}
204
Fred Drakefc576191998-04-04 07:15:02 +0000205Note that operations such as \function{mul()} or \function{max()} make
206no distinction between mono and stereo fragments, i.e.\ all samples
207are treated equal. If this is a problem the stereo fragment should be
208split into two mono fragments first and recombined later. Here is an
209example of how to do that:
210
Fred Drake19479911998-02-13 06:58:54 +0000211\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000212def mul_stereo(sample, width, lfactor, rfactor):
213 lsample = audioop.tomono(sample, width, 1, 0)
214 rsample = audioop.tomono(sample, width, 0, 1)
215 lsample = audioop.mul(sample, width, lfactor)
216 rsample = audioop.mul(sample, width, rfactor)
217 lsample = audioop.tostereo(lsample, width, 1, 0)
218 rsample = audioop.tostereo(rsample, width, 0, 1)
219 return audioop.add(lsample, rsample, width)
Fred Drake19479911998-02-13 06:58:54 +0000220\end{verbatim}
Fred Drakefc576191998-04-04 07:15:02 +0000221
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000222If you use the ADPCM coder to build network packets and you want your
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000223protocol to be stateless (i.e.\ to be able to tolerate packet loss)
Guido van Rossum470be141995-03-17 16:07:09 +0000224you should not only transmit the data but also the state. Note that
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000225you should send the \var{initial} state (the one you passed to
Fred Drakefc576191998-04-04 07:15:02 +0000226\function{lin2adpcm()}) along to the decoder, not the final state (as
227returned by the coder). If you want to use \function{struct.struct()}
228to store the state in binary you can code the first element (the
229predicted value) in 16 bits and the second (the delta index) in 8.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000230
231The ADPCM coders have never been tried against other ADPCM coders,
Guido van Rossum470be141995-03-17 16:07:09 +0000232only against themselves. It could well be that I misinterpreted the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000233standards in which case they will not be interoperable with the
234respective standards.
235
Fred Drakefc576191998-04-04 07:15:02 +0000236The \function{find*()} routines might look a bit funny at first sight.
Guido van Rossum470be141995-03-17 16:07:09 +0000237They are primarily meant to do echo cancellation. A reasonably
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000238fast way to do this is to pick the most energetic piece of the output
239sample, locate that in the input sample and subtract the whole output
240sample from the input sample:
Fred Drakefc576191998-04-04 07:15:02 +0000241
Fred Drake19479911998-02-13 06:58:54 +0000242\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000243def echocancel(outputdata, inputdata):
244 pos = audioop.findmax(outputdata, 800) # one tenth second
245 out_test = outputdata[pos*2:]
246 in_test = inputdata[pos*2:]
247 ipos, factor = audioop.findfit(in_test, out_test)
248 # Optional (for better cancellation):
249 # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],
250 # out_test)
251 prefill = '\0'*(pos+ipos)*2
252 postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata))
253 outputdata = prefill + audioop.mul(outputdata,2,-factor) + postfill
254 return audioop.add(inputdata, outputdata, 2)
Fred Drake19479911998-02-13 06:58:54 +0000255\end{verbatim}