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/>.
21 #include "audio_dialog.h"
22 #include "audio_plot.h"
23 #include "check_box.h"
24 #include "static_text.h"
26 #include "lib/analyse_audio_job.h"
27 #include "lib/audio_analysis.h"
28 #include "lib/audio_content.h"
30 #include "lib/job_manager.h"
31 #include <libxml++/libxml++.h>
32 #include <boost/filesystem.hpp>
37 using std::make_shared;
39 using std::shared_ptr;
43 using boost::optional;
44 using boost::const_pointer_cast;
45 using std::dynamic_pointer_cast;
46 using namespace dcpomatic;
47 #if BOOST_VERSION >= 106100
48 using namespace boost::placeholders;
51 /** @param parent Parent window.
52 * @param film Film we are using.
53 * @param content Content to analyse, or 0 to analyse all of the film's audio.
55 AudioDialog::AudioDialog (wxWindow* parent, shared_ptr<Film> film, weak_ptr<FilmViewer> viewer, shared_ptr<Content> content)
63 /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
64 the window above all others (and not just our own) it's better than nothing for now.
66 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
68 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
74 , _channels (film->audio_channels ())
77 wxFont subheading_font (*wxNORMAL_FONT);
78 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
80 auto overall_sizer = new wxBoxSizer (wxVERTICAL);
81 auto lr_sizer = new wxBoxSizer (wxHORIZONTAL);
83 auto left = new wxBoxSizer (wxVERTICAL);
85 _cursor = new StaticText (this, wxT("Cursor: none"));
86 left->Add (_cursor, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
87 _plot = new AudioPlot (this, viewer);
88 left->Add (_plot, 1, wxTOP | wxEXPAND, 12);
89 _sample_peak = new StaticText (this, wxT (""));
90 left->Add (_sample_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
91 _true_peak = new StaticText (this, wxT (""));
92 left->Add (_true_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
93 _integrated_loudness = new StaticText (this, wxT (""));
94 left->Add (_integrated_loudness, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
95 _loudness_range = new StaticText (this, wxT (""));
96 left->Add (_loudness_range, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
97 _leqm = new StaticText (this, wxT(""));
98 left->Add (_leqm, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
100 lr_sizer->Add (left, 1, wxALL | wxEXPAND, 12);
102 auto right = new wxBoxSizer (wxVERTICAL);
105 auto m = new StaticText (this, _("Channels"));
106 m->SetFont (subheading_font);
107 right->Add (m, 1, wxTOP | wxBOTTOM, 16);
110 for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
111 _channel_checkbox[i] = new CheckBox (this, std_to_wx(audio_channel_name(i)));
112 _channel_checkbox[i]->SetForegroundColour(wxColour(_plot->colour(i)));
113 right->Add (_channel_checkbox[i], 0, wxEXPAND | wxALL, 3);
114 _channel_checkbox[i]->Bind (wxEVT_CHECKBOX, boost::bind (&AudioDialog::channel_clicked, this, _1));
117 show_or_hide_channel_checkboxes ();
120 auto m = new StaticText (this, _("Type"));
121 m->SetFont (subheading_font);
122 right->Add (m, 1, wxTOP, 16);
125 wxString const types[] = {
130 for (int i = 0; i < AudioPoint::COUNT; ++i) {
131 _type_checkbox[i] = new CheckBox (this, types[i]);
132 right->Add (_type_checkbox[i], 0, wxEXPAND | wxALL, 3);
133 _type_checkbox[i]->Bind (wxEVT_CHECKBOX, boost::bind (&AudioDialog::type_clicked, this, _1));
137 auto m = new StaticText (this, _("Smoothing"));
138 m->SetFont (subheading_font);
139 right->Add (m, 1, wxTOP, 16);
142 _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
143 _smoothing->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&AudioDialog::smoothing_changed, this));
144 right->Add (_smoothing, 0, wxEXPAND);
146 lr_sizer->Add (right, 0, wxALL, 12);
148 overall_sizer->Add (lr_sizer, 0, wxEXPAND);
150 #ifdef DCPOMATIC_LINUX
151 auto buttons = CreateSeparatedButtonSizer (wxCLOSE);
153 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
157 SetSizer (overall_sizer);
158 overall_sizer->Layout ();
159 overall_sizer->SetSizeHints (this);
161 _film_connection = film->Change.connect (boost::bind(&AudioDialog::film_change, this, _1, _2));
162 _film_content_connection = film->ContentChange.connect(boost::bind(&AudioDialog::content_change, this, _1, _3));
163 DCPOMATIC_ASSERT (film->directory());
165 SetTitle(wxString::Format(_("DCP-o-matic audio - %s"), std_to_wx(content->path(0).string())));
167 SetTitle(wxString::Format(_("DCP-o-matic audio - %s"), std_to_wx(film->directory().get().string())));
171 _playlist = make_shared<Playlist>();
172 const_pointer_cast<Playlist>(_playlist)->add(film, content);
174 _playlist = film->playlist ();
177 _plot->Cursor.connect (bind (&AudioDialog::set_cursor, this, _1, _2));
182 AudioDialog::show_or_hide_channel_checkboxes ()
184 for (int i = 0; i < _channels; ++i) {
185 _channel_checkbox[i]->Show ();
188 for (int i = _channels; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
189 _channel_checkbox[i]->Hide ();
195 AudioDialog::try_to_load_analysis ()
201 auto film = _film.lock ();
202 DCPOMATIC_ASSERT (film);
204 auto check = _content.lock();
206 auto const path = film->audio_analysis_path (_playlist);
207 if (!boost::filesystem::exists (path)) {
208 _plot->set_analysis (shared_ptr<AudioAnalysis> ());
211 for (auto i: JobManager::instance()->get()) {
212 if (dynamic_pointer_cast<AnalyseAudioJob>(i)) {
217 JobManager::instance()->analyse_audio (
218 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
224 _analysis.reset (new AudioAnalysis (path));
225 } catch (OldFormatError& e) {
226 /* An old analysis file: recreate it */
227 JobManager::instance()->analyse_audio (
228 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
231 } catch (xmlpp::exception& e) {
232 /* Probably a (very) old-style analysis file: recreate it */
233 JobManager::instance()->analyse_audio (
234 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
239 _plot->set_analysis (_analysis);
240 _plot->set_gain_correction (_analysis->gain_correction (_playlist));
242 show_or_hide_channel_checkboxes ();
244 /* Set up some defaults if no check boxes are checked */
247 while (i < _channels && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
251 if (i == _channels) {
252 /* Nothing checked; check mapped ones */
255 auto content = _content.lock ();
258 mapped = content->audio->mapping().mapped_output_channels ();
260 mapped = film->mapped_audio_channels ();
263 for (auto i: mapped) {
264 if (_channel_checkbox[i]) {
265 _channel_checkbox[i]->SetValue (true);
266 _plot->set_channel_visible (i, true);
272 while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
276 if (i == AudioPoint::COUNT) {
277 for (int i = 0; i < AudioPoint::COUNT; ++i) {
278 _type_checkbox[i]->SetValue (true);
279 _plot->set_type_visible (i, true);
287 AudioDialog::analysis_finished ()
289 auto film = _film.lock ();
291 /* This should not happen, but if it does we should just give up quietly */
295 if (!boost::filesystem::exists (film->audio_analysis_path (_playlist))) {
296 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
299 _plot->set_message (_("Could not analyse audio."));
303 try_to_load_analysis ();
307 AudioDialog::channel_clicked (wxCommandEvent& ev)
310 while (c < _channels && ev.GetEventObject() != _channel_checkbox[c]) {
314 DCPOMATIC_ASSERT (c < _channels);
316 _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
320 AudioDialog::film_change (ChangeType type, Film::Property p)
322 if (type != ChangeType::DONE) {
326 if (p == Film::Property::AUDIO_CHANNELS) {
327 auto film = _film.lock ();
329 _channels = film->audio_channels ();
330 try_to_load_analysis ();
336 AudioDialog::content_change (ChangeType type, int p)
338 if (type != ChangeType::DONE) {
342 if (p == AudioContentProperty::STREAMS) {
343 try_to_load_analysis ();
344 } else if (p == AudioContentProperty::GAIN) {
345 if (_playlist->content().size() == 1 && _analysis) {
346 /* We can use a short-cut to render the effect of this
347 change, rather than recalculating everything.
349 _plot->set_gain_correction (_analysis->gain_correction (_playlist));
352 try_to_load_analysis ();
358 AudioDialog::type_clicked (wxCommandEvent& ev)
361 while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
365 DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
367 _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
371 AudioDialog::smoothing_changed ()
373 _plot->set_smoothing (_smoothing->GetValue ());
377 AudioDialog::setup_statistics ()
383 auto film = _film.lock ();
388 auto const peak = _analysis->overall_sample_peak ();
389 float const peak_dB = linear_to_db(peak.first.peak) + _analysis->gain_correction(_playlist);
390 _sample_peak->SetLabel (
392 _("Sample peak is %.2fdB at %s on %s"),
394 time_to_timecode (peak.first.time, film->video_frame_rate ()).data (),
395 std_to_wx (short_audio_channel_name (peak.second)).data ()
400 _sample_peak->SetForegroundColour (wxColour (255, 0, 0));
402 _sample_peak->SetForegroundColour (wxColour (0, 0, 0));
405 if (_analysis->overall_true_peak()) {
406 float const peak = _analysis->overall_true_peak().get();
407 float const peak_dB = linear_to_db(peak) + _analysis->gain_correction(_playlist);
409 _true_peak->SetLabel (wxString::Format (_("True peak is %.2fdB"), peak_dB));
412 _true_peak->SetForegroundColour (wxColour (255, 0, 0));
414 _true_peak->SetForegroundColour (wxColour (0, 0, 0));
418 /* XXX: check whether it's ok to add dB gain to these quantities */
420 if (static_cast<bool>(_analysis->integrated_loudness())) {
421 _integrated_loudness->SetLabel (
423 _("Integrated loudness %.2f LUFS"),
424 _analysis->integrated_loudness().get() + _analysis->gain_correction (_playlist)
429 if (static_cast<bool>(_analysis->loudness_range())) {
430 _loudness_range->SetLabel (
432 _("Loudness range %.2f LU"),
433 _analysis->loudness_range().get() + _analysis->gain_correction (_playlist)
438 if (static_cast<bool>(_analysis->leqm())) {
441 _("LEQ(m) %.2fdB"), _analysis->leqm().get() + _analysis->gain_correction(_playlist)
448 AudioDialog::Show (bool show)
450 bool const r = wxDialog::Show (show);
451 try_to_load_analysis ();
456 AudioDialog::set_cursor (optional<DCPTime> time, optional<float> db)
459 _cursor->SetLabel (_("Cursor: none"));
463 auto film = _film.lock();
464 DCPOMATIC_ASSERT (film);
465 _cursor->SetLabel (wxString::Format (_("Cursor: %.1fdB at %s"), *db, time->timecode(film->video_frame_rate())));