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