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

◆ loop()

void loop ( )

Definition at line 164 of file simple.h.

164 {
165 if (triggered) {
166 FASTLED_WARN("Triggered");
167 }
168
169 // x = pointX.as_int();
170 y = HEIGHT / 2;
171
172 bool do_frame = doFrame();
173
174 while (AudioSample sample = audio.next()) {
175 if (!do_frame) {
176 continue;
177 }
178 float fade = audioFadeTracker(sample.pcm().data(), sample.pcm().size());
179 shiftUp();
180 // FASTLED_WARN("Audio sample size: " << sample.pcm().size());
181 soundLevelMeter.processBlock(sample.pcm());
182 // FASTLED_WARN("")
183 auto dbfs = soundLevelMeter.getDBFS();
184 FASTLED_UNUSED(dbfs);
185 // FASTLED_WARN("getDBFS: " << dbfs);
186 int32_t max = 0;
187 for (size_t i = 0; i < sample.pcm().size(); ++i) {
188 int32_t x = ABS(sample.pcm()[i]);
189 if (x > max) {
190 max = x;
191 }
192 }
193 float anim =
194 fl::map_range<float, float>(max, 0.0f, 32768.0f, 0.0f, 1.0f);
195 anim = fl::clamp(anim, 0.0f, 1.0f);
196
197 x = fl::map_range<float, float>(anim, 0.0f, 1.0f, 0.0f, WIDTH - 1);
198 // FASTLED_WARN("x: " << x);
199
200 // fft.run(sample.pcm(), &fftOut);
201 sample.fft(&fftOut);
202
203 // FASTLED_ASSERT(fftOut.bins_raw.size() == WIDTH_2X,
204 // "FFT bins size mismatch");
205
206 if (enableFFT) {
207 auto max_x = fftOut.bins_raw.size() - 1;
208 FASTLED_UNUSED(max_x);
209 for (size_t i = 0; i < fftOut.bins_raw.size(); ++i) {
210 auto x = i;
211 auto v = fftOut.bins_db[i];
212 // Map audio intensity to a position in the heat palette (0-255)
213 v = fl::map_range<float, float>(v, 45, 70, 0, 1.f);
214 v = fl::clamp(v, 0.0f, 1.0f);
215 uint8_t heatIndex =
216 fl::map_range<float, uint8_t>(v, 0, 1, 0, 255);
217
218 // FASTLED_WARN(v);
219
220 // Use FastLED's built-in HeatColors palette
221 auto c = ColorFromPalette(HeatColors_p, heatIndex);
222 c.fadeToBlackBy(255 - heatIndex);
223 framebuffer[frameBufferXY(x, 0)] = c;
224 // FASTLED_WARN("y: " << i << " b: " << b);
225 }
226 }
227
228 if (enableVolumeVis) {
229 framebuffer[frameBufferXY(x, HEIGHT / 2)] = CRGB(0, 255, 0);
230 }
231
232 if (enableRMS) {
233 float rms = sample.rms();
234 FASTLED_WARN("RMS: " << rms);
235 rms = fl::map_range<float, float>(rms, 0.0f, 32768.0f, 0.0f, 1.0f);
236 rms = fl::clamp(rms, 0.0f, 1.0f) * WIDTH;
237 framebuffer[frameBufferXY(rms, HEIGHT * 3 / 4)] = CRGB(0, 0, 255);
238 }
239 if (true) {
240 uint16_t fade_width = fade * (WIDTH - 1);
241 uint16_t h = HEIGHT / 4;
242 // yellow
243 int index = frameBufferXY(fade_width, h);
244 auto c = CRGB(255, 255, 0);
245 framebuffer[index] = c;
246 }
247 }
248
249 // now downscale the framebuffer to the led matrix
251
252 FastLED.show();
253}
CRGB leds[NUM_LEDS]
int y
Definition simple.h:93
UICheckbox enableRMS("Enable RMS visualization", false)
void shiftUp()
Definition simple.h:134
bool triggered
Definition simple.h:94
float rms(Slice< const int16_t > data)
Definition simple.h:98
UICheckbox enableFFT("Enable FFT visualization", true)
int x
Definition simple.h:92
CRGB framebuffer[NUM_LEDS]
Definition simple.h:77
XYMap ledsXY(WIDTH/2, HEIGHT/2, IS_SERPINTINE)
SoundLevelMeter soundLevelMeter(.0, 0.0)
FFTBins fftOut(WIDTH)
MaxFadeTracker audioFadeTracker(attackTimeSeconds.value(), decayTimeSeconds.value(), outputTimeSec.value(), 44100)
UICheckbox enableVolumeVis("Enable volume visualization", false)
bool doFrame()
Definition simple.h:154
XYMap frameBufferXY(WIDTH, HEIGHT, IS_SERPINTINE)
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:74
#define WIDTH
Definition advanced.h:36
UIAudio audio("Audio Input")
#define HEIGHT
Definition advanced.h:37
const TProgmemRGBPalette16 HeatColors_p
Approximate "black body radiation" palette, akin to the FastLED HeatColor() function.
#define ABS(x)
Definition math_macros.h:45
void downscale(const CRGB *src, const XYMap &srcXY, CRGB *dst, const XYMap &dstXY)
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
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
#define FASTLED_UNUSED(x)
Definition unused.h:4
#define FASTLED_WARN
Definition warn.h:7

References ABS, audio(), audioFadeTracker(), fl::clamp(), fl::ColorFromPalette(), doFrame(), fl::downscale(), enableFFT(), enableRMS(), enableVolumeVis(), FastLED, FASTLED_UNUSED, FASTLED_WARN, fftOut(), framebuffer, frameBufferXY(), HeatColors_p, HEIGHT, leds, ledsXY(), rms(), shiftUp(), soundLevelMeter(), triggered, WIDTH, x, and y.

+ Here is the call graph for this function: