2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "audio_plot.h"
21 #include "lib/audio_decoder.h"
22 #include "lib/audio_analysis.h"
23 #include "lib/compose.hpp"
24 #include "wx/wx_util.h"
25 #include <wx/graphics.h>
26 #include <boost/bind.hpp>
35 using boost::shared_ptr;
37 int const AudioPlot::_minimum = -70;
38 int const AudioPlot::max_smoothing = 128;
40 AudioPlot::AudioPlot (wxWindow* parent)
41 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
42 , _smoothing (max_smoothing / 2)
43 , _gain_correction (0)
46 SetDoubleBuffered (true);
49 for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
50 _channel_visible[i] = false;
53 for (int i = 0; i < AudioPoint::COUNT; ++i) {
54 _type_visible[i] = false;
57 _colours.push_back (wxColour ( 0, 0, 0));
58 _colours.push_back (wxColour (255, 0, 0));
59 _colours.push_back (wxColour ( 0, 255, 0));
60 _colours.push_back (wxColour (139, 0, 204));
61 _colours.push_back (wxColour ( 0, 0, 255));
62 _colours.push_back (wxColour ( 0, 139, 0));
63 _colours.push_back (wxColour ( 0, 0, 139));
64 _colours.push_back (wxColour (255, 255, 0));
65 _colours.push_back (wxColour ( 0, 255, 255));
66 _colours.push_back (wxColour (255, 0, 255));
67 _colours.push_back (wxColour (255, 0, 139));
68 _colours.push_back (wxColour (139, 0, 255));
70 set_analysis (shared_ptr<AudioAnalysis> ());
72 #if MAX_DCP_AUDIO_CHANNELS != 12
73 #warning AudioPlot::AudioPlot is expecting the wrong MAX_DCP_AUDIO_CHANNELS
76 Bind (wxEVT_PAINT, boost::bind (&AudioPlot::paint, this));
78 SetMinSize (wxSize (640, 512));
82 AudioPlot::set_analysis (shared_ptr<AudioAnalysis> a)
87 _message = _("Please wait; audio is being analysed...");
94 AudioPlot::set_channel_visible (int c, bool v)
96 _channel_visible[c] = v;
101 AudioPlot::set_type_visible (int t, bool v)
103 _type_visible[t] = v;
108 AudioPlot::set_message (wxString s)
116 double db_label_width;
128 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
133 if (!_analysis || _analysis->channels() == 0) {
134 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
135 gc->DrawText (_message, 32, 32);
139 wxGraphicsPath grid = gc->CreatePath ();
140 gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
141 wxDouble db_label_height;
142 wxDouble db_label_descent;
143 wxDouble db_label_leading;
145 gc->GetTextExtent (wxT ("-80dB"), &metrics.db_label_width, &db_label_height, &db_label_descent, &db_label_leading);
147 metrics.db_label_width += 8;
149 int const data_width = GetSize().GetWidth() - metrics.db_label_width;
150 /* Assume all channels have the same number of points */
151 metrics.x_scale = data_width / float (_analysis->points (0));
152 metrics.height = GetSize().GetHeight ();
153 metrics.y_origin = 32;
154 metrics.y_scale = (metrics.height - metrics.y_origin) / -_minimum;
156 for (int i = _minimum; i <= 0; i += 10) {
157 int const y = (metrics.height - (i - _minimum) * metrics.y_scale) - metrics.y_origin;
158 grid.MoveToPoint (metrics.db_label_width - 4, y);
159 grid.AddLineToPoint (metrics.db_label_width + data_width, y);
160 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), 0, y - (db_label_height / 2));
163 gc->SetPen (wxPen (wxColour (200, 200, 200)));
164 gc->StrokePath (grid);
166 gc->DrawText (_("Time"), data_width, metrics.height - metrics.y_origin + db_label_height / 2);
168 if (_type_visible[AudioPoint::PEAK]) {
169 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
170 wxGraphicsPath p = gc->CreatePath ();
171 if (_channel_visible[c] && c < _analysis->channels()) {
172 plot_peak (p, c, metrics);
174 wxColour const col = _colours[c];
175 gc->SetPen (wxPen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxPENSTYLE_SOLID));
180 if (_type_visible[AudioPoint::RMS]) {
181 for (int c = 0; c < MAX_DCP_AUDIO_CHANNELS; ++c) {
182 wxGraphicsPath p = gc->CreatePath ();
183 if (_channel_visible[c] && c < _analysis->channels()) {
184 plot_rms (p, c, metrics);
186 wxColour const col = _colours[c];
187 gc->SetPen (wxPen (col, 1, wxPENSTYLE_SOLID));
192 wxGraphicsPath axes = gc->CreatePath ();
193 axes.MoveToPoint (metrics.db_label_width, 0);
194 axes.AddLineToPoint (metrics.db_label_width, metrics.height - metrics.y_origin);
195 axes.AddLineToPoint (metrics.db_label_width + data_width, metrics.height - metrics.y_origin);
196 gc->SetPen (wxPen (wxColour (0, 0, 0)));
197 gc->StrokePath (axes);
203 AudioPlot::y_for_linear (float p, Metrics const & metrics) const
209 return metrics.height - (20 * log10(p) - _minimum) * metrics.y_scale - metrics.y_origin;
213 AudioPlot::plot_peak (wxGraphicsPath& path, int channel, Metrics const & metrics) const
215 if (_analysis->points (channel) == 0) {
219 path.MoveToPoint (metrics.db_label_width, y_for_linear (get_point(channel, 0)[AudioPoint::PEAK], metrics));
222 int const N = _analysis->points(channel);
223 for (int i = 0; i < N; ++i) {
224 float const p = get_point(channel, i)[AudioPoint::PEAK];
225 peak -= 0.01f * (1 - log10 (_smoothing) / log10 (max_smoothing));
228 } else if (peak < 0) {
232 path.AddLineToPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (peak, metrics));
237 AudioPlot::plot_rms (wxGraphicsPath& path, int channel, Metrics const & metrics) const
239 if (_analysis->points (channel) == 0) {
243 path.MoveToPoint (metrics.db_label_width, y_for_linear (get_point(channel, 0)[AudioPoint::RMS], metrics));
245 list<float> smoothing;
247 int const N = _analysis->points(channel);
249 float const first = get_point(channel, 0)[AudioPoint::RMS];
250 float const last = get_point(channel, N - 1)[AudioPoint::RMS];
252 int const before = _smoothing / 2;
253 int const after = _smoothing - before;
255 /* Pre-load the smoothing list */
256 for (int i = 0; i < before; ++i) {
257 smoothing.push_back (first);
259 for (int i = 0; i < after; ++i) {
261 smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
263 smoothing.push_back (last);
267 for (int i = 0; i < N; ++i) {
269 int const next_for_window = i + after;
271 if (next_for_window < N) {
272 smoothing.push_back (get_point(channel, i)[AudioPoint::RMS]);
274 smoothing.push_back (last);
277 smoothing.pop_front ();
280 for (list<float>::const_iterator j = smoothing.begin(); j != smoothing.end(); ++j) {
284 if (!smoothing.empty ()) {
285 p = sqrt (p / smoothing.size ());
288 path.AddLineToPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (p, metrics));
293 AudioPlot::set_smoothing (int s)
300 AudioPlot::set_gain_correction (double gain)
302 _gain_correction = gain;
307 AudioPlot::get_point (int channel, int point) const
309 AudioPoint p = _analysis->get_point (channel, point);
310 for (int i = 0; i < AudioPoint::COUNT; ++i) {
311 p[i] *= pow (10, _gain_correction / 20);