Debian Codegraph @

src/peq-response.c

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2025 Geoffrey D. Bennett <[email protected]>
2 // SPDX-License-Identifier: GPL-3.0-or-later
3
4 #include <math.h>
5 #include <pango/pangocairo.h>
6 #include "peq-response.h"
7
8 // Preferred widget size
9 #define PREF_WIDTH 400
10 #define PREF_HEIGHT 300
11
12 // Margins (fixed in pixels)
13 #define LABEL_MARGIN_LEFT 28
14 #define LABEL_MARGIN_RIGHT 12
15 #define LABEL_MARGIN_BOTTOM 15
16 #define PADDING 3
17
18 #define FREQ_MIN 20.0
19 #define FREQ_MAX 20000.0
20 #define Q_MIN 0.1
21 #define Q_MAX 10.0
22
23 // Toggle hit region dimensions (pixels)
24 #define TOGGLE_HIT_WIDTH 30
25 #define TOGGLE_HIT_HEIGHT 16
26
27 #define SAMPLE_RATE 48000.0
28
29 // Band colors (up to 8)
30 static const double band_colors[][3] = {
31 { 0.4, 0.7, 0.9 }, // blue
32 { 0.4, 0.8, 0.4 }, // green
33 { 0.9, 0.9, 0.3 }, // yellow
34 { 0.9, 0.6, 0.3 }, // orange
35 { 0.9, 0.4, 0.4 }, // red
36 { 0.7, 0.4, 0.9 }, // purple
37 { 0.4, 0.9, 0.9 }, // cyan
38 { 0.9, 0.4, 0.7 }, // pink
39 };
40
41 // Graph area calculated from widget size
42 struct graph_area {
43 double left, right, top, bottom;
44 double width, height;
45 double db_min, db_max;
46 };
47
48 struct _GtkFilterResponse {
49 GtkWidget parent_instance;
50
51 int num_bands;
52 struct biquad_params bands[FILTER_RESPONSE_MAX_BANDS];
53 struct biquad_coeffs coeffs[FILTER_RESPONSE_MAX_BANDS];
54 gboolean band_enabled[FILTER_RESPONSE_MAX_BANDS];
55 gboolean dsp_enabled; // overall DSP enable state
56 double db_range; // DB_RANGE_NARROW or GAIN_DB_LIMIT
57 int highlight_band; // -1 for none
58 int internal_highlight; // -1 for none, set by hover
59 gboolean hover_toggle; // mouse over range toggle
60
61 // Drag state
62 int drag_band; // -1 for none
63 double drag_offset_x; // offset from handle center to click point
64 double drag_offset_y;
65 };
66
67 G_DEFINE_TYPE(GtkFilterResponse, gtk_filter_response, GTK_TYPE_WIDGET)
68
69 enum {
70 SIGNAL_FILTER_CHANGED,
71 SIGNAL_HIGHLIGHT_CHANGED,
72 N_SIGNALS
73 };
74
75 static guint signals[N_SIGNALS];
76
77 // Calculate graph area from widget dimensions
78 static void calc_graph_area(
79 int width, int height, double db_range, struct graph_area *g
80 ) {
81 g->left = LABEL_MARGIN_LEFT;
82 g->right = width - LABEL_MARGIN_RIGHT;
83 g->top = PADDING;
84 g->bottom = height - LABEL_MARGIN_BOTTOM;
85 g->width = g->right - g->left;
86 g->height = g->bottom - g->top;
87 g->db_min = -db_range;
88 g->db_max = db_range;
89 }
90
91 // Calculate graph area from response widget state
92 static void get_response_graph_area(
93 GtkFilterResponse *response, struct graph_area *g
94 ) {
95 calc_graph_area(
96 gtk_widget_get_width(GTK_WIDGET(response)),
97 gtk_widget_get_height(GTK_WIDGET(response)),
98 response->db_range,
99 g
100 );
101 }
102
103 // Convert frequency to X coordinate (log scale)
104 static double freq_to_x(const struct graph_area *g, double freq) {
105 double log_min = log10(FREQ_MIN);
106 double log_max = log10(FREQ_MAX);
107 double log_freq = log10(freq);
108 return g->left + (log_freq - log_min) / (log_max - log_min) * g->width;
109 }
110
111 // Convert dB to Y coordinate
112 static double db_to_y(const struct graph_area *g, double db) {
113 return g->bottom -
114 (db - g->db_min) / (g->db_max - g->db_min) * g->height;
115 }
116
117 // Convert X coordinate to frequency (log scale)
118 static double x_to_freq(const struct graph_area *g, double x) {
119 double log_min = log10(FREQ_MIN);
120 double log_max = log10(FREQ_MAX);
121 double t = (x - g->left) / g->width;
122 double log_freq = log_min + t * (log_max - log_min);
123 return pow(10.0, log_freq);
124 }
125
126 // Convert Y coordinate to dB
127 static double y_to_db(const struct graph_area *g, double y) {
128 double t = (g->bottom - y) / g->height;
129 return g->db_min + t * (g->db_max - g->db_min);
130 }
131
132 // Convert Q to Y coordinate (log scale, higher Q = higher Y)
133 static double q_to_y(const struct graph_area *g, double q) {
134 double log_min = log10(Q_MIN);
135 double log_max = log10(Q_MAX);
136 double log_q = log10(q);
137 double t = (log_q - log_min) / (log_max - log_min);
138 return g->bottom - t * g->height;
139 }
140
141 // Convert Y coordinate to Q (log scale, higher Y = higher Q)
142 static double y_to_q(const struct graph_area *g, double y) {
143 double t = (g->bottom - y) / g->height;
144 double log_min = log10(Q_MIN);
145 double log_max = log10(Q_MAX);
146 double log_q = log_min + t * (log_max - log_min);
147 return pow(10.0, log_q);
148 }
149
150 // Draw a single filter response curve with shading to 0 dB line
151 static void draw_filter_response(
152 cairo_t *cr,
153 const struct graph_area *g,
154 const struct biquad_coeffs *coeffs,
155 double r,
156 double gc,
157 double b,
158 double alpha,
159 gboolean dashed
160 ) {
161 cairo_save(cr);
162
163 double y0 = db_to_y(g, 0);
164 double x_start = freq_to_x(g, FREQ_MIN);
165 double x_end = freq_to_x(g, FREQ_MAX);
166
167 // Build closed path for fill
168 cairo_move_to(cr, x_start, y0);
169 for (double freq = FREQ_MIN; freq <= FREQ_MAX; freq *= 1.02) {
170 double db = biquad_response_db(coeffs, freq, SAMPLE_RATE);
171 double x = freq_to_x(g, freq);
172 double y = db_to_y(g, db);
173 cairo_line_to(cr, x, y);
174 }
175 cairo_line_to(cr, x_end, y0);
176 cairo_close_path(cr);
177
178 // Fill with translucent color
179 cairo_set_source_rgba(cr, r, gc, b, alpha * 0.3);
180 cairo_fill(cr);
181
182 // Build new path for stroke (curve only)
183 gboolean first = TRUE;
184 for (double freq = FREQ_MIN; freq <= FREQ_MAX; freq *= 1.02) {
185 double db = biquad_response_db(coeffs, freq, SAMPLE_RATE);
186 double x = freq_to_x(g, freq);
187 double y = db_to_y(g, db);
188 if (first) {
189 cairo_move_to(cr, x, y);
190 first = FALSE;
191 } else {
192 cairo_line_to(cr, x, y);
193 }
194 }
195
196 cairo_set_source_rgba(cr, r, gc, b, alpha);
197 cairo_set_line_width(cr, 1.5);
198
199 if (dashed) {
200 double dashes[] = { 4.0, 4.0 };
201 cairo_set_dash(cr, dashes, 2, 0);
202 }
203
204 cairo_stroke(cr);
205
206 cairo_restore(cr);
207 }
208
209 // Draw filter handle showing freq/gain and Q
210 static void draw_filter_handle(
211 cairo_t *cr,
212 const struct graph_area *ga,
213 const struct biquad_params *params,
214 const struct biquad_coeffs *coeffs,
215 int band_num,
216 double r,
217 double g,
218 double b,
219 double alpha,
220 gboolean highlighted,
221 gboolean enabled
222 ) {
223 cairo_save(cr);
224 cairo_new_path(cr);
225
226 double x = freq_to_x(ga, params->freq);
227 double y;
228
229 // Determine Y position based on filter type
230 if (biquad_type_uses_gain(params->type)) {
231 y = db_to_y(ga, params->gain_db);
232 } else {
233 // For LP/HP/BP/Notch, Y represents Q
234 y = q_to_y(ga, params->q);
235 }
236
237 double radius = highlighted ? 12 : 10;
238 double line_width = highlighted ? 2 : 1.5;
239
240 // Draw Q whiskers for filters with gain (except pure gain which has no Q)
241 if (biquad_type_uses_gain(params->type) &&
242 params->type != BIQUAD_TYPE_GAIN) {
243 // Calculate bandwidth as ratio for log scale symmetry
244 double ratio = pow(2.0, 0.5 / params->q);
245 double freq_lo = params->freq / ratio;
246 double freq_hi = params->freq * ratio;
247 if (freq_lo < FREQ_MIN) freq_lo = FREQ_MIN;
248 if (freq_hi > FREQ_MAX) freq_hi = FREQ_MAX;
249 double x_lo = freq_to_x(ga, freq_lo) - radius;
250 double x_hi = freq_to_x(ga, freq_hi) + radius;
251
252 cairo_set_source_rgba(cr, r, g, b, alpha * 0.8);
253 cairo_set_line_width(cr, line_width);
254 cairo_move_to(cr, x_lo, y);
255 cairo_line_to(cr, x_hi, y);
256 cairo_stroke(cr);
257
258 // Draw small vertical ticks at bandwidth edges
259 double tick_size = highlighted ? 4 : 3;
260 cairo_move_to(cr, x_lo, y - tick_size);
261 cairo_line_to(cr, x_lo, y + tick_size);
262 cairo_move_to(cr, x_hi, y - tick_size);
263 cairo_line_to(cr, x_hi, y + tick_size);
264 cairo_stroke(cr);
265 }
266
267 // Draw center circle (dark grey background)
268 cairo_arc(cr, x, y, radius, 0, 2 * M_PI);
269 cairo_set_source_rgba(cr, 0.2, 0.2, 0.2, alpha);
270 cairo_fill_preserve(cr);
271 cairo_set_source_rgba(cr, r, g, b, alpha);
272 cairo_set_line_width(cr, 1.5);
273 if (!enabled) {
274 double dashes[] = { 3.0, 3.0 };
275 cairo_set_dash(cr, dashes, 2, 0);
276 }
277 cairo_stroke(cr);
278 cairo_set_dash(cr, NULL, 0, 0);
279
280 // Draw band number in band color using Pango for proper centering
281 char label[12];
282 snprintf(label, sizeof(label), "%d", band_num);
283
284 PangoLayout *layout = pango_cairo_create_layout(cr);
285 PangoFontDescription *font_desc = pango_font_description_from_string(
286 highlighted ? "Sans Bold 10" : "Sans Bold 9"
287 );
288 pango_layout_set_font_description(layout, font_desc);
289 pango_layout_set_text(layout, label, -1);
290
291 int text_width, text_height;
292 pango_layout_get_pixel_size(layout, &text_width, &text_height);
293
294 cairo_set_source_rgba(cr, r, g, b, alpha);
295 cairo_move_to(cr, x - text_width / 2.0, y - text_height / 2.0);
296 pango_cairo_show_layout(cr, layout);
297
298 pango_font_description_free(font_desc);
299 g_object_unref(layout);
300
301 cairo_restore(cr);
302 }
303
304 // Find which band handle (if any) is at the given position
305 // Returns band index (0-based) or -1 if none
306 static int find_band_at_position(
307 GtkFilterResponse *response,
308 double mx,
309 double my
310 ) {
311 struct graph_area g;
312 get_response_graph_area(response, &g);
313
314 double hit_radius = 12; // slightly larger than visual radius for easier targeting
315 int closest_band = -1;
316 double closest_dist = hit_radius * hit_radius;
317
318 for (int i = 0; i < response->num_bands; i++) {
319 const struct biquad_params *params = &response->bands[i];
320
321 double x = freq_to_x(&g, params->freq);
322 double y;
323
324 if (biquad_type_uses_gain(params->type)) {
325 y = db_to_y(&g, params->gain_db);
326 } else {
327 y = q_to_y(&g, params->q);
328 }
329
330 double dx = mx - x;
331 double dy = my - y;
332 double dist_sq = dx * dx + dy * dy;
333
334 if (dist_sq < closest_dist) {
335 closest_dist = dist_sq;
336 closest_band = i;
337 }
338 }
339
340 return closest_band;
341 }
342
343 // Check if a point is in the range toggle region
344 static gboolean is_in_toggle_region(
345 const struct graph_area *g, double x, double y
346 ) {
347 return x >= g->right - TOGGLE_HIT_WIDTH &&
348 y <= g->top + TOGGLE_HIT_HEIGHT;
349 }
350
351 // Motion event callbacks
352 static void response_motion(
353 GtkEventControllerMotion *controller,
354 double x,
355 double y,
356 GtkFilterResponse *response
357 ) {
358 struct graph_area g;
359 get_response_graph_area(response, &g);
360
361 gboolean in_toggle = is_in_toggle_region(&g, x, y);
362 int band = find_band_at_position(response, x, y);
363
364 gboolean changed = FALSE;
365
366 if (in_toggle != response->hover_toggle) {
367 response->hover_toggle = in_toggle;
368 changed = TRUE;
369 }
370
371 if (band != response->internal_highlight) {
372 response->internal_highlight = band;
373 response->highlight_band = band;
374 g_signal_emit(
375 response, signals[SIGNAL_HIGHLIGHT_CHANGED], 0, band
376 );
377 changed = TRUE;
378 }
379
380 if (changed)
381 gtk_widget_queue_draw(GTK_WIDGET(response));
382 }
383
384 static void response_leave(
385 GtkEventControllerMotion *controller,
386 GtkFilterResponse *response
387 ) {
388 gboolean changed = response->hover_toggle;
389 response->hover_toggle = FALSE;
390
391 if (response->internal_highlight != -1) {
392 response->internal_highlight = -1;
393 response->highlight_band = -1;
394 g_signal_emit(
395 response, signals[SIGNAL_HIGHLIGHT_CHANGED], 0, -1
396 );
397 changed = TRUE;
398 }
399
400 if (changed)
401 gtk_widget_queue_draw(GTK_WIDGET(response));
402 }
403
404 double gtk_filter_response_get_db_range(GtkFilterResponse *response) {
405 return response->db_range;
406 }
407
408 void gtk_filter_response_set_db_range(
409 GtkFilterResponse *response, double new_range
410 ) {
411 response->db_range = new_range;
412
413 // Clamp existing band gains and notify
414 for (int i = 0; i < response->num_bands; i++) {
415 struct biquad_params *params = &response->bands[i];
416 if (params->gain_db < -new_range ||
417 params->gain_db > new_range) {
418 params->gain_db = CLAMP(params->gain_db,
419 -new_range, new_range);
420 biquad_calculate(
421 params, SAMPLE_RATE, &response->coeffs[i]
422 );
423 g_signal_emit(
424 response, signals[SIGNAL_FILTER_CHANGED], 0, i, params
425 );
426 }
427 }
428
429 gtk_widget_queue_draw(GTK_WIDGET(response));
430 }
431
432 void gtk_filter_response_auto_range(GtkFilterResponse *response) {
433 double range = DB_RANGE_NARROW;
434 for (int i = 0; i < response->num_bands; i++) {
435 if (response->bands[i].gain_db > DB_RANGE_NARROW ||
436 response->bands[i].gain_db < -DB_RANGE_NARROW) {
437 range = GAIN_DB_LIMIT;
438 break;
439 }
440 }
441 gtk_filter_response_set_db_range(response, range);
442 }
443
444 // Drag gesture callbacks
445 static void response_drag_begin(
446 GtkGestureDrag *gesture,
447 double start_x,
448 double start_y,
449 GtkFilterResponse *response
450 ) {
451 int band = find_band_at_position(response, start_x, start_y);
452
453 struct graph_area g;
454 get_response_graph_area(response, &g);
455
456 if (band >= 0) {
457 response->drag_band = band;
458 response->highlight_band = band;
459
460 // Calculate offset from handle center to click point
461 const struct biquad_params *params = &response->bands[band];
462 double handle_x = freq_to_x(&g, params->freq);
463 double handle_y;
464 if (biquad_type_uses_gain(params->type)) {
465 handle_y = db_to_y(&g, params->gain_db);
466 } else {
467 handle_y = q_to_y(&g, params->q);
468 }
469
470 response->drag_offset_x = start_x - handle_x;
471 response->drag_offset_y = start_y - handle_y;
472
473 gtk_gesture_set_state(
474 GTK_GESTURE(gesture), GTK_EVENT_SEQUENCE_CLAIMED
475 );
476 } else if (is_in_toggle_region(&g, start_x, start_y)) {
477 // Toggle range
478 double new_range =
479 response->db_range == GAIN_DB_LIMIT
480 ? DB_RANGE_NARROW : GAIN_DB_LIMIT;
481 gtk_filter_response_set_db_range(response, new_range);
482 gtk_gesture_set_state(
483 GTK_GESTURE(gesture), GTK_EVENT_SEQUENCE_CLAIMED
484 );
485 }
486 }
487
488 static void response_drag_update(
489 GtkGestureDrag *gesture,
490 double offset_x,
491 double offset_y,
492 GtkFilterResponse *response
493 ) {
494 if (response->drag_band < 0)
495 return;
496
497 double start_x, start_y;
498 gtk_gesture_drag_get_start_point(gesture, &start_x, &start_y);
499
500 // Calculate handle center position (subtract click offset from mouse position)
501 double x = start_x + offset_x - response->drag_offset_x;
502 double y = start_y + offset_y - response->drag_offset_y;
503
504 struct graph_area g;
505 get_response_graph_area(response, &g);
506
507 // Convert position to freq
508 double freq = x_to_freq(&g, x);
509 if (freq < FREQ_MIN) freq = FREQ_MIN;
510 if (freq > FREQ_MAX) freq = FREQ_MAX;
511
512 // Update the band
513 struct biquad_params *params = &response->bands[response->drag_band];
514 params->freq = freq;
515
516 if (biquad_type_uses_gain(params->type)) {
517 // Y adjusts gain
518 params->gain_db = CLAMP(
519 y_to_db(&g, y), g.db_min, g.db_max
520 );
521 } else {
522 // Y adjusts Q (higher = narrower)
523 double q = y_to_q(&g, y);
524 if (q < Q_MIN) q = Q_MIN;
525 if (q > Q_MAX) q = Q_MAX;
526 params->q = q;
527 }
528
529 // Recalculate coefficients
530 biquad_calculate(params, SAMPLE_RATE, &response->coeffs[response->drag_band]);
531
532 // Emit signal to notify external code
533 g_signal_emit(response, signals[SIGNAL_FILTER_CHANGED], 0,
534 response->drag_band, params);
535
536 gtk_widget_queue_draw(GTK_WIDGET(response));
537 }
538
539 static void response_drag_end(
540 GtkGestureDrag *gesture,
541 double offset_x,
542 double offset_y,
543 GtkFilterResponse *response
544 ) {
545 response->drag_band = -1;
546 }
547
548 // Scroll callback to adjust Q for filters with gain
549 static gboolean response_scroll(
550 GtkEventControllerScroll *controller,
551 double dx,
552 double dy,
553 GtkFilterResponse *response
554 ) {
555 // Only adjust if hovering over a band
556 if (response->internal_highlight < 0)
557 return FALSE;
558
559 int band = response->internal_highlight;
560 struct biquad_params *params = &response->bands[band];
561
562 // Only adjust Q for filters with gain
563 if (!biquad_type_uses_gain(params->type))
564 return FALSE;
565
566 // Adjust Q logarithmically (scroll up = higher Q = narrower)
567 double factor = (dy > 0) ? 0.9 : 1.1;
568 double new_q = params->q * factor;
569
570 // Clamp Q
571 if (new_q < Q_MIN) new_q = Q_MIN;
572 if (new_q > Q_MAX) new_q = Q_MAX;
573
574 params->q = new_q;
575
576 // Recalculate coefficients
577 biquad_calculate(params, SAMPLE_RATE, &response->coeffs[band]);
578
579 // Emit signal to notify external code
580 g_signal_emit(response, signals[SIGNAL_FILTER_CHANGED], 0, band, params);
581
582 gtk_widget_queue_draw(GTK_WIDGET(response));
583
584 return TRUE;
585 }
586
587 // Calculate combined response at a frequency (includes all enabled bands)
588 static double combined_response_db(GtkFilterResponse *response, double freq) {
589 double total_db = 0.0;
590
591 for (int i = 0; i < response->num_bands; i++) {
592 if (response->band_enabled[i])
593 total_db += biquad_response_db(&response->coeffs[i], freq, SAMPLE_RATE);
594 }
595
596 return total_db;
597 }
598
599 static void response_snapshot(GtkWidget *widget, GtkSnapshot *snapshot) {
600 GtkFilterResponse *response = GTK_FILTER_RESPONSE(widget);
601
602 int width = gtk_widget_get_width(widget);
603 int height = gtk_widget_get_height(widget);
604
605 struct graph_area g;
606 calc_graph_area(width, height, response->db_range, &g);
607
608 cairo_t *cr = gtk_snapshot_append_cairo(
609 snapshot,
610 &GRAPHENE_RECT_INIT(0, 0, width, height)
611 );
612
613 // Background
614 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
615 cairo_rectangle(cr, g.left, g.top, g.width, g.height);
616 cairo_fill(cr);
617
618 // Grid lines - horizontal (dB)
619 cairo_set_source_rgba(cr, 1, 1, 1, 0.15);
620 cairo_set_line_width(cr, 0.5);
621 for (double db = g.db_min; db <= g.db_max; db += 6) {
622 double y = db_to_y(&g, db);
623 cairo_move_to(cr, g.left, y);
624 cairo_line_to(cr, g.right, y);
625 }
626 cairo_stroke(cr);
627
628 // Grid lines - vertical (frequency)
629 double freqs[] = { 20, 30, 40, 50, 60, 70, 80, 90,
630 100, 200, 300, 400, 500, 600, 700, 800, 900,
631 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000,
632 10000, 20000 };
633 for (int i = 0; i < 28; i++) {
634 double x = freq_to_x(&g, freqs[i]);
635 cairo_move_to(cr, x, g.top);
636 cairo_line_to(cr, x, g.bottom);
637 }
638 cairo_stroke(cr);
639
640 // 0dB reference line
641 cairo_set_source_rgba(cr, 1, 1, 1, 0.3);
642 cairo_set_line_width(cr, 1);
643 double y0 = db_to_y(&g, 0);
644 cairo_move_to(cr, g.left, y0);
645 cairo_line_to(cr, g.right, y0);
646 cairo_stroke(cr);
647
648 // Axis labels
649 cairo_set_source_rgba(cr, 1, 1, 1, 0.6);
650 cairo_select_font_face(
651 cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL
652 );
653 cairo_set_font_size(cr, 8);
654
655 // Y-axis labels (dB)
656 for (double db = g.db_min; db <= g.db_max; db += 6) {
657 char label[8];
658 snprintf(label, sizeof(label), "%+.0f", db);
659 cairo_text_extents_t extents;
660 cairo_text_extents(cr, label, &extents);
661 double y = db_to_y(&g, db);
662 cairo_move_to(cr, g.left - extents.width - 3, y + extents.height / 2);
663 cairo_show_text(cr, label);
664 }
665
666 // X-axis labels (frequency)
667 const char *freq_labels[] = { "20", "50", "100", "200", "500", "1k", "2k", "5k", "10k", "20k" };
668 double freq_values[] = { 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000 };
669 for (int i = 0; i < 10; i++) {
670 cairo_text_extents_t extents;
671 cairo_text_extents(cr, freq_labels[i], &extents);
672 double x = freq_to_x(&g, freq_values[i]);
673 cairo_move_to(cr, x - extents.width / 2, g.bottom + extents.height + 3);
674 cairo_show_text(cr, freq_labels[i]);
675 }
676
677 // Clip to graph area for curves
678 cairo_save(cr);
679 cairo_rectangle(cr, g.left, g.top, g.width, g.height);
680 cairo_clip(cr);
681
682 // Draw individual filter curves (skip highlighted band for now)
683 // When DSP is disabled, all curves are muted
684 for (int i = 0; i < response->num_bands; i++) {
685 if (i == response->highlight_band)
686 continue;
687 int color_idx = i % 8;
688 gboolean band_on = response->band_enabled[i] && response->dsp_enabled;
689 draw_filter_response(
690 cr, &g, &response->coeffs[i],
691 band_colors[color_idx][0],
692 band_colors[color_idx][1],
693 band_colors[color_idx][2],
694 band_on ? 0.5 : 0.3,
695 !band_on // dashed if band or DSP disabled
696 );
697 }
698
699 // Draw combined response curve
700 // White solid if enabled, grey dashed if DSP disabled
701 cairo_save(cr);
702 cairo_set_line_width(cr, 2);
703 if (response->dsp_enabled) {
704 cairo_set_source_rgb(cr, 1, 1, 1);
705 } else {
706 cairo_set_source_rgb(cr, 0.6, 0.6, 0.6);
707 double dashes[] = { 6.0, 4.0 };
708 cairo_set_dash(cr, dashes, 2, 0);
709 }
710
711 gboolean first = TRUE;
712 for (double freq = FREQ_MIN; freq <= FREQ_MAX; freq *= 1.02) {
713 double db = combined_response_db(response, freq);
714 double x = freq_to_x(&g, freq);
715 double y = db_to_y(&g, db);
716
717 if (first) {
718 cairo_move_to(cr, x, y);
719 first = FALSE;
720 } else {
721 cairo_line_to(cr, x, y);
722 }
723 }
724 cairo_stroke(cr);
725 cairo_restore(cr);
726
727 // Draw highlighted band last (on top of everything) with full opacity
728 if (response->highlight_band >= 0 &&
729 response->highlight_band < response->num_bands) {
730 int i = response->highlight_band;
731 int color_idx = i % 8;
732 gboolean band_on = response->band_enabled[i] && response->dsp_enabled;
733 draw_filter_response(
734 cr, &g, &response->coeffs[i],
735 band_colors[color_idx][0],
736 band_colors[color_idx][1],
737 band_colors[color_idx][2],
738 band_on ? 1.0 : 0.6,
739 !band_on
740 );
741 }
742
743 // Draw filter handles (freq/gain bubbles with Q whiskers)
744 if (response->highlight_band >= 0) {
745 // When highlighted, draw non-highlighted first, then highlighted on top
746 for (int i = 0; i < response->num_bands; i++) {
747 if (i == response->highlight_band)
748 continue;
749 int color_idx = i % 8;
750 gboolean band_on = response->band_enabled[i] && response->dsp_enabled;
751 double alpha = band_on ? 0.7 : 0.4;
752 draw_filter_handle(
753 cr, &g, &response->bands[i], &response->coeffs[i],
754 i + 1,
755 band_colors[color_idx][0],
756 band_colors[color_idx][1],
757 band_colors[color_idx][2],
758 alpha, FALSE, band_on
759 );
760 }
761 // Highlighted handle last
762 if (response->highlight_band < response->num_bands) {
763 int i = response->highlight_band;
764 int color_idx = i % 8;
765 gboolean band_on = response->band_enabled[i] && response->dsp_enabled;
766 double alpha = band_on ? 1.0 : 0.6;
767 draw_filter_handle(
768 cr, &g, &response->bands[i], &response->coeffs[i],
769 i + 1,
770 band_colors[color_idx][0],
771 band_colors[color_idx][1],
772 band_colors[color_idx][2],
773 alpha, TRUE, band_on
774 );
775 }
776 } else {
777 // No highlight: draw in reverse order so 1 is on top
778 for (int i = response->num_bands - 1; i >= 0; i--) {
779 int color_idx = i % 8;
780 gboolean band_on = response->band_enabled[i] && response->dsp_enabled;
781 double alpha = band_on ? 0.7 : 0.4;
782 draw_filter_handle(
783 cr, &g, &response->bands[i], &response->coeffs[i],
784 i + 1,
785 band_colors[color_idx][0],
786 band_colors[color_idx][1],
787 band_colors[color_idx][2],
788 alpha, FALSE, band_on
789 );
790 }
791 }
792
793 cairo_restore(cr);
794
795 // Range toggle indicator (top-right corner)
796 double toggle_alpha = response->hover_toggle ? 0.9 : 0.4;
797 cairo_set_source_rgba(cr, 1, 1, 1, toggle_alpha);
798 cairo_set_font_size(cr, 9);
799 char range_label[8];
800 snprintf(range_label, sizeof(range_label),
801 "\xc2\xb1%d", (int)response->db_range);
802 cairo_text_extents_t rext;
803 cairo_text_extents(cr, range_label, &rext);
804 double rx = g.right - rext.width - 4;
805 double ry = g.top + rext.height + 4;
806 cairo_move_to(cr, rx, ry);
807 cairo_show_text(cr, range_label);
808
809 cairo_destroy(cr);
810 }
811
812 static void response_measure(
813 GtkWidget *widget,
814 GtkOrientation orientation,
815 int for_size,
816 int *minimum,
817 int *natural,
818 int *minimum_baseline,
819 int *natural_baseline
820 ) {
821 if (orientation == GTK_ORIENTATION_HORIZONTAL) {
822 *minimum = PREF_WIDTH / 2;
823 *natural = PREF_WIDTH;
824 } else {
825 *minimum = PREF_HEIGHT / 2;
826 *natural = PREF_HEIGHT;
827 }
828 *minimum_baseline = -1;
829 *natural_baseline = -1;
830 }
831
832 static void gtk_filter_response_class_init(GtkFilterResponseClass *klass) {
833 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
834
835 widget_class->snapshot = response_snapshot;
836 widget_class->measure = response_measure;
837
838 // Signal emitted when a filter is changed by dragging
839 signals[SIGNAL_FILTER_CHANGED] = g_signal_new(
840 "filter-changed",
841 G_TYPE_FROM_CLASS(klass),
842 G_SIGNAL_RUN_LAST,
843 0,
844 NULL, NULL,
845 NULL,
846 G_TYPE_NONE,
847 2,
848 G_TYPE_INT, // band index
849 G_TYPE_POINTER // biquad_params pointer
850 );
851
852 // Signal emitted when highlight changes (from hover in response widget)
853 signals[SIGNAL_HIGHLIGHT_CHANGED] = g_signal_new(
854 "highlight-changed",
855 G_TYPE_FROM_CLASS(klass),
856 G_SIGNAL_RUN_LAST,
857 0,
858 NULL, NULL,
859 NULL,
860 G_TYPE_NONE,
861 1,
862 G_TYPE_INT // band index (-1 for none)
863 );
864 }
865
866 static void gtk_filter_response_init(GtkFilterResponse *response) {
867 response->num_bands = 0;
868 response->dsp_enabled = TRUE;
869 response->db_range = GAIN_DB_LIMIT;
870 response->highlight_band = -1;
871 response->internal_highlight = -1;
872 response->drag_band = -1;
873
874 for (int i = 0; i < FILTER_RESPONSE_MAX_BANDS; i++) {
875 response->band_enabled[i] = TRUE;
876 response->bands[i].type = BIQUAD_TYPE_PEAKING;
877 response->bands[i].freq = 1000.0;
878 response->bands[i].q = 1.0;
879 response->bands[i].gain_db = 0.0;
880 }
881
882 // Add motion controller for hover detection
883 GtkEventController *motion = gtk_event_controller_motion_new();
884 g_signal_connect(motion, "motion", G_CALLBACK(response_motion), response);
885 g_signal_connect(motion, "leave", G_CALLBACK(response_leave), response);
886 gtk_widget_add_controller(GTK_WIDGET(response), motion);
887
888 // Add drag gesture for handle dragging
889 GtkGesture *drag = gtk_gesture_drag_new();
890 g_signal_connect(drag, "drag-begin", G_CALLBACK(response_drag_begin), response);
891 g_signal_connect(drag, "drag-update", G_CALLBACK(response_drag_update), response);
892 g_signal_connect(drag, "drag-end", G_CALLBACK(response_drag_end), response);
893 gtk_widget_add_controller(GTK_WIDGET(response), GTK_EVENT_CONTROLLER(drag));
894
895 // Add scroll controller for Q adjustment
896 GtkEventController *scroll = gtk_event_controller_scroll_new(
897 GTK_EVENT_CONTROLLER_SCROLL_VERTICAL
898 );
899 g_signal_connect(scroll, "scroll", G_CALLBACK(response_scroll), response);
900 gtk_widget_add_controller(GTK_WIDGET(response), scroll);
901 }
902
903 GtkWidget *gtk_filter_response_new(int num_bands) {
904 GtkFilterResponse *response = g_object_new(GTK_TYPE_FILTER_RESPONSE, NULL);
905
906 if (num_bands > FILTER_RESPONSE_MAX_BANDS)
907 num_bands = FILTER_RESPONSE_MAX_BANDS;
908 if (num_bands < 0)
909 num_bands = 0;
910
911 response->num_bands = num_bands;
912
913 // Initialize coefficients for all bands
914 for (int i = 0; i < num_bands; i++) {
915 biquad_calculate(
916 &response->bands[i], SAMPLE_RATE, &response->coeffs[i]
917 );
918 }
919
920 return GTK_WIDGET(response);
921 }
922
923 void gtk_filter_response_set_filter(
924 GtkFilterResponse *response,
925 int band,
926 const struct biquad_params *params
927 ) {
928 if (band < 0 || band >= response->num_bands)
929 return;
930
931 response->bands[band] = *params;
932 biquad_calculate(params, SAMPLE_RATE, &response->coeffs[band]);
933 gtk_widget_queue_draw(GTK_WIDGET(response));
934 }
935
936 void gtk_filter_response_set_band_enabled(
937 GtkFilterResponse *response,
938 int band,
939 gboolean enabled
940 ) {
941 if (band < 0 || band >= response->num_bands)
942 return;
943
944 if (response->band_enabled[band] != enabled) {
945 response->band_enabled[band] = enabled;
946 gtk_widget_queue_draw(GTK_WIDGET(response));
947 }
948 }
949
950 void gtk_filter_response_set_dsp_enabled(
951 GtkFilterResponse *response,
952 gboolean enabled
953 ) {
954 if (response->dsp_enabled != enabled) {
955 response->dsp_enabled = enabled;
956 gtk_widget_queue_draw(GTK_WIDGET(response));
957 }
958 }
959
960 void gtk_filter_response_set_highlight(
961 GtkFilterResponse *response,
962 int band
963 ) {
964 if (response->highlight_band != band) {
965 response->highlight_band = band;
966 gtk_widget_queue_draw(GTK_WIDGET(response));
967 }
968 }