Skip to content

Commit

Permalink
Sound: add air absorption
Browse files Browse the repository at this point in the history
currently hard-coded to 0.1f, for testing
Air absorption is basically a low-pass filter
relative to distance between sound source and listener,
so when in your base or zoomed out, front battles
will be heard as only low frequencies.
  • Loading branch information
hoijui committed Feb 23, 2010
1 parent c26c98e commit 195a5e7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
10 changes: 10 additions & 0 deletions rts/System/Sound/ALShared.h
Expand Up @@ -3,6 +3,16 @@

#include "LogOutput.h"

// Copied from efx.h, as this header only exists under windows
// so far (February 2010).
// This will change in future versions of OpenAL.
#if !defined(AL_AIR_ABSORPTION_FACTOR)
#define AL_AIR_ABSORPTION_FACTOR 0x20007
#define AL_MIN_AIR_ABSORPTION_FACTOR 0.0f
#define AL_MAX_AIR_ABSORPTION_FACTOR 10.0f
#define AL_DEFAULT_AIR_ABSORPTION_FACTOR 0.0f
#endif // !defined(AL_AIR_ABSORPTION_FACTOR)

extern const CLogSubsystem LOG_SOUND;

bool CheckError(const char* msg);
Expand Down
2 changes: 2 additions & 0 deletions rts/System/Sound/Sound.cpp
Expand Up @@ -309,6 +309,8 @@ void CSound::StartThread(int maxSounds)
}
}

SoundSource::SetAirAbsorption(0.1f);

// Generate sound sources
for (int i = 0; i < maxSounds; i++)
{
Expand Down
34 changes: 34 additions & 0 deletions rts/System/Sound/SoundSource.cpp
Expand Up @@ -9,11 +9,13 @@
#include "OggStream.h"
#include "ALShared.h"
#include "float3.h"
#include "Util.h"
#include "LogOutput.h"

float SoundSource::globalPitch = 1.0;
float SoundSource::heightAdjustedRolloffModifier = 1.0;
float SoundSource::referenceDistance = 200.0f;
float SoundSource::airAbsorption = 0.0f;

struct SoundSource::StreamControl
{
Expand Down Expand Up @@ -144,6 +146,9 @@ void SoundSource::Play(SoundItem* item, const float3& pos, float3 velocity, floa
alSourcei(id, AL_BUFFER, item->buffer->GetId());
alSourcef(id, AL_GAIN, item->GetGain() * volume);
alSourcef(id, AL_PITCH, item->GetPitch() * globalPitch);
if (airAbsorption > 0.0f) {
alSourcef(id, AL_AIR_ABSORPTION_FACTOR, airAbsorption);
}
velocity *= item->dopplerScale;
alSource3f(id, AL_VELOCITY, velocity.x, velocity.y, velocity.z);
if (item->loopTime > 0)
Expand Down Expand Up @@ -273,3 +278,32 @@ void SoundSource::SetVolume(float newVol)
alSourcef(id, AL_GAIN, newVol * curStream->current->volume);
}
}

void SoundSource::SetAirAbsorption(float factor)
{
// check if we can use air absorption
std::string noAirAbsorpReason = "";
if (!alcIsExtensionPresent(NULL, "ALC_EXT_EFX"))
{
noAirAbsorpReason = "ALC_EXT_EFX not supported";
}
else if (factor <= AL_MIN_AIR_ABSORPTION_FACTOR)
{
noAirAbsorpReason = "absorption value (" + FloatToString(factor) + ") <= minimum (" + FloatToString(AL_MIN_AIR_ABSORPTION_FACTOR) + ")";
}
else if (factor > AL_MAX_AIR_ABSORPTION_FACTOR)
{
noAirAbsorpReason = "absorption value (" + FloatToString(factor) + ") > maximum (" + FloatToString(AL_MAX_AIR_ABSORPTION_FACTOR) + ")";
}

if (noAirAbsorpReason.empty())
{
airAbsorption = factor;
LogObject(LOG_SOUND) << "air absorption enabled, value: " << airAbsorption;
}
else
{
airAbsorption = 0.0f;
LogObject(LOG_SOUND) << "air absorption disabled, reason: " << noAirAbsorpReason;
}
}
9 changes: 9 additions & 0 deletions rts/System/Sound/SoundSource.h
Expand Up @@ -51,6 +51,14 @@ class SoundSource : boost::noncopyable
heightAdjustedRolloffModifier = mod > 0.0f ? mod : 0.0f;
};

/**
* Sets the ammount of air absorption.
* Filters out high-frequency sounds, relative to distance.
* Higher value -> filter out a lot on small distances already.
* @param factor from 0.0f (disabled) till 10.0f
*/
static void SetAirAbsorption(float factor = 0.0f);

private:
/// pitch shared by all sources
static float globalPitch;
Expand All @@ -63,6 +71,7 @@ class SoundSource : boost::noncopyable
unsigned loopStop;
static float heightAdjustedRolloffModifier;
static float referenceDistance;
static float airAbsorption;
};

#endif

0 comments on commit 195a5e7

Please sign in to comment.