17db96d56Sopenharmony_ci:mod:`audioop` --- Manipulate raw audio data
27db96d56Sopenharmony_ci============================================
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci.. module:: audioop
57db96d56Sopenharmony_ci   :synopsis: Manipulate raw audio data.
67db96d56Sopenharmony_ci   :deprecated:
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ci.. deprecated-removed:: 3.11 3.13
97db96d56Sopenharmony_ci   The :mod:`audioop` module is deprecated
107db96d56Sopenharmony_ci   (see :pep:`PEP 594 <594#audioop>` for details).
117db96d56Sopenharmony_ci
127db96d56Sopenharmony_ci--------------
137db96d56Sopenharmony_ci
147db96d56Sopenharmony_ciThe :mod:`audioop` module contains some useful operations on sound fragments.
157db96d56Sopenharmony_ciIt operates on sound fragments consisting of signed integer samples 8, 16, 24
167db96d56Sopenharmony_cior 32 bits wide, stored in :term:`bytes-like objects <bytes-like object>`.  All scalar items are
177db96d56Sopenharmony_ciintegers, unless specified otherwise.
187db96d56Sopenharmony_ci
197db96d56Sopenharmony_ci.. versionchanged:: 3.4
207db96d56Sopenharmony_ci   Support for 24-bit samples was added.
217db96d56Sopenharmony_ci   All functions now accept any :term:`bytes-like object`.
227db96d56Sopenharmony_ci   String input now results in an immediate error.
237db96d56Sopenharmony_ci
247db96d56Sopenharmony_ci.. index::
257db96d56Sopenharmony_ci   single: Intel/DVI ADPCM
267db96d56Sopenharmony_ci   single: ADPCM, Intel/DVI
277db96d56Sopenharmony_ci   single: a-LAW
287db96d56Sopenharmony_ci   single: u-LAW
297db96d56Sopenharmony_ci
307db96d56Sopenharmony_ciThis module provides support for a-LAW, u-LAW and Intel/DVI ADPCM encodings.
317db96d56Sopenharmony_ci
327db96d56Sopenharmony_ci.. This para is mostly here to provide an excuse for the index entries...
337db96d56Sopenharmony_ci
347db96d56Sopenharmony_ciA few of the more complicated operations only take 16-bit samples, otherwise the
357db96d56Sopenharmony_cisample size (in bytes) is always a parameter of the operation.
367db96d56Sopenharmony_ci
377db96d56Sopenharmony_ciThe module defines the following variables and functions:
387db96d56Sopenharmony_ci
397db96d56Sopenharmony_ci
407db96d56Sopenharmony_ci.. exception:: error
417db96d56Sopenharmony_ci
427db96d56Sopenharmony_ci   This exception is raised on all errors, such as unknown number of bytes per
437db96d56Sopenharmony_ci   sample, etc.
447db96d56Sopenharmony_ci
457db96d56Sopenharmony_ci
467db96d56Sopenharmony_ci.. function:: add(fragment1, fragment2, width)
477db96d56Sopenharmony_ci
487db96d56Sopenharmony_ci   Return a fragment which is the addition of the two samples passed as parameters.
497db96d56Sopenharmony_ci   *width* is the sample width in bytes, either ``1``, ``2``, ``3`` or ``4``.  Both
507db96d56Sopenharmony_ci   fragments should have the same length.  Samples are truncated in case of overflow.
517db96d56Sopenharmony_ci
527db96d56Sopenharmony_ci
537db96d56Sopenharmony_ci.. function:: adpcm2lin(adpcmfragment, width, state)
547db96d56Sopenharmony_ci
557db96d56Sopenharmony_ci   Decode an Intel/DVI ADPCM coded fragment to a linear fragment.  See the
567db96d56Sopenharmony_ci   description of :func:`lin2adpcm` for details on ADPCM coding. Return a tuple
577db96d56Sopenharmony_ci   ``(sample, newstate)`` where the sample has the width specified in *width*.
587db96d56Sopenharmony_ci
597db96d56Sopenharmony_ci
607db96d56Sopenharmony_ci.. function:: alaw2lin(fragment, width)
617db96d56Sopenharmony_ci
627db96d56Sopenharmony_ci   Convert sound fragments in a-LAW encoding to linearly encoded sound fragments.
637db96d56Sopenharmony_ci   a-LAW encoding always uses 8 bits samples, so *width* refers only to the sample
647db96d56Sopenharmony_ci   width of the output fragment here.
657db96d56Sopenharmony_ci
667db96d56Sopenharmony_ci
677db96d56Sopenharmony_ci.. function:: avg(fragment, width)
687db96d56Sopenharmony_ci
697db96d56Sopenharmony_ci   Return the average over all samples in the fragment.
707db96d56Sopenharmony_ci
717db96d56Sopenharmony_ci
727db96d56Sopenharmony_ci.. function:: avgpp(fragment, width)
737db96d56Sopenharmony_ci
747db96d56Sopenharmony_ci   Return the average peak-peak value over all samples in the fragment. No
757db96d56Sopenharmony_ci   filtering is done, so the usefulness of this routine is questionable.
767db96d56Sopenharmony_ci
777db96d56Sopenharmony_ci
787db96d56Sopenharmony_ci.. function:: bias(fragment, width, bias)
797db96d56Sopenharmony_ci
807db96d56Sopenharmony_ci   Return a fragment that is the original fragment with a bias added to each
817db96d56Sopenharmony_ci   sample.  Samples wrap around in case of overflow.
827db96d56Sopenharmony_ci
837db96d56Sopenharmony_ci
847db96d56Sopenharmony_ci.. function:: byteswap(fragment, width)
857db96d56Sopenharmony_ci
867db96d56Sopenharmony_ci   "Byteswap" all samples in a fragment and returns the modified fragment.
877db96d56Sopenharmony_ci   Converts big-endian samples to little-endian and vice versa.
887db96d56Sopenharmony_ci
897db96d56Sopenharmony_ci   .. versionadded:: 3.4
907db96d56Sopenharmony_ci
917db96d56Sopenharmony_ci
927db96d56Sopenharmony_ci.. function:: cross(fragment, width)
937db96d56Sopenharmony_ci
947db96d56Sopenharmony_ci   Return the number of zero crossings in the fragment passed as an argument.
957db96d56Sopenharmony_ci
967db96d56Sopenharmony_ci
977db96d56Sopenharmony_ci.. function:: findfactor(fragment, reference)
987db96d56Sopenharmony_ci
997db96d56Sopenharmony_ci   Return a factor *F* such that ``rms(add(fragment, mul(reference, -F)))`` is
1007db96d56Sopenharmony_ci   minimal, i.e., return the factor with which you should multiply *reference* to
1017db96d56Sopenharmony_ci   make it match as well as possible to *fragment*.  The fragments should both
1027db96d56Sopenharmony_ci   contain 2-byte samples.
1037db96d56Sopenharmony_ci
1047db96d56Sopenharmony_ci   The time taken by this routine is proportional to ``len(fragment)``.
1057db96d56Sopenharmony_ci
1067db96d56Sopenharmony_ci
1077db96d56Sopenharmony_ci.. function:: findfit(fragment, reference)
1087db96d56Sopenharmony_ci
1097db96d56Sopenharmony_ci   Try to match *reference* as well as possible to a portion of *fragment* (which
1107db96d56Sopenharmony_ci   should be the longer fragment).  This is (conceptually) done by taking slices
1117db96d56Sopenharmony_ci   out of *fragment*, using :func:`findfactor` to compute the best match, and
1127db96d56Sopenharmony_ci   minimizing the result.  The fragments should both contain 2-byte samples.
1137db96d56Sopenharmony_ci   Return a tuple ``(offset, factor)`` where *offset* is the (integer) offset into
1147db96d56Sopenharmony_ci   *fragment* where the optimal match started and *factor* is the (floating-point)
1157db96d56Sopenharmony_ci   factor as per :func:`findfactor`.
1167db96d56Sopenharmony_ci
1177db96d56Sopenharmony_ci
1187db96d56Sopenharmony_ci.. function:: findmax(fragment, length)
1197db96d56Sopenharmony_ci
1207db96d56Sopenharmony_ci   Search *fragment* for a slice of length *length* samples (not bytes!) with
1217db96d56Sopenharmony_ci   maximum energy, i.e., return *i* for which ``rms(fragment[i*2:(i+length)*2])``
1227db96d56Sopenharmony_ci   is maximal.  The fragments should both contain 2-byte samples.
1237db96d56Sopenharmony_ci
1247db96d56Sopenharmony_ci   The routine takes time proportional to ``len(fragment)``.
1257db96d56Sopenharmony_ci
1267db96d56Sopenharmony_ci
1277db96d56Sopenharmony_ci.. function:: getsample(fragment, width, index)
1287db96d56Sopenharmony_ci
1297db96d56Sopenharmony_ci   Return the value of sample *index* from the fragment.
1307db96d56Sopenharmony_ci
1317db96d56Sopenharmony_ci
1327db96d56Sopenharmony_ci.. function:: lin2adpcm(fragment, width, state)
1337db96d56Sopenharmony_ci
1347db96d56Sopenharmony_ci   Convert samples to 4 bit Intel/DVI ADPCM encoding.  ADPCM coding is an adaptive
1357db96d56Sopenharmony_ci   coding scheme, whereby each 4 bit number is the difference between one sample
1367db96d56Sopenharmony_ci   and the next, divided by a (varying) step.  The Intel/DVI ADPCM algorithm has
1377db96d56Sopenharmony_ci   been selected for use by the IMA, so it may well become a standard.
1387db96d56Sopenharmony_ci
1397db96d56Sopenharmony_ci   *state* is a tuple containing the state of the coder.  The coder returns a tuple
1407db96d56Sopenharmony_ci   ``(adpcmfrag, newstate)``, and the *newstate* should be passed to the next call
1417db96d56Sopenharmony_ci   of :func:`lin2adpcm`.  In the initial call, ``None`` can be passed as the state.
1427db96d56Sopenharmony_ci   *adpcmfrag* is the ADPCM coded fragment packed 2 4-bit values per byte.
1437db96d56Sopenharmony_ci
1447db96d56Sopenharmony_ci
1457db96d56Sopenharmony_ci.. function:: lin2alaw(fragment, width)
1467db96d56Sopenharmony_ci
1477db96d56Sopenharmony_ci   Convert samples in the audio fragment to a-LAW encoding and return this as a
1487db96d56Sopenharmony_ci   bytes object.  a-LAW is an audio encoding format whereby you get a dynamic
1497db96d56Sopenharmony_ci   range of about 13 bits using only 8 bit samples.  It is used by the Sun audio
1507db96d56Sopenharmony_ci   hardware, among others.
1517db96d56Sopenharmony_ci
1527db96d56Sopenharmony_ci
1537db96d56Sopenharmony_ci.. function:: lin2lin(fragment, width, newwidth)
1547db96d56Sopenharmony_ci
1557db96d56Sopenharmony_ci   Convert samples between 1-, 2-, 3- and 4-byte formats.
1567db96d56Sopenharmony_ci
1577db96d56Sopenharmony_ci   .. note::
1587db96d56Sopenharmony_ci
1597db96d56Sopenharmony_ci      In some audio formats, such as .WAV files, 16, 24 and 32 bit samples are
1607db96d56Sopenharmony_ci      signed, but 8 bit samples are unsigned.  So when converting to 8 bit wide
1617db96d56Sopenharmony_ci      samples for these formats, you need to also add 128 to the result::
1627db96d56Sopenharmony_ci
1637db96d56Sopenharmony_ci         new_frames = audioop.lin2lin(frames, old_width, 1)
1647db96d56Sopenharmony_ci         new_frames = audioop.bias(new_frames, 1, 128)
1657db96d56Sopenharmony_ci
1667db96d56Sopenharmony_ci      The same, in reverse, has to be applied when converting from 8 to 16, 24
1677db96d56Sopenharmony_ci      or 32 bit width samples.
1687db96d56Sopenharmony_ci
1697db96d56Sopenharmony_ci
1707db96d56Sopenharmony_ci.. function:: lin2ulaw(fragment, width)
1717db96d56Sopenharmony_ci
1727db96d56Sopenharmony_ci   Convert samples in the audio fragment to u-LAW encoding and return this as a
1737db96d56Sopenharmony_ci   bytes object.  u-LAW is an audio encoding format whereby you get a dynamic
1747db96d56Sopenharmony_ci   range of about 14 bits using only 8 bit samples.  It is used by the Sun audio
1757db96d56Sopenharmony_ci   hardware, among others.
1767db96d56Sopenharmony_ci
1777db96d56Sopenharmony_ci
1787db96d56Sopenharmony_ci.. function:: max(fragment, width)
1797db96d56Sopenharmony_ci
1807db96d56Sopenharmony_ci   Return the maximum of the *absolute value* of all samples in a fragment.
1817db96d56Sopenharmony_ci
1827db96d56Sopenharmony_ci
1837db96d56Sopenharmony_ci.. function:: maxpp(fragment, width)
1847db96d56Sopenharmony_ci
1857db96d56Sopenharmony_ci   Return the maximum peak-peak value in the sound fragment.
1867db96d56Sopenharmony_ci
1877db96d56Sopenharmony_ci
1887db96d56Sopenharmony_ci.. function:: minmax(fragment, width)
1897db96d56Sopenharmony_ci
1907db96d56Sopenharmony_ci   Return a tuple consisting of the minimum and maximum values of all samples in
1917db96d56Sopenharmony_ci   the sound fragment.
1927db96d56Sopenharmony_ci
1937db96d56Sopenharmony_ci
1947db96d56Sopenharmony_ci.. function:: mul(fragment, width, factor)
1957db96d56Sopenharmony_ci
1967db96d56Sopenharmony_ci   Return a fragment that has all samples in the original fragment multiplied by
1977db96d56Sopenharmony_ci   the floating-point value *factor*.  Samples are truncated in case of overflow.
1987db96d56Sopenharmony_ci
1997db96d56Sopenharmony_ci
2007db96d56Sopenharmony_ci.. function:: ratecv(fragment, width, nchannels, inrate, outrate, state[, weightA[, weightB]])
2017db96d56Sopenharmony_ci
2027db96d56Sopenharmony_ci   Convert the frame rate of the input fragment.
2037db96d56Sopenharmony_ci
2047db96d56Sopenharmony_ci   *state* is a tuple containing the state of the converter.  The converter returns
2057db96d56Sopenharmony_ci   a tuple ``(newfragment, newstate)``, and *newstate* should be passed to the next
2067db96d56Sopenharmony_ci   call of :func:`ratecv`.  The initial call should pass ``None`` as the state.
2077db96d56Sopenharmony_ci
2087db96d56Sopenharmony_ci   The *weightA* and *weightB* arguments are parameters for a simple digital filter
2097db96d56Sopenharmony_ci   and default to ``1`` and ``0`` respectively.
2107db96d56Sopenharmony_ci
2117db96d56Sopenharmony_ci
2127db96d56Sopenharmony_ci.. function:: reverse(fragment, width)
2137db96d56Sopenharmony_ci
2147db96d56Sopenharmony_ci   Reverse the samples in a fragment and returns the modified fragment.
2157db96d56Sopenharmony_ci
2167db96d56Sopenharmony_ci
2177db96d56Sopenharmony_ci.. function:: rms(fragment, width)
2187db96d56Sopenharmony_ci
2197db96d56Sopenharmony_ci   Return the root-mean-square of the fragment, i.e. ``sqrt(sum(S_i^2)/n)``.
2207db96d56Sopenharmony_ci
2217db96d56Sopenharmony_ci   This is a measure of the power in an audio signal.
2227db96d56Sopenharmony_ci
2237db96d56Sopenharmony_ci
2247db96d56Sopenharmony_ci.. function:: tomono(fragment, width, lfactor, rfactor)
2257db96d56Sopenharmony_ci
2267db96d56Sopenharmony_ci   Convert a stereo fragment to a mono fragment.  The left channel is multiplied by
2277db96d56Sopenharmony_ci   *lfactor* and the right channel by *rfactor* before adding the two channels to
2287db96d56Sopenharmony_ci   give a mono signal.
2297db96d56Sopenharmony_ci
2307db96d56Sopenharmony_ci
2317db96d56Sopenharmony_ci.. function:: tostereo(fragment, width, lfactor, rfactor)
2327db96d56Sopenharmony_ci
2337db96d56Sopenharmony_ci   Generate a stereo fragment from a mono fragment.  Each pair of samples in the
2347db96d56Sopenharmony_ci   stereo fragment are computed from the mono sample, whereby left channel samples
2357db96d56Sopenharmony_ci   are multiplied by *lfactor* and right channel samples by *rfactor*.
2367db96d56Sopenharmony_ci
2377db96d56Sopenharmony_ci
2387db96d56Sopenharmony_ci.. function:: ulaw2lin(fragment, width)
2397db96d56Sopenharmony_ci
2407db96d56Sopenharmony_ci   Convert sound fragments in u-LAW encoding to linearly encoded sound fragments.
2417db96d56Sopenharmony_ci   u-LAW encoding always uses 8 bits samples, so *width* refers only to the sample
2427db96d56Sopenharmony_ci   width of the output fragment here.
2437db96d56Sopenharmony_ci
2447db96d56Sopenharmony_ciNote that operations such as :func:`.mul` or :func:`.max` make no distinction
2457db96d56Sopenharmony_cibetween mono and stereo fragments, i.e. all samples are treated equal.  If this
2467db96d56Sopenharmony_ciis a problem the stereo fragment should be split into two mono fragments first
2477db96d56Sopenharmony_ciand recombined later.  Here is an example of how to do that::
2487db96d56Sopenharmony_ci
2497db96d56Sopenharmony_ci   def mul_stereo(sample, width, lfactor, rfactor):
2507db96d56Sopenharmony_ci       lsample = audioop.tomono(sample, width, 1, 0)
2517db96d56Sopenharmony_ci       rsample = audioop.tomono(sample, width, 0, 1)
2527db96d56Sopenharmony_ci       lsample = audioop.mul(lsample, width, lfactor)
2537db96d56Sopenharmony_ci       rsample = audioop.mul(rsample, width, rfactor)
2547db96d56Sopenharmony_ci       lsample = audioop.tostereo(lsample, width, 1, 0)
2557db96d56Sopenharmony_ci       rsample = audioop.tostereo(rsample, width, 0, 1)
2567db96d56Sopenharmony_ci       return audioop.add(lsample, rsample, width)
2577db96d56Sopenharmony_ci
2587db96d56Sopenharmony_ciIf you use the ADPCM coder to build network packets and you want your protocol
2597db96d56Sopenharmony_cito be stateless (i.e. to be able to tolerate packet loss) you should not only
2607db96d56Sopenharmony_citransmit the data but also the state.  Note that you should send the *initial*
2617db96d56Sopenharmony_cistate (the one you passed to :func:`lin2adpcm`) along to the decoder, not the
2627db96d56Sopenharmony_cifinal state (as returned by the coder).  If you want to use
2637db96d56Sopenharmony_ci:class:`struct.Struct` to store the state in binary you can code the first
2647db96d56Sopenharmony_cielement (the predicted value) in 16 bits and the second (the delta index) in 8.
2657db96d56Sopenharmony_ci
2667db96d56Sopenharmony_ciThe ADPCM coders have never been tried against other ADPCM coders, only against
2677db96d56Sopenharmony_cithemselves.  It could well be that I misinterpreted the standards in which case
2687db96d56Sopenharmony_cithey will not be interoperable with the respective standards.
2697db96d56Sopenharmony_ci
2707db96d56Sopenharmony_ciThe :func:`find\*` routines might look a bit funny at first sight. They are
2717db96d56Sopenharmony_ciprimarily meant to do echo cancellation.  A reasonably fast way to do this is to
2727db96d56Sopenharmony_cipick the most energetic piece of the output sample, locate that in the input
2737db96d56Sopenharmony_cisample and subtract the whole output sample from the input sample::
2747db96d56Sopenharmony_ci
2757db96d56Sopenharmony_ci   def echocancel(outputdata, inputdata):
2767db96d56Sopenharmony_ci       pos = audioop.findmax(outputdata, 800)    # one tenth second
2777db96d56Sopenharmony_ci       out_test = outputdata[pos*2:]
2787db96d56Sopenharmony_ci       in_test = inputdata[pos*2:]
2797db96d56Sopenharmony_ci       ipos, factor = audioop.findfit(in_test, out_test)
2807db96d56Sopenharmony_ci       # Optional (for better cancellation):
2817db96d56Sopenharmony_ci       # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],
2827db96d56Sopenharmony_ci       #              out_test)
2837db96d56Sopenharmony_ci       prefill = '\0'*(pos+ipos)*2
2847db96d56Sopenharmony_ci       postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata))
2857db96d56Sopenharmony_ci       outputdata = prefill + audioop.mul(outputdata, 2, -factor) + postfill
2867db96d56Sopenharmony_ci       return audioop.add(inputdata, outputdata, 2)
2877db96d56Sopenharmony_ci
288