2 Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "audio_dialog.h"
22 #include "audio_plot.h"
24 #include "lib/audio_analysis.h"
26 #include "lib/analyse_audio_job.h"
27 #include "lib/audio_content.h"
28 #include "lib/job_manager.h"
29 #include <libxml++/libxml++.h>
30 #include <boost/filesystem.hpp>
31 #include <boost/foreach.hpp>
38 using boost::shared_ptr;
40 using boost::optional;
41 using boost::const_pointer_cast;
42 using boost::dynamic_pointer_cast;
44 /** @param parent Parent window.
45 * @param film Film we are using.
46 * @param content Content to analyse, or 0 to analyse all of the film's audio.
48 AudioDialog::AudioDialog (wxWindow* parent, shared_ptr<Film> film, shared_ptr<Content> content)
56 /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
57 the window above all others (and not just our own) it's better than nothing for now.
59 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
61 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
66 , _channels (film->audio_channels ())
69 wxFont subheading_font (*wxNORMAL_FONT);
70 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
72 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
73 wxBoxSizer* lr_sizer = new wxBoxSizer (wxHORIZONTAL);
75 wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
77 _plot = new AudioPlot (this);
78 left->Add (_plot, 1, wxTOP | wxEXPAND, 12);
79 _sample_peak = new wxStaticText (this, wxID_ANY, wxT (""));
80 left->Add (_sample_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
81 _true_peak = new wxStaticText (this, wxID_ANY, wxT (""));
82 left->Add (_true_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
83 _integrated_loudness = new wxStaticText (this, wxID_ANY, wxT (""));
84 left->Add (_integrated_loudness, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
85 _loudness_range = new wxStaticText (this, wxID_ANY, wxT (""));
86 left->Add (_loudness_range, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
88 lr_sizer->Add (left, 1, wxALL | wxEXPAND, 12);
90 wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
93 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Channels"));
94 m->SetFont (subheading_font);
95 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM, 16);
98 for (int i = 0; i < _channels; ++i) {
99 _channel_checkbox[i] = new wxCheckBox (this, wxID_ANY, std_to_wx (audio_channel_name (i)));
100 _channel_checkbox[i]->SetForegroundColour(wxColour(_plot->colour(i)));
101 right->Add (_channel_checkbox[i], 0, wxEXPAND | wxALL, 3);
102 _channel_checkbox[i]->Bind (wxEVT_CHECKBOX, boost::bind (&AudioDialog::channel_clicked, this, _1));
105 for (int i = _channels; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
106 _channel_checkbox[i] = 0;
110 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Type"));
111 m->SetFont (subheading_font);
112 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
115 wxString const types[] = {
120 for (int i = 0; i < AudioPoint::COUNT; ++i) {
121 _type_checkbox[i] = new wxCheckBox (this, wxID_ANY, types[i]);
122 right->Add (_type_checkbox[i], 0, wxEXPAND | wxALL, 3);
123 _type_checkbox[i]->Bind (wxEVT_CHECKBOX, boost::bind (&AudioDialog::type_clicked, this, _1));
127 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Smoothing"));
128 m->SetFont (subheading_font);
129 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
132 _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
133 _smoothing->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&AudioDialog::smoothing_changed, this));
134 right->Add (_smoothing, 0, wxEXPAND);
136 lr_sizer->Add (right, 0, wxALL, 12);
138 overall_sizer->Add (lr_sizer, 0, wxEXPAND);
140 #ifdef DCPOMATIC_LINUX
141 wxSizer* buttons = CreateSeparatedButtonSizer (wxCLOSE);
143 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
147 SetSizer (overall_sizer);
148 overall_sizer->Layout ();
149 overall_sizer->SetSizeHints (this);
151 _film_connection = film->ContentChanged.connect (boost::bind (&AudioDialog::content_changed, this, _2));
152 SetTitle (_("DCP-o-matic audio"));
155 _playlist.reset (new Playlist ());
156 const_pointer_cast<Playlist> (_playlist)->add (content);
158 _playlist = film->playlist ();
163 AudioDialog::try_to_load_analysis ()
169 shared_ptr<const Film> film = _film.lock ();
170 DCPOMATIC_ASSERT (film);
172 shared_ptr<Content> check = _content.lock();
174 boost::filesystem::path const path = film->audio_analysis_path (_playlist);
175 if (!boost::filesystem::exists (path)) {
176 _plot->set_analysis (shared_ptr<AudioAnalysis> ());
178 JobManager::instance()->analyse_audio (
179 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
185 _analysis.reset (new AudioAnalysis (path));
186 } catch (OldFormatError& e) {
187 /* An old analysis file: recreate it */
188 JobManager::instance()->analyse_audio (
189 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
192 } catch (xmlpp::exception& e) {
193 /* Probably a (very) old-style analysis file: recreate it */
194 JobManager::instance()->analyse_audio (
195 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
200 _plot->set_analysis (_analysis);
201 _plot->set_gain_correction (_analysis->gain_correction (_playlist));
204 /* Set up some defaults if no check boxes are checked */
207 while (i < _channels && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
211 if (i == _channels) {
212 /* Nothing checked; check mapped ones */
215 shared_ptr<Content> content = _content.lock ();
218 mapped = content->audio->mapping().mapped_output_channels ();
220 mapped = film->mapped_audio_channels ();
223 BOOST_FOREACH (int i, mapped) {
224 if (_channel_checkbox[i]) {
225 _channel_checkbox[i]->SetValue (true);
226 _plot->set_channel_visible (i, true);
232 while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
236 if (i == AudioPoint::COUNT) {
237 for (int i = 0; i < AudioPoint::COUNT; ++i) {
238 _type_checkbox[i]->SetValue (true);
239 _plot->set_type_visible (i, true);
247 AudioDialog::analysis_finished ()
249 shared_ptr<const Film> film = _film.lock ();
251 /* This should not happen, but if it does we should just give up quietly */
255 if (!boost::filesystem::exists (film->audio_analysis_path (_playlist))) {
256 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
259 _plot->set_message (_("Could not analyse audio."));
263 try_to_load_analysis ();
267 AudioDialog::channel_clicked (wxCommandEvent& ev)
270 while (c < _channels && ev.GetEventObject() != _channel_checkbox[c]) {
274 DCPOMATIC_ASSERT (c < _channels);
276 _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
280 AudioDialog::content_changed (int p)
282 if (p == AudioContentProperty::STREAMS) {
283 try_to_load_analysis ();
284 } else if (p == AudioContentProperty::GAIN) {
285 if (_playlist->content().size() == 1 && _analysis) {
286 /* We can use a short-cut to render the effect of this
287 change, rather than recalculating everything.
289 _plot->set_gain_correction (_analysis->gain_correction (_playlist));
292 try_to_load_analysis ();
298 AudioDialog::type_clicked (wxCommandEvent& ev)
301 while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
305 DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
307 _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
311 AudioDialog::smoothing_changed ()
313 _plot->set_smoothing (_smoothing->GetValue ());
317 AudioDialog::setup_statistics ()
323 shared_ptr<Film> film = _film.lock ();
328 pair<AudioAnalysis::PeakTime, int> const peak = _analysis->overall_sample_peak ();
329 float const peak_dB = 20 * log10 (peak.first.peak) + _analysis->gain_correction (_playlist);
330 _sample_peak->SetLabel (
332 _("Sample peak is %.2fdB at %s on %s"),
334 time_to_timecode (peak.first.time, film->video_frame_rate ()).data (),
335 std_to_wx (short_audio_channel_name (peak.second)).data ()
340 _sample_peak->SetForegroundColour (wxColour (255, 0, 0));
342 _sample_peak->SetForegroundColour (wxColour (0, 0, 0));
345 if (_analysis->overall_true_peak()) {
346 float const peak = _analysis->overall_true_peak().get();
347 float const peak_dB = 20 * log10 (peak) + _analysis->gain_correction (_playlist);
349 _true_peak->SetLabel (wxString::Format (_("True peak is %.2fdB"), peak_dB));
352 _true_peak->SetForegroundColour (wxColour (255, 0, 0));
354 _true_peak->SetForegroundColour (wxColour (0, 0, 0));
358 /* XXX: check whether it's ok to add dB gain to these quantities */
360 if (static_cast<bool>(_analysis->integrated_loudness ())) {
361 _integrated_loudness->SetLabel (
363 _("Integrated loudness %.2f LUFS"),
364 _analysis->integrated_loudness().get() + _analysis->gain_correction (_playlist)
369 if (static_cast<bool>(_analysis->loudness_range ())) {
370 _loudness_range->SetLabel (
372 _("Loudness range %.2f LU"),
373 _analysis->loudness_range().get() + _analysis->gain_correction (_playlist)
380 AudioDialog::Show (bool show)
382 bool const r = wxDialog::Show (show);
383 try_to_load_analysis ();