FastLED 3.9.15
Loading...
Searching...
No Matches

◆ drawWaveform()

void drawWaveform ( const Slice< const int16_t > & pcm,
float  )

Definition at line 258 of file advanced.h.

258 {
259 clearDisplay();
260 CRGBPalette16 palette = getCurrentPalette();
261
262 int samplesPerPixel = pcm.size() / WIDTH;
263 int centerY = HEIGHT / 2;
264
265 for (size_t x = 0; x < WIDTH; x++) {
266 size_t sampleIndex = x * samplesPerPixel;
267 if (sampleIndex >= pcm.size()) break;
268
269 // Get the raw sample value
270 float sample = float(pcm[sampleIndex]) / 32768.0f; // Normalize to -1.0 to 1.0
271
272 // Apply logarithmic scaling to prevent saturation
273 float absSample = fabsf(sample);
274 float logAmplitude = 0.0f;
275
276 if (absSample > 0.001f) { // Avoid log(0)
277 // Logarithmic compression: log10(1 + gain * sample)
278 float scaledSample = absSample * audioGain.value() * autoGainValue;
279 logAmplitude = log10f(1.0f + scaledSample * 9.0f) / log10f(10.0f); // Normalize to 0-1
280 }
281
282 // Apply smooth sensitivity curve
283 logAmplitude = powf(logAmplitude, 0.7f); // Gamma correction for better visual response
284
285 // Calculate amplitude in pixels
286 int amplitude = int(logAmplitude * (HEIGHT / 2));
287 amplitude = fl::clamp(amplitude, 0, HEIGHT / 2);
288
289 // Preserve the sign for proper waveform display
290 if (sample < 0) amplitude = -amplitude;
291
292 // Color mapping based on amplitude intensity
293 uint8_t colorIndex = fl::map_range<int, uint8_t>(abs(amplitude), 0, HEIGHT/2, 40, 255);
294 CRGB color = ColorFromPalette(palette, colorIndex + hue);
295
296 // Apply brightness scaling for low amplitudes
297 if (abs(amplitude) < HEIGHT / 4) {
298 color.fadeToBlackBy(128 - (abs(amplitude) * 512 / HEIGHT));
299 }
300
301 // Draw vertical line from center
302 if (amplitude == 0) {
303 // Draw center point for zero amplitude
304 int ledIndex = xyMap(x, centerY);
305 if (ledIndex >= 0 && ledIndex < NUM_LEDS) {
306 leds[ledIndex] = color.fadeToBlackBy(200);
307 }
308 } else {
309 // Draw line from center to amplitude
310 int startY = (amplitude > 0) ? centerY : centerY + amplitude;
311 int endY = (amplitude > 0) ? centerY + amplitude : centerY;
312
313 for (int y = startY; y <= endY; y++) {
314 if (y >= 0 && y < HEIGHT) {
315 int ledIndex = xyMap(x, y);
316 if (ledIndex >= 0 && ledIndex < NUM_LEDS) {
317 // Fade edges for smoother appearance
318 CRGB pixelColor = color;
319 if (y == startY || y == endY) {
320 pixelColor.fadeToBlackBy(100);
321 }
322 leds[ledIndex] = pixelColor;
323 }
324 }
325 }
326 }
327 }
328}
CRGB leds[NUM_LEDS]
#define NUM_LEDS
uint8_t hue
int y
Definition simple.h:93
int x
Definition simple.h:92
fl::XYMap xyMap
Definition ColorBoost.h:61
UINumberField palette("Palette", 0, 0, 2)
#define WIDTH
Definition advanced.h:36
float autoGainValue
Definition advanced.h:91
UISlider audioGain("Audio Gain", 1.0f, 0.1f, 5.0f, 0.1f)
CRGBPalette16 getCurrentPalette()
Definition advanced.h:103
#define HEIGHT
Definition advanced.h:37
void clearDisplay()
Definition advanced.h:167
CRGB ColorFromPalette(const CRGBPalette16 &pal, fl::u8 index, fl::u8 brightness, TBlendType blendType)
FASTLED_FORCE_INLINE T clamp(T value, T min, T max)
Definition clamp.h:10
CRGB & fadeToBlackBy(fl::u8 fadefactor)
fadeToBlackBy is a synonym for nscale8(), as a fade instead of a scale
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86

References audioGain(), autoGainValue, fl::clamp(), clearDisplay(), ColorFromPalette(), CRGB::fadeToBlackBy(), getCurrentPalette(), HEIGHT, hue, leds, NUM_LEDS, palette(), fl::Slice< T >::size(), WIDTH, x, xyMap, and y.

Referenced by loop().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: