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