2 Copyright (C) 2013-2021 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/>.
22 #include "audio_dialog.h"
23 #include "audio_plot.h"
24 #include "check_box.h"
25 #include "static_text.h"
27 #include "lib/analyse_audio_job.h"
28 #include "lib/audio_analysis.h"
29 #include "lib/audio_content.h"
31 #include "lib/job_manager.h"
32 #include "lib/maths_util.h"
33 #include <libxml++/libxml++.h>
34 #include <boost/filesystem.hpp>
40 using std::make_shared;
42 using std::shared_ptr;
46 using boost::optional;
47 using boost::const_pointer_cast;
48 using std::dynamic_pointer_cast;
49 using namespace dcpomatic;
50 #if BOOST_VERSION >= 106100
51 using namespace boost::placeholders;
55 /** @param parent Parent window.
56 * @param film Film we are using.
57 * @param content Content to analyse, or 0 to analyse all of the film's audio.
59 AudioDialog::AudioDialog (wxWindow* parent, shared_ptr<Film> film, FilmViewer& viewer, shared_ptr<Content> content)
67 /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
68 the window above all others (and not just our own) it's better than nothing for now.
70 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
72 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
77 , _channels (film->audio_channels ())
80 wxFont subheading_font (*wxNORMAL_FONT);
81 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
83 auto overall_sizer = new wxBoxSizer (wxVERTICAL);
84 auto lr_sizer = new wxBoxSizer (wxHORIZONTAL);
86 auto left = new wxBoxSizer (wxVERTICAL);
88 _cursor = new StaticText (this, wxT("Cursor: none"));
89 left->Add (_cursor, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
90 _plot = new AudioPlot (this, viewer);
91 left->Add (_plot, 1, wxTOP | wxEXPAND, 12);
92 _sample_peak = new StaticText (this, wxT (""));
93 left->Add (_sample_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
94 _true_peak = new StaticText (this, wxT (""));
95 left->Add (_true_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
96 _integrated_loudness = new StaticText (this, wxT (""));
97 left->Add (_integrated_loudness, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
98 _loudness_range = new StaticText (this, wxT (""));
99 left->Add (_loudness_range, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
100 _leqm = new StaticText (this, wxT(""));
101 left->Add (_leqm, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
103 lr_sizer->Add (left, 1, wxALL | wxEXPAND, 12);
105 auto right = new wxBoxSizer (wxVERTICAL);
108 auto m = new StaticText (this, _("Channels"));
109 m->SetFont (subheading_font);
110 right->Add (m, 1, wxTOP | wxBOTTOM, 16);
113 for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
114 _channel_checkbox[i] = new CheckBox (this, std_to_wx(audio_channel_name(i)));
115 _channel_checkbox[i]->SetForegroundColour(wxColour(_plot->colour(i)));
116 right->Add (_channel_checkbox[i], 0, wxEXPAND | wxALL, 3);
117 _channel_checkbox[i]->bind(&AudioDialog::channel_clicked, this, _1);
120 show_or_hide_channel_checkboxes ();
123 auto m = new StaticText (this, _("Type"));
124 m->SetFont (subheading_font);
125 right->Add (m, 1, wxTOP, 16);
128 wxString const types[] = {
133 for (int i = 0; i < AudioPoint::COUNT; ++i) {
134 _type_checkbox[i] = new CheckBox (this, types[i]);
135 right->Add (_type_checkbox[i], 0, wxEXPAND | wxALL, 3);
136 _type_checkbox[i]->bind(&AudioDialog::type_clicked, this, _1);
140 auto m = new StaticText (this, _("Smoothing"));
141 m->SetFont (subheading_font);
142 right->Add (m, 1, wxTOP, 16);
145 _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
146 _smoothing->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&AudioDialog::smoothing_changed, this));
147 right->Add (_smoothing, 0, wxEXPAND);
149 lr_sizer->Add (right, 0, wxALL, 12);
151 overall_sizer->Add (lr_sizer, 0, wxEXPAND);
153 #ifdef DCPOMATIC_LINUX
154 auto buttons = CreateSeparatedButtonSizer (wxCLOSE);
156 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
160 SetSizer (overall_sizer);
161 overall_sizer->Layout ();
162 overall_sizer->SetSizeHints (this);
164 _film_connection = film->Change.connect (boost::bind(&AudioDialog::film_change, this, _1, _2));
165 _film_content_connection = film->ContentChange.connect(boost::bind(&AudioDialog::content_change, this, _1, _3));
166 DCPOMATIC_ASSERT (film->directory());
168 SetTitle(wxString::Format(_("DCP-o-matic audio - %s"), std_to_wx(content->path(0).string())));
170 SetTitle(wxString::Format(_("DCP-o-matic audio - %s"), std_to_wx(film->directory().get().string())));
174 _playlist = make_shared<Playlist>();
175 const_pointer_cast<Playlist>(_playlist)->add(film, content);
177 _playlist = film->playlist ();
180 _plot->Cursor.connect (bind (&AudioDialog::set_cursor, this, _1, _2));
185 AudioDialog::show_or_hide_channel_checkboxes ()
187 for (int i = 0; i < _channels; ++i) {
188 _channel_checkbox[i]->Show ();
191 for (int i = _channels; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
192 _channel_checkbox[i]->Hide ();
198 AudioDialog::try_to_load_analysis ()
204 auto film = _film.lock ();
205 DCPOMATIC_ASSERT (film);
207 auto check = _content.lock();
209 auto const path = film->audio_analysis_path (_playlist);
210 if (!boost::filesystem::exists (path)) {
211 _plot->set_analysis (shared_ptr<AudioAnalysis> ());
214 for (auto i: JobManager::instance()->get()) {
215 if (dynamic_pointer_cast<AnalyseAudioJob>(i)) {
220 JobManager::instance()->analyse_audio (
221 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
227 _analysis.reset (new AudioAnalysis (path));
228 } catch (OldFormatError& e) {
229 /* An old analysis file: recreate it */
230 JobManager::instance()->analyse_audio (
231 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
234 } catch (xmlpp::exception& e) {
235 /* Probably a (very) old-style analysis file: recreate it */
236 JobManager::instance()->analyse_audio (
237 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
242 _plot->set_analysis (_analysis);
243 _plot->set_gain_correction (_analysis->gain_correction (_playlist));
245 show_or_hide_channel_checkboxes ();
247 /* Set up some defaults if no check boxes are checked */
250 while (i < _channels && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
254 if (i == _channels) {
255 /* Nothing checked; check mapped ones */
258 auto content = _content.lock ();
261 mapped = content->audio->mapping().mapped_output_channels ();
263 mapped = film->mapped_audio_channels ();
266 for (auto i: mapped) {
267 if (_channel_checkbox[i]) {
268 _channel_checkbox[i]->SetValue (true);
269 _plot->set_channel_visible (i, true);
275 while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
279 if (i == AudioPoint::COUNT) {
280 for (int i = 0; i < AudioPoint::COUNT; ++i) {
281 _type_checkbox[i]->SetValue (true);
282 _plot->set_type_visible (i, true);
290 AudioDialog::analysis_finished ()
292 auto film = _film.lock ();
294 /* This should not happen, but if it does we should just give up quietly */
298 if (!boost::filesystem::exists (film->audio_analysis_path (_playlist))) {
299 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
302 _plot->set_message (_("Could not analyse audio."));
306 try_to_load_analysis ();
310 AudioDialog::channel_clicked (wxCommandEvent& ev)
313 while (c < _channels && ev.GetEventObject() != _channel_checkbox[c]) {
317 DCPOMATIC_ASSERT (c < _channels);
319 _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
323 AudioDialog::film_change (ChangeType type, Film::Property p)
325 if (type != ChangeType::DONE) {
329 if (p == Film::Property::AUDIO_CHANNELS) {
330 auto film = _film.lock ();
332 _channels = film->audio_channels ();
333 try_to_load_analysis ();
339 AudioDialog::content_change (ChangeType type, int p)
341 if (type != ChangeType::DONE) {
345 if (p == AudioContentProperty::STREAMS) {
346 try_to_load_analysis ();
347 } else if (p == AudioContentProperty::GAIN) {
348 if (_playlist->content().size() == 1 && _analysis) {
349 /* We can use a short-cut to render the effect of this
350 change, rather than recalculating everything.
352 _plot->set_gain_correction (_analysis->gain_correction (_playlist));
355 try_to_load_analysis ();
361 AudioDialog::type_clicked (wxCommandEvent& ev)
364 while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
368 DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
370 _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
374 AudioDialog::smoothing_changed ()
376 _plot->set_smoothing (_smoothing->GetValue ());
380 AudioDialog::setup_statistics ()
386 auto film = _film.lock ();
391 auto const peak = _analysis->overall_sample_peak ();
392 float const peak_dB = linear_to_db(peak.first.peak) + _analysis->gain_correction(_playlist);
393 _sample_peak->SetLabel (
395 _("Sample peak is %.2fdB at %s on %s"),
397 time_to_timecode (peak.first.time, film->video_frame_rate ()).data (),
398 std_to_wx (short_audio_channel_name (peak.second)).data ()
402 wxColour const peaking = *wxRED;
403 wxColour const not_peaking = gui_is_dark() ? *wxWHITE : *wxBLACK;
406 _sample_peak->SetForegroundColour(peaking);
408 _sample_peak->SetForegroundColour(not_peaking);
411 if (_analysis->overall_true_peak()) {
412 float const peak = _analysis->overall_true_peak().get();
413 float const peak_dB = linear_to_db(peak) + _analysis->gain_correction(_playlist);
415 _true_peak->SetLabel (wxString::Format (_("True peak is %.2fdB"), peak_dB));
418 _true_peak->SetForegroundColour(peaking);
420 _true_peak->SetForegroundColour(not_peaking);
424 /* XXX: check whether it's ok to add dB gain to these quantities */
426 if (static_cast<bool>(_analysis->integrated_loudness())) {
427 _integrated_loudness->SetLabel (
429 _("Integrated loudness %.2f LUFS"),
430 _analysis->integrated_loudness().get() + _analysis->gain_correction (_playlist)
435 if (static_cast<bool>(_analysis->loudness_range())) {
436 _loudness_range->SetLabel (
438 _("Loudness range %.2f LU"),
439 _analysis->loudness_range().get() + _analysis->gain_correction (_playlist)
444 if (static_cast<bool>(_analysis->leqm())) {
447 _("LEQ(m) %.2fdB"), _analysis->leqm().get() + _analysis->gain_correction(_playlist)
454 AudioDialog::Show (bool show)
456 bool const r = wxDialog::Show (show);
457 try_to_load_analysis ();
462 AudioDialog::set_cursor (optional<DCPTime> time, optional<float> db)
465 _cursor->SetLabel (_("Cursor: none"));
469 auto film = _film.lock();
470 DCPOMATIC_ASSERT (film);
471 _cursor->SetLabel (wxString::Format (_("Cursor: %.1fdB at %s"), *db, time->timecode(film->video_frame_rate())));