-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudioop.html
More file actions
456 lines (409 loc) · 34.9 KB
/
audioop.html
File metadata and controls
456 lines (409 loc) · 34.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_TW">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>23.1. audioop — Manipulate raw audio data — Python 3.7.0 說明文件</title>
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/translations.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.7.0 說明文件 中搜尋"
href="../_static/opensearch.xml"/>
<link rel="author" title="關於這些文件" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜尋" href="../search.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="next" title="23.2. aifc — Read and write AIFF and AIFC files" href="aifc.html" />
<link rel="prev" title="23. Multimedia Services" href="mm.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
<link rel="canonical" href="https://docs.python.org/3/library/audioop.html" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/switchers.js"></script>
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>瀏覽</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="aifc.html" title="23.2. aifc — Read and write AIFF and AIFC files"
accesskey="N">下一頁</a> |</li>
<li class="right" >
<a href="mm.html" title="23. Multimedia Services"
accesskey="P">上一頁</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="language_switcher_placeholder">zh_TW</span>
<span class="version_switcher_placeholder">3.7.0</span>
<a href="../index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 標準函式庫 (Standard Library)</a> »</li>
<li class="nav-item nav-item-2"><a href="mm.html" accesskey="U">23. Multimedia Services</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="module-audioop">
<span id="audioop-manipulate-raw-audio-data"></span><h1>23.1. <a class="reference internal" href="#module-audioop" title="audioop: Manipulate raw audio data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">audioop</span></code></a> — Manipulate raw audio data<a class="headerlink" href="#module-audioop" title="本標題的永久連結">¶</a></h1>
<hr class="docutils" />
<p>The <a class="reference internal" href="#module-audioop" title="audioop: Manipulate raw audio data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">audioop</span></code></a> 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 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a>. All scalar items are
integers, unless specified otherwise.</p>
<div class="versionchanged">
<p><span class="versionmodified">3.4 版更變: </span>Support for 24-bit samples was added.
All functions now accept any <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>.
String input now results in an immediate error.</p>
</div>
<p id="index-0">This module provides support for a-LAW, u-LAW and Intel/DVI ADPCM encodings.</p>
<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>
<p>The module defines the following variables and functions:</p>
<dl class="exception">
<dt id="audioop.error">
<em class="property">exception </em><code class="descclassname">audioop.</code><code class="descname">error</code><a class="headerlink" href="#audioop.error" title="本定義的永久連結">¶</a></dt>
<dd><p>This exception is raised on all errors, such as unknown number of bytes per
sample, etc.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.add">
<code class="descclassname">audioop.</code><code class="descname">add</code><span class="sig-paren">(</span><em>fragment1</em>, <em>fragment2</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.add" title="本定義的永久連結">¶</a></dt>
<dd><p>Return a fragment which is the addition of the two samples passed as parameters.
<em>width</em> is the sample width in bytes, either <code class="docutils literal notranslate"><span class="pre">1</span></code>, <code class="docutils literal notranslate"><span class="pre">2</span></code>, <code class="docutils literal notranslate"><span class="pre">3</span></code> or <code class="docutils literal notranslate"><span class="pre">4</span></code>. Both
fragments should have the same length. Samples are truncated in case of overflow.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.adpcm2lin">
<code class="descclassname">audioop.</code><code class="descname">adpcm2lin</code><span class="sig-paren">(</span><em>adpcmfragment</em>, <em>width</em>, <em>state</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.adpcm2lin" title="本定義的永久連結">¶</a></dt>
<dd><p>Decode an Intel/DVI ADPCM coded fragment to a linear fragment. See the
description of <a class="reference internal" href="#audioop.lin2adpcm" title="audioop.lin2adpcm"><code class="xref py py-func docutils literal notranslate"><span class="pre">lin2adpcm()</span></code></a> for details on ADPCM coding. Return a tuple
<code class="docutils literal notranslate"><span class="pre">(sample,</span> <span class="pre">newstate)</span></code> where the sample has the width specified in <em>width</em>.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.alaw2lin">
<code class="descclassname">audioop.</code><code class="descname">alaw2lin</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.alaw2lin" title="本定義的永久連結">¶</a></dt>
<dd><p>Convert sound fragments in a-LAW encoding to linearly encoded sound fragments.
a-LAW encoding always uses 8 bits samples, so <em>width</em> refers only to the sample
width of the output fragment here.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.avg">
<code class="descclassname">audioop.</code><code class="descname">avg</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.avg" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the average over all samples in the fragment.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.avgpp">
<code class="descclassname">audioop.</code><code class="descname">avgpp</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.avgpp" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the average peak-peak value over all samples in the fragment. No
filtering is done, so the usefulness of this routine is questionable.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.bias">
<code class="descclassname">audioop.</code><code class="descname">bias</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>bias</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.bias" title="本定義的永久連結">¶</a></dt>
<dd><p>Return a fragment that is the original fragment with a bias added to each
sample. Samples wrap around in case of overflow.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.byteswap">
<code class="descclassname">audioop.</code><code class="descname">byteswap</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.byteswap" title="本定義的永久連結">¶</a></dt>
<dd><p>「Byteswap」 all samples in a fragment and returns the modified fragment.
Converts big-endian samples to little-endian and vice versa.</p>
<div class="versionadded">
<p><span class="versionmodified">3.4 版新加入.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="audioop.cross">
<code class="descclassname">audioop.</code><code class="descname">cross</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.cross" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the number of zero crossings in the fragment passed as an argument.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.findfactor">
<code class="descclassname">audioop.</code><code class="descname">findfactor</code><span class="sig-paren">(</span><em>fragment</em>, <em>reference</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.findfactor" title="本定義的永久連結">¶</a></dt>
<dd><p>Return a factor <em>F</em> such that <code class="docutils literal notranslate"><span class="pre">rms(add(fragment,</span> <span class="pre">mul(reference,</span> <span class="pre">-F)))</span></code> is
minimal, i.e., return the factor with which you should multiply <em>reference</em> to
make it match as well as possible to <em>fragment</em>. The fragments should both
contain 2-byte samples.</p>
<p>The time taken by this routine is proportional to <code class="docutils literal notranslate"><span class="pre">len(fragment)</span></code>.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.findfit">
<code class="descclassname">audioop.</code><code class="descname">findfit</code><span class="sig-paren">(</span><em>fragment</em>, <em>reference</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.findfit" title="本定義的永久連結">¶</a></dt>
<dd><p>Try to match <em>reference</em> as well as possible to a portion of <em>fragment</em> (which
should be the longer fragment). This is (conceptually) done by taking slices
out of <em>fragment</em>, using <a class="reference internal" href="#audioop.findfactor" title="audioop.findfactor"><code class="xref py py-func docutils literal notranslate"><span class="pre">findfactor()</span></code></a> to compute the best match, and
minimizing the result. The fragments should both contain 2-byte samples.
Return a tuple <code class="docutils literal notranslate"><span class="pre">(offset,</span> <span class="pre">factor)</span></code> where <em>offset</em> is the (integer) offset into
<em>fragment</em> where the optimal match started and <em>factor</em> is the (floating-point)
factor as per <a class="reference internal" href="#audioop.findfactor" title="audioop.findfactor"><code class="xref py py-func docutils literal notranslate"><span class="pre">findfactor()</span></code></a>.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.findmax">
<code class="descclassname">audioop.</code><code class="descname">findmax</code><span class="sig-paren">(</span><em>fragment</em>, <em>length</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.findmax" title="本定義的永久連結">¶</a></dt>
<dd><p>Search <em>fragment</em> for a slice of length <em>length</em> samples (not bytes!) with
maximum energy, i.e., return <em>i</em> for which <code class="docutils literal notranslate"><span class="pre">rms(fragment[i*2:(i+length)*2])</span></code>
is maximal. The fragments should both contain 2-byte samples.</p>
<p>The routine takes time proportional to <code class="docutils literal notranslate"><span class="pre">len(fragment)</span></code>.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.getsample">
<code class="descclassname">audioop.</code><code class="descname">getsample</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>index</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.getsample" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the value of sample <em>index</em> from the fragment.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.lin2adpcm">
<code class="descclassname">audioop.</code><code class="descname">lin2adpcm</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>state</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.lin2adpcm" title="本定義的永久連結">¶</a></dt>
<dd><p>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>
<p><em>state</em> is a tuple containing the state of the coder. The coder returns a tuple
<code class="docutils literal notranslate"><span class="pre">(adpcmfrag,</span> <span class="pre">newstate)</span></code>, and the <em>newstate</em> should be passed to the next call
of <a class="reference internal" href="#audioop.lin2adpcm" title="audioop.lin2adpcm"><code class="xref py py-func docutils literal notranslate"><span class="pre">lin2adpcm()</span></code></a>. In the initial call, <code class="docutils literal notranslate"><span class="pre">None</span></code> can be passed as the state.
<em>adpcmfrag</em> is the ADPCM coded fragment packed 2 4-bit values per byte.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.lin2alaw">
<code class="descclassname">audioop.</code><code class="descname">lin2alaw</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.lin2alaw" title="本定義的永久連結">¶</a></dt>
<dd><p>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.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.lin2lin">
<code class="descclassname">audioop.</code><code class="descname">lin2lin</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>newwidth</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.lin2lin" title="本定義的永久連結">¶</a></dt>
<dd><p>Convert samples between 1-, 2-, 3- and 4-byte formats.</p>
<div class="admonition note">
<p class="first admonition-title">備註</p>
<p>In some audio formats, such as .WAV files, 16, 24 and 32 bit samples are
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"><pre><span></span><span class="n">new_frames</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">lin2lin</span><span class="p">(</span><span class="n">frames</span><span class="p">,</span> <span class="n">old_width</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">new_frames</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">bias</span><span class="p">(</span><span class="n">new_frames</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">128</span><span class="p">)</span>
</pre></div>
</div>
<p class="last">The same, in reverse, has to be applied when converting from 8 to 16, 24
or 32 bit width samples.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="audioop.lin2ulaw">
<code class="descclassname">audioop.</code><code class="descname">lin2ulaw</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.lin2ulaw" title="本定義的永久連結">¶</a></dt>
<dd><p>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.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.max">
<code class="descclassname">audioop.</code><code class="descname">max</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.max" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the maximum of the <em>absolute value</em> of all samples in a fragment.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.maxpp">
<code class="descclassname">audioop.</code><code class="descname">maxpp</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.maxpp" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the maximum peak-peak value in the sound fragment.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.minmax">
<code class="descclassname">audioop.</code><code class="descname">minmax</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.minmax" title="本定義的永久連結">¶</a></dt>
<dd><p>Return a tuple consisting of the minimum and maximum values of all samples in
the sound fragment.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.mul">
<code class="descclassname">audioop.</code><code class="descname">mul</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>factor</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.mul" title="本定義的永久連結">¶</a></dt>
<dd><p>Return a fragment that has all samples in the original fragment multiplied by
the floating-point value <em>factor</em>. Samples are truncated in case of overflow.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.ratecv">
<code class="descclassname">audioop.</code><code class="descname">ratecv</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>nchannels</em>, <em>inrate</em>, <em>outrate</em>, <em>state</em><span class="optional">[</span>, <em>weightA</em><span class="optional">[</span>, <em>weightB</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#audioop.ratecv" title="本定義的永久連結">¶</a></dt>
<dd><p>Convert the frame rate of the input fragment.</p>
<p><em>state</em> is a tuple containing the state of the converter. The converter returns
a tuple <code class="docutils literal notranslate"><span class="pre">(newfragment,</span> <span class="pre">newstate)</span></code>, and <em>newstate</em> should be passed to the next
call of <a class="reference internal" href="#audioop.ratecv" title="audioop.ratecv"><code class="xref py py-func docutils literal notranslate"><span class="pre">ratecv()</span></code></a>. The initial call should pass <code class="docutils literal notranslate"><span class="pre">None</span></code> as the state.</p>
<p>The <em>weightA</em> and <em>weightB</em> arguments are parameters for a simple digital filter
and default to <code class="docutils literal notranslate"><span class="pre">1</span></code> and <code class="docutils literal notranslate"><span class="pre">0</span></code> respectively.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.reverse">
<code class="descclassname">audioop.</code><code class="descname">reverse</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.reverse" title="本定義的永久連結">¶</a></dt>
<dd><p>Reverse the samples in a fragment and returns the modified fragment.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.rms">
<code class="descclassname">audioop.</code><code class="descname">rms</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.rms" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the root-mean-square of the fragment, i.e. <code class="docutils literal notranslate"><span class="pre">sqrt(sum(S_i^2)/n)</span></code>.</p>
<p>This is a measure of the power in an audio signal.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.tomono">
<code class="descclassname">audioop.</code><code class="descname">tomono</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>lfactor</em>, <em>rfactor</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.tomono" title="本定義的永久連結">¶</a></dt>
<dd><p>Convert a stereo fragment to a mono fragment. The left channel is multiplied by
<em>lfactor</em> and the right channel by <em>rfactor</em> before adding the two channels to
give a mono signal.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.tostereo">
<code class="descclassname">audioop.</code><code class="descname">tostereo</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em>, <em>lfactor</em>, <em>rfactor</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.tostereo" title="本定義的永久連結">¶</a></dt>
<dd><p>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 <em>lfactor</em> and right channel samples by <em>rfactor</em>.</p>
</dd></dl>
<dl class="function">
<dt id="audioop.ulaw2lin">
<code class="descclassname">audioop.</code><code class="descname">ulaw2lin</code><span class="sig-paren">(</span><em>fragment</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.ulaw2lin" title="本定義的永久連結">¶</a></dt>
<dd><p>Convert sound fragments in u-LAW encoding to linearly encoded sound fragments.
u-LAW encoding always uses 8 bits samples, so <em>width</em> refers only to the sample
width of the output fragment here.</p>
</dd></dl>
<p>Note that operations such as <a class="reference internal" href="#audioop.mul" title="audioop.mul"><code class="xref py py-func docutils literal notranslate"><span class="pre">mul()</span></code></a> or <a class="reference internal" href="#audioop.max" title="audioop.max"><code class="xref py py-func docutils literal notranslate"><span class="pre">max()</span></code></a> 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>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">mul_stereo</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="n">lfactor</span><span class="p">,</span> <span class="n">rfactor</span><span class="p">):</span>
<span class="n">lsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">tomono</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">rsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">tomono</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">lsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">mul</span><span class="p">(</span><span class="n">lsample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="n">lfactor</span><span class="p">)</span>
<span class="n">rsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">mul</span><span class="p">(</span><span class="n">rsample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="n">rfactor</span><span class="p">)</span>
<span class="n">lsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">tostereo</span><span class="p">(</span><span class="n">lsample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">rsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">tostereo</span><span class="p">(</span><span class="n">rsample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">return</span> <span class="n">audioop</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">lsample</span><span class="p">,</span> <span class="n">rsample</span><span class="p">,</span> <span class="n">width</span><span class="p">)</span>
</pre></div>
</div>
<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 <em>initial</em>
state (the one you passed to <a class="reference internal" href="#audioop.lin2adpcm" title="audioop.lin2adpcm"><code class="xref py py-func docutils literal notranslate"><span class="pre">lin2adpcm()</span></code></a>) along to the decoder, not the
final state (as returned by the coder). If you want to use
<a class="reference internal" href="struct.html#struct.Struct" title="struct.Struct"><code class="xref py py-class docutils literal notranslate"><span class="pre">struct.Struct</span></code></a> 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>
<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>
<p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">find*()</span></code> 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>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">echocancel</span><span class="p">(</span><span class="n">outputdata</span><span class="p">,</span> <span class="n">inputdata</span><span class="p">):</span>
<span class="n">pos</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">findmax</span><span class="p">(</span><span class="n">outputdata</span><span class="p">,</span> <span class="mi">800</span><span class="p">)</span> <span class="c1"># one tenth second</span>
<span class="n">out_test</span> <span class="o">=</span> <span class="n">outputdata</span><span class="p">[</span><span class="n">pos</span><span class="o">*</span><span class="mi">2</span><span class="p">:]</span>
<span class="n">in_test</span> <span class="o">=</span> <span class="n">inputdata</span><span class="p">[</span><span class="n">pos</span><span class="o">*</span><span class="mi">2</span><span class="p">:]</span>
<span class="n">ipos</span><span class="p">,</span> <span class="n">factor</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">findfit</span><span class="p">(</span><span class="n">in_test</span><span class="p">,</span> <span class="n">out_test</span><span class="p">)</span>
<span class="c1"># Optional (for better cancellation):</span>
<span class="c1"># factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],</span>
<span class="c1"># out_test)</span>
<span class="n">prefill</span> <span class="o">=</span> <span class="s1">'</span><span class="se">\0</span><span class="s1">'</span><span class="o">*</span><span class="p">(</span><span class="n">pos</span><span class="o">+</span><span class="n">ipos</span><span class="p">)</span><span class="o">*</span><span class="mi">2</span>
<span class="n">postfill</span> <span class="o">=</span> <span class="s1">'</span><span class="se">\0</span><span class="s1">'</span><span class="o">*</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">inputdata</span><span class="p">)</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">prefill</span><span class="p">)</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">outputdata</span><span class="p">))</span>
<span class="n">outputdata</span> <span class="o">=</span> <span class="n">prefill</span> <span class="o">+</span> <span class="n">audioop</span><span class="o">.</span><span class="n">mul</span><span class="p">(</span><span class="n">outputdata</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="n">factor</span><span class="p">)</span> <span class="o">+</span> <span class="n">postfill</span>
<span class="k">return</span> <span class="n">audioop</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">inputdata</span><span class="p">,</span> <span class="n">outputdata</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h4>上個主題</h4>
<p class="topless"><a href="mm.html"
title="上一章">23. Multimedia Services</a></p>
<h4>下個主題</h4>
<p class="topless"><a href="aifc.html"
title="下一章">23.2. <code class="docutils literal notranslate"><span class="pre">aifc</span></code> — Read and write AIFF and AIFC files</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.7/Doc/library/audioop.rst"
rel="nofollow">Show Source
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>瀏覽</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="aifc.html" title="23.2. aifc — Read and write AIFF and AIFC files"
>下一頁</a> |</li>
<li class="right" >
<a href="mm.html" title="23. Multimedia Services"
>上一頁</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="language_switcher_placeholder">zh_TW</span>
<span class="version_switcher_placeholder">3.7.0</span>
<a href="../index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 標準函式庫 (Standard Library)</a> »</li>
<li class="nav-item nav-item-2"><a href="mm.html" >23. Multimedia Services</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Copyright</a> 2001-2018, Python Software Foundation.
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
Last updated on 8月 22, 2018.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.7.
</div>
</body>
</html>