| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#ifndef DEBUG_UI_SCOPE_H |
| 11 |
#define DEBUG_UI_SCOPE_H |
| 12 |
|
| 13 |
#include "imgui.h" |
| 14 |
#include <stdint.h> |
| 15 |
#include <stddef.h> |
| 16 |
#include <stdio.h> |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "debug_ui_bridge.h" |
| 21 |
#include "debug_ui_settings.h" |
| 22 |
|
| 23 |
|
| 24 |
#define DBGUI_SCOPE_AUTO_GAIN 0.0f |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
static inline int |
| 32 |
dbgui_scope_trigger_offset(const int16_t *samples, int count, int window) |
| 33 |
{ |
| 34 |
int latest = count - window; |
| 35 |
if (latest <= 0) { |
| 36 |
return 0; |
| 37 |
} |
| 38 |
for (int i = latest; i > 0; --i) { |
| 39 |
if (samples[i - 1] < 0 && samples[i] >= 0) { |
| 40 |
return i; |
| 41 |
} |
| 42 |
} |
| 43 |
return latest; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
static inline int |
| 59 |
dbgui_scope(const char *id, const int16_t *samples, int count, ImVec2 size, |
| 60 |
ImU32 color, int window, float gain, bool trigger, const char *overlay) |
| 61 |
{ |
| 62 |
if (size.x <= 0.0f) { |
| 63 |
size.x = ImGui::GetContentRegionAvail().x; |
| 64 |
} |
| 65 |
if (size.x < 16.0f) { |
| 66 |
size.x = 16.0f; |
| 67 |
} |
| 68 |
if (size.y <= 0.0f) { |
| 69 |
size.y = ImGui::GetTextLineHeight() * 4.0f; |
| 70 |
} |
| 71 |
|
| 72 |
ImGui::PushID(id); |
| 73 |
ImGui::InvisibleButton("##scope", size); |
| 74 |
const ImVec2 p0 = ImGui::GetItemRectMin(); |
| 75 |
const ImVec2 p1 = ImGui::GetItemRectMax(); |
| 76 |
ImGui::PopID(); |
| 77 |
|
| 78 |
ImDrawList *dl = ImGui::GetWindowDrawList(); |
| 79 |
dl->AddRectFilled(p0, p1, IM_COL32(12, 14, 18, 255)); |
| 80 |
|
| 81 |
|
| 82 |
const ImU32 grid = IM_COL32(52, 58, 68, 255); |
| 83 |
const ImU32 axis = IM_COL32(90, 100, 115, 255); |
| 84 |
for (int i = 1; i < 4; ++i) { |
| 85 |
float x = p0.x + (p1.x - p0.x) * (float)i / 4.0f; |
| 86 |
float y = p0.y + (p1.y - p0.y) * (float)i / 4.0f; |
| 87 |
dl->AddLine(ImVec2(x, p0.y), ImVec2(x, p1.y), grid); |
| 88 |
dl->AddLine(ImVec2(p0.x, y), ImVec2(p1.x, y), (i == 2) ? axis : grid); |
| 89 |
} |
| 90 |
dl->AddRect(p0, p1, IM_COL32(70, 78, 90, 255)); |
| 91 |
|
| 92 |
if (samples == NULL || count <= 0) { |
| 93 |
return 0; |
| 94 |
} |
| 95 |
if (window <= 0 || window > count) { |
| 96 |
window = count; |
| 97 |
} |
| 98 |
|
| 99 |
const int start = trigger ? dbgui_scope_trigger_offset(samples, count, window) |
| 100 |
: (count - window); |
| 101 |
|
| 102 |
|
| 103 |
int peak = 0; |
| 104 |
for (int i = 0; i < window; ++i) { |
| 105 |
int v = samples[start + i]; |
| 106 |
if (v < 0) { |
| 107 |
v = -v; |
| 108 |
} |
| 109 |
if (v > peak) { |
| 110 |
peak = v; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
const float half = (p1.y - p0.y) * 0.5f - 1.0f; |
| 115 |
const float mid = (p0.y + p1.y) * 0.5f; |
| 116 |
float scale; |
| 117 |
if (gain > 0.0f) { |
| 118 |
scale = half * gain; |
| 119 |
} else { |
| 120 |
|
| 121 |
|
| 122 |
scale = (peak > 64) ? (half / (float)peak) : (half / 32768.0f); |
| 123 |
} |
| 124 |
|
| 125 |
dl->PushClipRect(p0, p1, true); |
| 126 |
|
| 127 |
const int columns = (int)(p1.x - p0.x); |
| 128 |
if (window <= columns) { |
| 129 |
|
| 130 |
for (int i = 1; i < window; ++i) { |
| 131 |
float xa = p0.x + (p1.x - p0.x) * (float)(i - 1) / (float)(window - 1); |
| 132 |
float xb = p0.x + (p1.x - p0.x) * (float)i / (float)(window - 1); |
| 133 |
float ya = mid - (float)samples[start + i - 1] * scale; |
| 134 |
float yb = mid - (float)samples[start + i] * scale; |
| 135 |
dl->AddLine(ImVec2(xa, ya), ImVec2(xb, yb), color, 1.0f); |
| 136 |
} |
| 137 |
} else { |
| 138 |
|
| 139 |
|
| 140 |
for (int c = 0; c < columns; ++c) { |
| 141 |
int lo = (int)((int64_t)c * window / columns); |
| 142 |
int hi = (int)((int64_t)(c + 1) * window / columns); |
| 143 |
if (hi <= lo) { |
| 144 |
hi = lo + 1; |
| 145 |
} |
| 146 |
int vmin = 32767, vmax = -32768; |
| 147 |
for (int i = lo; i < hi && i < window; ++i) { |
| 148 |
int v = samples[start + i]; |
| 149 |
if (v < vmin) { |
| 150 |
vmin = v; |
| 151 |
} |
| 152 |
if (v > vmax) { |
| 153 |
vmax = v; |
| 154 |
} |
| 155 |
} |
| 156 |
float x = p0.x + (float)c + 0.5f; |
| 157 |
float ya = mid - (float)vmax * scale; |
| 158 |
float yb = mid - (float)vmin * scale; |
| 159 |
if (yb - ya < 1.0f) { |
| 160 |
yb = ya + 1.0f; |
| 161 |
} |
| 162 |
dl->AddLine(ImVec2(x, ya), ImVec2(x, yb), color, 1.0f); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
dl->PopClipRect(); |
| 167 |
|
| 168 |
if (overlay && overlay[0]) { |
| 169 |
dl->AddText(ImVec2(p0.x + 4.0f, p0.y + 2.0f), IM_COL32(190, 198, 210, 220), overlay); |
| 170 |
} |
| 171 |
return peak; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
static inline void |
| 177 |
dbgui_level_meter(const char *id, float frac, ImVec2 size, ImU32 color) |
| 178 |
{ |
| 179 |
if (size.x <= 0.0f) { |
| 180 |
size.x = 80.0f; |
| 181 |
} |
| 182 |
if (size.y <= 0.0f) { |
| 183 |
size.y = ImGui::GetTextLineHeight(); |
| 184 |
} |
| 185 |
|
| 186 |
bool clipped = frac > 1.0f; |
| 187 |
if (clipped) { |
| 188 |
frac = 1.0f; |
| 189 |
} |
| 190 |
if (frac < 0.0f) { |
| 191 |
frac = 0.0f; |
| 192 |
} |
| 193 |
|
| 194 |
ImGui::PushID(id); |
| 195 |
ImGui::InvisibleButton("##meter", size); |
| 196 |
const ImVec2 p0 = ImGui::GetItemRectMin(); |
| 197 |
const ImVec2 p1 = ImGui::GetItemRectMax(); |
| 198 |
ImGui::PopID(); |
| 199 |
|
| 200 |
ImDrawList *dl = ImGui::GetWindowDrawList(); |
| 201 |
dl->AddRectFilled(p0, p1, IM_COL32(24, 26, 30, 255)); |
| 202 |
if (frac > 0.0f) { |
| 203 |
ImVec2 fill(p0.x + (p1.x - p0.x) * frac, p1.y); |
| 204 |
dl->AddRectFilled(p0, fill, clipped ? IM_COL32(220, 80, 70, 255) : color); |
| 205 |
} |
| 206 |
dl->AddRect(p0, p1, IM_COL32(70, 78, 90, 255)); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
struct DbgScopeControls { |
| 213 |
int window = 512; |
| 214 |
float gain = DBGUI_SCOPE_AUTO_GAIN; |
| 215 |
bool trigger = true; |
| 216 |
bool freeze = false; |
| 217 |
}; |
| 218 |
|
| 219 |
|
| 220 |
static inline void |
| 221 |
dbgui_scope_controls(DbgScopeControls *c, int max_window) |
| 222 |
{ |
| 223 |
ImGui::SetNextItemWidth(140.0f); |
| 224 |
ImGui::SliderInt("Window", &c->window, 32, max_window, "%d smp"); |
| 225 |
ImGui::SameLine(); |
| 226 |
|
| 227 |
bool autoscale = (c->gain <= 0.0f); |
| 228 |
if (ImGui::Checkbox("Auto gain", &autoscale)) { |
| 229 |
c->gain = autoscale ? DBGUI_SCOPE_AUTO_GAIN : 1.0f; |
| 230 |
} |
| 231 |
if (!autoscale) { |
| 232 |
ImGui::SameLine(); |
| 233 |
ImGui::SetNextItemWidth(120.0f); |
| 234 |
ImGui::SliderFloat("Gain", &c->gain, 0.05f, 16.0f, "%.2fx", |
| 235 |
ImGuiSliderFlags_Logarithmic); |
| 236 |
} |
| 237 |
ImGui::SameLine(); |
| 238 |
ImGui::Checkbox("Trigger", &c->trigger); |
| 239 |
ImGui::SetItemTooltip("Align the trace to a rising zero crossing so a steady " |
| 240 |
"tone stands still."); |
| 241 |
ImGui::SameLine(); |
| 242 |
ImGui::Checkbox("Freeze", &c->freeze); |
| 243 |
ImGui::SetItemTooltip("Stop refreshing the traces. Capture also stops, since " |
| 244 |
"it is only armed while the debugger reads it."); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
static inline bool |
| 257 |
dbgui_audio_hold(void) |
| 258 |
{ |
| 259 |
return DEBUGIsPaused() && debug_ui_settings().audio_hold_on_pause; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
static inline void |
| 265 |
dbgui_audio_hold_note(void) |
| 266 |
{ |
| 267 |
ImGui::SameLine(); |
| 268 |
ImGui::TextColored(ImVec4(0.95f, 0.70f, 0.30f, 1.0f), "held (paused)"); |
| 269 |
ImGui::SetItemTooltip("The machine is paused, so audio is silent and nothing new is\n" |
| 270 |
"captured. These are the values from the moment it stopped."); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
static inline void |
| 279 |
dbgui_audio_predicted_note(const char *what) |
| 280 |
{ |
| 281 |
ImGui::SameLine(); |
| 282 |
ImGui::TextColored(ImVec4(0.70f, 0.80f, 1.00f, 1.0f), "projected"); |
| 283 |
ImGui::SetItemTooltip("Paused with no captured audio, so these traces are computed from\n" |
| 284 |
"%s - what it would emit next, using the same\n" |
| 285 |
"renderer live audio uses.\n\n" |
| 286 |
"It is a projection, not a recording: change the chip state and it\n" |
| 287 |
"updates immediately. Real captured audio replaces it on resume.", |
| 288 |
what); |
| 289 |
} |
| 290 |
|
| 291 |
#endif |