Kerala Cyber Warriors
KCW Uploader V1.1

Path : /usr/share/doc/python2.7-dev/html/library/
File Upload :
Current File : //usr/share/doc/python2.7-dev/html/library/audioop.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>21.1. audioop — Manipulate raw audio data &#8212; Python 2.7.18 documentation</title>
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 2.7.18 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="next" title="21.2. imageop — Manipulate raw image data" href="imageop.html" />
    <link rel="prev" title="21. Multimedia Services" href="mm.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="file:///usr/share/doc/python2.7/html/library/audioop.html" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
 
    

  </head><body>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
    This document is for an old version of Python that is <a href="https://devguide.python.org/devcycle/#end-of-life-branches">no longer supported</a>.
    You should install the python3 and python3-doc packages and read the 
    <a href="file:///usr/share/doc/python3-doc/html/library/audioop.html"> Python documentation for the Python3 version packaged in this release</a>.
</div>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="imageop.html" title="21.2. imageop — Manipulate raw image data"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="mm.html" title="21. Multimedia Services"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">Python 2.7.18 documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="mm.html" accesskey="U">21. Multimedia Services</a> &#187;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-audioop">
<span id="audioop-manipulate-raw-audio-data"></span><h1>21.1. <a class="reference internal" href="#module-audioop" title="audioop: Manipulate raw audio data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">audioop</span></code></a> — Manipulate raw audio data<a class="headerlink" href="#module-audioop" title="Permalink to this headline">¶</a></h1>
<p>The <a class="reference internal" href="#module-audioop" title="audioop: Manipulate raw audio data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">audioop</span></code></a> module contains some useful operations on sound fragments.
It operates on sound fragments consisting of signed integer samples 8, 16 or 32
bits wide, stored in Python strings.  This is the same format as used by the
<a class="reference internal" href="al.html#module-al" title="al: Audio functions on the SGI. (deprecated) (IRIX)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">al</span></code></a> and <a class="reference internal" href="sunaudio.html#module-sunaudiodev" title="sunaudiodev: Access to Sun audio hardware. (deprecated) (SunOS)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sunaudiodev</span></code></a> modules.  All scalar items are integers, unless
specified otherwise.</p>
<p id="index-0">This module provides support for a-LAW, u-LAW and Intel/DVI ADPCM encodings.</p>
<p>A few of the more complicated operations only take 16-bit samples, otherwise the
sample size (in bytes) is always a parameter of the operation.</p>
<p>The module defines the following variables and functions:</p>
<dl class="exception">
<dt id="audioop.error">
<em class="property">exception </em><code class="descclassname">audioop.</code><code class="descname">error</code><a class="headerlink" href="#audioop.error" title="Permalink to this definition">¶</a></dt>
<dd><p>This exception is raised on all errors, such as unknown number of bytes per
sample, etc.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.add">
<code class="descclassname">audioop.</code><code class="descname">add</code><span class="sig-paren">(</span><em>fragment1</em>, <em>fragment2</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.add" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a fragment which is the addition of the two samples passed as parameters.
<em>width</em> is the sample width in bytes, either <code class="docutils literal notranslate"><span class="pre">1</span></code>, <code class="docutils literal notranslate"><span class="pre">2</span></code> or <code class="docutils literal notranslate"><span class="pre">4</span></code>.  Both
fragments should have the same length.  Samples are truncated in case of overflow.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.adpcm2lin">
<code class="descclassname">audioop.</code><code class="descname">adpcm2lin</code><span class="sig-paren">(</span><em>adpcmfragment</em>, <em>width</em>, <em>state</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.adpcm2lin" title="Permalink to this definition">¶</a></dt>
<dd><p>Decode an Intel/DVI ADPCM coded fragment to a linear fragment.  See the
description of <a class="reference internal" href="#audioop.lin2adpcm" title="audioop.lin2adpcm"><code class="xref py py-func docutils literal notranslate"><span class="pre">lin2adpcm()</span></code></a> for details on ADPCM coding. Return a tuple
<code class="docutils literal notranslate"><span class="pre">(sample,</span> <span class="pre">newstate)</span></code> where the sample has the width specified in <em>width</em>.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.alaw2lin">
<code class="descclassname">audioop.</code><code class="descname">alaw2lin</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.alaw2lin" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert sound fragments in a-LAW encoding to linearly encoded sound fragments.
a-LAW encoding always uses 8 bits samples, so <em>width</em> refers only to the sample
width of the output fragment here.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="audioop.avg">
<code class="descclassname">audioop.</code><code class="descname">avg</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.avg" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the average over all samples in the fragment.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.avgpp">
<code class="descclassname">audioop.</code><code class="descname">avgpp</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.avgpp" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the average peak-peak value over all samples in the fragment. No
filtering is done, so the usefulness of this routine is questionable.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.bias">
<code class="descclassname">audioop.</code><code class="descname">bias</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>bias</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.bias" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a fragment that is the original fragment with a bias added to each
sample.  Samples wrap around in case of overflow.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.cross">
<code class="descclassname">audioop.</code><code class="descname">cross</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.cross" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the number of zero crossings in the fragment passed as an argument.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.findfactor">
<code class="descclassname">audioop.</code><code class="descname">findfactor</code><span class="sig-paren">(</span><em>fragment</em>, <em>reference</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.findfactor" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a factor <em>F</em> such that <code class="docutils literal notranslate"><span class="pre">rms(add(fragment,</span> <span class="pre">mul(reference,</span> <span class="pre">-F)))</span></code> is
minimal, i.e., return the factor with which you should multiply <em>reference</em> to
make it match as well as possible to <em>fragment</em>.  The fragments should both
contain 2-byte samples.</p>
<p>The time taken by this routine is proportional to <code class="docutils literal notranslate"><span class="pre">len(fragment)</span></code>.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.findfit">
<code class="descclassname">audioop.</code><code class="descname">findfit</code><span class="sig-paren">(</span><em>fragment</em>, <em>reference</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.findfit" title="Permalink to this definition">¶</a></dt>
<dd><p>Try to match <em>reference</em> as well as possible to a portion of <em>fragment</em> (which
should be the longer fragment).  This is (conceptually) done by taking slices
out of <em>fragment</em>, using <a class="reference internal" href="#audioop.findfactor" title="audioop.findfactor"><code class="xref py py-func docutils literal notranslate"><span class="pre">findfactor()</span></code></a> to compute the best match, and
minimizing the result.  The fragments should both contain 2-byte samples.
Return a tuple <code class="docutils literal notranslate"><span class="pre">(offset,</span> <span class="pre">factor)</span></code> where <em>offset</em> is the (integer) offset into
<em>fragment</em> where the optimal match started and <em>factor</em> is the (floating-point)
factor as per <a class="reference internal" href="#audioop.findfactor" title="audioop.findfactor"><code class="xref py py-func docutils literal notranslate"><span class="pre">findfactor()</span></code></a>.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.findmax">
<code class="descclassname">audioop.</code><code class="descname">findmax</code><span class="sig-paren">(</span><em>fragment</em>, <em>length</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.findmax" title="Permalink to this definition">¶</a></dt>
<dd><p>Search <em>fragment</em> for a slice of length <em>length</em> samples (not bytes!) with
maximum energy, i.e., return <em>i</em> for which <code class="docutils literal notranslate"><span class="pre">rms(fragment[i*2:(i+length)*2])</span></code>
is maximal.  The fragments should both contain 2-byte samples.</p>
<p>The routine takes time proportional to <code class="docutils literal notranslate"><span class="pre">len(fragment)</span></code>.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.getsample">
<code class="descclassname">audioop.</code><code class="descname">getsample</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>index</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.getsample" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the value of sample <em>index</em> from the fragment.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.lin2adpcm">
<code class="descclassname">audioop.</code><code class="descname">lin2adpcm</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>state</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.lin2adpcm" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert samples to 4 bit Intel/DVI ADPCM encoding.  ADPCM coding is an adaptive
coding scheme, whereby each 4 bit number is the difference between one sample
and the next, divided by a (varying) step.  The Intel/DVI ADPCM algorithm has
been selected for use by the IMA, so it may well become a standard.</p>
<p><em>state</em> is a tuple containing the state of the coder.  The coder returns a tuple
<code class="docutils literal notranslate"><span class="pre">(adpcmfrag,</span> <span class="pre">newstate)</span></code>, and the <em>newstate</em> should be passed to the next call
of <a class="reference internal" href="#audioop.lin2adpcm" title="audioop.lin2adpcm"><code class="xref py py-func docutils literal notranslate"><span class="pre">lin2adpcm()</span></code></a>.  In the initial call, <code class="docutils literal notranslate"><span class="pre">None</span></code> can be passed as the state.
<em>adpcmfrag</em> is the ADPCM coded fragment packed 2 4-bit values per byte.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.lin2alaw">
<code class="descclassname">audioop.</code><code class="descname">lin2alaw</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.lin2alaw" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert samples in the audio fragment to a-LAW encoding and return this as a
Python string.  a-LAW is an audio encoding format whereby you get a dynamic
range of about 13 bits using only 8 bit samples.  It is used by the Sun audio
hardware, among others.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="audioop.lin2lin">
<code class="descclassname">audioop.</code><code class="descname">lin2lin</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>newwidth</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.lin2lin" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert samples between 1-, 2- and 4-byte formats.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>In some audio formats, such as .WAV files, 16 and 32 bit samples are
signed, but 8 bit samples are unsigned.  So when converting to 8 bit wide
samples for these formats, you need to also add 128 to the result:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">new_frames</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">lin2lin</span><span class="p">(</span><span class="n">frames</span><span class="p">,</span> <span class="n">old_width</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">new_frames</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">bias</span><span class="p">(</span><span class="n">new_frames</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">128</span><span class="p">)</span>
</pre></div>
</div>
<p class="last">The same, in reverse, has to be applied when converting from 8 to 16 or 32
bit width samples.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="audioop.lin2ulaw">
<code class="descclassname">audioop.</code><code class="descname">lin2ulaw</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.lin2ulaw" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert samples in the audio fragment to u-LAW encoding and return this as a
Python string.  u-LAW is an audio encoding format whereby you get a dynamic
range of about 14 bits using only 8 bit samples.  It is used by the Sun audio
hardware, among others.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.max">
<code class="descclassname">audioop.</code><code class="descname">max</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.max" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the maximum of the <em>absolute value</em> of all samples in a fragment.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.maxpp">
<code class="descclassname">audioop.</code><code class="descname">maxpp</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.maxpp" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the maximum peak-peak value in the sound fragment.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.minmax">
<code class="descclassname">audioop.</code><code class="descname">minmax</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.minmax" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a tuple consisting of the minimum and maximum values of all samples in
the sound fragment.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.mul">
<code class="descclassname">audioop.</code><code class="descname">mul</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>factor</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.mul" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a fragment that has all samples in the original fragment multiplied by
the floating-point value <em>factor</em>.  Samples are truncated in case of overflow.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.ratecv">
<code class="descclassname">audioop.</code><code class="descname">ratecv</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>nchannels</em>, <em>inrate</em>, <em>outrate</em>, <em>state</em><span class="optional">[</span>, <em>weightA</em><span class="optional">[</span>, <em>weightB</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#audioop.ratecv" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert the frame rate of the input fragment.</p>
<p><em>state</em> is a tuple containing the state of the converter.  The converter returns
a tuple <code class="docutils literal notranslate"><span class="pre">(newfragment,</span> <span class="pre">newstate)</span></code>, and <em>newstate</em> should be passed to the next
call of <a class="reference internal" href="#audioop.ratecv" title="audioop.ratecv"><code class="xref py py-func docutils literal notranslate"><span class="pre">ratecv()</span></code></a>.  The initial call should pass <code class="docutils literal notranslate"><span class="pre">None</span></code> as the state.</p>
<p>The <em>weightA</em> and <em>weightB</em> arguments are parameters for a simple digital filter
and default to <code class="docutils literal notranslate"><span class="pre">1</span></code> and <code class="docutils literal notranslate"><span class="pre">0</span></code> respectively.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.reverse">
<code class="descclassname">audioop.</code><code class="descname">reverse</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.reverse" title="Permalink to this definition">¶</a></dt>
<dd><p>Reverse the samples in a fragment and returns the modified fragment.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.rms">
<code class="descclassname">audioop.</code><code class="descname">rms</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.rms" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the root-mean-square of the fragment, i.e. <code class="docutils literal notranslate"><span class="pre">sqrt(sum(S_i^2)/n)</span></code>.</p>
<p>This is a measure of the power in an audio signal.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.tomono">
<code class="descclassname">audioop.</code><code class="descname">tomono</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>lfactor</em>, <em>rfactor</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.tomono" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert a stereo fragment to a mono fragment.  The left channel is multiplied by
<em>lfactor</em> and the right channel by <em>rfactor</em> before adding the two channels to
give a mono signal.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.tostereo">
<code class="descclassname">audioop.</code><code class="descname">tostereo</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>lfactor</em>, <em>rfactor</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.tostereo" title="Permalink to this definition">¶</a></dt>
<dd><p>Generate a stereo fragment from a mono fragment.  Each pair of samples in the
stereo fragment are computed from the mono sample, whereby left channel samples
are multiplied by <em>lfactor</em> and right channel samples by <em>rfactor</em>.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.ulaw2lin">
<code class="descclassname">audioop.</code><code class="descname">ulaw2lin</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.ulaw2lin" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert sound fragments in u-LAW encoding to linearly encoded sound fragments.
u-LAW encoding always uses 8 bits samples, so <em>width</em> refers only to the sample
width of the output fragment here.</p>
</dd></dl>

<p>Note that operations such as <a class="reference internal" href="#audioop.mul" title="audioop.mul"><code class="xref py py-func docutils literal notranslate"><span class="pre">mul()</span></code></a> or <a class="reference internal" href="#audioop.max" title="audioop.max"><code class="xref py py-func docutils literal notranslate"><span class="pre">max()</span></code></a> make no distinction
between mono and stereo fragments, i.e. all samples are treated equal.  If this
is a problem the stereo fragment should be split into two mono fragments first
and recombined later.  Here is an example of how to do that:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">mul_stereo</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="n">lfactor</span><span class="p">,</span> <span class="n">rfactor</span><span class="p">):</span>
    <span class="n">lsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">tomono</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
    <span class="n">rsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">tomono</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">lsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">mul</span><span class="p">(</span><span class="n">lsample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="n">lfactor</span><span class="p">)</span>
    <span class="n">rsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">mul</span><span class="p">(</span><span class="n">rsample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="n">rfactor</span><span class="p">)</span>
    <span class="n">lsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">tostereo</span><span class="p">(</span><span class="n">lsample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
    <span class="n">rsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">tostereo</span><span class="p">(</span><span class="n">rsample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">audioop</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">lsample</span><span class="p">,</span> <span class="n">rsample</span><span class="p">,</span> <span class="n">width</span><span class="p">)</span>
</pre></div>
</div>
<p>If you use the ADPCM coder to build network packets and you want your protocol
to be stateless (i.e. to be able to tolerate packet loss) you should not only
transmit the data but also the state.  Note that you should send the <em>initial</em>
state (the one you passed to <a class="reference internal" href="#audioop.lin2adpcm" title="audioop.lin2adpcm"><code class="xref py py-func docutils literal notranslate"><span class="pre">lin2adpcm()</span></code></a>) along to the decoder, not the
final state (as returned by the coder).  If you want to use
<a class="reference internal" href="struct.html#struct.Struct" title="struct.Struct"><code class="xref py py-class docutils literal notranslate"><span class="pre">struct.Struct</span></code></a> to store the state in binary you can code the first
element (the predicted value) in 16 bits and the second (the delta index) in 8.</p>
<p>The ADPCM coders have never been tried against other ADPCM coders, only against
themselves.  It could well be that I misinterpreted the standards in which case
they will not be interoperable with the respective standards.</p>
<p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">find*()</span></code> routines might look a bit funny at first sight. They are
primarily meant to do echo cancellation.  A reasonably fast way to do this is to
pick the most energetic piece of the output sample, locate that in the input
sample and subtract the whole output sample from the input sample:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">echocancel</span><span class="p">(</span><span class="n">outputdata</span><span class="p">,</span> <span class="n">inputdata</span><span class="p">):</span>
    <span class="n">pos</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">findmax</span><span class="p">(</span><span class="n">outputdata</span><span class="p">,</span> <span class="mi">800</span><span class="p">)</span>    <span class="c1"># one tenth second</span>
    <span class="n">out_test</span> <span class="o">=</span> <span class="n">outputdata</span><span class="p">[</span><span class="n">pos</span><span class="o">*</span><span class="mi">2</span><span class="p">:]</span>
    <span class="n">in_test</span> <span class="o">=</span> <span class="n">inputdata</span><span class="p">[</span><span class="n">pos</span><span class="o">*</span><span class="mi">2</span><span class="p">:]</span>
    <span class="n">ipos</span><span class="p">,</span> <span class="n">factor</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">findfit</span><span class="p">(</span><span class="n">in_test</span><span class="p">,</span> <span class="n">out_test</span><span class="p">)</span>
    <span class="c1"># Optional (for better cancellation):</span>
    <span class="c1"># factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],</span>
    <span class="c1">#              out_test)</span>
    <span class="n">prefill</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\0</span><span class="s1">&#39;</span><span class="o">*</span><span class="p">(</span><span class="n">pos</span><span class="o">+</span><span class="n">ipos</span><span class="p">)</span><span class="o">*</span><span class="mi">2</span>
    <span class="n">postfill</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\0</span><span class="s1">&#39;</span><span class="o">*</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">inputdata</span><span class="p">)</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">prefill</span><span class="p">)</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">outputdata</span><span class="p">))</span>
    <span class="n">outputdata</span> <span class="o">=</span> <span class="n">prefill</span> <span class="o">+</span> <span class="n">audioop</span><span class="o">.</span><span class="n">mul</span><span class="p">(</span><span class="n">outputdata</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="n">factor</span><span class="p">)</span> <span class="o">+</span> <span class="n">postfill</span>
    <span class="k">return</span> <span class="n">audioop</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">inputdata</span><span class="p">,</span> <span class="n">outputdata</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="mm.html"
                        title="previous chapter">21. Multimedia Services</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="imageop.html"
                        title="next chapter">21.2. <code class="docutils literal notranslate"><span class="pre">imageop</span></code> — Manipulate raw image data</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/library/audioop.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="imageop.html" title="21.2. imageop — Manipulate raw image data"
             >next</a> |</li>
        <li class="right" >
          <a href="mm.html" title="21. Multimedia Services"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">Python 2.7.18 documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="mm.html" >21. Multimedia Services</a> &#187;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2024, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on November 21, 2024.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.8.5.
    </div>

  </body>
</html>

-=[ KCW uplo4d3r c0ded by cJ_n4p573r ]=-
Ⓒ2017 ҠЄГѦLѦ СүѣЄГ ЩѦГГіѺГՏ