Tidy up creation of analysis a bit.
[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         , _channel (0)
41         , _gain (0)
42 {
43         Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (AudioPlot::paint), 0, this);
44
45         SetMinSize (wxSize (640, 512));
46 }
47
48 void
49 AudioPlot::set_analysis (shared_ptr<AudioAnalysis> a)
50 {
51         _analysis = a;
52         _channel = 0;
53         Refresh ();
54 }
55
56 void
57 AudioPlot::set_channel (int c)
58 {
59         _channel = c;
60         Refresh ();
61 }
62
63 void
64 AudioPlot::paint (wxPaintEvent &)
65 {
66         wxPaintDC dc (this);
67
68         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
69         if (!gc) {
70                 return;
71         }
72
73         if (!_analysis) {
74                 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
75                 gc->DrawText (_("Please wait; audio is being analysed..."), 32, 32);
76                 return;
77         }
78         
79         int const width = GetSize().GetWidth();
80         float const xs = width / float (_analysis->points (_channel));
81         int const height = GetSize().GetHeight ();
82         float const ys = height / -_minimum;
83
84         wxGraphicsPath grid = gc->CreatePath ();
85         gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
86         for (int i = _minimum; i <= 0; i += 10) {
87                 int const y = height - (i - _minimum) * ys;
88                 grid.MoveToPoint (0, y);
89                 grid.AddLineToPoint (width, y);
90                 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), width - 32, y - 12);
91         }
92         gc->SetPen (*wxLIGHT_GREY_PEN);
93         gc->StrokePath (grid);
94
95         wxGraphicsPath path[AudioPoint::COUNT];
96
97         for (int i = 0; i < AudioPoint::COUNT; ++i) {
98                 path[i] = gc->CreatePath ();
99                 path[i].MoveToPoint (0, height - (max (_analysis->get_point(_channel, 0)[i], float (_minimum)) - _minimum + _gain) * ys);
100         }
101
102         for (int i = 0; i < _analysis->points(_channel); ++i) {
103                 for (int j = 0; j < AudioPoint::COUNT; ++j) {
104                         path[j].AddLineToPoint (i * xs, height - (max (_analysis->get_point(_channel, i)[j], float (_minimum)) - _minimum + _gain) * ys);
105                 }
106         }
107
108         gc->SetPen (*wxBLUE_PEN);
109         gc->StrokePath (path[AudioPoint::RMS]);
110
111         gc->SetPen (*wxRED_PEN);
112         gc->StrokePath (path[AudioPoint::PEAK]);
113
114         delete gc;
115 }
116
117 void
118 AudioPlot::set_gain (float g)
119 {
120         _gain = g;
121         Refresh ();
122 }