Basic UI.
[dcpomatic.git] / src / wx / audio_plot.cc
1 #include <iostream>
2 #include <boost/bind.hpp>
3 #include <wx/graphics.h>
4 #include "audio_plot.h"
5 #include "lib/decoder_factory.h"
6 #include "lib/audio_decoder.h"
7 #include "lib/audio_analysis.h"
8 #include "wx/wx_util.h"
9
10 using std::cout;
11 using std::vector;
12 using std::max;
13 using std::min;
14 using boost::bind;
15 using boost::shared_ptr;
16
17 AudioPlot::AudioPlot (wxWindow* parent, shared_ptr<AudioAnalysis> a, int c)
18         : wxPanel (parent)
19         , _analysis (a)
20         , _channel (c)
21 {
22         Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (AudioPlot::paint), 0, this);
23
24         SetMinSize (wxSize (640, 512));
25 }
26
27 void
28 AudioPlot::paint (wxPaintEvent &)
29 {
30         wxPaintDC dc (this);
31         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
32         if (!gc) {
33                 return;
34         }
35
36         int const width = GetSize().GetWidth();
37         float const xs = width / float (_analysis->points (_channel));
38         int const height = GetSize().GetHeight ();
39         float const ys = height / 60;
40
41         wxGraphicsPath grid = gc->CreatePath ();
42         gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
43         for (int i = -60; i <= 0; i += 10) {
44                 int const y = height - (i + 60) * ys;
45                 grid.MoveToPoint (0, y);
46                 grid.AddLineToPoint (width, y);
47                 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), width - 32, y - 12);
48         }
49         gc->SetPen (*wxLIGHT_GREY_PEN);
50         gc->StrokePath (grid);
51
52         wxGraphicsPath path[AudioPoint::COUNT];
53
54         for (int i = 0; i < AudioPoint::COUNT; ++i) {
55                 path[i] = gc->CreatePath ();
56                 path[i].MoveToPoint (0, height - (max (_analysis->get_point(_channel, 0)[i], -60.0f) + 60) * ys);
57         }
58
59         for (int i = 0; i < _analysis->points(_channel); ++i) {
60                 for (int j = 0; j < AudioPoint::COUNT; ++j) {
61                         path[j].AddLineToPoint (i * xs, height - (max (_analysis->get_point(_channel, i)[j], -60.0f) + 60) * ys);
62                 }
63         }
64
65         gc->SetPen (*wxBLUE_PEN);
66         gc->StrokePath (path[AudioPoint::RMS]);
67
68         gc->SetPen (*wxRED_PEN);
69         gc->StrokePath (path[AudioPoint::PEAK]);
70
71         delete gc;
72 }