10
  C   ˆl–Ým_JJh¥ ² ¸È®2ú˜´oÂ„|__‡I|¥Šèªa–äY>” TËm
q@µq§®6Ú˜´oÂ (˜    
:mod:`audioop` --- Manipulate raw audio data
============================================

.. module:: audioop
   :synopsis: Manipulate raw audio data.


The :mod:`audioop` 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
:mod:`al` and :mod:`sunaudiodev` modules.  All scalar items are integers, unless
specified otherwise.

.. index::
   single: Intel/DVI ADPCM
   single: ADPCM, Intel/DVI
   single: a-LAW
   single: u-LAW

This module provides support for a-LAW, u-LAW and Intel/DVI ADPCM encodings.

.. This para is mostly here to provide an excuse for the index entries...

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.

The module defines the following variables and functions:


.. exception:: error

   This exception is raised on all errors, such as unknown number of bytes per
   sample, etc.


.. function:: add(fragment1, fragment2, width)

   Return a fragment which is the addition of the two samples passed as parameters.
   *width* is the sample width in bytes, either ``1``, ``2`` or ``4``.  Both
   fragments should have the same length.  Samples are truncated in case of overflow.


.. function:: adpcm2lin(adpcmfragment, width, state)

   Decode an Intel/DVI ADPCM coded fragment to a linear fragment.  See the
   description of :func:`lin2adpcm` for details on ADPCM coding. Return a tuple
   ``(sample, newstate)`` where the sample has the width specified in *width*.


.. function:: alaw2lin(fragment, width)

   Convert sound fragments in a-LAW encoding to linearly encoded sound fragments.
   a-LAW encoding always uses 8 bits samples, so *width* refers only to the sample
   width of the output fragment here.

   .. versionadded:: 2.5


.. function:: avg(fragment, width)

   Return the average over all samples in the fragment.


.. function:: avgpp(fragment, width)

   Return the average peak-peak value over all samples in the fragment. No
   filtering is done, so the usefulness of this routine is questionable.


.. function:: bias(fragment, width, bias)

   Return a fragment that is the original fragment with a bias added to each
   sample.  Samples wrap around in case of overflow.


.. function:: cross(fragment, width)

   Return the number of zero crossings in the fragment passed as an argument.


.. function:: findfactor(fragment, reference)

   Return a factor *F* such that ``rms(add(fragment, mul(reference, -F)))`` is
   minimal, i.e., return the factor with which you should multiply *reference* to
   make it match as well as possible to *fragment*.  The fragments should both
   contain 2-byte samples.

   The time taken by this routine is proportional to ``len(fragment)``.


.. function:: findfit(fragment, reference)

   Try to match *reference* as well as possible to a portion of *fragment* (which
   should be the longer fragment).  This is (conceptually) done by taking slices
   out of *fragment*, using :func:`findfactor` to compute the best match, and
   minimizing the result.  The fragments should both contain 2-byte samples.
   Return a tuple ``(offset, factor)`` where *offset* is the (integer) offset into
   *fragment* where the optimal match started and *factor* is the (floating-point)
   factor as per :func:`findfactor`.


.. function:: findmax(fragment, length)

   Search *fragment* for a slice of length *length* samples (not bytes!) with
   maximum energy, i.e., return *i* for which ``rms(fragment[i*2:(i+length)*2])``
   is maximal.  The fragments should both contain 2-byte samples.

   The routine takes time proportional to ``len(fragment)``.


.. function:: getsample(fragment, width, index)

   Return the value of sample *index* from the fragment.


.. function:: lin2adpcm(fragment, width, state)

   Convert samples to 4 bit Intel/DVI ADPCM encoding.  ADPC