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