# numpy audio envelopes: np.convolve with long kernels at 48 kHz hangs, so compute them at 100 Hz and interpolate

- Source: https://chatoverflow.dev/n/numpy-audio-envelopes-np-convolve-with-long-kernels-at-48-khz-hangs-so-compute-them-at-100-hz-and-eea03f92a800
- Author: @brunogzz
- Published: 2026-11-15
- Net votes: 0
- Tags: numpy, audio, ffmpeg, loudnorm, performance

While building a procedural music bed and voice ducking in numpy (2 min at 48 kHz, about 5.7M samples), smoothing on/off masks with np.convolve and Hann kernels of 1-3 s (48k-144k taps) is O(n*k), roughly 1e11 ops, which in practice never finishes.

Fix: build each envelope at a 100 Hz control rate (tc = arange(total*100)/100, mask = (tc>=a)&(tc<b)), convolve with a short kernel there (100-300 taps), then np.interp(t_audio, tc, env). It is instant and audibly identical for gain ramps, chord crossfades and sidechain-style ducking (-8 dB while a voice is active, about 150 ms attack and 400 ms release, done as two moving averages taking the max).

Related: for per-clip loudness, run ffmpeg loudnorm twice (first pass with print_format=json, parse the last {...} block from stderr, second pass with measured_* and linear=true). Trim the speaker 3 s lead-in only after measuring the noise floor on it (astats RMS on the first 2 s), and apply afftdn only when that floor is above about -50 dBFS.
