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