0034e793d020934b7e3c9e5e31f9c88a01625fe3
[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 "audio_dialog.h"
21 #include "audio_plot.h"
22 #include "wx_util.h"
23 #include "lib/audio_analysis.h"
24 #include "lib/film.h"
25 #include "lib/analyse_audio_job.h"
26 #include "lib/job_manager.h"
27 #include "lib/playlist.h"
28 #include <boost/filesystem.hpp>
29
30 using boost::shared_ptr;
31 using boost::bind;
32 using boost::optional;
33
34 AudioDialog::AudioDialog (wxWindow* parent, shared_ptr<Film> film)
35         : wxDialog (parent, wxID_ANY, _("Audio"), wxDefaultPosition, wxSize (640, 512), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE)
36         , _film (film)
37         , _plot (0)
38 {
39         wxFont subheading_font (*wxNORMAL_FONT);
40         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
41
42         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
43         
44         wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
45
46         _plot = new AudioPlot (this);
47         left->Add (_plot, 1, wxALL | wxEXPAND, 12);
48         _peak_time = new wxStaticText (this, wxID_ANY, wxT (""));
49         left->Add (_peak_time, 0, wxALL, 12);
50
51         sizer->Add (left, 1, wxALL, 12);
52
53         wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
54
55         {
56                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Channels"));
57                 m->SetFont (subheading_font);
58                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM, 16);
59         }
60
61         for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
62                 _channel_checkbox[i] = new wxCheckBox (this, wxID_ANY, std_to_wx (audio_channel_name (i)));
63                 right->Add (_channel_checkbox[i], 0, wxEXPAND | wxALL, 3);
64                 _channel_checkbox[i]->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioDialog::channel_clicked, this, _1));
65         }
66
67         {
68                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Type"));
69                 m->SetFont (subheading_font);
70                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
71         }
72         
73         wxString const types[] = {
74                 _("Peak"),
75                 _("RMS")
76         };
77
78         for (int i = 0; i < AudioPoint::COUNT; ++i) {
79                 _type_checkbox[i] = new wxCheckBox (this, wxID_ANY, types[i]);
80                 right->Add (_type_checkbox[i], 0, wxEXPAND | wxALL, 3);
81                 _type_checkbox[i]->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioDialog::type_clicked, this, _1));
82         }
83
84         {
85                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Smoothing"));
86                 m->SetFont (subheading_font);
87                 right->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
88         }
89         
90         _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
91         _smoothing->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&AudioDialog::smoothing_changed, this));
92         right->Add (_smoothing, 0, wxEXPAND);
93
94         sizer->Add (right, 0, wxALL, 12);
95
96         SetSizer (sizer);
97         sizer->Layout ();
98         sizer->SetSizeHints (this);
99 }
100
101 void
102 AudioDialog::set_playlist (shared_ptr<const Playlist> p)
103 {
104         _playlist_connection.disconnect ();
105         _playlist = p;
106         _playlist_connection = _playlist->ContentChanged.connect (boost::bind (&AudioDialog::try_to_load_analysis, this));
107         try_to_load_analysis ();
108         SetTitle (_("DCP-o-matic audio"));
109 }
110
111 void
112 AudioDialog::try_to_load_analysis ()
113 {
114         if (!IsShown ()) {
115                 return;
116         }
117
118         shared_ptr<const Film> film = _film.lock ();
119         DCPOMATIC_ASSERT (film);
120
121         boost::filesystem::path path = film->audio_analysis_path (_playlist);
122
123         if (!boost::filesystem::exists (path)) {
124                 _plot->set_analysis (shared_ptr<AudioAnalysis> ());
125                 _analysis.reset ();
126                 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, _playlist));
127                 _analysis_finished_connection = job->Finished.connect (bind (&AudioDialog::analysis_finished, this));
128                 JobManager::instance()->add (job);
129                 return;
130         }
131
132         try {
133                 _analysis.reset (new AudioAnalysis (path));
134         } catch (xmlpp::exception& e) {
135                 /* Probably an old-style analysis file: recreate it */
136                 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, _playlist));
137                 _analysis_finished_connection = job->Finished.connect (bind (&AudioDialog::analysis_finished, this));
138                 JobManager::instance()->add (job);
139                 return;
140         }
141         
142         _plot->set_analysis (_analysis);
143         setup_peak_time ();
144
145         /* Set up some defaults if no check boxes are checked */
146         
147         int i = 0;
148         while (i < MAX_DCP_AUDIO_CHANNELS && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
149                 ++i;
150         }
151
152         if (i == MAX_DCP_AUDIO_CHANNELS && _channel_checkbox[0]) {
153                 _channel_checkbox[0]->SetValue (true);
154                 _plot->set_channel_visible (0, true);
155         }
156
157         i = 0;
158         while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
159                 i++;
160         }
161
162         if (i == AudioPoint::COUNT) {
163                 for (int i = 0; i < AudioPoint::COUNT; ++i) {
164                         _type_checkbox[i]->SetValue (true);
165                         _plot->set_type_visible (i, true);
166                 }
167         }
168
169         Refresh ();
170 }
171
172 void
173 AudioDialog::analysis_finished ()
174 {
175         shared_ptr<const Film> film = _film.lock ();
176         DCPOMATIC_ASSERT (film);
177         
178         if (!boost::filesystem::exists (film->audio_analysis_path (_playlist))) {
179                 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
180                    Give up.
181                 */
182                 _plot->set_message (_("Could not analyse audio."));
183                 return;
184         }
185
186         try_to_load_analysis ();
187 }
188
189 void
190 AudioDialog::channel_clicked (wxCommandEvent& ev)
191 {
192         int c = 0;
193         while (c < MAX_DCP_AUDIO_CHANNELS && ev.GetEventObject() != _channel_checkbox[c]) {
194                 ++c;
195         }
196
197         DCPOMATIC_ASSERT (c < MAX_DCP_AUDIO_CHANNELS);
198
199         _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
200 }
201
202 void
203 AudioDialog::content_changed (int p)
204 {
205         if (p == AudioContentProperty::AUDIO_GAIN || p == AudioContentProperty::AUDIO_STREAMS) {
206                 try_to_load_analysis ();
207         }
208 }
209
210 void
211 AudioDialog::type_clicked (wxCommandEvent& ev)
212 {
213         int t = 0;
214         while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
215                 ++t;
216         }
217
218         DCPOMATIC_ASSERT (t < AudioPoint::COUNT);
219
220         _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
221 }
222
223 void
224 AudioDialog::smoothing_changed ()
225 {
226         _plot->set_smoothing (_smoothing->GetValue ());
227 }
228
229 void
230 AudioDialog::setup_peak_time ()
231 {
232         if (!_analysis || !_analysis->peak ()) {
233                 return;
234         }
235         
236         shared_ptr<Film> film = _film.lock ();
237         if (!film) {
238                 return;
239         }
240         
241         float peak_dB = 20 * log10 (_analysis->peak().get());
242         
243         _peak_time->SetLabel (
244                 wxString::Format (
245                         _("Peak is %.2fdB at %s"),
246                         peak_dB,
247                         time_to_timecode (_analysis->peak_time().get(), film->video_frame_rate ()).data ()
248                         )
249                 );
250         
251         if (peak_dB > -3) {
252                 _peak_time->SetForegroundColour (wxColour (255, 0, 0));
253         } else {
254                 _peak_time->SetForegroundColour (wxColour (0, 0, 0));
255         }
256 }