2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "audio_dialog.h"
21 #include "audio_plot.h"
23 #include "lib/audio_analysis.h"
25 #include "lib/analyse_audio_job.h"
26 #include "lib/audio_content.h"
27 #include "lib/job_manager.h"
28 #include <libxml++/libxml++.h>
29 #include <boost/filesystem.hpp>
33 using boost::shared_ptr;
35 using boost::optional;
36 using boost::const_pointer_cast;
37 using boost::dynamic_pointer_cast;
39 /** @param content Content to analyse, or 0 to analyse all of the film's audio */
40 AudioDialog::AudioDialog (wxWindow* parent, shared_ptr<Film> film, shared_ptr<AudioContent> content)
41 : wxDialog (parent, wxID_ANY, _("Audio"), wxDefaultPosition, wxSize (640, 512), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE)
45 wxFont subheading_font (*wxNORMAL_FONT);
46 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
48 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
49 wxBoxSizer* lr_sizer = new wxBoxSizer (wxHORIZONTAL);
51 wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
53 _plot = new AudioPlot (this);
54 left->Add (_plot, 1, wxALL | wxEXPAND, 12);
55 _peak_time = new wxStaticText (this, wxID_ANY, wxT (""));
56 left->Add (_peak_time, 0, wxALL, 12);
58 lr_sizer->Add (left, 1, wxALL, 12);
60 wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
63 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Channels"));
64 m->SetFont (subheading_font);
65 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM, 16);
68 for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
69 _channel_checkbox[i] = new wxCheckBox (this, wxID_ANY, std_to_wx (audio_channel_name (i)));
70 right->Add (_channel_checkbox[i], 0, wxEXPAND | wxALL, 3);
71 _channel_checkbox[i]->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioDialog::channel_clicked, this, _1));
75 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Type"));
76 m->SetFont (subheading_font);
77 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
80 wxString const types[] = {
85 for (int i = 0; i < AudioPoint::COUNT; ++i) {
86 _type_checkbox[i] = new wxCheckBox (this, wxID_ANY, types[i]);
87 right->Add (_type_checkbox[i], 0, wxEXPAND | wxALL, 3);
88 _type_checkbox[i]->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioDialog::type_clicked, this, _1));
92 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Smoothing"));
93 m->SetFont (subheading_font);
94 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
97 _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
98 _smoothing->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&AudioDialog::smoothing_changed, this));
99 right->Add (_smoothing, 0, wxEXPAND);
101 lr_sizer->Add (right, 0, wxALL, 12);
103 overall_sizer->Add (lr_sizer);
105 #ifdef DCPOMATIC_LINUX
106 wxSizer* buttons = CreateSeparatedButtonSizer (wxCLOSE);
108 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
112 SetSizer (overall_sizer);
113 overall_sizer->Layout ();
114 overall_sizer->SetSizeHints (this);
116 _film_connection = film->ContentChanged.connect (boost::bind (&AudioDialog::content_changed, this, _2));
117 SetTitle (_("DCP-o-matic audio"));
120 _playlist.reset (new Playlist ());
121 const_pointer_cast<Playlist> (_playlist)->add (content);
123 _playlist = film->playlist ();
128 AudioDialog::try_to_load_analysis ()
134 shared_ptr<const Film> film = _film.lock ();
135 DCPOMATIC_ASSERT (film);
137 boost::filesystem::path const path = film->audio_analysis_path (_playlist);
138 if (!boost::filesystem::exists (path)) {
139 _plot->set_analysis (shared_ptr<AudioAnalysis> ());
141 JobManager::instance()->analyse_audio (film, _playlist, _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this));
146 _analysis.reset (new AudioAnalysis (path));
147 } catch (xmlpp::exception& e) {
148 /* Probably an old-style analysis file: recreate it */
149 JobManager::instance()->analyse_audio (film, _playlist, _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this));
153 _plot->set_analysis (_analysis);
154 _plot->set_gain_correction (_analysis->gain_correction (_playlist));
157 /* Set up some defaults if no check boxes are checked */
160 while (i < MAX_DCP_AUDIO_CHANNELS && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
164 if (i == MAX_DCP_AUDIO_CHANNELS && _channel_checkbox[0]) {
165 _channel_checkbox[0]->SetValue (true);
166 _plot->set_channel_visible (0, true);
170 while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
174 if (i == AudioPoint::COUNT) {
175 for (int i = 0; i < AudioPoint::COUNT; ++i) {
176 _type_checkbox[i]->SetValue (true);
177 _plot->set_type_visible (i, true);
185 AudioDialog::analysis_finished ()
187 shared_ptr<const Film> film = _film.lock ();
188 DCPOMATIC_ASSERT (film);
190 if (!boost::filesystem::exists (film->audio_analysis_path (_playlist))) {
191 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
194 _plot->set_message (_("Could not analyse audio."));
198 try_to_load_analysis ();
202 AudioDialog::channel_clicked (wxCommandEvent& ev)
205 while (c < MAX_DCP_AUDIO_CHANNELS && ev.GetEventObject() != _channel_checkbox[c]) {
209 DCPOMATIC_ASSERT (c < MAX_DCP_AUDIO_CHANNELS);
211 _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
215 AudioDialog::content_changed (int p)
217 if (p == AudioContentProperty::AUDIO_STREAMS) {
218 try_to_load_analysis ();
219 } else if (p == AudioContentProperty::AUDIO_GAIN) {
220 if (_playlist->content().size() == 1) {
221 /* We can use a short-cut to render the effect of this
222 change, rather than recalculating everything.
224 _plot->set_gain_correction (_analysis->gain_correction (_playlist));
227 try_to_load_analysis ();
233 AudioDialog::type_clicked (wxCommandEvent& ev)
236 while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
240 DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
242 _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
246 AudioDialog::smoothing_changed ()
248 _plot->set_smoothing (_smoothing->GetValue ());
252 AudioDialog::setup_peak_time ()
254 if (!_analysis || !_analysis->peak ()) {
258 shared_ptr<Film> film = _film.lock ();
263 float const peak_dB = 20 * log10 (_analysis->peak().get()) + _analysis->gain_correction (_playlist);
265 _peak_time->SetLabel (
267 _("Peak is %.2fdB at %s"),
269 time_to_timecode (_analysis->peak_time().get(), film->video_frame_rate ()).data ()
274 _peak_time->SetForegroundColour (wxColour (255, 0, 0));
276 _peak_time->SetForegroundColour (wxColour (0, 0, 0));
281 AudioDialog::Show (bool show)
283 bool const r = wxDialog::Show (show);
284 try_to_load_analysis ();