0dd1bec30fed2c10ef01cc7782e331ea0d1a1f47 from master; fix sometimes-missing audio...
[dcpomatic.git] / src / wx / audio_dialog.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <boost/filesystem.hpp>
21 #include "lib/audio_analysis.h"
22 #include "lib/film.h"
23 #include "lib/audio_content.h"
24 #include "audio_dialog.h"
25 #include "audio_plot.h"
26 #include "wx_util.h"
27
28 using boost::shared_ptr;
29 using boost::bind;
30 using boost::optional;
31
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)
34         , _film (film)
35         , _plot (0)
36 {
37         wxFont subheading_font (*wxNORMAL_FONT);
38         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
39
40         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
41         
42         wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
43
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);
48
49         sizer->Add (left, 1, wxALL, 12);
50
51         wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
52
53         {
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);
57         }
58
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));
63         }
64
65         {
66                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Type"));
67                 m->SetFont (subheading_font);
68                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
69         }
70         
71         wxString const types[] = {
72                 _("Peak"),
73                 _("RMS")
74         };
75
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));
80         }
81
82         {
83                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Smoothing"));
84                 m->SetFont (subheading_font);
85                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
86         }
87         
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);
91
92         sizer->Add (right, 0, wxALL, 12);
93
94         SetSizer (sizer);
95         sizer->Layout ();
96         sizer->SetSizeHints (this);
97 }
98
99 void
100 AudioDialog::set_content (shared_ptr<AudioContent> c)
101 {
102         _content_changed_connection.disconnect ();
103
104         _content = c;
105
106         try_to_load_analysis ();
107         _plot->set_gain (_content->audio_gain ());
108
109         _content_changed_connection = _content->Changed.connect (bind (&AudioDialog::content_changed, this, _2));
110
111         SetTitle (wxString::Format (_("DCP-o-matic audio - %s"), std_to_wx(_content->path_summary()).data()));
112 }
113
114 void
115 AudioDialog::try_to_load_analysis ()
116 {
117         if (!IsShown ()) {
118                 return;
119         }
120
121         if (!boost::filesystem::exists (_content->audio_analysis_path())) {
122                 _plot->set_analysis (shared_ptr<AudioAnalysis> ());
123                 _analysis.reset ();
124                 _analysis_finished_connection = _content->analyse_audio (bind (&AudioDialog::analysis_finished, this));
125                 return;
126         }
127         
128         _analysis.reset (new AudioAnalysis (_content->audio_analysis_path ()));
129         _plot->set_analysis (_analysis);
130         setup_peak_time ();
131
132         /* Set up some defaults if no check boxes are checked */
133         
134         int i = 0;
135         while (i < MAX_DCP_AUDIO_CHANNELS && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
136                 ++i;
137         }
138
139         if (i == MAX_DCP_AUDIO_CHANNELS && _channel_checkbox[0]) {
140                 _channel_checkbox[0]->SetValue (true);
141                 _plot->set_channel_visible (0, true);
142         }
143
144         i = 0;
145         while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
146                 i++;
147         }
148
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);
153                 }
154         }
155
156         Refresh ();
157 }
158
159 void
160 AudioDialog::analysis_finished ()
161 {
162         if (!boost::filesystem::exists (_content->audio_analysis_path())) {
163                 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
164                    Give up.
165                 */
166                 _plot->set_message (_("Could not analyse audio."));
167                 return;
168         }
169
170         try_to_load_analysis ();
171 }
172
173 void
174 AudioDialog::channel_clicked (wxCommandEvent& ev)
175 {
176         int c = 0;
177         while (c < MAX_DCP_AUDIO_CHANNELS && ev.GetEventObject() != _channel_checkbox[c]) {
178                 ++c;
179         }
180
181         DCPOMATIC_ASSERT (c < MAX_DCP_AUDIO_CHANNELS);
182
183         _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
184 }
185
186 void
187 AudioDialog::content_changed (int p)
188 {
189         if (p == AudioContentProperty::AUDIO_GAIN) {
190                 _plot->set_gain (_content->audio_gain ());
191                 setup_peak_time ();
192         } else if (p == AudioContentProperty::AUDIO_MAPPING) {
193                 try_to_load_analysis ();
194         }
195 }
196
197 void
198 AudioDialog::type_clicked (wxCommandEvent& ev)
199 {
200         int t = 0;
201         while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
202                 ++t;
203         }
204
205         DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
206
207         _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
208 }
209
210 void
211 AudioDialog::smoothing_changed ()
212 {
213         _plot->set_smoothing (_smoothing->GetValue ());
214 }
215
216 void
217 AudioDialog::setup_peak_time ()
218 {
219         if (!_analysis || !_analysis->peak ()) {
220                 return;
221         }
222         
223         shared_ptr<Film> film = _film.lock ();
224         if (!film) {
225                 return;
226         }
227         
228         float peak_dB = 20 * log10 (_analysis->peak().get()) + _content->audio_gain();
229         
230         _peak_time->SetLabel (
231                 wxString::Format (
232                         _("Peak is %.2fdB at %s"),
233                         peak_dB,
234                         time_to_timecode (_analysis->peak_time().get(), film->video_frame_rate ()).data ()
235                         )
236                 );
237         
238         if (peak_dB > -3) {
239                 _peak_time->SetForegroundColour (wxColour (255, 0, 0));
240         } else {
241                 _peak_time->SetForegroundColour (wxColour (0, 0, 0));
242         }
243 }