📄 module-audioop.html
字号:
not packed (due to laziness on the side of the author). Its use is
discouraged.
</dl>
<P>
<dl><dt><b><a name='l2h-2781'><tt class='function'>lin2ulaw</tt></a></b> (<var>fragment, width</var>)
<dd>
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.
</dl>
<P>
<dl><dt><b><a name='l2h-2782'><tt class='function'>minmax</tt></a></b> (<var>fragment, width</var>)
<dd>
Return a tuple consisting of the minimum and maximum values of all
samples in the sound fragment.
</dl>
<P>
<dl><dt><b><a name='l2h-2783'><tt class='function'>max</tt></a></b> (<var>fragment, width</var>)
<dd>
Return the maximum of the <i>absolute value</i> of all samples in a
fragment.
</dl>
<P>
<dl><dt><b><a name='l2h-2784'><tt class='function'>maxpp</tt></a></b> (<var>fragment, width</var>)
<dd>
Return the maximum peak-peak value in the sound fragment.
</dl>
<P>
<dl><dt><b><a name='l2h-2785'><tt class='function'>mul</tt></a></b> (<var>fragment, width, factor</var>)
<dd>
Return a fragment that has all samples in the original fragment
multiplied by the floating-point value <var>factor</var>. Overflow is
silently ignored.
</dl>
<P>
<dl><dt><b><a name='l2h-2786'><tt class='function'>ratecv</tt></a></b> (<var>fragment, width, nchannels, inrate, outrate,
state</var><big>[</big><var>, weightA</var><big>[</big><var>, weightB</var><big>]</big><big>]</big>)
<dd>
Convert the frame rate of the input fragment.
<P>
<var>state</var> is a tuple containing the state of the converter. The
converter returns a tuple <code>(<var>newfragment</var>, <var>newstate</var>)</code>,
and <var>newstate</var> should be passed to the next call of
<tt class="function">ratecv()</tt>.
<P>
The <var>weightA</var> and <var>weightB</var> arguments are parameters for a
simple digital filter and default to <code>1</code> and <code>0</code> respectively.
</dl>
<P>
<dl><dt><b><a name='l2h-2787'><tt class='function'>reverse</tt></a></b> (<var>fragment, width</var>)
<dd>
Reverse the samples in a fragment and returns the modified fragment.
</dl>
<P>
<dl><dt><b><a name='l2h-2788'><tt class='function'>rms</tt></a></b> (<var>fragment, width</var>)
<dd>
Return the root-mean-square of the fragment, i.e.
<BR><P></P>
<DIV ALIGN="CENTER" CLASS="mathdisplay">
<!-- MATH
\begin{displaymath}
\catcode`_=8
\sqrt{\frac{\sum{{S_{i}}^{2}}}{n}}
\end{displaymath}
-->
<IMG
WIDTH="60" HEIGHT="55" BORDER="0"
SRC="img1.gif" tppabs="http://www.python.org/doc/current/lib/img1.gif"
ALT="\begin{displaymath}
\catcode\lq _=8
\sqrt{\frac{\sum{{S_{i}}^{2}}}{n}}
\end{displaymath}">
</DIV>
<BR CLEAR="ALL">
<P></P>
This is a measure of the power in an audio signal.
</dl>
<P>
<dl><dt><b><a name='l2h-2789'><tt class='function'>tomono</tt></a></b> (<var>fragment, width, lfactor, rfactor</var>)
<dd>
Convert a stereo fragment to a mono fragment. The left channel is
multiplied by <var>lfactor</var> and the right channel by <var>rfactor</var>
before adding the two channels to give a mono signal.
</dl>
<P>
<dl><dt><b><a name='l2h-2790'><tt class='function'>tostereo</tt></a></b> (<var>fragment, width, lfactor, rfactor</var>)
<dd>
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 <var>lfactor</var> and right channel
samples by <var>rfactor</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-2791'><tt class='function'>ulaw2lin</tt></a></b> (<var>fragment, width</var>)
<dd>
Convert sound fragments in u-LAW encoding to linearly encoded sound
fragments. u-LAW encoding always uses 8 bits samples, so <var>width</var>
refers only to the sample width of the output fragment here.
</dl>
<P>
Note that operations such as <tt class="function">mul()</tt> or <tt class="function">max()</tt> 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>
<dl><dd><pre class="verbatim">
def mul_stereo(sample, width, lfactor, rfactor):
lsample = audioop.tomono(sample, width, 1, 0)
rsample = audioop.tomono(sample, width, 0, 1)
lsample = audioop.mul(sample, width, lfactor)
rsample = audioop.mul(sample, width, rfactor)
lsample = audioop.tostereo(lsample, width, 1, 0)
rsample = audioop.tostereo(rsample, width, 0, 1)
return audioop.add(lsample, rsample, width)
</pre></dl>
<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 <var>initial</var> state (the one you passed to
<tt class="function">lin2adpcm()</tt>) along to the decoder, not the final state (as
returned by the coder). If you want to use <tt class="function">struct.struct()</tt>
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>
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>
The <tt class="function">find*()</tt> 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>
<dl><dd><pre class="verbatim">
def echocancel(outputdata, inputdata):
pos = audioop.findmax(outputdata, 800) # one tenth second
out_test = outputdata[pos*2:]
in_test = inputdata[pos*2:]
ipos, factor = audioop.findfit(in_test, out_test)
# Optional (for better cancellation):
# factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],
# out_test)
prefill = '\0'*(pos+ipos)*2
postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata))
outputdata = prefill + audioop.mul(outputdata,2,-factor) + postfill
return audioop.add(inputdata, outputdata, 2)
</pre></dl>
<DIV CLASS="navigation"><p><hr><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>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -