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