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