Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Simplify manual sound rolloff handling
  • Loading branch information
kcat authored and coelckers committed Nov 25, 2020
1 parent 93f023d commit 51adcd4
Showing 1 changed file with 33 additions and 62 deletions.
95 changes: 33 additions & 62 deletions src/common/audio/sound/oalsound.cpp
Expand Up @@ -1304,7 +1304,7 @@ FISoundChannel *OpenALSoundRenderer::StartSound3D(SoundHandle sfx, SoundListener
if(AL.EXT_source_distance_model)
alSourcei(source, AL_DISTANCE_MODEL, AL_INVERSE_DISTANCE);
alSourcef(source, AL_REFERENCE_DISTANCE, rolloff->MinDistance/distscale);
alSourcef(source, AL_MAX_DISTANCE, (1000.f+rolloff->MinDistance)/distscale);
alSourcef(source, AL_MAX_DISTANCE, std::numeric_limits<float>::max());
alSourcef(source, AL_ROLLOFF_FACTOR, rolloff->RolloffFactor);
manualRolloff = false;
}
Expand All @@ -1324,77 +1324,49 @@ FISoundChannel *OpenALSoundRenderer::StartSound3D(SoundHandle sfx, SoundListener
// when AL_EXT_source_distance_model is not supported, we have to play
// around a bit to get appropriate distance attenation. What we do is
// calculate the attenuation that should be applied, then given an
// Inverse Distance rolloff model with OpenAL, reverse the calculation
// to get the distance needed for that much attenuation. The Inverse
// Distance calculation is:
// Inverse Distance rolloff model with OpenAL, calculate the reference
// distance that will achieve that much attenuation with the current
// distance. The Inverse Distance calculation is:
//
// Gain = MinDist / (MinDist + RolloffFactor*(Distance - MinDist))
//
// Thus, the reverse is:
// Simplifying for RolloffFactor=1, it can be broken down by:
//
// Distance = (MinDist/Gain - MinDist)/RolloffFactor + MinDist
// Gain = MinDist / (MinDist + (Distance - MinDist))
// Gain = MinDist / Distance
// Gain * Distance = MinDist
//
// This can be simplified by using a MinDist and RolloffFactor of 1,
// which makes it:
//
// Distance = 1.0f/Gain;
//
// The source position is then set that many units away from the
// listener position, and OpenAL takes care of the rest.
// The source's reference distance is then set according to the desired
// gain and effective distance from the listener, and OpenAL takes care
// of the rest.
if(AL.EXT_source_distance_model)
alSourcei(source, AL_DISTANCE_MODEL, AL_INVERSE_DISTANCE);
alSourcef(source, AL_REFERENCE_DISTANCE, 1.f);
alSourcef(source, AL_MAX_DISTANCE, 100000.f);
alSourcef(source, AL_ROLLOFF_FACTOR, 1.f);

if(AL.EXT_SOURCE_RADIUS)
{
/* Since the OpenAL distance is decoupled from the sound's distance, get the OpenAL
* distance that corresponds to the area radius. */
float gain = GetRolloff(rolloff, AREA_SOUND_RADIUS);
alSourcef(source, AL_SOURCE_RADIUS, (chanflags&SNDF_AREA) ?
// Clamp in case the max distance is <= the area radius
((gain > 0.00001f) ? 1.f/gain : 100000.f) : 0.f
);
}

if(dist_sqr < (0.0004f*0.0004f))
{
// Head relative
alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
alSource3f(source, AL_POSITION, 0.f, 0.f, 0.f);
}
else
{
float gain = GetRolloff(rolloff, sqrtf(dist_sqr) * distscale);
FVector3 dir = pos - listener->position;
dir.MakeResize((gain > 0.00001f) ? 1.f/gain : 100000.f);
dir += listener->position;
float dist = sqrtf(dist_sqr);
float gain = GetRolloff(rolloff, dist * distscale);
// Don't let the ref distance go to 0, or else distance attenuation is
// lost with the inverse distance model.
alSourcef(source, AL_REFERENCE_DISTANCE, std::max<float>(gain*dist, 0.0004f));
alSourcef(source, AL_MAX_DISTANCE, std::numeric_limits<float>::max());
alSourcef(source, AL_ROLLOFF_FACTOR, 1.f);
}

alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE);
alSource3f(source, AL_POSITION, dir[0], dir[1], -dir[2]);
}
if(dist_sqr < (0.0004f*0.0004f))
{
// Head relative
alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
alSource3f(source, AL_POSITION, 0.f, 0.f, 0.f);
}
else
{
if(AL.EXT_SOURCE_RADIUS)
alSourcef(source, AL_SOURCE_RADIUS, (chanflags&SNDF_AREA) ? AREA_SOUND_RADIUS : 0.f);

if(dist_sqr < (0.0004f*0.0004f))
{
// Head relative
alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
alSource3f(source, AL_POSITION, 0.f, 0.f, 0.f);
}
else
{
alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE);
alSource3f(source, AL_POSITION, pos[0], pos[1], -pos[2]);
}
alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE);
alSource3f(source, AL_POSITION, pos[0], pos[1], -pos[2]);
}
alSource3f(source, AL_VELOCITY, vel[0], vel[1], -vel[2]);
alSource3f(source, AL_DIRECTION, 0.f, 0.f, 0.f);
alSourcef(source, AL_DOPPLER_FACTOR, 0.f);
if(AL.EXT_SOURCE_RADIUS)
alSourcef(source, AL_SOURCE_RADIUS, (chanflags&SNDF_AREA) ? AREA_SOUND_RADIUS : 0.f);

alSourcei(source, AL_LOOPING, (chanflags&SNDF_LOOP) ? AL_TRUE : AL_FALSE);

Expand Down Expand Up @@ -1633,8 +1605,7 @@ void OpenALSoundRenderer::UpdateSoundParams3D(SoundListener *listener, FISoundCh
if(chan == NULL || chan->SysChannel == NULL)
return;

FVector3 dir = pos - listener->position;
float dist_sqr = (float)dir.LengthSquared();
float dist_sqr = (float)(pos - listener->position).LengthSquared();
chan->DistanceSqr = dist_sqr;

alDeferUpdatesSOFT();
Expand All @@ -1649,13 +1620,13 @@ void OpenALSoundRenderer::UpdateSoundParams3D(SoundListener *listener, FISoundCh
{
if(chan->ManualRolloff)
{
float gain = GetRolloff(&chan->Rolloff, sqrtf(dist_sqr)*chan->DistanceScale);
dir.MakeResize((gain > 0.00001f) ? 1.f/gain : 100000.f);
float dist = sqrtf(dist_sqr);
float gain = GetRolloff(&chan->Rolloff, dist * chan->DistanceScale);
alSourcef(source, AL_REFERENCE_DISTANCE, std::max<float>(gain*dist, 0.0004f));
}
dir += listener->position;

alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE);
alSource3f(source, AL_POSITION, dir[0], dir[1], -dir[2]);
alSource3f(source, AL_POSITION, pos[0], pos[1], -pos[2]);
}
alSource3f(source, AL_VELOCITY, vel[0], vel[1], -vel[2]);
getALError();
Expand Down

0 comments on commit 51adcd4

Please sign in to comment.