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