“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> | ||
− | = | + | = audioop — 处理原始音频数据 = |
− | + | ||
− | + | ----- | |
− | + | ||
− | + | [[#module-audioop|audioop]] 模块包含一些对声音片段有用的操作。 它对由 8、16、24 或 32 位宽的有符号整数样本组成的声音片段进行操作,这些样本存储在 [[../../glossary#term-bytes-like-object|字节类对象]] 中。 除非另有说明,否则所有标量项都是整数。 | |
<div class="versionchanged"> | <div class="versionchanged"> | ||
− | <span class="versionmodified changed"> | + | <span class="versionmodified changed"> 3.4 版更改: </span> 添加了对 24 位样本的支持。 所有函数现在都接受任何 [[../../glossary#term-bytes-like-object|字节的对象]] 。 字符串输入现在会导致立即错误。 |
− | |||
− | |||
</div> | </div> | ||
− | + | 该模块支持 a-LAW、u-LAW 和 Intel/DVI ADPCM 编码。 | |
− | + | 一些更复杂的操作只需要 16 位样本,否则样本大小(以字节为单位)始终是操作的参数。 | |
− | |||
− | + | 该模块定义了以下变量和函数: | |
− | ; ''exception'' < | + | ; ''<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> |
− | : | + | : 所有错误都会引发此异常,例如每个样本的字节数未知等。 |
− | ; < | + | ; <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> |
− | : | + | : 返回一个片段,它是作为参数传递的两个样本的相加。 ''width'' 是以字节为单位的样本宽度,<code>1</code>、<code>2</code>、<code>3</code> 或 <code>4</code>。 两个片段应该具有相同的长度。 在溢出的情况下截断样本。 |
− | ; < | + | ; <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> |
− | : | + | : 将 Intel/DVI ADPCM 编码片段解码为线性片段。 有关 ADPCM 编码的详细信息,请参阅 [[#audioop.lin2adpcm|lin2adpcm()]] 的说明。 返回一个元组 <code>(sample, newstate)</code>,其中样本具有 ''width'' 中指定的宽度。 |
− | ; < | + | ; <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> |
− | : | + | : 将 a-LAW 编码的声音片段转换为线性编码的声音片段。 a-LAW 编码总是使用 8 位样本,所以这里的 ''width'' 仅指输出片段的样本宽度。 |
− | ; < | + | ; <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> |
− | : | + | : 返回片段中所有样本的平均值。 |
− | ; < | + | ; <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> |
− | : | + | : 返回片段中所有样本的平均峰峰值。 没有进行过滤,所以这个例程的用处值得怀疑。 |
− | ; < | + | ; <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> |
− | : | + | : 返回一个片段,该片段是原始片段,每个样本都添加了偏差。 样品环绕以防溢出。 |
<dl> | <dl> | ||
− | <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> | + | <dd><p>“字节交换”片段中的所有样本并返回修改后的片段。 将大端样本转换为小端样本,反之亦然。</p> |
− | |||
<div class="versionadded"> | <div class="versionadded"> | ||
− | <p><span class="versionmodified added">3.4 | + | <p><span class="versionmodified added">3.4 版中的新功能。</span></p> |
</div></dd></dl> | </div></dd></dl> | ||
− | ; < | + | ; <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> |
− | : | + | : 返回作为参数传递的片段中的零交叉数。 |
<dl> | <dl> | ||
− | <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> | + | <dd><p>返回一个因子 ''F'' 使得 <code>rms(add(fragment, mul(reference, -F)))</code> 最小,即返回你应该乘以 ''reference'' 以使其与 ''尽可能匹配的因子片段''。 这些片段都应该包含 2 字节的样本。</p> |
− | + | <p>此例程所花费的时间与 <code>len(fragment)</code> 成正比。</p></dd></dl> | |
− | |||
− | |||
− | <p> | ||
− | ; < | + | ; <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> |
− | : | + | : 尝试将 ''reference'' 尽可能匹配到 ''fragment''(应该是较长的片段)的一部分。 这是(概念上)通过从 ''fragment'' 中取出切片,使用 [[#audioop.findfactor|findfactor()]] 计算最佳匹配并最小化结果来完成的。 这些片段都应该包含 2 字节的样本。 返回一个元组 <code>(offset, factor)</code>,其中 ''offset'' 是 ''fragment'' 的(整数)偏移量,其中最佳匹配开始,''factor'' 是(浮点数) ) 因子根据 [[#audioop.findfactor|findfactor()]]。 |
<dl> | <dl> | ||
− | <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> | + | <dd><p>在 ''fragment'' 中搜索具有最大能量的长度为 ''length'' 样本(不是字节!)的切片,即返回 ''i'' 其中 <code>rms(fragment[i*2:(i+length)*2])</code> 是最大的. 这些片段都应该包含 2 字节的样本。</p> |
− | + | <p>该例程花费的时间与 <code>len(fragment)</code> 成正比。</p></dd></dl> | |
− | |||
− | <p> | ||
− | ; < | + | ; <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> |
− | : | + | : 从片段返回样本 ''index'' 的值。 |
<dl> | <dl> | ||
− | <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> | + | <dd><p>将样本转换为 4 位 Intel/DVI ADPCM 编码。 ADPCM 编码是一种自适应编码方案,其中每 4 位数字是一个样本和下一个样本之间的差异,除以(变化的)步长。 IMA 选择了 Intel/DVI ADPCM 算法,因此它很可能成为标准。</p> |
− | + | <p>''state'' 是一个包含编码器状态的元组。 编码器返回一个元组 <code>(adpcmfrag, newstate)</code>,并且 ''newstate'' 应该传递给 [[#audioop.lin2adpcm|lin2adpcm()]] 的下一次调用。 在初始调用中,<code>None</code> 可以作为状态传递。 ''adpcmfrag'' 是每字节 2 个 4 位值的 ADPCM 编码片段。</p></dd></dl> | |
− | |||
− | |||
− | <p>''state'' | ||
− | <code>(adpcmfrag, newstate)</code> | ||
− | |||
− | ''adpcmfrag'' | ||
− | ; < | + | ; <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> |
− | : | + | : 将音频片段中的样本转换为 a-LAW 编码并将其作为字节对象返回。 a-LAW 是一种音频编码格式,仅使用 8 位样本即可获得大约 13 位的动态范围。 它由 Sun 音频硬件等使用。 |
<dl> | <dl> | ||
− | <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> | + | <dd><p>在 1、2、3 和 4 字节格式之间转换样本。</p> |
<div class="admonition note"> | <div class="admonition note"> | ||
− | <p> | + | <p>笔记</p> |
− | <p> | + | <p>在某些音频格式中,例如 .WAV 文件,16、24 和 32 位样本是有符号的,但 8 位样本是无符号的。 因此,当为这些格式转换为 8 位宽的样本时,您还需要将 128 添加到结果中:</p> |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">new_frames = audioop.lin2lin(frames, old_width, 1) |
− | new_frames = audioop.bias(new_frames, 1, 128)</ | + | new_frames = audioop.bias(new_frames, 1, 128)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | <p> | + | <p>反过来,当将 8 位宽度样本转换为 16、24 或 32 位宽度样本时,必须应用相同的方法。</p> |
− | |||
</div></dd></dl> | </div></dd></dl> | ||
− | ; < | + | ; <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> |
− | : | + | : 将音频片段中的样本转换为 u-LAW 编码并将其作为字节对象返回。 u-LAW 是一种音频编码格式,仅使用 8 位样本即可获得大约 14 位的动态范围。 它由 Sun 音频硬件等使用。 |
− | ; < | + | ; <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> |
− | : | + | : 返回片段中所有样本的 ''绝对值'' 的最大值。 |
− | ; < | + | ; <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> |
− | : | + | : 返回声音片段中的最大峰峰值。 |
− | ; < | + | ; <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> |
− | : | + | : 返回由声音片段中所有样本的最小值和最大值组成的元组。 |
− | ; < | + | ; <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> |
− | : | + | : 返回一个片段,其中原始片段中的所有样本都乘以浮点值 ''因子'' 。 在溢出的情况下截断样本。 |
<dl> | <dl> | ||
− | <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> | + | <dd><p>转换输入片段的帧率。</p> |
− | <p>''state'' | + | <p>''state'' 是一个包含转换器状态的元组。 转换器返回一个元组 <code>(newfragment, newstate)</code>,并且 ''newstate'' 应该传递给 [[#audioop.ratecv|ratecv()]] 的下一次调用。 初始调用应通过 <code>None</code> 作为状态。</p> |
− | + | <p>''weightA'' 和 ''weightB'' 参数是简单数字滤波器的参数,分别默认为 <code>1</code> 和 <code>0</code>。</p></dd></dl> | |
− | |||
− | <p> | ||
− | |||
− | ; < | + | ; <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> |
− | : | + | : 反转片段中的样本并返回修改后的片段。 |
<dl> | <dl> | ||
− | <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> | + | <dd><p>返回片段的均方根,即 <code>sqrt(sum(S_i^2)/n)</code>。</p> |
− | <p> | + | <p>这是音频信号中功率的量度。</p></dd></dl> |
− | ; < | + | ; <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> |
− | : | + | : 将立体声片段转换为单声道片段。 左声道乘以''lfactor'',右声道乘以''rfactor'',然后将两个声道相加得到单声道信号。 |
− | ; < | + | ; <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> |
− | : | + | : 从单声道片段生成立体声片段。 立体声片段中的每对样本都是从单声道样本中计算出来的,其中左声道样本乘以 ''lfactor'',右声道样本乘以 ''rfactor''。 |
− | ; < | + | ; <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> |
− | : | + | : 将 u-LAW 编码的声音片段转换为线性编码的声音片段。 u-LAW 编码总是使用 8 位样本,所以这里的 ''width'' 仅指输出片段的样本宽度。 |
− | + | 请注意,诸如 [[#audioop.mul|mul()]] 或 [[#audioop.max|max()]] 之类的操作不区分单声道和立体声片段,即 所有样品都被平等对待。 如果这是一个问题,立体声片段应首先拆分为两个单声道片段,然后再重新组合。 以下是如何执行此操作的示例: | |
− | |||
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第167行: | 第144行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <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)</ | + | return audioop.add(lsample, rsample, width)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 如果您使用 ADPCM 编码器构建网络数据包,并且您希望您的协议是无状态的(即 为了能够容忍丢包),您不仅应该传输数据,还应该传输状态。 请注意,您应该将 ''初始'' 状态(您传递给 [[#audioop.lin2adpcm|lin2adpcm()]] 的状态)发送到解码器,而不是最终状态(由编码器返回)。 如果您想使用 [[../struct#struct|struct.Struct]] 以二进制形式存储状态,您可以将第一个元素(预测值)编码为 16 位,将第二个(增量索引)编码为 8。 | |
− | |||
− | |||
− | |||
− | |||
− | [[../struct#struct| | ||
− | |||
− | + | ADPCM 编码器从未与其他 ADPCM 编码器进行过对比,只是与它们自己进行过对比。 很可能是我误解了标准,在这种情况下,它们将无法与相应的标准互操作。 | |
− | |||
− | |||
− | + | <code>find*()</code> 程序乍一看可能有点滑稽。 它们主要用于消除回声。 一个相当快的方法是选择输出样本中最有活力的部分,在输入样本中找到它,然后从输入样本中减去整个输出样本: | |
− | |||
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第200行: | 第166行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <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)</ | + | 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 是以字节为单位的样本宽度,
1
、2
、3
或4
。 两个片段应该具有相同的长度。 在溢出的情况下截断样本。
- 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)
,其中 offset 是 fragment 的(整数)偏移量,其中最佳匹配开始,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 添加到结果中:
反过来,当将 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
作为状态。weightA 和 weightB 参数是简单数字滤波器的参数,分别默认为
1
和0
。
- 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() 之类的操作不区分单声道和立体声片段,即 所有样品都被平等对待。 如果这是一个问题,立体声片段应首先拆分为两个单声道片段,然后再重新组合。 以下是如何执行此操作的示例:
如果您使用 ADPCM 编码器构建网络数据包,并且您希望您的协议是无状态的(即 为了能够容忍丢包),您不仅应该传输数据,还应该传输状态。 请注意,您应该将 初始 状态(您传递给 lin2adpcm() 的状态)发送到解码器,而不是最终状态(由编码器返回)。 如果您想使用 struct.Struct 以二进制形式存储状态,您可以将第一个元素(预测值)编码为 16 位,将第二个(增量索引)编码为 8。
ADPCM 编码器从未与其他 ADPCM 编码器进行过对比,只是与它们自己进行过对比。 很可能是我误解了标准,在这种情况下,它们将无法与相应的标准互操作。
find*()
程序乍一看可能有点滑稽。 它们主要用于消除回声。 一个相当快的方法是选择输出样本中最有活力的部分,在输入样本中找到它,然后从输入样本中减去整个输出样本: