2 Copyright (C) 2013 Paul Davis
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.
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.
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.
20 #include <gtkmm2ext/cairo_widget.h>
21 #include <gtkmm2ext/gtk_ui.h>
22 #include <gtkmm2ext/utils.h>
23 #include <gtkmm2ext/rgb_macros.h>
25 #include <ardour/rc_configuration.h>
26 #include "ardour_ui.h"
29 #include "meter_patterns.h"
33 using namespace ARDOUR;
36 using namespace Gtkmm2ext;
38 using namespace ArdourMeter;
40 static const int max_pattern_metric_size = 1026;
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;
47 sigc::signal<void, int, ARDOUR::RouteGroup*, ARDOUR::MeterType> ArdourMeter::SetMeterTypeMulti;
49 namespace ArdourMeter {
50 typedef std::map<std::string,cairo_pattern_t*> TickPatterns;
51 typedef std::map<std::string,cairo_pattern_t*> MetricPatterns;
54 static ArdourMeter::TickPatterns ticks_patterns;
55 static ArdourMeter::MetricPatterns metric_patterns;
58 static cairo_pattern_t*
59 meter_render_ticks (Gtk::Widget& w, vector<ARDOUR::DataType> types)
61 Glib::RefPtr<Gdk::Window> win (w.get_window());
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";
70 cairo_surface_t* surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
71 cairo_t* cr = cairo_create (surface);
73 cairo_move_to (cr, 0, 0);
74 cairo_rectangle (cr, 0, 0, width, height);
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());
81 height = min(max_pattern_metric_size, height);
82 uint32_t peakcolor = ARDOUR_UI::config()->color_by_name ("meterbridge peaklabel");
84 for (vector<DataType>::const_iterator i = types.begin(); i != types.end(); ++i) {
87 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
89 if (types.size() > 1) {
90 /* we're overlaying more than 1 set of marks, so use different colours */
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());
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());
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());
106 std::map<int,float> points;
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));
117 points.insert (std::pair<int,float>(-25, 0.5));
119 points.insert (std::pair<int,float>(-20, 1.0));
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));
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));
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));
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));
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));
165 for (std::map<int,float>::const_iterator j = points.begin(); j != points.end(); ++j) {
166 cairo_set_line_width (cr, (j->second));
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));
179 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
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);
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);
198 cairo_pattern_t* pattern = cairo_pattern_create_for_surface (surface);
201 cairo_surface_destroy (surface);
207 static cairo_pattern_t*
208 meter_render_metrics (Gtk::Widget& w, vector<DataType> types)
210 Glib::RefPtr<Gdk::Window> win (w.get_window());
215 win->get_size (width, height);
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";
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());
224 Pango::AttrList audio_font_attributes;
225 Pango::AttrList midi_font_attributes;
226 Pango::AttrList unit_font_attributes;
228 Pango::AttrFontDesc* font_attr;
229 Pango::FontDescription font;
231 font = Pango::FontDescription (""); // use defaults
232 //font = get_font_for_style("gain-fader");
233 //font = w.get_style()->get_font();
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);
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);
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);
253 cairo_move_to (cr, 0, 0);
254 cairo_rectangle (cr, 0, 0, width, height);
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());
261 height = min(max_pattern_metric_size, height);
262 uint32_t peakcolor = ARDOUR_UI::config()->color_by_name ("meterbridge peaklabel");
264 for (vector<DataType>::const_iterator i = types.begin(); i != types.end(); ++i) {
268 if (types.size() > 1) {
269 /* we're overlaying more than 1 set of marks, so use different colours */
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());
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());
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());
286 std::map<int,float> points;
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));
299 points.insert (std::pair<int,float>(-25, 0.5));
301 points.insert (std::pair<int,float>(-15, 1.0));
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));
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));
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));
329 points.insert (std::pair<int,float>(127, 1.0));
336 for (std::map<int,float>::const_iterator j = points.begin(); j != points.end(); ++j) {
340 case DataType::AUDIO:
341 cairo_set_line_width (cr, (j->second));
343 cairo_set_source_rgb (cr,
344 UINT_RGBA_R_FLT(peakcolor),
345 UINT_RGBA_G_FLT(peakcolor),
346 UINT_RGBA_B_FLT(peakcolor));
348 fraction = log_meter (j->first);
349 snprintf (buf, sizeof (buf), "%+2d", j->first);
350 pos = height - (gint) floor (height * fraction);
352 cairo_move_to(cr, width-2.5, pos + .5);
353 cairo_line_to(cr, width, pos + .5);
355 cairo_move_to(cr, 0, pos + .5);
356 cairo_line_to(cr, 2.5, pos + .5);
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);
367 cairo_arc(cr, width - 2.0, pos + .5, 1.0, 0, 2 * M_PI);
369 cairo_arc(cr, 3, pos + .5, 1.0, 0, 2 * M_PI);
375 layout->set_text(buf);
377 /* we want logical extents, not ink extents here */
380 layout->get_pixel_size(tw, th);
382 int p = pos - (th / 2);
383 p = min (p, height - th);
386 cairo_move_to (cr, width-4-tw, p);
387 pango_cairo_show_layout (cr, layout->gobj());
391 if (types.size() == 1) {
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);
400 layout->set_text("vel");
401 layout->get_pixel_size(tw, th);
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());
410 cairo_pattern_t* pattern = cairo_pattern_create_for_surface (surface);
413 cairo_surface_destroy (surface);
420 ArdourMeter::meter_expose_ticks (GdkEventExpose *ev, std::vector<ARDOUR::DataType> types, Gtk::DrawingArea *mta)
422 Glib::RefPtr<Gdk::Window> win (mta->get_window());
425 cr = gdk_cairo_create (win->gobj());
427 /* clip to expose area */
429 gdk_cairo_rectangle (cr, &ev->area);
432 cairo_pattern_t* pattern;
433 TickPatterns::iterator i = ticks_patterns.find (mta->get_name());
435 if (i == ticks_patterns.end()) {
436 pattern = meter_render_ticks (*mta, types);
437 ticks_patterns[mta->get_name()] = pattern;
442 cairo_move_to (cr, 0, 0);
443 cairo_set_source (cr, pattern);
446 win->get_size (width, height);
448 cairo_rectangle (cr, 0, 0, width, height);
457 ArdourMeter::meter_expose_metrics (GdkEventExpose *ev, std::vector<ARDOUR::DataType> types, Gtk::DrawingArea *mma)
459 Glib::RefPtr<Gdk::Window> win (mma->get_window());
462 cr = gdk_cairo_create (win->gobj());
464 /* clip to expose area */
466 gdk_cairo_rectangle (cr, &ev->area);
469 cairo_pattern_t* pattern;
470 MetricPatterns::iterator i = metric_patterns.find (mma->get_name());
472 if (i == metric_patterns.end()) {
473 pattern = meter_render_metrics (*mma, types);
474 metric_patterns[mma->get_name()] = pattern;
479 cairo_move_to (cr, 0, 0);
480 cairo_set_source (cr, pattern);
483 win->get_size (width, height);
485 cairo_rectangle (cr, 0, 0, width, height);
494 ArdourMeter::meter_clear_pattern_cache(int which) {
495 MetricPatterns::iterator i = metric_patterns.begin();
496 TickPatterns::iterator j = ticks_patterns.begin();
498 while (i != metric_patterns.end()) {
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; }
504 cairo_pattern_destroy(i->second);
505 metric_patterns.erase(i++);
511 while (j != ticks_patterns.end()) {
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; }
517 cairo_pattern_destroy(j->second);
518 ticks_patterns.erase(j++);