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"
23 #include "film_viewer.h"
25 #include "lib/audio_decoder.h"
26 #include "lib/audio_analysis.h"
27 #include "lib/compose.hpp"
28 #include "lib/maths_util.h"
29 #include <wx/graphics.h>
30 #include <boost/bind/bind.hpp>
40 using std::shared_ptr;
44 using boost::optional;
45 #if BOOST_VERSION >= 106100
46 using namespace boost::placeholders;
48 using namespace dcpomatic;
51 int const AudioPlot::_minimum = -70;
52 int const AudioPlot::_cursor_size = 8;
53 int const AudioPlot::max_smoothing = 128;
56 AudioPlot::AudioPlot (wxWindow* parent, weak_ptr<FilmViewer> viewer)
57 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
59 , _smoothing (max_smoothing / 2)
60 , _gain_correction (0)
63 SetDoubleBuffered (true);
66 for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
67 _channel_visible[i] = false;
70 for (int i = 0; i < AudioPoint::COUNT; ++i) {
71 _type_visible[i] = false;
74 _colours.push_back (wxColour ( 0, 0, 0));
75 _colours.push_back (wxColour (255, 0, 0));
76 _colours.push_back (wxColour ( 0, 255, 0));
77 _colours.push_back (wxColour (139, 0, 204));
78 _colours.push_back (wxColour ( 0, 0, 255));
79 _colours.push_back (wxColour ( 0, 139, 0));
80 _colours.push_back (wxColour ( 0, 0, 139));
81 _colours.push_back (wxColour (255, 255, 0));
82 _colours.push_back (wxColour ( 0, 255, 255));
83 _colours.push_back (wxColour (255, 0, 255));
84 _colours.push_back (wxColour (255, 0, 139));
85 _colours.push_back (wxColour (139, 0, 255));
87 _colours.push_back (wxColour (139, 139, 255));
88 _colours.push_back (wxColour ( 0, 139, 255));
89 _colours.push_back (wxColour (255, 139, 139));
90 _colours.push_back (wxColour (255, 139, 0));
92 set_analysis (shared_ptr<AudioAnalysis> ());
94 #if MAX_DCP_AUDIO_CHANNELS != 16
95 #warning AudioPlot::AudioPlot is expecting the wrong MAX_DCP_AUDIO_CHANNELS
98 Bind (wxEVT_PAINT, boost::bind (&AudioPlot::paint, this));
99 Bind (wxEVT_MOTION, boost::bind (&AudioPlot::mouse_moved, this, _1));
100 Bind (wxEVT_LEAVE_WINDOW, boost::bind (&AudioPlot::mouse_leave, this, _1));
101 Bind (wxEVT_LEFT_DOWN, boost::bind(&AudioPlot::left_down, this));
103 SetMinSize (wxSize (640, 512));
108 AudioPlot::set_analysis (shared_ptr<AudioAnalysis> a)
113 _message = _("Please wait; audio is being analysed...");
121 AudioPlot::set_channel_visible (int c, bool v)
123 _channel_visible[c] = v;
129 AudioPlot::set_type_visible (int t, bool v)
131 _type_visible[t] = v;
137 AudioPlot::set_message (wxString s)
146 double db_label_width;
149 float x_scale; ///< pixels per data point
159 auto gc = wxGraphicsContext::Create (dc);
164 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
166 if (!_analysis || _analysis->channels() == 0) {
167 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
168 gc->DrawText (_message, 32, 32);
173 auto h_grid = gc->CreatePath ();
174 gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
175 wxDouble db_label_height;
176 wxDouble db_label_descent;
177 wxDouble db_label_leading;
179 gc->GetTextExtent (wxT ("-80dB"), &metrics.db_label_width, &db_label_height, &db_label_descent, &db_label_leading);
181 metrics.db_label_width += 8;
183 int const data_width = GetSize().GetWidth() - metrics.db_label_width;
184 /* Assume all channels have the same number of points */
185 metrics.x_scale = data_width / float (_analysis->points (0));
186 metrics.height = GetSize().GetHeight ();
187 metrics.y_origin = 32;
188 metrics.y_scale = (metrics.height - metrics.y_origin) / -_minimum;
190 for (int i = _minimum; i <= 0; i += 10) {
191 int const y = (metrics.height - (i - _minimum) * metrics.y_scale) - metrics.y_origin;
192 h_grid.MoveToPoint (metrics.db_label_width - 4, y);
193 h_grid.AddLineToPoint (metrics.db_label_width + data_width, y);
194 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), 0, y - (db_label_height / 2));
197 gc->SetPen (wxPen (wxColour (200, 200, 200)));
198 gc->StrokePath (h_grid);
200 /* Draw an x axis with marks */
202 auto v_grid = gc->CreatePath ();
204 DCPOMATIC_ASSERT (_analysis->samples_per_point() != 0.0);
205 double const pps = _analysis->sample_rate() * metrics.x_scale / _analysis->samples_per_point();
207 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
209 double const mark_interval = calculate_mark_interval (rint (128 / pps));
211 auto t = DCPTime::from_seconds (mark_interval);
212 while ((t.seconds() * pps) < data_width) {
213 double tc = t.seconds ();
214 int const h = tc / 3600;
216 int const m = tc / 60;
220 auto str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
223 wxDouble str_descent;
224 wxDouble str_leading;
225 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
227 int const tx = llrintf (metrics.db_label_width + t.seconds() * pps);
228 gc->DrawText (str, tx - str_width / 2, metrics.height - metrics.y_origin + db_label_height);
230 v_grid.MoveToPoint (tx, metrics.height - metrics.y_origin + 4);
231 v_grid.AddLineToPoint (tx, metrics.y_origin);
233 t += DCPTime::from_seconds (mark_interval);
236 gc->SetPen (wxPen (wxColour (200, 200, 200)));
237 gc->StrokePath (v_grid);
239 if (_type_visible[AudioPoint::PEAK]) {
240 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
241 auto p = gc->CreatePath ();
242 if (_channel_visible[c] && c < _analysis->channels()) {
243 plot_peak (p, c, metrics);
245 auto const col = _colours[c];
246 gc->SetPen (wxPen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxPENSTYLE_SOLID));
251 if (_type_visible[AudioPoint::RMS]) {
252 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
253 auto p = gc->CreatePath ();
254 if (_channel_visible[c] && c < _analysis->channels()) {
255 plot_rms (p, c, metrics);
257 auto const col = _colours[c];
258 gc->SetPen (wxPen (col, 1, wxPENSTYLE_SOLID));
263 auto axes = gc->CreatePath ();
264 axes.MoveToPoint (metrics.db_label_width, 0);
265 axes.AddLineToPoint (metrics.db_label_width, metrics.height - metrics.y_origin);
266 axes.AddLineToPoint (metrics.db_label_width + data_width, metrics.height - metrics.y_origin);
267 gc->SetPen (wxPen (wxColour (0, 0, 0)));
268 gc->StrokePath (axes);
271 auto cursor = gc->CreatePath ();
272 cursor.MoveToPoint (_cursor->draw.x - _cursor_size / 2, _cursor->draw.y - _cursor_size / 2);
273 cursor.AddLineToPoint (_cursor->draw.x + _cursor_size / 2, _cursor->draw.y + _cursor_size / 2);
274 cursor.MoveToPoint (_cursor->draw.x + _cursor_size / 2, _cursor->draw.y - _cursor_size / 2);
275 cursor.AddLineToPoint (_cursor->draw.x - _cursor_size / 2, _cursor->draw.y + _cursor_size / 2);
276 gc->StrokePath (cursor);
284 AudioPlot::y_for_linear (float p, Metrics const & metrics) const
290 return metrics.height - (linear_to_db(p) - _minimum) * metrics.y_scale - metrics.y_origin;
295 AudioPlot::plot_peak (wxGraphicsPath& path, int channel, Metrics const & metrics) const
297 if (_analysis->points (channel) == 0) {
301 _peak[channel] = PointList ();
304 int const N = _analysis->points(channel);
305 for (int i = 0; i < N; ++i) {
306 float const p = get_point(channel, i)[AudioPoint::PEAK];
307 peak -= 0.01f * (1 - log10 (_smoothing) / log10 (max_smoothing));
310 } else if (peak < 0) {
314 _peak[channel].push_back (
316 wxPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (peak, metrics)),
317 DCPTime::from_frames (i * _analysis->samples_per_point(), _analysis->sample_rate()),
323 DCPOMATIC_ASSERT (_peak.find(channel) != _peak.end());
325 path.MoveToPoint (_peak[channel][0].draw);
326 for (auto const& i: _peak[channel]) {
327 path.AddLineToPoint (i.draw);
333 AudioPlot::plot_rms (wxGraphicsPath& path, int channel, Metrics const & metrics) const
335 if (_analysis->points (channel) == 0) {
339 _rms[channel] = PointList();
341 list<float> smoothing;
343 int const N = _analysis->points(channel);
345 float const first = get_point(channel, 0)[AudioPoint::RMS];
346 float const last = get_point(channel, N - 1)[AudioPoint::RMS];
348 int const before = _smoothing / 2;
349 int const after = _smoothing - before;
351 /* Pre-load the smoothing list */
352 for (int i = 0; i < before; ++i) {
353 smoothing.push_back (first);
355 for (int i = 0; i < after; ++i) {
357 smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
359 smoothing.push_back (last);
363 for (int i = 0; i < N; ++i) {
365 int const next_for_window = i + after;
367 if (next_for_window < N) {
368 smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
370 smoothing.push_back (last);
373 smoothing.pop_front ();
376 for (list<float>::const_iterator j = smoothing.begin(); j != smoothing.end(); ++j) {
380 if (!smoothing.empty ()) {
381 p = sqrt (p / smoothing.size ());
384 _rms[channel].push_back (
386 wxPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (p, metrics)),
387 DCPTime::from_frames (i * _analysis->samples_per_point(), _analysis->sample_rate()),
393 DCPOMATIC_ASSERT (_rms.find(channel) != _rms.end());
395 path.MoveToPoint (_rms[channel][0].draw);
396 for (auto const& i: _rms[channel]) {
397 path.AddLineToPoint (i.draw);
403 AudioPlot::set_smoothing (int s)
413 AudioPlot::set_gain_correction (double gain)
415 _gain_correction = gain;
421 AudioPlot::get_point (int channel, int point) const
423 auto p = _analysis->get_point (channel, point);
424 for (int i = 0; i < AudioPoint::COUNT; ++i) {
425 p[i] *= db_to_linear(_gain_correction);
432 /** @param n Channel index.
433 * @return Colour used by that channel in the plot.
436 AudioPlot::colour (int n) const
438 DCPOMATIC_ASSERT (n < int(_colours.size()));
444 AudioPlot::left_down ()
447 if (auto fv = _viewer.lock()) {
448 fv->seek (_cursor->time, true);
455 AudioPlot::mouse_moved (wxMouseEvent& ev)
457 double min_dist = DBL_MAX;
460 auto search = [this](map<int, PointList> const & search, wxMouseEvent const & ev, double& min_dist, Point& min_point) {
461 for (auto const& i: search) {
462 if (_channel_visible[i.first]) {
463 for (auto const& j: i.second) {
464 double const dist = pow(ev.GetX() - j.draw.x, 2) + pow(ev.GetY() - j.draw.y, 2);
465 if (dist < min_dist) {
474 if (_type_visible[AudioPoint::RMS]) {
475 search (_rms, ev, min_dist, min_point);
477 if (_type_visible[AudioPoint::PEAK]) {
478 search (_peak, ev, min_dist, min_point);
481 _cursor = boost::none;
483 if (min_dist < DBL_MAX) {
484 wxRect before (min_point.draw.x - _cursor_size / 2, min_point.draw.y - _cursor_size / 2, _cursor_size, _cursor_size);
485 GetParent()->Refresh (true, &before);
487 wxRect after (min_point.draw.x - _cursor_size / 2, min_point.draw.y - _cursor_size / 2, _cursor_size, _cursor_size);
488 GetParent()->Refresh (true, &after);
489 Cursor (min_point.time, min_point.db);
495 AudioPlot::mouse_leave (wxMouseEvent &)
497 _cursor = boost::none;
499 Cursor (optional<DCPTime>(), optional<float>());