2 Copyright (C) 2013-2015 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>
36 using boost::shared_ptr;
38 int const AudioPlot::_minimum = -70;
39 int const AudioPlot::max_smoothing = 128;
41 AudioPlot::AudioPlot (wxWindow* parent)
42 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
43 , _smoothing (max_smoothing / 2)
44 , _gain_correction (0)
47 SetDoubleBuffered (true);
50 for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
51 _channel_visible[i] = false;
54 for (int i = 0; i < AudioPoint::COUNT; ++i) {
55 _type_visible[i] = false;
58 _colours.push_back (wxColour ( 0, 0, 0));
59 _colours.push_back (wxColour (255, 0, 0));
60 _colours.push_back (wxColour ( 0, 255, 0));
61 _colours.push_back (wxColour (139, 0, 204));
62 _colours.push_back (wxColour ( 0, 0, 255));
63 _colours.push_back (wxColour ( 0, 139, 0));
64 _colours.push_back (wxColour ( 0, 0, 139));
65 _colours.push_back (wxColour (255, 255, 0));
66 _colours.push_back (wxColour ( 0, 255, 255));
67 _colours.push_back (wxColour (255, 0, 255));
68 _colours.push_back (wxColour (255, 0, 139));
69 _colours.push_back (wxColour (139, 0, 255));
71 _colours.push_back (wxColour (139, 139, 255));
72 _colours.push_back (wxColour ( 0, 139, 255));
73 _colours.push_back (wxColour (255, 139, 139));
74 _colours.push_back (wxColour (255, 139, 0));
76 set_analysis (shared_ptr<AudioAnalysis> ());
78 #if MAX_DCP_AUDIO_CHANNELS != 16
79 #warning AudioPlot::AudioPlot is expecting the wrong MAX_DCP_AUDIO_CHANNELS
82 Bind (wxEVT_PAINT, boost::bind (&AudioPlot::paint, this));
84 SetMinSize (wxSize (640, 512));
88 AudioPlot::set_analysis (shared_ptr<AudioAnalysis> a)
93 _message = _("Please wait; audio is being analysed...");
100 AudioPlot::set_channel_visible (int c, bool v)
102 _channel_visible[c] = v;
107 AudioPlot::set_type_visible (int t, bool v)
109 _type_visible[t] = v;
114 AudioPlot::set_message (wxString s)
122 double db_label_width;
125 float x_scale; ///< pixels per data point
134 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
139 if (!_analysis || _analysis->channels() == 0) {
140 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
141 gc->DrawText (_message, 32, 32);
145 wxGraphicsPath h_grid = gc->CreatePath ();
146 gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
147 wxDouble db_label_height;
148 wxDouble db_label_descent;
149 wxDouble db_label_leading;
151 gc->GetTextExtent (wxT ("-80dB"), &metrics.db_label_width, &db_label_height, &db_label_descent, &db_label_leading);
153 metrics.db_label_width += 8;
155 int const data_width = GetSize().GetWidth() - metrics.db_label_width;
156 /* Assume all channels have the same number of points */
157 metrics.x_scale = data_width / float (_analysis->points (0));
158 metrics.height = GetSize().GetHeight ();
159 metrics.y_origin = 32;
160 metrics.y_scale = (metrics.height - metrics.y_origin) / -_minimum;
162 for (int i = _minimum; i <= 0; i += 10) {
163 int const y = (metrics.height - (i - _minimum) * metrics.y_scale) - metrics.y_origin;
164 h_grid.MoveToPoint (metrics.db_label_width - 4, y);
165 h_grid.AddLineToPoint (metrics.db_label_width + data_width, y);
166 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), 0, y - (db_label_height / 2));
169 gc->SetPen (wxPen (wxColour (200, 200, 200)));
170 gc->StrokePath (h_grid);
172 /* Draw an x axis with marks */
174 wxGraphicsPath v_grid = gc->CreatePath ();
176 DCPOMATIC_ASSERT (_analysis->samples_per_point() != 0.0);
177 double const pps = _analysis->sample_rate() * metrics.x_scale / _analysis->samples_per_point();
179 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
181 double const mark_interval = calculate_mark_interval (rint (128 / pps));
183 DCPTime t = DCPTime::from_seconds (mark_interval);
184 while ((t.seconds() * pps) < data_width) {
185 double tc = t.seconds ();
186 int const h = tc / 3600;
188 int const m = tc / 60;
192 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
195 wxDouble str_descent;
196 wxDouble str_leading;
197 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
199 int const tx = llrintf (metrics.db_label_width + t.seconds() * pps);
200 gc->DrawText (str, tx - str_width / 2, metrics.height - metrics.y_origin + db_label_height);
202 v_grid.MoveToPoint (tx, metrics.height - metrics.y_origin + 4);
203 v_grid.AddLineToPoint (tx, metrics.y_origin);
205 t += DCPTime::from_seconds (mark_interval);
208 gc->SetPen (wxPen (wxColour (200, 200, 200)));
209 gc->StrokePath (v_grid);
211 if (_type_visible[AudioPoint::PEAK]) {
212 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
213 wxGraphicsPath p = gc->CreatePath ();
214 if (_channel_visible[c] && c < _analysis->channels()) {
215 plot_peak (p, c, metrics);
217 wxColour const col = _colours[c];
218 gc->SetPen (wxPen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxPENSTYLE_SOLID));
223 if (_type_visible[AudioPoint::RMS]) {
224 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
225 wxGraphicsPath p = gc->CreatePath ();
226 if (_channel_visible[c] && c < _analysis->channels()) {
227 plot_rms (p, c, metrics);
229 wxColour const col = _colours[c];
230 gc->SetPen (wxPen (col, 1, wxPENSTYLE_SOLID));
235 wxGraphicsPath axes = gc->CreatePath ();
236 axes.MoveToPoint (metrics.db_label_width, 0);
237 axes.AddLineToPoint (metrics.db_label_width, metrics.height - metrics.y_origin);
238 axes.AddLineToPoint (metrics.db_label_width + data_width, metrics.height - metrics.y_origin);
239 gc->SetPen (wxPen (wxColour (0, 0, 0)));
240 gc->StrokePath (axes);
246 AudioPlot::y_for_linear (float p, Metrics const & metrics) const
252 return metrics.height - (20 * log10(p) - _minimum) * metrics.y_scale - metrics.y_origin;
256 AudioPlot::plot_peak (wxGraphicsPath& path, int channel, Metrics const & metrics) const
258 if (_analysis->points (channel) == 0) {
262 _peak[channel] = PointList ();
265 int const N = _analysis->points(channel);
266 for (int i = 0; i < N; ++i) {
267 float const p = get_point(channel, i)[AudioPoint::PEAK];
268 peak -= 0.01f * (1 - log10 (_smoothing) / log10 (max_smoothing));
271 } else if (peak < 0) {
275 _peak[channel].push_back (
277 wxPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (peak, metrics)),
278 DCPTime::from_frames (i * _analysis->samples_per_point(), _analysis->sample_rate()),
284 DCPOMATIC_ASSERT (_peak.find(channel) != _peak.end());
286 path.MoveToPoint (_peak[channel][0].draw);
287 BOOST_FOREACH (Point const & i, _peak[channel]) {
288 path.AddLineToPoint (i.draw);
293 AudioPlot::plot_rms (wxGraphicsPath& path, int channel, Metrics const & metrics) const
295 if (_analysis->points (channel) == 0) {
299 _rms[channel] = PointList();
301 list<float> smoothing;
303 int const N = _analysis->points(channel);
305 float const first = get_point(channel, 0)[AudioPoint::RMS];
306 float const last = get_point(channel, N - 1)[AudioPoint::RMS];
308 int const before = _smoothing / 2;
309 int const after = _smoothing - before;
311 /* Pre-load the smoothing list */
312 for (int i = 0; i < before; ++i) {
313 smoothing.push_back (first);
315 for (int i = 0; i < after; ++i) {
317 smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
319 smoothing.push_back (last);
323 for (int i = 0; i < N; ++i) {
325 int const next_for_window = i + after;
327 if (next_for_window < N) {
328 smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
330 smoothing.push_back (last);
333 smoothing.pop_front ();
336 for (list<float>::const_iterator j = smoothing.begin(); j != smoothing.end(); ++j) {
340 if (!smoothing.empty ()) {
341 p = sqrt (p / smoothing.size ());
344 _rms[channel].push_back (
346 wxPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (p, metrics)),
347 DCPTime::from_frames (i * _analysis->samples_per_point(), _analysis->sample_rate()),
353 DCPOMATIC_ASSERT (_rms.find(channel) != _rms.end());
355 path.MoveToPoint (_rms[channel][0].draw);
356 BOOST_FOREACH (Point const & i, _rms[channel]) {
357 path.AddLineToPoint (i.draw);
362 AudioPlot::set_smoothing (int s)
371 AudioPlot::set_gain_correction (double gain)
373 _gain_correction = gain;
378 AudioPlot::get_point (int channel, int point) const
380 AudioPoint p = _analysis->get_point (channel, point);
381 for (int i = 0; i < AudioPoint::COUNT; ++i) {
382 p[i] *= pow (10, _gain_correction / 20);
388 /** @param n Channel index.
389 * @return Colour used by that channel in the plot.
392 AudioPlot::colour (int n) const
394 DCPOMATIC_ASSERT (n < int(_colours.size()));