fix out-of-bounds array access in export report.
[ardour.git] / gtk2_ardour / export_report.cc
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #include <algorithm>
20
21 #include <pangomm/layout.h>
22 #include <gtkmm/label.h>
23 #include <gtkmm/table.h>
24 #include <gtkmm/stock.h>
25
26 #include "pbd/openuri.h"
27 #include "gtkmm2ext/utils.h"
28 #include "gtkmm2ext/utils.h"
29 #include "canvas/utils.h"
30 #include "canvas/colors.h"
31
32 #include "audiographer/general/analyser.h"
33
34 #include "ardour/audiofilesource.h"
35 #include "ardour/audioregion.h"
36 #include "ardour/auditioner.h"
37 #include "ardour/dB.h"
38 #include "ardour/region_factory.h"
39 #include "ardour/session.h"
40 #include "ardour/smf_source.h"
41 #include "ardour/source_factory.h"
42 #include "ardour/srcfilesource.h"
43
44 #include "audio_clock.h"
45 #include "export_report.h"
46 #include "logmeter.h"
47 #include "ui_config.h"
48
49 #include "i18n.h"
50
51 using namespace Gtk;
52 using namespace ARDOUR;
53
54 ExportReport::ExportReport (Session* session, StatusPtr s)
55         : ArdourDialog (_("Export Report/Analysis"))
56         , _session (session)
57         , stop_btn (0)
58         , play_btn (0)
59         , _audition_num (-1)
60         , _page_num (0)
61 {
62         init (s->result_map, true);
63 }
64
65 ExportReport::ExportReport (const std::string & title, const AnalysisResults & ar)
66         : ArdourDialog (title)
67         , _session (0)
68         , stop_btn (0)
69         , play_btn (0)
70         , _audition_num (-1)
71         , _page_num (0)
72 {
73         init (ar, false);
74 }
75
76 void
77 ExportReport::init (const AnalysisResults & ar, bool with_file)
78 {
79         set_resizable (false);
80         pages.set_scrollable ();
81
82         std::vector<double> dashes;
83         dashes.push_back (3.0);
84         dashes.push_back (5.0);
85
86         int page = 0;
87         for (AnalysisResults::const_iterator i = ar.begin (); i != ar.end (); ++i, ++page) {
88                 Label *l;
89                 VBox *vb = manage (new VBox ());
90                 Table *t = manage (new Table (4, 4));
91                 Table *wtbl = manage (new Table (3, 2));
92                 int wrow = 0;
93                 t->set_border_width (0);
94                 t->set_spacings (4);
95                 wtbl->set_spacings (4);
96                 vb->set_spacing (4);
97                 vb->set_border_width (4);
98                 vb->pack_start (*t, false, false, 2);
99                 vb->pack_start (*wtbl, false, false, 2);
100
101                 std::string path = i->first;
102                 ExportAnalysisPtr p = i->second;
103
104                 std::list<CimgPlayheadArea*> playhead_widgets;
105
106                 if (with_file) {
107                         l = manage (new Label (_("File:"), ALIGN_END));
108                         t->attach (*l, 0, 1, 0, 1);
109                         l = manage (new Label ());
110                         l->set_ellipsize (Pango::ELLIPSIZE_START);
111                         l->set_width_chars (48);
112                         l->set_max_width_chars (48);
113                         l->set_text (path);
114                         l->set_alignment (ALIGN_START, ALIGN_CENTER);
115                         t->attach (*l, 1, 3, 0, 1, FILL, SHRINK);
116
117                         Button *b = manage (new Button (_("Open Folder")));
118                         t->attach (*b, 3, 4, 0, 2, FILL, SHRINK);
119                         b->signal_clicked ().connect (sigc::bind (sigc::mem_fun (*this, &ExportReport::open_folder), path));
120                 }
121
122                 SoundFileInfo info;
123                 std::string errmsg;
124
125                 framecnt_t file_length = 0;
126                 framecnt_t sample_rate = 0;
127                 framecnt_t start_off = 0;
128                 unsigned int channels = 0;
129
130                 if (with_file && AudioFileSource::get_soundfile_info (path, info, errmsg)) {
131                         AudioClock * clock;
132
133                         file_length = info.length;
134                         sample_rate = info.samplerate;
135                         start_off = info.timecode;
136                         channels = info.channels;
137
138                         files.insert (std::make_pair (page, AuditionInfo (path, channels)));
139
140                         /* File Info Table */
141
142                         framecnt_t const nfr = _session ? _session->nominal_frame_rate () : 25;
143                         double src_coef = (double) nfr / info.samplerate;
144
145                         l = manage (new Label (_("Format:"), ALIGN_END));
146                         t->attach (*l, 0, 1, 1, 2);
147                         std::string fmt = info.format_name;
148                         std::replace (fmt.begin (), fmt.end (), '\n', ' ');
149                         l = manage (new Label ());
150                         l->set_ellipsize (Pango::ELLIPSIZE_START);
151                         l->set_width_chars (48);
152                         l->set_max_width_chars (48);
153                         l->set_text (fmt);
154                         l->set_alignment (ALIGN_START, ALIGN_CENTER);
155                         t->attach (*l, 1, 3, 1, 2, FILL, SHRINK);
156
157                         l = manage (new Label (_("Channels:"), ALIGN_END));
158                         t->attach (*l, 0, 1, 2, 3);
159                         l = manage (new Label (string_compose ("%1", info.channels), ALIGN_START));
160                         t->attach (*l, 1, 2, 2, 3);
161
162                         l = manage (new Label (_("Sample rate:"), ALIGN_END));
163                         t->attach (*l, 0, 1, 3, 4);
164                         l = manage (new Label (string_compose (_("%1 Hz"), info.samplerate), ALIGN_START));
165                         t->attach (*l, 1, 2, 3, 4);
166
167                         l = manage (new Label (_("Duration:"), ALIGN_END));
168                         t->attach (*l, 2, 3, 2, 3);
169                         clock = manage (new AudioClock ("sfboxLengthClock", true, "", false, false, true, false));
170                         clock->set_session (_session);
171                         clock->set_mode (AudioClock::MinSec);
172                         clock->set (info.length * src_coef + 0.5, true);
173                         t->attach (*clock, 3, 4, 2, 3);
174
175                         l = manage (new Label (_("Timecode:"), ALIGN_END));
176                         t->attach (*l, 2, 3, 3, 4);
177                         clock = manage (new AudioClock ("sfboxTimecodeClock", true, "", false, false, false, false));
178                         clock->set_session (_session);
179                         clock->set_mode (AudioClock::Timecode);
180                         clock->set (info.timecode * src_coef + 0.5, true);
181                         t->attach (*clock, 3, 4, 3, 4);
182                 } else if (with_file) {
183                         l = manage (new Label (_("Error:"), ALIGN_END));
184                         t->attach (*l, 0, 1, 1, 2);
185                         l = manage (new Label (errmsg, ALIGN_START));
186                         t->attach (*l, 1, 4, 1, 2);
187                 }
188
189                 int w, h;
190                 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (get_pango_context ());
191
192                 // calc geometry of numerics
193                 const float dbfs = accurate_coefficient_to_dB (p->peak);
194                 const float dbtp = accurate_coefficient_to_dB (p->truepeak);
195
196 #define TXTSIZE(LINE, TXT, FONT) {                                     \
197   layout->set_font_description (UIConfiguration::instance ().FONT ()); \
198   layout->set_text (TXT);                                              \
199   layout->get_pixel_size (w, h);                                       \
200   if (w > mnw) { mnw = w; }                                            \
201   if (h > lin[LINE]) { lin[LINE] = h; }                                \
202 }
203
204 #define TXTWIDTH(TXT, FONT) {                                          \
205   layout->set_font_description (UIConfiguration::instance ().FONT ()); \
206   layout->set_text (TXT);                                              \
207   layout->get_pixel_size (w, h);                                       \
208   if (w > mml) { mml = w; }                                            \
209 }
210
211                 int m_r = 0; // right side
212                 int mnh = 0; // mono height
213                 int mnw = 0; // max numeric width
214                 int anw = 0; // spectrum annotation text width
215
216                 int lin[6] = { 0, 0, 0, 0, 0, 0 }; // max line height
217
218                 TXTSIZE(0, _("(too short integration time)"), get_SmallFont);
219
220                 TXTSIZE(0, _("-888"), get_SmallMonospaceFont);
221                 anw = w;
222                 m_r = anw + 10;
223                 mnh = h + 1;
224
225                 TXTSIZE(0, _("Peak:"), get_SmallFont);
226                 TXTSIZE(1, string_compose (_("%1 dBFS"), std::setprecision (1), std::fixed, dbfs), get_LargeFont);
227                 TXTSIZE(2, _("True Peak:"), get_SmallFont);
228                 TXTSIZE(3, string_compose (_("%1 dBTP"), std::setprecision (1), std::fixed, dbtp), get_LargeFont);
229                 TXTSIZE(4, _("Normalization Gain:"), get_SmallFont);
230                 TXTSIZE(5, _("+888.88 dB"), get_SmallMonospaceFont);
231
232                 TXTSIZE(0, _("Integrated Loudness:"), get_SmallFont);
233                 TXTSIZE(1, string_compose (_("%1 LUFS"), std::setprecision (1), std::fixed, p->loudness), get_LargeFont);
234                 TXTSIZE(2, _("Loudness Range:"), get_SmallFont);
235                 TXTSIZE(3, string_compose (_("%1 LU"), std::setprecision (1), std::fixed, p->loudness_range), get_LargeFont);
236
237                 mnw += 8;
238                 const int ht = lin[0] * 1.25 + lin[1] * 1.25 + lin[2] * 1.25 + lin[3] *1.25 + lin[4] * 1.25 + lin[5];
239                 const int hh = std::max (100, ht + 12);
240                 const int htn = lin[0] * 1.25 + lin[1] * 1.25 + lin[2] * 1.25 + lin[3];
241                 int m_l =  2 * mnw + /*hist-width*/ 540 + /*box spacing*/ 8 - /*peak-width*/ 800 - m_r; // margin left
242
243                 int mml = 0; // min margin left -- ensure left margin is wide enough
244                 TXTWIDTH (_("Time"), get_SmallFont);
245                 TXTWIDTH (_("100"), get_SmallMonospaceFont);
246                 m_l = (std::max(anw + mnh + 14, std::max (m_l, mml + 8)) + 3) & ~3;
247
248                 mnw = (m_l - /*hist-width*/ 540 - /*box spacing*/ 8 + /*peak-width*/ 800 + m_r) / 2;
249                 const int nw2 = mnw / 2; // nums, horizontal center
250
251                 int y0[6];
252                 if (p->normalized) {
253                         y0[0] = (hh - ht) * .5;
254                 } else {
255                         y0[0] = (hh - htn) * .5;
256                 }
257                 y0[1] = y0[0] + lin[0] * 1.25;
258                 y0[2] = y0[1] + lin[1] * 1.25;
259                 y0[3] = y0[2] + lin[2] * 1.25;
260                 y0[4] = y0[3] + lin[3] * 1.25;
261                 y0[5] = y0[4] + lin[4] * 1.25;
262
263
264                 { /* peak, loudness and R128 histogram */
265                         Cairo::RefPtr<Cairo::ImageSurface> nums = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, mnw, hh);
266                         Cairo::RefPtr<Cairo::ImageSurface> ebur = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, mnw, hh);
267                         Cairo::RefPtr<Cairo::ImageSurface> hist = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, 540, hh);
268
269                         /* peak and true-peak numerics */
270                         Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create (nums);
271                         cr->set_source_rgba (0, 0, 0, 1.0);
272                         cr->paint ();
273
274                         layout->set_font_description (UIConfiguration::instance ().get_SmallFont ());
275                         layout->set_alignment (Pango::ALIGN_LEFT);
276                         layout->set_text (_("Peak:"));
277                         layout->get_pixel_size (w, h);
278                         cr->move_to (rint (nw2 - w * .5), y0[0]);
279                         cr->set_source_rgba (.7, .7, .7, 1.0);
280                         layout->show_in_cairo_context (cr);
281
282                         layout->set_font_description (UIConfiguration::instance ().get_LargeFont ());
283                         layout->set_text (string_compose (_("%1 dBFS"), std::setprecision (1), std::fixed, dbfs));
284                         layout->get_pixel_size (w, h);
285                         cr->move_to (rint (nw2 - w * .5), y0[1]);
286                         if (dbfs >= 0.f) { cr->set_source_rgba (1.0, .1, .1, 1.0); }
287                         else if (dbfs > -1.f) { cr->set_source_rgba (1.0, .7, .0, 1.0); }
288                         layout->show_in_cairo_context (cr);
289
290                         if (p->have_dbtp) {
291                                 layout->set_font_description (UIConfiguration::instance ().get_SmallFont ());
292                                 layout->set_text (_("True Peak:"));
293                                 layout->get_pixel_size (w, h);
294                                 cr->move_to (rint (nw2 - w * .5), y0[2]);
295                                 cr->set_source_rgba (.7, .7, .7, 1.0);
296                                 layout->show_in_cairo_context (cr);
297
298                                 layout->set_font_description (UIConfiguration::instance ().get_LargeFont ());
299                                 layout->set_text (string_compose (_("%1 dBTP"), std::setprecision (1), std::fixed, dbtp));
300                                 layout->get_pixel_size (w, h);
301                                 cr->move_to (rint (nw2 - w * .5), y0[3]);
302                                 if (dbtp >= 0.f) { cr->set_source_rgba (1.0, .1, .1, 1.0); }
303                                 else if (dbtp > -1.f) { cr->set_source_rgba (1.0, .7, .0, 1.0); }
304                                 layout->show_in_cairo_context (cr);
305                         }
306
307                         if (p->normalized) {
308                                 const float ndb = accurate_coefficient_to_dB (p->norm_gain_factor);
309                                 layout->set_font_description (UIConfiguration::instance ().get_SmallFont ());
310                                 layout->set_text (_("Normalization Gain:"));
311                                 layout->get_pixel_size (w, h);
312                                 cr->move_to (rint (nw2 - w * .5), y0[4]);
313                                 cr->set_source_rgba (.7, .7, .7, 1.0);
314                                 layout->show_in_cairo_context (cr);
315
316                                 layout->set_font_description (UIConfiguration::instance ().get_SmallMonospaceFont ());
317                                 layout->set_text (string_compose (_("%1 dB"), std::setprecision (2), std::showpos, std::fixed, ndb));
318
319                                 layout->get_pixel_size (w, h);
320                                 cr->move_to (rint (nw2 - w * .5), y0[5]);
321                                 // TODO tweak thresholds
322                                 if (p->norm_gain_factor < 1.0) {
323                                         cr->set_source_rgba (1.0, .7, .1, 1.0);
324                                 } else if (p->norm_gain_factor == 1.0) {
325                                         cr->set_source_rgba (.7, .7, .7, 1.0);
326                                 } else if (fabsf (ndb) < 12) {
327                                         cr->set_source_rgba (.1, 1.0, .1, 1.0);
328                                 } else if (fabsf (ndb) < 18) {
329                                         cr->set_source_rgba (1.0, .7, .1, 1.0);
330                                 } else {
331                                         cr->set_source_rgba (1.0, .1, .1, 1.0);
332                                 }
333                                 layout->show_in_cairo_context (cr);
334                         }
335
336                         nums->flush ();
337
338                         /* EBU R128 numerics */
339                         cr = Cairo::Context::create (ebur);
340                         cr->set_source_rgba (0, 0, 0, 1.0);
341                         cr->paint ();
342
343                         cr->set_source_rgba (.7, .7, .7, 1.0);
344
345                         if (!i->second->have_loudness) {
346                                 layout->set_alignment (Pango::ALIGN_CENTER);
347                                 layout->set_font_description (UIConfiguration::instance ().get_LargeFont ());
348                                 layout->set_text (_("Not\nAvailable"));
349                                 layout->get_pixel_size (w, h);
350                                 cr->move_to (rint (nw2 - w * .5), rint ((hh - h) * .5));
351                                 layout->show_in_cairo_context (cr);
352                         }
353                         else if (p->loudness == -200 && p->loudness_range == 0) {
354                                 layout->set_alignment (Pango::ALIGN_CENTER);
355                                 layout->set_font_description (UIConfiguration::instance ().get_LargeFont ());
356                                 layout->set_text (_("Not\nAvailable"));
357                                 layout->get_pixel_size (w, h);
358                                 cr->move_to (rint (nw2 - w * .5), rint (hh * .5 - h * .66));
359                                 layout->show_in_cairo_context (cr);
360                                 int yy = h * .5;
361
362                                 layout->set_font_description (UIConfiguration::instance ().get_SmallFont ());
363                                 layout->set_text (_("(too short integration time)"));
364                                 layout->get_pixel_size (w, h);
365                                 cr->move_to (rint (nw2 - w * .5), rint (hh * .5 + yy));
366                                 layout->show_in_cairo_context (cr);
367
368                         } else {
369                                 layout->set_font_description (UIConfiguration::instance ().get_SmallFont ());
370                                 layout->set_alignment (Pango::ALIGN_LEFT);
371                                 layout->set_text (_("Integrated Loudness:"));
372                                 layout->get_pixel_size (w, h);
373                                 cr->move_to (rint (nw2 - w * .5), y0[0]);
374                                 layout->show_in_cairo_context (cr);
375
376                                 layout->set_font_description (UIConfiguration::instance ().get_LargeFont ());
377                                 layout->set_text (string_compose (_("%1 LUFS"), std::setprecision (1), std::fixed,  p->loudness));
378                                 layout->get_pixel_size (w, h);
379                                 cr->move_to (rint (nw2 - w * .5), y0[1]);
380                                 layout->show_in_cairo_context (cr);
381
382                                 layout->set_font_description (UIConfiguration::instance ().get_SmallFont ());
383                                 layout->set_text (_("Loudness Range:"));
384                                 layout->get_pixel_size (w, h);
385                                 cr->move_to (rint (nw2 - w * .5), y0[2]);
386                                 layout->show_in_cairo_context (cr);
387
388                                 layout->set_font_description (UIConfiguration::instance ().get_LargeFont ());
389                                 layout->set_text (string_compose (_("%1 LU"), std::setprecision (1), std::fixed, p->loudness_range));
390                                 layout->get_pixel_size (w, h);
391                                 cr->move_to (rint (nw2 - w * .5), y0[3]);
392                                 layout->show_in_cairo_context (cr);
393                         }
394                         ebur->flush ();
395
396                         /* draw loudness histogram */
397                         cr = Cairo::Context::create (hist);
398                         cr->set_source_rgba (0, 0, 0, 1.0);
399                         cr->paint ();
400
401                         cr->set_source_rgba (.7, .7, .7, 1.0);
402                         cr->set_line_width (1.0);
403
404                         if (p->loudness_hist_max > 0 && i->second->have_loudness) {
405                                 // draw data
406                                 for (size_t x = 0 ; x < 510; ++x) {
407                                         cr->move_to (x - .5, hh);
408                                         cr->line_to (x - .5, (float) hh * (1.0 - p->loudness_hist[x] / (float) p->loudness_hist_max));
409                                         cr->stroke ();
410                                 }
411
412                                 layout->set_font_description (UIConfiguration::instance ().get_SmallerFont ());
413                                 layout->set_alignment (Pango::ALIGN_CENTER);
414
415                                 // x-axis label
416                                 layout->set_text (_("LUFS\n(short)"));
417                                 layout->get_pixel_size (w, h);
418                                 Gtkmm2ext::rounded_rectangle (cr, 5, 5, w + 2, h + 2, 4);
419                                 cr->set_source_rgba (.1, .1, .1, 0.7);
420                                 cr->fill ();
421                                 cr->move_to (6, 6);
422                                 cr->set_source_rgba (.9, .9, .9, 1.0);
423                                 layout->show_in_cairo_context (cr);
424
425                                 // y-axis label
426                                 layout->set_text (_("Multiplicity"));
427                                 layout->get_pixel_size (w, h);
428                                 Gtkmm2ext::rounded_rectangle (cr, 5, hh - w - 3, h + 2, w + 2, 4);
429                                 cr->set_source_rgba (.1, .1, .1, 0.7);
430                                 cr->fill ();
431                                 cr->save ();
432                                 cr->move_to (6, hh - 2);
433                                 cr->set_source_rgba (.9, .9, .9, 1.0);
434                                 cr->rotate (M_PI / -2.0);
435                                 layout->show_in_cairo_context (cr);
436                                 cr->restore ();
437
438                                 // x-Axis labels
439                                 layout->set_font_description (UIConfiguration::instance ().get_SmallMonospaceFont ());
440                                 layout->set_alignment (Pango::ALIGN_LEFT);
441                                 for (int g = -53; g <= -8; g += 5) {
442                                         // grid-lines. [110] -59LUFS .. [650]: -5 LUFS
443                                         layout->set_text (string_compose ("%1", std::setw(3), std::setfill(' '), g));
444                                         layout->get_pixel_size (w, h);
445
446                                         cr->set_operator (Cairo::OPERATOR_OVER);
447                                         Gtkmm2ext::rounded_rectangle (cr,
448                                                         rint ((g + 59.0) * 10.0 - h * .5), 5,
449                                                         h + 2, w + 2, 4);
450                                         const float pk = (g + 59.0) / 54.0;
451                                         ArdourCanvas::Color c = ArdourCanvas::hsva_to_color (252 - 260 * pk, .9, .3 + pk * .4, .6);
452                                         ArdourCanvas::set_source_rgba (cr, c);
453                                         cr->fill ();
454
455                                         cr->save ();
456                                         cr->set_source_rgba (.9, .9, .9, 1.0);
457                                         cr->move_to (rint ((g + 59.0) * 10.0 - h * .5), w + 6.0);
458                                         cr->rotate (M_PI / -2.0);
459                                         layout->show_in_cairo_context (cr);
460                                         cr->restore ();
461
462                                         cr->set_operator (Cairo::OPERATOR_ADD);
463                                         cr->save ();
464                                         cr->set_source_rgba (.3, .3, .3, 1.0);
465                                         cr->set_dash (dashes, 1.0);
466                                         cr->set_line_cap (Cairo::LINE_CAP_ROUND);
467                                         cr->move_to (rint ((g + 59.0) * 10.0) + .5, w + 8.0);
468                                         cr->line_to (rint ((g + 59.0) * 10.0) + .5, hh);
469                                         cr->stroke ();
470                                         cr->restore ();
471                                 }
472
473                         } else {
474                                 layout->set_alignment (Pango::ALIGN_CENTER);
475                                 layout->set_font_description (UIConfiguration::instance ().get_LargeFont ());
476                                 layout->set_text (_("Not\nAvailable"));
477                                 layout->get_pixel_size (w, h);
478                                 cr->move_to (rint ((510 - w) * .5), rint ((hh - h) * .5));
479                                 layout->show_in_cairo_context (cr);
480                         }
481
482                         hist->flush ();
483
484                         CimgArea *nu = manage (new CimgArea (nums));
485                         CimgArea *eb = manage (new CimgArea (ebur));
486                         CimgArea *hi = manage (new CimgArea (hist));
487                         HBox *hb = manage (new HBox ());
488                         hb->set_spacing (4);
489                         hb->pack_start (*nu, false, false);
490                         hb->pack_start (*hi, false, false);
491                         hb->pack_start (*eb, false, false);
492
493                         wtbl->attach (*hb, 0, 2, wrow, wrow + 1, SHRINK, SHRINK);
494                         ++wrow;
495                 }
496
497 #define XAXISLABEL(POS, TXT) {                            \
498   const float yy = rint (POS);                            \
499   layout->set_text (TXT);                                 \
500   layout->get_pixel_size (w, h);                          \
501   cr->move_to (m_l - 8 - w, rint ((POS) - h * .5));       \
502   cr->set_source_rgba (.9, .9, .9, 1.0);                  \
503   cr->set_operator (Cairo::OPERATOR_OVER);                \
504   layout->show_in_cairo_context (cr);                     \
505   cr->move_to (m_l - 4, yy - .5);                         \
506   cr->line_to (m_l + width, yy - .5);                     \
507   cr->set_source_rgba (.3, .3, .3, 1.0);                  \
508   cr->set_operator (Cairo::OPERATOR_ADD);                 \
509   cr->stroke ();                                          \
510 }
511
512
513                 {
514                         VBox *lrb = manage (new VBox());
515                         ToggleButton *log = manage (new ToggleButton (S_("Logscale|Lg")));
516                         ToggleButton *rec = manage (new ToggleButton (S_("Rectified|Rf")));
517                         lrb->pack_start (*log, false, false, 5);
518                         lrb->pack_end (*rec, false, false, 5);
519                         log->signal_toggled ().connect (sigc::bind (sigc::mem_fun (*this, &ExportReport::on_logscale_toggled), log));
520                         rec->signal_toggled ().connect (sigc::bind (sigc::mem_fun (*this, &ExportReport::on_rectivied_toggled), rec));
521                         lrb->show_all ();
522                         wtbl->attach (*lrb, 1, 2, wrow, wrow + p->n_channels, SHRINK, SHRINK);
523                 }
524
525                 for (uint32_t c = 0; c < p->n_channels; ++c) {
526                         /* draw waveform */
527                         const size_t width = sizeof (p->peaks) / sizeof (ARDOUR::PeakData::PeakDatum) / 4;
528
529                         Cairo::RefPtr<Cairo::ImageSurface> wave;
530                         Cairo::RefPtr<Cairo::ImageSurface> wave_log;
531                         Cairo::RefPtr<Cairo::ImageSurface> wave_rect;
532                         Cairo::RefPtr<Cairo::ImageSurface> wave_lr;
533                         draw_waveform(wave, p, c, m_l, width, anw, lin[0], false, false);
534                         draw_waveform(wave_log, p, c, m_l, width, anw, lin[0], true, false);
535                         draw_waveform(wave_rect, p, c, m_l, width, anw, lin[0], false, true);
536                         draw_waveform(wave_lr, p, c, m_l, width, anw, lin[0], true, true);
537
538                         CimgWaveArea *wv = manage (new CimgWaveArea (wave, wave_log, wave_rect, wave_lr, m_l, width));
539
540                         playhead_widgets.push_back (wv);
541                         waves.push_back (wv);
542                         wv->seek_playhead.connect (sigc::bind<0> (sigc::mem_fun (*this, &ExportReport::audition_seek), page));
543                         wtbl->attach (*wv, 0, 1, wrow, wrow + 1, SHRINK, SHRINK);
544                         ++wrow;
545                 }
546
547                 if (channels > 0 && file_length > 0 && sample_rate > 0)
548                 {
549                         /* Time Axis  -- re-use waveform width */
550                         const size_t width = sizeof (p->peaks) / sizeof (ARDOUR::PeakData::PeakDatum) / 4;
551                         layout->set_font_description (UIConfiguration::instance ().get_SmallMonospaceFont ());
552                         layout->set_text (_("00:00:00.000"));
553                         layout->get_pixel_size (w, h);
554                         int height = h * 1.75;
555                         Cairo::RefPtr<Cairo::ImageSurface> ytme = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, m_l + width, height);
556                         Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create (ytme);
557                         cr->set_operator (Cairo::OPERATOR_SOURCE);
558                         cr->set_source_rgba (0, 0, 0, 1.0);
559                         cr->paint ();
560                         cr->rectangle (0, 0, m_l, height);
561                         cr->set_source_rgba (0, 0, 0, 0);
562                         cr->fill ();
563                         cr->set_operator (Cairo::OPERATOR_OVER);
564
565                         cr->set_line_width (1.0);
566                         for (int i = 0; i <= 4; ++i) {
567                                 const float fract = (float) i / 4.0;
568                                 // " XX:XX:XX.XXX"  [space/minus] 12 chars = 13.
569                                 const float xalign = (i == 4) ? 1.0 : (i == 0) ? 1.0 / 13.0 : 7.0 / 13.0;
570
571                                 char buf[16];
572                                 AudioClock::print_minsec (start_off + file_length * fract,
573                                                 buf, sizeof (buf), sample_rate);
574
575                                 layout->set_text (buf);
576                                 layout->get_pixel_size (w, h);
577                                 cr->move_to (rint (m_l + width * fract - w * xalign), rint (.5 * (height - h)));
578                                 cr->set_source_rgba (.9, .9, .9, 1.0);
579                                 layout->show_in_cairo_context (cr);
580
581                                 cr->set_source_rgba (.7, .7, .7, 1.0);
582                                 cr->move_to (rint (m_l + width * fract) - .5, 0);
583                                 cr->line_to (rint (m_l + width * fract) - .5, ceil  (height * .15));
584                                 cr->move_to (rint (m_l + width * fract) - .5, floor (height * .85));
585                                 cr->line_to (rint (m_l + width * fract) - .5, height);
586                                 cr->stroke ();
587                         }
588
589                         layout->set_font_description (UIConfiguration::instance ().get_SmallFont ());
590                         layout->set_text (_("Time"));
591                         cr->set_source_rgba (.9, .9, .9, 1.0);
592                         layout->get_pixel_size (w, h);
593                         cr->move_to (rint (m_l - w - 8), rint (.5 * (height - h)));
594                         layout->show_in_cairo_context (cr);
595
596                         ytme->flush ();
597                         CimgPlayheadArea *tm = manage (new CimgPlayheadArea (ytme, m_l, width, true));
598                         playhead_widgets.push_back (tm);
599                         tm->seek_playhead.connect (sigc::bind<0> (sigc::mem_fun (*this, &ExportReport::audition_seek), page));
600                         wtbl->attach (*tm, 0, 1, wrow, wrow + 1, SHRINK, SHRINK);
601                         ++wrow;
602                 }
603
604                 {
605                         /* Draw Spectrum */
606                         const size_t swh = sizeof (p->spectrum) / sizeof (float);
607                         const size_t height = sizeof (p->spectrum[0]) / sizeof (float);
608                         const size_t width = swh / height;
609
610                         Cairo::RefPtr<Cairo::ImageSurface> spec = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, m_l + width, height);
611                         Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create (spec);
612                         cr->set_operator (Cairo::OPERATOR_SOURCE);
613                         cr->rectangle (0, 0, m_l, height);
614                         cr->set_source_rgba (0, 0, 0, 0);
615                         cr->fill ();
616                         cr->rectangle (m_l, 0, width, height);
617                         cr->set_source_rgba (0, 0, 0, 1.0);
618                         cr->fill ();
619                         cr->set_operator (Cairo::OPERATOR_OVER);
620
621                         for (size_t x = 0 ; x < width; ++x) {
622                                 for (size_t y = 0 ; y < height; ++y) {
623                                         const float pk = p->spectrum[x][y];
624                                         ArdourCanvas::Color c = ArdourCanvas::hsva_to_color (252 - 260 * pk, .9, sqrt(pk));
625                                         ArdourCanvas::set_source_rgba (cr, c);
626                                         cr->rectangle (m_l + x - .5, y - .5, 1, 1);
627                                         cr->fill ();
628                                 }
629                         }
630
631                         // Unit
632                         layout->set_font_description (UIConfiguration::instance ().get_SmallerFont ());
633                         layout->set_text (_("Hz"));
634                         layout->get_pixel_size (w, h);
635                         cr->move_to (rint (m_l - h - anw - 10), rint ((height + w) * .5));
636                         cr->set_source_rgba (.9, .9, .9, 1.0);
637                         cr->save ();
638                         cr->rotate (M_PI / -2.0);
639                         layout->show_in_cairo_context (cr);
640                         cr->restore ();
641
642                         // x-Axis
643                         cr->set_line_width (1.0);
644                         cr->set_dash (dashes, 2.0);
645                         cr->set_line_cap (Cairo::LINE_CAP_ROUND);
646
647                         layout->set_font_description (UIConfiguration::instance ().get_SmallMonospaceFont ());
648                         //XAXISLABEL (p->freq[0], _("50Hz"));
649                         XAXISLABEL (p->freq[1], _("100"));
650                         XAXISLABEL (p->freq[2], _("500"));
651                         XAXISLABEL (p->freq[3], _("1K"));
652                         XAXISLABEL (p->freq[4], _("5K"));
653                         XAXISLABEL (p->freq[5], _("10K"));
654                         spec->flush ();
655
656                         // annotations
657                         Cairo::RefPtr<Cairo::ImageSurface> scale = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, m_r, height);
658                         cr = Cairo::Context::create (scale);
659                         cr->set_operator (Cairo::OPERATOR_SOURCE);
660                         cr->rectangle (0, 0, m_r, height);
661                         cr->set_source_rgba (0, 0, 0, 0);
662                         cr->fill ();
663
664                         cr->set_operator (Cairo::OPERATOR_OVER);
665                         layout->set_font_description (UIConfiguration::instance ().get_SmallerFont ());
666                         layout->set_alignment (Pango::ALIGN_LEFT);
667                         layout->set_text (_("dBFS"));
668                         layout->get_pixel_size (w, h);
669                         cr->move_to (rint (.5 * (m_r - w)), height - h - 2);
670                         cr->set_source_rgba (.9, .9, .9, 1.0);
671                         layout->show_in_cairo_context (cr);
672
673                         int innertop = ceil (mnh * .5) + 1;
674                         size_t innerheight = (height - 2 * innertop - h - 2);
675
676                         cr->rectangle (1, innertop - 1,  m_r - 2 - anw, innerheight + 2);
677                         cr->set_source_rgba (0, 0, 0, 1.0);
678                         cr->fill_preserve ();
679                         cr->set_line_width (1.0);
680                         cr->set_source_rgba (.7, .7, .6, 1.0);
681                         cr->stroke ();
682
683                         for (size_t y = 0 ; y < innerheight - 2; ++y) {
684                                         const float pk = 1.0 - (float) y / innerheight;
685                                         ArdourCanvas::Color c = ArdourCanvas::hsva_to_color (252 - 260 * pk, .9, sqrt(pk));
686                                         ArdourCanvas::set_source_rgba (cr, c);
687                                         cr->rectangle (2, innertop + y + .5, m_r - 4 - anw, 1);
688                                         cr->fill ();
689                         }
690
691                         layout->set_font_description (UIConfiguration::instance ().get_SmallMonospaceFont ());
692                         for (int i = 0; i <= 4; ++i) {
693                                 const float fract = (float) i / 4.0;
694                                 const float yalign = .5; // (i == 4) ? 0 : (i == 0) ? 1.0 : .5;
695                                 char buf[16];
696                                 snprintf (buf, sizeof (buf), "%.0f", AudioGrapher::Analyser::fft_range_db * -fract);
697                                 layout->set_text (buf);
698                                 layout->get_pixel_size (w, h);
699
700                                 cr->move_to (m_r - anw, rint (innertop + fract * innerheight - h * yalign));
701                                 cr->set_source_rgba (.9, .9, .9, 1.0);
702                                 layout->show_in_cairo_context (cr);
703                         }
704                         scale->flush ();
705
706                         CimgPlayheadArea *sp = manage (new CimgPlayheadArea (spec, m_l, width));
707                         playhead_widgets.push_back (sp);
708                         sp->seek_playhead.connect (sigc::bind<0> (sigc::mem_fun (*this, &ExportReport::audition_seek), page));
709                         CimgArea *an = manage (new CimgArea (scale));
710                         wtbl->attach (*sp, 0, 1, wrow, wrow + 1, SHRINK, SHRINK);
711                         wtbl->attach (*an, 1, 2, wrow, wrow + 1, SHRINK, SHRINK);
712                         ++wrow;
713                 }
714
715                 timeline[page] = playhead_widgets;
716
717                 HBox *tab = manage (new HBox ());
718                 l = manage (new Label (Glib::path_get_basename (path)));
719                 Gtk::Image *img =  manage (new Image (Stock::MEDIA_PLAY, ICON_SIZE_MENU));
720                 tab->pack_start (*img);
721                 tab->pack_start (*l);
722                 l->show();
723                 tab->show();
724                 img->hide();
725                 pages.pages ().push_back (Notebook_Helpers::TabElem (*vb, *tab));
726                 pages.signal_switch_page().connect (sigc::mem_fun (*this, &ExportReport::on_switch_page));
727         }
728
729         pages.set_show_tabs (true);
730         pages.show_all ();
731         pages.set_name ("ExportReportNotebook");
732         pages.set_current_page (0);
733
734         get_vbox ()->set_spacing (4);
735         get_vbox ()->pack_start (pages, false, false);
736
737         if (_session) {
738                 _session->AuditionActive.connect(auditioner_connections, invalidator (*this), boost::bind (&ExportReport::audition_active, this, _1), gui_context());
739                 _session->the_auditioner()->AuditionProgress.connect(auditioner_connections, invalidator (*this), boost::bind (&ExportReport::audition_progress, this, _1, _2), gui_context());
740         }
741
742         if (_session && with_file) {
743                 play_btn = add_button (Stock::MEDIA_PLAY, RESPONSE_ACCEPT);
744                 stop_btn = add_button (Stock::MEDIA_STOP, RESPONSE_ACCEPT);
745         }
746         add_button (Stock::CLOSE, RESPONSE_CLOSE);
747
748         set_default_response (RESPONSE_CLOSE);
749         if (_session && with_file) {
750                 stop_btn->signal_clicked().connect (sigc::mem_fun (*this, &ExportReport::stop_audition));
751                 play_btn->signal_clicked().connect (sigc::mem_fun (*this, &ExportReport::play_audition));
752                 stop_btn->set_sensitive (false);
753         }
754         show_all ();
755 }
756
757 int
758 ExportReport::run ()
759 {
760         do {
761                 int i = ArdourDialog::run ();
762                 if (i == Gtk::RESPONSE_DELETE_EVENT || i == RESPONSE_CLOSE) {
763                         break;
764                 }
765         } while (1);
766
767         if (_session) {
768                 _session->cancel_audition();
769         }
770         return RESPONSE_CLOSE;
771 }
772
773 void
774 ExportReport::open_folder (std::string p)
775 {
776         PBD::open_folder (Glib::path_get_dirname(p));
777 }
778
779 void
780 ExportReport::audition_active (bool active)
781 {
782         if (!stop_btn || !play_btn) {
783                 return;
784         }
785         stop_btn->set_sensitive (active);
786         play_btn->set_sensitive (!active);
787
788         if (!active && _audition_num == _page_num && timeline.find (_audition_num) != timeline.end ()) {
789                 for (std::list<CimgPlayheadArea*>::const_iterator i = timeline[_audition_num].begin();
790                                 i != timeline[_audition_num].end();
791                                 ++i) {
792                         (*i)->set_playhead (-1);
793                 }
794         }
795
796         if (_audition_num >= 0 ) {
797                 Widget *page = pages.get_nth_page (_audition_num);
798                 HBox *box = static_cast<Gtk::HBox*> (pages.get_tab_label (*page));
799                 if (!active) {
800                         (*(box->get_children().begin()))->hide ();
801                 } else {
802                         (*(box->get_children().begin()))->show ();
803                 }
804         }
805
806         if (!active) {
807                 _audition_num = -1;
808         }
809 }
810
811 void
812 ExportReport::audition (std::string path, unsigned n_chn, int page)
813 {
814         assert (_session);
815         _session->cancel_audition();
816
817         if (n_chn ==0) { return; }
818
819         /* can't really happen, unless the user replaces the file while the dialog is open.. */
820         if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
821                 PBD::warning << string_compose(_("Could not read file: %1 (%2)."), path, strerror(errno)) << endmsg;
822                 return;
823         }
824         if (SMFSource::valid_midi_file (path)) { return; }
825
826         boost::shared_ptr<Region> r;
827         SourceList srclist;
828         boost::shared_ptr<AudioFileSource> afs;
829         bool old_sbp = AudioSource::get_build_peakfiles ();
830
831         /* don't even think of building peakfiles for these files */
832         AudioSource::set_build_peakfiles (false);
833
834         for (unsigned int n = 0; n < n_chn; ++n) {
835                 try {
836                         afs = boost::dynamic_pointer_cast<AudioFileSource> (
837                                 SourceFactory::createExternal (DataType::AUDIO, *_session,
838                                                                                  path, n,
839                                                                                  Source::Flag (ARDOUR::AudioFileSource::NoPeakFile), false));
840                         if (afs->sample_rate() != _session->nominal_frame_rate()) {
841                                 boost::shared_ptr<SrcFileSource> sfs (new SrcFileSource(*_session, afs, ARDOUR::SrcGood));
842                                 srclist.push_back(sfs);
843                         } else {
844                                 srclist.push_back(afs);
845                         }
846                 } catch (failed_constructor& err) {
847                         PBD::error << _("Could not access soundfile: ") << path << endmsg;
848                         AudioSource::set_build_peakfiles (old_sbp);
849                         return;
850                 }
851         }
852
853         AudioSource::set_build_peakfiles (old_sbp);
854
855         if (srclist.empty()) {
856                 return;
857         }
858
859         afs = boost::dynamic_pointer_cast<AudioFileSource> (srclist[0]);
860         std::string rname = region_name_from_path (afs->path(), false);
861
862         PBD::PropertyList plist;
863
864         plist.add (ARDOUR::Properties::start, 0);
865         plist.add (ARDOUR::Properties::length, srclist[0]->length(srclist[0]->timeline_position()));
866         plist.add (ARDOUR::Properties::name, rname);
867         plist.add (ARDOUR::Properties::layer, 0);
868
869         r = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (srclist, plist, false));
870
871         r->set_position(0);
872         _session->audition_region(r);
873         _audition_num = page;
874 }
875
876 void
877 ExportReport::play_audition ()
878 {
879         if (_audition_num >= 0 || !_session) { return; }
880         if (files.find (_page_num) == files.end()) { return; }
881         audition (files[_page_num].path, files[_page_num].channels, _page_num);
882 }
883
884 void
885 ExportReport::stop_audition ()
886 {
887         if (_audition_num == _page_num && timeline.find (_audition_num) != timeline.end ()) {
888                 for (std::list<CimgPlayheadArea*>::const_iterator i = timeline[_audition_num].begin();
889                                 i != timeline[_audition_num].end();
890                                 ++i) {
891                         (*i)->set_playhead (-1);
892                 }
893         }
894         if (_session) {
895                 _session->cancel_audition();
896         }
897 }
898
899 void
900 ExportReport::on_switch_page (GtkNotebookPage*, guint page_num)
901 {
902         if (_audition_num == _page_num) {
903                 for (std::list<CimgPlayheadArea*>::const_iterator i = timeline[_audition_num].begin();
904                                 i != timeline[_audition_num].end();
905                                 ++i) {
906                         (*i)->set_playhead (-1);
907                 }
908         }
909         _page_num = page_num;
910 }
911
912 void
913 ExportReport::audition_progress (framecnt_t pos, framecnt_t len)
914 {
915         if (_audition_num == _page_num && timeline.find (_audition_num) != timeline.end ()) {
916                 const float p = (float)pos / len;
917                 for (std::list<CimgPlayheadArea*>::const_iterator i = timeline[_audition_num].begin();
918                                 i != timeline[_audition_num].end();
919                                 ++i) {
920                         (*i)->set_playhead (p);
921                 }
922         }
923 }
924
925 void
926 ExportReport::audition_seek (int page, float pos)
927 {
928         if (_audition_num == page && _session) {
929                 _session->the_auditioner()->seek_to_percent (100.f * pos);
930         }
931 }
932
933 void
934 ExportReport::on_logscale_toggled (Gtk::ToggleButton* b)
935 {
936         bool en = b->get_active ();
937         for (std::list<CimgWaveArea*>::iterator i = waves.begin (); i != waves.end (); ++i) {
938                 (*i)->set_logscale (en);
939         }
940 }
941
942 void
943 ExportReport::on_rectivied_toggled (Gtk::ToggleButton* b)
944 {
945         bool en = b->get_active ();
946         for (std::list<CimgWaveArea*>::iterator i = waves.begin (); i != waves.end (); ++i) {
947                 (*i)->set_rectified (en);
948         }
949 }
950
951 void
952 ExportReport::draw_waveform (Cairo::RefPtr<Cairo::ImageSurface>& wave, ExportAnalysisPtr p, uint32_t c, int m_l, size_t width, int anw, int height, bool log, bool rect)
953 {
954         int w, h;
955         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (get_pango_context ());
956         const float height_2 = std::min (100, 8 * height / (int) p->n_channels); // TODO refine
957         const float ht = 2.f * height_2;
958
959         std::vector<double> dashes;
960         dashes.push_back (3.0);
961         dashes.push_back (5.0);
962
963         wave = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, m_l + width, ht);
964         Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create (wave);
965         cr->set_operator (Cairo::OPERATOR_SOURCE);
966         cr->rectangle (0, 0, m_l, ht);
967         cr->set_source_rgba (0, 0, 0, 0);
968         cr->fill ();
969         cr->rectangle (m_l, 0, width, ht);
970         cr->set_source_rgba (0, 0, 0, 1.0);
971         cr->fill ();
972         cr->set_operator (Cairo::OPERATOR_OVER);
973
974         cr->set_source_rgba (.7, .7, .7, 1.0);
975         cr->set_line_width (1.0);
976
977         // -1dB range
978         float clip_top;
979         float clip_bot;
980
981         if (rect) {
982                 clip_bot = ht;
983
984                 if (log) {
985                         clip_top = ht * (1.f - alt_log_meter (-1));
986                         for (size_t x = 0 ; x < width; ++x) {
987                                 const float v = alt_log_meter (fast_coefficient_to_dB (std::max (fabsf (p->peaks[c][x].max), fabsf (p->peaks[c][x].min))));
988                                 cr->move_to (m_l + x - .5, ht - ht * v);
989                                 cr->line_to (m_l + x - .5, ht);
990                         }
991                         cr->stroke ();
992                 } else {
993                         clip_top = ht * (1.f - dB_to_coefficient (-1));
994                         for (size_t x = 0 ; x < width; ++x) {
995                                 const float v = std::max (fabsf (p->peaks[c][x].max), fabsf (p->peaks[c][x].min));
996                                 cr->move_to (m_l + x - .5, ht - ht * v);
997                                 cr->line_to (m_l + x - .5, ht);
998                         }
999                         cr->stroke ();
1000                 }
1001         } else {
1002                 if (log) {
1003                         clip_top = height_2 - height_2 * alt_log_meter (-1);
1004                         clip_bot = height_2 + height_2 * alt_log_meter (-1);
1005                         for (size_t x = 0 ; x < width; ++x) {
1006                                 float pmax, pmin;
1007                                 if (p->peaks[c][x].max > 0) {
1008                                         pmax =  alt_log_meter (fast_coefficient_to_dB (p->peaks[c][x].max));
1009                                 } else {
1010                                         pmax = -alt_log_meter (fast_coefficient_to_dB (-p->peaks[c][x].max));
1011                                 }
1012
1013                                 if (p->peaks[c][x].min > 0) {
1014                                         pmin =  alt_log_meter (fast_coefficient_to_dB (p->peaks[c][x].min));
1015                                 } else {
1016                                         pmin = -alt_log_meter (fast_coefficient_to_dB (-p->peaks[c][x].min));
1017                                 }
1018                                 cr->move_to (m_l + x - .5, height_2 - height_2 * pmax);
1019                                 cr->line_to (m_l + x - .5, height_2 - height_2 * pmin);
1020                         }
1021                         cr->stroke ();
1022                 } else {
1023                         clip_top = height_2 - height_2 * dB_to_coefficient (-1);
1024                         clip_bot = height_2 + height_2 * dB_to_coefficient (-1);
1025                         for (size_t x = 0 ; x < width; ++x) {
1026                                 cr->move_to (m_l + x - .5, height_2 - height_2 * p->peaks[c][x].max);
1027                                 cr->line_to (m_l + x - .5, height_2 - height_2 * p->peaks[c][x].min);
1028                         }
1029                         cr->stroke ();
1030                 }
1031         }
1032
1033         // > 0dBFS
1034         cr->set_source_rgba (1.0, 0, 0, 1.0);
1035         for (size_t x = 0 ; x < width; ++x) {
1036                 if (p->peaks[c][x].max >= 1.0) {
1037                         cr->move_to (m_l + x - .5, 0);
1038                         cr->line_to (m_l + x - .5, clip_top);
1039                 }
1040                 if (p->peaks[c][x].min <= -1.0) {
1041                         cr->move_to (m_l + x - .5, clip_bot);
1042                         cr->line_to (m_l + x - .5, ht);
1043                 }
1044         }
1045         cr->stroke ();
1046
1047         // > -1dBTP
1048         cr->set_source_rgba (1.0, 0.7, 0, 0.7);
1049         for (std::set<framepos_t>::const_iterator i = p->truepeakpos[c].begin (); i != p->truepeakpos[c].end (); ++i) {
1050                 cr->move_to (m_l + (*i) - .5, clip_top);
1051                 cr->line_to (m_l + (*i) - .5, clip_bot);
1052                 cr->stroke ();
1053         }
1054
1055         if (!rect) {
1056                 // zero line
1057                 cr->set_source_rgba (.3, .3, .3, 0.7);
1058                 cr->move_to (m_l + 0, height_2 - .5);
1059                 cr->line_to (m_l + width, height_2 - .5);
1060                 cr->stroke ();
1061         }
1062
1063         // Unit
1064         layout->set_font_description (UIConfiguration::instance ().get_SmallerFont ());
1065         layout->set_alignment (Pango::ALIGN_LEFT);
1066         layout->set_text (_("dBFS"));
1067         layout->get_pixel_size (w, h);
1068         cr->move_to (rint (m_l - h - anw - 10), rint (height_2 + w * .5));
1069         cr->set_source_rgba (.9, .9, .9, 1.0);
1070         cr->save ();
1071         cr->rotate (M_PI / -2.0);
1072         layout->show_in_cairo_context (cr);
1073         cr->restore ();
1074
1075         // x-Axis
1076         cr->set_line_width (1.0);
1077         cr->set_dash (dashes, 2.0);
1078         cr->set_line_cap (Cairo::LINE_CAP_ROUND);
1079
1080         layout->set_font_description (UIConfiguration::instance ().get_SmallMonospaceFont ());
1081
1082         if (rect) {
1083                 if (log) {
1084                         XAXISLABEL ((ht - ht * alt_log_meter (-36)), _("-36"));
1085                         XAXISLABEL ((ht - ht * alt_log_meter (-18)), _("-18"));
1086                         XAXISLABEL ((ht - ht * alt_log_meter (-9)), _("-9"));
1087                         XAXISLABEL ((ht - ht * alt_log_meter (-3)), _("-3"));
1088                 } else {
1089                         XAXISLABEL ((ht - ht * .1259), _("-18"));
1090                         XAXISLABEL ((ht - ht * .3548), _("-9"));
1091                         XAXISLABEL ((ht - ht * .7079), _("-3"));
1092                 }
1093         } else {
1094                 if (log) {
1095                         XAXISLABEL ((height_2 - height_2 * alt_log_meter (-18)), _("-18"));
1096                         XAXISLABEL ((height_2 - height_2 * alt_log_meter (-9)), _("-9"));
1097                         XAXISLABEL ((height_2 - height_2 * alt_log_meter (-3)), _("-3"));
1098                         XAXISLABEL ((height_2 + height_2 * alt_log_meter (-18)), _("-18"));
1099                         XAXISLABEL ((height_2 + height_2 * alt_log_meter (-9)), _("-9"));
1100                         XAXISLABEL ((height_2 + height_2 * alt_log_meter (-3)), _("-3"));
1101                 } else {
1102                         XAXISLABEL (height_2 * 0.6452, _("-9"));
1103                         XAXISLABEL (height_2 * 1.3548, _("-9"));
1104                         XAXISLABEL (height_2 * 0.2921, _("-3"));
1105                         XAXISLABEL (height_2 * 1.7079, _("-3"));
1106                 }
1107         }
1108         wave->flush ();
1109 }