2 Copyright (C) 2013-2020 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "audio_plot.h"
24 #include "film_viewer.h"
25 #include "lib/audio_decoder.h"
26 #include "lib/audio_analysis.h"
27 #include "lib/compose.hpp"
28 #include <wx/graphics.h>
29 #include <boost/bind/bind.hpp>
41 using boost::optional;
42 using std::shared_ptr;
44 #if BOOST_VERSION >= 106100
45 using namespace boost::placeholders;
47 using namespace dcpomatic;
50 int const AudioPlot::_minimum = -70;
51 int const AudioPlot::_cursor_size = 8;
52 int const AudioPlot::max_smoothing = 128;
55 AudioPlot::AudioPlot (wxWindow* parent, weak_ptr<FilmViewer> viewer)
56 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
58 , _smoothing (max_smoothing / 2)
59 , _gain_correction (0)
62 SetDoubleBuffered (true);
65 for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
66 _channel_visible[i] = false;
69 for (int i = 0; i < AudioPoint::COUNT; ++i) {
70 _type_visible[i] = false;
73 _colours.push_back (wxColour ( 0, 0, 0));
74 _colours.push_back (wxColour (255, 0, 0));
75 _colours.push_back (wxColour ( 0, 255, 0));
76 _colours.push_back (wxColour (139, 0, 204));
77 _colours.push_back (wxColour ( 0, 0, 255));
78 _colours.push_back (wxColour ( 0, 139, 0));
79 _colours.push_back (wxColour ( 0, 0, 139));
80 _colours.push_back (wxColour (255, 255, 0));
81 _colours.push_back (wxColour ( 0, 255, 255));
82 _colours.push_back (wxColour (255, 0, 255));
83 _colours.push_back (wxColour (255, 0, 139));
84 _colours.push_back (wxColour (139, 0, 255));
86 _colours.push_back (wxColour (139, 139, 255));
87 _colours.push_back (wxColour ( 0, 139, 255));
88 _colours.push_back (wxColour (255, 139, 139));
89 _colours.push_back (wxColour (255, 139, 0));
91 set_analysis (shared_ptr<AudioAnalysis> ());
93 #if MAX_DCP_AUDIO_CHANNELS != 16
94 #warning AudioPlot::AudioPlot is expecting the wrong MAX_DCP_AUDIO_CHANNELS
97 Bind (wxEVT_PAINT, boost::bind (&AudioPlot::paint, this));
98 Bind (wxEVT_MOTION, boost::bind (&AudioPlot::mouse_moved, this, _1));
99 Bind (wxEVT_LEAVE_WINDOW, boost::bind (&AudioPlot::mouse_leave, this, _1));
100 Bind (wxEVT_LEFT_DOWN, boost::bind(&AudioPlot::left_down, this));
102 SetMinSize (wxSize (640, 512));
107 AudioPlot::set_analysis (shared_ptr<AudioAnalysis> a)
112 _message = _("Please wait; audio is being analysed...");
120 AudioPlot::set_channel_visible (int c, bool v)
122 _channel_visible[c] = v;
128 AudioPlot::set_type_visible (int t, bool v)
130 _type_visible[t] = v;
136 AudioPlot::set_message (wxString s)
145 double db_label_width;
148 float x_scale; ///< pixels per data point
158 auto gc = wxGraphicsContext::Create (dc);
163 if (!_analysis || _analysis->channels() == 0) {
164 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
165 gc->DrawText (_message, 32, 32);
170 auto h_grid = gc->CreatePath ();
171 gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
172 wxDouble db_label_height;
173 wxDouble db_label_descent;
174 wxDouble db_label_leading;
176 gc->GetTextExtent (wxT ("-80dB"), &metrics.db_label_width, &db_label_height, &db_label_descent, &db_label_leading);
178 metrics.db_label_width += 8;
180 int const data_width = GetSize().GetWidth() - metrics.db_label_width;
181 /* Assume all channels have the same number of points */
182 metrics.x_scale = data_width / float (_analysis->points (0));
183 metrics.height = GetSize().GetHeight ();
184 metrics.y_origin = 32;
185 metrics.y_scale = (metrics.height - metrics.y_origin) / -_minimum;
187 for (int i = _minimum; i <= 0; i += 10) {
188 int const y = (metrics.height - (i - _minimum) * metrics.y_scale) - metrics.y_origin;
189 h_grid.MoveToPoint (metrics.db_label_width - 4, y);
190 h_grid.AddLineToPoint (metrics.db_label_width + data_width, y);
191 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), 0, y - (db_label_height / 2));
194 gc->SetPen (wxPen (wxColour (200, 200, 200)));
195 gc->StrokePath (h_grid);
197 /* Draw an x axis with marks */
199 auto v_grid = gc->CreatePath ();
201 DCPOMATIC_ASSERT (_analysis->samples_per_point() != 0.0);
202 double const pps = _analysis->sample_rate() * metrics.x_scale / _analysis->samples_per_point();
204 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
206 double const mark_interval = calculate_mark_interval (rint (128 / pps));
208 auto t = DCPTime::from_seconds (mark_interval);
209 while ((t.seconds() * pps) < data_width) {
210 double tc = t.seconds ();
211 int const h = tc / 3600;
213 int const m = tc / 60;
217 auto str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
220 wxDouble str_descent;
221 wxDouble str_leading;
222 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
224 int const tx = llrintf (metrics.db_label_width + t.seconds() * pps);
225 gc->DrawText (str, tx - str_width / 2, metrics.height - metrics.y_origin + db_label_height);
227 v_grid.MoveToPoint (tx, metrics.height - metrics.y_origin + 4);
228 v_grid.AddLineToPoint (tx, metrics.y_origin);
230 t += DCPTime::from_seconds (mark_interval);
233 gc->SetPen (wxPen (wxColour (200, 200, 200)));
234 gc->StrokePath (v_grid);
236 if (_type_visible[AudioPoint::PEAK]) {
237 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
238 auto p = gc->CreatePath ();
239 if (_channel_visible[c] && c < _analysis->channels()) {
240 plot_peak (p, c, metrics);
242 auto const col = _colours[c];
243 gc->SetPen (wxPen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxPENSTYLE_SOLID));
248 if (_type_visible[AudioPoint::RMS]) {
249 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
250 auto p = gc->CreatePath ();
251 if (_channel_visible[c] && c < _analysis->channels()) {
252 plot_rms (p, c, metrics);
254 auto const col = _colours[c];
255 gc->SetPen (wxPen (col, 1, wxPENSTYLE_SOLID));
260 auto axes = gc->CreatePath ();
261 axes.MoveToPoint (metrics.db_label_width, 0);
262 axes.AddLineToPoint (metrics.db_label_width, metrics.height - metrics.y_origin);
263 axes.AddLineToPoint (metrics.db_label_width + data_width, metrics.height - metrics.y_origin);
264 gc->SetPen (wxPen (wxColour (0, 0, 0)));
265 gc->StrokePath (axes);
268 auto cursor = gc->CreatePath ();
269 cursor.MoveToPoint (_cursor->draw.x - _cursor_size / 2, _cursor->draw.y - _cursor_size / 2);
270 cursor.AddLineToPoint (_cursor->draw.x + _cursor_size / 2, _cursor->draw.y + _cursor_size / 2);
271 cursor.MoveToPoint (_cursor->draw.x + _cursor_size / 2, _cursor->draw.y - _cursor_size / 2);
272 cursor.AddLineToPoint (_cursor->draw.x - _cursor_size / 2, _cursor->draw.y + _cursor_size / 2);
273 gc->StrokePath (cursor);
281 AudioPlot::y_for_linear (float p, Metrics const & metrics) const
287 return metrics.height - (linear_to_db(p) - _minimum) * metrics.y_scale - metrics.y_origin;
292 AudioPlot::plot_peak (wxGraphicsPath& path, int channel, Metrics const & metrics) const
294 if (_analysis->points (channel) == 0) {
298 _peak[channel] = PointList ();
301 int const N = _analysis->points(channel);
302 for (int i = 0; i < N; ++i) {
303 float const p = get_point(channel, i)[AudioPoint::PEAK];
304 peak -= 0.01f * (1 - log10 (_smoothing) / log10 (max_smoothing));
307 } else if (peak < 0) {
311 _peak[channel].push_back (
313 wxPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (peak, metrics)),
314 DCPTime::from_frames (i * _analysis->samples_per_point(), _analysis->sample_rate()),
320 DCPOMATIC_ASSERT (_peak.find(channel) != _peak.end());
322 path.MoveToPoint (_peak[channel][0].draw);
323 for (auto const& i: _peak[channel]) {
324 path.AddLineToPoint (i.draw);
330 AudioPlot::plot_rms (wxGraphicsPath& path, int channel, Metrics const & metrics) const
332 if (_analysis->points (channel) == 0) {
336 _rms[channel] = PointList();
338 list<float> smoothing;
340 int const N = _analysis->points(channel);
342 float const first = get_point(channel, 0)[AudioPoint::RMS];
343 float const last = get_point(channel, N - 1)[AudioPoint::RMS];
345 int const before = _smoothing / 2;
346 int const after = _smoothing - before;
348 /* Pre-load the smoothing list */
349 for (int i = 0; i < before; ++i) {
350 smoothing.push_back (first);
352 for (int i = 0; i < after; ++i) {
354 smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
356 smoothing.push_back (last);
360 for (int i = 0; i < N; ++i) {
362 int const next_for_window = i + after;
364 if (next_for_window < N) {
365 smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
367 smoothing.push_back (last);
370 smoothing.pop_front ();
373 for (list<float>::const_iterator j = smoothing.begin(); j != smoothing.end(); ++j) {
377 if (!smoothing.empty ()) {
378 p = sqrt (p / smoothing.size ());
381 _rms[channel].push_back (
383 wxPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (p, metrics)),
384 DCPTime::from_frames (i * _analysis->samples_per_point(), _analysis->sample_rate()),
390 DCPOMATIC_ASSERT (_rms.find(channel) != _rms.end());
392 path.MoveToPoint (_rms[channel][0].draw);
393 for (auto const& i: _rms[channel]) {
394 path.AddLineToPoint (i.draw);
400 AudioPlot::set_smoothing (int s)
410 AudioPlot::set_gain_correction (double gain)
412 _gain_correction = gain;
418 AudioPlot::get_point (int channel, int point) const
420 auto p = _analysis->get_point (channel, point);
421 for (int i = 0; i < AudioPoint::COUNT; ++i) {
422 p[i] *= db_to_linear(_gain_correction);
429 /** @param n Channel index.
430 * @return Colour used by that channel in the plot.
433 AudioPlot::colour (int n) const
435 DCPOMATIC_ASSERT (n < int(_colours.size()));
441 AudioPlot::search (map<int, PointList> const & search, wxMouseEvent const & ev, double& min_dist, Point& min_point) const
443 for (auto const& i: search) {
444 for (auto const& j: i.second) {
445 double const dist = pow(ev.GetX() - j.draw.x, 2) + pow(ev.GetY() - j.draw.y, 2);
446 if (dist < min_dist) {
456 AudioPlot::left_down ()
459 if (auto fv = _viewer.lock()) {
460 fv->seek (_cursor->time, true);
467 AudioPlot::mouse_moved (wxMouseEvent& ev)
469 double min_dist = DBL_MAX;
472 search (_rms, ev, min_dist, min_point);
473 search (_peak, ev, min_dist, min_point);
477 if (min_dist < DBL_MAX) {
478 wxRect before (min_point.draw.x - _cursor_size / 2, min_point.draw.y - _cursor_size / 2, _cursor_size, _cursor_size);
479 GetParent()->Refresh (true, &before);
481 wxRect after (min_point.draw.x - _cursor_size / 2, min_point.draw.y - _cursor_size / 2, _cursor_size, _cursor_size);
482 GetParent()->Refresh (true, &after);
483 Cursor (min_point.time, min_point.db);
489 AudioPlot::mouse_leave (wxMouseEvent &)
493 Cursor (optional<DCPTime>(), optional<float>());