Line data Source code
1 : #pragma once
2 :
3 : #include "common.h"
4 : #include <array>
5 : #include <complex>
6 :
7 : namespace Amplitron {
8 :
9 : /**
10 : * @brief Performs Fast Fourier Transform (FFT) and frequency magnitude calculations.
11 : * Completely decoupled from GUI rendering libraries (ImGui/SDL).
12 : */
13 : class SpectrumAnalyzer {
14 : public:
15 : static constexpr int FFT_SIZE = 2048;
16 : static constexpr int FFT_BINS = FFT_SIZE / 2;
17 : static constexpr int DISPLAY_BARS = 96;
18 :
19 : SpectrumAnalyzer();
20 :
21 : /**
22 : * @brief Perform FFT analysis and update the smoothed frequency DB arrays.
23 : */
24 : void update(const float* input_samples,
25 : const float* output_samples,
26 : int sample_rate,
27 : float dt_seconds);
28 :
29 : // Getters for display data (so the UI only reads raw arrays)
30 966 : const std::array<float, DISPLAY_BARS>& smoothed_input_db() const { return smoothed_input_db_; }
31 388 : const std::array<float, DISPLAY_BARS>& smoothed_output_db() const { return smoothed_output_db_; }
32 196 : const std::array<float, DISPLAY_BARS>& input_peak_db() const { return input_peak_db_; }
33 196 : const std::array<float, DISPLAY_BARS>& output_peak_db() const { return output_peak_db_; }
34 :
35 : private:
36 : void compute_spectrum_bars(const float* samples,
37 : int sample_rate,
38 : std::array<float, DISPLAY_BARS>& bars_db);
39 : void run_fft(std::array<std::complex<float>, FFT_SIZE>& data) const;
40 :
41 : std::array<float, FFT_SIZE> window_{};
42 : std::array<std::complex<float>, FFT_SIZE> fft_work_{};
43 :
44 : std::array<float, DISPLAY_BARS> smoothed_input_db_{};
45 : std::array<float, DISPLAY_BARS> smoothed_output_db_{};
46 : std::array<float, DISPLAY_BARS> input_peak_db_{};
47 : std::array<float, DISPLAY_BARS> output_peak_db_{};
48 : };
49 :
50 : } // namespace Amplitron
|