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