From 3757571d4697e4253193fd7019a3b189e5eec3b4 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 14 Sep 2017 04:48:12 -0700 Subject: [PATCH] Set HRTF when initializing the device --- apps/openmw/mwsound/openal_output.cpp | 103 ++++++++++++++++-------- apps/openmw/mwsound/openal_output.hpp | 10 +-- apps/openmw/mwsound/sound_output.hpp | 11 ++- apps/openmw/mwsound/soundmanagerimp.cpp | 11 +-- 4 files changed, 85 insertions(+), 50 deletions(-) diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index a6258babe..b3ba4a07f 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -592,7 +592,7 @@ std::vector OpenAL_Output::enumerate() return devlist; } -bool OpenAL_Output::init(const std::string &devname) +bool OpenAL_Output::init(const std::string &devname, const std::string &hrtfname, HrtfMode hrtfmode) { deinit(); @@ -615,7 +615,47 @@ bool OpenAL_Output::init(const std::string &devname) std::cout << "Opened \""< attrs; + attrs.reserve(15); + if(ALC.SOFT_HRTF) + { + LPALCGETSTRINGISOFT alcGetStringiSOFT = 0; + getALCFunc(alcGetStringiSOFT, mDevice, "alcGetStringiSOFT"); + + attrs.push_back(ALC_HRTF_SOFT); + attrs.push_back(hrtfmode == HrtfMode::Disable ? ALC_FALSE : + hrtfmode == HrtfMode::Enable ? ALC_TRUE : + /*hrtfmode == HrtfMode::Auto ?*/ ALC_DONT_CARE_SOFT); + if(!hrtfname.empty()) + { + ALCint index = -1; + ALCint num_hrtf; + alcGetIntegerv(mDevice, ALC_NUM_HRTF_SPECIFIERS_SOFT, 1, &num_hrtf); + for(ALCint i = 0;i < num_hrtf;++i) + { + const ALCchar *entry = alcGetStringiSOFT(mDevice, ALC_HRTF_SPECIFIER_SOFT, i); + if(hrtfname == entry) + { + index = i; + break; + } + } + + if(index < 0) + std::cerr<< "Failed to find HRTF name \""< OpenAL_Output::enumerateHrtf() { std::vector ret; - if(!mDevice || !alcIsExtensionPresent(mDevice, "ALC_SOFT_HRTF")) + if(!mDevice || !ALC.SOFT_HRTF) return ret; LPALCGETSTRINGISOFT alcGetStringiSOFT = 0; @@ -823,9 +877,9 @@ std::vector OpenAL_Output::enumerateHrtf() return ret; } -void OpenAL_Output::enableHrtf(const std::string &hrtfname, bool auto_enable) +void OpenAL_Output::setHrtf(const std::string &hrtfname, HrtfMode hrtfmode) { - if(!alcIsExtensionPresent(mDevice, "ALC_SOFT_HRTF")) + if(!mDevice || !ALC.SOFT_HRTF) { std::cerr<< "HRTF extension not present" < attrs; + attrs.reserve(15); + attrs.push_back(ALC_HRTF_SOFT); - attrs.push_back(auto_enable ? ALC_DONT_CARE_SOFT : ALC_TRUE); + attrs.push_back(hrtfmode == HrtfMode::Disable ? ALC_FALSE : + hrtfmode == HrtfMode::Enable ? ALC_TRUE : + /*hrtfmode == HrtfMode::Auto ?*/ ALC_DONT_CARE_SOFT); if(!hrtfname.empty()) { ALCint index = -1; @@ -864,12 +922,12 @@ void OpenAL_Output::enableHrtf(const std::string &hrtfname, bool auto_enable) } } attrs.push_back(0); - alcResetDeviceSOFT(mDevice, &attrs[0]); + alcResetDeviceSOFT(mDevice, attrs.data()); ALCint hrtf_state; alcGetIntegerv(mDevice, ALC_HRTF_SOFT, 1, &hrtf_state); if(!hrtf_state) - std::cerr<< "Failed to enable HRTF" < attrs; - attrs.push_back(ALC_HRTF_SOFT); - attrs.push_back(ALC_FALSE); - attrs.push_back(0); - alcResetDeviceSOFT(mDevice, &attrs[0]); - - ALCint hrtf_state; - alcGetIntegerv(mDevice, ALC_HRTF_SOFT, 1, &hrtf_state); - if(hrtf_state) - std::cerr<< "Failed to disable HRTF" < IDDq; @@ -59,12 +60,11 @@ namespace MWSound public: virtual std::vector enumerate(); - virtual bool init(const std::string &devname=std::string()); + virtual bool init(const std::string &devname, const std::string &hrtfname, HrtfMode hrtfmode); virtual void deinit(); virtual std::vector enumerateHrtf(); - virtual void enableHrtf(const std::string &hrtfname, bool auto_enable); - virtual void disableHrtf(); + virtual void setHrtf(const std::string &hrtfname, HrtfMode hrtfmode); virtual Sound_Handle loadSound(const std::string &fname); virtual void unloadSound(Sound_Handle data); diff --git a/apps/openmw/mwsound/sound_output.hpp b/apps/openmw/mwsound/sound_output.hpp index 01dc8b5b9..ad18e0d40 100644 --- a/apps/openmw/mwsound/sound_output.hpp +++ b/apps/openmw/mwsound/sound_output.hpp @@ -19,17 +19,22 @@ namespace MWSound // An opaque handle for the implementation's sound instances. typedef void *Sound_Instance; + enum class HrtfMode { + Disable, + Enable, + Auto + }; + class Sound_Output { SoundManager &mManager; virtual std::vector enumerate() = 0; - virtual bool init(const std::string &devname=std::string()) = 0; + virtual bool init(const std::string &devname, const std::string &hrtfname, HrtfMode hrtfmode) = 0; virtual void deinit() = 0; virtual std::vector enumerateHrtf() = 0; - virtual void enableHrtf(const std::string &hrtfname, bool auto_enable) = 0; - virtual void disableHrtf() = 0; + virtual void setHrtf(const std::string &hrtfname, HrtfMode hrtfmode) = 0; virtual Sound_Handle loadSound(const std::string &fname) = 0; virtual void unloadSound(Sound_Handle data) = 0; diff --git a/apps/openmw/mwsound/soundmanagerimp.cpp b/apps/openmw/mwsound/soundmanagerimp.cpp index 51ce67096..7310e457f 100644 --- a/apps/openmw/mwsound/soundmanagerimp.cpp +++ b/apps/openmw/mwsound/soundmanagerimp.cpp @@ -85,6 +85,8 @@ namespace MWSound 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; std::cout << "Sound output: " << SOUND_OUT << std::endl; std::cout << "Sound decoder: " << SOUND_IN << std::endl; @@ -98,11 +100,11 @@ namespace MWSound std::cout.flush(); std::string devname = Settings::Manager::getString("device", "Sound"); - bool inited = mOutput->init(devname); + bool inited = mOutput->init(devname, hrtfname, hrtfmode); if(!inited && !devname.empty()) { std::cerr<< "Failed to initialize device \""<init(); + inited = mOutput->init(std::string(), hrtfname, hrtfmode); } if(!inited) { @@ -120,11 +122,6 @@ namespace MWSound ); std::cout.flush(); } - - if(hrtfstate == 0) - mOutput->disableHrtf(); - else if(!hrtfname.empty()) - mOutput->enableHrtf(hrtfname, hrtfstate<0); } SoundManager::~SoundManager()