Merge master.
[dcpomatic.git] / src / wx / audio_plot.cc
1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
2
3 /*
4     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
5
6     This program 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.
10
11     This program 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.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 #include <iostream>
23 #include <boost/bind.hpp>
24 #include <wx/graphics.h>
25 #include "audio_plot.h"
26 #include "lib/audio_decoder.h"
27 #include "lib/audio_analysis.h"
28 #include "wx/wx_util.h"
29
30 using std::cout;
31 using std::vector;
32 using std::list;
33 using std::max;
34 using std::min;
35 using boost::bind;
36 using boost::shared_ptr;
37
38 int const AudioPlot::_minimum = -70;
39 int const AudioPlot::max_smoothing = 128;
40
41 AudioPlot::AudioPlot (wxWindow* parent)
42         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
43         , _gain (0)
44         , _smoothing (max_smoothing / 2)
45 {
46         SetDoubleBuffered (true);
47
48         for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
49                 _channel_visible[i] = false;
50         }
51
52         for (int i = 0; i < AudioPoint::COUNT; ++i) {
53                 _type_visible[i] = false;
54         }
55
56         _colours.push_back (wxColour (  0,   0,   0));
57         _colours.push_back (wxColour (255,   0,   0));
58         _colours.push_back (wxColour (  0, 255,   0));
59         _colours.push_back (wxColour (139,   0, 204));
60         _colours.push_back (wxColour (  0,   0, 255));
61         _colours.push_back (wxColour (100, 100, 100));
62         
63         Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (AudioPlot::paint), 0, this);
64         
65         SetMinSize (wxSize (640, 512));
66 }
67
68 void
69 AudioPlot::set_analysis (shared_ptr<AudioAnalysis> a)
70 {
71         _analysis = a;
72
73         for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
74                 _channel_visible[i] = false;
75         }
76
77         for (int i = 0; i < AudioPoint::COUNT; ++i) {
78                 _type_visible[i] = false;
79         }
80         
81         Refresh ();
82 }
83
84 void
85 AudioPlot::set_channel_visible (int c, bool v)
86 {
87         _channel_visible[c] = v;
88         Refresh ();
89 }
90
91 void
92 AudioPlot::set_type_visible (int t, bool v)
93 {
94         _type_visible[t] = v;
95         Refresh ();
96 }
97
98 void
99 AudioPlot::paint (wxPaintEvent &)
100 {
101         wxPaintDC dc (this);
102
103         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
104         if (!gc) {
105                 return;
106         }
107
108         if (!_analysis || _analysis->channels() == 0) {
109                 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
110                 gc->DrawText (_("Please wait; audio is being analysed..."), 32, 32);
111                 return;
112         }
113
114         wxGraphicsPath grid = gc->CreatePath ();
115         gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
116         wxDouble db_label_height;
117         wxDouble db_label_descent;
118         wxDouble db_label_leading;
119         gc->GetTextExtent (wxT ("-80dB"), &_db_label_width, &db_label_height, &db_label_descent, &db_label_leading);
120
121         _db_label_width += 8;
122         
123         int const data_width = GetSize().GetWidth() - _db_label_width;
124         /* Assume all channels have the same number of points */
125         _x_scale = data_width / float (_analysis->points (0));
126         _height = GetSize().GetHeight ();
127         _y_origin = 32;
128         _y_scale = (_height - _y_origin) / -_minimum;
129
130         for (int i = _minimum; i <= 0; i += 10) {
131                 int const y = (_height - (i - _minimum) * _y_scale) - _y_origin;
132                 grid.MoveToPoint (_db_label_width - 4, y);
133                 grid.AddLineToPoint (_db_label_width + data_width, y);
134                 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), 0, y - (db_label_height / 2));
135         }
136
137         gc->SetPen (*wxLIGHT_GREY_PEN);
138         gc->StrokePath (grid);
139
140         gc->DrawText (_("Time"), data_width, _height - _y_origin + db_label_height / 2);
141
142         
143         if (_type_visible[AudioPoint::PEAK]) {
144                 for (int c = 0; c < MAX_AUDIO_CHANNELS; ++c) {
145                         wxGraphicsPath p = gc->CreatePath ();
146                         if (_channel_visible[c] && c < _analysis->channels()) {
147                                 plot_peak (p, c);
148                         }
149                         wxColour const col = _colours[c];
150 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
151                         gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxPENSTYLE_SOLID));
152 #else                   
153                         gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxSOLID));
154 #endif
155                         gc->StrokePath (p);
156                 }
157         }
158
159         if (_type_visible[AudioPoint::RMS]) {
160                 for (int c = 0; c < MAX_AUDIO_CHANNELS; ++c) {
161                         wxGraphicsPath p = gc->CreatePath ();
162                         if (_channel_visible[c] && c < _analysis->channels()) {
163                                 plot_rms (p, c);
164                         }
165                         wxColour const col = _colours[c];
166 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
167                         gc->SetPen (*wxThePenList->FindOrCreatePen (col, 1, wxPENSTYLE_SOLID));
168 #else
169                         gc->SetPen (*wxThePenList->FindOrCreatePen (col, 1, wxSOLID));
170 #endif                  
171                         gc->StrokePath (p);
172                 }
173         }
174
175         wxGraphicsPath axes = gc->CreatePath ();
176         axes.MoveToPoint (_db_label_width, 0);
177         axes.AddLineToPoint (_db_label_width, _height - _y_origin);
178         axes.AddLineToPoint (_db_label_width + data_width, _height - _y_origin);
179         gc->SetPen (*wxBLACK_PEN);
180         gc->StrokePath (axes);
181
182         delete gc;
183 }
184
185 float
186 AudioPlot::y_for_linear (float p) const
187 {
188         return _height - (20 * log10(p) - _minimum + _gain) * _y_scale - _y_origin;
189 }
190
191 void
192 AudioPlot::plot_peak (wxGraphicsPath& path, int channel) const
193 {
194         path.MoveToPoint (_db_label_width, y_for_linear (_analysis->get_point(channel, 0)[AudioPoint::PEAK]));
195
196         float peak = 0;
197         int const N = _analysis->points(channel);
198         for (int i = 0; i < N; ++i) {
199                 float const p = _analysis->get_point(channel, i)[AudioPoint::PEAK];
200                 peak -= 0.01f * (1 - log10 (_smoothing) / log10 (max_smoothing));
201                 if (p > peak) {
202                         peak = p;
203                 } else if (peak < 0) {
204                         peak = 0;
205                 }
206                 
207                 path.AddLineToPoint (_db_label_width + i * _x_scale, y_for_linear (peak));
208         }
209 }
210
211 void
212 AudioPlot::plot_rms (wxGraphicsPath& path, int channel) const
213 {
214         path.MoveToPoint (_db_label_width, y_for_linear (_analysis->get_point(channel, 0)[AudioPoint::RMS]));
215
216         list<float> smoothing;
217
218         int const N = _analysis->points(channel);
219
220         float const first = _analysis->get_point(channel, 0)[AudioPoint::RMS];
221         float const last = _analysis->get_point(channel, N - 1)[AudioPoint::RMS];
222
223         int const before = _smoothing / 2;
224         int const after = _smoothing - before;
225         
226         /* Pre-load the smoothing list */
227         for (int i = 0; i < before; ++i) {
228                 smoothing.push_back (first);
229         }
230         for (int i = 0; i < after; ++i) {
231                 if (i < N) {
232                         smoothing.push_back (_analysis->get_point(channel, i)[AudioPoint::RMS]);
233                 } else {
234                         smoothing.push_back (last);
235                 }
236         }
237         
238         for (int i = 0; i < N; ++i) {
239
240                 int const next_for_window = i + after;
241
242                 if (next_for_window < N) {
243                         smoothing.push_back (_analysis->get_point(channel, i)[AudioPoint::RMS]);
244                 } else {
245                         smoothing.push_back (last);
246                 }
247
248                 smoothing.pop_front ();
249
250                 float p = 0;
251                 for (list<float>::const_iterator j = smoothing.begin(); j != smoothing.end(); ++j) {
252                         p += pow (*j, 2);
253                 }
254
255                 p = sqrt (p / smoothing.size ());
256
257                 path.AddLineToPoint (_db_label_width + i * _x_scale, y_for_linear (p));
258         }
259 }
260
261 void
262 AudioPlot::set_gain (float g)
263 {
264         _gain = g;
265         Refresh ();
266 }
267
268 void
269 AudioPlot::set_smoothing (int s)
270 {
271         _smoothing = s;
272         Refresh ();
273 }