Remove audio delay from simple interface.
[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);
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         Config::Interface const interface = Config::instance()->interface_complexity();
122
123         int r = 0;
124
125         _reference->Show (interface == Config::INTERFACE_FULL);
126         _reference_note->Show (interface == Config::INTERFACE_FULL);
127
128         if (interface == Config::INTERFACE_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         _grid->Add (_gain_calculate_button, wxGBPosition(r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
148
149         ++r;
150
151         _delay_label->Show (interface == Config::INTERFACE_FULL);
152         _delay->show (interface == Config::INTERFACE_FULL);
153         _delay_ms_label->Show (interface == Config::INTERFACE_FULL);
154
155         if (interface == Config::INTERFACE_FULL) {
156                 add_label_to_sizer (_grid, _delay_label, true, wxGBPosition(r, 0));
157                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
158                 s->Add (_delay->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
159                 s->Add (_delay_ms_label, 0, wxALIGN_CENTER_VERTICAL);
160                 _grid->Add (s, wxGBPosition(r, 1));
161                 ++r;
162         }
163 }
164
165 AudioPanel::~AudioPanel ()
166 {
167         if (_audio_dialog) {
168                 _audio_dialog->Destroy ();
169                 _audio_dialog = 0;
170         }
171 }
172
173 void
174 AudioPanel::film_changed (Film::Property property)
175 {
176         switch (property) {
177         case Film::AUDIO_CHANNELS:
178         case Film::AUDIO_PROCESSOR:
179                 _mapping->set_output_channels (_parent->film()->audio_output_names ());
180                 setup_peak ();
181                 break;
182         case Film::VIDEO_FRAME_RATE:
183                 setup_description ();
184                 break;
185         case Film::REEL_TYPE:
186         case Film::INTEROP:
187                 setup_sensitivity ();
188                 break;
189         default:
190                 break;
191         }
192 }
193
194 void
195 AudioPanel::film_content_changed (int property)
196 {
197         ContentList ac = _parent->selected_audio ();
198         if (property == AudioContentProperty::STREAMS) {
199                 if (ac.size() == 1) {
200                         _mapping->set (ac.front()->audio->mapping());
201                         _mapping->set_input_channels (ac.front()->audio->channel_names ());
202
203                         vector<AudioMappingView::Group> groups;
204                         int c = 0;
205                         BOOST_FOREACH (shared_ptr<const AudioStream> i, ac.front()->audio->streams()) {
206                                 shared_ptr<const FFmpegAudioStream> f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
207                                 string name = "";
208                                 if (f) {
209                                         name = f->name;
210                                         if (f->codec_name) {
211                                                 name += " (" + f->codec_name.get() + ")";
212                                         }
213                                 }
214                                 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
215                                 c += i->channels ();
216                         }
217                         _mapping->set_input_groups (groups);
218
219                 } else {
220                         _mapping->set (AudioMapping ());
221                 }
222                 setup_description ();
223                 setup_peak ();
224                 _sizer->Layout ();
225         } else if (property == AudioContentProperty::GAIN) {
226                 setup_peak ();
227         } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
228                 if (ac.size() == 1) {
229                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
230                         checked_set (_reference, dcp ? dcp->reference_audio () : false);
231                 } else {
232                         checked_set (_reference, false);
233                 }
234
235                 setup_sensitivity ();
236         } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
237                 setup_description ();
238         }
239 }
240
241 void
242 AudioPanel::gain_calculate_button_clicked ()
243 {
244         GainCalculatorDialog* d = new GainCalculatorDialog (this);
245         int const r = d->ShowModal ();
246
247         if (r == wxID_CANCEL || d->wanted_fader() == 0 || d->actual_fader() == 0) {
248                 d->Destroy ();
249                 return;
250         }
251
252         _gain->wrapped()->SetValue (
253                 Config::instance()->cinema_sound_processor()->db_for_fader_change (
254                         d->wanted_fader (),
255                         d->actual_fader ()
256                         )
257                 );
258
259         /* This appears to be necessary, as the change is not signalled,
260            I think.
261         */
262         _gain->view_changed ();
263
264         d->Destroy ();
265 }
266
267 void
268 AudioPanel::setup_description ()
269 {
270         ContentList ac = _parent->selected_audio ();
271         if (ac.size () != 1) {
272                 checked_set (_description, wxT (""));
273                 return;
274         }
275
276         checked_set (_description, ac.front()->audio->processing_description(_parent->film()));
277 }
278
279 void
280 AudioPanel::mapping_changed (AudioMapping m)
281 {
282         ContentList c = _parent->selected_audio ();
283         if (c.size() == 1) {
284                 c.front()->audio->set_mapping (m);
285         }
286 }
287
288 void
289 AudioPanel::content_selection_changed ()
290 {
291         ContentList sel = _parent->selected_audio ();
292
293         _gain->set_content (sel);
294         _delay->set_content (sel);
295
296         film_content_changed (AudioContentProperty::STREAMS);
297         film_content_changed (AudioContentProperty::GAIN);
298         film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
299
300         setup_sensitivity ();
301 }
302
303 void
304 AudioPanel::setup_sensitivity ()
305 {
306         ContentList sel = _parent->selected_audio ();
307
308         shared_ptr<DCPContent> dcp;
309         if (sel.size() == 1) {
310                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
311         }
312
313         string why_not;
314         bool const can_reference = dcp && dcp->can_reference_audio (_parent->film(), why_not);
315         setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
316
317         if (_reference->GetValue ()) {
318                 _gain->wrapped()->Enable (false);
319                 _gain_calculate_button->Enable (false);
320                 _show->Enable (true);
321                 _peak->Enable (false);
322                 _delay->wrapped()->Enable (false);
323                 _mapping->Enable (false);
324                 _description->Enable (false);
325         } else {
326                 _gain->wrapped()->Enable (sel.size() == 1);
327                 _gain_calculate_button->Enable (sel.size() == 1);
328                 _show->Enable (sel.size() == 1);
329                 _peak->Enable (sel.size() == 1);
330                 _delay->wrapped()->Enable (sel.size() == 1);
331                 _mapping->Enable (sel.size() == 1);
332                 _description->Enable (sel.size() == 1);
333         }
334 }
335
336 void
337 AudioPanel::show_clicked ()
338 {
339         if (_audio_dialog) {
340                 _audio_dialog->Destroy ();
341                 _audio_dialog = 0;
342         }
343
344         ContentList ac = _parent->selected_audio ();
345         if (ac.size() != 1) {
346                 return;
347         }
348
349         _audio_dialog = new AudioDialog (this, _parent->film (), ac.front ());
350         _audio_dialog->Show ();
351 }
352
353 void
354 AudioPanel::setup_peak ()
355 {
356         ContentList sel = _parent->selected_audio ();
357         optional<float> peak_dB;
358
359         if (sel.size() != 1) {
360                 _peak->SetLabel (wxT (""));
361         } else {
362                 shared_ptr<Playlist> playlist (new Playlist);
363                 playlist->add (_parent->film(), sel.front());
364                 try {
365                         shared_ptr<AudioAnalysis> analysis (new AudioAnalysis (_parent->film()->audio_analysis_path (playlist)));
366                         peak_dB = 20 * log10 (analysis->overall_sample_peak().first.peak) + analysis->gain_correction (playlist);
367                         _peak->SetLabel (wxString::Format (_("Peak: %.2fdB"), *peak_dB));
368                 } catch (...) {
369                         _peak->SetLabel (_("Peak: unknown"));
370                 }
371         }
372
373         static wxColour normal = _peak->GetForegroundColour ();
374
375         if (peak_dB && *peak_dB > -0.5) {
376                 _peak->SetForegroundColour (wxColour (255, 0, 0));
377         } else if (peak_dB && *peak_dB > -3) {
378                 _peak->SetForegroundColour (wxColour (186, 120, 0));
379         } else {
380                 _peak->SetForegroundColour (normal);
381         }
382 }
383
384 void
385 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
386 {
387         if (old_active && *old_active == "analyse_audio") {
388                 setup_peak ();
389                 _mapping->Enable (true);
390         } else if (new_active && *new_active == "analyse_audio") {
391                 _mapping->Enable (false);
392         }
393 }
394
395 void
396 AudioPanel::reference_clicked ()
397 {
398         ContentList c = _parent->selected ();
399         if (c.size() != 1) {
400                 return;
401         }
402
403         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
404         if (!d) {
405                 return;
406         }
407
408         d->set_reference_audio (_reference->GetValue ());
409 }
410
411 void
412 AudioPanel::set_film (shared_ptr<Film>)
413 {
414         /* We are changing film, so destroy any audio dialog for the old one */
415         if (_audio_dialog) {
416                 _audio_dialog->Destroy ();
417                 _audio_dialog = 0;
418         }
419 }