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

◆ drawWaveform()

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

Definition at line 256 of file advanced.h.

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