No-op: remove all trailing whitespace.
[dcpomatic.git] / src / wx / audio_dialog.cc
1 /*
2     Copyright (C) 2013-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_dialog.h"
21 #include "audio_plot.h"
22 #include "wx_util.h"
23 #include "lib/audio_analysis.h"
24 #include "lib/film.h"
25 #include "lib/analyse_audio_job.h"
26 #include "lib/audio_content.h"
27 #include "lib/job_manager.h"
28 #include <boost/filesystem.hpp>
29
30 using boost::shared_ptr;
31 using boost::bind;
32 using boost::optional;
33
34 AudioDialog::AudioDialog (wxWindow* parent, shared_ptr<Film> film)
35         : wxDialog (parent, wxID_ANY, _("Audio"), wxDefaultPosition, wxSize (640, 512), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE)
36         , _film (film)
37         , _plot (0)
38 {
39         wxFont subheading_font (*wxNORMAL_FONT);
40         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
41
42         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
43         wxBoxSizer* lr_sizer = new wxBoxSizer (wxHORIZONTAL);
44
45         wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
46
47         _plot = new AudioPlot (this);
48         left->Add (_plot, 1, wxALL | wxEXPAND, 12);
49         _peak_time = new wxStaticText (this, wxID_ANY, wxT (""));
50         left->Add (_peak_time, 0, wxALL, 12);
51
52         lr_sizer->Add (left, 1, wxALL, 12);
53
54         wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
55
56         {
57                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Channels"));
58                 m->SetFont (subheading_font);
59                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM, 16);
60         }
61
62         for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
63                 _channel_checkbox[i] = new wxCheckBox (this, wxID_ANY, std_to_wx (audio_channel_name (i)));
64                 right->Add (_channel_checkbox[i], 0, wxEXPAND | wxALL, 3);
65                 _channel_checkbox[i]->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioDialog::channel_clicked, this, _1));
66         }
67
68         {
69                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Type"));
70                 m->SetFont (subheading_font);
71                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
72         }
73
74         wxString const types[] = {
75                 _("Peak"),
76                 _("RMS")
77         };
78
79         for (int i = 0; i < AudioPoint::COUNT; ++i) {
80                 _type_checkbox[i] = new wxCheckBox (this, wxID_ANY, types[i]);
81                 right->Add (_type_checkbox[i], 0, wxEXPAND | wxALL, 3);
82                 _type_checkbox[i]->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioDialog::type_clicked, this, _1));
83         }
84
85         {
86                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Smoothing"));
87                 m->SetFont (subheading_font);
88                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
89         }
90
91         _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
92         _smoothing->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&AudioDialog::smoothing_changed, this));
93         right->Add (_smoothing, 0, wxEXPAND);
94
95         lr_sizer->Add (right, 0, wxALL, 12);
96
97         overall_sizer->Add (lr_sizer);
98
99 #ifdef DCPOMATIC_LINUX
100         wxSizer* buttons = CreateSeparatedButtonSizer (wxCLOSE);
101         if (buttons) {
102                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
103         }
104 #endif
105
106         SetSizer (overall_sizer);
107         overall_sizer->Layout ();
108         overall_sizer->SetSizeHints (this);
109
110         _film_connection = film->ContentChanged.connect (boost::bind (&AudioDialog::try_to_load_analysis, this));
111         SetTitle (_("DCP-o-matic audio"));
112 }
113
114 void
115 AudioDialog::try_to_load_analysis ()
116 {
117         if (!IsShown ()) {
118                 return;
119         }
120
121         shared_ptr<const Film> film = _film.lock ();
122         DCPOMATIC_ASSERT (film);
123
124         boost::filesystem::path path = film->audio_analysis_path ();
125
126         if (!boost::filesystem::exists (path)) {
127                 _plot->set_analysis (shared_ptr<AudioAnalysis> ());
128                 _analysis.reset ();
129                 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film));
130                 _analysis_finished_connection = job->Finished.connect (bind (&AudioDialog::analysis_finished, this));
131                 JobManager::instance()->add (job);
132                 return;
133         }
134
135         try {
136                 _analysis.reset (new AudioAnalysis (path));
137         } catch (xmlpp::exception& e) {
138                 /* Probably an old-style analysis file: recreate it */
139                 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film));
140                 _analysis_finished_connection = job->Finished.connect (bind (&AudioDialog::analysis_finished, this));
141                 JobManager::instance()->add (job);
142                 return;
143         }
144
145         _plot->set_analysis (_analysis);
146         setup_peak_time ();
147
148         /* Set up some defaults if no check boxes are checked */
149
150         int i = 0;
151         while (i < MAX_DCP_AUDIO_CHANNELS && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
152                 ++i;
153         }
154
155         if (i == MAX_DCP_AUDIO_CHANNELS && _channel_checkbox[0]) {
156                 _channel_checkbox[0]->SetValue (true);
157                 _plot->set_channel_visible (0, true);
158         }
159
160         i = 0;
161         while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
162                 i++;
163         }
164
165         if (i == AudioPoint::COUNT) {
166                 for (int i = 0; i < AudioPoint::COUNT; ++i) {
167                         _type_checkbox[i]->SetValue (true);
168                         _plot->set_type_visible (i, true);
169                 }
170         }
171
172         Refresh ();
173 }
174
175 void
176 AudioDialog::analysis_finished ()
177 {
178         shared_ptr<const Film> film = _film.lock ();
179         DCPOMATIC_ASSERT (film);
180
181         if (!boost::filesystem::exists (film->audio_analysis_path ())) {
182                 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
183                    Give up.
184                 */
185                 _plot->set_message (_("Could not analyse audio."));
186                 return;
187         }
188
189         try_to_load_analysis ();
190 }
191
192 void
193 AudioDialog::channel_clicked (wxCommandEvent& ev)
194 {
195         int c = 0;
196         while (c < MAX_DCP_AUDIO_CHANNELS && ev.GetEventObject() != _channel_checkbox[c]) {
197                 ++c;
198         }
199
200         DCPOMATIC_ASSERT (c < MAX_DCP_AUDIO_CHANNELS);
201
202         _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
203 }
204
205 void
206 AudioDialog::content_changed (int p)
207 {
208         if (p == AudioContentProperty::AUDIO_GAIN || p == AudioContentProperty::AUDIO_STREAMS) {
209                 try_to_load_analysis ();
210         }
211 }
212
213 void
214 AudioDialog::type_clicked (wxCommandEvent& ev)
215 {
216         int t = 0;
217         while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
218                 ++t;
219         }
220
221         DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
222
223         _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
224 }
225
226 void
227 AudioDialog::smoothing_changed ()
228 {
229         _plot->set_smoothing (_smoothing->GetValue ());
230 }
231
232 void
233 AudioDialog::setup_peak_time ()
234 {
235         if (!_analysis || !_analysis->peak ()) {
236                 return;
237         }
238
239         shared_ptr<Film> film = _film.lock ();
240         if (!film) {
241                 return;
242         }
243
244         float peak_dB = 20 * log10 (_analysis->peak().get());
245
246         _peak_time->SetLabel (
247                 wxString::Format (
248                         _("Peak is %.2fdB at %s"),
249                         peak_dB,
250                         time_to_timecode (_analysis->peak_time().get(), film->video_frame_rate ()).data ()
251                         )
252                 );
253
254         if (peak_dB > -3) {
255                 _peak_time->SetForegroundColour (wxColour (255, 0, 0));
256         } else {
257                 _peak_time->SetForegroundColour (wxColour (0, 0, 0));
258         }
259 }
260
261 bool
262 AudioDialog::Show (bool show)
263 {
264         bool const r = wxDialog::Show (show);
265         try_to_load_analysis ();
266         return r;
267 }
268
269