1f443d1e05a35e86af7574a610c5b0ac3f039d2e
[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 "gtkmm2ext/utils.h"
28 #include "gtkmm2ext/utils.h"
29 #include "canvas/utils.h"
30 #include "canvas/colors.h"
31
32 #include "ardour/audiofilesource.h"
33 #include "ardour/audioregion.h"
34 #include "ardour/auditioner.h"
35 #include "ardour/dB.h"
36 #include "ardour/region_factory.h"
37 #include "ardour/session.h"
38 #include "ardour/smf_source.h"
39 #include "ardour/source_factory.h"
40 #include "ardour/srcfilesource.h"
41
42 #include "audio_clock.h"
43 #include "ui_config.h"
44 #include "export_report.h"
45
46 #include "i18n.h"
47
48 using namespace Gtk;
49 using namespace ARDOUR;
50
51 ExportReport::ExportReport (Session* session, StatusPtr s)
52         : ArdourDialog (_("Export Report/Analysis"))
53         , status (s)
54         , _session (session)
55         , _audition_num (-1)
56         , _page_num (0)
57 {
58         set_resizable (false);
59         pages.set_scrollable ();
60
61         AnalysisResults & ar = status->result_map;
62
63         std::vector<double> dashes;
64         dashes.push_back (3.0);
65         dashes.push_back (5.0);
66
67         int page = 0;
68         for (AnalysisResults::iterator i = ar.begin (); i != ar.end (); ++i, ++page) {
69                 Label *l;
70                 VBox *vb = manage (new VBox ());
71                 Table *t = manage (new Table (4, 4));
72                 t->set_border_width (0);
73                 t->set_spacings (4);
74                 vb->set_spacing (4);
75                 vb->set_border_width (4);
76                 vb->pack_start (*t, false, false, 2);
77
78                 std::string path = i->first;
79                 ExportAnalysisPtr p = i->second;
80
81                 l = manage (new Label (_("File:"), ALIGN_END));
82                 t->attach (*l, 0, 1, 0, 1);
83                 l = manage (new Label ());
84                 l->set_ellipsize (Pango::ELLIPSIZE_START);
85                 l->set_width_chars (64);
86                 l->set_max_width_chars (64);
87                 l->set_text (path);
88                 l->set_alignment (ALIGN_START, ALIGN_CENTER);
89                 t->attach (*l, 1, 3, 0, 1, FILL, SHRINK);
90
91                 Button *b = manage (new Button (_("Open Folder")));
92                 t->attach (*b, 3, 4, 0, 1, FILL, SHRINK);
93                 b->signal_clicked ().connect (sigc::bind (sigc::mem_fun (*this, &ExportReport::open_folder), path));
94
95                 Button *start_btn = manage (new Button (_("Audition")));
96                 t->attach (*start_btn, 3, 4, 1, 2, FILL, SHRINK);
97
98                 SoundFileInfo info;
99                 std::string errmsg;
100
101                 framecnt_t file_length = 0;
102                 framecnt_t sample_rate = 0;
103                 framecnt_t start_off = 0;
104                 unsigned int channels = 0;
105
106                 if (AudioFileSource::get_soundfile_info (path, info, errmsg)) {
107                         AudioClock * clock;
108
109                         file_length = info.length;
110                         sample_rate = info.samplerate;
111                         start_off = info.timecode;
112                         channels = info.channels;
113
114                         /* File Info Table */
115
116                         framecnt_t const nfr = _session ? _session->nominal_frame_rate () : 25;
117                         double src_coef = (double) nfr / info.samplerate;
118
119                         l = manage (new Label (_("Format:"), ALIGN_END));
120                         t->attach (*l, 0, 1, 1, 2);
121                         std::string fmt = info.format_name;
122                         std::replace (fmt.begin (), fmt.end (), '\n', ' ');
123                         l = manage (new Label ());
124                         l->set_ellipsize (Pango::ELLIPSIZE_START);
125                         l->set_width_chars (64);
126                         l->set_max_width_chars (64);
127                         l->set_text (fmt);
128                         l->set_alignment (ALIGN_START, ALIGN_CENTER);
129                         t->attach (*l, 1, 3, 1, 2, FILL, SHRINK);
130
131                         l = manage (new Label (_("Channels:"), ALIGN_END));
132                         t->attach (*l, 0, 1, 2, 3);
133                         l = manage (new Label (string_compose ("%1", info.channels), ALIGN_START));
134                         t->attach (*l, 1, 2, 2, 3);
135
136                         l = manage (new Label (_("Sample rate:"), ALIGN_END));
137                         t->attach (*l, 0, 1, 3, 4);
138                         l = manage (new Label (string_compose (_("%1 Hz"), info.samplerate), ALIGN_START));
139                         t->attach (*l, 1, 2, 3, 4);
140
141                         l = manage (new Label (_("Duration:"), ALIGN_END));
142                         t->attach (*l, 2, 3, 2, 3);
143                         clock = manage (new AudioClock ("sfboxLengthClock", true, "", false, false, true, false));
144                         clock->set_session (_session);
145                         clock->set_mode (AudioClock::MinSec);
146                         clock->set (info.length * src_coef + 0.5, true);
147                         t->attach (*clock, 3, 4, 2, 3);
148
149                         l = manage (new Label (_("Timecode:"), ALIGN_END));
150                         t->attach (*l, 2, 3, 3, 4);
151                         clock = manage (new AudioClock ("sfboxTimecodeClock", true, "", false, false, false, false));
152                         clock->set_session (_session);
153                         clock->set_mode (AudioClock::Timecode);
154                         clock->set (info.timecode * src_coef + 0.5, true);
155                         t->attach (*clock, 3, 4, 3, 4);
156                 } else {
157                         l = manage (new Label (_("Error:"), ALIGN_END));
158                         t->attach (*l, 0, 1, 1, 2);
159                         l = manage (new Label (errmsg, ALIGN_START));
160                         t->attach (*l, 1, 4, 1, 2);
161                 }
162
163                 if (channels > 0 && file_length > 0 && sample_rate > 0 && _session) {
164                         start_btn->signal_clicked ().connect (sigc::bind (sigc::mem_fun (*this, &ExportReport::audition), path, channels, page));
165                 } else {
166                         start_btn->set_sensitive (false);
167                 }
168
169                 int w, h;
170                 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (get_pango_context ());
171
172                 // calc geometry of numerics
173                 const float dbfs = accurate_coefficient_to_dB (p->peak);
174                 const float dbtp = accurate_coefficient_to_dB (p->truepeak);
175
176 #define TXTSIZE(LINE, TXT, FONT) {                                     \
177   layout->set_font_description (UIConfiguration::instance ().FONT ()); \
178   layout->set_text (TXT);                                              \
179   layout->get_pixel_size (w, h);                                       \
180   if (w > mnw) { mnw = w; }                                            \
181   if (h > lin[LINE]) { lin[LINE] = h; }                                \
182 }
183
184 #define TXTWIDTH(TXT, FONT) {                                          \
185   layout->set_font_description (UIConfiguration::instance ().FONT ()); \
186   layout->set_text (TXT);                                              \
187   layout->get_pixel_size (w, h);                                       \
188   if (w > mml) { mml = w; }                                            \
189 }
190
191                 int mnw = 0; // max numeric width
192                 int lin[4] = { 0, 0, 0, 0 }; // max line height
193
194                 TXTSIZE(0, _("(too short integration time)"), get_SmallFont);
195
196                 TXTSIZE(0, _("Peak:"), get_SmallFont);
197                 TXTSIZE(1, string_compose (_("%1 dBFS"), std::setprecision (1), std::fixed, dbfs), get_LargeFont);
198                 TXTSIZE(2, _("True Peak:"), get_SmallFont);
199                 TXTSIZE(3, string_compose (_("%1 dBTP"), std::setprecision (1), std::fixed, dbtp), get_LargeFont);
200
201                 TXTSIZE(0, _("Integrated Loudness:"), get_SmallFont);
202                 TXTSIZE(1, string_compose (_("%1 LUFS"), std::setprecision (1), std::fixed, p->loudness), get_LargeFont);
203                 TXTSIZE(2, _("Loudness Range:"), get_SmallFont);
204                 TXTSIZE(3, string_compose (_("%1 LU"), std::setprecision (1), std::fixed, p->loudness_range), get_LargeFont);
205
206                 mnw += 8;
207                 const int ht = lin[0] * 1.25 + lin[1] * 1.25 + lin[2] * 1.25 + lin[3] + 8;
208                 const int hh = std::max (100, ht);
209                 int m_l =  2 * mnw + /*hist-width*/ 540 + /*box spacing*/ 8 - /*peak-width*/ 800; // margin left
210
211                 int mml = 0; // min margin left -- ensure left margin is wide enough
212                 TXTWIDTH (_("Time"), get_SmallFont);
213                 TXTWIDTH (_("100"), get_SmallMonospaceFont);
214                 m_l = (std::max (m_l, mml + 8) + 3) & ~3;
215
216                 mnw = (m_l - /*hist-width*/ 540 - /*box spacing*/ 8 + /*peak-width*/ 800) / 2;
217                 const int nw2 = mnw / 2; // nums, horizontal center
218
219                 int y0[4];
220                 y0[0] = (hh - ht) * .5 + lin[0] * .25;
221                 y0[1] = y0[0] + lin[0] * 1.25;
222                 y0[2] = y0[1] + lin[1] * 1.25;
223                 y0[3] = y0[2] + lin[2] * 1.25;
224
225
226                 { /* peak, loudness and R128 histogram */
227                         Cairo::RefPtr<Cairo::ImageSurface> nums = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, mnw, hh);
228                         Cairo::RefPtr<Cairo::ImageSurface> ebur = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, mnw, hh);
229                         Cairo::RefPtr<Cairo::ImageSurface> hist = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, 540, hh);
230
231                         /* peak and true-peak numerics */
232                         Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create (nums);
233                         cr->set_source_rgba (0, 0, 0, 1.0);
234                         cr->paint ();
235
236                         layout->set_font_description (UIConfiguration::instance ().get_SmallFont ());
237                         layout->set_alignment (Pango::ALIGN_LEFT);
238                         layout->set_text (_("Peak:"));
239                         layout->get_pixel_size (w, h);
240                         cr->move_to (rint (nw2 - w * .5), y0[0]);
241                         cr->set_source_rgba (.7, .7, .7, 1.0);
242                         layout->show_in_cairo_context (cr);
243
244                         layout->set_font_description (UIConfiguration::instance ().get_LargeFont ());
245                         layout->set_text (string_compose (_("%1 dBFS"), std::setprecision (1), std::fixed, dbfs));
246                         layout->get_pixel_size (w, h);
247                         cr->move_to (rint (nw2 - w * .5), y0[1]);
248                         if (dbfs >= 0.f) { cr->set_source_rgba (1.0, .1, .1, 1.0); }
249                         else if (dbfs > -1.f) { cr->set_source_rgba (1.0, .7, .0, 1.0); }
250                         layout->show_in_cairo_context (cr);
251
252                         if (p->have_dbtp) {
253                                 layout->set_font_description (UIConfiguration::instance ().get_SmallFont ());
254                                 layout->set_text (_("True Peak:"));
255                                 layout->get_pixel_size (w, h);
256                                 cr->move_to (rint (nw2 - w * .5), y0[2]);
257                                 cr->set_source_rgba (.7, .7, .7, 1.0);
258                                 layout->show_in_cairo_context (cr);
259
260                                 layout->set_font_description (UIConfiguration::instance ().get_LargeFont ());
261                                 layout->set_text (string_compose (_("%1 dBTP"), std::setprecision (1), std::fixed, dbtp));
262                                 layout->get_pixel_size (w, h);
263                                 cr->move_to (rint (nw2 - w * .5), y0[3]);
264                                 if (dbtp >= 0.f) { cr->set_source_rgba (1.0, .1, .1, 1.0); }
265                                 else if (dbtp > -1.f) { cr->set_source_rgba (1.0, .7, .0, 1.0); }
266                                 layout->show_in_cairo_context (cr);
267                         }
268
269                         nums->flush ();
270
271                         /* EBU R128 numerics */
272                         cr = Cairo::Context::create (ebur);
273                         cr->set_source_rgba (0, 0, 0, 1.0);
274                         cr->paint ();
275
276                         cr->set_source_rgba (.7, .7, .7, 1.0);
277
278                         if (!i->second->have_loudness) {
279                                 layout->set_alignment (Pango::ALIGN_CENTER);
280                                 layout->set_font_description (UIConfiguration::instance ().get_LargeFont ());
281                                 layout->set_text (_("Not\nAvailable"));
282                                 layout->get_pixel_size (w, h);
283                                 cr->move_to (rint (nw2 - w * .5), rint ((hh - h) * .5));
284                                 layout->show_in_cairo_context (cr);
285                         }
286                         else if (p->loudness == -200 && p->loudness_range == 0) {
287                                 layout->set_alignment (Pango::ALIGN_CENTER);
288                                 layout->set_font_description (UIConfiguration::instance ().get_LargeFont ());
289                                 layout->set_text (_("Not\nAvailable"));
290                                 layout->get_pixel_size (w, h);
291                                 cr->move_to (rint (nw2 - w * .5), rint (hh * .5 - h * .6));
292                                 layout->show_in_cairo_context (cr);
293                                 int yy = h * .5;
294
295                                 layout->set_font_description (UIConfiguration::instance ().get_SmallFont ());
296                                 layout->set_text (_("(too short integration time)"));
297                                 layout->get_pixel_size (w, h);
298                                 cr->move_to (rint (nw2 - w * .5), rint (hh * .5 + yy));
299                                 layout->show_in_cairo_context (cr);
300
301                         } else {
302                                 layout->set_font_description (UIConfiguration::instance ().get_SmallFont ());
303                                 layout->set_alignment (Pango::ALIGN_LEFT);
304                                 layout->set_text (_("Integrated Loudness:"));
305                                 layout->get_pixel_size (w, h);
306                                 cr->move_to (rint (nw2 - w * .5), y0[0]);
307                                 layout->show_in_cairo_context (cr);
308
309                                 layout->set_font_description (UIConfiguration::instance ().get_LargeFont ());
310                                 layout->set_text (string_compose (_("%1 LUFS"), std::setprecision (1), std::fixed,  p->loudness));
311                                 layout->get_pixel_size (w, h);
312                                 cr->move_to (rint (nw2 - w * .5), y0[1]);
313                                 layout->show_in_cairo_context (cr);
314
315                                 layout->set_font_description (UIConfiguration::instance ().get_SmallFont ());
316                                 layout->set_text (_("Loudness Range:"));
317                                 layout->get_pixel_size (w, h);
318                                 cr->move_to (rint (nw2 - w * .5), y0[2]);
319                                 layout->show_in_cairo_context (cr);
320
321                                 layout->set_font_description (UIConfiguration::instance ().get_LargeFont ());
322                                 layout->set_text (string_compose (_("%1 LU"), std::setprecision (1), std::fixed, p->loudness_range));
323                                 layout->get_pixel_size (w, h);
324                                 cr->move_to (rint (nw2 - w * .5), y0[3]);
325                                 layout->show_in_cairo_context (cr);
326                         }
327                         ebur->flush ();
328
329                         /* draw loudness histogram */
330                         cr = Cairo::Context::create (hist);
331                         cr->set_source_rgba (0, 0, 0, 1.0);
332                         cr->paint ();
333
334                         cr->set_source_rgba (.7, .7, .7, 1.0);
335                         cr->set_line_width (1.0);
336
337                         if (p->loudness_hist_max > 0 && i->second->have_loudness) {
338                                 for (size_t x = 0 ; x < 510; ++x) {
339                                         cr->move_to (x - .5, hh);
340                                         cr->line_to (x - .5, (float) hh * (1.0 - p->loudness_hist[x] / (float) p->loudness_hist_max));
341                                         cr->stroke ();
342                                 }
343                         }
344
345                         layout->set_font_description (UIConfiguration::instance ().get_SmallerFont ());
346                         layout->set_alignment (Pango::ALIGN_CENTER);
347
348                         // Label
349                         layout->set_text (_("LUFS\n(short)"));
350                         layout->get_pixel_size (w, h);
351                         Gtkmm2ext::rounded_rectangle (cr, 5, rint (.5 * (hh - w) - 1), h + 2, w + 2, 4);
352                         cr->set_source_rgba (.1, .1, .1, 0.7);
353                         cr->fill ();
354                         cr->save ();
355                         cr->move_to (6, rint (.5 * (hh + w)));
356                         cr->set_source_rgba (.9, .9, .9, 1.0);
357                         cr->rotate (M_PI / -2.0);
358                         layout->show_in_cairo_context (cr);
359                         cr->restore ();
360
361                         // x-Axis
362                         layout->set_font_description (UIConfiguration::instance ().get_SmallMonospaceFont ());
363                         layout->set_alignment (Pango::ALIGN_LEFT);
364                         for (int g = -53; g <= -8; g += 5) {
365                                 // grid-lines. [110] -59LUFS .. [650]: -5 LUFS
366                                 layout->set_text (string_compose ("%1", std::setw(3), std::setfill(' '), g));
367                                 layout->get_pixel_size (w, h);
368
369                                 cr->set_operator (Cairo::OPERATOR_OVER);
370                                 Gtkmm2ext::rounded_rectangle (cr,
371                                                 rint ((g + 59.0) * 10.0 - h * .5), 5,
372                                                 h + 2, w + 2, 4);
373                                 const float pk = (g + 59.0) / 54.0;
374                                 ArdourCanvas::Color c = ArdourCanvas::hsva_to_color (252 - 260 * pk, .9, .3 + pk * .4, .6);
375                                 ArdourCanvas::set_source_rgba (cr, c);
376                                 cr->fill ();
377
378                                 cr->save ();
379                                 cr->set_source_rgba (.9, .9, .9, 1.0);
380                                 cr->move_to (rint ((g + 59.0) * 10.0 - h * .5), w + 6.0);
381                                 cr->rotate (M_PI / -2.0);
382                                 layout->show_in_cairo_context (cr);
383                                 cr->restore ();
384
385                                 cr->set_operator (Cairo::OPERATOR_ADD);
386                                 cr->save ();
387                                 cr->set_source_rgba (.3, .3, .3, 1.0);
388                                 cr->set_dash (dashes, 1.0);
389                                 cr->set_line_cap (Cairo::LINE_CAP_ROUND);
390                                 cr->move_to (rint ((g + 59.0) * 10.0) + .5, w + 8.0);
391                                 cr->line_to (rint ((g + 59.0) * 10.0) + .5, hh);
392                                 cr->stroke ();
393                                 cr->restore ();
394                         }
395
396                         hist->flush ();
397
398                         CimgArea *nu = manage (new CimgArea (nums));
399                         CimgArea *eb = manage (new CimgArea (ebur));
400                         CimgArea *hi = manage (new CimgArea (hist));
401                         HBox *hb = manage (new HBox ());
402                         hb->set_spacing (4);
403                         hb->pack_start (*nu, false, false);
404                         hb->pack_start (*hi, false, false);
405                         hb->pack_start (*eb, false, false);
406                         vb->pack_start (*hb, false, false);
407                 }
408
409 #define XAXISLABEL(POS, TXT) {                            \
410   const float yy = rint (POS);                            \
411   layout->set_text (TXT);                                 \
412   layout->get_pixel_size (w, h);                          \
413   cr->move_to (m_l - 8 - w, rint ((POS) - h * .5));       \
414   cr->set_source_rgba (.9, .9, .9, 1.0);                  \
415   cr->set_operator (Cairo::OPERATOR_OVER);                \
416   layout->show_in_cairo_context (cr);                     \
417   cr->move_to (m_l - 4, yy - .5);                         \
418   cr->line_to (m_l + width, yy - .5);                     \
419   cr->set_source_rgba (.3, .3, .3, 1.0);                  \
420   cr->set_operator (Cairo::OPERATOR_ADD);                 \
421   cr->stroke ();                                          \
422 }
423
424                 for (uint32_t c = 0; c < p->n_channels; ++c) {
425                         /* draw waveform */
426                         const size_t width = sizeof (p->peaks) / sizeof (ARDOUR::PeakData::PeakDatum) / 4;
427                         const float height_2 = std::min (100, 8 * lin[0] / (int) p->n_channels); // TODO refine
428
429                         Cairo::RefPtr<Cairo::ImageSurface> wave = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, m_l + width, 2 * height_2);
430                         Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create (wave);
431                         cr->set_operator (Cairo::OPERATOR_SOURCE);
432                         cr->rectangle (0, 0, m_l, 2 * height_2);
433                         cr->set_source_rgba (0, 0, 0, 0);
434                         cr->fill ();
435                         cr->rectangle (m_l, 0, width, 2 * height_2);
436                         cr->set_source_rgba (0, 0, 0, 1.0);
437                         cr->fill ();
438                         cr->set_operator (Cairo::OPERATOR_OVER);
439
440                         cr->set_source_rgba (.7, .7, .7, 1.0);
441                         cr->set_line_width (1.0);
442                         for (size_t x = 0 ; x < width; ++x) {
443                                 cr->move_to (m_l + x - .5, height_2 - height_2 * p->peaks[c][x].max);
444                                 cr->line_to (m_l + x - .5, height_2 - height_2 * p->peaks[c][x].min);
445                         }
446                         cr->stroke ();
447
448                         // > 0dBFS
449                         cr->set_source_rgba (1.0, 0, 0, 1.0);
450                         for (size_t x = 0 ; x < width; ++x) {
451                                 if (p->peaks[c][x].max >= 1.0) {
452                                         cr->move_to (m_l + x - .5, 0);
453                                         cr->line_to (m_l + x - .5, height_2 * .22);
454                                 }
455                                 if (p->peaks[c][x].min <= -1.0) {
456                                         cr->move_to (m_l + x - .5, height_2 * 1.78);
457                                         cr->line_to (m_l + x - .5, height_2 * 2.);
458                                 }
459                         }
460                         cr->stroke ();
461
462
463                         // > -1dBTP
464                         cr->set_source_rgba (1.0, 0.7, 0, 0.7);
465                         for (std::set<framepos_t>::const_iterator i = p->truepeakpos[c].begin (); i != p->truepeakpos[c].end (); ++i) {
466                                 cr->move_to (m_l + (*i) - .5, height_2 * 0.22);
467                                 cr->line_to (m_l + (*i) - .5, height_2 * 1.78);
468                                 cr->stroke ();
469                         }
470
471                         // zero line
472                         cr->set_source_rgba (.3, .3, .3, 0.7);
473                         cr->move_to (m_l + 0, height_2 - .5);
474                         cr->line_to (m_l + width, height_2 - .5);
475                         cr->stroke ();
476
477                         // Unit
478                         layout->set_font_description (UIConfiguration::instance ().get_SmallerFont ());
479                         layout->set_alignment (Pango::ALIGN_LEFT);
480                         layout->set_text (_("dBFS"));
481                         layout->get_pixel_size (w, h);
482                         cr->move_to (rint (.5 * (m_l - h)), rint (height_2 + w * .5));
483                         cr->set_source_rgba (.9, .9, .9, 1.0);
484                         cr->save ();
485                         cr->rotate (M_PI / -2.0);
486                         layout->show_in_cairo_context (cr);
487                         cr->restore ();
488
489                         // x-Axis
490                         cr->set_line_width (1.0);
491                         cr->set_dash (dashes, 2.0);
492                         cr->set_line_cap (Cairo::LINE_CAP_ROUND);
493
494                         layout->set_font_description (UIConfiguration::instance ().get_SmallMonospaceFont ());
495                         XAXISLABEL (height_2 * 0.6452, _("-9"));
496                         XAXISLABEL (height_2 * 1.3548, _("-9"));
497                         XAXISLABEL (height_2 * 0.2921, _("-3"));
498                         XAXISLABEL (height_2 * 1.7079, _("-3"));
499
500                         wave->flush ();
501                         CimgArea *wv = manage (new CimgArea (wave));
502                         vb->pack_start (*wv);
503                 }
504
505                 if (channels > 0 && file_length > 0 && sample_rate > 0)
506                 {
507                         /* Time Axis  -- re-use waveform width */
508                         const size_t width = sizeof (p->peaks) / sizeof (ARDOUR::PeakData::PeakDatum) / 4;
509                         layout->set_font_description (UIConfiguration::instance ().get_SmallMonospaceFont ());
510                         layout->set_text (_("00:00:00.000"));
511                         layout->get_pixel_size (w, h);
512                         int height = h * 1.75;
513                         Cairo::RefPtr<Cairo::ImageSurface> ytme = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, m_l + width, height);
514                         Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create (ytme);
515                         cr->set_operator (Cairo::OPERATOR_SOURCE);
516                         cr->set_source_rgba (0, 0, 0, 1.0);
517                         cr->paint ();
518                         cr->rectangle (0, 0, m_l, height);
519                         cr->set_source_rgba (0, 0, 0, 0);
520                         cr->fill ();
521                         cr->set_operator (Cairo::OPERATOR_OVER);
522
523                         cr->set_line_width (1.0);
524                         for (int i = 0; i <= 4; ++i) {
525                                 const float fract = (float) i / 4.0;
526                                 // " XX:XX:XX.XXX"  [space/minus] 12 chars = 13.
527                                 const float xalign = (i == 4) ? 1.0 : (i == 0) ? 1.0 / 13.0 : 7.0 / 13.0;
528
529                                 char buf[16];
530                                 AudioClock::print_minsec (start_off + file_length * fract,
531                                                 buf, sizeof (buf), sample_rate);
532
533                                 layout->set_text (buf);
534                                 layout->get_pixel_size (w, h);
535                                 cr->move_to (rint (m_l + width * fract - w * xalign), rint (.5 * (height - h)));
536                                 cr->set_source_rgba (.9, .9, .9, 1.0);
537                                 layout->show_in_cairo_context (cr);
538
539                                 cr->set_source_rgba (.7, .7, .7, 1.0);
540                                 cr->move_to (rint (m_l + width * fract) - .5, 0);
541                                 cr->line_to (rint (m_l + width * fract) - .5, ceil  (height * .15));
542                                 cr->move_to (rint (m_l + width * fract) - .5, floor (height * .85));
543                                 cr->line_to (rint (m_l + width * fract) - .5, height);
544                                 cr->stroke ();
545                         }
546
547                         layout->set_font_description (UIConfiguration::instance ().get_SmallFont ());
548                         layout->set_text (_("Time"));
549                         cr->set_source_rgba (.9, .9, .9, 1.0);
550                         layout->get_pixel_size (w, h);
551                         cr->move_to (rint (.5 * (m_l - w)), rint (.5 * (height - h)));
552                         layout->show_in_cairo_context (cr);
553
554                         ytme->flush ();
555                         CimgArea *tm = manage (new CimgArea (ytme));
556                         tm->set_audition_axis (m_l, width);
557                         timeline.push_back (tm);
558                         vb->pack_start (*tm);
559                 } else {
560                         timeline.push_back (0);
561                 }
562
563                 {
564                         /* Draw Spectrum */
565                         const size_t swh = sizeof (p->spectrum) / sizeof (float);
566                         const size_t height = sizeof (p->spectrum[0]) / sizeof (float);
567                         const size_t width = swh / height;
568
569                         Cairo::RefPtr<Cairo::ImageSurface> spec = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, m_l + width, height);
570                         Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create (spec);
571                         cr->set_operator (Cairo::OPERATOR_SOURCE);
572                         cr->rectangle (0, 0, m_l, height);
573                         cr->set_source_rgba (0, 0, 0, 0);
574                         cr->fill ();
575                         cr->rectangle (m_l, 0, width, height);
576                         cr->set_source_rgba (0, 0, 0, 1.0);
577                         cr->fill ();
578                         cr->set_operator (Cairo::OPERATOR_OVER);
579
580                         for (size_t x = 0 ; x < width; ++x) {
581                                 for (size_t y = 0 ; y < height; ++y) {
582                                         const float pk = p->spectrum[x][y];
583                                         ArdourCanvas::Color c = ArdourCanvas::hsva_to_color (252 - 260 * pk, .9, .3 + pk * .4);
584                                         ArdourCanvas::set_source_rgba (cr, c);
585                                         cr->rectangle (m_l + x - .5, y - .5, 1, 1);
586                                         cr->fill ();
587                                 }
588                         }
589
590                         // Unit
591                         layout->set_font_description (UIConfiguration::instance ().get_SmallerFont ());
592                         layout->set_text (_("Hz"));
593                         layout->get_pixel_size (w, h);
594                         cr->move_to (rint (.5 * (m_l - h)), rint ((height + w) * .5));
595                         cr->set_source_rgba (.9, .9, .9, 1.0);
596                         cr->save ();
597                         cr->rotate (M_PI / -2.0);
598                         layout->show_in_cairo_context (cr);
599                         cr->restore ();
600
601                         // x-Axis
602                         cr->set_line_width (1.0);
603                         cr->set_dash (dashes, 2.0);
604                         cr->set_line_cap (Cairo::LINE_CAP_ROUND);
605
606                         layout->set_font_description (UIConfiguration::instance ().get_SmallMonospaceFont ());
607                         //XAXISLABEL (p->freq[0], _("50Hz"));
608                         XAXISLABEL (p->freq[1], _("100"));
609                         XAXISLABEL (p->freq[2], _("500"));
610                         XAXISLABEL (p->freq[3], _("1K"));
611                         XAXISLABEL (p->freq[4], _("5K"));
612                         XAXISLABEL (p->freq[5], _("10K"));
613
614                         spec->flush ();
615                         CimgArea *sp = manage (new CimgArea (spec));
616                         vb->pack_start (*sp);
617                 }
618
619                 pages.pages ().push_back (Notebook_Helpers::TabElem (*vb, Glib::path_get_basename (i->first)));
620                 pages.signal_switch_page().connect (sigc::mem_fun (*this, &ExportReport::on_switch_page));
621         }
622
623         pages.set_show_tabs (true);
624         pages.show_all ();
625         pages.set_name ("ExportReportNotebook");
626         pages.set_current_page (0);
627
628         get_vbox ()->set_spacing (4);
629         get_vbox ()->pack_start (pages, false, false);
630
631         if (_session) {
632                 _session->AuditionActive.connect(auditioner_connections, invalidator (*this), boost::bind (&ExportReport::audition_active, this, _1), gui_context());
633                 _session->the_auditioner()->AuditionProgress.connect(auditioner_connections, invalidator (*this), boost::bind (&ExportReport::audition_progress, this, _1, _2), gui_context());
634         }
635
636         stop_btn = add_button (Stock::MEDIA_STOP, RESPONSE_ACCEPT);
637         add_button (Stock::CLOSE, RESPONSE_CLOSE);
638
639         set_default_response (RESPONSE_CLOSE);
640         stop_btn->signal_clicked().connect (sigc::mem_fun (*this, &ExportReport::stop_audition));
641         stop_btn->set_sensitive (false);
642         show_all ();
643 }
644
645 int
646 ExportReport::run ()
647 {
648         do {
649                 int i = ArdourDialog::run ();
650                 if (i == Gtk::RESPONSE_DELETE_EVENT || i == RESPONSE_CLOSE) {
651                         break;
652                 }
653         } while (1);
654
655         if (_session) {
656                 _session->cancel_audition();
657         }
658         return RESPONSE_CLOSE;
659 }
660
661 void
662 ExportReport::open_folder (std::string p)
663 {
664         PBD::open_uri (Glib::path_get_dirname(p));
665 }
666
667 void
668 ExportReport::audition_active (bool active)
669 {
670         stop_btn->set_sensitive (active);
671 }
672
673 void
674 ExportReport::audition (std::string path, unsigned n_chn, int page)
675 {
676         assert (_session);
677         _session->cancel_audition();
678
679         /* can't really happen, unless the user replaces the file while the dialog is open.. */
680         if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
681                 PBD::warning << string_compose(_("Could not read file: %1 (%2)."), path, strerror(errno)) << endmsg;
682                 return;
683         }
684         if (SMFSource::valid_midi_file (path)) { return; }
685
686         boost::shared_ptr<Region> r;
687         SourceList srclist;
688         boost::shared_ptr<AudioFileSource> afs;
689         bool old_sbp = AudioSource::get_build_peakfiles ();
690
691         /* don't even think of building peakfiles for these files */
692         AudioSource::set_build_peakfiles (false);
693
694         for (unsigned int n = 0; n < n_chn; ++n) {
695                 try {
696                         afs = boost::dynamic_pointer_cast<AudioFileSource> (
697                                 SourceFactory::createExternal (DataType::AUDIO, *_session,
698                                                                                  path, n,
699                                                                                  Source::Flag (ARDOUR::AudioFileSource::NoPeakFile), false));
700                         if (afs->sample_rate() != _session->nominal_frame_rate()) {
701                                 boost::shared_ptr<SrcFileSource> sfs (new SrcFileSource(*_session, afs, ARDOUR::SrcGood));
702                                 srclist.push_back(sfs);
703                         } else {
704                                 srclist.push_back(afs);
705                         }
706                 } catch (failed_constructor& err) {
707                         PBD::error << _("Could not access soundfile: ") << path << endmsg;
708                         AudioSource::set_build_peakfiles (old_sbp);
709                         return;
710                 }
711         }
712
713         AudioSource::set_build_peakfiles (old_sbp);
714
715         if (srclist.empty()) {
716                 return;
717         }
718
719         afs = boost::dynamic_pointer_cast<AudioFileSource> (srclist[0]);
720         std::string rname = region_name_from_path (afs->path(), false);
721
722         PBD::PropertyList plist;
723
724         plist.add (ARDOUR::Properties::start, 0);
725         plist.add (ARDOUR::Properties::length, srclist[0]->length(srclist[0]->timeline_position()));
726         plist.add (ARDOUR::Properties::name, rname);
727         plist.add (ARDOUR::Properties::layer, 0);
728
729         r = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (srclist, plist, false));
730
731         r->set_position(0);
732         _session->audition_region(r);
733         _audition_num = page;
734 }
735
736 void
737 ExportReport::stop_audition ()
738 {
739         if (_audition_num == _page_num) {
740                 assert (timeline[_audition_num]);
741                 timeline[_audition_num]->set_playhead (-1);
742         }
743         if (_session) {
744                 _session->cancel_audition();
745         }
746         _audition_num = -1;
747 }
748
749 void
750 ExportReport::on_switch_page (GtkNotebookPage*, guint page_num)
751 {
752         if (_audition_num == _page_num) {
753                 assert (timeline[_audition_num]);
754                 timeline[_audition_num]->set_playhead (-1);
755         }
756         _page_num = page_num;
757 }
758
759 void
760 ExportReport::audition_progress (framecnt_t pos, framecnt_t len)
761 {
762         if (_audition_num == _page_num) {
763                 assert (timeline[_audition_num]);
764                 timeline[_audition_num]->set_playhead ((float)pos / len);
765         }
766 }