Merge master; fix crash on new film.
[dcpomatic.git] / src / wx / audio_plot.cc
1 /*
2     Copyright (C) 2013 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 <iostream>
21 #include <boost/bind.hpp>
22 #include <wx/graphics.h>
23 #include "audio_plot.h"
24 #include "lib/audio_decoder.h"
25 #include "lib/audio_analysis.h"
26 #include "wx/wx_util.h"
27
28 using std::cout;
29 using std::vector;
30 using std::list;
31 using std::max;
32 using std::min;
33 using boost::bind;
34 using boost::shared_ptr;
35
36 int const AudioPlot::_minimum = -70;
37 int const AudioPlot::max_smoothing = 128;
38
39 AudioPlot::AudioPlot (wxWindow* parent)
40         : wxPanel (parent)
41         , _gain (0)
42         , _smoothing (max_smoothing / 2)
43 {
44         SetDoubleBuffered (true);
45
46         for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
47                 _channel_visible[i] = false;
48         }
49
50         for (int i = 0; i < AudioPoint::COUNT; ++i) {
51                 _type_visible[i] = false;
52         }
53
54         _colours.push_back (wxColour (  0,   0,   0));
55         _colours.push_back (wxColour (255,   0,   0));
56         _colours.push_back (wxColour (  0, 255,   0));
57         _colours.push_back (wxColour (139,   0, 204));
58         _colours.push_back (wxColour (  0,   0, 255));
59         _colours.push_back (wxColour (100, 100, 100));
60         
61         Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (AudioPlot::paint), 0, this);
62         
63         SetMinSize (wxSize (640, 512));
64 }
65
66 void
67 AudioPlot::set_analysis (shared_ptr<AudioAnalysis> a)
68 {
69         _analysis = a;
70
71         for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
72                 _channel_visible[i] = false;
73         }
74
75         for (int i = 0; i < AudioPoint::COUNT; ++i) {
76                 _type_visible[i] = false;
77         }
78         
79         Refresh ();
80 }
81
82 void
83 AudioPlot::set_channel_visible (int c, bool v)
84 {
85         _channel_visible[c] = v;
86         Refresh ();
87 }
88
89 void
90 AudioPlot::set_type_visible (int t, bool v)
91 {
92         _type_visible[t] = v;
93         Refresh ();
94 }
95
96 void
97 AudioPlot::paint (wxPaintEvent &)
98 {
99         wxPaintDC dc (this);
100
101         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
102         if (!gc) {
103                 return;
104         }
105
106         if (!_analysis || _analysis->channels() == 0) {
107                 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
108                 gc->DrawText (_("Please wait; audio is being analysed..."), 32, 32);
109                 return;
110         }
111
112         wxGraphicsPath grid = gc->CreatePath ();
113         gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
114         wxDouble db_label_height;
115         wxDouble db_label_descent;
116         wxDouble db_label_leading;
117         gc->GetTextExtent (wxT ("-80dB"), &_db_label_width, &db_label_height, &db_label_descent, &db_label_leading);
118
119         _db_label_width += 8;
120         
121         int const data_width = GetSize().GetWidth() - _db_label_width;
122         /* Assume all channels have the same number of points */
123         _x_scale = data_width / float (_analysis->points (0));
124         _height = GetSize().GetHeight ();
125         _y_origin = 32;
126         _y_scale = (_height - _y_origin) / -_minimum;
127
128         for (int i = _minimum; i <= 0; i += 10) {
129                 int const y = (_height - (i - _minimum) * _y_scale) - _y_origin;
130                 grid.MoveToPoint (_db_label_width - 4, y);
131                 grid.AddLineToPoint (_db_label_width + data_width, y);
132                 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), 0, y - (db_label_height / 2));
133         }
134
135         gc->SetPen (*wxLIGHT_GREY_PEN);
136         gc->StrokePath (grid);
137
138         gc->DrawText (_("Time"), data_width, _height - _y_origin + db_label_height / 2);
139
140         
141         if (_type_visible[AudioPoint::PEAK]) {
142                 for (int c = 0; c < MAX_AUDIO_CHANNELS; ++c) {
143                         wxGraphicsPath p = gc->CreatePath ();
144                         if (_channel_visible[c] && c < _analysis->channels()) {
145                                 plot_peak (p, c);
146                         }
147                         wxColour const col = _colours[c];
148 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
149                         gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxPENSTYLE_SOLID));
150 #else                   
151                         gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxSOLID));
152 #endif
153                         gc->StrokePath (p);
154                 }
155         }
156
157         if (_type_visible[AudioPoint::RMS]) {
158                 for (int c = 0; c < MAX_AUDIO_CHANNELS; ++c) {
159                         wxGraphicsPath p = gc->CreatePath ();
160                         if (_channel_visible[c] && c < _analysis->channels()) {
161                                 plot_rms (p, c);
162                         }
163                         wxColour const col = _colours[c];
164 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
165                         gc->SetPen (*wxThePenList->FindOrCreatePen (col, 1, wxPENSTYLE_SOLID));
166 #else
167                         gc->SetPen (*wxThePenList->FindOrCreatePen (col, 1, wxSOLID));
168 #endif                  
169                         gc->StrokePath (p);
170                 }
171         }
172
173         wxGraphicsPath axes = gc->CreatePath ();
174         axes.MoveToPoint (_db_label_width, 0);
175         axes.AddLineToPoint (_db_label_width, _height - _y_origin);
176         axes.AddLineToPoint (_db_label_width + data_width, _height - _y_origin);
177         gc->SetPen (*wxBLACK_PEN);
178         gc->StrokePath (axes);
179
180         delete gc;
181 }
182
183 float
184 AudioPlot::y_for_linear (float p) const
185 {
186         return _height - (20 * log10(p) - _minimum + _gain) * _y_scale - _y_origin;
187 }
188
189 void
190 AudioPlot::plot_peak (wxGraphicsPath& path, int channel) const
191 {
192         path.MoveToPoint (_db_label_width, y_for_linear (_analysis->get_point(channel, 0)[AudioPoint::PEAK]));
193
194         float peak = 0;
195         int const N = _analysis->points(channel);
196         for (int i = 0; i < N; ++i) {
197                 float const p = _analysis->get_point(channel, i)[AudioPoint::PEAK];
198                 peak -= 0.01f * (1 - log10 (_smoothing) / log10 (max_smoothing));
199                 if (p > peak) {
200                         peak = p;
201                 } else if (peak < 0) {
202                         peak = 0;
203                 }
204                 
205                 path.AddLineToPoint (_db_label_width + i * _x_scale, y_for_linear (peak));
206         }
207 }
208
209 void
210 AudioPlot::plot_rms (wxGraphicsPath& path, int channel) const
211 {
212         path.MoveToPoint (_db_label_width, y_for_linear (_analysis->get_point(channel, 0)[AudioPoint::RMS]));
213
214         list<float> smoothing;
215
216         int const N = _analysis->points(channel);
217
218         float const first = _analysis->get_point(channel, 0)[AudioPoint::RMS];
219         float const last = _analysis->get_point(channel, N - 1)[AudioPoint::RMS];
220
221         int const before = _smoothing / 2;
222         int const after = _smoothing - before;
223         
224         /* Pre-load the smoothing list */
225         for (int i = 0; i < before; ++i) {
226                 smoothing.push_back (first);
227         }
228         for (int i = 0; i < after; ++i) {
229                 if (i < N) {
230                         smoothing.push_back (_analysis->get_point(channel, i)[AudioPoint::RMS]);
231                 } else {
232                         smoothing.push_back (last);
233                 }
234         }
235         
236         for (int i = 0; i < N; ++i) {
237
238                 int const next_for_window = i + after;
239
240                 if (next_for_window < N) {
241                         smoothing.push_back (_analysis->get_point(channel, i)[AudioPoint::RMS]);
242                 } else {
243                         smoothing.push_back (last);
244                 }
245
246                 smoothing.pop_front ();
247
248                 float p = 0;
249                 for (list<float>::const_iterator j = smoothing.begin(); j != smoothing.end(); ++j) {
250                         p += pow (*j, 2);
251                 }
252
253                 p = sqrt (p / smoothing.size ());
254
255                 path.AddLineToPoint (_db_label_width + i * _x_scale, y_for_linear (p));
256         }
257 }
258
259 void
260 AudioPlot::set_gain (float g)
261 {
262         _gain = g;
263         Refresh ();
264 }
265
266 void
267 AudioPlot::set_smoothing (int s)
268 {
269         _smoothing = s;
270         Refresh ();
271 }