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