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