Debian Codegraph @

src/compressor-curve.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 <stdio.h>
6 #include "compressor-curve.h"
7
8 #define CURVE_SIZE 150
9 #define LABEL_MARGIN 18
10 #define CURVE_PADDING 3
11 #define DB_MIN -60.0
12 #define DB_MAX 0.0
13
14 struct _GtkCompressorCurve {
15 GtkWidget parent_instance;
16
17 int threshold; // -40 to 0 dB
18 int ratio; // 2-100 (divide by 2 for actual ratio)
19 int knee_width; // 0-10 dB
20 int makeup_gain; // 0-24 dB
21 gboolean dsp_enabled; // overall DSP enable
22 double input_level_db; // current input level for dot display
23 double output_level_db; // current output level for dot display
24 };
25
26 G_DEFINE_TYPE(GtkCompressorCurve, gtk_compressor_curve, GTK_TYPE_WIDGET)
27
28 // Calculate compressor output for a given input (both in dB)
29 static double calculate_output(GtkCompressorCurve *curve, double input) {
30 double threshold = curve->threshold;
31 double ratio = curve->ratio / 2.0;
32 double knee = curve->knee_width;
33 double makeup = curve->makeup_gain;
34
35 double output;
36
37 if (knee <= 0) {
38 // Hard knee: no soft transition
39 if (input < threshold)
40 output = input;
41 else
42 output = threshold + (input - threshold) / ratio;
43 } else if (input < threshold - knee / 2) {
44 // Below knee region: unity gain
45 output = input;
46 } else if (input > threshold + knee / 2) {
47 // Above knee region: full compression
48 output = threshold + (input - threshold) / ratio;
49 } else {
50 // In knee region: smooth transition
51 double x = input - threshold + knee / 2;
52 double compression = (1.0 - 1.0 / ratio) * x * x / (2.0 * knee);
53 output = input - compression;
54 }
55
56 return output + makeup;
57 }
58
59 // Graph area boundaries (accounting for label margin)
60 #define GRAPH_LEFT LABEL_MARGIN
61 #define GRAPH_RIGHT (CURVE_SIZE - CURVE_PADDING)
62 #define GRAPH_TOP CURVE_PADDING
63 #define GRAPH_BOTTOM (CURVE_SIZE - LABEL_MARGIN)
64 #define GRAPH_WIDTH (GRAPH_RIGHT - GRAPH_LEFT)
65 #define GRAPH_HEIGHT (GRAPH_BOTTOM - GRAPH_TOP)
66
67 // Convert dB value to pixel coordinate
68 static double db_to_x(double db) {
69 return GRAPH_LEFT + (db - DB_MIN) / (DB_MAX - DB_MIN) * GRAPH_WIDTH;
70 }
71
72 static double db_to_y(double db) {
73 return GRAPH_BOTTOM - (db - DB_MIN) / (DB_MAX - DB_MIN) * GRAPH_HEIGHT;
74 }
75
76 static void curve_snapshot(GtkWidget *widget, GtkSnapshot *snapshot) {
77 GtkCompressorCurve *curve = GTK_COMPRESSOR_CURVE(widget);
78
79 int width = gtk_widget_get_width(widget);
80 int height = gtk_widget_get_height(widget);
81 int size = MIN(width, height);
82
83 cairo_t *cr = gtk_snapshot_append_cairo(
84 snapshot,
85 &GRAPHENE_RECT_INIT(0, 0, width, height)
86 );
87
88 // Center the square drawing area
89 double offset_x = (width - size) / 2.0;
90 double offset_y = (height - size) / 2.0;
91 cairo_translate(cr, offset_x, offset_y);
92 double scale = size / (double)CURVE_SIZE;
93 cairo_scale(cr, scale, scale);
94
95 // Background (graph area only)
96 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
97 cairo_rectangle(cr, GRAPH_LEFT, GRAPH_TOP, GRAPH_WIDTH, GRAPH_HEIGHT);
98 cairo_fill(cr);
99
100 // Grid lines (every 20 dB)
101 cairo_set_source_rgba(cr, 1, 1, 1, 0.15);
102 cairo_set_line_width(cr, 0.5);
103 for (double db = -40; db <= 0; db += 20) {
104 // Vertical
105 double x = db_to_x(db);
106 cairo_move_to(cr, x, GRAPH_TOP);
107 cairo_line_to(cr, x, GRAPH_BOTTOM);
108 // Horizontal
109 double y = db_to_y(db);
110 cairo_move_to(cr, GRAPH_LEFT, y);
111 cairo_line_to(cr, GRAPH_RIGHT, y);
112 }
113 cairo_stroke(cr);
114
115 // Axis labels
116 cairo_set_source_rgba(cr, 1, 1, 1, 0.6);
117 cairo_select_font_face(cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL,
118 CAIRO_FONT_WEIGHT_NORMAL);
119 cairo_set_font_size(cr, 8);
120
121 for (double db = -60; db <= 0; db += 20) {
122 char label[8];
123 snprintf(label, sizeof(label), "%.0f", db);
124
125 cairo_text_extents_t extents;
126 cairo_text_extents(cr, label, &extents);
127
128 // Y-axis labels (left side)
129 double y = db_to_y(db);
130 cairo_move_to(cr, GRAPH_LEFT - extents.width - 3,
131 y + extents.height / 2);
132 cairo_show_text(cr, label);
133
134 // X-axis labels (bottom)
135 double x = db_to_x(db);
136 cairo_move_to(cr, x - extents.width / 2,
137 GRAPH_BOTTOM + extents.height + 3);
138 cairo_show_text(cr, label);
139 }
140
141 // 1:1 reference line (dotted)
142 cairo_set_source_rgba(cr, 1, 1, 1, 0.25);
143 cairo_set_line_width(cr, 1);
144 double dashes[] = {3.0, 3.0};
145 cairo_set_dash(cr, dashes, 2, 0);
146 cairo_move_to(cr, db_to_x(DB_MIN), db_to_y(DB_MIN));
147 cairo_line_to(cr, db_to_x(DB_MAX), db_to_y(DB_MAX));
148 cairo_stroke(cr);
149 cairo_set_dash(cr, NULL, 0, 0);
150
151 // Threshold indicator (vertical line)
152 cairo_set_source_rgba(cr, 1, 0.8, 0.2, 0.4);
153 cairo_set_line_width(cr, 1);
154 double thresh_x = db_to_x(curve->threshold);
155 cairo_move_to(cr, thresh_x, GRAPH_TOP);
156 cairo_line_to(cr, thresh_x, GRAPH_BOTTOM);
157 cairo_stroke(cr);
158
159 // Transfer curve
160 // When enabled: white for normal, red for clipped
161 // When disabled: grey dashed
162 cairo_set_line_width(cr, 2);
163
164 gboolean is_enabled = curve->dsp_enabled;
165
166 if (!is_enabled) {
167 double dashes[] = { 6.0, 4.0 };
168 cairo_set_dash(cr, dashes, 2, 0);
169 }
170
171 // First pass: white/grey portion (output <= 0dB)
172 if (is_enabled)
173 cairo_set_source_rgb(cr, 1, 1, 1);
174 else
175 cairo_set_source_rgb(cr, 0.6, 0.6, 0.6);
176
177 gboolean first = TRUE;
178 for (double in_db = DB_MIN; in_db <= DB_MAX; in_db += 0.5) {
179 double out_db = calculate_output(curve, in_db);
180
181 if (out_db > DB_MAX) {
182 // End segment here
183 if (!first)
184 cairo_stroke(cr);
185 first = TRUE;
186 continue;
187 }
188
189 if (out_db < DB_MIN)
190 out_db = DB_MIN;
191
192 double x = db_to_x(in_db);
193 double y = db_to_y(out_db);
194
195 if (first) {
196 cairo_move_to(cr, x, y);
197 first = FALSE;
198 } else {
199 cairo_line_to(cr, x, y);
200 }
201 }
202 if (!first)
203 cairo_stroke(cr);
204
205 // Second pass: red/grey portion (output > 0dB, clamped to 0dB line)
206 if (is_enabled)
207 cairo_set_source_rgb(cr, 1, 0.3, 0.3);
208 else
209 cairo_set_source_rgb(cr, 0.6, 0.6, 0.6);
210
211 first = TRUE;
212 for (double in_db = DB_MIN; in_db <= DB_MAX; in_db += 0.5) {
213 double out_db = calculate_output(curve, in_db);
214
215 if (out_db <= DB_MAX) {
216 // End segment here
217 if (!first)
218 cairo_stroke(cr);
219 first = TRUE;
220 continue;
221 }
222
223 double x = db_to_x(in_db);
224 double y = db_to_y(DB_MAX); // Clamp to 0dB
225
226 if (first) {
227 cairo_move_to(cr, x, y);
228 first = FALSE;
229 } else {
230 cairo_line_to(cr, x, y);
231 }
232 }
233 if (!first)
234 cairo_stroke(cr);
235
236 // Draw level indicator dot if we have valid levels
237 if (is_enabled &&
238 curve->input_level_db > DB_MIN && curve->output_level_db > DB_MIN) {
239 double in_db = curve->input_level_db;
240 double out_db = curve->output_level_db;
241
242 // Clamp to graph bounds
243 if (in_db > DB_MAX) in_db = DB_MAX;
244 if (out_db > DB_MAX) out_db = DB_MAX;
245
246 double x = db_to_x(in_db);
247 double y = db_to_y(out_db);
248
249 // Draw filled circle - red if clipping, cyan otherwise
250 if (curve->input_level_db >= DB_MAX || curve->output_level_db >= DB_MAX)
251 cairo_set_source_rgb(cr, 1.0, 0.3, 0.3);
252 else
253 cairo_set_source_rgb(cr, 0.2, 0.8, 1.0);
254 cairo_arc(cr, x, y, 4, 0, 2 * M_PI);
255 cairo_fill(cr);
256 }
257
258 cairo_destroy(cr);
259 }
260
261 static void curve_measure(
262 GtkWidget *widget,
263 GtkOrientation orientation,
264 int for_size,
265 int *minimum,
266 int *natural,
267 int *minimum_baseline,
268 int *natural_baseline
269 ) {
270 *minimum = CURVE_SIZE;
271 *natural = CURVE_SIZE;
272 *minimum_baseline = -1;
273 *natural_baseline = -1;
274 }
275
276 static void gtk_compressor_curve_class_init(GtkCompressorCurveClass *klass) {
277 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
278
279 widget_class->snapshot = curve_snapshot;
280 widget_class->measure = curve_measure;
281 }
282
283 static void gtk_compressor_curve_init(GtkCompressorCurve *curve) {
284 // Default values
285 curve->threshold = -22;
286 curve->ratio = 8; // 4:1
287 curve->knee_width = 3;
288 curve->makeup_gain = 5;
289 curve->dsp_enabled = TRUE;
290 curve->input_level_db = -80.0;
291 curve->output_level_db = -80.0;
292 }
293
294 GtkWidget *gtk_compressor_curve_new(void) {
295 return g_object_new(GTK_TYPE_COMPRESSOR_CURVE, NULL);
296 }
297
298 void gtk_compressor_curve_set_threshold(GtkCompressorCurve *curve, int threshold) {
299 if (curve->threshold != threshold) {
300 curve->threshold = threshold;
301 gtk_widget_queue_draw(GTK_WIDGET(curve));
302 }
303 }
304
305 void gtk_compressor_curve_set_ratio(GtkCompressorCurve *curve, int ratio) {
306 if (curve->ratio != ratio) {
307 curve->ratio = ratio;
308 gtk_widget_queue_draw(GTK_WIDGET(curve));
309 }
310 }
311
312 void gtk_compressor_curve_set_knee_width(GtkCompressorCurve *curve, int knee_width) {
313 if (curve->knee_width != knee_width) {
314 curve->knee_width = knee_width;
315 gtk_widget_queue_draw(GTK_WIDGET(curve));
316 }
317 }
318
319 void gtk_compressor_curve_set_makeup_gain(GtkCompressorCurve *curve, int makeup_gain) {
320 if (curve->makeup_gain != makeup_gain) {
321 curve->makeup_gain = makeup_gain;
322 gtk_widget_queue_draw(GTK_WIDGET(curve));
323 }
324 }
325
326 void gtk_compressor_curve_set_dsp_enabled(GtkCompressorCurve *curve, gboolean enabled) {
327 if (curve->dsp_enabled != enabled) {
328 curve->dsp_enabled = enabled;
329 gtk_widget_queue_draw(GTK_WIDGET(curve));
330 }
331 }
332
333 void gtk_compressor_curve_set_levels(
334 GtkCompressorCurve *curve,
335 double input_db,
336 double output_db
337 ) {
338 curve->input_level_db = input_db;
339 curve->output_level_db = output_db;
340 gtk_widget_queue_draw(GTK_WIDGET(curve));
341 }