Skip to content

Commit c61dbf8

Browse files
committed
Destroy dead sounds
1 parent 97c4466 commit c61dbf8

File tree

7 files changed

+25
-22
lines changed

7 files changed

+25
-22
lines changed

src/engine/audio/Audio.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ namespace Audio {
5858
// in a frame, it means it sould be destroyed.
5959
struct entityLoop_t {
6060
bool addedThisFrame;
61+
bool persistent;
6162
std::shared_ptr<LoopingSound> sound;
6263
sfxHandle_t newSfx;
6364
sfxHandle_t oldSfx;
@@ -148,7 +149,7 @@ namespace Audio {
148149
UpdateListenerGain();
149150

150151
for (auto &loop : entityLoops) {
151-
loop = {false, nullptr, -1, -1};
152+
loop = {false, false, nullptr, -1, -1};
152153
}
153154

154155
return true;
@@ -164,7 +165,7 @@ namespace Audio {
164165
if (loop.sound) {
165166
loop.sound->Stop();
166167
}
167-
loop = {false, nullptr, -1, -1};
168+
loop = {false, false, nullptr, -1, -1};
168169
}
169170

170171
StopMusic();
@@ -196,17 +197,23 @@ namespace Audio {
196197
for (int i = 0; i < MAX_GENTITIES; i++) {
197198
entityLoop_t& loop = entityLoops[i];
198199
if (loop.sound and not loop.addedThisFrame) {
199-
// The loop wasn't added this frame, that means it has to be removed.
200-
loop.sound->soundGain = 0;
200+
if ( loop.persistent ) {
201+
loop.sound->soundGain = 0;
202+
} else {
203+
// The loop wasn't added this frame, that means it has to be removed.
204+
loop.sound->FadeOutAndDie();
205+
loop = { false, false, nullptr, -1, -1 };
206+
}
201207
} else if (loop.oldSfx != loop.newSfx) {
202208
// The last sfx added in the frame is not the current one being played
203209
// To mimic the previous sound system's behavior we sart playing the new one.
204210
loop.sound->FadeOutAndDie();
205211

206212
int newSfx = loop.newSfx;
207-
loop = {false, nullptr, -1, -1};
213+
bool persistent = loop.persistent;
214+
loop = {false, false, nullptr, -1, -1};
208215

209-
AddEntityLoopingSound(i, newSfx);
216+
AddEntityLoopingSound(i, newSfx, persistent);
210217
}
211218
}
212219

@@ -221,7 +228,7 @@ namespace Audio {
221228
loop.addedThisFrame = false;
222229
// if we are the unique owner of a loop pointer, then it means it was stopped, free it.
223230
if (loop.sound.use_count() == 1) {
224-
loop = {false, nullptr, -1, -1};
231+
loop = {false, false, nullptr, -1, -1};
225232
}
226233
}
227234

@@ -286,7 +293,7 @@ namespace Audio {
286293
AddSound(GetLocalEmitter(), std::make_shared<OneShotSound>(Sample::FromHandle(sfx)), 1);
287294
}
288295

289-
void AddEntityLoopingSound(int entityNum, sfxHandle_t sfx) {
296+
void AddEntityLoopingSound(int entityNum, sfxHandle_t sfx, bool persistent) {
290297
if (not initialized or not Sample::IsValidHandle(sfx) or not IsValidEntity(entityNum)) {
291298
return;
292299
}
@@ -300,6 +307,7 @@ namespace Audio {
300307
AddSound(GetEmitterForEntity(entityNum), loop.sound, 1);
301308
}
302309
loop.addedThisFrame = true;
310+
loop.persistent = persistent;
303311

304312
// We remember what is the last sfx asked because cgame expects the sfx added last in the frame to be played
305313
loop.newSfx = sfx;

src/engine/audio/Audio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace Audio {
5050
void StartSound(int entityNum, Vec3 origin, sfxHandle_t sfx);
5151
void StartLocalSound(int entityNum);
5252

53-
void AddEntityLoopingSound(int entityNum, sfxHandle_t sfx);
53+
void AddEntityLoopingSound(int entityNum, sfxHandle_t sfx, bool persistent);
5454
void ClearAllLoopingSounds();
5555
void ClearLoopingSoundsForEntity(int entityNum);
5656

src/engine/client/cg_msgdef.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ namespace Audio {
282282
using StartSoundMsg = IPC::Message<IPC::Id<VM::QVM, CG_S_STARTSOUND>, bool, Vec3, int, int>;
283283
using StartLocalSoundMsg = IPC::Message<IPC::Id<VM::QVM, CG_S_STARTLOCALSOUND>, int>;
284284
using ClearLoopingSoundsMsg = IPC::Message<IPC::Id<VM::QVM, CG_S_CLEARLOOPINGSOUNDS>>;
285-
using AddLoopingSoundMsg = IPC::Message<IPC::Id<VM::QVM, CG_S_ADDLOOPINGSOUND>, int, int>;
285+
using AddLoopingSoundMsg = IPC::Message<IPC::Id<VM::QVM, CG_S_ADDLOOPINGSOUND>, int, int, bool>;
286286
using StopLoopingSoundMsg = IPC::Message<IPC::Id<VM::QVM, CG_S_STOPLOOPINGSOUND>, int>;
287287
using UpdateEntityPositionMsg = IPC::Message<IPC::Id<VM::QVM, CG_S_UPDATEENTITYPOSITION>, int, Vec3>;
288288
using RespatializeMsg = IPC::Message<IPC::Id<VM::QVM, CG_S_RESPATIALIZE>, int, std::array<Vec3, 3>>;

src/engine/client/cl_cgame.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,8 +1506,8 @@ void CGameVM::CmdBuffer::HandleCommandBufferSyscall(int major, int minor, Util::
15061506
break;
15071507

15081508
case CG_S_ADDLOOPINGSOUND:
1509-
HandleMsg<Audio::AddLoopingSoundMsg>(std::move(reader), [this] (int entityNum, int sfx) {
1510-
Audio::AddEntityLoopingSound(entityNum, sfx);
1509+
HandleMsg<Audio::AddLoopingSoundMsg>(std::move(reader), [this] (int entityNum, int sfx, bool persistent) {
1510+
Audio::AddEntityLoopingSound(entityNum, sfx, persistent);
15111511
});
15121512
break;
15131513

src/engine/null/NullAudio.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace Audio {
6565
}
6666

6767

68-
void AddEntityLoopingSound(int, sfxHandle_t) {
68+
void AddEntityLoopingSound(int, sfxHandle_t, bool) {
6969
}
7070

7171
void ClearAllLoopingSounds() {

src/shared/client/cg_api.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,15 @@ void trap_S_ClearLoopingSounds( bool )
153153
cmdBuffer.SendMsg<Audio::ClearLoopingSoundsMsg>();
154154
}
155155

156-
void trap_S_AddLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfx )
156+
void trap_S_AddLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfx, bool persistent )
157157
{
158158
if (origin) {
159159
trap_S_UpdateEntityPosition(entityNum, origin);
160160
}
161161
if (velocity) {
162162
trap_S_UpdateEntityVelocity(entityNum, velocity);
163163
}
164-
cmdBuffer.SendMsg<Audio::AddLoopingSoundMsg>(entityNum, sfx);
165-
}
166-
167-
void trap_S_AddRealLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfx )
168-
{
169-
trap_S_AddLoopingSound(entityNum, origin, velocity, sfx);
164+
cmdBuffer.SendMsg<Audio::AddLoopingSoundMsg>( entityNum, sfx, persistent );
170165
}
171166

172167
void trap_S_StopLoopingSound( int entityNum )

src/shared/client/cg_api.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ void trap_CM_BatchMarkFragments(
5353
void trap_S_StartSound( vec3_t origin, int entityNum, soundChannel_t entchannel, sfxHandle_t sfx );
5454
void trap_S_StartLocalSound( sfxHandle_t sfx, soundChannel_t channelNum );
5555
void trap_S_ClearLoopingSounds( bool killall );
56-
void trap_S_AddLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfx );
57-
void trap_S_AddRealLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfx );
56+
void trap_S_AddLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfx,
57+
bool persistent = false );
5858
void trap_S_StopLoopingSound( int entityNum );
5959
void trap_S_UpdateEntityPosition( int entityNum, const vec3_t origin );
6060
void trap_S_Respatialize( int entityNum, const vec3_t origin, vec3_t axis[ 3 ], int inwater );

0 commit comments

Comments
 (0)