arduino-audio-tools
FFTDisplay.h
1 #pragma once
2 #include "AudioLibs/AudioFFT.h"
3 
4 namespace audio_tools {
5 
6 class FFTDisplay;
7 FFTDisplay *selfFFTDisplay = nullptr;
8 #if defined(USE_CONCURRENCY)
9 // fft mutex
10 static Mutex fft_mux;
11 #endif
17 class FFTDisplay {
18  public:
19  FFTDisplay(AudioFFTBase &fft) {
20  p_fft = &fft;
21  selfFFTDisplay = this;
22  }
23 
25  int fft_start_bin = 0;
27  int fft_group_bin = 1;
29  float fft_max_magnitude = 700.0f;
30 
31  void begin() {
32  // assign fft callback
33  AudioFFTConfig &fft_cfg = p_fft->config();
34  fft_cfg.callback = fftCallback;
35 
36  // number of bins
37  magnitudes.resize(p_fft->size());
38  for (int j = 0; j < p_fft->size(); j++) {
39  magnitudes[j] = 0;
40  }
41  }
42 
45  float getMagnitude(int x) {
46  // get magnitude from fft
47  float total = 0;
48  for (int j = 0; j < fft_group_bin; j++) {
49  int idx = fft_start_bin + (x * fft_group_bin) + j;
50  if (idx >= magnitudes.size()) {
51  idx = magnitudes.size() - 1;
52  }
53  total += magnitudes[idx];
54  }
55  return total / fft_group_bin;
56  }
57 
58  int getMagnitudeScaled(int x, int max) {
59  int result = mapFloat(getMagnitude(x), 0, fft_max_magnitude, 0.0f,
60  static_cast<float>(max));
61  if (result > max){
62  LOGD("fft_max_magnitude too small: current value is %f", getMagnitude(x))
63  }
64  // limit value to max
65  return min(result, max);
66  }
67 
69  static void fftCallback(AudioFFTBase &fft) {
70  selfFFTDisplay->loadMangnitudes();
71  };
72 
73  protected:
74  AudioFFTBase *p_fft = nullptr;
75  Vector<float> magnitudes{0};
76 
77  void loadMangnitudes() {
78  // just save magnitudes to be displayed
79 #if defined(USE_CONCURRENCY)
80  LockGuard guard(fft_mux);
81 #endif
82  for (int j = 0; j < p_fft->size(); j++) {
83  float value = p_fft->magnitude(j);
84  magnitudes[j] = value;
85  }
86  }
87 };
88 
89 } // namespace audio_tools
Executes FFT using audio data. The Driver which is passed in the constructor selects a specifc FFT im...
Definition: AudioFFT.h:82
float magnitude(int bin)
Calculates the magnitude of the fft result to determine the max value (bin is 0 to size())
Definition: AudioFFT.h:245
AudioFFTConfig & config()
Provides the actual configuration.
Definition: AudioFFT.h:283
int size()
The number of bins used by the FFT which are relevant for the result.
Definition: AudioFFT.h:177
Definition: FFTDisplay.h:17
float fft_max_magnitude
Influences the senitivity.
Definition: FFTDisplay.h:29
int fft_group_bin
group result by adding subsequent bins
Definition: FFTDisplay.h:27
static void fftCallback(AudioFFTBase &fft)
callback method which provides updated data from fft
Definition: FFTDisplay.h:69
int fft_start_bin
start bin which is displayed
Definition: FFTDisplay.h:25
float getMagnitude(int x)
Definition: FFTDisplay.h:45
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AnalogAudio.h:10
float mapFloat(float x, float in_min, float in_max, float out_min, float out_max)
Similar to Arduino map function but using floats.
Definition: AudioTypes.h:483
Configuration for AudioFFT. If there are more then 1 channel the channel_used is defining which chann...
Definition: AudioFFT.h:43
void(* callback)(AudioFFTBase &fft)
Callback method which is called after we got a new result.
Definition: AudioFFT.h:50