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