Potential fix for crash on loading a new film after starting an audio analysis.
[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         {
93                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Type"));
94                 m->SetFont (subheading_font);
95                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
96         }
97
98         wxString const types[] = {
99                 _("Peak"),
100                 _("RMS")
101         };
102
103         for (int i = 0; i < AudioPoint::COUNT; ++i) {
104                 _type_checkbox[i] = new wxCheckBox (this, wxID_ANY, types[i]);
105                 right->Add (_type_checkbox[i], 0, wxEXPAND | wxALL, 3);
106                 _type_checkbox[i]->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioDialog::type_clicked, this, _1));
107         }
108
109         {
110                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Smoothing"));
111                 m->SetFont (subheading_font);
112                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
113         }
114
115         _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
116         _smoothing->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&AudioDialog::smoothing_changed, this));
117         right->Add (_smoothing, 0, wxEXPAND);
118
119         lr_sizer->Add (right, 0, wxALL, 12);
120
121         overall_sizer->Add (lr_sizer);
122
123 #ifdef DCPOMATIC_LINUX
124         wxSizer* buttons = CreateSeparatedButtonSizer (wxCLOSE);
125         if (buttons) {
126                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
127         }
128 #endif
129
130         SetSizer (overall_sizer);
131         overall_sizer->Layout ();
132         overall_sizer->SetSizeHints (this);
133
134         _film_connection = film->ContentChanged.connect (boost::bind (&AudioDialog::content_changed, this, _2));
135         SetTitle (_("DCP-o-matic audio"));
136
137         if (content) {
138                 _playlist.reset (new Playlist ());
139                 const_pointer_cast<Playlist> (_playlist)->add (content);
140         } else {
141                 _playlist = film->playlist ();
142         }
143 }
144
145 void
146 AudioDialog::try_to_load_analysis ()
147 {
148         if (!IsShown ()) {
149                 return;
150         }
151
152         shared_ptr<const Film> film = _film.lock ();
153         DCPOMATIC_ASSERT (film);
154
155         boost::filesystem::path const path = film->audio_analysis_path (_playlist);
156         if (!boost::filesystem::exists (path)) {
157                 _plot->set_analysis (shared_ptr<AudioAnalysis> ());
158                 _analysis.reset ();
159                 JobManager::instance()->analyse_audio (film, _playlist, _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this));
160                 return;
161         }
162
163         try {
164                 _analysis.reset (new AudioAnalysis (path));
165         } catch (xmlpp::exception& e) {
166                 /* Probably an old-style analysis file: recreate it */
167                 JobManager::instance()->analyse_audio (film, _playlist, _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this));
168                 return;
169         }
170
171         _plot->set_analysis (_analysis);
172         _plot->set_gain_correction (_analysis->gain_correction (_playlist));
173         setup_statistics ();
174
175         /* Set up some defaults if no check boxes are checked */
176
177         int i = 0;
178         while (i < _channels && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
179                 ++i;
180         }
181
182         if (i == _channels) {
183                 /* Nothing checked; check mapped ones */
184
185                 list<int> mapped;
186                 shared_ptr<Content> content = _content.lock ();
187
188                 if (content) {
189                         mapped = content->audio->mapping().mapped_output_channels ();
190                 } else {
191                         mapped = film->mapped_audio_channels ();
192                 }
193
194                 BOOST_FOREACH (int i, mapped) {
195                         if (_channel_checkbox[i]) {
196                                 _channel_checkbox[i]->SetValue (true);
197                                 _plot->set_channel_visible (i, true);
198                         }
199                 }
200         }
201
202         i = 0;
203         while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
204                 i++;
205         }
206
207         if (i == AudioPoint::COUNT) {
208                 for (int i = 0; i < AudioPoint::COUNT; ++i) {
209                         _type_checkbox[i]->SetValue (true);
210                         _plot->set_type_visible (i, true);
211                 }
212         }
213
214         Refresh ();
215 }
216
217 void
218 AudioDialog::analysis_finished ()
219 {
220         shared_ptr<const Film> film = _film.lock ();
221         if (!film) {
222                 /* This should not happen, but if it does we should just give up quietly */
223                 return;
224         }
225
226         if (!boost::filesystem::exists (film->audio_analysis_path (_playlist))) {
227                 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
228                    Give up.
229                 */
230                 _plot->set_message (_("Could not analyse audio."));
231                 return;
232         }
233
234         try_to_load_analysis ();
235 }
236
237 void
238 AudioDialog::channel_clicked (wxCommandEvent& ev)
239 {
240         int c = 0;
241         while (c < _channels && ev.GetEventObject() != _channel_checkbox[c]) {
242                 ++c;
243         }
244
245         DCPOMATIC_ASSERT (c < _channels);
246
247         _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
248 }
249
250 void
251 AudioDialog::content_changed (int p)
252 {
253         if (p == AudioContentProperty::STREAMS) {
254                 try_to_load_analysis ();
255         } else if (p == AudioContentProperty::GAIN) {
256                 if (_playlist->content().size() == 1 && _analysis) {
257                         /* We can use a short-cut to render the effect of this
258                            change, rather than recalculating everything.
259                         */
260                         _plot->set_gain_correction (_analysis->gain_correction (_playlist));
261                         setup_statistics ();
262                 } else {
263                         try_to_load_analysis ();
264                 }
265         }
266 }
267
268 void
269 AudioDialog::type_clicked (wxCommandEvent& ev)
270 {
271         int t = 0;
272         while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
273                 ++t;
274         }
275
276         DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
277
278         _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
279 }
280
281 void
282 AudioDialog::smoothing_changed ()
283 {
284         _plot->set_smoothing (_smoothing->GetValue ());
285 }
286
287 void
288 AudioDialog::setup_statistics ()
289 {
290         if (!_analysis) {
291                 return;
292         }
293
294         shared_ptr<Film> film = _film.lock ();
295         if (!film) {
296                 return;
297         }
298
299         if (static_cast<bool>(_analysis->sample_peak ())) {
300
301                 float const peak_dB = 20 * log10 (_analysis->sample_peak().get()) + _analysis->gain_correction (_playlist);
302
303                 _sample_peak->SetLabel (
304                         wxString::Format (
305                                 _("Sample peak is %.2fdB at %s"),
306                                 peak_dB,
307                                 time_to_timecode (_analysis->sample_peak_time().get(), film->video_frame_rate ()).data ()
308                                 )
309                         );
310
311                 if (peak_dB > -3) {
312                         _sample_peak->SetForegroundColour (wxColour (255, 0, 0));
313                 } else {
314                         _sample_peak->SetForegroundColour (wxColour (0, 0, 0));
315                 }
316         }
317
318         if (static_cast<bool>(_analysis->true_peak ())) {
319                 float const peak_dB = 20 * log10 (_analysis->true_peak().get()) + _analysis->gain_correction (_playlist);
320
321                 _true_peak->SetLabel (wxString::Format (_("True peak is %.2fdB"), peak_dB));
322
323                 if (peak_dB > -3) {
324                         _true_peak->SetForegroundColour (wxColour (255, 0, 0));
325                 } else {
326                         _true_peak->SetForegroundColour (wxColour (0, 0, 0));
327                 }
328         }
329
330         /* XXX: check whether it's ok to add dB gain to these quantities */
331
332         if (static_cast<bool>(_analysis->integrated_loudness ())) {
333                 _integrated_loudness->SetLabel (
334                         wxString::Format (
335                                 _("Integrated loudness %.2f LUFS"),
336                                 _analysis->integrated_loudness().get() + _analysis->gain_correction (_playlist)
337                                 )
338                         );
339         }
340
341         if (static_cast<bool>(_analysis->loudness_range ())) {
342                 _loudness_range->SetLabel (
343                         wxString::Format (
344                                 _("Loudness range %.2f LU"),
345                                 _analysis->loudness_range().get() + _analysis->gain_correction (_playlist)
346                                 )
347                         );
348         }
349 }
350
351 bool
352 AudioDialog::Show (bool show)
353 {
354         bool const r = wxDialog::Show (show);
355         try_to_load_analysis ();
356         return r;
357 }