Add real-time mouse cursor readout in audio analysis (rest of #1082).
authorCarl Hetherington <cth@carlh.net>
Mon, 26 Mar 2018 22:51:02 +0000 (23:51 +0100)
committerCarl Hetherington <cth@carlh.net>
Mon, 26 Mar 2018 22:51:02 +0000 (23:51 +0100)
ChangeLog
src/wx/audio_dialog.cc
src/wx/audio_dialog.h
src/wx/audio_plot.cc
src/wx/audio_plot.h

index 60ab10a1010a91b75ae3bed8a5f75a412ef54d70..709ca463acfe1e1a5bb95d8a8a526ddae9e132d0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,7 @@
 2018-03-26  Carl Hetherington  <cth@carlh.net>
 
+       * Add real-time mouse cursor readout in audio analysis (rest of #1082)
+
        * Add time axis to audio analysis (part of #1082).
 
        * Note: from this point in the ChangeLog release versions
index 938ecd1d3396223a8d3066d4b470821c5e448bfb..b5d5634efeefb5948b14adba4f8f9697276a9546 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
@@ -74,6 +74,8 @@ AudioDialog::AudioDialog (wxWindow* parent, shared_ptr<Film> film, shared_ptr<Co
 
        wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
 
+       _cursor = new wxStaticText (this, wxID_ANY, wxT("Cursor: none"));
+       left->Add (_cursor, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP);
        _plot = new AudioPlot (this);
        left->Add (_plot, 1, wxTOP | wxEXPAND, 12);
        _sample_peak = new wxStaticText (this, wxID_ANY, wxT (""));
@@ -157,6 +159,8 @@ AudioDialog::AudioDialog (wxWindow* parent, shared_ptr<Film> film, shared_ptr<Co
        } else {
                _playlist = film->playlist ();
        }
+
+       _plot->Cursor.connect (bind (&AudioDialog::set_cursor, this, _1, _2));
 }
 
 void
@@ -383,3 +387,16 @@ AudioDialog::Show (bool show)
        try_to_load_analysis ();
        return r;
 }
+
+void
+AudioDialog::set_cursor (optional<DCPTime> time, optional<float> db)
+{
+       if (!time || !db) {
+               _cursor->SetLabel (_("Cursor: none"));
+               return;
+       }
+
+       shared_ptr<Film> film = _film.lock();
+       DCPOMATIC_ASSERT (film);
+       _cursor->SetLabel (wxString::Format (_("Cursor: %.1fdB at %s"), *db, time->timecode(film->video_frame_rate())));
+}
index 41be90b79ed6d4074b49a0a55bc93bf1d02c8583..6d44285463ba0823081a73b6e53695add2a0c0f0 100644 (file)
@@ -35,6 +35,8 @@ public:
 
        bool Show (bool show = true);
 
+       void set_cursor (boost::optional<DCPTime> time, boost::optional<float> db);
+
 private:
        void content_changed (int);
        void channel_clicked (wxCommandEvent &);
@@ -49,6 +51,7 @@ private:
        boost::weak_ptr<Content> _content;
        int _channels;
        boost::shared_ptr<const Playlist> _playlist;
+       wxStaticText* _cursor;
        AudioPlot* _plot;
        wxStaticText* _sample_peak;
        wxStaticText* _true_peak;
index 19e7bdac8420a48de65212dbfdcd631430d8bfee..cabbd206af5e0e22302e8d7decddc325bc3377c6 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
@@ -32,10 +32,13 @@ using std::vector;
 using std::list;
 using std::max;
 using std::min;
+using std::map;
 using boost::bind;
+using boost::optional;
 using boost::shared_ptr;
 
 int const AudioPlot::_minimum = -70;
+int const AudioPlot::_cursor_size = 8;
 int const AudioPlot::max_smoothing = 128;
 
 AudioPlot::AudioPlot (wxWindow* parent)
@@ -80,6 +83,8 @@ AudioPlot::AudioPlot (wxWindow* parent)
 #endif
 
        Bind (wxEVT_PAINT, boost::bind (&AudioPlot::paint, this));
+       Bind (wxEVT_MOTION, boost::bind (&AudioPlot::mouse_moved, this, _1));
+       Bind (wxEVT_LEAVE_WINDOW, boost::bind (&AudioPlot::mouse_leave, this, _1));
 
        SetMinSize (wxSize (640, 512));
 }
@@ -239,6 +244,17 @@ AudioPlot::paint ()
        gc->SetPen (wxPen (wxColour (0, 0, 0)));
        gc->StrokePath (axes);
 
+       if (_cursor) {
+               wxGraphicsPath cursor = gc->CreatePath ();
+               cursor.MoveToPoint (_cursor->draw.x - _cursor_size / 2, _cursor->draw.y - _cursor_size / 2);
+               cursor.AddLineToPoint (_cursor->draw.x + _cursor_size / 2, _cursor->draw.y + _cursor_size / 2);
+               cursor.MoveToPoint (_cursor->draw.x + _cursor_size / 2, _cursor->draw.y - _cursor_size / 2);
+               cursor.AddLineToPoint (_cursor->draw.x - _cursor_size / 2, _cursor->draw.y + _cursor_size / 2);
+               gc->StrokePath (cursor);
+
+
+       }
+
        delete gc;
 }
 
