FastLED 3.9.15
Loading...
Searching...
No Matches
BeatDetection.ino
Go to the documentation of this file.
1
7
8// @filter: (memory is large)
9
10#include <FastLED.h>
11
12#if defined(FL_IS_TEENSY)
13// Keep fbuild's library scanner aware of PJRC Audio sources for Teensy.
14#include <Audio.h>
15#endif
16
17#if !SKETCH_HAS_LARGE_MEMORY
18// Platform does not have enough memory
19void setup() {}
20void loop() {}
21#else
22
24#include "fl/ui/ui.h"
25
26// LED Configuration
27#define NUM_LEDS 60
28#define DATA_PIN 3
29#define BRIGHTNESS 128
30
32
33// Audio input
34fl::UIAudio audio("Audio Input");
35
36// Audio processor with beat detection
38
39// Beat detection state
40float currentBPM = 0.0f;
42uint32_t beatCount = 0;
43uint32_t onsetCount = 0;
44
45// Get color based on BPM (blue=slow, green=medium, red=fast)
46fl::CRGB getBPMColor(float bpm) {
47 if (bpm < 90.0f) {
48 return fl::CRGB::Blue;
49 } else if (bpm < 130.0f) {
50 return fl::CRGB::Green;
51 } else {
52 return fl::CRGB::Red;
53 }
54}
55
56void setup() {
57 Serial.begin(115200);
58 delay(1000);
59
60 Serial.println("Beat Detection Example");
61 Serial.println("=====================");
62
63 // Initialize LED strip with screen map for visualization
65 FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS)
66 .setScreenMap(screenMap);
67 FastLED.setBrightness(BRIGHTNESS);
68 FastLED.clear();
69 FastLED.show();
70
71 // Set up beat detection callbacks
72 audioProcessor.onBeat([]() {
73 beatCount++;
75 Serial.print("BEAT #");
76 Serial.println(beatCount);
77 });
78
79 audioProcessor.onOnset([](float strength) {
80 onsetCount++;
81 Serial.print("Onset strength=");
82 Serial.println(strength);
83 });
84
85 audioProcessor.onTempoChange([](float bpm, float confidence) {
86 currentBPM = bpm;
87 Serial.print("Tempo: ");
88 Serial.print(bpm);
89 Serial.print(" BPM (confidence: ");
90 Serial.print(confidence);
91 Serial.println(")");
92 });
93
94 Serial.println("Beat detector initialized");
95 Serial.println("Playing audio will trigger beat detection...");
96}
97
98void loop() {
99 // Get audio sample
101
102 if (sample.isValid()) {
103 // Process audio through the audio processor
104 audioProcessor.update(sample);
105 }
106
107 // Visualize beats on LED strip
108 uint32_t timeSinceBeat = fl::millis() - lastBeatTime;
109
110 if (timeSinceBeat < 100) {
111 // Flash bright on beat
112 fl::CRGB beatColor = getBPMColor(currentBPM);
113 fill_solid(leds, NUM_LEDS, beatColor);
114 } else if (timeSinceBeat < 200) {
115 // Fade out
116 fl::CRGB beatColor = getBPMColor(currentBPM);
117 beatColor.fadeToBlackBy(128);
118 fill_solid(leds, NUM_LEDS, beatColor);
119 } else {
120 // Idle: dim pulse at detected tempo
121 if (currentBPM > 0) {
122 // Pulse frequency based on BPM
123 float period_ms = (60000.0f / currentBPM);
124 float phase = fl::fmod(static_cast<float>(fl::millis()), period_ms) / period_ms;
125 uint8_t brightness = static_cast<uint8_t>((fl::sin(phase * 2.0f * 3.14159f) + 1.0f) * 32.0f);
126
127 fl::CRGB idleColor = getBPMColor(currentBPM);
128 idleColor.fadeToBlackBy(255 - brightness);
129 fill_solid(leds, NUM_LEDS, idleColor);
130 } else {
131 // No tempo detected yet
132 FastLED.clear();
133 }
134 }
135
136 FastLED.show();
137
138 // Print stats periodically
139 EVERY_N_SECONDS(10) {
140 Serial.println();
141 Serial.println("=== Beat Detection Stats ===");
142 Serial.print("Onsets: ");
143 Serial.print(onsetCount);
144 Serial.print(" | Beats: ");
145 Serial.println(beatCount);
146 Serial.print("Current BPM: ");
147 Serial.println(currentBPM);
148 Serial.println("============================");
149 }
150}
151
152#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.