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