]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - snd_main.c
fix GL11/GL13 fogged sprite rendering and q1bsp sky rendering - the
[xonotic/darkplaces.git] / snd_main.c
index e4a4934a2993458f8044e4bbd6e51e2fa3e00de8..40a8c187f60ff330a5dfbdf58277aceef22901eb 100644 (file)
@@ -25,6 +25,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "snd_ogg.h"
 #include "snd_modplug.h"
 #include "csprogs.h"
+#include "cl_collision.h"
 
 
 #define SND_MIN_SPEED 8000
@@ -150,7 +151,7 @@ static int current_swapstereo = false;
 static int current_channellayout = SND_CHANNELLAYOUT_AUTO;
 static int current_channellayout_used = SND_CHANNELLAYOUT_AUTO;
 
-static double spatialpower, spatialmin, spatialdiff, spatialoffset, spatialfactor;
+static float spatialpower, spatialmin, spatialdiff, spatialoffset, spatialfactor;
 typedef enum { SPATIAL_NONE, SPATIAL_LOG, SPATIAL_POW, SPATIAL_THRESH } spatialmethod_t;
 spatialmethod_t spatialmethod;
 
@@ -166,10 +167,10 @@ cvar_t snd_spatialization_min = {CVAR_SAVE, "snd_spatialization_min", "0.70", "m
 cvar_t snd_spatialization_max = {CVAR_SAVE, "snd_spatialization_max", "0.95", "maximum spatialization of sounds"};
 cvar_t snd_spatialization_power = {CVAR_SAVE, "snd_spatialization_power", "0", "exponent of the spatialization falloff curve (0: logarithmic)"};
 cvar_t snd_spatialization_control = {CVAR_SAVE, "snd_spatialization_control", "0", "enable spatialization control (headphone friendly mode)"};
+cvar_t snd_spatialization_occlusion = {CVAR_SAVE, "snd_spatialization_occlusion", "1", "enable occlusion testing on spatialized sounds, which simply quiets sounds that are blocked by the world"};
 
 // Cvars declared in snd_main.h (shared with other snd_*.c files)
-cvar_t _snd_mixahead = {CVAR_SAVE, "_snd_mixahead", "0.1", "how much sound to mix ahead of time"};
-cvar_t snd_streaming = { CVAR_SAVE, "snd_streaming", "1", "enables keeping compressed ogg sound files compressed, decompressing them only as needed, otherwise they will be decompressed completely at load (may use a lot of memory)"};
+cvar_t _snd_mixahead = {CVAR_SAVE, "_snd_mixahead", "0.15", "how much sound to mix ahead of time"};
 cvar_t snd_swapstereo = {CVAR_SAVE, "snd_swapstereo", "0", "swaps left/right speakers for old ISA soundblaster cards"};
 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)"};
@@ -809,6 +810,7 @@ void S_Init(void)
        Cvar_RegisterVariable(&snd_spatialization_max);
        Cvar_RegisterVariable(&snd_spatialization_power);
        Cvar_RegisterVariable(&snd_spatialization_control);
+       Cvar_RegisterVariable(&snd_spatialization_occlusion);
 
        Cvar_RegisterVariable(&snd_speed);
        Cvar_RegisterVariable(&snd_width);
@@ -842,7 +844,6 @@ void S_Init(void)
        Cvar_RegisterVariable(&nosound);
        Cvar_RegisterVariable(&snd_precache);
        Cvar_RegisterVariable(&snd_initialized);
-       Cvar_RegisterVariable(&snd_streaming);
        Cvar_RegisterVariable(&ambient_level);
        Cvar_RegisterVariable(&ambient_fade);
        Cvar_RegisterVariable(&snd_noextraupdate);
@@ -921,6 +922,7 @@ void S_UnloadAllSounds_f (void)
 S_FindName
 ==================
 */
