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