Line data Source code
1 : #pragma once
2 :
3 : #include "audio/dsp/spectrum_analyzer.h"
4 : #include "audio/dsp/level_analyzer.h"
5 : #include "gui/ui_component.h"
6 : #include <imgui.h>
7 : #include <functional>
8 :
9 : namespace Amplitron {
10 :
11 : enum class SpectrumDisplayMode {
12 : Input = 0,
13 : Output = 1,
14 : Overlay = 2,
15 : };
16 :
17 8 : struct SpectrumSnapshot {
18 8 : std::array<float, SpectrumAnalyzer::DISPLAY_BARS> smoothed_input_db{};
19 8 : std::array<float, SpectrumAnalyzer::DISPLAY_BARS> smoothed_output_db{};
20 8 : std::array<float, SpectrumAnalyzer::DISPLAY_BARS> input_peak_db{};
21 8 : std::array<float, SpectrumAnalyzer::DISPLAY_BARS> output_peak_db{};
22 : };
23 :
24 8 : struct AnalyzerProps {
25 : // VU levels (pre-calculated by LevelAnalyzer in the audio engine)
26 8 : float smoothed_input_rms = 0.0f;
27 8 : float smoothed_output_rms = 0.0f;
28 8 : float input_peak_hold = 0.0f;
29 8 : float output_peak_hold = 0.0f;
30 8 : bool input_clip_active = false;
31 8 : bool output_clip_active = false;
32 8 : float input_clip_flash = 0.0f;
33 8 : float output_clip_flash = 0.0f;
34 :
35 : // Spectrum analyzer (pre-calculated by SpectrumAnalyzer in the audio engine)
36 : SpectrumSnapshot spectrum;
37 :
38 : std::function<void(bool)> on_expanded_changed;
39 : std::function<void(SpectrumDisplayMode)> on_mode_changed;
40 : std::function<void(bool)> on_set_analyzer_enabled;
41 : };
42 :
43 : /**
44 : * @brief Reactive real-time analyzer panel component.
45 : *
46 : * Receives all pre-computed level and spectrum data via AnalyzerProps.
47 : * Zero math or signal processing occurs here — only ImGui drawing calls.
48 : */
49 6 : class GuiAnalyzer : public UIComponent<AnalyzerProps> {
50 : public:
51 30 : GuiAnalyzer() = default;
52 :
53 : /** @brief Render the collapsible analyzer panel. */
54 : void render() override;
55 :
56 : /** @brief Height to reserve for this panel in the parent layout. */
57 2 : float analyzer_reserved_height() const { return expanded_ ? 245.0f : 38.0f; }
58 :
59 : SpectrumDisplayMode current_mode() const { return mode_; }
60 2 : bool is_expanded() const { return expanded_; }
61 :
62 : private:
63 : void render_vu_bar(const char* id,
64 : const char* label,
65 : float rms_level,
66 : float peak_hold,
67 : bool clip_active,
68 : float clip_flash,
69 : ImU32 base_color,
70 : ImU32 peak_color);
71 :
72 : void draw_spectrum(ImDrawList* dl,
73 : const ImVec2& pos,
74 : const ImVec2& size) const;
75 :
76 6 : bool expanded_ = true;
77 6 : SpectrumDisplayMode mode_ = SpectrumDisplayMode::Output;
78 : };
79 :
80 : } // namespace Amplitron
|