Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 1 | \section{Built-in Module \module{audioop}} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 2 | \declaremodule{builtin}{audioop} |
| 3 | |
| 4 | \modulesynopsis{Manipulate raw audio data.} |
| 5 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 6 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 7 | The \module{audioop} module contains some useful operations on sound |
| 8 | fragments. It operates on sound fragments consisting of signed |
| 9 | integer samples 8, 16 or 32 bits wide, stored in Python strings. This |
| 10 | is the same format as used by the \module{al} and \module{sunaudiodev} |
| 11 | modules. All scalar items are integers, unless specified otherwise. |
| 12 | |
| 13 | % This para is mostly here to provide an excuse for the index entries... |
| 14 | This module provides support for u-LAW and Intel/DVI ADPCM encodings. |
| 15 | \index{Intel/DVI ADPCM} |
| 16 | \index{ADPCM, Intel/DVI} |
| 17 | \index{u-LAW} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 18 | |
| 19 | A few of the more complicated operations only take 16-bit samples, |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 20 | otherwise the sample size (in bytes) is always a parameter of the |
| 21 | operation. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 22 | |
| 23 | The module defines the following variables and functions: |
| 24 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 25 | \begin{excdesc}{error} |
| 26 | This exception is raised on all errors, such as unknown number of bytes |
| 27 | per sample, etc. |
| 28 | \end{excdesc} |
| 29 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 30 | \begin{funcdesc}{add}{fragment1, fragment2, width} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 31 | Return a fragment which is the addition of the two samples passed as |
| 32 | parameters. \var{width} is the sample width in bytes, either |
| 33 | \code{1}, \code{2} or \code{4}. Both fragments should have the same |
| 34 | length. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 35 | \end{funcdesc} |
| 36 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 37 | \begin{funcdesc}{adpcm2lin}{adpcmfragment, width, state} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 38 | Decode an Intel/DVI ADPCM coded fragment to a linear fragment. See |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 39 | the description of \function{lin2adpcm()} for details on ADPCM coding. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 40 | Return a tuple \code{(\var{sample}, \var{newstate})} where the sample |
| 41 | has the width specified in \var{width}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 42 | \end{funcdesc} |
| 43 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 44 | \begin{funcdesc}{adpcm32lin}{adpcmfragment, width, state} |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 45 | Decode an alternative 3-bit ADPCM code. See \function{lin2adpcm3()} |
| 46 | for details. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 47 | \end{funcdesc} |
| 48 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 49 | \begin{funcdesc}{avg}{fragment, width} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 50 | Return the average over all samples in the fragment. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 51 | \end{funcdesc} |
| 52 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 53 | \begin{funcdesc}{avgpp}{fragment, width} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 54 | Return the average peak-peak value over all samples in the fragment. |
| 55 | No filtering is done, so the usefulness of this routine is |
| 56 | questionable. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 57 | \end{funcdesc} |
| 58 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 59 | \begin{funcdesc}{bias}{fragment, width, bias} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 60 | Return a fragment that is the original fragment with a bias added to |
| 61 | each sample. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 62 | \end{funcdesc} |
| 63 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 64 | \begin{funcdesc}{cross}{fragment, width} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 65 | Return the number of zero crossings in the fragment passed as an |
| 66 | argument. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 67 | \end{funcdesc} |
| 68 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 69 | \begin{funcdesc}{findfactor}{fragment, reference} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 70 | Return a factor \var{F} such that |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 71 | \code{rms(add(\var{fragment}, mul(\var{reference}, -\var{F})))} is |
| 72 | minimal, i.e., return the factor with which you should multiply |
| 73 | \var{reference} to make it match as well as possible to |
| 74 | \var{fragment}. The fragments should both contain 2-byte samples. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 75 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 76 | The time taken by this routine is proportional to |
| 77 | \code{len(\var{fragment})}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 78 | \end{funcdesc} |
| 79 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 80 | \begin{funcdesc}{findfit}{fragment, reference} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 81 | Try to match \var{reference} as well as possible to a portion of |
| 82 | \var{fragment} (which should be the longer fragment). This is |
| 83 | (conceptually) done by taking slices out of \var{fragment}, using |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 84 | \function{findfactor()} to compute the best match, and minimizing the |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 85 | result. The fragments should both contain 2-byte samples. Return a |
| 86 | tuple \code{(\var{offset}, \var{factor})} where \var{offset} is the |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 87 | (integer) offset into \var{fragment} where the optimal match started |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 88 | and \var{factor} is the (floating-point) factor as per |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 89 | \function{findfactor()}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 90 | \end{funcdesc} |
| 91 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 92 | \begin{funcdesc}{findmax}{fragment, length} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 93 | Search \var{fragment} for a slice of length \var{length} samples (not |
| 94 | bytes!)\ with maximum energy, i.e., return \var{i} for which |
| 95 | \code{rms(fragment[i*2:(i+length)*2])} is maximal. The fragments |
| 96 | should both contain 2-byte samples. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 97 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 98 | The routine takes time proportional to \code{len(\var{fragment})}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 99 | \end{funcdesc} |
| 100 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 101 | \begin{funcdesc}{getsample}{fragment, width, index} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 102 | Return the value of sample \var{index} from the fragment. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 103 | \end{funcdesc} |
| 104 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 105 | \begin{funcdesc}{lin2lin}{fragment, width, newwidth} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 106 | Convert samples between 1-, 2- and 4-byte formats. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 107 | \end{funcdesc} |
| 108 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 109 | \begin{funcdesc}{lin2adpcm}{fragment, width, state} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 110 | Convert samples to 4 bit Intel/DVI ADPCM encoding. ADPCM coding is an |
| 111 | adaptive coding scheme, whereby each 4 bit number is the difference |
| 112 | between one sample and the next, divided by a (varying) step. The |
| 113 | Intel/DVI ADPCM algorithm has been selected for use by the IMA, so it |
| 114 | may well become a standard. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 115 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 116 | \var{state} is a tuple containing the state of the coder. The coder |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 117 | returns a tuple \code{(\var{adpcmfrag}, \var{newstate})}, and the |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 118 | \var{newstate} should be passed to the next call of |
| 119 | \function{lin2adpcm()}. In the initial call, \code{None} can be |
| 120 | passed as the state. \var{adpcmfrag} is the ADPCM coded fragment |
| 121 | packed 2 4-bit values per byte. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 122 | \end{funcdesc} |
| 123 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 124 | \begin{funcdesc}{lin2adpcm3}{fragment, width, state} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 125 | This is an alternative ADPCM coder that uses only 3 bits per sample. |
| 126 | It is not compatible with the Intel/DVI ADPCM coder and its output is |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 127 | not packed (due to laziness on the side of the author). Its use is |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 128 | discouraged. |
| 129 | \end{funcdesc} |
| 130 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 131 | \begin{funcdesc}{lin2ulaw}{fragment, width} |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 132 | Convert samples in the audio fragment to u-LAW encoding and return |
| 133 | this as a Python string. u-LAW is an audio encoding format whereby |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 134 | you get a dynamic range of about 14 bits using only 8 bit samples. It |
| 135 | is used by the Sun audio hardware, among others. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 136 | \end{funcdesc} |
| 137 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 138 | \begin{funcdesc}{minmax}{fragment, width} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 139 | Return a tuple consisting of the minimum and maximum values of all |
| 140 | samples in the sound fragment. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 141 | \end{funcdesc} |
| 142 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 143 | \begin{funcdesc}{max}{fragment, width} |
Fred Drake | af8a015 | 1998-01-14 14:51:31 +0000 | [diff] [blame] | 144 | Return the maximum of the \emph{absolute value} of all samples in a |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 145 | fragment. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 146 | \end{funcdesc} |
| 147 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 148 | \begin{funcdesc}{maxpp}{fragment, width} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 149 | Return the maximum peak-peak value in the sound fragment. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 150 | \end{funcdesc} |
| 151 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 152 | \begin{funcdesc}{mul}{fragment, width, factor} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 153 | Return a fragment that has all samples in the original framgent |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 154 | multiplied by the floating-point value \var{factor}. Overflow is |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 155 | silently ignored. |
| 156 | \end{funcdesc} |
| 157 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 158 | \begin{funcdesc}{ratecv}{fragment, width, nchannels, inrate, outrate, |
| 159 | state\optional{, weightA\optional{, weightB}}} |
Guido van Rossum | 6fb6f10 | 1997-02-14 15:59:49 +0000 | [diff] [blame] | 160 | Convert the frame rate of the input fragment. |
| 161 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 162 | \var{state} is a tuple containing the state of the converter. The |
Guido van Rossum | 6fb6f10 | 1997-02-14 15:59:49 +0000 | [diff] [blame] | 163 | converter returns a tupl \code{(\var{newfragment}, \var{newstate})}, |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 164 | and \var{newstate} should be passed to the next call of |
| 165 | \function{ratecv()}. |
Guido van Rossum | 6fb6f10 | 1997-02-14 15:59:49 +0000 | [diff] [blame] | 166 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 167 | The \var{weightA} and \var{weightB} arguments are parameters for a |
| 168 | simple digital filter and default to \code{1} and \code{0} |
| 169 | respectively. |
Guido van Rossum | 6fb6f10 | 1997-02-14 15:59:49 +0000 | [diff] [blame] | 170 | \end{funcdesc} |
| 171 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 172 | \begin{funcdesc}{reverse}{fragment, width} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 173 | Reverse the samples in a fragment and returns the modified fragment. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 174 | \end{funcdesc} |
| 175 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 176 | \begin{funcdesc}{rms}{fragment, width} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 177 | Return the root-mean-square of the fragment, i.e. |
Fred Drake | a4c640e | 1998-05-11 19:51:11 +0000 | [diff] [blame] | 178 | %begin{latexonly} |
| 179 | \iftexi |
| 180 | %end{latexonly} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 181 | the square root of the quotient of the sum of all squared sample value, |
| 182 | divided by the sumber of samples. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 183 | %begin{latexonly} |
Fred Drake | a4c640e | 1998-05-11 19:51:11 +0000 | [diff] [blame] | 184 | \else |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 185 | % in eqn: sqrt { sum S sub i sup 2 over n } |
| 186 | \begin{displaymath} |
| 187 | \catcode`_=8 |
| 188 | \sqrt{\frac{\sum{{S_{i}}^{2}}}{n}} |
| 189 | \end{displaymath} |
Fred Drake | a4c640e | 1998-05-11 19:51:11 +0000 | [diff] [blame] | 190 | \fi |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 191 | %end{latexonly} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 192 | This is a measure of the power in an audio signal. |
| 193 | \end{funcdesc} |
| 194 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 195 | \begin{funcdesc}{tomono}{fragment, width, lfactor, rfactor} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 196 | Convert a stereo fragment to a mono fragment. The left channel is |
| 197 | multiplied by \var{lfactor} and the right channel by \var{rfactor} |
| 198 | before adding the two channels to give a mono signal. |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 199 | \end{funcdesc} |
| 200 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 201 | \begin{funcdesc}{tostereo}{fragment, width, lfactor, rfactor} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 202 | Generate a stereo fragment from a mono fragment. Each pair of samples |
| 203 | in the stereo fragment are computed from the mono sample, whereby left |
| 204 | channel samples are multiplied by \var{lfactor} and right channel |
| 205 | samples by \var{rfactor}. |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 206 | \end{funcdesc} |
| 207 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 208 | \begin{funcdesc}{ulaw2lin}{fragment, width} |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 209 | Convert sound fragments in u-LAW encoding to linearly encoded sound |
| 210 | fragments. u-LAW encoding always uses 8 bits samples, so \var{width} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 211 | refers only to the sample width of the output fragment here. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 212 | \end{funcdesc} |
| 213 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 214 | Note that operations such as \function{mul()} or \function{max()} make |
| 215 | no distinction between mono and stereo fragments, i.e.\ all samples |
| 216 | are treated equal. If this is a problem the stereo fragment should be |
| 217 | split into two mono fragments first and recombined later. Here is an |
| 218 | example of how to do that: |
| 219 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 220 | \begin{verbatim} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 221 | def mul_stereo(sample, width, lfactor, rfactor): |
| 222 | lsample = audioop.tomono(sample, width, 1, 0) |
| 223 | rsample = audioop.tomono(sample, width, 0, 1) |
| 224 | lsample = audioop.mul(sample, width, lfactor) |
| 225 | rsample = audioop.mul(sample, width, rfactor) |
| 226 | lsample = audioop.tostereo(lsample, width, 1, 0) |
| 227 | rsample = audioop.tostereo(rsample, width, 0, 1) |
| 228 | return audioop.add(lsample, rsample, width) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 229 | \end{verbatim} |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 231 | If you use the ADPCM coder to build network packets and you want your |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 232 | protocol to be stateless (i.e.\ to be able to tolerate packet loss) |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 233 | you should not only transmit the data but also the state. Note that |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 234 | you should send the \var{initial} state (the one you passed to |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 235 | \function{lin2adpcm()}) along to the decoder, not the final state (as |
| 236 | returned by the coder). If you want to use \function{struct.struct()} |
| 237 | to store the state in binary you can code the first element (the |
| 238 | predicted value) in 16 bits and the second (the delta index) in 8. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 239 | |
| 240 | The ADPCM coders have never been tried against other ADPCM coders, |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 241 | only against themselves. It could well be that I misinterpreted the |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 242 | standards in which case they will not be interoperable with the |
| 243 | respective standards. |
| 244 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 245 | The \function{find*()} routines might look a bit funny at first sight. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 246 | They are primarily meant to do echo cancellation. A reasonably |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 247 | fast way to do this is to pick the most energetic piece of the output |
| 248 | sample, locate that in the input sample and subtract the whole output |
| 249 | sample from the input sample: |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 250 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 251 | \begin{verbatim} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 252 | def echocancel(outputdata, inputdata): |
| 253 | pos = audioop.findmax(outputdata, 800) # one tenth second |
| 254 | out_test = outputdata[pos*2:] |
| 255 | in_test = inputdata[pos*2:] |
| 256 | ipos, factor = audioop.findfit(in_test, out_test) |
| 257 | # Optional (for better cancellation): |
| 258 | # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)], |
| 259 | # out_test) |
| 260 | prefill = '\0'*(pos+ipos)*2 |
| 261 | postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata)) |
| 262 | outputdata = prefill + audioop.mul(outputdata,2,-factor) + postfill |
| 263 | return audioop.add(inputdata, outputdata, 2) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 264 | \end{verbatim} |