Front-end to set up referencing of DCPs.
[dcpomatic.git] / src / wx / audio_panel.cc
1 /*
2     Copyright (C) 2012-2015 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 <wx/spinctrl.h>
32 #include <boost/lexical_cast.hpp>
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::lexical_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         {
60                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
61                 _show = new wxButton (this, wxID_ANY, _("Show graph of audio levels..."));
62                 s->Add (_show);
63                 _peak = new wxStaticText (this, wxID_ANY, wxT (""));
64                 s->Add (_peak, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
65                 grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
66                 ++r;
67         }
68
69         add_label_to_grid_bag_sizer (grid, this, _("Gain"), true, wxGBPosition (r, 0));
70         _gain = new ContentSpinCtrlDouble<AudioContent> (
71                 this,
72                 new wxSpinCtrlDouble (this),
73                 AudioContentProperty::AUDIO_GAIN,
74                 boost::mem_fn (&AudioContent::audio_gain),
75                 boost::mem_fn (&AudioContent::set_audio_gain)
76                 );
77
78         _gain->add (grid, wxGBPosition (r, 1));
79         add_label_to_grid_bag_sizer (grid, this, _("dB"), false, wxGBPosition (r, 2));
80         _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
81         grid->Add (_gain_calculate_button, wxGBPosition (r, 3));
82         ++r;
83
84         add_label_to_grid_bag_sizer (grid, this, _("Delay"), true, wxGBPosition (r, 0));
85         _delay = new ContentSpinCtrl<AudioContent> (
86                 this,
87                 new wxSpinCtrl (this),
88                 AudioContentProperty::AUDIO_DELAY,
89                 boost::mem_fn (&AudioContent::audio_delay),
90                 boost::mem_fn (&AudioContent::set_audio_delay)
91                 );
92
93         _delay->add (grid, wxGBPosition (r, 1));
94         /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
95         add_label_to_grid_bag_sizer (grid, this, _("ms"), false, wxGBPosition (r, 2));
96         ++r;
97
98         _mapping = new AudioMappingView (this);
99         _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
100         ++r;
101
102         _description = new wxStaticText (this, wxID_ANY, wxT (" \n"), wxDefaultPosition, wxDefaultSize);
103         _sizer->Add (_description, 0, wxALL, 12);
104         wxFont font = _description->GetFont();
105         font.SetStyle (wxFONTSTYLE_ITALIC);
106         font.SetPointSize (font.GetPointSize() - 1);
107         _description->SetFont (font);
108         ++r;
109
110         _gain->wrapped()->SetRange (-60, 60);
111         _gain->wrapped()->SetDigits (1);
112         _gain->wrapped()->SetIncrement (0.5);
113         _delay->wrapped()->SetRange (-1000, 1000);
114
115         _reference->Bind             (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioPanel::reference_clicked, this));
116         _show->Bind                  (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&AudioPanel::show_clicked, this));
117         _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
118
119         _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
120
121         JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1));
122 }
123
124 AudioPanel::~AudioPanel ()
125 {
126         if (_audio_dialog) {
127                 _audio_dialog->Destroy ();
128                 _audio_dialog = 0;
129         }
130 }
131
132 void
133 AudioPanel::film_changed (Film::Property property)
134 {
135         switch (property) {
136         case Film::AUDIO_CHANNELS:
137         case Film::AUDIO_PROCESSOR:
138                 _mapping->set_output_channels (_parent->film()->audio_output_names ());
139                 setup_peak ();
140                 break;
141         case Film::VIDEO_FRAME_RATE:
142                 setup_description ();
143                 break;
144         default:
145                 break;
146         }
147 }
148
149 void
150 AudioPanel::film_content_changed (int property)
151 {
152         AudioContentList ac = _parent->selected_audio ();
153         if (property == AudioContentProperty::AUDIO_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::AUDIO_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         }
175 }
176
177 void
178 AudioPanel::gain_calculate_button_clicked ()
179 {
180         GainCalculatorDialog* d = new GainCalculatorDialog (this);
181         int const r = d->ShowModal ();
182
183         if (r == wxID_CANCEL || d->wanted_fader() == 0 || d->actual_fader() == 0) {
184                 d->Destroy ();
185                 return;
186         }
187
188         _gain->wrapped()->SetValue (
189                 Config::instance()->cinema_sound_processor()->db_for_fader_change (
190                         d->wanted_fader (),
191                         d->actual_fader ()
192                         )
193                 );
194
195         /* This appears to be necessary, as the change is not signalled,
196            I think.
197         */
198         _gain->view_changed ();
199
200         d->Destroy ();
201 }
202
203 void
204 AudioPanel::setup_description ()
205 {
206         AudioContentList ac = _parent->selected_audio ();
207         if (ac.size () != 1) {
208                 checked_set (_description, wxT (""));
209                 return;
210         }
211
212         checked_set (_description, ac.front()->processing_description ());
213 }
214
215 void
216 AudioPanel::mapping_changed (AudioMapping m)
217 {
218         AudioContentList c = _parent->selected_audio ();
219         if (c.size() == 1) {
220                 c.front()->set_audio_mapping (m);
221         }
222 }
223
224 void
225 AudioPanel::content_selection_changed ()
226 {
227         AudioContentList sel = _parent->selected_audio ();
228
229         _gain->set_content (sel);
230         _delay->set_content (sel);
231
232         film_content_changed (AudioContentProperty::AUDIO_STREAMS);
233
234         setup_sensitivity ();
235 }
236
237 void
238 AudioPanel::setup_sensitivity ()
239 {
240         AudioContentList sel = _parent->selected_audio ();
241         _reference->Enable (sel.size() == 1 && dynamic_pointer_cast<DCPContent> (sel.front ()));
242
243         if (_reference->GetValue ()) {
244                 _gain->wrapped()->Enable (false);
245                 _gain_calculate_button->Enable (false);
246                 _peak->Enable (false);
247                 _delay->wrapped()->Enable (false);
248                 _mapping->Enable (false);
249                 _description->Enable (false);
250         } else {
251                 _gain->wrapped()->Enable (true);
252                 _gain_calculate_button->Enable (sel.size() == 1);
253                 _peak->Enable (true);
254                 _delay->wrapped()->Enable (true);
255                 _mapping->Enable (sel.size() == 1);
256                 _description->Enable (true);
257         }
258 }
259
260 void
261 AudioPanel::show_clicked ()
262 {
263         if (_audio_dialog) {
264                 _audio_dialog->Destroy ();
265                 _audio_dialog = 0;
266         }
267
268         AudioContentList ac = _parent->selected_audio ();
269         if (ac.size() != 1) {
270                 return;
271         }
272
273         _audio_dialog = new AudioDialog (this, _parent->film (), ac.front ());
274         _audio_dialog->Show ();
275 }
276
277 void
278 AudioPanel::setup_peak ()
279 {
280         AudioContentList sel = _parent->selected_audio ();
281         bool alert = false;
282
283         if (sel.size() != 1) {
284                 _peak->SetLabel (wxT (""));
285         } else {
286                 shared_ptr<Playlist> playlist (new Playlist);
287                 playlist->add (sel.front ());
288                 try {
289                         shared_ptr<AudioAnalysis> analysis (new AudioAnalysis (_parent->film()->audio_analysis_path (playlist)));
290                         if (analysis->peak ()) {
291                                 float const peak_dB = 20 * log10 (analysis->peak().get()) + analysis->gain_correction (playlist);
292                                 if (peak_dB > -3) {
293                                         alert = true;
294                                 }
295                                 _peak->SetLabel (wxString::Format (_("Peak: %.2fdB"), peak_dB));
296                         } else {
297                                 _peak->SetLabel (_("Peak: unknown"));
298                         }
299                 } catch (...) {
300                         _peak->SetLabel (_("Peak: unknown"));
301                 }
302         }
303
304         static wxColour normal = _peak->GetForegroundColour ();
305
306         if (alert) {
307                 _peak->SetForegroundColour (wxColour (255, 0, 0));
308         } else {
309                 _peak->SetForegroundColour (normal);
310         }
311 }
312
313 void
314 AudioPanel::active_jobs_changed (optional<string> j)
315 {
316         if (j && *j == "analyse_audio") {
317                 setup_peak ();
318         }
319 }
320
321 void
322 AudioPanel::reference_clicked ()
323 {
324         ContentList c = _parent->selected ();
325         if (c.size() != 1) {
326                 return;
327         }
328
329         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
330         if (!d) {
331                 return;
332         }
333
334         d->set_reference_audio (_reference->GetValue ());
335 }