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