Add fade in/out option to the content audio tab (#1026).
[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         , _audio_dialog (0)
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         _mapping = new AudioMappingView (this, _("Content"), _("content"), _("DCP"), _("DCP"));
114         _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
115
116         _description = new StaticText (this, wxT(" \n"), wxDefaultPosition, wxDefaultSize);
117         _sizer->Add (_description, 0, wxALL, 12);
118         _description->SetFont (font);
119
120         _gain->wrapped()->SetRange (-60, 60);
121         _gain->wrapped()->SetDigits (1);
122         _gain->wrapped()->SetIncrement (0.5);
123         _delay->wrapped()->SetRange (-1000, 1000);
124
125         content_selection_changed ();
126         film_changed (Film::Property::AUDIO_CHANNELS);
127         film_changed (Film::Property::VIDEO_FRAME_RATE);
128         film_changed (Film::Property::REEL_TYPE);
129
130         _reference->Bind             (wxEVT_CHECKBOX, boost::bind (&AudioPanel::reference_clicked, this));
131         _show->Bind                  (wxEVT_BUTTON,   boost::bind (&AudioPanel::show_clicked, this));
132         _gain_calculate_button->Bind (wxEVT_BUTTON,   boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
133
134         _fade_in->Changed.connect (boost::bind(&AudioPanel::fade_in_changed, this));
135         _fade_out->Changed.connect (boost::bind(&AudioPanel::fade_out_changed, this));
136
137         _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
138         _active_jobs_connection = JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1, _2));
139
140         add_to_grid ();
141
142         _sizer->Layout ();
143 }
144
145
146 void
147 AudioPanel::add_to_grid ()
148 {
149         int r = 0;
150
151         auto reference_sizer = new wxBoxSizer (wxVERTICAL);
152         reference_sizer->Add (_reference, 0);
153         reference_sizer->Add (_reference_note, 0);
154         _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 4));
155         ++r;
156
157         _grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
158         _grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
159         ++r;
160
161         add_label_to_sizer (_grid, _gain_label, true, wxGBPosition(r, 0));
162         {
163                 auto s = new wxBoxSizer (wxHORIZONTAL);
164                 s->Add (_gain->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
165                 s->Add (_gain_db_label, 0, wxALIGN_CENTER_VERTICAL);
166                 _grid->Add (s, wxGBPosition(r, 1));
167         }
168
169         _grid->Add (_gain_calculate_button, wxGBPosition(r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
170         ++r;
171
172         add_label_to_sizer (_grid, _delay_label, true, wxGBPosition(r, 0));
173         auto s = new wxBoxSizer (wxHORIZONTAL);
174         s->Add (_delay->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
175         s->Add (_delay_ms_label, 0, wxALIGN_CENTER_VERTICAL);
176         _grid->Add (s, wxGBPosition(r, 1));
177         ++r;
178
179         add_label_to_sizer (_grid, _fade_in_label, true, wxGBPosition(r, 0));
180         _grid->Add (_fade_in, wxGBPosition(r, 1), wxGBSpan(1, 3));
181         ++r;
182
183         add_label_to_sizer (_grid, _fade_out_label, true, wxGBPosition(r, 0));
184         _grid->Add (_fade_out, wxGBPosition(r, 1), wxGBSpan(1, 3));
185         ++r;
186 }
187
188
189 AudioPanel::~AudioPanel ()
190 {
191         if (_audio_dialog) {
192                 _audio_dialog->Destroy ();
193                 _audio_dialog = nullptr;
194         }
195 }
196
197 void
198 AudioPanel::film_changed (Film::Property property)
199 {
200         if (!_parent->film()) {
201                 return;
202         }
203
204         switch (property) {
205         case Film::Property::AUDIO_CHANNELS:
206         case Film::Property::AUDIO_PROCESSOR:
207                 _mapping->set_output_channels (_parent->film()->audio_output_names ());
208                 setup_peak ();
209                 break;
210         case Film::Property::VIDEO_FRAME_RATE:
211                 setup_description ();
212                 break;
213         case Film::Property::REEL_TYPE:
214         case Film::Property::INTEROP:
215                 setup_sensitivity ();
216                 break;
217         default:
218                 break;
219         }
220 }
221
222 void
223 AudioPanel::film_content_changed (int property)
224 {
225         auto ac = _parent->selected_audio ();
226         if (property == AudioContentProperty::STREAMS) {
227                 if (ac.size() == 1) {
228                         _mapping->set (ac.front()->audio->mapping());
229                         _mapping->set_input_channels (ac.front()->audio->channel_names ());
230
231                         vector<AudioMappingView::Group> groups;
232                         int c = 0;
233                         for (auto i: ac.front()->audio->streams()) {
234                                 auto f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
235                                 string name = "";
236                                 if (f) {
237                                         name = f->name;
238                                         if (f->codec_name) {
239                                                 name += " (" + f->codec_name.get() + ")";
240                                         }
241                                 }
242                                 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
243                                 c += i->channels ();
244                         }
245                         _mapping->set_input_groups (groups);
246
247                 } else {
248                         _mapping->set (AudioMapping ());
249                 }
250                 setup_description ();
251                 setup_peak ();
252                 layout ();
253         } else if (property == AudioContentProperty::GAIN) {
254                 setup_peak ();
255         } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
256                 if (ac.size() == 1) {
257                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
258                         checked_set (_reference, dcp ? dcp->reference_audio () : false);
259                 } else {
260                         checked_set (_reference, false);
261                 }
262
263                 setup_sensitivity ();
264         } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
265                 setup_description ();
266         } else if (property == AudioContentProperty::FADE_IN) {
267                 set<Frame> check;
268                 for (auto i: ac) {
269                         check.insert (i->audio->fade_in().get());
270                 }
271
272                 if (check.size() == 1) {
273                         _fade_in->set (
274                                 ac.front()->audio->fade_in(),
275                                 ac.front()->active_video_frame_rate(_parent->film())
276                                 );
277                 } else {
278                         _fade_in->clear ();
279                 }
280         } else if (property == AudioContentProperty::FADE_OUT) {
281                 set<Frame> check;
282                 for (auto i: ac) {
283                         check.insert (i->audio->fade_out().get());
284                 }
285
286                 if (check.size() == 1) {
287                         _fade_out->set (
288                                 ac.front()->audio->fade_out(),
289                                 ac.front()->active_video_frame_rate(_parent->film())
290                                 );
291                 } else {
292                         _fade_out->clear ();
293                 }
294         }
295 }
296
297 void
298 AudioPanel::gain_calculate_button_clicked ()
299 {
300         auto d = new GainCalculatorDialog (this);
301         auto const r = d->ShowModal ();
302         auto c = d->db_change();
303
304         if (r == wxID_CANCEL || !c) {
305                 d->Destroy ();
306                 return;
307         }
308
309         auto old_peak_dB = peak ();
310         auto old_value = _gain->wrapped()->GetValue();
311         _gain->wrapped()->SetValue(old_value + *c);
312
313         /* This appears to be necessary, as the change is not signalled,
314            I think.
315         */
316         _gain->view_changed ();
317
318         auto peak_dB = peak ();
319         if (old_peak_dB && *old_peak_dB < -0.5 && peak_dB && *peak_dB > -0.5) {
320                 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."));
321                 _gain->wrapped()->SetValue (old_value);
322                 _gain->view_changed ();
323         }
324
325         d->Destroy ();
326 }
327
328 void
329 AudioPanel::setup_description ()
330 {
331         auto ac = _parent->selected_audio ();
332         if (ac.size () != 1) {
333                 checked_set (_description, wxT (""));
334                 return;
335         }
336
337         checked_set (_description, ac.front()->audio->processing_description(_parent->film()));
338 }
339
340 void
341 AudioPanel::mapping_changed (AudioMapping m)
342 {
343         auto c = _parent->selected_audio ();
344         if (c.size() == 1) {
345                 c.front()->audio->set_mapping (m);
346         }
347 }
348
349 void
350 AudioPanel::content_selection_changed ()
351 {
352         auto sel = _parent->selected_audio ();
353
354         _gain->set_content (sel);
355         _delay->set_content (sel);
356
357         film_content_changed (AudioContentProperty::STREAMS);
358         film_content_changed (AudioContentProperty::GAIN);
359         film_content_changed (AudioContentProperty::FADE_IN);
360         film_content_changed (AudioContentProperty::FADE_OUT);
361         film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
362
363         setup_sensitivity ();
364 }
365
366 void
367 AudioPanel::setup_sensitivity ()
368 {
369         auto sel = _parent->selected_audio ();
370
371         shared_ptr<DCPContent> dcp;
372         if (sel.size() == 1) {
373                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
374         }
375
376         string why_not;
377         bool const can_reference = dcp && dcp->can_reference_audio (_parent->film(), why_not);
378         wxString cannot;
379         if (why_not.empty()) {
380                 cannot = _("Cannot reference this DCP's audio.");
381         } else {
382                 cannot = _("Cannot reference this DCP's audio: ") + std_to_wx(why_not);
383         }
384         setup_refer_button (_reference, _reference_note, dcp, can_reference, cannot);
385
386         auto const ref = _reference->GetValue();
387         auto const single = sel.size() == 1;
388
389         _gain->wrapped()->Enable (!ref);
390         _gain_calculate_button->Enable (!ref && single);
391         _show->Enable (single);
392         _peak->Enable (!ref && single);
393         _delay->wrapped()->Enable (!ref);
394         _mapping->Enable (!ref && single);
395         _description->Enable (!ref && single);
396 }
397
398 void
399 AudioPanel::show_clicked ()
400 {
401         if (_audio_dialog) {
402                 _audio_dialog->Destroy ();
403                 _audio_dialog = nullptr;
404         }
405
406         auto ac = _parent->selected_audio ();
407         if (ac.size() != 1) {
408                 return;
409         }
410
411         _audio_dialog = new AudioDialog (this, _parent->film(), _parent->film_viewer(), ac.front());
412         _audio_dialog->Show ();
413 }
414
415 /** @return If there is one selected piece of audio content, return its peak value in dB (if known) */
416 optional<float>
417 AudioPanel::peak () const
418 {
419         optional<float> peak_dB;
420
421         auto sel = _parent->selected_audio ();
422         if (sel.size() == 1) {
423                 auto playlist = make_shared<Playlist>();
424                 playlist->add (_parent->film(), sel.front());
425                 try {
426                         auto analysis = make_shared<AudioAnalysis>(_parent->film()->audio_analysis_path(playlist));
427                         peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
428                 } catch (...) {
429
430                 }
431         }
432
433         return peak_dB;
434 }
435
436 void
437 AudioPanel::setup_peak ()
438 {
439         auto sel = _parent->selected_audio ();
440
441         auto peak_dB = peak ();
442         if (sel.size() != 1) {
443                 _peak->SetLabel (wxT(""));
444         } else {
445                 peak_dB = peak ();
446                 if (peak_dB) {
447                         _peak->SetLabel (wxString::Format(_("Peak: %.2fdB"), *peak_dB));
448                 } else {
449                         _peak->SetLabel (_("Peak: unknown"));
450                 }
451         }
452
453         static auto normal = _peak->GetForegroundColour ();
454
455         if (peak_dB && *peak_dB > -0.5) {
456                 _peak->SetForegroundColour (wxColour (255, 0, 0));
457         } else if (peak_dB && *peak_dB > -3) {
458                 _peak->SetForegroundColour (wxColour (186, 120, 0));
459         } else {
460                 _peak->SetForegroundColour (normal);
461         }
462 }
463
464 void
465 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
466 {
467         if (old_active && *old_active == "analyse_audio") {
468                 setup_peak ();
469                 _mapping->Enable (true);
470         } else if (new_active && *new_active == "analyse_audio") {
471                 _mapping->Enable (false);
472         }
473 }
474
475 void
476 AudioPanel::reference_clicked ()
477 {
478         auto c = _parent->selected ();
479         if (c.size() != 1) {
480                 return;
481         }
482
483         auto d = dynamic_pointer_cast<DCPContent>(c.front());
484         if (!d) {
485                 return;
486         }
487
488         d->set_reference_audio (_reference->GetValue ());
489 }
490
491 void
492 AudioPanel::set_film (shared_ptr<Film>)
493 {
494         /* We are changing film, so destroy any audio dialog for the old one */
495         if (_audio_dialog) {
496                 _audio_dialog->Destroy ();
497                 _audio_dialog = nullptr;
498         }
499 }
500
501
502 void
503 AudioPanel::fade_in_changed ()
504 {
505         auto const hmsf = _fade_in->get();
506         for (auto i: _parent->selected_audio()) {
507                 auto const vfr = i->active_video_frame_rate(_parent->film());
508                 i->audio->set_fade_in (dcpomatic::ContentTime(hmsf, vfr));
509         }
510 }
511
512
513 void
514 AudioPanel::fade_out_changed ()
515 {
516         auto const hmsf = _fade_out->get();
517         for (auto i: _parent->selected_audio()) {
518                 auto const vfr = i->active_video_frame_rate (_parent->film());
519                 i->audio->set_fade_out (dcpomatic::ContentTime(hmsf, vfr));
520         }
521 }
522