@@ -276,7 +292,7 @@ AudioPlot::plot_peak (wxGraphicsPath& path, int channel, Metrics const & metrics
                        Point (
                                wxPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (peak, metrics)),
                                DCPTime::from_frames (i * _analysis->samples_per_point(), _analysis->sample_rate()),
-                               peak
+                               20 * log10(peak)
                                )
                        );
        }
@@ -345,7 +361,7 @@ AudioPlot::plot_rms (wxGraphicsPath& path, int channel, Metrics const & metrics)
                        Point (
                                wxPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (p, metrics)),
                                DCPTime::from_frames (i * _analysis->samples_per_point(), _analysis->sample_rate()),
-                               p
+                               20 * log10(p)
                                )
                        );
        }
@@ -394,3 +410,46 @@ AudioPlot::colour (int n) const
        DCPOMATIC_ASSERT (n < int(_colours.size()));
        return _colours[n];
 }
+
+void
+AudioPlot::search (map<int, PointList> const & search, wxMouseEvent const & ev, double& min_dist, Point& min_point) const
+{
+       for (map<int, PointList>::const_iterator i = search.begin(); i != search.end(); ++i) {
+               BOOST_FOREACH (Point const & j, i->second) {
+                       double const dist = pow(ev.GetX() - j.draw.x, 2) + pow(ev.GetY() - j.draw.y, 2);
+                       if (dist < min_dist) {
+                               min_dist = dist;
+                               min_point = j;
+                       }
+               }
+       }
+}
+
+void
+AudioPlot::mouse_moved (wxMouseEvent& ev)
+{
+       double min_dist = DBL_MAX;
+       Point min_point;
+
+       search (_rms, ev, min_dist, min_point);
+       search (_peak, ev, min_dist, min_point);
+
+       _cursor = optional<Point> ();
+
+       if (min_dist < DBL_MAX) {
+               wxRect before (min_point.draw.x - _cursor_size / 2, min_point.draw.y - _cursor_size / 2, _cursor_size, _cursor_size);
+               GetParent()->Refresh (true, &before);
+               _cursor = min_point;
+               wxRect after (min_point.draw.x - _cursor_size / 2, min_point.draw.y - _cursor_size / 2, _cursor_size, _cursor_size);
+               GetParent()->Refresh (true, &after);
+               Cursor (min_point.time, min_point.db);
+       }
+}
+
+void
+AudioPlot::mouse_leave (wxMouseEvent &)
+{
+       _cursor = optional<Point> ();
+       Refresh ();
+       Cursor (optional<DCPTime>(), optional<float>());
+}
index 203d7af8befcdfc2dd9255849cf4efb84fdab1a3..85306a58d4ee45acf2e210775f9100a094178e87 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 
 */
 
-#include <vector>
-#include <boost/shared_ptr.hpp>
-#include <wx/wx.h>
 #include "lib/util.h"
 #include "lib/audio_analysis.h"
+#include <wx/wx.h>
+#include <boost/shared_ptr.hpp>
+#include <boost/signals2.hpp>
+#include <vector>
 
 struct Metrics;
 
@@ -40,24 +41,17 @@ public:
 
        wxColour colour (int n) const;
 
+       boost::signals2::signal<void (boost::optional<DCPTime>, boost::optional<float>)> Cursor;
+
        static const int max_smoothing;
 
 private:
-       void paint ();
-       void plot_peak (wxGraphicsPath &, int, Metrics const &) const;
-       void plot_rms (wxGraphicsPath &, int, Metrics const &) const;
-       float y_for_linear (float, Metrics const &) const;
-       AudioPoint get_point (int channel, int point) const;
-
-       boost::shared_ptr<AudioAnalysis> _analysis;
-       bool _channel_visible[MAX_DCP_AUDIO_CHANNELS];
-       bool _type_visible[AudioPoint::COUNT];
-       int _smoothing;
-       std::vector<wxColour> _colours;
-       wxString _message;
-       float _gain_correction;
 
        struct Point {
+               Point ()
+                       : db(0)
+               {}
+
                Point (wxPoint draw_, DCPTime time_, float db_)
                        : draw(draw_)
                        , time(time_)
@@ -71,8 +65,28 @@ private:
 
        typedef std::vector<Point> PointList;
 
+       void paint ();
+       void plot_peak (wxGraphicsPath &, int, Metrics const &) const;
+       void plot_rms (wxGraphicsPath &, int, Metrics const &) const;
+       float y_for_linear (float, Metrics const &) const;
+       AudioPoint get_point (int channel, int point) const;
+       void mouse_moved (wxMouseEvent& ev);
+       void mouse_leave (wxMouseEvent& ev);
+       void search (std::map<int, PointList> const & search, wxMouseEvent const & ev, double& min_dist, Point& min_point) const;
+
+       boost::shared_ptr<AudioAnalysis> _analysis;
+       bool _channel_visible[MAX_DCP_AUDIO_CHANNELS];
+       bool _type_visible[AudioPoint::COUNT];
+       int _smoothing;
+       std::vector<wxColour> _colours;
+       wxString _message;
+       float _gain_correction;
+
        mutable std::map<int, PointList> _peak;
        mutable std::map<int, PointList> _rms;
 
+       boost::optional<Point> _cursor;
+
        static const int _minimum;
+       static const int _cursor_size;
 };