From 1710425a7fdf365e177bc70dae4b89eea90df13e Mon Sep 17 00:00:00 2001 From: divverent Date: Thu, 23 Sep 2010 05:17:14 +0000 Subject: [PATCH] allow bgmvolume to be > 1 to be able to exceed mastervolume (but this and other "> 1" values are still clamped to 10, i.e. 10 dB); apply ReplayGain in SND_Spatialize instead of early when setting channel volume to better guard against clipping git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@10484 d7cf8633-e32d-0410-b094-e92efae38249 --- snd_main.c | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/snd_main.c b/snd_main.c index a54591f1..5d61f51b 100644 --- a/snd_main.c +++ b/snd_main.c @@ -1264,7 +1264,7 @@ Spatializes a channel ================= */ extern cvar_t cl_gameplayfix_soundsmovewithentities; -void SND_Spatialize(channel_t *ch, qboolean isstatic) +void SND_Spatialize_WithSfx(channel_t *ch, qboolean isstatic, sfx_t *sfx) { int i; double f; @@ -1374,6 +1374,18 @@ void SND_Spatialize(channel_t *ch, qboolean isstatic) // always apply "master" mastervol *= mastervolume.value; + // add in ReplayGain very late; prevent clipping when close + if(sfx) + if(sfx->volume_peak > 0) + { + // Replaygain support + // Con_DPrintf("Setting volume on ReplayGain-enabled track... %f -> ", fvol); + mastervol *= sfx->volume_mult; + if(mastervol * sfx->volume_peak > 65536) + mastervol = 65536 / sfx->volume_peak; + // Con_DPrintf("%f\n", fvol); + } + // clamp HERE to keep relative volumes of the channels correct mastervol = bound(0, mastervol, 65536); @@ -1528,13 +1540,17 @@ void SND_Spatialize(channel_t *ch, qboolean isstatic) ch->listener_volume[i] = 0; } } +void SND_Spatialize(channel_t *ch, qboolean isstatic) +{ + sfx_t *sfx = ch->sfx; + SND_Spatialize_WithSfx(ch, isstatic, sfx); +} // ======================================================================= // Start a sound effect // ======================================================================= -static void S_SetChannelVolume_WithSfx (unsigned int ch_ind, float fvol, sfx_t *sfx); void S_PlaySfxOnChannel (sfx_t *sfx, channel_t *target_chan, unsigned int flags, vec3_t origin, float fvol, float attenuation, qboolean isstatic, int entnum, int entchannel, int startpos) { if (!sfx) @@ -1569,12 +1585,9 @@ void S_PlaySfxOnChannel (sfx_t *sfx, channel_t *target_chan, unsigned int flags, else target_chan->dist_mult = attenuation / snd_soundradius.value; - // we have to set the channel volume AFTER the sfx because the function - // needs it for replaygain support - S_SetChannelVolume_WithSfx(target_chan - channels, fvol, sfx); - // set the listener volumes - SND_Spatialize (target_chan, isstatic); + S_SetChannelVolume(target_chan - channels, fvol); + SND_Spatialize_WithSfx (target_chan, isstatic, sfx); // Lock the SFX during play S_LockSfx (sfx); @@ -1763,25 +1776,9 @@ void S_PauseGameSounds (qboolean toggle) } } -static void S_SetChannelVolume_WithSfx (unsigned int ch_ind, float fvol, sfx_t *sfx) -{ - if(sfx->volume_peak > 0) - { - // Replaygain support - // Con_DPrintf("Setting volume on ReplayGain-enabled track... %f -> ", fvol); - fvol *= sfx->volume_mult; - if(fvol * sfx->volume_peak > 1) - fvol = 1 / sfx->volume_peak; - // Con_DPrintf("%f\n", fvol); - } - channels[ch_ind].master_vol = (int)(fvol * 65536.0f); -} - void S_SetChannelVolume(unsigned int ch_ind, float fvol) { - sfx_t *sfx = channels[ch_ind].sfx; - if(sfx) - S_SetChannelVolume_WithSfx(ch_ind, fvol, sfx); + channels[ch_ind].master_vol = (int)(fvol * 65536.0f); } float S_GetChannelPosition (unsigned int ch_ind) -- 2.39.2