From e2f18c83c880b7e61c8c7ba0f45adc819bbb6e1d Mon Sep 17 00:00:00 2001 From: molivier Date: Fri, 2 Apr 2004 13:23:20 +0000 Subject: [PATCH] Added a CHANNELFLAG_LOCALSOUND flag for channels playing a client-side sound, such as menu sounds for instance. Added the ability to pause/resume channels. When the game is paused, all non-local sounds are now automatically paused (previously, no new sound was started during the pause, but those already created were played normally). git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@4078 d7cf8633-e32d-0410-b094-e92efae38249 --- cl_parse.c | 6 ++++++ snd_dma.c | 50 +++++++++++++++++++++++++++++++++++++++++++------- snd_mix.c | 11 +++++++++++ snd_null.c | 11 ++++++++++- sound.h | 6 +++++- 5 files changed, 75 insertions(+), 9 deletions(-) diff --git a/cl_parse.c b/cl_parse.c index 88042842..b8c47759 100644 --- a/cl_parse.c +++ b/cl_parse.c @@ -1667,9 +1667,15 @@ void CL_ParseServerMessage(void) case svc_setpause: cl.paused = MSG_ReadByte (); if (cl.paused) + { CDAudio_Pause (); + S_PauseGameSounds (); + } else + { CDAudio_Resume (); + S_ResumeGameSounds (); + } break; case svc_signonnum: diff --git a/snd_dma.c b/snd_dma.c index 24525fee..ea4d6a40 100644 --- a/snd_dma.c +++ b/snd_dma.c @@ -527,7 +527,7 @@ void SND_Spatialize(channel_t *ch, int isstatic) // Start a sound effect // ======================================================================= -void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation) +int S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation) { channel_t *target_chan, *check; int vol; @@ -535,14 +535,14 @@ void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float f size_t skip; if (!sound_started || !sfx || !sfx->fetcher || nosound.integer) - return; + return -1; vol = fvol*255; // pick a channel to play on target_chan = SND_PickChannel(entnum, entchannel); if (!target_chan) - return; + return -1; // spatialize memset (target_chan, 0, sizeof(*target_chan)); @@ -561,7 +561,7 @@ void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float f if (!S_LoadSound (sfx, true)) { target_chan->sfx = NULL; - return; // couldn't load the sound's data + return -1; // couldn't load the sound's data } target_chan->sfx = sfx; @@ -590,6 +590,8 @@ void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float f break; } } + + return (channels - target_chan); } void S_StopSound(int entnum, int entchannel) @@ -622,6 +624,34 @@ void S_StopAllSoundsC(void) S_StopAllSounds(true); } +void S_PauseGameSounds (void) +{ + unsigned int i; + + for (i = 0; i < total_channels; i++) + { + channel_t *ch; + + ch = &channels[i]; + if (ch->sfx != NULL && ! (ch->flags & CHANNELFLAG_LOCALSOUND)) + ch->flags |= CHANNELFLAG_PAUSED; + } +} + +void S_ResumeGameSounds (void) +{ + unsigned int i; + + for (i = 0; i < total_channels; i++) + { + channel_t *ch; + + ch = &channels[i]; + if (ch->sfx != NULL && ! (ch->flags & CHANNELFLAG_LOCALSOUND)) + ch->flags &= ~CHANNELFLAG_PAUSED; + } +} + void S_ClearBuffer(void) { int clear; @@ -963,7 +993,7 @@ console functions static void S_Play_Common(float fvol, float attenuation) { - int i; + int i, ch_ind; char name[256]; sfx_t *sfx; @@ -985,7 +1015,9 @@ static void S_Play_Common(float fvol, float attenuation) else i++; - S_StartSound(-1, 0, sfx, listener_vieworigin, fvol, attenuation); + ch_ind = S_StartSound(-1, 0, sfx, listener_vieworigin, fvol, attenuation); + if (ch_ind >= 0) + channels[ch_ind].flags |= CHANNELFLAG_LOCALSOUND; } } @@ -1027,6 +1059,7 @@ void S_SoundList(void) void S_LocalSound (char *sound) { sfx_t *sfx; + int ch_ind; if (!snd_initialized.integer || nosound.integer) return; @@ -1037,7 +1070,10 @@ void S_LocalSound (char *sound) Con_Printf("S_LocalSound: can't precache %s\n", sound); return; } - S_StartSound (cl.viewentity, -1, sfx, vec3_origin, 1, 1); + + ch_ind = S_StartSound (cl.viewentity, -1, sfx, vec3_origin, 1, 1); + if (ch_ind >= 0) + channels[ch_ind].flags |= CHANNELFLAG_LOCALSOUND; } diff --git a/snd_mix.c b/snd_mix.c index f6cd8e88..e4759cfd 100644 --- a/snd_mix.c +++ b/snd_mix.c @@ -296,6 +296,17 @@ void S_PaintChannels(int endtime) if (!S_LoadSound (sfx, true)) continue; + // if the channel is paused + if (ch->flags & CHANNELFLAG_PAUSED) + { + size_t pausedtime; + + pausedtime = end - paintedtime; + ch->lastptime += pausedtime; + ch->end += pausedtime; + continue; + } + // if the sound hasn't been painted last time, update his position if (ch->lastptime < paintedtime) { diff --git a/snd_null.c b/snd_null.c index a1c45869..d6f1269f 100755 --- a/snd_null.c +++ b/snd_null.c @@ -72,14 +72,23 @@ void S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation) { } -void S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation) +int S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation) { + return -1; } void S_StopSound (int entnum, int entchannel) { } +void S_PauseGameSounds (void) +{ +} + +void S_ResumeGameSounds (void) +{ +} + sfx_t *S_GetCached(const char *name) { return NULL; diff --git a/sound.h b/sound.h index 8aff39c8..4107320a 100644 --- a/sound.h +++ b/sound.h @@ -69,6 +69,8 @@ typedef struct // channel_t flags #define CHANNELFLAG_NONE 0 #define CHANNELFLAG_FORCELOOP (1 << 0) // force looping even if the sound is not looped +#define CHANNELFLAG_LOCALSOUND (1 << 1) // non-game sound (ex: menu sound) +#define CHANNELFLAG_PAUSED (1 << 2) typedef struct { @@ -99,10 +101,12 @@ struct snd_fetcher_s void S_Init (void); void S_Startup (void); void S_Shutdown (void); -void S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation); +int S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation); void S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation); void S_StopSound (int entnum, int entchannel); void S_StopAllSounds(qboolean clear); +void S_PauseGameSounds (void); +void S_ResumeGameSounds (void); void S_ClearBuffer (void); void S_Update(vec3_t origin, vec3_t forward, vec3_t left, vec3_t up); void S_ExtraUpdate (void); -- 2.39.2