From b54e52330c914bac48745e200b96d943e7aa78a0 Mon Sep 17 00:00:00 2001
From: Christoph Oelckers <coelckers@users.noreply.github.com>
Date: Sun, 28 Nov 2021 21:57:03 +0100
Subject: [PATCH] - Blood: don't play the same looped sound multiple times on
 the same actor.

This caused some strong echoing on E4M6.
---
 source/common/audio/sound/s_sound.cpp       | 4 +++-
 source/common/audio/sound/s_soundinternal.h | 2 +-
 source/games/blood/src/sfx.cpp              | 6 +++++-
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/source/common/audio/sound/s_sound.cpp b/source/common/audio/sound/s_sound.cpp
index 315dd0438..5201891c0 100644
--- a/source/common/audio/sound/s_sound.cpp
+++ b/source/common/audio/sound/s_sound.cpp
@@ -1084,13 +1084,14 @@ void SoundEngine::SetPitch(FSoundChan *chan, float pitch)
 // Is a sound being played by a specific emitter?
 //==========================================================================
 
-int SoundEngine::GetSoundPlayingInfo (int sourcetype, const void *source, int sound_id)
+int SoundEngine::GetSoundPlayingInfo (int sourcetype, const void *source, int sound_id, int chann)
 {
 	int count = 0;
 	if (sound_id > 0)
 	{
 		for (FSoundChan *chan = Channels; chan != NULL; chan = chan->NextChan)
 		{
+			if (chann != -1 && chann != chan->EntChannel) continue;
 			if (chan->OrgID == sound_id && (sourcetype == SOURCE_Any ||
 				(chan->SourceType == sourcetype &&
 				chan->Source == source)))
@@ -1103,6 +1104,7 @@ int SoundEngine::GetSoundPlayingInfo (int sourcetype, const void *source, int so
 	{
 		for (FSoundChan* chan = Channels; chan != NULL; chan = chan->NextChan)
 		{
+			if (chann != -1 && chann != chan->EntChannel) continue;
 			if ((sourcetype == SOURCE_Any || (chan->SourceType == sourcetype &&	chan->Source == source)))
 			{
 				count++;
diff --git a/source/common/audio/sound/s_soundinternal.h b/source/common/audio/sound/s_soundinternal.h
index d35cb524a..2184c65c6 100644
--- a/source/common/audio/sound/s_soundinternal.h
+++ b/source/common/audio/sound/s_soundinternal.h
@@ -303,7 +303,7 @@ public:
 	bool IsSourcePlayingSomething(int sourcetype, const void* actor, int channel, int sound_id = -1);
 
 	// Stop and resume music, during game PAUSE.
-	int GetSoundPlayingInfo(int sourcetype, const void* source, int sound_id);
+	int GetSoundPlayingInfo(int sourcetype, const void* source, int sound_id, int chan = -1);
 	void UnloadAllSounds();
 	void Reset();
 	void MarkUsed(int num);
diff --git a/source/games/blood/src/sfx.cpp b/source/games/blood/src/sfx.cpp
index e6985a79c..de1645bce 100644
--- a/source/games/blood/src/sfx.cpp
+++ b/source/games/blood/src/sfx.cpp
@@ -204,7 +204,11 @@ void sfxPlay3DSoundCP(spritetype* pSprite, int soundId, int playchannel, int pla
 
     auto sfx = soundEngine->GetSfx(sid);
     EChanFlags flags = playchannel == -1 ? CHANF_OVERLAP : CHANF_NONE;
-    if (sfx && sfx->LoopStart >= 0) flags |= CHANF_LOOP;
+    if (sfx && sfx->LoopStart >= 0)
+    {
+        flags |= CHANF_LOOP;
+        flags &= ~CHANF_OVERLAP;
+    }
 
     soundEngine->StartSound(SOURCE_Actor, pSprite, &svec, playchannel, flags, sid, volume * (0.8f / 80.f), attenuation, nullptr, pitch / 65536.f);
 }