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