Maybe de-flicker the audio display a bit on windows.
[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::max;
32 using std::min;
33 using boost::bind;
34 using boost::shared_ptr;
35
36 int const AudioPlot::_minimum = -90;
37
38 AudioPlot::AudioPlot (wxWindow* parent)
39         : wxPanel (parent)
40         , _gain (0)
41 {
42         SetDoubleBuffered (true);
43 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
44         SetBackgroundStyle (wxBG_STYLE_PAINT);
45 #endif  
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) {
108                 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
109                 gc->DrawText (_("Please wait; audio is being analysed..."), 32, 32);
110                 return;
111         }
112         
113         int const width = GetSize().GetWidth();
114         /* Assume all channels have the same number of points */
115         float const xs = width / float (_analysis->points (0));
116         int const height = GetSize().GetHeight ();
117         float const ys = height / -_minimum;
118
119         wxGraphicsPath grid = gc->CreatePath ();
120         gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
121         for (int i = _minimum; i <= 0; i += 10) {
122                 int const y = height - (i - _minimum) * ys;
123                 grid.MoveToPoint (0, y);
124                 grid.AddLineToPoint (width, y);
125                 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), width - 32, y - 12);
126         }
127         gc->SetPen (*wxLIGHT_GREY_PEN);
128         gc->StrokePath (grid);
129
130         for (int c = 0; c < MAX_AUDIO_CHANNELS; ++c) {
131                 if (!_channel_visible[c] || c >= _analysis->channels()) {
132                         continue;
133                 }
134
135                 wxGraphicsPath path[AudioPoint::COUNT];
136                 
137                 for (int i = 0; i < AudioPoint::COUNT; ++i) {
138                         if (!_type_visible[i]) {
139                                 continue;
140                         }
141                         
142                         path[i] = gc->CreatePath ();
143                         path[i].MoveToPoint (0, height - (max (_analysis->get_point(c, 0)[i], float (_minimum)) - _minimum + _gain) * ys);
144                 }
145
146                 for (int i = 0; i < _analysis->points(c); ++i) {
147                         for (int j = 0; j < AudioPoint::COUNT; ++j) {
148                                 if (!_type_visible[j]) {
149                                         continue;
150                                 }
151                                 
152                                 path[j].AddLineToPoint (i * xs, height - (max (_analysis->get_point(c, i)[j], float (_minimum)) - _minimum + _gain) * ys);
153                         }
154                 }
155
156                 wxColour const col = _colours[c];
157
158                 if (_type_visible[AudioPoint::RMS]) {
159                         gc->SetPen (*wxThePenList->FindOrCreatePen (col));
160                         gc->StrokePath (path[AudioPoint::RMS]);
161                 }
162
163                 if (_type_visible[AudioPoint::PEAK]) {
164                         gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2)));
165                         gc->StrokePath (path[AudioPoint::PEAK]);
166                 }
167         }
168
169         delete gc;
170 }
171
172 void
173 AudioPlot::set_gain (float g)
174 {
175         _gain = g;
176         Refresh ();
177 }