Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
waveform_overview_view.hpp
Go to the documentation of this file.
1#pragma once
2
10
13#include <pulp/view/view.hpp>
14
15#include <algorithm>
16#include <cstdint>
17
18namespace pulp::view {
19
29class WaveformOverviewView : public View {
30 public:
32
37 overview_ = overview;
38 channel_ = channel;
39 view_all();
40 }
41
43 overview_ = nullptr;
44 vp_start_ = 0;
45 vp_end_ = 0;
46 }
47
48 bool has_overview() const {
49 return overview_ != nullptr && !overview_->empty();
50 }
51
53 uint64_t source_frames() const {
54 return overview_ ? overview_->info().num_source_frames : 0;
55 }
56
57 // ── Viewport ─────────────────────────────────────────────────────────
58
61 void set_viewport(uint64_t start_frame, uint64_t end_frame) {
62 const uint64_t total = source_frames();
63 vp_start_ = std::min(start_frame, total);
64 vp_end_ = std::min(end_frame, total);
65 if (vp_end_ < vp_start_)
66 vp_end_ = vp_start_;
67 }
68
69 uint64_t viewport_start() const {
70 return vp_start_;
71 }
72 uint64_t viewport_end() const {
73 return vp_end_;
74 }
75
77 void view_all() {
78 vp_start_ = 0;
79 vp_end_ = source_frames();
80 }
81
82 // ── Styling ──────────────────────────────────────────────────────────
84 line_color_ = c;
85 }
87 bg_color_ = c;
88 }
90 return line_color_;
91 }
93 return bg_color_;
94 }
95
100 bool column_peak(uint64_t f0, uint64_t f1, float& out_min, float& out_max) const {
101 if (!has_overview() || f1 <= f0)
102 return false;
103 const auto& lvl = overview_->level(0);
104 if (lvl.samples_per_peak == 0 || lvl.peaks.empty())
105 return false;
106
107 uint64_t p0 = f0 / lvl.samples_per_peak;
108 uint64_t p1 = (f1 + lvl.samples_per_peak - 1) / lvl.samples_per_peak;
109 if (p1 <= p0)
110 p1 = p0 + 1;
111 if (p0 >= lvl.peaks_per_channel)
112 return false;
113 p1 = std::min<uint64_t>(p1, lvl.peaks_per_channel);
114
115 float lo = 1.0f, hi = -1.0f;
116 const auto fold = [&](uint32_t ch) {
117 if (ch >= lvl.peaks.size())
118 return;
119 const auto& col = lvl.peaks[ch];
120 for (uint64_t i = p0; i < p1 && i < col.size(); ++i) {
121 lo = std::min(lo, col[i].min());
122 hi = std::max(hi, col[i].max());
123 }
124 };
126 for (uint32_t ch = 0; ch < lvl.peaks.size(); ++ch)
127 fold(ch);
128 } else {
129 fold(channel_);
130 }
131 if (hi < lo)
132 return false; // no peaks landed in the window
133 out_min = lo;
134 out_max = hi;
135 return true;
136 }
137
138 void paint(canvas::Canvas& canvas) override {
139 const Rect b = local_bounds();
140
141 canvas.set_fill_color(bg_color_);
142 canvas.fill_rect(0.0f, 0.0f, b.width, b.height);
143
144 if (!has_overview() || vp_end_ <= vp_start_ || b.width < 1.0f)
145 return;
146
147 const float cy = b.height * 0.5f;
148 const float half_h = b.height * 0.5f;
149 canvas.set_stroke_color(line_color_);
150 canvas.set_line_width(1.0f);
151
152 const uint64_t span = vp_end_ - vp_start_;
153 const int cols = static_cast<int>(b.width);
154 for (int px = 0; px < cols; ++px) {
155 const uint64_t cf0 =
156 vp_start_ + span * static_cast<uint64_t>(px) / static_cast<uint64_t>(cols);
157 uint64_t cf1 =
158 vp_start_ + span * static_cast<uint64_t>(px + 1) / static_cast<uint64_t>(cols);
159 if (cf1 <= cf0)
160 cf1 = cf0 + 1;
161
162 float lo = 0.0f, hi = 0.0f;
163 if (!column_peak(cf0, cf1, lo, hi))
164 continue;
165
166 const float x = static_cast<float>(px) + 0.5f;
167 const float y_top = cy - hi * half_h;
168 float y_bot = cy - lo * half_h;
169 if (y_bot - y_top < 1.0f)
170 y_bot = y_top + 1.0f; // keep 1px minimum
171 canvas.stroke_line(x, y_top, x, y_bot);
172 }
173 }
174
175 private:
176 const pulp::audio::WaveformOverview* overview_ = nullptr;
178 uint64_t vp_start_ = 0;
179 uint64_t vp_end_ = 0;
180 canvas::Color line_color_ = canvas::Color::rgba8(100, 180, 250);
181 canvas::Color bg_color_ = canvas::Color::rgba8(20, 20, 30);
182};
183
184} // namespace pulp::view
Definition waveform_overview.hpp:89
static constexpr uint32_t kAllChannels
Definition waveform_overview.hpp:133
Definition canvas.hpp:240
Rect local_bounds() const
Definition view.hpp:88
void paint(canvas::Canvas &canvas) override
Definition waveform_overview_view.hpp:138
void view_all()
Reset the viewport to the whole source.
Definition waveform_overview_view.hpp:77
canvas::Color background_color() const
Definition waveform_overview_view.hpp:92
bool has_overview() const
Definition waveform_overview_view.hpp:48
void clear_overview()
Definition waveform_overview_view.hpp:42
canvas::Color line_color() const
Definition waveform_overview_view.hpp:89
uint64_t viewport_end() const
Definition waveform_overview_view.hpp:72
uint64_t viewport_start() const
Definition waveform_overview_view.hpp:69
void set_line_color(canvas::Color c)
Definition waveform_overview_view.hpp:83
uint64_t source_frames() const
Total source frames behind the overview (0 when none is set).
Definition waveform_overview_view.hpp:53
void set_overview(const pulp::audio::WaveformOverview *overview, uint32_t channel=pulp::audio::WaveformOverview::kAllChannels)
Point the view at an overview (borrowed — the caller keeps it alive, or calls clear_overview).
Definition waveform_overview_view.hpp:35
void set_background_color(canvas::Color c)
Definition waveform_overview_view.hpp:86
bool column_peak(uint64_t f0, uint64_t f1, float &out_min, float &out_max) const
Folded (min, max) over the source-frame window [f0, f1), using the finest overview level.
Definition waveform_overview_view.hpp:100
void set_viewport(uint64_t start_frame, uint64_t end_frame)
Source-frame window [start, end) to display.
Definition waveform_overview_view.hpp:61
Definition affine_transform.hpp:24
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
@ px
Absolute pixels.
Definition geometry.hpp:393
@ c
Definition input_events.hpp:39
@ i
Definition input_events.hpp:40
@ b
Definition input_events.hpp:39
@ x
Definition input_events.hpp:41
@ f1
Definition input_events.hpp:61
Definition canvas.hpp:52
static constexpr Color rgba8(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Construct from 8-bit channels [0,255] — convenience for legacy/hex code.
Definition canvas.hpp:61
Axis-aligned rectangle in continuous (float) coordinates.
Definition geometry.hpp:52