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