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