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