Supporters update.
[dcpomatic.git] / src / wx / audio_plot.cc
1 /*
2     Copyright (C) 2013-2020 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
22 #include "audio_plot.h"
23 #include "film_viewer.h"
24 #include "wx_util.h"
25 #include "lib/audio_decoder.h"
26 #include "lib/audio_analysis.h"
27 #include "lib/compose.hpp"
28 #include "lib/constants.h"
29 #include "lib/maths_util.h"
30 #include <dcp/warnings.h>
31 LIBDCP_DISABLE_WARNINGS
32 #include <wx/graphics.h>
33 LIBDCP_ENABLE_WARNINGS
34 #include <boost/bind/bind.hpp>
35 #include <cfloat>
36
37
38 using std::list;
39 using std::map;
40 using std::max;
41 using std::min;
42 using std::shared_ptr;
43 using std::vector;
44 using std::weak_ptr;
45 using boost::bind;
46 using boost::optional;
47 #if BOOST_VERSION >= 106100
48 using namespace boost::placeholders;
49 #endif
50 using namespace dcpomatic;
51
52
53 int const AudioPlot::_minimum = -70;
54 int const AudioPlot::_cursor_size = 8;
55 int const AudioPlot::max_smoothing = 128;
56
57
58 AudioPlot::AudioPlot(wxWindow* parent, FilmViewer& viewer)
59         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
60         , _viewer (viewer)
61         , _smoothing (max_smoothing / 2)
62         , _gain_correction (0)
63 {
64 #ifndef __WXOSX__
65         SetDoubleBuffered (true);
66 #endif
67
68         for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
69                 _channel_visible[i] = false;
70         }
71
72         for (int i = 0; i < AudioPoint::COUNT; ++i) {
73                 _type_visible[i] = false;
74         }
75
76         if (gui_is_dark()) {
77                 _colours.push_back(wxColour(255, 255, 255));
78         } else {
79                 _colours.push_back(wxColour(0, 0, 0));
80         }
81         _colours.push_back (wxColour (255,   0,   0));
82         _colours.push_back (wxColour (  0, 255,   0));
83         _colours.push_back (wxColour (139,   0, 204));
84         _colours.push_back (wxColour (  0,   0, 255));
85         _colours.push_back (wxColour (  0, 139,   0));
86         _colours.push_back (wxColour (  0,   0, 139));
87         _colours.push_back (wxColour (255, 255,   0));
88         _colours.push_back (wxColour (  0, 255, 255));
89         _colours.push_back (wxColour (255,   0, 255));
90         _colours.push_back (wxColour (255,   0, 139));
91         _colours.push_back (wxColour (139,   0, 255));
92
93         _colours.push_back (wxColour (139, 139, 255));
94         _colours.push_back (wxColour (  0, 139, 255));
95         _colours.push_back (wxColour (255, 139, 139));
96         _colours.push_back (wxColour (255, 139,   0));
97
98         set_analysis (shared_ptr<AudioAnalysis> ());
99
100 #if MAX_DCP_AUDIO_CHANNELS != 16
101 #warning AudioPlot::AudioPlot is expecting the wrong MAX_DCP_AUDIO_CHANNELS
102 #endif
103
104         Bind (wxEVT_PAINT, boost::bind (&AudioPlot::paint, this));
105         Bind (wxEVT_MOTION, boost::bind (&AudioPlot::mouse_moved, this, _1));
106         Bind (wxEVT_LEAVE_WINDOW, boost::bind (&AudioPlot::mouse_leave, this, _1));
107         Bind (wxEVT_LEFT_DOWN, boost::bind(&AudioPlot::left_down, this));
108
109         SetMinSize (wxSize (640, 512));
110 }
111
112
113 void
114 AudioPlot::set_analysis (shared_ptr<AudioAnalysis> a)
115 {
116         _analysis = a;
117
118         if (!a) {
119                 _message = _("Please wait; audio is being analysed...");
120         }
121
122         Refresh ();
123 }
124
125
126 void
127 AudioPlot::set_channel_visible (int c, bool v)
128 {
129         _channel_visible[c] = v;
130         Refresh ();
131 }
132
133
134 void
135 AudioPlot::set_type_visible (int t, bool v)
136 {
137         _type_visible[t] = v;
138         Refresh ();
139 }
140
141
142 void
143 AudioPlot::set_message (wxString s)
144 {
145         _message = s;
146         Refresh ();
147 }
148
149
150 struct Metrics
151 {
152         double db_label_width;
153         int height;
154         int y_origin;
155         float x_scale; ///< pixels per data point
156         float y_scale;
157 };
158
159
160 void
161 AudioPlot::paint ()
162 {
163         wxPaintDC dc (this);
164
165         auto gc = wxGraphicsContext::Create (dc);
166         if (!gc) {
167                 return;
168         }
169
170         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
171
172         if (!_analysis || _analysis->channels() == 0) {
173                 gc->SetFont(gc->CreateFont(*wxNORMAL_FONT, gui_is_dark() ? *wxWHITE : *wxBLACK));
174                 gc->DrawText (_message, 32, 32);
175                 delete gc;
176                 return;
177         }
178
179         auto h_grid = gc->CreatePath ();
180         gc->SetFont(gc->CreateFont(*wxSMALL_FONT, gui_is_dark() ? *wxWHITE : *wxBLACK));
181         wxDouble db_label_height;
182         wxDouble db_label_descent;
183         wxDouble db_label_leading;
184         Metrics metrics;
185         gc->GetTextExtent (wxT ("-80dB"), &metrics.db_label_width, &db_label_height, &db_label_descent, &db_label_leading);
186
187         metrics.db_label_width += 8;
188
189         int const data_width = GetSize().GetWidth() - metrics.db_label_width;
190         /* Assume all channels have the same number of points */
191         metrics.x_scale = data_width / float (_analysis->points (0));
192         metrics.height = GetSize().GetHeight ();
193         metrics.y_origin = 32;
194         metrics.y_scale = (metrics.height - metrics.y_origin) / -_minimum;
195
196         for (int i = _minimum; i <= 0; i += 10) {
197                 int const y = (metrics.height - (i - _minimum) * metrics.y_scale) - metrics.y_origin;
198                 h_grid.MoveToPoint (metrics.db_label_width - 4, y);
199                 h_grid.AddLineToPoint (metrics.db_label_width + data_width, y);
200                 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), 0, y - (db_label_height / 2));
201         }
202
203         wxColour const grid_colour = gui_is_dark() ? wxColour(80, 80, 80) : wxColour(200, 200, 200);
204
205         gc->SetPen(wxPen(grid_colour));
206         gc->StrokePath (h_grid);
207
208         /* Draw an x axis with marks */
209
210         auto v_grid = gc->CreatePath ();
211
212         DCPOMATIC_ASSERT (_analysis->samples_per_point() != 0.0);
213         double const pps = _analysis->sample_rate() * metrics.x_scale / _analysis->samples_per_point();
214
215         double const mark_interval = calculate_mark_interval (rint (128 / pps));
216
217         auto t = DCPTime::from_seconds (mark_interval);
218         while ((t.seconds() * pps) < data_width) {
219                 double tc = t.seconds ();
220                 int const h = tc / 3600;
221                 tc -= h * 3600;
222                 int const m = tc / 60;
223                 tc -= m * 60;
224                 int const s = tc;
225
226                 auto str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
227                 wxDouble str_width;
228                 wxDouble str_height;
229                 wxDouble str_descent;
230                 wxDouble str_leading;
231                 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
232
233                 int const tx = llrintf (metrics.db_label_width + t.seconds() * pps);
234                 gc->DrawText (str, tx - str_width / 2, metrics.height - metrics.y_origin + db_label_height);
235
236                 v_grid.MoveToPoint (tx, metrics.height - metrics.y_origin + 4);
237                 v_grid.AddLineToPoint (tx, metrics.y_origin);
238
239                 t += DCPTime::from_seconds (mark_interval);
240         }
241
242         gc->SetPen(wxPen(grid_colour));
243         gc->StrokePath (v_grid);
244
245         if (_type_visible[AudioPoint::PEAK]) {
246                 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
247                         auto p = gc->CreatePath ();
248                         if (_channel_visible[c] && c < _analysis->channels()) {
249                                 plot_peak (p, c, metrics);
250                         }
251                         auto const col = _colours[c];
252                         gc->SetPen (wxPen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxPENSTYLE_SOLID));
253                         gc->StrokePath (p);
254                 }
255         }
256
257         if (_type_visible[AudioPoint::RMS]) {
258                 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
259                         auto p = gc->CreatePath ();
260                         if (_channel_visible[c] && c < _analysis->channels()) {
261                                 plot_rms (p, c, metrics);
262                         }
263                         auto const col = _colours[c];
264                         gc->SetPen (wxPen (col, 1, wxPENSTYLE_SOLID));
265                         gc->StrokePath (p);
266                 }
267         }
268
269         auto axes = gc->CreatePath ();
270         axes.MoveToPoint (metrics.db_label_width, 0);
271         axes.AddLineToPoint (metrics.db_label_width, metrics.height - metrics.y_origin);
272         axes.AddLineToPoint (metrics.db_label_width + data_width, metrics.height - metrics.y_origin);
273         gc->SetPen(wxPen(grid_colour));
274         gc->StrokePath (axes);
275
276         if (_cursor) {
277                 auto cursor = gc->CreatePath ();
278                 cursor.MoveToPoint (_cursor->draw.x - _cursor_size / 2, _cursor->draw.y - _cursor_size / 2);
279                 cursor.AddLineToPoint (_cursor->draw.x + _cursor_size / 2, _cursor->draw.y + _cursor_size / 2);
280                 cursor.MoveToPoint (_cursor->draw.x + _cursor_size / 2, _cursor->draw.y - _cursor_size / 2);
281                 cursor.AddLineToPoint (_cursor->draw.x - _cursor_size / 2, _cursor->draw.y + _cursor_size / 2);
282                 gc->StrokePath (cursor);
283         }
284
285         delete gc;
286 }
287
288
289 float
290 AudioPlot::y_for_linear (float p, Metrics const & metrics) const
291 {
292         if (p < 1e-4) {
293                 p = 1e-4;
294         }
295
296         return metrics.height - (linear_to_db(p) - _minimum) * metrics.y_scale - metrics.y_origin;
297 }
298
299
300 void
301 AudioPlot::plot_peak (wxGraphicsPath& path, int channel, Metrics const & metrics) const
302 {
303         if (_analysis->points (channel) == 0) {
304                 return;
305         }
306
307         _peak[channel] = PointList ();
308
309         float peak = 0;
310         int const N = _analysis->points(channel);
311         for (int i = 0; i < N; ++i) {
312                 float const p = get_point(channel, i)[AudioPoint::PEAK];
313                 peak -= 0.01f * (1 - log10 (_smoothing) / log10 (max_smoothing));
314                 if (p > peak) {
315                         peak = p;
316                 } else if (peak < 0) {
317                         peak = 0;
318                 }
319
320                 _peak[channel].push_back (
321                         Point (
322                                 wxPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (peak, metrics)),
323                                 DCPTime::from_frames (i * _analysis->samples_per_point(), _analysis->sample_rate()),
324                                 linear_to_db(peak)
325                                 )
326                         );
327         }
328
329         DCPOMATIC_ASSERT (_peak.find(channel) != _peak.end());
330
331         path.MoveToPoint (_peak[channel][0].draw);
332         for (auto const& i: _peak[channel]) {
333                 path.AddLineToPoint (i.draw);
334         }
335 }
336
337
338 void
339 AudioPlot::plot_rms (wxGraphicsPath& path, int channel, Metrics const & metrics) const
340 {
341         if (_analysis->points (channel) == 0) {
342                 return;
343         }
344
345         _rms[channel] = PointList();
346
347         list<float> smoothing;
348
349         int const N = _analysis->points(channel);
350
351         float const first = get_point(channel, 0)[AudioPoint::RMS];
352         float const last = get_point(channel, N - 1)[AudioPoint::RMS];
353
354         int const before = _smoothing / 2;
355         int const after = _smoothing - before;
356
357         /* Pre-load the smoothing list */
358         for (int i = 0; i < before; ++i) {
359                 smoothing.push_back (first);
360         }
361         for (int i = 0; i < after; ++i) {
362                 if (i < N) {
363                         smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
364                 } else {
365                         smoothing.push_back (last);
366                 }
367         }
368
369         for (int i = 0; i < N; ++i) {
370
371                 int const next_for_window = i + after;
372
373                 if (next_for_window < N) {
374                         smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
375                 } else {
376                         smoothing.push_back (last);
377                 }
378
379                 smoothing.pop_front ();
380
381                 float p = 0;
382                 for (list<float>::const_iterator j = smoothing.begin(); j != smoothing.end(); ++j) {
383                         p += pow (*j, 2);
384                 }
385
386                 if (!smoothing.empty ()) {
387                         p = sqrt (p / smoothing.size ());
388                 }
389
390                 _rms[channel].push_back (
391                         Point (
392                                 wxPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (p, metrics)),
393                                 DCPTime::from_frames (i * _analysis->samples_per_point(), _analysis->sample_rate()),
394                                 linear_to_db(p)
395                                 )
396                         );
397         }
398
399         DCPOMATIC_ASSERT (_rms.find(channel) != _rms.end());
400
401         path.MoveToPoint (_rms[channel][0].draw);
402         for (auto const& i: _rms[channel]) {
403                 path.AddLineToPoint (i.draw);
404         }
405 }
406
407
408 void
409 AudioPlot::set_smoothing (int s)
410 {
411         _smoothing = s;
412         _rms.clear ();
413         _peak.clear ();
414         Refresh ();
415 }
416
417
418 void
419 AudioPlot::set_gain_correction (double gain)
420 {
421         _gain_correction = gain;
422         Refresh ();
423 }
424
425
426 AudioPoint
427 AudioPlot::get_point (int channel, int point) const
428 {
429         auto p = _analysis->get_point (channel, point);
430         for (int i = 0; i < AudioPoint::COUNT; ++i) {
431                 p[i] *= db_to_linear(_gain_correction);
432         }
433
434         return p;
435 }
436
437
438 /** @param n Channel index.
439  *  @return Colour used by that channel in the plot.
440  */
441 wxColour
442 AudioPlot::colour (int n) const
443 {
444         DCPOMATIC_ASSERT (n < int(_colours.size()));
445         return _colours[n];
446 }
447
448
449 void
450 AudioPlot::left_down ()
451 {
452         if (_cursor) {
453                 _viewer.seek(_cursor->time, true);
454         }
455 }
456
457
458 void
459 AudioPlot::mouse_moved (wxMouseEvent& ev)
460 {
461         double min_dist = DBL_MAX;
462         Point min_point;
463
464         auto search = [this](map<int, PointList> const & search, wxMouseEvent const & ev, double& min_dist, Point& min_point) {
465                 for (auto const& i: search) {
466                         if (_channel_visible[i.first]) {
467                                 for (auto const& j: i.second) {
468                                         double const dist = pow(ev.GetX() - j.draw.x, 2) + pow(ev.GetY() - j.draw.y, 2);
469                                         if (dist < min_dist) {
470                                                 min_dist = dist;
471                                                 min_point = j;
472                                         }
473                                 }
474                         }
475                 }
476         };
477
478         if (_type_visible[AudioPoint::RMS]) {
479                 search (_rms, ev, min_dist, min_point);
480         }
481         if (_type_visible[AudioPoint::PEAK]) {
482                 search (_peak, ev, min_dist, min_point);
483         }
484
485         _cursor = boost::none;
486
487         if (min_dist < DBL_MAX) {
488                 wxRect before (min_point.draw.x - _cursor_size / 2, min_point.draw.y - _cursor_size / 2, _cursor_size, _cursor_size);
489                 GetParent()->Refresh (true, &before);
490                 _cursor = min_point;
491                 wxRect after (min_point.draw.x - _cursor_size / 2, min_point.draw.y - _cursor_size / 2, _cursor_size, _cursor_size);
492                 GetParent()->Refresh (true, &after);
493                 Cursor (min_point.time, min_point.db);
494         }
495 }
496
497
498 void
499 AudioPlot::mouse_leave (wxMouseEvent &)
500 {
501         _cursor = boost::none;
502         Refresh ();
503         Cursor (optional<DCPTime>(), optional<float>());
504 }