Skip to content

fix: GetBandGainDB when a group center lands exactly on an octave band - #28

Open
lmtanco wants to merge 1 commit into
3DTune-In:masterfrom
lmtanco:fix/getbandgaindb-fallthrough
Open

fix: GetBandGainDB when a group center lands exactly on an octave band#28
lmtanco wants to merge 1 commit into
3DTune-In:masterfrom
lmtanco:fix/getbandgaindb-fallthrough

Conversation

@lmtanco

@lmtanco lmtanco commented Sep 4, 2026

Copy link
Copy Markdown
Member

Summary

CGammatoneMultibandExpander::GetBandGainDB interpolates a group band's static
attenuation between its two neighbouring octave bands using two weights that always sum to 1. The branching only covers weights that are negative (the edge sentinels used by SetGroups) or strictly positive:

else if (gammatoneLowerBandGroupFactors[bandIndex] > 0 && gammatoneHigherBandGroupFactors[bandIndex] > 0)
{
    return (octaveBandAttenuations[gammatoneLowerBandGroupIndices[bandIndex]]) * gammatoneLowerBandGroupFactors[bandIndex] +
        (octaveBandAttenuations[gammatoneHigherBandGroupIndices[bandIndex]]) * gammatoneHigherBandGroupFactors[bandIndex];
}
else return 0.0f;

When a group's reconstructed centre frequency (the geometric mean of its two limits, computed in SetGroups) lands exactly on one of the expander's internal octave bands, one of the two weights is exactly 0.0f. That case falls through to the else branch and silently returns 0.0f, discarding the band's configured attenuation, even though the correct value is just that of the other interpolation term (the other weight is exactly 1.0f).

For example with the limits {500, 2000} Hz the reconstructed centre is exactly 1000.0 Hz (500 * 2000 = 1e6, and its square root are both representable exactly as floats), so the band centred at 1000 Hz would lose its static attenuation.

Fix

Just use >=0 instead of >0 in the interpolation branch, as the formula is correct when one weight is 0 and the other is 1.

How to test

HAHLSimulation::CGammatoneMultibandExpander expander;
expander.Setup(48000, 125.0f, 8, true);          // octave bands 125..16000 Hz
expander.SetAttenuationForOctaveBand(3, 10.0f);   // 10 dB at the 1000 Hz octave band
expander.SetGroups({500.0f, 2000.0f});            // group 1 centres exactly on 1000 Hz

// Before this fix: returns 0.0f (bug)
// After this fix:  returns 10.0f (correct)
float attenuationDB = expander.GetBandGainDB(1);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant