Revert "Stop sources immediately when the sound is forced to stop"

This reverts commit 6b8cebe979.
This commit is contained in:
drfrag 2020-02-18 15:35:03 +01:00
parent f5f766b948
commit 82c4750710
2 changed files with 86 additions and 13 deletions

View file

@ -185,7 +185,7 @@ class OpenALSoundStream : public SoundStream
if(Renderer->FreeSfx.Size() == 0)
{
FSoundChan *lowest = Renderer->FindLowestChannel();
if(lowest) Renderer->StopChannel(lowest);
if(lowest) Renderer->ForceStopChannel(lowest);
if(Renderer->FreeSfx.Size() == 0)
return false;
@ -667,6 +667,14 @@ OpenALSoundRenderer::OpenALSoundRenderer()
return;
}
ALCint refresh=0;
alcGetIntegerv(Device, ALC_REFRESH, 1, &refresh);
if(refresh > 0)
{
// Round up instead of down
UpdateTimeMS = (1000+refresh-1) / refresh;
}
ALCint numMono=0, numStereo=0;
alcGetIntegerv(Device, ALC_MONO_SOURCES, 1, &numMono);
alcGetIntegerv(Device, ALC_STEREO_SOURCES, 1, &numStereo);
@ -1141,7 +1149,7 @@ void OpenALSoundRenderer::UnloadSound(SoundHandle sfx)
if((ALuint)bufID == buffer)
{
FSoundChan *next = schan->NextChan;
StopChannel(schan);
ForceStopChannel(schan);
schan = next;
continue;
}
@ -1149,6 +1157,20 @@ void OpenALSoundRenderer::UnloadSound(SoundHandle sfx)
schan = schan->NextChan;
}
// Make sure to kill any currently fading sounds too
for(auto iter = FadingSources.begin();iter != FadingSources.end();)
{
ALint bufID = 0;
alGetSourcei(iter->first, AL_BUFFER, &bufID);
if(static_cast<ALuint>(bufID) == buffer)
{
FreeSource(iter->first);
iter = FadingSources.erase(iter);
}
else
++iter;
}
alDeleteBuffers(1, &buffer);
getALError();
}
@ -1172,7 +1194,7 @@ FISoundChannel *OpenALSoundRenderer::StartSound(SoundHandle sfx, float vol, int
if(FreeSfx.Size() == 0)
{
FSoundChan *lowest = FindLowestChannel();
if(lowest) StopChannel(lowest);
if(lowest) ForceStopChannel(lowest);
if(FreeSfx.Size() == 0)
return NULL;
@ -1278,7 +1300,7 @@ FISoundChannel *OpenALSoundRenderer::StartSound3D(SoundHandle sfx, SoundListener
{
if(lowest->Priority < priority || (lowest->Priority == priority &&
lowest->DistanceSqr > dist_sqr))
StopChannel(lowest);
ForceStopChannel(lowest);
}
if(FreeSfx.Size() == 0)
return NULL;
@ -1481,15 +1503,8 @@ void OpenALSoundRenderer::ChannelPitch(FISoundChannel *chan, float pitch)
alSourcef(source, AL_PITCH, std::max(pitch, 0.0001f));
}
void OpenALSoundRenderer::StopChannel(FISoundChannel *chan)
void OpenALSoundRenderer::FreeSource(ALuint source)
{
if(chan == NULL || chan->SysChannel == NULL)
return;
ALuint source = GET_PTRID(chan->SysChannel);
// Release first, so it can be properly marked as evicted if it's being killed
soundEngine->ChannelEnded(chan);
alSourceRewind(source);
alSourcei(source, AL_BUFFER, 0);
getALError();
@ -1505,6 +1520,41 @@ void OpenALSoundRenderer::StopChannel(FISoundChannel *chan)
FreeSfx.Push(source);
}
void OpenALSoundRenderer::StopChannel(FISoundChannel *chan)
{
if(chan == NULL || chan->SysChannel == NULL)
return;
ALuint source = GET_PTRID(chan->SysChannel);
// Release first, so it can be properly marked as evicted if it's being killed
soundEngine->ChannelEnded(chan);
ALint state = AL_INITIAL;
alGetSourcei(source, AL_SOURCE_STATE, &state);
if(state != AL_PLAYING)
FreeSource(source);
else
{
// The sound is being killed while playing, so set its gain to 0 and track it
// as it fades.
alSourcef(source, AL_GAIN, 0.f);
getALError();
FadingSources.insert(std::make_pair(
source, std::chrono::steady_clock::now().time_since_epoch().count()
));
}
}
void OpenALSoundRenderer::ForceStopChannel(FISoundChannel *chan)
{
ALuint source = GET_PTRID(chan->SysChannel);
if(!source) return;
soundEngine->ChannelEnded(chan);
FreeSource(source);
}
unsigned int OpenALSoundRenderer::GetPosition(FISoundChannel *chan)
{
@ -1771,6 +1821,23 @@ void OpenALSoundRenderer::UpdateSounds()
{
alProcessUpdatesSOFT();
if(!FadingSources.empty())
{
auto cur_time = std::chrono::steady_clock::now().time_since_epoch();
for(auto iter = FadingSources.begin();iter != FadingSources.end();)
{
auto time_diff = std::chrono::duration_cast<std::chrono::milliseconds>(cur_time -
std::chrono::steady_clock::time_point::duration(iter->second));
if(time_diff.count() >= UpdateTimeMS)
{
FreeSource(iter->first);
iter = FadingSources.erase(iter);
}
else
++iter;
}
}
if(ALC.EXT_disconnect)
{
ALCint connected = ALC_TRUE;
@ -1919,7 +1986,7 @@ void OpenALSoundRenderer::PurgeStoppedSources()
{
if(schan->SysChannel != NULL && src == GET_PTRID(schan->SysChannel))
{
StopChannel(schan);
ForceStopChannel(schan);
break;
}
schan = schan->NextChan;

View file

@ -244,8 +244,10 @@ private:
void RemoveStream(OpenALSoundStream *stream);
void LoadReverb(const ReverbContainer *env);
void FreeSource(ALuint source);
void PurgeStoppedSources();
static FSoundChan *FindLowestChannel();
void ForceStopChannel(FISoundChannel *chan);
std::thread StreamThread;
std::mutex StreamLock;
@ -266,6 +268,10 @@ private:
TArray<ALuint> ReverbSfx;
TArray<ALuint> SfxGroup;
int UpdateTimeMS;
using SourceTimeMap = std::unordered_map<ALuint,int64_t>;
SourceTimeMap FadingSources;
const ReverbContainer *PrevEnvironment;
typedef TMap<uint16_t,ALuint> EffectMap;