summaryrefslogtreecommitdiff
path: root/plugins/gtkui/ddbequalizer.vala
blob: 82a1f0604e630391abb4ee695a0009379c642100 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
    SuperEQ GTK Widget for for DeaDBeeF
    Copyright (C) 2010 Viktor Semykin <thesame.ml@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

// sripped down and polished by Alexey Yakovenko <waker@users.sourceforge.net>

using Gtk;
using Gdk;

const string[] freqs = {
    "55 Hz","77 Hz","110 Hz","156 Hz","220 Hz","311 Hz","440 Hz","622 Hz","880 Hz",
    "1.2 kHz","1.8 kHz","2.5 kHz","3.5 kHz","5 kHz","7 kHz","10 kHz","14 kHz","20 kHz"
};

namespace Ddb {
    public class Equalizer : Gtk.DrawingArea
    {
        public signal void on_changed ();

        private double[] values = new double [bands];
        private double preamp = 0.5;

        private int mouse_y = -1;
        
        private bool curve_hook = false;
        private bool preamp_hook = false;

        private int eq_margin_bottom = -1;
        private int eq_margin_left = -1;
        static const int spot_size = 3;
        static const int bands = 18;

//        Gdk.Cursor moving_cursor = new Gdk.Cursor (Gdk.CursorType.FLEUR);
//        Gdk.Cursor updown_cursor = new Gdk.Cursor (Gdk.CursorType.double_ARROW);
        Gdk.Cursor pointer_cursor = new Gdk.Cursor (Gdk.CursorType.LEFT_PTR);

        construct
        {
            eq_margin_bottom = (int)(Pango.units_to_double (get_style ().font_desc.get_size ()) * Gdk.Screen.get_default ().get_resolution () / 72 + 4);
            eq_margin_left = eq_margin_bottom * 4;

            //color_changed ();
        }

        public override bool configure_event (Gdk.EventConfigure event) {
            Gtkui.init_theme_colors ();
            return false;
        }

          public override void realize () {
              base.realize ();
              add_events (Gdk.EventMask.EXPOSURE_MASK | Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK | Gdk.EventMask.LEAVE_NOTIFY_MASK | Gdk.EventMask.POINTER_MOTION_MASK);
          }

