2591
|
1 /*
|
|
2 * Audacious - a cross-platform multimedia player
|
|
3 * Copyright (c) 2007 Tomasz Moń
|
|
4 *
|
|
5 * Based on:
|
|
6 * BMP - Cross-platform multimedia player
|
|
7 * Copyright (C) 2003-2004 BMP development team.
|
|
8 * XMMS:
|
|
9 * Copyright (C) 1998-2003 XMMS development team.
|
|
10 *
|
|
11 * This program is free software; you can redistribute it and/or modify
|
|
12 * it under the terms of the GNU General Public License as published by
|
|
13 * the Free Software Foundation; under version 3 of the License.
|
|
14 *
|
|
15 * This program is distributed in the hope that it will be useful,
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 * GNU General Public License for more details.
|
|
19 *
|
|
20 * You should have received a copy of the GNU General Public License
|
|
21 * along with this program; If not, see <http://www.gnu.org/licenses>.
|
|
22 */
|
|
23
|
|
24 #include "ui_skin.h"
|
|
25 #include "ui_skinned_equalizer_graph.h"
|
|
26 #include "skins_cfg.h"
|
|
27 #include <audacious/plugin.h>
|
|
28
|
|
29 #define UI_TYPE_SKINNED_EQUALIZER_GRAPH (ui_skinned_equalizer_graph_get_type())
|
|
30
|
|
31 enum {
|
|
32 DOUBLED,
|
|
33 LAST_SIGNAL
|
|
34 };
|
|
35
|
|
36 static void ui_skinned_equalizer_graph_class_init (UiSkinnedEqualizerGraphClass *klass);
|
|
37 static void ui_skinned_equalizer_graph_init (UiSkinnedEqualizerGraph *equalizer_graph);
|
|
38 static void ui_skinned_equalizer_graph_destroy (GtkObject *object);
|
|
39 static void ui_skinned_equalizer_graph_realize (GtkWidget *widget);
|
|
40 static void ui_skinned_equalizer_graph_size_request (GtkWidget *widget, GtkRequisition *requisition);
|
|
41 static void ui_skinned_equalizer_graph_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
|
|
42 static gboolean ui_skinned_equalizer_graph_expose (GtkWidget *widget, GdkEventExpose *event);
|
|
43 static void ui_skinned_equalizer_graph_toggle_scaled (UiSkinnedEqualizerGraph *equalizer_graph);
|
|
44
|
|
45 static GtkWidgetClass *parent_class = NULL;
|
|
46 static guint equalizer_graph_signals[LAST_SIGNAL] = { 0 };
|
|
47
|
|
48 GType ui_skinned_equalizer_graph_get_type() {
|
|
49 static GType equalizer_graph_type = 0;
|
|
50 if (!equalizer_graph_type) {
|
|
51 static const GTypeInfo equalizer_graph_info = {
|
|
52 sizeof (UiSkinnedEqualizerGraphClass),
|
|
53 NULL,
|
|
54 NULL,
|
|
55 (GClassInitFunc) ui_skinned_equalizer_graph_class_init,
|
|
56 NULL,
|
|
57 NULL,
|
|
58 sizeof (UiSkinnedEqualizerGraph),
|
|
59 0,
|
|
60 (GInstanceInitFunc) ui_skinned_equalizer_graph_init,
|
|
61 };
|
|
62 equalizer_graph_type = g_type_register_static (GTK_TYPE_WIDGET, "UiSkinnedEqualizerGraph", &equalizer_graph_info, 0);
|
|
63 }
|
|
64
|
|
65 return equalizer_graph_type;
|
|
66 }
|
|
67
|
|
68 static void ui_skinned_equalizer_graph_class_init(UiSkinnedEqualizerGraphClass *klass) {
|
|
69 GObjectClass *gobject_class;
|
|
70 GtkObjectClass *object_class;
|
|
71 GtkWidgetClass *widget_class;
|
|
72
|
|
73 gobject_class = G_OBJECT_CLASS(klass);
|
|
74 object_class = (GtkObjectClass*) klass;
|
|
75 widget_class = (GtkWidgetClass*) klass;
|
|
76 parent_class = gtk_type_class (gtk_widget_get_type ());
|
|
77
|
|
78 object_class->destroy = ui_skinned_equalizer_graph_destroy;
|
|
79
|
|
80 widget_class->realize = ui_skinned_equalizer_graph_realize;
|
|
81 widget_class->expose_event = ui_skinned_equalizer_graph_expose;
|
|
82 widget_class->size_request = ui_skinned_equalizer_graph_size_request;
|
|
83 widget_class->size_allocate = ui_skinned_equalizer_graph_size_allocate;
|
|
84
|
|
85 klass->scaled = ui_skinned_equalizer_graph_toggle_scaled;
|
|
86
|
|
87 equalizer_graph_signals[DOUBLED] =
|
|
88 g_signal_new ("toggle-scaled", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
|
|
89 G_STRUCT_OFFSET (UiSkinnedEqualizerGraphClass, scaled), NULL, NULL,
|
|
90 gtk_marshal_VOID__VOID, G_TYPE_NONE, 0);
|
|
91 }
|
|
92
|
|
93 static void ui_skinned_equalizer_graph_init(UiSkinnedEqualizerGraph *equalizer_graph) {
|
|
94 equalizer_graph->width = 113;
|
|
95 equalizer_graph->height = 19;
|
|
96 }
|
|
97
|
|
98 GtkWidget* ui_skinned_equalizer_graph_new(GtkWidget *fixed, gint x, gint y) {
|
|
99 UiSkinnedEqualizerGraph *equalizer_graph = g_object_new (ui_skinned_equalizer_graph_get_type (), NULL);
|
|
100
|
|
101 equalizer_graph->x = x;
|
|
102 equalizer_graph->y = y;
|
|
103 equalizer_graph->skin_index = SKIN_EQMAIN;
|
|
104 equalizer_graph->scaled = FALSE;
|
|
105
|
|
106 gtk_fixed_put(GTK_FIXED(fixed), GTK_WIDGET(equalizer_graph), equalizer_graph->x, equalizer_graph->y);
|
|
107
|
|
108 return GTK_WIDGET(equalizer_graph);
|
|
109 }
|
|
110
|
|
111 static void ui_skinned_equalizer_graph_destroy(GtkObject *object) {
|
|
112 UiSkinnedEqualizerGraph *equalizer_graph;
|
|
113
|
|
114 g_return_if_fail (object != NULL);
|
|
115 g_return_if_fail (UI_SKINNED_IS_EQUALIZER_GRAPH (object));
|
|
116
|
|
117 equalizer_graph = UI_SKINNED_EQUALIZER_GRAPH (object);
|
|
118
|
|
119 if (GTK_OBJECT_CLASS (parent_class)->destroy)
|
|
120 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
|
|
121 }
|
|
122
|
|
123 static void ui_skinned_equalizer_graph_realize(GtkWidget *widget) {
|
|
124 UiSkinnedEqualizerGraph *equalizer_graph;
|
|
125 GdkWindowAttr attributes;
|
|
126 gint attributes_mask;
|
|
127
|
|
128 g_return_if_fail (widget != NULL);
|
|
129 g_return_if_fail (UI_SKINNED_IS_EQUALIZER_GRAPH(widget));
|
|
130
|
|
131 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
|
|
132 equalizer_graph = UI_SKINNED_EQUALIZER_GRAPH(widget);
|
|
133
|
|
134 attributes.x = widget->allocation.x;
|
|
135 attributes.y = widget->allocation.y;
|
|
136 attributes.width = widget->allocation.width;
|
|
137 attributes.height = widget->allocation.height;
|
|
138 attributes.wclass = GDK_INPUT_OUTPUT;
|
|
139 attributes.window_type = GDK_WINDOW_CHILD;
|
|
140 attributes.event_mask = gtk_widget_get_events(widget);
|
|
141 attributes.event_mask |= GDK_EXPOSURE_MASK;
|
|
142 attributes.visual = gtk_widget_get_visual(widget);
|
|
143 attributes.colormap = gtk_widget_get_colormap(widget);
|
|
144
|
|
145 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
|
|
146 widget->window = gdk_window_new(widget->parent->window, &attributes, attributes_mask);
|
|
147
|
|
148 widget->style = gtk_style_attach(widget->style, widget->window);
|
|
149
|
|
150 gdk_window_set_user_data(widget->window, widget);
|
|
151 }
|
|
152
|
|
153 static void ui_skinned_equalizer_graph_size_request(GtkWidget *widget, GtkRequisition *requisition) {
|
|
154 UiSkinnedEqualizerGraph *equalizer_graph = UI_SKINNED_EQUALIZER_GRAPH(widget);
|
|
155
|
|
156 requisition->width = equalizer_graph->width*(equalizer_graph->scaled ? config.scale_factor : 1);
|
|
157 requisition->height = equalizer_graph->height*(equalizer_graph->scaled ? config.scale_factor : 1);
|
|
158 }
|
|
159
|
|
160 static void ui_skinned_equalizer_graph_size_allocate(GtkWidget *widget, GtkAllocation *allocation) {
|
|
161 UiSkinnedEqualizerGraph *equalizer_graph = UI_SKINNED_EQUALIZER_GRAPH (widget);
|
|
162
|
|
163 widget->allocation = *allocation;
|
|
164 widget->allocation.x *= (equalizer_graph->scaled ? config.scale_factor : 1);
|
|
165 widget->allocation.y *= (equalizer_graph->scaled ? config.scale_factor : 1);
|
|
166 if (GTK_WIDGET_REALIZED (widget))
|
|
167 gdk_window_move_resize(widget->window, widget->allocation.x, widget->allocation.y, allocation->width, allocation->height);
|
|
168
|
|
169 equalizer_graph->x = widget->allocation.x/(equalizer_graph->scaled ? config.scale_factor : 1);
|
|
170 equalizer_graph->y = widget->allocation.y/(equalizer_graph->scaled ? config.scale_factor : 1);
|
|
171 }
|
|
172
|
|
173 void
|
|
174 init_spline(gfloat * x, gfloat * y, gint n, gfloat * y2)
|
|
175 {
|
|
176 gint i, k;
|
|
177 gfloat p, qn, sig, un, *u;
|
|
178
|
|
179 u = (gfloat *) g_malloc(n * sizeof(gfloat));
|
|
180
|
|
181 y2[0] = u[0] = 0.0;
|
|
182
|
|
183 for (i = 1; i < n - 1; i++) {
|
|
184 sig = ((gfloat) x[i] - x[i - 1]) / ((gfloat) x[i + 1] - x[i - 1]);
|
|
185 p = sig * y2[i - 1] + 2.0;
|
|
186 y2[i] = (sig - 1.0) / p;
|
|
187 u[i] =
|
|
188 (((gfloat) y[i + 1] - y[i]) / (x[i + 1] - x[i])) -
|
|
189 (((gfloat) y[i] - y[i - 1]) / (x[i] - x[i - 1]));
|
|
190 u[i] = (6.0 * u[i] / (x[i + 1] - x[i - 1]) - sig * u[i - 1]) / p;
|
|
191 }
|
|
192 qn = un = 0.0;
|
|
193
|
|
194 y2[n - 1] = (un - qn * u[n - 2]) / (qn * y2[n - 2] + 1.0);
|
|
195 for (k = n - 2; k >= 0; k--)
|
|
196 y2[k] = y2[k] * y2[k + 1] + u[k];
|
|
197 g_free(u);
|
|
198 }
|
|
199
|
|
200 gfloat
|
|
201 eval_spline(gfloat xa[], gfloat ya[], gfloat y2a[], gint n, gfloat x)
|
|
202 {
|
|
203 gint klo, khi, k;
|
|
204 gfloat h, b, a;
|
|
205
|
|
206 klo = 0;
|
|
207 khi = n - 1;
|
|
208 while (khi - klo > 1) {
|
|
209 k = (khi + klo) >> 1;
|
|
210 if (xa[k] > x)
|
|
211 khi = k;
|
|
212 else
|
|
213 klo = k;
|
|
214 }
|
|
215 h = xa[khi] - xa[klo];
|
|
216 a = (xa[khi] - x) / h;
|
|
217 b = (x - xa[klo]) / h;
|
|
218 return (a * ya[klo] + b * ya[khi] +
|
|
219 ((a * a * a - a) * y2a[klo] +
|
|
220 (b * b * b - b) * y2a[khi]) * (h * h) / 6.0);
|
|
221 }
|
|
222
|
|
223 static gboolean ui_skinned_equalizer_graph_expose(GtkWidget *widget, GdkEventExpose *event) {
|
|
224 g_return_val_if_fail (widget != NULL, FALSE);
|
|
225 g_return_val_if_fail (UI_SKINNED_IS_EQUALIZER_GRAPH (widget), FALSE);
|
|
226 g_return_val_if_fail (event != NULL, FALSE);
|
|
227
|
|
228 UiSkinnedEqualizerGraph *equalizer_graph = UI_SKINNED_EQUALIZER_GRAPH (widget);
|
|
229 g_return_val_if_fail (equalizer_graph->width > 0 && equalizer_graph->height > 0, FALSE);
|
|
230
|
|
231 GdkPixbuf *obj = NULL;
|
|
232
|
|
233 obj = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, equalizer_graph->width, equalizer_graph->height);
|
|
234
|
|
235 guint32 cols[19], rowstride;
|
|
236 gint i, y, ymin, ymax, py = 0;
|
|
237 gfloat x[] = { 0, 11, 23, 35, 47, 59, 71, 83, 97, 109 }, yf[10];
|
|
238 guchar* pixels, *p;
|
|
239 gint n_channels;
|
|
240 /*
|
|
241 * This avoids the init_spline() function to be inlined.
|
|
242 * Inlining the function caused troubles when compiling with
|
|
243 * `-O' (at least on FreeBSD).
|
|
244 */
|
|
245 void (*__init_spline) (gfloat *, gfloat *, gint, gfloat *) = init_spline;
|
|
246
|
|
247 skin_draw_pixbuf(widget, aud_active_skin, obj, equalizer_graph->skin_index, 0, 294, 0, 0,
|
|
248 equalizer_graph->width, equalizer_graph->height);
|
|
249 skin_draw_pixbuf(widget, aud_active_skin, obj, equalizer_graph->skin_index, 0, 314,
|
|
250 0, 9 + ((aud_cfg->equalizer_preamp * 9) / 20),
|
|
251 equalizer_graph->width, 1);
|
|
252
|
|
253 skin_get_eq_spline_colors(aud_active_skin, cols);
|
|
254
|
|
255 __init_spline(x, aud_cfg->equalizer_bands, 10, yf);
|
|
256 for (i = 0; i < 109; i++) {
|
|
257 y = 9 -
|
|
258 (gint) ((eval_spline(x, aud_cfg->equalizer_bands, yf, 10, i) *
|
|
259 9.0) / EQUALIZER_MAX_GAIN);
|
|
260 if (y < 0)
|
|
261 y = 0;
|
|
262 if (y > 18)
|
|
263 y = 18;
|
|
264 if (!i)
|
|
265 py = y;
|
|
266 if (y < py) {
|
|
267 ymin = y;
|
|
268 ymax = py;
|
|
269 }
|
|
270 else {
|
|
271 ymin = py;
|
|
272 ymax = y;
|
|
273 }
|
|
274 py = y;
|
|
275
|
|
276 pixels = gdk_pixbuf_get_pixels(obj);
|
|
277 rowstride = gdk_pixbuf_get_rowstride(obj);
|
|
278 n_channels = gdk_pixbuf_get_n_channels(obj);
|
|
279
|
|
280 for (y = ymin; y <= ymax; y++)
|
|
281 {
|
|
282 p = pixels + (y * rowstride) + (( i + 2) * n_channels);
|
|
283 p[0] = (cols[y] & 0xff0000) >> 16;
|
|
284 p[1] = (cols[y] & 0x00ff00) >> 8;
|
|
285 p[2] = (cols[y] & 0x0000ff);
|
|
286 /* do we really need to treat the alpha channel? */
|
|
287 /*if (n_channels == 4)
|
|
288 p[3] = cols[y] >> 24;*/
|
|
289 }
|
|
290 }
|
|
291
|
|
292 ui_skinned_widget_draw(widget, obj, equalizer_graph->width, equalizer_graph->height, equalizer_graph->scaled);
|
|
293
|
|
294 g_object_unref(obj);
|
|
295
|
|
296 return FALSE;
|
|
297 }
|
|
298
|
|
299 static void ui_skinned_equalizer_graph_toggle_scaled(UiSkinnedEqualizerGraph *equalizer_graph) {
|
|
300 GtkWidget *widget = GTK_WIDGET (equalizer_graph);
|
|
301
|
|
302 equalizer_graph->scaled = !equalizer_graph->scaled;
|
|
303 gtk_widget_set_size_request(widget, equalizer_graph->width*(equalizer_graph->scaled ? config.scale_factor : 1),
|
|
304 equalizer_graph->height*(equalizer_graph->scaled ? config.scale_factor : 1));
|
|
305
|
|
306 gtk_widget_queue_draw(GTK_WIDGET(equalizer_graph));
|
|
307 }
|