📄 module-audioop.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>14.1 audioop -- Manipulate raw audio data</title>
<META NAME="description" CONTENT="14.1 audioop -- Manipulate raw audio data">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-imageop.html" tppabs="http://www.python.org/doc/current/lib/module-imageop.html">
<LINK REL="previous" href="mmedia.html" tppabs="http://www.python.org/doc/current/lib/mmedia.html">
<LINK REL="up" href="mmedia.html" tppabs="http://www.python.org/doc/current/lib/mmedia.html">
<LINK REL="next" href="module-imageop.html" tppabs="http://www.python.org/doc/current/lib/module-imageop.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="mmedia.html" tppabs="http://www.python.org/doc/current/lib/mmedia.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="mmedia.html" tppabs="http://www.python.org/doc/current/lib/mmedia.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="module-imageop.html" tppabs="http://www.python.org/doc/current/lib/module-imageop.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="mmedia.html" tppabs="http://www.python.org/doc/current/lib/mmedia.html">14. Multimedia Services</A>
<b class="navlabel">Up:</b> <a class="sectref" href="mmedia.html" tppabs="http://www.python.org/doc/current/lib/mmedia.html">14. Multimedia Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-imageop.html" tppabs="http://www.python.org/doc/current/lib/module-imageop.html">14.2 imageop </A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION0016100000000000000000">
14.1 <tt class="module">audioop</tt> --
Manipulate raw audio data</A>
</H1>
<P>
<P>
The <tt class="module">audioop</tt> 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 <tt class='module'><a href="module-al.html" tppabs="http://www.python.org/doc/current/lib/module-al.html">al</a></tt> and <tt class='module'><a href="module-sunaudiodev.html" tppabs="http://www.python.org/doc/current/lib/module-sunaudiodev.html">sunaudiodev</a></tt>
modules. All scalar items are integers, unless specified otherwise.
<P>
This module provides support for u-LAW and Intel/DVI ADPCM encodings.
<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>
The module defines the following variables and functions:
<P>
<dl><dt><b><a name='l2h-2766'><tt class='exception'>error</tt></a></b>
<dd>
This exception is raised on all errors, such as unknown number of bytes
per sample, etc.
</dl>
<P>
<dl><dt><b><a name='l2h-2767'><tt class='function'>add</tt></a></b> (<var>fragment1, fragment2, width</var>)
<dd>
Return a fragment which is the addition of the two samples passed as
parameters. <var>width</var> is the sample width in bytes, either
<code>1</code>, <code>2</code> or <code>4</code>. Both fragments should have the same
length.
</dl>
<P>
<dl><dt><b><a name='l2h-2768'><tt class='function'>adpcm2lin</tt></a></b> (<var>adpcmfragment, width, state</var>)
<dd>
Decode an Intel/DVI ADPCM coded fragment to a linear fragment. See
the description of <tt class="function">lin2adpcm()</tt> for details on ADPCM coding.
Return a tuple <code>(<var>sample</var>, <var>newstate</var>)</code> where the sample
has the width specified in <var>width</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-2769'><tt class='function'>adpcm32lin</tt></a></b> (<var>adpcmfragment, width, state</var>)
<dd>
Decode an alternative 3-bit ADPCM code. See <tt class="function">lin2adpcm3()</tt>
for details.
</dl>
<P>
<dl><dt><b><a name='l2h-2770'><tt class='function'>avg</tt></a></b> (<var>fragment, width</var>)
<dd>
Return the average over all samples in the fragment.
</dl>
<P>
<dl><dt><b><a name='l2h-2771'><tt class='function'>avgpp</tt></a></b> (<var>fragment, width</var>)
<dd>
Return the average peak-peak value over all samples in the fragment.
No filtering is done, so the usefulness of this routine is
questionable.
</dl>
<P>
<dl><dt><b><a name='l2h-2772'><tt class='function'>bias</tt></a></b> (<var>fragment, width, bias</var>)
<dd>
Return a fragment that is the original fragment with a bias added to
each sample.
</dl>
<P>
<dl><dt><b><a name='l2h-2773'><tt class='function'>cross</tt></a></b> (<var>fragment, width</var>)
<dd>
Return the number of zero crossings in the fragment passed as an
argument.
</dl>
<P>
<dl><dt><b><a name='l2h-2774'><tt class='function'>findfactor</tt></a></b> (<var>fragment, reference</var>)
<dd>
Return a factor <var>F</var> such that
<code>rms(add(<var>fragment</var>, mul(<var>reference</var>, -<var>F</var>)))</code> is
minimal, i.e., return the factor with which you should multiply
<var>reference</var> to make it match as well as possible to
<var>fragment</var>. The fragments should both contain 2-byte samples.
<P>
The time taken by this routine is proportional to
<code>len(<var>fragment</var>)</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-2775'><tt class='function'>findfit</tt></a></b> (<var>fragment, reference</var>)
<dd>
Try to match <var>reference</var> as well as possible to a portion of
<var>fragment</var> (which should be the longer fragment). This is
(conceptually) done by taking slices out of <var>fragment</var>, using
<tt class="function">findfactor()</tt> to compute the best match, and minimizing the
result. The fragments should both contain 2-byte samples. Return a
tuple <code>(<var>offset</var>, <var>factor</var>)</code> where <var>offset</var> is the
(integer) offset into <var>fragment</var> where the optimal match started
and <var>factor</var> is the (floating-point) factor as per
<tt class="function">findfactor()</tt>.
</dl>
<P>
<dl><dt><b><a name='l2h-2776'><tt class='function'>findmax</tt></a></b> (<var>fragment, length</var>)
<dd>
Search <var>fragment</var> for a slice of length <var>length</var> samples (not
bytes!) with maximum energy, i.e., return <var>i</var> for which
<code>rms(fragment[i*2:(i+length)*2])</code> is maximal. The fragments
should both contain 2-byte samples.
<P>
The routine takes time proportional to <code>len(<var>fragment</var>)</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-2777'><tt class='function'>getsample</tt></a></b> (<var>fragment, width, index</var>)
<dd>
Return the value of sample <var>index</var> from the fragment.
</dl>
<P>
<dl><dt><b><a name='l2h-2778'><tt class='function'>lin2lin</tt></a></b> (<var>fragment, width, newwidth</var>)
<dd>
Convert samples between 1-, 2- and 4-byte formats.
</dl>
<P>
<dl><dt><b><a name='l2h-2779'><tt class='function'>lin2adpcm</tt></a></b> (<var>fragment, width, state</var>)
<dd>
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>
<var>state</var> is a tuple containing the state of the coder. The coder
returns a tuple <code>(<var>adpcmfrag</var>, <var>newstate</var>)</code>, and the
<var>newstate</var> should be passed to the next call of
<tt class="function">lin2adpcm()</tt>. In the initial call, <code>None</code> can be
passed as the state. <var>adpcmfrag</var> is the ADPCM coded fragment
packed 2 4-bit values per byte.
</dl>
<P>
<dl><dt><b><a name='l2h-2780'><tt class='function'>lin2adpcm3</tt></a></b> (<var>fragment, width, state</var>)
<dd>
This is an alternative ADPCM coder that uses only 3 bits per sample.
It is not compatible with the Intel/DVI ADPCM coder and its output is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -