Skip to content

Commit

Permalink
🎮 Rework frequency-dependent absorption for sound
Browse files Browse the repository at this point in the history
* No longer expose corresponding CVars since internal handling should suffice
* the previous calculation for the air absorption factor for underwater environments incorrectly targeted 5 MHz instead of 5 kHz
* AirAbsorptionGainHF is no longer adjusted since it should be determined by the preset
  • Loading branch information
Hiradur committed Nov 2, 2024
1 parent 3b8c016 commit 2dbc482
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
2 changes: 0 additions & 2 deletions source/main/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,6 @@ CVar* io_discord_rpc;
CVar* io_invert_orbitcam;

// Audio
CVar* audio_air_absorption_factor;
CVar* audio_air_absorption_gain_hf;
CVar* audio_master_volume;
CVar* audio_enable_creak;
CVar* audio_enable_obstruction;
Expand Down
2 changes: 0 additions & 2 deletions source/main/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,6 @@ extern CVar* io_discord_rpc;
extern CVar* io_invert_orbitcam;

// Audio
extern CVar* audio_air_absorption_factor;
extern CVar* audio_air_absorption_gain_hf;
extern CVar* audio_master_volume;
extern CVar* audio_enable_creak;
extern CVar* audio_enable_obstruction;
Expand Down
4 changes: 1 addition & 3 deletions source/main/audio/SoundManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ void SoundManager::Update(const float dt_sec)
for(int hardware_index = 0; hardware_index < hardware_sources_num; hardware_index++)
{
// update air absorption factor
alSourcef(hardware_sources[hardware_index], AL_AIR_ABSORPTION_FACTOR, App::audio_air_absorption_factor->getFloat());
alSourcef(hardware_sources[hardware_index], AL_AIR_ABSORPTION_FACTOR, m_air_absorption_factor);

this->UpdateObstructionFilter(hardware_index);
}
Expand Down Expand Up @@ -433,8 +433,6 @@ void SoundManager::UpdateListenerEffectSlot(const float dt_sec)

EFXEAXREVERBPROPERTIES current_environmental_properties = *m_listener_efx_reverb_properties;

current_environmental_properties.flAirAbsorptionGainHF = App::audio_air_absorption_gain_hf->getFloat();

// early reflections panning, delay and strength
if (App::audio_enable_reflection_panning->getBool() && m_efx_reverb_engine == EfxReverbEngine::EAXREVERB)
{
Expand Down
12 changes: 12 additions & 0 deletions source/main/audio/SoundManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ class SoundManager
*/
void SetDopplerFactor(const float doppler_factor) const { alDopplerFactor(doppler_factor); }

/**
* Sets the air absorptions factor for the direct path of all sounds.
* @param air_absorption_factor Air absorption factor within the range of AL_AIR_ABSORPTION_FACTOR.
*/
void SetAirAbsorptionFactor(const float air_absorption_factor) { m_air_absorption_factor = air_absorption_factor; }

/**
* @return current value set for the air absorption factor for the direct path of sounds
*/
float GetAirAbsorptionFactor() const { return m_air_absorption_factor; }

/**
* Returns the number of currently used hardware sources. In a typical scenario,
* this value changes dynamically.
Expand Down Expand Up @@ -233,6 +244,7 @@ class SoundManager
bool m_efx_is_available = false;
ALuint m_listener_slot = 0;
ALuint m_efx_outdoor_obstruction_lowpass_filter_id = 0;
float m_air_absorption_factor = 1.0f;
EfxReverbEngine m_efx_reverb_engine = EfxReverbEngine::NONE;
const EFXEAXREVERBPROPERTIES* m_listener_efx_reverb_properties = nullptr;
std::map<std::string, EFXEAXREVERBPROPERTIES> m_efx_properties_map;
Expand Down
14 changes: 6 additions & 8 deletions source/main/audio/SoundScriptManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,18 +361,16 @@ void SoundScriptManager::SetListenerEnvironment(Vector3 listener_position)
{
sound_manager->SetSpeedOfSound(1522.0f); // assume listener is in sea water (i.e. salt water)
/*
According to the Francois-Garrison formula for frequency-dependant absorption at 5kHz in water
and assuming the Air Absorption Gain HF property of OpenAL is set to the minimum of 0.892,
the absorption factor should be ~11.25, which is just slightly above the maximum of 10.0.
*/
App::audio_air_absorption_factor->setVal(10.0f);
App::audio_air_absorption_gain_hf->setVal(0.892f);
* According to the Francois-Garrison formula for frequency-dependant absorption at 5kHz in seawater,
* the absorption should be 0.334 db/km. OpenAL multiplies the Air Absorption Factor with an internal
* value of 0.05dB/m, so we need a factor of 0.00668f.
*/
sound_manager->SetAirAbsorptionFactor(0.00668f);
}
else
{
sound_manager->SetSpeedOfSound(343.3f); // assume listener is in air at 20° celsius
App::audio_air_absorption_factor->setVal(1.0f);
App::audio_air_absorption_gain_hf->setVal(0.994f);
sound_manager->SetAirAbsorptionFactor(1.0f);
}

if (App::audio_enable_efx->getBool())
Expand Down
2 changes: 0 additions & 2 deletions source/main/system/CVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ void Console::cVarSetupBuiltins()
App::io_discord_rpc = this->cVarCreate("io_discord_rpc", "Discord Rich Presence", CVAR_ARCHIVE | CVAR_TYPE_BOOL, "true");
App::io_invert_orbitcam = this->cVarCreate("io_invert_orbitcam", "Invert orbit camera", CVAR_ARCHIVE | CVAR_TYPE_BOOL, "false");

App::audio_air_absorption_factor = this->cVarCreate("audio_air_absorption_factor", "Air absorption factor", CVAR_TYPE_FLOAT, "1.0");
App::audio_air_absorption_gain_hf = this->cVarCreate("audio_air_absorption_gain_hf", "Air absorption Gain HF", CVAR_TYPE_FLOAT, "0.994");
App::audio_master_volume = this->cVarCreate("audio_master_volume", "Sound Volume", CVAR_ARCHIVE | CVAR_TYPE_FLOAT, "1.0");
App::audio_enable_creak = this->cVarCreate("audio_enable_creak", "Creak Sound", CVAR_ARCHIVE | CVAR_TYPE_BOOL, "false");
App::audio_enable_obstruction = this->cVarCreate("audio_enable_obstruction", "Obstruction of sounds", CVAR_ARCHIVE | CVAR_TYPE_BOOL, "false");
Expand Down

0 comments on commit 2dbc482

Please sign in to comment.