Rename _size -> _video_size
[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/config.h"
32 #include "lib/ffmpeg_audio_stream.h"
33 #include "lib/ffmpeg_content.h"
34 #include "lib/cinema_sound_processor.h"
35 #include "lib/job_manager.h"
36 #include "lib/dcp_content.h"
37 #include "lib/audio_content.h"
38 #include <wx/spinctrl.h>
39 #include <iostream>
40
41
42 using std::vector;
43 using std::cout;
44 using std::string;
45 using std::list;
46 using std::pair;
47 using std::make_shared;
48 using std::dynamic_pointer_cast;
49 using std::shared_ptr;
50 using boost::optional;
51 #if BOOST_VERSION >= 106100
52 using namespace boost::placeholders;
53 #endif
54
55
56 AudioPanel::AudioPanel (ContentPanel* p)
57         : ContentSubPanel (p, _("Audio"))
58         , _audio_dialog (0)
59 {
60
61 }
62
63
64 void
65 AudioPanel::create ()
66 {
67         _reference = new CheckBox (this, _("Use this DCP's audio as OV and make VF"));
68         _reference_note = new StaticText (this, wxT(""));
69         _reference_note->Wrap (200);
70         auto font = _reference_note->GetFont();
71         font.SetStyle(wxFONTSTYLE_ITALIC);
72         font.SetPointSize(font.GetPointSize() - 1);
73         _reference_note->SetFont(font);
74
75         _show = new Button (this, _("Show graph of audio levels..."));
76         _peak = new StaticText (this, wxT (""));
77
78         _gain_label = create_label (this, _("Gain"), true);
79         _gain = new ContentSpinCtrlDouble<AudioContent> (
80                 this,
81                 new wxSpinCtrlDouble (this),
82                 AudioContentProperty::GAIN,
83                 &Content::audio,
84                 boost::mem_fn (&AudioContent::gain),
85                 boost::mem_fn (&AudioContent::set_gain)
86                 );
87
88         _gain_db_label = create_label (this, _("dB"), false);
89         _gain_calculate_button = new Button (this, _("Calculate..."));
90
91         _delay_label = create_label (this, _("Delay"), true);
92         _delay = new ContentSpinCtrl<AudioContent> (
93                 this,
94                 new wxSpinCtrl (this),
95                 AudioContentProperty::DELAY,
96                 &Content::audio,
97                 boost::mem_fn (&AudioContent::delay),
98                 boost::mem_fn (&AudioContent::set_delay)
99                 );
100
101         /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
102         _delay_ms_label = create_label (this, _("ms"), false);
103
104         _mapping = new AudioMappingView (this, _("Content"), _("content"), _("DCP"), _("DCP"));
105         _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
106
107         _description = new StaticText (this, wxT(" \n"), wxDefaultPosition, wxDefaultSize);
108         _sizer->Add (_description, 0, wxALL, 12);
109         _description->SetFont (font);
110
111         _gain->wrapped()->SetRange (-60, 60);
112         _gain->wrapped()->SetDigits (1);
113         _gain->wrapped()->SetIncrement (0.5);
114         _delay->wrapped()->SetRange (-1000, 1000);
115
116         content_selection_changed ();
117         film_changed (Film::Property::AUDIO_CHANNELS);
118         film_changed (Film::Property::VIDEO_FRAME_RATE);
119         film_changed (Film::Property::REEL_TYPE);
120
121         _reference->Bind             (wxEVT_CHECKBOX, boost::bind (&AudioPanel::reference_clicked, this));
122         _show->Bind                  (wxEVT_BUTTON,   boost::bind (&AudioPanel::show_clicked, this));
123         _gain_calculate_button->Bind (wxEVT_BUTTON,   boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
124
125         _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
126         _active_jobs_connection = JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1, _2));
127
128         add_to_grid ();
129
130         _sizer->Layout ();
131 }
132
133 void
134 AudioPanel::add_to_grid ()
135 {
136         int r = 0;
137
138         auto reference_sizer = new wxBoxSizer (wxVERTICAL);
139         reference_sizer->Add (_reference, 0);
140         reference_sizer->Add (_reference_note, 0);
141         _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 4));
142         ++r;
143
144         _grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
145         _grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
146         ++r;
147
148         add_label_to_sizer (_grid, _gain_label, true, wxGBPosition(r, 0));
149         {
150                 auto s = new wxBoxSizer (wxHORIZONTAL);
151                 s->Add (_gain->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
152                 s->Add (_gain_db_label, 0, wxALIGN_CENTER_VERTICAL);
153                 _grid->Add (s, wxGBPosition(r, 1));
154         }
155
156         _grid->Add (_gain_calculate_button, wxGBPosition(r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
157         ++r;
158
159         add_label_to_sizer (_grid, _delay_label, true, wxGBPosition(r, 0));
160         auto s = new wxBoxSizer (wxHORIZONTAL);
161         s->Add (_delay->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
162         s->Add (_delay_ms_label, 0, wxALIGN_CENTER_VERTICAL);
163         _grid->Add (s, wxGBPosition(r, 1));
164         ++r;
165 }
166
167 AudioPanel::~AudioPanel ()
168 {
169         if (_audio_dialog) {
170                 _audio_dialog->Destroy ();
171                 _audio_dialog = nullptr;
172         }
173 }
174
175 void
176 AudioPanel::film_changed (Film::Property property)
177 {
178         if (!_parent->film()) {
179                 return;
180         }
181
182         switch (property) {
183         case Film::Property::AUDIO_CHANNELS:
184         case Film::Property::AUDIO_PROCESSOR:
185                 _mapping->set_output_channels (_parent->film()->audio_output_names ());
186                 setup_peak ();
187                 break;
188         case Film::Property::VIDEO_FRAME_RATE:
189                 setup_description ();
190                 break;
191         case Film::Property::REEL_TYPE:
192         case Film::Property::INTEROP:
193                 setup_sensitivity ();
194                 break;
195         default:
196                 break;
197         }
198 }
199
200 void
201 AudioPanel::film_content_changed (int property)
202 {
203         auto ac = _parent->selected_audio ();
204         if (property == AudioContentProperty::STREAMS) {
205                 if (ac.size() == 1) {
206                         _mapping->set (ac.front()->audio->mapping());
207                         _mapping->set_input_channels (ac.front()->audio->channel_names ());
208
209                         vector<AudioMappingView::Group> groups;
210                         int c = 0;
211                         for (auto i: ac.front()->audio->streams()) {
212                                 auto f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
213                                 string name = "";
214                                 if (f) {
215                                         name = f->name;
216                                         if (f->codec_name) {
217                                                 name += " (" + f->codec_name.get() + ")";
218                                         }
219                                 }
220                                 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
221                                 c += i->channels ();
222                         }
223                         _mapping->set_input_groups (groups);
224
225                 } else {
226                         _mapping->set (AudioMapping ());
227                 }
228                 setup_description ();
229                 setup_peak ();
230                 layout ();
231         } else if (property == AudioContentProperty::GAIN) {
232                 setup_peak ();
233         } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
234                 if (ac.size() == 1) {
235                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
236                         checked_set (_reference, dcp ? dcp->reference_audio () : false);
237                 } else {
238                         checked_set (_reference, false);
239                 }
240
241                 setup_sensitivity ();
242         } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
243                 setup_description ();
244         }
245 }
246
247 void
248 AudioPanel::gain_calculate_button_clicked ()
249 {
250         auto d = new GainCalculatorDialog (this);
251         auto const r = d->ShowModal ();
252         auto c = d->db_change();
253
254         if (r == wxID_CANCEL || !c) {
255                 d->Destroy ();
256                 return;
257         }
258
259         auto old_peak_dB = peak ();
260         auto old_value = _gain->wrapped()->GetValue();
261         _gain->wrapped()->SetValue(old_value + *c);
262
263         /* This appears to be necessary, as the change is not signalled,
264            I think.
265         */
266         _gain->view_changed ();
267
268         auto peak_dB = peak ();
269         if (old_peak_dB && *old_peak_dB < -0.5 && peak_dB && *peak_dB > -0.5) {
270                 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."));
271                 _gain->wrapped()->SetValue (old_value);
272                 _gain->view_changed ();
273         }
274
275         d->Destroy ();
276 }
277
278 void
279 AudioPanel::setup_description ()
280 {
281         auto ac = _parent->selected_audio ();
282         if (ac.size () != 1) {
283                 checked_set (_description, wxT (""));
284                 return;
285         }
286
287         checked_set (_description, ac.front()->audio->processing_description(_parent->film()));
288 }
289
290 void
291 AudioPanel::mapping_changed (AudioMapping m)
292 {
293         auto c = _parent->selected_audio ();
294         if (c.size() == 1) {
295                 c.front()->audio->set_mapping (m);
296         }
297 }
298
299 void
300 AudioPanel::content_selection_changed ()
301 {
302         auto sel = _parent->selected_audio ();
303
304         _gain->set_content (sel);
305         _delay->set_content (sel);
306
307         film_content_changed (AudioContentProperty::STREAMS);
308         film_content_changed (AudioContentProperty::GAIN);
309         film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
310
311         setup_sensitivity ();
312 }
313
314 void
315 AudioPanel::setup_sensitivity ()
316 {
317         auto sel = _parent->selected_audio ();
318
319         shared_ptr<DCPContent> dcp;
320         if (sel.size() == 1) {
321                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
322         }
323
324         string why_not;
325         bool const can_reference = dcp && dcp->can_reference_audio (_parent->film(), why_not);
326         wxString cannot;
327         if (why_not.empty()) {
328                 cannot = _("Cannot reference this DCP's audio.");
329         } else {
330                 cannot = _("Cannot reference this DCP's audio: ") + std_to_wx(why_not);
331         }
332         setup_refer_button (_reference, _reference_note, dcp, can_reference, cannot);
333
334         auto const ref = _reference->GetValue();
335         auto const single = sel.size() == 1;
336
337         _gain->wrapped()->Enable (!ref);
338         _gain_calculate_button->Enable (!ref && single);
339         _show->Enable (single);
340         _peak->Enable (!ref && single);
341         _delay->wrapped()->Enable (!ref);
342         _mapping->Enable (!ref && single);
343         _description->Enable (!ref && single);
344 }
345
346 void
347 AudioPanel::show_clicked ()
348 {
349         if (_audio_dialog) {
350                 _audio_dialog->Destroy ();
351                 _audio_dialog = nullptr;
352         }
353
354         auto ac = _parent->selected_audio ();
355         if (ac.size() != 1) {
356                 return;
357         }
358
359         _audio_dialog = new AudioDialog (this, _parent->film(), _parent->film_viewer(), ac.front());
360         _audio_dialog->Show ();
361 }
362
363 /** @return If there is one selected piece of audio content, return its peak value in dB (if known) */
364 optional<float>
365 AudioPanel::peak () const
366 {
367         optional<float> peak_dB;
368
369         auto sel = _parent->selected_audio ();
370         if (sel.size() == 1) {
371                 auto playlist = make_shared<Playlist>();
372                 playlist->add (_parent->film(), sel.front());
373                 try {
374                         auto analysis = make_shared<AudioAnalysis>(_parent->film()->audio_analysis_path(playlist));
375                         peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
376                 } catch (...) {
377
378                 }
379         }
380
381         return peak_dB;
382 }
383
384 void
385 AudioPanel::setup_peak ()
386 {
387         auto sel = _parent->selected_audio ();
388
389         auto peak_dB = peak ();
390         if (sel.size() != 1) {
391                 _peak->SetLabel (wxT(""));
392         } else {
393                 peak_dB = peak ();
394                 if (peak_dB) {
395                         _peak->SetLabel (wxString::Format(_("Peak: %.2fdB"), *peak_dB));
396                 } else {
397                         _peak->SetLabel (_("Peak: unknown"));
398                 }
399         }
400
401         static auto normal = _peak->GetForegroundColour ();
402
403         if (peak_dB && *peak_dB > -0.5) {
404                 _peak->SetForegroundColour (wxColour (255, 0, 0));
405         } else if (peak_dB && *peak_dB > -3) {
406                 _peak->SetForegroundColour (wxColour (186, 120, 0));
407         } else {
408                 _peak->SetForegroundColour (normal);
409         }
410 }
411
412 void
413 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
414 {
415         if (old_active && *old_active == "analyse_audio") {
416                 setup_peak ();
417                 _mapping->Enable (true);
418         } else if (new_active && *new_active == "analyse_audio") {
419                 _mapping->Enable (false);
420         }
421 }
422
423 void
424 AudioPanel::reference_clicked ()
425 {
426         auto c = _parent->selected ();
427         if (c.size() != 1) {
428                 return;
429         }
430
431         auto d = dynamic_pointer_cast<DCPContent>(c.front());
432         if (!d) {
433                 return;
434         }
435
436         d->set_reference_audio (_reference->GetValue ());
437 }
438
439 void
440 AudioPanel::set_film (shared_ptr<Film>)
441 {
442         /* We are changing film, so destroy any audio dialog for the old one */
443         if (_audio_dialog) {
444                 _audio_dialog->Destroy ();
445                 _audio_dialog = nullptr;
446         }
447 }
448