From bc2f854fc8dc1344a8c9db1178e8e5b712dca39e Mon Sep 17 00:00:00 2001 From: LAGonauta Date: Mon, 11 Dec 2023 21:07:52 -0500 Subject: [PATCH] Squashed commit of the following: commit 05daf15bc111d9137555e41d907c7bb4cd82b204 Author: LAGonauta Date: Mon Dec 4 21:20:25 2023 -0500 Add basic settings manager So cvars are not sprinkled on the codebase --- CMakeLists.txt | 1 + include/AudioEngine.hpp | 11 --- include/Config/SettingsManager.hpp | 37 +++++++++ include/Enums/OccluderType.hpp | 10 +++ include/Enums/XFiWorkaround.hpp | 11 +++ include/Loaders/LocalAudioDecoder.hpp | 2 +- src/AudioEngine.cpp | 42 ++++------- src/Config/SettingsManager.cpp | 103 ++++++++++++++++++++++++++ src/Effects/EnvEffects.cpp | 35 ++------- src/Loaders/LocalAudioDecoder.cpp | 2 +- src/Loaders/SoundLoader.cpp | 2 +- src/metaaudio.cpp | 2 + 12 files changed, 187 insertions(+), 71 deletions(-) create mode 100644 include/Config/SettingsManager.hpp create mode 100644 include/Enums/OccluderType.hpp create mode 100644 include/Enums/XFiWorkaround.hpp create mode 100644 src/Config/SettingsManager.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 88d740e..76abf02 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ set(SOURCES src/Utilities/AudioCache.cpp src/interface.cpp src/Config/EfxJsonReader.cpp + src/Config/SettingsManager.cpp src/Effects/GoldSrcOcclusionCalculator.cpp src/Effects/SteamAudioOcclusionCalculator.cpp src/Utilities/Fade.cpp diff --git a/include/AudioEngine.hpp b/include/AudioEngine.hpp index 1bf441c..8b2dc6e 100644 --- a/include/AudioEngine.hpp +++ b/include/AudioEngine.hpp @@ -17,17 +17,6 @@ namespace MetaAudio private: std::unordered_map known_sfx; - //engine cvars - cvar_t* nosound = nullptr; - cvar_t* volume = nullptr; - cvar_t* sxroomwater_type = nullptr; - cvar_t* sxroom_type = nullptr; - cvar_t* snd_show = nullptr; - - //active control - cvar_t* al_doppler = nullptr; - cvar_t* al_xfi_workaround = nullptr; - cvar_t* al_occluder = nullptr; bool openal_started = false; bool openal_mute = false; diff --git a/include/Config/SettingsManager.hpp b/include/Config/SettingsManager.hpp new file mode 100644 index 0000000..98770b6 --- /dev/null +++ b/include/Config/SettingsManager.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include "Enums/XFiWorkaround.hpp" +#include "Enums/OccluderType.hpp" + +namespace MetaAudio +{ + class SettingsManager final + { + public: + void Init(const cl_enginefunc_t& engFuncs); + + bool NoSound(); + + bool OcclusionEnabled(); + bool OcclusionFade(); + + bool ReverbEnabled(); + size_t ReverbType(); + size_t ReverbUnderwaterType(); + + bool SoundShow(); + + float DopplerFactor(); + + float Volume(); + + OccluderType Occluder(); + + XFiWorkaround XfiWorkaround(); + }; +} + +#ifndef _METAAUDIO_SETTINGS_MANAGER_ +#define _METAAUDIO_SETTINGS_MANAGER_ +extern MetaAudio::SettingsManager settings; +#endif \ No newline at end of file diff --git a/include/Enums/OccluderType.hpp b/include/Enums/OccluderType.hpp new file mode 100644 index 0000000..655e595 --- /dev/null +++ b/include/Enums/OccluderType.hpp @@ -0,0 +1,10 @@ +#pragma once + +namespace MetaAudio +{ + enum class OccluderType + { + GoldSrc, + SteamAudio + }; +} \ No newline at end of file diff --git a/include/Enums/XFiWorkaround.hpp b/include/Enums/XFiWorkaround.hpp new file mode 100644 index 0000000..b463a45 --- /dev/null +++ b/include/Enums/XFiWorkaround.hpp @@ -0,0 +1,11 @@ +#pragma once + +namespace MetaAudio +{ + enum class XFiWorkaround + { + Disabled, + Timer, + Streaming + }; +} \ No newline at end of file diff --git a/include/Loaders/LocalAudioDecoder.hpp b/include/Loaders/LocalAudioDecoder.hpp index 26171ab..bb4378a 100644 --- a/include/Loaders/LocalAudioDecoder.hpp +++ b/include/Loaders/LocalAudioDecoder.hpp @@ -14,7 +14,7 @@ namespace MetaAudio private: struct Audio { - wavinfo_t info; + wavinfo_t info{}; alure::Vector data; }; diff --git a/src/AudioEngine.cpp b/src/AudioEngine.cpp index 1f2ae75..9a3bbb3 100644 --- a/src/AudioEngine.cpp +++ b/src/AudioEngine.cpp @@ -8,6 +8,7 @@ #include "Effects/SteamAudioOcclusionCalculator.hpp" #include "SoundSources/SoundSourceFactory.hpp" +#include "Config/SettingsManager.hpp" namespace MetaAudio { @@ -316,13 +317,10 @@ namespace MetaAudio } else { - if (volume) - al_listener.setGain(std::clamp(volume->value, 0.0f, 1.0f)); - else - al_listener.setGain(1.0f); + al_listener.setGain(std::clamp(settings.Volume(), 0.0f, 1.0f)); } - al_context->setDopplerFactor(std::clamp(al_doppler->value, 0.0f, 10.0f)); + al_context->setDopplerFactor(std::clamp(settings.DopplerFactor(), 0.0f, 10.0f)); std::pair alure_orientation( alure::Vector3(orientation[0], orientation[1], orientation[2]), @@ -359,17 +357,15 @@ namespace MetaAudio al_listener.setPosition(AL_UnpackVector(origin)); al_listener.setOrientation(alure_orientation); - int roomtype = 0; bool underwater = (*gAudEngine.cl_waterlevel > 2) ? true : false; - if (sxroomwater_type && sxroom_type) - { - roomtype = underwater ? (int)sxroomwater_type->value : (int)sxroom_type->value; - } + int roomtype = underwater ? + (int)settings.ReverbUnderwaterType() : + (int)settings.ReverbType(); al_efx->InterplEffect(roomtype); channel_manager->ForEachChannel([&](aud_channel_t& channel) { SND_Spatialize(&channel, false); }); - if (snd_show && snd_show->value) + if (settings.SoundShow()) { std::string output; size_t total = 0; @@ -434,7 +430,7 @@ namespace MetaAudio return; } - if (nosound && nosound->value) + if (settings.NoSound()) { return; } @@ -532,7 +528,7 @@ namespace MetaAudio } else { - if (al_xfi_workaround->value == 2.0f || sc->force_streaming) + if (settings.XfiWorkaround() == XFiWorkaround::Streaming || sc->force_streaming) { ch->sound_source = SoundSourceFactory::GetStreamingSource(al_context->createDecoder(sc->buffer.getName()), al_context->createSource(), 16384, 4); } @@ -756,27 +752,15 @@ namespace MetaAudio void AudioEngine::S_Init() { - al_doppler = gEngfuncs.pfnRegisterVariable("al_doppler", "1", FCVAR_EXTDLL); - al_xfi_workaround = gEngfuncs.pfnRegisterVariable("al_xfi_workaround", "0", FCVAR_EXTDLL); - if (gSteamAudio.iplCleanup != nullptr) - { - al_occluder = gEngfuncs.pfnRegisterVariable("al_occluder", "0", FCVAR_EXTDLL); - } - gAudEngine.S_Init(); if (!gEngfuncs.CheckParm("-nosound", NULL)) { - nosound = gEngfuncs.pfnGetCvarPointer("nosound"); - volume = gEngfuncs.pfnGetCvarPointer("volume"); - sxroomwater_type = gEngfuncs.pfnGetCvarPointer("waterroom_type"); - sxroom_type = gEngfuncs.pfnGetCvarPointer("room_type"); - snd_show = gEngfuncs.pfnGetCvarPointer("snd_show"); - S_StopAllSounds(true); } SteamAudio_Init(); + settings.Init(gEngfuncs); AL_ResetEFX(); channel_manager = alure::MakeUnique(); @@ -785,13 +769,13 @@ namespace MetaAudio std::shared_ptr AudioEngine::GetOccluder() { - if (!al_occluder || al_occluder->value == 0.0f) + if (settings.Occluder() == OccluderType::SteamAudio) { - return std::make_shared(*gEngfuncs.pEventAPI); + return std::make_shared(sa_meshloader, *gEngfuncs.pEventAPI); } else { - return std::make_shared(sa_meshloader, *gEngfuncs.pEventAPI); + return std::make_shared(*gEngfuncs.pEventAPI); } } diff --git a/src/Config/SettingsManager.cpp b/src/Config/SettingsManager.cpp new file mode 100644 index 0000000..7f18b20 --- /dev/null +++ b/src/Config/SettingsManager.cpp @@ -0,0 +1,103 @@ +#include "snd_local.h" +#include "Config/SettingsManager.hpp" +#include "dynamic_steamaudio.h" + +extern MetaAudio::SteamAudio gSteamAudio; + +namespace MetaAudio +{ + //engine cvars + static cvar_t* nosound = nullptr; + static cvar_t* snd_show = nullptr; + static cvar_t* sxroom_off = nullptr; + static cvar_t* sxroom_type = nullptr; + static cvar_t* sxroomwater_type = nullptr; + static cvar_t* volume = nullptr; + + //active control + static cvar_t* al_doppler = nullptr; + static cvar_t* al_occluder = nullptr; + static cvar_t* al_occlusion = nullptr; + static cvar_t* al_occlusion_fade = nullptr; + static cvar_t* al_xfi_workaround = nullptr; + + static constexpr char* DEFAULT_XFI_WORKAROUND = "0"; // Disabled + static constexpr char* DEFAULT_OCCLUDER = "0"; // GoldSrc + static constexpr char* DEFAULT_OCCLUSION = "1"; + static constexpr char* DEFAULT_OCCLUSION_FADE = "1"; + static constexpr char* DEFAULT_DOPPLER_FACTOR = "0.3"; + + void SettingsManager::Init(const cl_enginefunc_t& engFuncs) + { + if (al_xfi_workaround == nullptr) al_xfi_workaround = engFuncs.pfnRegisterVariable("al_xfi_workaround", DEFAULT_XFI_WORKAROUND, FCVAR_EXTDLL); + if (al_doppler == nullptr) al_doppler = engFuncs.pfnRegisterVariable("al_doppler", DEFAULT_DOPPLER_FACTOR, FCVAR_EXTDLL); + if (gSteamAudio.iplCleanup != nullptr && al_occluder == nullptr) al_occluder = engFuncs.pfnRegisterVariable("al_occluder", DEFAULT_OCCLUDER, FCVAR_EXTDLL); + if (al_occlusion == nullptr) al_occlusion = engFuncs.pfnRegisterVariable("al_occlusion", DEFAULT_OCCLUSION, FCVAR_EXTDLL); + if (al_occlusion_fade == nullptr) al_occlusion_fade = engFuncs.pfnRegisterVariable("al_occlusion_fade", DEFAULT_OCCLUSION_FADE, FCVAR_EXTDLL); + + if (!gEngfuncs.CheckParm("-nosound", NULL)) + { + if (nosound == nullptr) nosound = engFuncs.pfnGetCvarPointer("nosound"); + if (volume == nullptr) volume = engFuncs.pfnGetCvarPointer("volume"); + if (sxroomwater_type == nullptr) sxroomwater_type = engFuncs.pfnGetCvarPointer("waterroom_type"); + if (sxroom_type == nullptr) sxroom_type = engFuncs.pfnGetCvarPointer("room_type"); + if (sxroom_off == nullptr) sxroom_off = engFuncs.pfnGetCvarPointer("room_off"); + if (snd_show == nullptr) snd_show = engFuncs.pfnGetCvarPointer("snd_show"); + } + } + + bool SettingsManager::NoSound() + { + return nosound == nullptr || static_cast(nosound->value); + } + + bool SettingsManager::SoundShow() + { + return static_cast(snd_show->value); + } + + float SettingsManager::DopplerFactor() + { + return al_doppler->value; + } + + float SettingsManager::Volume() + { + return volume->value; + } + + bool SettingsManager::ReverbEnabled() + { + return !static_cast(sxroom_off->value); + } + + size_t SettingsManager::ReverbType() + { + return static_cast(sxroom_type->value); + } + + size_t SettingsManager::ReverbUnderwaterType() + { + return static_cast(sxroomwater_type->value); + } + + XFiWorkaround SettingsManager::XfiWorkaround() + { + return static_cast(al_xfi_workaround->value); + } + + OccluderType SettingsManager::Occluder() + { + return al_occluder == nullptr ? OccluderType::GoldSrc : static_cast(al_occluder->value); + } + + bool SettingsManager::OcclusionEnabled() + { + return static_cast(al_occlusion->value); + } + + bool SettingsManager::OcclusionFade() + { + return static_cast(al_occlusion_fade->value); + } +} \ No newline at end of file diff --git a/src/Effects/EnvEffects.cpp b/src/Effects/EnvEffects.cpp index f5dee57..8fdcc2b 100644 --- a/src/Effects/EnvEffects.cpp +++ b/src/Effects/EnvEffects.cpp @@ -7,6 +7,7 @@ #include "event_api.h" #include "snd_local.h" #include "Config/EfxJsonReader.hpp" +#include "Config/SettingsManager.hpp" #include "Effects/EnvEffects.hpp" #include "Effects/GoldSrcOcclusionCalculator.hpp" #include "Effects/SteamAudioOcclusionCalculator.hpp" @@ -14,15 +15,8 @@ #include "Workarounds/XFiWorkarounds.hpp" #include "SoundSources/BaseSoundSource.hpp" -extern cvar_t* sxroomwater_type; -extern cvar_t* sxroom_type; - namespace MetaAudio { - static cvar_t* al_occlusion = nullptr; - static cvar_t* al_occlusion_fade = nullptr; - static cvar_t* sxroom_off = nullptr; - // HL1 DSPROPERTY_EAXBUFFER_REVERBMIX seems to be always set to 0.38, // with no adjustment of reverb intensity with distance. // Reverb adjustment with distance is disabled per-source. @@ -55,7 +49,7 @@ namespace MetaAudio void EnvEffects::InterplEffect(int roomtype) { EFXEAXREVERBPROPERTIES desired = presets_room[0]; - if (roomtype > 0 && roomtype < CSXROOM && sxroom_off && !sxroom_off->value) + if (roomtype > 0 && roomtype < CSXROOM && settings.ReverbEnabled()) { desired = presets_room[roomtype]; } @@ -115,7 +109,7 @@ namespace MetaAudio if (ch->entnum != *gAudEngine.cl_viewentity && pent != nullptr && sent != nullptr) { // Detect collisions and reduce gain on occlusion - if (al_occlusion->value) + if (settings.OcclusionEnabled()) { // Check occlusion only on those entities that can be heard. float distance = alure::Vector3(ch->origin[0], ch->origin[1], ch->origin[2]).getDistanceSquared( @@ -156,9 +150,9 @@ namespace MetaAudio ch->HighGain.target = 1.0f; } - FadeToNewValue(al_occlusion_fade->value, ch->firstpass, ch->LowGain); - FadeToNewValue(al_occlusion_fade->value, ch->firstpass, ch->MidGain); - FadeToNewValue(al_occlusion_fade->value, ch->firstpass, ch->HighGain); + FadeToNewValue(settings.OcclusionFade(), ch->firstpass, ch->LowGain); + FadeToNewValue(settings.OcclusionFade(), ch->firstpass, ch->MidGain); + FadeToNewValue(settings.OcclusionFade(), ch->firstpass, ch->HighGain); params.mGain = ch->MidGain.current; params.mGainHF = ch->HighGain.current; @@ -189,21 +183,6 @@ namespace MetaAudio EnvEffects::EnvEffects(alure::Context& al_context, ALCuint max_sends, std::shared_ptr occlusion_calculator) : occlusion_calculator(occlusion_calculator) { - if (al_occlusion == nullptr) - { - al_occlusion = gEngfuncs.pfnRegisterVariable("al_occlusion", "1", FCVAR_EXTDLL); - } - - if (al_occlusion_fade == nullptr) - { - al_occlusion_fade = gEngfuncs.pfnRegisterVariable("al_occlusion_fade", "1", FCVAR_EXTDLL); - } - - if (sxroom_off == nullptr) - { - sxroom_off = gEngfuncs.pfnGetCvarPointer("room_off"); - } - const char* _al_maxsends; CommandLine()->CheckParm("-al_maxsends", &_al_maxsends); @@ -381,7 +360,7 @@ namespace MetaAudio void EnvEffects::OverrideEffects() { - std::array directory; + std::array directory{}; FILESYSTEM_ANY_GETCURRENTDIRECTORY(directory.data(), directory.size()); diff --git a/src/Loaders/LocalAudioDecoder.cpp b/src/Loaders/LocalAudioDecoder.cpp index 1173a55..03a2eca 100644 --- a/src/Loaders/LocalAudioDecoder.cpp +++ b/src/Loaders/LocalAudioDecoder.cpp @@ -20,7 +20,7 @@ namespace MetaAudio return false; } - auto audioData = m_data[full_path]; + auto& audioData = m_data[full_path]; info = audioData.info; auto loop_points = dec->getLoopPoints(); diff --git a/src/Loaders/SoundLoader.cpp b/src/Loaders/SoundLoader.cpp index ddd600d..0dab2e8 100644 --- a/src/Loaders/SoundLoader.cpp +++ b/src/Loaders/SoundLoader.cpp @@ -193,7 +193,7 @@ namespace MetaAudio if (sc == nullptr) return nullptr; - wavinfo_t info = wavinfo_t(); + wavinfo_t info{}; //We can't interfere with Alure, so we need a copy of the data for mouth movement. if (!m_decoder->GetWavinfo(info, file_path.value(), sc->data)) return nullptr; diff --git a/src/metaaudio.cpp b/src/metaaudio.cpp index f571291..7b2a288 100644 --- a/src/metaaudio.cpp +++ b/src/metaaudio.cpp @@ -4,8 +4,10 @@ #include "Utilities/AudioCache.hpp" #include "Loaders/SoundLoader.hpp" #include "Vox/VoxManager.hpp" +#include "Config/SettingsManager.hpp" #include "AudioEngine.hpp" +MetaAudio::SettingsManager settings; MetaAudio::SteamAudio gSteamAudio; static std::shared_ptr sound_loader; static std::unique_ptr audio_engine;