diff --git a/apps/launcher/settingspage.cpp b/apps/launcher/settingspage.cpp index 07570272dd..5869cc3a73 100644 --- a/apps/launcher/settingspage.cpp +++ b/apps/launcher/settingspage.cpp @@ -55,6 +55,20 @@ namespace { value.set(spinBox.value()); } + + int toIndex(Settings::HrtfMode value) + { + switch (value) + { + case Settings::HrtfMode::Auto: + return 0; + case Settings::HrtfMode::Disable: + return 1; + case Settings::HrtfMode::Enable: + return 2; + } + return 0; + } } Launcher::SettingsPage::SettingsPage(Config::GameSettings& gameSettings, QWidget* parent) @@ -210,11 +224,7 @@ bool Launcher::SettingsPage::loadSettings() audioDeviceSelectorComboBox->setCurrentIndex(audioDeviceIndex); } } - const int hrtfEnabledIndex = Settings::sound().mHrtfEnable; - if (hrtfEnabledIndex >= -1 && hrtfEnabledIndex <= 1) - { - enableHRTFComboBox->setCurrentIndex(hrtfEnabledIndex + 1); - } + enableHRTFComboBox->setCurrentIndex(toIndex(Settings::sound().mHrtfEnable)); const std::string& selectedHRTFProfile = Settings::sound().mHrtf; if (selectedHRTFProfile.empty() == false) { @@ -356,7 +366,12 @@ void Launcher::SettingsPage::saveSettings() else Settings::sound().mDevice.set({}); - Settings::sound().mHrtfEnable.set(enableHRTFComboBox->currentIndex() - 1); + static constexpr std::array hrtfModes{ + Settings::HrtfMode::Auto, + Settings::HrtfMode::Disable, + Settings::HrtfMode::Enable, + }; + Settings::sound().mHrtfEnable.set(hrtfModes[enableHRTFComboBox->currentIndex()]); if (hrtfProfileSelectorComboBox->currentIndex() != 0) Settings::sound().mHrtf.set(hrtfProfileSelectorComboBox->currentText().toStdString()); diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt index 16aea9736b..57e2b9d708 100644 --- a/apps/openmw/CMakeLists.txt +++ b/apps/openmw/CMakeLists.txt @@ -69,7 +69,7 @@ add_openmw_dir (mwlua add_openmw_dir (mwsound soundmanagerimp openal_output ffmpeg_decoder sound sound_buffer sound_decoder sound_output - loudness movieaudiofactory alext efx efx-presets regionsoundselector watersoundupdater volumesettings + loudness movieaudiofactory alext efx efx-presets regionsoundselector watersoundupdater ) add_openmw_dir (mwworld diff --git a/apps/openmw/mwsound/sound_buffer.cpp b/apps/openmw/mwsound/sound_buffer.cpp index d2645a1fe8..a3fdcb8b5c 100644 --- a/apps/openmw/mwsound/sound_buffer.cpp +++ b/apps/openmw/mwsound/sound_buffer.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include @@ -37,11 +37,9 @@ namespace MWSound SoundBufferPool::SoundBufferPool(Sound_Output& output) : mOutput(&output) - , mBufferCacheMax(std::max(Settings::Manager::getInt("buffer cache max", "Sound"), 1) * 1024 * 1024) + , mBufferCacheMax(Settings::sound().mBufferCacheMax * 1024 * 1024) , mBufferCacheMin( - std::min(static_cast(std::max(Settings::Manager::getInt("buffer cache min", "Sound"), 1)) - * 1024 * 1024, - mBufferCacheMax)) + std::min(static_cast(Settings::sound().mBufferCacheMin) * 1024 * 1024, mBufferCacheMax)) { } diff --git a/apps/openmw/mwsound/sound_output.hpp b/apps/openmw/mwsound/sound_output.hpp index d7c35fbbc6..8eec20bcba 100644 --- a/apps/openmw/mwsound/sound_output.hpp +++ b/apps/openmw/mwsound/sound_output.hpp @@ -5,6 +5,8 @@ #include #include +#include + #include "../mwbase/soundmanager.hpp" namespace MWSound @@ -19,19 +21,14 @@ namespace MWSound // An opaque handle for the implementation's sound instances. typedef void* Sound_Instance; - enum class HrtfMode - { - Disable, - Enable, - Auto - }; - enum Environment { Env_Normal, Env_Underwater }; + using HrtfMode = Settings::HrtfMode; + class Sound_Output { SoundManager& mManager; diff --git a/apps/openmw/mwsound/soundmanagerimp.cpp b/apps/openmw/mwsound/soundmanagerimp.cpp index 73cfeba3db..64f8959218 100644 --- a/apps/openmw/mwsound/soundmanagerimp.cpp +++ b/apps/openmw/mwsound/soundmanagerimp.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -73,6 +74,33 @@ namespace MWSound return 1.0; } + + // Gets the combined volume settings for the given sound type + float volumeFromType(Type type) + { + float volume = Settings::sound().mMasterVolume; + + switch (type) + { + case Type::Sfx: + volume *= Settings::sound().mSfxVolume; + break; + case Type::Voice: + volume *= Settings::sound().mVoiceVolume; + break; + case Type::Foot: + volume *= Settings::sound().mFootstepsVolume; + break; + case Type::Music: + volume *= Settings::sound().mMusicVolume; + break; + case Type::Movie: + case Type::Mask: + break; + } + + return volume; + } } // For combining PlayMode and Type flags @@ -103,12 +131,7 @@ namespace MWSound return; } - const std::string& hrtfname = Settings::Manager::getString("hrtf", "Sound"); - int hrtfstate = Settings::Manager::getInt("hrtf enable", "Sound"); - HrtfMode hrtfmode = hrtfstate < 0 ? HrtfMode::Auto : hrtfstate > 0 ? HrtfMode::Enable : HrtfMode::Disable; - - const std::string& devname = Settings::Manager::getString("device", "Sound"); - if (!mOutput->init(devname, hrtfname, hrtfmode)) + if (!mOutput->init(Settings::sound().mDevice, Settings::sound().mHrtf, Settings::sound().mHrtfEnable)) { Log(Debug::Error) << "Failed to initialize audio output, sound disabled"; return; @@ -219,12 +242,6 @@ namespace MWSound return sound; } - // Gets the combined volume settings for the given sound type - float SoundManager::volumeFromType(Type type) const - { - return mVolumeSettings.getVolumeFromType(type); - } - void SoundManager::stopMusic() { if (mMusic) @@ -1156,8 +1173,6 @@ namespace MWSound void SoundManager::processChangedSettings(const Settings::CategorySettingVector& settings) { - mVolumeSettings.update(); - if (!mOutput->isInitialized()) return; mOutput->startUpdate(); diff --git a/apps/openmw/mwsound/soundmanagerimp.hpp b/apps/openmw/mwsound/soundmanagerimp.hpp index 94d407c11b..6154d202cd 100644 --- a/apps/openmw/mwsound/soundmanagerimp.hpp +++ b/apps/openmw/mwsound/soundmanagerimp.hpp @@ -16,7 +16,6 @@ #include "regionsoundselector.hpp" #include "sound_buffer.hpp" #include "type.hpp" -#include "volumesettings.hpp" #include "watersoundupdater.hpp" namespace VFS @@ -57,8 +56,6 @@ namespace MWSound std::unordered_map> mMusicToPlay; // A list with music files not yet played std::string mLastPlayedMusic; // The music file that was last played - VolumeSettings mVolumeSettings; - WaterSoundUpdater mWaterSoundUpdater; SoundBufferPool mSoundBuffers; @@ -144,8 +141,6 @@ namespace MWSound void updateWaterSound(); void updateMusic(float duration); - float volumeFromType(Type type) const; - enum class WaterSoundAction { DoNothing, diff --git a/apps/openmw/mwsound/volumesettings.cpp b/apps/openmw/mwsound/volumesettings.cpp deleted file mode 100644 index 4306bc5268..0000000000 --- a/apps/openmw/mwsound/volumesettings.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "volumesettings.hpp" - -#include - -#include - -namespace MWSound -{ - namespace - { - float clamp(float value) - { - return std::clamp(value, 0.f, 1.f); - } - } - - VolumeSettings::VolumeSettings() - : mMasterVolume(clamp(Settings::Manager::getFloat("master volume", "Sound"))) - , mSFXVolume(clamp(Settings::Manager::getFloat("sfx volume", "Sound"))) - , mMusicVolume(clamp(Settings::Manager::getFloat("music volume", "Sound"))) - , mVoiceVolume(clamp(Settings::Manager::getFloat("voice volume", "Sound"))) - , mFootstepsVolume(clamp(Settings::Manager::getFloat("footsteps volume", "Sound"))) - { - } - - float VolumeSettings::getVolumeFromType(Type type) const - { - float volume = mMasterVolume; - - switch (type) - { - case Type::Sfx: - volume *= mSFXVolume; - break; - case Type::Voice: - volume *= mVoiceVolume; - break; - case Type::Foot: - volume *= mFootstepsVolume; - break; - case Type::Music: - volume *= mMusicVolume; - break; - case Type::Movie: - case Type::Mask: - break; - } - - return volume; - } - - void VolumeSettings::update() - { - *this = VolumeSettings(); - } -} diff --git a/apps/openmw/mwsound/volumesettings.hpp b/apps/openmw/mwsound/volumesettings.hpp deleted file mode 100644 index 884a3e1bca..0000000000 --- a/apps/openmw/mwsound/volumesettings.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef GAME_SOUND_VOLUMESETTINGS_H -#define GAME_SOUND_VOLUMESETTINGS_H - -#include "type.hpp" - -namespace MWSound -{ - class VolumeSettings - { - public: - VolumeSettings(); - - float getVolumeFromType(Type type) const; - - void update(); - - private: - float mMasterVolume; - float mSFXVolume; - float mMusicVolume; - float mVoiceVolume; - float mFootstepsVolume; - }; -} - -#endif diff --git a/components/settings/categories/sound.hpp b/components/settings/categories/sound.hpp index f4ce7e0269..43313e622d 100644 --- a/components/settings/categories/sound.hpp +++ b/components/settings/categories/sound.hpp @@ -1,16 +1,11 @@ #ifndef OPENMW_COMPONENTS_SETTINGS_CATEGORIES_SOUND_H #define OPENMW_COMPONENTS_SETTINGS_CATEGORIES_SOUND_H +#include "components/settings/hrtfmode.hpp" #include "components/settings/sanitizerimpl.hpp" #include "components/settings/settingvalue.hpp" -#include -#include -#include - -#include #include -#include namespace Settings { @@ -26,7 +21,7 @@ namespace Settings SettingValue mVoiceVolume{ mIndex, "Sound", "voice volume", makeClampSanitizerFloat(0, 1) }; SettingValue mBufferCacheMin{ mIndex, "Sound", "buffer cache min", makeMaxSanitizerInt(1) }; SettingValue mBufferCacheMax{ mIndex, "Sound", "buffer cache max", makeMaxSanitizerInt(1) }; - SettingValue mHrtfEnable{ mIndex, "Sound", "hrtf enable", makeEnumSanitizerInt({ -1, 0, 1 }) }; + SettingValue mHrtfEnable{ mIndex, "Sound", "hrtf enable" }; SettingValue mHrtf{ mIndex, "Sound", "hrtf" }; }; } diff --git a/components/settings/hrtfmode.hpp b/components/settings/hrtfmode.hpp new file mode 100644 index 0000000000..9b741922b5 --- /dev/null +++ b/components/settings/hrtfmode.hpp @@ -0,0 +1,14 @@ +#ifndef OPENMW_COMPONENTS_SETTINGS_HRTFMODE_H +#define OPENMW_COMPONENTS_SETTINGS_HRTFMODE_H + +namespace Settings +{ + enum class HrtfMode + { + Disable, + Enable, + Auto, + }; +} + +#endif diff --git a/components/settings/settings.cpp b/components/settings/settings.cpp index bab8f41c1d..7b31bf6aad 100644 --- a/components/settings/settings.cpp +++ b/components/settings/settings.cpp @@ -103,6 +103,22 @@ namespace Settings throw std::invalid_argument("Invalid LightingMethod value: " + std::to_string(static_cast(value))); } + + int toInt(HrtfMode value) + { + switch (value) + { + case HrtfMode::Auto: + return -1; + case HrtfMode::Disable: + return 0; + case HrtfMode::Enable: + return 1; + } + + Log(Debug::Warning) << "Invalid HRTF mode value: " << static_cast(value) << ", fallback to auto (-1)"; + return -1; + } } CategorySettingValueMap Manager::mDefaultSettings = CategorySettingValueMap(); @@ -480,6 +496,11 @@ namespace Settings setString(setting, category, toString(value)); } + void Manager::set(std::string_view setting, std::string_view category, HrtfMode value) + { + setInt(setting, category, toInt(value)); + } + void Manager::recordInit(std::string_view setting, std::string_view category) { sInitialized.emplace(category, setting); diff --git a/components/settings/settings.hpp b/components/settings/settings.hpp index 0177b5fbf2..a5b75fd445 100644 --- a/components/settings/settings.hpp +++ b/components/settings/settings.hpp @@ -3,6 +3,7 @@ #include "categories.hpp" #include "gyroscopeaxis.hpp" +#include "hrtfmode.hpp" #include "navmeshrendermode.hpp" #include @@ -112,6 +113,7 @@ namespace Settings static void set(std::string_view setting, std::string_view category, const std::vector& value); static void set(std::string_view setting, std::string_view category, const MyGUI::Colour& value); static void set(std::string_view setting, std::string_view category, SceneUtil::LightingMethod value); + static void set(std::string_view setting, std::string_view category, HrtfMode value); private: static std::set> sInitialized; @@ -226,6 +228,17 @@ namespace Settings { return parseLightingMethod(getString(setting, category)); } + + template <> + inline HrtfMode Manager::getImpl(std::string_view setting, std::string_view category) + { + const int value = getInt(setting, category); + if (value < 0) + return HrtfMode::Auto; + if (value > 0) + return HrtfMode::Enable; + return HrtfMode::Disable; + } } #endif // COMPONENTS_SETTINGS_H diff --git a/components/settings/settingvalue.hpp b/components/settings/settingvalue.hpp index 35726cb25b..7b49c9bda2 100644 --- a/components/settings/settingvalue.hpp +++ b/components/settings/settingvalue.hpp @@ -41,6 +41,7 @@ namespace Settings GyroscopeAxis, NavMeshRenderMode, LightingMethod, + HrtfMode, }; template @@ -154,6 +155,12 @@ namespace Settings return SettingValueType::LightingMethod; } + template <> + inline constexpr SettingValueType getSettingValueType() + { + return SettingValueType::HrtfMode; + } + inline constexpr std::string_view getSettingValueTypeName(SettingValueType type) { switch (type) @@ -194,6 +201,8 @@ namespace Settings return "navmesh render mode"; case SettingValueType::LightingMethod: return "lighting method"; + case SettingValueType::HrtfMode: + return "hrtf mode"; } return "unsupported"; }