Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / python / lib / module-audioop.html
CommitLineData
86530b38
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="module-imageop.html" />
13<link rel="prev" href="mmedia.html" />
14<link rel="parent" href="mmedia.html" />
15<link rel="next" href="module-imageop.html" />
16<meta name='aesop' content='information' />
17<title>14.1 audioop -- Manipulate raw audio data</title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="14. Multimedia Services"
25 href="mmedia.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="14. Multimedia Services"
28 href="mmedia.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="14.2 imageop "
31 href="module-imageop.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="mmedia.html">14. Multimedia Services</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="mmedia.html">14. Multimedia Services</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="module-imageop.html">14.2 imageop </A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION0016100000000000000000">
5614.1 <tt class="module">audioop</tt> --
57 Manipulate raw audio data</A>
58</H1>
59
60<P>
61<A NAME="module-audioop"></A>
62
63<P>
64The <tt class="module">audioop</tt> module contains some useful operations on sound
65fragments. It operates on sound fragments consisting of signed
66integer samples 8, 16 or 32 bits wide, stored in Python strings. This
67is the same format as used by the <tt class="module"><a href="module-al.html">al</a></tt> and <tt class="module"><a href="module-sunaudiodev.html">sunaudiodev</a></tt>
68modules. All scalar items are integers, unless specified otherwise.
69
70<P>
71This module provides support for u-LAW and Intel/DVI ADPCM encodings.
72<a id='l2h-4620' xml:id='l2h-4620'></a>
73
74<P>
75A few of the more complicated operations only take 16-bit samples,
76otherwise the sample size (in bytes) is always a parameter of the
77operation.
78
79<P>
80The module defines the following variables and functions:
81
82<P>
83<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-4594' xml:id='l2h-4594' class="exception">error</tt></b></dt>
84<dd>
85This exception is raised on all errors, such as unknown number of bytes
86per sample, etc.
87</dd></dl>
88
89<P>
90<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
91 <td><nobr><b><tt id='l2h-4595' xml:id='l2h-4595' class="function">add</tt></b>(</nobr></td>
92 <td><var>fragment1, fragment2, width</var>)</td></tr></table></dt>
93<dd>
94Return a fragment which is the addition of the two samples passed as
95parameters. <var>width</var> is the sample width in bytes, either
96<code>1</code>, <code>2</code> or <code>4</code>. Both fragments should have the same
97length.
98</dl>
99
100<P>
101<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
102 <td><nobr><b><tt id='l2h-4596' xml:id='l2h-4596' class="function">adpcm2lin</tt></b>(</nobr></td>
103 <td><var>adpcmfragment, width, state</var>)</td></tr></table></dt>
104<dd>
105Decode an Intel/DVI ADPCM coded fragment to a linear fragment. See
106the description of <tt class="function">lin2adpcm()</tt> for details on ADPCM coding.
107Return a tuple <code>(<var>sample</var>, <var>newstate</var>)</code> where the sample
108has the width specified in <var>width</var>.
109</dl>
110
111<P>
112<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
113 <td><nobr><b><tt id='l2h-4597' xml:id='l2h-4597' class="function">adpcm32lin</tt></b>(</nobr></td>
114 <td><var>adpcmfragment, width, state</var>)</td></tr></table></dt>
115<dd>
116Decode an alternative 3-bit ADPCM code. See <tt class="function">lin2adpcm3()</tt>
117for details.
118</dl>
119
120<P>
121<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
122 <td><nobr><b><tt id='l2h-4598' xml:id='l2h-4598' class="function">avg</tt></b>(</nobr></td>
123 <td><var>fragment, width</var>)</td></tr></table></dt>
124<dd>
125Return the average over all samples in the fragment.
126</dl>
127
128<P>
129<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
130 <td><nobr><b><tt id='l2h-4599' xml:id='l2h-4599' class="function">avgpp</tt></b>(</nobr></td>
131 <td><var>fragment, width</var>)</td></tr></table></dt>
132<dd>
133Return the average peak-peak value over all samples in the fragment.
134No filtering is done, so the usefulness of this routine is
135questionable.
136</dl>
137
138<P>
139<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
140 <td><nobr><b><tt id='l2h-4600' xml:id='l2h-4600' class="function">bias</tt></b>(</nobr></td>
141 <td><var>fragment, width, bias</var>)</td></tr></table></dt>
142<dd>
143Return a fragment that is the original fragment with a bias added to
144each sample.
145</dl>
146
147<P>
148<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
149 <td><nobr><b><tt id='l2h-4601' xml:id='l2h-4601' class="function">cross</tt></b>(</nobr></td>
150 <td><var>fragment, width</var>)</td></tr></table></dt>
151<dd>
152Return the number of zero crossings in the fragment passed as an
153argument.
154</dl>
155
156<P>
157<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
158 <td><nobr><b><tt id='l2h-4602' xml:id='l2h-4602' class="function">findfactor</tt></b>(</nobr></td>
159 <td><var>fragment, reference</var>)</td></tr></table></dt>
160<dd>
161Return a factor <var>F</var> such that
162<code>rms(add(<var>fragment</var>, mul(<var>reference</var>, -<var>F</var>)))</code> is
163minimal, i.e., return the factor with which you should multiply
164<var>reference</var> to make it match as well as possible to
165<var>fragment</var>. The fragments should both contain 2-byte samples.
166
167<P>
168The time taken by this routine is proportional to
169<code>len(<var>fragment</var>)</code>.
170</dl>
171
172<P>
173<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
174 <td><nobr><b><tt id='l2h-4603' xml:id='l2h-4603' class="function">findfit</tt></b>(</nobr></td>
175 <td><var>fragment, reference</var>)</td></tr></table></dt>
176<dd>
177Try to match <var>reference</var> as well as possible to a portion of
178<var>fragment</var> (which should be the longer fragment). This is
179(conceptually) done by taking slices out of <var>fragment</var>, using
180<tt class="function">findfactor()</tt> to compute the best match, and minimizing the
181result. The fragments should both contain 2-byte samples. Return a
182tuple <code>(<var>offset</var>, <var>factor</var>)</code> where <var>offset</var> is the
183(integer) offset into <var>fragment</var> where the optimal match started
184and <var>factor</var> is the (floating-point) factor as per
185<tt class="function">findfactor()</tt>.
186</dl>
187
188<P>
189<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
190 <td><nobr><b><tt id='l2h-4604' xml:id='l2h-4604' class="function">findmax</tt></b>(</nobr></td>
191 <td><var>fragment, length</var>)</td></tr></table></dt>
192<dd>
193Search <var>fragment</var> for a slice of length <var>length</var> samples (not
194bytes!) with maximum energy, i.e., return <var>i</var> for which
195<code>rms(fragment[i*2:(i+length)*2])</code> is maximal. The fragments
196should both contain 2-byte samples.
197
198<P>
199The routine takes time proportional to <code>len(<var>fragment</var>)</code>.
200</dl>
201
202<P>
203<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
204 <td><nobr><b><tt id='l2h-4605' xml:id='l2h-4605' class="function">getsample</tt></b>(</nobr></td>
205 <td><var>fragment, width, index</var>)</td></tr></table></dt>
206<dd>
207Return the value of sample <var>index</var> from the fragment.
208</dl>
209
210<P>
211<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
212 <td><nobr><b><tt id='l2h-4606' xml:id='l2h-4606' class="function">lin2lin</tt></b>(</nobr></td>
213 <td><var>fragment, width, newwidth</var>)</td></tr></table></dt>
214<dd>
215Convert samples between 1-, 2- and 4-byte formats.
216</dl>
217
218<P>
219<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
220 <td><nobr><b><tt id='l2h-4607' xml:id='l2h-4607' class="function">lin2adpcm</tt></b>(</nobr></td>
221 <td><var>fragment, width, state</var>)</td></tr></table></dt>
222<dd>
223Convert samples to 4 bit Intel/DVI ADPCM encoding. ADPCM coding is an
224adaptive coding scheme, whereby each 4 bit number is the difference
225between one sample and the next, divided by a (varying) step. The
226Intel/DVI ADPCM algorithm has been selected for use by the IMA, so it
227may well become a standard.
228
229<P>
230<var>state</var> is a tuple containing the state of the coder. The coder
231returns a tuple <code>(<var>adpcmfrag</var>, <var>newstate</var>)</code>, and the
232<var>newstate</var> should be passed to the next call of
233<tt class="function">lin2adpcm()</tt>. In the initial call, <code>None</code> can be
234passed as the state. <var>adpcmfrag</var> is the ADPCM coded fragment
235packed 2 4-bit values per byte.
236</dl>
237
238<P>
239<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
240 <td><nobr><b><tt id='l2h-4608' xml:id='l2h-4608' class="function">lin2adpcm3</tt></b>(</nobr></td>
241 <td><var>fragment, width, state</var>)</td></tr></table></dt>
242<dd>
243This is an alternative ADPCM coder that uses only 3 bits per sample.
244It is not compatible with the Intel/DVI ADPCM coder and its output is
245not packed (due to laziness on the side of the author). Its use is
246discouraged.
247</dl>
248
249<P>
250<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
251 <td><nobr><b><tt id='l2h-4609' xml:id='l2h-4609' class="function">lin2ulaw</tt></b>(</nobr></td>
252 <td><var>fragment, width</var>)</td></tr></table></dt>
253<dd>
254Convert samples in the audio fragment to u-LAW encoding and return
255this as a Python string. u-LAW is an audio encoding format whereby
256you get a dynamic range of about 14 bits using only 8 bit samples. It
257is used by the Sun audio hardware, among others.
258</dl>
259
260<P>
261<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
262 <td><nobr><b><tt id='l2h-4610' xml:id='l2h-4610' class="function">minmax</tt></b>(</nobr></td>
263 <td><var>fragment, width</var>)</td></tr></table></dt>
264<dd>
265Return a tuple consisting of the minimum and maximum values of all
266samples in the sound fragment.
267</dl>
268
269<P>
270<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
271 <td><nobr><b><tt id='l2h-4611' xml:id='l2h-4611' class="function">max</tt></b>(</nobr></td>
272 <td><var>fragment, width</var>)</td></tr></table></dt>
273<dd>
274Return the maximum of the <em>absolute value</em> of all samples in a
275fragment.
276</dl>
277
278<P>
279<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
280 <td><nobr><b><tt id='l2h-4612' xml:id='l2h-4612' class="function">maxpp</tt></b>(</nobr></td>
281 <td><var>fragment, width</var>)</td></tr></table></dt>
282<dd>
283Return the maximum peak-peak value in the sound fragment.
284</dl>
285
286<P>
287<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
288 <td><nobr><b><tt id='l2h-4613' xml:id='l2h-4613' class="function">mul</tt></b>(</nobr></td>
289 <td><var>fragment, width, factor</var>)</td></tr></table></dt>
290<dd>
291Return a fragment that has all samples in the original fragment
292multiplied by the floating-point value <var>factor</var>. Overflow is
293silently ignored.
294</dl>
295
296<P>
297<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
298 <td><nobr><b><tt id='l2h-4614' xml:id='l2h-4614' class="function">ratecv</tt></b>(</nobr></td>
299 <td><var>fragment, width, nchannels, inrate, outrate,
300 state</var><big>[</big><var>, weightA</var><big>[</big><var>, weightB</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
301<dd>
302Convert the frame rate of the input fragment.
303
304<P>
305<var>state</var> is a tuple containing the state of the converter. The
306converter returns a tuple <code>(<var>newfragment</var>, <var>newstate</var>)</code>,
307and <var>newstate</var> should be passed to the next call of
308<tt class="function">ratecv()</tt>. The initial call should pass <code>None</code>
309as the state.
310
311<P>
312The <var>weightA</var> and <var>weightB</var> arguments are parameters for a
313simple digital filter and default to <code>1</code> and <code>0</code> respectively.
314</dl>
315
316<P>
317<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
318 <td><nobr><b><tt id='l2h-4615' xml:id='l2h-4615' class="function">reverse</tt></b>(</nobr></td>
319 <td><var>fragment, width</var>)</td></tr></table></dt>
320<dd>
321Reverse the samples in a fragment and returns the modified fragment.
322</dl>
323
324<P>
325<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
326 <td><nobr><b><tt id='l2h-4616' xml:id='l2h-4616' class="function">rms</tt></b>(</nobr></td>
327 <td><var>fragment, width</var>)</td></tr></table></dt>
328<dd>
329Return the root-mean-square of the fragment, i.e.
330<BR><P></P>
331<DIV ALIGN="CENTER" CLASS="mathdisplay">
332<!-- MATH
333 \begin{displaymath}
334\catcode`_=8
335\sqrt{\frac{\sum{{S_{i}}^{2}}}{n}}
336\end{displaymath}
337 -->
338
339<IMG
340 WIDTH="61" HEIGHT="55" BORDER="0"
341 SRC="img3.png"
342 ALT="\begin{displaymath}
343\catcode\lq _=8
344\sqrt{\frac{\sum{{S_{i}}^{2}}}{n}}
345\end{displaymath}">
346</DIV>
347<BR CLEAR="ALL">
348<P></P>
349This is a measure of the power in an audio signal.
350</dl>
351
352<P>
353<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
354 <td><nobr><b><tt id='l2h-4617' xml:id='l2h-4617' class="function">tomono</tt></b>(</nobr></td>
355 <td><var>fragment, width, lfactor, rfactor</var>)</td></tr></table></dt>
356<dd>
357Convert a stereo fragment to a mono fragment. The left channel is
358multiplied by <var>lfactor</var> and the right channel by <var>rfactor</var>
359before adding the two channels to give a mono signal.
360</dl>
361
362<P>
363<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
364 <td><nobr><b><tt id='l2h-4618' xml:id='l2h-4618' class="function">tostereo</tt></b>(</nobr></td>
365 <td><var>fragment, width, lfactor, rfactor</var>)</td></tr></table></dt>
366<dd>
367Generate a stereo fragment from a mono fragment. Each pair of samples
368in the stereo fragment are computed from the mono sample, whereby left
369channel samples are multiplied by <var>lfactor</var> and right channel
370samples by <var>rfactor</var>.
371</dl>
372
373<P>
374<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
375 <td><nobr><b><tt id='l2h-4619' xml:id='l2h-4619' class="function">ulaw2lin</tt></b>(</nobr></td>
376 <td><var>fragment, width</var>)</td></tr></table></dt>
377<dd>
378Convert sound fragments in u-LAW encoding to linearly encoded sound
379fragments. u-LAW encoding always uses 8 bits samples, so <var>width</var>
380refers only to the sample width of the output fragment here.
381</dl>
382
383<P>
384Note that operations such as <tt class="function">mul()</tt> or <tt class="function">max()</tt> make
385no distinction between mono and stereo fragments, i.e. all samples
386are treated equal. If this is a problem the stereo fragment should be
387split into two mono fragments first and recombined later. Here is an
388example of how to do that:
389
390<P>
391<div class="verbatim"><pre>
392def mul_stereo(sample, width, lfactor, rfactor):
393 lsample = audioop.tomono(sample, width, 1, 0)
394 rsample = audioop.tomono(sample, width, 0, 1)
395 lsample = audioop.mul(sample, width, lfactor)
396 rsample = audioop.mul(sample, width, rfactor)
397 lsample = audioop.tostereo(lsample, width, 1, 0)
398 rsample = audioop.tostereo(rsample, width, 0, 1)
399 return audioop.add(lsample, rsample, width)
400</pre></div>
401
402<P>
403If you use the ADPCM coder to build network packets and you want your
404protocol to be stateless (i.e. to be able to tolerate packet loss)
405you should not only transmit the data but also the state. Note that
406you should send the <var>initial</var> state (the one you passed to
407<tt class="function">lin2adpcm()</tt>) along to the decoder, not the final state (as
408returned by the coder). If you want to use <tt class="function">struct.struct()</tt>
409to store the state in binary you can code the first element (the
410predicted value) in 16 bits and the second (the delta index) in 8.
411
412<P>
413The ADPCM coders have never been tried against other ADPCM coders,
414only against themselves. It could well be that I misinterpreted the
415standards in which case they will not be interoperable with the
416respective standards.
417
418<P>
419The <tt class="function">find*()</tt> routines might look a bit funny at first sight.
420They are primarily meant to do echo cancellation. A reasonably
421fast way to do this is to pick the most energetic piece of the output
422sample, locate that in the input sample and subtract the whole output
423sample from the input sample:
424
425<P>
426<div class="verbatim"><pre>
427def echocancel(outputdata, inputdata):
428 pos = audioop.findmax(outputdata, 800) # one tenth second
429 out_test = outputdata[pos*2:]
430 in_test = inputdata[pos*2:]
431 ipos, factor = audioop.findfit(in_test, out_test)
432 # Optional (for better cancellation):
433 # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],
434 # out_test)
435 prefill = '\0'*(pos+ipos)*2
436 postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata))
437 outputdata = prefill + audioop.mul(outputdata,2,-factor) + postfill
438 return audioop.add(inputdata, outputdata, 2)
439</pre></div>
440
441<DIV CLASS="navigation">
442<div class='online-navigation'>
443<p></p><hr />
444<table align="center" width="100%" cellpadding="0" cellspacing="2">
445<tr>
446<td class='online-navigation'><a rel="prev" title="14. Multimedia Services"
447 href="mmedia.html"><img src='../icons/previous.png'
448 border='0' height='32' alt='Previous Page' width='32' /></A></td>
449<td class='online-navigation'><a rel="parent" title="14. Multimedia Services"
450 href="mmedia.html"><img src='../icons/up.png'
451 border='0' height='32' alt='Up One Level' width='32' /></A></td>
452<td class='online-navigation'><a rel="next" title="14.2 imageop "
453 href="module-imageop.html"><img src='../icons/next.png'
454 border='0' height='32' alt='Next Page' width='32' /></A></td>
455<td align="center" width="100%">Python Library Reference</td>
456<td class='online-navigation'><a rel="contents" title="Table of Contents"
457 href="contents.html"><img src='../icons/contents.png'
458 border='0' height='32' alt='Contents' width='32' /></A></td>
459<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
460 border='0' height='32' alt='Module Index' width='32' /></a></td>
461<td class='online-navigation'><a rel="index" title="Index"
462 href="genindex.html"><img src='../icons/index.png'
463 border='0' height='32' alt='Index' width='32' /></A></td>
464</tr></table>
465<div class='online-navigation'>
466<b class="navlabel">Previous:</b>
467<a class="sectref" rel="prev" href="mmedia.html">14. Multimedia Services</A>
468<b class="navlabel">Up:</b>
469<a class="sectref" rel="parent" href="mmedia.html">14. Multimedia Services</A>
470<b class="navlabel">Next:</b>
471<a class="sectref" rel="next" href="module-imageop.html">14.2 imageop </A>
472</div>
473</div>
474<hr />
475<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
476</DIV>
477<!--End of Navigation Panel-->
478<ADDRESS>
479See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
480</ADDRESS>
481</BODY>
482</HTML>