]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - snd_main.c
added support for dpsoftrast synchronization for fpsscaling
[xonotic/darkplaces.git] / snd_main.c
index 685316ca63a6356cd4e5bb4125b64cbc2b898c8f..fac45a4f6a23a4ce08e2aa8cdc8a0a5eea600bba 100644 (file)
@@ -164,7 +164,7 @@ cvar_t mastervolume = {CVAR_SAVE, "mastervolume", "0.7", "master volume"};
 cvar_t volume = {CVAR_SAVE, "volume", "0.7", "volume of sound effects"};
 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", "2000", "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_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_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"};
@@ -227,7 +227,7 @@ static cvar_t snd_show = {0, "snd_show", "0", "shows some statistics about sound
 // (48KHz because a lot of onboard sound cards sucks at any other speed)
 static cvar_t snd_speed = {CVAR_SAVE, "snd_speed", "48000", "sound output frequency, in hertz"};
 static cvar_t snd_width = {CVAR_SAVE, "snd_width", "2", "sound output precision, in bytes (1 and 2 supported)"};
-static cvar_t snd_channels = {CVAR_SAVE, "snd_channels", "2", "number of channels for the sound ouput (2 for stereo; up to 8 supported for 3D sound)"};
+static cvar_t snd_channels = {CVAR_SAVE, "snd_channels", "2", "number of channels for the sound output (2 for stereo; up to 8 supported for 3D sound)"};
 
 // Ambient sounds
 static sfx_t* ambient_sfxs [2] = { NULL, NULL };
@@ -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;
@@ -1364,6 +1364,31 @@ void SND_Spatialize(channel_t *ch, qboolean isstatic)
                }
        }
 
+       // If this channel does not manage its own volume (like CD tracks)
+       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, mastervol, 655360);
+
+       // 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);
+
        // 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->dist_mult == 0)
@@ -1371,8 +1396,10 @@ void SND_Spatialize(channel_t *ch, qboolean isstatic)
                ch->prologic_invert = 1;
                if (snd_spatialization_prologic.integer != 0)
                {
-                       vol = mastervol * snd_speakerlayout.listeners[i].ambientvolume * sqrt(0.5);
-                       ch->listener_volume[0] = ch->listener_volume[1] = (int)bound(0, vol, 255);
+                       vol = mastervol * snd_speakerlayout.listeners[0].ambientvolume * sqrt(0.5);
+                       ch->listener_volume[0] = (int)bound(0, vol, 65536);
+                       vol = mastervol * snd_speakerlayout.listeners[1].ambientvolume * sqrt(0.5);
+                       ch->listener_volume[1] = (int)bound(0, vol, 65536);
                        for (i = 2;i < SND_LISTENERS;i++)
                                ch->listener_volume[i] = 0;
                }
@@ -1381,7 +1408,7 @@ void SND_Spatialize(channel_t *ch, qboolean isstatic)
                        for (i = 0;i < SND_LISTENERS;i++)
                        {
                                vol = mastervol * snd_speakerlayout.listeners[i].ambientvolume;
-                               ch->listener_volume[i] = (int)bound(0, vol, 255);
+                               ch->listener_volume[i] = (int)bound(0, vol, 65536);
                        }
                }
        }
@@ -1466,9 +1493,9 @@ void SND_Spatialize(channel_t *ch, qboolean isstatic)
                                }
 
                                vol = intensity * sqrt(angle_factor);
-                               ch->listener_volume[0] = (int)bound(0, vol, 255);
+                               ch->listener_volume[0] = (int)bound(0, vol, 65536);
                                vol = intensity * sqrt(1 - angle_factor);
-                               ch->listener_volume[1] = (int)bound(0, vol, 255);
+                               ch->listener_volume[1] = (int)bound(0, vol, 65536);
                                for (i = 2;i < SND_LISTENERS;i++)
                                        ch->listener_volume[i] = 0;
                        }
@@ -1504,7 +1531,7 @@ void SND_Spatialize(channel_t *ch, qboolean isstatic)
 
                                        vol = intensity * max(0, source_vec[0] * snd_speakerlayout.listeners[i].dotscale + snd_speakerlayout.listeners[i].dotbias);
 
-                                       ch->listener_volume[i] = (int)bound(0, vol, 255);
+                                       ch->listener_volume[i] = (int)bound(0, vol, 65536);
                                }
                        }
                }
@@ -1513,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)
@@ -1554,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);
@@ -1748,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 * 255.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)
@@ -1855,6 +1867,7 @@ void S_UpdateAmbientSounds (void)
                vol = (int)ambientlevels[ambient_channel];
                if (vol < 8)
                        vol = 0;
+               vol *= 256;
 
                // Don't adjust volume too fast
                // FIXME: this rounds off to an int each frame, meaning there is little to no fade at extremely high framerates!
@@ -1862,13 +1875,13 @@ void S_UpdateAmbientSounds (void)
                {
                        if (chan->master_vol < vol)
                        {
-                               chan->master_vol += (int)((cl.time - cl.oldtime) * ambient_fade.value);
+                               chan->master_vol += (int)((cl.time - cl.oldtime) * 256.0 * ambient_fade.value);
                                if (chan->master_vol > vol)
                                        chan->master_vol = vol;
                        }
                        else if (chan->master_vol > vol)
                        {
-                               chan->master_vol -= (int)((cl.time - cl.oldtime) * ambient_fade.value);
+                               chan->master_vol -= (int)((cl.time - cl.oldtime) * 256.0 * ambient_fade.value);
                                if (chan->master_vol < vol)
                                        chan->master_vol = vol;
                        }
@@ -1876,14 +1889,15 @@ void S_UpdateAmbientSounds (void)
 
                if (snd_spatialization_prologic.integer != 0)
                {
-                       chan->listener_volume[0] = chan->listener_volume[1] = (int)(chan->master_vol * ambient_level.value * snd_speakerlayout.listeners[i].ambientvolume * sqrt(0.5));
+                       chan->listener_volume[0] = (int)bound(0, chan->master_vol * ambient_level.value * volume.value * mastervolume.value * snd_speakerlayout.listeners[0].ambientvolume * sqrt(0.5), 65536);
+                       chan->listener_volume[1] = (int)bound(0, chan->master_vol * ambient_level.value * volume.value * mastervolume.value * snd_speakerlayout.listeners[1].ambientvolume * sqrt(0.5), 65536);
                        for (i = 2;i < SND_LISTENERS;i++)
                                chan->listener_volume[i] = 0;
                }
                else
                {
                        for (i = 0;i < SND_LISTENERS;i++)
-                               chan->listener_volume[i] = (int)(chan->master_vol * ambient_level.value * snd_speakerlayout.listeners[i].ambientvolume);
+                               chan->listener_volume[i] = (int)bound(0, chan->master_vol * ambient_level.value * volume.value * mastervolume.value * snd_speakerlayout.listeners[i].ambientvolume, 65536);
                }
        }
 }
@@ -2180,7 +2194,7 @@ void S_Update(const matrix4x4_t *listenermatrix)
                        {
                                for (j = 0;j < SND_LISTENERS;j++)
                                {
-                                       combine->listener_volume[j] += ch->listener_volume[j];
+                                       combine->listener_volume[j] = bound(0, combine->listener_volume[j] + ch->listener_volume[j], 65536);
                                        ch->listener_volume[j] = 0;
                                }
                        }