No-op; fix GPL address and use the explicit-program-name version.
[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_content.h"
29 #include "lib/cinema_sound_processor.h"
30 #include "lib/job_manager.h"
31 #include "lib/dcp_content.h"
32 #include "lib/audio_content.h"
33 #include <wx/spinctrl.h>
34 #include <boost/foreach.hpp>
35 #include <iostream>
36
37 using std::vector;
38 using std::cout;
39 using std::string;
40 using std::list;
41 using std::pair;
42 using boost::dynamic_pointer_cast;
43 using boost::shared_ptr;
44 using boost::optional;
45
46 AudioPanel::AudioPanel (ContentPanel* p)
47         : ContentSubPanel (p, _("Audio"))
48         , _audio_dialog (0)
49 {
50         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
51         _sizer->Add (grid, 0, wxALL, 8);
52
53         int r = 0;
54
55         _reference = new wxCheckBox (this, wxID_ANY, _("Refer to existing DCP"));
56         grid->Add (_reference, wxGBPosition (r, 0), wxGBSpan (1, 2));
57         ++r;
58
59         _show = new wxButton (this, wxID_ANY, _("Show graph of audio levels..."));
60         grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
61         _peak = new wxStaticText (this, wxID_ANY, wxT (""));
62         grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
63         ++r;
64
65         add_label_to_sizer (grid, this, _("Gain"), true, wxGBPosition (r, 0));
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->add (grid, wxGBPosition (r, 1));
76         add_label_to_sizer (grid, this, _("dB"), false, wxGBPosition (r, 2));
77         _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
78         grid->Add (_gain_calculate_button, wxGBPosition (r, 3));
79         ++r;
80
81         add_label_to_sizer (grid, this, _("Delay"), true, wxGBPosition (r, 0));
82         _delay = new ContentSpinCtrl<AudioContent> (
83                 this,
84                 new wxSpinCtrl (this),
85                 AudioContentProperty::DELAY,
86                 &Content::audio,
87                 boost::mem_fn (&AudioContent::delay),
88                 boost::mem_fn (&AudioContent::set_delay)
89                 );
90
91         _delay->add (grid, wxGBPosition (r, 1));
92         /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
93         add_label_to_sizer (grid, this, _("ms"), false, wxGBPosition (r, 2));
94         ++r;
95
96         _mapping = new AudioMappingView (this);
97         _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
98         ++r;
99
100         _description = new wxStaticText (this, wxID_ANY, wxT (" \n"), wxDefaultPosition, wxDefaultSize);
101         _sizer->Add (_description, 0, wxALL, 12);
102         wxFont font = _description->GetFont();
103         font.SetStyle (wxFONTSTYLE_ITALIC);
104         font.SetPointSize (font.GetPointSize() - 1);
105         _description->SetFont (font);
106         ++r;
107
108         _gain->wrapped()->SetRange (-60, 60);
109         _gain->wrapped()->SetDigits (1);
110         _gain->wrapped()->SetIncrement (0.5);
111         _delay->wrapped()->SetRange (-1000, 1000);
112
113         _reference->Bind             (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioPanel::reference_clicked, this));
114         _show->Bind                  (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&AudioPanel::show_clicked, this));
115         _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
116
117         _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
118
119         JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1));
120 }
121
122 AudioPanel::~AudioPanel ()
123 {
124         if (_audio_dialog) {
125                 _audio_dialog->Destroy ();
126                 _audio_dialog = 0;
127         }
128 }
129
130 void
131 AudioPanel::film_changed (Film::Property property)
132 {
133         switch (property) {
134         case Film::AUDIO_CHANNELS:
135         case Film::AUDIO_PROCESSOR:
136                 _mapping->set_output_channels (_parent->film()->audio_output_names ());
137                 setup_peak ();
138                 break;
139         case Film::VIDEO_FRAME_RATE:
140                 setup_description ();
141                 break;
142         case Film::REEL_TYPE:
143                 setup_sensitivity ();
144         default:
145                 break;
146         }
147 }
148
149 void
150 AudioPanel::film_content_changed (int property)
151 {
152         ContentList ac = _parent->selected_audio ();
153         if (property == AudioContentProperty::STREAMS) {
154                 if (ac.size() == 1) {
155                         _mapping->set (ac.front()->audio->mapping());
156                         _mapping->set_input_channels (ac.front()->audio->channel_names ());
157                 } else {
158                         _mapping->set (AudioMapping ());
159                 }
160                 setup_description ();
161                 setup_peak ();
162                 _sizer->Layout ();
163         } else if (property == AudioContentProperty::GAIN) {
164                 setup_peak ();
165         } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
166                 if (ac.size() == 1) {
167                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
168                         checked_set (_reference, dcp ? dcp->reference_audio () : false);
169                 } else {
170                         checked_set (_reference, false);
171                 }
172
173                 setup_sensitivity ();
174         } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
175                 setup_description ();
176         }
177 }
178
179 void
180 AudioPanel::gain_calculate_button_clicked ()
181 {
182         GainCalculatorDialog* d = new GainCalculatorDialog (this);
183         int const r = d->ShowModal ();
184
185         if (r == wxID_CANCEL || d->wanted_fader() == 0 || d->actual_fader() == 0) {
186                 d->Destroy ();
187                 return;
188         }
189
190         _gain->wrapped()->SetValue (
191                 Config::instance()->cinema_sound_processor()->db_for_fader_change (
192                         d->wanted_fader (),
193                         d->actual_fader ()
194                         )
195                 );
196
197         /* This appears to be necessary, as the change is not signalled,
198            I think.
199         */
200         _gain->view_changed ();
201
202         d->Destroy ();
203 }
204
205 void
206 AudioPanel::setup_description ()
207 {
208         ContentList ac = _parent->selected_audio ();
209         if (ac.size () != 1) {
210                 checked_set (_description, wxT (""));
211                 return;
212         }
213
214         checked_set (_description, ac.front()->audio->processing_description ());
215 }
216
217 void
218 AudioPanel::mapping_changed (AudioMapping m)
219 {
220         ContentList c = _parent->selected_audio ();
221         if (c.size() == 1) {
222                 c.front()->audio->set_mapping (m);
223         }
224 }
225
226 void
227 AudioPanel::content_selection_changed ()
228 {
229         ContentList sel = _parent->selected_audio ();
230
231         _gain->set_content (sel);
232         _delay->set_content (sel);
233
234         film_content_changed (AudioContentProperty::STREAMS);
235         film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
236
237         setup_sensitivity ();
238 }
239
240 void
241 AudioPanel::setup_sensitivity ()
242 {
243         ContentList sel = _parent->selected_audio ();
244
245         shared_ptr<DCPContent> dcp;
246         if (sel.size() == 1) {
247                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
248         }
249
250         list<string> why_not;
251         bool const can_reference = dcp && dcp->can_reference_audio (why_not);
252         setup_refer_button (_reference, dcp, can_reference, why_not);
253
254         if (_reference->GetValue ()) {
255                 _gain->wrapped()->Enable (false);
256                 _gain_calculate_button->Enable (false);
257                 _peak->Enable (false);
258                 _delay->wrapped()->Enable (false);
259                 _mapping->Enable (false);
260                 _description->Enable (false);
261         } else {
262                 _gain->wrapped()->Enable (true);
263                 _gain_calculate_button->Enable (sel.size() == 1);
264                 _peak->Enable (true);
265                 _delay->wrapped()->Enable (true);
266                 _mapping->Enable (sel.size() == 1);
267                 _description->Enable (true);
268         }
269 }
270
271 void
272 AudioPanel::show_clicked ()
273 {
274         if (_audio_dialog) {
275                 _audio_dialog->Destroy ();
276                 _audio_dialog = 0;
277         }
278
279         ContentList ac = _parent->selected_audio ();
280         if (ac.size() != 1) {
281                 return;
282         }
283
284         _audio_dialog = new AudioDialog (this, _parent->film (), ac.front ());
285         _audio_dialog->Show ();
286 }
287
288 void
289 AudioPanel::setup_peak ()
290 {
291         ContentList sel = _parent->selected_audio ();
292         bool alert = false;
293
294         if (sel.size() != 1) {
295                 _peak->SetLabel (wxT (""));
296         } else {
297                 shared_ptr<Playlist> playlist (new Playlist);
298                 playlist->add (sel.front ());
299                 try {
300                         shared_ptr<AudioAnalysis> analysis (new AudioAnalysis (_parent->film()->audio_analysis_path (playlist)));
301                         if (analysis->sample_peak ()) {
302                                 float const peak_dB = 20 * log10 (analysis->sample_peak().get()) + analysis->gain_correction (playlist);
303                                 if (peak_dB > -3) {
304                                         alert = true;
305                                 }
306                                 _peak->SetLabel (wxString::Format (_("Peak: %.2fdB"), peak_dB));
307                         } else {
308                                 _peak->SetLabel (_("Peak: unknown"));
309                         }
310                 } catch (...) {
311                         _peak->SetLabel (_("Peak: unknown"));
312                 }
313         }
314
315         static wxColour normal = _peak->GetForegroundColour ();
316
317         if (alert) {
318                 _peak->SetForegroundColour (wxColour (255, 0, 0));
319         } else {
320                 _peak->SetForegroundColour (normal);
321         }
322 }
323
324 void
325 AudioPanel::active_jobs_changed (optional<string> j)
326 {
327         if (j && *j == "analyse_audio") {
328                 setup_peak ();
329         }
330 }
331
332 void
333 AudioPanel::reference_clicked ()
334 {
335         ContentList c = _parent->selected ();
336         if (c.size() != 1) {
337                 return;
338         }
339
340         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
341         if (!d) {
342                 return;
343         }
344
345         d->set_reference_audio (_reference->GetValue ());
346 }