move ardour meter globals into dedicated namespace (TODO meter_patters typedef)
[ardour.git] / gtk2_ardour / meter_patterns.cc
1 /*
2     Copyright (C) 2013 Paul Davis
3     Author: Robin Gareus
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <gtkmm2ext/cairo_widget.h>
21 #include <gtkmm2ext/gtk_ui.h>
22 #include <gtkmm2ext/utils.h>
23 #include <gtkmm2ext/rgb_macros.h>
24
25 #include <ardour/rc_configuration.h>
26 #include "ardour_ui.h"
27 #include "utils.h"
28 #include "logmeter.h"
29 #include "meter_patterns.h"
30
31 #include "i18n.h"
32
33 using namespace ARDOUR;
34 using namespace PBD;
35 using namespace Gtk;
36 using namespace Gtkmm2ext;
37 using namespace std;
38 using namespace ArdourMeter;
39
40 static const int max_pattern_metric_size = 1026;
41
42 sigc::signal<void> ArdourMeter::ResetAllPeakDisplays;
43 sigc::signal<void,ARDOUR::Route*> ArdourMeter::ResetRoutePeakDisplays;
44 sigc::signal<void,ARDOUR::RouteGroup*> ArdourMeter::ResetGroupPeakDisplays;
45 sigc::signal<void> ArdourMeter::RedrawMetrics;
46
47 sigc::signal<void, int, ARDOUR::RouteGroup*, ARDOUR::MeterType> ArdourMeter::SetMeterTypeMulti;
48
49 namespace ArdourMeter {
50         typedef std::map<std::string,cairo_pattern_t*> TickPatterns;
51         typedef std::map<std::string,cairo_pattern_t*> MetricPatterns;
52 }
53
54 static ArdourMeter::TickPatterns ticks_patterns;
55 static ArdourMeter::MetricPatterns metric_patterns;
56
57
58 static cairo_pattern_t*
59 meter_render_ticks (Gtk::Widget& w, vector<ARDOUR::DataType> types)
60 {
61         Glib::RefPtr<Gdk::Window> win (w.get_window());
62
63         bool background;
64         gint width, height;
65         win->get_size (width, height);
66         background = types.size() == 0
67                 || w.get_name().substr(w.get_name().length() - 4) == "Left"
68                 || w.get_name().substr(w.get_name().length() - 5) == "Right";
69
70         cairo_surface_t* surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
71         cairo_t* cr = cairo_create (surface);
72
73         cairo_move_to (cr, 0, 0);
74         cairo_rectangle (cr, 0, 0, width, height);
75         {
76                 Gdk::Color c = w.get_style()->get_bg (background ? Gtk::STATE_ACTIVE : Gtk::STATE_NORMAL);
77                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
78         }
79         cairo_fill (cr);
80
81         height = min(max_pattern_metric_size, height);
82         uint32_t peakcolor = ARDOUR_UI::config()->color_by_name ("meterbridge peaklabel");
83
84         for (vector<DataType>::const_iterator i = types.begin(); i != types.end(); ++i) {
85
86                 Gdk::Color c;
87                 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
88
89                 if (types.size() > 1) {
90                         /* we're overlaying more than 1 set of marks, so use different colours */
91                         switch (*i) {
92                         case DataType::AUDIO:
93                                 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
94                                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
95                                 break;
96                         case DataType::MIDI:
97                                 c = w.get_style()->get_fg (Gtk::STATE_ACTIVE);
98                                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
99                                 break;
100                         }
101                 } else {
102                         c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
103                         cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
104                 }
105
106                 std::map<int,float> points;
107
108                 switch (*i) {
109                 case DataType::AUDIO:
110                         points.insert (std::pair<int,float>(-60, 0.5));
111                         points.insert (std::pair<int,float>(-50, 0.5));
112                         points.insert (std::pair<int,float>(-40, 0.5));
113                         points.insert (std::pair<int,float>(-30, 0.5));
114                         if (Config->get_meter_line_up_level() == MeteringLineUp24) {
115                                 points.insert (std::pair<int,float>(-24, 0.5));
116                         } else {
117                                 points.insert (std::pair<int,float>(-25, 0.5));
118                         }
119                         points.insert (std::pair<int,float>(-20, 1.0));
120
121                         points.insert (std::pair<int,float>(-19, 0.5));
122                         points.insert (std::pair<int,float>(-18, 1.0));
123                         points.insert (std::pair<int,float>(-17, 0.5));
124                         points.insert (std::pair<int,float>(-16, 0.5));
125                         points.insert (std::pair<int,float>(-15, 1.0));
126
127                         points.insert (std::pair<int,float>(-14, 0.5));
128                         points.insert (std::pair<int,float>(-13, 0.5));
129                         points.insert (std::pair<int,float>(-12, 0.5));
130                         points.insert (std::pair<int,float>(-11, 0.5));
131                         points.insert (std::pair<int,float>(-10, 1.0));
132
133                         points.insert (std::pair<int,float>( -9, 0.5));
134                         points.insert (std::pair<int,float>( -8, 0.5));
135                         points.insert (std::pair<int,float>( -7, 0.5));
136                         points.insert (std::pair<int,float>( -6, 0.5));
137                         points.insert (std::pair<int,float>( -5, 1.0));
138                         points.insert (std::pair<int,float>( -4, 0.5));
139                         points.insert (std::pair<int,float>( -3, 0.5));
140                         points.insert (std::pair<int,float>( -2, 0.5));
141                         points.insert (std::pair<int,float>( -1, 0.5));
142
143                         points.insert (std::pair<int,float>(  0, 1.0));
144                         points.insert (std::pair<int,float>(  1, 0.5));
145                         points.insert (std::pair<int,float>(  2, 0.5));
146                         points.insert (std::pair<int,float>(  3, 0.5));
147                         points.insert (std::pair<int,float>(  4, 0.5));
148                         points.insert (std::pair<int,float>(  5, 0.5));
149                         break;
150
151                 case DataType::MIDI:
152                         points.insert (std::pair<int,float>(  0, 1.0));
153                         points.insert (std::pair<int,float>( 16, 0.5));
154                         points.insert (std::pair<int,float>( 32, 0.5));
155                         points.insert (std::pair<int,float>( 48, 0.5));
156                         points.insert (std::pair<int,float>( 64, 1.0));
157                         points.insert (std::pair<int,float>( 80, 0.5));
158                         points.insert (std::pair<int,float>( 96, 0.5));
159                         points.insert (std::pair<int,float>(100, 1.0));
160                         points.insert (std::pair<int,float>(112, 0.5));
161                         points.insert (std::pair<int,float>(127, 1.0));
162                         break;
163                 }
164
165                 for (std::map<int,float>::const_iterator j = points.begin(); j != points.end(); ++j) {
166                         cairo_set_line_width (cr, (j->second));
167
168                         float fraction = 0;
169                         gint pos;
170
171                         switch (*i) {
172                         case DataType::AUDIO:
173                                 if (j->first >= 0 || j->first == -9) {
174                                         cairo_set_source_rgb (cr,
175                                                         UINT_RGBA_R_FLT(peakcolor),
176                                                         UINT_RGBA_G_FLT(peakcolor),
177                                                         UINT_RGBA_B_FLT(peakcolor));
178                                 } else {
179                                         cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
180                                 }
181                                 fraction = log_meter (j->first);
182                                 pos = height - (gint) floor (height * fraction);
183                                 cairo_move_to(cr, 0, pos + .5);
184                                 cairo_line_to(cr, 3, pos + .5);
185                                 cairo_stroke (cr);
186                                 break;
187                         case DataType::MIDI:
188                                 fraction = (j->first) / 127.0;
189                                 pos = 1 + height - (gint) floor (height * fraction);
190                                 pos = min (pos, height);
191                                 cairo_arc(cr, 1.5, pos + .5, 1.0, 0, 2 * M_PI);
192                                 cairo_fill(cr);
193                                 break;
194                         }
195                 }
196         }
197
198         cairo_pattern_t* pattern = cairo_pattern_create_for_surface (surface);
199
200         cairo_destroy (cr);
201         cairo_surface_destroy (surface);
202
203         return pattern;
204 }
205
206
207 static cairo_pattern_t*
208 meter_render_metrics (Gtk::Widget& w, vector<DataType> types)
209 {
210         Glib::RefPtr<Gdk::Window> win (w.get_window());
211
212         bool tickleft;
213         bool background;
214         gint width, height;
215         win->get_size (width, height);
216
217         tickleft = w.get_name().substr(w.get_name().length() - 4) == "Left";
218         background = types.size() == 0 || tickleft || w.get_name().substr(w.get_name().length() - 5) == "Right";
219
220         cairo_surface_t* surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
221         cairo_t* cr = cairo_create (surface);
222         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create(w.get_pango_context());
223
224         Pango::AttrList audio_font_attributes;
225         Pango::AttrList midi_font_attributes;
226         Pango::AttrList unit_font_attributes;
227
228         Pango::AttrFontDesc* font_attr;
229         Pango::FontDescription font;
230
231         font = Pango::FontDescription (""); // use defaults
232         //font = get_font_for_style("gain-fader");
233         //font = w.get_style()->get_font();
234
235         font.set_weight (Pango::WEIGHT_NORMAL);
236         font.set_size (9.0 * PANGO_SCALE);
237         font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
238         audio_font_attributes.change (*font_attr);
239         delete font_attr;
240
241         font.set_weight (Pango::WEIGHT_ULTRALIGHT);
242         font.set_stretch (Pango::STRETCH_ULTRA_CONDENSED);
243         font.set_size (7.5 * PANGO_SCALE);
244         font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
245         midi_font_attributes.change (*font_attr);
246         delete font_attr;
247
248         font.set_size (7.0 * PANGO_SCALE);
249         font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
250         unit_font_attributes.change (*font_attr);
251         delete font_attr;
252
253         cairo_move_to (cr, 0, 0);
254         cairo_rectangle (cr, 0, 0, width, height);
255         {
256                 Gdk::Color c = w.get_style()->get_bg (background ? Gtk::STATE_ACTIVE : Gtk::STATE_NORMAL);
257                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
258         }
259         cairo_fill (cr);
260
261         height = min(max_pattern_metric_size, height);
262         uint32_t peakcolor = ARDOUR_UI::config()->color_by_name ("meterbridge peaklabel");
263
264         for (vector<DataType>::const_iterator i = types.begin(); i != types.end(); ++i) {
265
266                 Gdk::Color c;
267
268                 if (types.size() > 1) {
269                         /* we're overlaying more than 1 set of marks, so use different colours */
270                         Gdk::Color c;
271                         switch (*i) {
272                         case DataType::AUDIO:
273                                 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
274                                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
275                                 break;
276                         case DataType::MIDI:
277                                 c = w.get_style()->get_fg (Gtk::STATE_ACTIVE);
278                                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
279                                 break;
280                         }
281                 } else {
282                         c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
283                         cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
284                 }
285
286                 std::map<int,float> points;
287
288                 switch (*i) {
289                 case DataType::AUDIO:
290                         layout->set_attributes (audio_font_attributes);
291                         points.insert (std::pair<int,float>(-50, 0.5));
292                         points.insert (std::pair<int,float>(-40, 0.5));
293                         points.insert (std::pair<int,float>(-30, 0.5));
294                         points.insert (std::pair<int,float>(-20, 1.0));
295                         if (types.size() == 1) {
296                                 if (Config->get_meter_line_up_level() == MeteringLineUp24) {
297                                         points.insert (std::pair<int,float>(-24, 0.5));
298                                 } else {
299                                         points.insert (std::pair<int,float>(-25, 0.5));
300                                 }
301                                 points.insert (std::pair<int,float>(-15, 1.0));
302                         }
303                         points.insert (std::pair<int,float>(-18, 1.0));
304                         points.insert (std::pair<int,float>(-10, 1.0));
305                         points.insert (std::pair<int,float>( -5, 1.0));
306                         points.insert (std::pair<int,float>( -3, 0.5));
307                         points.insert (std::pair<int,float>(  0, 1.0));
308                         points.insert (std::pair<int,float>(  3, 0.5));
309                         break;
310
311                 case DataType::MIDI:
312                         layout->set_attributes (midi_font_attributes);
313                         points.insert (std::pair<int,float>(  0, 1.0));
314                         if (types.size() == 1) {
315                                 points.insert (std::pair<int,float>( 16, 0.5));
316                                 points.insert (std::pair<int,float>( 32, 0.5));
317                                 points.insert (std::pair<int,float>( 48, 0.5));
318                                 points.insert (std::pair<int,float>( 64, 1.0));
319                                 points.insert (std::pair<int,float>( 80, 0.5));
320                                 points.insert (std::pair<int,float>( 96, 0.5));
321                                 points.insert (std::pair<int,float>(100, 0.5));
322                                 points.insert (std::pair<int,float>(112, 0.5));
323                         } else {
324                                 /* labels that don't overlay with dB */
325                                 points.insert (std::pair<int,float>( 24, 0.5));
326                                 points.insert (std::pair<int,float>( 48, 0.5));
327                                 points.insert (std::pair<int,float>( 72, 0.5));
328                         }
329                         points.insert (std::pair<int,float>(127, 1.0));
330                         break;
331                 }
332
333                 char buf[32];
334                 gint pos;
335
336                 for (std::map<int,float>::const_iterator j = points.begin(); j != points.end(); ++j) {
337
338                         float fraction = 0;
339                         switch (*i) {
340                         case DataType::AUDIO:
341                                 cairo_set_line_width (cr, (j->second));
342                                 if (j->first >= 0) {
343                                         cairo_set_source_rgb (cr,
344                                                         UINT_RGBA_R_FLT(peakcolor),
345                                                         UINT_RGBA_G_FLT(peakcolor),
346                                                         UINT_RGBA_B_FLT(peakcolor));
347                                 }
348                                 fraction = log_meter (j->first);
349                                 snprintf (buf, sizeof (buf), "%+2d", j->first);
350                                 pos = height - (gint) floor (height * fraction);
351                                 if (tickleft) {
352                                         cairo_move_to(cr, width-2.5, pos + .5);
353                                         cairo_line_to(cr, width, pos + .5);
354                                 } else {
355                                         cairo_move_to(cr, 0, pos + .5);
356                                         cairo_line_to(cr, 2.5, pos + .5);
357                                 }
358                                 cairo_stroke (cr);
359                                 break;
360                         case DataType::MIDI:
361                                 cairo_set_line_width (cr, 1.0);
362                                 fraction = (j->first) / 127.0;
363                                 snprintf (buf, sizeof (buf), "%3d", j->first);
364                                 pos = 1 + height - (gint) rintf (height * fraction);
365                                 pos = min (pos, height);
366                                 if (tickleft) {
367                                         cairo_arc(cr, width - 2.0, pos + .5, 1.0, 0, 2 * M_PI);
368                                 } else {
369                                         cairo_arc(cr, 3, pos + .5, 1.0, 0, 2 * M_PI);
370                                 }
371                                 cairo_fill(cr);
372                                 break;
373                         }
374
375                         layout->set_text(buf);
376
377                         /* we want logical extents, not ink extents here */
378
379                         int tw, th;
380                         layout->get_pixel_size(tw, th);
381
382                         int p = pos - (th / 2);
383                         p = min (p, height - th);
384                         p = max (p, 0);
385
386                         cairo_move_to (cr, width-4-tw, p);
387                         pango_cairo_show_layout (cr, layout->gobj());
388                 }
389         }
390
391         if (types.size() == 1) {
392                 int tw, th;
393                 layout->set_attributes (unit_font_attributes);
394                 switch (types.at(0)) {
395                         case DataType::AUDIO:
396                                 layout->set_text("dBFS");
397                                 layout->get_pixel_size(tw, th);
398                                 break;
399                         case DataType::MIDI:
400                                 layout->set_text("vel");
401                                 layout->get_pixel_size(tw, th);
402                                 break;
403                 }
404                 Gdk::Color c = w.get_style()->get_fg (Gtk::STATE_ACTIVE);
405                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
406                 cairo_move_to (cr, 2, height - th - 1.5);
407                 pango_cairo_show_layout (cr, layout->gobj());
408         }
409
410         cairo_pattern_t* pattern = cairo_pattern_create_for_surface (surface);
411
412         cairo_destroy (cr);
413         cairo_surface_destroy (surface);
414
415         return pattern;
416 }
417
418
419 gint
420 ArdourMeter::meter_expose_ticks (GdkEventExpose *ev, std::vector<ARDOUR::DataType> types, Gtk::DrawingArea *mta)
421 {
422         Glib::RefPtr<Gdk::Window> win (mta->get_window());
423         cairo_t* cr;
424
425         cr = gdk_cairo_create (win->gobj());
426
427         /* clip to expose area */
428
429         gdk_cairo_rectangle (cr, &ev->area);
430         cairo_clip (cr);
431
432         cairo_pattern_t* pattern;
433         TickPatterns::iterator i = ticks_patterns.find (mta->get_name());
434
435         if (i == ticks_patterns.end()) {
436                 pattern = meter_render_ticks (*mta, types);
437                 ticks_patterns[mta->get_name()] = pattern;
438         } else {
439                 pattern = i->second;
440         }
441
442         cairo_move_to (cr, 0, 0);
443         cairo_set_source (cr, pattern);
444
445         gint width, height;
446         win->get_size (width, height);
447
448         cairo_rectangle (cr, 0, 0, width, height);
449         cairo_fill (cr);
450
451         cairo_destroy (cr);
452
453         return true;
454 }
455
456 gint
457 ArdourMeter::meter_expose_metrics (GdkEventExpose *ev, std::vector<ARDOUR::DataType> types, Gtk::DrawingArea *mma)
458 {
459         Glib::RefPtr<Gdk::Window> win (mma->get_window());
460         cairo_t* cr;
461
462         cr = gdk_cairo_create (win->gobj());
463
464         /* clip to expose area */
465
466         gdk_cairo_rectangle (cr, &ev->area);
467         cairo_clip (cr);
468
469         cairo_pattern_t* pattern;
470         MetricPatterns::iterator i = metric_patterns.find (mma->get_name());
471
472         if (i == metric_patterns.end()) {
473                 pattern = meter_render_metrics (*mma, types);
474                 metric_patterns[mma->get_name()] = pattern;
475         } else {
476                 pattern = i->second;
477         }
478
479         cairo_move_to (cr, 0, 0);
480         cairo_set_source (cr, pattern);
481
482         gint width, height;
483         win->get_size (width, height);
484
485         cairo_rectangle (cr, 0, 0, width, height);
486         cairo_fill (cr);
487
488         cairo_destroy (cr);
489
490         return true;
491 }
492
493 void
494 ArdourMeter::meter_clear_pattern_cache(int which) {
495         MetricPatterns::iterator i = metric_patterns.begin();
496         TickPatterns::iterator j = ticks_patterns.begin();
497
498         while (i != metric_patterns.end()) {
499                 int m = 4;
500                 std::string n = i->first;
501                 if (n.substr(n.length() - 4) == "Left")  { m = 1; }
502                 if (n.substr(n.length() - 5) == "Right") { m = 2; }
503                 if (which & m) {
504                         cairo_pattern_destroy(i->second);
505                         metric_patterns.erase(i++);
506                 } else {
507                         ++i;
508                 }
509         }
510
511         while (j != ticks_patterns.end()) {
512                 int m = 4;
513                 std::string n = j->first;
514                 if (n.substr(n.length() - 4) == "Left")  { m = 1; }
515                 if (n.substr(n.length() - 5) == "Right") { m = 2; }
516                 if (which & m) {
517                         cairo_pattern_destroy(j->second);
518                         ticks_patterns.erase(j++);
519                 } else {
520                         ++j;
521                 }
522         }
523         RedrawMetrics();
524 }