+
+/** @return If there is one selected piece of audio content, return its peak value in dB (if known) */
+optional<float>
+AudioPanel::peak () const
+{
+ optional<float> peak_dB;
+
+ auto sel = _parent->selected_audio ();
+ if (sel.size() == 1) {
+ auto playlist = make_shared<Playlist>();
+ playlist->add (_parent->film(), sel.front());
+ try {
+ /* 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 (...) {
+
+ }
+ }
+
+ return peak_dB;
+}
+
+