Fix crash when analysing audio of large-channel-count files.
[dcpomatic.git] / src / wx / audio_dialog.cc
1 /*
2     Copyright (C) 2013-2016 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 "lib/audio_analysis.h"
25 #include "lib/film.h"
26 #include "lib/analyse_audio_job.h"
27 #include "lib/audio_content.h"
28 #include "lib/job_manager.h"
29 #include <libxml++/libxml++.h>
30 #include <boost/filesystem.hpp>
31 #include <boost/foreach.hpp>
32 #include <iostream>
33
34 using std::cout;
35 using std::list;
36 using boost::shared_ptr;
37 using boost::bind;
38 using boost::optional;
39 using boost::const_pointer_cast;
40 using boost::dynamic_pointer_cast;
41
42 /** @param content Content to analyse, or 0 to analyse all of the film's audio */
43 AudioDialog::AudioDialog (wxWindow* parent, shared_ptr<Film> film, shared_ptr<Content> content)
44         : wxDialog (
45                 parent,
46                 wxID_ANY,
47                 _("Audio"),
48                 wxDefaultPosition,
49                 wxSize (640, 512),
50                 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
51                 )
52         , _film (film)
53         , _content (content)
54         , _channels (film->audio_channels ())
55         , _plot (0)
56 {
57         wxFont subheading_font (*wxNORMAL_FONT);
58         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
59
60         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
61         wxBoxSizer* lr_sizer = new wxBoxSizer (wxHORIZONTAL);
62
63         wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
64
65         _plot = new AudioPlot (this);
66         left->Add (_plot, 1, wxTOP | wxEXPAND, 12);
67         _sample_peak = new wxStaticText (this, wxID_ANY, wxT (""));
68         left->Add (_sample_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
69         _true_peak = new wxStaticText (this, wxID_ANY, wxT (""));
70         left->Add (_true_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
71         _integrated_loudness = new wxStaticText (this, wxID_ANY, wxT (""));
72         left->Add (_integrated_loudness, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
73         _loudness_range = new wxStaticText (this, wxID_ANY, wxT (""));
74         left->Add (_loudness_range, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
75
76         lr_sizer->Add (left, 1, wxALL, 12);
77
78         wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
79
80         {
81                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Channels"));
82                 m->SetFont (subheading_font);
83                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM, 16);
84         }
85
86         for (int i = 0; i < _channels; ++i) {
87                 _channel_checkbox[i] = new wxCheckBox (this, wxID_ANY, std_to_wx (audio_channel_name (i)));
88                 right->Add (_channel_checkbox[i], 0, wxEXPAND | wxALL, 3);
89                 _channel_checkbox[i]->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioDialog::channel_clicked, this, _1));
90         }
91
92         for (int i = _channels; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
93                 _channel_checkbox[i] = 0;
94         }
95
96         {
97                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Type"));
98                 m->SetFont (subheading_font);
99                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
100         }
101
102         wxString const types[] = {
103                 _("Peak"),
104                 _("RMS")
105         };
106
107         for (int i = 0; i < AudioPoint::COUNT; ++i) {
108                 _type_checkbox[i] = new wxCheckBox (this, wxID_ANY, types[i]);
109                 right->Add (_type_checkbox[i], 0, wxEXPAND | wxALL, 3);
110                 _type_checkbox[i]->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioDialog::type_clicked, this, _1));
111         }
112
113         {
114                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Smoothing"));
115                 m->SetFont (subheading_font);
116                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
117         }
118
119         _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
120         _smoothing->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&AudioDialog::smoothing_changed, this));
121         right->Add (_smoothing, 0, wxEXPAND);
122
123         lr_sizer->Add (right, 0, wxALL, 12);
124
125         overall_sizer->Add (lr_sizer);
126
127 #ifdef DCPOMATIC_LINUX
128         wxSizer* buttons = CreateSeparatedButtonSizer (wxCLOSE);
129         if (buttons) {
130                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
131         }
132 #endif
133
134         SetSizer (overall_sizer);
135         overall_sizer->Layout ();
136         overall_sizer->SetSizeHints (this);
137
138         _film_connection = film->ContentChanged.connect (boost::bind (&AudioDialog::content_changed, this, _2));
139         SetTitle (_("DCP-o-matic audio"));
140
141         if (content) {
142                 _playlist.reset (new Playlist ());
143                 const_pointer_cast<Playlist> (_playlist)->add (content);
144         } else {
145                 _playlist = film->playlist ();
146         }
147 }
148
149 void
150 AudioDialog::try_to_load_analysis ()
151 {
152         if (!IsShown ()) {
153                 return;
154         }
155
156         shared_ptr<const Film> film = _film.lock ();
157         DCPOMATIC_ASSERT (film);
158
159         boost::filesystem::path const path = film->audio_analysis_path (_playlist);
160         if (!boost::filesystem::exists (path)) {
161                 _plot->set_analysis (shared_ptr<AudioAnalysis> ());
162                 _analysis.reset ();
163                 JobManager::instance()->analyse_audio (film, _playlist, _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this));
164                 return;
165         }
166
167         try {
168                 _analysis.reset (new AudioAnalysis (path));
169         } catch (xmlpp::exception& e) {
170                 /* Probably an old-style analysis file: recreate it */
171                 JobManager::instance()->analyse_audio (film, _playlist, _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this));
172                 return;
173         }
174
175         _plot->set_analysis (_analysis);
176         _plot->set_gain_correction (_analysis->gain_correction (_playlist));
177         setup_statistics ();
178
179         /* Set up some defaults if no check boxes are checked */
180
181         int i = 0;
182         while (i < _channels && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
183                 ++i;
184         }
185
186         if (i == _channels) {
187                 /* Nothing checked; check mapped ones */
188
189                 list<int> mapped;
190                 shared_ptr<Content> content = _content.lock ();
191
192                 if (content) {
193                         mapped = content->audio->mapping().mapped_output_channels ();
194                 } else {
195                         mapped = film->mapped_audio_channels ();
196                 }
197
198                 BOOST_FOREACH (int i, mapped) {
199                         if (_channel_checkbox[i]) {
200                                 _channel_checkbox[i]->SetValue (true);
201                                 _plot->set_channel_visible (i, true);
202                         }
203                 }
204         }
205
206         i = 0;
207         while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
208                 i++;
209         }
210
211         if (i == AudioPoint::COUNT) {
212                 for (int i = 0; i < AudioPoint::COUNT; ++i) {
213                         _type_checkbox[i]->SetValue (true);
214                         _plot->set_type_visible (i, true);
215                 }
216         }
217
218         Refresh ();
219 }
220
221 void
222 AudioDialog::analysis_finished ()
223 {
224         shared_ptr<const Film> film = _film.lock ();
225         if (!film) {
226                 /* This should not happen, but if it does we should just give up quietly */
227                 return;
228         }
229
230         if (!boost::filesystem::exists (film->audio_analysis_path (_playlist))) {
231                 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
232                    Give up.
233                 */
234                 _plot->set_message (_("Could not analyse audio."));
235                 return;
236         }
237
238         try_to_load_analysis ();
239 }
240
241 void
242 AudioDialog::channel_clicked (wxCommandEvent& ev)
243 {
244         int c = 0;
245         while (c < _channels && ev.GetEventObject() != _channel_checkbox[c]) {
246                 ++c;
247         }
248
249         DCPOMATIC_ASSERT (c < _channels);
250
251         _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
252 }
253
254 void
255 AudioDialog::content_changed (int p)
256 {
257         if (p == AudioContentProperty::STREAMS) {
258                 try_to_load_analysis ();
259         } else if (p == AudioContentProperty::GAIN) {
260                 if (_playlist->content().size() == 1 && _analysis) {
261                         /* We can use a short-cut to render the effect of this
262                            change, rather than recalculating everything.
263                         */
264                         _plot->set_gain_correction (_analysis->gain_correction (_playlist));
265                         setup_statistics ();
266                 } else {
267                         try_to_load_analysis ();
268                 }
269         }
270 }
271
272 void
273 AudioDialog::type_clicked (wxCommandEvent& ev)
274 {
275         int t = 0;
276         while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
277                 ++t;
278         }
279
280         DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
281
282         _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
283 }
284
285 void
286 AudioDialog::smoothing_changed ()
287 {
288         _plot->set_smoothing (_smoothing->GetValue ());
289 }
290
291 void
292 AudioDialog::setup_statistics ()
293 {
294         if (!_analysis) {
295                 return;
296         }
297
298         shared_ptr<Film> film = _film.lock ();
299         if (!film) {
300                 return;
301         }
302
303         if (static_cast<bool>(_analysis->sample_peak ())) {
304
305                 float const peak_dB = 20 * log10 (_analysis->sample_peak().get()) + _analysis->gain_correction (_playlist);
306
307                 _sample_peak->SetLabel (
308                         wxString::Format (
309                                 _("Sample peak is %.2fdB at %s"),
310                                 peak_dB,
311                                 time_to_timecode (_analysis->sample_peak_time().get(), film->video_frame_rate ()).data ()
312                                 )
313                         );
314
315                 if (peak_dB > -3) {
316                         _sample_peak->SetForegroundColour (wxColour (255, 0, 0));
317                 } else {
318                         _sample_peak->SetForegroundColour (wxColour (0, 0, 0));
319                 }
320         }
321
322         if (static_cast<bool>(_analysis->true_peak ())) {
323                 float const peak_dB = 20 * log10 (_analysis->true_peak().get()) + _analysis->gain_correction (_playlist);
324
325                 _true_peak->SetLabel (wxString::Format (_("True peak is %.2fdB"), peak_dB));
326
327                 if (peak_dB > -3) {
328                         _true_peak->SetForegroundColour (wxColour (255, 0, 0));
329                 } else {
330                         _true_peak->SetForegroundColour (wxColour (0, 0, 0));
331                 }
332         }
333
334         /* XXX: check whether it's ok to add dB gain to these quantities */
335
336         if (static_cast<bool>(_analysis->integrated_loudness ())) {
337                 _integrated_loudness->SetLabel (
338                         wxString::Format (
339                                 _("Integrated loudness %.2f LUFS"),
340                                 _analysis->integrated_loudness().get() + _analysis->gain_correction (_playlist)
341                                 )
342                         );
343         }
344
345         if (static_cast<bool>(_analysis->loudness_range ())) {
346                 _loudness_range->SetLabel (
347                         wxString::Format (
348                                 _("Loudness range %.2f LU"),
349                                 _analysis->loudness_range().get() + _analysis->gain_correction (_playlist)
350                                 )
351                         );
352         }
353 }
354
355 bool
356 AudioDialog::Show (bool show)
357 {
358         bool const r = wxDialog::Show (show);
359         try_to_load_analysis ();
360         return r;
361 }