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