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));
128 _analysis.reset (new AudioAnalysis (_content->audio_analysis_path ()));
129 _plot->set_analysis (_analysis);
132 /* Set up some defaults if no check boxes are checked */
135 while (i < MAX_DCP_AUDIO_CHANNELS && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
139 if (i == MAX_DCP_AUDIO_CHANNELS && _channel_checkbox[0]) {
140 _channel_checkbox[0]->SetValue (true);
141 _plot->set_channel_visible (0, true);
145 while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
149 if (i == AudioPoint::COUNT) {
150 for (int i = 0; i < AudioPoint::COUNT; ++i) {
151 _type_checkbox[i]->SetValue (true);
152 _plot->set_type_visible (i, true);
158 AudioDialog::analysis_finished ()
160 if (!boost::filesystem::exists (_content->audio_analysis_path())) {
161 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
164 _plot->set_message (_("Could not analyse audio."));
168 try_to_load_analysis ();
172 AudioDialog::channel_clicked (wxCommandEvent& ev)
175 while (c < MAX_DCP_AUDIO_CHANNELS && ev.GetEventObject() != _channel_checkbox[c]) {
179 DCPOMATIC_ASSERT (c < MAX_DCP_AUDIO_CHANNELS);
181 _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
185 AudioDialog::content_changed (int p)
187 if (p == AudioContentProperty::AUDIO_GAIN) {
188 _plot->set_gain (_content->audio_gain ());
190 } else if (p == AudioContentProperty::AUDIO_MAPPING) {
191 try_to_load_analysis ();
196 AudioDialog::type_clicked (wxCommandEvent& ev)
199 while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
203 DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
205 _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
209 AudioDialog::smoothing_changed ()
211 _plot->set_smoothing (_smoothing->GetValue ());
215 AudioDialog::setup_peak_time ()
217 if (!_analysis || !_analysis->peak ()) {
221 shared_ptr<Film> film = _film.lock ();
226 float peak_dB = 20 * log10 (_analysis->peak().get()) + _content->audio_gain();
228 _peak_time->SetLabel (
230 _("Peak is %.2fdB at %s"),
232 time_to_timecode (_analysis->peak_time().get(), film->video_frame_rate ()).data ()
237 _peak_time->SetForegroundColour (wxColour (255, 0, 0));
239 _peak_time->SetForegroundColour (wxColour (0, 0, 0));