ValveSoftware / steam-audio

Steam Audio
https://valvesoftware.github.io/steam-audio/
Apache License 2.0
2.2k stars 152 forks source link

Minimum distance in inverse distance attenuation and reflection produces NaN or weird output buffers #346

Closed dkotd closed 2 months ago

dkotd commented 2 months ago

System Information

Issue Description When I enable inverse distance attenuation with minDistance = 0.0f and reflection mode at the same time, iplReflectionEffectApply returns an output buffer with NaN values. When minDistance = 0.01f, the output buffer of the reflection sounds incredibly loud.

Sample Code

IPLSimulationInputs inputs {};
inputs.flags = IPLSimulationFlags(IPL_SIMULATIONFLAGS_DIRECT | IPL_SIMULATIONFLAGS_REFLECTIONS);

inputs.directFlags = IPL_DIRECTSIMULATIONFLAGS_DISTANCEATTENUATION;
IPLDistanceAttenuationModel model {};
model.type = IPL_DISTANCEATTENUATIONTYPE_INVERSEDISTANCE;
model.minDistance = 0.0f;
inputs.distanceAttenuationModel = model;

...

iplReflectionEffectApply(reflectionEffect, &params, &inBuff, &outBuff, nullptr); // outBuff has NaN
lakulish commented 2 months ago

The inverse-distance attenuation model implemented in Steam Audio does the following:

return 1.0f / std::max(distance, minDistance);

So if minDistance is very small, the computed value is very large (as you can see when minDistance is set to 0.01). In particular, when minDistance is 0, and distance also becomes 0, then the expression evaluates to infinity, so we want minDistance > 0. This has to do with the fact that true point sources cannot physically exist in the real world.