FastLED 3.9.15
Loading...
Searching...
No Matches
BeatDetection.ino

Demonstrates beat detection using the Processor facade.

Demonstrates beat detection using the Processor facade.Visualizes beats and tempo on an LED strip - flashes on beat detection.

// @filter: (memory is large)
#include <FastLED.h>
#if defined(FL_IS_TEENSY)
// Keep fbuild's library scanner aware of PJRC Audio sources for Teensy.
#include <Audio.h>
#endif
#if !SKETCH_HAS_LARGE_MEMORY
// Platform does not have enough memory
void setup() {}
void loop() {}
#else
#include "fl/ui/ui.h"
// LED Configuration
#define NUM_LEDS 60
#define DATA_PIN 3
#define BRIGHTNESS 128
// Audio input
fl::UIAudio audio("Audio Input");
// Audio processor with beat detection
// Beat detection state
float currentBPM = 0.0f;
uint32_t beatCount = 0;
uint32_t onsetCount = 0;
// Get color based on BPM (blue=slow, green=medium, red=fast)
fl::CRGB getBPMColor(float bpm) {
if (bpm < 90.0f) {
} else if (bpm < 130.0f) {
} else {
return fl::CRGB::Red;
}
}
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("Beat Detection Example");
Serial.println("=====================");
// Initialize LED strip with screen map for visualization
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS)
.setScreenMap(screenMap);
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
FastLED.show();
// Set up beat detection callbacks
audioProcessor.onBeat([]() {
beatCount++;
Serial.print("BEAT #");
Serial.println(beatCount);
});
audioProcessor.onOnset([](float strength) {
onsetCount++;
Serial.print("Onset strength=");
Serial.println(strength);
});
audioProcessor.onTempoChange([](float bpm, float confidence) {
currentBPM = bpm;
Serial.print("Tempo: ");
Serial.print(bpm);
Serial.print(" BPM (confidence: ");
Serial.print(confidence);
Serial.println(")");
});
Serial.println("Beat detector initialized");
Serial.println("Playing audio will trigger beat detection...");
}
void loop() {
// Get audio sample
if (sample.isValid()) {
// Process audio through the audio processor
audioProcessor.update(sample);
}
// Visualize beats on LED strip
uint32_t timeSinceBeat = fl::millis() - lastBeatTime;
if (timeSinceBeat < 100) {
// Flash bright on beat
fl::CRGB beatColor = getBPMColor(currentBPM);
fill_solid(leds, NUM_LEDS, beatColor);
} else if (timeSinceBeat < 200) {
// Fade out
fl::CRGB beatColor = getBPMColor(currentBPM);
beatColor.fadeToBlackBy(128);
fill_solid(leds, NUM_LEDS, beatColor);
} else {
// Idle: dim pulse at detected tempo
if (currentBPM > 0) {
// Pulse frequency based on BPM
float period_ms = (60000.0f / currentBPM);
float phase = fl::fmod(static_cast<float>(fl::millis()), period_ms) / period_ms;
uint8_t brightness = static_cast<uint8_t>((fl::sin(phase * 2.0f * 3.14159f) + 1.0f) * 32.0f);
fl::CRGB idleColor = getBPMColor(currentBPM);
idleColor.fadeToBlackBy(255 - brightness);
fill_solid(leds, NUM_LEDS, idleColor);
} else {
// No tempo detected yet
FastLED.clear();
}
}
FastLED.show();
// Print stats periodically
Serial.println();
Serial.println("=== Beat Detection Stats ===");
Serial.print("Onsets: ");
Serial.print(onsetCount);
Serial.print(" | Beats: ");
Serial.println(beatCount);
Serial.print("Current BPM: ");
Serial.println(currentBPM);
Serial.println("============================");
}
}
#endif
void setup()
void loop()
fl::UIAudio audio("Audio Input")
fl::audio::Processor audioProcessor
#define NUM_LEDS
fl::CRGB leds[NUM_LEDS]
fl::UISlider brightness("Brightness", BRIGHTNESS, 0, 255)
#define BRIGHTNESS
#define DATA_PIN
Definition ClientReal.h:82
void bpm()
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
uint32_t lastBeatTime
Definition advanced.h:88
static ScreenMap DefaultStrip(int numLeds, float cm_between_leds=1.5f, float cm_led_diameter=0.2f, float completion=.9f) FL_NOEXCEPT
void fill_solid(CRGB *targetArray, int numToFill, const CRGB &color) FL_NOEXCEPT
Fill a range of LEDs with a solid color.
Definition fill.cpp.hpp:9
constexpr EOrder GRB
Definition eorder.h:19
fl::ScreenMap screenMap
Definition Corkscrew.h:101
#define EVERY_N_SECONDS(N)
Checks whether to execute a block of code every N seconds.
Definition lib8tion.h:1010
fl::u32 uint32_t
Definition s16x16x4.h:219
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.
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
enable_if<!is_integral< T >::value, T >::type fmod(T x, T y) FL_NOEXCEPT
Definition math.h:339
enable_if< is_fixed_point< T >::value, T >::type sin(T angle) 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
@ Green
<div style='background:#008000;width:4em;height:4em;'></div>
Definition crgb.h:558
@ Red
<div style='background:#FF0000;width:4em;height:4em;'></div>
Definition crgb.h:622
@ Blue
<div style='background:#0000FF;width:4em;height:4em;'></div>
Definition crgb.h:512
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
#define Serial
Definition serial.h:304
Aggregator header for the fl/ui/ family of per-element UI types.