From e1fc33c38fc9907b3bb433497e903187abc1809e Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 12 May 2017 12:08:41 -0700 Subject: [PATCH] Don't downmix sounds when AL_SOFT_source_spatialize is available --- src/sound/oalsound.cpp | 22 +++++++++++++++++++--- src/sound/oalsound.h | 7 +++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/sound/oalsound.cpp b/src/sound/oalsound.cpp index 77068cf96..6a0c9ff1c 100644 --- a/src/sound/oalsound.cpp +++ b/src/sound/oalsound.cpp @@ -285,6 +285,8 @@ class OpenALSoundStream : public SoundStream } if(Renderer->AL.EXT_SOURCE_RADIUS) alSourcef(Source, AL_SOURCE_RADIUS, 0.f); + if(Renderer->AL.SOFT_source_spatialize) + alSourcei(Source, AL_SOURCE_SPATIALIZE_SOFT, AL_AUTO_SOFT); alGenBuffers(BufferCount, Buffers); return (getALError() == AL_NO_ERROR); @@ -816,6 +818,7 @@ OpenALSoundRenderer::OpenALSoundRenderer() AL.SOFT_deferred_updates = !!alIsExtensionPresent("AL_SOFT_deferred_updates"); AL.SOFT_loop_points = !!alIsExtensionPresent("AL_SOFT_loop_points"); AL.SOFT_source_resampler = !!alIsExtensionPresent("AL_SOFT_source_resampler"); + AL.SOFT_source_spatialize = !!alIsExtensionPresent("AL_SOFT_source_spatialize"); alDopplerFactor(0.5f); alSpeedOfSound(343.3f * 96.0f); @@ -1177,6 +1180,9 @@ std::pair OpenALSoundRenderer::LoadSoundRaw(uint8_t *sfxdata, if(length == 0) return std::make_pair(retval, true); + /* Only downmix to mono if we can't spatialize multi-channel sounds. */ + monoize = monoize && !AL.SOFT_source_spatialize; + if(bits == -8) { // Simple signed->unsigned conversion @@ -1264,7 +1270,7 @@ std::pair OpenALSoundRenderer::LoadSoundRaw(uint8_t *sfxdata, } retval.data = MAKE_PTRID(buffer); - return std::make_pair(retval, channels==1); + return std::make_pair(retval, AL.SOFT_source_spatialize || channels==1); } void FindLoopTags(FileReader *fr, uint32_t *start, bool *startass, uint32_t *end, bool *endass); @@ -1280,6 +1286,9 @@ std::pair OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int uint32_t loop_start = 0, loop_end = ~0u; bool startass = false, endass = false; + /* Only downmix to mono if we can't spatialize multi-channel sounds. */ + monoize = monoize && !AL.SOFT_source_spatialize; + if (!memcmp(sfxdata, "OggS", 4) || !memcmp(sfxdata, "FLAC", 4)) { MemoryReader mr((char*)sfxdata, length); @@ -1377,7 +1386,7 @@ std::pair OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int pBuffer->type = type; pBuffer->srate = srate; } - return std::make_pair(retval, (chans == ChannelConfig_Mono || monoize)); + return std::make_pair(retval, AL.SOFT_source_spatialize || chans == ChannelConfig_Mono || monoize); } std::pair OpenALSoundRenderer::LoadSoundBuffered(FSoundLoadBuffer *pBuffer, bool monoize) @@ -1389,6 +1398,9 @@ std::pair OpenALSoundRenderer::LoadSoundBuffered(FSoundLoadBu auto chans = pBuffer->chans; uint32_t loop_start = pBuffer->loop_start, loop_end = pBuffer->loop_end; + /* Only downmix to mono if we can't spatialize multi-channel sounds. */ + monoize = monoize && !AL.SOFT_source_spatialize; + if (chans == ChannelConfig_Mono || monoize) { if (type == SampleType_UInt8) format = AL_FORMAT_MONO8; @@ -1461,7 +1473,7 @@ std::pair OpenALSoundRenderer::LoadSoundBuffered(FSoundLoadBu } retval.data = MAKE_PTRID(buffer); - return std::make_pair(retval, (chans == ChannelConfig_Mono || monoize)); + return std::make_pair(retval, AL.SOFT_source_spatialize || chans == ChannelConfig_Mono || monoize); } void OpenALSoundRenderer::UnloadSound(SoundHandle sfx) @@ -1560,6 +1572,8 @@ FISoundChannel *OpenALSoundRenderer::StartSound(SoundHandle sfx, float vol, int alSourcef(source, AL_GAIN, SfxVolume*vol); if(AL.EXT_SOURCE_RADIUS) alSourcef(source, AL_SOURCE_RADIUS, 0.f); + if(AL.SOFT_source_spatialize) + alSourcei(source, AL_SOURCE_SPATIALIZE_SOFT, AL_AUTO_SOFT); if(EnvSlot) { @@ -1768,6 +1782,8 @@ FISoundChannel *OpenALSoundRenderer::StartSound3D(SoundHandle sfx, SoundListener alSourcef(source, AL_MAX_GAIN, SfxVolume); alSourcef(source, AL_GAIN, SfxVolume*vol); + if(AL.SOFT_source_spatialize) + alSourcei(source, AL_SOURCE_SPATIALIZE_SOFT, AL_TRUE); if(EnvSlot) { diff --git a/src/sound/oalsound.h b/src/sound/oalsound.h index 6c79707bf..7a1ed2a23 100644 --- a/src/sound/oalsound.h +++ b/src/sound/oalsound.h @@ -109,6 +109,12 @@ AL_API const ALchar* AL_APIENTRY alGetStringiSOFT(ALenum pname, ALsizei index); #endif #endif +#ifndef AL_SOFT_source_spatialize +#define AL_SOFT_source_spatialize +#define AL_SOURCE_SPATIALIZE_SOFT 0x1214 +#define AL_AUTO_SOFT 0x0002 +#endif + class OpenALSoundStream; @@ -184,6 +190,7 @@ private: bool SOFT_deferred_updates; bool SOFT_loop_points; bool SOFT_source_resampler; + bool SOFT_source_spatialize; } AL; // EFX Extension function pointer variables. Loaded after context creation