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