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 <boost/filesystem.hpp>
21 #include "lib/audio_analysis.h"
23 #include "lib/audio_content.h"
24 #include "audio_dialog.h"
25 #include "audio_plot.h"
28 using boost::shared_ptr;
30 using boost::optional;
32 AudioDialog::AudioDialog (wxWindow* parent, shared_ptr<Film> film)
33 : wxDialog (parent, wxID_ANY, _("Audio"), wxDefaultPosition, wxSize (640, 512), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE)
37 wxFont subheading_font (*wxNORMAL_FONT);
38 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
40 wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
42 wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
44 _plot = new AudioPlot (this);
45 left->Add (_plot, 1, wxALL | wxEXPAND, 12);
46 _peak_time = new wxStaticText (this, wxID_ANY, wxT (""));
47 left->Add (_peak_time, 0, wxALL, 12);
49 sizer->Add (left, 1, wxALL, 12);
51 wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
54 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Channels"));
55 m->SetFont (subheading_font);
56 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM, 16);
59 for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
60 _channel_checkbox[i] = new wxCheckBox (this, wxID_ANY, std_to_wx (audio_channel_name (i)));
61 right->Add (_channel_checkbox[i], 0, wxEXPAND | wxALL, 3);
62 _channel_checkbox[i]->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioDialog::channel_clicked, this, _1));
66 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Type"));
67 m->SetFont (subheading_font);
68 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
71 wxString const types[] = {
76 for (int i = 0; i < AudioPoint::COUNT; ++i) {
77 _type_checkbox[i] = new wxCheckBox (this, wxID_ANY, types[i]);
78 right->Add (_type_checkbox[i], 0, wxEXPAND | wxALL, 3);
79 _type_checkbox[i]->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioDialog::type_clicked, this, _1));
83 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Smoothing"));
84 m->SetFont (subheading_font);
85 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
88 _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
89 _smoothing->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&AudioDialog::smoothing_changed, this));
90 right->Add (_smoothing, 0, wxEXPAND);
92 sizer->Add (right, 0, wxALL, 12);
96 sizer->SetSizeHints (this);
100 AudioDialog::set_content (shared_ptr<AudioContent> c)
102 _content_changed_connection.disconnect ();
106 try_to_load_analysis ();
107 _plot->set_gain (_content->audio_gain ());
109 _content_changed_connection = _content->Changed.connect (bind (&AudioDialog::content_changed, this, _2));
111 SetTitle (wxString::Format (_("DCP-o-matic audio - %s"), std_to_wx(_content->path_summary()).data()));
115 AudioDialog::try_to_load_analysis ()
121 if (!boost::filesystem::exists (_content->audio_analysis_path())) {
122 _plot->set_analysis (shared_ptr<AudioAnalysis> ());
124 _analysis_finished_connection = _content->analyse_audio (bind (&AudioDialog::analysis_finished, this));
129 _analysis.reset (new AudioAnalysis (_content->audio_analysis_path ()));
130 } catch (xmlpp::exception& e) {
131 /* Probably an old-style analysis file: recreate it */
132 _analysis_finished_connection = _content->analyse_audio (bind (&AudioDialog::analysis_finished, this));
136 _plot->set_analysis (_analysis);
139 /* Set up some defaults if no check boxes are checked */
142 while (i < MAX_DCP_AUDIO_CHANNELS && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
146 if (i == MAX_DCP_AUDIO_CHANNELS && _channel_checkbox[0]) {
147 _channel_checkbox[0]->SetValue (true);
148 _plot->set_channel_visible (0, true);
152 while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
156 if (i == AudioPoint::COUNT) {
157 for (int i = 0; i < AudioPoint::COUNT; ++i) {
158 _type_checkbox[i]->SetValue (true);
159 _plot->set_type_visible (i, true);
167 AudioDialog::analysis_finished ()
169 if (!boost::filesystem::exists (_content->audio_analysis_path())) {
170 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
173 _plot->set_message (_("Could not analyse audio."));
177 try_to_load_analysis ();
181 AudioDialog::channel_clicked (wxCommandEvent& ev)
184 while (c < MAX_DCP_AUDIO_CHANNELS && ev.GetEventObject() != _channel_checkbox[c]) {
188 DCPOMATIC_ASSERT (c < MAX_DCP_AUDIO_CHANNELS);
190 _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
194 AudioDialog::content_changed (int p)
196 if (p == AudioContentProperty::AUDIO_GAIN) {
197 _plot->set_gain (_content->audio_gain ());
199 } else if (p == AudioContentProperty::AUDIO_MAPPING) {
200 try_to_load_analysis ();
205 AudioDialog::type_clicked (wxCommandEvent& ev)
208 while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
212 DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
214 _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
218 AudioDialog::smoothing_changed ()
220 _plot->set_smoothing (_smoothing->GetValue ());
224 AudioDialog::setup_peak_time ()
226 if (!_analysis || !_analysis->peak ()) {
230 shared_ptr<Film> film = _film.lock ();
235 float peak_dB = 20 * log10 (_analysis->peak().get()) + _content->audio_gain();
237 _peak_time->SetLabel (
239 _("Peak is %.2fdB at %s"),
241 time_to_timecode (_analysis->peak_time().get(), film->video_frame_rate ()).data ()
246 _peak_time->SetForegroundColour (wxColour (255, 0, 0));
248 _peak_time->SetForegroundColour (wxColour (0, 0, 0));