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