#include "snd_modplug.h"
#include "csprogs.h"
#include "cl_collision.h"
+#include "cdaudio.h"
#define SND_MIN_SPEED 8000
cvar_t snd_initialized = { CVAR_READONLY, "snd_initialized", "0", "indicates the sound subsystem is active"};
cvar_t snd_staticvolume = {CVAR_SAVE, "snd_staticvolume", "1", "volume of ambient sound effects (such as swampy sounds at the start of e1m2)"};
cvar_t snd_soundradius = {CVAR_SAVE, "snd_soundradius", "1200", "radius of weapon sounds and other standard sound effects (monster idle noises are half this radius and flickering light noises are one third of this radius)"};
+cvar_t snd_attenuation_exponent = {CVAR_SAVE, "snd_attenuation_exponent", "1", "Exponent of (1-radius) in sound attenuation formula"};
+cvar_t snd_attenuation_decibel = {CVAR_SAVE, "snd_attenuation_decibel", "0", "Decibel sound attenuation per sound radius distance"};
cvar_t snd_spatialization_min_radius = {CVAR_SAVE, "snd_spatialization_min_radius", "10000", "use minimum spatialization above to this radius"};
cvar_t snd_spatialization_max_radius = {CVAR_SAVE, "snd_spatialization_max_radius", "100", "use maximum spatialization below this radius"};
cvar_t snd_spatialization_min = {CVAR_SAVE, "snd_spatialization_min", "0.70", "minimum spatializazion of sounds"};
extern cvar_t v_flipped;
cvar_t snd_channellayout = {0, "snd_channellayout", "0", "channel layout. Can be 0 (auto - snd_restart needed), 1 (standard layout), or 2 (ALSA layout)"};
cvar_t snd_mutewhenidle = {CVAR_SAVE, "snd_mutewhenidle", "1", "whether to disable sound output when game window is inactive"};
+cvar_t snd_maxchannelvolume = {CVAR_SAVE, "snd_maxchannelvolume", "10", "maximum volume of a single sound"};
+cvar_t snd_softclip = {CVAR_SAVE, "snd_softclip", "0", "Use soft-clipping. Soft-clipping can make the sound more smooth if very high volume levels are used. Enable this option if the dynamic range of the loudspeakers is very low. WARNING: This feature creates distortion and should be considered a last resort."};
+//cvar_t snd_softclip = {CVAR_SAVE, "snd_softclip", "0", "Use soft-clipping (when set to 2, use it even if output is floating point). Soft-clipping can make the sound more smooth if very high volume levels are used. Enable this option if the dynamic range of the loudspeakers is very low. WARNING: This feature creates distortion and should be considered a last resort."};
cvar_t snd_entchannel0volume = {CVAR_SAVE, "snd_entchannel0volume", "1", "volume multiplier of the auto-allocate entity channel of regular entities (DEPRECATED)"};
cvar_t snd_entchannel1volume = {CVAR_SAVE, "snd_entchannel1volume", "1", "volume multiplier of the 1st entity channel of regular entities (DEPRECATED)"};
cvar_t snd_entchannel2volume = {CVAR_SAVE, "snd_entchannel2volume", "1", "volume multiplier of the 2nd entity channel of regular entities (DEPRECATED)"};
static cvar_t snd_startloopingsounds = {0, "snd_startloopingsounds", "1", "whether to start sounds that would loop (you want this to be 1); existing sounds are not affected"};
static cvar_t snd_startnonloopingsounds = {0, "snd_startnonloopingsounds", "1", "whether to start sounds that would not loop (you want this to be 1); existing sounds are not affected"};
+// randomization
+static cvar_t snd_identicalsoundrandomization_time = {0, "snd_identicalsoundrandomization_time", "0.1", "how much seconds to randomly skip (positive) or delay (negative) sounds when multiple identical sounds are started on the same frame"};
+static cvar_t snd_identicalsoundrandomization_tics = {0, "snd_identicalsoundrandomization_tics", "0", "if nonzero, how many tics to limit sound randomization as defined by snd_identicalsoundrandomization_time"};
+
// Ambient sounds
static sfx_t* ambient_sfxs [2] = { NULL, NULL };
static const char* ambient_names [2] = { "sound/ambience/water1.wav", "sound/ambience/wind2.wav" };
}
-void S_SoundInfo_f(void)
+static void S_SoundInfo_f(void)
{
if (snd_renderbuffer == NULL)
{
sound_spatialized = false;
}
-void S_Restart_f(void)
+static void S_Restart_f(void)
{
// NOTE: we can't free all sounds if we are running a map (this frees sfx_t that are still referenced by precaches)
// So, refuse to do this if we are connected.
Cvar_RegisterVariable(&snd_channel6volume);
Cvar_RegisterVariable(&snd_channel7volume);
+ Cvar_RegisterVariable(&snd_attenuation_exponent);
+ Cvar_RegisterVariable(&snd_attenuation_decibel);
+
Cvar_RegisterVariable(&snd_spatialization_min_radius);
Cvar_RegisterVariable(&snd_spatialization_max_radius);
Cvar_RegisterVariable(&snd_spatialization_min);
Cvar_RegisterVariable(&snd_width);
Cvar_RegisterVariable(&snd_channels);
Cvar_RegisterVariable(&snd_mutewhenidle);
+ Cvar_RegisterVariable(&snd_maxchannelvolume);
+ Cvar_RegisterVariable(&snd_softclip);
Cvar_RegisterVariable(&snd_startloopingsounds);
Cvar_RegisterVariable(&snd_startnonloopingsounds);
+ Cvar_RegisterVariable(&snd_identicalsoundrandomization_time);
+ Cvar_RegisterVariable(&snd_identicalsoundrandomization_tics);
+
// COMMANDLINEOPTION: Sound: -nosound disables sound (including CD audio)
if (COM_CheckParm("-nosound"))
{
Picks a channel based on priorities, empty slots, number of channels
=================
*/
-channel_t *SND_PickChannel(int entnum, int entchannel)
+static channel_t *SND_PickChannel(int entnum, int entchannel)
{
int ch_idx;
int first_to_die;
=================
*/
extern cvar_t cl_gameplayfix_soundsmovewithentities;
-void SND_Spatialize_WithSfx(channel_t *ch, qboolean isstatic, sfx_t *sfx)
+static void SND_Spatialize_WithSfx(channel_t *ch, qboolean isstatic, sfx_t *sfx)
{
int i;
double f;
float angle_side, angle_front, angle_factor, mixspeed;
vec_t dist, mastervol, intensity;
vec3_t source_vec;
+ char vabuf[1024];
// update sound origin if we know about the entity
if (ch->entnum > 0 && cls.state == ca_connected && cl_gameplayfix_soundsmovewithentities.integer)
case 5: mastervol *= snd_channel5volume.value; break;
case 6: mastervol *= snd_channel6volume.value; break;
case 7: mastervol *= snd_channel7volume.value; break;
- default: mastervol *= Cvar_VariableValueOr(va("snd_channel%dvolume", CHAN_ENGINE2CVAR(ch->entchannel)), 1.0); break;
+ default: mastervol *= Cvar_VariableValueOr(va(vabuf, sizeof(vabuf), "snd_channel%dvolume", CHAN_ENGINE2CVAR(ch->entchannel)), 1.0); break;
}
}
if (!(ch->flags & CHANNELFLAG_FULLVOLUME))
mastervol *= volume.value;
- // clamp HERE to allow to go at most 10dB past mastervolume (before clamping), when mastervolume < -10dB (so relative volumes don't get too messy)
- mastervol = bound(0.0f, mastervol, 10.0f);
+ if(snd_maxchannelvolume.value > 0)
+ {
+ // clamp HERE to allow to go at most 10dB past mastervolume (before clamping), when mastervolume < -10dB (so relative volumes don't get too messy)
+ mastervol = bound(0.0f, mastervol, 10.0f * snd_maxchannelvolume.value);
+ }
// always apply "master"
mastervol *= mastervolume.value;
// Replaygain support
// Con_DPrintf("Setting volume on ReplayGain-enabled track... %f -> ", fvol);
mastervol *= sfx->volume_mult;
- if(mastervol * sfx->volume_peak > 1.0f)
- mastervol = 1.0f / sfx->volume_peak;
+ if(snd_maxchannelvolume.value > 0)
+ {
+ if(mastervol * sfx->volume_peak > snd_maxchannelvolume.value)
+ mastervol = snd_maxchannelvolume.value / sfx->volume_peak;
+ }
// Con_DPrintf("%f\n", fvol);
}
- // clamp HERE to keep relative volumes of the channels correct
- mastervol = bound(0.0f, mastervol, 1.0f);
+ if(snd_maxchannelvolume.value > 0)
+ {
+ // clamp HERE to keep relative volumes of the channels correct
+ mastervol = min(mastervol, snd_maxchannelvolume.value);
+ }
+
+ mastervol = max(0.0f, mastervol);
ch->mixspeed = mixspeed;
// calculate stereo seperation and distance attenuation
VectorSubtract(listener_origin, ch->origin, source_vec);
dist = VectorLength(source_vec);
- intensity = mastervol * (1.0f - dist * ch->distfade);
+ f = dist * ch->distfade;
+
+ f =
+ ((snd_attenuation_exponent.value == 0) ? 1.0 : pow(1.0 - min(1.0, f), (double)snd_attenuation_exponent.value))
+ *
+ ((snd_attenuation_decibel.value == 0) ? 1.0 : pow(0.1, 0.1 * snd_attenuation_decibel.value * f));
+
+ intensity = mastervol * f;
if (intensity > 0)
{
qboolean occluded = false;
ch->volume[i] = 0;
}
}
-void SND_Spatialize(channel_t *ch, qboolean isstatic)
+static void SND_Spatialize(channel_t *ch, qboolean isstatic)
{
sfx_t *sfx = ch->sfx;
SND_Spatialize_WithSfx(ch, isstatic, sfx);
// Start a sound effect
// =======================================================================
-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, float fspeed)
+static 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, float fspeed)
{
if (!sfx)
{
{
if (check == target_chan)
continue;
- if (check->sfx == sfx && check->position == 0)
+ if (check->sfx == sfx && check->position == 0 && check->basespeed == fspeed)
{
+ // calculate max offset
+ float maxtime = snd_identicalsoundrandomization_time.value;
+ float maxtics = snd_identicalsoundrandomization_tics.value;
+ float maxticsdelta = ((cls.state == ca_connected) ? (maxtics * (cl.mtime[0] - cl.mtime[1])) : 0);
+ float maxdelta = 0;
+ if(maxticsdelta == 0 || fabs(maxticsdelta) > fabs(maxtime))
+ maxdelta = maxtime;
+ else
+ maxdelta = fabs(maxticsdelta) * ((maxtime > 0) ? 1 : -1);
+
// use negative pos offset to delay this sound effect
- startpos = (int)lhrandom(0, -0.1 * snd_renderbuffer->format.speed);
+ startpos = lhrandom(0, maxdelta * sfx->format.speed);
break;
}
}
return (target_chan - channels);
}
-int S_StartSound_StartPosition (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation, float startposition)
-{
- return S_StartSound_StartPosition_Flags(entnum, entchannel, sfx, origin, fvol, attenuation, startposition, CHANNELFLAG_NONE, 1.0f);
-}
-
int S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
{
- return S_StartSound_StartPosition(entnum, entchannel, sfx, origin, fvol, attenuation, 0);
+ return S_StartSound_StartPosition_Flags(entnum, entchannel, sfx, origin, fvol, attenuation, 0, CHANNELFLAG_NONE, 1.0f);
}
void S_StopChannel (unsigned int channel_ind, qboolean lockmutex, qboolean freesfx)
}
}
-extern void CDAudio_Stop(void);
void S_StopAllSounds (void)
{
unsigned int i;
S_UpdateAmbientSounds
===================
*/
-void S_UpdateAmbientSounds (void)
+static void S_UpdateAmbientSounds (void)
{
int i;
float vol;