From 8136b7002c74f5dc33450d64a1ba359fb013917c Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Sun, 10 Dec 2023 11:42:02 +0100 Subject: [PATCH] Fix floating point exception in Spectrum Analyzer (#7018) Fix a floating point exception in the Spectrum Analyzer which occurs if the amplitude is 0. The amplitude is used in the calculation of a logarithm which is not defined for values smaller or equal to 0. The fix is to replace a value of 0 with the minimum positive float value. Negative values are not handled because it seems that only values greater or equal to 0 are fed into the method. This assumption is asserted in case this ever changes. --- plugins/SpectrumAnalyzer/SaProcessor.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/SpectrumAnalyzer/SaProcessor.cpp b/plugins/SpectrumAnalyzer/SaProcessor.cpp index a79d52bdc..38e8e9e92 100644 --- a/plugins/SpectrumAnalyzer/SaProcessor.cpp +++ b/plugins/SpectrumAnalyzer/SaProcessor.cpp @@ -41,6 +41,9 @@ #include "LocklessRingBuffer.h" #include "SaControls.h" +#include +#include + namespace lmms { @@ -650,7 +653,8 @@ float SaProcessor::ampToYPixel(float amplitude, unsigned int height) const if (m_controls->m_logYModel.value()) { // logarithmic scale: convert linear amplitude to dB (relative to 1.0) - float amplitude_dB = 10 * log10(amplitude); + assert (amplitude >= 0); + float amplitude_dB = 10 * std::log10(std::max(amplitude, std::numeric_limits::min())); if (amplitude_dB < getAmpRangeMin()) { return height;