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 ArdourMeter::meter_type_string (ARDOUR::MeterType mt)
65 return _("RMS + Peak");
82 static cairo_pattern_t*
83 meter_render_ticks (Gtk::Widget& w, vector<ARDOUR::DataType> types)
85 Glib::RefPtr<Gdk::Window> win (w.get_window());
89 win->get_size (width, height);
90 background = types.size() == 0
91 || w.get_name().substr(w.get_name().length() - 4) == "Left"
92 || w.get_name().substr(w.get_name().length() - 5) == "Right";
94 cairo_surface_t* surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
95 cairo_t* cr = cairo_create (surface);
97 cairo_move_to (cr, 0, 0);
98 cairo_rectangle (cr, 0, 0, width, height);
100 Gdk::Color c = w.get_style()->get_bg (background ? Gtk::STATE_ACTIVE : Gtk::STATE_NORMAL);
101 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
105 height = min(max_pattern_metric_size, height);
106 uint32_t peakcolor = ARDOUR_UI::config()->color_by_name ("meterbridge peaklabel");
108 for (vector<DataType>::const_iterator i = types.begin(); i != types.end(); ++i) {
111 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
113 if (types.size() > 1) {
114 /* we're overlaying more than 1 set of marks, so use different colours */
116 case DataType::AUDIO:
117 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
118 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
121 c = w.get_style()->get_fg (Gtk::STATE_ACTIVE);
122 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
126 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
127 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
130 std::map<int,float> points;
133 case DataType::AUDIO:
134 points.insert (std::pair<int,float>(-60, 0.5));
135 points.insert (std::pair<int,float>(-50, 0.5));
136 points.insert (std::pair<int,float>(-40, 0.5));
137 points.insert (std::pair<int,float>(-30, 0.5));
138 if (Config->get_meter_line_up_level() == MeteringLineUp24) {
139 points.insert (std::pair<int,float>(-24, 0.5));
141 points.insert (std::pair<int,float>(-25, 0.5));
143 points.insert (std::pair<int,float>(-20, 1.0));
145 points.insert (std::pair<int,float>(-19, 0.5));
146 points.insert (std::pair<int,float>(-18, 1.0));
147 points.insert (std::pair<int,float>(-17, 0.5));
148 points.insert (std::pair<int,float>(-16, 0.5));
149 points.insert (std::pair<int,float>(-15, 1.0));
151 points.insert (std::pair<int,float>(-14, 0.5));
152 points.insert (std::pair<int,float>(-13, 0.5));
153 points.insert (std::pair<int,float>(-12, 0.5));
154 points.insert (std::pair<int,float>(-11, 0.5));
155 points.insert (std::pair<int,float>(-10, 1.0));
157 points.insert (std::pair<int,float>( -9, 0.5));
158 points.insert (std::pair<int,float>( -8, 0.5));
159 points.insert (std::pair<int,float>( -7, 0.5));
160 points.insert (std::pair<int,float>( -6, 0.5));
161 points.insert (std::pair<int,float>( -5, 1.0));
162 points.insert (std::pair<int,float>( -4, 0.5));
163 points.insert (std::pair<int,float>( -3, 0.5));
164 points.insert (std::pair<int,float>( -2, 0.5));
165 points.insert (std::pair<int,float>( -1, 0.5));
167 points.insert (std::pair<int,float>( 0, 1.0));
168 points.insert (std::pair<int,float>( 1, 0.5));
169 points.insert (std::pair<int,float>( 2, 0.5));
170 points.insert (std::pair<int,float>( 3, 0.5));
171 points.insert (std::pair<int,float>( 4, 0.5));
172 points.insert (std::pair<int,float>( 5, 0.5));
176 points.insert (std::pair<int,float>( 0, 1.0));
177 points.insert (std::pair<int,float>( 16, 0.5));
178 points.insert (std::pair<int,float>( 32, 0.5));
179 points.insert (std::pair<int,float>( 48, 0.5));
180 points.insert (std::pair<int,float>( 64, 1.0));
181 points.insert (std::pair<int,float>( 80, 0.5));
182 points.insert (std::pair<int,float>( 96, 0.5));
183 points.insert (std::pair<int,float>(100, 1.0));
184 points.insert (std::pair<int,float>(112, 0.5));
185 points.insert (std::pair<int,float>(127, 1.0));
189 for (std::map<int,float>::const_iterator j = points.begin(); j != points.end(); ++j) {
190 cairo_set_line_width (cr, (j->second));
196 case DataType::AUDIO:
197 if (j->first >= 0 || j->first == -9) {
198 cairo_set_source_rgb (cr,
199 UINT_RGBA_R_FLT(peakcolor),
200 UINT_RGBA_G_FLT(peakcolor),
201 UINT_RGBA_B_FLT(peakcolor));
203 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
205 fraction = log_meter (j->first);
206 pos = height - (gint) floor (height * fraction);
207 cairo_move_to(cr, 0, pos + .5);
208 cairo_line_to(cr, 3, pos + .5);
212 fraction = (j->first) / 127.0;
213 pos = 1 + height - (gint) floor (height * fraction);
214 pos = min (pos, height);
215 cairo_arc(cr, 1.5, pos + .5, 1.0, 0, 2 * M_PI);
222 cairo_pattern_t* pattern = cairo_pattern_create_for_surface (surface);
225 cairo_surface_destroy (surface);
231 static cairo_pattern_t*
232 meter_render_metrics (Gtk::Widget& w, vector<DataType> types)
234 Glib::RefPtr<Gdk::Window> win (w.get_window());
236 bool tickleft, tickright;
239 win->get_size (width, height);
241 tickleft = w.get_name().substr(w.get_name().length() - 4) == "Left";
242 tickright = w.get_name().substr(w.get_name().length() - 5) == "Right";
243 background = types.size() == 0 || tickleft || tickright;
245 if (!tickleft && !tickright) {
249 cairo_surface_t* surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
250 cairo_t* cr = cairo_create (surface);
251 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create(w.get_pango_context());
253 Pango::AttrList audio_font_attributes;
254 Pango::AttrList midi_font_attributes;
255 Pango::AttrList unit_font_attributes;
257 Pango::AttrFontDesc* font_attr;
258 Pango::FontDescription font;
260 font = Pango::FontDescription ("ArdourMono");
261 double fixfontsize = 81920.0 / (double) ARDOUR::Config->get_font_scale();
263 font.set_weight (Pango::WEIGHT_NORMAL);
264 font.set_size (9.0 * PANGO_SCALE * fixfontsize);
265 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
266 audio_font_attributes.change (*font_attr);
269 font.set_weight (Pango::WEIGHT_ULTRALIGHT);
270 font.set_stretch (Pango::STRETCH_ULTRA_CONDENSED);
271 font.set_size (8.0 * PANGO_SCALE * fixfontsize);
272 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
273 midi_font_attributes.change (*font_attr);
276 font.set_size (6.0 * PANGO_SCALE * fixfontsize);
277 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
278 unit_font_attributes.change (*font_attr);
281 cairo_move_to (cr, 0, 0);
282 cairo_rectangle (cr, 0, 0, width, height);
284 Gdk::Color c = w.get_style()->get_bg (background ? Gtk::STATE_ACTIVE : Gtk::STATE_NORMAL);
285 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
289 height = min(max_pattern_metric_size, height);
290 uint32_t peakcolor = ARDOUR_UI::config()->color_by_name ("meterbridge peaklabel");
292 for (vector<DataType>::const_iterator i = types.begin(); i != types.end(); ++i) {
296 if (types.size() > 1) {
297 /* we're overlaying more than 1 set of marks, so use different colours */
300 case DataType::AUDIO:
301 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
302 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
305 c = w.get_style()->get_fg (Gtk::STATE_ACTIVE);
306 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
310 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
311 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
314 std::map<int,float> points;
317 case DataType::AUDIO:
318 layout->set_attributes (audio_font_attributes);
319 points.insert (std::pair<int,float>(-50, 0.5));
320 points.insert (std::pair<int,float>(-40, 0.5));
321 points.insert (std::pair<int,float>(-30, 0.5));
322 points.insert (std::pair<int,float>(-20, 1.0));
323 if (types.size() == 1) {
324 if (Config->get_meter_line_up_level() == MeteringLineUp24) {
325 points.insert (std::pair<int,float>(-24, 0.5));
327 points.insert (std::pair<int,float>(-25, 0.5));
329 points.insert (std::pair<int,float>(-15, 1.0));
331 points.insert (std::pair<int,float>(-18, 1.0));
332 points.insert (std::pair<int,float>(-10, 1.0));
333 points.insert (std::pair<int,float>( -5, 1.0));
334 points.insert (std::pair<int,float>( -3, 0.5));
335 points.insert (std::pair<int,float>( 0, 1.0));
336 points.insert (std::pair<int,float>( 3, 0.5));
340 layout->set_attributes (midi_font_attributes);
341 points.insert (std::pair<int,float>( 0, 1.0));
342 if (types.size() == 1) {
343 points.insert (std::pair<int,float>( 16, 0.5));
344 points.insert (std::pair<int,float>( 32, 0.5));
345 points.insert (std::pair<int,float>( 48, 0.5));
346 points.insert (std::pair<int,float>( 64, 1.0));
347 points.insert (std::pair<int,float>( 80, 0.5));
348 points.insert (std::pair<int,float>( 96, 0.5));
349 points.insert (std::pair<int,float>(100, 0.5));
350 points.insert (std::pair<int,float>(112, 0.5));
352 /* labels that don't overlay with dB */
353 points.insert (std::pair<int,float>( 24, 0.5));
354 points.insert (std::pair<int,float>( 48, 0.5));
355 points.insert (std::pair<int,float>( 72, 0.5));
357 points.insert (std::pair<int,float>(127, 1.0));
364 for (std::map<int,float>::const_iterator j = points.begin(); j != points.end(); ++j) {
368 case DataType::AUDIO:
369 cairo_set_line_width (cr, (j->second));
371 cairo_set_source_rgb (cr,
372 UINT_RGBA_R_FLT(peakcolor),
373 UINT_RGBA_G_FLT(peakcolor),
374 UINT_RGBA_B_FLT(peakcolor));
376 fraction = log_meter (j->first);
377 snprintf (buf, sizeof (buf), "%+2d", j->first);
378 pos = height - (gint) floor (height * fraction);
380 cairo_move_to(cr, width-1.5, pos + .5);
381 cairo_line_to(cr, width, pos + .5);
383 } else if (tickright) {
384 cairo_move_to(cr, 0, pos + .5);
385 cairo_line_to(cr, 1.5, pos + .5);
390 cairo_set_line_width (cr, 1.0);
391 fraction = (j->first) / 127.0;
392 snprintf (buf, sizeof (buf), "%3d", j->first);
393 pos = 1 + height - (gint) rintf (height * fraction);
394 pos = min (pos, height);
396 cairo_arc(cr, width - 2.0, pos + .5, 1.0, 0, 2 * M_PI);
398 } else if (tickright) {
399 cairo_arc(cr, 3, pos + .5, 1.0, 0, 2 * M_PI);
405 layout->set_text(buf);
407 /* we want logical extents, not ink extents here */
410 layout->get_pixel_size(tw, th);
412 int p = pos - (th / 2) - 1;
413 p = min (p, height - th);
416 cairo_move_to (cr, width-3-tw, p);
417 pango_cairo_show_layout (cr, layout->gobj());
421 if (types.size() == 1) {
423 layout->set_attributes (unit_font_attributes);
424 switch (types.at(0)) {
425 case DataType::AUDIO:
426 layout->set_text("dBFS");
427 layout->get_pixel_size(tw, th);
430 layout->set_text("vel");
431 layout->get_pixel_size(tw, th);
434 Gdk::Color c = w.get_style()->get_fg (Gtk::STATE_ACTIVE);
435 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
436 cairo_move_to (cr, 2, height - th - 1.5);
437 pango_cairo_show_layout (cr, layout->gobj());
440 cairo_pattern_t* pattern = cairo_pattern_create_for_surface (surface);
443 cairo_surface_destroy (surface);
450 ArdourMeter::meter_expose_ticks (GdkEventExpose *ev, std::vector<ARDOUR::DataType> types, Gtk::DrawingArea *mta)
452 Glib::RefPtr<Gdk::Window> win (mta->get_window());
455 cr = gdk_cairo_create (win->gobj());
457 /* clip to expose area */
459 gdk_cairo_rectangle (cr, &ev->area);
462 cairo_pattern_t* pattern;
463 TickPatterns::iterator i = ticks_patterns.find (mta->get_name());
465 if (i == ticks_patterns.end()) {
466 pattern = meter_render_ticks (*mta, types);
467 ticks_patterns[mta->get_name()] = pattern;
472 cairo_move_to (cr, 0, 0);
473 cairo_set_source (cr, pattern);
476 win->get_size (width, height);
478 cairo_rectangle (cr, 0, 0, width, height);
487 ArdourMeter::meter_expose_metrics (GdkEventExpose *ev, std::vector<ARDOUR::DataType> types, Gtk::DrawingArea *mma)
489 Glib::RefPtr<Gdk::Window> win (mma->get_window());
492 cr = gdk_cairo_create (win->gobj());
494 /* clip to expose area */
496 gdk_cairo_rectangle (cr, &ev->area);
499 cairo_pattern_t* pattern;
500 MetricPatterns::iterator i = metric_patterns.find (mma->get_name());
502 if (i == metric_patterns.end()) {
503 pattern = meter_render_metrics (*mma, types);
504 metric_patterns[mma->get_name()] = pattern;
509 cairo_move_to (cr, 0, 0);
510 cairo_set_source (cr, pattern);
513 win->get_size (width, height);
515 cairo_rectangle (cr, 0, 0, width, height);
524 ArdourMeter::meter_clear_pattern_cache(int which) {
525 MetricPatterns::iterator i = metric_patterns.begin();
526 TickPatterns::iterator j = ticks_patterns.begin();
528 while (i != metric_patterns.end()) {
530 std::string n = i->first;
531 if (n.substr(n.length() - 4) == "Left") { m = 1; }
532 if (n.substr(n.length() - 5) == "Right") { m = 2; }
534 cairo_pattern_destroy(i->second);
535 metric_patterns.erase(i++);
541 while (j != ticks_patterns.end()) {
543 std::string n = j->first;
544 if (n.substr(n.length() - 4) == "Left") { m = 1; }
545 if (n.substr(n.length() - 5) == "Right") { m = 2; }
547 cairo_pattern_destroy(j->second);
548 ticks_patterns.erase(j++);