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