Merge branch '2.0' of git.carlh.net:git/dcpomatic into 2.0
[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         try {
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));
133                 return;
134         }
135         
136         _plot->set_analysis (_analysis);
137         setup_peak_time ();
138
139         /* Set up some defaults if no check boxes are checked */
140         
141         int i = 0;
142         while (i < MAX_DCP_AUDIO_CHANNELS && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
143                 ++i;
144         }
145
146         if (i == MAX_DCP_AUDIO_CHANNELS && _channel_checkbox[0]) {
147                 _channel_checkbox[0]->SetValue (true);
148                 _plot->set_channel_visible (0, true);
149         }
150
151         i = 0;
152         while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
153                 i++;
154         }
155
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);
160                 }
161         }
162
163         Refresh ();
164 }
165
166 void
167 AudioDialog::analysis_finished ()
168 {
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.
171                    Give up.
172                 */
173                 _plot->set_message (_("Could not analyse audio."));
174                 return;
175         }
176
177         try_to_load_analysis ();
178 }
179
180 void
181 AudioDialog::channel_clicked (wxCommandEvent& ev)
182 {
183         int c = 0;
184         while (c < MAX_DCP_AUDIO_CHANNELS && ev.GetEventObject() != _channel_checkbox[c]) {
185                 ++c;
186         }
187
188         DCPOMATIC_ASSERT (c < MAX_DCP_AUDIO_CHANNELS);
189
190         _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
191 }
192
193 void
194 AudioDialog::content_changed (int p)
195 {
196         if (p == AudioContentProperty::AUDIO_GAIN) {
197                 _plot->set_gain (_content->audio_gain ());
198                 setup_peak_time ();
199         } else if (p == AudioContentProperty::AUDIO_MAPPING) {
200                 try_to_load_analysis ();
201         }
202 }
203
204 void
205 AudioDialog::type_clicked (wxCommandEvent& ev)
206 {
207         int t = 0;
208         while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
209                 ++t;
210         }
211
212         DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
213
214         _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
215 }
216
217 void
218 AudioDialog::smoothing_changed ()
219 {
220         _plot->set_smoothing (_smoothing->GetValue ());
221 }
222
223 void
224 AudioDialog::setup_peak_time ()
225 {
226         if (!_analysis || !_analysis->peak ()) {
227                 return;
228         }
229         
230         shared_ptr<Film> film = _film.lock ();
231         if (!film) {
232                 return;
233         }
234         
235         float peak_dB = 20 * log10 (_analysis->peak().get()) + _content->audio_gain();
236         
237         _peak_time->SetLabel (
238                 wxString::Format (
239                         _("Peak is %.2fdB at %s"),
240                         peak_dB,
241                         time_to_timecode (_analysis->peak_time().get(), film->video_frame_rate ()).data ()
242                         )
243                 );
244         
245         if (peak_dB > -3) {
246                 _peak_time->SetForegroundColour (wxColour (255, 0, 0));
247         } else {
248                 _peak_time->SetForegroundColour (wxColour (0, 0, 0));
249         }
250 }