“Python/docs/3.9/library/audioop”的版本间差异

来自菜鸟教程
Python/docs/3.9/library/audioop
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:audioop — 处理原始音频数据 — Python 文档}}
 
<div id="module-audioop" class="section">
 
<div id="module-audioop" class="section">
  
 
<span id="audioop-manipulate-raw-audio-data"></span>
 
<span id="audioop-manipulate-raw-audio-data"></span>
= [[#module-audioop|<code>audioop</code>]] --- Manipulate raw audio data =
+
= audioop — 处理原始音频数据 =
  
The [[#module-audioop|<code>audioop</code>]] module contains some useful operations on sound fragments.
+
 
It operates on sound fragments consisting of signed integer samples 8, 16, 24
+
-----
or 32 bits wide, stored in [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like objects</span>]]. All scalar items are
+
 
integers, unless specified otherwise.
+
[[#module-audioop|audioop]] 模块包含一些对声音片段有用的操作。 它对由 8、16、24 或 32 位宽的有符号整数样本组成的声音片段进行操作,这些样本存储在 [[../../glossary#term-bytes-like-object|字节类对象]] 中。 除非另有说明,否则所有标量项都是整数。
  
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<span class="versionmodified changed">3.4 版更改: </span>Support for 24-bit samples was added.
+
<span class="versionmodified changed"> 3.4 版更改: </span> 添加了对 24 位样本的支持。 所有函数现在都接受任何 [[../../glossary#term-bytes-like-object|字节的对象]] 。 字符串输入现在会导致立即错误。
All functions now accept any [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]].
 
String input now results in an immediate error.
 
  
  
 
</div>
 
</div>
This module provides support for a-LAW, u-LAW and Intel/DVI ADPCM encodings.
+
该模块支持 a-LAW、u-LAW Intel/DVI ADPCM 编码。
  
A few of the more complicated operations only take 16-bit samples, otherwise the
+
一些更复杂的操作只需要 16 位样本,否则样本大小(以字节为单位)始终是操作的参数。
sample size (in bytes) is always a parameter of the operation.
 
  
The module defines the following variables and functions:
+
该模块定义了以下变量和函数:
  
; ''exception'' <code>audioop.</code><code>error</code>
+
; ''<span class="pre">exception</span>'' <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">error</span></span>
: This exception is raised on all errors, such as unknown number of bytes per sample, etc.
+
: 所有错误都会引发此异常,例如每个样本的字节数未知等。
  
; <code>audioop.</code><code>add</code><span class="sig-paren">(</span>''<span class="n">fragment1</span>'', ''<span class="n">fragment2</span>'', ''<span class="n">width</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">add</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment1</span></span>'', ''<span class="n"><span class="pre">fragment2</span></span>'', ''<span class="n"><span class="pre">width</span></span>''<span class="sig-paren">)</span>
: Return a fragment which is the addition of the two samples passed as parameters. ''width'' is the sample width in bytes, either <code>1</code>, <code>2</code>, <code>3</code> or <code>4</code>. Both fragments should have the same length. Samples are truncated in case of overflow.
+
: 返回一个片段,它是作为参数传递的两个样本的相加。 ''width'' 是以字节为单位的样本宽度,<code>1</code><code>2</code><code>3</code> <code>4</code>。 两个片段应该具有相同的长度。 在溢出的情况下截断样本。
  
; <code>audioop.</code><code>adpcm2lin</code><span class="sig-paren">(</span>''<span class="n">adpcmfragment</span>'', ''<span class="n">width</span>'', ''<span class="n">state</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">adpcm2lin</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">adpcmfragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>'', ''<span class="n"><span class="pre">state</span></span>''<span class="sig-paren">)</span>
: Decode an Intel/DVI ADPCM coded fragment to a linear fragment. See the description of [[#audioop.lin2adpcm|<code>lin2adpcm()</code>]] for details on ADPCM coding. Return a tuple <code>(sample, newstate)</code> where the sample has the width specified in ''width''.
+
: Intel/DVI ADPCM 编码片段解码为线性片段。 有关 ADPCM 编码的详细信息,请参阅 [[#audioop.lin2adpcm|lin2adpcm()]] 的说明。 返回一个元组 <code>(sample, newstate)</code>,其中样本具有 ''width'' 中指定的宽度。
  
; <code>audioop.</code><code>alaw2lin</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">alaw2lin</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>''<span class="sig-paren">)</span>
: 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.
+
: a-LAW 编码的声音片段转换为线性编码的声音片段。 a-LAW 编码总是使用 8 位样本,所以这里的 ''width'' 仅指输出片段的样本宽度。
  
; <code>audioop.</code><code>avg</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">avg</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>''<span class="sig-paren">)</span>
: Return the average over all samples in the fragment.
+
: 返回片段中所有样本的平均值。
  
; <code>audioop.</code><code>avgpp</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">avgpp</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>''<span class="sig-paren">)</span>
: Return the average peak-peak value over all samples in the fragment. No filtering is done, so the usefulness of this routine is questionable.
+
: 返回片段中所有样本的平均峰峰值。 没有进行过滤,所以这个例程的用处值得怀疑。
  
; <code>audioop.</code><code>bias</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>'', ''<span class="n">bias</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">bias</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>'', ''<span class="n"><span class="pre">bias</span></span>''<span class="sig-paren">)</span>
: Return a fragment that is the original fragment with a bias added to each sample. Samples wrap around in case of overflow.
+
: 返回一个片段,该片段是原始片段,每个样本都添加了偏差。 样品环绕以防溢出。
  
 
<dl>
 
<dl>
<dt><code>audioop.</code><code>byteswap</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">byteswap</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>&quot;Byteswap&quot; all samples in a fragment and returns the modified fragment.
+
<dd><p>“字节交换”片段中的所有样本并返回修改后的片段。 将大端样本转换为小端样本,反之亦然。</p>
Converts big-endian samples to little-endian and vice versa.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.4 新版功能.</span></p>
+
<p><span class="versionmodified added">3.4 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; <code>audioop.</code><code>cross</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">cross</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>''<span class="sig-paren">)</span>
: Return the number of zero crossings in the fragment passed as an argument.
+
: 返回作为参数传递的片段中的零交叉数。
  
 
<dl>
 
<dl>
<dt><code>audioop.</code><code>findfactor</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">reference</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">findfactor</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">reference</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Return a factor ''F'' such that <code>rms(add(fragment, mul(reference, -F)))</code> is
+
<dd><p>返回一个因子 ''F'' 使得 <code>rms(add(fragment, mul(reference, -F)))</code> 最小,即返回你应该乘以 ''reference'' 以使其与 ''尽可能匹配的因子片段''。 这些片段都应该包含 2 字节的样本。</p>
minimal, i.e., return the factor with which you should multiply ''reference'' to
+
<p>此例程所花费的时间与 <code>len(fragment)</code> 成正比。</p></dd></dl>
make it match as well as possible to ''fragment''. The fragments should both
 
contain 2-byte samples.</p>
 
<p>The time taken by this routine is proportional to <code>len(fragment)</code>.</p></dd></dl>
 
  
; <code>audioop.</code><code>findfit</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">reference</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">findfit</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">reference</span></span>''<span class="sig-paren">)</span>
: 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 [[#audioop.findfactor|<code>findfactor()</code>]] to compute the best match, and minimizing the result. The fragments should both contain 2-byte samples. Return a tuple <code>(offset, factor)</code> where ''offset'' is the (integer) offset into ''fragment'' where the optimal match started and ''factor'' is the (floating-point) factor as per [[#audioop.findfactor|<code>findfactor()</code>]].
+
: 尝试将 ''reference'' 尽可能匹配到 ''fragment''(应该是较长的片段)的一部分。 这是(概念上)通过从 ''fragment'' 中取出切片,使用 [[#audioop.findfactor|findfactor()]] 计算最佳匹配并最小化结果来完成的。 这些片段都应该包含 2 字节的样本。 返回一个元组 <code>(offset, factor)</code>,其中 ''offset'' ''fragment'' 的(整数)偏移量,其中最佳匹配开始,''factor'' 是(浮点数) ) 因子根据 [[#audioop.findfactor|findfactor()]]
  
 
<dl>
 
<dl>
<dt><code>audioop.</code><code>findmax</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">length</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">findmax</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">length</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Search ''fragment'' for a slice of length ''length'' samples (not bytes!) with
+
<dd><p>''fragment'' 中搜索具有最大能量的长度为 ''length'' 样本(不是字节!)的切片,即返回 ''i'' 其中 <code>rms(fragment[i*2:(i+length)*2])</code> 是最大的. 这些片段都应该包含 2 字节的样本。</p>
maximum energy, i.e., return ''i'' for which <code>rms(fragment[i*2:(i+length)*2])</code>
+
<p>该例程花费的时间与 <code>len(fragment)</code> 成正比。</p></dd></dl>
is maximal. The fragments should both contain 2-byte samples.</p>
 
<p>The routine takes time proportional to <code>len(fragment)</code>.</p></dd></dl>
 
  
; <code>audioop.</code><code>getsample</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>'', ''<span class="n">index</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">getsample</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>'', ''<span class="n"><span class="pre">index</span></span>''<span class="sig-paren">)</span>
: Return the value of sample ''index'' from the fragment.
+
: 从片段返回样本 ''index'' 的值。
  
 
<dl>
 
<dl>
<dt><code>audioop.</code><code>lin2adpcm</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>'', ''<span class="n">state</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">lin2adpcm</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>'', ''<span class="n"><span class="pre">state</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Convert samples to 4 bit Intel/DVI ADPCM encoding. ADPCM coding is an adaptive
+
<dd><p>将样本转换为 4 Intel/DVI ADPCM 编码。 ADPCM 编码是一种自适应编码方案,其中每 4 位数字是一个样本和下一个样本之间的差异,除以(变化的)步长。 IMA 选择了 Intel/DVI ADPCM 算法,因此它很可能成为标准。</p>
coding scheme, whereby each 4 bit number is the difference between one sample
+
<p>''state'' 是一个包含编码器状态的元组。 编码器返回一个元组 <code>(adpcmfrag, newstate)</code>,并且 ''newstate'' 应该传递给 [[#audioop.lin2adpcm|lin2adpcm()]] 的下一次调用。 在初始调用中,<code>None</code> 可以作为状态传递。 ''adpcmfrag'' 是每字节 2 4 位值的 ADPCM 编码片段。</p></dd></dl>
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>''state'' is a tuple containing the state of the coder. The coder returns a tuple
 
<code>(adpcmfrag, newstate)</code>, and the ''newstate'' should be passed to the next call
 
of [[#audioop.lin2adpcm|<code>lin2adpcm()</code>]]. In the initial call, <code>None</code> can be passed as the state.
 
''adpcmfrag'' is the ADPCM coded fragment packed 2 4-bit values per byte.</p></dd></dl>
 
  
; <code>audioop.</code><code>lin2alaw</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">lin2alaw</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>''<span class="sig-paren">)</span>
: Convert samples in the audio fragment to a-LAW encoding and return this as a bytes object. 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.
+
: 将音频片段中的样本转换为 a-LAW 编码并将其作为字节对象返回。 a-LAW 是一种音频编码格式,仅使用 8 位样本即可获得大约 13 位的动态范围。 它由 Sun 音频硬件等使用。
  
 
<dl>
 
<dl>
<dt><code>audioop.</code><code>lin2lin</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>'', ''<span class="n">newwidth</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">lin2lin</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>'', ''<span class="n"><span class="pre">newwidth</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Convert samples between 1-, 2-, 3- and 4-byte formats.</p>
+
<dd><p>在 1、2、3 和 4 字节格式之间转换样本。</p>
 
<div class="admonition note">
 
<div class="admonition note">
  
<p>注解</p>
+
<p>笔记</p>
<p>In some audio formats, such as .WAV files, 16, 24 and 32 bit samples are
+
<p>在某些音频格式中,例如 .WAV 文件,16、24 和 32 位样本是有符号的,但 8 位样本是无符号的。 因此,当为这些格式转换为 8 位宽的样本时,您还需要将 128 添加到结果中:</p>
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-python3 notranslate">
 
<div class="highlight-python3 notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>new_frames = audioop.lin2lin(frames, old_width, 1)
+
<syntaxhighlight lang="python3">new_frames = audioop.lin2lin(frames, old_width, 1)
new_frames = audioop.bias(new_frames, 1, 128)</pre>
+
new_frames = audioop.bias(new_frames, 1, 128)</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>The same, in reverse, has to be applied when converting from 8 to 16, 24
+
<p>反过来,当将 8 位宽度样本转换为 16、24 或 32 位宽度样本时,必须应用相同的方法。</p>
or 32 bit width samples.</p>
 
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; <code>audioop.</code><code>lin2ulaw</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">lin2ulaw</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>''<span class="sig-paren">)</span>
: Convert samples in the audio fragment to u-LAW encoding and return this as a bytes object. 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.
+
: 将音频片段中的样本转换为 u-LAW 编码并将其作为字节对象返回。 u-LAW 是一种音频编码格式,仅使用 8 位样本即可获得大约 14 位的动态范围。 它由 Sun 音频硬件等使用。
  
; <code>audioop.</code><code>max</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">max</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>''<span class="sig-paren">)</span>
: Return the maximum of the ''absolute value'' of all samples in a fragment.
+
: 返回片段中所有样本的 ''绝对值'' 的最大值。
  
; <code>audioop.</code><code>maxpp</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">maxpp</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>''<span class="sig-paren">)</span>
: Return the maximum peak-peak value in the sound fragment.
+
: 返回声音片段中的最大峰峰值。
  
; <code>audioop.</code><code>minmax</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">minmax</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>''<span class="sig-paren">)</span>
: Return a tuple consisting of the minimum and maximum values of all samples in the sound fragment.
+
: 返回由声音片段中所有样本的最小值和最大值组成的元组。
  
; <code>audioop.</code><code>mul</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>'', ''<span class="n">factor</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">mul</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>'', ''<span class="n"><span class="pre">factor</span></span>''<span class="sig-paren">)</span>
: Return a fragment that has all samples in the original fragment multiplied by the floating-point value ''factor''. Samples are truncated in case of overflow.
+
: 返回一个片段,其中原始片段中的所有样本都乘以浮点值 ''因子'' 。 在溢出的情况下截断样本。
  
 
<dl>
 
<dl>
<dt><code>audioop.</code><code>ratecv</code><span class="sig-paren">(</span>''fragment'', ''width'', ''nchannels'', ''inrate'', ''outrate'', ''state''<span class="optional">[</span>, ''weightA''<span class="optional">[</span>, ''weightB''<span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">ratecv</span></span><span class="sig-paren">(</span>''<span class="pre">fragment</span>'', ''<span class="pre">width</span>'', ''<span class="pre">nchannels</span>'', ''<span class="pre">inrate</span>'', ''<span class="pre">outrate</span>'', ''<span class="pre">state</span>''<span class="optional">[</span>, ''<span class="pre">weightA</span>''<span class="optional">[</span>, ''<span class="pre">weightB</span>''<span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span></dt>
<dd><p>Convert the frame rate of the input fragment.</p>
+
<dd><p>转换输入片段的帧率。</p>
<p>''state'' is a tuple containing the state of the converter. The converter returns
+
<p>''state'' 是一个包含转换器状态的元组。 转换器返回一个元组 <code>(newfragment, newstate)</code>,并且 ''newstate'' 应该传递给 [[#audioop.ratecv|ratecv()]] 的下一次调用。 初始调用应通过 <code>None</code> 作为状态。</p>
a tuple <code>(newfragment, newstate)</code>, and ''newstate'' should be passed to the next
+
<p>''weightA'' ''weightB'' 参数是简单数字滤波器的参数,分别默认为 <code>1</code> <code>0</code></p></dd></dl>
call of [[#audioop.ratecv|<code>ratecv()</code>]]. The initial call should pass <code>None</code> as the state.</p>
 
<p>The ''weightA'' and ''weightB'' arguments are parameters for a simple digital filter
 
and default to <code>1</code> and <code>0</code> respectively.</p></dd></dl>
 
  
; <code>audioop.</code><code>reverse</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">reverse</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>''<span class="sig-paren">)</span>
: Reverse the samples in a fragment and returns the modified fragment.
+
: 反转片段中的样本并返回修改后的片段。
  
 
<dl>
 
<dl>
<dt><code>audioop.</code><code>rms</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">rms</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Return the root-mean-square of the fragment, i.e. <code>sqrt(sum(S_i^2)/n)</code>.</p>
+
<dd><p>返回片段的均方根,即 <code>sqrt(sum(S_i^2)/n)</code></p>
<p>This is a measure of the power in an audio signal.</p></dd></dl>
+
<p>这是音频信号中功率的量度。</p></dd></dl>
  
; <code>audioop.</code><code>tomono</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>'', ''<span class="n">lfactor</span>'', ''<span class="n">rfactor</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">tomono</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>'', ''<span class="n"><span class="pre">lfactor</span></span>'', ''<span class="n"><span class="pre">rfactor</span></span>''<span class="sig-paren">)</span>
: Convert a stereo fragment to a mono fragment. The left channel is multiplied by ''lfactor'' and the right channel by ''rfactor'' before adding the two channels to give a mono signal.
+
: 将立体声片段转换为单声道片段。 左声道乘以''lfactor'',右声道乘以''rfactor'',然后将两个声道相加得到单声道信号。
  
; <code>audioop.</code><code>tostereo</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>'', ''<span class="n">lfactor</span>'', ''<span class="n">rfactor</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">tostereo</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>'', ''<span class="n"><span class="pre">lfactor</span></span>'', ''<span class="n"><span class="pre">rfactor</span></span>''<span class="sig-paren">)</span>
: 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 ''lfactor'' and right channel samples by ''rfactor''.
+
: 从单声道片段生成立体声片段。 立体声片段中的每对样本都是从单声道样本中计算出来的,其中左声道样本乘以 ''lfactor'',右声道样本乘以 ''rfactor''
  
; <code>audioop.</code><code>ulaw2lin</code><span class="sig-paren">(</span>''<span class="n">fragment</span>'', ''<span class="n">width</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">audioop.</span></span><span class="sig-name descname"><span class="pre">ulaw2lin</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fragment</span></span>'', ''<span class="n"><span class="pre">width</span></span>''<span class="sig-paren">)</span>
: Convert sound fragments in u-LAW encoding to linearly encoded sound fragments. u-LAW encoding always uses 8 bits samples, so ''width'' refers only to the sample width of the output fragment here.
+
: u-LAW 编码的声音片段转换为线性编码的声音片段。 u-LAW 编码总是使用 8 位样本,所以这里的 ''width'' 仅指输出片段的样本宽度。
  
Note that operations such as [[#audioop.mul|<code>mul()</code>]] or [[#audioop.max|<code>max()</code>]] make no distinction
+
请注意,诸如 [[#audioop.mul|mul()]] [[#audioop.max|max()]] 之类的操作不区分单声道和立体声片段,即 所有样品都被平等对待。 如果这是一个问题,立体声片段应首先拆分为两个单声道片段,然后再重新组合。 以下是如何执行此操作的示例:
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:
 
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第167行: 第144行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>def mul_stereo(sample, width, lfactor, rfactor):
+
<syntaxhighlight lang="python3">def mul_stereo(sample, width, lfactor, rfactor):
 
     lsample = audioop.tomono(sample, width, 1, 0)
 
     lsample = audioop.tomono(sample, width, 1, 0)
 
     rsample = audioop.tomono(sample, width, 0, 1)
 
     rsample = audioop.tomono(sample, width, 0, 1)
第174行: 第151行:
 
     lsample = audioop.tostereo(lsample, width, 1, 0)
 
     lsample = audioop.tostereo(lsample, width, 1, 0)
 
     rsample = audioop.tostereo(rsample, width, 0, 1)
 
     rsample = audioop.tostereo(rsample, width, 0, 1)
     return audioop.add(lsample, rsample, width)</pre>
+
     return audioop.add(lsample, rsample, width)</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
If you use the ADPCM coder to build network packets and you want your protocol
+
如果您使用 ADPCM 编码器构建网络数据包,并且您希望您的协议是无状态的(即 为了能够容忍丢包),您不仅应该传输数据,还应该传输状态。 请注意,您应该将 ''初始'' 状态(您传递给 [[#audioop.lin2adpcm|lin2adpcm()]] 的状态)发送到解码器,而不是最终状态(由编码器返回)。 如果您想使用 [[../struct#struct|struct.Struct]] 以二进制形式存储状态,您可以将第一个元素(预测值)编码为 16 位,将第二个(增量索引)编码为 8。
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 ''initial''
 
state (the one you passed to [[#audioop.lin2adpcm|<code>lin2adpcm()</code>]]) along to the decoder, not the
 
final state (as returned by the coder). If you want to use
 
[[../struct#struct|<code>struct.Struct</code>]] 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.
 
  
The ADPCM coders have never been tried against other ADPCM coders, only against
+
ADPCM 编码器从未与其他 ADPCM 编码器进行过对比,只是与它们自己进行过对比。 很可能是我误解了标准,在这种情况下,它们将无法与相应的标准互操作。
themselves. It could well be that I misinterpreted the standards in which case
 
they will not be interoperable with the respective standards.
 
  
The <code>find*()</code> routines might look a bit funny at first sight. They are
+
<code>find*()</code> 程序乍一看可能有点滑稽。 它们主要用于消除回声。 一个相当快的方法是选择输出样本中最有活力的部分,在输入样本中找到它,然后从输入样本中减去整个输出样本:
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:
 
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第200行: 第166行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>def echocancel(outputdata, inputdata):
+
<syntaxhighlight lang="python3">def echocancel(outputdata, inputdata):
 
     pos = audioop.findmax(outputdata, 800)    # one tenth second
 
     pos = audioop.findmax(outputdata, 800)    # one tenth second
 
     out_test = outputdata[pos*2:]
 
     out_test = outputdata[pos*2:]
第211行: 第177行:
 
     postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata))
 
     postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata))
 
     outputdata = prefill + audioop.mul(outputdata, 2, -factor) + postfill
 
     outputdata = prefill + audioop.mul(outputdata, 2, -factor) + postfill
     return audioop.add(inputdata, outputdata, 2)</pre>
+
     return audioop.add(inputdata, outputdata, 2)</syntaxhighlight>
 +
 
 +
</div>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:51的最新版本

audioop — 处理原始音频数据


audioop 模块包含一些对声音片段有用的操作。 它对由 8、16、24 或 32 位宽的有符号整数样本组成的声音片段进行操作,这些样本存储在 字节类对象 中。 除非另有说明,否则所有标量项都是整数。

3.4 版更改: 添加了对 24 位样本的支持。 所有函数现在都接受任何 字节的对象 。 字符串输入现在会导致立即错误。


该模块支持 a-LAW、u-LAW 和 Intel/DVI ADPCM 编码。

一些更复杂的操作只需要 16 位样本,否则样本大小(以字节为单位)始终是操作的参数。

该模块定义了以下变量和函数:

exception audioop.error
所有错误都会引发此异常,例如每个样本的字节数未知等。
audioop.add(fragment1, fragment2, width)
返回一个片段,它是作为参数传递的两个样本的相加。 width 是以字节为单位的样本宽度,1234。 两个片段应该具有相同的长度。 在溢出的情况下截断样本。
audioop.adpcm2lin(adpcmfragment, width, state)
将 Intel/DVI ADPCM 编码片段解码为线性片段。 有关 ADPCM 编码的详细信息,请参阅 lin2adpcm() 的说明。 返回一个元组 (sample, newstate),其中样本具有 width 中指定的宽度。
audioop.alaw2lin(fragment, width)
将 a-LAW 编码的声音片段转换为线性编码的声音片段。 a-LAW 编码总是使用 8 位样本,所以这里的 width 仅指输出片段的样本宽度。
audioop.avg(fragment, width)
返回片段中所有样本的平均值。
audioop.avgpp(fragment, width)
返回片段中所有样本的平均峰峰值。 没有进行过滤,所以这个例程的用处值得怀疑。
audioop.bias(fragment, width, bias)
返回一个片段,该片段是原始片段,每个样本都添加了偏差。 样品环绕以防溢出。
audioop.byteswap(fragment, width)

“字节交换”片段中的所有样本并返回修改后的片段。 将大端样本转换为小端样本,反之亦然。

3.4 版中的新功能。

audioop.cross(fragment, width)
返回作为参数传递的片段中的零交叉数。
audioop.findfactor(fragment, reference)

返回一个因子 F 使得 rms(add(fragment, mul(reference, -F))) 最小,即返回你应该乘以 reference 以使其与 尽可能匹配的因子片段。 这些片段都应该包含 2 字节的样本。

此例程所花费的时间与 len(fragment) 成正比。

audioop.findfit(fragment, reference)
尝试将 reference 尽可能匹配到 fragment(应该是较长的片段)的一部分。 这是(概念上)通过从 fragment 中取出切片,使用 findfactor() 计算最佳匹配并最小化结果来完成的。 这些片段都应该包含 2 字节的样本。 返回一个元组 (offset, factor),其中 offsetfragment 的(整数)偏移量,其中最佳匹配开始,factor 是(浮点数) ) 因子根据 findfactor()
audioop.findmax(fragment, length)

fragment 中搜索具有最大能量的长度为 length 样本(不是字节!)的切片,即返回 i 其中 rms(fragment[i*2:(i+length)*2]) 是最大的. 这些片段都应该包含 2 字节的样本。

该例程花费的时间与 len(fragment) 成正比。

audioop.getsample(fragment, width, index)
从片段返回样本 index 的值。
audioop.lin2adpcm(fragment, width, state)

将样本转换为 4 位 Intel/DVI ADPCM 编码。 ADPCM 编码是一种自适应编码方案,其中每 4 位数字是一个样本和下一个样本之间的差异,除以(变化的)步长。 IMA 选择了 Intel/DVI ADPCM 算法,因此它很可能成为标准。

state 是一个包含编码器状态的元组。 编码器返回一个元组 (adpcmfrag, newstate),并且 newstate 应该传递给 lin2adpcm() 的下一次调用。 在初始调用中,None 可以作为状态传递。 adpcmfrag 是每字节 2 个 4 位值的 ADPCM 编码片段。

audioop.lin2alaw(fragment, width)
将音频片段中的样本转换为 a-LAW 编码并将其作为字节对象返回。 a-LAW 是一种音频编码格式,仅使用 8 位样本即可获得大约 13 位的动态范围。 它由 Sun 音频硬件等使用。
audioop.lin2lin(fragment, width, newwidth)

在 1、2、3 和 4 字节格式之间转换样本。

笔记

在某些音频格式中,例如 .WAV 文件,16、24 和 32 位样本是有符号的,但 8 位样本是无符号的。 因此,当为这些格式转换为 8 位宽的样本时,您还需要将 128 添加到结果中:

new_frames = audioop.lin2lin(frames, old_width, 1)
new_frames = audioop.bias(new_frames, 1, 128)

反过来,当将 8 位宽度样本转换为 16、24 或 32 位宽度样本时,必须应用相同的方法。

audioop.lin2ulaw(fragment, width)
将音频片段中的样本转换为 u-LAW 编码并将其作为字节对象返回。 u-LAW 是一种音频编码格式,仅使用 8 位样本即可获得大约 14 位的动态范围。 它由 Sun 音频硬件等使用。
audioop.max(fragment, width)
返回片段中所有样本的 绝对值 的最大值。
audioop.maxpp(fragment, width)
返回声音片段中的最大峰峰值。
audioop.minmax(fragment, width)
返回由声音片段中所有样本的最小值和最大值组成的元组。
audioop.mul(fragment, width, factor)
返回一个片段,其中原始片段中的所有样本都乘以浮点值 因子 。 在溢出的情况下截断样本。
audioop.ratecv(fragment, width, nchannels, inrate, outrate, state[, weightA[, weightB]])

转换输入片段的帧率。

state 是一个包含转换器状态的元组。 转换器返回一个元组 (newfragment, newstate),并且 newstate 应该传递给 ratecv() 的下一次调用。 初始调用应通过 None 作为状态。

weightAweightB 参数是简单数字滤波器的参数,分别默认为 10

audioop.reverse(fragment, width)
反转片段中的样本并返回修改后的片段。
audioop.rms(fragment, width)

返回片段的均方根,即 sqrt(sum(S_i^2)/n)

这是音频信号中功率的量度。

audioop.tomono(fragment, width, lfactor, rfactor)
将立体声片段转换为单声道片段。 左声道乘以lfactor,右声道乘以rfactor,然后将两个声道相加得到单声道信号。
audioop.tostereo(fragment, width, lfactor, rfactor)
从单声道片段生成立体声片段。 立体声片段中的每对样本都是从单声道样本中计算出来的,其中左声道样本乘以 lfactor,右声道样本乘以 rfactor
audioop.ulaw2lin(fragment, width)
将 u-LAW 编码的声音片段转换为线性编码的声音片段。 u-LAW 编码总是使用 8 位样本,所以这里的 width 仅指输出片段的样本宽度。

请注意,诸如 mul()max() 之类的操作不区分单声道和立体声片段,即 所有样品都被平等对待。 如果这是一个问题,立体声片段应首先拆分为两个单声道片段,然后再重新组合。 以下是如何执行此操作的示例:

def mul_stereo(sample, width, lfactor, rfactor):
    lsample = audioop.tomono(sample, width, 1, 0)
    rsample = audioop.tomono(sample, width, 0, 1)
    lsample = audioop.mul(lsample, width, lfactor)
    rsample = audioop.mul(rsample, width, rfactor)
    lsample = audioop.tostereo(lsample, width, 1, 0)
    rsample = audioop.tostereo(rsample, width, 0, 1)
    return audioop.add(lsample, rsample, width)

如果您使用 ADPCM 编码器构建网络数据包,并且您希望您的协议是无状态的(即 为了能够容忍丢包),您不仅应该传输数据,还应该传输状态。 请注意,您应该将 初始 状态(您传递给 lin2adpcm() 的状态)发送到解码器,而不是最终状态(由编码器返回)。 如果您想使用 struct.Struct 以二进制形式存储状态,您可以将第一个元素(预测值)编码为 16 位,将第二个(增量索引)编码为 8。

ADPCM 编码器从未与其他 ADPCM 编码器进行过对比,只是与它们自己进行过对比。 很可能是我误解了标准,在这种情况下,它们将无法与相应的标准互操作。

find*() 程序乍一看可能有点滑稽。 它们主要用于消除回声。 一个相当快的方法是选择输出样本中最有活力的部分,在输入样本中找到它,然后从输入样本中减去整个输出样本:

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)