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