Skip to content

Commit

Permalink
Fix fader display for Equalizer shelves and peaks
Browse files Browse the repository at this point in the history
The peak values for the shelves and peaks of the Equalizer plugin are computed in `EqEffect::peakBand`. The previous implementation evaluated the bins of the corresponding frequency spectrum and determined the "loudest" one. The value of this bin was then converted to dbFS and mapped to the interval [0, inf[ where all values less or equal to -60 dbFS were mapped to 0 and a value of 40 dbFS was mapped to 1. So effectively everything was mapped somewhere into [0, 1] yet in a quite "distorted" way because a signal of 40 dbFS resulted in being displayed as unity in the fader.

This commit directly returns the value of the maximum bin, i.e. it does not map first to dbFS and then linearize the result anymore. This should work because the `Fader` class assumes a "linear" input signal and if the value of the bin was previously mapped to dbFS it should have some "linear" character. Please note that this is still somewhat of a "proxy" value because ideally the summed amplitude of all relevant bins in the frequency range would be shown and not just the "loudest" one.

## Other changes
Rename `peakBand` to `linearPeakBand` to make more clear that a linear value is returned.

Handle a potential division by zero by checking the value of `fft->getEnergy()` before using it.

Index into `fft->m_bands` so that no parallel incrementing of the pointer is needed. This also enables the removal of the local variable `b`.
  • Loading branch information
michaelgregorius committed Feb 23, 2024
1 parent 0f3ce40 commit 449ab02
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
2 changes: 2 additions & 0 deletions plugins/Eq/EqControls.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class EqControls : public EffectControls
float m_inPeakR;
float m_outPeakL;
float m_outPeakR;

// The following are linear peaks
float m_lowShelfPeakL, m_lowShelfPeakR;
float m_para1PeakL, m_para1PeakR;
float m_para2PeakL, m_para2PeakR;
Expand Down
35 changes: 20 additions & 15 deletions plugins/Eq/EqEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,21 +287,26 @@ bool EqEffect::processAudioBuffer( sampleFrame *buf, const fpp_t frames )



float EqEffect::peakBand( float minF, float maxF, EqAnalyser *fft, int sr )
float EqEffect::linearPeakBand(float minF, float maxF, EqAnalyser* fft, int sr)
{
float peak = -60;
float *b = fft->m_bands;
float h = 0;
for( int x = 0; x < MAX_BANDS; x++, b++ )
auto const fftEnergy = fft->getEnergy();
if (fftEnergy == 0)
{
if( bandToFreq( x ,sr) >= minF && bandToFreq( x,sr ) <= maxF )
// No energy -> no signal
return 0;
}

float peakLinear = 0.;

for (int i = 0; i < MAX_BANDS; ++i)
{
if (bandToFreq(i, sr) >= minF && bandToFreq(i, sr) <= maxF)
{
h = 20 * ( log10( *b / fft->getEnergy() ) );
peak = h > peak ? h : peak;
peakLinear = std::max(peakLinear, fft->m_bands[i] / fftEnergy);
}
}

return ( peak + 60 ) / 100;
return peakLinear;
}


Expand All @@ -310,41 +315,41 @@ float EqEffect::peakBand( float minF, float maxF, EqAnalyser *fft, int sr )
void EqEffect::setBandPeaks( EqAnalyser *fft, int samplerate )
{
m_eqControls.m_lowShelfPeakR = m_eqControls.m_lowShelfPeakL =
peakBand( m_eqControls.m_lowShelfFreqModel.value()
linearPeakBand( m_eqControls.m_lowShelfFreqModel.value()
* ( 1 - m_eqControls.m_lowShelfResModel.value() * 0.5 ),
m_eqControls.m_lowShelfFreqModel.value(),
fft , samplerate );

m_eqControls.m_para1PeakL = m_eqControls.m_para1PeakR =
peakBand( m_eqControls.m_para1FreqModel.value()
linearPeakBand( m_eqControls.m_para1FreqModel.value()
* ( 1 - m_eqControls.m_para1BwModel.value() * 0.5 ),
m_eqControls.m_para1FreqModel.value()
* ( 1 + m_eqControls.m_para1BwModel.value() * 0.5 ),
fft , samplerate );

m_eqControls.m_para2PeakL = m_eqControls.m_para2PeakR =
peakBand( m_eqControls.m_para2FreqModel.value()
linearPeakBand( m_eqControls.m_para2FreqModel.value()
* ( 1 - m_eqControls.m_para2BwModel.value() * 0.5 ),
m_eqControls.m_para2FreqModel.value()
* ( 1 + m_eqControls.m_para2BwModel.value() * 0.5 ),
fft , samplerate );

m_eqControls.m_para3PeakL = m_eqControls.m_para3PeakR =
peakBand( m_eqControls.m_para3FreqModel.value()
linearPeakBand( m_eqControls.m_para3FreqModel.value()
* ( 1 - m_eqControls.m_para3BwModel.value() * 0.5 ),
m_eqControls.m_para3FreqModel.value()
* ( 1 + m_eqControls.m_para3BwModel.value() * 0.5 ),
fft , samplerate );

m_eqControls.m_para4PeakL = m_eqControls.m_para4PeakR =
peakBand( m_eqControls.m_para4FreqModel.value()
linearPeakBand( m_eqControls.m_para4FreqModel.value()
* ( 1 - m_eqControls.m_para4BwModel.value() * 0.5 ),
m_eqControls.m_para4FreqModel.value()
* ( 1 + m_eqControls.m_para4BwModel.value() * 0.5 ),
fft , samplerate );

m_eqControls.m_highShelfPeakL = m_eqControls.m_highShelfPeakR =
peakBand( m_eqControls.m_highShelfFreqModel.value(),
linearPeakBand( m_eqControls.m_highShelfFreqModel.value(),
m_eqControls.m_highShelfFreqModel.value()
* ( 1 + m_eqControls.m_highShelfResModel.value() * 0.5 ),
fft, samplerate );
Expand Down
2 changes: 1 addition & 1 deletion plugins/Eq/EqEffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class EqEffect : public Effect
float m_inGain;
float m_outGain;

float peakBand( float minF, float maxF, EqAnalyser *, int );
float linearPeakBand(float minF, float maxF, EqAnalyser*, int);

inline float bandToFreq ( int index , int sampleRate )
{
Expand Down

0 comments on commit 449ab02

Please sign in to comment.