| |
| .. versionadded:: 2.5 |
| |
| |
| .. function:: lin2lin(fragment, width, newwidth) |
| |
| Convert samples between 1-, 2- and 4-byte formats. |
| |
n | .. note:: |
| |
| In some audio formats, such as .WAV files, 16 and 32 bit samples are |
| signed, but 8 bit samples are unsigned. So when converting to 8 bit wide |
| samples for these formats, you need to also add 128 to the result:: |
| |
| new_frames = audioop.lin2lin(frames, old_width, 1) |
| new_frames = audioop.bias(new_frames, 1, 128) |
| |
| The same, in reverse, has to be applied when converting from 8 to 16 or 32 |
| bit width samples. |
| |
| |
| .. function:: lin2ulaw(fragment, width) |
| |
| Convert samples in the audio fragment to u-LAW encoding and return this as a |
| Python string. u-LAW is an audio encoding format whereby you get a dynamic |
| range of about 14 bits using only 8 bit samples. It is used by the Sun audio |
| hardware, among others. |
| |
| |
| .. function:: reverse(fragment, width) |
| |
| Reverse the samples in a fragment and returns the modified fragment. |
| |
| |
| .. function:: rms(fragment, width) |
| |
n | Return the root-mean-square of the fragment, i.e. :: |
n | Return the root-mean-square of the fragment, i.e. ``sqrt(sum(S_i^2)/n)``. |
| |
| XXX: translate this math |
| \catcode`_=8 |
| \sqrt{\frac{\sum{{S_{i}}^{2}}}{n}} |
| |
| This is a measure of the power in an audio signal. |
| |
| |
| .. function:: tomono(fragment, width, lfactor, rfactor) |
| |
| Convert a stereo fragment to a mono fragment. The left channel is multiplied by |
| *lfactor* and the right channel by *rfactor* before adding the two channels to |
| sample and subtract the whole output sample from the input sample:: |
| |
| def echocancel(outputdata, inputdata): |
| pos = audioop.findmax(outputdata, 800) # one tenth second |
| out_test = outputdata[pos*2:] |
| in_test = inputdata[pos*2:] |
| ipos, factor = audioop.findfit(in_test, out_test) |
| # Optional (for better cancellation): |
t | # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)], |
t | # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)], |
| # out_test) |
| prefill = '\0'*(pos+ipos)*2 |
| postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata)) |
| outputdata = prefill + audioop.mul(outputdata,2,-factor) + postfill |
| return audioop.add(inputdata, outputdata, 2) |
| |