2 Copyright (C) 2013-2018 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/>.
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>
38 using boost::optional;
39 using boost::shared_ptr;
41 int const AudioPlot::_minimum = -70;
42 int const AudioPlot::_cursor_size = 8;
43 int const AudioPlot::max_smoothing = 128;
45 AudioPlot::AudioPlot (wxWindow* parent)
46 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
47 , _smoothing (max_smoothing / 2)
48 , _gain_correction (0)
51 SetDoubleBuffered (true);
54 for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
55 _channel_visible[i] = false;
58 for (int i = 0; i < AudioPoint::COUNT; ++i) {
59 _type_visible[i] = false;
62 _colours.push_back (wxColour ( 0, 0, 0));
63 _colours.push_back (wxColour (255, 0, 0));
64 _colours.push_back (wxColour ( 0, 255, 0));
65 _colours.push_back (wxColour (139, 0, 204));
66 _colours.push_back (wxColour ( 0, 0, 255));
67 _colours.push_back (wxColour ( 0, 139, 0));
68 _colours.push_back (wxColour ( 0, 0, 139));
69 _colours.push_back (wxColour (255, 255, 0));
70 _colours.push_back (wxColour ( 0, 255, 255));
71 _colours.push_back (wxColour (255, 0, 255));
72 _colours.push_back (wxColour (255, 0, 139));
73 _colours.push_back (wxColour (139, 0, 255));
75 _colours.push_back (wxColour (139, 139, 255));
76 _colours.push_back (wxColour ( 0, 139, 255));
77 _colours.push_back (wxColour (255, 139, 139));
78 _colours.push_back (wxColour (255, 139, 0));
80 set_analysis (shared_ptr<AudioAnalysis> ());
82 #if MAX_DCP_AUDIO_CHANNELS != 16
83 #warning AudioPlot::AudioPlot is expecting the wrong MAX_DCP_AUDIO_CHANNELS
86 Bind (wxEVT_PAINT, boost::bind (&AudioPlot::paint, this));
87 Bind (wxEVT_MOTION, boost::bind (&AudioPlot::mouse_moved, this, _1));
88 Bind (wxEVT_LEAVE_WINDOW, boost::bind (&AudioPlot::mouse_leave, this, _1));
90 SetMinSize (wxSize (640, 512));
94 AudioPlot::set_analysis (shared_ptr<AudioAnalysis> a)
99 _message = _("Please wait; audio is being analysed...");
106 AudioPlot::set_channel_visible (int c, bool v)
108 _channel_visible[c] = v;
113 AudioPlot::set_type_visible (int t, bool v)
115 _type_visible[t] = v;
120 AudioPlot::set_message (wxString s)
128 double db_label_width;
131 float x_scale; ///< pixels per data point
140 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
145 if (!_analysis || _analysis->channels() == 0) {
146 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
147 gc->DrawText (_message, 32, 32);
151 wxGraphicsPath h_grid = gc->CreatePath ();
152 gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
153 wxDouble db_label_height;
154 wxDouble db_label_descent;
155 wxDouble db_label_leading;
157 gc->GetTextExtent (wxT ("-80dB"), &metrics.db_label_width, &db_label_height, &db_label_descent, &db_label_leading);
159 metrics.db_label_width += 8;
161 int const data_width = GetSize().GetWidth() - metrics.db_label_width;
162 /* Assume all channels have the same number of points */
163 metrics.x_scale = data_width / float (_analysis->points (0));
164 metrics.height = GetSize().GetHeight ();
165 metrics.y_origin = 32;
166 metrics.y_scale = (metrics.height - metrics.y_origin) / -_minimum;
168 for (int i = _minimum; i <= 0; i += 10) {
169 int const y = (metrics.height - (i - _minimum) * metrics.y_scale) - metrics.y_origin;
170 h_grid.MoveToPoint (metrics.db_label_width - 4, y);
171 h_grid.AddLineToPoint (metrics.db_label_width + data_width, y);
172 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), 0, y - (db_label_height / 2));
175 gc->SetPen (wxPen (wxColour (200, 200, 200)));
176 gc->StrokePath (h_grid);
178 /* Draw an x axis with marks */
180 wxGraphicsPath v_grid = gc->CreatePath ();
182 DCPOMATIC_ASSERT (_analysis->samples_per_point() != 0.0);
183 double const pps = _analysis->sample_rate() * metrics.x_scale / _analysis->samples_per_point();
185 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
187 double const mark_interval = calculate_mark_interval (rint (128 / pps));
189 DCPTime t = DCPTime::from_seconds (mark_interval);
190 while ((t.seconds() * pps) < data_width) {
191 double tc = t.seconds ();
192 int const h = tc / 3600;
194 int const m = tc / 60;
198 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
201 wxDouble str_descent;
202 wxDouble str_leading;
203 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
205 int const tx = llrintf (metrics.db_label_width + t.seconds() * pps);
206 gc->DrawText (str, tx - str_width / 2, metrics.height - metrics.y_origin + db_label_height);
208 v_grid.MoveToPoint (tx, metrics.height - metrics.y_origin + 4);
209 v_grid.AddLineToPoint (tx, metrics.y_origin);
211 t += DCPTime::from_seconds (mark_interval);
214 gc->SetPen (wxPen (wxColour (200, 200, 200)));
215 gc->StrokePath (v_grid);
217 if (_type_visible[AudioPoint::PEAK]) {
218 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
219 wxGraphicsPath p = gc->CreatePath ();
220 if (_channel_visible[c] && c < _analysis->channels()) {
221 plot_peak (p, c, metrics);
223 wxColour const col = _colours[c];
224 gc->SetPen (wxPen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxPENSTYLE_SOLID));
229 if (_type_visible[AudioPoint::RMS]) {
230 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
231 wxGraphicsPath p = gc->CreatePath ();
232 if (_channel_visible[c] && c < _analysis->channels()) {
233 plot_rms (p, c, metrics);
235 wxColour const col = _colours[c];
236 gc->SetPen (wxPen (col, 1, wxPENSTYLE_SOLID));
241 wxGraphicsPath axes = gc->CreatePath ();
242 axes.MoveToPoint (metrics.db_label_width, 0);
243 axes.AddLineToPoint (metrics.db_label_width, metrics.height - metrics.y_origin);
244 axes.AddLineToPoint (metrics.db_label_width + data_width, metrics.height - metrics.y_origin);
245 gc->SetPen (wxPen (wxColour (0, 0, 0)));
246 gc->StrokePath (axes);
249 wxGraphicsPath cursor = gc->CreatePath ();
250 cursor.MoveToPoint (_cursor->draw.x - _cursor_size / 2, _cursor->draw.y - _cursor_size / 2);
251 cursor.AddLineToPoint (_cursor->draw.x + _cursor_size / 2, _cursor->draw.y + _cursor_size / 2);
252 cursor.MoveToPoint (_cursor->draw.x + _cursor_size / 2, _cursor->draw.y - _cursor_size / 2);
253 cursor.AddLineToPoint (_cursor->draw.x - _cursor_size / 2, _cursor->draw.y + _cursor_size / 2);
254 gc->StrokePath (cursor);
263 AudioPlot::y_for_linear (float p, Metrics const & metrics) const
269 return metrics.height - (20 * log10(p) - _minimum) * metrics.y_scale - metrics.y_origin;
273 AudioPlot::plot_peak (wxGraphicsPath& path, int channel, Metrics const & metrics) const
275 if (_analysis->points (channel) == 0) {
279 _peak[channel] = PointList ();
282 int const N = _analysis->points(channel);
283 for (int i = 0; i < N; ++i) {
284 float const p = get_point(channel, i)[AudioPoint::PEAK];
285 peak -= 0.01f * (1 - log10 (_smoothing) / log10 (max_smoothing));
288 } else if (peak < 0) {
292 _peak[channel].push_back (
294 wxPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (peak, metrics)),
295 DCPTime::from_frames (i * _analysis->samples_per_point(), _analysis->sample_rate()),
301 DCPOMATIC_ASSERT (_peak.find(channel) != _peak.end());
303 path.MoveToPoint (_peak[channel][0].draw);
304 BOOST_FOREACH (Point const & i, _peak[channel]) {
305 path.AddLineToPoint (i.draw);
310 AudioPlot::plot_rms (wxGraphicsPath& path, int channel, Metrics const & metrics) const
312 if (_analysis->points (channel) == 0) {
316 _rms[channel] = PointList();
318 list<float> smoothing;
320 int const N = _analysis->points(channel);
322 float const first = get_point(channel, 0)[AudioPoint::RMS];
323 float const last = get_point(channel, N - 1)[AudioPoint::RMS];
325 int const before = _smoothing / 2;
326 int const after = _smoothing - before;
328 /* Pre-load the smoothing list */
329 for (int i = 0; i < before; ++i) {
330 smoothing.push_back (first);
332 for (int i = 0; i < after; ++i) {
334 smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
336 smoothing.push_back (last);
340 for (int i = 0; i < N; ++i) {
342 int const next_for_window = i + after;
344 if (next_for_window < N) {
345 smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
347 smoothing.push_back (last);
350 smoothing.pop_front ();
353 for (list<float>::const_iterator j = smoothing.begin(); j != smoothing.end(); ++j) {
357 if (!smoothing.empty ()) {
358 p = sqrt (p / smoothing.size ());
361 _rms[channel].push_back (
363 wxPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (p, metrics)),
364 DCPTime::from_frames (i * _analysis->samples_per_point(), _analysis->sample_rate()),
370 DCPOMATIC_ASSERT (_rms.find(channel) != _rms.end());
372 path.MoveToPoint (_rms[channel][0].draw);
373 BOOST_FOREACH (Point const & i, _rms[channel]) {
374 path.AddLineToPoint (i.draw);
379 AudioPlot::set_smoothing (int s)
388 AudioPlot::set_gain_correction (double gain)
390 _gain_correction = gain;
395 AudioPlot::get_point (int channel, int point) const
397 AudioPoint p = _analysis->get_point (channel, point);
398 for (int i = 0; i < AudioPoint::COUNT; ++i) {
399 p[i] *= pow (10, _gain_correction / 20);
405 /** @param n Channel index.
406 * @return Colour used by that channel in the plot.
409 AudioPlot::colour (int n) const
411 DCPOMATIC_ASSERT (n < int(_colours.size()));
416 AudioPlot::search (map<int, PointList> const & search, wxMouseEvent const & ev, double& min_dist, Point& min_point) const
418 for (map<int, PointList>::const_iterator i = search.begin(); i != search.end(); ++i) {
419 BOOST_FOREACH (Point const & j, i->second) {
420 double const dist = pow(ev.GetX() - j.draw.x, 2) + pow(ev.GetY() - j.draw.y, 2);
421 if (dist < min_dist) {
430 AudioPlot::mouse_moved (wxMouseEvent& ev)
432 double min_dist = DBL_MAX;
435 search (_rms, ev, min_dist, min_point);
436 search (_peak, ev, min_dist, min_point);
438 _cursor = optional<Point> ();
440 if (min_dist < DBL_MAX) {
441 wxRect before (min_point.draw.x - _cursor_size / 2, min_point.draw.y - _cursor_size / 2, _cursor_size, _cursor_size);
442 GetParent()->Refresh (true, &before);
444 wxRect after (min_point.draw.x - _cursor_size / 2, min_point.draw.y - _cursor_size / 2, _cursor_size, _cursor_size);
445 GetParent()->Refresh (true, &after);
446 Cursor (min_point.time, min_point.db);
451 AudioPlot::mouse_leave (wxMouseEvent &)
453 _cursor = optional<Point> ();
455 Cursor (optional<DCPTime>(), optional<float>());