Skip to main content

Module audioop

Module audioop 

Source
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 add of b onto a[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’s val < minval + 1 lower test.
gcd 🔒
get_raw
GETRAWSAMPLE — the signed sample at byte offset pos.
get_sample32 🔒
GETSAMPLE32 — a sample scaled to occupy the top of an int.
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).