Cleanup: move some methods from util to maths_util.
[dcpomatic.git] / src / wx / audio_panel.cc
1 /*
2     Copyright (C) 2012-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
22 #include "audio_dialog.h"
23 #include "audio_mapping_view.h"
24 #include "audio_panel.h"
25 #include "check_box.h"
26 #include "content_panel.h"
27 #include "dcpomatic_button.h"
28 #include "gain_calculator_dialog.h"
29 #include "static_text.h"
30 #include "wx_util.h"
31 #include "lib/config.h"
32 #include "lib/ffmpeg_audio_stream.h"
33 #include "lib/ffmpeg_content.h"
34 #include "lib/cinema_sound_processor.h"
35 #include "lib/job_manager.h"
36 #include "lib/dcp_content.h"
37 #include "lib/audio_content.h"
38 #include "lib/maths_util.h"
39 #include <wx/spinctrl.h>
40 #include <iostream>
41
42
43 using std::vector;
44 using std::cout;
45 using std::string;
46 using std::list;
47 using std::pair;
48 using std::make_shared;
49 using std::dynamic_pointer_cast;
50 using std::shared_ptr;
51 using boost::optional;
52 #if BOOST_VERSION >= 106100
53 using namespace boost::placeholders;
54 #endif
55
56
57 AudioPanel::AudioPanel (ContentPanel* p)
58         : ContentSubPanel (p, _("Audio"))
59         , _audio_dialog (0)
60 {
61
62 }
63
64
65 void
66 AudioPanel::create ()
67 {
68         _reference = new CheckBox (this, _("Use this DCP's audio as OV and make VF"));
69         _reference_note = new StaticText (this, wxT(""));
70         _reference_note->Wrap (200);
71         auto font = _reference_note->GetFont();
72         font.SetStyle(wxFONTSTYLE_ITALIC);
73         font.SetPointSize(font.GetPointSize() - 1);
74         _reference_note->SetFont(font);
75
76         _show = new Button (this, _("Show graph of audio levels..."));
77         _peak = new StaticText (this, wxT (""));
78
79         _gain_label = create_label (this, _("Gain"), true);
80         _gain = new ContentSpinCtrlDouble<AudioContent> (
81                 this,
82                 new wxSpinCtrlDouble (this),
83                 AudioContentProperty::GAIN,
84                 &Content::audio,
85                 boost::mem_fn (&AudioContent::gain),
86                 boost::mem_fn (&AudioContent::set_gain)
87                 );
88
89         _gain_db_label = create_label (this, _("dB"), false);
90         _gain_calculate_button = new Button (this, _("Calculate..."));
91
92         _delay_label = create_label (this, _("Delay"), true);
93         _delay = new ContentSpinCtrl<AudioContent> (
94                 this,
95                 new wxSpinCtrl (this),
96                 AudioContentProperty::DELAY,
97                 &Content::audio,
98                 boost::mem_fn (&AudioContent::delay),
99                 boost::mem_fn (&AudioContent::set_delay)
100                 );
101
102         /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
103         _delay_ms_label = create_label (this, _("ms"), false);
104
105         _mapping = new AudioMappingView (this, _("Content"), _("content"), _("DCP"), _("DCP"));
106         _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
107
108         _description = new StaticText (this, wxT(" \n"), wxDefaultPosition, wxDefaultSize);
109         _sizer->Add (_description, 0, wxALL, 12);
110         _description->SetFont (font);
111
112         _gain->wrapped()->SetRange (-60, 60);
113         _gain->wrapped()->SetDigits (1);
114         _gain->wrapped()->SetIncrement (0.5);
115         _delay->wrapped()->SetRange (-1000, 1000);
116
117         content_selection_changed ();
118         film_changed (Film::Property::AUDIO_CHANNELS);
119         film_changed (Film::Property::VIDEO_FRAME_RATE);
120         film_changed (Film::Property::REEL_TYPE);
121
122         _reference->Bind             (wxEVT_CHECKBOX, boost::bind (&AudioPanel::reference_clicked, this));
123         _show->Bind                  (wxEVT_BUTTON,   boost::bind (&AudioPanel::show_clicked, this));
124         _gain_calculate_button->Bind (wxEVT_BUTTON,   boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
125
126         _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
127         _active_jobs_connection = JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1, _2));
128
129         add_to_grid ();
130
131         _sizer->Layout ();
132 }
133
134 void
135 AudioPanel::add_to_grid ()
136 {
137         int r = 0;
138
139         auto reference_sizer = new wxBoxSizer (wxVERTICAL);
140         reference_sizer->Add (_reference, 0);
141         reference_sizer->Add (_reference_note, 0);
142         _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 4));
143         ++r;
144
145         _grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
146         _grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
147         ++r;
148
149         add_label_to_sizer (_grid, _gain_label, true, wxGBPosition(r, 0));
150         {
151                 auto s = new wxBoxSizer (wxHORIZONTAL);
152                 s->Add (_gain->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
153                 s->Add (_gain_db_label, 0, wxALIGN_CENTER_VERTICAL);
154                 _grid->Add (s, wxGBPosition(r, 1));
155         }
156
157         _grid->Add (_gain_calculate_button, wxGBPosition(r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
158         ++r;
159
160         add_label_to_sizer (_grid, _delay_label, true, wxGBPosition(r, 0));
161         auto s = new wxBoxSizer (wxHORIZONTAL);
162         s->Add (_delay->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
163         s->Add (_delay_ms_label, 0, wxALIGN_CENTER_VERTICAL);
164         _grid->Add (s, wxGBPosition(r, 1));
165         ++r;
166 }
167
168 AudioPanel::~AudioPanel ()
169 {
170         if (_audio_dialog) {
171                 _audio_dialog->Destroy ();
172                 _audio_dialog = nullptr;
173         }
174 }
175
176 void
177 AudioPanel::film_changed (Film::Property property)
178 {
179         if (!_parent->film()) {
180                 return;
181         }
182
183         switch (property) {
184         case Film::Property::AUDIO_CHANNELS:
185         case Film::Property::AUDIO_PROCESSOR:
186                 _mapping->set_output_channels (_parent->film()->audio_output_names ());
187                 setup_peak ();
188                 break;
189         case Film::Property::VIDEO_FRAME_RATE:
190                 setup_description ();
191                 break;
192         case Film::Property::REEL_TYPE:
193         case Film::Property::INTEROP:
194                 setup_sensitivity ();
195                 break;
196         default:
197                 break;
198         }
199 }
200
201 void
202 AudioPanel::film_content_changed (int property)
203 {
204         auto ac = _parent->selected_audio ();
205         if (property == AudioContentProperty::STREAMS) {
206                 if (ac.size() == 1) {
207                         _mapping->set (ac.front()->audio->mapping());
208                         _mapping->set_input_channels (ac.front()->audio->channel_names ());
209
210                         vector<AudioMappingView::Group> groups;
211                         int c = 0;
212                         for (auto i: ac.front()->audio->streams()) {
213                                 auto f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
214                                 string name = "";
215                                 if (f) {
216                                         name = f->name;
217                                         if (f->codec_name) {
218                                                 name += " (" + f->codec_name.get() + ")";
219                                         }
220                                 }
221                                 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
222                                 c += i->channels ();
223                         }
224                         _mapping->set_input_groups (groups);
225
226                 } else {
227                         _mapping->set (AudioMapping ());
228                 }
229                 setup_description ();
230                 setup_peak ();
231                 layout ();
232         } else if (property == AudioContentProperty::GAIN) {
233                 setup_peak ();
234         } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
235                 if (ac.size() == 1) {
236                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
237                         checked_set (_reference, dcp ? dcp->reference_audio () : false);
238                 } else {
239                         checked_set (_reference, false);
240                 }
241
242                 setup_sensitivity ();
243         } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
244                 setup_description ();
245         }
246 }
247
248 void
249 AudioPanel::gain_calculate_button_clicked ()
250 {
251         auto d = new GainCalculatorDialog (this);
252         auto const r = d->ShowModal ();
253         auto c = d->db_change();
254
255         if (r == wxID_CANCEL || !c) {
256                 d->Destroy ();
257                 return;
258         }
259
260         auto old_peak_dB = peak ();
261         auto old_value = _gain->wrapped()->GetValue();
262         _gain->wrapped()->SetValue(old_value + *c);
263
264         /* This appears to be necessary, as the change is not signalled,
265            I think.
266         */
267         _gain->view_changed ();
268
269         auto peak_dB = peak ();
270         if (old_peak_dB && *old_peak_dB < -0.5 && peak_dB && *peak_dB > -0.5) {
271                 error_dialog (this, _("It is not possible to adjust the content's gain for this fader change as it would cause the DCP's audio to clip.  The gain has not been changed."));
272                 _gain->wrapped()->SetValue (old_value);
273                 _gain->view_changed ();
274         }
275
276         d->Destroy ();
277 }
278
279 void
280 AudioPanel::setup_description ()
281 {
282         auto ac = _parent->selected_audio ();
283         if (ac.size () != 1) {
284                 checked_set (_description, wxT (""));
285                 return;
286         }
287
288         checked_set (_description, ac.front()->audio->processing_description(_parent->film()));
289 }
290
291 void
292 AudioPanel::mapping_changed (AudioMapping m)
293 {
294         auto c = _parent->selected_audio ();
295         if (c.size() == 1) {
296                 c.front()->audio->set_mapping (m);
297         }
298 }
299
300 void
301 AudioPanel::content_selection_changed ()
302 {
303         auto sel = _parent->selected_audio ();
304
305         _gain->set_content (sel);
306         _delay->set_content (sel);
307
308         film_content_changed (AudioContentProperty::STREAMS);
309         film_content_changed (AudioContentProperty::GAIN);
310         film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
311
312         setup_sensitivity ();
313 }
314
315 void
316 AudioPanel::setup_sensitivity ()
317 {
318         auto sel = _parent->selected_audio ();
319
320         shared_ptr<DCPContent> dcp;
321         if (sel.size() == 1) {
322                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
323         }
324
325         string why_not;
326         bool const can_reference = dcp && dcp->can_reference_audio (_parent->film(), why_not);
327         wxString cannot;
328         if (why_not.empty()) {
329                 cannot = _("Cannot reference this DCP's audio.");
330         } else {
331                 cannot = _("Cannot reference this DCP's audio: ") + std_to_wx(why_not);
332         }
333         setup_refer_button (_reference, _reference_note, dcp, can_reference, cannot);
334
335         auto const ref = _reference->GetValue();
336         auto const single = sel.size() == 1;
337
338         _gain->wrapped()->Enable (!ref);
339         _gain_calculate_button->Enable (!ref && single);
340         _show->Enable (single);
341         _peak->Enable (!ref && single);
342         _delay->wrapped()->Enable (!ref);
343         _mapping->Enable (!ref && single);
344         _description->Enable (!ref && single);
345 }
346
347 void
348 AudioPanel::show_clicked ()
349 {
350         if (_audio_dialog) {
351                 _audio_dialog->Destroy ();
352                 _audio_dialog = nullptr;
353         }
354
355         auto ac = _parent->selected_audio ();
356         if (ac.size() != 1) {
357                 return;
358         }
359
360         _audio_dialog = new AudioDialog (this, _parent->film(), _parent->film_viewer(), ac.front());
361         _audio_dialog->Show ();
362 }
363
364 /** @return If there is one selected piece of audio content, return its peak value in dB (if known) */
365 optional<float>
366 AudioPanel::peak () const
367 {
368         optional<float> peak_dB;
369
370         auto sel = _parent->selected_audio ();
371         if (sel.size() == 1) {
372                 auto playlist = make_shared<Playlist>();
373                 playlist->add (_parent->film(), sel.front());
374                 try {
375                         auto analysis = make_shared<AudioAnalysis>(_parent->film()->audio_analysis_path(playlist));
376                         peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
377                 } catch (...) {
378
379                 }
380         }
381
382         return peak_dB;
383 }
384
385 void
386 AudioPanel::setup_peak ()
387 {
388         auto sel = _parent->selected_audio ();
389
390         auto peak_dB = peak ();
391         if (sel.size() != 1) {
392                 _peak->SetLabel (wxT(""));
393         } else {
394                 peak_dB = peak ();
395                 if (peak_dB) {
396                         _peak->SetLabel (wxString::Format(_("Peak: %.2fdB"), *peak_dB));
397                 } else {
398                         _peak->SetLabel (_("Peak: unknown"));
399                 }
400         }
401
402         static auto normal = _peak->GetForegroundColour ();
403
404         if (peak_dB && *peak_dB > -0.5) {
405                 _peak->SetForegroundColour (wxColour (255, 0, 0));
406         } else if (peak_dB && *peak_dB > -3) {
407                 _peak->SetForegroundColour (wxColour (186, 120, 0));
408         } else {
409                 _peak->SetForegroundColour (normal);
410         }
411 }
412
413 void
414 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
415 {
416         if (old_active && *old_active == "analyse_audio") {
417                 setup_peak ();
418                 _mapping->Enable (true);
419         } else if (new_active && *new_active == "analyse_audio") {
420                 _mapping->Enable (false);
421         }
422 }
423
424 void
425 AudioPanel::reference_clicked ()
426 {
427         auto c = _parent->selected ();
428         if (c.size() != 1) {
429                 return;
430         }
431
432         auto d = dynamic_pointer_cast<DCPContent>(c.front());
433         if (!d) {
434                 return;
435         }
436
437         d->set_reference_audio (_reference->GetValue ());
438 }
439
440 void
441 AudioPanel::set_film (shared_ptr<Film>)
442 {
443         /* We are changing film, so destroy any audio dialog for the old one */
444         if (_audio_dialog) {
445                 _audio_dialog->Destroy ();
446                 _audio_dialog = nullptr;
447         }
448 }
449