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