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;
45 using namespace dcpomatic;
47 /** @param parent Parent window.
48 * @param film Film we are using.
49 * @param content Content to analyse, or 0 to analyse all of the film's audio.
51 AudioDialog::AudioDialog (wxWindow* parent, shared_ptr<Film> film, shared_ptr<Content> content)
59 /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
60 the window above all others (and not just our own) it's better than nothing for now.
62 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
64 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
69 , _channels (film->audio_channels ())
72 wxFont subheading_font (*wxNORMAL_FONT);
73 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
75 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
76 wxBoxSizer* lr_sizer = new wxBoxSizer (wxHORIZONTAL);
78 wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
80 _cursor = new StaticText (this, wxT("Cursor: none"));
81 left->Add (_cursor, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
82 _plot = new AudioPlot (this);
83 left->Add (_plot, 1, wxTOP | wxEXPAND, 12);
84 _sample_peak = new StaticText (this, wxT (""));
85 left->Add (_sample_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
86 _true_peak = new StaticText (this, wxT (""));
87 left->Add (_true_peak, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
88 _integrated_loudness = new StaticText (this, wxT (""));
89 left->Add (_integrated_loudness, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
90 _loudness_range = new StaticText (this, wxT (""));
91 left->Add (_loudness_range, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
93 lr_sizer->Add (left, 1, wxALL | wxEXPAND, 12);
95 wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
98 wxStaticText* m = new StaticText (this, _("Channels"));
99 m->SetFont (subheading_font);
100 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM, 16);
103 for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
104 _channel_checkbox[i] = new CheckBox (this, std_to_wx(audio_channel_name(i)));
105 _channel_checkbox[i]->SetForegroundColour(wxColour(_plot->colour(i)));
106 right->Add (_channel_checkbox[i], 0, wxEXPAND | wxALL, 3);
107 _channel_checkbox[i]->Bind (wxEVT_CHECKBOX, boost::bind (&AudioDialog::channel_clicked, this, _1));
111 wxStaticText* m = new StaticText (this, _("Type"));
112 m->SetFont (subheading_font);
113 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
116 wxString const types[] = {
121 for (int i = 0; i < AudioPoint::COUNT; ++i) {
122 _type_checkbox[i] = new CheckBox (this, types[i]);
123 right->Add (_type_checkbox[i], 0, wxEXPAND | wxALL, 3);
124 _type_checkbox[i]->Bind (wxEVT_CHECKBOX, boost::bind (&AudioDialog::type_clicked, this, _1));
128 wxStaticText* m = new StaticText (this, _("Smoothing"));
129 m->SetFont (subheading_font);
130 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
133 _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
134 _smoothing->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&AudioDialog::smoothing_changed, this));
135 right->Add (_smoothing, 0, wxEXPAND);
137 lr_sizer->Add (right, 0, wxALL, 12);
139 overall_sizer->Add (lr_sizer, 0, wxEXPAND);
141 #ifdef DCPOMATIC_LINUX
142 wxSizer* buttons = CreateSeparatedButtonSizer (wxCLOSE);
144 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
148 SetSizer (overall_sizer);
149 overall_sizer->Layout ();
150 overall_sizer->SetSizeHints (this);
152 _film_connection = film->Change.connect (boost::bind(&AudioDialog::film_change, this, _1, _2));
153 _film_content_connection = film->ContentChange.connect (boost::bind (&AudioDialog::content_change, this, _1, _3));
154 DCPOMATIC_ASSERT (film->directory());
155 SetTitle(wxString::Format(_("DCP-o-matic audio - %s"), std_to_wx(film->directory().get().string())));
158 _playlist.reset (new Playlist ());
159 const_pointer_cast<Playlist>(_playlist)->add(film, content);
161 _playlist = film->playlist ();
164 _plot->Cursor.connect (bind (&AudioDialog::set_cursor, this, _1, _2));
168 AudioDialog::try_to_load_analysis ()
174 shared_ptr<const Film> film = _film.lock ();
175 DCPOMATIC_ASSERT (film);
177 shared_ptr<Content> check = _content.lock();
179 boost::filesystem::path const path = film->audio_analysis_path (_playlist);
180 if (!boost::filesystem::exists (path)) {
181 _plot->set_analysis (shared_ptr<AudioAnalysis> ());
184 BOOST_FOREACH (shared_ptr<Job> i, JobManager::instance()->get()) {
185 if (dynamic_pointer_cast<AnalyseAudioJob>(i)) {
190 JobManager::instance()->analyse_audio (
191 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
197 _analysis.reset (new AudioAnalysis (path));
198 } catch (OldFormatError& e) {
199 /* An old analysis file: recreate it */
200 JobManager::instance()->analyse_audio (
201 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
204 } catch (xmlpp::exception& e) {
205 /* Probably a (very) old-style analysis file: recreate it */
206 JobManager::instance()->analyse_audio (
207 film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
212 _plot->set_analysis (_analysis);
213 _plot->set_gain_correction (_analysis->gain_correction (_playlist));
216 for (int i = 0; i < _channels; ++i) {
217 _channel_checkbox[i]->Show ();
220 for (int i = _channels; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
221 _channel_checkbox[i]->Hide ();
224 /* Set up some defaults if no check boxes are checked */
227 while (i < _channels && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
231 if (i == _channels) {
232 /* Nothing checked; check mapped ones */
235 shared_ptr<Content> content = _content.lock ();
238 mapped = content->audio->mapping().mapped_output_channels ();
240 mapped = film->mapped_audio_channels ();
243 BOOST_FOREACH (int i, mapped) {
244 if (_channel_checkbox[i]) {
245 _channel_checkbox[i]->SetValue (true);
246 _plot->set_channel_visible (i, true);
252 while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
256 if (i == AudioPoint::COUNT) {
257 for (int i = 0; i < AudioPoint::COUNT; ++i) {
258 _type_checkbox[i]->SetValue (true);
259 _plot->set_type_visible (i, true);
267 AudioDialog::analysis_finished ()
269 shared_ptr<const Film> film = _film.lock ();
271 /* This should not happen, but if it does we should just give up quietly */
275 if (!boost::filesystem::exists (film->audio_analysis_path (_playlist))) {
276 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
279 _plot->set_message (_("Could not analyse audio."));
283 try_to_load_analysis ();
287 AudioDialog::channel_clicked (wxCommandEvent& ev)
290 while (c < _channels && ev.GetEventObject() != _channel_checkbox[c]) {
294 DCPOMATIC_ASSERT (c < _channels);
296 _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
300 AudioDialog::film_change (ChangeType type, int p)
302 if (type != CHANGE_TYPE_DONE) {
306 if (p == Film::AUDIO_CHANNELS) {
307 shared_ptr<Film> film = _film.lock ();
309 _channels = film->audio_channels ();
310 try_to_load_analysis ();
316 AudioDialog::content_change (ChangeType type, int p)
318 if (type != CHANGE_TYPE_DONE) {
322 if (p == AudioContentProperty::STREAMS) {
323 try_to_load_analysis ();
324 } else if (p == AudioContentProperty::GAIN) {
325 if (_playlist->content().size() == 1 && _analysis) {
326 /* We can use a short-cut to render the effect of this
327 change, rather than recalculating everything.
329 _plot->set_gain_correction (_analysis->gain_correction (_playlist));
332 try_to_load_analysis ();
338 AudioDialog::type_clicked (wxCommandEvent& ev)
341 while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
345 DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
347 _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
351 AudioDialog::smoothing_changed ()
353 _plot->set_smoothing (_smoothing->GetValue ());
357 AudioDialog::setup_statistics ()
363 shared_ptr<Film> film = _film.lock ();
368 pair<AudioAnalysis::PeakTime, int> const peak = _analysis->overall_sample_peak ();
369 float const peak_dB = 20 * log10 (peak.first.peak) + _analysis->gain_correction (_playlist);
370 _sample_peak->SetLabel (
372 _("Sample peak is %.2fdB at %s on %s"),
374 time_to_timecode (peak.first.time, film->video_frame_rate ()).data (),
375 std_to_wx (short_audio_channel_name (peak.second)).data ()
380 _sample_peak->SetForegroundColour (wxColour (255, 0, 0));
382 _sample_peak->SetForegroundColour (wxColour (0, 0, 0));
385 if (_analysis->overall_true_peak()) {
386 float const peak = _analysis->overall_true_peak().get();
387 float const peak_dB = 20 * log10 (peak) + _analysis->gain_correction (_playlist);
389 _true_peak->SetLabel (wxString::Format (_("True peak is %.2fdB"), peak_dB));
392 _true_peak->SetForegroundColour (wxColour (255, 0, 0));
394 _true_peak->SetForegroundColour (wxColour (0, 0, 0));
398 /* XXX: check whether it's ok to add dB gain to these quantities */
400 if (static_cast<bool>(_analysis->integrated_loudness ())) {
401 _integrated_loudness->SetLabel (
403 _("Integrated loudness %.2f LUFS"),
404 _analysis->integrated_loudness().get() + _analysis->gain_correction (_playlist)
409 if (static_cast<bool>(_analysis->loudness_range ())) {
410 _loudness_range->SetLabel (
412 _("Loudness range %.2f LU"),
413 _analysis->loudness_range().get() + _analysis->gain_correction (_playlist)
420 AudioDialog::Show (bool show)
422 bool const r = wxDialog::Show (show);
423 try_to_load_analysis ();
428 AudioDialog::set_cursor (optional<DCPTime> time, optional<float> db)
431 _cursor->SetLabel (_("Cursor: none"));
435 shared_ptr<Film> film = _film.lock();
436 DCPOMATIC_ASSERT (film);
437 _cursor->SetLabel (wxString::Format (_("Cursor: %.1fdB at %s"), *db, time->timecode(film->video_frame_rate())));