From 1fadfdf60bb2c02086c2c9689ea44c73bed41571 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Tue, 26 Feb 2013 08:15:51 +0000 Subject: [PATCH] Pretty dumb smoothing. --- src/lib/analyse_audio_job.cc | 3 +-- src/lib/audio_analysis.cc | 28 ++++++++++++++++++++++++++++ src/lib/audio_analysis.h | 2 ++ src/wx/audio_dialog.cc | 11 +++++++++++ src/wx/audio_dialog.h | 2 ++ src/wx/audio_plot.cc | 25 +++++++++++++++++++++++-- src/wx/audio_plot.h | 2 ++ 7 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/lib/analyse_audio_job.cc b/src/lib/analyse_audio_job.cc index 588e9fc3d..bcabb6c91 100644 --- a/src/lib/analyse_audio_job.cc +++ b/src/lib/analyse_audio_job.cc @@ -95,8 +95,7 @@ AnalyseAudioJob::audio (shared_ptr b) _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], fabsf (s)); if ((_done % _samples_per_point) == 0) { - _current[j][AudioPoint::RMS] = 20 * log10 (sqrt (_current[j][AudioPoint::RMS] / _samples_per_point)); - _current[j][AudioPoint::PEAK] = 20 * log10 (_current[j][AudioPoint::PEAK]); + _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point); _analysis->add_point (j, _current[j]); _current[j] = AudioPoint (); diff --git a/src/lib/audio_analysis.cc b/src/lib/audio_analysis.cc index b29ed1707..0cf08c5bd 100644 --- a/src/lib/audio_analysis.cc +++ b/src/lib/audio_analysis.cc @@ -31,6 +31,8 @@ using std::ofstream; using std::ifstream; using std::vector; using std::cout; +using std::max; +using std::list; AudioPoint::AudioPoint () { @@ -121,3 +123,29 @@ AudioAnalysis::write (string filename) f.close (); boost::filesystem::rename (tmp, filename); } + +float +AudioAnalysis::smooth (list const & data, AudioPoint::Type t) +{ + float val; + + switch (t) { + case AudioPoint::PEAK: + /* XXX: fall-off, or something...? */ + val = -200; + for (list::const_iterator i = data.begin(); i != data.end(); ++i) { + val = max (val, *i); + } + return val; + case AudioPoint::RMS: + val = 0; + for (list::const_iterator i = data.begin(); i != data.end(); ++i) { + val += pow (*i, 2); + } + return sqrt (val / data.size()); + default: + assert (false); + } + + return 0; +} diff --git a/src/lib/audio_analysis.h b/src/lib/audio_analysis.h index c2d8db876..a8cfbdeca 100644 --- a/src/lib/audio_analysis.h +++ b/src/lib/audio_analysis.h @@ -22,6 +22,7 @@ #include #include +#include class AudioPoint { @@ -59,6 +60,7 @@ public: void write (std::string); + static float smooth (std::list const &, AudioPoint::Type); private: std::vector > _data; diff --git a/src/wx/audio_dialog.cc b/src/wx/audio_dialog.cc index 701c8263a..bcec01332 100644 --- a/src/wx/audio_dialog.cc +++ b/src/wx/audio_dialog.cc @@ -61,6 +61,11 @@ AudioDialog::AudioDialog (wxWindow* parent) _type_checkbox[i]->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (AudioDialog::type_clicked), 0, this); } + _smoothing = new wxSlider (this, wxID_ANY, 1, 1, 128); + _smoothing->Connect (wxID_ANY, wxEVT_SCROLL_THUMBTRACK, wxScrollEventHandler (AudioDialog::smoothing_changed), 0, this); + table->Add (_smoothing, 1, wxEXPAND); + table->AddSpacer (0); + sizer->Add (table, 0, wxALL, 12); SetSizer (sizer); @@ -178,3 +183,9 @@ AudioDialog::type_clicked (wxCommandEvent& ev) _plot->set_type_visible (t, _type_checkbox[t]->GetValue ()); } + +void +AudioDialog::smoothing_changed (wxScrollEvent &) +{ + _plot->set_smoothing (_smoothing->GetValue ()); +} diff --git a/src/wx/audio_dialog.h b/src/wx/audio_dialog.h index c3875023f..16cb356fe 100644 --- a/src/wx/audio_dialog.h +++ b/src/wx/audio_dialog.h @@ -37,6 +37,7 @@ private: void film_changed (Film::Property); void channel_clicked (wxCommandEvent &); void type_clicked (wxCommandEvent &); + void smoothing_changed (wxScrollEvent &); void try_to_load_analysis (); void setup_channels (); @@ -44,6 +45,7 @@ private: AudioPlot* _plot; wxCheckBox* _channel_checkbox[MAX_AUDIO_CHANNELS]; wxCheckBox* _type_checkbox[AudioPoint::COUNT]; + wxSlider* _smoothing; boost::signals2::scoped_connection _film_changed_connection; boost::signals2::scoped_connection _film_audio_analysis_finished_connection; }; diff --git a/src/wx/audio_plot.cc b/src/wx/audio_plot.cc index ad69b6e1d..d938d0c27 100644 --- a/src/wx/audio_plot.cc +++ b/src/wx/audio_plot.cc @@ -28,6 +28,7 @@ using std::cout; using std::vector; +using std::list; using std::max; using std::min; using boost::bind; @@ -38,6 +39,7 @@ int const AudioPlot::_minimum = -70; AudioPlot::AudioPlot (wxWindow* parent) : wxPanel (parent) , _gain (0) + , _smoothing (1) { SetDoubleBuffered (true); @@ -149,21 +151,33 @@ AudioPlot::paint (wxPaintEvent &) } path[i] = gc->CreatePath (); + + float const val = 20 * log10 (_analysis->get_point(c, 0)[i]); + path[i].MoveToPoint ( db_label_width, - height - (max (_analysis->get_point(c, 0)[i], float (_minimum)) - _minimum + _gain) * ys - yo + height - (max (val, float (_minimum)) - _minimum + _gain) * ys - yo ); } + list 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 (j))); path[j].AddLineToPoint ( i * xs + db_label_width, - height - (max (_analysis->get_point(c, i)[j], float (_minimum)) - _minimum + _gain) * ys - yo + height - (max (val, float (_minimum)) - _minimum + _gain) * ys - yo ); } } @@ -197,3 +211,10 @@ AudioPlot::set_gain (float g) _gain = g; Refresh (); } + +void +AudioPlot::set_smoothing (int s) +{ + _smoothing = s; + Refresh (); +} diff --git a/src/wx/audio_plot.h b/src/wx/audio_plot.h index 4ac7f848c..fe8862d54 100644 --- a/src/wx/audio_plot.h +++ b/src/wx/audio_plot.h @@ -32,6 +32,7 @@ public: void set_channel_visible (int c, bool v); void set_type_visible (int t, bool v); void set_gain (float); + void set_smoothing (int); private: void paint (wxPaintEvent &); @@ -41,6 +42,7 @@ private: bool _type_visible[AudioPoint::COUNT]; /** gain to apply in dB */ float _gain; + int _smoothing; std::vector _colours; -- 2.30.2