Supporters update.
[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                 setup_sensitivity();
212                 break;
213         case FilmProperty::VIDEO_FRAME_RATE:
214                 setup_description ();
215                 break;
216         case FilmProperty::REEL_TYPE:
217         case FilmProperty::INTEROP:
218                 setup_sensitivity ();
219                 break;
220         default:
221                 break;
222         }
223 }
224
225
226 void
227 AudioPanel::film_content_changed (int property)
228 {
229         auto ac = _parent->selected_audio ();
230         if (property == AudioContentProperty::STREAMS) {
231                 if (ac.size() == 1) {
232                         _mapping->set (ac.front()->audio->mapping());
233                         _mapping->set_input_channels (ac.front()->audio->channel_names ());
234
235                         vector<AudioMappingView::Group> groups;
236                         int c = 0;
237                         for (auto i: ac.front()->audio->streams()) {
238                                 auto f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
239                                 string name = "";
240                                 if (f) {
241                                         name = f->name;
242                                         if (f->codec_name) {
243                                                 name += " (" + f->codec_name.get() + ")";
244                                         }
245                                 }
246                                 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
247                                 c += i->channels ();
248                         }
249                         _mapping->set_input_groups (groups);
250
251                 } else {
252                         _mapping->set (AudioMapping ());
253                 }
254                 setup_description ();
255                 setup_peak ();
256                 layout ();
257         } else if (property == AudioContentProperty::GAIN) {
258                 /* This is a bit aggressive but probably not so bad */
259                 _peak_cache.clear();
260                 setup_peak ();
261         } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
262                 if (ac.size() == 1) {
263                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
264                         checked_set (_reference, dcp ? dcp->reference_audio () : false);
265                 } else {
266                         checked_set (_reference, false);
267                 }
268
269                 setup_sensitivity ();
270         } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
271                 setup_description ();
272         } else if (property == AudioContentProperty::FADE_IN) {
273                 set<Frame> check;
274                 for (auto i: ac) {
275                         check.insert (i->audio->fade_in().get());
276                 }
277
278                 if (check.size() == 1) {
279                         _fade_in->set (
280                                 ac.front()->audio->fade_in(),
281                                 ac.front()->active_video_frame_rate(_parent->film())
282                                 );
283                 } else {
284                         _fade_in->clear ();
285                 }
286         } else if (property == AudioContentProperty::FADE_OUT) {
287                 set<Frame> check;
288                 for (auto i: ac) {
289                         check.insert (i->audio->fade_out().get());
290                 }
291
292                 if (check.size() == 1) {
293                         _fade_out->set (
294                                 ac.front()->audio->fade_out(),
295                                 ac.front()->active_video_frame_rate(_parent->film())
296                                 );
297                 } else {
298                         _fade_out->clear ();
299                 }
300         } else if (property == AudioContentProperty::USE_SAME_FADES_AS_VIDEO) {
301                 setup_sensitivity ();
302         }
303 }
304
305
306 void
307 AudioPanel::gain_calculate_button_clicked ()
308 {
309         GainCalculatorDialog dialog(this);
310         auto const r = dialog.ShowModal();
311         auto change = dialog.db_change();
312
313         if (r == wxID_CANCEL || !change) {
314                 return;
315         }
316
317         auto old_peak_dB = peak ();
318         auto old_value = _gain->wrapped()->GetValue();
319         _gain->wrapped()->SetValue(old_value + *change);
320
321         /* This appears to be necessary, as the change is not signalled,
322            I think.
323         */
324         _gain->view_changed ();
325
326         auto peak_dB = peak ();
327         if (old_peak_dB && *old_peak_dB < -0.5 && peak_dB && *peak_dB > -0.5) {
328                 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."));
329                 _gain->wrapped()->SetValue (old_value);
330                 _gain->view_changed ();
331         }
332 }
333
334
335 void
336 AudioPanel::setup_description ()
337 {
338         auto ac = _parent->selected_audio ();
339         if (ac.size () != 1) {
340                 checked_set (_description, wxT (""));
341                 return;
342         }
343
344         checked_set (_description, ac.front()->audio->processing_description(_parent->film()));
345 }
346
347
348 void
349 AudioPanel::mapping_changed (AudioMapping m)
350 {
351         auto c = _parent->selected_audio ();
352         if (c.size() == 1) {
353                 c.front()->audio->set_mapping (m);
354         }
355 }
356
357
358 void
359 AudioPanel::content_selection_changed ()
360 {
361         auto sel = _parent->selected_audio ();
362
363         _gain->set_content (sel);
364         _delay->set_content (sel);
365
366         film_content_changed (AudioContentProperty::STREAMS);
367         film_content_changed (AudioContentProperty::GAIN);
368         film_content_changed (AudioContentProperty::FADE_IN);
369         film_content_changed (AudioContentProperty::FADE_OUT);
370         film_content_changed (AudioContentProperty::USE_SAME_FADES_AS_VIDEO);
371         film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
372
373         setup_sensitivity ();
374 }
375
376
377 void
378 AudioPanel::setup_sensitivity ()
379 {
380         auto sel = _parent->selected_audio ();
381
382         shared_ptr<DCPContent> dcp;
383         if (sel.size() == 1) {
384                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
385         }
386
387         string why_not;
388         bool const can_reference = dcp && dcp->can_reference_audio (_parent->film(), why_not);
389         wxString cannot;
390         if (why_not.empty()) {
391                 cannot = _("Cannot reference this DCP's audio.");
392         } else {
393                 cannot = _("Cannot reference this DCP's audio: ") + std_to_wx(why_not);
394         }
395         setup_refer_button (_reference, _reference_note, dcp, can_reference, cannot);
396
397         auto const ref = _reference->GetValue();
398         auto const single = sel.size() == 1;
399
400         auto const all_have_video = std::all_of(sel.begin(), sel.end(), [](shared_ptr<const Content> c) { return static_cast<bool>(c->video); });
401
402         _gain->wrapped()->Enable (!ref);
403         _gain_calculate_button->Enable (!ref && single);
404         _show->Enable (single);
405         _peak->Enable (!ref && single);
406         _delay->wrapped()->Enable (!ref);
407         _mapping->Enable (!ref && single);
408         _description->Enable (!ref && single);
409         _fade_in->Enable (!_use_same_fades_as_video->GetValue());
410         _fade_out->Enable (!_use_same_fades_as_video->GetValue());
411         _use_same_fades_as_video->Enable (!ref && all_have_video);
412 }
413
414
415 void
416 AudioPanel::show_clicked ()
417 {
418         _audio_dialog.reset();
419
420         auto ac = _parent->selected_audio ();
421         if (ac.size() != 1) {
422                 return;
423         }
424
425         _audio_dialog.reset(this, _parent->film(), _parent->film_viewer(), ac.front());
426         _audio_dialog->Show ();
427 }
428
429
430 /** @return If there is one selected piece of audio content, return its peak value in dB (if known) */
431 optional<float>
432 AudioPanel::peak () const
433 {
434         optional<float> peak_dB;
435
436         auto sel = _parent->selected_audio ();
437         if (sel.size() == 1) {
438                 auto playlist = make_shared<Playlist>();
439                 playlist->add (_parent->film(), sel.front());
440                 try {
441                         /* Loading the audio analysis file is slow, and this ::peak() is called a few times when
442                          * the content selection is changed, so cache it.
443                          */
444                         auto const path = _parent->film()->audio_analysis_path(playlist);
445                         auto cached = _peak_cache.find(path);
446                         if (cached != _peak_cache.end()) {
447                                 peak_dB = cached->second;
448                         } else {
449                                 auto analysis = make_shared<AudioAnalysis>(path);
450                                 peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
451                                 _peak_cache[path] = *peak_dB;
452                         }
453                 } catch (...) {
454
455                 }
456         }
457
458         return peak_dB;
459 }
460
461
462 void
463 AudioPanel::setup_peak ()
464 {
465         auto sel = _parent->selected_audio ();
466
467         auto peak_dB = peak ();
468         if (sel.size() != 1) {
469                 _peak->SetLabel (wxT(""));
470         } else {
471                 peak_dB = peak ();
472                 if (peak_dB) {
473                         _peak->SetLabel (wxString::Format(_("Peak: %.2fdB"), *peak_dB));
474                 } else {
475                         _peak->SetLabel (_("Peak: unknown"));
476                 }
477         }
478
479         static auto normal = _peak->GetForegroundColour ();
480
481         if (peak_dB && *peak_dB > -0.5) {
482                 _peak->SetForegroundColour (wxColour (255, 0, 0));
483         } else if (peak_dB && *peak_dB > -3) {
484                 _peak->SetForegroundColour (wxColour (186, 120, 0));
485         } else {
486                 _peak->SetForegroundColour (normal);
487         }
488 }
489
490
491 void
492 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
493 {
494         if (old_active && *old_active == "analyse_audio") {
495                 setup_peak ();
496                 _mapping->Enable (true);
497         } else if (new_active && *new_active == "analyse_audio") {
498                 _mapping->Enable (false);
499         }
500 }
501
502
503 void
504 AudioPanel::reference_clicked ()
505 {
506         auto c = _parent->selected ();
507         if (c.size() != 1) {
508                 return;
509         }
510
511         auto d = dynamic_pointer_cast<DCPContent>(c.front());
512         if (!d) {
513                 return;
514         }
515
516         d->set_reference_audio (_reference->GetValue ());
517 }
518
519
520 void
521 AudioPanel::set_film (shared_ptr<Film>)
522 {
523         /* We are changing film, so destroy any audio dialog for the old one */
524         _audio_dialog.reset();
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