arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
FFTDisplay.h
1#pragma once
2#include "AudioTools/AudioLibs/AudioFFT.h"
3#include "AudioTools/Concurrency/LockGuard.h"
4
5namespace audio_tools {
6
7class FFTDisplay;
8static FFTDisplay *selfFFTDisplay = nullptr;
9#if defined(USE_CONCURRENCY)
10// fft mutex
11static Mutex fft_mux;
12#endif
19 public:
21 p_fft = &fft;
22 selfFFTDisplay = this;
23 }
24
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 privded by write() and/or an inverse FFT where the samples are made ava...
Definition AudioFFT.h:191
float magnitude(int bin)
Definition AudioFFT.h:411
AudioFFTConfig & config()
Provides the actual configuration.
Definition AudioFFT.h:482
int size()
The number of bins used by the FFT which are relevant for the result.
Definition AudioFFT.h:344
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
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioConfig.h:885
Configuration for AudioFFT. If there are more then 1 channel the channel_used is defining which chann...
Definition AudioFFT.h:40
void(* callback)(AudioFFTBase &fft)
Callback method which is called after we got a new result.
Definition AudioFFT.h:47