        public override bool
        expose_event (Gdk.EventExpose event)
        {
            Gdk.Color fore_bright_color = Gtkui.get_bar_foreground_color ();

            Gdk.Color c1 = fore_bright_color;
            Gdk.Color c2 = Gtkui.get_bar_background_color ();
            Gdk.Color fore_dark_color = c2;
            fore_dark_color.red += (int16)((c1.red - c2.red) * 0.5);
            fore_dark_color.green += (int16)((c1.green - c2.green) * 0.5);
            fore_dark_color.blue += (int16)((c1.blue - c2.blue) * 0.5);

            Allocation alloc;
            get_allocation (out alloc);

            int width = alloc.width;
            int height = alloc.height;

            var ctx = Gdk.cairo_create (get_window ());
            ctx.set_antialias (Cairo.Antialias.NONE);
            ctx.set_line_width (1.0);
            ctx.set_source_rgba (c2.red/65535.0, c2.green/65535.0, c2.blue/65535.0, 1);
            ctx.rectangle (0, 0, width, height);
            ctx.fill ();

            ctx.set_source_rgba (fore_dark_color.red/65535.0, fore_dark_color.green/65535.0, fore_dark_color.blue/65535.0, 1);

            //drawing grid:
            double step = (double)(width - eq_margin_left) / (double)(bands+1);
            int i;
            for (i = 0; i < bands; i++)
            {
                //does anyone know why this method is static?
                ctx.move_to ((int)((i+1)*step)+eq_margin_left, 0);
                ctx.line_to ((int)((i+1)*step)+eq_margin_left, height - eq_margin_bottom);
            }

            double vstep = (double)(height-eq_margin_bottom);
            for (double di=0; di < 2; di += 0.25)
            {
                ctx.move_to (eq_margin_left, (int)((di-preamp)*vstep));
                ctx.line_to (width, (int)((di-preamp)*vstep));
            }
            ctx.stroke ();

            ctx.set_source_rgba (fore_bright_color.red/65535.0, fore_bright_color.green/65535.0, fore_bright_color.blue/65535.0, 1);

            //drawing freqs:

            Pango.Layout l = Pango.cairo_create_layout (ctx);
            var pctx = l.get_context ();

            var fd = get_style ().font_desc.copy ();

            fd.set_size ((int)(get_style ().font_desc.get_size () * 0.7));
            pctx.set_font_description (fd);
            for (i = 0; i < bands; i++)
            {
                ctx.save ();
                l.set_text (freqs[i], (int)freqs[i].length);
                Pango.Rectangle ink, log;
                l.get_pixel_extents (out ink, out log);
                int offs = 2;
                if ((i % 2) != 0) {
                    offs += 2;
                }
                ctx.move_to ( (int)((i+1)*step)+eq_margin_left - ink.width/2, height-eq_margin_bottom + offs);
                Pango.cairo_show_layout (ctx, l);
                ctx.restore ();
            }
            fd.set_size ((int)(get_style ().font_desc.get_size ()));
            pctx.set_font_description (fd);
            
            //drawing db's:
            l.set_width (eq_margin_left-1);
            l.set_alignment (Pango.Alignment.RIGHT);

            int fontsize = (int)(Pango.units_to_double (fd.get_size ()) * Gdk.Screen.get_default ().get_resolution () / 72);

            if ((mouse_y >= 0) && (mouse_y < height - eq_margin_bottom))
            {
                ctx.save ();
                double db = scale((double)(mouse_y-1) / (double)(height - eq_margin_bottom - 2));
                string tmp = "%s%.1fdB".printf (db > 0 ? "+" : "", db);
                l.set_text (tmp, (int)tmp.length);
                ctx.move_to (eq_margin_left-1, mouse_y-3);
                Pango.cairo_show_layout (ctx, l);
                ctx.restore ();
            }
            
            ctx.save ();
            string tmp;
            double val = scale(1);
            tmp = "%s%.1fdB".printf (val > 0 ? "+" : "", val);
            l.set_text (tmp, (int)tmp.length);
            ctx.move_to (eq_margin_left-1, height-eq_margin_bottom-fontsize);
            Pango.cairo_show_layout (ctx, l);
            ctx.restore ();

            ctx.save ();
            val = scale(0);
            tmp = "%s%.1fdB".printf (val > 0 ? "+" : "", val);
            l.set_text (tmp, (int)tmp.length);
            ctx.move_to (eq_margin_left-1, 1);
            Pango.cairo_show_layout (ctx, l);
            ctx.restore ();

            ctx.save ();
            l.set_text ("+0dB", 4);
            ctx.move_to (eq_margin_left-1, (int)((1-preamp)*(height-eq_margin_bottom))-fontsize/2);
            Pango.cairo_show_layout (ctx, l);
            ctx.restore ();

            ctx.save ();
            l.set_text ("preamp", 6);
            l.set_alignment (Pango.Alignment.LEFT);
            ctx.move_to (1, height-eq_margin_bottom+2);
            Pango.cairo_show_layout (ctx, l);
            ctx.restore ();

            // frame
            ctx.rectangle (eq_margin_left, 0, width-eq_margin_left-1, height-eq_margin_bottom-1);
            ctx.stroke ();

            //draw preamp
            ctx.rectangle (0, (int)(preamp * (height-eq_margin_bottom)), 11, height);
            ctx.clip ();

            ctx.set_source_rgba (fore_bright_color.red/65535.0, fore_bright_color.green/65535.0, fore_bright_color.blue/65535.0, 1.0);
            int count = (int)((height-eq_margin_bottom) / 6)+1;
            for (int j = 0; j < count; j++) {
                ctx.rectangle (1, height-eq_margin_bottom-j*6 - 6, 11, 4);
            }
            ctx.fill ();
            ctx.reset_clip ();

            //drawing bars:
            int bar_w = 11;
            if (step < bar_w)
                bar_w = (int)step-1;


            for (i = 0; i < bands; i++)
            {
                ctx.reset_clip ();
                ctx.rectangle ((int)((i+1)*step)+eq_margin_left - bar_w/2, (int)(values[i] * (height-eq_margin_bottom)), 11, height);
                ctx.clip ();
                count = (int)((height-eq_margin_bottom) * (1-values[i]) / 6)+1;
                for (int j = 0; j < count; j++) {
                    ctx.rectangle ( (int)((i+1)*step)+eq_margin_left - bar_w/2, height-eq_margin_bottom-j*6 - 6, bar_w, 4);
                }
                ctx.fill ();
            }

            //drawing mouse coordinates:
            ctx.reset_clip ();
            ctx.set_dash (new double[] {4, 4}, 0);
            ctx.move_to (eq_margin_left+1, mouse_y);
            ctx.line_to (width, mouse_y);
            ctx.stroke ();
            
            return false;
        }
 
        private inline double
        scale (double val)
        {
            double k = -40;
            double d = 20;
            return (val + preamp - 0.5) * k + d;
        }

        private bool
        in_curve_area (double x, double y)
        {
            return
                x > eq_margin_left &&
                x < allocation.width-1 &&
                y > 1 &&
                y < allocation.height-eq_margin_bottom;
        }

        private void
        update_eq_drag (double x, double y) {
            double band_width = (double)(allocation.width - eq_margin_left) / (double)(bands+1);
            int band = (int)GLib.Math.floor ((x - eq_margin_left) / band_width - 0.5);
            if (band < 0) {
                band = 0;
            }
            if (band >= bands) {
                band = band-1;
            }
            if (band >= 0 && band < bands) {
                values[band] = y / (double)(allocation.height - eq_margin_bottom);
                if (values[band] > 1) {
                    values[band] = 1;
                }
                else if (values[band] < 0) {
                    values[band] = 0;
                }
                on_changed ();
            }
        }

        /* Mouse button got pressed over widget */
        public override bool
        button_press_event (Gdk.EventButton event)
        {
            if (in_curve_area ((int)event.x, (int)event.y))
            {
                curve_hook = true;
                update_eq_drag ((int)event.x, (int)event.y);
                mouse_y = (int)event.y;
                queue_draw ();
                return false;
            }

            if (event.x <= 11 &&
                event.y > 1 &&
                event.y <= allocation.height-eq_margin_bottom &&
                event.button == 1
                )
            {
                preamp = event.y / (double)(allocation.height - eq_margin_bottom);
                on_changed ();
                preamp_hook = true;
                mouse_y = (int)event.y;
                queue_draw ();
            }

            return false;
        }

        /* Mouse button got released */
        public override bool
        button_release_event (Gdk.EventButton event)
        {
            curve_hook = false;
            preamp_hook = false;
            get_window().set_cursor (pointer_cursor);
            return false;
        }

        public override bool
        leave_notify_event (Gdk.EventCrossing event)
        {
            mouse_y = -1;
            queue_draw();
            return false;
        }

        /* Mouse pointer moved over widget */
        public override bool
        motion_notify_event (Gdk.EventMotion event) {
            double y = event.y / (double)(allocation.height - eq_margin_bottom);
            if (y < 0) y = 0;
            if (y > 1) y = 1;

            if (preamp_hook)
            {
                preamp = y;
                on_changed ();
                queue_draw();
                return false;
            }
            
            if (!in_curve_area ((int)event.x, (int)event.y))
                mouse_y = -1;
            else
                mouse_y = (int)event.y;

            if (curve_hook)
            {
                update_eq_drag ((int)event.x, (int)event.y);
                mouse_y = (int)event.y;
            }
            queue_draw ();
            return false;
        }

        public void
        set_band (int band, double v) {
            values[band] = 1 - (v + 20.0) / 40.0;
        }

        public double
        get_band (int band) {
            return ((1 - values[band]) * 40.0) - 20.0;
        }

        public void
        set_preamp (double v) {
            preamp = 1 - (v + 20.0) / 40.0;
        }

        public double
        get_preamp () {
            return ((1 - preamp) * 40.0) - 20.0;
        }

    }
}