Skip to content

Commit

Permalink
Add basic settings manager
Browse files Browse the repository at this point in the history
So cvars are not sprinkled on the codebase
  • Loading branch information
LAGonauta committed Dec 5, 2023
1 parent 014ee07 commit 05daf15
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 71 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions include/AudioEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@ namespace MetaAudio
private:
std::unordered_map<alure::String, sfx_t> 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;

Expand Down
37 changes: 37 additions & 0 deletions include/Config/SettingsManager.hpp
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions include/Enums/OccluderType.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

namespace MetaAudio
{
enum class OccluderType
{
GoldSrc,
SteamAudio
};
}
11 changes: 11 additions & 0 deletions include/Enums/XFiWorkaround.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

namespace MetaAudio
{
enum class XFiWorkaround
{
Disabled,
Timer,
Streaming
};
}
2 changes: 1 addition & 1 deletion include/Loaders/LocalAudioDecoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace MetaAudio
private:
struct Audio
{
wavinfo_t info;
wavinfo_t info{};
alure::Vector<ALubyte> data;
};

Expand Down
42 changes: 13 additions & 29 deletions src/AudioEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Effects/SteamAudioOcclusionCalculator.hpp"

#include "SoundSources/SoundSourceFactory.hpp"
#include "Config/SettingsManager.hpp"

namespace MetaAudio
{
Expand Down Expand Up @@ -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::Vector3, alure::Vector3> alure_orientation(
alure::Vector3(orientation[0], orientation[1], orientation[2]),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -434,7 +430,7 @@ namespace MetaAudio
return;
}

if (nosound && nosound->value)
if (settings.NoSound())
{
return;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<ChannelManager>();
Expand All @@ -785,13 +769,13 @@ namespace MetaAudio

std::shared_ptr<IOcclusionCalculator> AudioEngine::GetOccluder()
{
if (!al_occluder || al_occluder->value == 0.0f)
if (settings.Occluder() == OccluderType::SteamAudio)
{
return std::make_shared<GoldSrcOcclusionCalculator>(*gEngfuncs.pEventAPI);
return std::make_shared<SteamAudioOcclusionCalculator>(sa_meshloader, *gEngfuncs.pEventAPI);
}
else
{
return std::make_shared<SteamAudioOcclusionCalculator>(sa_meshloader, *gEngfuncs.pEventAPI);
return std::make_shared<GoldSrcOcclusionCalculator>(*gEngfuncs.pEventAPI);
}
}

Expand Down
103 changes: 103 additions & 0 deletions src/Config/SettingsManager.cpp
Original file line number Diff line number Diff line change
@@ -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<bool>(nosound->value);
}

bool SettingsManager::SoundShow()
{
return static_cast<bool>(snd_show->value);
}

float SettingsManager::DopplerFactor()
{
return al_doppler->value;
}

float SettingsManager::Volume()
{
return volume->value;
}

bool SettingsManager::ReverbEnabled()
{
return !static_cast<bool>(sxroom_off->value);
}

size_t SettingsManager::ReverbType()
{
return static_cast<size_t>(sxroom_type->value);
}

size_t SettingsManager::ReverbUnderwaterType()
{
return static_cast<size_t>(sxroomwater_type->value);
}

XFiWorkaround SettingsManager::XfiWorkaround()
{
return static_cast<XFiWorkaround>(al_xfi_workaround->value);
}

OccluderType SettingsManager::Occluder()
{
return al_occluder == nullptr ? OccluderType::GoldSrc : static_cast<OccluderType>(al_occluder->value);
}

bool SettingsManager::OcclusionEnabled()
{
return static_cast<bool>(al_occlusion->value);
}

bool SettingsManager::OcclusionFade()
{
return static_cast<bool>(al_occlusion_fade->value);
}
}
35 changes: 7 additions & 28 deletions src/Effects/EnvEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,16 @@
#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"
#include "Workarounds/NoWorkarounds.hpp"
#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.
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -189,21 +183,6 @@ namespace MetaAudio

EnvEffects::EnvEffects(alure::Context& al_context, ALCuint max_sends, std::shared_ptr<IOcclusionCalculator> 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);

Expand Down Expand Up @@ -381,7 +360,7 @@ namespace MetaAudio

void EnvEffects::OverrideEffects()
{
std::array<char, 256> directory;
std::array<char, 256> directory{};

FILESYSTEM_ANY_GETCURRENTDIRECTORY(directory.data(), directory.size());

Expand Down
Loading

0 comments on commit 05daf15

Please sign in to comment.