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