Peak value of audio into the audio tab.
[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 <wx/spinctrl.h>
31 #include <boost/lexical_cast.hpp>
32 #include <boost/foreach.hpp>
33
34 using std::vector;
35 using std::cout;
36 using std::string;
37 using std::list;
38 using std::pair;
39 using boost::dynamic_pointer_cast;
40 using boost::lexical_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         {
54                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
55                 _show = new wxButton (this, wxID_ANY, _("Show graph of audio levels..."));
56                 s->Add (_show);
57                 _peak = new wxStaticText (this, wxID_ANY, wxT (""));
58                 s->Add (_peak, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
59                 grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
60                 ++r;
61         }
62
63         add_label_to_grid_bag_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_grid_bag_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_grid_bag_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_grid_bag_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         _show->Bind                  (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&AudioPanel::show_clicked, this));
110         _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
111
112         _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
113
114         JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1));
115 }
116
117 AudioPanel::~AudioPanel ()
118 {
119         if (_audio_dialog) {
120                 _audio_dialog->Destroy ();
121                 _audio_dialog = 0;
122         }
123 }
124
125 void
126 AudioPanel::film_changed (Film::Property property)
127 {
128         switch (property) {
129         case Film::AUDIO_CHANNELS:
130         case Film::AUDIO_PROCESSOR:
131                 _mapping->set_output_channels (_parent->film()->audio_output_names ());
132                 setup_peak ();
133                 break;
134         case Film::VIDEO_FRAME_RATE:
135                 setup_description ();
136                 break;
137         default:
138                 break;
139         }
140 }
141
142 void
143 AudioPanel::film_content_changed (int property)
144 {
145         if (property == AudioContentProperty::AUDIO_STREAMS) {
146                 AudioContentList ac = _parent->selected_audio ();
147                 if (ac.size() == 1) {
148                         _mapping->set (ac.front()->audio_mapping());
149                         _mapping->set_input_channels (ac.front()->audio_channel_names ());
150                 } else {
151                         _mapping->set (AudioMapping ());
152                 }
153                 setup_description ();
154                 setup_peak ();
155                 _sizer->Layout ();
156         } else if (property == AudioContentProperty::AUDIO_GAIN) {
157                 setup_peak ();
158         }
159 }
160
161 void
162 AudioPanel::gain_calculate_button_clicked ()
163 {
164         GainCalculatorDialog* d = new GainCalculatorDialog (this);
165         int const r = d->ShowModal ();
166
167         if (r == wxID_CANCEL || d->wanted_fader() == 0 || d->actual_fader() == 0) {
168                 d->Destroy ();
169                 return;
170         }
171
172         _gain->wrapped()->SetValue (
173                 Config::instance()->cinema_sound_processor()->db_for_fader_change (
174                         d->wanted_fader (),
175                         d->actual_fader ()
176                         )
177                 );
178
179         /* This appears to be necessary, as the change is not signalled,
180            I think.
181         */
182         _gain->view_changed ();
183
184         d->Destroy ();
185 }
186
187 void
188 AudioPanel::setup_description ()
189 {
190         AudioContentList ac = _parent->selected_audio ();
191         if (ac.size () != 1) {
192                 checked_set (_description, wxT (""));
193                 return;
194         }
195
196         checked_set (_description, ac.front()->processing_description ());
197 }
198
199 void
200 AudioPanel::mapping_changed (AudioMapping m)
201 {
202         AudioContentList c = _parent->selected_audio ();
203         if (c.size() == 1) {
204                 c.front()->set_audio_mapping (m);
205         }
206 }
207
208 void
209 AudioPanel::content_selection_changed ()
210 {
211         AudioContentList sel = _parent->selected_audio ();
212
213         _gain->set_content (sel);
214         _delay->set_content (sel);
215
216         _gain_calculate_button->Enable (sel.size() == 1);
217         _mapping->Enable (sel.size() == 1);
218
219         film_content_changed (AudioContentProperty::AUDIO_STREAMS);
220 }
221
222 void
223 AudioPanel::show_clicked ()
224 {
225         if (_audio_dialog) {
226                 _audio_dialog->Destroy ();
227                 _audio_dialog = 0;
228         }
229
230         AudioContentList ac = _parent->selected_audio ();
231         if (ac.size() != 1) {
232                 return;
233         }
234
235         _audio_dialog = new AudioDialog (this, _parent->film (), ac.front ());
236         _audio_dialog->Show ();
237 }
238
239 void
240 AudioPanel::setup_peak ()
241 {
242         AudioContentList sel = _parent->selected_audio ();
243         bool alert = false;
244
245         if (sel.size() != 1) {
246                 _peak->SetLabel (wxT (""));
247         } else {
248                 shared_ptr<Playlist> playlist (new Playlist);
249                 playlist->add (sel.front ());
250                 try {
251                         shared_ptr<AudioAnalysis> analysis (new AudioAnalysis (_parent->film()->audio_analysis_path (playlist)));
252                         if (analysis->peak ()) {
253                                 float const peak_dB = 20 * log10 (analysis->peak().get()) + analysis->gain_correction (playlist);
254                                 if (peak_dB > -3) {
255                                         alert = true;
256                                 }
257                                 _peak->SetLabel (wxString::Format (_("Peak: %.2fdB"), peak_dB));
258                         } else {
259                                 _peak->SetLabel (_("Peak: unknown"));
260                         }
261                 } catch (...) {
262                         _peak->SetLabel (_("Peak: unknown"));
263                 }
264         }
265
266         static wxColour normal = _peak->GetForegroundColour ();
267
268         if (alert) {
269                 _peak->SetForegroundColour (wxColour (255, 0, 0));
270         } else {
271                 _peak->SetForegroundColour (normal);
272         }
273 }
274
275 void
276 AudioPanel::active_jobs_changed (optional<string> j)
277 {
278         if (j && *j == "analyse_audio") {
279                 setup_peak ();
280         }
281 }