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