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