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