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_ui.h"
28 #include "meter_patterns.h"
32 using namespace ARDOUR;
35 using namespace Gtkmm2ext;
39 static const int max_pattern_metric_size = 1026;
41 sigc::signal<void> ResetAllPeakDisplays;
42 sigc::signal<void,ARDOUR::RouteGroup*> ResetGroupPeakDisplays;
45 meter_render_ticks (Gtk::Widget& w, vector<ARDOUR::DataType> types)
47 Glib::RefPtr<Gdk::Window> win (w.get_window());
51 win->get_size (width, height);
53 w.get_name().substr(w.get_name().length() - 4) == "Left"
54 || w.get_name().substr(w.get_name().length() - 5) == "Right";
56 cairo_surface_t* surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
57 cairo_t* cr = cairo_create (surface);
59 cairo_move_to (cr, 0, 0);
60 cairo_rectangle (cr, 0, 0, width, height);
62 Gdk::Color c = w.get_style()->get_bg (background ? Gtk::STATE_ACTIVE : Gtk::STATE_NORMAL);
63 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
67 height = min(max_pattern_metric_size, height);
68 uint32_t peakcolor = ARDOUR_UI::config()->color_by_name ("meterbridge peaklabel");
70 for (vector<DataType>::const_iterator i = types.begin(); i != types.end(); ++i) {
73 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
75 if (types.size() > 1) {
76 /* we're overlaying more than 1 set of marks, so use different colours */
79 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
80 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
83 c = w.get_style()->get_fg (Gtk::STATE_ACTIVE);
84 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
88 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
89 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
92 std::map<int,float> points;
96 points.insert (std::pair<int,float>(-60, 0.5));
97 points.insert (std::pair<int,float>(-50, 0.5));
98 points.insert (std::pair<int,float>(-40, 0.5));
99 points.insert (std::pair<int,float>(-30, 0.5));
100 points.insert (std::pair<int,float>(-25, 0.5));
101 points.insert (std::pair<int,float>(-20, 1.0));
103 points.insert (std::pair<int,float>(-19, 0.5));
104 points.insert (std::pair<int,float>(-18, 1.0));
105 points.insert (std::pair<int,float>(-17, 0.5));
106 points.insert (std::pair<int,float>(-16, 0.5));
107 points.insert (std::pair<int,float>(-15, 1.0));
109 points.insert (std::pair<int,float>(-14, 0.5));
110 points.insert (std::pair<int,float>(-13, 0.5));
111 points.insert (std::pair<int,float>(-12, 0.5));
112 points.insert (std::pair<int,float>(-11, 0.5));
113 points.insert (std::pair<int,float>(-10, 1.0));
115 points.insert (std::pair<int,float>( -9, 0.5));
116 points.insert (std::pair<int,float>( -8, 0.5));
117 points.insert (std::pair<int,float>( -7, 0.5));
118 points.insert (std::pair<int,float>( -6, 0.5));
119 points.insert (std::pair<int,float>( -5, 1.0));
120 points.insert (std::pair<int,float>( -4, 0.5));
121 points.insert (std::pair<int,float>( -3, 0.5));
122 points.insert (std::pair<int,float>( -2, 0.5));
123 points.insert (std::pair<int,float>( -1, 0.5));
125 points.insert (std::pair<int,float>( 0, 1.0));
126 points.insert (std::pair<int,float>( 1, 0.5));
127 points.insert (std::pair<int,float>( 2, 0.5));
128 points.insert (std::pair<int,float>( 3, 0.5));
129 points.insert (std::pair<int,float>( 4, 0.5));
130 points.insert (std::pair<int,float>( 5, 0.5));
134 points.insert (std::pair<int,float>( 0, 1.0));
135 points.insert (std::pair<int,float>( 16, 0.5));
136 points.insert (std::pair<int,float>( 32, 0.5));
137 points.insert (std::pair<int,float>( 48, 0.5));
138 points.insert (std::pair<int,float>( 64, 1.0));
139 points.insert (std::pair<int,float>( 80, 0.5));
140 points.insert (std::pair<int,float>( 96, 0.5));
141 points.insert (std::pair<int,float>(100, 1.0));
142 points.insert (std::pair<int,float>(112, 0.5));
143 points.insert (std::pair<int,float>(127, 1.0));
147 for (std::map<int,float>::const_iterator j = points.begin(); j != points.end(); ++j) {
148 cairo_set_line_width (cr, (j->second));
154 case DataType::AUDIO:
155 if (j->first >= 0 || j->first == -9) {
156 cairo_set_source_rgb (cr,
157 UINT_RGBA_R_FLT(peakcolor),
158 UINT_RGBA_G_FLT(peakcolor),
159 UINT_RGBA_B_FLT(peakcolor));
161 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
163 fraction = log_meter (j->first);
164 pos = height - (gint) floor (height * fraction);
165 cairo_move_to(cr, 0, pos + .5);
166 cairo_line_to(cr, 3, pos + .5);
170 fraction = (j->first) / 127.0;
171 pos = 1 + height - (gint) floor (height * fraction);
172 pos = min (pos, height);
173 cairo_arc(cr, 1.5, pos + .5, 1.0, 0, 2 * M_PI);
180 cairo_pattern_t* pattern = cairo_pattern_create_for_surface (surface);
183 cairo_surface_destroy (surface);
190 meter_render_metrics (Gtk::Widget& w, vector<DataType> types)
192 Glib::RefPtr<Gdk::Window> win (w.get_window());
197 win->get_size (width, height);
199 tickleft = w.get_name().substr(w.get_name().length() - 4) == "Left";
200 background = tickleft || w.get_name().substr(w.get_name().length() - 5) == "Right";
202 cairo_surface_t* surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
203 cairo_t* cr = cairo_create (surface);
204 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create(w.get_pango_context());
206 Pango::AttrList audio_font_attributes;
207 Pango::AttrList midi_font_attributes;
208 Pango::AttrList unit_font_attributes;
210 Pango::AttrFontDesc* font_attr;
211 Pango::FontDescription font;
213 font = Pango::FontDescription (""); // use defaults
214 //font = get_font_for_style("gain-fader");
215 //font = w.get_style()->get_font();
217 font.set_weight (Pango::WEIGHT_NORMAL);
218 font.set_size (9.0 * PANGO_SCALE);
219 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
220 audio_font_attributes.change (*font_attr);
223 font.set_weight (Pango::WEIGHT_ULTRALIGHT);
224 font.set_stretch (Pango::STRETCH_ULTRA_CONDENSED);
225 font.set_size (7.5 * PANGO_SCALE);
226 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
227 midi_font_attributes.change (*font_attr);
230 font.set_size (7.0 * PANGO_SCALE);
231 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
232 unit_font_attributes.change (*font_attr);
235 cairo_move_to (cr, 0, 0);
236 cairo_rectangle (cr, 0, 0, width, height);
238 Gdk::Color c = w.get_style()->get_bg (background ? Gtk::STATE_ACTIVE : Gtk::STATE_NORMAL);
239 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
243 height = min(max_pattern_metric_size, height);
244 uint32_t peakcolor = ARDOUR_UI::config()->color_by_name ("meterbridge peaklabel");
246 for (vector<DataType>::const_iterator i = types.begin(); i != types.end(); ++i) {
250 if (types.size() > 1) {
251 /* we're overlaying more than 1 set of marks, so use different colours */
254 case DataType::AUDIO:
255 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
256 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
259 c = w.get_style()->get_fg (Gtk::STATE_ACTIVE);
260 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
264 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
265 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
268 std::map<int,float> points;
271 case DataType::AUDIO:
272 layout->set_attributes (audio_font_attributes);
273 points.insert (std::pair<int,float>(-50, 0.5));
274 points.insert (std::pair<int,float>(-40, 0.5));
275 points.insert (std::pair<int,float>(-30, 0.5));
276 points.insert (std::pair<int,float>(-20, 1.0));
277 if (types.size() == 1) {
278 points.insert (std::pair<int,float>(-25, 0.5));
279 points.insert (std::pair<int,float>(-15, 1.0));
281 points.insert (std::pair<int,float>(-18, 1.0));
282 points.insert (std::pair<int,float>(-10, 1.0));
283 points.insert (std::pair<int,float>( -5, 1.0));
284 points.insert (std::pair<int,float>( -3, 0.5));
285 points.insert (std::pair<int,float>( 0, 1.0));
286 points.insert (std::pair<int,float>( 3, 0.5));
290 layout->set_attributes (midi_font_attributes);
291 points.insert (std::pair<int,float>( 0, 1.0));
292 if (types.size() == 1) {
293 points.insert (std::pair<int,float>( 16, 0.5));
294 points.insert (std::pair<int,float>( 32, 0.5));
295 points.insert (std::pair<int,float>( 48, 0.5));
296 points.insert (std::pair<int,float>( 64, 1.0));
297 points.insert (std::pair<int,float>( 80, 0.5));
298 points.insert (std::pair<int,float>( 96, 0.5));
299 points.insert (std::pair<int,float>(100, 0.5));
300 points.insert (std::pair<int,float>(112, 0.5));
302 /* labels that don't overlay with dB */
303 points.insert (std::pair<int,float>( 24, 0.5));
304 points.insert (std::pair<int,float>( 48, 0.5));
305 points.insert (std::pair<int,float>( 72, 0.5));
307 points.insert (std::pair<int,float>(127, 1.0));
314 for (std::map<int,float>::const_iterator j = points.begin(); j != points.end(); ++j) {
318 case DataType::AUDIO:
319 cairo_set_line_width (cr, (j->second));
321 cairo_set_source_rgb (cr,
322 UINT_RGBA_R_FLT(peakcolor),
323 UINT_RGBA_G_FLT(peakcolor),
324 UINT_RGBA_B_FLT(peakcolor));
326 fraction = log_meter (j->first);
327 snprintf (buf, sizeof (buf), "%+2d", j->first);
328 pos = height - (gint) floor (height * fraction);
330 cairo_move_to(cr, width-2.5, pos + .5);
331 cairo_line_to(cr, width, pos + .5);
333 cairo_move_to(cr, 0, pos + .5);
334 cairo_line_to(cr, 2.5, pos + .5);
339 cairo_set_line_width (cr, 1.0);
340 fraction = (j->first) / 127.0;
341 snprintf (buf, sizeof (buf), "%3d", j->first);
342 pos = 1 + height - (gint) rintf (height * fraction);
343 pos = min (pos, height);
345 cairo_arc(cr, width - 2.0, pos + .5, 1.0, 0, 2 * M_PI);
347 cairo_arc(cr, 3, pos + .5, 1.0, 0, 2 * M_PI);
353 layout->set_text(buf);
355 /* we want logical extents, not ink extents here */
358 layout->get_pixel_size(tw, th);
360 int p = pos - (th / 2);
361 p = min (p, height - th);
364 cairo_move_to (cr, width-4-tw, p);
365 pango_cairo_show_layout (cr, layout->gobj());
369 if (types.size() == 1) {
371 layout->set_attributes (unit_font_attributes);
372 switch (types.at(0)) {
373 case DataType::AUDIO:
374 layout->set_text("dBFS");
375 layout->get_pixel_size(tw, th);
378 layout->set_text("vel");
379 layout->get_pixel_size(tw, th);
382 Gdk::Color c = w.get_style()->get_fg (Gtk::STATE_ACTIVE);
383 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
384 cairo_move_to (cr, 1, height - th - 1.5);
385 pango_cairo_show_layout (cr, layout->gobj());
388 cairo_pattern_t* pattern = cairo_pattern_create_for_surface (surface);
391 cairo_surface_destroy (surface);
397 typedef std::map<std::string,cairo_pattern_t*> TickPatterns;
398 static TickPatterns ticks_patterns;
400 gint meter_expose_ticks (GdkEventExpose *ev, std::vector<ARDOUR::DataType> types, Gtk::DrawingArea *mta)
402 Glib::RefPtr<Gdk::Window> win (mta->get_window());
405 cr = gdk_cairo_create (win->gobj());
407 /* clip to expose area */
409 gdk_cairo_rectangle (cr, &ev->area);
412 cairo_pattern_t* pattern;
413 TickPatterns::iterator i = ticks_patterns.find (mta->get_name());
415 if (i == ticks_patterns.end()) {
416 pattern = meter_render_ticks (*mta, types);
417 ticks_patterns[mta->get_name()] = pattern;
422 cairo_move_to (cr, 0, 0);
423 cairo_set_source (cr, pattern);
426 win->get_size (width, height);
428 cairo_rectangle (cr, 0, 0, width, height);
436 typedef std::map<std::string,cairo_pattern_t*> MetricPatterns;
437 static MetricPatterns metric_patterns;
439 gint meter_expose_metrics (GdkEventExpose *ev, std::vector<ARDOUR::DataType> types, Gtk::DrawingArea *mma)
441 Glib::RefPtr<Gdk::Window> win (mma->get_window());
444 cr = gdk_cairo_create (win->gobj());
446 /* clip to expose area */
448 gdk_cairo_rectangle (cr, &ev->area);
451 cairo_pattern_t* pattern;
452 MetricPatterns::iterator i = metric_patterns.find (mma->get_name());
454 if (i == metric_patterns.end()) {
455 pattern = meter_render_metrics (*mma, types);
456 metric_patterns[mma->get_name()] = pattern;
461 cairo_move_to (cr, 0, 0);
462 cairo_set_source (cr, pattern);
465 win->get_size (width, height);
467 cairo_rectangle (cr, 0, 0, width, height);
475 void meter_clear_pattern_cache() {
476 metric_patterns.clear();
477 ticks_patterns.clear();