Skip to content

Commit

Permalink
🔧 Move SetListenerEnvironment method from SoundScriptManager to Sound…
Browse files Browse the repository at this point in the history
…Manager class and rename it to UpdateListenerEnvironment
  • Loading branch information
Hiradur committed Nov 30, 2024
1 parent bb9cb7c commit db43be9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 60 deletions.
39 changes: 32 additions & 7 deletions source/main/audio/SoundManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,9 @@ void SoundManager::Update(const float dt_sec)
const auto water = App::GetGameContext()->GetTerrain()->getWater();
m_listener_is_underwater = (water != nullptr ? water->IsUnderWater(m_listener_position) : false);

recomputeAllSources();
UpdateAlListener();
this->recomputeAllSources();
this->UpdateAlListener();
this->UpdateListenerEnvironment();

if(App::audio_enable_efx->getBool())
{
Expand All @@ -407,6 +408,35 @@ void SoundManager::SetListener(Ogre::Vector3 position, Ogre::Vector3 direction,
m_listener_velocity = velocity;
}

void SoundManager::UpdateListenerEnvironment()
{
const EFXEAXREVERBPROPERTIES* listener_reverb_properties = nullptr;

if (App::audio_engine_controls_environmental_audio->getBool())
{
if (this->ListenerIsUnderwater())
{
this->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 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.
*/
this->SetAirAbsorptionFactor(0.00668f);
}
else
{
this->SetSpeedOfSound(343.3f); // assume listener is in air at 20° celsius
this->SetAirAbsorptionFactor(1.0f);
}

if (App::audio_enable_efx->getBool())
{
m_listener_efx_reverb_properties = this->GetReverbPresetAt(m_listener_position);
}
}
}

void SoundManager::UpdateAlListener()
{
float orientation[6];
Expand Down Expand Up @@ -466,11 +496,6 @@ const EFXEAXREVERBPROPERTIES* SoundManager::GetReverbPresetAt(const Ogre::Vector
}
}

void SoundManager::SetListenerEnvironment(const EFXEAXREVERBPROPERTIES* listener_reverb_properties)
{
m_listener_efx_reverb_properties = listener_reverb_properties;
}

void SoundManager::UpdateListenerEffectSlot(const float dt_sec)
{
if(m_listener_efx_reverb_properties == nullptr)
Expand Down
13 changes: 6 additions & 7 deletions source/main/audio/SoundManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,6 @@ class SoundManager
*/
void SetListener(Ogre::Vector3 position, Ogre::Vector3 direction, Ogre::Vector3 up, Ogre::Vector3 velocity);

/**
* Updates the EFX/EAX reverb preset that is used as a base for updating the listener's effect slot.
* @param listener_environment The preset that will be used for the listener environment.
* @see UpdateListenerEffectSlot()
*/
void SetListenerEnvironment(const EFXEAXREVERBPROPERTIES* listener_efx_reverb_properties);

/**
* Unlike the name suggests, this sets the listener's gain to 0, essentially muting all sounds.
*/
Expand Down Expand Up @@ -194,6 +187,12 @@ class SoundManager
* @see SetListener()
*/
void UpdateAlListener();

/**
* Determines several properties of the environment of the listener and updates OpenAL to use them.
*/
void UpdateListenerEnvironment();

void recomputeAllSources();

/**
Expand Down
40 changes: 0 additions & 40 deletions source/main/audio/SoundScriptManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

#include "Actor.h"
#include "CameraManager.h"
#include "GameContext.h"
#include "Sound.h"
#include "SoundManager.h"
#include "Utils.h"
Expand Down Expand Up @@ -321,7 +320,6 @@ void SoundScriptManager::update(float dt_sec)
Ogre::Vector3 camera_direction = camera_node->getOrientation() * -Ogre::Vector3::UNIT_Z;

SetListener(camera_position, camera_direction, camera_up, camera_velocity);
SetListenerEnvironment(camera_position);

sound_manager->Update(dt_sec);
}
Expand All @@ -334,44 +332,6 @@ void SoundScriptManager::SetListener(Vector3 position, Vector3 direction, Vector
sound_manager->SetListener(position, direction, up, velocity);
}

void SoundScriptManager::SetListenerEnvironment(Vector3 listener_position)
{
if (disabled)
return;

const EFXEAXREVERBPROPERTIES* listener_reverb_properties = nullptr;

if (App::audio_engine_controls_environmental_audio->getBool())
{
if (sound_manager->ListenerIsUnderwater())
{
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 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
sound_manager->SetAirAbsorptionFactor(1.0f);
}

if (App::audio_enable_efx->getBool())
{
listener_reverb_properties = sound_manager->GetReverbPresetAt(listener_position);
}
}

if (App::audio_enable_efx->getBool())
{
// always update the environment in case it was changed via console or script
sound_manager->SetListenerEnvironment(listener_reverb_properties);
}
}

const StringVector& SoundScriptManager::getScriptPatterns(void) const
{
return script_patterns;
Expand Down
6 changes: 0 additions & 6 deletions source/main/audio/SoundScriptManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@

#include <OgreScriptLoader.h>

#ifdef __APPLE__
#include <OpenAL/efx-presets.h>
#else
#include <AL/efx-presets.h>
#endif

#define SOUND_PLAY_ONCE(_ACTOR_, _TRIG_) App::GetSoundScriptManager()->trigOnce ( (_ACTOR_), (_TRIG_) )
#define SOUND_START(_ACTOR_, _TRIG_) App::GetSoundScriptManager()->trigStart ( (_ACTOR_), (_TRIG_) )
#define SOUND_STOP(_ACTOR_, _TRIG_) App::GetSoundScriptManager()->trigStop ( (_ACTOR_), (_TRIG_) )
Expand Down

0 comments on commit db43be9

Please sign in to comment.