mirror of
https://github.com/python/cpython.git
synced 2025-01-25 18:44:28 +08:00
c5605dffdb
svn+ssh://svn.python.org/python/branches/py3k ................ r73941 | georg.brandl | 2009-07-11 12:39:00 +0200 (Sa, 11 Jul 2009) | 9 lines Merged revisions 73940 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r73940 | georg.brandl | 2009-07-11 12:37:38 +0200 (Sa, 11 Jul 2009) | 1 line #6430: add note about size of "u" type. ........ ................ r73942 | georg.brandl | 2009-07-11 12:39:23 +0200 (Sa, 11 Jul 2009) | 1 line #6430: remove mention of "w" array typecode. ................ r73943 | georg.brandl | 2009-07-11 12:43:08 +0200 (Sa, 11 Jul 2009) | 1 line #6421: The self argument of module-level PyCFunctions is now a reference to the module object. ................ r74076 | georg.brandl | 2009-07-18 11:07:48 +0200 (Sa, 18 Jul 2009) | 1 line #6502: add missing comma in docstring. ................ r74094 | georg.brandl | 2009-07-19 09:25:56 +0200 (So, 19 Jul 2009) | 10 lines Recorded merge of revisions 74089 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74089 | senthil.kumaran | 2009-07-19 04:43:43 +0200 (So, 19 Jul 2009) | 3 lines Fix for issue5102, timeout value propages between redirects, proxy, digest and auth handlers. Fixed tests to reflect the same. ........ ................ r74186 | georg.brandl | 2009-07-23 11:19:09 +0200 (Do, 23 Jul 2009) | 9 lines Recorded merge of revisions 74185 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74185 | georg.brandl | 2009-07-23 11:17:09 +0200 (Do, 23 Jul 2009) | 1 line Fix the "pylocals" gdb command. ........ ................ r74211 | georg.brandl | 2009-07-26 16:48:09 +0200 (So, 26 Jul 2009) | 9 lines Recorded merge of revisions 74210 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74210 | georg.brandl | 2009-07-26 16:44:23 +0200 (So, 26 Jul 2009) | 1 line Move member descriptions inside the classes. ........ ................ r74212 | georg.brandl | 2009-07-26 16:54:51 +0200 (So, 26 Jul 2009) | 9 lines Merged revisions 74209 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74209 | georg.brandl | 2009-07-26 16:37:28 +0200 (So, 26 Jul 2009) | 1 line builtin -> built-in. ........ ................ r74213 | georg.brandl | 2009-07-26 17:02:41 +0200 (So, 26 Jul 2009) | 9 lines Merged revisions 74207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74207 | georg.brandl | 2009-07-26 16:19:57 +0200 (So, 26 Jul 2009) | 1 line #6577: fix (hopefully) all links to builtin instead of module/class-specific objects. ........ ................ r74214 | georg.brandl | 2009-07-26 17:03:49 +0200 (So, 26 Jul 2009) | 9 lines Merged revisions 74205 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74205 | georg.brandl | 2009-07-26 15:36:39 +0200 (So, 26 Jul 2009) | 1 line #6576: fix cross-refs in re docs. ........ ................ r74247 | georg.brandl | 2009-07-29 09:27:08 +0200 (Mi, 29 Jul 2009) | 9 lines Merged revisions 74239 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74239 | georg.brandl | 2009-07-28 18:55:32 +0000 (Di, 28 Jul 2009) | 1 line Clarify quote_plus() usage. ........ ................ r74254 | georg.brandl | 2009-07-29 18:14:16 +0200 (Mi, 29 Jul 2009) | 1 line #6586: fix return/argument type doc for os.read() and os.write(). ................ r74262 | alexandre.vassalotti | 2009-07-29 21:54:39 +0200 (Mi, 29 Jul 2009) | 57 lines Merged revisions 74074,74077,74111,74188,74192-74193,74200,74252-74253,74258-74261 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74074 | georg.brandl | 2009-07-18 05:03:10 -0400 (Sat, 18 Jul 2009) | 1 line #6513: fix example code: warning categories are classes, not instances. ........ r74077 | georg.brandl | 2009-07-18 05:43:40 -0400 (Sat, 18 Jul 2009) | 1 line #6489: fix an ambiguity in getiterator() documentation. ........ r74111 | benjamin.peterson | 2009-07-20 09:30:10 -0400 (Mon, 20 Jul 2009) | 1 line remove docs for deprecated -p option ........ r74188 | benjamin.peterson | 2009-07-23 10:25:31 -0400 (Thu, 23 Jul 2009) | 1 line use bools ........ r74192 | georg.brandl | 2009-07-24 12:28:38 -0400 (Fri, 24 Jul 2009) | 1 line Fix arg types of et#. ........ r74193 | georg.brandl | 2009-07-24 12:46:38 -0400 (Fri, 24 Jul 2009) | 1 line Dont put "void" in signature for nullary functions. ........ r74200 | georg.brandl | 2009-07-25 09:02:15 -0400 (Sat, 25 Jul 2009) | 1 line #6571: add index entries for more operators. ........ r74252 | georg.brandl | 2009-07-29 12:06:31 -0400 (Wed, 29 Jul 2009) | 1 line #6593: fix link targets. ........ r74253 | georg.brandl | 2009-07-29 12:09:17 -0400 (Wed, 29 Jul 2009) | 1 line #6591: add reference to ioctl in fcntl module for platforms other than Windows. ........ r74258 | georg.brandl | 2009-07-29 12:57:05 -0400 (Wed, 29 Jul 2009) | 1 line Add a link to readline, and mention IPython and bpython. ........ r74259 | georg.brandl | 2009-07-29 13:07:21 -0400 (Wed, 29 Jul 2009) | 1 line Fix some markup and small factual glitches found by M. Markert. ........ r74260 | georg.brandl | 2009-07-29 13:15:20 -0400 (Wed, 29 Jul 2009) | 1 line Fix a few markup glitches. ........ r74261 | georg.brandl | 2009-07-29 13:50:25 -0400 (Wed, 29 Jul 2009) | 1 line Rewrite the section about classes a bit; mostly tidbits, and a larger update to the section about "private" variables to reflect the Pythonic consensus better. ........ ................ r74311 | georg.brandl | 2009-08-04 22:29:27 +0200 (Di, 04 Aug 2009) | 1 line Slightly improve buffer-related error message. ................ r74334 | georg.brandl | 2009-08-06 19:51:03 +0200 (Do, 06 Aug 2009) | 1 line #6648: mention surrogateescape handler where all standard handlers are listed. ................ r74368 | georg.brandl | 2009-08-13 09:56:35 +0200 (Do, 13 Aug 2009) | 21 lines Merged revisions 74328,74332-74333,74365 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74328 | georg.brandl | 2009-08-06 17:06:25 +0200 (Do, 06 Aug 2009) | 1 line Fix base keyword arg name for int() and long(). ........ r74332 | georg.brandl | 2009-08-06 19:23:21 +0200 (Do, 06 Aug 2009) | 1 line Fix punctuation and one copy-paste error. ........ r74333 | georg.brandl | 2009-08-06 19:43:55 +0200 (Do, 06 Aug 2009) | 1 line #6658: fix two typos. ........ r74365 | georg.brandl | 2009-08-13 09:48:05 +0200 (Do, 13 Aug 2009) | 1 line #6679: Remove mention that sub supports no flags. ........ ................
269 lines
9.9 KiB
ReStructuredText
269 lines
9.9 KiB
ReStructuredText
:mod:`audioop` --- Manipulate raw audio data
|
|
============================================
|
|
|
|
.. module:: audioop
|
|
:synopsis: Manipulate raw audio data.
|
|
|
|
|
|
The :mod:`audioop` module contains some useful operations on sound fragments.
|
|
It operates on sound fragments consisting of signed integer samples 8, 16 or 32
|
|
bits wide, stored in Python strings. All scalar items are integers, unless
|
|
specified otherwise.
|
|
|
|
.. index::
|
|
single: Intel/DVI ADPCM
|
|
single: ADPCM, Intel/DVI
|
|
single: a-LAW
|
|
single: u-LAW
|
|
|
|
This module provides support for a-LAW, u-LAW and Intel/DVI ADPCM encodings.
|
|
|
|
.. This para is mostly here to provide an excuse for the index entries...
|
|
|
|
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.
|
|
|
|
The module defines the following variables and functions:
|
|
|
|
|
|
.. exception:: error
|
|
|
|
This exception is raised on all errors, such as unknown number of bytes per
|
|
sample, etc.
|
|
|
|
|
|
.. function:: add(fragment1, fragment2, width)
|
|
|
|
Return a fragment which is the addition of the two samples passed as parameters.
|
|
*width* is the sample width in bytes, either ``1``, ``2`` or ``4``. Both
|
|
fragments should have the same length.
|
|
|
|
|
|
.. function:: adpcm2lin(adpcmfragment, width, state)
|
|
|
|
Decode an Intel/DVI ADPCM coded fragment to a linear fragment. See the
|
|
description of :func:`lin2adpcm` for details on ADPCM coding. Return a tuple
|
|
``(sample, newstate)`` where the sample has the width specified in *width*.
|
|
|
|
|
|
.. function:: alaw2lin(fragment, width)
|
|
|
|
Convert sound fragments in a-LAW encoding to linearly encoded sound fragments.
|
|
a-LAW encoding always uses 8 bits samples, so *width* refers only to the sample
|
|
width of the output fragment here.
|
|
|
|
|
|
.. function:: avg(fragment, width)
|
|
|
|
Return the average over all samples in the fragment.
|
|
|
|
|
|
.. function:: avgpp(fragment, width)
|
|
|
|
Return the average peak-peak value over all samples in the fragment. No
|
|
filtering is done, so the usefulness of this routine is questionable.
|
|
|
|
|
|
.. function:: bias(fragment, width, bias)
|
|
|
|
Return a fragment that is the original fragment with a bias added to each
|
|
sample.
|
|
|
|
|
|
.. function:: cross(fragment, width)
|
|
|
|
Return the number of zero crossings in the fragment passed as an argument.
|
|
|
|
|
|
.. function:: findfactor(fragment, reference)
|
|
|
|
Return a factor *F* such that ``rms(add(fragment, mul(reference, -F)))`` is
|
|
minimal, i.e., return the factor with which you should multiply *reference* to
|
|
make it match as well as possible to *fragment*. The fragments should both
|
|
contain 2-byte samples.
|
|
|
|
The time taken by this routine is proportional to ``len(fragment)``.
|
|
|
|
|
|
.. function:: findfit(fragment, reference)
|
|
|
|
Try to match *reference* as well as possible to a portion of *fragment* (which
|
|
should be the longer fragment). This is (conceptually) done by taking slices
|
|
out of *fragment*, using :func:`findfactor` to compute the best match, and
|
|
minimizing the result. The fragments should both contain 2-byte samples.
|
|
Return a tuple ``(offset, factor)`` where *offset* is the (integer) offset into
|
|
*fragment* where the optimal match started and *factor* is the (floating-point)
|
|
factor as per :func:`findfactor`.
|
|
|
|
|
|
.. function:: findmax(fragment, length)
|
|
|
|
Search *fragment* for a slice of length *length* samples (not bytes!) with
|
|
maximum energy, i.e., return *i* for which ``rms(fragment[i*2:(i+length)*2])``
|
|
is maximal. The fragments should both contain 2-byte samples.
|
|
|
|
The routine takes time proportional to ``len(fragment)``.
|
|
|
|
|
|
.. function:: getsample(fragment, width, index)
|
|
|
|
Return the value of sample *index* from the fragment.
|
|
|
|
|
|
.. function:: lin2adpcm(fragment, width, state)
|
|
|
|
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.
|
|
|
|
*state* is a tuple containing the state of the coder. The coder returns a tuple
|
|
``(adpcmfrag, newstate)``, and the *newstate* should be passed to the next call
|
|
of :func:`lin2adpcm`. In the initial call, ``None`` can be passed as the state.
|
|
*adpcmfrag* is the ADPCM coded fragment packed 2 4-bit values per byte.
|
|
|
|
|
|
.. function:: lin2alaw(fragment, width)
|
|
|
|
Convert samples in the audio fragment to a-LAW encoding and return this as a
|
|
Python string. 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.
|
|
|
|
|
|
.. function:: lin2lin(fragment, width, newwidth)
|
|
|
|
Convert samples between 1-, 2- and 4-byte formats.
|
|
|
|
.. note::
|
|
|
|
In some audio formats, such as .WAV files, 16 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::
|
|
|
|
new_frames = audioop.lin2lin(frames, old_width, 1)
|
|
new_frames = audioop.bias(new_frames, 1, 128)
|
|
|
|
The same, in reverse, has to be applied when converting from 8 to 16 or 32
|
|
bit width samples.
|
|
|
|
|
|
.. function:: lin2ulaw(fragment, width)
|
|
|
|
Convert samples in the audio fragment to u-LAW encoding and return this as a
|
|
Python string. 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.
|
|
|
|
|
|
.. function:: minmax(fragment, width)
|
|
|
|
Return a tuple consisting of the minimum and maximum values of all samples in
|
|
the sound fragment.
|
|
|
|
|
|
.. function:: max(fragment, width)
|
|
|
|
Return the maximum of the *absolute value* of all samples in a fragment.
|
|
|
|
|
|
.. function:: maxpp(fragment, width)
|
|
|
|
Return the maximum peak-peak value in the sound fragment.
|
|
|
|
|
|
.. function:: mul(fragment, width, factor)
|
|
|
|
Return a fragment that has all samples in the original fragment multiplied by
|
|
the floating-point value *factor*. Overflow is silently ignored.
|
|
|
|
|
|
.. function:: ratecv(fragment, width, nchannels, inrate, outrate, state[, weightA[, weightB]])
|
|
|
|
Convert the frame rate of the input fragment.
|
|
|
|
*state* is a tuple containing the state of the converter. The converter returns
|
|
a tuple ``(newfragment, newstate)``, and *newstate* should be passed to the next
|
|
call of :func:`ratecv`. The initial call should pass ``None`` as the state.
|
|
|
|
The *weightA* and *weightB* arguments are parameters for a simple digital filter
|
|
and default to ``1`` and ``0`` respectively.
|
|
|
|
|
|
.. function:: reverse(fragment, width)
|
|
|
|
Reverse the samples in a fragment and returns the modified fragment.
|
|
|
|
|
|
.. function:: rms(fragment, width)
|
|
|
|
Return the root-mean-square of the fragment, i.e. ``sqrt(sum(S_i^2)/n)``.
|
|
|
|
This is a measure of the power in an audio signal.
|
|
|
|
|
|
.. function:: tomono(fragment, width, lfactor, rfactor)
|
|
|
|
Convert a stereo fragment to a mono fragment. The left channel is multiplied by
|
|
*lfactor* and the right channel by *rfactor* before adding the two channels to
|
|
give a mono signal.
|
|
|
|
|
|
.. function:: tostereo(fragment, width, lfactor, rfactor)
|
|
|
|
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 *lfactor* and right channel samples by *rfactor*.
|
|
|
|
|
|
.. function:: ulaw2lin(fragment, width)
|
|
|
|
Convert sound fragments in u-LAW encoding to linearly encoded sound fragments.
|
|
u-LAW encoding always uses 8 bits samples, so *width* refers only to the sample
|
|
width of the output fragment here.
|
|
|
|
Note that operations such as :func:`.mul` or :func:`.max` 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::
|
|
|
|
def mul_stereo(sample, width, lfactor, rfactor):
|
|
lsample = audioop.tomono(sample, width, 1, 0)
|
|
rsample = audioop.tomono(sample, width, 0, 1)
|
|
lsample = audioop.mul(sample, width, lfactor)
|
|
rsample = audioop.mul(sample, width, rfactor)
|
|
lsample = audioop.tostereo(lsample, width, 1, 0)
|
|
rsample = audioop.tostereo(rsample, width, 0, 1)
|
|
return audioop.add(lsample, rsample, width)
|
|
|
|
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 *initial*
|
|
state (the one you passed to :func:`lin2adpcm`) along to the decoder, not the
|
|
final state (as returned by the coder). If you want to use
|
|
:func:`struct.struct` 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.
|
|
|
|
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.
|
|
|
|
The :func:`find\*` 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::
|
|
|
|
def echocancel(outputdata, inputdata):
|
|
pos = audioop.findmax(outputdata, 800) # one tenth second
|
|
out_test = outputdata[pos*2:]
|
|
in_test = inputdata[pos*2:]
|
|
ipos, factor = audioop.findfit(in_test, out_test)
|
|
# Optional (for better cancellation):
|
|
# factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],
|
|
# out_test)
|
|
prefill = '\0'*(pos+ipos)*2
|
|
postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata))
|
|
outputdata = prefill + audioop.mul(outputdata,2,-factor) + postfill
|
|
return audioop.add(inputdata, outputdata, 2)
|
|
|