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