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