No-op: remove all trailing whitespace.
[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, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
41         , _smoothing (max_smoothing / 2)
42 {
43 #ifndef __WXOSX__
44         SetDoubleBuffered (true);
45 #endif
46
47         for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
48                 _channel_visible[i] = false;
49         }
50
51         for (int i = 0; i < AudioPoint::COUNT; ++i) {
52                 _type_visible[i] = false;
53         }
54
55         _colours.push_back (wxColour (  0,   0,   0));
56         _colours.push_back (wxColour (255,   0,   0));
57         _colours.push_back (wxColour (  0, 255,   0));
58         _colours.push_back (wxColour (139,   0, 204));
59         _colours.push_back (wxColour (  0,   0, 255));
60         _colours.push_back (wxColour (  0, 139,   0));
61         _colours.push_back (wxColour (  0,   0, 139));
62         _colours.push_back (wxColour (255, 255,   0));
63         _colours.push_back (wxColour (  0, 255, 255));
64         _colours.push_back (wxColour (255,   0, 255));
65         _colours.push_back (wxColour (255,   0, 139));
66         _colours.push_back (wxColour (139,   0, 255));
67
68         set_analysis (shared_ptr<AudioAnalysis> ());
69
70 #if MAX_DCP_AUDIO_CHANNELS != 12
71 #warning AudioPlot::AudioPlot is expecting the wrong MAX_DCP_AUDIO_CHANNELS
72 #endif
73
74         Bind (wxEVT_PAINT, boost::bind (&AudioPlot::paint, this));
75
76         SetMinSize (wxSize (640, 512));
77 }
78
79 void
80 AudioPlot::set_analysis (shared_ptr<AudioAnalysis> a)
81 {
82         _analysis = a;
83
84         if (!a) {
85                 _message = _("Please wait; audio is being analysed...");
86         }
87
88         Refresh ();
89 }
90
91 void
92 AudioPlot::set_channel_visible (int c, bool v)
93 {
94         _channel_visible[c] = v;
95         Refresh ();
96 }
97
98 void
99 AudioPlot::set_type_visible (int t, bool v)
100 {
101         _type_visible[t] = v;
102         Refresh ();
103 }
104
105 void
106 AudioPlot::set_message (wxString s)
107 {
108         _message = s;
109         Refresh ();
110 }
111
112 struct Metrics
113 {
114         double db_label_width;
115         int height;
116         int y_origin;
117         float x_scale;
118         float y_scale;
119 };
120
121 void
122 AudioPlot::paint ()
123 {
124         wxPaintDC dc (this);
125
126         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
127         if (!gc) {
128                 return;
129         }
130
131         if (!_analysis || _analysis->channels() == 0) {
132                 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
133                 gc->DrawText (_message, 32, 32);
134                 return;
135         }
136
137         wxGraphicsPath grid = gc->CreatePath ();
138         gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
139         wxDouble db_label_height;
140         wxDouble db_label_descent;
141         wxDouble db_label_leading;
142         Metrics metrics;
143         gc->GetTextExtent (wxT ("-80dB"), &metrics.db_label_width, &db_label_height, &db_label_descent, &db_label_leading);
144
145         metrics.db_label_width += 8;
146
147         int const data_width = GetSize().GetWidth() - metrics.db_label_width;
148         /* Assume all channels have the same number of points */
149         metrics.x_scale = data_width / float (_analysis->points (0));
150         metrics.height = GetSize().GetHeight ();
151         metrics.y_origin = 32;
152         metrics.y_scale = (metrics.height - metrics.y_origin) / -_minimum;
153
154         for (int i = _minimum; i <= 0; i += 10) {
155                 int const y = (metrics.height - (i - _minimum) * metrics.y_scale) - metrics.y_origin;
156                 grid.MoveToPoint (metrics.db_label_width - 4, y);
157                 grid.AddLineToPoint (metrics.db_label_width + data_width, y);
158                 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), 0, y - (db_label_height / 2));
159         }
160
161         gc->SetPen (wxPen (wxColour (200, 200, 200)));
162         gc->StrokePath (grid);
163
164         gc->DrawText (_("Time"), data_width, metrics.height - metrics.y_origin + db_label_height / 2);
165
166         if (_type_visible[AudioPoint::PEAK]) {
167                 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
168                         wxGraphicsPath p = gc->CreatePath ();
169                         if (_channel_visible[c] && c < _analysis->channels()) {
170                                 plot_peak (p, c, metrics);
171                         }
172                         wxColour const col = _colours[c];
173                         gc->SetPen (wxPen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxPENSTYLE_SOLID));
174                         gc->StrokePath (p);
175                 }
176         }
177
178         if (_type_visible[AudioPoint::RMS]) {
179                 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
180                         wxGraphicsPath p = gc->CreatePath ();
181                         if (_channel_visible[c] && c < _analysis->channels()) {
182                                 plot_rms (p, c, metrics);
183                         }
184                         wxColour const col = _colours[c];
185                         gc->SetPen (wxPen (col, 1, wxPENSTYLE_SOLID));
186                         gc->StrokePath (p);
187                 }
188         }
189
190         wxGraphicsPath axes = gc->CreatePath ();
191         axes.MoveToPoint (metrics.db_label_width, 0);
192         axes.AddLineToPoint (metrics.db_label_width, metrics.height - metrics.y_origin);
193         axes.AddLineToPoint (metrics.db_label_width + data_width, metrics.height - metrics.y_origin);
194         gc->SetPen (wxPen (wxColour (0, 0, 0)));
195         gc->StrokePath (axes);
196
197         delete gc;
198 }
199
200 float
201 AudioPlot::y_for_linear (float p, Metrics const & metrics) const
202 {
203         if (p < 1e-4) {
204                 p = 1e-4;
205         }
206
207         return metrics.height - (20 * log10(p) - _minimum) * metrics.y_scale - metrics.y_origin;
208 }
209
210 void
211 AudioPlot::plot_peak (wxGraphicsPath& path, int channel, Metrics const & metrics) const
212 {
213         if (_analysis->points (channel) == 0) {
214                 return;
215         }
216
217         path.MoveToPoint (metrics.db_label_width, y_for_linear (_analysis->get_point(channel, 0)[AudioPoint::PEAK], metrics));
218
219         float peak = 0;
220         int const N = _analysis->points(channel);
221         for (int i = 0; i < N; ++i) {
222                 float const p = _analysis->get_point(channel, i)[AudioPoint::PEAK];
223                 peak -= 0.01f * (1 - log10 (_smoothing) / log10 (max_smoothing));
224                 if (p > peak) {
225                         peak = p;
226                 } else if (peak < 0) {
227                         peak = 0;
228                 }
229
230                 path.AddLineToPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (peak, metrics));
231         }
232 }
233
234 void
235 AudioPlot::plot_rms (wxGraphicsPath& path, int channel, Metrics const & metrics) const
236 {
237         if (_analysis->points (channel) == 0) {
238                 return;
239         }
240
241         path.MoveToPoint (metrics.db_label_width, y_for_linear (_analysis->get_point(channel, 0)[AudioPoint::RMS], metrics));
242
243         list<float> smoothing;
244
245         int const N = _analysis->points(channel);
246
247         float const first = _analysis->get_point(channel, 0)[AudioPoint::RMS];
248         float const last = _analysis->get_point(channel, N - 1)[AudioPoint::RMS];
249
250         int const before = _smoothing / 2;
251         int const after = _smoothing - before;
252
253         /* Pre-load the smoothing list */
254         for (int i = 0; i < before; ++i) {
255                 smoothing.push_back (first);
256         }
257         for (int i = 0; i < after; ++i) {
258                 if (i < N) {
259                         smoothing.push_back (_analysis->get_point(channel, i)[AudioPoint::RMS]);
260                 } else {
261                         smoothing.push_back (last);
262                 }
263         }
264
265         for (int i = 0; i < N; ++i) {
266
267                 int const next_for_window = i + after;
268
269                 if (next_for_window < N) {
270                         smoothing.push_back (_analysis->get_point(channel, i)[AudioPoint::RMS]);
271                 } else {
272                         smoothing.push_back (last);
273                 }
274
275                 smoothing.pop_front ();
276
277                 float p = 0;
278                 for (list<float>::const_iterator j = smoothing.begin(); j != smoothing.end(); ++j) {
279                         p += pow (*j, 2);
280                 }
281
282                 if (!smoothing.empty ()) {
283                         p = sqrt (p / smoothing.size ());
284                 }
285
286                 path.AddLineToPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (p, metrics));
287         }
288 }
289
290 void
291 AudioPlot::set_smoothing (int s)
292 {
293         _smoothing = s;
294         Refresh ();
295 }