std::shared_ptr
[dcpomatic.git] / src / wx / audio_panel.cc
1 /*
2     Copyright (C) 2012-2018 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 "static_text.h"
28 #include "check_box.h"
29 #include "dcpomatic_button.h"
30 #include "lib/config.h"
31 #include "lib/ffmpeg_audio_stream.h"
32 #include "lib/ffmpeg_content.h"
33 #include "lib/cinema_sound_processor.h"
34 #include "lib/job_manager.h"
35 #include "lib/dcp_content.h"
36 #include "lib/audio_content.h"
37 #include <wx/spinctrl.h>
38 #include <boost/foreach.hpp>
39 #include <iostream>
40
41 using std::vector;
42 using std::cout;
43 using std::string;
44 using std::list;
45 using std::pair;
46 using std::dynamic_pointer_cast;
47 using std::shared_ptr;
48 using boost::optional;
49 #if BOOST_VERSION >= 106100
50 using namespace boost::placeholders;
51 #endif
52
53 AudioPanel::AudioPanel (ContentPanel* p)
54         : ContentSubPanel (p, _("Audio"))
55         , _audio_dialog (0)
56 {
57         _reference = new CheckBox (this, _("Use this DCP's audio as OV and make VF"));
58         _reference_note = new StaticText (this, wxT(""));
59         _reference_note->Wrap (200);
60         wxFont font = _reference_note->GetFont();
61         font.SetStyle(wxFONTSTYLE_ITALIC);
62         font.SetPointSize(font.GetPointSize() - 1);
63         _reference_note->SetFont(font);
64
65         _show = new Button (this, _("Show graph of audio levels..."));
66         _peak = new StaticText (this, wxT (""));
67
68         _gain_label = create_label (this, _("Gain"), true);
69         _gain = new ContentSpinCtrlDouble<AudioContent> (
70                 this,
71                 new wxSpinCtrlDouble (this),
72                 AudioContentProperty::GAIN,
73                 &Content::audio,
74                 boost::mem_fn (&AudioContent::gain),
75                 boost::mem_fn (&AudioContent::set_gain)
76                 );
77
78         _gain_db_label = create_label (this, _("dB"), false);
79         _gain_calculate_button = new Button (this, _("Calculate..."));
80
81         _delay_label = create_label (this, _("Delay"), true);
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         /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
92         _delay_ms_label = create_label (this, _("ms"), false);
93
94         _mapping = new AudioMappingView (this, _("Content"), _("content"), _("DCP"), _("DCP"));
95         _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
96
97         _description = new StaticText (this, wxT(" \n"), wxDefaultPosition, wxDefaultSize);
98         _sizer->Add (_description, 0, wxALL, 12);
99         _description->SetFont (font);
100
101         _gain->wrapped()->SetRange (-60, 60);
102         _gain->wrapped()->SetDigits (1);
103         _gain->wrapped()->SetIncrement (0.5);
104         _delay->wrapped()->SetRange (-1000, 1000);
105
106         content_selection_changed ();
107         film_changed (Film::AUDIO_CHANNELS);
108         film_changed (Film::VIDEO_FRAME_RATE);
109         film_changed (Film::REEL_TYPE);
110
111         _reference->Bind             (wxEVT_CHECKBOX, boost::bind (&AudioPanel::reference_clicked, this));
112         _show->Bind                  (wxEVT_BUTTON,   boost::bind (&AudioPanel::show_clicked, this));
113         _gain_calculate_button->Bind (wxEVT_BUTTON,   boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
114
115         _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
116         _active_jobs_connection = JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1, _2));
117
118         add_to_grid ();
119 }
120
121 void
122 AudioPanel::add_to_grid ()
123 {
124         int r = 0;
125
126         wxBoxSizer* reference_sizer = new wxBoxSizer (wxVERTICAL);
127         reference_sizer->Add (_reference, 0);
128         reference_sizer->Add (_reference_note, 0);
129         _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 4));
130         ++r;
131
132         _grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
133         _grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
134         ++r;
135
136         add_label_to_sizer (_grid, _gain_label, true, wxGBPosition(r, 0));
137         {
138                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
139                 s->Add (_gain->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
140                 s->Add (_gain_db_label, 0, wxALIGN_CENTER_VERTICAL);
141                 _grid->Add (s, wxGBPosition(r, 1));
142         }
143
144         _grid->Add (_gain_calculate_button, wxGBPosition(r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
145         ++r;
146
147         add_label_to_sizer (_grid, _delay_label, true, wxGBPosition(r, 0));
148         wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
149         s->Add (_delay->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
150         s->Add (_delay_ms_label, 0, wxALIGN_CENTER_VERTICAL);
151         _grid->Add (s, wxGBPosition(r, 1));
152         ++r;
153 }
154
155 AudioPanel::~AudioPanel ()
156 {
157         if (_audio_dialog) {
158                 _audio_dialog->Destroy ();
159                 _audio_dialog = 0;
160         }
161 }
162
163 void
164 AudioPanel::film_changed (Film::Property property)
165 {
166         if (!_parent->film()) {
167                 return;
168         }
169
170         switch (property) {
171         case Film::AUDIO_CHANNELS:
172         case Film::AUDIO_PROCESSOR:
173                 _mapping->set_output_channels (_parent->film()->audio_output_names ());
174                 setup_peak ();
175                 break;
176         case Film::VIDEO_FRAME_RATE:
177                 setup_description ();
178                 break;
179         case Film::REEL_TYPE:
180         case Film::INTEROP:
181                 setup_sensitivity ();
182                 break;
183         default:
184                 break;
185         }
186 }
187
188 void
189 AudioPanel::film_content_changed (int property)
190 {
191         ContentList ac = _parent->selected_audio ();
192         if (property == AudioContentProperty::STREAMS) {
193                 if (ac.size() == 1) {
194                         _mapping->set (ac.front()->audio->mapping());
195                         _mapping->set_input_channels (ac.front()->audio->channel_names ());
196
197                         vector<AudioMappingView::Group> groups;
198                         int c = 0;
199                         BOOST_FOREACH (shared_ptr<const AudioStream> i, ac.front()->audio->streams()) {
200                                 shared_ptr<const FFmpegAudioStream> f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
201                                 string name = "";
202                                 if (f) {
203                                         name = f->name;
204                                         if (f->codec_name) {
205                                                 name += " (" + f->codec_name.get() + ")";
206                                         }
207                                 }
208                                 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
209                                 c += i->channels ();
210                         }
211                         _mapping->set_input_groups (groups);
212
213                 } else {
214                         _mapping->set (AudioMapping ());
215                 }
216                 setup_description ();
217                 setup_peak ();
218                 _sizer->Layout ();
219         } else if (property == AudioContentProperty::GAIN) {
220                 setup_peak ();
221         } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
222                 if (ac.size() == 1) {
223                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
224                         checked_set (_reference, dcp ? dcp->reference_audio () : false);
225                 } else {
226                         checked_set (_reference, false);
227                 }
228
229                 setup_sensitivity ();
230         } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
231                 setup_description ();
232         }
233 }
234
235 void
236 AudioPanel::gain_calculate_button_clicked ()
237 {
238         GainCalculatorDialog* d = new GainCalculatorDialog (this);
239         int const r = d->ShowModal ();
240         optional<float> c = d->db_change();
241
242         if (r == wxID_CANCEL || !c) {
243                 d->Destroy ();
244                 return;
245         }
246
247         optional<float> old_peak_dB = peak ();
248         double old_value = _gain->wrapped()->GetValue();
249         _gain->wrapped()->SetValue(old_value + *c);
250
251         /* This appears to be necessary, as the change is not signalled,
252            I think.
253         */
254         _gain->view_changed ();
255
256         optional<float> peak_dB = peak ();
257         if (old_peak_dB && *old_peak_dB < -0.5 && peak_dB && *peak_dB > -0.5) {
258                 error_dialog (this, _("It is not possible to adjust the content's gain for this fader change as it would cause the DCP's audio to clip.  The gain has not been changed."));
259                 _gain->wrapped()->SetValue (old_value);
260                 _gain->view_changed ();
261         }
262
263         d->Destroy ();
264 }
265
266 void
267 AudioPanel::setup_description ()
268 {
269         ContentList ac = _parent->selected_audio ();
270         if (ac.size () != 1) {
271                 checked_set (_description, wxT (""));
272                 return;
273         }
274
275         checked_set (_description, ac.front()->audio->processing_description(_parent->film()));
276 }
277
278 void
279 AudioPanel::mapping_changed (AudioMapping m)
280 {
281         ContentList c = _parent->selected_audio ();
282         if (c.size() == 1) {
283                 c.front()->audio->set_mapping (m);
284         }
285 }
286
287 void
288 AudioPanel::content_selection_changed ()
289 {
290         ContentList sel = _parent->selected_audio ();
291
292         _gain->set_content (sel);
293         _delay->set_content (sel);
294
295         film_content_changed (AudioContentProperty::STREAMS);
296         film_content_changed (AudioContentProperty::GAIN);
297         film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
298
299         setup_sensitivity ();
300 }
301
302 void
303 AudioPanel::setup_sensitivity ()
304 {
305         ContentList sel = _parent->selected_audio ();
306
307         shared_ptr<DCPContent> dcp;
308         if (sel.size() == 1) {
309                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
310         }
311
312         string why_not;
313         bool const can_reference = dcp && dcp->can_reference_audio (_parent->film(), why_not);
314         wxString cannot;
315         if (why_not.empty()) {
316                 cannot = _("Cannot reference this DCP's audio.");
317         } else {
318                 cannot = _("Cannot reference this DCP's audio: ") + std_to_wx(why_not);
319         }
320         setup_refer_button (_reference, _reference_note, dcp, can_reference, cannot);
321
322         if (_reference->GetValue ()) {
323                 _gain->wrapped()->Enable (false);
324                 _gain_calculate_button->Enable (false);
325                 _show->Enable (true);
326                 _peak->Enable (false);
327                 _delay->wrapped()->Enable (false);
328                 _mapping->Enable (false);
329                 _description->Enable (false);
330         } else {
331                 _gain->wrapped()->Enable (sel.size() == 1);
332                 _gain_calculate_button->Enable (sel.size() == 1);
333                 _show->Enable (sel.size() == 1);
334                 _peak->Enable (sel.size() == 1);
335                 _delay->wrapped()->Enable (sel.size() == 1);
336                 _mapping->Enable (sel.size() == 1);
337                 _description->Enable (sel.size() == 1);
338         }
339 }
340
341 void
342 AudioPanel::show_clicked ()
343 {
344         if (_audio_dialog) {
345                 _audio_dialog->Destroy ();
346                 _audio_dialog = 0;
347         }
348
349         ContentList ac = _parent->selected_audio ();
350         if (ac.size() != 1) {
351                 return;
352         }
353
354         _audio_dialog = new AudioDialog (this, _parent->film(), _parent->film_viewer(), ac.front());
355         _audio_dialog->Show ();
356 }
357
358 /** @return If there is one selected piece of audio content, return its peak value in dB (if known) */
359 optional<float>
360 AudioPanel::peak () const
361 {
362         optional<float> peak_dB;
363
364         ContentList sel = _parent->selected_audio ();
365         if (sel.size() == 1) {
366                 shared_ptr<Playlist> playlist (new Playlist);
367                 playlist->add (_parent->film(), sel.front());
368                 try {
369                         shared_ptr<AudioAnalysis> analysis (new AudioAnalysis(_parent->film()->audio_analysis_path(playlist)));
370                         peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
371                 } catch (...) {
372
373                 }
374         }
375
376         return peak_dB;
377 }
378
379 void
380 AudioPanel::setup_peak ()
381 {
382         ContentList sel = _parent->selected_audio ();
383
384         optional<float> peak_dB = peak ();
385         if (sel.size() != 1) {
386                 _peak->SetLabel (wxT(""));
387         } else {
388                 peak_dB = peak ();
389                 if (peak_dB) {
390                         _peak->SetLabel (wxString::Format(_("Peak: %.2fdB"), *peak_dB));
391                 } else {
392                         _peak->SetLabel (_("Peak: unknown"));
393                 }
394         }
395
396         static wxColour normal = _peak->GetForegroundColour ();
397
398         if (peak_dB && *peak_dB > -0.5) {
399                 _peak->SetForegroundColour (wxColour (255, 0, 0));
400         } else if (peak_dB && *peak_dB > -3) {
401                 _peak->SetForegroundColour (wxColour (186, 120, 0));
402         } else {
403                 _peak->SetForegroundColour (normal);
404         }
405 }
406
407 void
408 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
409 {
410         if (old_active && *old_active == "analyse_audio") {
411                 setup_peak ();
412                 _mapping->Enable (true);
413         } else if (new_active && *new_active == "analyse_audio") {
414                 _mapping->Enable (false);
415         }
416 }
417
418 void
419 AudioPanel::reference_clicked ()
420 {
421         ContentList c = _parent->selected ();
422         if (c.size() != 1) {
423                 return;
424         }
425
426         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
427         if (!d) {
428                 return;
429         }
430
431         d->set_reference_audio (_reference->GetValue ());
432 }
433
434 void
435 AudioPanel::set_film (shared_ptr<Film>)
436 {
437         /* We are changing film, so destroy any audio dialog for the old one */
438         if (_audio_dialog) {
439                 _audio_dialog->Destroy ();
440                 _audio_dialog = 0;
441         }
442 }