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