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