Pretty dumb smoothing.
[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
39 AudioPlot::AudioPlot (wxWindow* parent)
40         : wxPanel (parent)
41         , _gain (0)
42         , _smoothing (1)
43 {
44         SetDoubleBuffered (true);
45
46         for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
47                 _channel_visible[i] = false;
48         }
49
50         for (int i = 0; i < AudioPoint::COUNT; ++i) {
51                 _type_visible[i] = false;
52         }
53
54         _colours.push_back (wxColour (  0,   0,   0));
55         _colours.push_back (wxColour (255,   0,   0));
56         _colours.push_back (wxColour (  0, 255,   0));
57         _colours.push_back (wxColour (139,   0, 204));
58         _colours.push_back (wxColour (  0,   0, 255));
59         _colours.push_back (wxColour (100, 100, 100));
60         
61         Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (AudioPlot::paint), 0, this);
62         
63         SetMinSize (wxSize (640, 512));
64 }
65
66 void
67 AudioPlot::set_analysis (shared_ptr<AudioAnalysis> a)
68 {
69         _analysis = a;
70
71         for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
72                 _channel_visible[i] = false;
73         }
74
75         for (int i = 0; i < AudioPoint::COUNT; ++i) {
76                 _type_visible[i] = false;
77         }
78         
79         Refresh ();
80 }
81
82 void
83 AudioPlot::set_channel_visible (int c, bool v)
84 {
85         _channel_visible[c] = v;
86         Refresh ();
87 }
88
89 void
90 AudioPlot::set_type_visible (int t, bool v)
91 {
92         _type_visible[t] = v;
93         Refresh ();
94 }
95
96 void
97 AudioPlot::paint (wxPaintEvent &)
98 {
99         wxPaintDC dc (this);
100
101         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
102         if (!gc) {
103                 return;
104         }
105
106         if (!_analysis || _analysis->channels() == 0) {
107                 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
108                 gc->DrawText (_("Please wait; audio is being analysed..."), 32, 32);
109                 return;
110         }
111
112         wxGraphicsPath grid = gc->CreatePath ();
113         gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
114         wxDouble db_label_width;
115         wxDouble db_label_height;
116         wxDouble db_label_descent;
117         wxDouble db_label_leading;
118         gc->GetTextExtent (_("-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         float const xs = data_width / float (_analysis->points (0));
125         int const height = GetSize().GetHeight ();
126         int const yo = 32;
127         float const ys = (height - yo) / -_minimum;
128
129         for (int i = _minimum; i <= 0; i += 10) {
130                 int const y = (height - (i - _minimum) * ys) - yo;
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 - yo + db_label_height / 2);
140
141         for (int c = 0; c < MAX_AUDIO_CHANNELS; ++c) {
142                 if (!_channel_visible[c] || c >= _analysis->channels()) {
143                         continue;
144                 }
145
146                 wxGraphicsPath path[AudioPoint::COUNT];
147                 
148                 for (int i = 0; i < AudioPoint::COUNT; ++i) {
149                         if (!_type_visible[i]) {
150                                 continue;
151                         }
152                         
153                         path[i] = gc->CreatePath ();
154
155                         float const val = 20 * log10 (_analysis->get_point(c, 0)[i]);
156                         
157                         path[i].MoveToPoint (
158                                 db_label_width,
159                                 height - (max (val, float (_minimum)) - _minimum + _gain) * ys - yo
160                                 );
161                 }
162
163                 list<float> smoothing[AudioPoint::COUNT];
164
165                 for (int i = 0; i < _analysis->points(c); ++i) {
166                         for (int j = 0; j < AudioPoint::COUNT; ++j) {
167                                 if (!_type_visible[j]) {
168                                         continue;
169                                 }
170
171                                 smoothing[j].push_back (_analysis->get_point(c, i)[j]);
172                                 if (int(smoothing[j].size()) > _smoothing) {
173                                         smoothing[j].pop_front ();
174                                 }
175
176                                 float const val = 20 * log10 (_analysis->smooth (smoothing[j], static_cast<AudioPoint::Type> (j)));
177                                 
178                                 path[j].AddLineToPoint (
179                                         i * xs + db_label_width,
180                                         height - (max (val, float (_minimum)) - _minimum + _gain) * ys - yo
181                                         );
182                         }
183                 }
184
185                 wxColour const col = _colours[c];
186
187                 if (_type_visible[AudioPoint::RMS]) {
188                         gc->SetPen (*wxThePenList->FindOrCreatePen (col));
189                         gc->StrokePath (path[AudioPoint::RMS]);
190                 }
191
192                 if (_type_visible[AudioPoint::PEAK]) {
193                         gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2)));
194                         gc->StrokePath (path[AudioPoint::PEAK]);
195                 }
196         }
197
198         wxGraphicsPath axes = gc->CreatePath ();
199         axes.MoveToPoint (db_label_width, 0);
200         axes.AddLineToPoint (db_label_width, height - yo);
201         axes.AddLineToPoint (db_label_width + data_width, height - yo);
202         gc->SetPen (*wxBLACK_PEN);
203         gc->StrokePath (axes);
204
205         delete gc;
206 }
207
208 void
209 AudioPlot::set_gain (float g)
210 {
211         _gain = g;
212         Refresh ();
213 }
214
215 void
216 AudioPlot::set_smoothing (int s)
217 {
218         _smoothing = s;
219         Refresh ();
220 }