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