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