+sfx_t changevolume_sfx = {""};
 sfx_t *S_FindName (const char *name)
 {
        sfx_t *sfx;
@@ -928,6 +930,9 @@ sfx_t *S_FindName (const char *name)
        if (!snd_initialized.integer)
                return NULL;
 
+       if(!strcmp(name, changevolume_sfx.name))
+               return &changevolume_sfx;
+
        if (strlen (name) >= sizeof (sfx->name))
        {
                Con_Printf ("S_FindName: sound name too long (%s)\n", name);
@@ -1100,7 +1105,7 @@ S_IsSoundPrecached
 */
 qboolean S_IsSoundPrecached (const sfx_t *sfx)
 {
-       return (sfx != NULL && sfx->fetcher != NULL);
+       return (sfx != NULL && sfx->fetcher != NULL) || (sfx == &changevolume_sfx);
 }
 
 /*
@@ -1237,18 +1242,20 @@ void SND_Spatialize(channel_t *ch, qboolean isstatic)
        // update sound origin if we know about the entity
        if (ch->entnum > 0 && cls.state == ca_connected && cl_gameplayfix_soundsmovewithentities.integer)
        {
-               if (ch->entnum >= 32768)
+               if (ch->entnum >= MAX_EDICTS)
                {
                        //Con_Printf("-- entnum %i origin %f %f %f neworigin %f %f %f\n", 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 (ch->entnum > 32768)
+                       if (ch->entnum > MAX_EDICTS)
                                if (!CL_VM_GetEntitySoundOrigin(ch->entnum, ch->origin))
-                                       ch->entnum = 32768; // entity was removed, disown sound
+                                       ch->entnum = MAX_EDICTS; // entity was removed, disown sound
                }
                else if (cl.entities[ch->entnum].state_current.active)
                {
+                       dp_model_t *model;
                        //Con_Printf("-- entnum %i origin %f %f %f neworigin %f %f %f\n", 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.entities[ch->entnum].state_current.modelindex && cl.model_precache[cl.entities[ch->entnum].state_current.modelindex] && cl.model_precache[cl.entities[ch->entnum].state_current.modelindex]->soundfromcenter)
+                       model = CL_GetModelByIndex(cl.entities[ch->entnum].state_current.modelindex);
+                       if (model && model->soundfromcenter)
                                VectorMAM(0.5f, cl.entities[ch->entnum].render.mins, 0.5f, cl.entities[ch->entnum].render.maxs, ch->origin);
                        else
                                Matrix4x4_OriginFromMatrix(&cl.entities[ch->entnum].render.matrix, ch->origin);
@@ -1262,7 +1269,7 @@ void SND_Spatialize(channel_t *ch, qboolean isstatic)
                mastervol *= snd_staticvolume.value;
        else if(!(ch->flags & CHANNELFLAG_FULLVOLUME)) // same as SND_PaintChannel uses
        {
-               if(ch->entnum >= 32768)
+               if(ch->entnum >= MAX_EDICTS)
                {
                        switch(ch->entchannel)
                        {
@@ -1357,7 +1364,8 @@ void SND_Spatialize(channel_t *ch, qboolean isstatic)
                                                VectorScale(source_vec, f, source_vec);
                                                break;
                                        case SPATIAL_POW:
-                                               f = spatialmin + spatialdiff * bound(0, (pow(dist, spatialpower) - spatialoffset) * spatialfactor, 1);
+                                               f = (pow(dist, spatialpower) - spatialoffset) * spatialfactor;
+                                               f = spatialmin + spatialdiff * bound(0, f, 1);
                                                VectorScale(source_vec, f, source_vec);
                                                break;
                                        case SPATIAL_THRESH:
@@ -1370,6 +1378,17 @@ 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);
+
+                               if (snd_spatialization_occlusion.integer)
+                               {
+                                       if (cl.worldmodel
+                                       && cl.worldmodel->brush.TraceLineOfSight
+                                       && !cl.worldmodel->brush.TraceLineOfSight(cl.worldmodel, listener_origin, ch->origin))
+                                       {
+                                               vol *= 0.5f;
+                                       }
+                               }
+
                                ch->listener_volume[i] = (int)bound(0, vol, 255);
                        }
                }
@@ -1431,12 +1450,30 @@ void S_PlaySfxOnChannel (sfx_t *sfx, channel_t *target_chan, unsigned int flags,
 
 int S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
 {
-       channel_t *target_chan, *check;
+       channel_t *target_chan, *check, *ch;
        int             ch_idx;
 
        if (snd_renderbuffer == NULL || sfx == NULL || nosound.integer)
                return -1;
 
+       if(sfx == &changevolume_sfx)
+       {
+               if(entchannel == 0)
+                       return -1;
+               for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++)
+               {
+                       ch = &channels[ch_idx];
+                       if (ch->entnum == entnum && (ch->entchannel == entchannel || entchannel == -1) )
+                       {
+                               S_SetChannelVolume(ch_idx, fvol);
+                               ch->dist_mult = attenuation / snd_soundradius.value;
+                               SND_Spatialize(ch, false);
+                               return ch_idx;
+                       }
+               }
+               return -1;
+       }
+
        if (sfx->fetcher == NULL)
                return -1;
 
@@ -1476,17 +1513,18 @@ void S_StopChannel (unsigned int channel_ind, qboolean lockmutex)
        if (channel_ind >= total_channels)
                return;
 
+       // we have to lock an audio mutex to prevent crashes if an audio mixer
+       // thread is currently mixing this channel
+       // the SndSys_LockRenderBuffer function uses such a mutex in
+       // threaded sound backends
+       if (lockmutex && !simsound)
+               SndSys_LockRenderBuffer();
+       
        ch = &channels[channel_ind];
        if (ch->sfx != NULL)
        {
                sfx_t *sfx = ch->sfx;
 
-               // we have to lock an audio mutex to prevent crashes if an audio mixer
-               // thread is currently mixing this channel
-               // the SndSys_LockRenderBuffer function uses such a mutex in
-               // threaded sound backends
-               if (lockmutex)
-                       SndSys_LockRenderBuffer();
                if (sfx->fetcher != NULL)
                {
                        snd_fetcher_endsb_t fetcher_endsb = sfx->fetcher->endsb;
@@ -1499,9 +1537,9 @@ void S_StopChannel (unsigned int channel_ind, qboolean lockmutex)
 
                ch->fetcher_data = NULL;
                ch->sfx = NULL;
-               if (lockmutex)
-                       SndSys_UnlockRenderBuffer();
        }
+       if (lockmutex && !simsound)
+               SndSys_UnlockRenderBuffer();
 }
 
 
@@ -1548,18 +1586,18 @@ void S_StopAllSounds (void)
        // stop CD audio because it may be using a faketrack
        CDAudio_Stop();
 
-       for (i = 0; i < total_channels; i++)
-               S_StopChannel (i, true);
-
-       total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;   // no statics
-       memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
-
-       // Mute the contents of the submittion buffer
        if (simsound || SndSys_LockRenderBuffer ())
        {
                int clear;
                size_t memsize;
 
+               for (i = 0; i < total_channels; i++)
+                       S_StopChannel (i, false);
+
+               total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;   // no statics
+               memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
+
+               // Mute the contents of the submittion buffer
                clear = (snd_renderbuffer->format.width == 1) ? 0x80 : 0;
                memsize = snd_renderbuffer->maxframes * snd_renderbuffer->format.width * snd_renderbuffer->format.channels;
                memset(snd_renderbuffer->ring, clear, memsize);