enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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 "pbd/i18n.h"
51
52 using namespace Gtk;
53 using namespace ARDOUR;
54
55 ExportReport::ExportReport (Session* session, StatusPtr s)
56         : ArdourDialog (_("Export Report/Analysis"))
57         , _session (session)
58         , stop_btn (0)
59         , play_btn (0)
60         , _audition_num (-1)
61         , _page_num (0)
62 {
63         init (s->result_map, true);
64 }
65
66 ExportReport::ExportReport (const std::string & title, const AnalysisResults & ar)
67         : ArdourDialog (title)
68         , _session (0)
69         , stop_btn (0)
70         , play_btn (0)
71         , _audition_num (-1)
72         , _page_num (0)
73 {
74         init (ar, false);
75 }
76
77 void
78 ExportReport::init (const AnalysisResults & ar, bool with_file)
79 {
80         set_resizable (false);
81         pages.set_scrollable ();
82
83         std::vector<double> dashes;
84         dashes.push_back (3.0);
85         dashes.push_back (5.0);
86
87         int page = 0;
88         for (AnalysisResults::const_iterator i = ar.begin (); i != ar.end (); ++i, ++page) {
89                 Label *l;
90                 VBox *vb = manage (new VBox ());
91                 Table *t = manage (new Table (4, 4));
92                 Table *wtbl = manage (new Table (3, 2));
93                 int wrow = 0;
94                 t->set_border_width (0);
95                 t->set_spacings (4);
96                 wtbl->set_spacings (4);
97                 vb->set_spacing (4);
98                 vb->set_border_width (4);
99                 vb->pack_start (*t, false, false, 2);
100                 vb->pack_start (*wtbl, false, false, 2);
101
102                 std::string path = i->first;
103                 ExportAnalysisPtr p = i->second;
104
105                 std::list<CimgPlayheadArea*> playhead_widgets;
106
107                 if (with_file) {
108                         l = manage (new Label (_("File:"), ALIGN_END));
109                         t->attach (*l, 0, 1, 0, 1);
110                         l = manage (new Label ());
111                         l->set_ellipsize (Pango::ELLIPSIZE_START);
112                         l->set_width_chars (48);
113                         l->set_max_width_chars (48);
114                         l->set_text (path);
115                         l->set_alignment (ALIGN_START, ALIGN_CENTER);
116                         t->attach (*l, 1, 3, 0, 1, FILL, SHRINK);
117
118                         Button *b = manage (new Button (_("Open Folder")));
119                         t->attach (*b, 3, 4, 0, 2, FILL, SHRINK);
120                         b->signal_clicked ().connect (sigc::bind (sigc::mem_fun (*this, &ExportReport::open_folder), path));
121                 }
122
123                 SoundFileInfo info;
124                 std::string errmsg;
125
126                 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                         Gtkmm2ext::UI::instance()->set_tip (log, _("Logscale"));
606                         Gtkmm2ext::UI::instance()->set_tip (rec, _("Rectified"));
607
608                         lrb->pack_start (*log, false, false, 5);
609                         lrb->pack_end (*rec, false, false, 5);
610                         log->signal_toggled ().connect (sigc::bind (sigc::mem_fun (*this, &ExportReport::on_logscale_toggled), log));
611                         rec->signal_toggled ().connect (sigc::bind (sigc::mem_fun (*this, &ExportReport::on_rectivied_toggled), rec));
612                         lrb->show_all ();
613                         wtbl->attach (*lrb, 1, 2, wrow, wrow + p->n_channels, SHRINK, SHRINK);
614                 }
615
616                 for (uint32_t c = 0; c < p->n_channels; ++c) {
617                         /* draw waveform */
618                         const size_t width = sizeof (p->peaks) / sizeof (ARDOUR::PeakData::PeakDatum) / 4;
619
620                         Cairo::RefPtr<Cairo::ImageSurface> wave;
621                         Cairo::RefPtr<Cairo::ImageSurface> wave_log;
622                         Cairo::RefPtr<Cairo::ImageSurface> wave_rect;
623                         Cairo::RefPtr<Cairo::ImageSurface> wave_lr;
624                         draw_waveform(wave, p, c, m_l, width, anw, waveh2, false, false);
625                         draw_waveform(wave_log, p, c, m_l, width, anw, waveh2, true, false);
626                         draw_waveform(wave_rect, p, c, m_l, width, anw, waveh2, false, true);
627                         draw_waveform(wave_lr, p, c, m_l, width, anw, waveh2, true, true);
628
629                         CimgWaveArea *wv = manage (new CimgWaveArea (wave, wave_log, wave_rect, wave_lr, m_l, width));
630
631                         playhead_widgets.push_back (wv);
632                         waves.push_back (wv);
633                         wv->seek_playhead.connect (sigc::bind<0> (sigc::mem_fun (*this, &ExportReport::audition_seek), page));
634                         wtbl->attach (*wv, 0, 1, wrow, wrow + 1, SHRINK, SHRINK);
635                         ++wrow;
636
637                         if (png_surface) {
638                                 Cairo::RefPtr<Cairo::Context> pcx = Cairo::Context::create (png_surface);
639                                 pcx->set_source (wave, 0, png_y0);
640                                 pcx->paint ();
641                                 png_y0 += 2 * waveh2 + 4;
642                         }
643                 }
644
645                 if (channels > 0 && file_length > 0 && sample_rate > 0)
646                 {
647                         /* Time Axis  -- re-use waveform width */
648                         const size_t width = sizeof (p->peaks) / sizeof (ARDOUR::PeakData::PeakDatum) / 4;
649                         layout->set_font_description (UIConfiguration::instance ().get_SmallMonospaceFont ());
650                         layout->set_text (_("00:00:00.000"));
651                         layout->get_pixel_size (w, h);
652                         int height = h * 1.75;
653                         Cairo::RefPtr<Cairo::ImageSurface> ytme = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, m_l + width, height);
654                         Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create (ytme);
655                         cr->set_operator (Cairo::OPERATOR_SOURCE);
656                         cr->set_source_rgba (0, 0, 0, 1.0);
657                         cr->paint ();
658                         cr->rectangle (0, 0, m_l, height);
659                         cr->set_source_rgba (0, 0, 0, 0);
660                         cr->fill ();
661                         cr->set_operator (Cairo::OPERATOR_OVER);
662
663                         cr->set_line_width (1.0);
664                         for (int i = 0; i <= 4; ++i) {
665                                 const float fract = (float) i / 4.0;
666                                 // " XX:XX:XX.XXX"  [space/minus] 12 chars = 13.
667                                 const float xalign = (i == 4) ? 1.0 : (i == 0) ? 1.0 / 13.0 : 7.0 / 13.0;
668
669                                 char buf[16];
670                                 AudioClock::print_minsec (start_off + file_length * fract,
671                                                 buf, sizeof (buf), sample_rate);
672
673                                 layout->set_text (buf);
674                                 layout->get_pixel_size (w, h);
675                                 cr->move_to (rint (m_l + width * fract - w * xalign), rint (.5 * (height - h)));
676                                 cr->set_source_rgba (.9, .9, .9, 1.0);
677                                 layout->show_in_cairo_context (cr);
678
679                                 cr->set_source_rgba (.7, .7, .7, 1.0);
680                                 cr->move_to (rint (m_l + width * fract) - .5, 0);
681                                 cr->line_to (rint (m_l + width * fract) - .5, ceil  (height * .15));
682                                 cr->move_to (rint (m_l + width * fract) - .5, floor (height * .85));
683                                 cr->line_to (rint (m_l + width * fract) - .5, height);
684                                 cr->stroke ();
685                         }
686
687                         layout->set_font_description (UIConfiguration::instance ().get_SmallFont ());
688                         layout->set_text (_("Time"));
689                         cr->set_source_rgba (.9, .9, .9, 1.0);
690                         layout->get_pixel_size (w, h);
691                         cr->move_to (rint (m_l - w - 8), rint (.5 * (height - h)));
692                         layout->show_in_cairo_context (cr);
693
694                         ytme->flush ();
695                         CimgPlayheadArea *tm = manage (new CimgPlayheadArea (ytme, m_l, width, true));
696                         playhead_widgets.push_back (tm);
697                         tm->seek_playhead.connect (sigc::bind<0> (sigc::mem_fun (*this, &ExportReport::audition_seek), page));
698                         wtbl->attach (*tm, 0, 1, wrow, wrow + 1, SHRINK, SHRINK);
699                         ++wrow;
700
701                         if (png_surface) {
702                                 Cairo::RefPtr<Cairo::Context> pcx = Cairo::Context::create (png_surface);
703                                 pcx->set_source (ytme, 0, png_y0);
704                                 pcx->paint ();
705                                 png_y0 += height + 4;
706                         }
707                 }
708
709                 {
710                         /* Draw Spectrum */
711                         const size_t height = specth;
712                         const size_t swh = sizeof (p->spectrum) / sizeof (float);
713                         const size_t width = swh / height;
714
715                         Cairo::RefPtr<Cairo::ImageSurface> spec = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, m_l + width, height);
716                         Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create (spec);
717                         cr->set_operator (Cairo::OPERATOR_SOURCE);
718                         cr->rectangle (0, 0, m_l, height);
719                         cr->set_source_rgba (0, 0, 0, 0);
720                         cr->fill ();
721                         cr->rectangle (m_l, 0, width, height);
722                         cr->set_source_rgba (0, 0, 0, 1.0);
723                         cr->fill ();
724                         cr->set_operator (Cairo::OPERATOR_OVER);
725
726                         for (size_t x = 0 ; x < width; ++x) {
727                                 for (size_t y = 0 ; y < height; ++y) {
728                                         const float pk = p->spectrum[x][y];
729                                         ArdourCanvas::Color c = ArdourCanvas::hsva_to_color (252 - 260 * pk, .9, sqrt(pk));
730                                         ArdourCanvas::set_source_rgba (cr, c);
731                                         cr->rectangle (m_l + x - .5, y - .5, 1, 1);
732                                         cr->fill ();
733                                 }
734                         }
735
736                         // Unit
737                         layout->set_font_description (UIConfiguration::instance ().get_SmallerFont ());
738                         layout->set_text (_("Hz"));
739                         layout->get_pixel_size (w, h);
740                         cr->move_to (rint (m_l - h - anw - 10), rint ((height + w) * .5));
741                         cr->set_source_rgba (.9, .9, .9, 1.0);
742                         cr->save ();
743                         cr->rotate (M_PI / -2.0);
744                         layout->show_in_cairo_context (cr);
745                         cr->restore ();
746
747                         // x-Axis
748                         cr->set_line_width (1.0);
749                         cr->set_dash (dashes, 2.0);
750                         cr->set_line_cap (Cairo::LINE_CAP_ROUND);
751
752                         layout->set_font_description (UIConfiguration::instance ().get_SmallMonospaceFont ());
753                         //XAXISLABEL (p->freq[0], _("50Hz"));
754                         XAXISLABEL (p->freq[1], _("100"));
755                         XAXISLABEL (p->freq[2], _("500"));
756                         XAXISLABEL (p->freq[3], _("1K"));
757                         XAXISLABEL (p->freq[4], _("5K"));
758                         XAXISLABEL (p->freq[5], _("10K"));
759                         spec->flush ();
760
761                         // annotations
762                         Cairo::RefPtr<Cairo::ImageSurface> scale = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, m_r, height);
763                         cr = Cairo::Context::create (scale);
764                         cr->set_operator (Cairo::OPERATOR_SOURCE);
765                         cr->rectangle (0, 0, m_r, height);
766                         cr->set_source_rgba (0, 0, 0, 0);
767                         cr->fill ();
768
769                         cr->set_operator (Cairo::OPERATOR_OVER);
770                         layout->set_font_description (UIConfiguration::instance ().get_SmallerFont ());
771                         layout->set_alignment (Pango::ALIGN_LEFT);
772                         layout->set_text (_("dBFS"));
773                         layout->get_pixel_size (w, h);
774                         cr->move_to (rint (.5 * (m_r - w)), height - h - 2);
775                         cr->set_source_rgba (.9, .9, .9, 1.0);
776                         layout->show_in_cairo_context (cr);
777
778                         int innertop = ceil (mnh * .5) + 1;
779                         size_t innerheight = (height - 2 * innertop - h - 2);
780
781                         cr->rectangle (1, innertop - 1,  m_r - 2 - anw, innerheight + 2);
782                         cr->set_source_rgba (0, 0, 0, 1.0);
783                         cr->fill_preserve ();
784                         cr->set_line_width (1.0);
785                         cr->set_source_rgba (.7, .7, .6, 1.0);
786                         cr->stroke ();
787
788                         for (size_t y = 0 ; y < innerheight - 2; ++y) {
789                                         const float pk = 1.0 - (float) y / innerheight;
790                                         ArdourCanvas::Color c = ArdourCanvas::hsva_to_color (252 - 260 * pk, .9, sqrt(pk));
791                                         ArdourCanvas::set_source_rgba (cr, c);
792                                         cr->rectangle (2, innertop + y + .5, m_r - 4 - anw, 1);
793                                         cr->fill ();
794                         }
795
796                         layout->set_font_description (UIConfiguration::instance ().get_SmallMonospaceFont ());
797                         for (int i = 0; i <= 4; ++i) {
798                                 const float fract = (float) i / 4.0;
799                                 const float yalign = .5; // (i == 4) ? 0 : (i == 0) ? 1.0 : .5;
800                                 char buf[16];
801                                 snprintf (buf, sizeof (buf), "%.0f", AudioGrapher::Analyser::fft_range_db * -fract);
802                                 layout->set_text (buf);
803                                 layout->get_pixel_size (w, h);
804
805                                 cr->move_to (m_r - anw, rint (innertop + fract * innerheight - h * yalign));
806                                 cr->set_source_rgba (.9, .9, .9, 1.0);
807                                 layout->show_in_cairo_context (cr);
808                         }
809                         scale->flush ();
810
811                         CimgPlayheadArea *sp = manage (new CimgPlayheadArea (spec, m_l, width));
812                         playhead_widgets.push_back (sp);
813                         sp->seek_playhead.connect (sigc::bind<0> (sigc::mem_fun (*this, &ExportReport::audition_seek), page));
814                         CimgArea *an = manage (new CimgArea (scale));
815                         wtbl->attach (*sp, 0, 1, wrow, wrow + 1, SHRINK, SHRINK);
816                         wtbl->attach (*an, 1, 2, wrow, wrow + 1, SHRINK, SHRINK);
817                         ++wrow;
818
819                         if (png_surface) {
820                                 Cairo::RefPtr<Cairo::Context> pcx = Cairo::Context::create (png_surface);
821                                 pcx->set_source (spec, 0, png_y0);
822                                 pcx->paint ();
823                                 pcx->set_source (scale, png_w - m_r, png_y0);
824                                 pcx->paint ();
825                         }
826                 }
827
828                 timeline[page] = playhead_widgets;
829
830                 HBox *tab = manage (new HBox ());
831                 l = manage (new Label (Glib::path_get_basename (path)));
832                 Gtk::Image *img =  manage (new Image (Stock::MEDIA_PLAY, ICON_SIZE_MENU));
833                 tab->pack_start (*img);
834                 tab->pack_start (*l);
835                 l->show();
836                 tab->show();
837                 img->hide();
838                 pages.pages ().push_back (Notebook_Helpers::TabElem (*vb, *tab));
839                 pages.signal_switch_page().connect (sigc::mem_fun (*this, &ExportReport::on_switch_page));
840
841                 if (png_surface) {
842                         assert (with_file && !path.empty ());
843                         std::string imgpath = Glib::build_filename (Glib::path_get_dirname (path), PBD::basename_nosuffix (path) + ".png");
844                         PBD::info << string_compose(_("Writing Export Analysis Image: %1."), imgpath) << endmsg;
845                         png_surface->write_to_png (imgpath);
846                 }
847         }
848
849         pages.set_show_tabs (true);
850         pages.show_all ();
851         pages.set_name ("ExportReportNotebook");
852         pages.set_current_page (0);
853
854         get_vbox ()->set_spacing (4);
855         get_vbox ()->pack_start (pages, false, false);
856
857         if (_session) {
858                 _session->AuditionActive.connect(auditioner_connections, invalidator (*this), boost::bind (&ExportReport::audition_active, this, _1), gui_context());
859                 _session->the_auditioner()->AuditionProgress.connect(auditioner_connections, invalidator (*this), boost::bind (&ExportReport::audition_progress, this, _1, _2), gui_context());
860         }
861
862         if (_session && with_file) {
863                 play_btn = add_button (Stock::MEDIA_PLAY, RESPONSE_ACCEPT);
864                 stop_btn = add_button (Stock::MEDIA_STOP, RESPONSE_ACCEPT);
865         }
866         add_button (Stock::CLOSE, RESPONSE_CLOSE);
867
868         set_default_response (RESPONSE_CLOSE);
869         if (_session && with_file) {
870                 stop_btn->signal_clicked().connect (sigc::mem_fun (*this, &ExportReport::stop_audition));
871                 play_btn->signal_clicked().connect (sigc::mem_fun (*this, &ExportReport::play_audition));
872                 stop_btn->set_sensitive (false);
873         }
874         show_all ();
875 }
876
877 int
878 ExportReport::run ()
879 {
880         do {
881                 int i = ArdourDialog::run ();
882                 if (i == Gtk::RESPONSE_DELETE_EVENT || i == RESPONSE_CLOSE) {
883                         break;
884                 }
885         } while (1);
886
887         if (_session) {
888                 _session->cancel_audition();
889         }
890         return RESPONSE_CLOSE;
891 }
892
893 void
894 ExportReport::open_folder (std::string p)
895 {
896         PBD::open_folder (Glib::path_get_dirname(p));
897 }
898
899 void
900 ExportReport::audition_active (bool active)
901 {
902         if (!stop_btn || !play_btn) {
903                 return;
904         }
905         stop_btn->set_sensitive (active);
906         play_btn->set_sensitive (!active);
907
908         if (!active && _audition_num == _page_num && timeline.find (_audition_num) != timeline.end ()) {
909                 for (std::list<CimgPlayheadArea*>::const_iterator i = timeline[_audition_num].begin();
910                                 i != timeline[_audition_num].end();
911                                 ++i) {
912                         (*i)->set_playhead (-1);
913                 }
914         }
915
916         if (_audition_num >= 0 ) {
917                 Widget *page = pages.get_nth_page (_audition_num);
918                 HBox *box = static_cast<Gtk::HBox*> (pages.get_tab_label (*page));
919                 if (!active) {
920                         (*(box->get_children().begin()))->hide ();
921                 } else {
922                         (*(box->get_children().begin()))->show ();
923                 }
924         }
925
926         if (!active) {
927                 _audition_num = -1;
928         }
929 }
930
931 void
932 ExportReport::audition (std::string path, unsigned n_chn, int page)
933 {
934         assert (_session);
935         _session->cancel_audition();
936
937         if (n_chn ==0) { return; }
938
939         /* can't really happen, unless the user replaces the file while the dialog is open.. */
940         if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
941                 PBD::warning << string_compose(_("Could not read file: %1 (%2)."), path, strerror(errno)) << endmsg;
942                 return;
943         }
944         if (SMFSource::valid_midi_file (path)) { return; }
945
946         boost::shared_ptr<Region> r;
947         SourceList srclist;
948         boost::shared_ptr<AudioFileSource> afs;
949         bool old_sbp = AudioSource::get_build_peakfiles ();
950
951         /* don't even think of building peakfiles for these files */
952         AudioSource::set_build_peakfiles (false);
953
954         for (unsigned int n = 0; n < n_chn; ++n) {
955                 try {
956                         afs = boost::dynamic_pointer_cast<AudioFileSource> (
957                                 SourceFactory::createExternal (DataType::AUDIO, *_session,
958                                                                                  path, n,
959                                                                                  Source::Flag (ARDOUR::AudioFileSource::NoPeakFile), false));
960                         if (afs->sample_rate() != _session->nominal_frame_rate()) {
961                                 boost::shared_ptr<SrcFileSource> sfs (new SrcFileSource(*_session, afs, ARDOUR::SrcGood));
962                                 srclist.push_back(sfs);
963                         } else {
964                                 srclist.push_back(afs);
965                         }
966                 } catch (failed_constructor& err) {
967                         PBD::error << _("Could not access soundfile: ") << path << endmsg;
968                         AudioSource::set_build_peakfiles (old_sbp);
969                         return;
970                 }
971         }
972
973         AudioSource::set_build_peakfiles (old_sbp);
974
975         if (srclist.empty()) {
976                 return;
977         }
978
979         afs = boost::dynamic_pointer_cast<AudioFileSource> (srclist[0]);
980         std::string rname = region_name_from_path (afs->path(), false);
981
982         PBD::PropertyList plist;
983
984         plist.add (ARDOUR::Properties::start, 0);
985         plist.add (ARDOUR::Properties::length, srclist[0]->length(srclist[0]->timeline_position()));
986         plist.add (ARDOUR::Properties::name, rname);
987         plist.add (ARDOUR::Properties::layer, 0);
988
989         r = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (srclist, plist, false));
990
991         r->set_position(0);
992         _session->audition_region(r);
993         _audition_num = page;
994 }
995
996 void
997 ExportReport::play_audition ()
998 {
999         if (_audition_num >= 0 || !_session) { return; }
1000         if (files.find (_page_num) == files.end()) { return; }
1001         audition (files[_page_num].path, files[_page_num].channels, _page_num);
1002 }
1003
1004 void
1005 ExportReport::stop_audition ()
1006 {
1007         if (_audition_num == _page_num && timeline.find (_audition_num) != timeline.end ()) {
1008                 for (std::list<CimgPlayheadArea*>::const_iterator i = timeline[_audition_num].begin();
1009                                 i != timeline[_audition_num].end();
1010                                 ++i) {
1011                         (*i)->set_playhead (-1);
1012                 }
1013         }
1014         if (_session) {
1015                 _session->cancel_audition();
1016         }
1017 }
1018
1019 void
1020 ExportReport::on_switch_page (GtkNotebookPage*, guint page_num)
1021 {
1022         if (_audition_num == _page_num) {
1023                 for (std::list<CimgPlayheadArea*>::const_iterator i = timeline[_audition_num].begin();
1024                                 i != timeline[_audition_num].end();
1025                                 ++i) {
1026                         (*i)->set_playhead (-1);
1027                 }
1028         }
1029         _page_num = page_num;
1030 }
1031
1032 void
1033 ExportReport::audition_progress (framecnt_t pos, framecnt_t len)
1034 {
1035         if (_audition_num == _page_num && timeline.find (_audition_num) != timeline.end ()) {
1036                 const float p = (float)pos / len;
1037                 for (std::list<CimgPlayheadArea*>::const_iterator i = timeline[_audition_num].begin();
1038                                 i != timeline[_audition_num].end();
1039                                 ++i) {
1040                         (*i)->set_playhead (p);
1041                 }
1042         }
1043 }
1044
1045 void
1046 ExportReport::audition_seek (int page, float pos)
1047 {
1048         if (_audition_num == page && _session) {
1049                 _session->the_auditioner()->seek_to_percent (100.f * pos);
1050         }
1051 }
1052
1053 void
1054 ExportReport::on_logscale_toggled (Gtk::ToggleButton* b)
1055 {
1056         bool en = b->get_active ();
1057         for (std::list<CimgWaveArea*>::iterator i = waves.begin (); i != waves.end (); ++i) {
1058                 (*i)->set_logscale (en);
1059         }
1060 }
1061
1062 void
1063 ExportReport::on_rectivied_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_rectified (en);
1068         }
1069 }
1070
1071 void
1072 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)
1073 {
1074         int w, h;
1075         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (get_pango_context ());
1076         const float ht = 2.f * height_2;
1077
1078         std::vector<double> dashes;
1079         dashes.push_back (3.0);
1080         dashes.push_back (5.0);
1081
1082         wave = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, m_l + width, ht);
1083         Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create (wave);
1084         cr->set_operator (Cairo::OPERATOR_SOURCE);
1085         cr->rectangle (0, 0, m_l, ht);
1086         cr->set_source_rgba (0, 0, 0, 0);
1087         cr->fill ();
1088         cr->rectangle (m_l, 0, width, ht);
1089         cr->set_source_rgba (0, 0, 0, 1.0);
1090         cr->fill ();
1091         cr->set_operator (Cairo::OPERATOR_OVER);
1092
1093         cr->set_source_rgba (.7, .7, .7, 1.0);
1094         cr->set_line_width (1.0);
1095
1096         // -1dB range
1097         float clip_top;
1098         float clip_bot;
1099
1100         if (rect) {
1101                 clip_bot = ht;
1102
1103                 if (log) {
1104                         clip_top = ht * (1.f - alt_log_meter (-1));
1105                         for (size_t x = 0 ; x < width; ++x) {
1106                                 const float v = alt_log_meter (fast_coefficient_to_dB (std::max (fabsf (p->peaks[c][x].max), fabsf (p->peaks[c][x].min))));
1107                                 cr->move_to (m_l + x - .5, ht - ht * v);
1108                                 cr->line_to (m_l + x - .5, ht);
1109                         }
1110                         cr->stroke ();
1111                 } else {
1112                         clip_top = ht * (1.f - dB_to_coefficient (-1));
1113                         for (size_t x = 0 ; x < width; ++x) {
1114                                 const float v = std::max (fabsf (p->peaks[c][x].max), fabsf (p->peaks[c][x].min));
1115                                 cr->move_to (m_l + x - .5, ht - ht * v);
1116                                 cr->line_to (m_l + x - .5, ht);
1117                         }
1118                         cr->stroke ();
1119                 }
1120         } else {
1121                 if (log) {
1122                         clip_top = height_2 - height_2 * alt_log_meter (-1);
1123                         clip_bot = height_2 + height_2 * alt_log_meter (-1);
1124                         for (size_t x = 0 ; x < width; ++x) {
1125                                 float pmax, pmin;
1126                                 if (p->peaks[c][x].max > 0) {
1127                                         pmax =  alt_log_meter (fast_coefficient_to_dB (p->peaks[c][x].max));
1128                                 } else {
1129                                         pmax = -alt_log_meter (fast_coefficient_to_dB (-p->peaks[c][x].max));
1130                                 }
1131
1132                                 if (p->peaks[c][x].min > 0) {
1133                                         pmin =  alt_log_meter (fast_coefficient_to_dB (p->peaks[c][x].min));
1134                                 } else {
1135                                         pmin = -alt_log_meter (fast_coefficient_to_dB (-p->peaks[c][x].min));
1136                                 }
1137                                 cr->move_to (m_l + x - .5, height_2 - height_2 * pmax);
1138                                 cr->line_to (m_l + x - .5, height_2 - height_2 * pmin);
1139                         }
1140                         cr->stroke ();
1141                 } else {
1142                         clip_top = height_2 - height_2 * dB_to_coefficient (-1);
1143                         clip_bot = height_2 + height_2 * dB_to_coefficient (-1);
1144                         for (size_t x = 0 ; x < width; ++x) {
1145                                 cr->move_to (m_l + x - .5, height_2 - height_2 * p->peaks[c][x].max);
1146                                 cr->line_to (m_l + x - .5, height_2 - height_2 * p->peaks[c][x].min);
1147                         }
1148                         cr->stroke ();
1149                 }
1150         }
1151
1152         // >= 0dBFS
1153         cr->set_source_rgba (1.0, 0, 0, 1.0);
1154         for (size_t x = 0 ; x < width; ++x) {
1155                 if (p->peaks[c][x].max >= 1.0) {
1156                         cr->move_to (m_l + x - .5, 0);
1157                         cr->line_to (m_l + x - .5, clip_top);
1158                 }
1159                 if (p->peaks[c][x].min <= -1.0) {
1160                         cr->move_to (m_l + x - .5, clip_bot);
1161                         cr->line_to (m_l + x - .5, ht);
1162                 }
1163         }
1164         cr->stroke ();
1165
1166         // >= -1dBTP (coeff >= .89125, libs/vamp-plugins/TruePeak.cpp)
1167         cr->set_source_rgba (1.0, 0.7, 0, 0.7);
1168         for (std::set<framepos_t>::const_iterator i = p->truepeakpos[c].begin (); i != p->truepeakpos[c].end (); ++i) {
1169                 cr->move_to (m_l + (*i) - .5, clip_top);
1170                 cr->line_to (m_l + (*i) - .5, clip_bot);
1171                 cr->stroke ();
1172         }
1173
1174         if (!rect) {
1175                 // zero line
1176                 cr->set_source_rgba (.3, .3, .3, 0.7);
1177                 cr->move_to (m_l + 0, height_2 - .5);
1178                 cr->line_to (m_l + width, height_2 - .5);
1179                 cr->stroke ();
1180         }
1181
1182         // Unit
1183         layout->set_font_description (UIConfiguration::instance ().get_SmallerFont ());
1184         layout->set_alignment (Pango::ALIGN_LEFT);
1185         layout->set_text (_("dBFS"));
1186         layout->get_pixel_size (w, h);
1187         cr->move_to (rint (m_l - h - anw - 10), rint (height_2 + w * .5));
1188         cr->set_source_rgba (.9, .9, .9, 1.0);
1189         cr->save ();
1190         cr->rotate (M_PI / -2.0);
1191         layout->show_in_cairo_context (cr);
1192         cr->restore ();
1193
1194         // x-Axis
1195         cr->set_line_width (1.0);
1196         cr->set_dash (dashes, 2.0);
1197         cr->set_line_cap (Cairo::LINE_CAP_ROUND);
1198
1199         layout->set_font_description (UIConfiguration::instance ().get_SmallMonospaceFont ());
1200
1201         if (rect) {
1202                 if (log) {
1203                         XAXISLABEL ((ht - ht * alt_log_meter (-36)), _("-36"));
1204                         XAXISLABEL ((ht - ht * alt_log_meter (-18)), _("-18"));
1205                         XAXISLABEL ((ht - ht * alt_log_meter (-9)), _("-9"));
1206                         XAXISLABEL ((ht - ht * alt_log_meter (-3)), _("-3"));
1207                 } else {
1208                         XAXISLABEL ((ht - ht * .1259), _("-18"));
1209                         XAXISLABEL ((ht - ht * .3548), _("-9"));
1210                         XAXISLABEL ((ht - ht * .7079), _("-3"));
1211                 }
1212         } else {
1213                 if (log) {
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                         XAXISLABEL ((height_2 + height_2 * alt_log_meter (-18)), _("-18"));
1218                         XAXISLABEL ((height_2 + height_2 * alt_log_meter (-9)), _("-9"));
1219                         XAXISLABEL ((height_2 + height_2 * alt_log_meter (-3)), _("-3"));
1220                 } else {
1221                         XAXISLABEL (height_2 * 0.6452, _("-9"));
1222                         XAXISLABEL (height_2 * 1.3548, _("-9"));
1223                         XAXISLABEL (height_2 * 0.2921, _("-3"));
1224                         XAXISLABEL (height_2 * 1.7079, _("-3"));
1225                 }
1226         }
1227         wave->flush ();
1228 }