Expand description
The audioop primitives pydub is built on, transcribed from CPython’s
Modules/audioop.c (shipped for Python 3.13+ as audioop-lts).
Every conversion keeps the C code’s arithmetic: products are taken in
double, clamped with fbound and then floored (not truncated), so
-3 * 0.5 is -2. ratecv interpolates on 32-bit-shifted samples and
casts with C’s truncating (int). Those two rounding rules are the
difference between a byte-identical DAW layer and one that is merely
close, so nothing here is “tidied”.
Buffers are raw little-endian bytes of width 1, 2 or 4, as in C.
Functions§
- add
audioop.add(fragment1, fragment2, width)— saturating sum.- add_
into - In-place
addofbontoa[offset..]. Same arithmetic; used where the pydub code builds a new buffer that would be byte-identical anyway. - bounds 🔒
- Smallest and largest sample for a width, as
doubles like the C tables. - check_
len 🔒 - fbound 🔒
fbound: clamp into range, with C’sval < minval + 1lower test.- gcd 🔒
- get_raw
GETRAWSAMPLE— the signed sample at byte offsetpos.- get_
sample32 🔒 GETSAMPLE32— a sample scaled to occupy the top of anint.- int_
bounds 🔒 - lin2lin
audioop.lin2lin(fragment, width, newwidth).- mul
audioop.mul(fragment, width, factor).- put_raw
SETRAWSAMPLE— append one sample, truncating to the width.- put_
sample32 🔒 SETSAMPLE32— the inverse, with an arithmetic shift.- ratecv
audioop.ratecv(fragment, width, nchannels, inrate, outrate, None)with the default weights (1, 0). Only the converted bytes are returned; pydub discards the state.- tomono
audioop.tomono(fragment, width, lfactor, rfactor).- tostereo
audioop.tostereo(fragment, width, lfactor, rfactor).