]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - snd_main.c
Add GL_ExtensionSupported in vid_null.c because vid_shared.c relies on
[xonotic/darkplaces.git] / snd_main.c
index e76bc8047e78207f5d91e01c787b072fa23fc1a1..62807eb0983a79cb7529913aea7aaf29b46b5599 100644 (file)
@@ -23,9 +23,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #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
@@ -49,24 +51,24 @@ static const speakerlayout_t snd_speakerlayouts[] =
        {
                "surround71", 8,
                {
-                       {0, 45, 0.2, 0.2, 0.5}, // front left
-                       {1, 315, 0.2, 0.2, 0.5}, // front right
-                       {2, 135, 0.2, 0.2, 0.5}, // rear left
-                       {3, 225, 0.2, 0.2, 0.5}, // rear right
-                       {4, 0, 0.2, 0.2, 0.5}, // front center
+                       {0, 45, 0.2f, 0.2f, 0.5f}, // front left
+                       {1, 315, 0.2f, 0.2f, 0.5f}, // front right
+                       {2, 135, 0.2f, 0.2f, 0.5f}, // rear left
+                       {3, 225, 0.2f, 0.2f, 0.5f}, // rear right
+                       {4, 0, 0.2f, 0.2f, 0.5f}, // front center
                        {5, 0, 0, 0, 0}, // lfe (we don't have any good lfe sound sources and it would take some filtering work to generate them (and they'd probably still be wrong), so...  no lfe)
-                       {6, 90, 0.2, 0.2, 0.5}, // side left
-                       {7, 180, 0.2, 0.2, 0.5}, // side right
+                       {6, 90, 0.2f, 0.2f, 0.5f}, // side left
+                       {7, 180, 0.2f, 0.2f, 0.5f}, // side right
                }
        },
        {
                "surround51", 6,
                {
-                       {0, 45, 0.2, 0.2, 0.5}, // front left
-                       {1, 315, 0.2, 0.2, 0.5}, // front right
-                       {2, 135, 0.2, 0.2, 0.5}, // rear left
-                       {3, 225, 0.2, 0.2, 0.5}, // rear right
-                       {4, 0, 0.2, 0.2, 0.5}, // front center
+                       {0, 45, 0.2f, 0.2f, 0.5f}, // front left
+                       {1, 315, 0.2f, 0.2f, 0.5f}, // front right
+                       {2, 135, 0.2f, 0.2f, 0.5f}, // rear left
+                       {3, 225, 0.2f, 0.2f, 0.5f}, // rear right
+                       {4, 0, 0.2f, 0.2f, 0.5f}, // front center
                        {5, 0, 0, 0, 0}, // lfe (we don't have any good lfe sound sources and it would take some filtering work to generate them (and they'd probably still be wrong), so...  no lfe)
                        {0, 0, 0, 0, 0},
                        {0, 0, 0, 0, 0},
@@ -77,10 +79,10 @@ static const speakerlayout_t snd_speakerlayouts[] =
                // channel of its own
                "surround40", 4,
                {
-                       {0, 45, 0.3, 0.3, 0.8}, // front left
-                       {1, 315, 0.3, 0.3, 0.8}, // front right
-                       {2, 135, 0.3, 0.3, 0.8}, // rear left
-                       {3, 225, 0.3, 0.3, 0.8}, // rear right
+                       {0, 45, 0.3f, 0.3f, 0.8f}, // front left
+                       {1, 315, 0.3f, 0.3f, 0.8f}, // front right
+                       {2, 135, 0.3f, 0.3f, 0.8f}, // rear left
+                       {3, 225, 0.3f, 0.3f, 0.8f}, // rear right
                        {0, 0, 0, 0, 0},
                        {0, 0, 0, 0, 0},
                        {0, 0, 0, 0, 0},
@@ -92,8 +94,8 @@ static const speakerlayout_t snd_speakerlayouts[] =
                // channel of its own
                "stereo", 2,
                {
-                       {0, 90, 0.5, 0.5, 1}, // side left
-                       {1, 270, 0.5, 0.5, 1}, // side right
+                       {0, 90, 0.5f, 0.5f, 1}, // side left
+                       {1, 270, 0.5f, 0.5f, 1}, // side right
                        {0, 0, 0, 0, 0},
                        {0, 0, 0, 0, 0},
                        {0, 0, 0, 0, 0},
@@ -325,7 +327,7 @@ static void S_SoundList_f (void)
                {
                        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' : ' ',
@@ -344,7 +346,7 @@ static void S_SoundList_f (void)
 }
 
 
-void S_SoundInfo_f(void)
+static void S_SoundInfo_f(void)
 {
        if (snd_renderbuffer == NULL)
        {
@@ -776,7 +778,7 @@ void S_Shutdown(void)
        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.
@@ -914,7 +916,6 @@ void S_Init(void)
        memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
 
        OGG_OpenLibrary ();
-       ModPlug_OpenLibrary ();
 }
 
 
@@ -928,7 +929,6 @@ Shutdown and free all resources
 void S_Terminate (void)
 {
        S_Shutdown ();
-       ModPlug_CloseLibrary ();
        OGG_CloseLibrary ();
 
        // Free all SFXs
@@ -1220,7 +1220,7 @@ SND_PickChannel
 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;
@@ -1261,7 +1261,7 @@ channel_t *SND_PickChannel(int entnum, int entchannel)
                }
 
                // 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
@@ -1293,13 +1293,14 @@ Spatializes a channel
 =================
 */
 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)
@@ -1322,6 +1323,13 @@ void SND_Spatialize_WithSfx(channel_t *ch, qboolean isstatic, sfx_t *sfx)
                        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;
@@ -1406,7 +1414,7 @@ void SND_Spatialize_WithSfx(channel_t *ch, qboolean isstatic, sfx_t *sfx)
                        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;
                }
        }
 
@@ -1450,7 +1458,7 @@ void SND_Spatialize_WithSfx(channel_t *ch, qboolean isstatic, sfx_t *sfx)
 
        // 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)
@@ -1474,7 +1482,7 @@ void SND_Spatialize_WithSfx(channel_t *ch, qboolean isstatic, sfx_t *sfx)
                f = dist * ch->distfade;
 
                f =
-                       ((snd_attenuation_exponent.value == 0) ? 1.0 : pow(1.0 - min(1.0, f), snd_attenuation_exponent.value))
+                       ((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));
 
@@ -1485,7 +1493,7 @@ void SND_Spatialize_WithSfx(channel_t *ch, qboolean isstatic, sfx_t *sfx)
                        if (snd_spatialization_occlusion.integer)
                        {
                                if(snd_spatialization_occlusion.integer & 1)
-                                       if(listener_pvs)
+                                       if(listener_pvs && cl.worldmodel)
                                        {
                                                int cluster = cl.worldmodel->brush.PointInLeaf(cl.worldmodel, ch->origin)->clusterindex;
                                                if(cluster >= 0 && cluster < 8 * listener_pvsbytes && !CHECKPVSBIT(listener_pvs, cluster))
@@ -1494,7 +1502,7 @@ void SND_Spatialize_WithSfx(channel_t *ch, qboolean isstatic, sfx_t *sfx)
 
                                if(snd_spatialization_occlusion.integer & 2)
                                        if(!occluded)
-                                               if(cl.worldmodel && cl.worldmodel->brush.TraceLineOfSight && !cl.worldmodel->brush.TraceLineOfSight(cl.worldmodel, listener_origin, ch->origin))
+                                               if(cl.worldmodel && cl.worldmodel->brush.TraceLineOfSight && !cl.worldmodel->brush.TraceLineOfSight(cl.worldmodel, listener_origin, ch->origin, ch->origin, ch->origin))
                                                        occluded = true;
                        }
                        if(occluded)
@@ -1597,7 +1605,7 @@ void SND_Spatialize_WithSfx(channel_t *ch, qboolean isstatic, sfx_t *sfx)
                                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);
@@ -1608,7 +1616,7 @@ void SND_Spatialize(channel_t *ch, qboolean isstatic)
 // 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)
        {
@@ -1668,7 +1676,7 @@ void S_PlaySfxOnChannel (sfx_t *sfx, channel_t *target_chan, unsigned int flags,
 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;
@@ -1684,6 +1692,9 @@ int S_StartSound_StartPosition_Flags (int entnum, int entchannel, sfx_t *sfx, ve
                        {
                                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;
@@ -1756,17 +1767,17 @@ void S_StopChannel (unsigned int channel_ind, qboolean lockmutex, qboolean frees
        
        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);
 }
 
 
@@ -1801,7 +1812,6 @@ void S_StopSound(int entnum, int entchannel)
                }
 }
 
-extern void CDAudio_Stop(void);
 void S_StopAllSounds (void)
 {
        unsigned int i;
@@ -1810,8 +1820,10 @@ void S_StopAllSounds (void)
        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 ())
        {
@@ -1923,7 +1935,7 @@ void S_StaticSound (sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
 S_UpdateAmbientSounds
 ===================
 */
-void S_UpdateAmbientSounds (void)
+static void S_UpdateAmbientSounds (void)
 {
        int                     i;
        float           vol;