#include "snd_main.h"
#include "snd_ogg.h"
-#include "snd_modplug.h"
#include "csprogs.h"
#include "cl_collision.h"
+#ifdef CONFIG_CD
+#include "cdaudio.h"
+#endif
#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_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_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
{
unsigned int size;
- size = sfx->memsize;
+ size = (unsigned int)sfx->memsize;
Con_Printf ("%c%c%c(%5iHz %2db %6s) %8i : %s\n",
(sfx->loopstart < sfx->total_length) ? 'L' : ' ',
(sfx->flags & SFXFLAG_STREAMED) ? 'S' : ' ',
}
-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);
memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
OGG_OpenLibrary ();
- ModPlug_OpenLibrary ();
}
void S_Terminate (void)
{
S_Shutdown ();
- ModPlug_CloseLibrary ();
OGG_CloseLibrary ();
// Free all SFXs
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;
}
// don't let monster sounds override player sounds
- if (ch->entnum == cl.viewentity && entnum != cl.viewentity)
+ if ((ch->entnum == cl.viewentity || ch->entnum == CL_VM_GetViewEntity()) && !(entnum == cl.viewentity || entnum == CL_VM_GetViewEntity()))
continue;
// don't override looped sounds
=================
*/
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)
else
Matrix4x4_OriginFromMatrix(&cl.entities[ch->entnum].render.matrix, ch->origin);
}
+ else if (cl.csqc_server2csqcentitynumber[ch->entnum])
+ {
+ //Con_Printf("-- entnum %i (client %i) origin %f %f %f neworigin %f %f %f\n", ch->entnum, cl.csqc_server2csqcentitynumber[ch->entnum], ch->origin[0], ch->origin[1], ch->origin[2], cl.entities[ch->entnum].state_current.origin[0], cl.entities[ch->entnum].state_current.origin[1], cl.entities[ch->entnum].state_current.origin[2]);
+
+ if (!CL_VM_GetEntitySoundOrigin(cl.csqc_server2csqcentitynumber[ch->entnum] + MAX_EDICTS, ch->origin))
+ ch->entnum = MAX_EDICTS; // entity was removed, disown sound
+ }
}
mastervol = ch->basevolume;
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;
// anything coming from the view entity will always be full volume
// LordHavoc: make sounds with ATTN_NONE have no spatialization
- if (ch->entnum == cl.viewentity || ch->distfade == 0)
+ if (ch->entnum == cl.viewentity || ch->entnum == CL_VM_GetViewEntity() || ch->distfade == 0)
{
ch->prologic_invert = 1;
if (snd_spatialization_prologic.integer != 0)
// 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)
{
int S_StartSound_StartPosition_Flags (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation, float startposition, int flags, float fspeed)
{
channel_t *target_chan, *check, *ch;
- int ch_idx, startpos;
+ int ch_idx, startpos, i;
if (snd_renderbuffer == NULL || sfx == NULL || nosound.integer)
return -1;
{
S_SetChannelVolume(ch_idx, fvol);
S_SetChannelSpeed(ch_idx, fspeed);
+ for(i = 1; i > 0 && (i <= flags || i <= (int) channels[ch_idx].flags); i <<= 1)
+ if((flags ^ channels[ch_idx].flags) & i)
+ S_SetChannelFlag(ch_idx, i, (flags & i) != 0);
ch->distfade = attenuation / snd_soundradius.value;
SND_Spatialize(ch, false);
return ch_idx;
float maxtics = snd_identicalsoundrandomization_tics.value;
float maxticsdelta = ((cls.state == ca_connected) ? (maxtics * (cl.mtime[0] - cl.mtime[1])) : 0);
float maxdelta = 0;
- if(maxtics == 0 || fabs(maxticsdelta) > fabs(maxtime))
+ if(maxticsdelta == 0 || fabs(maxticsdelta) > fabs(maxtime))
maxdelta = maxtime;
else
maxdelta = fabs(maxticsdelta) * ((maxtime > 0) ? 1 : -1);
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)
ch = &channels[channel_ind];
sfx = ch->sfx;
- if (ch->sfx != NULL)
+ if (sfx != NULL)
{
if (sfx->fetcher != NULL && sfx->fetcher->stopchannel != NULL)
sfx->fetcher->stopchannel(ch);
ch->fetcher_data = NULL;
ch->sfx = NULL;
+ if (freesfx)
+ S_FreeSfx(sfx, true);
}
if (lockmutex && !simsound)
SndSys_UnlockRenderBuffer();
- if (freesfx)
- S_FreeSfx(sfx, true);
}
}
}
-extern void CDAudio_Stop(void);
void S_StopAllSounds (void)
{
unsigned int i;
if (snd_renderbuffer == NULL)
return;
+#ifdef CONFIG_CD
// stop CD audio because it may be using a faketrack
CDAudio_Stop();
+#endif
if (simsound || SndSys_LockRenderBuffer ())
{
S_UpdateAmbientSounds
===================
*/
-void S_UpdateAmbientSounds (void)
+static void S_UpdateAmbientSounds (void)
{
int i;
float vol;