diff --git a/Source_Files/Sound/AudioPlayer.cpp b/Source_Files/Sound/AudioPlayer.cpp index 602b59c56..78bbb6b1e 100644 --- a/Source_Files/Sound/AudioPlayer.cpp +++ b/Source_Files/Sound/AudioPlayer.cpp @@ -140,5 +140,11 @@ bool AudioPlayer::SetUpALSourceInit() { alSourcei(audio_source->source_id, AL_REFERENCE_DISTANCE, 0); alSourcei(audio_source->source_id, AL_MAX_DISTANCE, 0); alSourcei(audio_source->source_id, AL_DIRECT_FILTER, AL_FILTER_NULL); + +#ifdef AL_SOFT_source_spatialize + if (OpenALManager::Get()->IsExtensionSupported(OpenALManager::OptionalExtension::Spatialization)) + alSourcei(audio_source->source_id, AL_SOURCE_SPATIALIZE_SOFT, AL_AUTO_SOFT); +#endif + return alGetError() == AL_NO_ERROR; } \ No newline at end of file diff --git a/Source_Files/Sound/OpenALManager.cpp b/Source_Files/Sound/OpenALManager.cpp index c95c4ffcb..2160ac855 100644 --- a/Source_Files/Sound/OpenALManager.cpp +++ b/Source_Files/Sound/OpenALManager.cpp @@ -43,7 +43,15 @@ bool OpenALManager::Init(const AudioParameters& parameters) { } instance = new OpenALManager(parameters); - return instance->OpenDevice() && instance->GenerateSources() && instance->GenerateEffects(); + return instance->OpenDevice() && instance->LoadOptionalExtensions() && instance->GenerateSources() && instance->GenerateEffects(); +} + +bool OpenALManager::LoadOptionalExtensions() { + + for (auto& extension : mapping_extensions_names) + extension_support[extension.first] = alIsExtensionPresent(extension.second.c_str()); + + return true; } void OpenALManager::ProcessAudioQueue() { diff --git a/Source_Files/Sound/OpenALManager.h b/Source_Files/Sound/OpenALManager.h index ba3b3b17b..c052ec161 100644 --- a/Source_Files/Sound/OpenALManager.h +++ b/Source_Files/Sound/OpenALManager.h @@ -48,6 +48,12 @@ struct AudioParameters { class OpenALManager { public: + + enum class OptionalExtension + { + Spatialization + }; + static OpenALManager* Get() { return instance; } static bool Init(const AudioParameters& parameters); static void Shutdown(); @@ -74,6 +80,7 @@ class OpenALManager { bool IsPaused() const { return paused_audio; } ALCint GetRenderingFormat() const { return openal_rendering_format; } ALuint GetLowPassFilter(float highFrequencyGain) const; + bool IsExtensionSupported(OptionalExtension extension) const { return extension_support.at(extension); } private: static OpenALManager* instance; ALCdevice* p_ALCDevice = nullptr; @@ -116,6 +123,9 @@ class OpenALManager { static LPALFILTERI alFilteri; static LPALFILTERF alFilterf; + std::unordered_map extension_support; + bool LoadOptionalExtensions(); + static void MixerCallback(void* usr, uint8* stream, int len); SDL_AudioSpec sdl_audio_specs_obtained; AudioParameters audio_parameters; @@ -164,6 +174,10 @@ class OpenALManager { {ALC_6POINT1_SOFT, ChannelType::_6_1}, {ALC_7POINT1_SOFT, ChannelType::_7_1} }; + + const std::unordered_map mapping_extensions_names = { + {OptionalExtension::Spatialization, "AL_SOFT_source_spatialize"} + }; }; #endif diff --git a/Source_Files/Sound/SoundPlayer.cpp b/Source_Files/Sound/SoundPlayer.cpp index a541d2d11..f6062ed23 100644 --- a/Source_Files/Sound/SoundPlayer.cpp +++ b/Source_Files/Sound/SoundPlayer.cpp @@ -231,10 +231,20 @@ bool SoundPlayer::SetUpALSourceInit() { alSource3i(audio_source->source_id, AL_DIRECTION, 0, 0, 0); alSourcei(audio_source->source_id, AL_REFERENCE_DISTANCE, 0); alSourcei(audio_source->source_id, AL_MAX_DISTANCE, 0); + +#ifdef AL_SOFT_source_spatialize + if (OpenALManager::Get()->IsExtensionSupported(OpenALManager::OptionalExtension::Spatialization)) + alSourcei(audio_source->source_id, AL_SOURCE_SPATIALIZE_SOFT, AL_AUTO_SOFT); +#endif } else { alSourcei(audio_source->source_id, AL_DISTANCE_MODEL, AL_INVERSE_DISTANCE_CLAMPED); alSourcei(audio_source->source_id, AL_SOURCE_RELATIVE, AL_FALSE); + +#ifdef AL_SOFT_source_spatialize + if (OpenALManager::Get()->IsExtensionSupported(OpenALManager::OptionalExtension::Spatialization)) + alSourcei(audio_source->source_id, AL_SOURCE_SPATIALIZE_SOFT, AL_TRUE); +#endif } return alGetError() == AL_NO_ERROR;