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