Skip to content

Commit

Permalink
Fix NaNs in basic filters and Equalizer
Browse files Browse the repository at this point in the history
Fix several NaNs in the context of the basic filters and the Equalizer.

The coefficients of the `BiQuad` were not initialized which seems to have led to NaNs down the line.

Fix a division by zero in `EqEffect::peakBand` and `EqSpectrumView::paintEvent` if the energy is zero.

The condition `m_peakSum <= 0` was removed from `EqSpectrumView::paintEvent` because the value is initialized to 0 down the line and later only potentially positive values are added.
  • Loading branch information
michaelgregorius committed Mar 11, 2024
1 parent 1be2a20 commit f3448d0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
7 changes: 6 additions & 1 deletion include/BasicFilters.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@ template<ch_cnt_t CHANNELS>
class BiQuad
{
public:
BiQuad()
BiQuad() :
m_a1(0.),
m_a2(0.),
m_b0(0.),
m_b1(0.),
m_b2(0.)
{
clearHistory();
}
Expand Down
8 changes: 7 additions & 1 deletion plugins/Eq/EqEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,20 @@ bool EqEffect::processAudioBuffer( sampleFrame *buf, const fpp_t frames )

float EqEffect::peakBand( float minF, float maxF, EqAnalyser *fft, int sr )
{
auto const fftEnergy = fft->getEnergy();
if (fftEnergy == 0.)
{
return 0.;
}

float peak = -60;
float *b = fft->m_bands;
float h = 0;
for( int x = 0; x < MAX_BANDS; x++, b++ )
{
if( bandToFreq( x ,sr) >= minF && bandToFreq( x,sr ) <= maxF )
{
h = 20 * ( log10( *b / fft->getEnergy() ) );
h = 20 * ( log10( *b / fftEnergy ) );
peak = h > peak ? h : peak;
}
}
Expand Down
8 changes: 4 additions & 4 deletions plugins/Eq/EqSpectrumView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ EqSpectrumView::EqSpectrumView(EqAnalyser *b, QWidget *_parent) :

void EqSpectrumView::paintEvent(QPaintEvent *event)
{
const float energy = m_analyser->getEnergy();
if( energy <= 0 && m_peakSum <= 0 )
const float energy = m_analyser->getEnergy();
if (energy <= 0)
{
//dont draw anything
// If there is no energy in the signal we don't need to draw anything
return;
}

Expand All @@ -238,7 +238,7 @@ void EqSpectrumView::paintEvent(QPaintEvent *event)
const float fallOff = 1.07;
for( int x = 0; x < MAX_BANDS; ++x, ++bands )
{
peak = ( fh * 2.0 / 3.0 * ( 20 * ( log10( *bands / energy ) ) - LOWER_Y ) / ( - LOWER_Y ) );
peak = *bands != 0 ? ( fh * 2.0 / 3.0 * ( 20 * ( log10( *bands / energy ) ) - LOWER_Y ) / ( - LOWER_Y ) ) : 0.;
if( peak < 0 )
{
peak = 0;
Expand Down

0 comments on commit f3448d0

Please sign in to comment.