2 :mod:`audioop` --- Manipulate raw audio data
3 ============================================
6 :synopsis: Manipulate raw audio data.
9 The :mod:`audioop` module contains some useful operations on sound fragments.
10 It operates on sound fragments consisting of signed integer samples 8, 16 or 32
11 bits wide, stored in Python strings. This is the same format as used by the
12 :mod:`al` and :mod:`sunaudiodev` modules. All scalar items are integers, unless
16 single: Intel/DVI ADPCM
17 single: ADPCM, Intel/DVI
21 This module provides support for a-LAW, u-LAW and Intel/DVI ADPCM encodings.
23 .. This para is mostly here to provide an excuse for the index entries...
25 A few of the more complicated operations only take 16-bit samples, otherwise the
26 sample size (in bytes) is always a parameter of the operation.
28 The module defines the following variables and functions:
33 This exception is raised on all errors, such as unknown number of bytes per
37 .. function:: add(fragment1, fragment2, width)
39 Return a fragment which is the addition of the two samples passed as parameters.
40 *width* is the sample width in bytes, either ``1``, ``2`` or ``4``. Both
41 fragments should have the same length.
44 .. function:: adpcm2lin(adpcmfragment, width, state)
46 Decode an Intel/DVI ADPCM coded fragment to a linear fragment. See the
47 description of :func:`lin2adpcm` for details on ADPCM coding. Return a tuple
48 ``(sample, newstate)`` where the sample has the width specified in *width*.
51 .. function:: alaw2lin(fragment, width)
53 Convert sound fragments in a-LAW encoding to linearly encoded sound fragments.
54 a-LAW encoding always uses 8 bits samples, so *width* refers only to the sample
55 width of the output fragment here.
60 .. function:: avg(fragment, width)
62 Return the average over all samples in the fragment.
65 .. function:: avgpp(fragment, width)
67 Return the average peak-peak value over all samples in the fragment. No
68 filtering is done, so the usefulness of this routine is questionable.
71 .. function:: bias(fragment, width, bias)
73 Return a fragment that is the original fragment with a bias added to each
77 .. function:: cross(fragment, width)
79 Return the number of zero crossings in the fragment passed as an argument.
82 .. function:: findfactor(fragment, reference)
84 Return a factor *F* such that ``rms(add(fragment, mul(reference, -F)))`` is
85 minimal, i.e., return the factor with which you should multiply *reference* to
86 make it match as well as possible to *fragment*. The fragments should both
87 contain 2-byte samples.
89 The time taken by this routine is proportional to ``len(fragment)``.
92 .. function:: findfit(fragment, reference)
94 Try to match *reference* as well as possible to a portion of *fragment* (which
95 should be the longer fragment). This is (conceptually) done by taking slices
96 out of *fragment*, using :func:`findfactor` to compute the best match, and
97 minimizing the result. The fragments should both contain 2-byte samples.
98 Return a tuple ``(offset, factor)`` where *offset* is the (integer) offset into
99 *fragment* where the optimal match started and *factor* is the (floating-point)
100 factor as per :func:`findfactor`.
103 .. function:: findmax(fragment, length)
105 Search *fragment* for a slice of length *length* samples (not bytes!) with
106 maximum energy, i.e., return *i* for which ``rms(fragment[i*2:(i+length)*2])``
107 is maximal. The fragments should both contain 2-byte samples.
109 The routine takes time proportional to ``len(fragment)``.
112 .. function:: getsample(fragment, width, index)
114 Return the value of sample *index* from the fragment.
117 .. function:: lin2adpcm(fragment, width, state)
119 Convert samples to 4 bit Intel/DVI ADPCM encoding. ADPCM coding is an adaptive
120 coding scheme, whereby each 4 bit number is the difference between one sample
121 and the next, divided by a (varying) step. The Intel/DVI ADPCM algorithm has
122 been selected for use by the IMA, so it may well become a standard.
124 *state* is a tuple containing the state of the coder. The coder returns a tuple
125 ``(adpcmfrag, newstate)``, and the *newstate* should be passed to the next call
126 of :func:`lin2adpcm`. In the initial call, ``None`` can be passed as the state.
127 *adpcmfrag* is the ADPCM coded fragment packed 2 4-bit values per byte.
130 .. function:: lin2alaw(fragment, width)
132 Convert samples in the audio fragment to a-LAW encoding and return this as a
133 Python string. a-LAW is an audio encoding format whereby you get a dynamic
134 range of about 13 bits using only 8 bit samples. It is used by the Sun audio
135 hardware, among others.
137 .. versionadded:: 2.5
140 .. function:: lin2lin(fragment, width, newwidth)
142 Convert samples between 1-, 2- and 4-byte formats.
145 .. function:: lin2ulaw(fragment, width)
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.
153 .. function:: minmax(fragment, width)
155 Return a tuple consisting of the minimum and maximum values of all samples in
159 .. function:: max(fragment, width)
161 Return the maximum of the *absolute value* of all samples in a fragment.
164 .. function:: maxpp(fragment, width)
166 Return the maximum peak-peak value in the sound fragment.
169 .. function:: mul(fragment, width, factor)
171 Return a fragment that has all samples in the original fragment multiplied by
172 the floating-point value *factor*. Overflow is silently ignored.
175 .. function:: ratecv(fragment, width, nchannels, inrate, outrate, state[, weightA[, weightB]])
177 Convert the frame rate of the input fragment.
179 *state* is a tuple containing the state of the converter. The converter returns
180 a tuple ``(newfragment, newstate)``, and *newstate* should be passed to the next
181 call of :func:`ratecv`. The initial call should pass ``None`` as the state.
183 The *weightA* and *weightB* arguments are parameters for a simple digital filter
184 and default to ``1`` and ``0`` respectively.
187 .. function:: reverse(fragment, width)
189 Reverse the samples in a fragment and returns the modified fragment.
192 .. function:: rms(fragment, width)
194 Return the root-mean-square of the fragment, i.e. ``sqrt(sum(S_i^2)/n)``.
196 This is a measure of the power in an audio signal.
199 .. function:: tomono(fragment, width, lfactor, rfactor)
201 Convert a stereo fragment to a mono fragment. The left channel is multiplied by
202 *lfactor* and the right channel by *rfactor* before adding the two channels to
206 .. function:: tostereo(fragment, width, lfactor, rfactor)
208 Generate a stereo fragment from a mono fragment. Each pair of samples in the
209 stereo fragment are computed from the mono sample, whereby left channel samples
210 are multiplied by *lfactor* and right channel samples by *rfactor*.
213 .. function:: ulaw2lin(fragment, width)
215 Convert sound fragments in u-LAW encoding to linearly encoded sound fragments.
216 u-LAW encoding always uses 8 bits samples, so *width* refers only to the sample
217 width of the output fragment here.
219 Note that operations such as :func:`mul` or :func:`max` make no distinction
220 between mono and stereo fragments, i.e. all samples are treated equal. If this
221 is a problem the stereo fragment should be split into two mono fragments first
222 and recombined later. Here is an example of how to do that::
224 def mul_stereo(sample, width, lfactor, rfactor):
225 lsample = audioop.tomono(sample, width, 1, 0)
226 rsample = audioop.tomono(sample, width, 0, 1)
227 lsample = audioop.mul(sample, width, lfactor)
228 rsample = audioop.mul(sample, width, rfactor)
229 lsample = audioop.tostereo(lsample, width, 1, 0)
230 rsample = audioop.tostereo(rsample, width, 0, 1)
231 return audioop.add(lsample, rsample, width)
233 If you use the ADPCM coder to build network packets and you want your protocol
234 to be stateless (i.e. to be able to tolerate packet loss) you should not only
235 transmit the data but also the state. Note that you should send the *initial*
236 state (the one you passed to :func:`lin2adpcm`) along to the decoder, not the
237 final state (as returned by the coder). If you want to use
238 :func:`struct.struct` to store the state in binary you can code the first
239 element (the predicted value) in 16 bits and the second (the delta index) in 8.
241 The ADPCM coders have never been tried against other ADPCM coders, only against
242 themselves. It could well be that I misinterpreted the standards in which case
243 they will not be interoperable with the respective standards.
245 The :func:`find\*` routines might look a bit funny at first sight. They are
246 primarily meant to do echo cancellation. A reasonably fast way to do this is to
247 pick the most energetic piece of the output sample, locate that in the input
248 sample and subtract the whole output sample from the input sample::
250 def echocancel(outputdata, inputdata):
251 pos = audioop.findmax(outputdata, 800) # one tenth second
252 out_test = outputdata[pos*2:]
253 in_test = inputdata[pos*2:]
254 ipos, factor = audioop.findfit(in_test, out_test)
255 # Optional (for better cancellation):
256 # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],
258 prefill = '\0'*(pos+ipos)*2
259 postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata))
260 outputdata = prefill + audioop.mul(outputdata,2,-factor) + postfill
261 return audioop.add(inputdata, outputdata, 2)