Skip to main content

Module segment

Module segment 

Source
Expand description

pydub’s AudioSegment, transcribed operation by operation.

The pipeline’s DAW layers are only byte-identical to Python’s if every quirk of pydub 0.25.1 survives, and several are load-bearing:

  • AudioSegment.silent() is 11025 Hz mono. Overlaying a 44.1 kHz stereo stem onto it converts the whole layer through ratecv, which changes its frame count.
  • AudioSegment.empty() is width 1, rate 1, mono; the first += syncs it up to the appended stem.
  • Slicing is by milliseconds through int(ms * rate / 1000.0), so seg[a:] can drop or pad a few trailing frames. overlay is built from two such slices, so it can change a buffer’s length.
  • Negative positions count from the end, and byte slices use Python’s clamping rules — a fade longer than its clip reaches both.

Buffers are raw little-endian bytes, as pydub keeps them, so the same arithmetic applies whatever the sample width.

Modules§

libm
The C library’s pow, log and log10, called at run time.

Structs§

Segment
An immutable-in-spirit block of interleaved PCM. Methods consume and return segments the way pydub’s _spawn hands back new objects.

Functions§

db_to_float
pydub’s db_to_float in amplitude mode: 10 ** (db / 20).
floor_div 🔒
Python’s // on ints: rounds toward negative infinity.
io_err 🔒
py_array_int 🔒
int(x) stored into an array('h'): truncates toward zero; a value outside the type would raise OverflowError in Python.
py_round
Python’s round() of a float to an int: ties go to even.
py_slice 🔒
Python byte slicing data[a:b]: negative indexes count from the end, both ends clamp, and an inverted range is empty.
ratio_to_db
pydub’s ratio_to_db in amplitude mode: 20 * log(ratio, 10), which CPython evaluates as log(ratio) / log(10) — not log10(ratio). 20 * log(2, 10) is 6.020599913279623; 20 * log10(2) is ...624.
read_wav
pydub’s read_wav_audio (with extract_wav_headers): scan at most ten sub-chunks up to data, honour its declared size, reject non-PCM.