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