Fix failure to update peak on changing gain (#2286).
[dcpomatic.git] / src / wx / audio_panel.cc
index cfea4f0264a5b8a55ea200e1b94de5ea11f9db89..5e8370d46bfcb5b258f0219517faccbcef498cdb 100644 (file)
 #include "lib/ffmpeg_content.h"
 #include "lib/job_manager.h"
 #include "lib/maths_util.h"
-#include "lib/warnings.h"
-DCPOMATIC_DISABLE_WARNINGS
+#include <dcp/warnings.h>
+LIBDCP_DISABLE_WARNINGS
 #include <wx/spinctrl.h>
-DCPOMATIC_ENABLE_WARNINGS
-#include <iostream>
+LIBDCP_ENABLE_WARNINGS
 
 
-using std::cout;
 using std::dynamic_pointer_cast;
 using std::list;
 using std::make_shared;
@@ -59,6 +57,9 @@ using namespace boost::placeholders;
 using namespace dcpomatic;
 
 
+std::map<boost::filesystem::path, float> AudioPanel::_peak_cache;
+
+
 AudioPanel::AudioPanel (ContentPanel* p)
        : ContentSubPanel (p, _("Audio"))
 {
@@ -260,6 +261,8 @@ AudioPanel::film_content_changed (int property)
                setup_peak ();
                layout ();
        } else if (property == AudioContentProperty::GAIN) {
+               /* This is a bit aggressive but probably not so bad */
+               _peak_cache.clear();
                setup_peak ();
        } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
                if (ac.size() == 1) {
@@ -447,8 +450,18 @@ AudioPanel::peak () const
                auto playlist = make_shared<Playlist>();
                playlist->add (_parent->film(), sel.front());
                try {
-                       auto analysis = make_shared<AudioAnalysis>(_parent->film()->audio_analysis_path(playlist));
-                       peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
+                       /* Loading the audio analysis file is slow, and this ::peak() is called a few times when
+                        * the content selection is changed, so cache it.
+                        */
+                       auto const path = _parent->film()->audio_analysis_path(playlist);
+                       auto cached = _peak_cache.find(path);
+                       if (cached != _peak_cache.end()) {
+                               peak_dB = cached->second;
+                       } else {
+                               auto analysis = make_shared<AudioAnalysis>(path);
+                               peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
+                               _peak_cache[path] = *peak_dB;
+                       }
                } catch (...) {
 
                }