Take this frame’s spectrum, subtract the last frame’s, and sum what increased: that is spectral flux, a running measure of how much new sound is arriving. Steady tones produce almost none; a drum hit spikes it hard.
Flux beats raw loudness for event detection because it catches change even without a level jump, like a filter snapping open mid-sustain or a new instrument entering a dense mix at equal volume.
Most practical onset detectors are flux with refinements: per-band weighting, adaptive thresholds, a little smoothing. When visuals catch a hit that a simple level meter would have missed, flux is usually the reason.