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;
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 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 grid.MoveToPoint (metrics.db_label_width - 4, y);
165 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 (grid);
172 gc->DrawText (_("Time"), data_width, metrics.height - metrics.y_origin + db_label_height / 2);
174 if (_type_visible[AudioPoint::PEAK]) {
175 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
176 wxGraphicsPath p = gc->CreatePath ();
177 if (_channel_visible[c] && c < _analysis->channels()) {
178 plot_peak (p, c, metrics);
180 wxColour const col = _colours[c];
181 gc->SetPen (wxPen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxPENSTYLE_SOLID));
186 if (_type_visible[AudioPoint::RMS]) {
187 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
188 wxGraphicsPath p = gc->CreatePath ();
189 if (_channel_visible[c] && c < _analysis->channels()) {
190 plot_rms (p, c, metrics);
192 wxColour const col = _colours[c];
193 gc->SetPen (wxPen (col, 1, wxPENSTYLE_SOLID));
198 wxGraphicsPath axes = gc->CreatePath ();
199 axes.MoveToPoint (metrics.db_label_width, 0);
200 axes.AddLineToPoint (metrics.db_label_width, metrics.height - metrics.y_origin);
201 axes.AddLineToPoint (metrics.db_label_width + data_width, metrics.height - metrics.y_origin);
202 gc->SetPen (wxPen (wxColour (0, 0, 0)));
203 gc->StrokePath (axes);
209 AudioPlot::y_for_linear (float p, Metrics const & metrics) const
215 return metrics.height - (20 * log10(p) - _minimum) * metrics.y_scale - metrics.y_origin;
219 AudioPlot::plot_peak (wxGraphicsPath& path, int channel, Metrics const & metrics) const
221 if (_analysis->points (channel) == 0) {
225 path.MoveToPoint (metrics.db_label_width, y_for_linear (get_point(channel, 0)[AudioPoint::PEAK], metrics));
228 int const N = _analysis->points(channel);
229 for (int i = 0; i < N; ++i) {
230 float const p = get_point(channel, i)[AudioPoint::PEAK];
231 peak -= 0.01f * (1 - log10 (_smoothing) / log10 (max_smoothing));
234 } else if (peak < 0) {
238 path.AddLineToPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (peak, metrics));
243 AudioPlot::plot_rms (wxGraphicsPath& path, int channel, Metrics const & metrics) const
245 if (_analysis->points (channel) == 0) {
249 path.MoveToPoint (metrics.db_label_width, y_for_linear (get_point(channel, 0)[AudioPoint::RMS], metrics));
251 list<float> smoothing;
253 int const N = _analysis->points(channel);
255 float const first = get_point(channel, 0)[AudioPoint::RMS];
256 float const last = get_point(channel, N - 1)[AudioPoint::RMS];
258 int const before = _smoothing / 2;
259 int const after = _smoothing - before;
261 /* Pre-load the smoothing list */
262 for (int i = 0; i < before; ++i) {
263 smoothing.push_back (first);
265 for (int i = 0; i < after; ++i) {
267 smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
269 smoothing.push_back (last);
273 for (int i = 0; i < N; ++i) {
275 int const next_for_window = i + after;
277 if (next_for_window < N) {
278 smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
280 smoothing.push_back (last);
283 smoothing.pop_front ();
286 for (list<float>::const_iterator j = smoothing.begin(); j != smoothing.end(); ++j) {
290 if (!smoothing.empty ()) {
291 p = sqrt (p / smoothing.size ());
294 path.AddLineToPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (p, metrics));
299 AudioPlot::set_smoothing (int s)
306 AudioPlot::set_gain_correction (double gain)
308 _gain_correction = gain;
313 AudioPlot::get_point (int channel, int point) const
315 AudioPoint p = _analysis->get_point (channel, point);
316 for (int i = 0; i < AudioPoint::COUNT; ++i) {
317 p[i] *= pow (10, _gain_correction / 20);