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