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