Issue #5262: Improved fix.
[python.git] / Doc / library / audioop.rst
blob8b7aa9e9e515168fb0fee8902b6c8d88a9b417c4
2 :mod:`audioop` --- Manipulate raw audio data
3 ============================================
5 .. module:: audioop
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
13 specified otherwise.
15 .. index::
16    single: Intel/DVI ADPCM
17    single: ADPCM, Intel/DVI
18    single: a-LAW
19    single: u-LAW
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:
31 .. exception:: error
33    This exception is raised on all errors, such as unknown number of bytes per
34    sample, etc.
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.
57    .. versionadded:: 2.5
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
74    sample.
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.
144    .. note::
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::
150          new_frames = audioop.lin2lin(frames, old_width, 1)
151          new_frames = audioop.bias(new_frames, 1, 128)
153       The same, in reverse, has to be applied when converting from 8 to 16 or 32
154       bit width samples.
157 .. function:: lin2ulaw(fragment, width)
159    Convert samples in the audio fragment to u-LAW encoding and return this as a
160    Python string.  u-LAW is an audio encoding format whereby you get a dynamic
161    range of about 14 bits using only 8 bit samples.  It is used by the Sun audio
162    hardware, among others.
165 .. function:: minmax(fragment, width)
167    Return a tuple consisting of the minimum and maximum values of all samples in
168    the sound fragment.
171 .. function:: max(fragment, width)
173    Return the maximum of the *absolute value* of all samples in a fragment.
176 .. function:: maxpp(fragment, width)
178    Return the maximum peak-peak value in the sound fragment.
181 .. function:: mul(fragment, width, factor)
183    Return a fragment that has all samples in the original fragment multiplied by
184    the floating-point value *factor*.  Overflow is silently ignored.
187 .. function:: ratecv(fragment, width, nchannels, inrate, outrate, state[, weightA[, weightB]])
189    Convert the frame rate of the input fragment.
191    *state* is a tuple containing the state of the converter.  The converter returns
192    a tuple ``(newfragment, newstate)``, and *newstate* should be passed to the next
193    call of :func:`ratecv`.  The initial call should pass ``None`` as the state.
195    The *weightA* and *weightB* arguments are parameters for a simple digital filter
196    and default to ``1`` and ``0`` respectively.
199 .. function:: reverse(fragment, width)
201    Reverse the samples in a fragment and returns the modified fragment.
204 .. function:: rms(fragment, width)
206    Return the root-mean-square of the fragment, i.e. ``sqrt(sum(S_i^2)/n)``.
208    This is a measure of the power in an audio signal.
211 .. function:: tomono(fragment, width, lfactor, rfactor)
213    Convert a stereo fragment to a mono fragment.  The left channel is multiplied by
214    *lfactor* and the right channel by *rfactor* before adding the two channels to
215    give a mono signal.
218 .. function:: tostereo(fragment, width, lfactor, rfactor)
220    Generate a stereo fragment from a mono fragment.  Each pair of samples in the
221    stereo fragment are computed from the mono sample, whereby left channel samples
222    are multiplied by *lfactor* and right channel samples by *rfactor*.
225 .. function:: ulaw2lin(fragment, width)
227    Convert sound fragments in u-LAW encoding to linearly encoded sound fragments.
228    u-LAW encoding always uses 8 bits samples, so *width* refers only to the sample
229    width of the output fragment here.
231 Note that operations such as :func:`mul` or :func:`max` make no distinction
232 between mono and stereo fragments, i.e. all samples are treated equal.  If this
233 is a problem the stereo fragment should be split into two mono fragments first
234 and recombined later.  Here is an example of how to do that::
236    def mul_stereo(sample, width, lfactor, rfactor):
237        lsample = audioop.tomono(sample, width, 1, 0)
238        rsample = audioop.tomono(sample, width, 0, 1)
239        lsample = audioop.mul(sample, width, lfactor)
240        rsample = audioop.mul(sample, width, rfactor)
241        lsample = audioop.tostereo(lsample, width, 1, 0)
242        rsample = audioop.tostereo(rsample, width, 0, 1)
243        return audioop.add(lsample, rsample, width)
245 If you use the ADPCM coder to build network packets and you want your protocol
246 to be stateless (i.e. to be able to tolerate packet loss) you should not only
247 transmit the data but also the state.  Note that you should send the *initial*
248 state (the one you passed to :func:`lin2adpcm`) along to the decoder, not the
249 final state (as returned by the coder).  If you want to use
250 :func:`struct.struct` to store the state in binary you can code the first
251 element (the predicted value) in 16 bits and the second (the delta index) in 8.
253 The ADPCM coders have never been tried against other ADPCM coders, only against
254 themselves.  It could well be that I misinterpreted the standards in which case
255 they will not be interoperable with the respective standards.
257 The :func:`find\*` routines might look a bit funny at first sight. They are
258 primarily meant to do echo cancellation.  A reasonably fast way to do this is to
259 pick the most energetic piece of the output sample, locate that in the input
260 sample and subtract the whole output sample from the input sample::
262    def echocancel(outputdata, inputdata):
263        pos = audioop.findmax(outputdata, 800)    # one tenth second
264        out_test = outputdata[pos*2:]
265        in_test = inputdata[pos*2:]
266        ipos, factor = audioop.findfit(in_test, out_test)
267        # Optional (for better cancellation):
268        # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],
269        #              out_test)
270        prefill = '\0'*(pos+ipos)*2
271        postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata))
272        outputdata = prefill + audioop.mul(outputdata,2,-factor) + postfill
273        return audioop.add(inputdata, outputdata, 2)