2 Copyright (C) 2013-2018 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 "static_text.h"
25 #include "check_box.h"
26 #include "lib/audio_analysis.h"
28 #include "lib/analyse_audio_job.h"
29 #include "lib/audio_content.h"
30 #include "lib/job_manager.h"
31 #include <libxml++/libxml++.h>
32 #include <boost/filesystem.hpp>
33 #include <boost/foreach.hpp>
40 using boost::shared_ptr;
42 using boost::optional;
43 using boost::const_pointer_cast;
44 using boost::dynamic_pointer_cast;
46 /** @param parent Parent window.
47 * @param film Film we are using.
48 * @param content Content to analyse, or 0 to analyse all of the film's audio.
50 AudioDialog::AudioDialog (wxWindow* parent, shared_ptr<Film> film, shared_ptr<Content> content)
58 /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
59 the window above all others (and not just our own) it's better than nothing for now.
61 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
63 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
68 , _channels (film->audio_channels ())
71 wxFont subheading_font (*wxNORMAL_FONT);
72 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
74 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
75 wxBoxSizer* lr_sizer = new wxBoxSizer (wxHORIZONTAL);
77 wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
79 _cursor = new StaticText (this, wxT("Cursor: none"));
80 left->Add (_cursor, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
81 _plot = new AudioPlot (this);
82 left->Add (_plot, 1, wxTOP | wxEXPAND, 12);
83 _sample_peak = new StaticText (this, wxT (""));
84 left->Add (_sample_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
85 _true_peak = new StaticText (this, wxT (""));
86 left->Add (_true_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
87 _integrated_loudness = new StaticText (this, wxT (""));
88 left->Add (_integrated_loudness, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
89 _loudness_range = new StaticText (this, wxT (""));
90 left->Add (_loudness_range, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
92 lr_sizer->Add (left, 1, wxALL | wxEXPAND, 12);
94 wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
97 wxStaticText* m = new StaticText (this, _("Channels"));
98 m->SetFont (subheading_font);
99 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM, 16);
102 for (int i = 0; i < _channels; ++i) {
103 _channel_checkbox[i] = new CheckBox (this, std_to_wx(audio_channel_name(i)));
104 _channel_checkbox[i]->SetForegroundColour(wxColour(_plot->colour(i)));
105 right->Add (_channel_checkbox[i], 0, wxEXPAND | wxALL, 3);
106 _channel_checkbox[i]->Bind (wxEVT_CHECKBOX, boost::bind (&AudioDialog::channel_clicked, this, _1));
109 for (int i = _channels; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
110 _channel_checkbox[i] = 0;
114 wxStaticText* m = new StaticText (this, _("Type"));
115 m->SetFont (subheading_font);
116 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
119 wxString const types[] = {
124 for (int i = 0; i < AudioPoint::COUNT; ++i) {
125 _type_checkbox[i] = new CheckBox (this, types[i]);
126 right->Add (_type_checkbox[i], 0, wxEXPAND | wxALL, 3);
127 _type_checkbox[i]->Bind (wxEVT_CHECKBOX, boost::bind (&AudioDialog::type_clicked, this, _1));
131 wxStaticText* m = new StaticText (this, _("Smoothing"));
132 m->SetFont (subheading_font);
133 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
136 _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
137 _smoothing->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&AudioDialog::smoothing_changed, this));
138 right->Add (_smoothing, 0, wxEXPAND);
140 lr_sizer->Add (right, 0, wxALL, 12);
142 overall_sizer->Add (lr_sizer, 0, wxEXPAND);
144 #ifdef DCPOMATIC_LINUX
145 wxSizer* buttons = CreateSeparatedButtonSizer (wxCLOSE);
147 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
151 SetSizer (overall_sizer);
152 overall_sizer->Layout ();
153 overall_sizer->SetSizeHints (this);
155 _film_connection = film->ContentChange.connect (boost::bind (&AudioDialog::content_change, this, _1, _3));
156 DCPOMATIC_ASSERT (film->directory());
157 SetTitle(wxString::Format(_("DCP-o-matic audio - %s"), std_to_wx(film->directory().get().string())));
160 _playlist.reset (new Playlist ());
161 const_pointer_cast<Playlist>(_playlist)->add(film, content);
163 _playlist = film->playlist ();
166 _plot->Cursor.connect (bind (&AudioDialog::set_cursor, this, _1, _2));
170 AudioDialog::try_to_load_analysis ()
176 shared_ptr<const Film> film = _film.lock ();
177 DCPOMATIC_ASSERT (film);
179 shared_ptr<Content> check = _content.lock();
181 boost::filesystem::path const path = film->audio_analysis_path (_playlist);
182 if (!boost::filesystem::exists (path)) {
183 _plot->set_analysis (shared_ptr<AudioAnalysis> ());
185 JobManager::instance()->analyse_audio (
186 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
192 _analysis.reset (new AudioAnalysis (path));
193 } catch (OldFormatError& e) {
194 /* An old analysis file: recreate it */
195 JobManager::instance()->analyse_audio (
196 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
199 } catch (xmlpp::exception& e) {
200 /* Probably a (very) old-style analysis file: recreate it */
201 JobManager::instance()->analyse_audio (
202 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
207 _plot->set_analysis (_analysis);
208 _plot->set_gain_correction (_analysis->gain_correction (_playlist));
211 /* Set up some defaults if no check boxes are checked */
214 while (i < _channels && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
218 if (i == _channels) {
219 /* Nothing checked; check mapped ones */
222 shared_ptr<Content> content = _content.lock ();
225 mapped = content->audio->mapping().mapped_output_channels ();
227 mapped = film->mapped_audio_channels ();
230 BOOST_FOREACH (int i, mapped) {
231 if (_channel_checkbox[i]) {
232 _channel_checkbox[i]->SetValue (true);
233 _plot->set_channel_visible (i, true);
239 while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
243 if (i == AudioPoint::COUNT) {
244 for (int i = 0; i < AudioPoint::COUNT; ++i) {
245 _type_checkbox[i]->SetValue (true);
246 _plot->set_type_visible (i, true);
254 AudioDialog::analysis_finished ()
256 shared_ptr<const Film> film = _film.lock ();
258 /* This should not happen, but if it does we should just give up quietly */
262 if (!boost::filesystem::exists (film->audio_analysis_path (_playlist))) {
263 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
266 _plot->set_message (_("Could not analyse audio."));
270 try_to_load_analysis ();
274 AudioDialog::channel_clicked (wxCommandEvent& ev)
277 while (c < _channels && ev.GetEventObject() != _channel_checkbox[c]) {
281 DCPOMATIC_ASSERT (c < _channels);
283 _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
287 AudioDialog::content_change (ChangeType type, int p)
289 if (type != CHANGE_TYPE_DONE) {
293 if (p == AudioContentProperty::STREAMS) {
294 try_to_load_analysis ();
295 } else if (p == AudioContentProperty::GAIN) {
296 if (_playlist->content().size() == 1 && _analysis) {
297 /* We can use a short-cut to render the effect of this
298 change, rather than recalculating everything.
300 _plot->set_gain_correction (_analysis->gain_correction (_playlist));
303 try_to_load_analysis ();
309 AudioDialog::type_clicked (wxCommandEvent& ev)
312 while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
316 DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
318 _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
322 AudioDialog::smoothing_changed ()
324 _plot->set_smoothing (_smoothing->GetValue ());
328 AudioDialog::setup_statistics ()
334 shared_ptr<Film> film = _film.lock ();
339 pair<AudioAnalysis::PeakTime, int> const peak = _analysis->overall_sample_peak ();
340 float const peak_dB = 20 * log10 (peak.first.peak) + _analysis->gain_correction (_playlist);
341 _sample_peak->SetLabel (
343 _("Sample peak is %.2fdB at %s on %s"),
345 time_to_timecode (peak.first.time, film->video_frame_rate ()).data (),
346 std_to_wx (short_audio_channel_name (peak.second)).data ()
351 _sample_peak->SetForegroundColour (wxColour (255, 0, 0));
353 _sample_peak->SetForegroundColour (wxColour (0, 0, 0));
356 if (_analysis->overall_true_peak()) {
357 float const peak = _analysis->overall_true_peak().get();
358 float const peak_dB = 20 * log10 (peak) + _analysis->gain_correction (_playlist);
360 _true_peak->SetLabel (wxString::Format (_("True peak is %.2fdB"), peak_dB));
363 _true_peak->SetForegroundColour (wxColour (255, 0, 0));
365 _true_peak->SetForegroundColour (wxColour (0, 0, 0));
369 /* XXX: check whether it's ok to add dB gain to these quantities */
371 if (static_cast<bool>(_analysis->integrated_loudness ())) {
372 _integrated_loudness->SetLabel (
374 _("Integrated loudness %.2f LUFS"),
375 _analysis->integrated_loudness().get() + _analysis->gain_correction (_playlist)
380 if (static_cast<bool>(_analysis->loudness_range ())) {
381 _loudness_range->SetLabel (
383 _("Loudness range %.2f LU"),
384 _analysis->loudness_range().get() + _analysis->gain_correction (_playlist)
391 AudioDialog::Show (bool show)
393 bool const r = wxDialog::Show (show);
394 try_to_load_analysis ();
399 AudioDialog::set_cursor (optional<DCPTime> time, optional<float> db)
402 _cursor->SetLabel (_("Cursor: none"));
406 shared_ptr<Film> film = _film.lock();
407 DCPOMATIC_ASSERT (film);
408 _cursor->SetLabel (wxString::Format (_("Cursor: %.1fdB at %s"), *db, time->timecode(film->video_frame_rate())));