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

◆ loop()

void loop ( )

Definition at line 143 of file Audio.ino.

143 {
144 if (triggered) {
145 FASTLED_WARN("Triggered");
146 }
147 // fl::clear(framebuffer);
148 // fl::clear(framebuffer);
149
150 static uint32_t frame = 0;
151
152 // x = pointX.as_int();
153 y = HEIGHT / 2;
154
155 bool do_frame = doFrame();
156
157 while (AudioSample sample = audio.next()) {
158 if (!do_frame) {
159 continue;
160 }
161 float fade = audioFadeTracker(sample.pcm().data(), sample.pcm().size());
162 shiftUp();
163 // FASTLED_WARN("Audio sample size: " << sample.pcm().size());
164 soundLevelMeter.processBlock(sample.pcm());
165 // FASTLED_WARN("")
166 auto dbfs = soundLevelMeter.getDBFS();
167 // FASTLED_WARN("getDBFS: " << dbfs);
168 int32_t max = 0;
169 for (int i = 0; i < sample.pcm().size(); ++i) {
170 int32_t x = ABS(sample.pcm()[i]);
171 if (x > max) {
172 max = x;
173 }
174 }
175 float anim =
176 fl::map_range<float, float>(max, 0.0f, 32768.0f, 0.0f, 1.0f);
177 anim = fl::clamp(anim, 0.0f, 1.0f);
178
179 x = fl::map_range<float, float>(anim, 0.0f, 1.0f, 0.0f, WIDTH - 1);
180 // FASTLED_WARN("x: " << x);
181
182 // fft.run(sample.pcm(), &fftOut);
183 sample.fft(&fftOut);
184
185 // FASTLED_ASSERT(fftOut.bins_raw.size() == WIDTH_2X,
186 // "FFT bins size mismatch");
187
188 if (enableFFT) {
189 auto max_x = fftOut.bins_raw.size() - 1;
190 for (int i = 0; i < fftOut.bins_raw.size(); ++i) {
191 auto x = i;
192 auto v = fftOut.bins_db[i];
193 // Map audio intensity to a position in the heat palette (0-255)
194 v = fl::map_range<float, float>(v, 45, 70, 0, 1.f);
195 v = fl::clamp(v, 0.0f, 1.0f);
196 uint8_t heatIndex =
197 fl::map_range<float, uint8_t>(v, 0, 1, 0, 255);
198
199 // FASTLED_WARN(v);
200
201 // Use FastLED's built-in HeatColors palette
202 auto c = ColorFromPalette(HeatColors_p, heatIndex);
203 c.fadeToBlackBy(255 - heatIndex);
204 framebuffer[frameBufferXY(x, 0)] = c;
205 // FASTLED_WARN("y: " << i << " b: " << b);
206 }
207 }
208
209 if (enableVolumeVis) {
210 framebuffer[frameBufferXY(x, HEIGHT / 2)] = CRGB(0, 255, 0);
211 }
212
213 if (enableRMS) {
214 float rms = sample.rms();
215 FASTLED_WARN("RMS: " << rms);
216 rms = fl::map_range<float, float>(rms, 0.0f, 32768.0f, 0.0f, 1.0f);
217 rms = fl::clamp(rms, 0.0f, 1.0f) * WIDTH;
218 framebuffer[frameBufferXY(rms, HEIGHT * 3 / 4)] = CRGB(0, 0, 255);
219 }
220 if (true) {
221 uint16_t fade_width = fade * (WIDTH - 1);
222 uint16_t h = HEIGHT / 4;
223 // yellow
224 int index = frameBufferXY(fade_width, h);
225 auto c = CRGB(255, 255, 0);
226 framebuffer[index] = c;
227 }
228 }
229
230 // now downscale the framebuffer to the led matrix
232
233 FastLED.show();
234}
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
int y
Definition Audio.ino:72
void shiftUp()
Definition Audio.ino:113
#define WIDTH
Definition Audio.ino:33
UICheckbox enableRMS("Enable RMS visualization", false)
SoundLevelMeter soundLevelMeter(.0, 0.0)
bool triggered
Definition Audio.ino:73
float rms(Slice< const int16_t > data)
Definition Audio.ino:77
UICheckbox enableFFT("Enable FFT visualization", true)
int x
Definition Audio.ino:71
XYMap ledsXY(WIDTH/2, HEIGHT/2, IS_SERPINTINE)
CRGB framebuffer[NUM_LEDS]
Definition Audio.ino:56
UICheckbox enableVolumeVis("Enable volume visualization", false)
UIAudio audio("Audio")
XYMap frameBufferXY(WIDTH, HEIGHT, IS_SERPINTINE)
MaxFadeTracker audioFadeTracker(attackTimeSeconds.value(), decayTimeSeconds.value(), outputTimeSec.value(), 44100)
bool doFrame()
Definition Audio.ino:133
#define HEIGHT
Definition Audio.ino:32
FFTBins fftOut(WIDTH)
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:58
const TProgmemRGBPalette16 HeatColors_p
Approximate "black body radiation" palette, akin to the FastLED HeatColor() function.
#define ABS(x)
Definition math_macros.h:19
void downscale(const CRGB *src, const XYMap &srcXY, CRGB *dst, const XYMap &dstXY)
FASTLED_FORCE_INLINE T clamp(T value, T min, T max)
Definition clamp.h:10
CRGB ColorFromPalette(const CRGBPalette16 &pal, uint8_t index, uint8_t brightness, TBlendType blendType)
FASTLED_FORCE_INLINE U map_range(T value, T in_min, T in_max, U out_min, U out_max)
Definition map_range.h:26
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:55
#define FASTLED_WARN
Definition warn.h:7

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

+ Here is the call graph for this function: