Merge pull request #804 from MihailRis/fix-al-related-code

stabilise al effects-related code
This commit is contained in:
MihailRis 2026-02-28 00:27:28 +03:00 • committed by GitHub
commit 246e0faa52
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 79 additions and 74 deletions

View file

@ -47,6 +47,7 @@ static LPALDELETEEFFECTS alDeleteEffects = nullptr;
static LPALISEFFECT alIsEffect = nullptr; static LPALISEFFECT alIsEffect = nullptr;
static LPALEFFECTI alEffecti = nullptr; static LPALEFFECTI alEffecti = nullptr;
static LPALEFFECTF alEffectf = nullptr; static LPALEFFECTF alEffectf = nullptr;
static LPALGETEFFECTF alGetEffectf = nullptr;
static LPALGENFILTERS alGenFilters = nullptr; static LPALGENFILTERS alGenFilters = nullptr;
static LPALDELETEFILTERS alDeleteFilters = nullptr; static LPALDELETEFILTERS alDeleteFilters = nullptr;
static LPALISFILTER alIsFilter = nullptr; static LPALISFILTER alIsFilter = nullptr;
@ -562,6 +563,7 @@ static bool request_efx_ext_functions() {
&& get_proc_address("alIsEffect", alIsEffect) && get_proc_address("alIsEffect", alIsEffect)
&& get_proc_address("alEffecti", alEffecti) && get_proc_address("alEffecti", alEffecti)
&& get_proc_address("alEffectf", alEffectf) && get_proc_address("alEffectf", alEffectf)
&& get_proc_address("alGetEffectf", alGetEffectf)
&& get_proc_address("alGenFilters", alGenFilters) && get_proc_address("alGenFilters", alGenFilters)
&& get_proc_address("alDeleteFilters", alDeleteFilters) && get_proc_address("alDeleteFilters", alDeleteFilters)
&& get_proc_address("alIsFilter", alIsFilter) && get_proc_address("alIsFilter", alIsFilter)
@ -799,77 +801,73 @@ void ALAudio::setAcoustics(Acoustics acoustics) {
alAuxiliaryEffectSloti(effectSlots[0], AL_EFFECTSLOT_EFFECT, 0); alAuxiliaryEffectSloti(effectSlots[0], AL_EFFECTSLOT_EFFECT, 0);
return; return;
} }
auto setEffectValue = [](int effect, ALenum param, float value, float min, float max) {
const float smoothFactor = 0.5f;
value = glm::clamp(value, min, max);
float prevValue = 0.0f;
alGetEffectf(effect, param, &prevValue);
AL_CHECK(alEffectf(
effect,
param,
glm::clamp(
prevValue * (1.0f - smoothFactor) + value * smoothFactor,
min,
max
)
));
};
int reverbEffect = effects[REVERB_EFFECT]; int reverbEffect = effects[REVERB_EFFECT];
AL_CHECK(alEffectf( setEffectValue(
reverbEffect, reverbEffect,
AL_REVERB_GAIN, AL_REVERB_GAIN,
glm::clamp(glm::min(acoustics.reverbGain, 0.8f) * acoustics.reverbGain *
(1.0f - glm::min(1.0f, acoustics.reverbAbsorption)), (1.0f - glm::min(1.0f, acoustics.reverbAbsorption)),
AL_REVERB_MIN_GAIN, AL_REVERB_MIN_GAIN,
AL_REVERB_MAX_GAIN) AL_REVERB_MAX_GAIN
)); );
AL_CHECK(alEffectf( setEffectValue(
reverbEffect, reverbEffect,
AL_REVERB_REFLECTIONS_DELAY, AL_REVERB_REFLECTIONS_DELAY,
glm::clamp( acoustics.reverbReflectionsDelay,
acoustics.reverbReflectionsDelay, AL_REVERB_MIN_REFLECTIONS_DELAY,
AL_REVERB_MIN_REFLECTIONS_DELAY, AL_REVERB_MAX_REFLECTIONS_DELAY
AL_REVERB_MAX_REFLECTIONS_DELAY );
) setEffectValue(
));
AL_CHECK(alEffectf(
reverbEffect, reverbEffect,
AL_REVERB_LATE_REVERB_DELAY, AL_REVERB_LATE_REVERB_DELAY,
glm::clamp( acoustics.reverbLateReflectionsDelay,
acoustics.reverbLateReflectionsDelay, AL_REVERB_MIN_LATE_REVERB_DELAY,
AL_REVERB_MIN_LATE_REVERB_DELAY, AL_REVERB_MAX_LATE_REVERB_DELAY
AL_REVERB_MAX_LATE_REVERB_DELAY );
) setEffectValue(
));
AL_CHECK(alEffectf(
reverbEffect, reverbEffect,
AL_REVERB_DECAY_TIME, AL_REVERB_DECAY_TIME,
glm::clamp( acoustics.reverbDecayTime,
acoustics.reverbDecayTime, AL_REVERB_MIN_DECAY_TIME,
AL_REVERB_MIN_DECAY_TIME, AL_REVERB_MAX_DECAY_TIME
AL_REVERB_MAX_DECAY_TIME );
) setEffectValue(
));
AL_CHECK(alEffectf(
reverbEffect, reverbEffect,
AL_REVERB_ROOM_ROLLOFF_FACTOR, AL_REVERB_ROOM_ROLLOFF_FACTOR,
glm::clamp( acoustics.reverbRoomRolloff,
acoustics.reverbRoomRolloff, AL_REVERB_MIN_ROOM_ROLLOFF_FACTOR,
AL_REVERB_MIN_ROOM_ROLLOFF_FACTOR, AL_REVERB_MAX_ROOM_ROLLOFF_FACTOR
AL_REVERB_MAX_ROOM_ROLLOFF_FACTOR );
) setEffectValue(
));
AL_CHECK(alEffectf(
reverbEffect, reverbEffect,
AL_REVERB_REFLECTIONS_GAIN, AL_REVERB_REFLECTIONS_GAIN,
glm::clamp( glm::pow(acoustics.reverbGain, 2.0f),
glm::pow(acoustics.reverbGain, 2.0f), AL_REVERB_MIN_REFLECTIONS_GAIN,
AL_REVERB_MIN_REFLECTIONS_GAIN, AL_REVERB_MAX_REFLECTIONS_GAIN
AL_REVERB_MAX_REFLECTIONS_GAIN );
) setEffectValue(
));
AL_CHECK(alEffectf(
reverbEffect, reverbEffect,
AL_REVERB_LATE_REVERB_GAIN, AL_REVERB_LATE_REVERB_GAIN,
glm::clamp( glm::pow(acoustics.reverbGain, 2.0f),
glm::pow(acoustics.reverbGain, 2.0f), AL_REVERB_MIN_LATE_REVERB_GAIN,
AL_REVERB_MIN_LATE_REVERB_GAIN, AL_REVERB_MAX_LATE_REVERB_GAIN
AL_REVERB_MAX_LATE_REVERB_GAIN );
)
));
AL_CHECK(alEffectf(
reverbEffect,
AL_REVERB_DECAY_HFRATIO,
glm::clamp(
1.0f - acoustics.reverbAbsorption,
AL_REVERB_MIN_DECAY_HFRATIO,
AL_REVERB_MAX_DECAY_HFRATIO
)
));
alAuxiliaryEffectSloti(effectSlots[0], AL_EFFECTSLOT_EFFECT, reverbEffect); alAuxiliaryEffectSloti(effectSlots[0], AL_EFFECTSLOT_EFFECT, reverbEffect);
} }

View file

@ -1,29 +1,30 @@
#include "Decorator.hpp" #include "Decorator.hpp"
#include "ParticlesRenderer.hpp"
#include "WorldRenderer.hpp"
#include "TextsRenderer.hpp"
#include "TextNote.hpp"
#include "assets/Assets.hpp"
#include "assets/assets_util.hpp" #include "assets/assets_util.hpp"
#include "assets/Assets.hpp"
#include "audio/audio.hpp"
#include "content/Content.hpp" #include "content/Content.hpp"
#include "voxels/Chunks.hpp" #include "debug/Logger.hpp"
#include "voxels/Chunk.hpp"
#include "voxels/Block.hpp"
#include "world/Level.hpp"
#include "window/Camera.hpp"
#include "objects/Player.hpp"
#include "objects/Players.hpp"
#include "objects/Entities.hpp"
#include "objects/Entity.hpp"
#include "logic/LevelController.hpp"
#include "util/stringutil.hpp"
#include "engine/Engine.hpp" #include "engine/Engine.hpp"
#include "engine/EnginePaths.hpp" #include "engine/EnginePaths.hpp"
#include "io/io.hpp" #include "io/io.hpp"
#include "audio/audio.hpp" #include "logic/LevelController.hpp"
#include "maths/util.hpp" #include "maths/util.hpp"
#include "debug/Logger.hpp" #include "objects/Entities.hpp"
#include "objects/Entity.hpp"
#include "objects/Player.hpp"
#include "objects/Players.hpp"
#include "ParticlesRenderer.hpp"
#include "settings.hpp"
#include "TextNote.hpp"
#include "TextsRenderer.hpp"
#include "util/stringutil.hpp"
#include "voxels/Block.hpp"
#include "voxels/Chunk.hpp"
#include "voxels/Chunks.hpp"
#include "window/Camera.hpp"
#include "world/Level.hpp"
#include "WorldRenderer.hpp"
namespace fs = std::filesystem; namespace fs = std::filesystem;
@ -48,6 +49,7 @@ Decorator::Decorator(
) )
: level(*controller.getLevel()), : level(*controller.getLevel()),
assets(assets), assets(assets),
settings(engine.getSettings()),
player(player), player(player),
renderer(renderer) { renderer(renderer) {
controller.getBlocksController()->listenBlockInteraction( controller.getBlocksController()->listenBlockInteraction(
@ -90,6 +92,9 @@ void Decorator::addParticles(const Block& def, const glm::ivec3& pos) {
} }
void Decorator::updateAcoustics(const Camera& camera) { void Decorator::updateAcoustics(const Camera& camera) {
if (!settings.audio.acousticEffects.get()) {
return;
}
audio::Acoustics acoustics {}; audio::Acoustics acoustics {};
util::PseudoRandom random(34621U); util::PseudoRandom random(34621U);

View file

@ -20,10 +20,12 @@ class LevelController;
class WorldRenderer; class WorldRenderer;
struct Weather; struct Weather;
struct WeatherPreset; struct WeatherPreset;
struct EngineSettings;
class Decorator { class Decorator {
const Level& level; const Level& level;
const Assets& assets; const Assets& assets;
const EngineSettings& settings;
Player& player; Player& player;
WorldRenderer& renderer; WorldRenderer& renderer;
std::unordered_map<glm::ivec3, uint64_t> blockEmitters; std::unordered_map<glm::ivec3, uint64_t> blockEmitters;