From: Carl Hetherington Date: Mon, 26 Mar 2018 22:51:02 +0000 (+0100) Subject: Add real-time mouse cursor readout in audio analysis (rest of #1082). X-Git-Tag: v2.13.6~3 X-Git-Url: https://main.carlh.net/gitweb/?p=dcpomatic.git;a=commitdiff_plain;h=a423c0a0a6c891d0cacd42a3ac04c110c5f2be2e;hp=37c28f4c76df89bc84d773beda1bb90be1cedd1a Add real-time mouse cursor readout in audio analysis (rest of #1082). --- diff --git a/ChangeLog b/ChangeLog index 60ab10a10..709ca463a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 2018-03-26 Carl Hetherington + * 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 diff --git a/src/wx/audio_dialog.cc b/src/wx/audio_dialog.cc index 938ecd1d3..b5d5634ef 100644 --- a/src/wx/audio_dialog.cc +++ b/src/wx/audio_dialog.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2013-2016 Carl Hetherington + Copyright (C) 2013-2018 Carl Hetherington This file is part of DCP-o-matic. @@ -74,6 +74,8 @@ AudioDialog::AudioDialog (wxWindow* parent, shared_ptr film, shared_ptrAdd (_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, shared_ptrplaylist (); } + + _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 time, optional db) +{ + if (!time || !db) { + _cursor->SetLabel (_("Cursor: none")); + return; + } + + shared_ptr film = _film.lock(); + DCPOMATIC_ASSERT (film); + _cursor->SetLabel (wxString::Format (_("Cursor: %.1fdB at %s"), *db, time->timecode(film->video_frame_rate()))); +} diff --git a/src/wx/audio_dialog.h b/src/wx/audio_dialog.h index 41be90b79..6d4428546 100644 --- a/src/wx/audio_dialog.h +++ b/src/wx/audio_dialog.h @@ -35,6 +35,8 @@ public: bool Show (bool show = true); + void set_cursor (boost::optional time, boost::optional db); + private: void content_changed (int); void channel_clicked (wxCommandEvent &); @@ -49,6 +51,7 @@ private: boost::weak_ptr _content; int _channels; boost::shared_ptr _playlist; + wxStaticText* _cursor; AudioPlot* _plot; wxStaticText* _sample_peak; wxStaticText* _true_peak; diff --git a/src/wx/audio_plot.cc b/src/wx/audio_plot.cc index 19e7bdac8..cabbd206a 100644 --- a/src/wx/audio_plot.cc +++ b/src/wx/audio_plot.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2013-2015 Carl Hetherington + Copyright (C) 2013-2018 Carl Hetherington 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 const & search, wxMouseEvent const & ev, double& min_dist, Point& min_point) const +{ + for (map::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 (); + + 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 (); + Refresh (); + Cursor (optional(), optional()); +} diff --git a/src/wx/audio_plot.h b/src/wx/audio_plot.h index 203d7af8b..85306a58d 100644 --- a/src/wx/audio_plot.h +++ b/src/wx/audio_plot.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2012-2015 Carl Hetherington + Copyright (C) 2012-2018 Carl Hetherington This file is part of DCP-o-matic. @@ -18,11 +18,12 @@ */ -#include -#include -#include #include "lib/util.h" #include "lib/audio_analysis.h" +#include +#include +#include +#include struct Metrics; @@ -40,24 +41,17 @@ public: wxColour colour (int n) const; + boost::signals2::signal, boost::optional)> 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 _analysis; - bool _channel_visible[MAX_DCP_AUDIO_CHANNELS]; - bool _type_visible[AudioPoint::COUNT]; - int _smoothing; - std::vector _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 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 const & search, wxMouseEvent const & ev, double& min_dist, Point& min_point) const; + + boost::shared_ptr _analysis; + bool _channel_visible[MAX_DCP_AUDIO_CHANNELS]; + bool _type_visible[AudioPoint::COUNT]; + int _smoothing; + std::vector _colours; + wxString _message; + float _gain_correction; + mutable std::map _peak; mutable std::map _rms; + boost::optional _cursor; + static const int _minimum; + static const int _cursor_size; };