tabs -> spaces.
[dcpomatic.git] / src / wx / audio_plot.cc
index 9b2e6b1b0eef8cb218b370418619a64b44a8c800..b2be40036be74d9d5e591b286d30f95b65cc69ad 100644 (file)
@@ -21,7 +21,6 @@
 #include <boost/bind.hpp>
 #include <wx/graphics.h>
 #include "audio_plot.h"
-#include "lib/decoder_factory.h"
 #include "lib/audio_decoder.h"
 #include "lib/audio_analysis.h"
 #include "wx/wx_util.h"
@@ -38,11 +37,13 @@ int const AudioPlot::_minimum = -70;
 int const AudioPlot::max_smoothing = 128;
 
 AudioPlot::AudioPlot (wxWindow* parent)
-       : wxPanel (parent)
+       : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
        , _gain (0)
        , _smoothing (max_smoothing / 2)
 {
+#ifndef __WXOSX__      
        SetDoubleBuffered (true);
+#endif 
 
        for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
                _channel_visible[i] = false;
@@ -52,11 +53,11 @@ AudioPlot::AudioPlot (wxWindow* parent)
                _type_visible[i] = false;
        }
 
-       _colours.push_back (wxColour (  0,   0,   0));
-       _colours.push_back (wxColour (255,   0,   0));
-       _colours.push_back (wxColour (  0, 255,   0));
+       _colours.push_back (wxColour (  0,   0,   0));
+       _colours.push_back (wxColour (255,   0,   0));
+       _colours.push_back (wxColour (  0, 255,   0));
        _colours.push_back (wxColour (139,   0, 204));
-       _colours.push_back (wxColour (  0,   0, 255));
+       _colours.push_back (wxColour (  0,   0, 255));
        _colours.push_back (wxColour (100, 100, 100));
        
        Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (AudioPlot::paint), 0, this);
@@ -115,7 +116,7 @@ AudioPlot::paint (wxPaintEvent &)
        wxDouble db_label_height;
        wxDouble db_label_descent;
        wxDouble db_label_leading;
-       gc->GetTextExtent (_("-80dB"), &_db_label_width, &db_label_height, &db_label_descent, &db_label_leading);
+       gc->GetTextExtent (wxT ("-80dB"), &_db_label_width, &db_label_height, &db_label_descent, &db_label_leading);
 
        _db_label_width += 8;
        
@@ -146,11 +147,7 @@ AudioPlot::paint (wxPaintEvent &)
                                plot_peak (p, c);
                        }
                        wxColour const col = _colours[c];
-#if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
                        gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxPENSTYLE_SOLID));
-#else                  
-                       gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2), 1, wxSOLID));
-#endif
                        gc->StrokePath (p);
                }
        }
@@ -162,11 +159,7 @@ AudioPlot::paint (wxPaintEvent &)
                                plot_rms (p, c);
                        }
                        wxColour const col = _colours[c];
-#if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
                        gc->SetPen (*wxThePenList->FindOrCreatePen (col, 1, wxPENSTYLE_SOLID));
-#else
-                       gc->SetPen (*wxThePenList->FindOrCreatePen (col, 1, wxSOLID));
-#endif                 
                        gc->StrokePath (p);
                }
        }
@@ -190,6 +183,10 @@ AudioPlot::y_for_linear (float p) const
 void
 AudioPlot::plot_peak (wxGraphicsPath& path, int channel) const
 {
+       if (_analysis->points (channel) == 0) {
+               return;
+       }
+       
        path.MoveToPoint (_db_label_width, y_for_linear (_analysis->get_point(channel, 0)[AudioPoint::PEAK]));
 
        float peak = 0;
@@ -210,24 +207,53 @@ AudioPlot::plot_peak (wxGraphicsPath& path, int channel) const
 void
 AudioPlot::plot_rms (wxGraphicsPath& path, int channel) const
 {
+       if (_analysis->points (channel) == 0) {
+               return;
+       }
+       
        path.MoveToPoint (_db_label_width, y_for_linear (_analysis->get_point(channel, 0)[AudioPoint::RMS]));
 
        list<float> smoothing;
+
        int const N = _analysis->points(channel);
+
+       float const first = _analysis->get_point(channel, 0)[AudioPoint::RMS];
+       float const last = _analysis->get_point(channel, N - 1)[AudioPoint::RMS];
+
+       int const before = _smoothing / 2;
+       int const after = _smoothing - before;
+       
+       /* Pre-load the smoothing list */
+       for (int i = 0; i < before; ++i) {
+               smoothing.push_back (first);
+       }
+       for (int i = 0; i < after; ++i) {
+               if (i < N) {
+                       smoothing.push_back (_analysis->get_point(channel, i)[AudioPoint::RMS]);
+               } else {
+                       smoothing.push_back (last);
+               }
+       }
+       
        for (int i = 0; i < N; ++i) {
 
-               smoothing.push_back (_analysis->get_point(channel, i)[AudioPoint::RMS]);
-               if (int(smoothing.size()) > _smoothing) {
-                       smoothing.pop_front ();
+               int const next_for_window = i + after;
+
+               if (next_for_window < N) {
+                       smoothing.push_back (_analysis->get_point(channel, i)[AudioPoint::RMS]);
+               } else {
+                       smoothing.push_back (last);
                }
 
+               smoothing.pop_front ();
+
                float p = 0;
                for (list<float>::const_iterator j = smoothing.begin(); j != smoothing.end(); ++j) {
                        p += pow (*j, 2);
                }
 
                p = sqrt (p / smoothing.size ());
-               
+
                path.AddLineToPoint (_db_label_width + i * _x_scale, y_for_linear (p));
        }
 }