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