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