Move video frame rate ('prepared-for') into Content.
[dcpomatic.git] / src / wx / audio_panel.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "audio_panel.h"
21 #include "audio_mapping_view.h"
22 #include "wx_util.h"
23 #include "gain_calculator_dialog.h"
24 #include "content_panel.h"
25 #include "audio_dialog.h"
26 #include "lib/config.h"
27 #include "lib/ffmpeg_content.h"
28 #include "lib/cinema_sound_processor.h"
29 #include "lib/job_manager.h"
30 #include "lib/dcp_content.h"
31 #include "lib/audio_content.h"
32 #include <wx/spinctrl.h>
33 #include <boost/foreach.hpp>
34 #include <iostream>
35
36 using std::vector;
37 using std::cout;
38 using std::string;
39 using std::list;
40 using std::pair;
41 using boost::dynamic_pointer_cast;
42 using boost::shared_ptr;
43 using boost::optional;
44
45 AudioPanel::AudioPanel (ContentPanel* p)
46         : ContentSubPanel (p, _("Audio"))
47         , _audio_dialog (0)
48 {
49         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
50         _sizer->Add (grid, 0, wxALL, 8);
51
52         int r = 0;
53
54         _reference = new wxCheckBox (this, wxID_ANY, _("Refer to existing DCP"));
55         grid->Add (_reference, wxGBPosition (r, 0), wxGBSpan (1, 2));
56         ++r;
57
58         _show = new wxButton (this, wxID_ANY, _("Show graph of audio levels..."));
59         grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
60         _peak = new wxStaticText (this, wxID_ANY, wxT (""));
61         grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
62         ++r;
63
64         add_label_to_sizer (grid, this, _("Gain"), true, wxGBPosition (r, 0));
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->add (grid, wxGBPosition (r, 1));
75         add_label_to_sizer (grid, this, _("dB"), false, wxGBPosition (r, 2));
76         _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
77         grid->Add (_gain_calculate_button, wxGBPosition (r, 3));
78         ++r;
79
80         add_label_to_sizer (grid, this, _("Delay"), true, wxGBPosition (r, 0));
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         _delay->add (grid, wxGBPosition (r, 1));
91         /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
92         add_label_to_sizer (grid, this, _("ms"), false, wxGBPosition (r, 2));
93         ++r;
94
95         _mapping = new AudioMappingView (this);
96         _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
97         ++r;
98
99         _description = new wxStaticText (this, wxID_ANY, wxT (" \n"), wxDefaultPosition, wxDefaultSize);
100         _sizer->Add (_description, 0, wxALL, 12);
101         wxFont font = _description->GetFont();
102         font.SetStyle (wxFONTSTYLE_ITALIC);
103         font.SetPointSize (font.GetPointSize() - 1);
104         _description->SetFont (font);
105         ++r;
106
107         _gain->wrapped()->SetRange (-60, 60);
108         _gain->wrapped()->SetDigits (1);
109         _gain->wrapped()->SetIncrement (0.5);
110         _delay->wrapped()->SetRange (-1000, 1000);
111
112         _reference->Bind             (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioPanel::reference_clicked, this));
113         _show->Bind                  (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&AudioPanel::show_clicked, this));
114         _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
115
116         _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
117
118         JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1));
119 }
120
121 AudioPanel::~AudioPanel ()
122 {
123         if (_audio_dialog) {
124                 _audio_dialog->Destroy ();
125                 _audio_dialog = 0;
126         }
127 }
128
129 void
130 AudioPanel::film_changed (Film::Property property)
131 {
132         switch (property) {
133         case Film::AUDIO_CHANNELS:
134         case Film::AUDIO_PROCESSOR:
135                 _mapping->set_output_channels (_parent->film()->audio_output_names ());
136                 setup_peak ();
137                 break;
138         case Film::VIDEO_FRAME_RATE:
139                 setup_description ();
140                 break;
141         case Film::REEL_TYPE:
142                 setup_sensitivity ();
143         default:
144                 break;
145         }
146 }
147
148 void
149 AudioPanel::film_content_changed (int property)
150 {
151         ContentList ac = _parent->selected_audio ();
152         if (property == AudioContentProperty::STREAMS) {
153                 if (ac.size() == 1) {
154                         _mapping->set (ac.front()->audio->mapping());
155                         _mapping->set_input_channels (ac.front()->audio->channel_names ());
156                 } else {
157                         _mapping->set (AudioMapping ());
158                 }
159                 setup_description ();
160                 setup_peak ();
161                 _sizer->Layout ();
162         } else if (property == AudioContentProperty::GAIN) {
163                 setup_peak ();
164         } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
165                 if (ac.size() == 1) {
166                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
167                         checked_set (_reference, dcp ? dcp->reference_audio () : false);
168                 } else {
169                         checked_set (_reference, false);
170                 }
171
172                 setup_sensitivity ();
173         } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
174                 setup_description ();
175         }
176 }
177
178 void
179 AudioPanel::gain_calculate_button_clicked ()
180 {
181         GainCalculatorDialog* d = new GainCalculatorDialog (this);
182         int const r = d->ShowModal ();
183
184         if (r == wxID_CANCEL || d->wanted_fader() == 0 || d->actual_fader() == 0) {
185                 d->Destroy ();
186                 return;
187         }
188
189         _gain->wrapped()->SetValue (
190                 Config::instance()->cinema_sound_processor()->db_for_fader_change (
191                         d->wanted_fader (),
192                         d->actual_fader ()
193                         )
194                 );
195
196         /* This appears to be necessary, as the change is not signalled,
197            I think.
198         */
199         _gain->view_changed ();
200
201         d->Destroy ();
202 }
203
204 void
205 AudioPanel::setup_description ()
206 {
207         ContentList ac = _parent->selected_audio ();
208         if (ac.size () != 1) {
209                 checked_set (_description, wxT (""));
210                 return;
211         }
212
213         checked_set (_description, ac.front()->audio->processing_description ());
214 }
215
216 void
217 AudioPanel::mapping_changed (AudioMapping m)
218 {
219         ContentList c = _parent->selected_audio ();
220         if (c.size() == 1) {
221                 c.front()->audio->set_mapping (m);
222         }
223 }
224
225 void
226 AudioPanel::content_selection_changed ()
227 {
228         ContentList sel = _parent->selected_audio ();
229
230         _gain->set_content (sel);
231         _delay->set_content (sel);
232
233         film_content_changed (AudioContentProperty::STREAMS);
234         film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
235
236         setup_sensitivity ();
237 }
238
239 void
240 AudioPanel::setup_sensitivity ()
241 {
242         ContentList sel = _parent->selected_audio ();
243
244         shared_ptr<DCPContent> dcp;
245         if (sel.size() == 1) {
246                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
247         }
248
249         list<string> why_not;
250         bool const can_reference = dcp && dcp->can_reference_audio (why_not);
251         setup_refer_button (_reference, dcp, can_reference, why_not);
252
253         if (_reference->GetValue ()) {
254                 _gain->wrapped()->Enable (false);
255                 _gain_calculate_button->Enable (false);
256                 _peak->Enable (false);
257                 _delay->wrapped()->Enable (false);
258                 _mapping->Enable (false);
259                 _description->Enable (false);
260         } else {
261                 _gain->wrapped()->Enable (true);
262                 _gain_calculate_button->Enable (sel.size() == 1);
263                 _peak->Enable (true);
264                 _delay->wrapped()->Enable (true);
265                 _mapping->Enable (sel.size() == 1);
266                 _description->Enable (true);
267         }
268 }
269
270 void
271 AudioPanel::show_clicked ()
272 {
273         if (_audio_dialog) {
274                 _audio_dialog->Destroy ();
275                 _audio_dialog = 0;
276         }
277
278         ContentList ac = _parent->selected_audio ();
279         if (ac.size() != 1) {
280                 return;
281         }
282
283         _audio_dialog = new AudioDialog (this, _parent->film (), ac.front ());
284         _audio_dialog->Show ();
285 }
286
287 void
288 AudioPanel::setup_peak ()
289 {
290         ContentList sel = _parent->selected_audio ();
291         bool alert = false;
292
293         if (sel.size() != 1) {
294                 _peak->SetLabel (wxT (""));
295         } else {
296                 shared_ptr<Playlist> playlist (new Playlist);
297                 playlist->add (sel.front ());
298                 try {
299                         shared_ptr<AudioAnalysis> analysis (new AudioAnalysis (_parent->film()->audio_analysis_path (playlist)));
300                         if (analysis->sample_peak ()) {
301                                 float const peak_dB = 20 * log10 (analysis->sample_peak().get()) + analysis->gain_correction (playlist);
302                                 if (peak_dB > -3) {
303                                         alert = true;
304                                 }
305                                 _peak->SetLabel (wxString::Format (_("Peak: %.2fdB"), peak_dB));
306                         } else {
307                                 _peak->SetLabel (_("Peak: unknown"));
308                         }
309                 } catch (...) {
310                         _peak->SetLabel (_("Peak: unknown"));
311                 }
312         }
313
314         static wxColour normal = _peak->GetForegroundColour ();
315
316         if (alert) {
317                 _peak->SetForegroundColour (wxColour (255, 0, 0));
318         } else {
319                 _peak->SetForegroundColour (normal);
320         }
321 }
322
323 void
324 AudioPanel::active_jobs_changed (optional<string> j)
325 {
326         if (j && *j == "analyse_audio") {
327                 setup_peak ();
328         }
329 }
330
331 void
332 AudioPanel::reference_clicked ()
333 {
334         ContentList c = _parent->selected ();
335         if (c.size() != 1) {
336                 return;
337         }
338
339         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
340         if (!d) {
341                 return;
342         }
343
344         d->set_reference_audio (_reference->GetValue ());
345 }