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