41b2a9d396c4faea163c360cd1b9ac5fecebcd4d
[dcpomatic.git] / src / wx / audio_dialog.cc
1 /*
2     Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "audio_dialog.h"
22 #include "audio_plot.h"
23 #include "wx_util.h"
24 #include "static_text.h"
25 #include "check_box.h"
26 #include "lib/audio_analysis.h"
27 #include "lib/film.h"
28 #include "lib/analyse_audio_job.h"
29 #include "lib/audio_content.h"
30 #include "lib/job_manager.h"
31 #include <libxml++/libxml++.h>
32 #include <boost/filesystem.hpp>
33 #include <iostream>
34
35 using std::cout;
36 using std::list;
37 using std::vector;
38 using std::pair;
39 using std::shared_ptr;
40 using std::weak_ptr;
41 using boost::bind;
42 using boost::optional;
43 using boost::const_pointer_cast;
44 using std::dynamic_pointer_cast;
45 using namespace dcpomatic;
46 #if BOOST_VERSION >= 106100
47 using namespace boost::placeholders;
48 #endif
49
50 /** @param parent Parent window.
51  *  @param film Film we are using.
52  *  @param content Content to analyse, or 0 to analyse all of the film's audio.
53  */
54 AudioDialog::AudioDialog (wxWindow* parent, shared_ptr<Film> film, weak_ptr<FilmViewer> viewer, shared_ptr<Content> content)
55         : wxDialog (
56                 parent,
57                 wxID_ANY,
58                 _("Audio"),
59                 wxDefaultPosition,
60                 wxSize (640, 512),
61 #ifdef DCPOMATIC_OSX
62                 /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
63                    the window above all others (and not just our own) it's better than nothing for now.
64                 */
65                 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
66 #else
67                 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
68 #endif
69                 )
70         , _film (film)
71         , _viewer (viewer)
72         , _content (content)
73         , _channels (film->audio_channels ())
74         , _plot (0)
75 {
76         wxFont subheading_font (*wxNORMAL_FONT);
77         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
78
79         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
80         wxBoxSizer* lr_sizer = new wxBoxSizer (wxHORIZONTAL);
81
82         wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
83
84         _cursor = new StaticText (this, wxT("Cursor: none"));
85         left->Add (_cursor, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
86         _plot = new AudioPlot (this, viewer);
87         left->Add (_plot, 1, wxTOP | wxEXPAND, 12);
88         _sample_peak = new StaticText (this, wxT (""));
89         left->Add (_sample_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
90         _true_peak = new StaticText (this, wxT (""));
91         left->Add (_true_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
92         _integrated_loudness = new StaticText (this, wxT (""));
93         left->Add (_integrated_loudness, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
94         _loudness_range = new StaticText (this, wxT (""));
95         left->Add (_loudness_range, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
96         _leqm = new StaticText (this, wxT(""));
97         left->Add (_leqm, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
98
99         lr_sizer->Add (left, 1, wxALL | wxEXPAND, 12);
100
101         wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
102
103         {
104                 wxStaticText* m = new StaticText (this, _("Channels"));
105                 m->SetFont (subheading_font);
106                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM, 16);
107         }
108
109         for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
110                 _channel_checkbox[i] = new CheckBox (this, std_to_wx(audio_channel_name(i)));
111                 _channel_checkbox[i]->SetForegroundColour(wxColour(_plot->colour(i)));
112                 right->Add (_channel_checkbox[i], 0, wxEXPAND | wxALL, 3);
113                 _channel_checkbox[i]->Bind (wxEVT_CHECKBOX, boost::bind (&AudioDialog::channel_clicked, this, _1));
114         }
115
116         show_or_hide_channel_checkboxes ();
117
118         {
119                 wxStaticText* m = new StaticText (this, _("Type"));
120                 m->SetFont (subheading_font);
121                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
122         }
123
124         wxString const types[] = {
125                 _("Peak"),
126                 _("RMS")
127         };
128
129         for (int i = 0; i < AudioPoint::COUNT; ++i) {
130                 _type_checkbox[i] = new CheckBox (this, types[i]);
131                 right->Add (_type_checkbox[i], 0, wxEXPAND | wxALL, 3);
132                 _type_checkbox[i]->Bind (wxEVT_CHECKBOX, boost::bind (&AudioDialog::type_clicked, this, _1));
133         }
134
135         {
136                 wxStaticText* m = new StaticText (this, _("Smoothing"));
137                 m->SetFont (subheading_font);
138                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
139         }
140
141         _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
142         _smoothing->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&AudioDialog::smoothing_changed, this));
143         right->Add (_smoothing, 0, wxEXPAND);
144
145         lr_sizer->Add (right, 0, wxALL, 12);
146
147         overall_sizer->Add (lr_sizer, 0, wxEXPAND);
148
149 #ifdef DCPOMATIC_LINUX
150         wxSizer* buttons = CreateSeparatedButtonSizer (wxCLOSE);
151         if (buttons) {
152                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
153         }
154 #endif
155
156         SetSizer (overall_sizer);
157         overall_sizer->Layout ();
158         overall_sizer->SetSizeHints (this);
159
160         _film_connection = film->Change.connect (boost::bind(&AudioDialog::film_change, this, _1, _2));
161         _film_content_connection = film->ContentChange.connect (boost::bind (&AudioDialog::content_change, this, _1, _3));
162         DCPOMATIC_ASSERT (film->directory());
163         SetTitle(wxString::Format(_("DCP-o-matic audio - %s"), std_to_wx(film->directory().get().string())));
164
165         if (content) {
166                 _playlist.reset (new Playlist ());
167                 const_pointer_cast<Playlist>(_playlist)->add(film, content);
168         } else {
169                 _playlist = film->playlist ();
170         }
171
172         _plot->Cursor.connect (bind (&AudioDialog::set_cursor, this, _1, _2));
173 }
174
175
176 void
177 AudioDialog::show_or_hide_channel_checkboxes ()
178 {
179         for (int i = 0; i < _channels; ++i) {
180                 _channel_checkbox[i]->Show ();
181         }
182
183         for (int i = _channels; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
184                 _channel_checkbox[i]->Hide ();
185         }
186 }
187
188
189 void
190 AudioDialog::try_to_load_analysis ()
191 {
192         if (!IsShown ()) {
193                 return;
194         }
195
196         shared_ptr<const Film> film = _film.lock ();
197         DCPOMATIC_ASSERT (film);
198
199         shared_ptr<Content> check = _content.lock();
200
201         boost::filesystem::path const path = film->audio_analysis_path (_playlist);
202         if (!boost::filesystem::exists (path)) {
203                 _plot->set_analysis (shared_ptr<AudioAnalysis> ());
204                 _analysis.reset ();
205
206                 for (auto i: JobManager::instance()->get()) {
207                         if (dynamic_pointer_cast<AnalyseAudioJob>(i)) {
208                                 i->cancel ();
209                         }
210                 }
211
212                 JobManager::instance()->analyse_audio (
213                         film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
214                         );
215                 return;
216         }
217
218         try {
219                 _analysis.reset (new AudioAnalysis (path));
220         } catch (OldFormatError& e) {
221                 /* An old analysis file: recreate it */
222                 JobManager::instance()->analyse_audio (
223                         film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
224                         );
225                 return;
226         } catch (xmlpp::exception& e) {
227                 /* Probably a (very) old-style analysis file: recreate it */
228                 JobManager::instance()->analyse_audio (
229                         film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
230                         );
231                 return;
232         }
233
234         _plot->set_analysis (_analysis);
235         _plot->set_gain_correction (_analysis->gain_correction (_playlist));
236         setup_statistics ();
237         show_or_hide_channel_checkboxes ();
238
239         /* Set up some defaults if no check boxes are checked */
240
241         int i = 0;
242         while (i < _channels && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
243                 ++i;
244         }
245
246         if (i == _channels) {
247                 /* Nothing checked; check mapped ones */
248
249                 list<int> mapped;
250                 shared_ptr<Content> content = _content.lock ();
251
252                 if (content) {
253                         mapped = content->audio->mapping().mapped_output_channels ();
254                 } else {
255                         mapped = film->mapped_audio_channels ();
256                 }
257
258                 for (auto i: mapped) {
259                         if (_channel_checkbox[i]) {
260                                 _channel_checkbox[i]->SetValue (true);
261                                 _plot->set_channel_visible (i, true);
262                         }
263                 }
264         }
265
266         i = 0;
267         while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
268                 i++;
269         }
270
271         if (i == AudioPoint::COUNT) {
272                 for (int i = 0; i < AudioPoint::COUNT; ++i) {
273                         _type_checkbox[i]->SetValue (true);
274                         _plot->set_type_visible (i, true);
275                 }
276         }
277
278         Refresh ();
279 }
280
281 void
282 AudioDialog::analysis_finished ()
283 {
284         shared_ptr<const Film> film = _film.lock ();
285         if (!film) {
286                 /* This should not happen, but if it does we should just give up quietly */
287                 return;
288         }
289
290         if (!boost::filesystem::exists (film->audio_analysis_path (_playlist))) {
291                 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
292                    Give up.
293                 */
294                 _plot->set_message (_("Could not analyse audio."));
295                 return;
296         }
297
298         try_to_load_analysis ();
299 }
300
301 void
302 AudioDialog::channel_clicked (wxCommandEvent& ev)
303 {
304         int c = 0;
305         while (c < _channels && ev.GetEventObject() != _channel_checkbox[c]) {
306                 ++c;
307         }
308
309         DCPOMATIC_ASSERT (c < _channels);
310
311         _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
312 }
313
314 void
315 AudioDialog::film_change (ChangeType type, int p)
316 {
317         if (type != CHANGE_TYPE_DONE) {
318                 return;
319         }
320
321         if (p == Film::AUDIO_CHANNELS) {
322                 shared_ptr<Film> film = _film.lock ();
323                 if (film) {
324                         _channels = film->audio_channels ();
325                         try_to_load_analysis ();
326                 }
327         }
328 }
329
330 void
331 AudioDialog::content_change (ChangeType type, int p)
332 {
333         if (type != CHANGE_TYPE_DONE) {
334                 return;
335         }
336
337         if (p == AudioContentProperty::STREAMS) {
338                 try_to_load_analysis ();
339         } else if (p == AudioContentProperty::GAIN) {
340                 if (_playlist->content().size() == 1 && _analysis) {
341                         /* We can use a short-cut to render the effect of this
342                            change, rather than recalculating everything.
343                         */
344                         _plot->set_gain_correction (_analysis->gain_correction (_playlist));
345                         setup_statistics ();
346                 } else {
347                         try_to_load_analysis ();
348                 }
349         }
350 }
351
352 void
353 AudioDialog::type_clicked (wxCommandEvent& ev)
354 {
355         int t = 0;
356         while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
357                 ++t;
358         }
359
360         DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
361
362         _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
363 }
364
365 void
366 AudioDialog::smoothing_changed ()
367 {
368         _plot->set_smoothing (_smoothing->GetValue ());
369 }
370
371 void
372 AudioDialog::setup_statistics ()
373 {
374         if (!_analysis) {
375                 return;
376         }
377
378         shared_ptr<Film> film = _film.lock ();
379         if (!film) {
380                 return;
381         }
382
383         pair<AudioAnalysis::PeakTime, int> const peak = _analysis->overall_sample_peak ();
384         float const peak_dB = linear_to_db(peak.first.peak) + _analysis->gain_correction(_playlist);
385         _sample_peak->SetLabel (
386                 wxString::Format (
387                         _("Sample peak is %.2fdB at %s on %s"),
388                         peak_dB,
389                         time_to_timecode (peak.first.time, film->video_frame_rate ()).data (),
390                         std_to_wx (short_audio_channel_name (peak.second)).data ()
391                         )
392                 );
393
394         if (peak_dB > -3) {
395                 _sample_peak->SetForegroundColour (wxColour (255, 0, 0));
396         } else {
397                 _sample_peak->SetForegroundColour (wxColour (0, 0, 0));
398         }
399
400         if (_analysis->overall_true_peak()) {
401                 float const peak = _analysis->overall_true_peak().get();
402                 float const peak_dB = linear_to_db(peak) + _analysis->gain_correction(_playlist);
403
404                 _true_peak->SetLabel (wxString::Format (_("True peak is %.2fdB"), peak_dB));
405
406                 if (peak_dB > -3) {
407                         _true_peak->SetForegroundColour (wxColour (255, 0, 0));
408                 } else {
409                         _true_peak->SetForegroundColour (wxColour (0, 0, 0));
410                 }
411         }
412
413         /* XXX: check whether it's ok to add dB gain to these quantities */
414
415         if (static_cast<bool>(_analysis->integrated_loudness ())) {
416                 _integrated_loudness->SetLabel (
417                         wxString::Format (
418                                 _("Integrated loudness %.2f LUFS"),
419                                 _analysis->integrated_loudness().get() + _analysis->gain_correction (_playlist)
420                                 )
421                         );
422         }
423
424         if (static_cast<bool>(_analysis->loudness_range ())) {
425                 _loudness_range->SetLabel (
426                         wxString::Format (
427                                 _("Loudness range %.2f LU"),
428                                 _analysis->loudness_range().get() + _analysis->gain_correction (_playlist)
429                                 )
430                         );
431         }
432
433         if (static_cast<bool>(_analysis->leqm())) {
434                 _leqm->SetLabel(
435                         wxString::Format(
436                                 _("LEQ(m) %.2fdB"), _analysis->leqm().get() + _analysis->gain_correction(_playlist)
437                                 )
438                         );
439         }
440 }
441
442 bool
443 AudioDialog::Show (bool show)
444 {
445         bool const r = wxDialog::Show (show);
446         try_to_load_analysis ();
447         return r;
448 }
449
450 void
451 AudioDialog::set_cursor (optional<DCPTime> time, optional<float> db)
452 {
453         if (!time || !db) {
454                 _cursor->SetLabel (_("Cursor: none"));
455                 return;
456         }
457
458         shared_ptr<Film> film = _film.lock();
459         DCPOMATIC_ASSERT (film);
460         _cursor->SetLabel (wxString::Format (_("Cursor: %.1fdB at %s"), *db, time->timecode(film->video_frame_rate())));
461 }