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

◆ loop()

void loop ( )

Definition at line 188 of file simple.h.

188 {
189 if (triggered) {
190 FL_WARN("Triggered");
191 }
192
193 // x = pointX.as_int();
194 y = HEIGHT / 2;
195
196 bool do_frame = doFrame();
197
198 while (fl::audio::Sample sample = audio.next()) {
199 if (!do_frame) {
200 continue;
201 }
202
203 // Process pitch detection if enabled
205 // Convert int16_t samples to float for pitch detection
206 static float floatBuffer[512];
207 size_t numSamples = fl::min(sample.pcm().size(), (size_t)512);
208 for (size_t i = 0; i < numSamples; i++) {
209 floatBuffer[i] = sample.pcm()[i] / 32768.0f;
210 }
211 pitchEngine->processFrame(floatBuffer, numSamples);
212 }
213
214 float fade = audioFadeTracker(sample.pcm().data(), sample.pcm().size());
215 shiftUp();
216 // FL_WARN("Audio sample size: " << sample.pcm().size());
217 soundLevelMeter.processBlock(sample.pcm());
218 // FL_WARN("")
219 auto dbfs = soundLevelMeter.getDBFS();
220 FASTLED_UNUSED(dbfs);
221 // FL_WARN("getDBFS: " << dbfs);
222 int32_t max = 0;
223 for (size_t i = 0; i < sample.pcm().size(); ++i) {
224 int32_t x = ABS(sample.pcm()[i]);
225 if (x > max) {
226 max = x;
227 }
228 }
229 float anim =
230 fl::map_range<float, float>(max, 0.0f, 32768.0f, 0.0f, 1.0f);
231 anim = fl::clamp(anim, 0.0f, 1.0f);
232
233 x = fl::map_range<float, float>(anim, 0.0f, 1.0f, 0.0f, WIDTH - 1);
234 // FL_WARN("x: " << x);
235
236 // fft.run(sample.pcm(), &fftOut);
237 sample.fft(&fftOut);
238
239 if (enableFFT) {
240 auto max_x = fftOut.raw().size() - 1;
241 FASTLED_UNUSED(max_x);
242 for (size_t i = 0; i < fftOut.raw().size(); ++i) {
243 auto x = i;
244 auto v = fftOut.db()[i];
245 // Map audio intensity to a position in the heat palette (0-255)
246 v = fl::map_range<float, float>(v, 45, 70, 0, 1.f);
247 v = fl::clamp(v, 0.0f, 1.0f);
248 uint8_t heatIndex =
249 fl::map_range<float, uint8_t>(v, 0, 1, 0, 255);
250
251 // FL_WARN(v);
252
253 // Use FastLED's built-in HeatColors palette
254 auto c = ColorFromPalette(HeatColors_p, heatIndex);
255 c.fadeToBlackBy(255 - heatIndex);
256 framebuffer[frameBufferXY(x, 0)] = c;
257 // FL_WARN("y: " << i << " b: " << b);
258 }
259 }
260
261 if (enableVolumeVis) {
262 framebuffer[frameBufferXY(x, HEIGHT / 2)] = fl::CRGB(0, 255, 0);
263 }
264
265 if (enableRMS) {
266 float rms = sample.rms();
267 FL_WARN("RMS: " << rms);
268 rms = fl::map_range<float, float>(rms, 0.0f, 32768.0f, 0.0f, 1.0f);
269 rms = fl::clamp(rms, 0.0f, 1.0f) * WIDTH;
270 framebuffer[frameBufferXY(rms, HEIGHT * 3 / 4)] = fl::CRGB(0, 0, 255);
271 }
272
273 // Display pitch detection result
275 // Map MIDI note to position (common range: 40-88)
276 float notePos = fl::map_range<float, float>(currentMIDINote, 40.0f, 88.0f, 0.0f, 1.0f);
277 notePos = fl::clamp(notePos, 0.0f, 1.0f);
278 uint16_t note_x = notePos * (WIDTH - 1);
279 uint16_t h = HEIGHT / 8;
280 // magenta color for pitch
281 framebuffer[frameBufferXY(note_x, h)] = fl::CRGB(255, 0, 255);
282 }
283
284 if (true) {
285 uint16_t fade_width = fade * (WIDTH - 1);
286 uint16_t h = HEIGHT / 4;
287 // yellow
288 int index = frameBufferXY(fade_width, h);
289 auto c = fl::CRGB(255, 255, 0);
290 framebuffer[index] = c;
291 }
292 }
293
294 // now downscale the framebuffer to the led matrix
296
297 FastLED.show();
298}
fl::UIAudio audio("Audio Input")
fl::CRGB leds[NUM_LEDS]
fl::CRGB framebuffer[NUM_LEDS]
Definition simple.h:77
int y
Definition simple.h:93
void shiftUp()
Definition simple.h:158
fl::MaxFadeTracker audioFadeTracker(attackTimeSeconds.value(), decayTimeSeconds.value(), outputTimeSec.value(), 44100)
fl::audio::SoundLevelMeter soundLevelMeter(.0, 0.0)
bool triggered
Definition simple.h:94
fl::audio::fft::Bins fftOut(WIDTH)
float rms(fl::span< const int16_t > data)
Definition simple.h:104
int x
Definition simple.h:92
fl::XYMap frameBufferXY(WIDTH, HEIGHT, IS_SERPINTINE)
fl::UICheckbox enableRMS("Enable RMS visualization", false)
fl::UICheckbox enableFFT("Enable FFT visualization", true)
bool doFrame()
Definition simple.h:178
fl::UICheckbox enablePitchDetect("Enable pitch detection", false)
fl::XYMap ledsXY(WIDTH/2, HEIGHT/2, IS_SERPINTINE)
fl::UICheckbox enableVolumeVis("Enable volume visualization", false)
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
Definition math.h:75
#define WIDTH
uint8_t currentMIDINote
Definition advanced.h:104
#define HEIGHT
bool noteIsOn
Definition advanced.h:106
fl::SoundToMIDIEngine * pitchEngine
Definition advanced.h:103
CRGB ColorFromPalette(const CRGBPalette16 &pal, fl::u8 index, fl::u8 brightness, TBlendType blendType)
const TProgmemRGBPalette16 HeatColors_p
Approximate "black body radiation" palette, akin to the FastLED HeatColor() function.
#define FL_WARN(X)
Definition log.h:276
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
fl::u16 uint16_t
Definition s16x16x4.h:214
void downscale(const CRGB *src, const XYMap &srcXY, CRGB *dst, const XYMap &dstXY)
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
FASTLED_FORCE_INLINE float fade(float t)
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
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
#define FASTLED_UNUSED(x)

References audio, audioFadeTracker(), fl::clamp(), ColorFromPalette(), currentMIDINote, doFrame(), enableFFT(), enablePitchDetect(), enableRMS(), enableVolumeVis(), FastLED, FASTLED_UNUSED, fftOut(), FL_WARN, framebuffer, frameBufferXY(), HeatColors_p, HEIGHT, leds, ledsXY(), fl::map_range(), max(), fl::min(), noteIsOn, pitchEngine, rms(), shiftUp(), soundLevelMeter(), triggered, WIDTH, x, and y.

+ Here is the call graph for this function: