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