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