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