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