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