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)
42 SetDoubleBuffered (true);
44 for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
45 _channel_visible[i] = false;
48 for (int i = 0; i < AudioPoint::COUNT; ++i) {
49 _type_visible[i] = false;
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));
59 Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (AudioPlot::paint), 0, this);
61 SetMinSize (wxSize (640, 512));
65 AudioPlot::set_analysis (shared_ptr<AudioAnalysis> a)
69 for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
70 _channel_visible[i] = false;
73 for (int i = 0; i < AudioPoint::COUNT; ++i) {
74 _type_visible[i] = false;
81 AudioPlot::set_channel_visible (int c, bool v)
83 _channel_visible[c] = v;
88 AudioPlot::set_type_visible (int t, bool v)
95 AudioPlot::paint (wxPaintEvent &)
99 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
105 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
106 gc->DrawText (_("Please wait; audio is being analysed..."), 32, 32);
110 int const width = GetSize().GetWidth();
111 /* Assume all channels have the same number of points */
112 float const xs = width / float (_analysis->points (0));
113 int const height = GetSize().GetHeight ();
114 float const ys = height / -_minimum;
115 int const border = 8;
117 wxGraphicsPath grid = gc->CreatePath ();
118 gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
119 for (int i = _minimum; i <= 0; i += 10) {
120 int const y = height - (i - _minimum) * ys;
121 grid.MoveToPoint (0, y);
122 grid.AddLineToPoint (width, y);
123 gc->DrawText (std_to_wx (String::compose ("%1dB", i)), width - 32, y - 12);
125 gc->SetPen (*wxLIGHT_GREY_PEN);
126 gc->StrokePath (grid);
128 wxGraphicsPath axes = gc->CreatePath ();
129 axes.MoveToPoint (border, border);
130 axes.AddLineToPoint (border, height - border);
131 axes.AddLineToPoint (width - border, height - border);
132 gc->SetPen (*wxBLACK_PEN);
133 gc->StrokePath (axes);
135 for (int c = 0; c < MAX_AUDIO_CHANNELS; ++c) {
136 if (!_channel_visible[c] || c >= _analysis->channels()) {
140 wxGraphicsPath path[AudioPoint::COUNT];
142 for (int i = 0; i < AudioPoint::COUNT; ++i) {
143 if (!_type_visible[i]) {
147 path[i] = gc->CreatePath ();
148 path[i].MoveToPoint (0, height - (max (_analysis->get_point(c, 0)[i], float (_minimum)) - _minimum + _gain) * ys);
151 for (int i = 0; i < _analysis->points(c); ++i) {
152 for (int j = 0; j < AudioPoint::COUNT; ++j) {
153 if (!_type_visible[j]) {
157 path[j].AddLineToPoint (i * xs, height - (max (_analysis->get_point(c, i)[j], float (_minimum)) - _minimum + _gain) * ys);
161 wxColour const col = _colours[c];
163 if (_type_visible[AudioPoint::RMS]) {
164 gc->SetPen (*wxThePenList->FindOrCreatePen (col));
165 gc->StrokePath (path[AudioPoint::RMS]);
168 if (_type_visible[AudioPoint::PEAK]) {
169 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2)));
170 gc->StrokePath (path[AudioPoint::PEAK]);
178 AudioPlot::set_gain (float g)