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