FastLED 3.10.8
Loading...
Searching...
No Matches
fl::audio::FrequencyBinMapper Class Reference

Detailed Description

FrequencyBinMapper maps FFT output bins to perceptually-spaced frequency channels.

This class converts the linear FFT spectrum into logarithmically-spaced frequency bands that better match human hearing. It supports both 16-bin (WLED-compatible) and 32-bin (higher resolution) modes with pre-calculated bin boundaries for efficient real-time processing.

Frequency ranges (16-bin mode, log spacing 20-16000 Hz):

  • Bass: bins 0-1 (20-80 Hz)
  • Mid: bins 6-7 (320-640 Hz)
  • Treble: bins 14-15 (5120-16000 Hz)

Usage:

config.sampleRate = 22050;
config.fftBinCount = 256; // 512-sample FFT / 2
mapper.configure(config);
// Map FFT output to frequency bins
Bins fftOutput = ...; // From FFT
vector<float> frequencyBins(16);
mapper.mapBins(fftOutput.raw(), frequencyBins);
// Access specific ranges
float bassEnergy = mapper.getBassEnergy(frequencyBins);
float midEnergy = mapper.getMidEnergy(frequencyBins);
float trebleEnergy = mapper.getTrebleEnergy(frequencyBins);
void configure(const FrequencyBinMapperConfig &config) FL_NO_EXCEPT
Configure the frequency bin mapper This calculates bin boundaries and FFT-to-frequency bin mappings.
float getBassEnergy(span< const float > frequencyBins) const FL_NO_EXCEPT
Get bass energy (average of bins 0-1 in 16-bin mode)
float getTrebleEnergy(span< const float > frequencyBins) const FL_NO_EXCEPT
Get treble energy (average of bins 14-15 in 16-bin mode)
float getMidEnergy(span< const float > frequencyBins) const FL_NO_EXCEPT
Get mid energy (average of bins 6-7 in 16-bin mode)
void mapBins(span< const float > fftBins, span< float > outputBins) const FL_NO_EXCEPT
Map FFT bins to frequency channels.
u32 fftBinCount
Number of FFT bins available from FFT output For 512-sample FFT at 22050 Hz: 256 bins (512/2)
u32 sampleRate
Sample rate (Hz) - must match FFT sample rate.
FrequencyBinMode mode
Number of output frequency bins (16 or 32)
Configuration for frequency bin mapping.

Definition at line 71 of file frequency_bin_mapper.h.

#include <frequency_bin_mapper.h>

+ Collaboration diagram for fl::audio::FrequencyBinMapper:

Classes

struct  BinMapping
 Mapping from output bins to FFT bin ranges Each entry contains (startBin, endBin) for averaging. More...
 
struct  FrequencyRange
 Get frequency boundaries for a specific output bin. More...
 
struct  Stats
 Get statistics (for debugging/monitoring) More...
 

Public Member Functions

 FrequencyBinMapper () FL_NO_EXCEPT
 
 FrequencyBinMapper (const FrequencyBinMapperConfig &config) FL_NO_EXCEPT
 
 ~FrequencyBinMapper () FL_NO_EXCEPT
 
void configure (const FrequencyBinMapperConfig &config) FL_NO_EXCEPT
 Configure the frequency bin mapper This calculates bin boundaries and FFT-to-frequency bin mappings.
 
float getBassEnergy (span< const float > frequencyBins) const FL_NO_EXCEPT
 Get bass energy (average of bins 0-1 in 16-bin mode)
 
FrequencyRange getBinFrequencyRange (size binIndex) const FL_NO_EXCEPT
 
const FrequencyBinMapperConfig & getConfig () const FL_NO_EXCEPT
 Get current configuration.
 
float getMidEnergy (span< const float > frequencyBins) const FL_NO_EXCEPT
 Get mid energy (average of bins 6-7 in 16-bin mode)
 
size getNumBins () const FL_NO_EXCEPT
 Get number of output bins (16 or 32)
 
const Stats & getStats () const FL_NO_EXCEPT
 
float getTrebleEnergy (span< const float > frequencyBins) const FL_NO_EXCEPT
 Get treble energy (average of bins 14-15 in 16-bin mode)
 
void mapBins (span< const float > fftBins, span< float > outputBins) const FL_NO_EXCEPT
 Map FFT bins to frequency channels.
 

Private Member Functions

void calculateBinBoundaries () FL_NO_EXCEPT
 Calculate frequency bin boundaries (linear or logarithmic spacing)
 
void calculateBinMappings () FL_NO_EXCEPT
 Calculate FFT bin to frequency bin mappings Pre-calculates which FFT bins contribute to each frequency bin.
 
void calculateLinearFrequencies () FL_NO_EXCEPT
 Calculate linearly-spaced frequency boundaries.
 
void calculateLogFrequencies () FL_NO_EXCEPT
 Calculate logarithmically-spaced frequency boundaries.
 
float frequencyToFFTBin (float frequency) const FL_NO_EXCEPT
 Convert frequency (Hz) to FFT bin index.
 

Private Attributes

vector< float > mBinFrequencies
 Pre-calculated frequency boundaries for each output bin Size: numBins + 1 (includes both lower and upper edges)
 
vector< BinMapping > mBinMappings
 
FrequencyBinMapperConfig mConfig
 
Stats mStats
 

Static Private Attributes

static constexpr size BASS_BIN_END = 2
 
static constexpr size BASS_BIN_START = 0
 Bass/mid/treble bin indices (for 16-bin mode) These are pre-calculated based on the bin count.
 
static constexpr size MID_BIN_END = 8
 
static constexpr size MID_BIN_START = 6
 
static constexpr size TREBLE_BIN_END = 16
 
static constexpr size TREBLE_BIN_START = 14
 

The documentation for this class was generated from the following files: