Pretty dumb smoothing.
[dcpomatic.git] / src / wx / audio_plot.cc
index 41d074dad485539b210ef34278fde82595a6b117..d938d0c278fbe968604fae203959cd6ec5baaf33 100644 (file)
 
 using std::cout;
 using std::vector;
+using std::list;
 using std::max;
 using std::min;
 using boost::bind;
 using boost::shared_ptr;
 
-int const AudioPlot::_minimum = -90;
+int const AudioPlot::_minimum = -70;
 
 AudioPlot::AudioPlot (wxWindow* parent)
        : wxPanel (parent)
-       , _channel (0)
        , _gain (0)
+       , _smoothing (1)
 {
-       Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (AudioPlot::paint), 0, this);
+       SetDoubleBuffered (true);
+
+       for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
+               _channel_visible[i] = false;
+       }
+
+       for (int i = 0; i < AudioPoint::COUNT; ++i) {
+               _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 (139,   0, 204));
+       _colours.push_back (wxColour (  0,   0, 255));
+       _colours.push_back (wxColour (100, 100, 100));
+       
+       Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (AudioPlot::paint), 0, this);
+       
        SetMinSize (wxSize (640, 512));
 }
 
@@ -49,14 +67,29 @@ void
 AudioPlot::set_analysis (shared_ptr<AudioAnalysis> a)
 {
        _analysis = a;
-       _channel = 0;
+
+       for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
+               _channel_visible[i] = false;
+       }
+
+       for (int i = 0; i < AudioPoint::COUNT; ++i) {
+               _type_visible[i] = false;
+       }
+       
        Refresh ();
 }
 
 void
-AudioPlot::set_channel (int c)
+AudioPlot::set_channel_visible (int c, bool v)
 {
-       _channel = c;
+       _channel_visible[c] = v;
+       Refresh ();
+}
+
+void
+AudioPlot::set_type_visible (int t, bool v)
+{
+       _type_visible[t] = v;
        Refresh ();
 }
 
@@ -70,46 +103,104 @@ AudioPlot::paint (wxPaintEvent &)
                return;
        }
 
-       if (!_analysis) {
+       if (!_analysis || _analysis->channels() == 0) {
                gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
                gc->DrawText (_("Please wait; audio is being analysed..."), 32, 32);
                return;
        }
-       
-       int const width = GetSize().GetWidth();
-       float const xs = width / float (_analysis->points (_channel));
-       int const height = GetSize().GetHeight ();
-       float const ys = height / -_minimum;
 
        wxGraphicsPath grid = gc->CreatePath ();
        gc->SetFont (gc->CreateFont (*wxSMALL_FONT));
+       wxDouble db_label_width;
+       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);
+
+       db_label_width += 8;
+       
+       int const data_width = GetSize().GetWidth() - db_label_width;
+       /* Assume all channels have the same number of points */
+       float const xs = data_width / float (_analysis->points (0));
+       int const height = GetSize().GetHeight ();
+       int const yo = 32;
+       float const ys = (height - yo) / -_minimum;
+
        for (int i = _minimum; i <= 0; i += 10) {
-               int const y = height - (i - _minimum) * ys;
-               grid.MoveToPoint (0, y);
-               grid.AddLineToPoint (width, y);
-               gc->DrawText (std_to_wx (String::compose ("%1dB", i)), width - 32, y - 12);
+               int const y = (height - (i - _minimum) * ys) - yo;
+               grid.MoveToPoint (db_label_width - 4, y);
+               grid.AddLineToPoint (db_label_width + data_width, y);
+               gc->DrawText (std_to_wx (String::compose ("%1dB", i)), 0, y - (db_label_height / 2));
        }
+
        gc->SetPen (*wxLIGHT_GREY_PEN);
        gc->StrokePath (grid);
 
-       wxGraphicsPath path[AudioPoint::COUNT];
+       gc->DrawText (_("Time"), data_width, height - yo + db_label_height / 2);
 
-       for (int i = 0; i < AudioPoint::COUNT; ++i) {
-               path[i] = gc->CreatePath ();
-               path[i].MoveToPoint (0, height - (max (_analysis->get_point(_channel, 0)[i], float (_minimum)) - _minimum + _gain) * ys);
-       }
+       for (int c = 0; c < MAX_AUDIO_CHANNELS; ++c) {
+               if (!_channel_visible[c] || c >= _analysis->channels()) {
+                       continue;
+               }
 
-       for (int i = 0; i < _analysis->points(_channel); ++i) {
-               for (int j = 0; j < AudioPoint::COUNT; ++j) {
-                       path[j].AddLineToPoint (i * xs, height - (max (_analysis->get_point(_channel, i)[j], float (_minimum)) - _minimum + _gain) * ys);
+               wxGraphicsPath path[AudioPoint::COUNT];
+               
+               for (int i = 0; i < AudioPoint::COUNT; ++i) {
+                       if (!_type_visible[i]) {
+                               continue;
+                       }
+                       
+                       path[i] = gc->CreatePath ();
+
+                       float const val = 20 * log10 (_analysis->get_point(c, 0)[i]);
+                       
+                       path[i].MoveToPoint (
+                               db_label_width,
+                               height - (max (val, float (_minimum)) - _minimum + _gain) * ys - yo
+                               );
                }
-       }
 
-       gc->SetPen (*wxBLUE_PEN);
-       gc->StrokePath (path[AudioPoint::RMS]);
+               list<float> smoothing[AudioPoint::COUNT];
+
+               for (int i = 0; i < _analysis->points(c); ++i) {
+                       for (int j = 0; j < AudioPoint::COUNT; ++j) {
+                               if (!_type_visible[j]) {
+                                       continue;
+                               }
+
+                               smoothing[j].push_back (_analysis->get_point(c, i)[j]);
+                               if (int(smoothing[j].size()) > _smoothing) {
+                                       smoothing[j].pop_front ();
+                               }
+
+                               float const val = 20 * log10 (_analysis->smooth (smoothing[j], static_cast<AudioPoint::Type> (j)));
+                               
+                               path[j].AddLineToPoint (
+                                       i * xs + db_label_width,
+                                       height - (max (val, float (_minimum)) - _minimum + _gain) * ys - yo
+                                       );
+                       }
+               }
 
-       gc->SetPen (*wxRED_PEN);
-       gc->StrokePath (path[AudioPoint::PEAK]);
+               wxColour const col = _colours[c];
+
+               if (_type_visible[AudioPoint::RMS]) {
+                       gc->SetPen (*wxThePenList->FindOrCreatePen (col));
+                       gc->StrokePath (path[AudioPoint::RMS]);
+               }
+
+               if (_type_visible[AudioPoint::PEAK]) {
+                       gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (col.Red(), col.Green(), col.Blue(), col.Alpha() / 2)));
+                       gc->StrokePath (path[AudioPoint::PEAK]);
+               }
+       }
+
+       wxGraphicsPath axes = gc->CreatePath ();
+       axes.MoveToPoint (db_label_width, 0);
+       axes.AddLineToPoint (db_label_width, height - yo);
+       axes.AddLineToPoint (db_label_width + data_width, height - yo);
+       gc->SetPen (*wxBLACK_PEN);
+       gc->StrokePath (axes);
 
        delete gc;
 }
@@ -120,3 +211,10 @@ AudioPlot::set_gain (float g)
        _gain = g;
        Refresh ();
 }
+
+void
+AudioPlot::set_smoothing (int s)
+{
+       _smoothing = s;
+       Refresh ();
+}