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