]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - snd_dma.c
upgraded network protocol to DP5, now sends precise entity angles (except for EF_LOWP...
[xonotic/darkplaces.git] / snd_dma.c
index 4a98b3a59ca0c29d66f0d2717e74d8a3f0ccffae..e3b4a09b439aab65cbba8b6072660f897ffb73db 100644 (file)
--- a/snd_dma.c
+++ b/snd_dma.c
@@ -22,9 +22,16 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "quakedef.h"
 
 #ifdef _WIN32
-#include "winquake.h"
+#include <windows.h>
+#include <dsound.h>
+extern DWORD gSndBufSize;
+extern LPDIRECTSOUND pDS;
+extern LPDIRECTSOUNDBUFFER pDSBuf;
 #endif
 
+#include "ogg.h"
+
+
 void S_Play(void);
 void S_PlayVol(void);
 void S_Play2(void);
@@ -42,16 +49,16 @@ int total_channels;
 
 int snd_blocked = 0;
 static qboolean snd_ambient = 1;
-qboolean snd_initialized = false;
+cvar_t snd_initialized = { CVAR_READONLY, "snd_initialized", "0"};
 
 // pointer should go away
 volatile dma_t *shm = 0;
 volatile dma_t sn;
 
-vec3_t listener_origin;
-vec3_t listener_forward;
-vec3_t listener_right;
-vec3_t listener_up;
+vec3_t listener_vieworigin;
+vec3_t listener_viewforward;
+vec3_t listener_viewleft;
+vec3_t listener_viewup;
 vec_t sound_nominal_clip_dist=1000.0;
 mempool_t *snd_mempool;
 
@@ -144,7 +151,7 @@ void S_LoadSounds(void)
 
 void S_Startup(void)
 {
-       if (!snd_initialized)
+       if (!snd_initialized.integer)
                return;
 
        shm = &sn;
@@ -236,6 +243,7 @@ void S_Init(void)
 
        Cvar_RegisterVariable(&nosound);
        Cvar_RegisterVariable(&snd_precache);
+       Cvar_RegisterVariable(&snd_initialized);
        Cvar_RegisterVariable(&bgmbuffer);
        Cvar_RegisterVariable(&ambient_level);
        Cvar_RegisterVariable(&ambient_fade);
@@ -244,7 +252,7 @@ void S_Init(void)
        Cvar_RegisterVariable(&_snd_mixahead);
        Cvar_RegisterVariable(&snd_swapstereo); // LordHavoc: for people with backwards sound wiring
 
-       snd_initialized = true;
+       Cvar_SetValueQuick(&snd_initialized, true);
 
        known_sfx = Mem_Alloc(snd_mempool, MAX_SFX*sizeof(sfx_t));
        num_sfx = 0;
@@ -256,6 +264,8 @@ void S_Init(void)
 
        total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;   // no statics
        memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
+
+       OGG_OpenLibrary ();
 }
 
 
@@ -265,7 +275,7 @@ void S_Init(void)
 
 /*
 =========
-S_IsCached
+S_GetCached
 
 =========
 */
@@ -273,12 +283,12 @@ sfx_t *S_GetCached (const char *name)
 {
        int i;
 
-       if (!snd_initialized)
+       if (!snd_initialized.integer)
                return NULL;
-       
+
        if (!name)
                Host_Error("S_IsCached: NULL\n");
-       
+
        if (strlen(name) >= MAX_QPATH)
                Host_Error("Sound name too long: %s", name);
 
@@ -300,7 +310,7 @@ sfx_t *S_FindName (char *name)
        int i;
        sfx_t *sfx;
 
-       if (!snd_initialized)
+       if (!snd_initialized.integer)
                return NULL;
 
        if (!name)
@@ -332,9 +342,53 @@ S_TouchSound
 */
 void S_TouchSound (char *name)
 {
-       S_FindName(name);
+       sfx_t *sfx;
+
+       sfx = S_FindName (name);
+
+       // Set the "used" flag for this sound
+       if (sfx != NULL)
+               sfx->flags |= SFXFLAG_USED;
+}
+
+
+/*
+==================
+S_ClearUsed
+
+Reset the "used" flag of all precached sounds
+==================
+*/
+void S_ClearUsed (void)
+{
+       int i;
+
+       for (i = 0; i < num_sfx; i++)
+               known_sfx[i].flags &= ~SFXFLAG_USED;
+}
+
+
+/*
+==================
+S_PurgeUnused
+
+Free all precached sounds without the "used" flag
+==================
+*/
+void S_PurgeUnused (void)
+{
+       int i;
+       sfx_t *sfx;
+
+       for (i = 0; i < num_sfx; i++)
+       {
+               sfx = &known_sfx[i];
+               if (! (sfx->flags & SFXFLAG_USED))
+                       S_UnloadSound (sfx);
+       }
 }
 
+
 /*
 ==================
 S_PrecacheSound
@@ -345,7 +399,7 @@ sfx_t *S_PrecacheSound (char *name, int complain)
 {
        sfx_t *sfx;
 
-       if (!snd_initialized)
+       if (!snd_initialized.integer)
                return NULL;
 
        sfx = S_FindName(name);
@@ -432,15 +486,15 @@ void SND_Spatialize(channel_t *ch, int isstatic)
                }
 
                // calculate stereo seperation and distance attenuation
-               VectorSubtract(ch->origin, listener_origin, source_vec);
+               VectorSubtract(ch->origin, listener_vieworigin, source_vec);
                dist = VectorNormalizeLength(source_vec);
                // distance
                scale = ch->master_vol * (1.0 - (dist * ch->dist_mult));
                // panning
-               pan = scale * DotProduct(listener_right, source_vec);
+               pan = scale * DotProduct(listener_viewleft, source_vec);
                // calculate the volumes
-               ch->leftvol = (int) (scale - pan);
-               ch->rightvol = (int) (scale + pan);
+               ch->leftvol = (int) (scale + pan);
+               ch->rightvol = (int) (scale - pan);
        }
 
        // LordHavoc: allow adjusting volume of static sounds
@@ -468,7 +522,7 @@ void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float f
        int             ch_idx;
        int             skip;
 
-       if (!sound_started || !sfx || nosound.integer)
+       if (!sound_started || !sfx || !sfx->sfxcache || nosound.integer)
                return;
 
        vol = fvol*255;
@@ -487,8 +541,9 @@ void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float f
        target_chan->entchannel = entchannel;
        SND_Spatialize(target_chan, false);
 
-       if (!target_chan->leftvol && !target_chan->rightvol)
-               return;         // not audible at all
+       // LordHavoc: spawn the sound anyway because the player might teleport to it
+       //if (!target_chan->leftvol && !target_chan->rightvol)
+       //      return;         // not audible at all
 
 // new channel
        sc = S_LoadSound (sfx, true);
@@ -576,7 +631,7 @@ void S_ClearBuffer(void)
 
                reps = 0;
 
-               while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pData, &dwSize, NULL, NULL, 0)) != DS_OK)
+               while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, (LPVOID*)&pData, &dwSize, NULL, NULL, 0)) != DS_OK)
                {
                        if (hresult != DSERR_BUFFERLOST)
                        {
@@ -676,12 +731,13 @@ void S_UpdateAmbientSounds (void)
        if (!snd_ambient || ambient_level.value <= 0 || !cl.worldmodel || !cl.worldmodel->brush.AmbientSoundLevelsForPoint)
                return;
 
-       cl.worldmodel->brush.AmbientSoundLevelsForPoint(cl.worldmodel, listener_origin, ambientlevels, sizeof(ambientlevels));
+       cl.worldmodel->brush.AmbientSoundLevelsForPoint(cl.worldmodel, listener_vieworigin, ambientlevels, sizeof(ambientlevels));
 
 // calc ambient sound levels
        for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
        {
-               if (ambient_sfx[ambient_channel] && ambient_sfx[ambient_channel]->silentlymissing)
+               if (ambient_sfx[ambient_channel] &&
+                       (ambient_sfx[ambient_channel]->flags & SFXFLAG_SILENTLYMISSING))
                        continue;
                chan = &channels[ambient_channel];
                chan->forceloop = true;
@@ -717,20 +773,20 @@ S_Update
 Called once each time through the main loop
 ============
 */
-void S_Update(vec3_t origin, vec3_t forward, vec3_t right, vec3_t up)
+void S_Update(vec3_t origin, vec3_t forward, vec3_t left, vec3_t up)
 {
        int                     i, j;
        int                     total;
        channel_t       *ch;
        channel_t       *combine;
 
-       if (!snd_initialized || (snd_blocked > 0))
+       if (!snd_initialized.integer || (snd_blocked > 0))
                return;
 
-       VectorCopy(origin, listener_origin);
-       VectorCopy(forward, listener_forward);
-       VectorCopy(right, listener_right);
-       VectorCopy(up, listener_up);
+       VectorCopy(origin, listener_vieworigin);
+       VectorCopy(forward, listener_viewforward);
+       VectorCopy(left, listener_viewleft);
+       VectorCopy(up, listener_viewup);
 
 // update general area ambient sound sources
        S_UpdateAmbientSounds ();
@@ -810,12 +866,8 @@ void GetSoundtime(void)
 
 // it is possible to miscount buffers if it has wrapped twice between
 // calls to S_Update.  Oh well.
-#ifdef __sun__
-       soundtime = SNDDMA_GetSamples();
-#else
        samplepos = SNDDMA_GetDMAPos();
 
-
        if (samplepos < oldsamplepos)
        {
                buffers++;                                      // buffer wrapped
@@ -830,7 +882,6 @@ void GetSoundtime(void)
        oldsamplepos = samplepos;
 
        soundtime = buffers*fullsamples + samplepos/shm->channels;
-#endif
 }
 
 void IN_Accumulate (void);
@@ -875,7 +926,7 @@ void S_Update_(void)
 
                if (pDSBuf)
                {
-                       if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DD_OK)
+                       if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DS_OK)
                                Con_Printf ("Couldn't get sound buffer status\n");
 
                        if (dwStatus & DSBSTATUS_BUFFERLOST)
@@ -900,7 +951,7 @@ console functions
 ===============================================================================
 */
 
-void S_Play(void)
+static void S_Play_Common(float fvol, float attenuation)
 {
        int     i;
        char name[256];
@@ -910,62 +961,37 @@ void S_Play(void)
        while (i<Cmd_Argc())
        {
                if (!strrchr(Cmd_Argv(i), '.'))
+                       snprintf(name, sizeof(name), "%s.wav", Cmd_Argv(i));
+               else
+                       strlcpy(name, Cmd_Argv(i), sizeof(name));
+               sfx = S_PrecacheSound(name, true);
+
+               // If we need to get the volume from the command line
+               if (fvol == -1.0f)
                {
-                       strcpy(name, Cmd_Argv(i));
-                       strcat(name, ".wav");
+                       fvol = atof(Cmd_Argv(i+1));
+                       i += 2;
                }
                else
-                       strcpy(name, Cmd_Argv(i));
-               sfx = S_PrecacheSound(name, true);
-               S_StartSound(-1, 0, sfx, listener_origin, 1.0, 1.0);
-               i++;
+                       i++;
+
+               S_StartSound(-1, 0, sfx, listener_vieworigin, fvol, attenuation);
        }
 }
 
-void S_Play2(void)
+void S_Play(void)
 {
-       int     i;
-       char name[256];
-       sfx_t   *sfx;
+       S_Play_Common (1.0f, 1.0f);
+}
 
-       i = 1;
-       while (i<Cmd_Argc())
-       {
-               if (!strrchr(Cmd_Argv(i), '.'))
-               {
-                       strcpy(name, Cmd_Argv(i));
-                       strcat(name, ".wav");
-               }
-               else
-                       strcpy(name, Cmd_Argv(i));
-               sfx = S_PrecacheSound(name, true);
-               S_StartSound(-1, 0, sfx, listener_origin, 1.0, 0.0);
-               i++;
-       }
+void S_Play2(void)
+{
+       S_Play_Common (1.0f, 0.0f);
 }
 
 void S_PlayVol(void)
 {
-       int i;
-       float vol;
-       char name[256];
-       sfx_t   *sfx;
-
-       i = 1;
-       while (i<Cmd_Argc())
-       {
-               if (!strrchr(Cmd_Argv(i), '.'))
-               {
-                       strcpy(name, Cmd_Argv(i));
-                       strcat(name, ".wav");
-               }
-               else
-                       strcpy(name, Cmd_Argv(i));
-               sfx = S_PrecacheSound(name, true);
-               vol = atof(Cmd_Argv(i+1));
-               S_StartSound(-1, 0, sfx, listener_origin, vol, 1.0);
-               i+=2;
-       }
+       S_Play_Common (-1.0f, 0.0f);
 }
 
 void S_SoundList(void)
@@ -994,7 +1020,7 @@ void S_LocalSound (char *sound)
 {
        sfx_t   *sfx;
 
-       if (!snd_initialized || nosound.integer)
+       if (!snd_initialized.integer || nosound.integer)
                return;
 
        sfx = S_PrecacheSound (sound, true);
@@ -1007,21 +1033,6 @@ void S_LocalSound (char *sound)
 }
 
 
-void S_ClearPrecache (void)
-{
-}
-
-
-void S_BeginPrecaching (void)
-{
-}
-
-
-void S_EndPrecaching (void)
-{
-}
-
-
 #define RAWSAMPLESBUFFER 32768
 short s_rawsamplesbuffer[RAWSAMPLESBUFFER * 2];
 int s_rawsamplesbuffer_start;