2 Copyright (C) 2013 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.
21 #include <boost/bind.hpp>
22 #include <wx/graphics.h>
23 #include "audio_plot.h"
24 #include "lib/audio_decoder.h"
25 #include "lib/audio_analysis.h"
26 #include "wx/wx_util.h"
34 using boost::shared_ptr;
36 int const AudioPlot::_minimum = -70;
37 int const AudioPlot::max_smoothing = 128;
39 AudioPlot::AudioPlot (wxWindow* parent)
40 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
42 , _smoothing (max_smoothing / 2)
43 , _message (_("Please wait; audio is being analysed..."))
46 SetDoubleBuffered (true);
49 for (int i = 0; i < MAX_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 (100, 100, 100));
64 Bind (wxEVT_PAINT, boost::bind (&AudioPlot::paint, this));
66 SetMinSize (wxSize (640, 512));
70 AudioPlot::set_analysis (shared_ptr<AudioAnalysis> a)
74 for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
75 _channel_visible[i] = false;
78 for (int i = 0; i < AudioPoint::COUNT; ++i) {
79 _type_visible[i] = false;
86 AudioPlot::set_channel_visible (int c, bool v)
88 _channel_visible[c] = v;
93 AudioPlot::set_type_visible (int t, bool v)
100 AudioPlot::set_message (wxString s)
111 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
116 if (!_analysis || _analysis->channels() == 0) {
117 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
118 gc->DrawText (_message, 32, 32);
122 wxGraphicsPath grid = gc->CreatePath ();
123 gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
124 wxDouble db_label_height;
125 wxDouble db_label_descent;
126 wxDouble db_label_leading;
127 gc->GetTextExtent (wxT ("-80dB"), &_db_label_width, &db_label_height, &db_label_descent, &db_label_leading);
129 _db_label_width += 8;
131 int const data_width = GetSize().GetWidth() - _db_label_width;
132 /* Assume all channels have the same number of points */
133 _x_scale = data_width / float (_analysis->points (0));
134 _height = GetSize().GetHeight ();
136 _y_scale = (_height - _y_origin) / -_minimum;
138 for (int i = _minimum; i <= 0; i += 10) {
139 int const y = (_height - (i - _minimum) * _y_scale) - _y_origin;
140 grid.MoveToPoint (_db_label_width - 4, y);
141 grid.AddLineToPoint (_db_label_width + data_width, y);
142 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), 0, y - (db_label_height / 2));
145 gc->SetPen (*wxLIGHT_GREY_PEN);
146 gc->StrokePath (grid);
148 gc->DrawText (_("DCPTime"), data_width, _height - _y_origin + db_label_height / 2);
150 if (_type_visible[AudioPoint::PEAK]) {
151 for (int c = 0; c < MAX_AUDIO_CHANNELS; ++c) {
152 wxGraphicsPath p = gc->CreatePath ();
153 if (_channel_visible[c] && c < _analysis->channels()) {
156 wxColour const col = _colours[c];
157 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxPENSTYLE_SOLID));
162 if (_type_visible[AudioPoint::RMS]) {
163 for (int c = 0; c < MAX_AUDIO_CHANNELS; ++c) {
164 wxGraphicsPath p = gc->CreatePath ();
165 if (_channel_visible[c] && c < _analysis->channels()) {
168 wxColour const col = _colours[c];
169 gc->SetPen (*wxThePenList->FindOrCreatePen (col, 1, wxPENSTYLE_SOLID));
174 wxGraphicsPath axes = gc->CreatePath ();
175 axes.MoveToPoint (_db_label_width, 0);
176 axes.AddLineToPoint (_db_label_width, _height - _y_origin);
177 axes.AddLineToPoint (_db_label_width + data_width, _height - _y_origin);
178 gc->SetPen (*wxBLACK_PEN);
179 gc->StrokePath (axes);
185 AudioPlot::y_for_linear (float p) const
187 return _height - (20 * log10(p) - _minimum + _gain) * _y_scale - _y_origin;
191 AudioPlot::plot_peak (wxGraphicsPath& path, int channel) const
193 if (_analysis->points (channel) == 0) {
197 path.MoveToPoint (_db_label_width, y_for_linear (_analysis->get_point(channel, 0)[AudioPoint::PEAK]));
200 int const N = _analysis->points(channel);
201 for (int i = 0; i < N; ++i) {
202 float const p = _analysis->get_point(channel, i)[AudioPoint::PEAK];
203 peak -= 0.01f * (1 - log10 (_smoothing) / log10 (max_smoothing));
206 } else if (peak < 0) {
210 path.AddLineToPoint (_db_label_width + i * _x_scale, y_for_linear (peak));
215 AudioPlot::plot_rms (wxGraphicsPath& path, int channel) const
217 if (_analysis->points (channel) == 0) {
221 path.MoveToPoint (_db_label_width, y_for_linear (_analysis->get_point(channel, 0)[AudioPoint::RMS]));
223 list<float> smoothing;
225 int const N = _analysis->points(channel);
227 float const first = _analysis->get_point(channel, 0)[AudioPoint::RMS];
228 float const last = _analysis->get_point(channel, N - 1)[AudioPoint::RMS];
230 int const before = _smoothing / 2;
231 int const after = _smoothing - before;
233 /* Pre-load the smoothing list */
234 for (int i = 0; i < before; ++i) {
235 smoothing.push_back (first);
237 for (int i = 0; i < after; ++i) {
239 smoothing.push_back (_analysis->get_point(channel, i)[AudioPoint::RMS]);
241 smoothing.push_back (last);
245 for (int i = 0; i < N; ++i) {
247 int const next_for_window = i + after;
249 if (next_for_window < N) {
250 smoothing.push_back (_analysis->get_point(channel, i)[AudioPoint::RMS]);
252 smoothing.push_back (last);
255 smoothing.pop_front ();
258 for (list<float>::const_iterator j = smoothing.begin(); j != smoothing.end(); ++j) {
262 if (smoothing.size() > 0) {
263 p = sqrt (p / smoothing.size ());
266 path.AddLineToPoint (_db_label_width + i * _x_scale, y_for_linear (p));
271 AudioPlot::set_gain (float g)
278 AudioPlot::set_smoothing (int s)