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