| 499 | } |
| 500 | |
| 501 | void loop() { |
| 502 | // Check if audio is enabled |
| 503 | if (!enableAudio) { |
| 504 | // Show a simple test pattern |
| 505 | fill_rainbow(leds, NUM_LEDS, hue++, 7); |
| 506 | FastLED.show(); |
| 507 | delay(20); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | // Process only one audio sample per frame to avoid accumulation |
| 512 | fl::audio::Sample sample = audio.next(); |
| 513 | if (sample.isValid()) { |
| 514 | // Process pitch detection if enabled |
| 515 | if (pitchDetectEnable && pitchEngine) { |
| 516 | // Convert int16_t samples to float for pitch detection |
| 517 | static float floatBuffer[FFT_SIZE]; |
| 518 | size_t numSamples = fl::min(sample.pcm().size(), (size_t)FFT_SIZE); |
| 519 | for (size_t i = 0; i < numSamples; i++) { |
| 520 | floatBuffer[i] = sample.pcm()[i] / 32768.0f; |
| 521 | } |
| 522 | pitchEngine->processFrame(floatBuffer, numSamples); |
| 523 | } |
| 524 | |
| 525 | // Update sound meter |
| 526 | soundMeter.processBlock(sample.pcm()); |
| 527 | |
| 528 | // Get audio levels |
| 529 | float rms = sample.rms() / 32768.0f; |
| 530 | |
| 531 | // Calculate peak |
| 532 | int32_t maxSample = 0; |
| 533 | for (size_t i = 0; i < sample.pcm().size(); i++) { |
| 534 | int32_t absSample = fl::fabsf(sample.pcm()[i]); |
| 535 | if (absSample > maxSample) { |
| 536 | maxSample = absSample; |
| 537 | } |
| 538 | } |
| 539 | float peak = float(maxSample) / 32768.0f; |
| 540 | peakLevel = peakLevel * 0.9f + peak * 0.1f; // Smooth peak |
| 541 | |
| 542 | // Update auto gain |
| 543 | updateAutoGain(rms); |
| 544 | |
| 545 | // Beat detection |
| 546 | if (beatDetect) { |
| 547 | isBeat = detectBeat(peak); |
| 548 | } |
| 549 | |
| 550 | // Get FFT data - create local Bins to avoid accumulation |
| 551 | fl::audio::fft::Bins fftBins(NUM_BANDS); |
| 552 | sample.fft(&fftBins); |
| 553 | |
| 554 | // Update color animation |
| 555 | hue += 1; |
| 556 | |
| 557 | // Apply beat flash |
| 558 | if (isBeat && beatFlash) { |
nothing calls this directly
no test coverage detected