The PI controller, 2/3rds of a PID controller takes output from the display side of things, and feeds it back into a sensitivity variable to make response to sound adaptive.
kp is the proportional coefficient - controls how much it reacts immediately
ki is the integral coefficient - controls how much it reacts over time
start, min, max control the possible ranges of integral values to keep it from getting too crazy from positive or negative feedback.
targetFill is the desired average brightness, and is used to calculate the error fed into the PI controller. brightnessFeedback is calculated each animation frame and adds up all of the pixel brightnesses (clamped between 0 and 2 per pixel).
In other words, it tries to make it more sensitive when LEDs are lit below targetFill, and less sensitive when they are more lit above targetFill.
You could hard-code sensitivity to some value instead, but it wouldn’t adapt to quiet and loud environments.
Aside from general sensitivity, the averages array is used as a slow reacting frequency filter, this keeps long bursts of frequencies from dominating the animation (constant bass, long lead ups, etc), while highlighting shorter bursts (beats, notes, etc).
You could create a PI controller for each frequency bucket, so that each was adaptive in its own band. This would make an adaptively flat response, but would end up bringing out noise in mostly unused frequencies.
Another way of doing it is creating an array or function that modifies sensitivity based on the frequency. Like an EQ, you could tune it to pick out frequencies or bands you were more interested in.
something like
eq = array(32);
eq[0] = .5;
eq[1] = .6;
//...
eq[31] = 1.5
//then in beforeRender where it calculates averages and vals, multiply this with sensitivity and use it instead
for (i = 0; i < 32; i++) {
eqSensitivity = sensitivity * eq[i]
averages[i] = max(.00001, averages[i] * (1 - dw) + frequencyData[i] * dw * eqSensitivity)
vals[i] = (frequencyData[i] * eqSensitivity - averages[i]*2) * 10 * (averages[i] * 1000 + 1)
}