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 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
165 if (!_analysis || _analysis->channels() == 0) {
166 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
167 gc->DrawText (_message, 32, 32);
172 auto h_grid = gc->CreatePath ();
173 gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
174 wxDouble db_label_height;
175 wxDouble db_label_descent;
176 wxDouble db_label_leading;
178 gc->GetTextExtent (wxT ("-80dB"), &metrics.db_label_width, &db_label_height, &db_label_descent, &db_label_leading);
180 metrics.db_label_width += 8;
182 int const data_width = GetSize().GetWidth() - metrics.db_label_width;
183 /* Assume all channels have the same number of points */
184 metrics.x_scale = data_width / float (_analysis->points (0));
185 metrics.height = GetSize().GetHeight ();
186 metrics.y_origin = 32;
187 metrics.y_scale = (metrics.height - metrics.y_origin) / -_minimum;
189 for (int i = _minimum; i <= 0; i += 10) {
190 int const y = (metrics.height - (i - _minimum) * metrics.y_scale) - metrics.y_origin;
191 h_grid.MoveToPoint (metrics.db_label_width - 4, y);
192 h_grid.AddLineToPoint (metrics.db_label_width + data_width, y);
193 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), 0, y - (db_label_height / 2));
196 gc->SetPen (wxPen (wxColour (200, 200, 200)));
197 gc->StrokePath (h_grid);
199 /* Draw an x axis with marks */
201 auto v_grid = gc->CreatePath ();
203 DCPOMATIC_ASSERT (_analysis->samples_per_point() != 0.0);
204 double const pps = _analysis->sample_rate() * metrics.x_scale / _analysis->samples_per_point();
206 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
208 double const mark_interval = calculate_mark_interval (rint (128 / pps));
210 auto t = DCPTime::from_seconds (mark_interval);
211 while ((t.seconds() * pps) < data_width) {
212 double tc = t.seconds ();
213 int const h = tc / 3600;
215 int const m = tc / 60;
219 auto str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
222 wxDouble str_descent;
223 wxDouble str_leading;
224 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
226 int const tx = llrintf (metrics.db_label_width + t.seconds() * pps);
227 gc->DrawText (str, tx - str_width / 2, metrics.height - metrics.y_origin + db_label_height);
229 v_grid.MoveToPoint (tx, metrics.height - metrics.y_origin + 4);
230 v_grid.AddLineToPoint (tx, metrics.y_origin);
232 t += DCPTime::from_seconds (mark_interval);
235 gc->SetPen (wxPen (wxColour (200, 200, 200)));
236 gc->StrokePath (v_grid);
238 if (_type_visible[AudioPoint::PEAK]) {
239 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
240 auto p = gc->CreatePath ();
241 if (_channel_visible[c] && c < _analysis->channels()) {
242 plot_peak (p, c, metrics);
244 auto const col = _colours[c];
245 gc->SetPen (wxPen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxPENSTYLE_SOLID));
250 if (_type_visible[AudioPoint::RMS]) {
251 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
252 auto p = gc->CreatePath ();
253 if (_channel_visible[c] && c < _analysis->channels()) {
254 plot_rms (p, c, metrics);
256 auto const col = _colours[c];
257 gc->SetPen (wxPen (col, 1, wxPENSTYLE_SOLID));
262 auto axes = gc->CreatePath ();
263 axes.MoveToPoint (metrics.db_label_width, 0);
264 axes.AddLineToPoint (metrics.db_label_width, metrics.height - metrics.y_origin);
265 axes.AddLineToPoint (metrics.db_label_width + data_width, metrics.height - metrics.y_origin);
266 gc->SetPen (wxPen (wxColour (0, 0, 0)));
267 gc->StrokePath (axes);
270 auto cursor = gc->CreatePath ();
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 cursor.MoveToPoint (_cursor->draw.x + _cursor_size / 2, _cursor->draw.y - _cursor_size / 2);
274 cursor.AddLineToPoint (_cursor->draw.x - _cursor_size / 2, _cursor->draw.y + _cursor_size / 2);
275 gc->StrokePath (cursor);
283 AudioPlot::y_for_linear (float p, Metrics const & metrics) const
289 return metrics.height - (linear_to_db(p) - _minimum) * metrics.y_scale - metrics.y_origin;
294 AudioPlot::plot_peak (wxGraphicsPath& path, int channel, Metrics const & metrics) const
296 if (_analysis->points (channel) == 0) {
300 _peak[channel] = PointList ();
303 int const N = _analysis->points(channel);
304 for (int i = 0; i < N; ++i) {
305 float const p = get_point(channel, i)[AudioPoint::PEAK];
306 peak -= 0.01f * (1 - log10 (_smoothing) / log10 (max_smoothing));
309 } else if (peak < 0) {
313 _peak[channel].push_back (
315 wxPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (peak, metrics)),
316 DCPTime::from_frames (i * _analysis->samples_per_point(), _analysis->sample_rate()),
322 DCPOMATIC_ASSERT (_peak.find(channel) != _peak.end());
324 path.MoveToPoint (_peak[channel][0].draw);
325 for (auto const& i: _peak[channel]) {
326 path.AddLineToPoint (i.draw);
332 AudioPlot::plot_rms (wxGraphicsPath& path, int channel, Metrics const & metrics) const
334 if (_analysis->points (channel) == 0) {
338 _rms[channel] = PointList();
340 list<float> smoothing;
342 int const N = _analysis->points(channel);
344 float const first = get_point(channel, 0)[AudioPoint::RMS];
345 float const last = get_point(channel, N - 1)[AudioPoint::RMS];
347 int const before = _smoothing / 2;
348 int const after = _smoothing - before;
350 /* Pre-load the smoothing list */
351 for (int i = 0; i < before; ++i) {
352 smoothing.push_back (first);
354 for (int i = 0; i < after; ++i) {
356 smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
358 smoothing.push_back (last);
362 for (int i = 0; i < N; ++i) {
364 int const next_for_window = i + after;
366 if (next_for_window < N) {
367 smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
369 smoothing.push_back (last);
372 smoothing.pop_front ();
375 for (list<float>::const_iterator j = smoothing.begin(); j != smoothing.end(); ++j) {
379 if (!smoothing.empty ()) {
380 p = sqrt (p / smoothing.size ());
383 _rms[channel].push_back (
385 wxPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (p, metrics)),
386 DCPTime::from_frames (i * _analysis->samples_per_point(), _analysis->sample_rate()),
392 DCPOMATIC_ASSERT (_rms.find(channel) != _rms.end());
394 path.MoveToPoint (_rms[channel][0].draw);
395 for (auto const& i: _rms[channel]) {
396 path.AddLineToPoint (i.draw);
402 AudioPlot::set_smoothing (int s)
412 AudioPlot::set_gain_correction (double gain)
414 _gain_correction = gain;
420 AudioPlot::get_point (int channel, int point) const
422 auto p = _analysis->get_point (channel, point);
423 for (int i = 0; i < AudioPoint::COUNT; ++i) {
424 p[i] *= db_to_linear(_gain_correction);
431 /** @param n Channel index.
432 * @return Colour used by that channel in the plot.
435 AudioPlot::colour (int n) const
437 DCPOMATIC_ASSERT (n < int(_colours.size()));
443 AudioPlot::left_down ()
446 if (auto fv = _viewer.lock()) {
447 fv->seek (_cursor->time, true);
454 AudioPlot::mouse_moved (wxMouseEvent& ev)
456 double min_dist = DBL_MAX;
459 auto search = [this](map<int, PointList> const & search, wxMouseEvent const & ev, double& min_dist, Point& min_point) {
460 for (auto const& i: search) {
461 if (_channel_visible[i.first]) {
462 for (auto const& j: i.second) {
463 double const dist = pow(ev.GetX() - j.draw.x, 2) + pow(ev.GetY() - j.draw.y, 2);
464 if (dist < min_dist) {
473 if (_type_visible[AudioPoint::RMS]) {
474 search (_rms, ev, min_dist, min_point);
476 if (_type_visible[AudioPoint::PEAK]) {
477 search (_peak, ev, min_dist, min_point);
482 if (min_dist < DBL_MAX) {
483 wxRect before (min_point.draw.x - _cursor_size / 2, min_point.draw.y - _cursor_size / 2, _cursor_size, _cursor_size);
484 GetParent()->Refresh (true, &before);
486 wxRect after (min_point.draw.x - _cursor_size / 2, min_point.draw.y - _cursor_size / 2, _cursor_size, _cursor_size);
487 GetParent()->Refresh (true, &after);
488 Cursor (min_point.time, min_point.db);
494 AudioPlot::mouse_leave (wxMouseEvent &)
498 Cursor (optional<DCPTime>(), optional<float>());