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