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. Samples are truncated in case of overflow.

Decode an Intel/DVI ADPCM coded fragment to a linear fragment. See the
description of lin2adpcm() for details on ADPCM coding. Return a tuple
(sample,newstate) where the sample has the width specified in width.

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.

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 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 findfactor().

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.

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 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.

Convert samples in the audio fragment to a-LAW encoding and return this as a
bytes object. 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.

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:

Convert samples in the audio fragment to u-LAW encoding and return this as a
bytes object. 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.

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 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.

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.

Note that operations such as mul() or 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:

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 lin2adpcm()) along to the decoder, not the
final state (as returned by the coder). If you want to use
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 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: