From c193bb77994b4b80f851a5c71915f8b5e1a0d5ac Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Thu, 5 May 2016 20:12:26 +0200 Subject: [PATCH 01/14] - removed the special case 0 in A_SpawnItem, meaning 'in front of the player'. As it turned out this has been broken for many, many years, so one can assume that most content using this function depends on this special case not working. I could track it down to at least 2008. --- src/thingdef/thingdef_codeptr.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/thingdef/thingdef_codeptr.cpp b/src/thingdef/thingdef_codeptr.cpp index 34a9cf02c..e5d080095 100644 --- a/src/thingdef/thingdef_codeptr.cpp +++ b/src/thingdef/thingdef_codeptr.cpp @@ -2433,12 +2433,6 @@ DEFINE_ACTION_FUNCTION_PARAMS(AActor, A_SpawnItem) ACTION_RETURN_BOOL(true); } - if (distance == 0) - { - // use the minimum distance that does not result in an overlap - distance = (self->radius + GetDefaultByType(missile)->radius); - } - if (ACTION_CALL_FROM_WEAPON()) { // Used from a weapon, so use some ammo From 52995f8fc2973e1bd1fdd3361b920d7eb2695439 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Wed, 4 May 2016 20:33:17 +0200 Subject: [PATCH 02/14] - Fixed missing sector light clamp in DFlicker. For some reason, this code didn't care about the sector light limits. Found by code inspection. --- src/p_lights.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/p_lights.cpp b/src/p_lights.cpp index 10bf9edf4..90d1da869 100644 --- a/src/p_lights.cpp +++ b/src/p_lights.cpp @@ -301,9 +301,9 @@ void DFlicker::Tick () DFlicker::DFlicker (sector_t *sector, int upper, int lower) : DLighting (sector) { - m_MaxLight = upper; - m_MinLight = lower; - sector->lightlevel = upper; + m_MaxLight = sector_t::ClampLight(upper); + m_MinLight = sector_t::ClampLight(lower); + sector->lightlevel = m_MaxLight; m_Count = (pr_flicker()&64)+1; } From 55294c58065ae5b0724068f2f7549088e5c08ade Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 1 May 2016 21:07:41 -0700 Subject: [PATCH 03/14] Use the correct OpenAL lib name on Linux --- src/sound/oalsound.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sound/oalsound.cpp b/src/sound/oalsound.cpp index 56dfb4a4c..3e3b70fb4 100644 --- a/src/sound/oalsound.cpp +++ b/src/sound/oalsound.cpp @@ -69,7 +69,7 @@ static void* hmodOpenAL; #ifdef __APPLE__ #define OPENALLIB "OpenAL.framework/OpenAL" #else -#define OPENALLIB "libopenal.so" +#define OPENALLIB "libopenal.so.1" #endif #define LoadLibrary(x) dlopen((x), RTLD_LAZY) #define GetProcAddress(a,b) dlsym((a),(b)) From bf5ac9b4764f57eb355bf7e904f615809a218519 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 2 May 2016 01:56:12 -0700 Subject: [PATCH 04/14] Use AL_EXT_SOURCE_RADIUS for area sounds --- src/sound/oalsound.cpp | 59 ++++++++++++++++++++++++++++-------------- src/sound/oalsound.h | 6 +++++ 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/sound/oalsound.cpp b/src/sound/oalsound.cpp index 3e3b70fb4..48c2b53f8 100644 --- a/src/sound/oalsound.cpp +++ b/src/sound/oalsound.cpp @@ -273,6 +273,8 @@ class OpenALSoundStream : public SoundStream alSourcei(Source, AL_DIRECT_FILTER, AL_FILTER_NULL); alSource3i(Source, AL_AUXILIARY_SEND_FILTER, 0, 0, AL_FILTER_NULL); } + if(Renderer->AL.EXT_SOURCE_RADIUS) + alSourcef(Source, AL_SOURCE_RADIUS, 0.f); alGenBuffers(BufferCount, Buffers); return (getALError() == AL_NO_ERROR); @@ -626,7 +628,7 @@ public: extern ReverbContainer *ForcedEnvironment; -#define AREA_SOUND_RADIUS (128.f) +#define AREA_SOUND_RADIUS (32.f) #define PITCH_MULT (0.7937005f) /* Approx. 4 semitones lower; what Nash suggested */ @@ -746,6 +748,7 @@ OpenALSoundRenderer::OpenALSoundRenderer() ALC.EXT_EFX = !!alcIsExtensionPresent(Device, "ALC_EXT_EFX"); ALC.EXT_disconnect = !!alcIsExtensionPresent(Device, "ALC_EXT_disconnect");; AL.EXT_source_distance_model = !!alIsExtensionPresent("AL_EXT_source_distance_model"); + AL.EXT_SOURCE_RADIUS = !!alIsExtensionPresent("AL_EXT_SOURCE_RADIUS"); AL.SOFT_deferred_updates = !!alIsExtensionPresent("AL_SOFT_deferred_updates"); AL.SOFT_loop_points = !!alIsExtensionPresent("AL_SOFT_loop_points"); @@ -1262,6 +1265,8 @@ FISoundChannel *OpenALSoundRenderer::StartSound(SoundHandle sfx, float vol, int alSourcef(source, AL_ROLLOFF_FACTOR, 0.f); alSourcef(source, AL_MAX_GAIN, SfxVolume); alSourcef(source, AL_GAIN, SfxVolume*vol); + if(AL.EXT_SOURCE_RADIUS) + alSourcef(source, AL_SOURCE_RADIUS, 0.f); if(EnvSlot) { @@ -1404,7 +1409,15 @@ FISoundChannel *OpenALSoundRenderer::StartSound3D(SoundHandle sfx, SoundListener float gain = GetRolloff(rolloff, sqrtf(dist_sqr) * distscale); dir.MakeResize((gain > 0.00001f) ? 1.f/gain : 100000.f); } - if((chanflags&SNDF_AREA) && dist_sqr < AREA_SOUND_RADIUS*AREA_SOUND_RADIUS) + if(AL.EXT_SOURCE_RADIUS) + { + /* Since the OpenAL distance is decoupled from the sound's distance, get the OpenAL + * distance that corresponds to the area radius. */ + alSourcef(source, AL_SOURCE_RADIUS, (chanflags&SNDF_AREA) ? + 1.f / GetRolloff(rolloff, AREA_SOUND_RADIUS) : 0.f + ); + } + else if((chanflags&SNDF_AREA) && dist_sqr < AREA_SOUND_RADIUS*AREA_SOUND_RADIUS) { FVector3 amb(0.f, !(dir.Y>=0.f) ? -1.f : 1.f, 0.f); float a = sqrtf(dist_sqr) / AREA_SOUND_RADIUS; @@ -1414,20 +1427,24 @@ FISoundChannel *OpenALSoundRenderer::StartSound3D(SoundHandle sfx, SoundListener alSource3f(source, AL_POSITION, dir[0], dir[1], -dir[2]); } - else if((chanflags&SNDF_AREA) && dist_sqr < AREA_SOUND_RADIUS*AREA_SOUND_RADIUS) + else { - FVector3 dir = pos - listener->position; + FVector3 dir = pos; + if(AL.EXT_SOURCE_RADIUS) + alSourcef(source, AL_SOURCE_RADIUS, (chanflags&SNDF_AREA) ? AREA_SOUND_RADIUS : 0.f); + else if((chanflags&SNDF_AREA) && dist_sqr < AREA_SOUND_RADIUS*AREA_SOUND_RADIUS) + { + dir -= listener->position; - float mindist = rolloff->MinDistance/distscale; - FVector3 amb(0.f, !(dir.Y>=0.f) ? -mindist : mindist, 0.f); - float a = sqrtf(dist_sqr) / AREA_SOUND_RADIUS; - dir = amb + (dir-amb)*a; + float mindist = rolloff->MinDistance/distscale; + FVector3 amb(0.f, !(dir.Y>=0.f) ? -mindist : mindist, 0.f); + float a = sqrtf(dist_sqr) / AREA_SOUND_RADIUS; + dir = amb + (dir-amb)*a; - dir += listener->position; + dir += listener->position; + } alSource3f(source, AL_POSITION, dir[0], dir[1], -dir[2]); } - else - alSource3f(source, AL_POSITION, pos[0], pos[1], -pos[2]); alSource3f(source, AL_VELOCITY, vel[0], vel[1], -vel[2]); alSource3f(source, AL_DIRECTION, 0.f, 0.f, 0.f); @@ -1631,26 +1648,26 @@ void OpenALSoundRenderer::UpdateSoundParams3D(SoundListener *listener, FISoundCh if(chan == NULL || chan->SysChannel == NULL) return; - alDeferUpdatesSOFT(); - FVector3 dir = pos - listener->position; chan->DistanceSqr = (float)dir.LengthSquared(); if(chan->ManualRolloff) { - if(dir.DoesNotApproximatelyEqual(FVector3(0.f, 0.f, 0.f))) - { - float gain = GetRolloff(&chan->Rolloff, sqrtf(chan->DistanceSqr) * chan->DistanceScale); - dir.MakeResize((gain > 0.00001f) ? 1.f/gain : 100000.f); - } - if(areasound && chan->DistanceSqr < AREA_SOUND_RADIUS*AREA_SOUND_RADIUS) + if(!AL.EXT_SOURCE_RADIUS && areasound && + chan->DistanceSqr < AREA_SOUND_RADIUS*AREA_SOUND_RADIUS) { FVector3 amb(0.f, !(dir.Y>=0.f) ? -1.f : 1.f, 0.f); float a = sqrtf(chan->DistanceSqr) / AREA_SOUND_RADIUS; dir = amb + (dir-amb)*a; } + if(dir.DoesNotApproximatelyEqual(FVector3(0.f, 0.f, 0.f))) + { + float gain = GetRolloff(&chan->Rolloff, sqrtf(chan->DistanceSqr)*chan->DistanceScale); + dir.MakeResize((gain > 0.00001f) ? 1.f/gain : 100000.f); + } } - else if(areasound && chan->DistanceSqr < AREA_SOUND_RADIUS*AREA_SOUND_RADIUS) + else if(!AL.EXT_SOURCE_RADIUS && areasound && + chan->DistanceSqr < AREA_SOUND_RADIUS*AREA_SOUND_RADIUS) { float mindist = chan->Rolloff.MinDistance / chan->DistanceScale; FVector3 amb(0.f, !(dir.Y>=0.f) ? -mindist : mindist, 0.f); @@ -1659,6 +1676,8 @@ void OpenALSoundRenderer::UpdateSoundParams3D(SoundListener *listener, FISoundCh } dir += listener->position; + alDeferUpdatesSOFT(); + ALuint source = GET_PTRID(chan->SysChannel); alSource3f(source, AL_POSITION, dir[0], dir[1], -dir[2]); alSource3f(source, AL_VELOCITY, vel[0], vel[1], -vel[2]); diff --git a/src/sound/oalsound.h b/src/sound/oalsound.h index 2644b171a..c9ebc70ea 100644 --- a/src/sound/oalsound.h +++ b/src/sound/oalsound.h @@ -58,6 +58,11 @@ #define AL_FORMAT_71CHN32 0x1212 #endif +#ifndef AL_EXT_SOURCE_RADIUS +#define AL_EXT_SOURCE_RADIUS 1 +#define AL_SOURCE_RADIUS 0x1031 +#endif + #include "efx.h" @@ -127,6 +132,7 @@ private: } ALC; struct { bool EXT_source_distance_model; + bool EXT_SOURCE_RADIUS; bool SOFT_deferred_updates; bool SOFT_loop_points; } AL; From 12c9281a7e484daee75e935297c84457d76e3b27 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 2 May 2016 02:46:01 -0700 Subject: [PATCH 05/14] Avoid redundantly setting DistanceScale --- src/sound/oalsound.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/sound/oalsound.cpp b/src/sound/oalsound.cpp index 48c2b53f8..b9421221b 100644 --- a/src/sound/oalsound.cpp +++ b/src/sound/oalsound.cpp @@ -1326,7 +1326,6 @@ FISoundChannel *OpenALSoundRenderer::StartSound(SoundHandle sfx, float vol, int chan->Rolloff.RolloffType = ROLLOFF_Log; chan->Rolloff.RolloffFactor = 0.f; chan->Rolloff.MinDistance = 1.f; - chan->DistanceScale = 1.f; chan->DistanceSqr = 0.f; chan->ManualRolloff = false; @@ -1510,7 +1509,6 @@ FISoundChannel *OpenALSoundRenderer::StartSound3D(SoundHandle sfx, SoundListener else chan->SysChannel = MAKE_PTRID(source); chan->Rolloff = *rolloff; - chan->DistanceScale = distscale; chan->DistanceSqr = dist_sqr; chan->ManualRolloff = manualRolloff; From c3cef91a4f1a0953676fe3271f77750de34b9b18 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 2 May 2016 05:07:23 -0700 Subject: [PATCH 06/14] Properly sum the channels being downmixed to mono --- src/sound/oalsound.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sound/oalsound.cpp b/src/sound/oalsound.cpp index b9421221b..c13787c7b 100644 --- a/src/sound/oalsound.cpp +++ b/src/sound/oalsound.cpp @@ -1045,7 +1045,7 @@ std::pair OpenALSoundRenderer::LoadSoundRaw(BYTE *sfxdata, int { int sum = 0; for(int c = 0;c < channels;c++) - sum = ((short*)sfxdata)[i*channels + c]; + sum += ((short*)sfxdata)[i*channels + c]; ((short*)sfxdata)[i] = sum / channels; } } @@ -1055,7 +1055,7 @@ std::pair OpenALSoundRenderer::LoadSoundRaw(BYTE *sfxdata, int { int sum = 0; for(int c = 0;c < channels;c++) - sum = sfxdata[i*channels + c] - 128; + sum += sfxdata[i*channels + c] - 128; sfxdata[i] = (sum / channels) + 128; } } From e1c8890c5e9d46981150d2cea2deae474722aafc Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 2 May 2016 13:34:46 -0700 Subject: [PATCH 07/14] Set the appropriate start offset for restarted sounds --- src/sound/oalsound.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/sound/oalsound.cpp b/src/sound/oalsound.cpp index c13787c7b..2e351fa0e 100644 --- a/src/sound/oalsound.cpp +++ b/src/sound/oalsound.cpp @@ -40,6 +40,8 @@ #include #endif +#include + #include "except.h" #include "doomstat.h" #include "templates.h" @@ -1472,7 +1474,7 @@ FISoundChannel *OpenALSoundRenderer::StartSound3D(SoundHandle sfx, SoundListener else alSourcef(source, AL_PITCH, PITCH(pitch)); - if(!reuse_chan) + if(!reuse_chan || reuse_chan->StartTime.AsOne == 0) alSourcef(source, AL_SEC_OFFSET, 0.f); else { @@ -1480,8 +1482,11 @@ FISoundChannel *OpenALSoundRenderer::StartSound3D(SoundHandle sfx, SoundListener alSourcef(source, AL_SEC_OFFSET, reuse_chan->StartTime.Lo/1000.f); else { - // FIXME: set offset based on the current time and the StartTime - alSourcef(source, AL_SAMPLE_OFFSET, 0.f); + float offset = std::chrono::duration_cast>( + std::chrono::steady_clock::now().time_since_epoch() - + std::chrono::steady_clock::time_point::duration(reuse_chan->StartTime.AsOne) + ).count(); + if(offset > 0.f) alSourcef(source, AL_SEC_OFFSET, offset); } } if(getALError() != AL_NO_ERROR) @@ -1819,7 +1824,7 @@ void OpenALSoundRenderer::MarkStartTime(FISoundChannel *chan) { // FIXME: Get current time (preferably from the audio clock, but the system // time will have to do) - chan->StartTime.AsOne = 0; + chan->StartTime.AsOne = std::chrono::steady_clock::now().time_since_epoch().count(); } float OpenALSoundRenderer::GetAudibility(FISoundChannel *chan) From 26a8a8a1ba583aed21949109afdf8ec28fe1cc68 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 2 May 2016 14:23:07 -0700 Subject: [PATCH 08/14] Pause OpenAL processing using ALC_SOFT_pause_device when inactive --- src/sound/oalsound.cpp | 21 +++++++++++++++++++-- src/sound/oalsound.h | 4 ++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/sound/oalsound.cpp b/src/sound/oalsound.cpp index 2e351fa0e..c9ba16f2b 100644 --- a/src/sound/oalsound.cpp +++ b/src/sound/oalsound.cpp @@ -690,7 +690,12 @@ template static void LoadALFunc(const char *name, T *x) { *x = reinterpret_cast(alGetProcAddress(name)); } +template +static void LoadALCFunc(ALCdevice *device, const char *name, T *x) +{ *x = reinterpret_cast(alcGetProcAddress(device, name)); } + #define LOAD_FUNC(x) (LoadALFunc(#x, &x)) +#define LOAD_DEV_FUNC(d, x) (LoadALCFunc(d, #x, &x)) OpenALSoundRenderer::OpenALSoundRenderer() : Device(NULL), Context(NULL), SFXPaused(0), PrevEnvironment(NULL), EnvSlot(0) { @@ -748,7 +753,8 @@ OpenALSoundRenderer::OpenALSoundRenderer() DPrintf(" Extensions: " TEXTCOLOR_ORANGE"%s\n", alGetString(AL_EXTENSIONS)); ALC.EXT_EFX = !!alcIsExtensionPresent(Device, "ALC_EXT_EFX"); - ALC.EXT_disconnect = !!alcIsExtensionPresent(Device, "ALC_EXT_disconnect");; + ALC.EXT_disconnect = !!alcIsExtensionPresent(Device, "ALC_EXT_disconnect"); + ALC.SOFT_pause_device = !!alcIsExtensionPresent(Device, "ALC_SOFT_pause_device"); AL.EXT_source_distance_model = !!alIsExtensionPresent("AL_EXT_source_distance_model"); AL.EXT_SOURCE_RADIUS = !!alIsExtensionPresent("AL_EXT_SOURCE_RADIUS"); AL.SOFT_deferred_updates = !!alIsExtensionPresent("AL_SOFT_deferred_updates"); @@ -771,6 +777,12 @@ OpenALSoundRenderer::OpenALSoundRenderer() alProcessUpdatesSOFT = _wrap_ProcessUpdatesSOFT; } + if(ALC.SOFT_pause_device) + { + LOAD_DEV_FUNC(Device, alcDevicePauseSOFT); + LOAD_DEV_FUNC(Device, alcDeviceResumeSOFT); + } + ALenum err = getALError(); if(err != AL_NO_ERROR) { @@ -910,6 +922,7 @@ OpenALSoundRenderer::OpenALSoundRenderer() if(EnvSlot) Printf(" EFX enabled\n"); } +#undef LOAD_DEV_FUNC #undef LOAD_FUNC OpenALSoundRenderer::~OpenALSoundRenderer() @@ -1599,10 +1612,14 @@ void OpenALSoundRenderer::SetInactive(SoundRenderer::EInactiveState state) { case SoundRenderer::INACTIVE_Active: alListenerf(AL_GAIN, 1.0f); + if(ALC.SOFT_pause_device) + alcDeviceResumeSOFT(Device); break; - /* FIXME: This doesn't stop anything. */ case SoundRenderer::INACTIVE_Complete: + if(ALC.SOFT_pause_device) + alcDevicePauseSOFT(Device); + /* fall-through */ case SoundRenderer::INACTIVE_Mute: alListenerf(AL_GAIN, 0.0f); break; diff --git a/src/sound/oalsound.h b/src/sound/oalsound.h index c9ebc70ea..88dcbc238 100644 --- a/src/sound/oalsound.h +++ b/src/sound/oalsound.h @@ -129,6 +129,7 @@ private: struct { bool EXT_EFX; bool EXT_disconnect; + bool SOFT_pause_device; } ALC; struct { bool EXT_source_distance_model; @@ -180,6 +181,9 @@ private: ALvoid (AL_APIENTRY*alDeferUpdatesSOFT)(void); ALvoid (AL_APIENTRY*alProcessUpdatesSOFT)(void); + void (ALC_APIENTRY*alcDevicePauseSOFT)(ALCdevice *device); + void (ALC_APIENTRY*alcDeviceResumeSOFT)(ALCdevice *device); + void LoadReverb(const ReverbContainer *env); void PurgeStoppedSources(); static FSoundChan *FindLowestChannel(); From 66c61a7c8b8e15bbc731c87242c0cee682df98d9 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Tue, 3 May 2016 04:33:03 -0700 Subject: [PATCH 09/14] Set the appropriate start offset for non-3D sounds too --- src/sound/oalsound.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/sound/oalsound.cpp b/src/sound/oalsound.cpp index c9ba16f2b..8303324d1 100644 --- a/src/sound/oalsound.cpp +++ b/src/sound/oalsound.cpp @@ -1302,7 +1302,7 @@ FISoundChannel *OpenALSoundRenderer::StartSound(SoundHandle sfx, float vol, int else alSourcef(source, AL_PITCH, PITCH(pitch)); - if(!reuse_chan) + if(!reuse_chan || reuse_chan->StartTime.AsOne == 0) alSourcef(source, AL_SEC_OFFSET, 0.f); else { @@ -1310,8 +1310,11 @@ FISoundChannel *OpenALSoundRenderer::StartSound(SoundHandle sfx, float vol, int alSourcef(source, AL_SEC_OFFSET, reuse_chan->StartTime.Lo/1000.f); else { - // FIXME: set offset based on the current time and the StartTime - alSourcef(source, AL_SEC_OFFSET, 0.f); + float offset = std::chrono::duration_cast>( + std::chrono::steady_clock::now().time_since_epoch() - + std::chrono::steady_clock::time_point::duration(reuse_chan->StartTime.AsOne) + ).count(); + if(offset > 0.f) alSourcef(source, AL_SEC_OFFSET, offset); } } if(getALError() != AL_NO_ERROR) From e298f8389b452890c1b7306fdfe073a23fdd71a7 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Tue, 3 May 2016 21:05:13 -0700 Subject: [PATCH 10/14] Avoid divide-by-0 when the area sound radius is less than the max distance --- src/sound/oalsound.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sound/oalsound.cpp b/src/sound/oalsound.cpp index 8303324d1..71f021416 100644 --- a/src/sound/oalsound.cpp +++ b/src/sound/oalsound.cpp @@ -1431,7 +1431,8 @@ FISoundChannel *OpenALSoundRenderer::StartSound3D(SoundHandle sfx, SoundListener /* Since the OpenAL distance is decoupled from the sound's distance, get the OpenAL * distance that corresponds to the area radius. */ alSourcef(source, AL_SOURCE_RADIUS, (chanflags&SNDF_AREA) ? - 1.f / GetRolloff(rolloff, AREA_SOUND_RADIUS) : 0.f + // Clamp in case the max distance is <= the area radius + 1.f/MAX(GetRolloff(rolloff, AREA_SOUND_RADIUS), 0.00001f) : 0.f ); } else if((chanflags&SNDF_AREA) && dist_sqr < AREA_SOUND_RADIUS*AREA_SOUND_RADIUS) From 0b7b78c5d233a6066d92e872ac0b943821ed0fe2 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Wed, 4 May 2016 12:05:43 -0700 Subject: [PATCH 11/14] Better handle channel configs when monoizing in LoadSound --- src/sound/oalsound.cpp | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/sound/oalsound.cpp b/src/sound/oalsound.cpp index 71f021416..8b13854c9 100644 --- a/src/sound/oalsound.cpp +++ b/src/sound/oalsound.cpp @@ -40,6 +40,7 @@ #include #endif +#include #include #include "except.h" @@ -636,6 +637,15 @@ extern ReverbContainer *ForcedEnvironment; #define PITCH(pitch) (snd_pitched ? (pitch)/128.f : 1.f) +static size_t GetChannelCount(ChannelConfig chans) +{ + switch(chans) + { + case ChannelConfig_Mono: return 1; + case ChannelConfig_Stereo: return 2; + } + return 0; +} static float GetRolloff(const FRolloffInfo *rolloff, float distance) { @@ -1142,7 +1152,7 @@ std::pair OpenALSoundRenderer::LoadSound(BYTE *sfxdata, int le SampleType type; int srate; - SoundDecoder *decoder = CreateDecoder(&reader); + std::unique_ptr decoder(CreateDecoder(&reader)); if(!decoder) return std::make_pair(retval, true); decoder->getInfo(&srate, &chans, &type); @@ -1161,46 +1171,53 @@ std::pair OpenALSoundRenderer::LoadSound(BYTE *sfxdata, int le { Printf("Unsupported audio format: %s, %s\n", GetChannelConfigName(chans), GetSampleTypeName(type)); - delete decoder; return std::make_pair(retval, true); } TArray data = decoder->readAll(); if(chans != ChannelConfig_Mono && monoize) { - // TODO: Handle this better if ChannelConfig ever gets more channel configurations. - size_t frames = data.Size() / 2 / (type == SampleType_Int16 ? 2 : 1); + size_t chancount = GetChannelCount(chans); + size_t frames = data.Size() / chancount / + (type == SampleType_Int16 ? 2 : 1); if(type == SampleType_Int16) { short *sfxdata = (short*)&data[0]; for(size_t i = 0;i < frames;i++) - sfxdata[i] = (sfxdata[i*2 + 0]-0 + sfxdata[i*2 + 1]-0)/2; + { + int sum = 0; + for(size_t c = 0;c < chancount;c++) + sum += sfxdata[i*chancount + c]; + sfxdata[i] = sum / chancount; + } } else if(type == SampleType_UInt8) { BYTE *sfxdata = (BYTE*)&data[0]; for(size_t i = 0;i < frames;i++) - sfxdata[i] = (sfxdata[i*2 + 0]-128 + sfxdata[i*2 + 1]-128)/2 + 128; + { + int sum = 0; + for(size_t c = 0;c < chancount;c++) + sum += sfxdata[i*chancount + c] - 128; + sfxdata[i] = (sum / chancount) + 128; + } } - data.Resize(data.Size()/2); + data.Resize(data.Size()/chancount); } + ALenum err; ALuint buffer = 0; alGenBuffers(1, &buffer); alBufferData(buffer, format, &data[0], data.Size(), srate); - - ALenum err; if((err=getALError()) != AL_NO_ERROR) { Printf("Failed to buffer data: %s\n", alGetString(err)); alDeleteBuffers(1, &buffer); getALError(); - delete decoder; return std::make_pair(retval, true); } retval.data = MAKE_PTRID(buffer); - delete decoder; return std::make_pair(retval, (chans == ChannelConfig_Mono || monoize)); } From fa46965a71559b8fe479098b83efbf044655604a Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 5 May 2016 09:18:00 -0700 Subject: [PATCH 12/14] Check for OpenAL device errors after pausing/resuming the device --- src/sound/oalsound.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/sound/oalsound.cpp b/src/sound/oalsound.cpp index 8b13854c9..95fa4d39a 100644 --- a/src/sound/oalsound.cpp +++ b/src/sound/oalsound.cpp @@ -1634,12 +1634,18 @@ void OpenALSoundRenderer::SetInactive(SoundRenderer::EInactiveState state) case SoundRenderer::INACTIVE_Active: alListenerf(AL_GAIN, 1.0f); if(ALC.SOFT_pause_device) + { alcDeviceResumeSOFT(Device); + getALCError(Device); + } break; case SoundRenderer::INACTIVE_Complete: if(ALC.SOFT_pause_device) + { alcDevicePauseSOFT(Device); + getALCError(Device); + } /* fall-through */ case SoundRenderer::INACTIVE_Mute: alListenerf(AL_GAIN, 0.0f); From ba1574e65a210478b84a3434cd817c9030f78d08 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 5 May 2016 10:36:53 -0700 Subject: [PATCH 13/14] Don't define OpenAL redirect macros with the IDE's parser At least with KDevelop4, macros like this interefere with contextual info by causing it to show information about the macro itself (where it's defined and what it defines to), rather than the function (parameters, comments, etc). It also gets in the way of auto-completion. --- src/sound/oalload.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/sound/oalload.h b/src/sound/oalload.h index 986131b2c..3cee30d58 100644 --- a/src/sound/oalload.h +++ b/src/sound/oalload.h @@ -22,7 +22,7 @@ static oalloadentry oalfuncs[] = { { NULL, 0 } }; - +#ifndef IN_IDE_PARSER #define alEnable p_alEnable #define alDisable p_alDisable #define alIsEnabled p_alIsEnabled @@ -116,6 +116,7 @@ static oalloadentry oalfuncs[] = { #define alcCaptureStart p_alcCaptureStart #define alcCaptureStop p_alcCaptureStop #define alcCaptureSamples p_alcCaptureSamples +#endif #endif -#endif \ No newline at end of file +#endif From fbf845982be7fc2f3df832cf6accf3cb302991d9 Mon Sep 17 00:00:00 2001 From: Jason Yundt Date: Sun, 1 May 2016 17:38:14 -0400 Subject: [PATCH 14/14] FORCE_INTERNAL_GME now defaults to OFF --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b7d4e7a9c..5f0bf8293 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -193,7 +193,7 @@ set( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${DEB_C_FLAGS} -D_DEBUG" ) option(FORCE_INTERNAL_ZLIB "Use internal zlib") option(FORCE_INTERNAL_JPEG "Use internal jpeg") option(FORCE_INTERNAL_BZIP2 "Use internal bzip2") -option(FORCE_INTERNAL_GME "Use internal gme" ON) +option(FORCE_INTERNAL_GME "Use internal gme") # Fast math flags, required by some subprojects set( ZD_FASTMATH_FLAG "" )