Repository navigation
Description
What happened?
With the AudioReactive Gravimeter effect, the meter tracks volume normally until it reaches the top. At that point every bar goes (almost) black and only the peak/gravity pixel stays lit. At full volume the whole segment is dark except that one pixel.
Versions
16.0.1 (ESP32 release build)
Same code on main @ d2c00c5
To Reproduce Bug
Setup
ESP32-PICO-D4 (M5Stack Atom Echo), built-in PDM mic (SPM1423), AGC on. SK6812 RGBW. Seen on an 8×8 matrix with 1D→2D "Bar" mapping, but the cause doesn't depend on the mapping, so 1D strips are affected too.
Steps to reproduce
Select Gravimeter with default intensity (128) and a black secondary colour (the default).
Play loud audio so the meter reaches the top of the segment.
The bars disappear, and only the peak pixel remains.
Cause
In mode_gravcenter_base(), Gravimeter mode (FX.cpp#L6856 @ v16.0.1):
SEGMENT.setPixelColor(i, color_blend(SEGCOLOR(1), SEGMENT.color_from_palette(index, false, PALETTE_SOLID_WRAP, 0), uint8_t(segmentSampleAvg*8)));
For Gravimeter, segmentSampleAvg = volumeSmth * intensity / 255 * 0.25, which can reach about 63.75. The bar height mapf(segmentSampleAvg2, 0, 64, 0, SEGLEN-1) reaches full height at segmentSampleAvg == 32, which is exactly where segmentSampleAvg8 passes 255. Casting that out-of-range float to uint8_t wraps on ESP32 (it's undefined behaviour in C++). The blend amount drops to near 0, so every bar becomes SEGCOLOR(1) (black by default). The peak pixel is drawn straight from the palette, so it stays lit.
Gravcenter, Gravcentric and Gravfreq use the same expression but scale by 0.125, which tops out at about 31.9, so they don't overflow.
Suggested fix
Clamp before the cast:
uint8_t(fminf(segmentSampleAvg * 8.0f, 255.0f))
The same clamp could go on the Gravcenter line (#L6878–6879) for safety.
Workaround
Noisemeter has no such overflow, or keep Gravimeter's intensity below 128 (but then it never reaches the top row).
Expected Behavior
At max intensity, all lights should be lit, rather than the top row of the matrix only.
Install Method
Self-Compiled
What version of WLED?
16.0.1 (ESP32 release build)
Which microcontroller/board are you seeing the problem on?
ESP32
Relevant log/trace output
Anything else?
No response
Code of Conduct
- I agree to follow this project's Code of Conduct
Activity
DedeHai commentedon Oct 8, 2026
@coderabbitai is this a regression that was recently introduced or is this intended behaviour of the effect?
coderabbitai commentedon Oct 8, 2026
🧩 Analysis chain
🏁 Script executed:
Length of output: 3457
🏁 Script executed:
Length of output: 7885
🏁 Script executed:
Length of output: 8744
🏁 Script executed:
Length of output: 77275
🏁 Script executed:
Length of output: 14566
@DedeHaiThis is a bug, not intended effect behavior. The specific blackout at full height traces to#4245in December 2024, rather than a recent 16.0.1 change.The history shows:
segmentSampleAvg *= 0.25and passedsegmentSampleAvg*8without clamping. However, the oldcolor_blend()accepted auint16_t. At a blend value of 256 with a black secondary color, it still produced the palette color.#4245, commit26397ee8, added the explicituint8_t(segmentSampleAvg*8)conversion. At full meter height,segmentSampleAvgreaches 32, so the conversion receives 256. That explains the reported ESP32 wrap to zero and the black bars.#4145merged the gravity effects intomode_gravcenter_base(). It preserved the problematic conversion; it did not introduce it.There was already a range-handling defect in the older code.
#4245changed how that defect appears at the full-height threshold.The proposed clamp is appropriate:
This conclusion comes from source history. I did not reproduce the effect on hardware.
You are interacting with an AI system.
softhack007 commentedon Oct 8, 2026
In the old version of gravimeter, the overflow was intended behaviour - it produces a nice "hit the bar" effect in color_from_palette.
DedeHai commentedon Oct 8, 2026
@softhack007 I quickly checked but could not pinpoint on how that mechanism used to work, it seems a bit hacky. any Idea on how to make it behave as it was?
edit: I checked and cooked up a solution, please review
DedeHai commentedon Oct 8, 2026
@coderabbitai please check if there are other places where color_blend() may have been used with overflows prior to #4245
coderabbitai commentedon Oct 8, 2026
🧩 Analysis chain
🏁 Script executed:
Length of output: 40954
🏁 Script executed:
Length of output: 65510
🏁 Script executed:
Length of output: 13129
🏁 Script executed:
Length of output: 19776
@DedeHaiYes. I found other effects that passed blend values above 255 before#4245. I checked the call sites at217d2aeb, the parent of that change.Confirmed cases besides Gravimeter
All locations below refer to historical
wled00/FX.cpp.#4245SEGENV.aux0 / ripples[i].state * 2SEGENV.aux0is 255.uint8_t(2*255/state). This also changes integer-division rounding at some later states.2 * fftResult[...]uint8_tcast.fftResult[i % 16] * 4uint8_tcast.volumeRaw * SEGMENT.intensity / 64pixBrifrominttouint8_t.pixBrifrominttouint8_t./ 4.0fuint8_t./ 8.0fuint8_t./ 16.0fuint8_t.Historical source: Ripple Peak, Matripix, Pixelwave, and FFT effects.
Additional conditional cases
volumeSmth * intensity / 255. Unlike Gravimeter, it stays within 255 for normal 0–255 volume input. However, received audio-sync volume has no upper clamp.volumeSmthdirectly. It can exceed 255 with audio-sync input for the same reason.I did not find another comparable case in the remaining inspected calls. Expressions in Twinklefox, Exploding Fireworks, and Drip stay within range through branch conditions or clamps. Calls with
b16=trueintentionally used the 0–65535 range; those are not overflow cases.Why this matters
The old implementation accepted
uint16_t, but its default arithmetic used a blend maximum of 255. It did not clamp larger arguments.RGBW32()then narrowed each calculated channel to a byte.That behavior is not equivalent to narrowing the blend argument to a byte first. It is also not equivalent to
color_blend16(), which uses a different scale.I would prioritize Ripple Peak, Blurz, Noisemove, Matripix, Pixelwave, and Freqmap for compatibility checks. Their out-of-range inputs follow directly from normal effect operation or sound simulation. Source inspection establishes the behavior change, but it does not establish whether each effect intended that behavior.
Thanks for the clarification about Gravimeter’s intended overflow behavior. My earlier blanket description of that behavior as a defect was too broad. A clamp prevents the blackout, but it does not preserve the old overflow effect.
This was a source audit. I did not test the effects on hardware.
✏️ Learnings added
You are interacting with an AI system.
DedeHai commentedon Oct 8, 2026
@softhack007 some of the FX the rabbit flagged are definitely affected but IMHO for the better (no random colors due to overflow). Some of the flagged ones do not actually overflow in practice. I do not really use AR effects so I can not tell if any others need adjustment. What is your verdict here?
softhack007 commentedon Oct 9, 2026
@DedeHai thanks, I'll check the effects on the weekend (had a working week full of meetings 🥵 ).
Maybe for the other effects, it would be simplest to just clamp blend values to 255, instead of wrapping around by implicit
%256. so we'd usemax(xx, 255U)instead ofuint8_t(xx). I think only gravimeter was explicitly relying on old blend > 255 behaviour.softhack007 commentedon Oct 9, 2026
@coderabbitai related to #5891 (comment) - how did the original colorFromPalette() function and FastLED 3.6.x handle blend values > 255?
coderabbitai commentedon Oct 9, 2026
coderabbitai commentedon Oct 9, 2026
🧩 Analysis chain
🏁 Script executed:
Length of output: 28544
@softhack007FastLED narrowed integer inputs to 8 bits before processing. The old WLEDcolor_blend()did not. These are different operations.I checked FastLED 3.6.0 and the WLED source immediately before
#4245.FastLED
ColorFromPalette()The relevant signature was:
It had no numeric blend-amount parameter.
blendTypeselected the interpolation mode.If an integer above 255 was passed as
indexorbrightness, conversion touint8_treduced it modulo 256:Thus, index 256 selected palette index 0. Brightness 256 became 0 and produced black.
For interpolation, FastLED calculated
f2 = (index & 0x0F) << 4. This weight could not exceed 240. An out-of-range blend weight never reached that calculation.FastLED’s separate
blend()andnblend()functions also used an 8-bit blend amount (fract8). Integer inputs above 255 narrowed before blending.FastLED source
WLED
Segment::color_from_palette()The historical wrapper accepted a
uint16_tindex and auint8_tbrightness.If
mappingwas enabled, it first mapped the index from segment coordinates to palette coordinates. The resulting index then narrowed to 8 bits atscale8()orColorFromPalette().wrap = falsedid not clamp an oversized index. It restricted interpolation near the palette end.WLED wrapper source
Old WLED
color_blend()With
b16 = false, this function accepted auint16_tblend, but used a 255-scale calculation:It did not clamp the blend or narrow it to 8 bits first. The calculated channels narrowed afterward.
For black as
color1and RGB(200, 100, 50)ascolor2:(200, 100, 50)(142, 199, 99)At 510, the calculated red channel was 398. Narrowing that channel produced 142.
This last operation produced Gravimeter’s legacy color change. Gravimeter passed
segmentSampleAvg * 8tocolor_blend(), not tocolor_from_palette(). Neither FastLED’s 8-bit blending nor clamping to 255 reproduces that behavior.Sources: old blending implementation, Gravimeter call.
One important distinction: modulo-256 conversion applies to integer inputs. Direct conversion of an out-of-range floating-point value to
uint8_thas undefined behavior in C++; portable code must not rely on it wrapping.You are interacting with an AI system.
softhack007 commentedon Oct 9, 2026
@DedeHai this is another important side-note: directly casting a float result that does not fit into uint8_t (float < 0 or float > 255.0) is undefined behaviour - we already had other effects where this did not work on the C3, but it worked on extensa esp32. The portable solution would be to cast through
int, for example(uint8_t(int(segmentSampleAvg*8))). It would still wrap-around however, the difference is just that casting trough signed integer removes the undefined behaviour problem.If I remember correctly, the scaling hacks were already there in WLED-SR, but I always wanted to replace them so no intermediate down-and-upscaling is needed. The main intention was to use
SampleAvgas float and avoidmap()- otherwise bars started to behave very jumpy on long LED strips (>125 leds)