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

◆ drawWaveform()

void drawWaveform ( const fl::span< const int16_t > & pcm,
float  )

Definition at line 268 of file advanced.h.

268 {
269 clearDisplay();
270 fl::CRGBPalette16 palette = getCurrentPalette();
271
272 int samplesPerPixel = pcm.size() / WIDTH;
273 int centerY = HEIGHT / 2;
274
275 for (size_t x = 0; x < WIDTH; x++) {
276 size_t sampleIndex = x * samplesPerPixel;
277 if (sampleIndex >= pcm.size()) break;
278
279 // Get the raw sample value
280 float sample = float(pcm[sampleIndex]) / 32768.0f; // Normalize to -1.0 to 1.0
281
282 // Apply logarithmic scaling to prevent saturation
283 float absSample = fl::fabsf(sample);
284 float logAmplitude = 0.0f;
285
286 if (absSample > 0.001f) { // Avoid log(0)
287 // Logarithmic compression: log10(1 + gain * sample)
288 float scaledSample = absSample * audioGain.value() * autoGainValue;
289 logAmplitude = fl::log10f(1.0f + scaledSample * 9.0f) / fl::log10f(10.0f); // Normalize to 0-1
290 }
291
292 // Apply smooth sensitivity curve
293 logAmplitude = fl::powf(logAmplitude, 0.7f); // Gamma correction for better visual response
294
295 // Calculate amplitude in pixels
296 int amplitude = int(logAmplitude * (HEIGHT / 2));
297 amplitude = fl::clamp(amplitude, 0, HEIGHT / 2);
298
299 // Preserve the sign for proper waveform display
300 if (sample < 0) amplitude = -amplitude;
301
302 // Color mapping based on amplitude intensity
303 uint8_t colorIndex = fl::map_range<int, uint8_t>(abs(amplitude), 0, HEIGHT/2, 40, 255);
304 fl::CRGB color = ColorFromPalette(palette, colorIndex + hue);
305
306 // Apply brightness scaling for low amplitudes
307 if (abs(amplitude) < HEIGHT / 4) {
308 color.fadeToBlackBy(128 - (abs(amplitude) * 512 / HEIGHT));
309 }
310
311 // Draw vertical line from center
312 if (amplitude == 0) {
313 // Draw center point for zero amplitude
314 int ledIndex = xyMap(x, centerY);
315 if (ledIndex >= 0 && ledIndex < NUM_LEDS) {
316 leds[ledIndex] = color.fadeToBlackBy(200);
317 }
318 } else {
319 // Draw line from center to amplitude
320 int startY = (amplitude > 0) ? centerY : centerY + amplitude;
321 int endY = (amplitude > 0) ? centerY + amplitude : centerY;
322
323 for (int y = startY; y <= endY; y++) {
324 if (y >= 0 && y < HEIGHT) {
325 int ledIndex = xyMap(x, y);
326 if (ledIndex >= 0 && ledIndex < NUM_LEDS) {
327 // Fade edges for smoother appearance
328 fl::CRGB pixelColor = color;
329 if (y == startY || y == endY) {
330 pixelColor.fadeToBlackBy(100);
331 }
332 leds[ledIndex] = pixelColor;
333 }
334 }
335 }
336 }
337 }
338}
fl::XYMap xyMap
#define NUM_LEDS
fl::CRGB leds[NUM_LEDS]
int y
Definition simple.h:93
int x
Definition simple.h:92
constexpr enable_if< is_fixed_point< T >::value, T >::type abs(T x) FL_NOEXCEPT
UINumberField palette("Palette", 0, 0, 2)
#define WIDTH
fl::CRGBPalette16 getCurrentPalette()
Definition advanced.h:109
float autoGainValue
Definition advanced.h:90
fl::UISlider audioGain("Audio Gain", 1.0f, 0.1f, 5.0f, 0.1f)
uint8_t hue
Definition advanced.h:94
#define HEIGHT
void clearDisplay()
Definition advanced.h:173
constexpr fl::size size() const FL_NOEXCEPT
Definition span.h:458
CRGB ColorFromPalette(const CRGBPalette16 &pal, fl::u8 index, fl::u8 brightness, TBlendType blendType)
float powf(float base, float exponent) FL_NOEXCEPT
Definition math.h:436
FASTLED_FORCE_INLINE U map_range(T value, T in_min, T in_max, U out_min, U out_max) FL_NOEXCEPT
Definition math.h:174
float log10f(float value) FL_NOEXCEPT
Definition math.h:424
CRGB sample(const CRGB *grid, const XYMap &xyMap, float x, float y, SampleMode mode)
Sample a pixel from a 2D CRGB grid at floating-point coordinates.
Definition sample.cpp.hpp:9
float fabsf(float value) FL_NOEXCEPT
Definition math.h:508
constexpr enable_if< is_fixed_point< T >::value, T >::type clamp(T x, T lo, T hi) FL_NOEXCEPT
unsigned char uint8_t
Definition s16x16x4.h:209
CRGB & fadeToBlackBy(u8 fadefactor) FL_NOEXCEPT
fadeToBlackBy is a synonym for nscale8(), as a fade instead of a scale
Definition crgb.cpp.hpp:111
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38

References abs(), audioGain(), autoGainValue, fl::clamp(), clearDisplay(), ColorFromPalette(), fl::fabsf(), fl::CRGB::fadeToBlackBy(), getCurrentPalette(), HEIGHT, hue, leds, fl::log10f(), fl::map_range(), NUM_LEDS, palette(), fl::powf(), fl::span< T, Extent >::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: