rest25/library/audioop.rst => rest262/library/audioop.rst
15.. index::
16   single: Intel/DVI ADPCM
17   single: ADPCM, Intel/DVI
18   single: a-LAW
19   single: u-LAW
20
21This module provides support for a-LAW, u-LAW and Intel/DVI ADPCM encodings.
22
n23-.. % This para is mostly here to provide an excuse for the index entries...
n23+.. This para is mostly here to provide an excuse for the index entries...
24
25A few of the more complicated operations only take 16-bit samples, otherwise the
26sample size (in bytes) is always a parameter of the operation.
27
28The module defines the following variables and functions:
29
30
31.. exception:: error
136
137   .. versionadded:: 2.5
138
139
140.. function:: lin2lin(fragment, width, newwidth)
141
142   Convert samples between 1-, 2- and 4-byte formats.
143
n144+   .. note::
145+ 
146+      In some audio formats, such as .WAV files, 16 and 32 bit samples are
147+      signed, but 8 bit samples are unsigned.  So when converting to 8 bit wide
148+      samples for these formats, you need to also add 128 to the result::
149+ 
150+         new_frames = audioop.lin2lin(frames, old_width, 1)
151+         new_frames = audioop.bias(new_frames, 1, 128)
152+ 
153+      The same, in reverse, has to be applied when converting from 8 to 16 or 32
154+      bit width samples.
155+ 
144
145.. function:: lin2ulaw(fragment, width)
146
147   Convert samples in the audio fragment to u-LAW encoding and return this as a
148   Python string.  u-LAW is an audio encoding format whereby you get a dynamic
149   range of about 14 bits using only 8 bit samples.  It is used by the Sun audio
150   hardware, among others.
151
186
187.. function:: reverse(fragment, width)
188
189   Reverse the samples in a fragment and returns the modified fragment.
190
191
192.. function:: rms(fragment, width)
193
n194-   Return the root-mean-square of the fragment, i.e.  ::
n206+   Return the root-mean-square of the fragment, i.e. ``sqrt(sum(S_i^2)/n)``.
195- 
196-      XXX: translate this math
197-      \catcode`_=8
198-      \sqrt{\frac{\sum{{S_{i}}^{2}}}{n}}
199
200   This is a measure of the power in an audio signal.
201
202
203.. function:: tomono(fragment, width, lfactor, rfactor)
204
205   Convert a stereo fragment to a mono fragment.  The left channel is multiplied by
206   *lfactor* and the right channel by *rfactor* before adding the two channels to
252sample and subtract the whole output sample from the input sample::
253
254   def echocancel(outputdata, inputdata):
255       pos = audioop.findmax(outputdata, 800)    # one tenth second
256       out_test = outputdata[pos*2:]
257       in_test = inputdata[pos*2:]
258       ipos, factor = audioop.findfit(in_test, out_test)
259       # Optional (for better cancellation):
t260-       # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)], 
t268+       # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],
261       #              out_test)
262       prefill = '\0'*(pos+ipos)*2
263       postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata))
264       outputdata = prefill + audioop.mul(outputdata,2,-factor) + postfill
265       return audioop.add(inputdata, outputdata, 2)
266
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op