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