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

◆ loop()

void loop ( )

Definition at line 501 of file advanced.h.

501 {
502 // Check if audio is enabled
503 if (!enableAudio) {
504 // Show a simple test pattern
506 FastLED.show();
507 delay(20);
508 return;
509 }
510
511 // Process only one audio sample per frame to avoid accumulation
513 if (sample.isValid()) {
514 // Process pitch detection if enabled
516 // Convert int16_t samples to float for pitch detection
517 static float floatBuffer[FFT_SIZE];
518 size_t numSamples = fl::min(sample.pcm().size(), (size_t)FFT_SIZE);
519 for (size_t i = 0; i < numSamples; i++) {
520 floatBuffer[i] = sample.pcm()[i] / 32768.0f;
521 }
522 pitchEngine->processFrame(floatBuffer, numSamples);
523 }
524
525 // Update sound meter
526 soundMeter.processBlock(sample.pcm());
527
528 // Get audio levels
529 float rms = sample.rms() / 32768.0f;
530
531 // Calculate peak
532 int32_t maxSample = 0;
533 for (size_t i = 0; i < sample.pcm().size(); i++) {
534 int32_t absSample = fl::fabsf(sample.pcm()[i]);
535 if (absSample > maxSample) {
536 maxSample = absSample;
537 }
538 }
539 float peak = float(maxSample) / 32768.0f;
540 peakLevel = peakLevel * 0.9f + peak * 0.1f; // Smooth peak
541
542 // Update auto gain
544
545 // Beat detection
546 if (beatDetect) {
547 isBeat = detectBeat(peak);
548 }
549
550 // Get FFT data - create local Bins to avoid accumulation
552 sample.fft(&fftBins);
553
554 // Update color animation
555 hue += 1;
556
557 // Apply beat flash
558 if (isBeat && beatFlash) {
559 for (int i = 0; i < NUM_LEDS; i++) {
560 leds[i].fadeLightBy(-50); // Make brighter
561 }
562 }
563
564 // Draw selected visualization
565 switch (visualMode.as_int()) {
566 case 0: // Spectrum Bars
567 drawSpectrumBars(&fftBins, peakLevel);
568 break;
569
570 case 1: // Radial Spectrum
571 drawRadialSpectrum(&fftBins, peakLevel);
572 break;
573
574 case 2: // Waveform
576 break;
577
578 case 3: // VU Meter
580 break;
581
582 case 4: // Matrix Rain
584 break;
585
586 case 5: // Fire Effect
588 break;
589
590 case 6: // Plasma Wave
592 break;
593 }
594
595 // Add pitch visualizer overlay if enabled
597 // Display pitch as a horizontal line at a specific height
598 int noteY = HEIGHT - 4; // Near the top
599
600 // Map MIDI note to X position (common singing range: 40-88)
601 float notePos = fl::map_range<float, float>(currentMIDINote, 40.0f, 88.0f, 0.0f, 1.0f);
602 notePos = fl::clamp(notePos, 0.0f, 1.0f);
603 int noteX = notePos * (WIDTH - 1);
604
605 // Draw pitch indicator - brightness based on velocity
607 fl::CRGB pitchColor = fl::CRGB(255, 0, 255); // Magenta
608 pitchColor.fadeToBlackBy(255 - brightness);
609
610 // Draw a small cross at the pitch position
611 for (int dx = -2; dx <= 2; dx++) {
612 int x = noteX + dx;
613 if (x >= 0 && x < WIDTH) {
614 int ledIndex = xyMap(x, noteY);
615 if (ledIndex >= 0 && ledIndex < NUM_LEDS) {
616 leds[ledIndex] = pitchColor;
617 }
618 }
619 }
620 for (int dy = -2; dy <= 2; dy++) {
621 int y = noteY + dy;
622 if (y >= 0 && y < HEIGHT) {
623 int ledIndex = xyMap(noteX, y);
624 if (ledIndex >= 0 && ledIndex < NUM_LEDS) {
625 leds[ledIndex] = pitchColor;
626 }
627 }
628 }
629 }
630 }
631
632 FastLED.show();
633
634 // Add a small delay to prevent tight loops in WebAssembly
635 #ifdef __EMSCRIPTEN__
636 delay(1);
637 #endif
638}
fl::UIAudio audio("Audio Input")
fl::XYMap xyMap
#define NUM_LEDS
fl::CRGB leds[NUM_LEDS]
fl::UISlider brightness("Brightness", BRIGHTNESS, 0, 255)
int y
Definition simple.h:93
float rms(fl::span< const int16_t > data)
Definition simple.h:104
int x
Definition simple.h:92
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
fl::UIDropdown visualMode("Visualization Mode", {"Spectrum Bars", "Radial Spectrum", "Waveform", "VU Meter", "Matrix Rain", "Fire Effect", "Plasma Wave"})
void drawRadialSpectrum(fl::audio::fft::Bins *fft, float)
Definition advanced.h:232
void drawMatrixRain(float peak)
Definition advanced.h:382
bool detectBeat(float energy)
Definition advanced.h:123
bool isBeat
Definition advanced.h:89
#define WIDTH
static const int NUM_BANDS
Definition advanced.h:82
void drawVUMeter(float rms, float peak)
Definition advanced.h:341
void drawFireEffect(float peak)
Definition advanced.h:408
#define FFT_SIZE
void updateAutoGain(float level)
Definition advanced.h:154
fl::UICheckbox beatDetect("Beat Detection", true)
uint8_t currentMIDINote
Definition advanced.h:104
float peakLevel
Definition advanced.h:91
fl::UICheckbox pitchDetectEnable("Pitch Detection", false)
fl::UICheckbox pitchVisualizer("Show Pitch Visualizer", false)
fl::UICheckbox enableAudio("Enable Audio", true)
uint8_t currentVelocity
Definition advanced.h:105
fl::UICheckbox beatFlash("Beat Flash", true)
uint8_t hue
Definition advanced.h:94
void drawSpectrumBars(fl::audio::fft::Bins *fft, float)
Definition advanced.h:182
#define HEIGHT
void drawWaveform(const fl::span< const int16_t > &pcm, float)
Definition advanced.h:268
bool noteIsOn
Definition advanced.h:106
void drawPlasmaWave(float peak)
Definition advanced.h:432
fl::audio::SoundLevelMeter soundMeter(0.0, 0.0)
fl::SoundToMIDIEngine * pitchEngine
Definition advanced.h:103
void fill_rainbow(CRGB *targetArray, int numToFill, fl::u8 initialhue, fl::u8 deltahue=5) FL_NOEXCEPT
Fill a range of LEDs with a rainbow of colors.
Definition fill.cpp.hpp:29
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NOEXCEPT
Definition math.h:71
fl::CRGB CRGB
Definition video.h:15
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
void delay(u32 ms, bool run_async=true) FL_NOEXCEPT
Public delay wrapper that keeps bare Arduino delay() preferred after using fl::delay; while still all...
Definition delay.h:98
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
fl::i32 int32_t
Definition s16x16x4.h:220
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 audio, beatDetect(), beatFlash(), brightness, fl::clamp(), currentMIDINote, currentVelocity, detectBeat(), drawFireEffect(), drawMatrixRain(), drawPlasmaWave(), drawRadialSpectrum(), drawSpectrumBars(), drawVUMeter(), drawWaveform(), enableAudio(), fl::fabsf(), fl::CRGB::fadeToBlackBy(), FastLED, FFT_SIZE, fill_rainbow(), HEIGHT, hue, isBeat, leds, fl::map_range(), fl::min(), noteIsOn, NUM_BANDS, NUM_LEDS, peakLevel, pitchDetectEnable(), pitchEngine, pitchVisualizer(), rms(), soundMeter(), updateAutoGain(), visualMode(), WIDTH, x, xyMap, and y.

+ Here is the call graph for this function: