summaryrefslogtreecommitdiff
path: root/plugins/gtkui/ddbequalizer.vala
blob: 4b82671c68e9ef22512d3f7a1333558b73452b0b (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
/*
    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>

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 margin_bottom = -1;
        private int 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
        {
            margin_bottom = (int)(Pango.units_to_double (get_style ().font_desc.get_size ()) * Gdk.Screen.get_default ().get_resolution () / 72 + 4);
            margin_left = 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);

            int width = allocation.width;
            int height = allocation.height;

            Gdk.Drawable d = get_window();
            var gc = d.create_gc (Gdk.GCValues(), 0);
            //Gdk.Rectangle rc = {0, 0, allocation.width, allocation.height};
            //gc.set_clip_rectangle (rc);

            gc.set_rgb_fg_color (c2);

            d.draw_rectangle (gc, true, 0, 0, width, height);

            gc.set_rgb_fg_color (fore_dark_color);
            //drawing grid:
            double step = (double)(width - margin_left) / (double)(bands+1);
            int i;
            for (i = 0; i < bands; i++)
            {
                //does anyone know why this method is static?
                Gdk.draw_line (d, gc,
                    (int)((i+1)*step)+margin_left,
                    0,
                    (int)((i+1)*step)+margin_left,
                    height - margin_bottom);
            }

            double vstep = (double)(height-margin_bottom);
            for (double di=0; di < 2; di += 0.25)
            {
                Gdk.draw_line (d, gc,
                    margin_left,
                    (int)((di-preamp)*vstep),
                    width,
                    (int)((di-preamp)*vstep));
            }

            gc.set_rgb_fg_color (fore_bright_color);

            //drawing freqs:
            Pango.Layout l = create_pango_layout (null);
            var ctx = l.get_context ();

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

//            var fd = ctx.get_font_description ();
            fd.set_size ((int)(get_style ().font_desc.get_size () * 0.7));
            ctx.set_font_description (fd);
            for (i = 0; i < bands; i++)
            {
                l.set_text (freqs[i], (int)freqs[i].len());
                Pango.Rectangle ink, log;
                l.get_pixel_extents (out ink, out log);
                int offs = 2;
                if ((i % 2) != 0) {
                    offs += 2;
                }
                Gdk.draw_layout (d, gc, (int)((i+1)*step)+margin_left - ink.width/2, height-margin_bottom + offs, l);
            }
            fd.set_size ((int)(get_style ().font_desc.get_size ()));
            ctx.set_font_description (fd);
            
            //drawing db's:
            l.set_width (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 - margin_bottom))
            {
                double db = scale((double)(mouse_y-1) / (double)(height - margin_bottom - 2));
                string tmp = "%s%.1fdB".printf (db > 0 ? "+" : "", db);
                l.set_text (tmp, (int)tmp.len());
                Gdk.draw_layout (d, gc, margin_left-1, mouse_y-3, l);
            }
            
            string tmp;
            double val = scale(1);
            tmp = "%s%.1fdB".printf (val > 0 ? "+" : "", val);
            l.set_text (tmp, (int)tmp.len());
            Gdk.draw_layout (d, gc, margin_left-1, height-margin_bottom-fontsize, l);

            val = scale(0);
            tmp = "%s%.1fdB".printf (val > 0 ? "+" : "", val);
            l.set_text (tmp, (int)tmp.len());
            Gdk.draw_layout (d, gc, margin_left-1, 1, l);

            l.set_text ("+0dB", 4);
            Gdk.draw_layout (d, gc, margin_left-1, (int)((1-preamp)*(height-margin_bottom))-fontsize/2, l);

            l.set_text ("preamp", 6);
            l.set_alignment (Pango.Alignment.LEFT);
            Gdk.draw_layout (d, gc, 1, height-margin_bottom+2, l);

            d.draw_rectangle (gc, false, margin_left, 0, width-margin_left-1, height-margin_bottom-1);
            gc.set_line_attributes (2, Gdk.LineStyle.SOLID, Gdk.CapStyle.NOT_LAST, Gdk.JoinStyle.MITER);
            
            //draw preamp
            gc.set_clip_rectangle ({0, (int)(preamp * (height-margin_bottom)), 11, height});

            gc.set_rgb_fg_color (fore_bright_color);
            int count = (int)((height-margin_bottom) / 6)+1;
            for (int j = 0; j < count; j++)
                d.draw_rectangle (
                    gc,
                    true,
                    1,
                    height-margin_bottom-j*6 - 6,
                    11,
                    4);

            gc.set_clip_rectangle ({margin_left+1, 1, width-margin_left-2, height-margin_bottom-2});

            //drawing bars:
            gc.set_rgb_fg_color (fore_bright_color);

            int bar_w = 11;
            if (step < bar_w)
                bar_w = (int)step-1;


            for (i = 0; i < bands; i++)
            {
                gc.set_clip_rectangle ({
                    (int)((i+1)*step)+margin_left - bar_w/2,
                    (int)(values[i] * (height-margin_bottom)),
                    11,
                    height});
                count = (int)((height-margin_bottom) * (1-values[i]) / 6)+1;
                for (int j = 0; j < count; j++)
                    d.draw_rectangle (
                        gc,
                        true,
                        (int)((i+1)*step)+margin_left - bar_w/2,
                        height-margin_bottom-j*6 - 6,
                        bar_w,
                        4);
            }
            gc.set_clip_rectangle ({0, 0, width, height});

            //drawing mouse coordinates:
            gc.set_line_attributes (1, Gdk.LineStyle.ON_OFF_DASH, Gdk.CapStyle.NOT_LAST, Gdk.JoinStyle.MITER);
            Gdk.draw_line (d, gc, margin_left+1, mouse_y, width, mouse_y);
            
            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 > margin_left &&
                x < allocation.width-1 &&
                y > 1 &&
                y < allocation.height-margin_bottom;
        }

        private void
        update_eq_drag (double x, double y) {
            double band_width = (double)(allocation.width - margin_left) / (double)(bands+1);
            int band = (int)GLib.Math.floor ((x - 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 - 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-margin_bottom &&
                event.button == 1
                )
            {
                preamp = event.y / (double)(allocation.height - 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 - 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;
        }

    }
}