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