FastLED 3.9.15
Loading...
Searching...
No Matches
audio_reactive.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/fft.h"
4#include "fl/math.h"
5#include "fl/vector.h"
6#include "fl/stdint.h"
7#include "fl/int.h"
8#include "fl/audio.h"
9#include "crgb.h"
10#include "fl/colorutils.h"
11
12namespace fl {
13
14// Audio data structure - matches original WLED output
15struct AudioData {
16 float volume = 0.0f; // Overall volume level (0-255)
17 float volumeRaw = 0.0f; // Raw volume without smoothing
18 float peak = 0.0f; // Peak level (0-255)
19 bool beatDetected = false; // Beat detection flag
20 float frequencyBins[16] = {0}; // 16 frequency bins (matches WLED NUM_GEQ_CHANNELS)
21 float dominantFrequency = 0.0f; // Major peak frequency (Hz)
22 float magnitude = 0.0f; // FFT magnitude of dominant frequency
23 fl::u32 timestamp = 0; // millis() when data was captured
24};
25
27 fl::u8 gain = 128; // Input gain (0-255)
28 fl::u8 sensitivity = 128; // AGC sensitivity
29 bool agcEnabled = true; // Auto gain control
30 bool noiseGate = true; // Noise gate
31 fl::u8 attack = 50; // Attack time (ms) - how fast to respond to increases
32 fl::u8 decay = 200; // Decay time (ms) - how slow to respond to decreases
33 u16 sampleRate = 22050; // Sample rate (Hz)
34 fl::u8 scalingMode = 3; // 0=none, 1=log, 2=linear, 3=sqrt
35};
36
38public:
41
42 // Setup
43 void begin(const AudioConfig& config = AudioConfig{});
44 void setConfig(const AudioConfig& config);
45
46 // Process audio sample - this does all the work immediately
47 void processSample(const AudioSample& sample);
48
49 // Optional: update smoothing without new sample data
50 void update(fl::u32 currentTimeMs);
51
52 // Data access
53 const AudioData& getData() const;
54 const AudioData& getSmoothedData() const;
55
56 // Convenience accessors
57 float getVolume() const;
58 float getBass() const; // Average of bins 0-1
59 float getMid() const; // Average of bins 6-7
60 float getTreble() const; // Average of bins 14-15
61 bool isBeat() const;
62
63 // Effect helpers
65 CRGB volumeToColor(const CRGBPalette16& palette) const;
66 fl::u8 frequencyToScale255(fl::u8 binIndex) const;
67
68private:
69 // Internal processing methods
70 void processFFT(const AudioSample& sample);
72 void updateVolumeAndPeak(const AudioSample& sample);
73 void detectBeat(fl::u32 currentTimeMs);
74 void smoothResults();
75 void applyScaling();
76 void applyGain();
77
78 // Helper methods
79 float mapFrequencyBin(int fromBin, int toBin);
80 float computeRMS(const fl::vector<fl::i16>& samples);
81
82 // Configuration
84
85 // FFT processing
88
89 // Audio data
92
93 // Processing state
94 fl::u32 mLastBeatTime = 0;
95 static constexpr fl::u32 BEAT_COOLDOWN = 100; // 100ms minimum between beats
96
97 // Volume tracking for beat detection
98 float mPreviousVolume = 0.0f;
99 float mVolumeThreshold = 10.0f;
100
101 // Pink noise compensation (from WLED)
102 static constexpr float PINK_NOISE_COMPENSATION[16] = {
103 1.70f, 1.71f, 1.73f, 1.78f, 1.68f, 1.56f, 1.55f, 1.63f,
104 1.79f, 1.62f, 1.80f, 2.06f, 2.47f, 3.35f, 6.83f, 9.55f
105 };
106
107 // AGC state
108 float mAGCMultiplier = 1.0f;
109 float mMaxSample = 0.0f;
110 float mAverageLevel = 0.0f;
111};
112
113
114} // namespace fl
UINumberField palette("Palette", 0, 0, 2)
CRGB volumeToColor(const CRGBPalette16 &palette) const
const AudioData & getData() const
void update(fl::u32 currentTimeMs)
void processFFT(const AudioSample &sample)
fl::u8 volumeToScale255() const
void detectBeat(fl::u32 currentTimeMs)
void mapFFTBinsToFrequencyChannels()
float getBass() const
float computeRMS(const fl::vector< fl::i16 > &samples)
static constexpr float PINK_NOISE_COMPENSATION[16]
void updateVolumeAndPeak(const AudioSample &sample)
void setConfig(const AudioConfig &config)
static constexpr fl::u32 BEAT_COOLDOWN
AudioData mSmoothedData
void begin(const AudioConfig &config=AudioConfig{})
float getVolume() const
float getTreble() const
float mapFrequencyBin(int fromBin, int toBin)
const AudioData & getSmoothedData() const
fl::u8 frequencyToScale255(fl::u8 binIndex) const
void processSample(const AudioSample &sample)
AudioConfig mConfig
Definition fft.h:98
Defines the red, green, and blue (RGB) pixel struct.
Utility functions for color fill, palettes, blending, and more.
unsigned char u8
Definition int.h:17
HeapVector< T, Allocator > vector
Definition vector.h:1214
IMPORTANT!
Definition crgb.h:20
float frequencyBins[16]
float dominantFrequency
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86