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, weak_ptr<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
78 , _channels (film->audio_channels ())
81 wxFont subheading_font (*wxNORMAL_FONT);
82 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
84 auto overall_sizer = new wxBoxSizer (wxVERTICAL);
85 auto lr_sizer = new wxBoxSizer (wxHORIZONTAL);
87 auto left = new wxBoxSizer (wxVERTICAL);
89 _cursor = new StaticText (this, wxT("Cursor: none"));
90 left->Add (_cursor, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
91 _plot = new AudioPlot (this, viewer);
92 left->Add (_plot, 1, wxTOP | wxEXPAND, 12);
93 _sample_peak = new StaticText (this, wxT (""));
94 left->Add (_sample_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
95 _true_peak = new StaticText (this, wxT (""));
96 left->Add (_true_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
97 _integrated_loudness = new StaticText (this, wxT (""));
98 left->Add (_integrated_loudness, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
99 _loudness_range = new StaticText (this, wxT (""));
100 left->Add (_loudness_range, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
101 _leqm = new StaticText (this, wxT(""));
102 left->Add (_leqm, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
104 lr_sizer->Add (left, 1, wxALL | wxEXPAND, 12);
106 auto right = new wxBoxSizer (wxVERTICAL);
109 auto m = new StaticText (this, _("Channels"));
110 m->SetFont (subheading_font);
111 right->Add (m, 1, wxTOP | wxBOTTOM, 16);
114 for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
115 _channel_checkbox[i] = new CheckBox (this, std_to_wx(audio_channel_name(i)));
116 _channel_checkbox[i]->SetForegroundColour(wxColour(_plot->colour(i)));
117 right->Add (_channel_checkbox[i], 0, wxEXPAND | wxALL, 3);
118 _channel_checkbox[i]->Bind (wxEVT_CHECKBOX, boost::bind (&AudioDialog::channel_clicked, this, _1));
121 show_or_hide_channel_checkboxes ();
124 auto m = new StaticText (this, _("Type"));
125 m->SetFont (subheading_font);
126 right->Add (m, 1, wxTOP, 16);
129 wxString const types[] = {
134 for (int i = 0; i < AudioPoint::COUNT; ++i) {
135 _type_checkbox[i] = new CheckBox (this, types[i]);
136 right->Add (_type_checkbox[i], 0, wxEXPAND | wxALL, 3);
137 _type_checkbox[i]->Bind (wxEVT_CHECKBOX, boost::bind (&AudioDialog::type_clicked, this, _1));
141 auto m = new StaticText (this, _("Smoothing"));
142 m->SetFont (subheading_font);
143 right->Add (m, 1, wxTOP, 16);
146 _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
147 _smoothing->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&AudioDialog::smoothing_changed, this));
148 right->Add (_smoothing, 0, wxEXPAND);
150 lr_sizer->Add (right, 0, wxALL, 12);
152 overall_sizer->Add (lr_sizer, 0, wxEXPAND);
154 #ifdef DCPOMATIC_LINUX
155 auto buttons = CreateSeparatedButtonSizer (wxCLOSE);
157 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
161 SetSizer (overall_sizer);
162 overall_sizer->Layout ();
163 overall_sizer->SetSizeHints (this);
165 _film_connection = film->Change.connect (boost::bind(&AudioDialog::film_change, this, _1, _2));
166 _film_content_connection = film->ContentChange.connect(boost::bind(&AudioDialog::content_change, this, _1, _3));
167 DCPOMATIC_ASSERT (film->directory());
169 SetTitle(wxString::Format(_("DCP-o-matic audio - %s"), std_to_wx(content->path(0).string())));
171 SetTitle(wxString::Format(_("DCP-o-matic audio - %s"), std_to_wx(film->directory().get().string())));
175 _playlist = make_shared<Playlist>();
176 const_pointer_cast<Playlist>(_playlist)->add(film, content);
178 _playlist = film->playlist ();
181 _plot->Cursor.connect (bind (&AudioDialog::set_cursor, this, _1, _2));
186 AudioDialog::show_or_hide_channel_checkboxes ()
188 for (int i = 0; i < _channels; ++i) {
189 _channel_checkbox[i]->Show ();
192 for (int i = _channels; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
193 _channel_checkbox[i]->Hide ();
199 AudioDialog::try_to_load_analysis ()
205 auto film = _film.lock ();
206 DCPOMATIC_ASSERT (film);
208 auto check = _content.lock();
210 auto const path = film->audio_analysis_path (_playlist);
211 if (!boost::filesystem::exists (path)) {
212 _plot->set_analysis (shared_ptr<AudioAnalysis> ());
215 for (auto i: JobManager::instance()->get()) {
216 if (dynamic_pointer_cast<AnalyseAudioJob>(i)) {
221 JobManager::instance()->analyse_audio (
222 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
228 _analysis.reset (new AudioAnalysis (path));
229 } catch (OldFormatError& e) {
230 /* An old analysis file: recreate it */
231 JobManager::instance()->analyse_audio (
232 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
235 } catch (xmlpp::exception& e) {
236 /* Probably a (very) old-style analysis file: recreate it */
237 JobManager::instance()->analyse_audio (
238 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
243 _plot->set_analysis (_analysis);
244 _plot->set_gain_correction (_analysis->gain_correction (_playlist));
246 show_or_hide_channel_checkboxes ();
248 /* Set up some defaults if no check boxes are checked */
251 while (i < _channels && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
255 if (i == _channels) {
256 /* Nothing checked; check mapped ones */
259 auto content = _content.lock ();
262 mapped = content->audio->mapping().mapped_output_channels ();
264 mapped = film->mapped_audio_channels ();
267 for (auto i: mapped) {
268 if (_channel_checkbox[i]) {
269 _channel_checkbox[i]->SetValue (true);
270 _plot->set_channel_visible (i, true);
276 while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
280 if (i == AudioPoint::COUNT) {
281 for (int i = 0; i < AudioPoint::COUNT; ++i) {
282 _type_checkbox[i]->SetValue (true);
283 _plot->set_type_visible (i, true);
291 AudioDialog::analysis_finished ()
293 auto film = _film.lock ();
295 /* This should not happen, but if it does we should just give up quietly */
299 if (!boost::filesystem::exists (film->audio_analysis_path (_playlist))) {
300 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
303 _plot->set_message (_("Could not analyse audio."));
307 try_to_load_analysis ();
311 AudioDialog::channel_clicked (wxCommandEvent& ev)
314 while (c < _channels && ev.GetEventObject() != _channel_checkbox[c]) {
318 DCPOMATIC_ASSERT (c < _channels);
320 _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
324 AudioDialog::film_change (ChangeType type, Film::Property p)
326 if (type != ChangeType::DONE) {
330 if (p == Film::Property::AUDIO_CHANNELS) {
331 auto film = _film.lock ();
333 _channels = film->audio_channels ();
334 try_to_load_analysis ();
340 AudioDialog::content_change (ChangeType type, int p)
342 if (type != ChangeType::DONE) {
346 if (p == AudioContentProperty::STREAMS) {
347 try_to_load_analysis ();
348 } else if (p == AudioContentProperty::GAIN) {
349 if (_playlist->content().size() == 1 && _analysis) {
350 /* We can use a short-cut to render the effect of this
351 change, rather than recalculating everything.
353 _plot->set_gain_correction (_analysis->gain_correction (_playlist));
356 try_to_load_analysis ();
362 AudioDialog::type_clicked (wxCommandEvent& ev)
365 while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
369 DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
371 _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
375 AudioDialog::smoothing_changed ()
377 _plot->set_smoothing (_smoothing->GetValue ());
381 AudioDialog::setup_statistics ()
387 auto film = _film.lock ();
392 auto const peak = _analysis->overall_sample_peak ();
393 float const peak_dB = linear_to_db(peak.first.peak) + _analysis->gain_correction(_playlist);
394 _sample_peak->SetLabel (
396 _("Sample peak is %.2fdB at %s on %s"),
398 time_to_timecode (peak.first.time, film->video_frame_rate ()).data (),
399 std_to_wx (short_audio_channel_name (peak.second)).data ()
404 _sample_peak->SetForegroundColour (wxColour (255, 0, 0));
406 _sample_peak->SetForegroundColour (wxColour (0, 0, 0));
409 if (_analysis->overall_true_peak()) {
410 float const peak = _analysis->overall_true_peak().get();
411 float const peak_dB = linear_to_db(peak) + _analysis->gain_correction(_playlist);
413 _true_peak->SetLabel (wxString::Format (_("True peak is %.2fdB"), peak_dB));
416 _true_peak->SetForegroundColour (wxColour (255, 0, 0));
418 _true_peak->SetForegroundColour (wxColour (0, 0, 0));
422 /* XXX: check whether it's ok to add dB gain to these quantities */
424 if (static_cast<bool>(_analysis->integrated_loudness())) {
425 _integrated_loudness->SetLabel (
427 _("Integrated loudness %.2f LUFS"),
428 _analysis->integrated_loudness().get() + _analysis->gain_correction (_playlist)
433 if (static_cast<bool>(_analysis->loudness_range())) {
434 _loudness_range->SetLabel (
436 _("Loudness range %.2f LU"),
437 _analysis->loudness_range().get() + _analysis->gain_correction (_playlist)
442 if (static_cast<bool>(_analysis->leqm())) {
445 _("LEQ(m) %.2fdB"), _analysis->leqm().get() + _analysis->gain_correction(_playlist)
452 AudioDialog::Show (bool show)
454 bool const r = wxDialog::Show (show);
455 try_to_load_analysis ();
460 AudioDialog::set_cursor (optional<DCPTime> time, optional<float> db)
463 _cursor->SetLabel (_("Cursor: none"));
467 auto film = _film.lock();
468 DCPOMATIC_ASSERT (film);
469 _cursor->SetLabel (wxString::Format (_("Cursor: %.1fdB at %s"), *db, time->timecode(film->video_frame_rate())));