| 1 |
|
| 2 |
|
| 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; |
| 18 |
int ratio; |
| 19 |
int knee_width; |
| 20 |
int makeup_gain; |
| 21 |
gboolean dsp_enabled; |
| 22 |
double input_level_db; |
| 23 |
double output_level_db; |
| 24 |
}; |
| 25 |
|
| 26 |
G_DEFINE_TYPE(GtkCompressorCurve, gtk_compressor_curve, GTK_TYPE_WIDGET) |
| 27 |
|
| 28 |
|
| 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 |
|
| 39 |
if (input < threshold) |
| 40 |
output = input; |
| 41 |
else |
| 42 |
output = threshold + (input - threshold) / ratio; |
| 43 |
} else if (input < threshold - knee / 2) { |
| 44 |
|
| 45 |
output = input; |
| 46 |
} else if (input > threshold + knee / 2) { |
| 47 |
|
| 48 |
output = threshold + (input - threshold) / ratio; |
| 49 |
} else { |
| 50 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 105 |
double x = db_to_x(db); |
| 106 |
cairo_move_to(cr, x, GRAPH_TOP); |
| 107 |
cairo_line_to(cr, x, GRAPH_BOTTOM); |
| 108 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 160 |
|
| 161 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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); |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 285 |
curve->threshold = -22; |
| 286 |
curve->ratio = 8; |
| 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 |
} |