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