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