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