2 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
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.
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.
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.
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"
34 using boost::shared_ptr;
36 int const AudioPlot::_minimum = -90;
38 AudioPlot::AudioPlot (wxWindow* parent)
43 Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (AudioPlot::paint), 0, this);
45 SetMinSize (wxSize (640, 512));
49 AudioPlot::set_analysis (shared_ptr<AudioAnalysis> a)
57 AudioPlot::set_channel (int c)
64 AudioPlot::paint (wxPaintEvent &)
72 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
77 int const width = GetSize().GetWidth();
78 float const xs = width / float (_analysis->points (_channel));
79 int const height = GetSize().GetHeight ();
80 float const ys = height / -_minimum;
82 wxGraphicsPath grid = gc->CreatePath ();
83 gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
84 for (int i = _minimum; i <= 0; i += 10) {
85 int const y = height - (i - _minimum) * ys;
86 grid.MoveToPoint (0, y);
87 grid.AddLineToPoint (width, y);
88 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), width - 32, y - 12);
90 gc->SetPen (*wxLIGHT_GREY_PEN);
91 gc->StrokePath (grid);
93 wxGraphicsPath path[AudioPoint::COUNT];
95 for (int i = 0; i < AudioPoint::COUNT; ++i) {
96 path[i] = gc->CreatePath ();
97 path[i].MoveToPoint (0, height - (max (_analysis->get_point(_channel, 0)[i], float (_minimum)) - _minimum + _gain) * ys);
100 for (int i = 0; i < _analysis->points(_channel); ++i) {
101 for (int j = 0; j < AudioPoint::COUNT; ++j) {
102 path[j].AddLineToPoint (i * xs, height - (max (_analysis->get_point(_channel, i)[j], float (_minimum)) - _minimum + _gain) * ys);
106 gc->SetPen (*wxBLUE_PEN);
107 gc->StrokePath (path[AudioPoint::RMS]);
109 gc->SetPen (*wxRED_PEN);
110 gc->StrokePath (path[AudioPoint::PEAK]);
116 AudioPlot::set_gain (float g)