Skip to content

Commit

Permalink
Add 3d sounds spatialization support for stereo sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolfering committed Dec 21, 2024
1 parent cbd11f1 commit a80a002
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Source_Files/Sound/AudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 9 additions & 1 deletion Source_Files/Sound/OpenALManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 14 additions & 0 deletions Source_Files/Sound/OpenALManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -116,6 +123,9 @@ class OpenALManager {
static LPALFILTERI alFilteri;
static LPALFILTERF alFilterf;

std::unordered_map<OptionalExtension, bool> extension_support;
bool LoadOptionalExtensions();

static void MixerCallback(void* usr, uint8* stream, int len);
SDL_AudioSpec sdl_audio_specs_obtained;
AudioParameters audio_parameters;
Expand Down Expand Up @@ -164,6 +174,10 @@ class OpenALManager {
{ALC_6POINT1_SOFT, ChannelType::_6_1},
{ALC_7POINT1_SOFT, ChannelType::_7_1}
};

const std::unordered_map<OptionalExtension, std::string> mapping_extensions_names = {
{OptionalExtension::Spatialization, "AL_SOFT_source_spatialize"}
};
};

#endif
10 changes: 10 additions & 0 deletions Source_Files/Sound/SoundPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a80a002

Please sign in to comment.