| 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"> |
| 56 | 14.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> |
| 64 | The <tt class="module">audioop</tt> module contains some useful operations on sound |
| 65 | fragments. It operates on sound fragments consisting of signed |
| 66 | integer samples 8, 16 or 32 bits wide, stored in Python strings. This |
| 67 | is 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> |
| 68 | modules. All scalar items are integers, unless specified otherwise. |
| 69 | |
| 70 | <P> |
| 71 | This module provides support for u-LAW and Intel/DVI ADPCM encodings. |
| 72 | <a id='l2h-4620' xml:id='l2h-4620'></a> |
| 73 | |
| 74 | <P> |
| 75 | A few of the more complicated operations only take 16-bit samples, |
| 76 | otherwise the sample size (in bytes) is always a parameter of the |
| 77 | operation. |
| 78 | |
| 79 | <P> |
| 80 | The module defines the following variables and functions: |
| 81 | |
| 82 | <P> |
| 83 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-4594' xml:id='l2h-4594' class="exception">error</tt></b></dt> |
| 84 | <dd> |
| 85 | This exception is raised on all errors, such as unknown number of bytes |
| 86 | per 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> |
| 94 | Return a fragment which is the addition of the two samples passed as |
| 95 | parameters. <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 |
| 97 | length. |
| 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> |
| 105 | Decode an Intel/DVI ADPCM coded fragment to a linear fragment. See |
| 106 | the description of <tt class="function">lin2adpcm()</tt> for details on ADPCM coding. |
| 107 | Return a tuple <code>(<var>sample</var>, <var>newstate</var>)</code> where the sample |
| 108 | has 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> |
| 116 | Decode an alternative 3-bit ADPCM code. See <tt class="function">lin2adpcm3()</tt> |
| 117 | for 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> |
| 125 | Return 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> |
| 133 | Return the average peak-peak value over all samples in the fragment. |
| 134 | No filtering is done, so the usefulness of this routine is |
| 135 | questionable. |
| 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> |
| 143 | Return a fragment that is the original fragment with a bias added to |
| 144 | each 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> |
| 152 | Return the number of zero crossings in the fragment passed as an |
| 153 | argument. |
| 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> |
| 161 | Return a factor <var>F</var> such that |
| 162 | <code>rms(add(<var>fragment</var>, mul(<var>reference</var>, -<var>F</var>)))</code> is |
| 163 | minimal, 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> |
| 168 | The 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> |
| 177 | Try 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 |
| 181 | result. The fragments should both contain 2-byte samples. Return a |
| 182 | tuple <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 |
| 184 | and <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> |
| 193 | Search <var>fragment</var> for a slice of length <var>length</var> samples (not |
| 194 | bytes!) 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 |
| 196 | should both contain 2-byte samples. |
| 197 | |
| 198 | <P> |
| 199 | The 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> |
| 207 | Return 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> |
| 215 | Convert 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> |
| 223 | Convert samples to 4 bit Intel/DVI ADPCM encoding. ADPCM coding is an |
| 224 | adaptive coding scheme, whereby each 4 bit number is the difference |
| 225 | between one sample and the next, divided by a (varying) step. The |
| 226 | Intel/DVI ADPCM algorithm has been selected for use by the IMA, so it |
| 227 | may well become a standard. |
| 228 | |
| 229 | <P> |
| 230 | <var>state</var> is a tuple containing the state of the coder. The coder |
| 231 | returns 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 |
| 234 | passed as the state. <var>adpcmfrag</var> is the ADPCM coded fragment |
| 235 | packed 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> |
| 243 | This is an alternative ADPCM coder that uses only 3 bits per sample. |
| 244 | It is not compatible with the Intel/DVI ADPCM coder and its output is |
| 245 | not packed (due to laziness on the side of the author). Its use is |
| 246 | discouraged. |
| 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> |
| 254 | Convert samples in the audio fragment to u-LAW encoding and return |
| 255 | this as a Python string. u-LAW is an audio encoding format whereby |
| 256 | you get a dynamic range of about 14 bits using only 8 bit samples. It |
| 257 | is 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> |
| 265 | Return a tuple consisting of the minimum and maximum values of all |
| 266 | samples 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> |
| 274 | Return the maximum of the <em>absolute value</em> of all samples in a |
| 275 | fragment. |
| 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> |
| 283 | Return 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> |
| 291 | Return a fragment that has all samples in the original fragment |
| 292 | multiplied by the floating-point value <var>factor</var>. Overflow is |
| 293 | silently 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> |
| 302 | Convert the frame rate of the input fragment. |
| 303 | |
| 304 | <P> |
| 305 | <var>state</var> is a tuple containing the state of the converter. The |
| 306 | converter returns a tuple <code>(<var>newfragment</var>, <var>newstate</var>)</code>, |
| 307 | and <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> |
| 309 | as the state. |
| 310 | |
| 311 | <P> |
| 312 | The <var>weightA</var> and <var>weightB</var> arguments are parameters for a |
| 313 | simple 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> |
| 321 | Reverse 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> |
| 329 | Return 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> |
| 349 | This 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> |
| 357 | Convert a stereo fragment to a mono fragment. The left channel is |
| 358 | multiplied by <var>lfactor</var> and the right channel by <var>rfactor</var> |
| 359 | before 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> |
| 367 | Generate a stereo fragment from a mono fragment. Each pair of samples |
| 368 | in the stereo fragment are computed from the mono sample, whereby left |
| 369 | channel samples are multiplied by <var>lfactor</var> and right channel |
| 370 | samples 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> |
| 378 | Convert sound fragments in u-LAW encoding to linearly encoded sound |
| 379 | fragments. u-LAW encoding always uses 8 bits samples, so <var>width</var> |
| 380 | refers only to the sample width of the output fragment here. |
| 381 | </dl> |
| 382 | |
| 383 | <P> |
| 384 | Note that operations such as <tt class="function">mul()</tt> or <tt class="function">max()</tt> make |
| 385 | no distinction between mono and stereo fragments, i.e. all samples |
| 386 | are treated equal. If this is a problem the stereo fragment should be |
| 387 | split into two mono fragments first and recombined later. Here is an |
| 388 | example of how to do that: |
| 389 | |
| 390 | <P> |
| 391 | <div class="verbatim"><pre> |
| 392 | def 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> |
| 403 | If you use the ADPCM coder to build network packets and you want your |
| 404 | protocol to be stateless (i.e. to be able to tolerate packet loss) |
| 405 | you should not only transmit the data but also the state. Note that |
| 406 | you 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 |
| 408 | returned by the coder). If you want to use <tt class="function">struct.struct()</tt> |
| 409 | to store the state in binary you can code the first element (the |
| 410 | predicted value) in 16 bits and the second (the delta index) in 8. |
| 411 | |
| 412 | <P> |
| 413 | The ADPCM coders have never been tried against other ADPCM coders, |
| 414 | only against themselves. It could well be that I misinterpreted the |
| 415 | standards in which case they will not be interoperable with the |
| 416 | respective standards. |
| 417 | |
| 418 | <P> |
| 419 | The <tt class="function">find*()</tt> routines might look a bit funny at first sight. |
| 420 | They are primarily meant to do echo cancellation. A reasonably |
| 421 | fast way to do this is to pick the most energetic piece of the output |
| 422 | sample, locate that in the input sample and subtract the whole output |
| 423 | sample from the input sample: |
| 424 | |
| 425 | <P> |
| 426 | <div class="verbatim"><pre> |
| 427 | def 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> |
| 479 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 480 | </ADDRESS> |
| 481 | </BODY> |
| 482 | </HTML> |